Add a new $_inferior convenience variable
[deliverable/binutils-gdb.git] / gdb / python / py-infthread.c
CommitLineData
595939de
PM
1/* Python interface to inferior threads.
2
618f726f 3 Copyright (C) 2009-2016 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
20#include "defs.h"
595939de
PM
21#include "gdbthread.h"
22#include "inferior.h"
23#include "python-internal.h"
24
e36122e9 25extern PyTypeObject thread_object_type
62eec1a5 26 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("thread_object");
595939de
PM
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
595939de
PM
39thread_object *
40create_thread_object (struct thread_info *tp)
41{
42 thread_object *thread_obj;
43
44 thread_obj = PyObject_New (thread_object, &thread_object_type);
45 if (!thread_obj)
46 return NULL;
47
48 thread_obj->thread = tp;
dfd4cc63 49 thread_obj->inf_obj = find_inferior_object (ptid_get_pid (tp->ptid));
595939de
PM
50
51 return thread_obj;
52}
53
595939de
PM
54static void
55thpy_dealloc (PyObject *self)
56{
57 Py_DECREF (((thread_object *) self)->inf_obj);
9a27f2c6 58 Py_TYPE (self)->tp_free (self);
595939de
PM
59}
60
4694da01
TT
61static PyObject *
62thpy_get_name (PyObject *self, void *ignore)
63{
64 thread_object *thread_obj = (thread_object *) self;
73ede765 65 const char *name;
4694da01
TT
66
67 THPY_REQUIRE_VALID (thread_obj);
68
69 name = thread_obj->thread->name;
70 if (name == NULL)
71 name = target_thread_name (thread_obj->thread);
72
73 if (name == NULL)
74 Py_RETURN_NONE;
75
76 return PyString_FromString (name);
77}
78
79static int
80thpy_set_name (PyObject *self, PyObject *newvalue, void *ignore)
81{
82 thread_object *thread_obj = (thread_object *) self;
83 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 {
256458bc 93 PyErr_SetString (PyExc_TypeError,
4694da01
TT
94 _("Cannot delete `name' attribute."));
95 return -1;
96 }
97 else if (newvalue == Py_None)
98 name = NULL;
99 else if (! gdbpy_is_string (newvalue))
100 {
101 PyErr_SetString (PyExc_TypeError,
102 _("The value of `name' must be a string."));
103 return -1;
104 }
105 else
106 {
107 name = python_string_to_host_string (newvalue);
108 if (! name)
109 return -1;
110 }
111
112 xfree (thread_obj->thread->name);
113 thread_obj->thread->name = name;
114
115 return 0;
116}
117
595939de
PM
118static PyObject *
119thpy_get_num (PyObject *self, void *closure)
120{
121 thread_object *thread_obj = (thread_object *) self;
122
123 THPY_REQUIRE_VALID (thread_obj);
124
125 return PyLong_FromLong (thread_obj->thread->num);
126}
127
128/* Getter for InferiorThread.ptid -> (pid, lwp, tid).
129 Returns a tuple with the thread's ptid components. */
007baf27 130
595939de
PM
131static PyObject *
132thpy_get_ptid (PyObject *self, void *closure)
133{
134 int pid;
135 long tid, lwp;
136 thread_object *thread_obj = (thread_object *) self;
595939de
PM
137
138 THPY_REQUIRE_VALID (thread_obj);
139
162078c8 140 return gdbpy_create_ptid_object (thread_obj->thread->ptid);
595939de
PM
141}
142
143/* Implementation of InferiorThread.switch ().
144 Makes this the GDB selected thread. */
007baf27 145
595939de
PM
146static PyObject *
147thpy_switch (PyObject *self, PyObject *args)
148{
149 thread_object *thread_obj = (thread_object *) self;
595939de
PM
150
151 THPY_REQUIRE_VALID (thread_obj);
152
492d29ea 153 TRY
595939de
PM
154 {
155 switch_to_thread (thread_obj->thread->ptid);
156 }
492d29ea
PA
157 CATCH (except, RETURN_MASK_ALL)
158 {
159 GDB_PY_HANDLE_EXCEPTION (except);
160 }
161 END_CATCH
595939de
PM
162
163 Py_RETURN_NONE;
164}
165
166/* Implementation of InferiorThread.is_stopped () -> Boolean.
167 Return whether the thread is stopped. */
007baf27 168
595939de
PM
169static PyObject *
170thpy_is_stopped (PyObject *self, PyObject *args)
171{
172 thread_object *thread_obj = (thread_object *) self;
173
174 THPY_REQUIRE_VALID (thread_obj);
175
176 if (is_stopped (thread_obj->thread->ptid))
177 Py_RETURN_TRUE;
178
179 Py_RETURN_FALSE;
180}
181
182/* Implementation of InferiorThread.is_running () -> Boolean.
183 Return whether the thread is running. */
007baf27 184
595939de
PM
185static PyObject *
186thpy_is_running (PyObject *self, PyObject *args)
187{
188 thread_object *thread_obj = (thread_object *) self;
189
190 THPY_REQUIRE_VALID (thread_obj);
191
192 if (is_running (thread_obj->thread->ptid))
193 Py_RETURN_TRUE;
194
195 Py_RETURN_FALSE;
196}
197
198/* Implementation of InferiorThread.is_exited () -> Boolean.
199 Return whether the thread is exited. */
007baf27 200
595939de
PM
201static PyObject *
202thpy_is_exited (PyObject *self, PyObject *args)
203{
204 thread_object *thread_obj = (thread_object *) self;
205
206 THPY_REQUIRE_VALID (thread_obj);
207
208 if (is_exited (thread_obj->thread->ptid))
209 Py_RETURN_TRUE;
210
211 Py_RETURN_FALSE;
212}
213
29703da4
PM
214/* Implementation of gdb.InfThread.is_valid (self) -> Boolean.
215 Returns True if this inferior Thread object still exists
216 in GDB. */
595939de 217
29703da4
PM
218static PyObject *
219thpy_is_valid (PyObject *self, PyObject *args)
220{
221 thread_object *thread_obj = (thread_object *) self;
222
223 if (! thread_obj->thread)
224 Py_RETURN_FALSE;
225
226 Py_RETURN_TRUE;
227}
595939de 228
162078c8
NB
229/* Return a reference to a new Python object representing a ptid_t.
230 The object is a tuple containing (pid, lwp, tid). */
231PyObject *
232gdbpy_create_ptid_object (ptid_t ptid)
233{
234 int pid;
235 long tid, lwp;
236 PyObject *ret;
237
238 ret = PyTuple_New (3);
239 if (!ret)
240 return NULL;
241
242 pid = ptid_get_pid (ptid);
243 lwp = ptid_get_lwp (ptid);
244 tid = ptid_get_tid (ptid);
245
246 PyTuple_SET_ITEM (ret, 0, PyInt_FromLong (pid));
247 PyTuple_SET_ITEM (ret, 1, PyInt_FromLong (lwp));
248 PyTuple_SET_ITEM (ret, 2, PyInt_FromLong (tid));
249
250 return ret;
251}
252
595939de
PM
253/* Implementation of gdb.selected_thread () -> gdb.InferiorThread.
254 Returns the selected thread object. */
007baf27 255
595939de
PM
256PyObject *
257gdbpy_selected_thread (PyObject *self, PyObject *args)
258{
259 PyObject *thread_obj;
260
261 thread_obj = (PyObject *) find_thread_object (inferior_ptid);
262 if (thread_obj)
263 {
264 Py_INCREF (thread_obj);
265 return thread_obj;
266 }
267
268 Py_RETURN_NONE;
269}
270
999633ed 271int
595939de
PM
272gdbpy_initialize_thread (void)
273{
274 if (PyType_Ready (&thread_object_type) < 0)
999633ed 275 return -1;
595939de 276
aa36459a
TT
277 return gdb_pymodule_addobject (gdb_module, "InferiorThread",
278 (PyObject *) &thread_object_type);
595939de
PM
279}
280
595939de
PM
281static PyGetSetDef thread_object_getset[] =
282{
4694da01
TT
283 { "name", thpy_get_name, thpy_set_name,
284 "The name of the thread, as set by the user or the OS.", NULL },
595939de
PM
285 { "num", thpy_get_num, NULL, "ID of the thread, as assigned by GDB.", NULL },
286 { "ptid", thpy_get_ptid, NULL, "ID of the thread, as assigned by the OS.",
287 NULL },
288
289 { NULL }
290};
291
292static PyMethodDef thread_object_methods[] =
293{
29703da4
PM
294 { "is_valid", thpy_is_valid, METH_NOARGS,
295 "is_valid () -> Boolean.\n\
296Return true if this inferior thread is valid, false if not." },
595939de
PM
297 { "switch", thpy_switch, METH_NOARGS,
298 "switch ()\n\
299Makes this the GDB selected thread." },
300 { "is_stopped", thpy_is_stopped, METH_NOARGS,
301 "is_stopped () -> Boolean\n\
302Return whether the thread is stopped." },
303 { "is_running", thpy_is_running, METH_NOARGS,
304 "is_running () -> Boolean\n\
305Return whether the thread is running." },
306 { "is_exited", thpy_is_exited, METH_NOARGS,
307 "is_exited () -> Boolean\n\
308Return whether the thread is exited." },
309
310 { NULL }
311};
312
e36122e9 313PyTypeObject thread_object_type =
595939de 314{
9a27f2c6 315 PyVarObject_HEAD_INIT (NULL, 0)
595939de
PM
316 "gdb.InferiorThread", /*tp_name*/
317 sizeof (thread_object), /*tp_basicsize*/
318 0, /*tp_itemsize*/
319 thpy_dealloc, /*tp_dealloc*/
320 0, /*tp_print*/
321 0, /*tp_getattr*/
322 0, /*tp_setattr*/
323 0, /*tp_compare*/
324 0, /*tp_repr*/
325 0, /*tp_as_number*/
326 0, /*tp_as_sequence*/
327 0, /*tp_as_mapping*/
328 0, /*tp_hash */
329 0, /*tp_call*/
330 0, /*tp_str*/
331 0, /*tp_getattro*/
332 0, /*tp_setattro*/
333 0, /*tp_as_buffer*/
334 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /*tp_flags*/
335 "GDB thread object", /* tp_doc */
336 0, /* tp_traverse */
337 0, /* tp_clear */
338 0, /* tp_richcompare */
339 0, /* tp_weaklistoffset */
340 0, /* tp_iter */
341 0, /* tp_iternext */
342 thread_object_methods, /* tp_methods */
343 0, /* tp_members */
344 thread_object_getset, /* tp_getset */
345 0, /* tp_base */
346 0, /* tp_dict */
347 0, /* tp_descr_get */
348 0, /* tp_descr_set */
349 0, /* tp_dictoffset */
350 0, /* tp_init */
351 0 /* tp_alloc */
352};
This page took 0.558497 seconds and 4 git commands to generate.