]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb/python: New InferiorThread.ptid_string attribute
authorAndrew Burgess <aburgess@redhat.com>
Tue, 9 Jan 2024 17:29:24 +0000 (17:29 +0000)
committerAndrew Burgess <aburgess@redhat.com>
Fri, 12 Jan 2024 09:22:25 +0000 (09:22 +0000)
This commit adds a new InferiorThread.ptid_string attribute.  This
read-only attribute contains the string returned by target_pid_to_str,
which actually converts a ptid (not pid) to a string.

This is the string that appears (at least in part) in the output of
'info threads' in the 'Target Id' column, but also in the thread
exited message that GDB prints.

Having access to this string from Python is useful for allowing
extensions identify threads in a similar way to how GDB core would
identify the thread.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Approved-By: Tom Tromey <tom@tromey.com>
gdb/NEWS
gdb/doc/python.texi
gdb/python/py-infthread.c
gdb/testsuite/gdb.python/py-infthread.exp

index 96a27be0fb2bdd45affe738046b94f1484fbff06..76bc131e9c942a86894c589622ed9dc0fd34f1ae 100644 (file)
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -91,6 +91,10 @@ show remote thread-options-packet
   ** New function gdb.interrupt(), that interrupts GDB as if the user
      typed control-c.
 
+  ** New gdb.InferiorThread.ptid_string attribute.  This read-only
+     attribute contains the string that appears in the 'Target Id'
+     column of the 'info threads' command output.
+
 * Debugger Adapter Protocol changes
 
   ** GDB now emits the "process" event.
index d74defeec0c36cc0a8e9a618797f2e3ef91d033c..da37348d663d467d63749847a4ea0c6235360183 100644 (file)
@@ -4084,6 +4084,13 @@ Either the LWPID or TID may be 0, which indicates that the operating system
 does not  use that identifier.
 @end defvar
 
+@defvar InferiorThread.ptid_string
+This read-only attribute contains a string representing
+@code{InferiorThread.ptid}.  This is the string that @value{GDBN} uses
+in the @samp{Target Id} column in the @kbd{info threads} output
+(@pxref{info_threads,,@samp{info threads}}).
+@end defvar
+
 @defvar InferiorThread.inferior
 The inferior this thread belongs to.  This attribute is represented as
 a @code{gdb.Inferior} object.  This attribute is not writable.
index 00d7171de64294582fd79b237f6ede6f0746503e..632984d9ce9c5bcd0df278d5f92f0d910dcb9271 100644 (file)
@@ -185,6 +185,30 @@ thpy_get_ptid (PyObject *self, void *closure)
   return gdbpy_create_ptid_object (thread_obj->thread->ptid);
 }
 
+/* Implement gdb.InferiorThread.ptid_string attribute.  */
+
+static PyObject *
+thpy_get_ptid_string (PyObject *self, void *closure)
+{
+  thread_object *thread_obj = (thread_object *) self;
+  THPY_REQUIRE_VALID (thread_obj);
+  ptid_t ptid = thread_obj->thread->ptid;
+
+  try
+    {
+      /* Select the correct inferior before calling a target_* function.  */
+      scoped_restore_current_thread restore_thread;
+      switch_to_inferior_no_thread (thread_obj->thread->inf);
+      std::string ptid_str = target_pid_to_str (ptid);
+      return PyUnicode_FromString (ptid_str.c_str ());
+    }
+  catch (const gdb_exception &except)
+    {
+      GDB_PY_HANDLE_EXCEPTION (except);
+      return nullptr;
+    }
+}
+
 /* Getter for InferiorThread.inferior -> Inferior.  */
 
 static PyObject *
@@ -388,6 +412,9 @@ static gdb_PyGetSetDef thread_object_getset[] =
     "Global number of the thread, as assigned by GDB.", NULL },
   { "ptid", thpy_get_ptid, NULL, "ID of the thread, as assigned by the OS.",
     NULL },
+  { "ptid_string", thpy_get_ptid_string, nullptr,
+    "A string representing ptid, as used by, for example, 'info threads'.",
+    nullptr },
   { "inferior", thpy_get_inferior, NULL,
     "The Inferior object this thread belongs to.", NULL },
 
index 0b10ce9ff77669cec8e32903c98154c6f203ea3e..3b07d84143f94f45819559f12e44002a42176bb7 100644 (file)
@@ -62,6 +62,14 @@ gdb_test "python print ('result = %s' % t0.num)" " = 1" "test InferiorThread.num
 gdb_test "python print ('result = %s' % t0.global_num)" " = 1" "test InferiorThread.global_num"
 gdb_test "python print ('result = %s' % str (t0.ptid))" " = \\(\[0-9\]+, \[0-9\]+, \[0-9\]+\\)" "test InferiorThread.ptid"
 
+# Test the InferiorThread.ptid_string attribute.  We don't test the
+# actual string contents as they vary based on target, but we check
+# that we get back a non-empty string.
+gdb_test "python print(type(t0.ptid_string))" "<class 'str'>" \
+    "check that InferiorThread.ptid_string is a string"
+gdb_test "python print(t0.ptid_string)" ".+" \
+    "check that InferiorThread.ptid_string is non-empty"
+
 gdb_py_test_silent_cmd "python i0 = t0.inferior" "test InferiorThread.inferior" 1
 gdb_test "python print ('result = %s' % i0.num)" " = 1" "test Inferior.num"