Home | History | Annotate | Line # | Download | only in python
py-objfile.c revision 1.3
      1  1.1  christos /* Python interface to objfiles.
      2  1.1  christos 
      3  1.3  christos    Copyright (C) 2008-2015 Free Software Foundation, Inc.
      4  1.1  christos 
      5  1.1  christos    This file is part of GDB.
      6  1.1  christos 
      7  1.1  christos    This program is free software; you can redistribute it and/or modify
      8  1.1  christos    it under the terms of the GNU General Public License as published by
      9  1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10  1.1  christos    (at your option) any later version.
     11  1.1  christos 
     12  1.1  christos    This program is distributed in the hope that it will be useful,
     13  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15  1.1  christos    GNU General Public License for more details.
     16  1.1  christos 
     17  1.1  christos    You should have received a copy of the GNU General Public License
     18  1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19  1.1  christos 
     20  1.1  christos #include "defs.h"
     21  1.1  christos #include "python-internal.h"
     22  1.1  christos #include "charset.h"
     23  1.1  christos #include "objfiles.h"
     24  1.1  christos #include "language.h"
     25  1.3  christos #include "build-id.h"
     26  1.3  christos #include "elf-bfd.h"
     27  1.3  christos #include "symtab.h"
     28  1.1  christos 
     29  1.1  christos typedef struct
     30  1.1  christos {
     31  1.1  christos   PyObject_HEAD
     32  1.1  christos 
     33  1.1  christos   /* The corresponding objfile.  */
     34  1.1  christos   struct objfile *objfile;
     35  1.1  christos 
     36  1.3  christos   /* Dictionary holding user-added attributes.
     37  1.3  christos      This is the __dict__ attribute of the object.  */
     38  1.3  christos   PyObject *dict;
     39  1.3  christos 
     40  1.1  christos   /* The pretty-printer list of functions.  */
     41  1.1  christos   PyObject *printers;
     42  1.1  christos 
     43  1.1  christos   /* The frame filter list of functions.  */
     44  1.1  christos   PyObject *frame_filters;
     45  1.1  christos   /* The type-printer list.  */
     46  1.1  christos   PyObject *type_printers;
     47  1.3  christos 
     48  1.3  christos   /* The debug method matcher list.  */
     49  1.3  christos   PyObject *xmethods;
     50  1.1  christos } objfile_object;
     51  1.1  christos 
     52  1.1  christos static PyTypeObject objfile_object_type
     53  1.1  christos     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("objfile_object");
     54  1.1  christos 
     55  1.1  christos static const struct objfile_data *objfpy_objfile_data_key;
     56  1.1  christos 
     57  1.3  christos /* Require that OBJF be a valid objfile.  */
     58  1.3  christos #define OBJFPY_REQUIRE_VALID(obj)				\
     59  1.3  christos   do {								\
     60  1.3  christos     if (!(obj)->objfile)					\
     61  1.3  christos       {								\
     62  1.3  christos 	PyErr_SetString (PyExc_RuntimeError,			\
     63  1.3  christos 			 _("Objfile no longer exists."));	\
     64  1.3  christos 	return NULL;						\
     65  1.3  christos       }								\
     66  1.3  christos   } while (0)
     67  1.3  christos 
     68  1.1  christos 
     69  1.1  christos 
     71  1.3  christos /* An Objfile method which returns the objfile's file name, or None.  */
     72  1.1  christos 
     73  1.1  christos static PyObject *
     74  1.1  christos objfpy_get_filename (PyObject *self, void *closure)
     75  1.1  christos {
     76  1.1  christos   objfile_object *obj = (objfile_object *) self;
     77  1.1  christos 
     78  1.1  christos   if (obj->objfile)
     79  1.1  christos     return PyString_Decode (objfile_name (obj->objfile),
     80  1.1  christos 			    strlen (objfile_name (obj->objfile)),
     81  1.1  christos 			    host_charset (), NULL);
     82  1.1  christos   Py_RETURN_NONE;
     83  1.1  christos }
     84  1.3  christos 
     85  1.3  christos /* If SELF is a separate debug-info file, return the "backlink" field.
     86  1.3  christos    Otherwise return None.  */
     87  1.3  christos 
     88  1.3  christos static PyObject *
     89  1.3  christos objfpy_get_owner (PyObject *self, void *closure)
     90  1.3  christos {
     91  1.3  christos   objfile_object *obj = (objfile_object *) self;
     92  1.3  christos   struct objfile *objfile = obj->objfile;
     93  1.3  christos   struct objfile *owner;
     94  1.3  christos 
     95  1.3  christos   OBJFPY_REQUIRE_VALID (obj);
     96  1.3  christos 
     97  1.3  christos   owner = objfile->separate_debug_objfile_backlink;
     98  1.3  christos   if (owner != NULL)
     99  1.3  christos     {
    100  1.3  christos       PyObject *result = objfile_to_objfile_object (owner);
    101  1.3  christos 
    102  1.3  christos       Py_XINCREF (result);
    103  1.3  christos       return result;
    104  1.3  christos     }
    105  1.3  christos   Py_RETURN_NONE;
    106  1.3  christos }
    107  1.3  christos 
    108  1.3  christos /* An Objfile method which returns the objfile's build id, or None.  */
    109  1.3  christos 
    110  1.3  christos static PyObject *
    111  1.3  christos objfpy_get_build_id (PyObject *self, void *closure)
    112  1.3  christos {
    113  1.3  christos   objfile_object *obj = (objfile_object *) self;
    114  1.3  christos   struct objfile *objfile = obj->objfile;
    115  1.3  christos   const struct elf_build_id *build_id = NULL;
    116  1.3  christos   volatile struct gdb_exception except;
    117  1.3  christos 
    118  1.3  christos   OBJFPY_REQUIRE_VALID (obj);
    119  1.3  christos 
    120  1.3  christos   TRY_CATCH (except, RETURN_MASK_ALL)
    121  1.3  christos     {
    122  1.3  christos       build_id = build_id_bfd_get (objfile->obfd);
    123  1.3  christos     }
    124  1.3  christos   GDB_PY_HANDLE_EXCEPTION (except);
    125  1.3  christos 
    126  1.3  christos   if (build_id != NULL)
    127  1.3  christos     {
    128  1.3  christos       char *hex_form = make_hex_string (build_id->data, build_id->size);
    129  1.3  christos       PyObject *result;
    130  1.3  christos 
    131  1.3  christos       result = PyString_Decode (hex_form, strlen (hex_form),
    132  1.3  christos 				host_charset (), NULL);
    133  1.3  christos       xfree (hex_form);
    134  1.3  christos       return result;
    135  1.3  christos     }
    136  1.3  christos 
    137  1.3  christos   Py_RETURN_NONE;
    138  1.3  christos }
    139  1.3  christos 
    140  1.3  christos /* An Objfile method which returns the objfile's progspace, or None.  */
    141  1.3  christos 
    142  1.3  christos static PyObject *
    143  1.3  christos objfpy_get_progspace (PyObject *self, void *closure)
    144  1.3  christos {
    145  1.3  christos   objfile_object *obj = (objfile_object *) self;
    146  1.3  christos 
    147  1.3  christos   if (obj->objfile)
    148  1.3  christos     {
    149  1.3  christos       PyObject *pspace =  pspace_to_pspace_object (obj->objfile->pspace);
    150  1.3  christos 
    151  1.3  christos       Py_XINCREF (pspace);
    152  1.3  christos       return pspace;
    153  1.3  christos     }
    154  1.3  christos 
    155  1.3  christos   Py_RETURN_NONE;
    156  1.3  christos }
    157  1.1  christos 
    158  1.1  christos static void
    159  1.1  christos objfpy_dealloc (PyObject *o)
    160  1.1  christos {
    161  1.1  christos   objfile_object *self = (objfile_object *) o;
    162  1.3  christos 
    163  1.1  christos   Py_XDECREF (self->dict);
    164  1.1  christos   Py_XDECREF (self->printers);
    165  1.1  christos   Py_XDECREF (self->frame_filters);
    166  1.3  christos   Py_XDECREF (self->type_printers);
    167  1.1  christos   Py_XDECREF (self->xmethods);
    168  1.1  christos   Py_TYPE (self)->tp_free (self);
    169  1.1  christos }
    170  1.3  christos 
    171  1.3  christos /* Initialize an objfile_object.
    172  1.3  christos    The result is a boolean indicating success.  */
    173  1.3  christos 
    174  1.3  christos static int
    175  1.3  christos objfpy_initialize (objfile_object *self)
    176  1.3  christos {
    177  1.3  christos   self->objfile = NULL;
    178  1.3  christos   self->dict = NULL;
    179  1.3  christos 
    180  1.3  christos   self->printers = PyList_New (0);
    181  1.3  christos   if (self->printers == NULL)
    182  1.3  christos     return 0;
    183  1.3  christos 
    184  1.3  christos   self->frame_filters = PyDict_New ();
    185  1.3  christos   if (self->frame_filters == NULL)
    186  1.3  christos     return 0;
    187  1.3  christos 
    188  1.3  christos   self->type_printers = PyList_New (0);
    189  1.3  christos   if (self->type_printers == NULL)
    190  1.3  christos     return 0;
    191  1.3  christos 
    192  1.3  christos   self->xmethods = PyList_New (0);
    193  1.3  christos   if (self->xmethods == NULL)
    194  1.3  christos     return 0;
    195  1.3  christos 
    196  1.3  christos   return 1;
    197  1.3  christos }
    198  1.1  christos 
    199  1.1  christos static PyObject *
    200  1.1  christos objfpy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
    201  1.1  christos {
    202  1.1  christos   objfile_object *self = (objfile_object *) type->tp_alloc (type, 0);
    203  1.1  christos 
    204  1.1  christos   if (self)
    205  1.3  christos     {
    206  1.1  christos       if (!objfpy_initialize (self))
    207  1.1  christos 	{
    208  1.1  christos 	  Py_DECREF (self);
    209  1.1  christos 	  return NULL;
    210  1.3  christos 	}
    211  1.1  christos     }
    212  1.1  christos 
    213  1.1  christos   return (PyObject *) self;
    214  1.1  christos }
    215  1.1  christos 
    216  1.1  christos PyObject *
    217  1.1  christos objfpy_get_printers (PyObject *o, void *ignore)
    218  1.1  christos {
    219  1.1  christos   objfile_object *self = (objfile_object *) o;
    220  1.1  christos 
    221  1.1  christos   Py_INCREF (self->printers);
    222  1.1  christos   return self->printers;
    223  1.1  christos }
    224  1.1  christos 
    225  1.1  christos static int
    226  1.1  christos objfpy_set_printers (PyObject *o, PyObject *value, void *ignore)
    227  1.1  christos {
    228  1.1  christos   PyObject *tmp;
    229  1.1  christos   objfile_object *self = (objfile_object *) o;
    230  1.1  christos 
    231  1.1  christos   if (! value)
    232  1.1  christos     {
    233  1.1  christos       PyErr_SetString (PyExc_TypeError,
    234  1.1  christos 		       _("Cannot delete the pretty_printers attribute."));
    235  1.1  christos       return -1;
    236  1.1  christos     }
    237  1.1  christos 
    238  1.1  christos   if (! PyList_Check (value))
    239  1.1  christos     {
    240  1.1  christos       PyErr_SetString (PyExc_TypeError,
    241  1.1  christos 		       _("The pretty_printers attribute must be a list."));
    242  1.1  christos       return -1;
    243  1.1  christos     }
    244  1.1  christos 
    245  1.1  christos   /* Take care in case the LHS and RHS are related somehow.  */
    246  1.1  christos   tmp = self->printers;
    247  1.1  christos   Py_INCREF (value);
    248  1.1  christos   self->printers = value;
    249  1.1  christos   Py_XDECREF (tmp);
    250  1.1  christos 
    251  1.1  christos   return 0;
    252  1.1  christos }
    253  1.1  christos 
    254  1.1  christos /* Return the Python dictionary attribute containing frame filters for
    255  1.1  christos    this object file.  */
    256  1.1  christos PyObject *
    257  1.1  christos objfpy_get_frame_filters (PyObject *o, void *ignore)
    258  1.1  christos {
    259  1.1  christos   objfile_object *self = (objfile_object *) o;
    260  1.1  christos 
    261  1.1  christos   Py_INCREF (self->frame_filters);
    262  1.1  christos   return self->frame_filters;
    263  1.1  christos }
    264  1.1  christos 
    265  1.1  christos /* Set this object file's frame filters dictionary to FILTERS.  */
    266  1.1  christos static int
    267  1.1  christos objfpy_set_frame_filters (PyObject *o, PyObject *filters, void *ignore)
    268  1.1  christos {
    269  1.1  christos   PyObject *tmp;
    270  1.1  christos   objfile_object *self = (objfile_object *) o;
    271  1.1  christos 
    272  1.1  christos   if (! filters)
    273  1.1  christos     {
    274  1.1  christos       PyErr_SetString (PyExc_TypeError,
    275  1.1  christos 		       _("Cannot delete the frame filters attribute."));
    276  1.1  christos       return -1;
    277  1.1  christos     }
    278  1.1  christos 
    279  1.1  christos   if (! PyDict_Check (filters))
    280  1.1  christos     {
    281  1.1  christos       PyErr_SetString (PyExc_TypeError,
    282  1.1  christos 		       _("The frame_filters attribute must be a dictionary."));
    283  1.1  christos       return -1;
    284  1.1  christos     }
    285  1.1  christos 
    286  1.1  christos   /* Take care in case the LHS and RHS are related somehow.  */
    287  1.1  christos   tmp = self->frame_filters;
    288  1.1  christos   Py_INCREF (filters);
    289  1.1  christos   self->frame_filters = filters;
    290  1.1  christos   Py_XDECREF (tmp);
    291  1.1  christos 
    292  1.1  christos   return 0;
    293  1.1  christos }
    294  1.1  christos 
    295  1.1  christos /* Get the 'type_printers' attribute.  */
    296  1.1  christos 
    297  1.1  christos static PyObject *
    298  1.1  christos objfpy_get_type_printers (PyObject *o, void *ignore)
    299  1.1  christos {
    300  1.1  christos   objfile_object *self = (objfile_object *) o;
    301  1.1  christos 
    302  1.1  christos   Py_INCREF (self->type_printers);
    303  1.1  christos   return self->type_printers;
    304  1.1  christos }
    305  1.3  christos 
    306  1.3  christos /* Get the 'xmethods' attribute.  */
    307  1.3  christos 
    308  1.3  christos PyObject *
    309  1.3  christos objfpy_get_xmethods (PyObject *o, void *ignore)
    310  1.3  christos {
    311  1.3  christos   objfile_object *self = (objfile_object *) o;
    312  1.3  christos 
    313  1.3  christos   Py_INCREF (self->xmethods);
    314  1.3  christos   return self->xmethods;
    315  1.3  christos }
    316  1.1  christos 
    317  1.1  christos /* Set the 'type_printers' attribute.  */
    318  1.1  christos 
    319  1.1  christos static int
    320  1.1  christos objfpy_set_type_printers (PyObject *o, PyObject *value, void *ignore)
    321  1.1  christos {
    322  1.1  christos   PyObject *tmp;
    323  1.1  christos   objfile_object *self = (objfile_object *) o;
    324  1.1  christos 
    325  1.1  christos   if (! value)
    326  1.1  christos     {
    327  1.1  christos       PyErr_SetString (PyExc_TypeError,
    328  1.1  christos 		       _("Cannot delete the type_printers attribute."));
    329  1.1  christos       return -1;
    330  1.1  christos     }
    331  1.1  christos 
    332  1.1  christos   if (! PyList_Check (value))
    333  1.1  christos     {
    334  1.1  christos       PyErr_SetString (PyExc_TypeError,
    335  1.1  christos 		       _("The type_printers attribute must be a list."));
    336  1.1  christos       return -1;
    337  1.1  christos     }
    338  1.1  christos 
    339  1.1  christos   /* Take care in case the LHS and RHS are related somehow.  */
    340  1.1  christos   tmp = self->type_printers;
    341  1.1  christos   Py_INCREF (value);
    342  1.1  christos   self->type_printers = value;
    343  1.1  christos   Py_XDECREF (tmp);
    344  1.1  christos 
    345  1.1  christos   return 0;
    346  1.1  christos }
    347  1.1  christos 
    348  1.1  christos /* Implementation of gdb.Objfile.is_valid (self) -> Boolean.
    349  1.1  christos    Returns True if this object file still exists in GDB.  */
    350  1.1  christos 
    351  1.1  christos static PyObject *
    352  1.1  christos objfpy_is_valid (PyObject *self, PyObject *args)
    353  1.1  christos {
    354  1.1  christos   objfile_object *obj = (objfile_object *) self;
    355  1.1  christos 
    356  1.1  christos   if (! obj->objfile)
    357  1.1  christos     Py_RETURN_FALSE;
    358  1.1  christos 
    359  1.1  christos   Py_RETURN_TRUE;
    360  1.1  christos }
    361  1.3  christos 
    362  1.3  christos /* Implementation of gdb.Objfile.add_separate_debug_file (self) -> Boolean.  */
    363  1.3  christos 
    364  1.3  christos static PyObject *
    365  1.3  christos objfpy_add_separate_debug_file (PyObject *self, PyObject *args, PyObject *kw)
    366  1.3  christos {
    367  1.3  christos   static char *keywords[] = { "file_name", NULL };
    368  1.3  christos   objfile_object *obj = (objfile_object *) self;
    369  1.3  christos   const char *file_name;
    370  1.3  christos   int symfile_flags = 0;
    371  1.3  christos   volatile struct gdb_exception except;
    372  1.3  christos 
    373  1.3  christos   OBJFPY_REQUIRE_VALID (obj);
    374  1.3  christos 
    375  1.3  christos   if (!PyArg_ParseTupleAndKeywords (args, kw, "s", keywords, &file_name))
    376  1.3  christos     return NULL;
    377  1.3  christos 
    378  1.3  christos   TRY_CATCH (except, RETURN_MASK_ALL)
    379  1.3  christos     {
    380  1.3  christos       bfd *abfd = symfile_bfd_open (file_name);
    381  1.3  christos 
    382  1.3  christos       symbol_file_add_separate (abfd, file_name, symfile_flags, obj->objfile);
    383  1.3  christos     }
    384  1.3  christos   GDB_PY_HANDLE_EXCEPTION (except);
    385  1.3  christos 
    386  1.3  christos   Py_RETURN_NONE;
    387  1.3  christos }
    388  1.3  christos 
    389  1.3  christos /* Subroutine of gdbpy_lookup_objfile_by_build_id to simplify it.
    390  1.3  christos    Return non-zero if STRING is a potentially valid build id.  */
    391  1.3  christos 
    392  1.3  christos static int
    393  1.3  christos objfpy_build_id_ok (const char *string)
    394  1.3  christos {
    395  1.3  christos   size_t i, n = strlen (string);
    396  1.3  christos 
    397  1.3  christos   if (n % 2 != 0)
    398  1.3  christos     return 0;
    399  1.3  christos   for (i = 0; i < n; ++i)
    400  1.3  christos     {
    401  1.3  christos       if (!isxdigit (string[i]))
    402  1.3  christos 	return 0;
    403  1.3  christos     }
    404  1.3  christos   return 1;
    405  1.3  christos }
    406  1.3  christos 
    407  1.3  christos /* Subroutine of gdbpy_lookup_objfile_by_build_id to simplify it.
    408  1.3  christos    Returns non-zero if BUILD_ID matches STRING.
    409  1.3  christos    It is assumed that objfpy_build_id_ok (string) returns TRUE.  */
    410  1.3  christos 
    411  1.3  christos static int
    412  1.3  christos objfpy_build_id_matches (const struct elf_build_id *build_id,
    413  1.3  christos 			 const char *string)
    414  1.3  christos {
    415  1.3  christos   size_t i;
    416  1.3  christos 
    417  1.3  christos   if (strlen (string) != 2 * build_id->size)
    418  1.3  christos     return 0;
    419  1.3  christos 
    420  1.3  christos   for (i = 0; i < build_id->size; ++i)
    421  1.3  christos     {
    422  1.3  christos       char c1 = string[i * 2], c2 = string[i * 2 + 1];
    423  1.3  christos       int byte = (host_hex_value (c1) << 4) | host_hex_value (c2);
    424  1.3  christos 
    425  1.3  christos       if (byte != build_id->data[i])
    426  1.3  christos 	return 0;
    427  1.3  christos     }
    428  1.3  christos 
    429  1.3  christos   return 1;
    430  1.3  christos }
    431  1.3  christos 
    432  1.3  christos /* Subroutine of gdbpy_lookup_objfile to simplify it.
    433  1.3  christos    Look up an objfile by its file name.  */
    434  1.3  christos 
    435  1.3  christos static struct objfile *
    436  1.3  christos objfpy_lookup_objfile_by_name (const char *name)
    437  1.3  christos {
    438  1.3  christos   struct objfile *objfile;
    439  1.3  christos 
    440  1.3  christos   ALL_OBJFILES (objfile)
    441  1.3  christos     {
    442  1.3  christos       if ((objfile->flags & OBJF_NOT_FILENAME) != 0)
    443  1.3  christos 	continue;
    444  1.3  christos       /* Don't return separate debug files.  */
    445  1.3  christos       if (objfile->separate_debug_objfile_backlink != NULL)
    446  1.3  christos 	continue;
    447  1.3  christos       if (compare_filenames_for_search (objfile_name (objfile), name))
    448  1.3  christos 	return objfile;
    449  1.3  christos     }
    450  1.3  christos 
    451  1.3  christos   return NULL;
    452  1.3  christos }
    453  1.3  christos 
    454  1.3  christos /* Subroutine of gdbpy_lookup_objfile to simplify it.
    455  1.3  christos    Look up an objfile by its build id.  */
    456  1.3  christos 
    457  1.3  christos static struct objfile *
    458  1.3  christos objfpy_lookup_objfile_by_build_id (const char *build_id)
    459  1.3  christos {
    460  1.3  christos   struct objfile *objfile;
    461  1.3  christos 
    462  1.3  christos   ALL_OBJFILES (objfile)
    463  1.3  christos     {
    464  1.3  christos       const struct elf_build_id *obfd_build_id;
    465  1.3  christos 
    466  1.3  christos       if (objfile->obfd == NULL)
    467  1.3  christos 	continue;
    468  1.3  christos       /* Don't return separate debug files.  */
    469  1.3  christos       if (objfile->separate_debug_objfile_backlink != NULL)
    470  1.3  christos 	continue;
    471  1.3  christos       obfd_build_id = build_id_bfd_get (objfile->obfd);
    472  1.3  christos       if (obfd_build_id == NULL)
    473  1.3  christos 	continue;
    474  1.3  christos       if (objfpy_build_id_matches (obfd_build_id, build_id))
    475  1.3  christos 	return objfile;
    476  1.3  christos     }
    477  1.3  christos 
    478  1.3  christos   return NULL;
    479  1.3  christos }
    480  1.3  christos 
    481  1.3  christos /* Implementation of gdb.lookup_objfile.  */
    482  1.3  christos 
    483  1.3  christos PyObject *
    484  1.3  christos gdbpy_lookup_objfile (PyObject *self, PyObject *args, PyObject *kw)
    485  1.3  christos {
    486  1.3  christos   static char *keywords[] = { "name", "by_build_id", NULL };
    487  1.3  christos   const char *name;
    488  1.3  christos   PyObject *by_build_id_obj = NULL;
    489  1.3  christos   int by_build_id;
    490  1.3  christos   struct objfile *objfile;
    491  1.3  christos 
    492  1.3  christos   if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O!", keywords,
    493  1.3  christos 				     &name, &PyBool_Type, &by_build_id_obj))
    494  1.3  christos     return NULL;
    495  1.3  christos 
    496  1.3  christos   by_build_id = 0;
    497  1.3  christos   if (by_build_id_obj != NULL)
    498  1.3  christos     {
    499  1.3  christos       int cmp = PyObject_IsTrue (by_build_id_obj);
    500  1.3  christos 
    501  1.3  christos       if (cmp < 0)
    502  1.3  christos 	return NULL;
    503  1.3  christos       by_build_id = cmp;
    504  1.3  christos     }
    505  1.3  christos 
    506  1.3  christos   if (by_build_id)
    507  1.3  christos     {
    508  1.3  christos       if (!objfpy_build_id_ok (name))
    509  1.3  christos 	{
    510  1.3  christos 	  PyErr_SetString (PyExc_TypeError, _("Not a valid build id."));
    511  1.3  christos 	  return NULL;
    512  1.3  christos 	}
    513  1.3  christos       objfile = objfpy_lookup_objfile_by_build_id (name);
    514  1.3  christos     }
    515  1.3  christos   else
    516  1.3  christos     objfile = objfpy_lookup_objfile_by_name (name);
    517  1.3  christos 
    518  1.3  christos   if (objfile != NULL)
    519  1.3  christos     {
    520  1.3  christos       PyObject *result = objfile_to_objfile_object (objfile);
    521  1.3  christos 
    522  1.3  christos       Py_XINCREF (result);
    523  1.3  christos       return result;
    524  1.3  christos     }
    525  1.3  christos 
    526  1.3  christos   PyErr_SetString (PyExc_ValueError, _("Objfile not found."));
    527  1.3  christos   return NULL;
    528  1.3  christos }
    529  1.1  christos 
    530  1.1  christos 
    531  1.1  christos 
    533  1.1  christos /* Clear the OBJFILE pointer in an Objfile object and remove the
    534  1.1  christos    reference.  */
    535  1.1  christos static void
    536  1.1  christos py_free_objfile (struct objfile *objfile, void *datum)
    537  1.1  christos {
    538  1.1  christos   struct cleanup *cleanup;
    539  1.1  christos   objfile_object *object = datum;
    540  1.1  christos 
    541  1.1  christos   cleanup = ensure_python_env (get_objfile_arch (objfile), current_language);
    542  1.1  christos   object->objfile = NULL;
    543  1.1  christos   Py_DECREF ((PyObject *) object);
    544  1.1  christos   do_cleanups (cleanup);
    545  1.1  christos }
    546  1.1  christos 
    547  1.1  christos /* Return a borrowed reference to the Python object of type Objfile
    548  1.1  christos    representing OBJFILE.  If the object has already been created,
    549  1.3  christos    return it.  Otherwise, create it.  Return NULL and set the Python
    550  1.1  christos    error on failure.  */
    551  1.1  christos 
    552  1.1  christos PyObject *
    553  1.1  christos objfile_to_objfile_object (struct objfile *objfile)
    554  1.1  christos {
    555  1.1  christos   objfile_object *object;
    556  1.1  christos 
    557  1.1  christos   object = objfile_data (objfile, objfpy_objfile_data_key);
    558  1.1  christos   if (!object)
    559  1.1  christos     {
    560  1.1  christos       object = PyObject_New (objfile_object, &objfile_object_type);
    561  1.3  christos       if (object)
    562  1.1  christos 	{
    563  1.1  christos 	  if (!objfpy_initialize (object))
    564  1.1  christos 	    {
    565  1.1  christos 	      Py_DECREF (object);
    566  1.1  christos 	      return NULL;
    567  1.3  christos 	    }
    568  1.1  christos 
    569  1.1  christos 	  object->objfile = objfile;
    570  1.1  christos 	  set_objfile_data (objfile, objfpy_objfile_data_key, object);
    571  1.1  christos 	}
    572  1.1  christos     }
    573  1.1  christos 
    574  1.1  christos   return (PyObject *) object;
    575  1.1  christos }
    576  1.1  christos 
    577  1.1  christos int
    578  1.1  christos gdbpy_initialize_objfile (void)
    579  1.1  christos {
    580  1.1  christos   objfpy_objfile_data_key
    581  1.1  christos     = register_objfile_data_with_cleanup (NULL, py_free_objfile);
    582  1.1  christos 
    583  1.1  christos   if (PyType_Ready (&objfile_object_type) < 0)
    584  1.1  christos     return -1;
    585  1.1  christos 
    586  1.1  christos   return gdb_pymodule_addobject (gdb_module, "Objfile",
    587  1.1  christos 				 (PyObject *) &objfile_object_type);
    588  1.1  christos }
    589  1.1  christos 
    590  1.1  christos 
    591  1.1  christos 
    593  1.1  christos static PyMethodDef objfile_object_methods[] =
    594  1.1  christos {
    595  1.1  christos   { "is_valid", objfpy_is_valid, METH_NOARGS,
    596  1.3  christos     "is_valid () -> Boolean.\n\
    597  1.3  christos Return true if this object file is valid, false if not." },
    598  1.3  christos 
    599  1.3  christos   { "add_separate_debug_file", (PyCFunction) objfpy_add_separate_debug_file,
    600  1.3  christos     METH_VARARGS | METH_KEYWORDS,
    601  1.1  christos     "add_separate_debug_file (file_name).\n\
    602  1.1  christos Add FILE_NAME to the list of files containing debug info for the objfile." },
    603  1.1  christos 
    604  1.1  christos   { NULL }
    605  1.1  christos };
    606  1.3  christos 
    607  1.3  christos static PyGetSetDef objfile_getset[] =
    608  1.1  christos {
    609  1.1  christos   { "__dict__", gdb_py_generic_dict, NULL,
    610  1.3  christos     "The __dict__ for this objfile.", &objfile_object_type },
    611  1.3  christos   { "filename", objfpy_get_filename, NULL,
    612  1.3  christos     "The objfile's filename, or None.", NULL },
    613  1.3  christos   { "owner", objfpy_get_owner, NULL,
    614  1.3  christos     "The objfile owner of separate debug info objfiles, or None.",
    615  1.3  christos     NULL },
    616  1.3  christos   { "build_id", objfpy_get_build_id, NULL,
    617  1.1  christos     "The objfile's build id, or None.", NULL },
    618  1.1  christos   { "progspace", objfpy_get_progspace, NULL,
    619  1.1  christos     "The objfile's progspace, or None.", NULL },
    620  1.1  christos   { "pretty_printers", objfpy_get_printers, objfpy_set_printers,
    621  1.1  christos     "Pretty printers.", NULL },
    622  1.1  christos   { "frame_filters", objfpy_get_frame_filters,
    623  1.3  christos     objfpy_set_frame_filters, "Frame Filters.", NULL },
    624  1.3  christos   { "type_printers", objfpy_get_type_printers, objfpy_set_type_printers,
    625  1.1  christos     "Type printers.", NULL },
    626  1.1  christos   { "xmethods", objfpy_get_xmethods, NULL,
    627  1.1  christos     "Debug methods.", NULL },
    628  1.1  christos   { NULL }
    629  1.1  christos };
    630  1.1  christos 
    631  1.1  christos static PyTypeObject objfile_object_type =
    632  1.1  christos {
    633  1.1  christos   PyVarObject_HEAD_INIT (NULL, 0)
    634  1.1  christos   "gdb.Objfile",		  /*tp_name*/
    635  1.1  christos   sizeof (objfile_object),	  /*tp_basicsize*/
    636  1.1  christos   0,				  /*tp_itemsize*/
    637  1.1  christos   objfpy_dealloc,		  /*tp_dealloc*/
    638  1.1  christos   0,				  /*tp_print*/
    639  1.1  christos   0,				  /*tp_getattr*/
    640  1.1  christos   0,				  /*tp_setattr*/
    641  1.1  christos   0,				  /*tp_compare*/
    642  1.1  christos   0,				  /*tp_repr*/
    643  1.1  christos   0,				  /*tp_as_number*/
    644  1.1  christos   0,				  /*tp_as_sequence*/
    645  1.1  christos   0,				  /*tp_as_mapping*/
    646  1.1  christos   0,				  /*tp_hash */
    647  1.1  christos   0,				  /*tp_call*/
    648  1.1  christos   0,				  /*tp_str*/
    649  1.1  christos   0,				  /*tp_getattro*/
    650  1.1  christos   0,				  /*tp_setattro*/
    651  1.1  christos   0,				  /*tp_as_buffer*/
    652  1.1  christos   Py_TPFLAGS_DEFAULT,		  /*tp_flags*/
    653  1.1  christos   "GDB objfile object",		  /* tp_doc */
    654  1.1  christos   0,				  /* tp_traverse */
    655  1.1  christos   0,				  /* tp_clear */
    656  1.1  christos   0,				  /* tp_richcompare */
    657  1.1  christos   0,				  /* tp_weaklistoffset */
    658  1.1  christos   0,				  /* tp_iter */
    659  1.1  christos   0,				  /* tp_iternext */
    660  1.1  christos   objfile_object_methods,	  /* tp_methods */
    661  1.1  christos   0,				  /* tp_members */
    662  1.1  christos   objfile_getset,		  /* tp_getset */
    663  1.1  christos   0,				  /* tp_base */
    664  1.3  christos   0,				  /* tp_dict */
    665  1.1  christos   0,				  /* tp_descr_get */
    666  1.1  christos   0,				  /* tp_descr_set */
    667  1.1  christos   offsetof (objfile_object, dict), /* tp_dictoffset */
    668  1.1  christos   0,				  /* tp_init */
    669                  0,				  /* tp_alloc */
    670                  objfpy_new,			  /* tp_new */
    671                };
    672