]> Git Repo - binutils.git/blob - gdb/python/py-infthread.c
gdb: remove SYMTAB_COMPUNIT macro, add getter/setter
[binutils.git] / gdb / python / py-infthread.c
1 /* Python interface to inferior threads.
2
3    Copyright (C) 2009-2022 Free Software Foundation, Inc.
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
20 #include "defs.h"
21 #include "gdbthread.h"
22 #include "inferior.h"
23 #include "python-internal.h"
24
25 extern PyTypeObject thread_object_type
26     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("thread_object");
27
28 /* Require that INFERIOR be a valid inferior ID.  */
29 #define THPY_REQUIRE_VALID(Thread)                              \
30   do {                                                          \
31     if (!Thread->thread)                                        \
32       {                                                         \
33         PyErr_SetString (PyExc_RuntimeError,                    \
34                          _("Thread no longer exists."));        \
35         return NULL;                                            \
36       }                                                         \
37   } while (0)
38
39 gdbpy_ref<thread_object>
40 create_thread_object (struct thread_info *tp)
41 {
42   gdbpy_ref<thread_object> thread_obj;
43
44   gdbpy_ref<inferior_object> inf_obj = inferior_to_inferior_object (tp->inf);
45   if (inf_obj == NULL)
46     return NULL;
47
48   thread_obj.reset (PyObject_New (thread_object, &thread_object_type));
49   if (thread_obj == NULL)
50     return NULL;
51
52   thread_obj->thread = tp;
53   thread_obj->inf_obj = (PyObject *) inf_obj.release ();
54
55   return thread_obj;
56 }
57
58 static void
59 thpy_dealloc (PyObject *self)
60 {
61   Py_DECREF (((thread_object *) self)->inf_obj);
62   Py_TYPE (self)->tp_free (self);
63 }
64
65 static PyObject *
66 thpy_get_name (PyObject *self, void *ignore)
67 {
68   thread_object *thread_obj = (thread_object *) self;
69
70   THPY_REQUIRE_VALID (thread_obj);
71
72   const char *name = thread_name (thread_obj->thread);
73   if (name == NULL)
74     Py_RETURN_NONE;
75
76   return PyString_FromString (name);
77 }
78
79 static int
80 thpy_set_name (PyObject *self, PyObject *newvalue, void *ignore)
81 {
82   thread_object *thread_obj = (thread_object *) self;
83   gdb::unique_xmalloc_ptr<char> name;
84
85   if (! thread_obj->thread)
86     {
87       PyErr_SetString (PyExc_RuntimeError, _("Thread no longer exists."));
88       return -1;
89     }
90
91   if (newvalue == NULL)
92     {
93       PyErr_SetString (PyExc_TypeError,
94                        _("Cannot delete `name' attribute."));
95       return -1;
96     }
97   else if (newvalue == Py_None)
98     {
99       /* Nothing.  */
100     }
101   else if (! gdbpy_is_string (newvalue))
102     {
103       PyErr_SetString (PyExc_TypeError,
104                        _("The value of `name' must be a string."));
105       return -1;
106     }
107   else
108     {
109       name = python_string_to_host_string (newvalue);
110       if (! name)
111         return -1;
112     }
113
114   thread_obj->thread->set_name (std::move (name));
115
116   return 0;
117 }
118
119 /* Getter for InferiorThread.num.  */
120
121 static PyObject *
122 thpy_get_num (PyObject *self, void *closure)
123 {
124   thread_object *thread_obj = (thread_object *) self;
125
126   THPY_REQUIRE_VALID (thread_obj);
127
128   gdbpy_ref<> result
129     = gdb_py_object_from_longest (thread_obj->thread->per_inf_num);
130   return result.release ();
131 }
132
133 /* Getter for InferiorThread.global_num.  */
134
135 static PyObject *
136 thpy_get_global_num (PyObject *self, void *closure)
137 {
138   thread_object *thread_obj = (thread_object *) self;
139
140   THPY_REQUIRE_VALID (thread_obj);
141
142   gdbpy_ref<> result
143     = gdb_py_object_from_longest (thread_obj->thread->global_num);
144   return result.release ();
145 }
146
147 /* Getter for InferiorThread.ptid  -> (pid, lwp, tid).
148    Returns a tuple with the thread's ptid components.  */
149
150 static PyObject *
151 thpy_get_ptid (PyObject *self, void *closure)
152 {
153   thread_object *thread_obj = (thread_object *) self;
154
155   THPY_REQUIRE_VALID (thread_obj);
156
157   return gdbpy_create_ptid_object (thread_obj->thread->ptid);
158 }
159
160 /* Getter for InferiorThread.inferior -> Inferior.  */
161
162 static PyObject *
163 thpy_get_inferior (PyObject *self, void *ignore)
164 {
165   thread_object *thread_obj = (thread_object *) self;
166
167   THPY_REQUIRE_VALID (thread_obj);
168   Py_INCREF (thread_obj->inf_obj);
169
170   return thread_obj->inf_obj;
171 }
172
173 /* Implementation of InferiorThread.switch ().
174    Makes this the GDB selected thread.  */
175
176 static PyObject *
177 thpy_switch (PyObject *self, PyObject *args)
178 {
179   thread_object *thread_obj = (thread_object *) self;
180
181   THPY_REQUIRE_VALID (thread_obj);
182
183   try
184     {
185       switch_to_thread (thread_obj->thread);
186     }
187   catch (const gdb_exception &except)
188     {
189       GDB_PY_HANDLE_EXCEPTION (except);
190     }
191
192   Py_RETURN_NONE;
193 }
194
195 /* Implementation of InferiorThread.is_stopped () -> Boolean.
196    Return whether the thread is stopped.  */
197
198 static PyObject *
199 thpy_is_stopped (PyObject *self, PyObject *args)
200 {
201   thread_object *thread_obj = (thread_object *) self;
202
203   THPY_REQUIRE_VALID (thread_obj);
204
205   if (thread_obj->thread->state == THREAD_STOPPED)
206     Py_RETURN_TRUE;
207
208   Py_RETURN_FALSE;
209 }
210
211 /* Implementation of InferiorThread.is_running () -> Boolean.
212    Return whether the thread is running.  */
213
214 static PyObject *
215 thpy_is_running (PyObject *self, PyObject *args)
216 {
217   thread_object *thread_obj = (thread_object *) self;
218
219   THPY_REQUIRE_VALID (thread_obj);
220
221   if (thread_obj->thread->state == THREAD_RUNNING)
222     Py_RETURN_TRUE;
223
224   Py_RETURN_FALSE;
225 }
226
227 /* Implementation of InferiorThread.is_exited () -> Boolean.
228    Return whether the thread is exited.  */
229
230 static PyObject *
231 thpy_is_exited (PyObject *self, PyObject *args)
232 {
233   thread_object *thread_obj = (thread_object *) self;
234
235   THPY_REQUIRE_VALID (thread_obj);
236
237   if (thread_obj->thread->state == THREAD_EXITED)
238     Py_RETURN_TRUE;
239
240   Py_RETURN_FALSE;
241 }
242
243 /* Implementation of gdb.InfThread.is_valid (self) -> Boolean.
244    Returns True if this inferior Thread object still exists
245    in GDB.  */
246
247 static PyObject *
248 thpy_is_valid (PyObject *self, PyObject *args)
249 {
250   thread_object *thread_obj = (thread_object *) self;
251
252   if (! thread_obj->thread)
253     Py_RETURN_FALSE;
254
255   Py_RETURN_TRUE;
256 }
257
258 /* Implementation of gdb.InferiorThread.handle (self) -> handle. */
259
260 static PyObject *
261 thpy_thread_handle (PyObject *self, PyObject *args)
262 {
263   thread_object *thread_obj = (thread_object *) self;
264   THPY_REQUIRE_VALID (thread_obj);
265
266   gdb::byte_vector hv;
267   
268   try
269     {
270       hv = target_thread_info_to_thread_handle (thread_obj->thread);
271     }
272   catch (const gdb_exception &except)
273     {
274       GDB_PY_HANDLE_EXCEPTION (except);
275     }
276
277   if (hv.size () == 0)
278     {
279       PyErr_SetString (PyExc_RuntimeError, _("Thread handle not found."));
280       return NULL;
281     }
282
283   PyObject *object = PyBytes_FromStringAndSize ((const char *) hv.data (),
284                                                 hv.size());
285   return object;
286 }
287
288 /* Return a reference to a new Python object representing a ptid_t.
289    The object is a tuple containing (pid, lwp, tid). */
290 PyObject *
291 gdbpy_create_ptid_object (ptid_t ptid)
292 {
293   int pid;
294   long lwp;
295   ULONGEST tid;
296   PyObject *ret;
297
298   ret = PyTuple_New (3);
299   if (!ret)
300     return NULL;
301
302   pid = ptid.pid ();
303   lwp = ptid.lwp ();
304   tid = ptid.tid ();
305
306   gdbpy_ref<> pid_obj = gdb_py_object_from_longest (pid);
307   if (pid_obj == nullptr)
308     return nullptr;
309   gdbpy_ref<> lwp_obj = gdb_py_object_from_longest (lwp);
310   if (lwp_obj == nullptr)
311     return nullptr;
312   gdbpy_ref<> tid_obj = gdb_py_object_from_ulongest (tid);
313   if (tid_obj == nullptr)
314     return nullptr;
315
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 ());
320
321   return ret;
322 }
323
324 /* Implementation of gdb.selected_thread () -> gdb.InferiorThread.
325    Returns the selected thread object.  */
326
327 PyObject *
328 gdbpy_selected_thread (PyObject *self, PyObject *args)
329 {
330   if (inferior_ptid != null_ptid)
331     return thread_to_thread_object (inferior_thread ()).release ();
332
333   Py_RETURN_NONE;
334 }
335
336 int
337 gdbpy_initialize_thread (void)
338 {
339   if (PyType_Ready (&thread_object_type) < 0)
340     return -1;
341
342   return gdb_pymodule_addobject (gdb_module, "InferiorThread",
343                                  (PyObject *) &thread_object_type);
344 }
345
346 static gdb_PyGetSetDef thread_object_getset[] =
347 {
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.",
355     NULL },
356   { "inferior", thpy_get_inferior, NULL,
357     "The Inferior object this thread belongs to.", NULL },
358
359   { NULL }
360 };
361
362 static PyMethodDef thread_object_methods[] =
363 {
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,
368     "switch ()\n\
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." },
382
383   { NULL }
384 };
385
386 PyTypeObject thread_object_type =
387 {
388   PyVarObject_HEAD_INIT (NULL, 0)
389   "gdb.InferiorThread",           /*tp_name*/
390   sizeof (thread_object),         /*tp_basicsize*/
391   0,                              /*tp_itemsize*/
392   thpy_dealloc,                   /*tp_dealloc*/
393   0,                              /*tp_print*/
394   0,                              /*tp_getattr*/
395   0,                              /*tp_setattr*/
396   0,                              /*tp_compare*/
397   0,                              /*tp_repr*/
398   0,                              /*tp_as_number*/
399   0,                              /*tp_as_sequence*/
400   0,                              /*tp_as_mapping*/
401   0,                              /*tp_hash */
402   0,                              /*tp_call*/
403   0,                              /*tp_str*/
404   0,                              /*tp_getattro*/
405   0,                              /*tp_setattro*/
406   0,                              /*tp_as_buffer*/
407   Py_TPFLAGS_DEFAULT,             /*tp_flags*/
408   "GDB thread object",            /* tp_doc */
409   0,                              /* tp_traverse */
410   0,                              /* tp_clear */
411   0,                              /* tp_richcompare */
412   0,                              /* tp_weaklistoffset */
413   0,                              /* tp_iter */
414   0,                              /* tp_iternext */
415   thread_object_methods,          /* tp_methods */
416   0,                              /* tp_members */
417   thread_object_getset,           /* tp_getset */
418   0,                              /* tp_base */
419   0,                              /* tp_dict */
420   0,                              /* tp_descr_get */
421   0,                              /* tp_descr_set */
422   0,                              /* tp_dictoffset */
423   0,                              /* tp_init */
424   0                               /* tp_alloc */
425 };
This page took 0.048561 seconds and 4 git commands to generate.