1 /* Python interface to program spaces.
3 Copyright (C) 2010-2014 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 /* The pretty-printer list of functions. */
38 /* The frame filter list of functions. */
39 PyObject
*frame_filters
;
40 /* The type-printer list. */
41 PyObject
*type_printers
;
44 static PyTypeObject pspace_object_type
45 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("pspace_object");
47 static const struct program_space_data
*pspy_pspace_data_key
;
51 /* An Objfile method which returns the objfile's file name, or None. */
54 pspy_get_filename (PyObject
*self
, void *closure
)
56 pspace_object
*obj
= (pspace_object
*) self
;
60 struct objfile
*objfile
= obj
->pspace
->symfile_object_file
;
63 return PyString_Decode (objfile_name (objfile
),
64 strlen (objfile_name (objfile
)),
65 host_charset (), NULL
);
71 pspy_dealloc (PyObject
*self
)
73 pspace_object
*ps_self
= (pspace_object
*) self
;
75 Py_XDECREF (ps_self
->printers
);
76 Py_XDECREF (ps_self
->frame_filters
);
77 Py_XDECREF (ps_self
->type_printers
);
78 Py_TYPE (self
)->tp_free (self
);
82 pspy_new (PyTypeObject
*type
, PyObject
*args
, PyObject
*keywords
)
84 pspace_object
*self
= (pspace_object
*) type
->tp_alloc (type
, 0);
90 self
->printers
= PyList_New (0);
97 self
->frame_filters
= PyDict_New ();
98 if (!self
->frame_filters
)
104 self
->type_printers
= PyList_New (0);
105 if (!self
->type_printers
)
111 return (PyObject
*) self
;
115 pspy_get_printers (PyObject
*o
, void *ignore
)
117 pspace_object
*self
= (pspace_object
*) o
;
119 Py_INCREF (self
->printers
);
120 return self
->printers
;
124 pspy_set_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
127 pspace_object
*self
= (pspace_object
*) o
;
131 PyErr_SetString (PyExc_TypeError
,
132 "cannot delete the pretty_printers attribute");
136 if (! PyList_Check (value
))
138 PyErr_SetString (PyExc_TypeError
,
139 "the pretty_printers attribute must be a list");
143 /* Take care in case the LHS and RHS are related somehow. */
144 tmp
= self
->printers
;
146 self
->printers
= value
;
152 /* Return the Python dictionary attribute containing frame filters for
153 this program space. */
155 pspy_get_frame_filters (PyObject
*o
, void *ignore
)
157 pspace_object
*self
= (pspace_object
*) o
;
159 Py_INCREF (self
->frame_filters
);
160 return self
->frame_filters
;
163 /* Set this object file's frame filters dictionary to FILTERS. */
165 pspy_set_frame_filters (PyObject
*o
, PyObject
*frame
, void *ignore
)
168 pspace_object
*self
= (pspace_object
*) o
;
172 PyErr_SetString (PyExc_TypeError
,
173 "cannot delete the frame filter attribute");
177 if (! PyDict_Check (frame
))
179 PyErr_SetString (PyExc_TypeError
,
180 "the frame filter attribute must be a dictionary");
184 /* Take care in case the LHS and RHS are related somehow. */
185 tmp
= self
->frame_filters
;
187 self
->frame_filters
= frame
;
193 /* Get the 'type_printers' attribute. */
196 pspy_get_type_printers (PyObject
*o
, void *ignore
)
198 pspace_object
*self
= (pspace_object
*) o
;
200 Py_INCREF (self
->type_printers
);
201 return self
->type_printers
;
204 /* Set the 'type_printers' attribute. */
207 pspy_set_type_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
210 pspace_object
*self
= (pspace_object
*) o
;
214 PyErr_SetString (PyExc_TypeError
,
215 "cannot delete the type_printers attribute");
219 if (! PyList_Check (value
))
221 PyErr_SetString (PyExc_TypeError
,
222 "the type_printers attribute must be a list");
226 /* Take care in case the LHS and RHS are related somehow. */
227 tmp
= self
->type_printers
;
229 self
->type_printers
= value
;
237 /* Clear the PSPACE pointer in a Pspace object and remove the reference. */
240 py_free_pspace (struct program_space
*pspace
, void *datum
)
242 struct cleanup
*cleanup
;
243 pspace_object
*object
= datum
;
244 struct gdbarch
*arch
= get_current_arch ();
246 cleanup
= ensure_python_env (arch
, current_language
);
247 object
->pspace
= NULL
;
248 Py_DECREF ((PyObject
*) object
);
249 do_cleanups (cleanup
);
252 /* Return a borrowed reference to the Python object of type Pspace
253 representing PSPACE. If the object has already been created,
254 return it. Otherwise, create it. Return NULL and set the Python
258 pspace_to_pspace_object (struct program_space
*pspace
)
260 pspace_object
*object
;
262 object
= program_space_data (pspace
, pspy_pspace_data_key
);
265 object
= PyObject_New (pspace_object
, &pspace_object_type
);
268 object
->pspace
= pspace
;
270 object
->printers
= PyList_New (0);
271 if (!object
->printers
)
277 object
->frame_filters
= PyDict_New ();
278 if (!object
->frame_filters
)
284 object
->type_printers
= PyList_New (0);
285 if (!object
->type_printers
)
291 set_program_space_data (pspace
, pspy_pspace_data_key
, object
);
295 return (PyObject
*) object
;
299 gdbpy_initialize_pspace (void)
302 = register_program_space_data_with_cleanup (NULL
, py_free_pspace
);
304 if (PyType_Ready (&pspace_object_type
) < 0)
307 return gdb_pymodule_addobject (gdb_module
, "Progspace",
308 (PyObject
*) &pspace_object_type
);
313 static PyGetSetDef pspace_getset
[] =
315 { "filename", pspy_get_filename
, NULL
,
316 "The progspace's main filename, or None.", NULL
},
317 { "pretty_printers", pspy_get_printers
, pspy_set_printers
,
318 "Pretty printers.", NULL
},
319 { "frame_filters", pspy_get_frame_filters
, pspy_set_frame_filters
,
320 "Frame filters.", NULL
},
321 { "type_printers", pspy_get_type_printers
, pspy_set_type_printers
,
322 "Type printers.", NULL
},
326 static PyTypeObject pspace_object_type
=
328 PyVarObject_HEAD_INIT (NULL
, 0)
329 "gdb.Progspace", /*tp_name*/
330 sizeof (pspace_object
), /*tp_basicsize*/
332 pspy_dealloc
, /*tp_dealloc*/
339 0, /*tp_as_sequence*/
347 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
348 "GDB progspace object", /* tp_doc */
351 0, /* tp_richcompare */
352 0, /* tp_weaklistoffset */
357 pspace_getset
, /* tp_getset */
360 0, /* tp_descr_get */
361 0, /* tp_descr_set */
362 0, /* tp_dictoffset */
365 pspy_new
, /* tp_new */