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 static 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 /* If SELF is a separate debug-info file, return the "backlink" field.
85 Otherwise return None. */
88 objfpy_get_owner (PyObject
*self
, void *closure
)
90 objfile_object
*obj
= (objfile_object
*) self
;
91 struct objfile
*objfile
= obj
->objfile
;
92 struct objfile
*owner
;
94 OBJFPY_REQUIRE_VALID (obj
);
96 owner
= objfile
->separate_debug_objfile_backlink
;
99 PyObject
*result
= objfile_to_objfile_object (owner
);
107 /* An Objfile method which returns the objfile's build id, or None. */
110 objfpy_get_build_id (PyObject
*self
, void *closure
)
112 objfile_object
*obj
= (objfile_object
*) self
;
113 struct objfile
*objfile
= obj
->objfile
;
114 const struct elf_build_id
*build_id
= NULL
;
115 volatile struct gdb_exception except
;
117 OBJFPY_REQUIRE_VALID (obj
);
119 TRY_CATCH (except
, RETURN_MASK_ALL
)
121 build_id
= build_id_bfd_get (objfile
->obfd
);
123 GDB_PY_HANDLE_EXCEPTION (except
);
125 if (build_id
!= NULL
)
127 char *hex_form
= make_hex_string (build_id
->data
, build_id
->size
);
130 result
= PyString_Decode (hex_form
, strlen (hex_form
),
131 host_charset (), NULL
);
139 /* An Objfile method which returns the objfile's progspace, or None. */
142 objfpy_get_progspace (PyObject
*self
, void *closure
)
144 objfile_object
*obj
= (objfile_object
*) self
;
148 PyObject
*pspace
= pspace_to_pspace_object (obj
->objfile
->pspace
);
158 objfpy_dealloc (PyObject
*o
)
160 objfile_object
*self
= (objfile_object
*) o
;
162 Py_XDECREF (self
->dict
);
163 Py_XDECREF (self
->printers
);
164 Py_XDECREF (self
->frame_filters
);
165 Py_XDECREF (self
->type_printers
);
166 Py_XDECREF (self
->xmethods
);
167 Py_TYPE (self
)->tp_free (self
);
170 /* Initialize an objfile_object.
171 The result is a boolean indicating success. */
174 objfpy_initialize (objfile_object
*self
)
176 self
->objfile
= NULL
;
179 self
->printers
= PyList_New (0);
180 if (self
->printers
== NULL
)
183 self
->frame_filters
= PyDict_New ();
184 if (self
->frame_filters
== NULL
)
187 self
->type_printers
= PyList_New (0);
188 if (self
->type_printers
== NULL
)
191 self
->xmethods
= PyList_New (0);
192 if (self
->xmethods
== NULL
)
199 objfpy_new (PyTypeObject
*type
, PyObject
*args
, PyObject
*keywords
)
201 objfile_object
*self
= (objfile_object
*) type
->tp_alloc (type
, 0);
205 if (!objfpy_initialize (self
))
212 return (PyObject
*) self
;
216 objfpy_get_printers (PyObject
*o
, void *ignore
)
218 objfile_object
*self
= (objfile_object
*) o
;
220 Py_INCREF (self
->printers
);
221 return self
->printers
;
225 objfpy_set_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
228 objfile_object
*self
= (objfile_object
*) o
;
232 PyErr_SetString (PyExc_TypeError
,
233 _("Cannot delete the pretty_printers attribute."));
237 if (! PyList_Check (value
))
239 PyErr_SetString (PyExc_TypeError
,
240 _("The pretty_printers attribute must be a list."));
244 /* Take care in case the LHS and RHS are related somehow. */
245 tmp
= self
->printers
;
247 self
->printers
= value
;
253 /* Return the Python dictionary attribute containing frame filters for
256 objfpy_get_frame_filters (PyObject
*o
, void *ignore
)
258 objfile_object
*self
= (objfile_object
*) o
;
260 Py_INCREF (self
->frame_filters
);
261 return self
->frame_filters
;
264 /* Set this object file's frame filters dictionary to FILTERS. */
266 objfpy_set_frame_filters (PyObject
*o
, PyObject
*filters
, void *ignore
)
269 objfile_object
*self
= (objfile_object
*) o
;
273 PyErr_SetString (PyExc_TypeError
,
274 _("Cannot delete the frame filters attribute."));
278 if (! PyDict_Check (filters
))
280 PyErr_SetString (PyExc_TypeError
,
281 _("The frame_filters attribute must be a dictionary."));
285 /* Take care in case the LHS and RHS are related somehow. */
286 tmp
= self
->frame_filters
;
288 self
->frame_filters
= filters
;
294 /* Get the 'type_printers' attribute. */
297 objfpy_get_type_printers (PyObject
*o
, void *ignore
)
299 objfile_object
*self
= (objfile_object
*) o
;
301 Py_INCREF (self
->type_printers
);
302 return self
->type_printers
;
305 /* Get the 'xmethods' attribute. */
308 objfpy_get_xmethods (PyObject
*o
, void *ignore
)
310 objfile_object
*self
= (objfile_object
*) o
;
312 Py_INCREF (self
->xmethods
);
313 return self
->xmethods
;
316 /* Set the 'type_printers' attribute. */
319 objfpy_set_type_printers (PyObject
*o
, PyObject
*value
, void *ignore
)
322 objfile_object
*self
= (objfile_object
*) o
;
326 PyErr_SetString (PyExc_TypeError
,
327 _("Cannot delete the type_printers attribute."));
331 if (! PyList_Check (value
))
333 PyErr_SetString (PyExc_TypeError
,
334 _("The type_printers attribute must be a list."));
338 /* Take care in case the LHS and RHS are related somehow. */
339 tmp
= self
->type_printers
;
341 self
->type_printers
= value
;
347 /* Implementation of gdb.Objfile.is_valid (self) -> Boolean.
348 Returns True if this object file still exists in GDB. */
351 objfpy_is_valid (PyObject
*self
, PyObject
*args
)
353 objfile_object
*obj
= (objfile_object
*) self
;
361 /* Implementation of gdb.Objfile.add_separate_debug_file (self) -> Boolean. */
364 objfpy_add_separate_debug_file (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
366 static char *keywords
[] = { "file_name", NULL
};
367 objfile_object
*obj
= (objfile_object
*) self
;
368 const char *file_name
;
369 int symfile_flags
= 0;
370 volatile struct gdb_exception except
;
372 OBJFPY_REQUIRE_VALID (obj
);
374 if (!PyArg_ParseTupleAndKeywords (args
, kw
, "s", keywords
, &file_name
))
377 TRY_CATCH (except
, RETURN_MASK_ALL
)
379 bfd
*abfd
= symfile_bfd_open (file_name
);
381 symbol_file_add_separate (abfd
, file_name
, symfile_flags
, obj
->objfile
);
383 GDB_PY_HANDLE_EXCEPTION (except
);
388 /* Subroutine of gdbpy_lookup_objfile_by_build_id to simplify it.
389 Return non-zero if STRING is a potentially valid build id. */
392 objfpy_build_id_ok (const char *string
)
394 size_t i
, n
= strlen (string
);
398 for (i
= 0; i
< n
; ++i
)
400 if (!isxdigit (string
[i
]))
406 /* Subroutine of gdbpy_lookup_objfile_by_build_id to simplify it.
407 Returns non-zero if BUILD_ID matches STRING.
408 It is assumed that objfpy_build_id_ok (string) returns TRUE. */
411 objfpy_build_id_matches (const struct elf_build_id
*build_id
,
416 if (strlen (string
) != 2 * build_id
->size
)
419 for (i
= 0; i
< build_id
->size
; ++i
)
421 char c1
= string
[i
* 2], c2
= string
[i
* 2 + 1];
422 int byte
= (host_hex_value (c1
) << 4) | host_hex_value (c2
);
424 if (byte
!= build_id
->data
[i
])
431 /* Subroutine of gdbpy_lookup_objfile to simplify it.
432 Look up an objfile by its file name. */
434 static struct objfile
*
435 objfpy_lookup_objfile_by_name (const char *name
)
437 struct objfile
*objfile
;
439 ALL_OBJFILES (objfile
)
441 if ((objfile
->flags
& OBJF_NOT_FILENAME
) != 0)
443 /* Don't return separate debug files. */
444 if (objfile
->separate_debug_objfile_backlink
!= NULL
)
446 if (compare_filenames_for_search (objfile_name (objfile
), name
))
453 /* Subroutine of gdbpy_lookup_objfile to simplify it.
454 Look up an objfile by its build id. */
456 static struct objfile
*
457 objfpy_lookup_objfile_by_build_id (const char *build_id
)
459 struct objfile
*objfile
;
461 ALL_OBJFILES (objfile
)
463 const struct elf_build_id
*obfd_build_id
;
465 if (objfile
->obfd
== NULL
)
467 /* Don't return separate debug files. */
468 if (objfile
->separate_debug_objfile_backlink
!= NULL
)
470 obfd_build_id
= build_id_bfd_get (objfile
->obfd
);
471 if (obfd_build_id
== NULL
)
473 if (objfpy_build_id_matches (obfd_build_id
, build_id
))
480 /* Implementation of gdb.lookup_objfile. */
483 gdbpy_lookup_objfile (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
485 static char *keywords
[] = { "name", "by_build_id", NULL
};
487 PyObject
*by_build_id_obj
= NULL
;
489 struct objfile
*objfile
;
491 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "s|O!", keywords
,
492 &name
, &PyBool_Type
, &by_build_id_obj
))
496 if (by_build_id_obj
!= NULL
)
498 int cmp
= PyObject_IsTrue (by_build_id_obj
);
507 if (!objfpy_build_id_ok (name
))
509 PyErr_SetString (PyExc_TypeError
, _("Not a valid build id."));
512 objfile
= objfpy_lookup_objfile_by_build_id (name
);
515 objfile
= objfpy_lookup_objfile_by_name (name
);
519 PyObject
*result
= objfile_to_objfile_object (objfile
);
525 PyErr_SetString (PyExc_ValueError
, _("Objfile not found."));
531 /* Clear the OBJFILE pointer in an Objfile object and remove the
534 py_free_objfile (struct objfile
*objfile
, void *datum
)
536 struct cleanup
*cleanup
;
537 objfile_object
*object
= datum
;
539 cleanup
= ensure_python_env (get_objfile_arch (objfile
), current_language
);
540 object
->objfile
= NULL
;
541 Py_DECREF ((PyObject
*) object
);
542 do_cleanups (cleanup
);
545 /* Return a borrowed reference to the Python object of type Objfile
546 representing OBJFILE. If the object has already been created,
547 return it. Otherwise, create it. Return NULL and set the Python
551 objfile_to_objfile_object (struct objfile
*objfile
)
553 objfile_object
*object
;
555 object
= objfile_data (objfile
, objfpy_objfile_data_key
);
558 object
= PyObject_New (objfile_object
, &objfile_object_type
);
561 if (!objfpy_initialize (object
))
567 object
->objfile
= objfile
;
568 set_objfile_data (objfile
, objfpy_objfile_data_key
, object
);
572 return (PyObject
*) object
;
576 gdbpy_initialize_objfile (void)
578 objfpy_objfile_data_key
579 = register_objfile_data_with_cleanup (NULL
, py_free_objfile
);
581 if (PyType_Ready (&objfile_object_type
) < 0)
584 return gdb_pymodule_addobject (gdb_module
, "Objfile",
585 (PyObject
*) &objfile_object_type
);
590 static PyMethodDef objfile_object_methods
[] =
592 { "is_valid", objfpy_is_valid
, METH_NOARGS
,
593 "is_valid () -> Boolean.\n\
594 Return true if this object file is valid, false if not." },
596 { "add_separate_debug_file", (PyCFunction
) objfpy_add_separate_debug_file
,
597 METH_VARARGS
| METH_KEYWORDS
,
598 "add_separate_debug_file (file_name).\n\
599 Add FILE_NAME to the list of files containing debug info for the objfile." },
604 static PyGetSetDef objfile_getset
[] =
606 { "__dict__", gdb_py_generic_dict
, NULL
,
607 "The __dict__ for this objfile.", &objfile_object_type
},
608 { "filename", objfpy_get_filename
, NULL
,
609 "The objfile's filename, or None.", NULL
},
610 { "owner", objfpy_get_owner
, NULL
,
611 "The objfile owner of separate debug info objfiles, or None.",
613 { "build_id", objfpy_get_build_id
, NULL
,
614 "The objfile's build id, or None.", NULL
},
615 { "progspace", objfpy_get_progspace
, NULL
,
616 "The objfile's progspace, or None.", NULL
},
617 { "pretty_printers", objfpy_get_printers
, objfpy_set_printers
,
618 "Pretty printers.", NULL
},
619 { "frame_filters", objfpy_get_frame_filters
,
620 objfpy_set_frame_filters
, "Frame Filters.", NULL
},
621 { "type_printers", objfpy_get_type_printers
, objfpy_set_type_printers
,
622 "Type printers.", NULL
},
623 { "xmethods", objfpy_get_xmethods
, NULL
,
624 "Debug methods.", NULL
},
628 static PyTypeObject objfile_object_type
=
630 PyVarObject_HEAD_INIT (NULL
, 0)
631 "gdb.Objfile", /*tp_name*/
632 sizeof (objfile_object
), /*tp_basicsize*/
634 objfpy_dealloc
, /*tp_dealloc*/
641 0, /*tp_as_sequence*/
649 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
650 "GDB objfile object", /* tp_doc */
653 0, /* tp_richcompare */
654 0, /* tp_weaklistoffset */
657 objfile_object_methods
, /* tp_methods */
659 objfile_getset
, /* tp_getset */
662 0, /* tp_descr_get */
663 0, /* tp_descr_set */
664 offsetof (objfile_object
, dict
), /* tp_dictoffset */
667 objfpy_new
, /* tp_new */