1 /* Python interface to objfiles.
3 Copyright (C) 2008-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"
30 /* The corresponding objfile. */
31 struct objfile *objfile;
33 /* Dictionary holding user-added attributes.
34 This is the __dict__ attribute of the object. */
37 /* The pretty-printer list of functions. */
40 /* The frame filter list of functions. */
41 PyObject *frame_filters;
42 /* The type-printer list. */
43 PyObject *type_printers;
45 /* The debug method matcher list. */
49 static PyTypeObject objfile_object_type
50 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("objfile_object");
52 static const struct objfile_data *objfpy_objfile_data_key;
56 /* An Objfile method which returns the objfile's file name, or None. */
58 objfpy_get_filename (PyObject *self, void *closure)
60 objfile_object *obj = (objfile_object *) self;
63 return PyString_Decode (objfile_name (obj->objfile),
64 strlen (objfile_name (obj->objfile)),
65 host_charset (), NULL);
69 /* An Objfile method which returns the objfile's progspace, or None. */
72 objfpy_get_progspace (PyObject *self, void *closure)
74 objfile_object *obj = (objfile_object *) self;
78 PyObject *pspace = pspace_to_pspace_object (obj->objfile->pspace);
88 objfpy_dealloc (PyObject *o)
90 objfile_object *self = (objfile_object *) o;
92 Py_XDECREF (self->dict);
93 Py_XDECREF (self->printers);
94 Py_XDECREF (self->frame_filters);
95 Py_XDECREF (self->type_printers);
96 Py_XDECREF (self->xmethods);
97 Py_TYPE (self)->tp_free (self);
100 /* Initialize an objfile_object.
101 The result is a boolean indicating success. */
104 objfpy_initialize (objfile_object *self)
106 self->objfile = NULL;
109 self->printers = PyList_New (0);
110 if (self->printers == NULL)
113 self->frame_filters = PyDict_New ();
114 if (self->frame_filters == NULL)
117 self->type_printers = PyList_New (0);
118 if (self->type_printers == NULL)
121 self->xmethods = PyList_New (0);
122 if (self->xmethods == NULL)
129 objfpy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
131 objfile_object *self = (objfile_object *) type->tp_alloc (type, 0);
135 if (!objfpy_initialize (self))
142 return (PyObject *) self;
146 objfpy_get_printers (PyObject *o, void *ignore)
148 objfile_object *self = (objfile_object *) o;
150 Py_INCREF (self->printers);
151 return self->printers;
155 objfpy_set_printers (PyObject *o, PyObject *value, void *ignore)
158 objfile_object *self = (objfile_object *) o;
162 PyErr_SetString (PyExc_TypeError,
163 _("Cannot delete the pretty_printers attribute."));
167 if (! PyList_Check (value))
169 PyErr_SetString (PyExc_TypeError,
170 _("The pretty_printers attribute must be a list."));
174 /* Take care in case the LHS and RHS are related somehow. */
175 tmp = self->printers;
177 self->printers = value;
183 /* Return the Python dictionary attribute containing frame filters for
186 objfpy_get_frame_filters (PyObject *o, void *ignore)
188 objfile_object *self = (objfile_object *) o;
190 Py_INCREF (self->frame_filters);
191 return self->frame_filters;
194 /* Set this object file's frame filters dictionary to FILTERS. */
196 objfpy_set_frame_filters (PyObject *o, PyObject *filters, void *ignore)
199 objfile_object *self = (objfile_object *) o;
203 PyErr_SetString (PyExc_TypeError,
204 _("Cannot delete the frame filters attribute."));
208 if (! PyDict_Check (filters))
210 PyErr_SetString (PyExc_TypeError,
211 _("The frame_filters attribute must be a dictionary."));
215 /* Take care in case the LHS and RHS are related somehow. */
216 tmp = self->frame_filters;
218 self->frame_filters = filters;
224 /* Get the 'type_printers' attribute. */
227 objfpy_get_type_printers (PyObject *o, void *ignore)
229 objfile_object *self = (objfile_object *) o;
231 Py_INCREF (self->type_printers);
232 return self->type_printers;
235 /* Get the 'xmethods' attribute. */
238 objfpy_get_xmethods (PyObject *o, void *ignore)
240 objfile_object *self = (objfile_object *) o;
242 Py_INCREF (self->xmethods);
243 return self->xmethods;
246 /* Set the 'type_printers' attribute. */
249 objfpy_set_type_printers (PyObject *o, PyObject *value, void *ignore)
252 objfile_object *self = (objfile_object *) o;
256 PyErr_SetString (PyExc_TypeError,
257 _("Cannot delete the type_printers attribute."));
261 if (! PyList_Check (value))
263 PyErr_SetString (PyExc_TypeError,
264 _("The type_printers attribute must be a list."));
268 /* Take care in case the LHS and RHS are related somehow. */
269 tmp = self->type_printers;
271 self->type_printers = value;
277 /* Implementation of gdb.Objfile.is_valid (self) -> Boolean.
278 Returns True if this object file still exists in GDB. */
281 objfpy_is_valid (PyObject *self, PyObject *args)
283 objfile_object *obj = (objfile_object *) self;
293 /* Clear the OBJFILE pointer in an Objfile object and remove the
296 py_free_objfile (struct objfile *objfile, void *datum)
298 struct cleanup *cleanup;
299 objfile_object *object = datum;
301 cleanup = ensure_python_env (get_objfile_arch (objfile), current_language);
302 object->objfile = NULL;
303 Py_DECREF ((PyObject *) object);
304 do_cleanups (cleanup);
307 /* Return a borrowed reference to the Python object of type Objfile
308 representing OBJFILE. If the object has already been created,
309 return it. Otherwise, create it. Return NULL and set the Python
313 objfile_to_objfile_object (struct objfile *objfile)
315 objfile_object *object;
317 object = objfile_data (objfile, objfpy_objfile_data_key);
320 object = PyObject_New (objfile_object, &objfile_object_type);
323 if (!objfpy_initialize (object))
329 object->objfile = objfile;
330 set_objfile_data (objfile, objfpy_objfile_data_key, object);
334 return (PyObject *) object;
338 gdbpy_initialize_objfile (void)
340 objfpy_objfile_data_key
341 = register_objfile_data_with_cleanup (NULL, py_free_objfile);
343 if (PyType_Ready (&objfile_object_type) < 0)
346 return gdb_pymodule_addobject (gdb_module, "Objfile",
347 (PyObject *) &objfile_object_type);
352 static PyMethodDef objfile_object_methods[] =
354 { "is_valid", objfpy_is_valid, METH_NOARGS,
355 "is_valid () -> Boolean.\n\
356 Return true if this object file is valid, false if not." },
361 static PyGetSetDef objfile_getset[] =
363 { "__dict__", gdb_py_generic_dict, NULL,
364 "The __dict__ for this objfile.", &objfile_object_type },
365 { "filename", objfpy_get_filename, NULL,
366 "The objfile's filename, or None.", NULL },
367 { "progspace", objfpy_get_progspace, NULL,
368 "The objfile's progspace, or None.", NULL },
369 { "pretty_printers", objfpy_get_printers, objfpy_set_printers,
370 "Pretty printers.", NULL },
371 { "frame_filters", objfpy_get_frame_filters,
372 objfpy_set_frame_filters, "Frame Filters.", NULL },
373 { "type_printers", objfpy_get_type_printers, objfpy_set_type_printers,
374 "Type printers.", NULL },
375 { "xmethods", objfpy_get_xmethods, NULL,
376 "Debug methods.", NULL },
380 static PyTypeObject objfile_object_type =
382 PyVarObject_HEAD_INIT (NULL, 0)
383 "gdb.Objfile", /*tp_name*/
384 sizeof (objfile_object), /*tp_basicsize*/
386 objfpy_dealloc, /*tp_dealloc*/
393 0, /*tp_as_sequence*/
401 Py_TPFLAGS_DEFAULT, /*tp_flags*/
402 "GDB objfile object", /* tp_doc */
405 0, /* tp_richcompare */
406 0, /* tp_weaklistoffset */
409 objfile_object_methods, /* tp_methods */
411 objfile_getset, /* tp_getset */
414 0, /* tp_descr_get */
415 0, /* tp_descr_set */
416 offsetof (objfile_object, dict), /* tp_dictoffset */
419 objfpy_new, /* tp_new */