1 /* Python interface to objfiles.
3 Copyright (C) 2008-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"
30 /* The corresponding objfile. */
31 struct objfile
*objfile
;
33 /* The pretty-printer list of functions. */
36 /* The frame filter list of functions. */
37 PyObject
*frame_filters
;
38 /* The type-printer list. */
39 PyObject
*type_printers
;
42 static PyTypeObject objfile_object_type
43 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("objfile_object");
45 static const struct objfile_data
*objfpy_objfile_data_key
;
49 /* An Objfile method which returns the objfile's file name, or None. */
51 objfpy_get_filename (PyObject
*self
, void *closure
)
53 objfile_object
*obj
= (objfile_object
*) self
;
56 return PyString_Decode (objfile_name (obj
->objfile
),
57 strlen (objfile_name (obj
->objfile
)),
58 host_charset (), NULL
);
63 objfpy_dealloc (PyObject
*o
)
65 objfile_object
*self
= (objfile_object
*) o
;
67 Py_XDECREF (self
->printers
);
68 Py_XDECREF (self
->frame_filters
);
69 Py_XDECREF (self
->type_printers
);
70 Py_TYPE (self
)->tp_free (self
);
74 objfpy_new (PyTypeObject
*type
, PyObject
*args
, PyObject
*keywords
)
76 objfile_object
*self
= (objfile_object
*) type
->tp_alloc (type
, 0);
82 self
->printers
= PyList_New (0);
89 self
->frame_filters
= PyDict_New ();
90 if (!self
->frame_filters
)
96 self
->type_printers
= PyList_New (0);
97 if (!self
->type_printers
)
103 return (PyObject
*) self
;
107 objfpy_get_printers (PyObject
*o
, void *ignore
)
109 objfile_object
*self
= (objfile_object
*) o
;
111 Py_INCREF (self
->printers
);
112 return self
->printers
;
116 objfpy_set_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
119 objfile_object
*self
= (objfile_object
*) o
;
123 PyErr_SetString (PyExc_TypeError
,
124 _("Cannot delete the pretty_printers attribute."));
128 if (! PyList_Check (value
))
130 PyErr_SetString (PyExc_TypeError
,
131 _("The pretty_printers attribute must be a list."));
135 /* Take care in case the LHS and RHS are related somehow. */
136 tmp
= self
->printers
;
138 self
->printers
= value
;
144 /* Return the Python dictionary attribute containing frame filters for
147 objfpy_get_frame_filters (PyObject
*o
, void *ignore
)
149 objfile_object
*self
= (objfile_object
*) o
;
151 Py_INCREF (self
->frame_filters
);
152 return self
->frame_filters
;
155 /* Set this object file's frame filters dictionary to FILTERS. */
157 objfpy_set_frame_filters (PyObject
*o
, PyObject
*filters
, void *ignore
)
160 objfile_object
*self
= (objfile_object
*) o
;
164 PyErr_SetString (PyExc_TypeError
,
165 _("Cannot delete the frame filters attribute."));
169 if (! PyDict_Check (filters
))
171 PyErr_SetString (PyExc_TypeError
,
172 _("The frame_filters attribute must be a dictionary."));
176 /* Take care in case the LHS and RHS are related somehow. */
177 tmp
= self
->frame_filters
;
179 self
->frame_filters
= filters
;
185 /* Get the 'type_printers' attribute. */
188 objfpy_get_type_printers (PyObject
*o
, void *ignore
)
190 objfile_object
*self
= (objfile_object
*) o
;
192 Py_INCREF (self
->type_printers
);
193 return self
->type_printers
;
196 /* Set the 'type_printers' attribute. */
199 objfpy_set_type_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
202 objfile_object
*self
= (objfile_object
*) o
;
206 PyErr_SetString (PyExc_TypeError
,
207 _("Cannot delete the type_printers attribute."));
211 if (! PyList_Check (value
))
213 PyErr_SetString (PyExc_TypeError
,
214 _("The type_printers attribute must be a list."));
218 /* Take care in case the LHS and RHS are related somehow. */
219 tmp
= self
->type_printers
;
221 self
->type_printers
= value
;
227 /* Implementation of gdb.Objfile.is_valid (self) -> Boolean.
228 Returns True if this object file still exists in GDB. */
231 objfpy_is_valid (PyObject
*self
, PyObject
*args
)
233 objfile_object
*obj
= (objfile_object
*) self
;
243 /* Clear the OBJFILE pointer in an Objfile object and remove the
246 py_free_objfile (struct objfile
*objfile
, void *datum
)
248 struct cleanup
*cleanup
;
249 objfile_object
*object
= datum
;
251 cleanup
= ensure_python_env (get_objfile_arch (objfile
), current_language
);
252 object
->objfile
= NULL
;
253 Py_DECREF ((PyObject
*) object
);
254 do_cleanups (cleanup
);
257 /* Return a borrowed reference to the Python object of type Objfile
258 representing OBJFILE. If the object has already been created,
259 return it. Otherwise, create it. Return NULL and set the Python
262 objfile_to_objfile_object (struct objfile
*objfile
)
264 objfile_object
*object
;
266 object
= objfile_data (objfile
, objfpy_objfile_data_key
);
269 object
= PyObject_New (objfile_object
, &objfile_object_type
);
272 object
->objfile
= objfile
;
274 object
->printers
= PyList_New (0);
275 if (!object
->printers
)
281 object
->frame_filters
= PyDict_New ();
282 if (!object
->frame_filters
)
288 object
->type_printers
= PyList_New (0);
289 if (!object
->type_printers
)
295 set_objfile_data (objfile
, objfpy_objfile_data_key
, object
);
299 return (PyObject
*) object
;
303 gdbpy_initialize_objfile (void)
305 objfpy_objfile_data_key
306 = register_objfile_data_with_cleanup (NULL
, py_free_objfile
);
308 if (PyType_Ready (&objfile_object_type
) < 0)
311 return gdb_pymodule_addobject (gdb_module
, "Objfile",
312 (PyObject
*) &objfile_object_type
);
317 static PyMethodDef objfile_object_methods
[] =
319 { "is_valid", objfpy_is_valid
, METH_NOARGS
,
320 "is_valid () -> Boolean.\n\
321 Return true if this object file is valid, false if not." },
326 static PyGetSetDef objfile_getset
[] =
328 { "filename", objfpy_get_filename
, NULL
,
329 "The objfile's filename, or None.", NULL
},
330 { "pretty_printers", objfpy_get_printers
, objfpy_set_printers
,
331 "Pretty printers.", NULL
},
332 { "frame_filters", objfpy_get_frame_filters
,
333 objfpy_set_frame_filters
, "Frame Filters.", NULL
},
334 { "type_printers", objfpy_get_type_printers
, objfpy_set_type_printers
,
335 "Type printers.", NULL
},
339 static PyTypeObject objfile_object_type
=
341 PyVarObject_HEAD_INIT (NULL
, 0)
342 "gdb.Objfile", /*tp_name*/
343 sizeof (objfile_object
), /*tp_basicsize*/
345 objfpy_dealloc
, /*tp_dealloc*/
352 0, /*tp_as_sequence*/
360 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
361 "GDB objfile object", /* tp_doc */
364 0, /* tp_richcompare */
365 0, /* tp_weaklistoffset */
368 objfile_object_methods
, /* tp_methods */
370 objfile_getset
, /* tp_getset */
373 0, /* tp_descr_get */
374 0, /* tp_descr_set */
375 0, /* tp_dictoffset */
378 objfpy_new
, /* tp_new */