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"
32 /* The corresponding pspace. */
33 struct program_space
*pspace
;
35 /* Dictionary holding user-added attributes.
36 This is the __dict__ attribute of the object. */
39 /* The pretty-printer list of functions. */
42 /* The frame filter list of functions. */
43 PyObject
*frame_filters
;
45 /* The frame unwinder list. */
46 PyObject
*frame_unwinders
;
48 /* The type-printer list. */
49 PyObject
*type_printers
;
51 /* The debug method list. */
55 extern PyTypeObject pspace_object_type
56 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("pspace_object");
58 static const struct program_space_data
*pspy_pspace_data_key
;
62 /* An Objfile method which returns the objfile's file name, or None. */
65 pspy_get_filename (PyObject
*self
, void *closure
)
67 pspace_object
*obj
= (pspace_object
*) self
;
71 struct objfile
*objfile
= obj
->pspace
->symfile_object_file
;
74 return host_string_to_python_string (objfile_name (objfile
));
80 pspy_dealloc (PyObject
*self
)
82 pspace_object
*ps_self
= (pspace_object
*) self
;
84 Py_XDECREF (ps_self
->dict
);
85 Py_XDECREF (ps_self
->printers
);
86 Py_XDECREF (ps_self
->frame_filters
);
87 Py_XDECREF (ps_self
->frame_unwinders
);
88 Py_XDECREF (ps_self
->type_printers
);
89 Py_XDECREF (ps_self
->xmethods
);
90 Py_TYPE (self
)->tp_free (self
);
93 /* Initialize a pspace_object.
94 The result is a boolean indicating success. */
97 pspy_initialize (pspace_object
*self
)
101 self
->dict
= PyDict_New ();
102 if (self
->dict
== NULL
)
105 self
->printers
= PyList_New (0);
106 if (self
->printers
== NULL
)
109 self
->frame_filters
= PyDict_New ();
110 if (self
->frame_filters
== NULL
)
113 self
->frame_unwinders
= PyList_New (0);
114 if (self
->frame_unwinders
== NULL
)
117 self
->type_printers
= PyList_New (0);
118 if (self
->type_printers
== NULL
)
121 self
->xmethods
= PyList_New (0);
122 if (self
->xmethods
== NULL
)
129 pspy_new (PyTypeObject
*type
, PyObject
*args
, PyObject
*keywords
)
131 pspace_object
*self
= (pspace_object
*) type
->tp_alloc (type
, 0);
135 if (!pspy_initialize (self
))
142 return (PyObject
*) self
;
146 pspy_get_printers (PyObject
*o
, void *ignore
)
148 pspace_object
*self
= (pspace_object
*) o
;
150 Py_INCREF (self
->printers
);
151 return self
->printers
;
155 pspy_set_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
158 pspace_object
*self
= (pspace_object
*) o
;
162 PyErr_SetString (PyExc_TypeError
,
163 "cannot delete the pretty_printers attribute");
167 if (! PyList_Check (value
))
169 PyErr_SetString (PyExc_TypeError
,
170 "the pretty_printers attribute must be a list");
174 /* Take care in case the LHS and RHS are related somehow. */
175 tmp
= self
->printers
;
177 self
->printers
= value
;
183 /* Return the Python dictionary attribute containing frame filters for
184 this program space. */
186 pspy_get_frame_filters (PyObject
*o
, void *ignore
)
188 pspace_object
*self
= (pspace_object
*) o
;
190 Py_INCREF (self
->frame_filters
);
191 return self
->frame_filters
;
194 /* Set this object file's frame filters dictionary to FILTERS. */
196 pspy_set_frame_filters (PyObject
*o
, PyObject
*frame
, void *ignore
)
199 pspace_object
*self
= (pspace_object
*) o
;
203 PyErr_SetString (PyExc_TypeError
,
204 "cannot delete the frame filter attribute");
208 if (! PyDict_Check (frame
))
210 PyErr_SetString (PyExc_TypeError
,
211 "the frame filter attribute must be a dictionary");
215 /* Take care in case the LHS and RHS are related somehow. */
216 tmp
= self
->frame_filters
;
218 self
->frame_filters
= frame
;
224 /* Return the list of the frame unwinders for this program space. */
227 pspy_get_frame_unwinders (PyObject
*o
, void *ignore
)
229 pspace_object
*self
= (pspace_object
*) o
;
231 Py_INCREF (self
->frame_unwinders
);
232 return self
->frame_unwinders
;
235 /* Set this program space's list of the unwinders to UNWINDERS. */
238 pspy_set_frame_unwinders (PyObject
*o
, PyObject
*unwinders
, void *ignore
)
241 pspace_object
*self
= (pspace_object
*) o
;
245 PyErr_SetString (PyExc_TypeError
,
246 "cannot delete the frame unwinders list");
250 if (!PyList_Check (unwinders
))
252 PyErr_SetString (PyExc_TypeError
,
253 "the frame unwinders attribute must be a list");
257 /* Take care in case the LHS and RHS are related somehow. */
258 tmp
= self
->frame_unwinders
;
259 Py_INCREF (unwinders
);
260 self
->frame_unwinders
= unwinders
;
266 /* Get the 'type_printers' attribute. */
269 pspy_get_type_printers (PyObject
*o
, void *ignore
)
271 pspace_object
*self
= (pspace_object
*) o
;
273 Py_INCREF (self
->type_printers
);
274 return self
->type_printers
;
277 /* Get the 'xmethods' attribute. */
280 pspy_get_xmethods (PyObject
*o
, void *ignore
)
282 pspace_object
*self
= (pspace_object
*) o
;
284 Py_INCREF (self
->xmethods
);
285 return self
->xmethods
;
288 /* Set the 'type_printers' attribute. */
291 pspy_set_type_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
294 pspace_object
*self
= (pspace_object
*) o
;
298 PyErr_SetString (PyExc_TypeError
,
299 "cannot delete the type_printers attribute");
303 if (! PyList_Check (value
))
305 PyErr_SetString (PyExc_TypeError
,
306 "the type_printers attribute must be a list");
310 /* Take care in case the LHS and RHS are related somehow. */
311 tmp
= self
->type_printers
;
313 self
->type_printers
= value
;
321 /* Clear the PSPACE pointer in a Pspace object and remove the reference. */
324 py_free_pspace (struct program_space
*pspace
, void *datum
)
326 pspace_object
*object
= (pspace_object
*) datum
;
327 /* This is a fiction, but we're in a nasty spot: The pspace is in the
328 process of being deleted, we can't rely on anything in it. Plus
329 this is one time when the current program space and current inferior
330 are not in sync: All inferiors that use PSPACE may no longer exist.
331 We don't need to do much here, and since "there is always an inferior"
332 using target_gdbarch suffices.
333 Note: We cannot call get_current_arch because it may try to access
334 the target, which may involve accessing data in the pspace currently
336 struct gdbarch
*arch
= target_gdbarch ();
338 gdbpy_enter
enter_py (arch
, current_language
);
339 object
->pspace
= NULL
;
340 Py_DECREF ((PyObject
*) object
);
343 /* Return a borrowed reference to the Python object of type Pspace
344 representing PSPACE. If the object has already been created,
345 return it. Otherwise, create it. Return NULL and set the Python
349 pspace_to_pspace_object (struct program_space
*pspace
)
351 pspace_object
*object
;
353 object
= (pspace_object
*) program_space_data (pspace
, pspy_pspace_data_key
);
356 object
= PyObject_New (pspace_object
, &pspace_object_type
);
359 if (!pspy_initialize (object
))
365 object
->pspace
= pspace
;
366 set_program_space_data (pspace
, pspy_pspace_data_key
, object
);
370 return (PyObject
*) object
;
374 gdbpy_initialize_pspace (void)
377 = register_program_space_data_with_cleanup (NULL
, py_free_pspace
);
379 if (PyType_Ready (&pspace_object_type
) < 0)
382 return gdb_pymodule_addobject (gdb_module
, "Progspace",
383 (PyObject
*) &pspace_object_type
);
388 static PyGetSetDef pspace_getset
[] =
390 { "__dict__", gdb_py_generic_dict
, NULL
,
391 "The __dict__ for this progspace.", &pspace_object_type
},
392 { "filename", pspy_get_filename
, NULL
,
393 "The progspace's main filename, or None.", NULL
},
394 { "pretty_printers", pspy_get_printers
, pspy_set_printers
,
395 "Pretty printers.", NULL
},
396 { "frame_filters", pspy_get_frame_filters
, pspy_set_frame_filters
,
397 "Frame filters.", NULL
},
398 { "frame_unwinders", pspy_get_frame_unwinders
, pspy_set_frame_unwinders
,
399 "Frame unwinders.", NULL
},
400 { "type_printers", pspy_get_type_printers
, pspy_set_type_printers
,
401 "Type printers.", NULL
},
402 { "xmethods", pspy_get_xmethods
, NULL
,
403 "Debug methods.", NULL
},
407 PyTypeObject pspace_object_type
=
409 PyVarObject_HEAD_INIT (NULL
, 0)
410 "gdb.Progspace", /*tp_name*/
411 sizeof (pspace_object
), /*tp_basicsize*/
413 pspy_dealloc
, /*tp_dealloc*/
420 0, /*tp_as_sequence*/
428 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
429 "GDB progspace object", /* tp_doc */
432 0, /* tp_richcompare */
433 0, /* tp_weaklistoffset */
438 pspace_getset
, /* tp_getset */
441 0, /* tp_descr_get */
442 0, /* tp_descr_set */
443 offsetof (pspace_object
, dict
), /* tp_dictoffset */
446 pspy_new
, /* tp_new */