1 /* Python interface to inferior threads.
3 Copyright (C) 2009-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "gdbthread.h"
23 #include "python-internal.h"
25 extern PyTypeObject thread_object_type
26 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("thread_object");
28 /* Require that INFERIOR be a valid inferior ID. */
29 #define THPY_REQUIRE_VALID(Thread) \
31 if (!Thread->thread) \
33 PyErr_SetString (PyExc_RuntimeError, \
34 _("Thread no longer exists.")); \
39 gdbpy_ref
<thread_object
>
40 create_thread_object (struct thread_info
*tp
)
42 gdbpy_ref
<thread_object
> thread_obj
;
44 gdbpy_ref
<inferior_object
> inf_obj
= inferior_to_inferior_object (tp
->inf
);
48 thread_obj
.reset (PyObject_New (thread_object
, &thread_object_type
));
49 if (thread_obj
== NULL
)
52 thread_obj
->thread
= tp
;
53 thread_obj
->inf_obj
= (PyObject
*) inf_obj
.release ();
54 thread_obj
->dict
= PyDict_New ();
55 if (thread_obj
->dict
== nullptr)
62 thpy_dealloc (PyObject
*self
)
64 thread_object
*thr_obj
= (thread_object
*) self
;
66 gdb_assert (thr_obj
->inf_obj
!= nullptr);
68 Py_DECREF (thr_obj
->inf_obj
);
69 Py_XDECREF (thr_obj
->dict
);
71 Py_TYPE (self
)->tp_free (self
);
75 thpy_get_name (PyObject
*self
, void *ignore
)
77 thread_object
*thread_obj
= (thread_object
*) self
;
79 THPY_REQUIRE_VALID (thread_obj
);
81 const char *name
= thread_name (thread_obj
->thread
);
85 return PyUnicode_FromString (name
);
88 /* Return a string containing target specific additional information about
89 the state of the thread, or None, if there is no such additional
93 thpy_get_details (PyObject
*self
, void *ignore
)
95 thread_object
*thread_obj
= (thread_object
*) self
;
97 THPY_REQUIRE_VALID (thread_obj
);
99 /* GCC can't tell that extra_info will always be assigned after the
100 'catch', so initialize it. */
101 const char *extra_info
= nullptr;
104 extra_info
= target_extra_thread_info (thread_obj
->thread
);
106 catch (const gdb_exception
&except
)
108 GDB_PY_HANDLE_EXCEPTION (except
);
110 if (extra_info
== nullptr)
113 return PyUnicode_FromString (extra_info
);
117 thpy_set_name (PyObject
*self
, PyObject
*newvalue
, void *ignore
)
119 thread_object
*thread_obj
= (thread_object
*) self
;
120 gdb::unique_xmalloc_ptr
<char> name
;
122 if (! thread_obj
->thread
)
124 PyErr_SetString (PyExc_RuntimeError
, _("Thread no longer exists."));
128 if (newvalue
== NULL
)
130 PyErr_SetString (PyExc_TypeError
,
131 _("Cannot delete `name' attribute."));
134 else if (newvalue
== Py_None
)
138 else if (! gdbpy_is_string (newvalue
))
140 PyErr_SetString (PyExc_TypeError
,
141 _("The value of `name' must be a string."));
146 name
= python_string_to_host_string (newvalue
);
151 thread_obj
->thread
->set_name (std::move (name
));
156 /* Getter for InferiorThread.num. */
159 thpy_get_num (PyObject
*self
, void *closure
)
161 thread_object
*thread_obj
= (thread_object
*) self
;
163 THPY_REQUIRE_VALID (thread_obj
);
166 = gdb_py_object_from_longest (thread_obj
->thread
->per_inf_num
);
167 return result
.release ();
170 /* Getter for InferiorThread.global_num. */
173 thpy_get_global_num (PyObject
*self
, void *closure
)
175 thread_object
*thread_obj
= (thread_object
*) self
;
177 THPY_REQUIRE_VALID (thread_obj
);
180 = gdb_py_object_from_longest (thread_obj
->thread
->global_num
);
181 return result
.release ();
184 /* Getter for InferiorThread.ptid -> (pid, lwp, tid).
185 Returns a tuple with the thread's ptid components. */
188 thpy_get_ptid (PyObject
*self
, void *closure
)
190 thread_object
*thread_obj
= (thread_object
*) self
;
192 THPY_REQUIRE_VALID (thread_obj
);
194 return gdbpy_create_ptid_object (thread_obj
->thread
->ptid
);
197 /* Implement gdb.InferiorThread.ptid_string attribute. */
200 thpy_get_ptid_string (PyObject
*self
, void *closure
)
202 thread_object
*thread_obj
= (thread_object
*) self
;
203 THPY_REQUIRE_VALID (thread_obj
);
204 ptid_t ptid
= thread_obj
->thread
->ptid
;
208 /* Select the correct inferior before calling a target_* function. */
209 scoped_restore_current_thread restore_thread
;
210 switch_to_inferior_no_thread (thread_obj
->thread
->inf
);
211 std::string ptid_str
= target_pid_to_str (ptid
);
212 return PyUnicode_FromString (ptid_str
.c_str ());
214 catch (const gdb_exception
&except
)
216 GDB_PY_HANDLE_EXCEPTION (except
);
221 /* Getter for InferiorThread.inferior -> Inferior. */
224 thpy_get_inferior (PyObject
*self
, void *ignore
)
226 thread_object
*thread_obj
= (thread_object
*) self
;
228 THPY_REQUIRE_VALID (thread_obj
);
229 Py_INCREF (thread_obj
->inf_obj
);
231 return thread_obj
->inf_obj
;
234 /* Implementation of InferiorThread.switch ().
235 Makes this the GDB selected thread. */
238 thpy_switch (PyObject
*self
, PyObject
*args
)
240 thread_object
*thread_obj
= (thread_object
*) self
;
242 THPY_REQUIRE_VALID (thread_obj
);
246 switch_to_thread (thread_obj
->thread
);
248 catch (const gdb_exception
&except
)
250 GDB_PY_HANDLE_EXCEPTION (except
);
256 /* Implementation of InferiorThread.is_stopped () -> Boolean.
257 Return whether the thread is stopped. */
260 thpy_is_stopped (PyObject
*self
, PyObject
*args
)
262 thread_object
*thread_obj
= (thread_object
*) self
;
264 THPY_REQUIRE_VALID (thread_obj
);
266 if (thread_obj
->thread
->state
== THREAD_STOPPED
)
272 /* Implementation of InferiorThread.is_running () -> Boolean.
273 Return whether the thread is running. */
276 thpy_is_running (PyObject
*self
, PyObject
*args
)
278 thread_object
*thread_obj
= (thread_object
*) self
;
280 THPY_REQUIRE_VALID (thread_obj
);
282 if (thread_obj
->thread
->state
== THREAD_RUNNING
)
288 /* Implementation of InferiorThread.is_exited () -> Boolean.
289 Return whether the thread is exited. */
292 thpy_is_exited (PyObject
*self
, PyObject
*args
)
294 thread_object
*thread_obj
= (thread_object
*) self
;
296 THPY_REQUIRE_VALID (thread_obj
);
298 if (thread_obj
->thread
->state
== THREAD_EXITED
)
304 /* Implementation of gdb.InfThread.is_valid (self) -> Boolean.
305 Returns True if this inferior Thread object still exists
309 thpy_is_valid (PyObject
*self
, PyObject
*args
)
311 thread_object
*thread_obj
= (thread_object
*) self
;
313 if (! thread_obj
->thread
)
319 /* Implementation of gdb.InferiorThread.handle (self) -> handle. */
322 thpy_thread_handle (PyObject
*self
, PyObject
*args
)
324 thread_object
*thread_obj
= (thread_object
*) self
;
325 THPY_REQUIRE_VALID (thread_obj
);
327 gdb::array_view
<const gdb_byte
> hv
;
331 hv
= target_thread_info_to_thread_handle (thread_obj
->thread
);
333 catch (const gdb_exception
&except
)
335 GDB_PY_HANDLE_EXCEPTION (except
);
340 PyErr_SetString (PyExc_RuntimeError
, _("Thread handle not found."));
344 PyObject
*object
= PyBytes_FromStringAndSize ((const char *) hv
.data (),
349 /* Implement repr() for gdb.InferiorThread. */
352 thpy_repr (PyObject
*self
)
354 thread_object
*thread_obj
= (thread_object
*) self
;
356 if (thread_obj
->thread
== nullptr)
357 return gdb_py_invalid_object_repr (self
);
359 thread_info
*thr
= thread_obj
->thread
;
360 return PyUnicode_FromFormat ("<%s id=%s target-id=\"%s\">",
361 Py_TYPE (self
)->tp_name
,
362 print_full_thread_id (thr
),
363 target_pid_to_str (thr
->ptid
).c_str ());
366 /* Return a reference to a new Python object representing a ptid_t.
367 The object is a tuple containing (pid, lwp, tid). */
369 gdbpy_create_ptid_object (ptid_t ptid
)
376 ret
= PyTuple_New (3);
384 gdbpy_ref
<> pid_obj
= gdb_py_object_from_longest (pid
);
385 if (pid_obj
== nullptr)
387 gdbpy_ref
<> lwp_obj
= gdb_py_object_from_longest (lwp
);
388 if (lwp_obj
== nullptr)
390 gdbpy_ref
<> tid_obj
= gdb_py_object_from_ulongest (tid
);
391 if (tid_obj
== nullptr)
394 /* Note that these steal references, hence the use of 'release'. */
395 PyTuple_SET_ITEM (ret
, 0, pid_obj
.release ());
396 PyTuple_SET_ITEM (ret
, 1, lwp_obj
.release ());
397 PyTuple_SET_ITEM (ret
, 2, tid_obj
.release ());
402 /* Implementation of gdb.selected_thread () -> gdb.InferiorThread.
403 Returns the selected thread object. */
406 gdbpy_selected_thread (PyObject
*self
, PyObject
*args
)
408 if (inferior_ptid
!= null_ptid
)
409 return thread_to_thread_object (inferior_thread ()).release ();
414 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
415 gdbpy_initialize_thread (void)
417 if (PyType_Ready (&thread_object_type
) < 0)
420 return gdb_pymodule_addobject (gdb_module
, "InferiorThread",
421 (PyObject
*) &thread_object_type
);
424 GDBPY_INITIALIZE_FILE (gdbpy_initialize_thread
);
428 static gdb_PyGetSetDef thread_object_getset
[] =
430 { "__dict__", gdb_py_generic_dict
, nullptr,
431 "The __dict__ for this thread.", &thread_object_type
},
432 { "name", thpy_get_name
, thpy_set_name
,
433 "The name of the thread, as set by the user or the OS.", NULL
},
434 { "details", thpy_get_details
, NULL
,
435 "A target specific string containing extra thread state details.",
437 { "num", thpy_get_num
, NULL
,
438 "Per-inferior number of the thread, as assigned by GDB.", NULL
},
439 { "global_num", thpy_get_global_num
, NULL
,
440 "Global number of the thread, as assigned by GDB.", NULL
},
441 { "ptid", thpy_get_ptid
, NULL
, "ID of the thread, as assigned by the OS.",
443 { "ptid_string", thpy_get_ptid_string
, nullptr,
444 "A string representing ptid, as used by, for example, 'info threads'.",
446 { "inferior", thpy_get_inferior
, NULL
,
447 "The Inferior object this thread belongs to.", NULL
},
452 static PyMethodDef thread_object_methods
[] =
454 { "is_valid", thpy_is_valid
, METH_NOARGS
,
455 "is_valid () -> Boolean.\n\
456 Return true if this inferior thread is valid, false if not." },
457 { "switch", thpy_switch
, METH_NOARGS
,
459 Makes this the GDB selected thread." },
460 { "is_stopped", thpy_is_stopped
, METH_NOARGS
,
461 "is_stopped () -> Boolean\n\
462 Return whether the thread is stopped." },
463 { "is_running", thpy_is_running
, METH_NOARGS
,
464 "is_running () -> Boolean\n\
465 Return whether the thread is running." },
466 { "is_exited", thpy_is_exited
, METH_NOARGS
,
467 "is_exited () -> Boolean\n\
468 Return whether the thread is exited." },
469 { "handle", thpy_thread_handle
, METH_NOARGS
,
470 "handle () -> handle\n\
471 Return thread library specific handle for thread." },
476 PyTypeObject thread_object_type
=
478 PyVarObject_HEAD_INIT (NULL
, 0)
479 "gdb.InferiorThread", /*tp_name*/
480 sizeof (thread_object
), /*tp_basicsize*/
482 thpy_dealloc
, /*tp_dealloc*/
487 thpy_repr
, /*tp_repr*/
489 0, /*tp_as_sequence*/
497 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
498 "GDB thread object", /* tp_doc */
501 0, /* tp_richcompare */
502 0, /* tp_weaklistoffset */
505 thread_object_methods
, /* tp_methods */
507 thread_object_getset
, /* tp_getset */
510 0, /* tp_descr_get */
511 0, /* tp_descr_set */
512 offsetof (thread_object
, dict
), /* tp_dictoffset */