diff --git a/lldb/bindings/interface/SBDebugger.i b/lldb/bindings/interface/SBDebugger.i --- a/lldb/bindings/interface/SBDebugger.i +++ b/lldb/bindings/interface/SBDebugger.i @@ -410,6 +410,9 @@ static bool StateIsStoppedState (lldb::StateType state); + void + SetDestroyCallback(lldb::SBDebuggerDestroyCallback destroy_callback, void *baton); + bool EnableLog (const char *channel, const char ** types); diff --git a/lldb/bindings/python/python-typemaps.swig b/lldb/bindings/python/python-typemaps.swig --- a/lldb/bindings/python/python-typemaps.swig +++ b/lldb/bindings/python/python-typemaps.swig @@ -382,6 +382,29 @@ $1 = $1 || PyCallable_Check(reinterpret_cast($input)); } +// For lldb::SBDebuggerDestroyCallback +%typemap(in) (lldb::SBDebuggerDestroyCallback destroy_callback, void *baton) { + if (!($input == Py_None || + PyCallable_Check(reinterpret_cast($input)))) { + PyErr_SetString(PyExc_TypeError, "Need a callable object or None!"); + SWIG_fail; + } + + // FIXME (filcab): We can't currently check if our callback is already + // LLDBSwigPythonCallPythonSBDebuggerTerminateCallback (to DECREF the previous + // baton) nor can we just remove all traces of a callback, if we want to + // revert to a file logging mechanism. + + // Don't lose the callback reference + Py_INCREF($input); + $1 = LLDBSwigPythonCallPythonSBDebuggerTerminateCallback; + $2 = $input; +} + +%typemap(typecheck) (lldb::SBDebuggerDestroyCallback destroy_callback, void *baton) { + $1 = $input == Py_None; + $1 = $1 || PyCallable_Check(reinterpret_cast($input)); +} %typemap(in) lldb::FileSP { PythonFile py_file(PyRefType::Borrowed, $input); diff --git a/lldb/bindings/python/python-wrapper.swig b/lldb/bindings/python/python-wrapper.swig --- a/lldb/bindings/python/python-wrapper.swig +++ b/lldb/bindings/python/python-wrapper.swig @@ -1038,4 +1038,16 @@ SWIG_PYTHON_THREAD_END_BLOCK; } } + +// For DebuggerTerminateCallback functions +static void LLDBSwigPythonCallPythonSBDebuggerTerminateCallback(lldb::user_id_t debugger_id, + void *baton) { + if (baton != Py_None) { + SWIG_PYTHON_THREAD_BEGIN_BLOCK; + PyObject *result = PyObject_CallFunction( + reinterpret_cast(baton), const_cast("l"), debugger_id); + Py_XDECREF(result); + SWIG_PYTHON_THREAD_END_BLOCK; + } +} %} diff --git a/lldb/include/lldb/API/SBDebugger.h b/lldb/include/lldb/API/SBDebugger.h --- a/lldb/include/lldb/API/SBDebugger.h +++ b/lldb/include/lldb/API/SBDebugger.h @@ -291,6 +291,9 @@ void SetLoggingCallback(lldb::LogOutputCallback log_callback, void *baton); + void SetDestroyCallback(lldb::SBDebuggerDestroyCallback destroy_callback, + void *baton); + // DEPRECATED void DispatchInput(void *baton, const void *data, size_t data_len); diff --git a/lldb/include/lldb/API/SBDefines.h b/lldb/include/lldb/API/SBDefines.h --- a/lldb/include/lldb/API/SBDefines.h +++ b/lldb/include/lldb/API/SBDefines.h @@ -110,6 +110,10 @@ typedef bool (*SBBreakpointHitCallback)(void *baton, SBProcess &process, SBThread &thread, lldb::SBBreakpointLocation &location); + +typedef void (*SBDebuggerDestroyCallback)(lldb::user_id_t debugger_id, + void *baton); + typedef void *ScriptedObject; } diff --git a/lldb/include/lldb/Core/Debugger.h b/lldb/include/lldb/Core/Debugger.h --- a/lldb/include/lldb/Core/Debugger.h +++ b/lldb/include/lldb/Core/Debugger.h @@ -450,6 +450,10 @@ static void ReportSymbolChange(const ModuleSpec &module_spec); + void + SetDestroyCallback(lldb_private::DebuggerDestroyCallback destroy_callback, + void *baton); + protected: friend class CommandInterpreter; friend class REPL; @@ -493,6 +497,8 @@ std::optional debugger_id, std::once_flag *once); + static void HandleDestroyCallback(const lldb::DebuggerSP &debugger_sp); + void PrintProgress(const ProgressEventData &data); bool StartEventHandlerThread(); @@ -590,6 +596,9 @@ llvm::once_flag m_clear_once; lldb::TargetSP m_dummy_target_sp; + lldb_private::DebuggerDestroyCallback m_destroy_callback = nullptr; + void *m_destroy_callback_baton = nullptr; + // Events for m_sync_broadcaster enum { eBroadcastBitEventThreadIsListening = (1 << 0), diff --git a/lldb/include/lldb/lldb-private-types.h b/lldb/include/lldb/lldb-private-types.h --- a/lldb/include/lldb/lldb-private-types.h +++ b/lldb/include/lldb/lldb-private-types.h @@ -116,6 +116,8 @@ using ValueObjectProviderTy = std::function; +typedef void (*DebuggerDestroyCallback)(lldb::user_id_t debugger_id, + void *baton); } // namespace lldb_private #endif // #if defined(__cplusplus) diff --git a/lldb/source/API/SBDebugger.cpp b/lldb/source/API/SBDebugger.cpp --- a/lldb/source/API/SBDebugger.cpp +++ b/lldb/source/API/SBDebugger.cpp @@ -1672,6 +1672,15 @@ } } +void SBDebugger::SetDestroyCallback( + lldb::SBDebuggerDestroyCallback destroy_callback, void *baton) { + LLDB_INSTRUMENT_VA(this, destroy_callback, baton); + if (m_opaque_sp) { + return m_opaque_sp->SetDestroyCallback( + destroy_callback, baton); + } +} + SBTrace SBDebugger::LoadTraceFromFile(SBError &error, const SBFileSpec &trace_description_file) { diff --git a/lldb/source/Core/Debugger.cpp b/lldb/source/Core/Debugger.cpp --- a/lldb/source/Core/Debugger.cpp +++ b/lldb/source/Core/Debugger.cpp @@ -560,6 +560,12 @@ assert(g_debugger_list_ptr && "Debugger::Terminate called without a matching Debugger::Initialize!"); + if (g_debugger_list_ptr && g_debugger_list_mutex_ptr) { + std::lock_guard guard(*g_debugger_list_mutex_ptr); + for (const auto &debugger : *g_debugger_list_ptr) + HandleDestroyCallback(debugger); + } + if (g_thread_pool) { // The destructor will wait for all the threads to complete. delete g_thread_pool; @@ -679,10 +685,19 @@ return debugger_sp; } +void Debugger::HandleDestroyCallback(const DebuggerSP &debugger_sp) { + if (debugger_sp->m_destroy_callback) { + debugger_sp->m_destroy_callback(debugger_sp->GetID(), + debugger_sp->m_destroy_callback_baton); + debugger_sp->m_destroy_callback = nullptr; + } +} + void Debugger::Destroy(DebuggerSP &debugger_sp) { if (!debugger_sp) return; + HandleDestroyCallback(debugger_sp); CommandInterpreter &cmd_interpreter = debugger_sp->GetCommandInterpreter(); if (cmd_interpreter.GetSaveSessionOnQuit()) { @@ -1285,6 +1300,12 @@ std::make_shared(log_callback, baton); } +void Debugger::SetDestroyCallback( + lldb_private::DebuggerDestroyCallback destroy_callback, void *baton) { + m_destroy_callback = destroy_callback; + m_destroy_callback_baton = baton; +} + static void PrivateReportProgress(Debugger &debugger, uint64_t progress_id, const std::string &message, uint64_t completed, uint64_t total, diff --git a/lldb/test/API/python_api/debugger/TestDebuggerAPI.py b/lldb/test/API/python_api/debugger/TestDebuggerAPI.py --- a/lldb/test/API/python_api/debugger/TestDebuggerAPI.py +++ b/lldb/test/API/python_api/debugger/TestDebuggerAPI.py @@ -30,7 +30,7 @@ self.dbg.SetPrompt(None) self.dbg.SetCurrentPlatform(None) self.dbg.SetCurrentPlatformSDKRoot(None) - + fresh_dbg = lldb.SBDebugger() self.assertEquals(len(fresh_dbg), 0) @@ -146,3 +146,16 @@ self.assertEqual(platform2.GetName(), expected_platform) self.assertTrue(platform2.GetWorkingDirectory().endswith("bar"), platform2.GetWorkingDirectory()) + + def test_SetDestroyCallback(self): + destroy_dbg_id = None + def foo(dbg_id): + # Need nonlocal to modify closure variable. + nonlocal destroy_dbg_id + destroy_dbg_id = dbg_id + + self.dbg.SetDestroyCallback(foo) + + original_dbg_id = self.dbg.GetID() + self.dbg.Destroy(self.dbg) + self.assertEqual(destroy_dbg_id, original_dbg_id)