1 /* Python interface to objfiles.
3 Copyright (C) 2008-2015 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
;
45 /* The type-printer list. */
46 PyObject
*type_printers
;
48 /* The debug method matcher list. */
52 extern PyTypeObject objfile_object_type
53 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("objfile_object");
55 static const struct objfile_data
*objfpy_objfile_data_key
;
57 /* Require that OBJF be a valid objfile. */
58 #define OBJFPY_REQUIRE_VALID(obj) \
60 if (!(obj)->objfile) \
62 PyErr_SetString (PyExc_RuntimeError, \
63 _("Objfile no longer exists.")); \
70 /* An Objfile method which returns the objfile's file name, or None. */
73 objfpy_get_filename (PyObject
*self
, void *closure
)
75 objfile_object
*obj
= (objfile_object
*) self
;
78 return PyString_Decode (objfile_name (obj
->objfile
),
79 strlen (objfile_name (obj
->objfile
)),
80 host_charset (), NULL
);
84 /* An Objfile method which returns the objfile's file name, as specified
85 by the user, or None. */
88 objfpy_get_username (PyObject
*self
, void *closure
)
90 objfile_object
*obj
= (objfile_object
*) self
;
94 const char *username
= obj
->objfile
->original_name
;
96 return PyString_Decode (username
, strlen (username
),
97 host_charset (), NULL
);
103 /* If SELF is a separate debug-info file, return the "backlink" field.
104 Otherwise return None. */
107 objfpy_get_owner (PyObject
*self
, void *closure
)
109 objfile_object
*obj
= (objfile_object
*) self
;
110 struct objfile
*objfile
= obj
->objfile
;
111 struct objfile
*owner
;
113 OBJFPY_REQUIRE_VALID (obj
);
115 owner
= objfile
->separate_debug_objfile_backlink
;
118 PyObject
*result
= objfile_to_objfile_object (owner
);
126 /* An Objfile method which returns the objfile's build id, or None. */
129 objfpy_get_build_id (PyObject
*self
, void *closure
)
131 objfile_object
*obj
= (objfile_object
*) self
;
132 struct objfile
*objfile
= obj
->objfile
;
133 const struct elf_build_id
*build_id
= NULL
;
134 volatile struct gdb_exception except
;
136 OBJFPY_REQUIRE_VALID (obj
);
138 TRY_CATCH (except
, RETURN_MASK_ALL
)
140 build_id
= build_id_bfd_get (objfile
->obfd
);
142 GDB_PY_HANDLE_EXCEPTION (except
);
144 if (build_id
!= NULL
)
146 char *hex_form
= make_hex_string (build_id
->data
, build_id
->size
);
149 result
= PyString_Decode (hex_form
, strlen (hex_form
),
150 host_charset (), NULL
);
158 /* An Objfile method which returns the objfile's progspace, or None. */
161 objfpy_get_progspace (PyObject
*self
, void *closure
)
163 objfile_object
*obj
= (objfile_object
*) self
;
167 PyObject
*pspace
= pspace_to_pspace_object (obj
->objfile
->pspace
);
177 objfpy_dealloc (PyObject
*o
)
179 objfile_object
*self
= (objfile_object
*) o
;
181 Py_XDECREF (self
->dict
);
182 Py_XDECREF (self
->printers
);
183 Py_XDECREF (self
->frame_filters
);
184 Py_XDECREF (self
->type_printers
);
185 Py_XDECREF (self
->xmethods
);
186 Py_TYPE (self
)->tp_free (self
);
189 /* Initialize an objfile_object.
190 The result is a boolean indicating success. */
193 objfpy_initialize (objfile_object
*self
)
195 self
->objfile
= NULL
;
198 self
->printers
= PyList_New (0);
199 if (self
->printers
== NULL
)
202 self
->frame_filters
= PyDict_New ();
203 if (self
->frame_filters
== 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 objfile_object
*self
= (objfile_object
*) type
->tp_alloc (type
, 0);
224 if (!objfpy_initialize (self
))
231 return (PyObject
*) self
;
235 objfpy_get_printers (PyObject
*o
, void *ignore
)
237 objfile_object
*self
= (objfile_object
*) o
;
239 Py_INCREF (self
->printers
);
240 return self
->printers
;
244 objfpy_set_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
247 objfile_object
*self
= (objfile_object
*) o
;
251 PyErr_SetString (PyExc_TypeError
,
252 _("Cannot delete the pretty_printers attribute."));
256 if (! PyList_Check (value
))
258 PyErr_SetString (PyExc_TypeError
,
259 _("The pretty_printers attribute must be a list."));
263 /* Take care in case the LHS and RHS are related somehow. */
264 tmp
= self
->printers
;
266 self
->printers
= value
;
272 /* Return the Python dictionary attribute containing frame filters for
275 objfpy_get_frame_filters (PyObject
*o
, void *ignore
)
277 objfile_object
*self
= (objfile_object
*) o
;
279 Py_INCREF (self
->frame_filters
);
280 return self
->frame_filters
;
283 /* Set this object file's frame filters dictionary to FILTERS. */
285 objfpy_set_frame_filters (PyObject
*o
, PyObject
*filters
, void *ignore
)
288 objfile_object
*self
= (objfile_object
*) o
;
292 PyErr_SetString (PyExc_TypeError
,
293 _("Cannot delete the frame filters attribute."));
297 if (! PyDict_Check (filters
))
299 PyErr_SetString (PyExc_TypeError
,
300 _("The frame_filters attribute must be a dictionary."));
304 /* Take care in case the LHS and RHS are related somehow. */
305 tmp
= self
->frame_filters
;
307 self
->frame_filters
= filters
;
313 /* Get the 'type_printers' attribute. */
316 objfpy_get_type_printers (PyObject
*o
, void *ignore
)
318 objfile_object
*self
= (objfile_object
*) o
;
320 Py_INCREF (self
->type_printers
);
321 return self
->type_printers
;
324 /* Get the 'xmethods' attribute. */
327 objfpy_get_xmethods (PyObject
*o
, void *ignore
)
329 objfile_object
*self
= (objfile_object
*) o
;
331 Py_INCREF (self
->xmethods
);
332 return self
->xmethods
;
335 /* Set the 'type_printers' attribute. */
338 objfpy_set_type_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
341 objfile_object
*self
= (objfile_object
*) o
;
345 PyErr_SetString (PyExc_TypeError
,
346 _("Cannot delete the type_printers attribute."));
350 if (! PyList_Check (value
))
352 PyErr_SetString (PyExc_TypeError
,
353 _("The type_printers attribute must be a list."));
357 /* Take care in case the LHS and RHS are related somehow. */
358 tmp
= self
->type_printers
;
360 self
->type_printers
= value
;
366 /* Implementation of gdb.Objfile.is_valid (self) -> Boolean.
367 Returns True if this object file still exists in GDB. */
370 objfpy_is_valid (PyObject
*self
, PyObject
*args
)
372 objfile_object
*obj
= (objfile_object
*) self
;
380 /* Implementation of gdb.Objfile.add_separate_debug_file (self) -> Boolean. */
383 objfpy_add_separate_debug_file (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
385 static char *keywords
[] = { "file_name", NULL
};
386 objfile_object
*obj
= (objfile_object
*) self
;
387 const char *file_name
;
388 int symfile_flags
= 0;
389 volatile struct gdb_exception except
;
391 OBJFPY_REQUIRE_VALID (obj
);
393 if (!PyArg_ParseTupleAndKeywords (args
, kw
, "s", keywords
, &file_name
))
396 TRY_CATCH (except
, RETURN_MASK_ALL
)
398 bfd
*abfd
= symfile_bfd_open (file_name
);
400 symbol_file_add_separate (abfd
, file_name
, symfile_flags
, obj
->objfile
);
402 GDB_PY_HANDLE_EXCEPTION (except
);
407 /* Subroutine of gdbpy_lookup_objfile_by_build_id to simplify it.
408 Return non-zero if STRING is a potentially valid build id. */
411 objfpy_build_id_ok (const char *string
)
413 size_t i
, n
= strlen (string
);
417 for (i
= 0; i
< n
; ++i
)
419 if (!isxdigit (string
[i
]))
425 /* Subroutine of gdbpy_lookup_objfile_by_build_id to simplify it.
426 Returns non-zero if BUILD_ID matches STRING.
427 It is assumed that objfpy_build_id_ok (string) returns TRUE. */
430 objfpy_build_id_matches (const struct elf_build_id
*build_id
,
435 if (strlen (string
) != 2 * build_id
->size
)
438 for (i
= 0; i
< build_id
->size
; ++i
)
440 char c1
= string
[i
* 2], c2
= string
[i
* 2 + 1];
441 int byte
= (host_hex_value (c1
) << 4) | host_hex_value (c2
);
443 if (byte
!= build_id
->data
[i
])
450 /* Subroutine of gdbpy_lookup_objfile to simplify it.
451 Look up an objfile by its file name. */
453 static struct objfile
*
454 objfpy_lookup_objfile_by_name (const char *name
)
456 struct objfile
*objfile
;
458 ALL_OBJFILES (objfile
)
460 const char *filename
;
462 if ((objfile
->flags
& OBJF_NOT_FILENAME
) != 0)
464 /* Don't return separate debug files. */
465 if (objfile
->separate_debug_objfile_backlink
!= NULL
)
468 filename
= objfile_filename (objfile
);
469 if (filename
!= NULL
&& compare_filenames_for_search (filename
, name
))
471 if (compare_filenames_for_search (objfile
->original_name
, name
))
478 /* Subroutine of gdbpy_lookup_objfile to simplify it.
479 Look up an objfile by its build id. */
481 static struct objfile
*
482 objfpy_lookup_objfile_by_build_id (const char *build_id
)
484 struct objfile
*objfile
;
486 ALL_OBJFILES (objfile
)
488 const struct elf_build_id
*obfd_build_id
;
490 if (objfile
->obfd
== NULL
)
492 /* Don't return separate debug files. */
493 if (objfile
->separate_debug_objfile_backlink
!= NULL
)
495 obfd_build_id
= build_id_bfd_get (objfile
->obfd
);
496 if (obfd_build_id
== NULL
)
498 if (objfpy_build_id_matches (obfd_build_id
, build_id
))
505 /* Implementation of gdb.lookup_objfile. */
508 gdbpy_lookup_objfile (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
510 static char *keywords
[] = { "name", "by_build_id", NULL
};
512 PyObject
*by_build_id_obj
= NULL
;
514 struct objfile
*objfile
;
516 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "s|O!", keywords
,
517 &name
, &PyBool_Type
, &by_build_id_obj
))
521 if (by_build_id_obj
!= NULL
)
523 int cmp
= PyObject_IsTrue (by_build_id_obj
);
532 if (!objfpy_build_id_ok (name
))
534 PyErr_SetString (PyExc_TypeError
, _("Not a valid build id."));
537 objfile
= objfpy_lookup_objfile_by_build_id (name
);
540 objfile
= objfpy_lookup_objfile_by_name (name
);
544 PyObject
*result
= objfile_to_objfile_object (objfile
);
550 PyErr_SetString (PyExc_ValueError
, _("Objfile not found."));
556 /* Clear the OBJFILE pointer in an Objfile object and remove the
559 py_free_objfile (struct objfile
*objfile
, void *datum
)
561 struct cleanup
*cleanup
;
562 objfile_object
*object
= datum
;
564 cleanup
= ensure_python_env (get_objfile_arch (objfile
), current_language
);
565 object
->objfile
= NULL
;
566 Py_DECREF ((PyObject
*) object
);
567 do_cleanups (cleanup
);
570 /* Return a borrowed reference to the Python object of type Objfile
571 representing OBJFILE. If the object has already been created,
572 return it. Otherwise, create it. Return NULL and set the Python
576 objfile_to_objfile_object (struct objfile
*objfile
)
578 objfile_object
*object
;
580 object
= objfile_data (objfile
, objfpy_objfile_data_key
);
583 object
= PyObject_New (objfile_object
, &objfile_object_type
);
586 if (!objfpy_initialize (object
))
592 object
->objfile
= objfile
;
593 set_objfile_data (objfile
, objfpy_objfile_data_key
, object
);
597 return (PyObject
*) object
;
601 gdbpy_initialize_objfile (void)
603 objfpy_objfile_data_key
604 = register_objfile_data_with_cleanup (NULL
, py_free_objfile
);
606 if (PyType_Ready (&objfile_object_type
) < 0)
609 return gdb_pymodule_addobject (gdb_module
, "Objfile",
610 (PyObject
*) &objfile_object_type
);
615 static PyMethodDef objfile_object_methods
[] =
617 { "is_valid", objfpy_is_valid
, METH_NOARGS
,
618 "is_valid () -> Boolean.\n\
619 Return true if this object file is valid, false if not." },
621 { "add_separate_debug_file", (PyCFunction
) objfpy_add_separate_debug_file
,
622 METH_VARARGS
| METH_KEYWORDS
,
623 "add_separate_debug_file (file_name).\n\
624 Add FILE_NAME to the list of files containing debug info for the objfile." },
629 static PyGetSetDef objfile_getset
[] =
631 { "__dict__", gdb_py_generic_dict
, NULL
,
632 "The __dict__ for this objfile.", &objfile_object_type
},
633 { "filename", objfpy_get_filename
, NULL
,
634 "The objfile's filename, or None.", NULL
},
635 { "username", objfpy_get_username
, NULL
,
636 "The name of the objfile as provided by the user, or None.", NULL
},
637 { "owner", objfpy_get_owner
, NULL
,
638 "The objfile owner of separate debug info objfiles, or None.",
640 { "build_id", objfpy_get_build_id
, NULL
,
641 "The objfile's build id, or None.", NULL
},
642 { "progspace", objfpy_get_progspace
, NULL
,
643 "The objfile's progspace, or None.", NULL
},
644 { "pretty_printers", objfpy_get_printers
, objfpy_set_printers
,
645 "Pretty printers.", NULL
},
646 { "frame_filters", objfpy_get_frame_filters
,
647 objfpy_set_frame_filters
, "Frame Filters.", NULL
},
648 { "type_printers", objfpy_get_type_printers
, objfpy_set_type_printers
,
649 "Type printers.", NULL
},
650 { "xmethods", objfpy_get_xmethods
, NULL
,
651 "Debug methods.", NULL
},
655 PyTypeObject objfile_object_type
=
657 PyVarObject_HEAD_INIT (NULL
, 0)
658 "gdb.Objfile", /*tp_name*/
659 sizeof (objfile_object
), /*tp_basicsize*/
661 objfpy_dealloc
, /*tp_dealloc*/
668 0, /*tp_as_sequence*/
676 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
677 "GDB objfile object", /* tp_doc */
680 0, /* tp_richcompare */
681 0, /* tp_weaklistoffset */
684 objfile_object_methods
, /* tp_methods */
686 objfile_getset
, /* tp_getset */
689 0, /* tp_descr_get */
690 0, /* tp_descr_set */
691 offsetof (objfile_object
, dict
), /* tp_dictoffset */
694 objfpy_new
, /* tp_new */