gdb/python/py-objfile.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Python interface to objfiles.

  2.    Copyright (C) 2008-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "python-internal.h"
  16. #include "charset.h"
  17. #include "objfiles.h"
  18. #include "language.h"
  19. #include "build-id.h"
  20. #include "elf-bfd.h"
  21. #include "symtab.h"

  22. typedef struct
  23. {
  24.   PyObject_HEAD

  25.   /* The corresponding objfile.  */
  26.   struct objfile *objfile;

  27.   /* Dictionary holding user-added attributes.
  28.      This is the __dict__ attribute of the object.  */
  29.   PyObject *dict;

  30.   /* The pretty-printer list of functions.  */
  31.   PyObject *printers;

  32.   /* The frame filter list of functions.  */
  33.   PyObject *frame_filters;
  34.   /* The type-printer list.  */
  35.   PyObject *type_printers;

  36.   /* The debug method matcher list.  */
  37.   PyObject *xmethods;
  38. } objfile_object;

  39. static PyTypeObject objfile_object_type
  40.     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("objfile_object");

  41. static const struct objfile_data *objfpy_objfile_data_key;

  42. /* Require that OBJF be a valid objfile.  */
  43. #define OBJFPY_REQUIRE_VALID(obj)                                \
  44.   do {                                                                \
  45.     if (!(obj)->objfile)                                        \
  46.       {                                                                \
  47.         PyErr_SetString (PyExc_RuntimeError,                        \
  48.                          _("Objfile no longer exists."));        \
  49.         return NULL;                                                \
  50.       }                                                                \
  51.   } while (0)



  52. /* An Objfile method which returns the objfile's file name, or None.  */

  53. static PyObject *
  54. objfpy_get_filename (PyObject *self, void *closure)
  55. {
  56.   objfile_object *obj = (objfile_object *) self;

  57.   if (obj->objfile)
  58.     return PyString_Decode (objfile_name (obj->objfile),
  59.                             strlen (objfile_name (obj->objfile)),
  60.                             host_charset (), NULL);
  61.   Py_RETURN_NONE;
  62. }

  63. /* If SELF is a separate debug-info file, return the "backlink" field.
  64.    Otherwise return None.  */

  65. static PyObject *
  66. objfpy_get_owner (PyObject *self, void *closure)
  67. {
  68.   objfile_object *obj = (objfile_object *) self;
  69.   struct objfile *objfile = obj->objfile;
  70.   struct objfile *owner;

  71.   OBJFPY_REQUIRE_VALID (obj);

  72.   owner = objfile->separate_debug_objfile_backlink;
  73.   if (owner != NULL)
  74.     {
  75.       PyObject *result = objfile_to_objfile_object (owner);

  76.       Py_XINCREF (result);
  77.       return result;
  78.     }
  79.   Py_RETURN_NONE;
  80. }

  81. /* An Objfile method which returns the objfile's build id, or None.  */

  82. static PyObject *
  83. objfpy_get_build_id (PyObject *self, void *closure)
  84. {
  85.   objfile_object *obj = (objfile_object *) self;
  86.   struct objfile *objfile = obj->objfile;
  87.   const struct elf_build_id *build_id = NULL;
  88.   volatile struct gdb_exception except;

  89.   OBJFPY_REQUIRE_VALID (obj);

  90.   TRY_CATCH (except, RETURN_MASK_ALL)
  91.     {
  92.       build_id = build_id_bfd_get (objfile->obfd);
  93.     }
  94.   GDB_PY_HANDLE_EXCEPTION (except);

  95.   if (build_id != NULL)
  96.     {
  97.       char *hex_form = make_hex_string (build_id->data, build_id->size);
  98.       PyObject *result;

  99.       result = PyString_Decode (hex_form, strlen (hex_form),
  100.                                 host_charset (), NULL);
  101.       xfree (hex_form);
  102.       return result;
  103.     }

  104.   Py_RETURN_NONE;
  105. }

  106. /* An Objfile method which returns the objfile's progspace, or None.  */

  107. static PyObject *
  108. objfpy_get_progspace (PyObject *self, void *closure)
  109. {
  110.   objfile_object *obj = (objfile_object *) self;

  111.   if (obj->objfile)
  112.     {
  113.       PyObject *pspace =  pspace_to_pspace_object (obj->objfile->pspace);

  114.       Py_XINCREF (pspace);
  115.       return pspace;
  116.     }

  117.   Py_RETURN_NONE;
  118. }

  119. static void
  120. objfpy_dealloc (PyObject *o)
  121. {
  122.   objfile_object *self = (objfile_object *) o;

  123.   Py_XDECREF (self->dict);
  124.   Py_XDECREF (self->printers);
  125.   Py_XDECREF (self->frame_filters);
  126.   Py_XDECREF (self->type_printers);
  127.   Py_XDECREF (self->xmethods);
  128.   Py_TYPE (self)->tp_free (self);
  129. }

  130. /* Initialize an objfile_object.
  131.    The result is a boolean indicating success.  */

  132. static int
  133. objfpy_initialize (objfile_object *self)
  134. {
  135.   self->objfile = NULL;
  136.   self->dict = NULL;

  137.   self->printers = PyList_New (0);
  138.   if (self->printers == NULL)
  139.     return 0;

  140.   self->frame_filters = PyDict_New ();
  141.   if (self->frame_filters == NULL)
  142.     return 0;

  143.   self->type_printers = PyList_New (0);
  144.   if (self->type_printers == NULL)
  145.     return 0;

  146.   self->xmethods = PyList_New (0);
  147.   if (self->xmethods == NULL)
  148.     return 0;

  149.   return 1;
  150. }

  151. static PyObject *
  152. objfpy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
  153. {
  154.   objfile_object *self = (objfile_object *) type->tp_alloc (type, 0);

  155.   if (self)
  156.     {
  157.       if (!objfpy_initialize (self))
  158.         {
  159.           Py_DECREF (self);
  160.           return NULL;
  161.         }
  162.     }

  163.   return (PyObject *) self;
  164. }

  165. PyObject *
  166. objfpy_get_printers (PyObject *o, void *ignore)
  167. {
  168.   objfile_object *self = (objfile_object *) o;

  169.   Py_INCREF (self->printers);
  170.   return self->printers;
  171. }

  172. static int
  173. objfpy_set_printers (PyObject *o, PyObject *value, void *ignore)
  174. {
  175.   PyObject *tmp;
  176.   objfile_object *self = (objfile_object *) o;

  177.   if (! value)
  178.     {
  179.       PyErr_SetString (PyExc_TypeError,
  180.                        _("Cannot delete the pretty_printers attribute."));
  181.       return -1;
  182.     }

  183.   if (! PyList_Check (value))
  184.     {
  185.       PyErr_SetString (PyExc_TypeError,
  186.                        _("The pretty_printers attribute must be a list."));
  187.       return -1;
  188.     }

  189.   /* Take care in case the LHS and RHS are related somehow.  */
  190.   tmp = self->printers;
  191.   Py_INCREF (value);
  192.   self->printers = value;
  193.   Py_XDECREF (tmp);

  194.   return 0;
  195. }

  196. /* Return the Python dictionary attribute containing frame filters for
  197.    this object file.  */
  198. PyObject *
  199. objfpy_get_frame_filters (PyObject *o, void *ignore)
  200. {
  201.   objfile_object *self = (objfile_object *) o;

  202.   Py_INCREF (self->frame_filters);
  203.   return self->frame_filters;
  204. }

  205. /* Set this object file's frame filters dictionary to FILTERS.  */
  206. static int
  207. objfpy_set_frame_filters (PyObject *o, PyObject *filters, void *ignore)
  208. {
  209.   PyObject *tmp;
  210.   objfile_object *self = (objfile_object *) o;

  211.   if (! filters)
  212.     {
  213.       PyErr_SetString (PyExc_TypeError,
  214.                        _("Cannot delete the frame filters attribute."));
  215.       return -1;
  216.     }

  217.   if (! PyDict_Check (filters))
  218.     {
  219.       PyErr_SetString (PyExc_TypeError,
  220.                        _("The frame_filters attribute must be a dictionary."));
  221.       return -1;
  222.     }

  223.   /* Take care in case the LHS and RHS are related somehow.  */
  224.   tmp = self->frame_filters;
  225.   Py_INCREF (filters);
  226.   self->frame_filters = filters;
  227.   Py_XDECREF (tmp);

  228.   return 0;
  229. }

  230. /* Get the 'type_printers' attribute.  */

  231. static PyObject *
  232. objfpy_get_type_printers (PyObject *o, void *ignore)
  233. {
  234.   objfile_object *self = (objfile_object *) o;

  235.   Py_INCREF (self->type_printers);
  236.   return self->type_printers;
  237. }

  238. /* Get the 'xmethods' attribute.  */

  239. PyObject *
  240. objfpy_get_xmethods (PyObject *o, void *ignore)
  241. {
  242.   objfile_object *self = (objfile_object *) o;

  243.   Py_INCREF (self->xmethods);
  244.   return self->xmethods;
  245. }

  246. /* Set the 'type_printers' attribute.  */

  247. static int
  248. objfpy_set_type_printers (PyObject *o, PyObject *value, void *ignore)
  249. {
  250.   PyObject *tmp;
  251.   objfile_object *self = (objfile_object *) o;

  252.   if (! value)
  253.     {
  254.       PyErr_SetString (PyExc_TypeError,
  255.                        _("Cannot delete the type_printers attribute."));
  256.       return -1;
  257.     }

  258.   if (! PyList_Check (value))
  259.     {
  260.       PyErr_SetString (PyExc_TypeError,
  261.                        _("The type_printers attribute must be a list."));
  262.       return -1;
  263.     }

  264.   /* Take care in case the LHS and RHS are related somehow.  */
  265.   tmp = self->type_printers;
  266.   Py_INCREF (value);
  267.   self->type_printers = value;
  268.   Py_XDECREF (tmp);

  269.   return 0;
  270. }

  271. /* Implementation of gdb.Objfile.is_valid (self) -> Boolean.
  272.    Returns True if this object file still exists in GDB.  */

  273. static PyObject *
  274. objfpy_is_valid (PyObject *self, PyObject *args)
  275. {
  276.   objfile_object *obj = (objfile_object *) self;

  277.   if (! obj->objfile)
  278.     Py_RETURN_FALSE;

  279.   Py_RETURN_TRUE;
  280. }

  281. /* Implementation of gdb.Objfile.add_separate_debug_file (self) -> Boolean.  */

  282. static PyObject *
  283. objfpy_add_separate_debug_file (PyObject *self, PyObject *args, PyObject *kw)
  284. {
  285.   static char *keywords[] = { "file_name", NULL };
  286.   objfile_object *obj = (objfile_object *) self;
  287.   const char *file_name;
  288.   int symfile_flags = 0;
  289.   volatile struct gdb_exception except;

  290.   OBJFPY_REQUIRE_VALID (obj);

  291.   if (!PyArg_ParseTupleAndKeywords (args, kw, "s", keywords, &file_name))
  292.     return NULL;

  293.   TRY_CATCH (except, RETURN_MASK_ALL)
  294.     {
  295.       bfd *abfd = symfile_bfd_open (file_name);

  296.       symbol_file_add_separate (abfd, file_name, symfile_flags, obj->objfile);
  297.     }
  298.   GDB_PY_HANDLE_EXCEPTION (except);

  299.   Py_RETURN_NONE;
  300. }

  301. /* Subroutine of gdbpy_lookup_objfile_by_build_id to simplify it.
  302.    Return non-zero if STRING is a potentially valid build id.  */

  303. static int
  304. objfpy_build_id_ok (const char *string)
  305. {
  306.   size_t i, n = strlen (string);

  307.   if (n % 2 != 0)
  308.     return 0;
  309.   for (i = 0; i < n; ++i)
  310.     {
  311.       if (!isxdigit (string[i]))
  312.         return 0;
  313.     }
  314.   return 1;
  315. }

  316. /* Subroutine of gdbpy_lookup_objfile_by_build_id to simplify it.
  317.    Returns non-zero if BUILD_ID matches STRING.
  318.    It is assumed that objfpy_build_id_ok (string) returns TRUE.  */

  319. static int
  320. objfpy_build_id_matches (const struct elf_build_id *build_id,
  321.                          const char *string)
  322. {
  323.   size_t i;

  324.   if (strlen (string) != 2 * build_id->size)
  325.     return 0;

  326.   for (i = 0; i < build_id->size; ++i)
  327.     {
  328.       char c1 = string[i * 2], c2 = string[i * 2 + 1];
  329.       int byte = (host_hex_value (c1) << 4) | host_hex_value (c2);

  330.       if (byte != build_id->data[i])
  331.         return 0;
  332.     }

  333.   return 1;
  334. }

  335. /* Subroutine of gdbpy_lookup_objfile to simplify it.
  336.    Look up an objfile by its file name.  */

  337. static struct objfile *
  338. objfpy_lookup_objfile_by_name (const char *name)
  339. {
  340.   struct objfile *objfile;

  341.   ALL_OBJFILES (objfile)
  342.     {
  343.       if ((objfile->flags & OBJF_NOT_FILENAME) != 0)
  344.         continue;
  345.       /* Don't return separate debug files.  */
  346.       if (objfile->separate_debug_objfile_backlink != NULL)
  347.         continue;
  348.       if (compare_filenames_for_search (objfile_name (objfile), name))
  349.         return objfile;
  350.     }

  351.   return NULL;
  352. }

  353. /* Subroutine of gdbpy_lookup_objfile to simplify it.
  354.    Look up an objfile by its build id.  */

  355. static struct objfile *
  356. objfpy_lookup_objfile_by_build_id (const char *build_id)
  357. {
  358.   struct objfile *objfile;

  359.   ALL_OBJFILES (objfile)
  360.     {
  361.       const struct elf_build_id *obfd_build_id;

  362.       if (objfile->obfd == NULL)
  363.         continue;
  364.       /* Don't return separate debug files.  */
  365.       if (objfile->separate_debug_objfile_backlink != NULL)
  366.         continue;
  367.       obfd_build_id = build_id_bfd_get (objfile->obfd);
  368.       if (obfd_build_id == NULL)
  369.         continue;
  370.       if (objfpy_build_id_matches (obfd_build_id, build_id))
  371.         return objfile;
  372.     }

  373.   return NULL;
  374. }

  375. /* Implementation of gdb.lookup_objfile.  */

  376. PyObject *
  377. gdbpy_lookup_objfile (PyObject *self, PyObject *args, PyObject *kw)
  378. {
  379.   static char *keywords[] = { "name", "by_build_id", NULL };
  380.   const char *name;
  381.   PyObject *by_build_id_obj = NULL;
  382.   int by_build_id;
  383.   struct objfile *objfile;

  384.   if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O!", keywords,
  385.                                      &name, &PyBool_Type, &by_build_id_obj))
  386.     return NULL;

  387.   by_build_id = 0;
  388.   if (by_build_id_obj != NULL)
  389.     {
  390.       int cmp = PyObject_IsTrue (by_build_id_obj);

  391.       if (cmp < 0)
  392.         return NULL;
  393.       by_build_id = cmp;
  394.     }

  395.   if (by_build_id)
  396.     {
  397.       if (!objfpy_build_id_ok (name))
  398.         {
  399.           PyErr_SetString (PyExc_TypeError, _("Not a valid build id."));
  400.           return NULL;
  401.         }
  402.       objfile = objfpy_lookup_objfile_by_build_id (name);
  403.     }
  404.   else
  405.     objfile = objfpy_lookup_objfile_by_name (name);

  406.   if (objfile != NULL)
  407.     {
  408.       PyObject *result = objfile_to_objfile_object (objfile);

  409.       Py_XINCREF (result);
  410.       return result;
  411.     }

  412.   PyErr_SetString (PyExc_ValueError, _("Objfile not found."));
  413.   return NULL;
  414. }



  415. /* Clear the OBJFILE pointer in an Objfile object and remove the
  416.    reference.  */
  417. static void
  418. py_free_objfile (struct objfile *objfile, void *datum)
  419. {
  420.   struct cleanup *cleanup;
  421.   objfile_object *object = datum;

  422.   cleanup = ensure_python_env (get_objfile_arch (objfile), current_language);
  423.   object->objfile = NULL;
  424.   Py_DECREF ((PyObject *) object);
  425.   do_cleanups (cleanup);
  426. }

  427. /* Return a borrowed reference to the Python object of type Objfile
  428.    representing OBJFILE.  If the object has already been created,
  429.    return it.  Otherwise, create it.  Return NULL and set the Python
  430.    error on failure.  */

  431. PyObject *
  432. objfile_to_objfile_object (struct objfile *objfile)
  433. {
  434.   objfile_object *object;

  435.   object = objfile_data (objfile, objfpy_objfile_data_key);
  436.   if (!object)
  437.     {
  438.       object = PyObject_New (objfile_object, &objfile_object_type);
  439.       if (object)
  440.         {
  441.           if (!objfpy_initialize (object))
  442.             {
  443.               Py_DECREF (object);
  444.               return NULL;
  445.             }

  446.           object->objfile = objfile;
  447.           set_objfile_data (objfile, objfpy_objfile_data_key, object);
  448.         }
  449.     }

  450.   return (PyObject *) object;
  451. }

  452. int
  453. gdbpy_initialize_objfile (void)
  454. {
  455.   objfpy_objfile_data_key
  456.     = register_objfile_data_with_cleanup (NULL, py_free_objfile);

  457.   if (PyType_Ready (&objfile_object_type) < 0)
  458.     return -1;

  459.   return gdb_pymodule_addobject (gdb_module, "Objfile",
  460.                                  (PyObject *) &objfile_object_type);
  461. }



  462. static PyMethodDef objfile_object_methods[] =
  463. {
  464.   { "is_valid", objfpy_is_valid, METH_NOARGS,
  465.     "is_valid () -> Boolean.\n\
  466. Return true if this object file is valid, false if not." },

  467.   { "add_separate_debug_file", (PyCFunction) objfpy_add_separate_debug_file,
  468.     METH_VARARGS | METH_KEYWORDS,
  469.     "add_separate_debug_file (file_name).\n\
  470. Add FILE_NAME to the list of files containing debug info for the objfile." },

  471.   { NULL }
  472. };

  473. static PyGetSetDef objfile_getset[] =
  474. {
  475.   { "__dict__", gdb_py_generic_dict, NULL,
  476.     "The __dict__ for this objfile.", &objfile_object_type },
  477.   { "filename", objfpy_get_filename, NULL,
  478.     "The objfile's filename, or None.", NULL },
  479.   { "owner", objfpy_get_owner, NULL,
  480.     "The objfile owner of separate debug info objfiles, or None.",
  481.     NULL },
  482.   { "build_id", objfpy_get_build_id, NULL,
  483.     "The objfile's build id, or None.", NULL },
  484.   { "progspace", objfpy_get_progspace, NULL,
  485.     "The objfile's progspace, or None.", NULL },
  486.   { "pretty_printers", objfpy_get_printers, objfpy_set_printers,
  487.     "Pretty printers.", NULL },
  488.   { "frame_filters", objfpy_get_frame_filters,
  489.     objfpy_set_frame_filters, "Frame Filters.", NULL },
  490.   { "type_printers", objfpy_get_type_printers, objfpy_set_type_printers,
  491.     "Type printers.", NULL },
  492.   { "xmethods", objfpy_get_xmethods, NULL,
  493.     "Debug methods.", NULL },
  494.   { NULL }
  495. };

  496. static PyTypeObject objfile_object_type =
  497. {
  498.   PyVarObject_HEAD_INIT (NULL, 0)
  499.   "gdb.Objfile",                  /*tp_name*/
  500.   sizeof (objfile_object),          /*tp_basicsize*/
  501.   0,                                  /*tp_itemsize*/
  502.   objfpy_dealloc,                  /*tp_dealloc*/
  503.   0,                                  /*tp_print*/
  504.   0,                                  /*tp_getattr*/
  505.   0,                                  /*tp_setattr*/
  506.   0,                                  /*tp_compare*/
  507.   0,                                  /*tp_repr*/
  508.   0,                                  /*tp_as_number*/
  509.   0,                                  /*tp_as_sequence*/
  510.   0,                                  /*tp_as_mapping*/
  511.   0,                                  /*tp_hash */
  512.   0,                                  /*tp_call*/
  513.   0,                                  /*tp_str*/
  514.   0,                                  /*tp_getattro*/
  515.   0,                                  /*tp_setattro*/
  516.   0,                                  /*tp_as_buffer*/
  517.   Py_TPFLAGS_DEFAULT,                  /*tp_flags*/
  518.   "GDB objfile object",                  /* tp_doc */
  519.   0,                                  /* tp_traverse */
  520.   0,                                  /* tp_clear */
  521.   0,                                  /* tp_richcompare */
  522.   0,                                  /* tp_weaklistoffset */
  523.   0,                                  /* tp_iter */
  524.   0,                                  /* tp_iternext */
  525.   objfile_object_methods,          /* tp_methods */
  526.   0,                                  /* tp_members */
  527.   objfile_getset,                  /* tp_getset */
  528.   0,                                  /* tp_base */
  529.   0,                                  /* tp_dict */
  530.   0,                                  /* tp_descr_get */
  531.   0,                                  /* tp_descr_set */
  532.   offsetof (objfile_object, dict), /* tp_dictoffset */
  533.   0,                                  /* tp_init */
  534.   0,                                  /* tp_alloc */
  535.   objfpy_new,                          /* tp_new */
  536. };