From ebc1bb277ccc583414b5695e10a708389a003b01 Mon Sep 17 00:00:00 2001 From: Caroline Tice Date: Wed, 30 Jun 2010 16:22:25 +0000 Subject: [PATCH] Add a unique ID to each debugger instance. Add functions to look up debugger by id Add global variable to lldb python module, to hold debugger id Modify embedded Python interpreter to update the global variable with the id of its current debugger. Modify the char ** typemap definition in lldb.swig to accept 'None' (for NULL) as a valid value. The point of all this is so that, when you drop into the embedded interpreter from the command interpreter (or when doing Python-based breakpoint commands), there is a way for the Python side to find/get the correct debugger instance ( by checking debugger_unique_id, then calling SBDebugger::FindDebuggerWithID on it). llvm-svn: 107287 --- lldb/include/lldb/API/SBDebugger.h | 3 +++ lldb/include/lldb/Core/Debugger.h | 9 +++++-- lldb/scripts/Python/append-debugger-id.py | 27 +++++++++++++++++++ lldb/scripts/Python/build-swig-Python.sh | 7 +++++ lldb/scripts/lldb.swig | 2 ++ lldb/source/API/SBDebugger.cpp | 10 ++++++- lldb/source/Core/Debugger.cpp | 21 +++++++++++++++ .../Interpreter/ScriptInterpreterPython.cpp | 4 +++ 8 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 lldb/scripts/Python/append-debugger-id.py diff --git a/lldb/include/lldb/API/SBDebugger.h b/lldb/include/lldb/API/SBDebugger.h index d55a77edfdfa..60f0a17324bf 100644 --- a/lldb/include/lldb/API/SBDebugger.h +++ b/lldb/include/lldb/API/SBDebugger.h @@ -129,6 +129,9 @@ public: void PushInputReader (lldb::SBInputReader &reader); + static SBDebugger + FindDebuggerWithID (int id); + private: // Use the static function: SBDebugger::Create(); diff --git a/lldb/include/lldb/Core/Debugger.h b/lldb/include/lldb/Core/Debugger.h index 503e250bee64..6ec23a10f49f 100644 --- a/lldb/include/lldb/Core/Debugger.h +++ b/lldb/include/lldb/Core/Debugger.h @@ -21,6 +21,7 @@ #include "lldb/Core/Listener.h" #include "lldb/Core/StreamFile.h" #include "lldb/Core/SourceManager.h" +#include "lldb/Core/UserID.h" #include "lldb/Target/ExecutionContext.h" #include "lldb/Target/TargetList.h" @@ -32,7 +33,8 @@ namespace lldb_private { /// /// Provides a global root objects for the debugger core. //---------------------------------------------------------------------- -class Debugger +class Debugger : + public UserID { public: @@ -139,6 +141,9 @@ public: void UpdateExecutionContext (ExecutionContext *override_context); + static lldb::DebuggerSP + FindDebuggerWithID (lldb::user_id_t id); + protected: static void @@ -165,7 +170,7 @@ protected: std::stack m_input_readers; std::string m_input_reader_data; - + private: // Use Debugger::CreateInstance() to get a shared pointer to a new diff --git a/lldb/scripts/Python/append-debugger-id.py b/lldb/scripts/Python/append-debugger-id.py new file mode 100644 index 000000000000..ab119a86c402 --- /dev/null +++ b/lldb/scripts/Python/append-debugger-id.py @@ -0,0 +1,27 @@ +# +# append-debugger-id.py +# +# This script adds a global variable, 'debugger_unique_id' to the lldb +# module (which was automatically generated via running swig), and +# initializes it to 0. +# + +import sys + +if len (sys.argv) != 2: + output_name = "./lldb.py" +else: + output_name = sys.argv[1] + "/lldb.py" + +# print "output_name is '" + output_name + "'" + +try: + f_out = open (output_name, 'a') +except IOError: + print "Error: Unable to open file for appending: " + output_name +else: + f_out.write ("debugger_unique_id = 0\n"); + try: + f_out.close() + except IOError: + print "Error occurred while close file." diff --git a/lldb/scripts/Python/build-swig-Python.sh b/lldb/scripts/Python/build-swig-Python.sh index 69c40a76b12e..d2ed2098a5b9 100755 --- a/lldb/scripts/Python/build-swig-Python.sh +++ b/lldb/scripts/Python/build-swig-Python.sh @@ -127,3 +127,10 @@ fi swig -c++ -shadow -python -I"${SRC_ROOT}/include" -I./. -outdir "${CONFIG_BUILD_DIR}" -o "${swig_output_file}" "${swig_input_file}" +# Append global variable to lldb Python module. + +current_dir=`pwd` +if [ -f "${current_dir}/append-debugger-id.py" ] +then + python ${current_dir}/append-debugger-id.py ${CONFIG_BUILD_DIR} +fi diff --git a/lldb/scripts/lldb.swig b/lldb/scripts/lldb.swig index 3d7d35a4ad50..4178c83a5f7d 100644 --- a/lldb/scripts/lldb.swig +++ b/lldb/scripts/lldb.swig @@ -43,6 +43,8 @@ } } $1[i] = 0; + } else if ($input == Py_None) { + $1 = NULL; } else { PyErr_SetString(PyExc_TypeError,"not a list"); return NULL; diff --git a/lldb/source/API/SBDebugger.cpp b/lldb/source/API/SBDebugger.cpp index d8d0dbbacb7d..84856ef988b4 100644 --- a/lldb/source/API/SBDebugger.cpp +++ b/lldb/source/API/SBDebugger.cpp @@ -51,7 +51,6 @@ SBDebugger::Create() return debugger; } - SBDebugger::SBDebugger () : m_opaque_sp () { @@ -549,3 +548,12 @@ SBDebugger::ref () const } +SBDebugger +SBDebugger::FindDebuggerWithID (int id) +{ + SBDebugger sb_debugger; + lldb::DebuggerSP debugger_sp = Debugger::FindDebuggerWithID (id); + if (debugger_sp) + sb_debugger.reset (debugger_sp); + return sb_debugger; +} diff --git a/lldb/source/Core/Debugger.cpp b/lldb/source/Core/Debugger.cpp index 0b7676ca7327..32b1bf3508a0 100644 --- a/lldb/source/Core/Debugger.cpp +++ b/lldb/source/Core/Debugger.cpp @@ -24,6 +24,8 @@ using namespace lldb_private; static uint32_t g_shared_debugger_refcount = 0; +static lldb::user_id_t g_unique_id = 1; + void Debugger::Initialize () { @@ -115,6 +117,7 @@ Debugger::FindTargetWithProcessID (lldb::pid_t pid) Debugger::Debugger () : + UserID (g_unique_id++), m_input_comm("debugger.input"), m_input_file (), m_output_file (), @@ -491,3 +494,21 @@ Debugger::UpdateExecutionContext (ExecutionContext *override_context) } } +DebuggerSP +Debugger::FindDebuggerWithID (lldb::user_id_t id) +{ + lldb::DebuggerSP debugger_sp; + + Mutex::Locker locker (GetDebuggerListMutex ()); + DebuggerList &debugger_list = GetDebuggerList(); + DebuggerList::iterator pos, end = debugger_list.end(); + for (pos = debugger_list.begin(); pos != end; ++pos) + { + if ((*pos).get()->GetID() == id) + { + debugger_sp = *pos; + break; + } + } + return debugger_sp; +} diff --git a/lldb/source/Interpreter/ScriptInterpreterPython.cpp b/lldb/source/Interpreter/ScriptInterpreterPython.cpp index 5fb0130f354a..d7a33639a32a 100644 --- a/lldb/source/Interpreter/ScriptInterpreterPython.cpp +++ b/lldb/source/Interpreter/ScriptInterpreterPython.cpp @@ -238,6 +238,10 @@ ScriptInterpreterPython::ScriptInterpreterPython (CommandInterpreter &interprete PyRun_SimpleString ("new_mode[3] = new_mode[3] | ECHO | ICANON"); PyRun_SimpleString ("new_mode[6][VEOF] = 255"); PyRun_SimpleString ("tcsetattr (new_stdin, TCSANOW, new_mode)"); + + run_string.Clear(); + run_string.Printf ("debugger_unique_id = %d", interpreter.GetDebugger().GetID()); + PyRun_SimpleString (run_string.GetData()); }