1 /* Python interface to objfiles.
3 Copyright (C) 2008-2016 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;
45 /* The list of frame unwinders. */
46 PyObject *frame_unwinders;
48 /* The type-printer list. */
49 PyObject *type_printers;
51 /* The debug method matcher list. */
55 extern PyTypeObject objfile_object_type
56 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("objfile_object");
58 static const struct objfile_data *objfpy_objfile_data_key;
60 /* Require that OBJF be a valid objfile. */
61 #define OBJFPY_REQUIRE_VALID(obj) \
63 if (!(obj)->objfile) \
65 PyErr_SetString (PyExc_RuntimeError, \
66 _("Objfile no longer exists.")); \
73 /* An Objfile method which returns the objfile's file name, or None. */
76 objfpy_get_filename (PyObject *self, void *closure)
78 objfile_object *obj = (objfile_object *) self;
81 return host_string_to_python_string (objfile_name (obj->objfile));
85 /* An Objfile method which returns the objfile's file name, as specified
86 by the user, or None. */
89 objfpy_get_username (PyObject *self, void *closure)
91 objfile_object *obj = (objfile_object *) self;
95 const char *username = obj->objfile->original_name;
97 return host_string_to_python_string (username);
103 /* If SELF is a separate debug-info file, return the "backlink" field.
104 Otherwise return None. */
107 objfpy_get_owner (PyObject *self, void *closure)
109 objfile_object *obj = (objfile_object *) self;
110 struct objfile *objfile = obj->objfile;
111 struct objfile *owner;
113 OBJFPY_REQUIRE_VALID (obj);
115 owner = objfile->separate_debug_objfile_backlink;
118 PyObject *result = objfile_to_objfile_object (owner);
126 /* An Objfile method which returns the objfile's build id, or None. */
129 objfpy_get_build_id (PyObject *self, void *closure)
131 objfile_object *obj = (objfile_object *) self;
132 struct objfile *objfile = obj->objfile;
133 const struct bfd_build_id *build_id = NULL;
135 OBJFPY_REQUIRE_VALID (obj);
139 build_id = build_id_bfd_get (objfile->obfd);
141 CATCH (except, RETURN_MASK_ALL)
143 GDB_PY_HANDLE_EXCEPTION (except);
147 if (build_id != NULL)
149 char *hex_form = make_hex_string (build_id->data, build_id->size);
152 result = host_string_to_python_string (hex_form);
160 /* An Objfile method which returns the objfile's progspace, or None. */
163 objfpy_get_progspace (PyObject *self, void *closure)
165 objfile_object *obj = (objfile_object *) self;
169 PyObject *pspace = pspace_to_pspace_object (obj->objfile->pspace);
179 objfpy_dealloc (PyObject *o)
181 objfile_object *self = (objfile_object *) o;
183 Py_XDECREF (self->dict);
184 Py_XDECREF (self->printers);
185 Py_XDECREF (self->frame_filters);
186 Py_XDECREF (self->frame_unwinders);
187 Py_XDECREF (self->type_printers);
188 Py_XDECREF (self->xmethods);
189 Py_TYPE (self)->tp_free (self);
192 /* Initialize an objfile_object.
193 The result is a boolean indicating success. */
196 objfpy_initialize (objfile_object *self)
198 self->objfile = NULL;
200 self->dict = PyDict_New ();
201 if (self->dict == NULL)
204 self->printers = PyList_New (0);
205 if (self->printers == NULL)
208 self->frame_filters = PyDict_New ();
209 if (self->frame_filters == NULL)
212 self->frame_unwinders = PyList_New (0);
213 if (self->frame_unwinders == NULL)
216 self->type_printers = PyList_New (0);
217 if (self->type_printers == NULL)
220 self->xmethods = PyList_New (0);
221 if (self->xmethods == NULL)
228 objfpy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
230 objfile_object *self = (objfile_object *) type->tp_alloc (type, 0);
234 if (!objfpy_initialize (self))
241 return (PyObject *) self;
245 objfpy_get_printers (PyObject *o, void *ignore)
247 objfile_object *self = (objfile_object *) o;
249 Py_INCREF (self->printers);
250 return self->printers;
254 objfpy_set_printers (PyObject *o, PyObject *value, void *ignore)
257 objfile_object *self = (objfile_object *) o;
261 PyErr_SetString (PyExc_TypeError,
262 _("Cannot delete the pretty_printers attribute."));
266 if (! PyList_Check (value))
268 PyErr_SetString (PyExc_TypeError,
269 _("The pretty_printers attribute must be a list."));
273 /* Take care in case the LHS and RHS are related somehow. */
274 tmp = self->printers;
276 self->printers = value;
282 /* Return the Python dictionary attribute containing frame filters for
285 objfpy_get_frame_filters (PyObject *o, void *ignore)
287 objfile_object *self = (objfile_object *) o;
289 Py_INCREF (self->frame_filters);
290 return self->frame_filters;
293 /* Set this object file's frame filters dictionary to FILTERS. */
295 objfpy_set_frame_filters (PyObject *o, PyObject *filters, void *ignore)
298 objfile_object *self = (objfile_object *) o;
302 PyErr_SetString (PyExc_TypeError,
303 _("Cannot delete the frame filters attribute."));
307 if (! PyDict_Check (filters))
309 PyErr_SetString (PyExc_TypeError,
310 _("The frame_filters attribute must be a dictionary."));
314 /* Take care in case the LHS and RHS are related somehow. */
315 tmp = self->frame_filters;
317 self->frame_filters = filters;
323 /* Return the frame unwinders attribute for this object file. */
326 objfpy_get_frame_unwinders (PyObject *o, void *ignore)
328 objfile_object *self = (objfile_object *) o;
330 Py_INCREF (self->frame_unwinders);
331 return self->frame_unwinders;
334 /* Set this object file's frame unwinders list to UNWINDERS. */
337 objfpy_set_frame_unwinders (PyObject *o, PyObject *unwinders, void *ignore)
340 objfile_object *self = (objfile_object *) o;
344 PyErr_SetString (PyExc_TypeError,
345 _("Cannot delete the frame unwinders attribute."));
349 if (!PyList_Check (unwinders))
351 PyErr_SetString (PyExc_TypeError,
352 _("The frame_unwinders attribute must be a list."));
356 /* Take care in case the LHS and RHS are related somehow. */
357 tmp = self->frame_unwinders;
358 Py_INCREF (unwinders);
359 self->frame_unwinders = unwinders;
365 /* Get the 'type_printers' attribute. */
368 objfpy_get_type_printers (PyObject *o, void *ignore)
370 objfile_object *self = (objfile_object *) o;
372 Py_INCREF (self->type_printers);
373 return self->type_printers;
376 /* Get the 'xmethods' attribute. */
379 objfpy_get_xmethods (PyObject *o, void *ignore)
381 objfile_object *self = (objfile_object *) o;
383 Py_INCREF (self->xmethods);
384 return self->xmethods;
387 /* Set the 'type_printers' attribute. */
390 objfpy_set_type_printers (PyObject *o, PyObject *value, void *ignore)
393 objfile_object *self = (objfile_object *) o;
397 PyErr_SetString (PyExc_TypeError,
398 _("Cannot delete the type_printers attribute."));
402 if (! PyList_Check (value))
404 PyErr_SetString (PyExc_TypeError,
405 _("The type_printers attribute must be a list."));
409 /* Take care in case the LHS and RHS are related somehow. */
410 tmp = self->type_printers;
412 self->type_printers = value;
418 /* Implementation of gdb.Objfile.is_valid (self) -> Boolean.
419 Returns True if this object file still exists in GDB. */
422 objfpy_is_valid (PyObject *self, PyObject *args)
424 objfile_object *obj = (objfile_object *) self;
432 /* Implementation of gdb.Objfile.add_separate_debug_file (self) -> Boolean. */
435 objfpy_add_separate_debug_file (PyObject *self, PyObject *args, PyObject *kw)
437 static char *keywords[] = { "file_name", NULL };
438 objfile_object *obj = (objfile_object *) self;
439 const char *file_name;
440 int symfile_flags = 0;
442 OBJFPY_REQUIRE_VALID (obj);
444 if (!PyArg_ParseTupleAndKeywords (args, kw, "s", keywords, &file_name))
449 bfd *abfd = symfile_bfd_open (file_name);
451 symbol_file_add_separate (abfd, file_name, symfile_flags, obj->objfile);
453 CATCH (except, RETURN_MASK_ALL)
455 GDB_PY_HANDLE_EXCEPTION (except);
462 /* Subroutine of gdbpy_lookup_objfile_by_build_id to simplify it.
463 Return non-zero if STRING is a potentially valid build id. */
466 objfpy_build_id_ok (const char *string)
468 size_t i, n = strlen (string);
472 for (i = 0; i < n; ++i)
474 if (!isxdigit (string[i]))
480 /* Subroutine of gdbpy_lookup_objfile_by_build_id to simplify it.
481 Returns non-zero if BUILD_ID matches STRING.
482 It is assumed that objfpy_build_id_ok (string) returns TRUE. */
485 objfpy_build_id_matches (const struct bfd_build_id *build_id,
490 if (strlen (string) != 2 * build_id->size)
493 for (i = 0; i < build_id->size; ++i)
495 char c1 = string[i * 2], c2 = string[i * 2 + 1];
496 int byte = (host_hex_value (c1) << 4) | host_hex_value (c2);
498 if (byte != build_id->data[i])
505 /* Subroutine of gdbpy_lookup_objfile to simplify it.
506 Look up an objfile by its file name. */
508 static struct objfile *
509 objfpy_lookup_objfile_by_name (const char *name)
511 struct objfile *objfile;
513 ALL_OBJFILES (objfile)
515 const char *filename;
517 if ((objfile->flags & OBJF_NOT_FILENAME) != 0)
519 /* Don't return separate debug files. */
520 if (objfile->separate_debug_objfile_backlink != NULL)
523 filename = objfile_filename (objfile);
524 if (filename != NULL && compare_filenames_for_search (filename, name))
526 if (compare_filenames_for_search (objfile->original_name, name))
533 /* Subroutine of gdbpy_lookup_objfile to simplify it.
534 Look up an objfile by its build id. */
536 static struct objfile *
537 objfpy_lookup_objfile_by_build_id (const char *build_id)
539 struct objfile *objfile;
541 ALL_OBJFILES (objfile)
543 const struct bfd_build_id *obfd_build_id;
545 if (objfile->obfd == NULL)
547 /* Don't return separate debug files. */
548 if (objfile->separate_debug_objfile_backlink != NULL)
550 obfd_build_id = build_id_bfd_get (objfile->obfd);
551 if (obfd_build_id == NULL)
553 if (objfpy_build_id_matches (obfd_build_id, build_id))
560 /* Implementation of gdb.lookup_objfile. */
563 gdbpy_lookup_objfile (PyObject *self, PyObject *args, PyObject *kw)
565 static char *keywords[] = { "name", "by_build_id", NULL };
567 PyObject *by_build_id_obj = NULL;
569 struct objfile *objfile;
571 if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O!", keywords,
572 &name, &PyBool_Type, &by_build_id_obj))
576 if (by_build_id_obj != NULL)
578 int cmp = PyObject_IsTrue (by_build_id_obj);
587 if (!objfpy_build_id_ok (name))
589 PyErr_SetString (PyExc_TypeError, _("Not a valid build id."));
592 objfile = objfpy_lookup_objfile_by_build_id (name);
595 objfile = objfpy_lookup_objfile_by_name (name);
599 PyObject *result = objfile_to_objfile_object (objfile);
605 PyErr_SetString (PyExc_ValueError, _("Objfile not found."));
611 /* Clear the OBJFILE pointer in an Objfile object and remove the
614 py_free_objfile (struct objfile *objfile, void *datum)
616 struct cleanup *cleanup;
617 objfile_object *object = (objfile_object *) datum;
619 cleanup = ensure_python_env (get_objfile_arch (objfile), current_language);
620 object->objfile = NULL;
621 Py_DECREF ((PyObject *) object);
622 do_cleanups (cleanup);
625 /* Return a borrowed reference to the Python object of type Objfile
626 representing OBJFILE. If the object has already been created,
627 return it. Otherwise, create it. Return NULL and set the Python
631 objfile_to_objfile_object (struct objfile *objfile)
633 objfile_object *object;
635 object = (objfile_object *) objfile_data (objfile, objfpy_objfile_data_key);
638 object = PyObject_New (objfile_object, &objfile_object_type);
641 if (!objfpy_initialize (object))
647 object->objfile = objfile;
648 set_objfile_data (objfile, objfpy_objfile_data_key, object);
652 return (PyObject *) object;
656 gdbpy_initialize_objfile (void)
658 objfpy_objfile_data_key
659 = register_objfile_data_with_cleanup (NULL, py_free_objfile);
661 if (PyType_Ready (&objfile_object_type) < 0)
664 return gdb_pymodule_addobject (gdb_module, "Objfile",
665 (PyObject *) &objfile_object_type);
670 static PyMethodDef objfile_object_methods[] =
672 { "is_valid", objfpy_is_valid, METH_NOARGS,
673 "is_valid () -> Boolean.\n\
674 Return true if this object file is valid, false if not." },
676 { "add_separate_debug_file", (PyCFunction) objfpy_add_separate_debug_file,
677 METH_VARARGS | METH_KEYWORDS,
678 "add_separate_debug_file (file_name).\n\
679 Add FILE_NAME to the list of files containing debug info for the objfile." },
684 static PyGetSetDef objfile_getset[] =
686 { "__dict__", gdb_py_generic_dict, NULL,
687 "The __dict__ for this objfile.", &objfile_object_type },
688 { "filename", objfpy_get_filename, NULL,
689 "The objfile's filename, or None.", NULL },
690 { "username", objfpy_get_username, NULL,
691 "The name of the objfile as provided by the user, or None.", NULL },
692 { "owner", objfpy_get_owner, NULL,
693 "The objfile owner of separate debug info objfiles, or None.",
695 { "build_id", objfpy_get_build_id, NULL,
696 "The objfile's build id, or None.", NULL },
697 { "progspace", objfpy_get_progspace, NULL,
698 "The objfile's progspace, or None.", NULL },
699 { "pretty_printers", objfpy_get_printers, objfpy_set_printers,
700 "Pretty printers.", NULL },
701 { "frame_filters", objfpy_get_frame_filters,
702 objfpy_set_frame_filters, "Frame Filters.", NULL },
703 { "frame_unwinders", objfpy_get_frame_unwinders,
704 objfpy_set_frame_unwinders, "Frame Unwinders", NULL },
705 { "type_printers", objfpy_get_type_printers, objfpy_set_type_printers,
706 "Type printers.", NULL },
707 { "xmethods", objfpy_get_xmethods, NULL,
708 "Debug methods.", NULL },
712 PyTypeObject objfile_object_type =
714 PyVarObject_HEAD_INIT (NULL, 0)
715 "gdb.Objfile", /*tp_name*/
716 sizeof (objfile_object), /*tp_basicsize*/
718 objfpy_dealloc, /*tp_dealloc*/
725 0, /*tp_as_sequence*/
733 Py_TPFLAGS_DEFAULT, /*tp_flags*/
734 "GDB objfile object", /* tp_doc */
737 0, /* tp_richcompare */
738 0, /* tp_weaklistoffset */
741 objfile_object_methods, /* tp_methods */
743 objfile_getset, /* tp_getset */
746 0, /* tp_descr_get */
747 0, /* tp_descr_set */
748 offsetof (objfile_object, dict), /* tp_dictoffset */
751 objfpy_new, /* tp_new */