This is the mail archive of the mailing list for the Archer project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

python: a patch for getting target thread id from a thread object


Here's a simple patch which adds a method for getting the target
thread id of a Thread object.

My use case is this: I'm writing a gdb script in python which allows
you to attach to running Python processes with a Python debugger
This is done by switching to the thread which has the Python GIL and
running there some Python commands which start the winpdb debugging
The ID of the thread which has the GIL is available in a global
(_PyThreadState_Current->thread_id). However, currently there's no way
for the python script to know to which thread it should switch, since
it only has access to the internal GDB thread number. The patch fixes


diff --git a/gdb/python/python-infthread.c b/gdb/python/python-infthread.c
index 21e4eab..7e97f8c 100644
--- a/gdb/python/python-infthread.c
+++ b/gdb/python/python-infthread.c
@@ -73,7 +73,19 @@ thpy_get_num (PyObject *self, void *closure)
   return PyLong_FromLong (thread_obj->thread->num);

+/* Implementation of Thread.get_target_id () -> str.
+   Returns the target ID.  */
+PyObject *
+thpy_get_target_id (PyObject *self, PyObject *args)
+  thread_object *thread_obj = (thread_object *) self;
+  THPY_REQUIRE_VALID (thread_obj);
+  return PyString_FromString (target_pid_to_str (thread_obj->thread->ptid));

 /* Implementation of Inferior.frames () -> (gdb.Frame, ...).
    Returns a tuple of all frame objects.  */
@@ -238,6 +251,9 @@ Return the newest frame in the thread." },
   { "switch", thpy_switch, METH_NOARGS,
     "switch ()\n\
 Makes this the GDB selected thread." },
+  { "get_target_id", thpy_get_target_id, METH_NOARGS,
+    "get_target_id () -> str\n\
+Return the target ID of the thread." },

   { NULL }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]