1 /* Python interface to inferior threads.
3 Copyright (C) 2009-2022 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 ();
59 thpy_dealloc (PyObject *self)
61 Py_DECREF (((thread_object *) self)->inf_obj);
62 Py_TYPE (self)->tp_free (self);
66 thpy_get_name (PyObject *self, void *ignore)
68 thread_object *thread_obj = (thread_object *) self;
70 THPY_REQUIRE_VALID (thread_obj);
72 const char *name = thread_name (thread_obj->thread);
76 return PyString_FromString (name);
80 thpy_set_name (PyObject *self, PyObject *newvalue, void *ignore)
82 thread_object *thread_obj = (thread_object *) self;
83 gdb::unique_xmalloc_ptr<char> name;
85 if (! thread_obj->thread)
87 PyErr_SetString (PyExc_RuntimeError, _("Thread no longer exists."));
93 PyErr_SetString (PyExc_TypeError,
94 _("Cannot delete `name' attribute."));
97 else if (newvalue == Py_None)
101 else if (! gdbpy_is_string (newvalue))
103 PyErr_SetString (PyExc_TypeError,
104 _("The value of `name' must be a string."));
109 name = python_string_to_host_string (newvalue);
114 thread_obj->thread->set_name (std::move (name));
119 /* Getter for InferiorThread.num. */
122 thpy_get_num (PyObject *self, void *closure)
124 thread_object *thread_obj = (thread_object *) self;
126 THPY_REQUIRE_VALID (thread_obj);
129 = gdb_py_object_from_longest (thread_obj->thread->per_inf_num);
130 return result.release ();
133 /* Getter for InferiorThread.global_num. */
136 thpy_get_global_num (PyObject *self, void *closure)
138 thread_object *thread_obj = (thread_object *) self;
140 THPY_REQUIRE_VALID (thread_obj);
143 = gdb_py_object_from_longest (thread_obj->thread->global_num);
144 return result.release ();
147 /* Getter for InferiorThread.ptid -> (pid, lwp, tid).
148 Returns a tuple with the thread's ptid components. */
151 thpy_get_ptid (PyObject *self, void *closure)
153 thread_object *thread_obj = (thread_object *) self;
155 THPY_REQUIRE_VALID (thread_obj);
157 return gdbpy_create_ptid_object (thread_obj->thread->ptid);
160 /* Getter for InferiorThread.inferior -> Inferior. */
163 thpy_get_inferior (PyObject *self, void *ignore)
165 thread_object *thread_obj = (thread_object *) self;
167 THPY_REQUIRE_VALID (thread_obj);
168 Py_INCREF (thread_obj->inf_obj);
170 return thread_obj->inf_obj;
173 /* Implementation of InferiorThread.switch ().
174 Makes this the GDB selected thread. */
177 thpy_switch (PyObject *self, PyObject *args)
179 thread_object *thread_obj = (thread_object *) self;
181 THPY_REQUIRE_VALID (thread_obj);
185 switch_to_thread (thread_obj->thread);
187 catch (const gdb_exception &except)
189 GDB_PY_HANDLE_EXCEPTION (except);
195 /* Implementation of InferiorThread.is_stopped () -> Boolean.
196 Return whether the thread is stopped. */
199 thpy_is_stopped (PyObject *self, PyObject *args)
201 thread_object *thread_obj = (thread_object *) self;
203 THPY_REQUIRE_VALID (thread_obj);
205 if (thread_obj->thread->state == THREAD_STOPPED)
211 /* Implementation of InferiorThread.is_running () -> Boolean.
212 Return whether the thread is running. */
215 thpy_is_running (PyObject *self, PyObject *args)
217 thread_object *thread_obj = (thread_object *) self;
219 THPY_REQUIRE_VALID (thread_obj);
221 if (thread_obj->thread->state == THREAD_RUNNING)
227 /* Implementation of InferiorThread.is_exited () -> Boolean.
228 Return whether the thread is exited. */
231 thpy_is_exited (PyObject *self, PyObject *args)
233 thread_object *thread_obj = (thread_object *) self;
235 THPY_REQUIRE_VALID (thread_obj);
237 if (thread_obj->thread->state == THREAD_EXITED)
243 /* Implementation of gdb.InfThread.is_valid (self) -> Boolean.
244 Returns True if this inferior Thread object still exists
248 thpy_is_valid (PyObject *self, PyObject *args)
250 thread_object *thread_obj = (thread_object *) self;
252 if (! thread_obj->thread)
258 /* Implementation of gdb.InferiorThread.handle (self) -> handle. */
261 thpy_thread_handle (PyObject *self, PyObject *args)
263 thread_object *thread_obj = (thread_object *) self;
264 THPY_REQUIRE_VALID (thread_obj);
270 hv = target_thread_info_to_thread_handle (thread_obj->thread);
272 catch (const gdb_exception &except)
274 GDB_PY_HANDLE_EXCEPTION (except);
279 PyErr_SetString (PyExc_RuntimeError, _("Thread handle not found."));
283 PyObject *object = PyBytes_FromStringAndSize ((const char *) hv.data (),
288 /* Return a reference to a new Python object representing a ptid_t.
289 The object is a tuple containing (pid, lwp, tid). */
291 gdbpy_create_ptid_object (ptid_t ptid)
298 ret = PyTuple_New (3);
306 gdbpy_ref<> pid_obj = gdb_py_object_from_longest (pid);
307 if (pid_obj == nullptr)
309 gdbpy_ref<> lwp_obj = gdb_py_object_from_longest (lwp);
310 if (lwp_obj == nullptr)
312 gdbpy_ref<> tid_obj = gdb_py_object_from_ulongest (tid);
313 if (tid_obj == nullptr)
316 /* Note that these steal references, hence the use of 'release'. */
317 PyTuple_SET_ITEM (ret, 0, pid_obj.release ());
318 PyTuple_SET_ITEM (ret, 1, lwp_obj.release ());
319 PyTuple_SET_ITEM (ret, 2, tid_obj.release ());
324 /* Implementation of gdb.selected_thread () -> gdb.InferiorThread.
325 Returns the selected thread object. */
328 gdbpy_selected_thread (PyObject *self, PyObject *args)
330 if (inferior_ptid != null_ptid)
331 return thread_to_thread_object (inferior_thread ()).release ();
337 gdbpy_initialize_thread (void)
339 if (PyType_Ready (&thread_object_type) < 0)
342 return gdb_pymodule_addobject (gdb_module, "InferiorThread",
343 (PyObject *) &thread_object_type);
346 static gdb_PyGetSetDef thread_object_getset[] =
348 { "name", thpy_get_name, thpy_set_name,
349 "The name of the thread, as set by the user or the OS.", NULL },
350 { "num", thpy_get_num, NULL,
351 "Per-inferior number of the thread, as assigned by GDB.", NULL },
352 { "global_num", thpy_get_global_num, NULL,
353 "Global number of the thread, as assigned by GDB.", NULL },
354 { "ptid", thpy_get_ptid, NULL, "ID of the thread, as assigned by the OS.",
356 { "inferior", thpy_get_inferior, NULL,
357 "The Inferior object this thread belongs to.", NULL },
362 static PyMethodDef thread_object_methods[] =
364 { "is_valid", thpy_is_valid, METH_NOARGS,
365 "is_valid () -> Boolean.\n\
366 Return true if this inferior thread is valid, false if not." },
367 { "switch", thpy_switch, METH_NOARGS,
369 Makes this the GDB selected thread." },
370 { "is_stopped", thpy_is_stopped, METH_NOARGS,
371 "is_stopped () -> Boolean\n\
372 Return whether the thread is stopped." },
373 { "is_running", thpy_is_running, METH_NOARGS,
374 "is_running () -> Boolean\n\
375 Return whether the thread is running." },
376 { "is_exited", thpy_is_exited, METH_NOARGS,
377 "is_exited () -> Boolean\n\
378 Return whether the thread is exited." },
379 { "handle", thpy_thread_handle, METH_NOARGS,
380 "handle () -> handle\n\
381 Return thread library specific handle for thread." },
386 PyTypeObject thread_object_type =
388 PyVarObject_HEAD_INIT (NULL, 0)
389 "gdb.InferiorThread", /*tp_name*/
390 sizeof (thread_object), /*tp_basicsize*/
392 thpy_dealloc, /*tp_dealloc*/
399 0, /*tp_as_sequence*/
407 Py_TPFLAGS_DEFAULT, /*tp_flags*/
408 "GDB thread object", /* tp_doc */
411 0, /* tp_richcompare */
412 0, /* tp_weaklistoffset */
415 thread_object_methods, /* tp_methods */
417 thread_object_getset, /* tp_getset */
420 0, /* tp_descr_get */
421 0, /* tp_descr_set */
422 0, /* tp_dictoffset */