1 /* Python interface to program spaces.
3 Copyright (C) 2010-2018 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/>. */
21 #include "python-internal.h"
23 #include "progspace.h"
26 #include "arch-utils.h"
35 /* The corresponding pspace. */
36 struct program_space
*pspace
;
38 /* Dictionary holding user-added attributes.
39 This is the __dict__ attribute of the object. */
42 /* The pretty-printer list of functions. */
45 /* The frame filter list of functions. */
46 PyObject
*frame_filters
;
48 /* The frame unwinder list. */
49 PyObject
*frame_unwinders
;
51 /* The type-printer list. */
52 PyObject
*type_printers
;
54 /* The debug method list. */
58 extern PyTypeObject pspace_object_type
59 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("pspace_object");
61 static const struct program_space_data
*pspy_pspace_data_key
;
63 /* Require that PSPACE_OBJ be a valid program space ID. */
64 #define PSPY_REQUIRE_VALID(pspace_obj) \
66 if (pspace_obj->pspace == nullptr) \
68 PyErr_SetString (PyExc_RuntimeError, \
69 _("Program space no longer exists.")); \
74 /* An Objfile method which returns the objfile's file name, or None. */
77 pspy_get_filename (PyObject
*self
, void *closure
)
79 pspace_object
*obj
= (pspace_object
*) self
;
83 struct objfile
*objfile
= obj
->pspace
->symfile_object_file
;
86 return host_string_to_python_string (objfile_name (objfile
));
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. */
109 pspy_initialize (pspace_object
*self
)
113 self
->dict
= PyDict_New ();
114 if (self
->dict
== NULL
)
117 self
->printers
= PyList_New (0);
118 if (self
->printers
== NULL
)
121 self
->frame_filters
= PyDict_New ();
122 if (self
->frame_filters
== NULL
)
125 self
->frame_unwinders
= PyList_New (0);
126 if (self
->frame_unwinders
== NULL
)
129 self
->type_printers
= PyList_New (0);
130 if (self
->type_printers
== NULL
)
133 self
->xmethods
= PyList_New (0);
134 if (self
->xmethods
== NULL
)
141 pspy_new (PyTypeObject
*type
, PyObject
*args
, PyObject
*keywords
)
143 gdbpy_ref
<pspace_object
> self ((pspace_object
*) type
->tp_alloc (type
, 0));
147 if (!pspy_initialize (self
.get ()))
151 return (PyObject
*) self
.release ();
155 pspy_get_printers (PyObject
*o
, void *ignore
)
157 pspace_object
*self
= (pspace_object
*) o
;
159 Py_INCREF (self
->printers
);
160 return self
->printers
;
164 pspy_set_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
167 pspace_object
*self
= (pspace_object
*) o
;
171 PyErr_SetString (PyExc_TypeError
,
172 "cannot delete the pretty_printers attribute");
176 if (! PyList_Check (value
))
178 PyErr_SetString (PyExc_TypeError
,
179 "the pretty_printers attribute must be a list");
183 /* Take care in case the LHS and RHS are related somehow. */
184 tmp
= self
->printers
;
186 self
->printers
= value
;
192 /* Return the Python dictionary attribute containing frame filters for
193 this program space. */
195 pspy_get_frame_filters (PyObject
*o
, void *ignore
)
197 pspace_object
*self
= (pspace_object
*) o
;
199 Py_INCREF (self
->frame_filters
);
200 return self
->frame_filters
;
203 /* Set this object file's frame filters dictionary to FILTERS. */
205 pspy_set_frame_filters (PyObject
*o
, PyObject
*frame
, void *ignore
)
208 pspace_object
*self
= (pspace_object
*) o
;
212 PyErr_SetString (PyExc_TypeError
,
213 "cannot delete the frame filter attribute");
217 if (! PyDict_Check (frame
))
219 PyErr_SetString (PyExc_TypeError
,
220 "the frame filter attribute must be a dictionary");
224 /* Take care in case the LHS and RHS are related somehow. */
225 tmp
= self
->frame_filters
;
227 self
->frame_filters
= frame
;
233 /* Return the list of the frame unwinders for this program space. */
236 pspy_get_frame_unwinders (PyObject
*o
, void *ignore
)
238 pspace_object
*self
= (pspace_object
*) o
;
240 Py_INCREF (self
->frame_unwinders
);
241 return self
->frame_unwinders
;
244 /* Set this program space's list of the unwinders to UNWINDERS. */
247 pspy_set_frame_unwinders (PyObject
*o
, PyObject
*unwinders
, void *ignore
)
250 pspace_object
*self
= (pspace_object
*) o
;
254 PyErr_SetString (PyExc_TypeError
,
255 "cannot delete the frame unwinders list");
259 if (!PyList_Check (unwinders
))
261 PyErr_SetString (PyExc_TypeError
,
262 "the frame unwinders attribute must be a list");
266 /* Take care in case the LHS and RHS are related somehow. */
267 tmp
= self
->frame_unwinders
;
268 Py_INCREF (unwinders
);
269 self
->frame_unwinders
= unwinders
;
275 /* Get the 'type_printers' attribute. */
278 pspy_get_type_printers (PyObject
*o
, void *ignore
)
280 pspace_object
*self
= (pspace_object
*) o
;
282 Py_INCREF (self
->type_printers
);
283 return self
->type_printers
;
286 /* Get the 'xmethods' attribute. */
289 pspy_get_xmethods (PyObject
*o
, void *ignore
)
291 pspace_object
*self
= (pspace_object
*) o
;
293 Py_INCREF (self
->xmethods
);
294 return self
->xmethods
;
297 /* Set the 'type_printers' attribute. */
300 pspy_set_type_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
303 pspace_object
*self
= (pspace_object
*) o
;
307 PyErr_SetString (PyExc_TypeError
,
308 "cannot delete the type_printers attribute");
312 if (! PyList_Check (value
))
314 PyErr_SetString (PyExc_TypeError
,
315 "the type_printers attribute must be a list");
319 /* Take care in case the LHS and RHS are related somehow. */
320 tmp
= self
->type_printers
;
322 self
->type_printers
= value
;
328 /* Implement the objfiles method. */
331 pspy_get_objfiles (PyObject
*self_
, PyObject
*args
)
333 pspace_object
*self
= (pspace_object
*) self_
;
335 PSPY_REQUIRE_VALID (self
);
337 gdbpy_ref
<> list (PyList_New (0));
341 if (self
->pspace
!= NULL
)
343 struct objfile
*objf
;
345 ALL_PSPACE_OBJFILES (self
->pspace
, objf
)
347 PyObject
*item
= objfile_to_objfile_object (objf
);
349 if (!item
|| PyList_Append (list
.get (), item
) == -1)
354 return list
.release ();
357 /* Implementation of solib_name (Long) -> String.
358 Returns the name of the shared library holding a given address, or None. */
361 pspy_solib_name (PyObject
*o
, PyObject
*args
)
365 pspace_object
*self
= (pspace_object
*) o
;
367 PSPY_REQUIRE_VALID (self
);
369 if (!PyArg_ParseTuple (args
, GDB_PY_LLU_ARG
, &pc
))
372 soname
= solib_name_from_address (self
->pspace
, pc
);
373 if (soname
== nullptr)
375 return host_string_to_python_string (soname
);
378 /* Return the innermost lexical block containing the specified pc value,
379 or 0 if there is none. */
381 pspy_block_for_pc (PyObject
*o
, PyObject
*args
)
383 pspace_object
*self
= (pspace_object
*) o
;
385 const struct block
*block
= NULL
;
386 struct compunit_symtab
*cust
= NULL
;
388 PSPY_REQUIRE_VALID (self
);
390 if (!PyArg_ParseTuple (args
, GDB_PY_LLU_ARG
, &pc
))
395 scoped_restore_current_program_space saver
;
397 set_current_program_space (self
->pspace
);
398 cust
= find_pc_compunit_symtab (pc
);
400 if (cust
!= NULL
&& COMPUNIT_OBJFILE (cust
) != NULL
)
401 block
= block_for_pc (pc
);
403 CATCH (except
, RETURN_MASK_ALL
)
405 GDB_PY_HANDLE_EXCEPTION (except
);
409 if (cust
== NULL
|| COMPUNIT_OBJFILE (cust
) == NULL
)
411 PyErr_SetString (PyExc_RuntimeError
,
412 _("Cannot locate object file for block."));
417 return block_to_block_object (block
, COMPUNIT_OBJFILE (cust
));
422 /* Implementation of the find_pc_line function.
423 Returns the gdb.Symtab_and_line object corresponding to a PC value. */
426 pspy_find_pc_line (PyObject
*o
, PyObject
*args
)
428 gdb_py_ulongest pc_llu
;
429 PyObject
*result
= NULL
; /* init for gcc -Wall */
430 pspace_object
*self
= (pspace_object
*) o
;
432 PSPY_REQUIRE_VALID (self
);
434 if (!PyArg_ParseTuple (args
, GDB_PY_LLU_ARG
, &pc_llu
))
439 struct symtab_and_line sal
;
441 scoped_restore_current_program_space saver
;
443 set_current_program_space (self
->pspace
);
445 pc
= (CORE_ADDR
) pc_llu
;
446 sal
= find_pc_line (pc
, 0);
447 result
= symtab_and_line_to_sal_object (sal
);
449 CATCH (except
, RETURN_MASK_ALL
)
451 GDB_PY_HANDLE_EXCEPTION (except
);
458 /* Implementation of is_valid (self) -> Boolean.
459 Returns True if this program space still exists in GDB. */
462 pspy_is_valid (PyObject
*o
, PyObject
*args
)
464 pspace_object
*self
= (pspace_object
*) o
;
466 if (self
->pspace
== NULL
)
474 /* Clear the PSPACE pointer in a Pspace object and remove the reference. */
477 py_free_pspace (struct program_space
*pspace
, void *datum
)
479 /* This is a fiction, but we're in a nasty spot: The pspace is in the
480 process of being deleted, we can't rely on anything in it. Plus
481 this is one time when the current program space and current inferior
482 are not in sync: All inferiors that use PSPACE may no longer exist.
483 We don't need to do much here, and since "there is always an inferior"
484 using target_gdbarch suffices.
485 Note: We cannot call get_current_arch because it may try to access
486 the target, which may involve accessing data in the pspace currently
488 struct gdbarch
*arch
= target_gdbarch ();
490 gdbpy_enter
enter_py (arch
, current_language
);
491 gdbpy_ref
<pspace_object
> object ((pspace_object
*) datum
);
492 object
->pspace
= NULL
;
495 /* Return a borrowed reference to the Python object of type Pspace
496 representing PSPACE. If the object has already been created,
497 return it. Otherwise, create it. Return NULL and set the Python
501 pspace_to_pspace_object (struct program_space
*pspace
)
503 gdbpy_ref
<pspace_object
> object
504 ((pspace_object
*) program_space_data (pspace
, pspy_pspace_data_key
));
507 object
.reset (PyObject_New (pspace_object
, &pspace_object_type
));
510 if (!pspy_initialize (object
.get ()))
513 object
->pspace
= pspace
;
514 set_program_space_data (pspace
, pspy_pspace_data_key
, object
.get ());
518 return (PyObject
*) object
.release ();
522 gdbpy_initialize_pspace (void)
525 = register_program_space_data_with_cleanup (NULL
, py_free_pspace
);
527 if (PyType_Ready (&pspace_object_type
) < 0)
530 return gdb_pymodule_addobject (gdb_module
, "Progspace",
531 (PyObject
*) &pspace_object_type
);
536 static gdb_PyGetSetDef pspace_getset
[] =
538 { "__dict__", gdb_py_generic_dict
, NULL
,
539 "The __dict__ for this progspace.", &pspace_object_type
},
540 { "filename", pspy_get_filename
, NULL
,
541 "The progspace's main filename, or None.", NULL
},
542 { "pretty_printers", pspy_get_printers
, pspy_set_printers
,
543 "Pretty printers.", NULL
},
544 { "frame_filters", pspy_get_frame_filters
, pspy_set_frame_filters
,
545 "Frame filters.", NULL
},
546 { "frame_unwinders", pspy_get_frame_unwinders
, pspy_set_frame_unwinders
,
547 "Frame unwinders.", NULL
},
548 { "type_printers", pspy_get_type_printers
, pspy_set_type_printers
,
549 "Type printers.", NULL
},
550 { "xmethods", pspy_get_xmethods
, NULL
,
551 "Debug methods.", NULL
},
555 static PyMethodDef progspace_object_methods
[] =
557 { "objfiles", pspy_get_objfiles
, METH_NOARGS
,
558 "Return a sequence of objfiles associated to this program space." },
559 { "solib_name", pspy_solib_name
, METH_VARARGS
,
560 "solib_name (Long) -> String.\n\
561 Return the name of the shared library holding a given address, or None." },
562 { "block_for_pc", pspy_block_for_pc
, METH_VARARGS
,
563 "Return the block containing the given pc value, or None." },
564 { "find_pc_line", pspy_find_pc_line
, METH_VARARGS
,
565 "find_pc_line (pc) -> Symtab_and_line.\n\
566 Return the gdb.Symtab_and_line object corresponding to the pc value." },
567 { "is_valid", pspy_is_valid
, METH_NOARGS
,
568 "is_valid () -> Boolean.\n\
569 Return true if this program space is valid, false if not." },
573 PyTypeObject pspace_object_type
=
575 PyVarObject_HEAD_INIT (NULL
, 0)
576 "gdb.Progspace", /*tp_name*/
577 sizeof (pspace_object
), /*tp_basicsize*/
579 pspy_dealloc
, /*tp_dealloc*/
586 0, /*tp_as_sequence*/
594 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
595 "GDB progspace object", /* tp_doc */
598 0, /* tp_richcompare */
599 0, /* tp_weaklistoffset */
602 progspace_object_methods
, /* tp_methods */
604 pspace_getset
, /* tp_getset */
607 0, /* tp_descr_get */
608 0, /* tp_descr_set */
609 offsetof (pspace_object
, dict
), /* tp_dictoffset */
612 pspy_new
, /* tp_new */