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"
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
))
86 /* An Objfile method which returns the objfile's file name, as specified
87 by the user, or None. */
90 objfpy_get_username (PyObject
*self
, void *closure
)
92 objfile_object
*obj
= (objfile_object
*) self
;
96 const char *username
= obj
->objfile
->original_name
;
98 return host_string_to_python_string (username
).release ();
104 /* If SELF is a separate debug-info file, return the "backlink" field.
105 Otherwise return None. */
108 objfpy_get_owner (PyObject
*self
, void *closure
)
110 objfile_object
*obj
= (objfile_object
*) self
;
111 struct objfile
*objfile
= obj
->objfile
;
112 struct objfile
*owner
;
114 OBJFPY_REQUIRE_VALID (obj
);
116 owner
= objfile
->separate_debug_objfile_backlink
;
118 return objfile_to_objfile_object (owner
).release ();
122 /* An Objfile method which returns the objfile's build id, or None. */
125 objfpy_get_build_id (PyObject
*self
, void *closure
)
127 objfile_object
*obj
= (objfile_object
*) self
;
128 struct objfile
*objfile
= obj
->objfile
;
129 const struct bfd_build_id
*build_id
= NULL
;
131 OBJFPY_REQUIRE_VALID (obj
);
135 build_id
= build_id_bfd_get (objfile
->obfd
);
137 catch (const gdb_exception
&except
)
139 GDB_PY_HANDLE_EXCEPTION (except
);
142 if (build_id
!= NULL
)
144 std::string hex_form
= bin2hex (build_id
->data
, build_id
->size
);
146 return host_string_to_python_string (hex_form
.c_str ()).release ();
152 /* An Objfile method which returns the objfile's progspace, or None. */
155 objfpy_get_progspace (PyObject
*self
, void *closure
)
157 objfile_object
*obj
= (objfile_object
*) self
;
160 return pspace_to_pspace_object (obj
->objfile
->pspace
).release ();
166 objfpy_dealloc (PyObject
*o
)
168 objfile_object
*self
= (objfile_object
*) o
;
170 Py_XDECREF (self
->dict
);
171 Py_XDECREF (self
->printers
);
172 Py_XDECREF (self
->frame_filters
);
173 Py_XDECREF (self
->frame_unwinders
);
174 Py_XDECREF (self
->type_printers
);
175 Py_XDECREF (self
->xmethods
);
176 Py_TYPE (self
)->tp_free (self
);
179 /* Initialize an objfile_object.
180 The result is a boolean indicating success. */
183 objfpy_initialize (objfile_object
*self
)
185 self
->objfile
= NULL
;
187 self
->dict
= PyDict_New ();
188 if (self
->dict
== NULL
)
191 self
->printers
= PyList_New (0);
192 if (self
->printers
== NULL
)
195 self
->frame_filters
= PyDict_New ();
196 if (self
->frame_filters
== NULL
)
199 self
->frame_unwinders
= PyList_New (0);
200 if (self
->frame_unwinders
== NULL
)
203 self
->type_printers
= PyList_New (0);
204 if (self
->type_printers
== NULL
)
207 self
->xmethods
= PyList_New (0);
208 if (self
->xmethods
== NULL
)
215 objfpy_new (PyTypeObject
*type
, PyObject
*args
, PyObject
*keywords
)
217 gdbpy_ref
<objfile_object
> self ((objfile_object
*) type
->tp_alloc (type
, 0));
221 if (!objfpy_initialize (self
.get ()))
225 return (PyObject
*) self
.release ();
229 objfpy_get_printers (PyObject
*o
, void *ignore
)
231 objfile_object
*self
= (objfile_object
*) o
;
233 Py_INCREF (self
->printers
);
234 return self
->printers
;
238 objfpy_set_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
240 objfile_object
*self
= (objfile_object
*) o
;
244 PyErr_SetString (PyExc_TypeError
,
245 _("Cannot delete the pretty_printers attribute."));
249 if (! PyList_Check (value
))
251 PyErr_SetString (PyExc_TypeError
,
252 _("The pretty_printers attribute must be a list."));
256 /* Take care in case the LHS and RHS are related somehow. */
257 gdbpy_ref
<> tmp (self
->printers
);
259 self
->printers
= value
;
264 /* Return the Python dictionary attribute containing frame filters for
267 objfpy_get_frame_filters (PyObject
*o
, void *ignore
)
269 objfile_object
*self
= (objfile_object
*) o
;
271 Py_INCREF (self
->frame_filters
);
272 return self
->frame_filters
;
275 /* Set this object file's frame filters dictionary to FILTERS. */
277 objfpy_set_frame_filters (PyObject
*o
, PyObject
*filters
, void *ignore
)
279 objfile_object
*self
= (objfile_object
*) o
;
283 PyErr_SetString (PyExc_TypeError
,
284 _("Cannot delete the frame filters attribute."));
288 if (! PyDict_Check (filters
))
290 PyErr_SetString (PyExc_TypeError
,
291 _("The frame_filters attribute must be a dictionary."));
295 /* Take care in case the LHS and RHS are related somehow. */
296 gdbpy_ref
<> tmp (self
->frame_filters
);
298 self
->frame_filters
= filters
;
303 /* Return the frame unwinders attribute for this object file. */
306 objfpy_get_frame_unwinders (PyObject
*o
, void *ignore
)
308 objfile_object
*self
= (objfile_object
*) o
;
310 Py_INCREF (self
->frame_unwinders
);
311 return self
->frame_unwinders
;
314 /* Set this object file's frame unwinders list to UNWINDERS. */
317 objfpy_set_frame_unwinders (PyObject
*o
, PyObject
*unwinders
, void *ignore
)
319 objfile_object
*self
= (objfile_object
*) o
;
323 PyErr_SetString (PyExc_TypeError
,
324 _("Cannot delete the frame unwinders attribute."));
328 if (!PyList_Check (unwinders
))
330 PyErr_SetString (PyExc_TypeError
,
331 _("The frame_unwinders attribute must be a list."));
335 /* Take care in case the LHS and RHS are related somehow. */
336 gdbpy_ref
<> tmp (self
->frame_unwinders
);
337 Py_INCREF (unwinders
);
338 self
->frame_unwinders
= unwinders
;
343 /* Get the 'type_printers' attribute. */
346 objfpy_get_type_printers (PyObject
*o
, void *ignore
)
348 objfile_object
*self
= (objfile_object
*) o
;
350 Py_INCREF (self
->type_printers
);
351 return self
->type_printers
;
354 /* Get the 'xmethods' attribute. */
357 objfpy_get_xmethods (PyObject
*o
, void *ignore
)
359 objfile_object
*self
= (objfile_object
*) o
;
361 Py_INCREF (self
->xmethods
);
362 return self
->xmethods
;
365 /* Set the 'type_printers' attribute. */
368 objfpy_set_type_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
370 objfile_object
*self
= (objfile_object
*) o
;
374 PyErr_SetString (PyExc_TypeError
,
375 _("Cannot delete the type_printers attribute."));
379 if (! PyList_Check (value
))
381 PyErr_SetString (PyExc_TypeError
,
382 _("The type_printers attribute must be a list."));
386 /* Take care in case the LHS and RHS are related somehow. */
387 gdbpy_ref
<> tmp (self
->type_printers
);
389 self
->type_printers
= value
;
394 /* Implementation of gdb.Objfile.is_valid (self) -> Boolean.
395 Returns True if this object file still exists in GDB. */
398 objfpy_is_valid (PyObject
*self
, PyObject
*args
)
400 objfile_object
*obj
= (objfile_object
*) self
;
408 /* Implementation of gdb.Objfile.add_separate_debug_file (self, string). */
411 objfpy_add_separate_debug_file (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
413 static const char *keywords
[] = { "file_name", NULL
};
414 objfile_object
*obj
= (objfile_object
*) self
;
415 const char *file_name
;
417 OBJFPY_REQUIRE_VALID (obj
);
419 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "s", keywords
, &file_name
))
424 gdb_bfd_ref_ptr
abfd (symfile_bfd_open (file_name
));
426 symbol_file_add_separate (abfd
.get (), file_name
, 0, obj
->objfile
);
428 catch (const gdb_exception
&except
)
430 GDB_PY_HANDLE_EXCEPTION (except
);
437 gdb.Objfile.lookup_global_symbol (self, string [, domain]) -> gdb.Symbol. */
440 objfpy_lookup_global_symbol (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
442 static const char *keywords
[] = { "name", "domain", NULL
};
443 objfile_object
*obj
= (objfile_object
*) self
;
444 const char *symbol_name
;
445 int domain
= VAR_DOMAIN
;
447 OBJFPY_REQUIRE_VALID (obj
);
449 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "s|i", keywords
, &symbol_name
,
455 struct symbol
*sym
= lookup_global_symbol_from_objfile
456 (obj
->objfile
, GLOBAL_BLOCK
, symbol_name
, (domain_enum
) domain
).symbol
;
460 return symbol_to_symbol_object (sym
);
462 catch (const gdb_exception
&except
)
464 GDB_PY_HANDLE_EXCEPTION (except
);
471 gdb.Objfile.lookup_static_symbol (self, string [, domain]) -> gdb.Symbol. */
474 objfpy_lookup_static_symbol (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
476 static const char *keywords
[] = { "name", "domain", NULL
};
477 objfile_object
*obj
= (objfile_object
*) self
;
478 const char *symbol_name
;
479 int domain
= VAR_DOMAIN
;
481 OBJFPY_REQUIRE_VALID (obj
);
483 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "s|i", keywords
, &symbol_name
,
489 struct symbol
*sym
= lookup_global_symbol_from_objfile
490 (obj
->objfile
, STATIC_BLOCK
, symbol_name
, (domain_enum
) domain
).symbol
;
494 return symbol_to_symbol_object (sym
);
496 catch (const gdb_exception
&except
)
498 GDB_PY_HANDLE_EXCEPTION (except
);
504 /* Implement repr() for gdb.Objfile. */
507 objfpy_repr (PyObject
*self_
)
509 objfile_object
*self
= (objfile_object
*) self_
;
510 objfile
*obj
= self
->objfile
;
513 return PyString_FromString ("<gdb.Objfile (invalid)>");
515 return PyString_FromFormat ("<gdb.Objfile filename=%s>",
516 objfile_filename (obj
));
519 /* Subroutine of gdbpy_lookup_objfile_by_build_id to simplify it.
520 Return non-zero if STRING is a potentially valid build id. */
523 objfpy_build_id_ok (const char *string
)
525 size_t i
, n
= strlen (string
);
529 for (i
= 0; i
< n
; ++i
)
531 if (!isxdigit (string
[i
]))
537 /* Subroutine of gdbpy_lookup_objfile_by_build_id to simplify it.
538 Returns non-zero if BUILD_ID matches STRING.
539 It is assumed that objfpy_build_id_ok (string) returns TRUE. */
542 objfpy_build_id_matches (const struct bfd_build_id
*build_id
,
547 if (strlen (string
) != 2 * build_id
->size
)
550 for (i
= 0; i
< build_id
->size
; ++i
)
552 char c1
= string
[i
* 2], c2
= string
[i
* 2 + 1];
553 int byte
= (host_hex_value (c1
) << 4) | host_hex_value (c2
);
555 if (byte
!= build_id
->data
[i
])
562 /* Subroutine of gdbpy_lookup_objfile to simplify it.
563 Look up an objfile by its file name. */
565 static struct objfile
*
566 objfpy_lookup_objfile_by_name (const char *name
)
568 for (objfile
*objfile
: current_program_space
->objfiles ())
570 const char *filename
;
572 if ((objfile
->flags
& OBJF_NOT_FILENAME
) != 0)
574 /* Don't return separate debug files. */
575 if (objfile
->separate_debug_objfile_backlink
!= NULL
)
578 filename
= objfile_filename (objfile
);
579 if (filename
!= NULL
&& compare_filenames_for_search (filename
, name
))
581 if (compare_filenames_for_search (objfile
->original_name
, name
))
588 /* Subroutine of gdbpy_lookup_objfile to simplify it.
589 Look up an objfile by its build id. */
591 static struct objfile
*
592 objfpy_lookup_objfile_by_build_id (const char *build_id
)
594 for (objfile
*objfile
: current_program_space
->objfiles ())
596 const struct bfd_build_id
*obfd_build_id
;
598 if (objfile
->obfd
== NULL
)
600 /* Don't return separate debug files. */
601 if (objfile
->separate_debug_objfile_backlink
!= NULL
)
603 obfd_build_id
= build_id_bfd_get (objfile
->obfd
);
604 if (obfd_build_id
== NULL
)
606 if (objfpy_build_id_matches (obfd_build_id
, build_id
))
613 /* Implementation of gdb.lookup_objfile. */
616 gdbpy_lookup_objfile (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
618 static const char *keywords
[] = { "name", "by_build_id", NULL
};
620 PyObject
*by_build_id_obj
= NULL
;
622 struct objfile
*objfile
;
624 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "s|O!", keywords
,
625 &name
, &PyBool_Type
, &by_build_id_obj
))
629 if (by_build_id_obj
!= NULL
)
631 int cmp
= PyObject_IsTrue (by_build_id_obj
);
640 if (!objfpy_build_id_ok (name
))
642 PyErr_SetString (PyExc_TypeError
, _("Not a valid build id."));
645 objfile
= objfpy_lookup_objfile_by_build_id (name
);
648 objfile
= objfpy_lookup_objfile_by_name (name
);
651 return objfile_to_objfile_object (objfile
).release ();
653 PyErr_SetString (PyExc_ValueError
, _("Objfile not found."));
659 /* Clear the OBJFILE pointer in an Objfile object and remove the
662 py_free_objfile (struct objfile
*objfile
, void *datum
)
664 gdbpy_enter
enter_py (get_objfile_arch (objfile
), current_language
);
665 gdbpy_ref
<objfile_object
> object ((objfile_object
*) datum
);
666 object
->objfile
= NULL
;
669 /* Return a new reference to the Python object of type Objfile
670 representing OBJFILE. If the object has already been created,
671 return it. Otherwise, create it. Return NULL and set the Python
675 objfile_to_objfile_object (struct objfile
*objfile
)
678 = ((PyObject
*) objfile_data (objfile
, objfpy_objfile_data_key
));
681 gdbpy_ref
<objfile_object
> object
682 ((objfile_object
*) PyObject_New (objfile_object
, &objfile_object_type
));
685 if (!objfpy_initialize (object
.get ()))
688 object
->objfile
= objfile
;
689 set_objfile_data (objfile
, objfpy_objfile_data_key
, object
.get ());
690 result
= (PyObject
*) object
.release ();
693 return gdbpy_ref
<>::new_reference (result
);
697 gdbpy_initialize_objfile (void)
699 objfpy_objfile_data_key
700 = register_objfile_data_with_cleanup (NULL
, py_free_objfile
);
702 if (PyType_Ready (&objfile_object_type
) < 0)
705 return gdb_pymodule_addobject (gdb_module
, "Objfile",
706 (PyObject
*) &objfile_object_type
);
711 static PyMethodDef objfile_object_methods
[] =
713 { "is_valid", objfpy_is_valid
, METH_NOARGS
,
714 "is_valid () -> Boolean.\n\
715 Return true if this object file is valid, false if not." },
717 { "add_separate_debug_file", (PyCFunction
) objfpy_add_separate_debug_file
,
718 METH_VARARGS
| METH_KEYWORDS
,
719 "add_separate_debug_file (file_name).\n\
720 Add FILE_NAME to the list of files containing debug info for the objfile." },
722 { "lookup_global_symbol", (PyCFunction
) objfpy_lookup_global_symbol
,
723 METH_VARARGS
| METH_KEYWORDS
,
724 "lookup_global_symbol (name [, domain]).\n\
725 Look up a global symbol in this objfile and return it." },
727 { "lookup_static_symbol", (PyCFunction
) objfpy_lookup_static_symbol
,
728 METH_VARARGS
| METH_KEYWORDS
,
729 "lookup_static_symbol (name [, domain]).\n\
730 Look up a static-linkage global symbol in this objfile and return it." },
735 static gdb_PyGetSetDef objfile_getset
[] =
737 { "__dict__", gdb_py_generic_dict
, NULL
,
738 "The __dict__ for this objfile.", &objfile_object_type
},
739 { "filename", objfpy_get_filename
, NULL
,
740 "The objfile's filename, or None.", NULL
},
741 { "username", objfpy_get_username
, NULL
,
742 "The name of the objfile as provided by the user, or None.", NULL
},
743 { "owner", objfpy_get_owner
, NULL
,
744 "The objfile owner of separate debug info objfiles, or None.",
746 { "build_id", objfpy_get_build_id
, NULL
,
747 "The objfile's build id, or None.", NULL
},
748 { "progspace", objfpy_get_progspace
, NULL
,
749 "The objfile's progspace, or None.", NULL
},
750 { "pretty_printers", objfpy_get_printers
, objfpy_set_printers
,
751 "Pretty printers.", NULL
},
752 { "frame_filters", objfpy_get_frame_filters
,
753 objfpy_set_frame_filters
, "Frame Filters.", NULL
},
754 { "frame_unwinders", objfpy_get_frame_unwinders
,
755 objfpy_set_frame_unwinders
, "Frame Unwinders", NULL
},
756 { "type_printers", objfpy_get_type_printers
, objfpy_set_type_printers
,
757 "Type printers.", NULL
},
758 { "xmethods", objfpy_get_xmethods
, NULL
,
759 "Debug methods.", NULL
},
763 PyTypeObject objfile_object_type
=
765 PyVarObject_HEAD_INIT (NULL
, 0)
766 "gdb.Objfile", /*tp_name*/
767 sizeof (objfile_object
), /*tp_basicsize*/
769 objfpy_dealloc
, /*tp_dealloc*/
774 objfpy_repr
, /*tp_repr*/
776 0, /*tp_as_sequence*/
784 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
785 "GDB objfile object", /* tp_doc */
788 0, /* tp_richcompare */
789 0, /* tp_weaklistoffset */
792 objfile_object_methods
, /* tp_methods */
794 objfile_getset
, /* tp_getset */
797 0, /* tp_descr_get */
798 0, /* tp_descr_set */
799 offsetof (objfile_object
, dict
), /* tp_dictoffset */
802 objfpy_new
, /* tp_new */