[Lldb-commits] [lldb] r107287 - in /lldb/trunk: include/lldb/API/SBDebugger.h include/lldb/Core/Debugger.h scripts/Python/append-debugger-id.py scripts/Python/build-swig-Python.sh scripts/lldb.swig source/API/SBDebugger.cpp source/Core/Debugger.cpp source/Interpreter/ScriptInterpreterPython.cpp

Caroline Tice ctice at apple.com
Wed Jun 30 14:04:20 PDT 2010


No, it is not actually correct.  The debugger_unique_id gets initialized to zero, which means
"there is no current debugger; if you want one you need to create one".  You should always
check the value of debugger_unique_id before trying to find the debugger; if the id is zero, then
there is no debugger to find.

Whenever you are using the lldb python module directly from Unix (or Linux or whatever), you
should expect that the debugger_unique_id is probably zero, and you should expect to have to
call lldb.SBDebugger.Create() to  create a debugger instance.

When running the command-line debugger of lldb (or lldb from inside Xcode), the debugger instance has
already been created by the command-line debugger (or Xcode), and you want to be able to find
and use the already created instance of the debugger.  When the command-line debugger starts up the
script interpreter, it updates the value of debugger_unique_id in the lldb python module to reflect the
id of the existing debugger.  Then when you use the 'script' command or the 'breakpoint add' command from
the command interpreter to slip into python-mode, you can use SBDebugger.FindDebuggerWithID (debugger_unique_id) to get the right instance of the debugger.  Of course, just to be on the safe side, you should
still check to make sure the id isn't zero...

Let me know if this is still not clear...

-- Caroline
ctice at apple.com


On Jun 30, 2010, at 12:19 PM, Johnny Chen wrote:

