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 /* If SELF is a separate debug-info file, return the "backlink" field.
84 Otherwise return None. */
87 objfpy_get_owner (PyObject *self, void *closure)
89 objfile_object *obj = (objfile_object *) self;
90 struct objfile *objfile = obj->objfile;
91 struct objfile *owner;
93 OBJFPY_REQUIRE_VALID (obj);
95 owner = objfile->separate_debug_objfile_backlink;
98 PyObject *result = objfile_to_objfile_object (owner);
106 /* An Objfile method which returns the objfile's build id, or None. */
109 objfpy_get_build_id (PyObject *self, void *closure)
111 objfile_object *obj = (objfile_object *) self;
112 struct objfile *objfile = obj->objfile;
113 const struct elf_build_id *build_id = NULL;
114 volatile struct gdb_exception except;
116 OBJFPY_REQUIRE_VALID (obj);
118 TRY_CATCH (except, RETURN_MASK_ALL)
120 build_id = build_id_bfd_get (objfile->obfd);
122 GDB_PY_HANDLE_EXCEPTION (except);
124 if (build_id != NULL)
126 char *hex_form = make_hex_string (build_id->data, build_id->size);
129 result = PyString_Decode (hex_form, strlen (hex_form),
130 host_charset (), NULL);
138 /* An Objfile method which returns the objfile's progspace, or None. */
141 objfpy_get_progspace (PyObject *self, void *closure)
143 objfile_object *obj = (objfile_object *) self;
147 PyObject *pspace = pspace_to_pspace_object (obj->objfile->pspace);
157 objfpy_dealloc (PyObject *o)
159 objfile_object *self = (objfile_object *) o;
161 Py_XDECREF (self->dict);
162 Py_XDECREF (self->printers);
163 Py_XDECREF (self->frame_filters);
164 Py_XDECREF (self->type_printers);
165 Py_XDECREF (self->xmethods);
166 Py_TYPE (self)->tp_free (self);
169 /* Initialize an objfile_object.
170 The result is a boolean indicating success. */
173 objfpy_initialize (objfile_object *self)
175 self->objfile = NULL;
178 self->printers = PyList_New (0);
179 if (self->printers == NULL)
182 self->frame_filters = PyDict_New ();
183 if (self->frame_filters == NULL)
186 self->type_printers = PyList_New (0);
187 if (self->type_printers == NULL)
190 self->xmethods = PyList_New (0);
191 if (self->xmethods == NULL)
198 objfpy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
200 objfile_object *self = (objfile_object *) type->tp_alloc (type, 0);
204 if (!objfpy_initialize (self))
211 return (PyObject *) self;
215 objfpy_get_printers (PyObject *o, void *ignore)
217 objfile_object *self = (objfile_object *) o;
219 Py_INCREF (self->printers);
220 return self->printers;
224 objfpy_set_printers (PyObject *o, PyObject *value, void *ignore)
227 objfile_object *self = (objfile_object *) o;
231 PyErr_SetString (PyExc_TypeError,
232 _("Cannot delete the pretty_printers attribute."));
236 if (! PyList_Check (value))
238 PyErr_SetString (PyExc_TypeError,
239 _("The pretty_printers attribute must be a list."));
243 /* Take care in case the LHS and RHS are related somehow. */
244 tmp = self->printers;
246 self->printers = value;
252 /* Return the Python dictionary attribute containing frame filters for
255 objfpy_get_frame_filters (PyObject *o, void *ignore)
257 objfile_object *self = (objfile_object *) o;
259 Py_INCREF (self->frame_filters);
260 return self->frame_filters;
263 /* Set this object file's frame filters dictionary to FILTERS. */
265 objfpy_set_frame_filters (PyObject *o, PyObject *filters, void *ignore)
268 objfile_object *self = (objfile_object *) o;
272 PyErr_SetString (PyExc_TypeError,
273 _("Cannot delete the frame filters attribute."));
277 if (! PyDict_Check (filters))
279 PyErr_SetString (PyExc_TypeError,
280 _("The frame_filters attribute must be a dictionary."));
284 /* Take care in case the LHS and RHS are related somehow. */
285 tmp = self->frame_filters;
287 self->frame_filters = filters;
293 /* Get the 'type_printers' attribute. */
296 objfpy_get_type_printers (PyObject *o, void *ignore)
298 objfile_object *self = (objfile_object *) o;
300 Py_INCREF (self->type_printers);
301 return self->type_printers;
304 /* Get the 'xmethods' attribute. */
307 objfpy_get_xmethods (PyObject *o, void *ignore)
309 objfile_object *self = (objfile_object *) o;
311 Py_INCREF (self->xmethods);
312 return self->xmethods;
315 /* Set the 'type_printers' attribute. */
318 objfpy_set_type_printers (PyObject *o, PyObject *value, void *ignore)
321 objfile_object *self = (objfile_object *) o;
325 PyErr_SetString (PyExc_TypeError,
326 _("Cannot delete the type_printers attribute."));
330 if (! PyList_Check (value))
332 PyErr_SetString (PyExc_TypeError,
333 _("The type_printers attribute must be a list."));
337 /* Take care in case the LHS and RHS are related somehow. */
338 tmp = self->type_printers;
340 self->type_printers = value;
346 /* Implementation of gdb.Objfile.is_valid (self) -> Boolean.
347 Returns True if this object file still exists in GDB. */
350 objfpy_is_valid (PyObject *self, PyObject *args)
352 objfile_object *obj = (objfile_object *) self;
360 /* Implementation of gdb.Objfile.add_separate_debug_file (self) -> Boolean. */
363 objfpy_add_separate_debug_file (PyObject *self, PyObject *args, PyObject *kw)
365 static char *keywords[] = { "file_name", NULL };
366 objfile_object *obj = (objfile_object *) self;
367 const char *file_name;
368 int symfile_flags = 0;
369 volatile struct gdb_exception except;
371 OBJFPY_REQUIRE_VALID (obj);
373 if (!PyArg_ParseTupleAndKeywords (args, kw, "s", keywords, &file_name))
376 TRY_CATCH (except, RETURN_MASK_ALL)
378 bfd *abfd = symfile_bfd_open (file_name);
380 symbol_file_add_separate (abfd, file_name, symfile_flags, obj->objfile);
382 GDB_PY_HANDLE_EXCEPTION (except);
389 /* Clear the OBJFILE pointer in an Objfile object and remove the
392 py_free_objfile (struct objfile *objfile, void *datum)
394 struct cleanup *cleanup;
395 objfile_object *object = datum;
397 cleanup = ensure_python_env (get_objfile_arch (objfile), current_language);
398 object->objfile = NULL;
399 Py_DECREF ((PyObject *) object);
400 do_cleanups (cleanup);
403 /* Return a borrowed reference to the Python object of type Objfile
404 representing OBJFILE. If the object has already been created,
405 return it. Otherwise, create it. Return NULL and set the Python
409 objfile_to_objfile_object (struct objfile *objfile)
411 objfile_object *object;
413 object = objfile_data (objfile, objfpy_objfile_data_key);
416 object = PyObject_New (objfile_object, &objfile_object_type);
419 if (!objfpy_initialize (object))
425 object->objfile = objfile;
426 set_objfile_data (objfile, objfpy_objfile_data_key, object);
430 return (PyObject *) object;
434 gdbpy_initialize_objfile (void)
436 objfpy_objfile_data_key
437 = register_objfile_data_with_cleanup (NULL, py_free_objfile);
439 if (PyType_Ready (&objfile_object_type) < 0)
442 return gdb_pymodule_addobject (gdb_module, "Objfile",
443 (PyObject *) &objfile_object_type);
448 static PyMethodDef objfile_object_methods[] =
450 { "is_valid", objfpy_is_valid, METH_NOARGS,
451 "is_valid () -> Boolean.\n\
452 Return true if this object file is valid, false if not." },
454 { "add_separate_debug_file", (PyCFunction) objfpy_add_separate_debug_file,
455 METH_VARARGS | METH_KEYWORDS,
456 "add_separate_debug_file (file_name).\n\
457 Add FILE_NAME to the list of files containing debug info for the objfile." },
462 static PyGetSetDef objfile_getset[] =
464 { "__dict__", gdb_py_generic_dict, NULL,
465 "The __dict__ for this objfile.", &objfile_object_type },
466 { "filename", objfpy_get_filename, NULL,
467 "The objfile's filename, or None.", NULL },
468 { "owner", objfpy_get_owner, NULL,
469 "The objfile owner of separate debug info objfiles, or None.",
471 { "build_id", objfpy_get_build_id, NULL,
472 "The objfile's build id, or None.", NULL },
473 { "progspace", objfpy_get_progspace, NULL,
474 "The objfile's progspace, or None.", NULL },
475 { "pretty_printers", objfpy_get_printers, objfpy_set_printers,
476 "Pretty printers.", NULL },
477 { "frame_filters", objfpy_get_frame_filters,
478 objfpy_set_frame_filters, "Frame Filters.", NULL },
479 { "type_printers", objfpy_get_type_printers, objfpy_set_type_printers,
480 "Type printers.", NULL },
481 { "xmethods", objfpy_get_xmethods, NULL,
482 "Debug methods.", NULL },
486 static PyTypeObject objfile_object_type =
488 PyVarObject_HEAD_INIT (NULL, 0)
489 "gdb.Objfile", /*tp_name*/
490 sizeof (objfile_object), /*tp_basicsize*/
492 objfpy_dealloc, /*tp_dealloc*/
499 0, /*tp_as_sequence*/
507 Py_TPFLAGS_DEFAULT, /*tp_flags*/
508 "GDB objfile object", /* tp_doc */
511 0, /* tp_richcompare */
512 0, /* tp_weaklistoffset */
515 objfile_object_methods, /* tp_methods */
517 objfile_getset, /* tp_getset */
520 0, /* tp_descr_get */
521 0, /* tp_descr_set */
522 offsetof (objfile_object, dict), /* tp_dictoffset */
525 objfpy_new, /* tp_new */