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 type-printer list. */
39 PyObject *type_printers;
42 static PyTypeObject pspace_object_type;
44 static const struct program_space_data *pspy_pspace_data_key;
48 /* An Objfile method which returns the objfile's file name, or None. */
51 pspy_get_filename (PyObject *self, void *closure)
53 pspace_object *obj = (pspace_object *) self;
57 struct objfile *objfile = obj->pspace->symfile_object_file;
60 return PyString_Decode (objfile->name, strlen (objfile->name),
61 host_charset (), NULL);
67 pspy_dealloc (PyObject *self)
69 pspace_object *ps_self = (pspace_object *) self;
71 Py_XDECREF (ps_self->printers);
72 Py_XDECREF (ps_self->type_printers);
73 Py_TYPE (self)->tp_free (self);
77 pspy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
79 pspace_object *self = (pspace_object *) type->tp_alloc (type, 0);
85 self->printers = PyList_New (0);
92 self->type_printers = PyList_New (0);
93 if (!self->type_printers)
99 return (PyObject *) self;
103 pspy_get_printers (PyObject *o, void *ignore)
105 pspace_object *self = (pspace_object *) o;
107 Py_INCREF (self->printers);
108 return self->printers;
112 pspy_set_printers (PyObject *o, PyObject *value, void *ignore)
115 pspace_object *self = (pspace_object *) o;
119 PyErr_SetString (PyExc_TypeError,
120 "cannot delete the pretty_printers attribute");
124 if (! PyList_Check (value))
126 PyErr_SetString (PyExc_TypeError,
127 "the pretty_printers attribute must be a list");
131 /* Take care in case the LHS and RHS are related somehow. */
132 tmp = self->printers;
134 self->printers = value;
140 /* Get the 'type_printers' attribute. */
143 pspy_get_type_printers (PyObject *o, void *ignore)
145 pspace_object *self = (pspace_object *) o;
147 Py_INCREF (self->type_printers);
148 return self->type_printers;
151 /* Set the 'type_printers' attribute. */
154 pspy_set_type_printers (PyObject *o, PyObject *value, void *ignore)
157 pspace_object *self = (pspace_object *) o;
161 PyErr_SetString (PyExc_TypeError,
162 "cannot delete the type_printers attribute");
166 if (! PyList_Check (value))
168 PyErr_SetString (PyExc_TypeError,
169 "the type_printers attribute must be a list");
173 /* Take care in case the LHS and RHS are related somehow. */
174 tmp = self->type_printers;
176 self->type_printers = value;
184 /* Clear the PSPACE pointer in a Pspace object and remove the reference. */
187 py_free_pspace (struct program_space *pspace, void *datum)
189 struct cleanup *cleanup;
190 pspace_object *object = datum;
191 struct gdbarch *arch = get_current_arch ();
193 cleanup = ensure_python_env (arch, current_language);
194 object->pspace = NULL;
195 Py_DECREF ((PyObject *) object);
196 do_cleanups (cleanup);
199 /* Return a borrowed reference to the Python object of type Pspace
200 representing PSPACE. If the object has already been created,
201 return it. Otherwise, create it. Return NULL and set the Python
205 pspace_to_pspace_object (struct program_space *pspace)
207 pspace_object *object;
209 object = program_space_data (pspace, pspy_pspace_data_key);
212 object = PyObject_New (pspace_object, &pspace_object_type);
215 object->pspace = pspace;
217 object->printers = PyList_New (0);
218 if (!object->printers)
224 object->type_printers = PyList_New (0);
225 if (!object->type_printers)
231 set_program_space_data (pspace, pspy_pspace_data_key, object);
235 return (PyObject *) object;
239 gdbpy_initialize_pspace (void)
242 = register_program_space_data_with_cleanup (NULL, py_free_pspace);
244 if (PyType_Ready (&pspace_object_type) < 0)
247 Py_INCREF (&pspace_object_type);
248 PyModule_AddObject (gdb_module, "Progspace",
249 (PyObject *) &pspace_object_type);
254 static PyGetSetDef pspace_getset[] =
256 { "filename", pspy_get_filename, NULL,
257 "The progspace's main filename, or None.", NULL },
258 { "pretty_printers", pspy_get_printers, pspy_set_printers,
259 "Pretty printers.", NULL },
260 { "type_printers", pspy_get_type_printers, pspy_set_type_printers,
261 "Type printers.", NULL },
265 static PyTypeObject pspace_object_type =
267 PyVarObject_HEAD_INIT (NULL, 0)
268 "gdb.Progspace", /*tp_name*/
269 sizeof (pspace_object), /*tp_basicsize*/
271 pspy_dealloc, /*tp_dealloc*/
278 0, /*tp_as_sequence*/
286 Py_TPFLAGS_DEFAULT, /*tp_flags*/
287 "GDB progspace object", /* tp_doc */
290 0, /* tp_richcompare */
291 0, /* tp_weaklistoffset */
296 pspace_getset, /* tp_getset */
299 0, /* tp_descr_get */
300 0, /* tp_descr_set */
301 0, /* tp_dictoffset */
304 pspy_new, /* tp_new */