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
;
43 /* The debug method list. */
47 static PyTypeObject pspace_object_type
48 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("pspace_object");
50 static const struct program_space_data
*pspy_pspace_data_key
;
54 /* An Objfile method which returns the objfile's file name, or None. */
57 pspy_get_filename (PyObject
*self
, void *closure
)
59 pspace_object
*obj
= (pspace_object
*) self
;
63 struct objfile
*objfile
= obj
->pspace
->symfile_object_file
;
66 return PyString_Decode (objfile_name (objfile
),
67 strlen (objfile_name (objfile
)),
68 host_charset (), NULL
);
74 pspy_dealloc (PyObject
*self
)
76 pspace_object
*ps_self
= (pspace_object
*) self
;
78 Py_XDECREF (ps_self
->printers
);
79 Py_XDECREF (ps_self
->frame_filters
);
80 Py_XDECREF (ps_self
->type_printers
);
81 Py_XDECREF (ps_self
->xmethods
);
82 Py_TYPE (self
)->tp_free (self
);
86 pspy_new (PyTypeObject
*type
, PyObject
*args
, PyObject
*keywords
)
88 pspace_object
*self
= (pspace_object
*) type
->tp_alloc (type
, 0);
94 self
->printers
= PyList_New (0);
101 self
->frame_filters
= PyDict_New ();
102 if (!self
->frame_filters
)
108 self
->type_printers
= PyList_New (0);
109 if (!self
->type_printers
)
115 self
->xmethods
= PyList_New (0);
116 if (self
->xmethods
== NULL
)
122 return (PyObject
*) self
;
126 pspy_get_printers (PyObject
*o
, void *ignore
)
128 pspace_object
*self
= (pspace_object
*) o
;
130 Py_INCREF (self
->printers
);
131 return self
->printers
;
135 pspy_set_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
138 pspace_object
*self
= (pspace_object
*) o
;
142 PyErr_SetString (PyExc_TypeError
,
143 "cannot delete the pretty_printers attribute");
147 if (! PyList_Check (value
))
149 PyErr_SetString (PyExc_TypeError
,
150 "the pretty_printers attribute must be a list");
154 /* Take care in case the LHS and RHS are related somehow. */
155 tmp
= self
->printers
;
157 self
->printers
= value
;
163 /* Return the Python dictionary attribute containing frame filters for
164 this program space. */
166 pspy_get_frame_filters (PyObject
*o
, void *ignore
)
168 pspace_object
*self
= (pspace_object
*) o
;
170 Py_INCREF (self
->frame_filters
);
171 return self
->frame_filters
;
174 /* Set this object file's frame filters dictionary to FILTERS. */
176 pspy_set_frame_filters (PyObject
*o
, PyObject
*frame
, void *ignore
)
179 pspace_object
*self
= (pspace_object
*) o
;
183 PyErr_SetString (PyExc_TypeError
,
184 "cannot delete the frame filter attribute");
188 if (! PyDict_Check (frame
))
190 PyErr_SetString (PyExc_TypeError
,
191 "the frame filter attribute must be a dictionary");
195 /* Take care in case the LHS and RHS are related somehow. */
196 tmp
= self
->frame_filters
;
198 self
->frame_filters
= frame
;
204 /* Get the 'type_printers' attribute. */
207 pspy_get_type_printers (PyObject
*o
, void *ignore
)
209 pspace_object
*self
= (pspace_object
*) o
;
211 Py_INCREF (self
->type_printers
);
212 return self
->type_printers
;
215 /* Get the 'xmethods' attribute. */
218 pspy_get_xmethods (PyObject
*o
, void *ignore
)
220 pspace_object
*self
= (pspace_object
*) o
;
222 Py_INCREF (self
->xmethods
);
223 return self
->xmethods
;
226 /* Set the 'type_printers' attribute. */
229 pspy_set_type_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
232 pspace_object
*self
= (pspace_object
*) o
;
236 PyErr_SetString (PyExc_TypeError
,
237 "cannot delete the type_printers attribute");
241 if (! PyList_Check (value
))
243 PyErr_SetString (PyExc_TypeError
,
244 "the type_printers attribute must be a list");
248 /* Take care in case the LHS and RHS are related somehow. */
249 tmp
= self
->type_printers
;
251 self
->type_printers
= value
;
259 /* Clear the PSPACE pointer in a Pspace object and remove the reference. */
262 py_free_pspace (struct program_space
*pspace
, void *datum
)
264 struct cleanup
*cleanup
;
265 pspace_object
*object
= datum
;
266 /* This is a fiction, but we're in a nasty spot: The pspace is in the
267 process of being deleted, we can't rely on anything in it. Plus
268 this is one time when the current program space and current inferior
269 are not in sync: All inferiors that use PSPACE may no longer exist.
270 We don't need to do much here, and since "there is always an inferior"
271 using target_gdbarch suffices.
272 Note: We cannot call get_current_arch because it may try to access
273 the target, which may involve accessing data in the pspace currently
275 struct gdbarch
*arch
= target_gdbarch ();
277 cleanup
= ensure_python_env (arch
, current_language
);
278 object
->pspace
= NULL
;
279 Py_DECREF ((PyObject
*) object
);
280 do_cleanups (cleanup
);
283 /* Return a borrowed reference to the Python object of type Pspace
284 representing PSPACE. If the object has already been created,
285 return it. Otherwise, create it. Return NULL and set the Python
289 pspace_to_pspace_object (struct program_space
*pspace
)
291 pspace_object
*object
;
293 object
= program_space_data (pspace
, pspy_pspace_data_key
);
296 object
= PyObject_New (pspace_object
, &pspace_object_type
);
299 object
->pspace
= pspace
;
301 object
->printers
= PyList_New (0);
302 if (!object
->printers
)
308 object
->frame_filters
= PyDict_New ();
309 if (!object
->frame_filters
)
315 object
->type_printers
= PyList_New (0);
316 if (!object
->type_printers
)
322 object
->xmethods
= PyList_New (0);
323 if (object
->xmethods
== NULL
)
329 set_program_space_data (pspace
, pspy_pspace_data_key
, object
);
333 return (PyObject
*) object
;
337 gdbpy_initialize_pspace (void)
340 = register_program_space_data_with_cleanup (NULL
, py_free_pspace
);
342 if (PyType_Ready (&pspace_object_type
) < 0)
345 return gdb_pymodule_addobject (gdb_module
, "Progspace",
346 (PyObject
*) &pspace_object_type
);
351 static PyGetSetDef pspace_getset
[] =
353 { "filename", pspy_get_filename
, NULL
,
354 "The progspace's main filename, or None.", NULL
},
355 { "pretty_printers", pspy_get_printers
, pspy_set_printers
,
356 "Pretty printers.", NULL
},
357 { "frame_filters", pspy_get_frame_filters
, pspy_set_frame_filters
,
358 "Frame filters.", NULL
},
359 { "type_printers", pspy_get_type_printers
, pspy_set_type_printers
,
360 "Type printers.", NULL
},
361 { "xmethods", pspy_get_xmethods
, NULL
,
362 "Debug methods.", NULL
},
366 static PyTypeObject pspace_object_type
=
368 PyVarObject_HEAD_INIT (NULL
, 0)
369 "gdb.Progspace", /*tp_name*/
370 sizeof (pspace_object
), /*tp_basicsize*/
372 pspy_dealloc
, /*tp_dealloc*/
379 0, /*tp_as_sequence*/
387 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
388 "GDB progspace object", /* tp_doc */
391 0, /* tp_richcompare */
392 0, /* tp_weaklistoffset */
397 pspace_getset
, /* tp_getset */
400 0, /* tp_descr_get */
401 0, /* tp_descr_set */
402 0, /* tp_dictoffset */
405 pspy_new
, /* tp_new */