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/>. */
20 #include "gdbthread.h"
22 #include "python-internal.h"
24 extern PyTypeObject thread_object_type
25 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("thread_object");
27 /* Require that INFERIOR be a valid inferior ID. */
28 #define THPY_REQUIRE_VALID(Thread) \
30 if (!Thread->thread) \
32 PyErr_SetString (PyExc_RuntimeError, \
33 _("Thread no longer exists.")); \
38 gdbpy_ref
<thread_object
>
39 create_thread_object (struct thread_info
*tp
)
41 gdbpy_ref
<thread_object
> thread_obj
;
43 gdbpy_ref
<inferior_object
> inf_obj
= inferior_to_inferior_object (tp
->inf
);
47 thread_obj
.reset (PyObject_New (thread_object
, &thread_object_type
));
48 if (thread_obj
== NULL
)
51 thread_obj
->thread
= tp
;
52 thread_obj
->inf_obj
= (PyObject
*) inf_obj
.release ();
53 thread_obj
->dict
= PyDict_New ();
54 if (thread_obj
->dict
== nullptr)
61 thpy_dealloc (PyObject
*self
)
63 thread_object
*thr_obj
= (thread_object
*) self
;
65 gdb_assert (thr_obj
->inf_obj
!= nullptr);
67 Py_DECREF (thr_obj
->inf_obj
);
68 Py_XDECREF (thr_obj
->dict
);
70 Py_TYPE (self
)->tp_free (self
);
74 thpy_get_name (PyObject
*self
, void *ignore
)
76 thread_object
*thread_obj
= (thread_object
*) self
;
78 THPY_REQUIRE_VALID (thread_obj
);
80 const char *name
= thread_name (thread_obj
->thread
);
84 return PyUnicode_FromString (name
);
87 /* Return a string containing target specific additional information about
88 the state of the thread, or None, if there is no such additional
92 thpy_get_details (PyObject
*self
, void *ignore
)
94 thread_object
*thread_obj
= (thread_object
*) self
;
96 THPY_REQUIRE_VALID (thread_obj
);
98 /* GCC can't tell that extra_info will always be assigned after the
99 'catch', so initialize it. */
100 const char *extra_info
= nullptr;
103 extra_info
= target_extra_thread_info (thread_obj
->thread
);
105 catch (const gdb_exception
&except
)
107 return gdbpy_handle_gdb_exception (nullptr, except
);
109 if (extra_info
== nullptr)
112 return PyUnicode_FromString (extra_info
);
116 thpy_set_name (PyObject
*self
, PyObject
*newvalue
, void *ignore
)
118 thread_object
*thread_obj
= (thread_object
*) self
;
119 gdb::unique_xmalloc_ptr
<char> name
;
121 if (! thread_obj
->thread
)
123 PyErr_SetString (PyExc_RuntimeError
, _("Thread no longer exists."));
127 if (newvalue
== NULL
)
129 PyErr_SetString (PyExc_TypeError
,
130 _("Cannot delete `name' attribute."));
133 else if (newvalue
== Py_None
)
137 else if (! gdbpy_is_string (newvalue
))
139 PyErr_SetString (PyExc_TypeError
,
140 _("The value of `name' must be a string."));
145 name
= python_string_to_host_string (newvalue
);
150 thread_obj
->thread
->set_name (std::move (name
));
155 /* Getter for InferiorThread.num. */
158 thpy_get_num (PyObject
*self
, void *closure
)
160 thread_object
*thread_obj
= (thread_object
*) self
;
162 THPY_REQUIRE_VALID (thread_obj
);
165 = gdb_py_object_from_longest (thread_obj
->thread
->per_inf_num
);
166 return result
.release ();
169 /* Getter for InferiorThread.global_num. */
172 thpy_get_global_num (PyObject
*self
, void *closure
)
174 thread_object
*thread_obj
= (thread_object
*) self
;
176 THPY_REQUIRE_VALID (thread_obj
);
179 = gdb_py_object_from_longest (thread_obj
->thread
->global_num
);
180 return result
.release ();
183 /* Getter for InferiorThread.ptid -> (pid, lwp, tid).
184 Returns a tuple with the thread's ptid components. */
187 thpy_get_ptid (PyObject
*self
, void *closure
)
189 thread_object
*thread_obj
= (thread_object
*) self
;
191 THPY_REQUIRE_VALID (thread_obj
);
193 return gdbpy_create_ptid_object (thread_obj
->thread
->ptid
);
196 /* Implement gdb.InferiorThread.ptid_string attribute. */
199 thpy_get_ptid_string (PyObject
*self
, void *closure
)
201 thread_object
*thread_obj
= (thread_object
*) self
;
202 THPY_REQUIRE_VALID (thread_obj
);
203 ptid_t ptid
= thread_obj
->thread
->ptid
;
207 /* Select the correct inferior before calling a target_* function. */
208 scoped_restore_current_thread restore_thread
;
209 switch_to_inferior_no_thread (thread_obj
->thread
->inf
);
210 std::string ptid_str
= target_pid_to_str (ptid
);
211 return PyUnicode_FromString (ptid_str
.c_str ());
213 catch (const gdb_exception
&except
)
215 return gdbpy_handle_gdb_exception (nullptr, except
);
219 /* Getter for InferiorThread.inferior -> Inferior. */
222 thpy_get_inferior (PyObject
*self
, void *ignore
)
224 thread_object
*thread_obj
= (thread_object
*) self
;
226 THPY_REQUIRE_VALID (thread_obj
);
227 Py_INCREF (thread_obj
->inf_obj
);
229 return thread_obj
->inf_obj
;
232 /* Implementation of InferiorThread.switch ().
233 Makes this the GDB selected thread. */
236 thpy_switch (PyObject
*self
, PyObject
*args
)
238 thread_object
*thread_obj
= (thread_object
*) self
;
240 THPY_REQUIRE_VALID (thread_obj
);
244 switch_to_thread (thread_obj
->thread
);
246 catch (const gdb_exception
&except
)
248 return gdbpy_handle_gdb_exception (nullptr, except
);
254 /* Implementation of InferiorThread.is_stopped () -> Boolean.
255 Return whether the thread is stopped. */
258 thpy_is_stopped (PyObject
*self
, PyObject
*args
)
260 thread_object
*thread_obj
= (thread_object
*) self
;
262 THPY_REQUIRE_VALID (thread_obj
);
264 if (thread_obj
->thread
->state
== THREAD_STOPPED
)
270 /* Implementation of InferiorThread.is_running () -> Boolean.
271 Return whether the thread is running. */
274 thpy_is_running (PyObject
*self
, PyObject
*args
)
276 thread_object
*thread_obj
= (thread_object
*) self
;
278 THPY_REQUIRE_VALID (thread_obj
);
280 if (thread_obj
->thread
->state
== THREAD_RUNNING
)
286 /* Implementation of InferiorThread.is_exited () -> Boolean.
287 Return whether the thread is exited. */
290 thpy_is_exited (PyObject
*self
, PyObject
*args
)
292 thread_object
*thread_obj
= (thread_object
*) self
;
294 THPY_REQUIRE_VALID (thread_obj
);
296 if (thread_obj
->thread
->state
== THREAD_EXITED
)
302 /* Implementation of gdb.InfThread.is_valid (self) -> Boolean.
303 Returns True if this inferior Thread object still exists
307 thpy_is_valid (PyObject
*self
, PyObject
*args
)
309 thread_object
*thread_obj
= (thread_object
*) self
;
311 if (! thread_obj
->thread
)
317 /* Implementation of gdb.InferiorThread.handle (self) -> handle. */
320 thpy_thread_handle (PyObject
*self
, PyObject
*args
)
322 thread_object
*thread_obj
= (thread_object
*) self
;
323 THPY_REQUIRE_VALID (thread_obj
);
325 gdb::array_view
<const gdb_byte
> hv
;
329 hv
= target_thread_info_to_thread_handle (thread_obj
->thread
);
331 catch (const gdb_exception
&except
)
333 return gdbpy_handle_gdb_exception (nullptr, except
);
338 PyErr_SetString (PyExc_RuntimeError
, _("Thread handle not found."));
342 PyObject
*object
= PyBytes_FromStringAndSize ((const char *) hv
.data (),
347 /* Implement repr() for gdb.InferiorThread. */
350 thpy_repr (PyObject
*self
)
352 thread_object
*thread_obj
= (thread_object
*) self
;
354 if (thread_obj
->thread
== nullptr)
355 return gdb_py_invalid_object_repr (self
);
357 thread_info
*thr
= thread_obj
->thread
;
358 return PyUnicode_FromFormat ("<%s id=%s target-id=\"%s\">",
359 Py_TYPE (self
)->tp_name
,
360 print_full_thread_id (thr
),
361 target_pid_to_str (thr
->ptid
).c_str ());
364 /* Return a reference to a new Python object representing a ptid_t.
365 The object is a tuple containing (pid, lwp, tid). */
367 gdbpy_create_ptid_object (ptid_t ptid
)
374 ret
= PyTuple_New (3);
382 gdbpy_ref
<> pid_obj
= gdb_py_object_from_longest (pid
);
383 if (pid_obj
== nullptr)
385 gdbpy_ref
<> lwp_obj
= gdb_py_object_from_longest (lwp
);
386 if (lwp_obj
== nullptr)
388 gdbpy_ref
<> tid_obj
= gdb_py_object_from_ulongest (tid
);
389 if (tid_obj
== nullptr)
392 /* Note that these steal references, hence the use of 'release'. */
393 PyTuple_SET_ITEM (ret
, 0, pid_obj
.release ());
394 PyTuple_SET_ITEM (ret
, 1, lwp_obj
.release ());
395 PyTuple_SET_ITEM (ret
, 2, tid_obj
.release ());
400 /* Implementation of gdb.selected_thread () -> gdb.InferiorThread.
401 Returns the selected thread object. */
404 gdbpy_selected_thread (PyObject
*self
, PyObject
*args
)
406 if (inferior_ptid
!= null_ptid
)
407 return thread_to_thread_object (inferior_thread ()).release ();
412 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
413 gdbpy_initialize_thread (void)
415 return gdbpy_type_ready (&thread_object_type
);
418 GDBPY_INITIALIZE_FILE (gdbpy_initialize_thread
);
422 static gdb_PyGetSetDef thread_object_getset
[] =
424 { "__dict__", gdb_py_generic_dict
, nullptr,
425 "The __dict__ for this thread.", &thread_object_type
},
426 { "name", thpy_get_name
, thpy_set_name
,
427 "The name of the thread, as set by the user or the OS.", NULL
},
428 { "details", thpy_get_details
, NULL
,
429 "A target specific string containing extra thread state details.",
431 { "num", thpy_get_num
, NULL
,
432 "Per-inferior number of the thread, as assigned by GDB.", NULL
},
433 { "global_num", thpy_get_global_num
, NULL
,
434 "Global number of the thread, as assigned by GDB.", NULL
},
435 { "ptid", thpy_get_ptid
, NULL
, "ID of the thread, as assigned by the OS.",
437 { "ptid_string", thpy_get_ptid_string
, nullptr,
438 "A string representing ptid, as used by, for example, 'info threads'.",
440 { "inferior", thpy_get_inferior
, NULL
,
441 "The Inferior object this thread belongs to.", NULL
},
446 static PyMethodDef thread_object_methods
[] =
448 { "is_valid", thpy_is_valid
, METH_NOARGS
,
449 "is_valid () -> Boolean.\n\
450 Return true if this inferior thread is valid, false if not." },
451 { "switch", thpy_switch
, METH_NOARGS
,
453 Makes this the GDB selected thread." },
454 { "is_stopped", thpy_is_stopped
, METH_NOARGS
,
455 "is_stopped () -> Boolean\n\
456 Return whether the thread is stopped." },
457 { "is_running", thpy_is_running
, METH_NOARGS
,
458 "is_running () -> Boolean\n\
459 Return whether the thread is running." },
460 { "is_exited", thpy_is_exited
, METH_NOARGS
,
461 "is_exited () -> Boolean\n\
462 Return whether the thread is exited." },
463 { "handle", thpy_thread_handle
, METH_NOARGS
,
464 "handle () -> handle\n\
465 Return thread library specific handle for thread." },
470 PyTypeObject thread_object_type
=
472 PyVarObject_HEAD_INIT (NULL
, 0)
473 "gdb.InferiorThread", /*tp_name*/
474 sizeof (thread_object
), /*tp_basicsize*/
476 thpy_dealloc
, /*tp_dealloc*/
481 thpy_repr
, /*tp_repr*/
483 0, /*tp_as_sequence*/
491 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
492 "GDB thread object", /* tp_doc */
495 0, /* tp_richcompare */
496 0, /* tp_weaklistoffset */
499 thread_object_methods
, /* tp_methods */
501 thread_object_getset
, /* tp_getset */
504 0, /* tp_descr_get */
505 0, /* tp_descr_set */
506 offsetof (thread_object
, dict
), /* tp_dictoffset */