Accept gdb.Value in more Python APIs
[binutils-gdb.git] / gdb / python / py-progspace.c
blob5ec5986fce87edec4b09623e3f0387a0d767b544
1 /* Python interface to program spaces.
3 Copyright (C) 2010-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/>. */
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"
26 #include "arch-utils.h"
27 #include "solib.h"
28 #include "block.h"
30 struct pspace_object
32 PyObject_HEAD
34 /* The corresponding pspace. */
35 struct program_space *pspace;
37 /* Dictionary holding user-added attributes.
38 This is the __dict__ attribute of the object. */
39 PyObject *dict;
41 /* The pretty-printer list of functions. */
42 PyObject *printers;
44 /* The frame filter list of functions. */
45 PyObject *frame_filters;
47 /* The frame unwinder list. */
48 PyObject *frame_unwinders;
50 /* The type-printer list. */
51 PyObject *type_printers;
53 /* The debug method list. */
54 PyObject *xmethods;
57 extern PyTypeObject pspace_object_type
58 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("pspace_object");
60 static const struct program_space_data *pspy_pspace_data_key;
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)
73 /* An Objfile method which returns the objfile's file name, or None. */
75 static PyObject *
76 pspy_get_filename (PyObject *self, void *closure)
78 pspace_object *obj = (pspace_object *) self;
80 if (obj->pspace)
82 struct objfile *objfile = obj->pspace->symfile_object_file;
84 if (objfile)
85 return (host_string_to_python_string (objfile_name (objfile))
86 .release ());
88 Py_RETURN_NONE;
91 static void
92 pspy_dealloc (PyObject *self)
94 pspace_object *ps_self = (pspace_object *) self;
96 Py_XDECREF (ps_self->dict);
97 Py_XDECREF (ps_self->printers);
98 Py_XDECREF (ps_self->frame_filters);
99 Py_XDECREF (ps_self->frame_unwinders);
100 Py_XDECREF (ps_self->type_printers);
101 Py_XDECREF (ps_self->xmethods);
102 Py_TYPE (self)->tp_free (self);
105 /* Initialize a pspace_object.
106 The result is a boolean indicating success. */
108 static int
109 pspy_initialize (pspace_object *self)
111 self->pspace = NULL;
113 self->dict = PyDict_New ();
114 if (self->dict == NULL)
115 return 0;
117 self->printers = PyList_New (0);
118 if (self->printers == NULL)
119 return 0;
121 self->frame_filters = PyDict_New ();
122 if (self->frame_filters == NULL)
123 return 0;
125 self->frame_unwinders = PyList_New (0);
126 if (self->frame_unwinders == NULL)
127 return 0;
129 self->type_printers = PyList_New (0);
130 if (self->type_printers == NULL)
131 return 0;
133 self->xmethods = PyList_New (0);
134 if (self->xmethods == NULL)
135 return 0;
137 return 1;
140 static PyObject *
141 pspy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
143 gdbpy_ref<pspace_object> self ((pspace_object *) type->tp_alloc (type, 0));
145 if (self != NULL)
147 if (!pspy_initialize (self.get ()))
148 return NULL;
151 return (PyObject *) self.release ();
154 PyObject *
155 pspy_get_printers (PyObject *o, void *ignore)
157 pspace_object *self = (pspace_object *) o;
159 Py_INCREF (self->printers);
160 return self->printers;
163 static int
164 pspy_set_printers (PyObject *o, PyObject *value, void *ignore)
166 pspace_object *self = (pspace_object *) o;
168 if (! value)
170 PyErr_SetString (PyExc_TypeError,
171 "cannot delete the pretty_printers attribute");
172 return -1;
175 if (! PyList_Check (value))
177 PyErr_SetString (PyExc_TypeError,
178 "the pretty_printers attribute must be a list");
179 return -1;
182 /* Take care in case the LHS and RHS are related somehow. */
183 gdbpy_ref<> tmp (self->printers);
184 Py_INCREF (value);
185 self->printers = value;
187 return 0;
190 /* Return the Python dictionary attribute containing frame filters for
191 this program space. */
192 PyObject *
193 pspy_get_frame_filters (PyObject *o, void *ignore)
195 pspace_object *self = (pspace_object *) o;
197 Py_INCREF (self->frame_filters);
198 return self->frame_filters;
201 /* Set this object file's frame filters dictionary to FILTERS. */
202 static int
203 pspy_set_frame_filters (PyObject *o, PyObject *frame, void *ignore)
205 pspace_object *self = (pspace_object *) o;
207 if (! frame)
209 PyErr_SetString (PyExc_TypeError,
210 "cannot delete the frame filter attribute");
211 return -1;
214 if (! PyDict_Check (frame))
216 PyErr_SetString (PyExc_TypeError,
217 "the frame filter attribute must be a dictionary");
218 return -1;
221 /* Take care in case the LHS and RHS are related somehow. */
222 gdbpy_ref<> tmp (self->frame_filters);
223 Py_INCREF (frame);
224 self->frame_filters = frame;
226 return 0;
229 /* Return the list of the frame unwinders for this program space. */
231 PyObject *
232 pspy_get_frame_unwinders (PyObject *o, void *ignore)
234 pspace_object *self = (pspace_object *) o;
236 Py_INCREF (self->frame_unwinders);
237 return self->frame_unwinders;
240 /* Set this program space's list of the unwinders to UNWINDERS. */
242 static int
243 pspy_set_frame_unwinders (PyObject *o, PyObject *unwinders, void *ignore)
245 pspace_object *self = (pspace_object *) o;
247 if (!unwinders)
249 PyErr_SetString (PyExc_TypeError,
250 "cannot delete the frame unwinders list");
251 return -1;
254 if (!PyList_Check (unwinders))
256 PyErr_SetString (PyExc_TypeError,
257 "the frame unwinders attribute must be a list");
258 return -1;
261 /* Take care in case the LHS and RHS are related somehow. */
262 gdbpy_ref<> tmp (self->frame_unwinders);
263 Py_INCREF (unwinders);
264 self->frame_unwinders = unwinders;
266 return 0;
269 /* Get the 'type_printers' attribute. */
271 static PyObject *
272 pspy_get_type_printers (PyObject *o, void *ignore)
274 pspace_object *self = (pspace_object *) o;
276 Py_INCREF (self->type_printers);
277 return self->type_printers;
280 /* Get the 'xmethods' attribute. */
282 PyObject *
283 pspy_get_xmethods (PyObject *o, void *ignore)
285 pspace_object *self = (pspace_object *) o;
287 Py_INCREF (self->xmethods);
288 return self->xmethods;
291 /* Set the 'type_printers' attribute. */
293 static int
294 pspy_set_type_printers (PyObject *o, PyObject *value, void *ignore)
296 pspace_object *self = (pspace_object *) o;
298 if (! value)
300 PyErr_SetString (PyExc_TypeError,
301 "cannot delete the type_printers attribute");
302 return -1;
305 if (! PyList_Check (value))
307 PyErr_SetString (PyExc_TypeError,
308 "the type_printers attribute must be a list");
309 return -1;
312 /* Take care in case the LHS and RHS are related somehow. */
313 gdbpy_ref<> tmp (self->type_printers);
314 Py_INCREF (value);
315 self->type_printers = value;
317 return 0;
320 /* Implement the objfiles method. */
322 static PyObject *
323 pspy_get_objfiles (PyObject *self_, PyObject *args)
325 pspace_object *self = (pspace_object *) self_;
327 PSPY_REQUIRE_VALID (self);
329 gdbpy_ref<> list (PyList_New (0));
330 if (list == NULL)
331 return NULL;
333 if (self->pspace != NULL)
335 for (objfile *objf : self->pspace->objfiles ())
337 gdbpy_ref<> item = objfile_to_objfile_object (objf);
339 if (item == nullptr
340 || PyList_Append (list.get (), item.get ()) == -1)
341 return NULL;
345 return list.release ();
348 /* Implementation of solib_name (Long) -> String.
349 Returns the name of the shared library holding a given address, or None. */
351 static PyObject *
352 pspy_solib_name (PyObject *o, PyObject *args)
354 CORE_ADDR pc;
355 PyObject *pc_obj;
357 pspace_object *self = (pspace_object *) o;
359 PSPY_REQUIRE_VALID (self);
361 if (!PyArg_ParseTuple (args, "O", &pc_obj))
362 return NULL;
363 if (get_addr_from_python (pc_obj, &pc) < 0)
364 return nullptr;
366 const char *soname = solib_name_from_address (self->pspace, pc);
367 if (soname == nullptr)
368 Py_RETURN_NONE;
369 return host_string_to_python_string (soname).release ();
372 /* Return the innermost lexical block containing the specified pc value,
373 or 0 if there is none. */
374 static PyObject *
375 pspy_block_for_pc (PyObject *o, PyObject *args)
377 pspace_object *self = (pspace_object *) o;
378 CORE_ADDR pc;
379 PyObject *pc_obj;
380 const struct block *block = NULL;
381 struct compunit_symtab *cust = NULL;
383 PSPY_REQUIRE_VALID (self);
385 if (!PyArg_ParseTuple (args, "O", &pc_obj))
386 return NULL;
387 if (get_addr_from_python (pc_obj, &pc) < 0)
388 return nullptr;
392 scoped_restore_current_program_space saver;
394 set_current_program_space (self->pspace);
395 cust = find_pc_compunit_symtab (pc);
397 if (cust != NULL && cust->objfile () != NULL)
398 block = block_for_pc (pc);
400 catch (const gdb_exception &except)
402 GDB_PY_HANDLE_EXCEPTION (except);
405 if (cust == NULL || cust->objfile () == NULL)
406 Py_RETURN_NONE;
408 if (block)
409 return block_to_block_object (block, cust->objfile ());
411 Py_RETURN_NONE;
414 /* Implementation of the find_pc_line function.
415 Returns the gdb.Symtab_and_line object corresponding to a PC value. */
417 static PyObject *
418 pspy_find_pc_line (PyObject *o, PyObject *args)
420 CORE_ADDR pc;
421 PyObject *result = NULL; /* init for gcc -Wall */
422 PyObject *pc_obj;
423 pspace_object *self = (pspace_object *) o;
425 PSPY_REQUIRE_VALID (self);
427 if (!PyArg_ParseTuple (args, "O", &pc_obj))
428 return NULL;
429 if (get_addr_from_python (pc_obj, &pc) < 0)
430 return nullptr;
434 struct symtab_and_line sal;
435 scoped_restore_current_program_space saver;
437 set_current_program_space (self->pspace);
439 sal = find_pc_line (pc, 0);
440 result = symtab_and_line_to_sal_object (sal);
442 catch (const gdb_exception &except)
444 GDB_PY_HANDLE_EXCEPTION (except);
447 return result;
450 /* Implementation of is_valid (self) -> Boolean.
451 Returns True if this program space still exists in GDB. */
453 static PyObject *
454 pspy_is_valid (PyObject *o, PyObject *args)
456 pspace_object *self = (pspace_object *) o;
458 if (self->pspace == NULL)
459 Py_RETURN_FALSE;
461 Py_RETURN_TRUE;
466 /* Clear the PSPACE pointer in a Pspace object and remove the reference. */
468 static void
469 py_free_pspace (struct program_space *pspace, void *datum)
471 /* This is a fiction, but we're in a nasty spot: The pspace is in the
472 process of being deleted, we can't rely on anything in it. Plus
473 this is one time when the current program space and current inferior
474 are not in sync: All inferiors that use PSPACE may no longer exist.
475 We don't need to do much here, and since "there is always an inferior"
476 using target_gdbarch suffices.
477 Note: We cannot call get_current_arch because it may try to access
478 the target, which may involve accessing data in the pspace currently
479 being deleted. */
480 struct gdbarch *arch = target_gdbarch ();
482 gdbpy_enter enter_py (arch);
483 gdbpy_ref<pspace_object> object ((pspace_object *) datum);
484 object->pspace = NULL;
487 /* Return a new reference to the Python object of type Pspace
488 representing PSPACE. If the object has already been created,
489 return it. Otherwise, create it. Return NULL and set the Python
490 error on failure. */
492 gdbpy_ref<>
493 pspace_to_pspace_object (struct program_space *pspace)
495 PyObject *result
496 ((PyObject *) program_space_data (pspace, pspy_pspace_data_key));
497 if (result == NULL)
499 gdbpy_ref<pspace_object> object
500 ((pspace_object *) PyObject_New (pspace_object, &pspace_object_type));
501 if (object == NULL)
502 return NULL;
503 if (!pspy_initialize (object.get ()))
504 return NULL;
506 object->pspace = pspace;
507 set_program_space_data (pspace, pspy_pspace_data_key, object.get ());
508 result = (PyObject *) object.release ();
511 return gdbpy_ref<>::new_reference (result);
514 /* See python-internal.h. */
516 struct program_space *
517 progspace_object_to_program_space (PyObject *obj)
519 gdb_assert (gdbpy_is_progspace (obj));
520 return ((pspace_object *) obj)->pspace;
523 /* See python-internal.h. */
525 bool
526 gdbpy_is_progspace (PyObject *obj)
528 return PyObject_TypeCheck (obj, &pspace_object_type);
531 void _initialize_py_progspace ();
532 void
533 _initialize_py_progspace ()
535 pspy_pspace_data_key
536 = register_program_space_data_with_cleanup (NULL, py_free_pspace);
540 gdbpy_initialize_pspace (void)
542 if (PyType_Ready (&pspace_object_type) < 0)
543 return -1;
545 return gdb_pymodule_addobject (gdb_module, "Progspace",
546 (PyObject *) &pspace_object_type);
551 static gdb_PyGetSetDef pspace_getset[] =
553 { "__dict__", gdb_py_generic_dict, NULL,
554 "The __dict__ for this progspace.", &pspace_object_type },
555 { "filename", pspy_get_filename, NULL,
556 "The progspace's main filename, or None.", NULL },
557 { "pretty_printers", pspy_get_printers, pspy_set_printers,
558 "Pretty printers.", NULL },
559 { "frame_filters", pspy_get_frame_filters, pspy_set_frame_filters,
560 "Frame filters.", NULL },
561 { "frame_unwinders", pspy_get_frame_unwinders, pspy_set_frame_unwinders,
562 "Frame unwinders.", NULL },
563 { "type_printers", pspy_get_type_printers, pspy_set_type_printers,
564 "Type printers.", NULL },
565 { "xmethods", pspy_get_xmethods, NULL,
566 "Debug methods.", NULL },
567 { NULL }
570 static PyMethodDef progspace_object_methods[] =
572 { "objfiles", pspy_get_objfiles, METH_NOARGS,
573 "Return a sequence of objfiles associated to this program space." },
574 { "solib_name", pspy_solib_name, METH_VARARGS,
575 "solib_name (Long) -> String.\n\
576 Return the name of the shared library holding a given address, or None." },
577 { "block_for_pc", pspy_block_for_pc, METH_VARARGS,
578 "Return the block containing the given pc value, or None." },
579 { "find_pc_line", pspy_find_pc_line, METH_VARARGS,
580 "find_pc_line (pc) -> Symtab_and_line.\n\
581 Return the gdb.Symtab_and_line object corresponding to the pc value." },
582 { "is_valid", pspy_is_valid, METH_NOARGS,
583 "is_valid () -> Boolean.\n\
584 Return true if this program space is valid, false if not." },
585 { NULL }
588 PyTypeObject pspace_object_type =
590 PyVarObject_HEAD_INIT (NULL, 0)
591 "gdb.Progspace", /*tp_name*/
592 sizeof (pspace_object), /*tp_basicsize*/
593 0, /*tp_itemsize*/
594 pspy_dealloc, /*tp_dealloc*/
595 0, /*tp_print*/
596 0, /*tp_getattr*/
597 0, /*tp_setattr*/
598 0, /*tp_compare*/
599 0, /*tp_repr*/
600 0, /*tp_as_number*/
601 0, /*tp_as_sequence*/
602 0, /*tp_as_mapping*/
603 0, /*tp_hash */
604 0, /*tp_call*/
605 0, /*tp_str*/
606 0, /*tp_getattro*/
607 0, /*tp_setattro*/
608 0, /*tp_as_buffer*/
609 Py_TPFLAGS_DEFAULT, /*tp_flags*/
610 "GDB progspace object", /* tp_doc */
611 0, /* tp_traverse */
612 0, /* tp_clear */
613 0, /* tp_richcompare */
614 0, /* tp_weaklistoffset */
615 0, /* tp_iter */
616 0, /* tp_iternext */
617 progspace_object_methods, /* tp_methods */
618 0, /* tp_members */
619 pspace_getset, /* tp_getset */
620 0, /* tp_base */
621 0, /* tp_dict */
622 0, /* tp_descr_get */
623 0, /* tp_descr_set */
624 offsetof (pspace_object, dict), /* tp_dictoffset */
625 0, /* tp_init */
626 0, /* tp_alloc */
627 pspy_new, /* tp_new */