]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/python/py-infthread.c
[gdb/testsuite] Use support_displaced_stepping in gdb.arch/amd64-disp-step-avx.exp
[thirdparty/binutils-gdb.git] / gdb / python / py-infthread.c
CommitLineData
595939de
PM
1/* Python interface to inferior threads.
2
d01e8234 3 Copyright (C) 2009-2025 Free Software Foundation, Inc.
595939de
PM
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
595939de
PM
20#include "gdbthread.h"
21#include "inferior.h"
22#include "python-internal.h"
23
e36122e9 24extern PyTypeObject thread_object_type
62eec1a5 25 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("thread_object");
595939de
PM
26
27/* Require that INFERIOR be a valid inferior ID. */
28#define THPY_REQUIRE_VALID(Thread) \
29 do { \
30 if (!Thread->thread) \
31 { \
32 PyErr_SetString (PyExc_RuntimeError, \
33 _("Thread no longer exists.")); \
34 return NULL; \
35 } \
36 } while (0)
37
05b08ac1 38gdbpy_ref<thread_object>
595939de
PM
39create_thread_object (struct thread_info *tp)
40{
05b08ac1 41 gdbpy_ref<thread_object> thread_obj;
595939de 42
61fd3e73
TT
43 gdbpy_ref<inferior_object> inf_obj = inferior_to_inferior_object (tp->inf);
44 if (inf_obj == NULL)
45 return NULL;
46
05b08ac1
TT
47 thread_obj.reset (PyObject_New (thread_object, &thread_object_type));
48 if (thread_obj == NULL)
595939de
PM
49 return NULL;
50
51 thread_obj->thread = tp;
61fd3e73 52 thread_obj->inf_obj = (PyObject *) inf_obj.release ();
1d586eda
AB
53 thread_obj->dict = PyDict_New ();
54 if (thread_obj->dict == nullptr)
55 return nullptr;
595939de
PM
56
57 return thread_obj;
58}
59
595939de
PM
60static void
61thpy_dealloc (PyObject *self)
62{
1d586eda
AB
63 thread_object *thr_obj = (thread_object *) self;
64
65 gdb_assert (thr_obj->inf_obj != nullptr);
66
67 Py_DECREF (thr_obj->inf_obj);
68 Py_XDECREF (thr_obj->dict);
69
9a27f2c6 70 Py_TYPE (self)->tp_free (self);
595939de
PM
71}
72
4694da01
TT
73static PyObject *
74thpy_get_name (PyObject *self, void *ignore)
75{
76 thread_object *thread_obj = (thread_object *) self;
4694da01
TT
77
78 THPY_REQUIRE_VALID (thread_obj);
79
25558938 80 const char *name = thread_name (thread_obj->thread);
4694da01
TT
81 if (name == NULL)
82 Py_RETURN_NONE;
83
5aee4587 84 return PyUnicode_FromString (name);
4694da01
TT
85}
86
659971cb
AB
87/* Return a string containing target specific additional information about
88 the state of the thread, or None, if there is no such additional
89 information. */
90
91static PyObject *
92thpy_get_details (PyObject *self, void *ignore)
93{
94 thread_object *thread_obj = (thread_object *) self;
95
96 THPY_REQUIRE_VALID (thread_obj);
97
29928b8e
TT
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;
659971cb
AB
101 try
102 {
103 extra_info = target_extra_thread_info (thread_obj->thread);
104 }
105 catch (const gdb_exception &except)
106 {
1ccb6f10 107 return gdbpy_handle_gdb_exception (nullptr, except);
659971cb
AB
108 }
109 if (extra_info == nullptr)
110 Py_RETURN_NONE;
111
5aee4587 112 return PyUnicode_FromString (extra_info);
659971cb
AB
113}
114
4694da01
TT
115static int
116thpy_set_name (PyObject *self, PyObject *newvalue, void *ignore)
117{
118 thread_object *thread_obj = (thread_object *) self;
9b972014 119 gdb::unique_xmalloc_ptr<char> name;
4694da01
TT
120
121 if (! thread_obj->thread)
122 {
123 PyErr_SetString (PyExc_RuntimeError, _("Thread no longer exists."));
124 return -1;
125 }
126
127 if (newvalue == NULL)
128 {
256458bc 129 PyErr_SetString (PyExc_TypeError,
4694da01
TT
130 _("Cannot delete `name' attribute."));
131 return -1;
132 }
133 else if (newvalue == Py_None)
9b972014
TT
134 {
135 /* Nothing. */
136 }
4694da01
TT
137 else if (! gdbpy_is_string (newvalue))
138 {
139 PyErr_SetString (PyExc_TypeError,
140 _("The value of `name' must be a string."));
141 return -1;
142 }
143 else
144 {
145 name = python_string_to_host_string (newvalue);
146 if (! name)
147 return -1;
148 }
149
25558938 150 thread_obj->thread->set_name (std::move (name));
4694da01
TT
151
152 return 0;
153}
154
5d5658a1
PA
155/* Getter for InferiorThread.num. */
156
595939de
PM
157static PyObject *
158thpy_get_num (PyObject *self, void *closure)
159{
160 thread_object *thread_obj = (thread_object *) self;
161
162 THPY_REQUIRE_VALID (thread_obj);
163
062534d4
TT
164 gdbpy_ref<> result
165 = gdb_py_object_from_longest (thread_obj->thread->per_inf_num);
166 return result.release ();
595939de
PM
167}
168
22a02324
PA
169/* Getter for InferiorThread.global_num. */
170
171static PyObject *
172thpy_get_global_num (PyObject *self, void *closure)
173{
174 thread_object *thread_obj = (thread_object *) self;
175
176 THPY_REQUIRE_VALID (thread_obj);
177
062534d4
TT
178 gdbpy_ref<> result
179 = gdb_py_object_from_longest (thread_obj->thread->global_num);
180 return result.release ();
22a02324
PA
181}
182
595939de
PM
183/* Getter for InferiorThread.ptid -> (pid, lwp, tid).
184 Returns a tuple with the thread's ptid components. */
007baf27 185
595939de
PM
186static PyObject *
187thpy_get_ptid (PyObject *self, void *closure)
188{
595939de 189 thread_object *thread_obj = (thread_object *) self;
595939de
PM
190
191 THPY_REQUIRE_VALID (thread_obj);
192
162078c8 193 return gdbpy_create_ptid_object (thread_obj->thread->ptid);
595939de
PM
194}
195
76118e16
AB
196/* Implement gdb.InferiorThread.ptid_string attribute. */
197
198static PyObject *
199thpy_get_ptid_string (PyObject *self, void *closure)
200{
201 thread_object *thread_obj = (thread_object *) self;
202 THPY_REQUIRE_VALID (thread_obj);
203 ptid_t ptid = thread_obj->thread->ptid;
204
205 try
206 {
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 ());
212 }
213 catch (const gdb_exception &except)
214 {
1ccb6f10 215 return gdbpy_handle_gdb_exception (nullptr, except);
76118e16
AB
216 }
217}
218
84654457
PA
219/* Getter for InferiorThread.inferior -> Inferior. */
220
221static PyObject *
222thpy_get_inferior (PyObject *self, void *ignore)
223{
224 thread_object *thread_obj = (thread_object *) self;
225
226 THPY_REQUIRE_VALID (thread_obj);
484d8d36 227 Py_INCREF (thread_obj->inf_obj);
84654457
PA
228
229 return thread_obj->inf_obj;
230}
231
595939de
PM
232/* Implementation of InferiorThread.switch ().
233 Makes this the GDB selected thread. */
007baf27 234
595939de
PM
235static PyObject *
236thpy_switch (PyObject *self, PyObject *args)
237{
238 thread_object *thread_obj = (thread_object *) self;
595939de
PM
239
240 THPY_REQUIRE_VALID (thread_obj);
241
a70b8144 242 try
595939de 243 {
00431a78 244 switch_to_thread (thread_obj->thread);
595939de 245 }
230d2906 246 catch (const gdb_exception &except)
492d29ea 247 {
1ccb6f10 248 return gdbpy_handle_gdb_exception (nullptr, except);
492d29ea 249 }
595939de
PM
250
251 Py_RETURN_NONE;
252}
253
254/* Implementation of InferiorThread.is_stopped () -> Boolean.
255 Return whether the thread is stopped. */
007baf27 256
595939de
PM
257static PyObject *
258thpy_is_stopped (PyObject *self, PyObject *args)
259{
260 thread_object *thread_obj = (thread_object *) self;
261
262 THPY_REQUIRE_VALID (thread_obj);
263
00431a78 264 if (thread_obj->thread->state == THREAD_STOPPED)
595939de
PM
265 Py_RETURN_TRUE;
266
267 Py_RETURN_FALSE;
268}
269
270/* Implementation of InferiorThread.is_running () -> Boolean.
271 Return whether the thread is running. */
007baf27 272
595939de
PM
273static PyObject *
274thpy_is_running (PyObject *self, PyObject *args)
275{
276 thread_object *thread_obj = (thread_object *) self;
277
278 THPY_REQUIRE_VALID (thread_obj);
279
00431a78 280 if (thread_obj->thread->state == THREAD_RUNNING)
595939de
PM
281 Py_RETURN_TRUE;
282
283 Py_RETURN_FALSE;
284}
285
286/* Implementation of InferiorThread.is_exited () -> Boolean.
287 Return whether the thread is exited. */
007baf27 288
595939de
PM
289static PyObject *
290thpy_is_exited (PyObject *self, PyObject *args)
291{
292 thread_object *thread_obj = (thread_object *) self;
293
294 THPY_REQUIRE_VALID (thread_obj);
295
00431a78 296 if (thread_obj->thread->state == THREAD_EXITED)
595939de
PM
297 Py_RETURN_TRUE;
298
299 Py_RETURN_FALSE;
300}
301
29703da4
PM
302/* Implementation of gdb.InfThread.is_valid (self) -> Boolean.
303 Returns True if this inferior Thread object still exists
304 in GDB. */
595939de 305
29703da4
PM
306static PyObject *
307thpy_is_valid (PyObject *self, PyObject *args)
308{
309 thread_object *thread_obj = (thread_object *) self;
310
311 if (! thread_obj->thread)
312 Py_RETURN_FALSE;
313
314 Py_RETURN_TRUE;
315}
595939de 316
cf63b016
KB
317/* Implementation of gdb.InferiorThread.handle (self) -> handle. */
318
319static PyObject *
320thpy_thread_handle (PyObject *self, PyObject *args)
321{
322 thread_object *thread_obj = (thread_object *) self;
323 THPY_REQUIRE_VALID (thread_obj);
324
1f08d324
TV
325 gdb::array_view<const gdb_byte> hv;
326
cf63b016
KB
327 try
328 {
329 hv = target_thread_info_to_thread_handle (thread_obj->thread);
330 }
331 catch (const gdb_exception &except)
332 {
1ccb6f10 333 return gdbpy_handle_gdb_exception (nullptr, except);
cf63b016
KB
334 }
335
336 if (hv.size () == 0)
337 {
338 PyErr_SetString (PyExc_RuntimeError, _("Thread handle not found."));
339 return NULL;
340 }
341
342 PyObject *object = PyBytes_FromStringAndSize ((const char *) hv.data (),
dda83cd7 343 hv.size());
cf63b016
KB
344 return object;
345}
346
1925bba8
AB
347/* Implement repr() for gdb.InferiorThread. */
348
349static PyObject *
350thpy_repr (PyObject *self)
351{
352 thread_object *thread_obj = (thread_object *) self;
353
354 if (thread_obj->thread == nullptr)
355 return gdb_py_invalid_object_repr (self);
356
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 ());
362}
363
162078c8
NB
364/* Return a reference to a new Python object representing a ptid_t.
365 The object is a tuple containing (pid, lwp, tid). */
366PyObject *
367gdbpy_create_ptid_object (ptid_t ptid)
368{
369 int pid;
96bbe3ef
TT
370 long lwp;
371 ULONGEST tid;
162078c8
NB
372 PyObject *ret;
373
374 ret = PyTuple_New (3);
375 if (!ret)
376 return NULL;
377
e99b03dc 378 pid = ptid.pid ();
e38504b3 379 lwp = ptid.lwp ();
cc6bcb54 380 tid = ptid.tid ();
162078c8 381
47f0e2ff
TT
382 gdbpy_ref<> pid_obj = gdb_py_object_from_longest (pid);
383 if (pid_obj == nullptr)
384 return nullptr;
385 gdbpy_ref<> lwp_obj = gdb_py_object_from_longest (lwp);
386 if (lwp_obj == nullptr)
387 return nullptr;
96bbe3ef 388 gdbpy_ref<> tid_obj = gdb_py_object_from_ulongest (tid);
47f0e2ff
TT
389 if (tid_obj == nullptr)
390 return nullptr;
391
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 ());
396
162078c8
NB
397 return ret;
398}
399
595939de
PM
400/* Implementation of gdb.selected_thread () -> gdb.InferiorThread.
401 Returns the selected thread object. */
007baf27 402
595939de
PM
403PyObject *
404gdbpy_selected_thread (PyObject *self, PyObject *args)
405{
00431a78 406 if (inferior_ptid != null_ptid)
db1337cc 407 return thread_to_thread_object (inferior_thread ()).release ();
595939de
PM
408
409 Py_RETURN_NONE;
410}
411
3965bff5 412static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
595939de
PM
413gdbpy_initialize_thread (void)
414{
336bb2a1 415 return gdbpy_type_ready (&thread_object_type);
595939de
PM
416}
417
3965bff5
AB
418GDBPY_INITIALIZE_FILE (gdbpy_initialize_thread);
419
420\f
421
0d1f4ceb 422static gdb_PyGetSetDef thread_object_getset[] =
595939de 423{
1d586eda
AB
424 { "__dict__", gdb_py_generic_dict, nullptr,
425 "The __dict__ for this thread.", &thread_object_type },
4694da01
TT
426 { "name", thpy_get_name, thpy_set_name,
427 "The name of the thread, as set by the user or the OS.", NULL },
659971cb
AB
428 { "details", thpy_get_details, NULL,
429 "A target specific string containing extra thread state details.",
430 NULL },
5d5658a1
PA
431 { "num", thpy_get_num, NULL,
432 "Per-inferior number of the thread, as assigned by GDB.", NULL },
22a02324
PA
433 { "global_num", thpy_get_global_num, NULL,
434 "Global number of the thread, as assigned by GDB.", NULL },
595939de
PM
435 { "ptid", thpy_get_ptid, NULL, "ID of the thread, as assigned by the OS.",
436 NULL },
76118e16
AB
437 { "ptid_string", thpy_get_ptid_string, nullptr,
438 "A string representing ptid, as used by, for example, 'info threads'.",
439 nullptr },
84654457
PA
440 { "inferior", thpy_get_inferior, NULL,
441 "The Inferior object this thread belongs to.", NULL },
595939de
PM
442
443 { NULL }
444};
445
446static PyMethodDef thread_object_methods[] =
447{
29703da4
PM
448 { "is_valid", thpy_is_valid, METH_NOARGS,
449 "is_valid () -> Boolean.\n\
450Return true if this inferior thread is valid, false if not." },
595939de
PM
451 { "switch", thpy_switch, METH_NOARGS,
452 "switch ()\n\
453Makes this the GDB selected thread." },
454 { "is_stopped", thpy_is_stopped, METH_NOARGS,
455 "is_stopped () -> Boolean\n\
456Return whether the thread is stopped." },
457 { "is_running", thpy_is_running, METH_NOARGS,
458 "is_running () -> Boolean\n\
459Return whether the thread is running." },
460 { "is_exited", thpy_is_exited, METH_NOARGS,
461 "is_exited () -> Boolean\n\
462Return whether the thread is exited." },
cf63b016
KB
463 { "handle", thpy_thread_handle, METH_NOARGS,
464 "handle () -> handle\n\
465Return thread library specific handle for thread." },
595939de
PM
466
467 { NULL }
468};
469
e36122e9 470PyTypeObject thread_object_type =
595939de 471{
9a27f2c6 472 PyVarObject_HEAD_INIT (NULL, 0)
595939de
PM
473 "gdb.InferiorThread", /*tp_name*/
474 sizeof (thread_object), /*tp_basicsize*/
475 0, /*tp_itemsize*/
476 thpy_dealloc, /*tp_dealloc*/
477 0, /*tp_print*/
478 0, /*tp_getattr*/
479 0, /*tp_setattr*/
480 0, /*tp_compare*/
1925bba8 481 thpy_repr, /*tp_repr*/
595939de
PM
482 0, /*tp_as_number*/
483 0, /*tp_as_sequence*/
484 0, /*tp_as_mapping*/
485 0, /*tp_hash */
486 0, /*tp_call*/
487 0, /*tp_str*/
488 0, /*tp_getattro*/
489 0, /*tp_setattro*/
490 0, /*tp_as_buffer*/
0b233e34 491 Py_TPFLAGS_DEFAULT, /*tp_flags*/
595939de
PM
492 "GDB thread object", /* tp_doc */
493 0, /* tp_traverse */
494 0, /* tp_clear */
495 0, /* tp_richcompare */
496 0, /* tp_weaklistoffset */
497 0, /* tp_iter */
498 0, /* tp_iternext */
499 thread_object_methods, /* tp_methods */
500 0, /* tp_members */
501 thread_object_getset, /* tp_getset */
502 0, /* tp_base */
503 0, /* tp_dict */
504 0, /* tp_descr_get */
505 0, /* tp_descr_set */
1d586eda 506 offsetof (thread_object, dict), /* tp_dictoffset */
595939de
PM
507 0, /* tp_init */
508 0 /* tp_alloc */
509};