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 return objfile_to_objfile_object (owner);
102 /* An Objfile method which returns the objfile's build id, or None. */
105 objfpy_get_build_id (PyObject *self, void *closure)
107 objfile_object *obj = (objfile_object *) self;
108 struct objfile *objfile = obj->objfile;
109 const struct elf_build_id *build_id = NULL;
110 volatile struct gdb_exception except;
112 OBJFPY_REQUIRE_VALID (obj);
114 TRY_CATCH (except, RETURN_MASK_ALL)
116 build_id = build_id_bfd_get (objfile->obfd);
118 GDB_PY_HANDLE_EXCEPTION (except);
120 if (build_id != NULL)
122 char *hex_form = make_hex_string (build_id->data, build_id->size);
125 result = PyString_Decode (hex_form, strlen (hex_form),
126 host_charset (), NULL);
134 /* An Objfile method which returns the objfile's progspace, or None. */
137 objfpy_get_progspace (PyObject *self, void *closure)
139 objfile_object *obj = (objfile_object *) self;
143 PyObject *pspace = pspace_to_pspace_object (obj->objfile->pspace);
153 objfpy_dealloc (PyObject *o)
155 objfile_object *self = (objfile_object *) o;
157 Py_XDECREF (self->dict);
158 Py_XDECREF (self->printers);
159 Py_XDECREF (self->frame_filters);
160 Py_XDECREF (self->type_printers);
161 Py_XDECREF (self->xmethods);
162 Py_TYPE (self)->tp_free (self);
165 /* Initialize an objfile_object.
166 The result is a boolean indicating success. */
169 objfpy_initialize (objfile_object *self)
171 self->objfile = NULL;
174 self->printers = PyList_New (0);
175 if (self->printers == NULL)
178 self->frame_filters = PyDict_New ();
179 if (self->frame_filters == NULL)
182 self->type_printers = PyList_New (0);
183 if (self->type_printers == NULL)
186 self->xmethods = PyList_New (0);
187 if (self->xmethods == NULL)
194 objfpy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
196 objfile_object *self = (objfile_object *) type->tp_alloc (type, 0);
200 if (!objfpy_initialize (self))
207 return (PyObject *) self;
211 objfpy_get_printers (PyObject *o, void *ignore)
213 objfile_object *self = (objfile_object *) o;
215 Py_INCREF (self->printers);
216 return self->printers;
220 objfpy_set_printers (PyObject *o, PyObject *value, void *ignore)
223 objfile_object *self = (objfile_object *) o;
227 PyErr_SetString (PyExc_TypeError,
228 _("Cannot delete the pretty_printers attribute."));
232 if (! PyList_Check (value))
234 PyErr_SetString (PyExc_TypeError,
235 _("The pretty_printers attribute must be a list."));
239 /* Take care in case the LHS and RHS are related somehow. */
240 tmp = self->printers;
242 self->printers = value;
248 /* Return the Python dictionary attribute containing frame filters for
251 objfpy_get_frame_filters (PyObject *o, void *ignore)
253 objfile_object *self = (objfile_object *) o;
255 Py_INCREF (self->frame_filters);
256 return self->frame_filters;
259 /* Set this object file's frame filters dictionary to FILTERS. */
261 objfpy_set_frame_filters (PyObject *o, PyObject *filters, void *ignore)
264 objfile_object *self = (objfile_object *) o;
268 PyErr_SetString (PyExc_TypeError,
269 _("Cannot delete the frame filters attribute."));
273 if (! PyDict_Check (filters))
275 PyErr_SetString (PyExc_TypeError,
276 _("The frame_filters attribute must be a dictionary."));
280 /* Take care in case the LHS and RHS are related somehow. */
281 tmp = self->frame_filters;
283 self->frame_filters = filters;
289 /* Get the 'type_printers' attribute. */
292 objfpy_get_type_printers (PyObject *o, void *ignore)
294 objfile_object *self = (objfile_object *) o;
296 Py_INCREF (self->type_printers);
297 return self->type_printers;
300 /* Get the 'xmethods' attribute. */
303 objfpy_get_xmethods (PyObject *o, void *ignore)
305 objfile_object *self = (objfile_object *) o;
307 Py_INCREF (self->xmethods);
308 return self->xmethods;
311 /* Set the 'type_printers' attribute. */
314 objfpy_set_type_printers (PyObject *o, PyObject *value, void *ignore)
317 objfile_object *self = (objfile_object *) o;
321 PyErr_SetString (PyExc_TypeError,
322 _("Cannot delete the type_printers attribute."));
326 if (! PyList_Check (value))
328 PyErr_SetString (PyExc_TypeError,
329 _("The type_printers attribute must be a list."));
333 /* Take care in case the LHS and RHS are related somehow. */
334 tmp = self->type_printers;
336 self->type_printers = value;
342 /* Implementation of gdb.Objfile.is_valid (self) -> Boolean.
343 Returns True if this object file still exists in GDB. */
346 objfpy_is_valid (PyObject *self, PyObject *args)
348 objfile_object *obj = (objfile_object *) self;
356 /* Implementation of gdb.Objfile.add_separate_debug_file (self) -> Boolean. */
359 objfpy_add_separate_debug_file (PyObject *self, PyObject *args, PyObject *kw)
361 static char *keywords[] = { "file_name", NULL };
362 objfile_object *obj = (objfile_object *) self;
363 const char *file_name;
364 int symfile_flags = 0;
365 volatile struct gdb_exception except;
367 OBJFPY_REQUIRE_VALID (obj);
369 if (!PyArg_ParseTupleAndKeywords (args, kw, "s", keywords, &file_name))
372 TRY_CATCH (except, RETURN_MASK_ALL)
374 bfd *abfd = symfile_bfd_open (file_name);
376 symbol_file_add_separate (abfd, file_name, symfile_flags, obj->objfile);
378 GDB_PY_HANDLE_EXCEPTION (except);
385 /* Clear the OBJFILE pointer in an Objfile object and remove the
388 py_free_objfile (struct objfile *objfile, void *datum)
390 struct cleanup *cleanup;
391 objfile_object *object = datum;
393 cleanup = ensure_python_env (get_objfile_arch (objfile), current_language);
394 object->objfile = NULL;
395 Py_DECREF ((PyObject *) object);
396 do_cleanups (cleanup);
399 /* Return a borrowed reference to the Python object of type Objfile
400 representing OBJFILE. If the object has already been created,
401 return it. Otherwise, create it. Return NULL and set the Python
405 objfile_to_objfile_object (struct objfile *objfile)
407 objfile_object *object;
409 object = objfile_data (objfile, objfpy_objfile_data_key);
412 object = PyObject_New (objfile_object, &objfile_object_type);
415 if (!objfpy_initialize (object))
421 object->objfile = objfile;
422 set_objfile_data (objfile, objfpy_objfile_data_key, object);
426 return (PyObject *) object;
430 gdbpy_initialize_objfile (void)
432 objfpy_objfile_data_key
433 = register_objfile_data_with_cleanup (NULL, py_free_objfile);
435 if (PyType_Ready (&objfile_object_type) < 0)
438 return gdb_pymodule_addobject (gdb_module, "Objfile",
439 (PyObject *) &objfile_object_type);
444 static PyMethodDef objfile_object_methods[] =
446 { "is_valid", objfpy_is_valid, METH_NOARGS,
447 "is_valid () -> Boolean.\n\
448 Return true if this object file is valid, false if not." },
450 { "add_separate_debug_file", (PyCFunction) objfpy_add_separate_debug_file,
451 METH_VARARGS | METH_KEYWORDS,
452 "add_separate_debug_file (file_name).\n\
453 Add FILE_NAME to the list of files containing debug info for the objfile." },
458 static PyGetSetDef objfile_getset[] =
460 { "__dict__", gdb_py_generic_dict, NULL,
461 "The __dict__ for this objfile.", &objfile_object_type },
462 { "filename", objfpy_get_filename, NULL,
463 "The objfile's filename, or None.", NULL },
464 { "owner", objfpy_get_owner, NULL,
465 "The objfile owner of separate debug info objfiles, or None.",
467 { "build_id", objfpy_get_build_id, NULL,
468 "The objfile's build id, or None.", NULL },
469 { "progspace", objfpy_get_progspace, NULL,
470 "The objfile's progspace, or None.", NULL },
471 { "pretty_printers", objfpy_get_printers, objfpy_set_printers,
472 "Pretty printers.", NULL },
473 { "frame_filters", objfpy_get_frame_filters,
474 objfpy_set_frame_filters, "Frame Filters.", NULL },
475 { "type_printers", objfpy_get_type_printers, objfpy_set_type_printers,
476 "Type printers.", NULL },
477 { "xmethods", objfpy_get_xmethods, NULL,
478 "Debug methods.", NULL },
482 static PyTypeObject objfile_object_type =
484 PyVarObject_HEAD_INIT (NULL, 0)
485 "gdb.Objfile", /*tp_name*/
486 sizeof (objfile_object), /*tp_basicsize*/
488 objfpy_dealloc, /*tp_dealloc*/
495 0, /*tp_as_sequence*/
503 Py_TPFLAGS_DEFAULT, /*tp_flags*/
504 "GDB objfile object", /* tp_doc */
507 0, /* tp_richcompare */
508 0, /* tp_weaklistoffset */
511 objfile_object_methods, /* tp_methods */
513 objfile_getset, /* tp_getset */
516 0, /* tp_descr_get */
517 0, /* tp_descr_set */
518 offsetof (objfile_object, dict), /* tp_dictoffset */
521 objfpy_new, /* tp_new */