> Hi Caroline,
> 
> Is the following usage of FindDebuggerWithID() correct?
> Does SBDebugger.Initialize() need to make sure that lldb.debugger_unique_id is sane?
> 
> python
> Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29) 
> [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import lldb
> import lldb
>>>> lldb.SBDebugger.Initialize()
> lldb.SBDebugger.Initialize()
>>>> dbg = lldb.SBDebugger.FindDebuggerWithID(lldb.debugger_unique_id)
> dbg = lldb.SBDebugger.FindDebuggerWithID(lldb.debugger_unique_id)
>>>> ci = dbg.GetCommandInterpreter()
> ci = dbg.GetCommandInterpreter()
>>>> res = res = lldb.SBCommandReturnObject()
> res = res = lldb.SBCommandReturnObject()
>>>> dbg.SetAsync(False)
> dbg.SetAsync(False)
>>>> ci.HandleCommand("help", res)
> ci.HandleCommand("help", res)
> 6
>>>> dbg.IsValid()
> dbg.IsValid()
> False
>>>> 
> 
> On Jun 30, 2010, at 9:22 AM, Caroline Tice wrote:
> 
>> Author: ctice
>> Date: Wed Jun 30 11:22:25 2010
>> New Revision: 107287
>> 
>> URL: http://llvm.org/viewvc/llvm-project?rev=107287&view=rev
>> Log:
>> 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).
>> 
>> 
>> Added:
>>   lldb/trunk/scripts/Python/append-debugger-id.py
>> Modified:
>>   lldb/trunk/include/lldb/API/SBDebugger.h
>>   lldb/trunk/include/lldb/Core/Debugger.h
>>   lldb/trunk/scripts/Python/build-swig-Python.sh
>>   lldb/trunk/scripts/lldb.swig
>>   lldb/trunk/source/API/SBDebugger.cpp
>>   lldb/trunk/source/Core/Debugger.cpp
>>   lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp
>> 
>> Modified: lldb/trunk/include/lldb/API/SBDebugger.h
>> URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBDebugger.h?rev=107287&r1=107286&r2=107287&view=diff
>> ==============================================================================
>> --- lldb/trunk/include/lldb/API/SBDebugger.h (original)
>> +++ lldb/trunk/include/lldb/API/SBDebugger.h Wed Jun 30 11:22:25 2010
>> @@ -129,6 +129,9 @@
>>    void
>>    PushInputReader (lldb::SBInputReader &reader);
>> 
>> +    static SBDebugger
>> +    FindDebuggerWithID (int id);
>> +
>> private:
>> 
>>    // Use the static function: SBDebugger::Create();
>> 
>> Modified: lldb/trunk/include/lldb/Core/Debugger.h
>> URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/Debugger.h?rev=107287&r1=107286&r2=107287&view=diff
>> ==============================================================================
>> --- lldb/trunk/include/lldb/Core/Debugger.h (original)
>> +++ lldb/trunk/include/lldb/Core/Debugger.h Wed Jun 30 11:22:25 2010
>> @@ -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 @@
>> ///
>> /// Provides a global root objects for the debugger core.
>> //----------------------------------------------------------------------
>> -class Debugger
>> +class Debugger :
>> +    public UserID
>> {
>> public:
>> 
>> @@ -139,6 +141,9 @@
>>    void
>>    UpdateExecutionContext (ExecutionContext *override_context);
>> 
>> +    static lldb::DebuggerSP
>> +    FindDebuggerWithID (lldb::user_id_t id);
>> +
>> protected:
>> 
>>    static void
>> @@ -165,7 +170,7 @@
>> 
>>    std::stack<lldb::InputReaderSP> m_input_readers;
>>    std::string m_input_reader_data;
>> -    
>> +
>> private:
>> 
>>    // Use Debugger::CreateInstance() to get a shared pointer to a new
>> 
>> Added: lldb/trunk/scripts/Python/append-debugger-id.py
>> URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/Python/append-debugger-id.py?rev=107287&view=auto
>> ==============================================================================
>> --- lldb/trunk/scripts/Python/append-debugger-id.py (added)
>> +++ lldb/trunk/scripts/Python/append-debugger-id.py Wed Jun 30 11:22:25 2010
>> @@ -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."
>> 
>> Modified: lldb/trunk/scripts/Python/build-swig-Python.sh
>> URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/Python/build-swig-Python.sh?rev=107287&r1=107286&r2=107287&view=diff
>> ==============================================================================
>> --- lldb/trunk/scripts/Python/build-swig-Python.sh (original)
>> +++ lldb/trunk/scripts/Python/build-swig-Python.sh Wed Jun 30 11:22:25 2010
>> @@ -127,3 +127,10 @@
>> 
>> 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
>> 
>> Modified: lldb/trunk/scripts/lldb.swig
>> URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/lldb.swig?rev=107287&r1=107286&r2=107287&view=diff
>> ==============================================================================
>> --- lldb/trunk/scripts/lldb.swig (original)
>> +++ lldb/trunk/scripts/lldb.swig Wed Jun 30 11:22:25 2010
>> @@ -43,6 +43,8 @@
>>      }
>>    }
>>    $1[i] = 0;
>> +  } else if ($input == Py_None) {
>> +    $1 =  NULL;
>>  } else {
>>    PyErr_SetString(PyExc_TypeError,"not a list");
>>    return NULL;
>> 
>> Modified: lldb/trunk/source/API/SBDebugger.cpp
>> URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SBDebugger.cpp?rev=107287&r1=107286&r2=107287&view=diff
>> ==============================================================================
>> --- lldb/trunk/source/API/SBDebugger.cpp (original)
>> +++ lldb/trunk/source/API/SBDebugger.cpp Wed Jun 30 11:22:25 2010
>> @@ -51,7 +51,6 @@
>>    return debugger;
>> }
>> 
>> -
>> SBDebugger::SBDebugger () :
>>    m_opaque_sp ()
>> {
>> @@ -549,3 +548,12 @@
>> }
>> 
>> 
>> +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;
>> +}
>> 
>> Modified: lldb/trunk/source/Core/Debugger.cpp
>> URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/Debugger.cpp?rev=107287&r1=107286&r2=107287&view=diff
>> ==============================================================================
>> --- lldb/trunk/source/Core/Debugger.cpp (original)
>> +++ lldb/trunk/source/Core/Debugger.cpp Wed Jun 30 11:22:25 2010
>> @@ -24,6 +24,8 @@
>> 
>> 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::Debugger () :
>> +    UserID (g_unique_id++),
>>    m_input_comm("debugger.input"),
>>    m_input_file (),
>>    m_output_file (),
>> @@ -491,3 +494,21 @@
>>    }
>> }
>> 
>> +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;
>> +}
>> 
>> Modified: lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp
>> URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp?rev=107287&r1=107286&r2=107287&view=diff
>> ==============================================================================
>> --- lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp (original)
>> +++ lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp Wed Jun 30 11:22:25 2010
>> @@ -238,6 +238,10 @@
>>        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());
>>    }
>> 
>> 
>> 
>> 
>> _______________________________________________
>> lldb-commits mailing list
>> lldb-commits at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/lldb-commits
> 





More information about the lldb-commits mailing list