1 /* Python interface to program spaces.
3 Copyright (C) 2010-2024 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 "python-internal.h"
22 #include "progspace.h"
25 #include "arch-utils.h"
29 #include "observable.h"
36 /* The corresponding pspace. */
37 struct program_space
*pspace
;
39 /* Dictionary holding user-added attributes.
40 This is the __dict__ attribute of the object. */
43 /* The pretty-printer list of functions. */
46 /* The frame filter list of functions. */
47 PyObject
*frame_filters
;
49 /* The frame unwinder list. */
50 PyObject
*frame_unwinders
;
52 /* The type-printer list. */
53 PyObject
*type_printers
;
55 /* The debug method list. */
58 /* The missing file handler list. */
59 PyObject
*missing_file_handlers
;
62 extern PyTypeObject pspace_object_type
63 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("pspace_object");
65 /* Clear the PSPACE pointer in a Pspace object and remove the reference. */
68 void operator() (pspace_object
*obj
)
70 /* This is a fiction, but we're in a nasty spot: The pspace is in the
71 process of being deleted, we can't rely on anything in it. Plus
72 this is one time when the current program space and current inferior
73 are not in sync: All inferiors that use PSPACE may no longer exist.
74 We don't need to do much here, and since "there is always an inferior"
75 using the current inferior's arch suffices.
76 Note: We cannot call get_current_arch because it may try to access
77 the target, which may involve accessing data in the pspace currently
79 gdbarch
*arch
= current_inferior ()->arch ();
81 gdbpy_enter
enter_py (arch
);
82 gdbpy_ref
<pspace_object
> object (obj
);
83 object
->pspace
= NULL
;
87 static const registry
<program_space
>::key
<pspace_object
, pspace_deleter
>
90 /* Require that PSPACE_OBJ be a valid program space ID. */
91 #define PSPY_REQUIRE_VALID(pspace_obj) \
93 if (pspace_obj->pspace == nullptr) \
95 PyErr_SetString (PyExc_RuntimeError, \
96 _("Program space no longer exists.")); \
101 /* An Objfile method which returns the objfile's file name, or None. */
104 pspy_get_filename (PyObject
*self
, void *closure
)
106 pspace_object
*obj
= (pspace_object
*) self
;
110 struct objfile
*objfile
= obj
->pspace
->symfile_object_file
;
113 return (host_string_to_python_string (objfile_name (objfile
))
119 /* Implement the gdb.Progspace.symbol_file attribute. Retun the
120 gdb.Objfile corresponding to the currently loaded symbol-file, or None
121 if no symbol-file is loaded. If the Progspace is invalid then raise an
125 pspy_get_symbol_file (PyObject
*self
, void *closure
)
127 pspace_object
*obj
= (pspace_object
*) self
;
129 PSPY_REQUIRE_VALID (obj
);
131 struct objfile
*objfile
= obj
->pspace
->symfile_object_file
;
133 if (objfile
!= nullptr)
134 return objfile_to_objfile_object (objfile
).release ();
139 /* Implement the gdb.Progspace.executable_filename attribute. Retun a
140 string containing the name of the current executable, or None if no
141 executable is currently set. If the Progspace is invalid then raise an
145 pspy_get_exec_file (PyObject
*self
, void *closure
)
147 pspace_object
*obj
= (pspace_object
*) self
;
149 PSPY_REQUIRE_VALID (obj
);
151 const char *filename
= obj
->pspace
->exec_filename ();
152 if (filename
!= nullptr)
153 return host_string_to_python_string (filename
).release ();
159 pspy_dealloc (PyObject
*self
)
161 pspace_object
*ps_self
= (pspace_object
*) self
;
163 Py_XDECREF (ps_self
->dict
);
164 Py_XDECREF (ps_self
->printers
);
165 Py_XDECREF (ps_self
->frame_filters
);
166 Py_XDECREF (ps_self
->frame_unwinders
);
167 Py_XDECREF (ps_self
->type_printers
);
168 Py_XDECREF (ps_self
->xmethods
);
169 Py_XDECREF (ps_self
->missing_file_handlers
);
170 Py_TYPE (self
)->tp_free (self
);
173 /* Initialize a pspace_object.
174 The result is a boolean indicating success. */
177 pspy_initialize (pspace_object
*self
)
181 self
->dict
= PyDict_New ();
182 if (self
->dict
== NULL
)
185 self
->printers
= PyList_New (0);
186 if (self
->printers
== NULL
)
189 self
->frame_filters
= PyDict_New ();
190 if (self
->frame_filters
== NULL
)
193 self
->frame_unwinders
= PyList_New (0);
194 if (self
->frame_unwinders
== NULL
)
197 self
->type_printers
= PyList_New (0);
198 if (self
->type_printers
== NULL
)
201 self
->xmethods
= PyList_New (0);
202 if (self
->xmethods
== NULL
)
205 self
->missing_file_handlers
= PyList_New (0);
206 if (self
->missing_file_handlers
== nullptr)
213 pspy_get_printers (PyObject
*o
, void *ignore
)
215 pspace_object
*self
= (pspace_object
*) o
;
217 Py_INCREF (self
->printers
);
218 return self
->printers
;
222 pspy_set_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
224 pspace_object
*self
= (pspace_object
*) o
;
228 PyErr_SetString (PyExc_TypeError
,
229 "cannot delete the pretty_printers attribute");
233 if (! PyList_Check (value
))
235 PyErr_SetString (PyExc_TypeError
,
236 "the pretty_printers attribute must be a list");
240 /* Take care in case the LHS and RHS are related somehow. */
241 gdbpy_ref
<> tmp (self
->printers
);
243 self
->printers
= value
;
248 /* Return the Python dictionary attribute containing frame filters for
249 this program space. */
251 pspy_get_frame_filters (PyObject
*o
, void *ignore
)
253 pspace_object
*self
= (pspace_object
*) o
;
255 Py_INCREF (self
->frame_filters
);
256 return self
->frame_filters
;
259 /* Set this object file's frame filters dictionary to FILTERS. */
261 pspy_set_frame_filters (PyObject
*o
, PyObject
*frame
, void *ignore
)
263 pspace_object
*self
= (pspace_object
*) o
;
267 PyErr_SetString (PyExc_TypeError
,
268 "cannot delete the frame filter attribute");
272 if (! PyDict_Check (frame
))
274 PyErr_SetString (PyExc_TypeError
,
275 "the frame filter attribute must be a dictionary");
279 /* Take care in case the LHS and RHS are related somehow. */
280 gdbpy_ref
<> tmp (self
->frame_filters
);
282 self
->frame_filters
= frame
;
287 /* Return the list of the frame unwinders for this program space. */
290 pspy_get_frame_unwinders (PyObject
*o
, void *ignore
)
292 pspace_object
*self
= (pspace_object
*) o
;
294 Py_INCREF (self
->frame_unwinders
);
295 return self
->frame_unwinders
;
298 /* Set this program space's list of the unwinders to UNWINDERS. */
301 pspy_set_frame_unwinders (PyObject
*o
, PyObject
*unwinders
, void *ignore
)
303 pspace_object
*self
= (pspace_object
*) o
;
307 PyErr_SetString (PyExc_TypeError
,
308 "cannot delete the frame unwinders list");
312 if (!PyList_Check (unwinders
))
314 PyErr_SetString (PyExc_TypeError
,
315 "the frame unwinders attribute must be a list");
319 /* Take care in case the LHS and RHS are related somehow. */
320 gdbpy_ref
<> tmp (self
->frame_unwinders
);
321 Py_INCREF (unwinders
);
322 self
->frame_unwinders
= unwinders
;
327 /* Get the 'type_printers' attribute. */
330 pspy_get_type_printers (PyObject
*o
, void *ignore
)
332 pspace_object
*self
= (pspace_object
*) o
;
334 Py_INCREF (self
->type_printers
);
335 return self
->type_printers
;
338 /* Get the 'xmethods' attribute. */
341 pspy_get_xmethods (PyObject
*o
, void *ignore
)
343 pspace_object
*self
= (pspace_object
*) o
;
345 Py_INCREF (self
->xmethods
);
346 return self
->xmethods
;
349 /* Return the list of missing debug handlers for this program space. */
352 pspy_get_missing_file_handlers (PyObject
*o
, void *ignore
)
354 pspace_object
*self
= (pspace_object
*) o
;
356 Py_INCREF (self
->missing_file_handlers
);
357 return self
->missing_file_handlers
;
360 /* Set this program space's list of missing debug handlers to HANDLERS. */
363 pspy_set_missing_file_handlers (PyObject
*o
, PyObject
*handlers
,
366 pspace_object
*self
= (pspace_object
*) o
;
368 if (handlers
== nullptr)
370 PyErr_SetString (PyExc_TypeError
,
371 "cannot delete the missing debug handlers list");
375 if (!PyList_Check (handlers
))
377 PyErr_SetString (PyExc_TypeError
,
378 "the missing debug handlers attribute must be a list");
382 /* Take care in case the LHS and RHS are related somehow. */
383 gdbpy_ref
<> tmp (self
->missing_file_handlers
);
384 Py_INCREF (handlers
);
385 self
->missing_file_handlers
= handlers
;
390 /* Set the 'type_printers' attribute. */
393 pspy_set_type_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
395 pspace_object
*self
= (pspace_object
*) o
;
399 PyErr_SetString (PyExc_TypeError
,
400 "cannot delete the type_printers attribute");
404 if (! PyList_Check (value
))
406 PyErr_SetString (PyExc_TypeError
,
407 "the type_printers attribute must be a list");
411 /* Take care in case the LHS and RHS are related somehow. */
412 gdbpy_ref
<> tmp (self
->type_printers
);
414 self
->type_printers
= value
;
419 /* Implement the objfiles method. */
422 pspy_get_objfiles (PyObject
*self_
, PyObject
*args
)
424 pspace_object
*self
= (pspace_object
*) self_
;
426 PSPY_REQUIRE_VALID (self
);
428 gdbpy_ref
<> list (PyList_New (0));
432 if (self
->pspace
!= NULL
)
434 for (objfile
*objf
: self
->pspace
->objfiles ())
436 gdbpy_ref
<> item
= objfile_to_objfile_object (objf
);
439 || PyList_Append (list
.get (), item
.get ()) == -1)
444 return list
.release ();
447 /* Implementation of solib_name (Long) -> String.
448 Returns the name of the shared library holding a given address, or None. */
451 pspy_solib_name (PyObject
*o
, PyObject
*args
)
456 pspace_object
*self
= (pspace_object
*) o
;
458 PSPY_REQUIRE_VALID (self
);
460 if (!PyArg_ParseTuple (args
, "O", &pc_obj
))
462 if (get_addr_from_python (pc_obj
, &pc
) < 0)
465 const char *soname
= solib_name_from_address (self
->pspace
, pc
);
466 if (soname
== nullptr)
468 return host_string_to_python_string (soname
).release ();
471 /* Implement objfile_for_address. */
474 pspy_objfile_for_address (PyObject
*o
, PyObject
*args
)
479 pspace_object
*self
= (pspace_object
*) o
;
481 PSPY_REQUIRE_VALID (self
);
483 if (!PyArg_ParseTuple (args
, "O", &addr_obj
))
485 if (get_addr_from_python (addr_obj
, &addr
) < 0)
488 struct objfile
*objf
= self
->pspace
->objfile_for_address (addr
);
492 return objfile_to_objfile_object (objf
).release ();
495 /* Return the innermost lexical block containing the specified pc value,
496 or 0 if there is none. */
498 pspy_block_for_pc (PyObject
*o
, PyObject
*args
)
500 pspace_object
*self
= (pspace_object
*) o
;
503 const struct block
*block
= NULL
;
504 struct compunit_symtab
*cust
= NULL
;
506 PSPY_REQUIRE_VALID (self
);
508 if (!PyArg_ParseTuple (args
, "O", &pc_obj
))
510 if (get_addr_from_python (pc_obj
, &pc
) < 0)
515 scoped_restore_current_program_space saver
;
517 set_current_program_space (self
->pspace
);
518 cust
= find_pc_compunit_symtab (pc
);
520 if (cust
!= NULL
&& cust
->objfile () != NULL
)
521 block
= block_for_pc (pc
);
523 catch (const gdb_exception
&except
)
525 return gdbpy_handle_gdb_exception (nullptr, except
);
528 if (cust
== NULL
|| cust
->objfile () == NULL
)
532 return block_to_block_object (block
, cust
->objfile ());
537 /* Implementation of the find_pc_line function.
538 Returns the gdb.Symtab_and_line object corresponding to a PC value. */
541 pspy_find_pc_line (PyObject
*o
, PyObject
*args
)
544 PyObject
*result
= NULL
; /* init for gcc -Wall */
546 pspace_object
*self
= (pspace_object
*) o
;
548 PSPY_REQUIRE_VALID (self
);
550 if (!PyArg_ParseTuple (args
, "O", &pc_obj
))
552 if (get_addr_from_python (pc_obj
, &pc
) < 0)
557 struct symtab_and_line sal
;
558 scoped_restore_current_program_space saver
;
560 set_current_program_space (self
->pspace
);
562 sal
= find_pc_line (pc
, 0);
563 result
= symtab_and_line_to_sal_object (sal
);
565 catch (const gdb_exception
&except
)
567 return gdbpy_handle_gdb_exception (nullptr, except
);
573 /* Implementation of is_valid (self) -> Boolean.
574 Returns True if this program space still exists in GDB. */
577 pspy_is_valid (PyObject
*o
, PyObject
*args
)
579 pspace_object
*self
= (pspace_object
*) o
;
581 if (self
->pspace
== NULL
)
589 /* Return a new reference to the Python object of type Pspace
590 representing PSPACE. If the object has already been created,
591 return it. Otherwise, create it. Return NULL and set the Python
595 pspace_to_pspace_object (struct program_space
*pspace
)
597 PyObject
*result
= (PyObject
*) pspy_pspace_data_key
.get (pspace
);
600 gdbpy_ref
<pspace_object
> object
601 ((pspace_object
*) PyObject_New (pspace_object
, &pspace_object_type
));
604 if (!pspy_initialize (object
.get ()))
607 object
->pspace
= pspace
;
608 pspy_pspace_data_key
.set (pspace
, object
.get ());
609 result
= (PyObject
*) object
.release ();
612 return gdbpy_ref
<>::new_reference (result
);
615 /* See python-internal.h. */
617 struct program_space
*
618 progspace_object_to_program_space (PyObject
*obj
)
620 gdb_assert (gdbpy_is_progspace (obj
));
621 return ((pspace_object
*) obj
)->pspace
;
624 /* See python-internal.h. */
627 gdbpy_is_progspace (PyObject
*obj
)
629 return PyObject_TypeCheck (obj
, &pspace_object_type
);
632 /* Emit an ExecutableChangedEvent event to REGISTRY. Return 0 on success,
633 or a negative value on error. PSPACE is the program_space in which the
634 current executable has changed, and RELOAD_P is true if the executable
635 path stayed the same, but the file on disk changed, or false if the
636 executable path actually changed. */
639 emit_executable_changed_event (eventregistry_object
*registry
,
640 struct program_space
*pspace
, bool reload_p
)
642 gdbpy_ref
<> event_obj
643 = create_event_object (&executable_changed_event_object_type
);
644 if (event_obj
== nullptr)
647 gdbpy_ref
<> py_pspace
= pspace_to_pspace_object (pspace
);
648 if (py_pspace
== nullptr
649 || evpy_add_attribute (event_obj
.get (), "progspace",
650 py_pspace
.get ()) < 0)
653 gdbpy_ref
<> py_reload_p (PyBool_FromLong (reload_p
? 1 : 0));
654 if (py_reload_p
== nullptr
655 || evpy_add_attribute (event_obj
.get (), "reload",
656 py_reload_p
.get ()) < 0)
659 return evpy_emit_event (event_obj
.get (), registry
);
662 /* Listener for the executable_changed observable, this is called when the
663 current executable within PSPACE changes. RELOAD_P is true if the
664 executable path stayed the same but the file changed on disk. RELOAD_P
665 is false if the executable path was changed. */
668 gdbpy_executable_changed (struct program_space
*pspace
, bool reload_p
)
670 if (!gdb_python_initialized
)
673 gdbpy_enter enter_py
;
675 if (!evregpy_no_listeners_p (gdb_py_events
.executable_changed
))
676 if (emit_executable_changed_event (gdb_py_events
.executable_changed
,
677 pspace
, reload_p
) < 0)
678 gdbpy_print_stack ();
681 /* Helper function to emit NewProgspaceEvent (when ADDING_P is true) or
682 FreeProgspaceEvent events (when ADDING_P is false). */
685 gdbpy_program_space_event (program_space
*pspace
, bool adding_p
)
687 if (!gdb_python_initialized
)
690 gdbpy_enter enter_py
;
692 eventregistry_object
*registry
;
693 PyTypeObject
*event_type
;
696 registry
= gdb_py_events
.new_progspace
;
697 event_type
= &new_progspace_event_object_type
;
701 registry
= gdb_py_events
.free_progspace
;
702 event_type
= &free_progspace_event_object_type
;
705 if (evregpy_no_listeners_p (registry
))
708 gdbpy_ref
<> pspace_obj
= pspace_to_pspace_object (pspace
);
709 if (pspace_obj
== nullptr)
711 gdbpy_print_stack ();
715 gdbpy_ref
<> event
= create_event_object (event_type
);
717 || evpy_add_attribute (event
.get (), "progspace",
718 pspace_obj
.get ()) < 0
719 || evpy_emit_event (event
.get (), registry
) < 0)
720 gdbpy_print_stack ();
723 /* Emit a NewProgspaceEvent to indicate PSPACE has been created. */
726 gdbpy_new_program_space_event (program_space
*pspace
)
728 gdbpy_program_space_event (pspace
, true);
731 /* Emit a FreeProgspaceEvent to indicate PSPACE is just about to be removed
735 gdbpy_free_program_space_event (program_space
*pspace
)
737 gdbpy_program_space_event (pspace
, false);
740 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
741 gdbpy_initialize_pspace (void)
743 gdb::observers::executable_changed
.attach (gdbpy_executable_changed
,
745 gdb::observers::new_program_space
.attach (gdbpy_new_program_space_event
,
747 gdb::observers::free_program_space
.attach (gdbpy_free_program_space_event
,
750 if (gdbpy_type_ready (&pspace_object_type
) < 0)
756 GDBPY_INITIALIZE_FILE (gdbpy_initialize_pspace
);
760 static gdb_PyGetSetDef pspace_getset
[] =
762 { "__dict__", gdb_py_generic_dict
, NULL
,
763 "The __dict__ for this progspace.", &pspace_object_type
},
764 { "filename", pspy_get_filename
, NULL
,
765 "The filename of the progspace's main symbol file, or None.", nullptr },
766 { "symbol_file", pspy_get_symbol_file
, nullptr,
767 "The gdb.Objfile for the progspace's main symbol file, or None.",
769 { "executable_filename", pspy_get_exec_file
, nullptr,
770 "The filename for the progspace's executable, or None.", nullptr},
771 { "pretty_printers", pspy_get_printers
, pspy_set_printers
,
772 "Pretty printers.", NULL
},
773 { "frame_filters", pspy_get_frame_filters
, pspy_set_frame_filters
,
774 "Frame filters.", NULL
},
775 { "frame_unwinders", pspy_get_frame_unwinders
, pspy_set_frame_unwinders
,
776 "Frame unwinders.", NULL
},
777 { "type_printers", pspy_get_type_printers
, pspy_set_type_printers
,
778 "Type printers.", NULL
},
779 { "xmethods", pspy_get_xmethods
, NULL
,
780 "Debug methods.", NULL
},
781 { "missing_file_handlers", pspy_get_missing_file_handlers
,
782 pspy_set_missing_file_handlers
, "Missing file handlers.", NULL
},
786 static PyMethodDef progspace_object_methods
[] =
788 { "objfiles", pspy_get_objfiles
, METH_NOARGS
,
789 "Return a sequence of objfiles associated to this program space." },
790 { "solib_name", pspy_solib_name
, METH_VARARGS
,
791 "solib_name (Long) -> String.\n\
792 Return the name of the shared library holding a given address, or None." },
793 { "objfile_for_address", pspy_objfile_for_address
, METH_VARARGS
,
794 "objfile_for_address (int) -> gdb.Objfile\n\
795 Return the objfile containing the given address, or None." },
796 { "block_for_pc", pspy_block_for_pc
, METH_VARARGS
,
797 "Return the block containing the given pc value, or None." },
798 { "find_pc_line", pspy_find_pc_line
, METH_VARARGS
,
799 "find_pc_line (pc) -> Symtab_and_line.\n\
800 Return the gdb.Symtab_and_line object corresponding to the pc value." },
801 { "is_valid", pspy_is_valid
, METH_NOARGS
,
802 "is_valid () -> Boolean.\n\
803 Return true if this program space is valid, false if not." },
807 PyTypeObject pspace_object_type
=
809 PyVarObject_HEAD_INIT (NULL
, 0)
810 "gdb.Progspace", /*tp_name*/
811 sizeof (pspace_object
), /*tp_basicsize*/
813 pspy_dealloc
, /*tp_dealloc*/
820 0, /*tp_as_sequence*/
828 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
829 "GDB progspace object", /* tp_doc */
832 0, /* tp_richcompare */
833 0, /* tp_weaklistoffset */
836 progspace_object_methods
, /* tp_methods */
838 pspace_getset
, /* tp_getset */
841 0, /* tp_descr_get */
842 0, /* tp_descr_set */
843 offsetof (pspace_object
, dict
), /* tp_dictoffset */