1 /* Python interface to objfiles.
3 Copyright (C) 2008-2023 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 /* Clear the OBJFILE pointer in an Objfile object and remove the
63 void operator() (objfile_object
*obj
)
66 gdbpy_ref
<objfile_object
> object (obj
);
67 object
->objfile
= nullptr;
71 static const registry
<objfile
>::key
<objfile_object
, objfpy_deleter
>
72 objfpy_objfile_data_key
;
74 /* Require that OBJF be a valid objfile. */
75 #define OBJFPY_REQUIRE_VALID(obj) \
77 if (!(obj)->objfile) \
79 PyErr_SetString (PyExc_RuntimeError, \
80 _("Objfile no longer exists.")); \
87 /* An Objfile method which returns the objfile's file name, or None. */
90 objfpy_get_filename (PyObject
*self
, void *closure
)
92 objfile_object
*obj
= (objfile_object
*) self
;
95 return (host_string_to_python_string (objfile_name (obj
->objfile
))
100 /* An Objfile method which returns the objfile's file name, as specified
101 by the user, or None. */
104 objfpy_get_username (PyObject
*self
, void *closure
)
106 objfile_object
*obj
= (objfile_object
*) self
;
110 const char *username
= obj
->objfile
->original_name
;
112 return host_string_to_python_string (username
).release ();
118 /* Get the 'is_file' attribute. */
121 objfpy_get_is_file (PyObject
*o
, void *ignore
)
123 objfile_object
*self
= (objfile_object
*) o
;
125 if (self
->objfile
!= nullptr)
126 return PyBool_FromLong ((self
->objfile
->flags
& OBJF_NOT_FILENAME
) == 0);
130 /* If SELF is a separate debug-info file, return the "backlink" field.
131 Otherwise return None. */
134 objfpy_get_owner (PyObject
*self
, void *closure
)
136 objfile_object
*obj
= (objfile_object
*) self
;
137 struct objfile
*objfile
= obj
->objfile
;
138 struct objfile
*owner
;
140 OBJFPY_REQUIRE_VALID (obj
);
142 owner
= objfile
->separate_debug_objfile_backlink
;
144 return objfile_to_objfile_object (owner
).release ();
148 /* An Objfile method which returns the objfile's build id, or None. */
151 objfpy_get_build_id (PyObject
*self
, void *closure
)
153 objfile_object
*obj
= (objfile_object
*) self
;
154 struct objfile
*objfile
= obj
->objfile
;
155 const struct bfd_build_id
*build_id
= NULL
;
157 OBJFPY_REQUIRE_VALID (obj
);
161 build_id
= build_id_bfd_get (objfile
->obfd
.get ());
163 catch (const gdb_exception
&except
)
165 GDB_PY_HANDLE_EXCEPTION (except
);
168 if (build_id
!= NULL
)
170 std::string hex_form
= bin2hex (build_id
->data
, build_id
->size
);
172 return host_string_to_python_string (hex_form
.c_str ()).release ();
178 /* An Objfile method which returns the objfile's progspace, or None. */
181 objfpy_get_progspace (PyObject
*self
, void *closure
)
183 objfile_object
*obj
= (objfile_object
*) self
;
186 return pspace_to_pspace_object (obj
->objfile
->pspace
).release ();
192 objfpy_dealloc (PyObject
*o
)
194 objfile_object
*self
= (objfile_object
*) o
;
196 Py_XDECREF (self
->dict
);
197 Py_XDECREF (self
->printers
);
198 Py_XDECREF (self
->frame_filters
);
199 Py_XDECREF (self
->frame_unwinders
);
200 Py_XDECREF (self
->type_printers
);
201 Py_XDECREF (self
->xmethods
);
202 Py_TYPE (self
)->tp_free (self
);
205 /* Initialize an objfile_object.
206 The result is a boolean indicating success. */
209 objfpy_initialize (objfile_object
*self
)
211 self
->objfile
= NULL
;
213 self
->dict
= PyDict_New ();
214 if (self
->dict
== NULL
)
217 self
->printers
= PyList_New (0);
218 if (self
->printers
== NULL
)
221 self
->frame_filters
= PyDict_New ();
222 if (self
->frame_filters
== NULL
)
225 self
->frame_unwinders
= PyList_New (0);
226 if (self
->frame_unwinders
== NULL
)
229 self
->type_printers
= PyList_New (0);
230 if (self
->type_printers
== NULL
)
233 self
->xmethods
= PyList_New (0);
234 if (self
->xmethods
== NULL
)
241 objfpy_new (PyTypeObject
*type
, PyObject
*args
, PyObject
*keywords
)
243 gdbpy_ref
<objfile_object
> self ((objfile_object
*) type
->tp_alloc (type
, 0));
247 if (!objfpy_initialize (self
.get ()))
251 return (PyObject
*) self
.release ();
255 objfpy_get_printers (PyObject
*o
, void *ignore
)
257 objfile_object
*self
= (objfile_object
*) o
;
259 Py_INCREF (self
->printers
);
260 return self
->printers
;
264 objfpy_set_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
266 objfile_object
*self
= (objfile_object
*) o
;
270 PyErr_SetString (PyExc_TypeError
,
271 _("Cannot delete the pretty_printers attribute."));
275 if (! PyList_Check (value
))
277 PyErr_SetString (PyExc_TypeError
,
278 _("The pretty_printers attribute must be a list."));
282 /* Take care in case the LHS and RHS are related somehow. */
283 gdbpy_ref
<> tmp (self
->printers
);
285 self
->printers
= value
;
290 /* Return the Python dictionary attribute containing frame filters for
293 objfpy_get_frame_filters (PyObject
*o
, void *ignore
)
295 objfile_object
*self
= (objfile_object
*) o
;
297 Py_INCREF (self
->frame_filters
);
298 return self
->frame_filters
;
301 /* Set this object file's frame filters dictionary to FILTERS. */
303 objfpy_set_frame_filters (PyObject
*o
, PyObject
*filters
, void *ignore
)
305 objfile_object
*self
= (objfile_object
*) o
;
309 PyErr_SetString (PyExc_TypeError
,
310 _("Cannot delete the frame filters attribute."));
314 if (! PyDict_Check (filters
))
316 PyErr_SetString (PyExc_TypeError
,
317 _("The frame_filters attribute must be a dictionary."));
321 /* Take care in case the LHS and RHS are related somehow. */
322 gdbpy_ref
<> tmp (self
->frame_filters
);
324 self
->frame_filters
= filters
;
329 /* Return the frame unwinders attribute for this object file. */
332 objfpy_get_frame_unwinders (PyObject
*o
, void *ignore
)
334 objfile_object
*self
= (objfile_object
*) o
;
336 Py_INCREF (self
->frame_unwinders
);
337 return self
->frame_unwinders
;
340 /* Set this object file's frame unwinders list to UNWINDERS. */
343 objfpy_set_frame_unwinders (PyObject
*o
, PyObject
*unwinders
, void *ignore
)
345 objfile_object
*self
= (objfile_object
*) o
;
349 PyErr_SetString (PyExc_TypeError
,
350 _("Cannot delete the frame unwinders attribute."));
354 if (!PyList_Check (unwinders
))
356 PyErr_SetString (PyExc_TypeError
,
357 _("The frame_unwinders attribute must be a list."));
361 /* Take care in case the LHS and RHS are related somehow. */
362 gdbpy_ref
<> tmp (self
->frame_unwinders
);
363 Py_INCREF (unwinders
);
364 self
->frame_unwinders
= unwinders
;
369 /* Get the 'type_printers' attribute. */
372 objfpy_get_type_printers (PyObject
*o
, void *ignore
)
374 objfile_object
*self
= (objfile_object
*) o
;
376 Py_INCREF (self
->type_printers
);
377 return self
->type_printers
;
380 /* Get the 'xmethods' attribute. */
383 objfpy_get_xmethods (PyObject
*o
, void *ignore
)
385 objfile_object
*self
= (objfile_object
*) o
;
387 Py_INCREF (self
->xmethods
);
388 return self
->xmethods
;
391 /* Set the 'type_printers' attribute. */
394 objfpy_set_type_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
396 objfile_object
*self
= (objfile_object
*) o
;
400 PyErr_SetString (PyExc_TypeError
,
401 _("Cannot delete the type_printers attribute."));
405 if (! PyList_Check (value
))
407 PyErr_SetString (PyExc_TypeError
,
408 _("The type_printers attribute must be a list."));
412 /* Take care in case the LHS and RHS are related somehow. */
413 gdbpy_ref
<> tmp (self
->type_printers
);
415 self
->type_printers
= value
;
420 /* Implementation of gdb.Objfile.is_valid (self) -> Boolean.
421 Returns True if this object file still exists in GDB. */
424 objfpy_is_valid (PyObject
*self
, PyObject
*args
)
426 objfile_object
*obj
= (objfile_object
*) self
;
434 /* Implementation of gdb.Objfile.add_separate_debug_file (self, string). */
437 objfpy_add_separate_debug_file (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
439 static const char *keywords
[] = { "file_name", NULL
};
440 objfile_object
*obj
= (objfile_object
*) self
;
441 const char *file_name
;
443 OBJFPY_REQUIRE_VALID (obj
);
445 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "s", keywords
, &file_name
))
450 gdb_bfd_ref_ptr
abfd (symfile_bfd_open (file_name
));
452 symbol_file_add_separate (abfd
, file_name
, 0, obj
->objfile
);
454 catch (const gdb_exception
&except
)
456 GDB_PY_HANDLE_EXCEPTION (except
);
463 gdb.Objfile.lookup_global_symbol (self, string [, domain]) -> gdb.Symbol. */
466 objfpy_lookup_global_symbol (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
468 static const char *keywords
[] = { "name", "domain", NULL
};
469 objfile_object
*obj
= (objfile_object
*) self
;
470 const char *symbol_name
;
471 int domain
= VAR_DOMAIN
;
473 OBJFPY_REQUIRE_VALID (obj
);
475 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "s|i", keywords
, &symbol_name
,
481 struct symbol
*sym
= lookup_global_symbol_from_objfile
482 (obj
->objfile
, GLOBAL_BLOCK
, symbol_name
, (domain_enum
) domain
).symbol
;
486 return symbol_to_symbol_object (sym
);
488 catch (const gdb_exception
&except
)
490 GDB_PY_HANDLE_EXCEPTION (except
);
497 gdb.Objfile.lookup_static_symbol (self, string [, domain]) -> gdb.Symbol. */
500 objfpy_lookup_static_symbol (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
502 static const char *keywords
[] = { "name", "domain", NULL
};
503 objfile_object
*obj
= (objfile_object
*) self
;
504 const char *symbol_name
;
505 int domain
= VAR_DOMAIN
;
507 OBJFPY_REQUIRE_VALID (obj
);
509 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "s|i", keywords
, &symbol_name
,
515 struct symbol
*sym
= lookup_global_symbol_from_objfile
516 (obj
->objfile
, STATIC_BLOCK
, symbol_name
, (domain_enum
) domain
).symbol
;
520 return symbol_to_symbol_object (sym
);
522 catch (const gdb_exception
&except
)
524 GDB_PY_HANDLE_EXCEPTION (except
);
530 /* Implement repr() for gdb.Objfile. */
533 objfpy_repr (PyObject
*self_
)
535 objfile_object
*self
= (objfile_object
*) self_
;
536 objfile
*obj
= self
->objfile
;
539 return PyUnicode_FromString ("<gdb.Objfile (invalid)>");
541 return PyUnicode_FromFormat ("<gdb.Objfile filename=%s>",
545 /* Subroutine of gdbpy_lookup_objfile_by_build_id to simplify it.
546 Return non-zero if STRING is a potentially valid build id. */
549 objfpy_build_id_ok (const char *string
)
551 size_t i
, n
= strlen (string
);
555 for (i
= 0; i
< n
; ++i
)
557 if (!isxdigit (string
[i
]))
563 /* Subroutine of gdbpy_lookup_objfile_by_build_id to simplify it.
564 Returns non-zero if BUILD_ID matches STRING.
565 It is assumed that objfpy_build_id_ok (string) returns TRUE. */
568 objfpy_build_id_matches (const struct bfd_build_id
*build_id
,
573 if (strlen (string
) != 2 * build_id
->size
)
576 for (i
= 0; i
< build_id
->size
; ++i
)
578 char c1
= string
[i
* 2], c2
= string
[i
* 2 + 1];
579 int byte
= (fromhex (c1
) << 4) | fromhex (c2
);
581 if (byte
!= build_id
->data
[i
])
588 /* Implementation of gdb.lookup_objfile. */
591 gdbpy_lookup_objfile (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
593 static const char *keywords
[] = { "name", "by_build_id", NULL
};
595 PyObject
*by_build_id_obj
= NULL
;
598 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "s|O!", keywords
,
599 &name
, &PyBool_Type
, &by_build_id_obj
))
603 if (by_build_id_obj
!= NULL
)
605 int cmp
= PyObject_IsTrue (by_build_id_obj
);
612 if (by_build_id
&& !objfpy_build_id_ok (name
))
614 PyErr_SetString (PyExc_TypeError
, _("Not a valid build id."));
618 struct objfile
*objfile
= nullptr;
620 gdbarch_iterate_over_objfiles_in_search_order
622 [&objfile
, name
] (struct objfile
*obj
)
624 /* Don't return separate debug files. */
625 if (obj
->separate_debug_objfile_backlink
!= nullptr)
628 bfd
*obfd
= obj
->obfd
.get ();
632 const bfd_build_id
*obfd_build_id
= build_id_bfd_get (obfd
);
633 if (obfd_build_id
== nullptr)
636 if (!objfpy_build_id_matches (obfd_build_id
, name
))
641 }, gdbpy_current_objfile
);
643 gdbarch_iterate_over_objfiles_in_search_order
645 [&objfile
, name
] (struct objfile
*obj
)
647 /* Don't return separate debug files. */
648 if (obj
->separate_debug_objfile_backlink
!= nullptr)
651 if ((obj
->flags
& OBJF_NOT_FILENAME
) != 0)
654 const char *filename
= objfile_filename (obj
);
656 && compare_filenames_for_search (filename
, name
))
662 if (compare_filenames_for_search (obj
->original_name
, name
))
669 }, gdbpy_current_objfile
);
672 return objfile_to_objfile_object (objfile
).release ();
674 PyErr_SetString (PyExc_ValueError
, _("Objfile not found."));
680 /* Return a new reference to the Python object of type Objfile
681 representing OBJFILE. If the object has already been created,
682 return it. Otherwise, create it. Return NULL and set the Python
686 objfile_to_objfile_object (struct objfile
*objfile
)
689 = (PyObject
*) objfpy_objfile_data_key
.get (objfile
);
692 gdbpy_ref
<objfile_object
> object
693 ((objfile_object
*) PyObject_New (objfile_object
, &objfile_object_type
));
696 if (!objfpy_initialize (object
.get ()))
699 object
->objfile
= objfile
;
700 objfpy_objfile_data_key
.set (objfile
, object
.get ());
701 result
= (PyObject
*) object
.release ();
704 return gdbpy_ref
<>::new_reference (result
);
708 gdbpy_initialize_objfile (void)
710 if (PyType_Ready (&objfile_object_type
) < 0)
713 return gdb_pymodule_addobject (gdb_module
, "Objfile",
714 (PyObject
*) &objfile_object_type
);
719 static PyMethodDef objfile_object_methods
[] =
721 { "is_valid", objfpy_is_valid
, METH_NOARGS
,
722 "is_valid () -> Boolean.\n\
723 Return true if this object file is valid, false if not." },
725 { "add_separate_debug_file", (PyCFunction
) objfpy_add_separate_debug_file
,
726 METH_VARARGS
| METH_KEYWORDS
,
727 "add_separate_debug_file (file_name).\n\
728 Add FILE_NAME to the list of files containing debug info for the objfile." },
730 { "lookup_global_symbol", (PyCFunction
) objfpy_lookup_global_symbol
,
731 METH_VARARGS
| METH_KEYWORDS
,
732 "lookup_global_symbol (name [, domain]).\n\
733 Look up a global symbol in this objfile and return it." },
735 { "lookup_static_symbol", (PyCFunction
) objfpy_lookup_static_symbol
,
736 METH_VARARGS
| METH_KEYWORDS
,
737 "lookup_static_symbol (name [, domain]).\n\
738 Look up a static-linkage global symbol in this objfile and return it." },
743 static gdb_PyGetSetDef objfile_getset
[] =
745 { "__dict__", gdb_py_generic_dict
, NULL
,
746 "The __dict__ for this objfile.", &objfile_object_type
},
747 { "filename", objfpy_get_filename
, NULL
,
748 "The objfile's filename, or None.", NULL
},
749 { "username", objfpy_get_username
, NULL
,
750 "The name of the objfile as provided by the user, or None.", NULL
},
751 { "owner", objfpy_get_owner
, NULL
,
752 "The objfile owner of separate debug info objfiles, or None.",
754 { "build_id", objfpy_get_build_id
, NULL
,
755 "The objfile's build id, or None.", NULL
},
756 { "progspace", objfpy_get_progspace
, NULL
,
757 "The objfile's progspace, or None.", NULL
},
758 { "pretty_printers", objfpy_get_printers
, objfpy_set_printers
,
759 "Pretty printers.", NULL
},
760 { "frame_filters", objfpy_get_frame_filters
,
761 objfpy_set_frame_filters
, "Frame Filters.", NULL
},
762 { "frame_unwinders", objfpy_get_frame_unwinders
,
763 objfpy_set_frame_unwinders
, "Frame Unwinders", NULL
},
764 { "type_printers", objfpy_get_type_printers
, objfpy_set_type_printers
,
765 "Type printers.", NULL
},
766 { "xmethods", objfpy_get_xmethods
, NULL
,
767 "Debug methods.", NULL
},
768 { "is_file", objfpy_get_is_file
, nullptr,
769 "Whether this objfile came from a file.", nullptr },
773 PyTypeObject objfile_object_type
=
775 PyVarObject_HEAD_INIT (NULL
, 0)
776 "gdb.Objfile", /*tp_name*/
777 sizeof (objfile_object
), /*tp_basicsize*/
779 objfpy_dealloc
, /*tp_dealloc*/
784 objfpy_repr
, /*tp_repr*/
786 0, /*tp_as_sequence*/
794 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
795 "GDB objfile object", /* tp_doc */
798 0, /* tp_richcompare */
799 0, /* tp_weaklistoffset */
802 objfile_object_methods
, /* tp_methods */
804 objfile_getset
, /* tp_getset */
807 0, /* tp_descr_get */
808 0, /* tp_descr_set */
809 offsetof (objfile_object
, dict
), /* tp_dictoffset */
812 objfpy_new
, /* tp_new */