1 /* Python interface to program spaces.
3 Copyright (C) 2010-2017 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"
33 /* The corresponding pspace. */
34 struct program_space
*pspace
;
36 /* Dictionary holding user-added attributes.
37 This is the __dict__ attribute of the object. */
40 /* The pretty-printer list of functions. */
43 /* The frame filter list of functions. */
44 PyObject
*frame_filters
;
46 /* The frame unwinder list. */
47 PyObject
*frame_unwinders
;
49 /* The type-printer list. */
50 PyObject
*type_printers
;
52 /* The debug method list. */
56 extern PyTypeObject pspace_object_type
57 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("pspace_object");
59 static const struct program_space_data
*pspy_pspace_data_key
;
63 /* An Objfile method which returns the objfile's file name, or None. */
66 pspy_get_filename (PyObject
*self
, void *closure
)
68 pspace_object
*obj
= (pspace_object
*) self
;
72 struct objfile
*objfile
= obj
->pspace
->symfile_object_file
;
75 return host_string_to_python_string (objfile_name (objfile
));
81 pspy_dealloc (PyObject
*self
)
83 pspace_object
*ps_self
= (pspace_object
*) self
;
85 Py_XDECREF (ps_self
->dict
);
86 Py_XDECREF (ps_self
->printers
);
87 Py_XDECREF (ps_self
->frame_filters
);
88 Py_XDECREF (ps_self
->frame_unwinders
);
89 Py_XDECREF (ps_self
->type_printers
);
90 Py_XDECREF (ps_self
->xmethods
);
91 Py_TYPE (self
)->tp_free (self
);
94 /* Initialize a pspace_object.
95 The result is a boolean indicating success. */
98 pspy_initialize (pspace_object
*self
)
102 self
->dict
= PyDict_New ();
103 if (self
->dict
== NULL
)
106 self
->printers
= PyList_New (0);
107 if (self
->printers
== NULL
)
110 self
->frame_filters
= PyDict_New ();
111 if (self
->frame_filters
== NULL
)
114 self
->frame_unwinders
= PyList_New (0);
115 if (self
->frame_unwinders
== NULL
)
118 self
->type_printers
= PyList_New (0);
119 if (self
->type_printers
== NULL
)
122 self
->xmethods
= PyList_New (0);
123 if (self
->xmethods
== NULL
)
130 pspy_new (PyTypeObject
*type
, PyObject
*args
, PyObject
*keywords
)
132 gdbpy_ref
<pspace_object
> self ((pspace_object
*) type
->tp_alloc (type
, 0));
136 if (!pspy_initialize (self
.get ()))
140 return (PyObject
*) self
.release ();
144 pspy_get_printers (PyObject
*o
, void *ignore
)
146 pspace_object
*self
= (pspace_object
*) o
;
148 Py_INCREF (self
->printers
);
149 return self
->printers
;
153 pspy_set_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
156 pspace_object
*self
= (pspace_object
*) o
;
160 PyErr_SetString (PyExc_TypeError
,
161 "cannot delete the pretty_printers attribute");
165 if (! PyList_Check (value
))
167 PyErr_SetString (PyExc_TypeError
,
168 "the pretty_printers attribute must be a list");
172 /* Take care in case the LHS and RHS are related somehow. */
173 tmp
= self
->printers
;
175 self
->printers
= value
;
181 /* Return the Python dictionary attribute containing frame filters for
182 this program space. */
184 pspy_get_frame_filters (PyObject
*o
, void *ignore
)
186 pspace_object
*self
= (pspace_object
*) o
;
188 Py_INCREF (self
->frame_filters
);
189 return self
->frame_filters
;
192 /* Set this object file's frame filters dictionary to FILTERS. */
194 pspy_set_frame_filters (PyObject
*o
, PyObject
*frame
, void *ignore
)
197 pspace_object
*self
= (pspace_object
*) o
;
201 PyErr_SetString (PyExc_TypeError
,
202 "cannot delete the frame filter attribute");
206 if (! PyDict_Check (frame
))
208 PyErr_SetString (PyExc_TypeError
,
209 "the frame filter attribute must be a dictionary");
213 /* Take care in case the LHS and RHS are related somehow. */
214 tmp
= self
->frame_filters
;
216 self
->frame_filters
= frame
;
222 /* Return the list of the frame unwinders for this program space. */
225 pspy_get_frame_unwinders (PyObject
*o
, void *ignore
)
227 pspace_object
*self
= (pspace_object
*) o
;
229 Py_INCREF (self
->frame_unwinders
);
230 return self
->frame_unwinders
;
233 /* Set this program space's list of the unwinders to UNWINDERS. */
236 pspy_set_frame_unwinders (PyObject
*o
, PyObject
*unwinders
, void *ignore
)
239 pspace_object
*self
= (pspace_object
*) o
;
243 PyErr_SetString (PyExc_TypeError
,
244 "cannot delete the frame unwinders list");
248 if (!PyList_Check (unwinders
))
250 PyErr_SetString (PyExc_TypeError
,
251 "the frame unwinders attribute must be a list");
255 /* Take care in case the LHS and RHS are related somehow. */
256 tmp
= self
->frame_unwinders
;
257 Py_INCREF (unwinders
);
258 self
->frame_unwinders
= unwinders
;
264 /* Get the 'type_printers' attribute. */
267 pspy_get_type_printers (PyObject
*o
, void *ignore
)
269 pspace_object
*self
= (pspace_object
*) o
;
271 Py_INCREF (self
->type_printers
);
272 return self
->type_printers
;
275 /* Get the 'xmethods' attribute. */
278 pspy_get_xmethods (PyObject
*o
, void *ignore
)
280 pspace_object
*self
= (pspace_object
*) o
;
282 Py_INCREF (self
->xmethods
);
283 return self
->xmethods
;
286 /* Set the 'type_printers' attribute. */
289 pspy_set_type_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
292 pspace_object
*self
= (pspace_object
*) o
;
296 PyErr_SetString (PyExc_TypeError
,
297 "cannot delete the type_printers attribute");
301 if (! PyList_Check (value
))
303 PyErr_SetString (PyExc_TypeError
,
304 "the type_printers attribute must be a list");
308 /* Take care in case the LHS and RHS are related somehow. */
309 tmp
= self
->type_printers
;
311 self
->type_printers
= value
;
319 /* Clear the PSPACE pointer in a Pspace object and remove the reference. */
322 py_free_pspace (struct program_space
*pspace
, void *datum
)
324 /* This is a fiction, but we're in a nasty spot: The pspace is in the
325 process of being deleted, we can't rely on anything in it. Plus
326 this is one time when the current program space and current inferior
327 are not in sync: All inferiors that use PSPACE may no longer exist.
328 We don't need to do much here, and since "there is always an inferior"
329 using target_gdbarch suffices.
330 Note: We cannot call get_current_arch because it may try to access
331 the target, which may involve accessing data in the pspace currently
333 struct gdbarch
*arch
= target_gdbarch ();
335 gdbpy_enter
enter_py (arch
, current_language
);
336 gdbpy_ref
<pspace_object
> object ((pspace_object
*) datum
);
337 object
->pspace
= NULL
;
340 /* Return a borrowed reference to the Python object of type Pspace
341 representing PSPACE. If the object has already been created,
342 return it. Otherwise, create it. Return NULL and set the Python
346 pspace_to_pspace_object (struct program_space
*pspace
)
348 gdbpy_ref
<pspace_object
> object
349 ((pspace_object
*) program_space_data (pspace
, pspy_pspace_data_key
));
352 object
.reset (PyObject_New (pspace_object
, &pspace_object_type
));
355 if (!pspy_initialize (object
.get ()))
358 object
->pspace
= pspace
;
359 set_program_space_data (pspace
, pspy_pspace_data_key
, object
.get ());
363 return (PyObject
*) object
.release ();
367 gdbpy_initialize_pspace (void)
370 = register_program_space_data_with_cleanup (NULL
, py_free_pspace
);
372 if (PyType_Ready (&pspace_object_type
) < 0)
375 return gdb_pymodule_addobject (gdb_module
, "Progspace",
376 (PyObject
*) &pspace_object_type
);
381 static PyGetSetDef pspace_getset
[] =
383 { "__dict__", gdb_py_generic_dict
, NULL
,
384 "The __dict__ for this progspace.", &pspace_object_type
},
385 { "filename", pspy_get_filename
, NULL
,
386 "The progspace's main filename, or None.", NULL
},
387 { "pretty_printers", pspy_get_printers
, pspy_set_printers
,
388 "Pretty printers.", NULL
},
389 { "frame_filters", pspy_get_frame_filters
, pspy_set_frame_filters
,
390 "Frame filters.", NULL
},
391 { "frame_unwinders", pspy_get_frame_unwinders
, pspy_set_frame_unwinders
,
392 "Frame unwinders.", NULL
},
393 { "type_printers", pspy_get_type_printers
, pspy_set_type_printers
,
394 "Type printers.", NULL
},
395 { "xmethods", pspy_get_xmethods
, NULL
,
396 "Debug methods.", NULL
},
400 PyTypeObject pspace_object_type
=
402 PyVarObject_HEAD_INIT (NULL
, 0)
403 "gdb.Progspace", /*tp_name*/
404 sizeof (pspace_object
), /*tp_basicsize*/
406 pspy_dealloc
, /*tp_dealloc*/
413 0, /*tp_as_sequence*/
421 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
422 "GDB progspace object", /* tp_doc */
425 0, /* tp_richcompare */
426 0, /* tp_weaklistoffset */
431 pspace_getset
, /* tp_getset */
434 0, /* tp_descr_get */
435 0, /* tp_descr_set */
436 offsetof (pspace_object
, dict
), /* tp_dictoffset */
439 pspy_new
, /* tp_new */