[Lldb-commits] [lldb] [lldb] Support CommandInterpreter print callbacks (PR #125006)

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Thu Jan 30 05:17:45 PST 2025


================
@@ -743,3 +743,41 @@ void SBCommand::SetFlags(uint32_t flags) {
   if (IsValid())
     m_opaque_sp->GetFlags().Set(flags);
 }
+
+namespace lldb_private {
+struct CommandCallbackData {
+  SBCommandPrintCallback callback;
+  void *callback_baton;
+};
+
+class CommandPrintCallbackBaton
+    : public lldb_private::TypedBaton<CommandCallbackData> {
+public:
+  CommandPrintCallbackBaton(SBCommandPrintCallback callback, void *baton)
+      : TypedBaton(std::make_unique<CommandCallbackData>()) {
+    getItem()->callback = callback;
+    getItem()->callback_baton = baton;
+  }
+
+  static lldb::CommandReturnObjectCallbackResult
+  PrivateCallback(lldb_private::CommandReturnObject &result, void *baton) {
+    if (baton) {
+      CommandCallbackData *data = (CommandCallbackData *)baton;
+      SBCommandReturnObject sb_result(result);
+      return data->callback(sb_result, data->callback_baton);
+    }
+    return eCommandReturnObjectPrintCallbackSkipped;
+  }
+};
+} // namespace lldb_private
+
+void SBCommandInterpreter::SetPrintCallback(
+    lldb::SBCommandPrintCallback callback, void *baton) {
+  LLDB_INSTRUMENT_VA(this, callback, baton);
+
+  BatonSP baton_sp =
+      std::make_shared<CommandPrintCallbackBaton>(callback, baton);
----------------
labath wrote:

I know this is here as a form of dependency inversion, and that we have this Baton thingy already, but it all fields unnecessarily elaborate. Couldn't the private callback type be a `std::function<CommandReturnObjectCallbackResult(CommandReturnObject&)>`, and just wrap things in a lambda?

https://github.com/llvm/llvm-project/pull/125006


More information about the lldb-commits mailing list