public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Cc: Andrew Burgess <aburgess@redhat.com>
Subject: [PATCH] gdb/python: New InferiorThread.ptid_string attribute
Date: Wed, 10 Jan 2024 15:54:38 +0000	[thread overview]
Message-ID: <eba29f55f1a2816bc55db0b1407f894e274f0f9d.1704821556.git.aburgess@redhat.com> (raw)
Message-ID: <20240110155438.45mxVODUrQSrBi5RsNx64XyjuEUzSJd9Bg4cCtu4CzM@z> (raw)
In-Reply-To: <cover.1704901918.git.aburgess@redhat.com>

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.
---
 gdb/NEWS                                  |  4 ++++
 gdb/doc/python.texi                       |  7 +++++++
 gdb/python/py-infthread.c                 | 20 ++++++++++++++++++++
 gdb/testsuite/gdb.python/py-infthread.exp |  8 ++++++++
 4 files changed, 39 insertions(+)

diff --git a/gdb/NEWS b/gdb/NEWS
index 11cd6c0663e..60e58daa501 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -87,6 +87,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.
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index d74defeec0c..da37348d663 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -4084,6 +4084,13 @@
 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.
diff --git a/gdb/python/py-infthread.c b/gdb/python/py-infthread.c
index 00d7171de64..7307b9e9da9 100644
--- a/gdb/python/py-infthread.c
+++ b/gdb/python/py-infthread.c
@@ -185,6 +185,23 @@ 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;
+
+  /* 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 ());
+}
+
 /* Getter for InferiorThread.inferior -> Inferior.  */
 
 static PyObject *
@@ -388,6 +405,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 },
 
diff --git a/gdb/testsuite/gdb.python/py-infthread.exp b/gdb/testsuite/gdb.python/py-infthread.exp
index 0b10ce9ff77..3b07d84143f 100644
--- a/gdb/testsuite/gdb.python/py-infthread.exp
+++ b/gdb/testsuite/gdb.python/py-infthread.exp
@@ -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"
 

base-commit: b7a5722ebdd24a0d15d56e96d30a649ea1d7b0ee
-- 
2.25.4


  reply	other threads:[~2024-01-10 15:54 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-05 11:48 [PATCH 0/6] Python __repr__() methods and new __dict__ attributes Andrew Burgess
2024-01-05 11:48 ` [PATCH 1/6] gdb/python: hoist common invalid object repr code into py-utils.c Andrew Burgess
2024-01-09 19:19   ` Tom Tromey
2024-01-05 11:48 ` [PATCH 2/6] gdb/python: add gdb.InferiorThread.__repr__() method Andrew Burgess
2024-01-05 11:48 ` [PATCH 3/6] gdb/python: add gdb.Frame.__repr__() method Andrew Burgess
2024-01-05 11:48 ` [PATCH 4/6] gdb/python: remove users ability to create gdb.Progspace objects Andrew Burgess
2024-01-05 13:27   ` Eli Zaretskii
2024-01-05 11:48 ` [PATCH 5/6] gdb/python: Add gdb.Inferior.__dict__ attribute Andrew Burgess
2024-01-05 13:33   ` Eli Zaretskii
2024-01-09 20:05   ` Tom Tromey
2024-01-05 11:48 ` [PATCH 6/6] gdb/python: Add gdb.InferiorThread.__dict__ attribute Andrew Burgess
2024-01-05 13:31   ` Eli Zaretskii
2024-01-09 20:11   ` Tom Tromey
2024-01-10 10:38     ` Andrew Burgess
2024-01-10 15:54 ` [PATCHv2 0/8] Python __repr__() methods and new __dict__ attributes Andrew Burgess
2024-01-09 17:32   ` Andrew Burgess [this message]
2024-01-09 18:50     ` [PATCH] gdb/python: New InferiorThread.ptid_string attribute Eli Zaretskii
2024-01-09 19:10     ` Tom Tromey
2024-01-12  9:39       ` [PUSHED] " Andrew Burgess
2024-01-10 15:54     ` [PATCH] " Andrew Burgess
2024-01-10 15:54   ` [PATCHv2 1/8] gdb/python: hoist common invalid object repr code into py-utils.c Andrew Burgess
2024-01-10 15:54   ` [PATCHv2 2/8] gdb/python: add gdb.InferiorThread.__repr__() method Andrew Burgess
2024-01-10 15:54   ` [PATCHv2 3/8] gdb/python: add gdb.Frame.__repr__() method Andrew Burgess
2024-01-10 15:54   ` [PATCHv2 4/8] gdb/python: remove users ability to create gdb.Progspace objects Andrew Burgess
2024-01-10 15:54   ` [PATCHv2 5/8] gdb/python: Add gdb.Inferior.__dict__ attribute Andrew Burgess
2024-01-10 15:54   ` [PATCHv2 6/8] gdb/python: Add gdb.InferiorThread.__dict__ attribute Andrew Burgess
2024-01-10 15:54   ` [PATCHv2 7/8] gdb/doc: add some notes on selecting suitable attribute names Andrew Burgess
2024-01-10 16:35     ` Eli Zaretskii
2024-01-11 10:48       ` Andrew Burgess
2024-01-11 10:56         ` Eli Zaretskii
2024-01-10 15:54   ` [PATCHv2 8/8] gdb/doc: update examples in gdb.Progspace and gdb.Objfile docs Andrew Burgess
2024-01-10 16:36     ` Eli Zaretskii
2024-01-10 18:08   ` [PATCHv2 0/8] Python __repr__() methods and new __dict__ attributes Tom Tromey
2024-01-12 13:44     ` Andrew Burgess
2024-01-12 14:57       ` Tom de Vries
2024-01-12 16:20         ` Andrew Burgess

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=eba29f55f1a2816bc55db0b1407f894e274f0f9d.1704821556.git.aburgess@redhat.com \
    --to=aburgess@redhat.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).