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 (obj
->objfile
->name
, strlen (obj
->objfile
->name
),
57 host_charset (), NULL
);
62 objfpy_dealloc (PyObject
*o
)
64 objfile_object
*self
= (objfile_object
*) o
;
66 Py_XDECREF (self
->printers
);
67 Py_XDECREF (self
->frame_filters
);
68 Py_XDECREF (self
->type_printers
);
69 Py_TYPE (self
)->tp_free (self
);
73 objfpy_new (PyTypeObject
*type
, PyObject
*args
, PyObject
*keywords
)
75 objfile_object
*self
= (objfile_object
*) type
->tp_alloc (type
, 0);
81 self
->printers
= PyList_New (0);
88 self
->frame_filters
= PyDict_New ();
89 if (!self
->frame_filters
)
95 self
->type_printers
= PyList_New (0);
96 if (!self
->type_printers
)
102 return (PyObject
*) self
;
106 objfpy_get_printers (PyObject
*o
, void *ignore
)
108 objfile_object
*self
= (objfile_object
*) o
;
110 Py_INCREF (self
->printers
);
111 return self
->printers
;
115 objfpy_set_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
118 objfile_object
*self
= (objfile_object
*) o
;
122 PyErr_SetString (PyExc_TypeError
,
123 _("Cannot delete the pretty_printers attribute."));
127 if (! PyList_Check (value
))
129 PyErr_SetString (PyExc_TypeError
,
130 _("The pretty_printers attribute must be a list."));
134 /* Take care in case the LHS and RHS are related somehow. */
135 tmp
= self
->printers
;
137 self
->printers
= value
;
143 /* Return the Python dictionary attribute containing frame filters for
146 objfpy_get_frame_filters (PyObject
*o
, void *ignore
)
148 objfile_object
*self
= (objfile_object
*) o
;
150 Py_INCREF (self
->frame_filters
);
151 return self
->frame_filters
;
154 /* Set this object file's frame filters dictionary to FILTERS. */
156 objfpy_set_frame_filters (PyObject
*o
, PyObject
*filters
, void *ignore
)
159 objfile_object
*self
= (objfile_object
*) o
;
163 PyErr_SetString (PyExc_TypeError
,
164 _("Cannot delete the frame filters attribute."));
168 if (! PyDict_Check (filters
))
170 PyErr_SetString (PyExc_TypeError
,
171 _("The frame_filters attribute must be a dictionary."));
175 /* Take care in case the LHS and RHS are related somehow. */
176 tmp
= self
->frame_filters
;
178 self
->frame_filters
= filters
;
184 /* Get the 'type_printers' attribute. */
187 objfpy_get_type_printers (PyObject
*o
, void *ignore
)
189 objfile_object
*self
= (objfile_object
*) o
;
191 Py_INCREF (self
->type_printers
);
192 return self
->type_printers
;
195 /* Set the 'type_printers' attribute. */
198 objfpy_set_type_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
201 objfile_object
*self
= (objfile_object
*) o
;
205 PyErr_SetString (PyExc_TypeError
,
206 _("Cannot delete the type_printers attribute."));
210 if (! PyList_Check (value
))
212 PyErr_SetString (PyExc_TypeError
,
213 _("The type_printers attribute must be a list."));
217 /* Take care in case the LHS and RHS are related somehow. */
218 tmp
= self
->type_printers
;
220 self
->type_printers
= value
;
226 /* Implementation of gdb.Objfile.is_valid (self) -> Boolean.
227 Returns True if this object file still exists in GDB. */
230 objfpy_is_valid (PyObject
*self
, PyObject
*args
)
232 objfile_object
*obj
= (objfile_object
*) self
;
242 /* Clear the OBJFILE pointer in an Objfile object and remove the
245 py_free_objfile (struct objfile
*objfile
, void *datum
)
247 struct cleanup
*cleanup
;
248 objfile_object
*object
= datum
;
250 cleanup
= ensure_python_env (get_objfile_arch (objfile
), current_language
);
251 object
->objfile
= NULL
;
252 Py_DECREF ((PyObject
*) object
);
253 do_cleanups (cleanup
);
256 /* Return a borrowed reference to the Python object of type Objfile
257 representing OBJFILE. If the object has already been created,
258 return it. Otherwise, create it. Return NULL and set the Python
261 objfile_to_objfile_object (struct objfile
*objfile
)
263 objfile_object
*object
;
265 object
= objfile_data (objfile
, objfpy_objfile_data_key
);
268 object
= PyObject_New (objfile_object
, &objfile_object_type
);
271 object
->objfile
= objfile
;
273 object
->printers
= PyList_New (0);
274 if (!object
->printers
)
280 object
->frame_filters
= PyDict_New ();
281 if (!object
->frame_filters
)
287 object
->type_printers
= PyList_New (0);
288 if (!object
->type_printers
)
294 set_objfile_data (objfile
, objfpy_objfile_data_key
, object
);
298 return (PyObject
*) object
;
302 gdbpy_initialize_objfile (void)
304 objfpy_objfile_data_key
305 = register_objfile_data_with_cleanup (NULL
, py_free_objfile
);
307 if (PyType_Ready (&objfile_object_type
) < 0)
310 return gdb_pymodule_addobject (gdb_module
, "Objfile",
311 (PyObject
*) &objfile_object_type
);
316 static PyMethodDef objfile_object_methods
[] =
318 { "is_valid", objfpy_is_valid
, METH_NOARGS
,
319 "is_valid () -> Boolean.\n\
320 Return true if this object file is valid, false if not." },
325 static PyGetSetDef objfile_getset
[] =
327 { "filename", objfpy_get_filename
, NULL
,
328 "The objfile's filename, or None.", NULL
},
329 { "pretty_printers", objfpy_get_printers
, objfpy_set_printers
,
330 "Pretty printers.", NULL
},
331 { "frame_filters", objfpy_get_frame_filters
,
332 objfpy_set_frame_filters
, "Frame Filters.", NULL
},
333 { "type_printers", objfpy_get_type_printers
, objfpy_set_type_printers
,
334 "Type printers.", NULL
},
338 static PyTypeObject objfile_object_type
=
340 PyVarObject_HEAD_INIT (NULL
, 0)
341 "gdb.Objfile", /*tp_name*/
342 sizeof (objfile_object
), /*tp_basicsize*/
344 objfpy_dealloc
, /*tp_dealloc*/
351 0, /*tp_as_sequence*/
359 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
360 "GDB objfile object", /* tp_doc */
363 0, /* tp_richcompare */
364 0, /* tp_weaklistoffset */
367 objfile_object_methods
, /* tp_methods */
369 objfile_getset
, /* tp_getset */
372 0, /* tp_descr_get */
373 0, /* tp_descr_set */
374 0, /* tp_dictoffset */
377 objfpy_new
, /* tp_new */