1 /* Python interface to objfiles.
3 Copyright (C) 2008-2019 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"
33 /* The corresponding objfile. */
34 struct objfile *objfile;
36 /* Dictionary holding user-added attributes.
37 This is the __dict__ attribute of the object. */
40 /* The pretty-printer list of functions. */
43 /* The frame filter list of functions. */
44 PyObject *frame_filters;
46 /* The list of frame unwinders. */
47 PyObject *frame_unwinders;
49 /* The type-printer list. */
50 PyObject *type_printers;
52 /* The debug method matcher list. */
56 extern PyTypeObject objfile_object_type
57 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("objfile_object");
59 static const struct objfile_data *objfpy_objfile_data_key;
61 /* Require that OBJF be a valid objfile. */
62 #define OBJFPY_REQUIRE_VALID(obj) \
64 if (!(obj)->objfile) \
66 PyErr_SetString (PyExc_RuntimeError, \
67 _("Objfile no longer exists.")); \
74 /* An Objfile method which returns the objfile's file name, or None. */
77 objfpy_get_filename (PyObject *self, void *closure)
79 objfile_object *obj = (objfile_object *) self;
82 return (host_string_to_python_string (objfile_name (obj->objfile))
87 /* An Objfile method which returns the objfile's file name, as specified
88 by the user, or None. */
91 objfpy_get_username (PyObject *self, void *closure)
93 objfile_object *obj = (objfile_object *) self;
97 const char *username = obj->objfile->original_name;
99 return host_string_to_python_string (username).release ();
105 /* If SELF is a separate debug-info file, return the "backlink" field.
106 Otherwise return None. */
109 objfpy_get_owner (PyObject *self, void *closure)
111 objfile_object *obj = (objfile_object *) self;
112 struct objfile *objfile = obj->objfile;
113 struct objfile *owner;
115 OBJFPY_REQUIRE_VALID (obj);
117 owner = objfile->separate_debug_objfile_backlink;
119 return objfile_to_objfile_object (owner).release ();
123 /* An Objfile method which returns the objfile's build id, or None. */
126 objfpy_get_build_id (PyObject *self, void *closure)
128 objfile_object *obj = (objfile_object *) self;
129 struct objfile *objfile = obj->objfile;
130 const struct bfd_build_id *build_id = NULL;
132 OBJFPY_REQUIRE_VALID (obj);
136 build_id = build_id_bfd_get (objfile->obfd);
138 CATCH (except, RETURN_MASK_ALL)
140 GDB_PY_HANDLE_EXCEPTION (except);
144 if (build_id != NULL)
146 gdb::unique_xmalloc_ptr<char> hex_form
147 (make_hex_string (build_id->data, build_id->size));
149 return host_string_to_python_string (hex_form.get ()).release ();
155 /* An Objfile method which returns the objfile's progspace, or None. */
158 objfpy_get_progspace (PyObject *self, void *closure)
160 objfile_object *obj = (objfile_object *) self;
163 return pspace_to_pspace_object (obj->objfile->pspace).release ();
169 objfpy_dealloc (PyObject *o)
171 objfile_object *self = (objfile_object *) o;
173 Py_XDECREF (self->dict);
174 Py_XDECREF (self->printers);
175 Py_XDECREF (self->frame_filters);
176 Py_XDECREF (self->frame_unwinders);
177 Py_XDECREF (self->type_printers);
178 Py_XDECREF (self->xmethods);
179 Py_TYPE (self)->tp_free (self);
182 /* Initialize an objfile_object.
183 The result is a boolean indicating success. */
186 objfpy_initialize (objfile_object *self)
188 self->objfile = NULL;
190 self->dict = PyDict_New ();
191 if (self->dict == NULL)
194 self->printers = PyList_New (0);
195 if (self->printers == NULL)
198 self->frame_filters = PyDict_New ();
199 if (self->frame_filters == NULL)
202 self->frame_unwinders = PyList_New (0);
203 if (self->frame_unwinders == NULL)
206 self->type_printers = PyList_New (0);
207 if (self->type_printers == NULL)
210 self->xmethods = PyList_New (0);
211 if (self->xmethods == NULL)
218 objfpy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
220 gdbpy_ref<objfile_object> self ((objfile_object *) type->tp_alloc (type, 0));
224 if (!objfpy_initialize (self.get ()))
228 return (PyObject *) self.release ();
232 objfpy_get_printers (PyObject *o, void *ignore)
234 objfile_object *self = (objfile_object *) o;
236 Py_INCREF (self->printers);
237 return self->printers;
241 objfpy_set_printers (PyObject *o, PyObject *value, void *ignore)
243 objfile_object *self = (objfile_object *) o;
247 PyErr_SetString (PyExc_TypeError,
248 _("Cannot delete the pretty_printers attribute."));
252 if (! PyList_Check (value))
254 PyErr_SetString (PyExc_TypeError,
255 _("The pretty_printers attribute must be a list."));
259 /* Take care in case the LHS and RHS are related somehow. */
260 gdbpy_ref<> tmp (self->printers);
262 self->printers = value;
267 /* Return the Python dictionary attribute containing frame filters for
270 objfpy_get_frame_filters (PyObject *o, void *ignore)
272 objfile_object *self = (objfile_object *) o;
274 Py_INCREF (self->frame_filters);
275 return self->frame_filters;
278 /* Set this object file's frame filters dictionary to FILTERS. */
280 objfpy_set_frame_filters (PyObject *o, PyObject *filters, void *ignore)
282 objfile_object *self = (objfile_object *) o;
286 PyErr_SetString (PyExc_TypeError,
287 _("Cannot delete the frame filters attribute."));
291 if (! PyDict_Check (filters))
293 PyErr_SetString (PyExc_TypeError,
294 _("The frame_filters attribute must be a dictionary."));
298 /* Take care in case the LHS and RHS are related somehow. */
299 gdbpy_ref<> tmp (self->frame_filters);
301 self->frame_filters = filters;
306 /* Return the frame unwinders attribute for this object file. */
309 objfpy_get_frame_unwinders (PyObject *o, void *ignore)
311 objfile_object *self = (objfile_object *) o;
313 Py_INCREF (self->frame_unwinders);
314 return self->frame_unwinders;
317 /* Set this object file's frame unwinders list to UNWINDERS. */
320 objfpy_set_frame_unwinders (PyObject *o, PyObject *unwinders, void *ignore)
322 objfile_object *self = (objfile_object *) o;
326 PyErr_SetString (PyExc_TypeError,
327 _("Cannot delete the frame unwinders attribute."));
331 if (!PyList_Check (unwinders))
333 PyErr_SetString (PyExc_TypeError,
334 _("The frame_unwinders attribute must be a list."));
338 /* Take care in case the LHS and RHS are related somehow. */
339 gdbpy_ref<> tmp (self->frame_unwinders);
340 Py_INCREF (unwinders);
341 self->frame_unwinders = unwinders;
346 /* Get the 'type_printers' attribute. */
349 objfpy_get_type_printers (PyObject *o, void *ignore)
351 objfile_object *self = (objfile_object *) o;
353 Py_INCREF (self->type_printers);
354 return self->type_printers;
357 /* Get the 'xmethods' attribute. */
360 objfpy_get_xmethods (PyObject *o, void *ignore)
362 objfile_object *self = (objfile_object *) o;
364 Py_INCREF (self->xmethods);
365 return self->xmethods;
368 /* Set the 'type_printers' attribute. */
371 objfpy_set_type_printers (PyObject *o, PyObject *value, void *ignore)
373 objfile_object *self = (objfile_object *) o;
377 PyErr_SetString (PyExc_TypeError,
378 _("Cannot delete the type_printers attribute."));
382 if (! PyList_Check (value))
384 PyErr_SetString (PyExc_TypeError,
385 _("The type_printers attribute must be a list."));
389 /* Take care in case the LHS and RHS are related somehow. */
390 gdbpy_ref<> tmp (self->type_printers);
392 self->type_printers = value;
397 /* Implementation of gdb.Objfile.is_valid (self) -> Boolean.
398 Returns True if this object file still exists in GDB. */
401 objfpy_is_valid (PyObject *self, PyObject *args)
403 objfile_object *obj = (objfile_object *) self;
411 /* Implementation of gdb.Objfile.add_separate_debug_file (self) -> Boolean. */
414 objfpy_add_separate_debug_file (PyObject *self, PyObject *args, PyObject *kw)
416 static const char *keywords[] = { "file_name", NULL };
417 objfile_object *obj = (objfile_object *) self;
418 const char *file_name;
420 OBJFPY_REQUIRE_VALID (obj);
422 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s", keywords, &file_name))
427 gdb_bfd_ref_ptr abfd (symfile_bfd_open (file_name));
429 symbol_file_add_separate (abfd.get (), file_name, 0, obj->objfile);
431 CATCH (except, RETURN_MASK_ALL)
433 GDB_PY_HANDLE_EXCEPTION (except);
440 /* Implement repr() for gdb.Objfile. */
443 objfpy_repr (PyObject *self_)
445 objfile_object *self = (objfile_object *) self_;
446 objfile *obj = self->objfile;
449 return PyString_FromString ("<gdb.Objfile (invalid)>");
451 return PyString_FromFormat ("<gdb.Objfile filename=%s>",
452 objfile_filename (obj));
455 /* Subroutine of gdbpy_lookup_objfile_by_build_id to simplify it.
456 Return non-zero if STRING is a potentially valid build id. */
459 objfpy_build_id_ok (const char *string)
461 size_t i, n = strlen (string);
465 for (i = 0; i < n; ++i)
467 if (!isxdigit (string[i]))
473 /* Subroutine of gdbpy_lookup_objfile_by_build_id to simplify it.
474 Returns non-zero if BUILD_ID matches STRING.
475 It is assumed that objfpy_build_id_ok (string) returns TRUE. */
478 objfpy_build_id_matches (const struct bfd_build_id *build_id,
483 if (strlen (string) != 2 * build_id->size)
486 for (i = 0; i < build_id->size; ++i)
488 char c1 = string[i * 2], c2 = string[i * 2 + 1];
489 int byte = (host_hex_value (c1) << 4) | host_hex_value (c2);
491 if (byte != build_id->data[i])
498 /* Subroutine of gdbpy_lookup_objfile to simplify it.
499 Look up an objfile by its file name. */
501 static struct objfile *
502 objfpy_lookup_objfile_by_name (const char *name)
504 struct objfile *objfile;
506 ALL_OBJFILES (objfile)
508 const char *filename;
510 if ((objfile->flags & OBJF_NOT_FILENAME) != 0)
512 /* Don't return separate debug files. */
513 if (objfile->separate_debug_objfile_backlink != NULL)
516 filename = objfile_filename (objfile);
517 if (filename != NULL && compare_filenames_for_search (filename, name))
519 if (compare_filenames_for_search (objfile->original_name, name))
526 /* Subroutine of gdbpy_lookup_objfile to simplify it.
527 Look up an objfile by its build id. */
529 static struct objfile *
530 objfpy_lookup_objfile_by_build_id (const char *build_id)
532 struct objfile *objfile;
534 ALL_OBJFILES (objfile)
536 const struct bfd_build_id *obfd_build_id;
538 if (objfile->obfd == NULL)
540 /* Don't return separate debug files. */
541 if (objfile->separate_debug_objfile_backlink != NULL)
543 obfd_build_id = build_id_bfd_get (objfile->obfd);
544 if (obfd_build_id == NULL)
546 if (objfpy_build_id_matches (obfd_build_id, build_id))
553 /* Implementation of gdb.lookup_objfile. */
556 gdbpy_lookup_objfile (PyObject *self, PyObject *args, PyObject *kw)
558 static const char *keywords[] = { "name", "by_build_id", NULL };
560 PyObject *by_build_id_obj = NULL;
562 struct objfile *objfile;
564 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|O!", keywords,
565 &name, &PyBool_Type, &by_build_id_obj))
569 if (by_build_id_obj != NULL)
571 int cmp = PyObject_IsTrue (by_build_id_obj);
580 if (!objfpy_build_id_ok (name))
582 PyErr_SetString (PyExc_TypeError, _("Not a valid build id."));
585 objfile = objfpy_lookup_objfile_by_build_id (name);
588 objfile = objfpy_lookup_objfile_by_name (name);
591 return objfile_to_objfile_object (objfile).release ();
593 PyErr_SetString (PyExc_ValueError, _("Objfile not found."));
599 /* Clear the OBJFILE pointer in an Objfile object and remove the
602 py_free_objfile (struct objfile *objfile, void *datum)
604 gdbpy_enter enter_py (get_objfile_arch (objfile), current_language);
605 gdbpy_ref<objfile_object> object ((objfile_object *) datum);
606 object->objfile = NULL;
609 /* Return a new reference to the Python object of type Objfile
610 representing OBJFILE. If the object has already been created,
611 return it. Otherwise, create it. Return NULL and set the Python
615 objfile_to_objfile_object (struct objfile *objfile)
618 = ((PyObject *) objfile_data (objfile, objfpy_objfile_data_key));
621 gdbpy_ref<objfile_object> object
622 ((objfile_object *) PyObject_New (objfile_object, &objfile_object_type));
625 if (!objfpy_initialize (object.get ()))
628 object->objfile = objfile;
629 set_objfile_data (objfile, objfpy_objfile_data_key, object.get ());
630 result = (PyObject *) object.release ();
633 return gdbpy_ref<>::new_reference (result);
637 gdbpy_initialize_objfile (void)
639 objfpy_objfile_data_key
640 = register_objfile_data_with_cleanup (NULL, py_free_objfile);
642 if (PyType_Ready (&objfile_object_type) < 0)
645 return gdb_pymodule_addobject (gdb_module, "Objfile",
646 (PyObject *) &objfile_object_type);
651 static PyMethodDef objfile_object_methods[] =
653 { "is_valid", objfpy_is_valid, METH_NOARGS,
654 "is_valid () -> Boolean.\n\
655 Return true if this object file is valid, false if not." },
657 { "add_separate_debug_file", (PyCFunction) objfpy_add_separate_debug_file,
658 METH_VARARGS | METH_KEYWORDS,
659 "add_separate_debug_file (file_name).\n\
660 Add FILE_NAME to the list of files containing debug info for the objfile." },
665 static gdb_PyGetSetDef objfile_getset[] =
667 { "__dict__", gdb_py_generic_dict, NULL,
668 "The __dict__ for this objfile.", &objfile_object_type },
669 { "filename", objfpy_get_filename, NULL,
670 "The objfile's filename, or None.", NULL },
671 { "username", objfpy_get_username, NULL,
672 "The name of the objfile as provided by the user, or None.", NULL },
673 { "owner", objfpy_get_owner, NULL,
674 "The objfile owner of separate debug info objfiles, or None.",
676 { "build_id", objfpy_get_build_id, NULL,
677 "The objfile's build id, or None.", NULL },
678 { "progspace", objfpy_get_progspace, NULL,
679 "The objfile's progspace, or None.", NULL },
680 { "pretty_printers", objfpy_get_printers, objfpy_set_printers,
681 "Pretty printers.", NULL },
682 { "frame_filters", objfpy_get_frame_filters,
683 objfpy_set_frame_filters, "Frame Filters.", NULL },
684 { "frame_unwinders", objfpy_get_frame_unwinders,
685 objfpy_set_frame_unwinders, "Frame Unwinders", NULL },
686 { "type_printers", objfpy_get_type_printers, objfpy_set_type_printers,
687 "Type printers.", NULL },
688 { "xmethods", objfpy_get_xmethods, NULL,
689 "Debug methods.", NULL },
693 PyTypeObject objfile_object_type =
695 PyVarObject_HEAD_INIT (NULL, 0)
696 "gdb.Objfile", /*tp_name*/
697 sizeof (objfile_object), /*tp_basicsize*/
699 objfpy_dealloc, /*tp_dealloc*/
704 objfpy_repr, /*tp_repr*/
706 0, /*tp_as_sequence*/
714 Py_TPFLAGS_DEFAULT, /*tp_flags*/
715 "GDB objfile object", /* tp_doc */
718 0, /* tp_richcompare */
719 0, /* tp_weaklistoffset */
722 objfile_object_methods, /* tp_methods */
724 objfile_getset, /* tp_getset */
727 0, /* tp_descr_get */
728 0, /* tp_descr_set */
729 offsetof (objfile_object, dict), /* tp_dictoffset */
732 objfpy_new, /* tp_new */