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"
32 /* The corresponding objfile. */
33 struct objfile
*objfile
;
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 matcher list. */
51 static PyTypeObject objfile_object_type
52 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("objfile_object");
54 static const struct objfile_data
*objfpy_objfile_data_key
;
56 /* Require that OBJF be a valid objfile. */
57 #define OBJFPY_REQUIRE_VALID(obj) \
59 if (!(obj)->objfile) \
61 PyErr_SetString (PyExc_RuntimeError, \
62 _("Objfile no longer exists.")); \
69 /* An Objfile method which returns the objfile's file name, or None. */
72 objfpy_get_filename (PyObject
*self
, void *closure
)
74 objfile_object
*obj
= (objfile_object
*) self
;
77 return PyString_Decode (objfile_name (obj
->objfile
),
78 strlen (objfile_name (obj
->objfile
)),
79 host_charset (), NULL
);
83 /* An Objfile method which returns the objfile's build id, or None. */
86 objfpy_get_build_id (PyObject
*self
, void *closure
)
88 objfile_object
*obj
= (objfile_object
*) self
;
89 struct objfile
*objfile
= obj
->objfile
;
90 const struct elf_build_id
*build_id
= NULL
;
91 volatile struct gdb_exception except
;
93 OBJFPY_REQUIRE_VALID (obj
);
95 TRY_CATCH (except
, RETURN_MASK_ALL
)
97 build_id
= build_id_bfd_get (objfile
->obfd
);
99 GDB_PY_HANDLE_EXCEPTION (except
);
101 if (build_id
!= NULL
)
103 char *hex_form
= make_hex_string (build_id
->data
, build_id
->size
);
106 result
= PyString_Decode (hex_form
, strlen (hex_form
),
107 host_charset (), NULL
);
115 /* An Objfile method which returns the objfile's progspace, or None. */
118 objfpy_get_progspace (PyObject
*self
, void *closure
)
120 objfile_object
*obj
= (objfile_object
*) self
;
124 PyObject
*pspace
= pspace_to_pspace_object (obj
->objfile
->pspace
);
134 objfpy_dealloc (PyObject
*o
)
136 objfile_object
*self
= (objfile_object
*) o
;
138 Py_XDECREF (self
->dict
);
139 Py_XDECREF (self
->printers
);
140 Py_XDECREF (self
->frame_filters
);
141 Py_XDECREF (self
->type_printers
);
142 Py_XDECREF (self
->xmethods
);
143 Py_TYPE (self
)->tp_free (self
);
146 /* Initialize an objfile_object.
147 The result is a boolean indicating success. */
150 objfpy_initialize (objfile_object
*self
)
152 self
->objfile
= NULL
;
155 self
->printers
= PyList_New (0);
156 if (self
->printers
== NULL
)
159 self
->frame_filters
= PyDict_New ();
160 if (self
->frame_filters
== NULL
)
163 self
->type_printers
= PyList_New (0);
164 if (self
->type_printers
== NULL
)
167 self
->xmethods
= PyList_New (0);
168 if (self
->xmethods
== NULL
)
175 objfpy_new (PyTypeObject
*type
, PyObject
*args
, PyObject
*keywords
)
177 objfile_object
*self
= (objfile_object
*) type
->tp_alloc (type
, 0);
181 if (!objfpy_initialize (self
))
188 return (PyObject
*) self
;
192 objfpy_get_printers (PyObject
*o
, void *ignore
)
194 objfile_object
*self
= (objfile_object
*) o
;
196 Py_INCREF (self
->printers
);
197 return self
->printers
;
201 objfpy_set_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
204 objfile_object
*self
= (objfile_object
*) o
;
208 PyErr_SetString (PyExc_TypeError
,
209 _("Cannot delete the pretty_printers attribute."));
213 if (! PyList_Check (value
))
215 PyErr_SetString (PyExc_TypeError
,
216 _("The pretty_printers attribute must be a list."));
220 /* Take care in case the LHS and RHS are related somehow. */
221 tmp
= self
->printers
;
223 self
->printers
= value
;
229 /* Return the Python dictionary attribute containing frame filters for
232 objfpy_get_frame_filters (PyObject
*o
, void *ignore
)
234 objfile_object
*self
= (objfile_object
*) o
;
236 Py_INCREF (self
->frame_filters
);
237 return self
->frame_filters
;
240 /* Set this object file's frame filters dictionary to FILTERS. */
242 objfpy_set_frame_filters (PyObject
*o
, PyObject
*filters
, void *ignore
)
245 objfile_object
*self
= (objfile_object
*) o
;
249 PyErr_SetString (PyExc_TypeError
,
250 _("Cannot delete the frame filters attribute."));
254 if (! PyDict_Check (filters
))
256 PyErr_SetString (PyExc_TypeError
,
257 _("The frame_filters attribute must be a dictionary."));
261 /* Take care in case the LHS and RHS are related somehow. */
262 tmp
= self
->frame_filters
;
264 self
->frame_filters
= filters
;
270 /* Get the 'type_printers' attribute. */
273 objfpy_get_type_printers (PyObject
*o
, void *ignore
)
275 objfile_object
*self
= (objfile_object
*) o
;
277 Py_INCREF (self
->type_printers
);
278 return self
->type_printers
;
281 /* Get the 'xmethods' attribute. */
284 objfpy_get_xmethods (PyObject
*o
, void *ignore
)
286 objfile_object
*self
= (objfile_object
*) o
;
288 Py_INCREF (self
->xmethods
);
289 return self
->xmethods
;
292 /* Set the 'type_printers' attribute. */
295 objfpy_set_type_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
298 objfile_object
*self
= (objfile_object
*) o
;
302 PyErr_SetString (PyExc_TypeError
,
303 _("Cannot delete the type_printers attribute."));
307 if (! PyList_Check (value
))
309 PyErr_SetString (PyExc_TypeError
,
310 _("The type_printers attribute must be a list."));
314 /* Take care in case the LHS and RHS are related somehow. */
315 tmp
= self
->type_printers
;
317 self
->type_printers
= value
;
323 /* Implementation of gdb.Objfile.is_valid (self) -> Boolean.
324 Returns True if this object file still exists in GDB. */
327 objfpy_is_valid (PyObject
*self
, PyObject
*args
)
329 objfile_object
*obj
= (objfile_object
*) self
;
339 /* Clear the OBJFILE pointer in an Objfile object and remove the
342 py_free_objfile (struct objfile
*objfile
, void *datum
)
344 struct cleanup
*cleanup
;
345 objfile_object
*object
= datum
;
347 cleanup
= ensure_python_env (get_objfile_arch (objfile
), current_language
);
348 object
->objfile
= NULL
;
349 Py_DECREF ((PyObject
*) object
);
350 do_cleanups (cleanup
);
353 /* Return a borrowed reference to the Python object of type Objfile
354 representing OBJFILE. If the object has already been created,
355 return it. Otherwise, create it. Return NULL and set the Python
359 objfile_to_objfile_object (struct objfile
*objfile
)
361 objfile_object
*object
;
363 object
= objfile_data (objfile
, objfpy_objfile_data_key
);
366 object
= PyObject_New (objfile_object
, &objfile_object_type
);
369 if (!objfpy_initialize (object
))
375 object
->objfile
= objfile
;
376 set_objfile_data (objfile
, objfpy_objfile_data_key
, object
);
380 return (PyObject
*) object
;
384 gdbpy_initialize_objfile (void)
386 objfpy_objfile_data_key
387 = register_objfile_data_with_cleanup (NULL
, py_free_objfile
);
389 if (PyType_Ready (&objfile_object_type
) < 0)
392 return gdb_pymodule_addobject (gdb_module
, "Objfile",
393 (PyObject
*) &objfile_object_type
);
398 static PyMethodDef objfile_object_methods
[] =
400 { "is_valid", objfpy_is_valid
, METH_NOARGS
,
401 "is_valid () -> Boolean.\n\
402 Return true if this object file is valid, false if not." },
407 static PyGetSetDef objfile_getset
[] =
409 { "__dict__", gdb_py_generic_dict
, NULL
,
410 "The __dict__ for this objfile.", &objfile_object_type
},
411 { "filename", objfpy_get_filename
, NULL
,
412 "The objfile's filename, or None.", NULL
},
413 { "build_id", objfpy_get_build_id
, NULL
,
414 "The objfile's build id, or None.", NULL
},
415 { "progspace", objfpy_get_progspace
, NULL
,
416 "The objfile's progspace, or None.", NULL
},
417 { "pretty_printers", objfpy_get_printers
, objfpy_set_printers
,
418 "Pretty printers.", NULL
},
419 { "frame_filters", objfpy_get_frame_filters
,
420 objfpy_set_frame_filters
, "Frame Filters.", NULL
},
421 { "type_printers", objfpy_get_type_printers
, objfpy_set_type_printers
,
422 "Type printers.", NULL
},
423 { "xmethods", objfpy_get_xmethods
, NULL
,
424 "Debug methods.", NULL
},
428 static PyTypeObject objfile_object_type
=
430 PyVarObject_HEAD_INIT (NULL
, 0)
431 "gdb.Objfile", /*tp_name*/
432 sizeof (objfile_object
), /*tp_basicsize*/
434 objfpy_dealloc
, /*tp_dealloc*/
441 0, /*tp_as_sequence*/
449 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
450 "GDB objfile object", /* tp_doc */
453 0, /* tp_richcompare */
454 0, /* tp_weaklistoffset */
457 objfile_object_methods
, /* tp_methods */
459 objfile_getset
, /* tp_getset */
462 0, /* tp_descr_get */
463 0, /* tp_descr_set */
464 offsetof (objfile_object
, dict
), /* tp_dictoffset */
467 objfpy_new
, /* tp_new */