gdb/python: Add gdb.InferiorThread.__dict__ attribute

The gdb.Objfile, gdb.Progspace, gdb.Type, and gdb.Inferior Python
types already have a __dict__ attribute, which allows users to create
user defined attributes within the objects.  This is useful if the
user wants to cache information within an object.

This commit adds the same functionality to the gdb.InferiorThread
type.

After this commit there is a new gdb.InferiorThread.__dict__
attribute, which is a dictionary.  A user can, for example, do this:

  (gdb) pi
  >>> t = gdb.selected_thread()
  >>> t._user_attribute = 123
  >>> t._user_attribute
  123
  >>>

There's a new test included.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Approved-By: Tom Tromey <tom@tromey.com>
This commit is contained in:
Andrew Burgess
2024-01-05 11:05:51 +00:00
parent 13cd9bceea
commit 1d586eda5c
5 changed files with 73 additions and 2 deletions

View File

@@ -51,6 +51,9 @@ create_thread_object (struct thread_info *tp)
thread_obj->thread = tp;
thread_obj->inf_obj = (PyObject *) inf_obj.release ();
thread_obj->dict = PyDict_New ();
if (thread_obj->dict == nullptr)
return nullptr;
return thread_obj;
}
@@ -58,7 +61,13 @@ create_thread_object (struct thread_info *tp)
static void
thpy_dealloc (PyObject *self)
{
Py_DECREF (((thread_object *) self)->inf_obj);
thread_object *thr_obj = (thread_object *) self;
gdb_assert (thr_obj->inf_obj != nullptr);
Py_DECREF (thr_obj->inf_obj);
Py_XDECREF (thr_obj->dict);
Py_TYPE (self)->tp_free (self);
}
@@ -418,6 +427,8 @@ GDBPY_INITIALIZE_FILE (gdbpy_initialize_thread);
static gdb_PyGetSetDef thread_object_getset[] =
{
{ "__dict__", gdb_py_generic_dict, nullptr,
"The __dict__ for this thread.", &thread_object_type },
{ "name", thpy_get_name, thpy_set_name,
"The name of the thread, as set by the user or the OS.", NULL },
{ "details", thpy_get_details, NULL,
@@ -498,7 +509,7 @@ PyTypeObject thread_object_type =
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
offsetof (thread_object, dict), /* tp_dictoffset */
0, /* tp_init */
0 /* tp_alloc */
};