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 (except
, RETURN_MASK_ALL
)
139 GDB_PY_HANDLE_EXCEPTION (except
);
143 if (build_id
!= NULL
)
145 gdb::unique_xmalloc_ptr
<char> hex_form
146 (make_hex_string (build_id
->data
, build_id
->size
));
148 return host_string_to_python_string (hex_form
.get ()).release ();
154 /* An Objfile method which returns the objfile's progspace, or None. */
157 objfpy_get_progspace (PyObject
*self
, void *closure
)
159 objfile_object
*obj
= (objfile_object
*) self
;
162 return pspace_to_pspace_object (obj
->objfile
->pspace
).release ();
168 objfpy_dealloc (PyObject
*o
)
170 objfile_object
*self
= (objfile_object
*) o
;
172 Py_XDECREF (self
->dict
);
173 Py_XDECREF (self
->printers
);
174 Py_XDECREF (self
->frame_filters
);
175 Py_XDECREF (self
->frame_unwinders
);
176 Py_XDECREF (self
->type_printers
);
177 Py_XDECREF (self
->xmethods
);
178 Py_TYPE (self
)->tp_free (self
);
181 /* Initialize an objfile_object.
182 The result is a boolean indicating success. */
185 objfpy_initialize (objfile_object
*self
)
187 self
->objfile
= NULL
;
189 self
->dict
= PyDict_New ();
190 if (self
->dict
== NULL
)
193 self
->printers
= PyList_New (0);
194 if (self
->printers
== NULL
)
197 self
->frame_filters
= PyDict_New ();
198 if (self
->frame_filters
== NULL
)
201 self
->frame_unwinders
= PyList_New (0);
202 if (self
->frame_unwinders
== NULL
)
205 self
->type_printers
= PyList_New (0);
206 if (self
->type_printers
== NULL
)
209 self
->xmethods
= PyList_New (0);
210 if (self
->xmethods
== NULL
)
217 objfpy_new (PyTypeObject
*type
, PyObject
*args
, PyObject
*keywords
)
219 gdbpy_ref
<objfile_object
> self ((objfile_object
*) type
->tp_alloc (type
, 0));
223 if (!objfpy_initialize (self
.get ()))
227 return (PyObject
*) self
.release ();
231 objfpy_get_printers (PyObject
*o
, void *ignore
)
233 objfile_object
*self
= (objfile_object
*) o
;
235 Py_INCREF (self
->printers
);
236 return self
->printers
;
240 objfpy_set_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
242 objfile_object
*self
= (objfile_object
*) o
;
246 PyErr_SetString (PyExc_TypeError
,
247 _("Cannot delete the pretty_printers attribute."));
251 if (! PyList_Check (value
))
253 PyErr_SetString (PyExc_TypeError
,
254 _("The pretty_printers attribute must be a list."));
258 /* Take care in case the LHS and RHS are related somehow. */
259 gdbpy_ref
<> tmp (self
->printers
);
261 self
->printers
= value
;
266 /* Return the Python dictionary attribute containing frame filters for
269 objfpy_get_frame_filters (PyObject
*o
, void *ignore
)
271 objfile_object
*self
= (objfile_object
*) o
;
273 Py_INCREF (self
->frame_filters
);
274 return self
->frame_filters
;
277 /* Set this object file's frame filters dictionary to FILTERS. */
279 objfpy_set_frame_filters (PyObject
*o
, PyObject
*filters
, void *ignore
)
281 objfile_object
*self
= (objfile_object
*) o
;
285 PyErr_SetString (PyExc_TypeError
,
286 _("Cannot delete the frame filters attribute."));
290 if (! PyDict_Check (filters
))
292 PyErr_SetString (PyExc_TypeError
,
293 _("The frame_filters attribute must be a dictionary."));
297 /* Take care in case the LHS and RHS are related somehow. */
298 gdbpy_ref
<> tmp (self
->frame_filters
);
300 self
->frame_filters
= filters
;
305 /* Return the frame unwinders attribute for this object file. */
308 objfpy_get_frame_unwinders (PyObject
*o
, void *ignore
)
310 objfile_object
*self
= (objfile_object
*) o
;
312 Py_INCREF (self
->frame_unwinders
);
313 return self
->frame_unwinders
;
316 /* Set this object file's frame unwinders list to UNWINDERS. */
319 objfpy_set_frame_unwinders (PyObject
*o
, PyObject
*unwinders
, void *ignore
)
321 objfile_object
*self
= (objfile_object
*) o
;
325 PyErr_SetString (PyExc_TypeError
,
326 _("Cannot delete the frame unwinders attribute."));
330 if (!PyList_Check (unwinders
))
332 PyErr_SetString (PyExc_TypeError
,
333 _("The frame_unwinders attribute must be a list."));
337 /* Take care in case the LHS and RHS are related somehow. */
338 gdbpy_ref
<> tmp (self
->frame_unwinders
);
339 Py_INCREF (unwinders
);
340 self
->frame_unwinders
= unwinders
;
345 /* Get the 'type_printers' attribute. */
348 objfpy_get_type_printers (PyObject
*o
, void *ignore
)
350 objfile_object
*self
= (objfile_object
*) o
;
352 Py_INCREF (self
->type_printers
);
353 return self
->type_printers
;
356 /* Get the 'xmethods' attribute. */
359 objfpy_get_xmethods (PyObject
*o
, void *ignore
)
361 objfile_object
*self
= (objfile_object
*) o
;
363 Py_INCREF (self
->xmethods
);
364 return self
->xmethods
;
367 /* Set the 'type_printers' attribute. */
370 objfpy_set_type_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
372 objfile_object
*self
= (objfile_object
*) o
;
376 PyErr_SetString (PyExc_TypeError
,
377 _("Cannot delete the type_printers attribute."));
381 if (! PyList_Check (value
))
383 PyErr_SetString (PyExc_TypeError
,
384 _("The type_printers attribute must be a list."));
388 /* Take care in case the LHS and RHS are related somehow. */
389 gdbpy_ref
<> tmp (self
->type_printers
);
391 self
->type_printers
= value
;
396 /* Implementation of gdb.Objfile.is_valid (self) -> Boolean.
397 Returns True if this object file still exists in GDB. */
400 objfpy_is_valid (PyObject
*self
, PyObject
*args
)
402 objfile_object
*obj
= (objfile_object
*) self
;
410 /* Implementation of gdb.Objfile.add_separate_debug_file (self) -> Boolean. */
413 objfpy_add_separate_debug_file (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
415 static const char *keywords
[] = { "file_name", NULL
};
416 objfile_object
*obj
= (objfile_object
*) self
;
417 const char *file_name
;
419 OBJFPY_REQUIRE_VALID (obj
);
421 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "s", keywords
, &file_name
))
426 gdb_bfd_ref_ptr
abfd (symfile_bfd_open (file_name
));
428 symbol_file_add_separate (abfd
.get (), file_name
, 0, obj
->objfile
);
430 CATCH (except
, RETURN_MASK_ALL
)
432 GDB_PY_HANDLE_EXCEPTION (except
);
439 /* Implement repr() for gdb.Objfile. */
442 objfpy_repr (PyObject
*self_
)
444 objfile_object
*self
= (objfile_object
*) self_
;
445 objfile
*obj
= self
->objfile
;
448 return PyString_FromString ("<gdb.Objfile (invalid)>");
450 return PyString_FromFormat ("<gdb.Objfile filename=%s>",
451 objfile_filename (obj
));
454 /* Subroutine of gdbpy_lookup_objfile_by_build_id to simplify it.
455 Return non-zero if STRING is a potentially valid build id. */
458 objfpy_build_id_ok (const char *string
)
460 size_t i
, n
= strlen (string
);
464 for (i
= 0; i
< n
; ++i
)
466 if (!isxdigit (string
[i
]))
472 /* Subroutine of gdbpy_lookup_objfile_by_build_id to simplify it.
473 Returns non-zero if BUILD_ID matches STRING.
474 It is assumed that objfpy_build_id_ok (string) returns TRUE. */
477 objfpy_build_id_matches (const struct bfd_build_id
*build_id
,
482 if (strlen (string
) != 2 * build_id
->size
)
485 for (i
= 0; i
< build_id
->size
; ++i
)
487 char c1
= string
[i
* 2], c2
= string
[i
* 2 + 1];
488 int byte
= (host_hex_value (c1
) << 4) | host_hex_value (c2
);
490 if (byte
!= build_id
->data
[i
])
497 /* Subroutine of gdbpy_lookup_objfile to simplify it.
498 Look up an objfile by its file name. */
500 static struct objfile
*
501 objfpy_lookup_objfile_by_name (const char *name
)
503 for (objfile
*objfile
: current_program_space
->objfiles ())
505 const char *filename
;
507 if ((objfile
->flags
& OBJF_NOT_FILENAME
) != 0)
509 /* Don't return separate debug files. */
510 if (objfile
->separate_debug_objfile_backlink
!= NULL
)
513 filename
= objfile_filename (objfile
);
514 if (filename
!= NULL
&& compare_filenames_for_search (filename
, name
))
516 if (compare_filenames_for_search (objfile
->original_name
, name
))
523 /* Subroutine of gdbpy_lookup_objfile to simplify it.
524 Look up an objfile by its build id. */
526 static struct objfile
*
527 objfpy_lookup_objfile_by_build_id (const char *build_id
)
529 for (objfile
*objfile
: current_program_space
->objfiles ())
531 const struct bfd_build_id
*obfd_build_id
;
533 if (objfile
->obfd
== NULL
)
535 /* Don't return separate debug files. */
536 if (objfile
->separate_debug_objfile_backlink
!= NULL
)
538 obfd_build_id
= build_id_bfd_get (objfile
->obfd
);
539 if (obfd_build_id
== NULL
)
541 if (objfpy_build_id_matches (obfd_build_id
, build_id
))
548 /* Implementation of gdb.lookup_objfile. */
551 gdbpy_lookup_objfile (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
553 static const char *keywords
[] = { "name", "by_build_id", NULL
};
555 PyObject
*by_build_id_obj
= NULL
;
557 struct objfile
*objfile
;
559 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "s|O!", keywords
,
560 &name
, &PyBool_Type
, &by_build_id_obj
))
564 if (by_build_id_obj
!= NULL
)
566 int cmp
= PyObject_IsTrue (by_build_id_obj
);
575 if (!objfpy_build_id_ok (name
))
577 PyErr_SetString (PyExc_TypeError
, _("Not a valid build id."));
580 objfile
= objfpy_lookup_objfile_by_build_id (name
);
583 objfile
= objfpy_lookup_objfile_by_name (name
);
586 return objfile_to_objfile_object (objfile
).release ();
588 PyErr_SetString (PyExc_ValueError
, _("Objfile not found."));
594 /* Clear the OBJFILE pointer in an Objfile object and remove the
597 py_free_objfile (struct objfile
*objfile
, void *datum
)
599 gdbpy_enter
enter_py (get_objfile_arch (objfile
), current_language
);
600 gdbpy_ref
<objfile_object
> object ((objfile_object
*) datum
);
601 object
->objfile
= NULL
;
604 /* Return a new reference to the Python object of type Objfile
605 representing OBJFILE. If the object has already been created,
606 return it. Otherwise, create it. Return NULL and set the Python
610 objfile_to_objfile_object (struct objfile
*objfile
)
613 = ((PyObject
*) objfile_data (objfile
, objfpy_objfile_data_key
));
616 gdbpy_ref
<objfile_object
> object
617 ((objfile_object
*) PyObject_New (objfile_object
, &objfile_object_type
));
620 if (!objfpy_initialize (object
.get ()))
623 object
->objfile
= objfile
;
624 set_objfile_data (objfile
, objfpy_objfile_data_key
, object
.get ());
625 result
= (PyObject
*) object
.release ();
628 return gdbpy_ref
<>::new_reference (result
);
632 gdbpy_initialize_objfile (void)
634 objfpy_objfile_data_key
635 = register_objfile_data_with_cleanup (NULL
, py_free_objfile
);
637 if (PyType_Ready (&objfile_object_type
) < 0)
640 return gdb_pymodule_addobject (gdb_module
, "Objfile",
641 (PyObject
*) &objfile_object_type
);
646 static PyMethodDef objfile_object_methods
[] =
648 { "is_valid", objfpy_is_valid
, METH_NOARGS
,
649 "is_valid () -> Boolean.\n\
650 Return true if this object file is valid, false if not." },
652 { "add_separate_debug_file", (PyCFunction
) objfpy_add_separate_debug_file
,
653 METH_VARARGS
| METH_KEYWORDS
,
654 "add_separate_debug_file (file_name).\n\
655 Add FILE_NAME to the list of files containing debug info for the objfile." },
660 static gdb_PyGetSetDef objfile_getset
[] =
662 { "__dict__", gdb_py_generic_dict
, NULL
,
663 "The __dict__ for this objfile.", &objfile_object_type
},
664 { "filename", objfpy_get_filename
, NULL
,
665 "The objfile's filename, or None.", NULL
},
666 { "username", objfpy_get_username
, NULL
,
667 "The name of the objfile as provided by the user, or None.", NULL
},
668 { "owner", objfpy_get_owner
, NULL
,
669 "The objfile owner of separate debug info objfiles, or None.",
671 { "build_id", objfpy_get_build_id
, NULL
,
672 "The objfile's build id, or None.", NULL
},
673 { "progspace", objfpy_get_progspace
, NULL
,
674 "The objfile's progspace, or None.", NULL
},
675 { "pretty_printers", objfpy_get_printers
, objfpy_set_printers
,
676 "Pretty printers.", NULL
},
677 { "frame_filters", objfpy_get_frame_filters
,
678 objfpy_set_frame_filters
, "Frame Filters.", NULL
},
679 { "frame_unwinders", objfpy_get_frame_unwinders
,
680 objfpy_set_frame_unwinders
, "Frame Unwinders", NULL
},
681 { "type_printers", objfpy_get_type_printers
, objfpy_set_type_printers
,
682 "Type printers.", NULL
},
683 { "xmethods", objfpy_get_xmethods
, NULL
,
684 "Debug methods.", NULL
},
688 PyTypeObject objfile_object_type
=
690 PyVarObject_HEAD_INIT (NULL
, 0)
691 "gdb.Objfile", /*tp_name*/
692 sizeof (objfile_object
), /*tp_basicsize*/
694 objfpy_dealloc
, /*tp_dealloc*/
699 objfpy_repr
, /*tp_repr*/
701 0, /*tp_as_sequence*/
709 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
710 "GDB objfile object", /* tp_doc */
713 0, /* tp_richcompare */
714 0, /* tp_weaklistoffset */
717 objfile_object_methods
, /* tp_methods */
719 objfile_getset
, /* tp_getset */
722 0, /* tp_descr_get */
723 0, /* tp_descr_set */
724 offsetof (objfile_object
, dict
), /* tp_dictoffset */
727 objfpy_new
, /* tp_new */