aboutsummaryrefslogtreecommitdiff
path: root/gdb/python/py-infthread.c
diff options
context:
space:
mode:
authorAndrew Burgess <aburgess@redhat.com>2024-01-05 11:05:51 +0000
committerAndrew Burgess <aburgess@redhat.com>2024-01-12 11:21:31 +0000
commit1d586eda5c90bb9e03e997682dce60811f978def (patch)
treea5f9b50150dce957035c62bd90f5e4ae7750d793 /gdb/python/py-infthread.c
parent13cd9bceea3c3a3081c463597146a11ade765e39 (diff)
downloadgdb-1d586eda5c90bb9e03e997682dce60811f978def.zip
gdb-1d586eda5c90bb9e03e997682dce60811f978def.tar.gz
gdb-1d586eda5c90bb9e03e997682dce60811f978def.tar.bz2
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>
Diffstat (limited to 'gdb/python/py-infthread.c')
-rw-r--r--gdb/python/py-infthread.c15
1 files changed, 13 insertions, 2 deletions
diff --git a/gdb/python/py-infthread.c b/gdb/python/py-infthread.c
index b5887c7..4211584 100644
--- a/gdb/python/py-infthread.c
+++ b/gdb/python/py-infthread.c
@@ -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 */
};