1 /* Python interface to program spaces.
3 Copyright (C) 2010-2013 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
, strlen (objfile
->name
),
64 host_charset (), NULL
);
70 pspy_dealloc (PyObject
*self
)
72 pspace_object
*ps_self
= (pspace_object
*) self
;
74 Py_XDECREF (ps_self
->printers
);
75 Py_XDECREF (ps_self
->frame_filters
);
76 Py_XDECREF (ps_self
->type_printers
);
77 Py_TYPE (self
)->tp_free (self
);
81 pspy_new (PyTypeObject
*type
, PyObject
*args
, PyObject
*keywords
)
83 pspace_object
*self
= (pspace_object
*) type
->tp_alloc (type
, 0);
89 self
->printers
= PyList_New (0);
96 self
->frame_filters
= PyDict_New ();
97 if (!self
->frame_filters
)
103 self
->type_printers
= PyList_New (0);
104 if (!self
->type_printers
)
110 return (PyObject
*) self
;
114 pspy_get_printers (PyObject
*o
, void *ignore
)
116 pspace_object
*self
= (pspace_object
*) o
;
118 Py_INCREF (self
->printers
);
119 return self
->printers
;
123 pspy_set_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
126 pspace_object
*self
= (pspace_object
*) o
;
130 PyErr_SetString (PyExc_TypeError
,
131 "cannot delete the pretty_printers attribute");
135 if (! PyList_Check (value
))
137 PyErr_SetString (PyExc_TypeError
,
138 "the pretty_printers attribute must be a list");
142 /* Take care in case the LHS and RHS are related somehow. */
143 tmp
= self
->printers
;
145 self
->printers
= value
;
151 /* Return the Python dictionary attribute containing frame filters for
152 this program space. */
154 pspy_get_frame_filters (PyObject
*o
, void *ignore
)
156 pspace_object
*self
= (pspace_object
*) o
;
158 Py_INCREF (self
->frame_filters
);
159 return self
->frame_filters
;
162 /* Set this object file's frame filters dictionary to FILTERS. */
164 pspy_set_frame_filters (PyObject
*o
, PyObject
*frame
, void *ignore
)
167 pspace_object
*self
= (pspace_object
*) o
;
171 PyErr_SetString (PyExc_TypeError
,
172 "cannot delete the frame filter attribute");
176 if (! PyDict_Check (frame
))
178 PyErr_SetString (PyExc_TypeError
,
179 "the frame filter attribute must be a dictionary");
183 /* Take care in case the LHS and RHS are related somehow. */
184 tmp
= self
->frame_filters
;
186 self
->frame_filters
= frame
;
192 /* Get the 'type_printers' attribute. */
195 pspy_get_type_printers (PyObject
*o
, void *ignore
)
197 pspace_object
*self
= (pspace_object
*) o
;
199 Py_INCREF (self
->type_printers
);
200 return self
->type_printers
;
203 /* Set the 'type_printers' attribute. */
206 pspy_set_type_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
209 pspace_object
*self
= (pspace_object
*) o
;
213 PyErr_SetString (PyExc_TypeError
,
214 "cannot delete the type_printers attribute");
218 if (! PyList_Check (value
))
220 PyErr_SetString (PyExc_TypeError
,
221 "the type_printers attribute must be a list");
225 /* Take care in case the LHS and RHS are related somehow. */
226 tmp
= self
->type_printers
;
228 self
->type_printers
= value
;
236 /* Clear the PSPACE pointer in a Pspace object and remove the reference. */
239 py_free_pspace (struct program_space
*pspace
, void *datum
)
241 struct cleanup
*cleanup
;
242 pspace_object
*object
= datum
;
243 struct gdbarch
*arch
= get_current_arch ();
245 cleanup
= ensure_python_env (arch
, current_language
);
246 object
->pspace
= NULL
;
247 Py_DECREF ((PyObject
*) object
);
248 do_cleanups (cleanup
);
251 /* Return a borrowed reference to the Python object of type Pspace
252 representing PSPACE. If the object has already been created,
253 return it. Otherwise, create it. Return NULL and set the Python
257 pspace_to_pspace_object (struct program_space
*pspace
)
259 pspace_object
*object
;
261 object
= program_space_data (pspace
, pspy_pspace_data_key
);
264 object
= PyObject_New (pspace_object
, &pspace_object_type
);
267 object
->pspace
= pspace
;
269 object
->printers
= PyList_New (0);
270 if (!object
->printers
)
276 object
->frame_filters
= PyDict_New ();
277 if (!object
->frame_filters
)
283 object
->type_printers
= PyList_New (0);
284 if (!object
->type_printers
)
290 set_program_space_data (pspace
, pspy_pspace_data_key
, object
);
294 return (PyObject
*) object
;
298 gdbpy_initialize_pspace (void)
301 = register_program_space_data_with_cleanup (NULL
, py_free_pspace
);
303 if (PyType_Ready (&pspace_object_type
) < 0)
306 return gdb_pymodule_addobject (gdb_module
, "Progspace",
307 (PyObject
*) &pspace_object_type
);
312 static PyGetSetDef pspace_getset
[] =
314 { "filename", pspy_get_filename
, NULL
,
315 "The progspace's main filename, or None.", NULL
},
316 { "pretty_printers", pspy_get_printers
, pspy_set_printers
,
317 "Pretty printers.", NULL
},
318 { "frame_filters", pspy_get_frame_filters
, pspy_set_frame_filters
,
319 "Frame filters.", NULL
},
320 { "type_printers", pspy_get_type_printers
, pspy_set_type_printers
,
321 "Type printers.", NULL
},
325 static PyTypeObject pspace_object_type
=
327 PyVarObject_HEAD_INIT (NULL
, 0)
328 "gdb.Progspace", /*tp_name*/
329 sizeof (pspace_object
), /*tp_basicsize*/
331 pspy_dealloc
, /*tp_dealloc*/
338 0, /*tp_as_sequence*/
346 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
347 "GDB progspace object", /* tp_doc */
350 0, /* tp_richcompare */
351 0, /* tp_weaklistoffset */
356 pspace_getset
, /* tp_getset */
359 0, /* tp_descr_get */
360 0, /* tp_descr_set */
361 0, /* tp_dictoffset */
364 pspy_new
, /* tp_new */