1 /* Python interface to program spaces.
3 Copyright (C) 2010-2015 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
;
44 /* The type-printer list. */
45 PyObject
*type_printers
;
47 /* The debug method list. */
51 extern PyTypeObject pspace_object_type
52 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("pspace_object");
54 static const struct program_space_data
*pspy_pspace_data_key
;
58 /* An Objfile method which returns the objfile's file name, or None. */
61 pspy_get_filename (PyObject
*self
, void *closure
)
63 pspace_object
*obj
= (pspace_object
*) self
;
67 struct objfile
*objfile
= obj
->pspace
->symfile_object_file
;
70 return PyString_Decode (objfile_name (objfile
),
71 strlen (objfile_name (objfile
)),
72 host_charset (), NULL
);
78 pspy_dealloc (PyObject
*self
)
80 pspace_object
*ps_self
= (pspace_object
*) self
;
82 Py_XDECREF (ps_self
->dict
);
83 Py_XDECREF (ps_self
->printers
);
84 Py_XDECREF (ps_self
->frame_filters
);
85 Py_XDECREF (ps_self
->type_printers
);
86 Py_XDECREF (ps_self
->xmethods
);
87 Py_TYPE (self
)->tp_free (self
);
90 /* Initialize a pspace_object.
91 The result is a boolean indicating success. */
94 pspy_initialize (pspace_object
*self
)
99 self
->printers
= PyList_New (0);
100 if (self
->printers
== NULL
)
103 self
->frame_filters
= PyDict_New ();
104 if (self
->frame_filters
== NULL
)
107 self
->type_printers
= PyList_New (0);
108 if (self
->type_printers
== NULL
)
111 self
->xmethods
= PyList_New (0);
112 if (self
->xmethods
== NULL
)
119 pspy_new (PyTypeObject
*type
, PyObject
*args
, PyObject
*keywords
)
121 pspace_object
*self
= (pspace_object
*) type
->tp_alloc (type
, 0);
125 if (!pspy_initialize (self
))
132 return (PyObject
*) self
;
136 pspy_get_printers (PyObject
*o
, void *ignore
)
138 pspace_object
*self
= (pspace_object
*) o
;
140 Py_INCREF (self
->printers
);
141 return self
->printers
;
145 pspy_set_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
148 pspace_object
*self
= (pspace_object
*) o
;
152 PyErr_SetString (PyExc_TypeError
,
153 "cannot delete the pretty_printers attribute");
157 if (! PyList_Check (value
))
159 PyErr_SetString (PyExc_TypeError
,
160 "the pretty_printers attribute must be a list");
164 /* Take care in case the LHS and RHS are related somehow. */
165 tmp
= self
->printers
;
167 self
->printers
= value
;
173 /* Return the Python dictionary attribute containing frame filters for
174 this program space. */
176 pspy_get_frame_filters (PyObject
*o
, void *ignore
)
178 pspace_object
*self
= (pspace_object
*) o
;
180 Py_INCREF (self
->frame_filters
);
181 return self
->frame_filters
;
184 /* Set this object file's frame filters dictionary to FILTERS. */
186 pspy_set_frame_filters (PyObject
*o
, PyObject
*frame
, void *ignore
)
189 pspace_object
*self
= (pspace_object
*) o
;
193 PyErr_SetString (PyExc_TypeError
,
194 "cannot delete the frame filter attribute");
198 if (! PyDict_Check (frame
))
200 PyErr_SetString (PyExc_TypeError
,
201 "the frame filter attribute must be a dictionary");
205 /* Take care in case the LHS and RHS are related somehow. */
206 tmp
= self
->frame_filters
;
208 self
->frame_filters
= frame
;
214 /* Get the 'type_printers' attribute. */
217 pspy_get_type_printers (PyObject
*o
, void *ignore
)
219 pspace_object
*self
= (pspace_object
*) o
;
221 Py_INCREF (self
->type_printers
);
222 return self
->type_printers
;
225 /* Get the 'xmethods' attribute. */
228 pspy_get_xmethods (PyObject
*o
, void *ignore
)
230 pspace_object
*self
= (pspace_object
*) o
;
232 Py_INCREF (self
->xmethods
);
233 return self
->xmethods
;
236 /* Set the 'type_printers' attribute. */
239 pspy_set_type_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
242 pspace_object
*self
= (pspace_object
*) o
;
246 PyErr_SetString (PyExc_TypeError
,
247 "cannot delete the type_printers attribute");
251 if (! PyList_Check (value
))
253 PyErr_SetString (PyExc_TypeError
,
254 "the type_printers attribute must be a list");
258 /* Take care in case the LHS and RHS are related somehow. */
259 tmp
= self
->type_printers
;
261 self
->type_printers
= value
;
269 /* Clear the PSPACE pointer in a Pspace object and remove the reference. */
272 py_free_pspace (struct program_space
*pspace
, void *datum
)
274 struct cleanup
*cleanup
;
275 pspace_object
*object
= datum
;
276 /* This is a fiction, but we're in a nasty spot: The pspace is in the
277 process of being deleted, we can't rely on anything in it. Plus
278 this is one time when the current program space and current inferior
279 are not in sync: All inferiors that use PSPACE may no longer exist.
280 We don't need to do much here, and since "there is always an inferior"
281 using target_gdbarch suffices.
282 Note: We cannot call get_current_arch because it may try to access
283 the target, which may involve accessing data in the pspace currently
285 struct gdbarch
*arch
= target_gdbarch ();
287 cleanup
= ensure_python_env (arch
, current_language
);
288 object
->pspace
= NULL
;
289 Py_DECREF ((PyObject
*) object
);
290 do_cleanups (cleanup
);
293 /* Return a borrowed reference to the Python object of type Pspace
294 representing PSPACE. If the object has already been created,
295 return it. Otherwise, create it. Return NULL and set the Python
299 pspace_to_pspace_object (struct program_space
*pspace
)
301 pspace_object
*object
;
303 object
= program_space_data (pspace
, pspy_pspace_data_key
);
306 object
= PyObject_New (pspace_object
, &pspace_object_type
);
309 if (!pspy_initialize (object
))
315 object
->pspace
= pspace
;
316 set_program_space_data (pspace
, pspy_pspace_data_key
, object
);
320 return (PyObject
*) object
;
324 gdbpy_initialize_pspace (void)
327 = register_program_space_data_with_cleanup (NULL
, py_free_pspace
);
329 if (PyType_Ready (&pspace_object_type
) < 0)
332 return gdb_pymodule_addobject (gdb_module
, "Progspace",
333 (PyObject
*) &pspace_object_type
);
338 static PyGetSetDef pspace_getset
[] =
340 { "__dict__", gdb_py_generic_dict
, NULL
,
341 "The __dict__ for this progspace.", &pspace_object_type
},
342 { "filename", pspy_get_filename
, NULL
,
343 "The progspace's main filename, or None.", NULL
},
344 { "pretty_printers", pspy_get_printers
, pspy_set_printers
,
345 "Pretty printers.", NULL
},
346 { "frame_filters", pspy_get_frame_filters
, pspy_set_frame_filters
,
347 "Frame filters.", NULL
},
348 { "type_printers", pspy_get_type_printers
, pspy_set_type_printers
,
349 "Type printers.", NULL
},
350 { "xmethods", pspy_get_xmethods
, NULL
,
351 "Debug methods.", NULL
},
355 PyTypeObject pspace_object_type
=
357 PyVarObject_HEAD_INIT (NULL
, 0)
358 "gdb.Progspace", /*tp_name*/
359 sizeof (pspace_object
), /*tp_basicsize*/
361 pspy_dealloc
, /*tp_dealloc*/
368 0, /*tp_as_sequence*/
376 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
377 "GDB progspace object", /* tp_doc */
380 0, /* tp_richcompare */
381 0, /* tp_weaklistoffset */
386 pspace_getset
, /* tp_getset */
389 0, /* tp_descr_get */
390 0, /* tp_descr_set */
391 offsetof (pspace_object
, dict
), /* tp_dictoffset */
394 pspy_new
, /* tp_new */