1 /* Python interface to objfiles.
3 Copyright (C) 2008-2018 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
));
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
);
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
;
119 PyObject
*result
= objfile_to_objfile_object (owner
);
127 /* An Objfile method which returns the objfile's build id, or None. */
130 objfpy_get_build_id (PyObject
*self
, void *closure
)
132 objfile_object
*obj
= (objfile_object
*) self
;
133 struct objfile
*objfile
= obj
->objfile
;
134 const struct bfd_build_id
*build_id
= NULL
;
136 OBJFPY_REQUIRE_VALID (obj
);
140 build_id
= build_id_bfd_get (objfile
->obfd
);
142 CATCH (except
, RETURN_MASK_ALL
)
144 GDB_PY_HANDLE_EXCEPTION (except
);
148 if (build_id
!= NULL
)
150 char *hex_form
= make_hex_string (build_id
->data
, build_id
->size
);
153 result
= host_string_to_python_string (hex_form
);
161 /* An Objfile method which returns the objfile's progspace, or None. */
164 objfpy_get_progspace (PyObject
*self
, void *closure
)
166 objfile_object
*obj
= (objfile_object
*) self
;
170 PyObject
*pspace
= pspace_to_pspace_object (obj
->objfile
->pspace
);
180 objfpy_dealloc (PyObject
*o
)
182 objfile_object
*self
= (objfile_object
*) o
;
184 Py_XDECREF (self
->dict
);
185 Py_XDECREF (self
->printers
);
186 Py_XDECREF (self
->frame_filters
);
187 Py_XDECREF (self
->frame_unwinders
);
188 Py_XDECREF (self
->type_printers
);
189 Py_XDECREF (self
->xmethods
);
190 Py_TYPE (self
)->tp_free (self
);
193 /* Initialize an objfile_object.
194 The result is a boolean indicating success. */
197 objfpy_initialize (objfile_object
*self
)
199 self
->objfile
= NULL
;
201 self
->dict
= PyDict_New ();
202 if (self
->dict
== NULL
)
205 self
->printers
= PyList_New (0);
206 if (self
->printers
== NULL
)
209 self
->frame_filters
= PyDict_New ();
210 if (self
->frame_filters
== NULL
)
213 self
->frame_unwinders
= PyList_New (0);
214 if (self
->frame_unwinders
== NULL
)
217 self
->type_printers
= PyList_New (0);
218 if (self
->type_printers
== NULL
)
221 self
->xmethods
= PyList_New (0);
222 if (self
->xmethods
== NULL
)
229 objfpy_new (PyTypeObject
*type
, PyObject
*args
, PyObject
*keywords
)
231 gdbpy_ref
<objfile_object
> self ((objfile_object
*) type
->tp_alloc (type
, 0));
235 if (!objfpy_initialize (self
.get ()))
239 return (PyObject
*) self
.release ();
243 objfpy_get_printers (PyObject
*o
, void *ignore
)
245 objfile_object
*self
= (objfile_object
*) o
;
247 Py_INCREF (self
->printers
);
248 return self
->printers
;
252 objfpy_set_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
255 objfile_object
*self
= (objfile_object
*) o
;
259 PyErr_SetString (PyExc_TypeError
,
260 _("Cannot delete the pretty_printers attribute."));
264 if (! PyList_Check (value
))
266 PyErr_SetString (PyExc_TypeError
,
267 _("The pretty_printers attribute must be a list."));
271 /* Take care in case the LHS and RHS are related somehow. */
272 tmp
= self
->printers
;
274 self
->printers
= value
;
280 /* Return the Python dictionary attribute containing frame filters for
283 objfpy_get_frame_filters (PyObject
*o
, void *ignore
)
285 objfile_object
*self
= (objfile_object
*) o
;
287 Py_INCREF (self
->frame_filters
);
288 return self
->frame_filters
;
291 /* Set this object file's frame filters dictionary to FILTERS. */
293 objfpy_set_frame_filters (PyObject
*o
, PyObject
*filters
, void *ignore
)
296 objfile_object
*self
= (objfile_object
*) o
;
300 PyErr_SetString (PyExc_TypeError
,
301 _("Cannot delete the frame filters attribute."));
305 if (! PyDict_Check (filters
))
307 PyErr_SetString (PyExc_TypeError
,
308 _("The frame_filters attribute must be a dictionary."));
312 /* Take care in case the LHS and RHS are related somehow. */
313 tmp
= self
->frame_filters
;
315 self
->frame_filters
= filters
;
321 /* Return the frame unwinders attribute for this object file. */
324 objfpy_get_frame_unwinders (PyObject
*o
, void *ignore
)
326 objfile_object
*self
= (objfile_object
*) o
;
328 Py_INCREF (self
->frame_unwinders
);
329 return self
->frame_unwinders
;
332 /* Set this object file's frame unwinders list to UNWINDERS. */
335 objfpy_set_frame_unwinders (PyObject
*o
, PyObject
*unwinders
, void *ignore
)
338 objfile_object
*self
= (objfile_object
*) o
;
342 PyErr_SetString (PyExc_TypeError
,
343 _("Cannot delete the frame unwinders attribute."));
347 if (!PyList_Check (unwinders
))
349 PyErr_SetString (PyExc_TypeError
,
350 _("The frame_unwinders attribute must be a list."));
354 /* Take care in case the LHS and RHS are related somehow. */
355 tmp
= self
->frame_unwinders
;
356 Py_INCREF (unwinders
);
357 self
->frame_unwinders
= unwinders
;
363 /* Get the 'type_printers' attribute. */
366 objfpy_get_type_printers (PyObject
*o
, void *ignore
)
368 objfile_object
*self
= (objfile_object
*) o
;
370 Py_INCREF (self
->type_printers
);
371 return self
->type_printers
;
374 /* Get the 'xmethods' attribute. */
377 objfpy_get_xmethods (PyObject
*o
, void *ignore
)
379 objfile_object
*self
= (objfile_object
*) o
;
381 Py_INCREF (self
->xmethods
);
382 return self
->xmethods
;
385 /* Set the 'type_printers' attribute. */
388 objfpy_set_type_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
391 objfile_object
*self
= (objfile_object
*) o
;
395 PyErr_SetString (PyExc_TypeError
,
396 _("Cannot delete the type_printers attribute."));
400 if (! PyList_Check (value
))
402 PyErr_SetString (PyExc_TypeError
,
403 _("The type_printers attribute must be a list."));
407 /* Take care in case the LHS and RHS are related somehow. */
408 tmp
= self
->type_printers
;
410 self
->type_printers
= value
;
416 /* Implementation of gdb.Objfile.is_valid (self) -> Boolean.
417 Returns True if this object file still exists in GDB. */
420 objfpy_is_valid (PyObject
*self
, PyObject
*args
)
422 objfile_object
*obj
= (objfile_object
*) self
;
430 /* Implementation of gdb.Objfile.add_separate_debug_file (self) -> Boolean. */
433 objfpy_add_separate_debug_file (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
435 static const char *keywords
[] = { "file_name", NULL
};
436 objfile_object
*obj
= (objfile_object
*) self
;
437 const char *file_name
;
439 OBJFPY_REQUIRE_VALID (obj
);
441 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "s", keywords
, &file_name
))
446 gdb_bfd_ref_ptr
abfd (symfile_bfd_open (file_name
));
448 symbol_file_add_separate (abfd
.get (), file_name
, 0, obj
->objfile
);
450 CATCH (except
, RETURN_MASK_ALL
)
452 GDB_PY_HANDLE_EXCEPTION (except
);
459 /* Implement repr() for gdb.Objfile. */
462 objfpy_repr (PyObject
*self_
)
464 objfile_object
*self
= (objfile_object
*) self_
;
465 objfile
*obj
= self
->objfile
;
468 return PyString_FromString ("<gdb.Objfile (invalid)>");
470 return PyString_FromFormat ("<gdb.Objfile filename=%s>",
471 objfile_filename (obj
));
474 /* Subroutine of gdbpy_lookup_objfile_by_build_id to simplify it.
475 Return non-zero if STRING is a potentially valid build id. */
478 objfpy_build_id_ok (const char *string
)
480 size_t i
, n
= strlen (string
);
484 for (i
= 0; i
< n
; ++i
)
486 if (!isxdigit (string
[i
]))
492 /* Subroutine of gdbpy_lookup_objfile_by_build_id to simplify it.
493 Returns non-zero if BUILD_ID matches STRING.
494 It is assumed that objfpy_build_id_ok (string) returns TRUE. */
497 objfpy_build_id_matches (const struct bfd_build_id
*build_id
,
502 if (strlen (string
) != 2 * build_id
->size
)
505 for (i
= 0; i
< build_id
->size
; ++i
)
507 char c1
= string
[i
* 2], c2
= string
[i
* 2 + 1];
508 int byte
= (host_hex_value (c1
) << 4) | host_hex_value (c2
);
510 if (byte
!= build_id
->data
[i
])
517 /* Subroutine of gdbpy_lookup_objfile to simplify it.
518 Look up an objfile by its file name. */
520 static struct objfile
*
521 objfpy_lookup_objfile_by_name (const char *name
)
523 struct objfile
*objfile
;
525 ALL_OBJFILES (objfile
)
527 const char *filename
;
529 if ((objfile
->flags
& OBJF_NOT_FILENAME
) != 0)
531 /* Don't return separate debug files. */
532 if (objfile
->separate_debug_objfile_backlink
!= NULL
)
535 filename
= objfile_filename (objfile
);
536 if (filename
!= NULL
&& compare_filenames_for_search (filename
, name
))
538 if (compare_filenames_for_search (objfile
->original_name
, name
))
545 /* Subroutine of gdbpy_lookup_objfile to simplify it.
546 Look up an objfile by its build id. */
548 static struct objfile
*
549 objfpy_lookup_objfile_by_build_id (const char *build_id
)
551 struct objfile
*objfile
;
553 ALL_OBJFILES (objfile
)
555 const struct bfd_build_id
*obfd_build_id
;
557 if (objfile
->obfd
== NULL
)
559 /* Don't return separate debug files. */
560 if (objfile
->separate_debug_objfile_backlink
!= NULL
)
562 obfd_build_id
= build_id_bfd_get (objfile
->obfd
);
563 if (obfd_build_id
== NULL
)
565 if (objfpy_build_id_matches (obfd_build_id
, build_id
))
572 /* Implementation of gdb.lookup_objfile. */
575 gdbpy_lookup_objfile (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
577 static const char *keywords
[] = { "name", "by_build_id", NULL
};
579 PyObject
*by_build_id_obj
= NULL
;
581 struct objfile
*objfile
;
583 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "s|O!", keywords
,
584 &name
, &PyBool_Type
, &by_build_id_obj
))
588 if (by_build_id_obj
!= NULL
)
590 int cmp
= PyObject_IsTrue (by_build_id_obj
);
599 if (!objfpy_build_id_ok (name
))
601 PyErr_SetString (PyExc_TypeError
, _("Not a valid build id."));
604 objfile
= objfpy_lookup_objfile_by_build_id (name
);
607 objfile
= objfpy_lookup_objfile_by_name (name
);
611 PyObject
*result
= objfile_to_objfile_object (objfile
);
617 PyErr_SetString (PyExc_ValueError
, _("Objfile not found."));
623 /* Clear the OBJFILE pointer in an Objfile object and remove the
626 py_free_objfile (struct objfile
*objfile
, void *datum
)
628 gdbpy_enter
enter_py (get_objfile_arch (objfile
), current_language
);
629 gdbpy_ref
<objfile_object
> object ((objfile_object
*) datum
);
630 object
->objfile
= NULL
;
633 /* Return a borrowed reference to the Python object of type Objfile
634 representing OBJFILE. If the object has already been created,
635 return it. Otherwise, create it. Return NULL and set the Python
639 objfile_to_objfile_object (struct objfile
*objfile
)
641 gdbpy_ref
<objfile_object
> object
642 ((objfile_object
*) objfile_data (objfile
, objfpy_objfile_data_key
));
645 object
.reset (PyObject_New (objfile_object
, &objfile_object_type
));
648 if (!objfpy_initialize (object
.get ()))
651 object
->objfile
= objfile
;
652 set_objfile_data (objfile
, objfpy_objfile_data_key
, object
.get ());
656 return (PyObject
*) object
.release ();
660 gdbpy_initialize_objfile (void)
662 objfpy_objfile_data_key
663 = register_objfile_data_with_cleanup (NULL
, py_free_objfile
);
665 if (PyType_Ready (&objfile_object_type
) < 0)
668 return gdb_pymodule_addobject (gdb_module
, "Objfile",
669 (PyObject
*) &objfile_object_type
);
674 static PyMethodDef objfile_object_methods
[] =
676 { "is_valid", objfpy_is_valid
, METH_NOARGS
,
677 "is_valid () -> Boolean.\n\
678 Return true if this object file is valid, false if not." },
680 { "add_separate_debug_file", (PyCFunction
) objfpy_add_separate_debug_file
,
681 METH_VARARGS
| METH_KEYWORDS
,
682 "add_separate_debug_file (file_name).\n\
683 Add FILE_NAME to the list of files containing debug info for the objfile." },
688 static gdb_PyGetSetDef objfile_getset
[] =
690 { "__dict__", gdb_py_generic_dict
, NULL
,
691 "The __dict__ for this objfile.", &objfile_object_type
},
692 { "filename", objfpy_get_filename
, NULL
,
693 "The objfile's filename, or None.", NULL
},
694 { "username", objfpy_get_username
, NULL
,
695 "The name of the objfile as provided by the user, or None.", NULL
},
696 { "owner", objfpy_get_owner
, NULL
,
697 "The objfile owner of separate debug info objfiles, or None.",
699 { "build_id", objfpy_get_build_id
, NULL
,
700 "The objfile's build id, or None.", NULL
},
701 { "progspace", objfpy_get_progspace
, NULL
,
702 "The objfile's progspace, or None.", NULL
},
703 { "pretty_printers", objfpy_get_printers
, objfpy_set_printers
,
704 "Pretty printers.", NULL
},
705 { "frame_filters", objfpy_get_frame_filters
,
706 objfpy_set_frame_filters
, "Frame Filters.", NULL
},
707 { "frame_unwinders", objfpy_get_frame_unwinders
,
708 objfpy_set_frame_unwinders
, "Frame Unwinders", NULL
},
709 { "type_printers", objfpy_get_type_printers
, objfpy_set_type_printers
,
710 "Type printers.", NULL
},
711 { "xmethods", objfpy_get_xmethods
, NULL
,
712 "Debug methods.", NULL
},
716 PyTypeObject objfile_object_type
=
718 PyVarObject_HEAD_INIT (NULL
, 0)
719 "gdb.Objfile", /*tp_name*/
720 sizeof (objfile_object
), /*tp_basicsize*/
722 objfpy_dealloc
, /*tp_dealloc*/
727 objfpy_repr
, /*tp_repr*/
729 0, /*tp_as_sequence*/
737 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
738 "GDB objfile object", /* tp_doc */
741 0, /* tp_richcompare */
742 0, /* tp_weaklistoffset */
745 objfile_object_methods
, /* tp_methods */
747 objfile_getset
, /* tp_getset */
750 0, /* tp_descr_get */
751 0, /* tp_descr_set */
752 offsetof (objfile_object
, dict
), /* tp_dictoffset */
755 objfpy_new
, /* tp_new */