Rename gdb exception types
[deliverable/binutils-gdb.git] / gdb / python / py-progspace.c
CommitLineData
fa33c3cd
DE
1/* Python interface to program spaces.
2
42a4f53d 3 Copyright (C) 2010-2019 Free Software Foundation, Inc.
fa33c3cd
DE
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 "python-internal.h"
22#include "charset.h"
23#include "progspace.h"
24#include "objfiles.h"
25#include "language.h"
b3422a0d 26#include "arch-utils.h"
8743a9cd
TT
27#include "solib.h"
28#include "block.h"
fa33c3cd
DE
29
30typedef struct
31{
32 PyObject_HEAD
33
34 /* The corresponding pspace. */
35 struct program_space *pspace;
36
02be9a71
DE
37 /* Dictionary holding user-added attributes.
38 This is the __dict__ attribute of the object. */
39 PyObject *dict;
40
fa33c3cd
DE
41 /* The pretty-printer list of functions. */
42 PyObject *printers;
18a9fc12 43
1e611234
PM
44 /* The frame filter list of functions. */
45 PyObject *frame_filters;
d11916aa
SS
46
47 /* The frame unwinder list. */
48 PyObject *frame_unwinders;
49
18a9fc12
TT
50 /* The type-printer list. */
51 PyObject *type_printers;
883964a7
SC
52
53 /* The debug method list. */
54 PyObject *xmethods;
fa33c3cd
DE
55} pspace_object;
56
e36122e9 57extern PyTypeObject pspace_object_type
62eec1a5 58 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("pspace_object");
fa33c3cd
DE
59
60static const struct program_space_data *pspy_pspace_data_key;
61
0ae1a321
SM
62/* Require that PSPACE_OBJ be a valid program space ID. */
63#define PSPY_REQUIRE_VALID(pspace_obj) \
64 do { \
65 if (pspace_obj->pspace == nullptr) \
66 { \
67 PyErr_SetString (PyExc_RuntimeError, \
68 _("Program space no longer exists.")); \
69 return NULL; \
70 } \
71 } while (0)
fa33c3cd
DE
72
73/* An Objfile method which returns the objfile's file name, or None. */
74
75static PyObject *
76pspy_get_filename (PyObject *self, void *closure)
77{
78 pspace_object *obj = (pspace_object *) self;
d59b6f6c 79
fa33c3cd
DE
80 if (obj->pspace)
81 {
82 struct objfile *objfile = obj->pspace->symfile_object_file;
d59b6f6c 83
d31d2fc3 84 if (objfile)
833d985d
TT
85 return (host_string_to_python_string (objfile_name (objfile))
86 .release ());
fa33c3cd
DE
87 }
88 Py_RETURN_NONE;
89}
90
91static void
92pspy_dealloc (PyObject *self)
93{
94 pspace_object *ps_self = (pspace_object *) self;
d59b6f6c 95
02be9a71 96 Py_XDECREF (ps_self->dict);
fa33c3cd 97 Py_XDECREF (ps_self->printers);
1e611234 98 Py_XDECREF (ps_self->frame_filters);
d11916aa 99 Py_XDECREF (ps_self->frame_unwinders);
18a9fc12 100 Py_XDECREF (ps_self->type_printers);
883964a7 101 Py_XDECREF (ps_self->xmethods);
9a27f2c6 102 Py_TYPE (self)->tp_free (self);
fa33c3cd
DE
103}
104
4e1bbde0
DE
105/* Initialize a pspace_object.
106 The result is a boolean indicating success. */
107
108static int
109pspy_initialize (pspace_object *self)
110{
111 self->pspace = NULL;
0f6ed0e0
TT
112
113 self->dict = PyDict_New ();
114 if (self->dict == NULL)
115 return 0;
4e1bbde0
DE
116
117 self->printers = PyList_New (0);
118 if (self->printers == NULL)
119 return 0;
120
121 self->frame_filters = PyDict_New ();
122 if (self->frame_filters == NULL)
123 return 0;
124
d11916aa
SS
125 self->frame_unwinders = PyList_New (0);
126 if (self->frame_unwinders == NULL)
127 return 0;
128
4e1bbde0
DE
129 self->type_printers = PyList_New (0);
130 if (self->type_printers == NULL)
131 return 0;
132
133 self->xmethods = PyList_New (0);
134 if (self->xmethods == NULL)
135 return 0;
136
137 return 1;
138}
139
fa33c3cd
DE
140static PyObject *
141pspy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
142{
88b6faea 143 gdbpy_ref<pspace_object> self ((pspace_object *) type->tp_alloc (type, 0));
d59b6f6c 144
88b6faea 145 if (self != NULL)
fa33c3cd 146 {
88b6faea
TT
147 if (!pspy_initialize (self.get ()))
148 return NULL;
fa33c3cd 149 }
4e1bbde0 150
88b6faea 151 return (PyObject *) self.release ();
fa33c3cd
DE
152}
153
154PyObject *
155pspy_get_printers (PyObject *o, void *ignore)
156{
157 pspace_object *self = (pspace_object *) o;
d59b6f6c 158
fa33c3cd
DE
159 Py_INCREF (self->printers);
160 return self->printers;
161}
162
163static int
164pspy_set_printers (PyObject *o, PyObject *value, void *ignore)
165{
fa33c3cd 166 pspace_object *self = (pspace_object *) o;
d59b6f6c 167
fa33c3cd
DE
168 if (! value)
169 {
170 PyErr_SetString (PyExc_TypeError,
171 "cannot delete the pretty_printers attribute");
172 return -1;
173 }
174
175 if (! PyList_Check (value))
176 {
177 PyErr_SetString (PyExc_TypeError,
178 "the pretty_printers attribute must be a list");
179 return -1;
180 }
181
182 /* Take care in case the LHS and RHS are related somehow. */
2a3c71d6 183 gdbpy_ref<> tmp (self->printers);
fa33c3cd
DE
184 Py_INCREF (value);
185 self->printers = value;
fa33c3cd
DE
186
187 return 0;
188}
189
1e611234
PM
190/* Return the Python dictionary attribute containing frame filters for
191 this program space. */
192PyObject *
193pspy_get_frame_filters (PyObject *o, void *ignore)
194{
195 pspace_object *self = (pspace_object *) o;
196
197 Py_INCREF (self->frame_filters);
198 return self->frame_filters;
199}
200
201/* Set this object file's frame filters dictionary to FILTERS. */
202static int
203pspy_set_frame_filters (PyObject *o, PyObject *frame, void *ignore)
204{
1e611234
PM
205 pspace_object *self = (pspace_object *) o;
206
207 if (! frame)
208 {
209 PyErr_SetString (PyExc_TypeError,
210 "cannot delete the frame filter attribute");
211 return -1;
212 }
213
214 if (! PyDict_Check (frame))
215 {
216 PyErr_SetString (PyExc_TypeError,
217 "the frame filter attribute must be a dictionary");
218 return -1;
219 }
220
221 /* Take care in case the LHS and RHS are related somehow. */
2a3c71d6 222 gdbpy_ref<> tmp (self->frame_filters);
1e611234
PM
223 Py_INCREF (frame);
224 self->frame_filters = frame;
1e611234
PM
225
226 return 0;
227}
228
d11916aa
SS
229/* Return the list of the frame unwinders for this program space. */
230
231PyObject *
232pspy_get_frame_unwinders (PyObject *o, void *ignore)
233{
234 pspace_object *self = (pspace_object *) o;
235
236 Py_INCREF (self->frame_unwinders);
237 return self->frame_unwinders;
238}
239
240/* Set this program space's list of the unwinders to UNWINDERS. */
241
242static int
243pspy_set_frame_unwinders (PyObject *o, PyObject *unwinders, void *ignore)
244{
d11916aa
SS
245 pspace_object *self = (pspace_object *) o;
246
247 if (!unwinders)
248 {
249 PyErr_SetString (PyExc_TypeError,
250 "cannot delete the frame unwinders list");
251 return -1;
252 }
253
254 if (!PyList_Check (unwinders))
255 {
256 PyErr_SetString (PyExc_TypeError,
257 "the frame unwinders attribute must be a list");
258 return -1;
259 }
260
261 /* Take care in case the LHS and RHS are related somehow. */
2a3c71d6 262 gdbpy_ref<> tmp (self->frame_unwinders);
d11916aa
SS
263 Py_INCREF (unwinders);
264 self->frame_unwinders = unwinders;
d11916aa
SS
265
266 return 0;
267}
268
18a9fc12
TT
269/* Get the 'type_printers' attribute. */
270
271static PyObject *
272pspy_get_type_printers (PyObject *o, void *ignore)
273{
274 pspace_object *self = (pspace_object *) o;
275
276 Py_INCREF (self->type_printers);
277 return self->type_printers;
278}
279
883964a7
SC
280/* Get the 'xmethods' attribute. */
281
282PyObject *
283pspy_get_xmethods (PyObject *o, void *ignore)
284{
285 pspace_object *self = (pspace_object *) o;
286
287 Py_INCREF (self->xmethods);
288 return self->xmethods;
289}
290
18a9fc12
TT
291/* Set the 'type_printers' attribute. */
292
293static int
294pspy_set_type_printers (PyObject *o, PyObject *value, void *ignore)
295{
18a9fc12
TT
296 pspace_object *self = (pspace_object *) o;
297
298 if (! value)
299 {
300 PyErr_SetString (PyExc_TypeError,
301 "cannot delete the type_printers attribute");
302 return -1;
303 }
304
305 if (! PyList_Check (value))
306 {
307 PyErr_SetString (PyExc_TypeError,
308 "the type_printers attribute must be a list");
309 return -1;
310 }
311
312 /* Take care in case the LHS and RHS are related somehow. */
2a3c71d6 313 gdbpy_ref<> tmp (self->type_printers);
18a9fc12
TT
314 Py_INCREF (value);
315 self->type_printers = value;
18a9fc12
TT
316
317 return 0;
318}
319
0ae1a321
SM
320/* Implement the objfiles method. */
321
322static PyObject *
323pspy_get_objfiles (PyObject *self_, PyObject *args)
324{
325 pspace_object *self = (pspace_object *) self_;
326
327 PSPY_REQUIRE_VALID (self);
328
8743a9cd
TT
329 gdbpy_ref<> list (PyList_New (0));
330 if (list == NULL)
331 return NULL;
332
333 if (self->pspace != NULL)
334 {
2030c079 335 for (objfile *objf : self->pspace->objfiles ())
8743a9cd 336 {
0a9db5ad 337 gdbpy_ref<> item = objfile_to_objfile_object (objf);
8743a9cd 338
0a9db5ad
TT
339 if (item == nullptr
340 || PyList_Append (list.get (), item.get ()) == -1)
8743a9cd
TT
341 return NULL;
342 }
343 }
344
345 return list.release ();
346}
347
348/* Implementation of solib_name (Long) -> String.
349 Returns the name of the shared library holding a given address, or None. */
350
351static PyObject *
352pspy_solib_name (PyObject *o, PyObject *args)
353{
354 char *soname;
5c4481cc 355 gdb_py_ulongest pc;
8743a9cd
TT
356 pspace_object *self = (pspace_object *) o;
357
358 PSPY_REQUIRE_VALID (self);
359
360 if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc))
361 return NULL;
362
363 soname = solib_name_from_address (self->pspace, pc);
364 if (soname == nullptr)
365 Py_RETURN_NONE;
833d985d 366 return host_string_to_python_string (soname).release ();
0ae1a321 367}
fa33c3cd 368
8743a9cd
TT
369/* Return the innermost lexical block containing the specified pc value,
370 or 0 if there is none. */
371static PyObject *
372pspy_block_for_pc (PyObject *o, PyObject *args)
373{
374 pspace_object *self = (pspace_object *) o;
375 gdb_py_ulongest pc;
376 const struct block *block = NULL;
377 struct compunit_symtab *cust = NULL;
378
379 PSPY_REQUIRE_VALID (self);
380
381 if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc))
382 return NULL;
383
a70b8144 384 try
8743a9cd
TT
385 {
386 scoped_restore_current_program_space saver;
387
388 set_current_program_space (self->pspace);
389 cust = find_pc_compunit_symtab (pc);
390
391 if (cust != NULL && COMPUNIT_OBJFILE (cust) != NULL)
392 block = block_for_pc (pc);
393 }
230d2906 394 catch (const gdb_exception &except)
8743a9cd
TT
395 {
396 GDB_PY_HANDLE_EXCEPTION (except);
397 }
8743a9cd
TT
398
399 if (cust == NULL || COMPUNIT_OBJFILE (cust) == NULL)
400 {
401 PyErr_SetString (PyExc_RuntimeError,
402 _("Cannot locate object file for block."));
403 return NULL;
404 }
405
406 if (block)
407 return block_to_block_object (block, COMPUNIT_OBJFILE (cust));
408
409 Py_RETURN_NONE;
410}
411
412/* Implementation of the find_pc_line function.
413 Returns the gdb.Symtab_and_line object corresponding to a PC value. */
414
415static PyObject *
416pspy_find_pc_line (PyObject *o, PyObject *args)
417{
418 gdb_py_ulongest pc_llu;
419 PyObject *result = NULL; /* init for gcc -Wall */
420 pspace_object *self = (pspace_object *) o;
421
422 PSPY_REQUIRE_VALID (self);
423
424 if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc_llu))
425 return NULL;
426
a70b8144 427 try
8743a9cd
TT
428 {
429 struct symtab_and_line sal;
430 CORE_ADDR pc;
431 scoped_restore_current_program_space saver;
432
433 set_current_program_space (self->pspace);
434
435 pc = (CORE_ADDR) pc_llu;
436 sal = find_pc_line (pc, 0);
437 result = symtab_and_line_to_sal_object (sal);
438 }
230d2906 439 catch (const gdb_exception &except)
8743a9cd
TT
440 {
441 GDB_PY_HANDLE_EXCEPTION (except);
442 }
8743a9cd
TT
443
444 return result;
445}
446
447/* Implementation of is_valid (self) -> Boolean.
448 Returns True if this program space still exists in GDB. */
449
450static PyObject *
451pspy_is_valid (PyObject *o, PyObject *args)
452{
453 pspace_object *self = (pspace_object *) o;
454
455 if (self->pspace == NULL)
456 Py_RETURN_FALSE;
457
458 Py_RETURN_TRUE;
459}
460
461\f
462
fa33c3cd
DE
463/* Clear the PSPACE pointer in a Pspace object and remove the reference. */
464
465static void
466py_free_pspace (struct program_space *pspace, void *datum)
467{
227533ac
DE
468 /* This is a fiction, but we're in a nasty spot: The pspace is in the
469 process of being deleted, we can't rely on anything in it. Plus
470 this is one time when the current program space and current inferior
471 are not in sync: All inferiors that use PSPACE may no longer exist.
472 We don't need to do much here, and since "there is always an inferior"
473 using target_gdbarch suffices.
474 Note: We cannot call get_current_arch because it may try to access
475 the target, which may involve accessing data in the pspace currently
476 being deleted. */
477 struct gdbarch *arch = target_gdbarch ();
fa33c3cd 478
bf7da5b0 479 gdbpy_enter enter_py (arch, current_language);
88b6faea 480 gdbpy_ref<pspace_object> object ((pspace_object *) datum);
fa33c3cd 481 object->pspace = NULL;
fa33c3cd
DE
482}
483
3c7aa307 484/* Return a new reference to the Python object of type Pspace
fa33c3cd
DE
485 representing PSPACE. If the object has already been created,
486 return it. Otherwise, create it. Return NULL and set the Python
487 error on failure. */
488
3c7aa307 489gdbpy_ref<>
fa33c3cd
DE
490pspace_to_pspace_object (struct program_space *pspace)
491{
3c7aa307
TT
492 PyObject *result
493 ((PyObject *) program_space_data (pspace, pspy_pspace_data_key));
494 if (result == NULL)
fa33c3cd 495 {
3c7aa307
TT
496 gdbpy_ref<pspace_object> object
497 ((pspace_object *) PyObject_New (pspace_object, &pspace_object_type));
498 if (object == NULL)
499 return NULL;
500 if (!pspy_initialize (object.get ()))
501 return NULL;
883964a7 502
3c7aa307
TT
503 object->pspace = pspace;
504 set_program_space_data (pspace, pspy_pspace_data_key, object.get ());
505 result = (PyObject *) object.release ();
fa33c3cd
DE
506 }
507
3c7aa307 508 return gdbpy_ref<>::new_reference (result);
fa33c3cd
DE
509}
510
999633ed 511int
fa33c3cd
DE
512gdbpy_initialize_pspace (void)
513{
514 pspy_pspace_data_key
8e260fc0 515 = register_program_space_data_with_cleanup (NULL, py_free_pspace);
fa33c3cd
DE
516
517 if (PyType_Ready (&pspace_object_type) < 0)
999633ed 518 return -1;
fa33c3cd 519
aa36459a
TT
520 return gdb_pymodule_addobject (gdb_module, "Progspace",
521 (PyObject *) &pspace_object_type);
fa33c3cd
DE
522}
523
524\f
525
0d1f4ceb 526static gdb_PyGetSetDef pspace_getset[] =
fa33c3cd 527{
02be9a71
DE
528 { "__dict__", gdb_py_generic_dict, NULL,
529 "The __dict__ for this progspace.", &pspace_object_type },
fa33c3cd
DE
530 { "filename", pspy_get_filename, NULL,
531 "The progspace's main filename, or None.", NULL },
532 { "pretty_printers", pspy_get_printers, pspy_set_printers,
533 "Pretty printers.", NULL },
1e611234
PM
534 { "frame_filters", pspy_get_frame_filters, pspy_set_frame_filters,
535 "Frame filters.", NULL },
d11916aa
SS
536 { "frame_unwinders", pspy_get_frame_unwinders, pspy_set_frame_unwinders,
537 "Frame unwinders.", NULL },
18a9fc12
TT
538 { "type_printers", pspy_get_type_printers, pspy_set_type_printers,
539 "Type printers.", NULL },
883964a7
SC
540 { "xmethods", pspy_get_xmethods, NULL,
541 "Debug methods.", NULL },
fa33c3cd
DE
542 { NULL }
543};
544
0ae1a321
SM
545static PyMethodDef progspace_object_methods[] =
546{
547 { "objfiles", pspy_get_objfiles, METH_NOARGS,
548 "Return a sequence of objfiles associated to this program space." },
8743a9cd
TT
549 { "solib_name", pspy_solib_name, METH_VARARGS,
550 "solib_name (Long) -> String.\n\
551Return the name of the shared library holding a given address, or None." },
552 { "block_for_pc", pspy_block_for_pc, METH_VARARGS,
553 "Return the block containing the given pc value, or None." },
554 { "find_pc_line", pspy_find_pc_line, METH_VARARGS,
555 "find_pc_line (pc) -> Symtab_and_line.\n\
556Return the gdb.Symtab_and_line object corresponding to the pc value." },
557 { "is_valid", pspy_is_valid, METH_NOARGS,
558 "is_valid () -> Boolean.\n\
559Return true if this program space is valid, false if not." },
0ae1a321
SM
560 { NULL }
561};
562
e36122e9 563PyTypeObject pspace_object_type =
fa33c3cd 564{
9a27f2c6 565 PyVarObject_HEAD_INIT (NULL, 0)
fa33c3cd
DE
566 "gdb.Progspace", /*tp_name*/
567 sizeof (pspace_object), /*tp_basicsize*/
568 0, /*tp_itemsize*/
569 pspy_dealloc, /*tp_dealloc*/
570 0, /*tp_print*/
571 0, /*tp_getattr*/
572 0, /*tp_setattr*/
573 0, /*tp_compare*/
574 0, /*tp_repr*/
575 0, /*tp_as_number*/
576 0, /*tp_as_sequence*/
577 0, /*tp_as_mapping*/
578 0, /*tp_hash */
579 0, /*tp_call*/
580 0, /*tp_str*/
581 0, /*tp_getattro*/
582 0, /*tp_setattro*/
583 0, /*tp_as_buffer*/
584 Py_TPFLAGS_DEFAULT, /*tp_flags*/
585 "GDB progspace object", /* tp_doc */
586 0, /* tp_traverse */
587 0, /* tp_clear */
588 0, /* tp_richcompare */
589 0, /* tp_weaklistoffset */
590 0, /* tp_iter */
591 0, /* tp_iternext */
0ae1a321 592 progspace_object_methods, /* tp_methods */
fa33c3cd
DE
593 0, /* tp_members */
594 pspace_getset, /* tp_getset */
595 0, /* tp_base */
596 0, /* tp_dict */
597 0, /* tp_descr_get */
598 0, /* tp_descr_set */
02be9a71 599 offsetof (pspace_object, dict), /* tp_dictoffset */
fa33c3cd
DE
600 0, /* tp_init */
601 0, /* tp_alloc */
602 pspy_new, /* tp_new */
603};
This page took 1.22006 seconds and 4 git commands to generate.