292d310801fdd4b4f4e78af53dcfe1982d9b8fe9
[external/binutils.git] / gdb / python / py-objfile.c
1 /* Python interface to objfiles.
2
3    Copyright (C) 2008-2014 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
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.
11
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.
16
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/>.  */
19
20 #include "defs.h"
21 #include "python-internal.h"
22 #include "charset.h"
23 #include "objfiles.h"
24 #include "language.h"
25 #include "build-id.h"
26 #include "elf-bfd.h"
27
28 typedef struct
29 {
30   PyObject_HEAD
31
32   /* The corresponding objfile.  */
33   struct objfile *objfile;
34
35   /* Dictionary holding user-added attributes.
36      This is the __dict__ attribute of the object.  */
37   PyObject *dict;
38
39   /* The pretty-printer list of functions.  */
40   PyObject *printers;
41
42   /* The frame filter list of functions.  */
43   PyObject *frame_filters;
44   /* The type-printer list.  */
45   PyObject *type_printers;
46
47   /* The debug method matcher list.  */
48   PyObject *xmethods;
49 } objfile_object;
50
51 static PyTypeObject objfile_object_type
52     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("objfile_object");
53
54 static const struct objfile_data *objfpy_objfile_data_key;
55
56 /* Require that OBJF be a valid objfile.  */
57 #define OBJFPY_REQUIRE_VALID(obj)                               \
58   do {                                                          \
59     if (!(obj)->objfile)                                        \
60       {                                                         \
61         PyErr_SetString (PyExc_RuntimeError,                    \
62                          _("Objfile no longer exists."));       \
63         return NULL;                                            \
64       }                                                         \
65   } while (0)
66
67 \f
68
69 /* An Objfile method which returns the objfile's file name, or None.  */
70
71 static PyObject *
72 objfpy_get_filename (PyObject *self, void *closure)
73 {
74   objfile_object *obj = (objfile_object *) self;
75
76   if (obj->objfile)
77     return PyString_Decode (objfile_name (obj->objfile),
78                             strlen (objfile_name (obj->objfile)),
79                             host_charset (), NULL);
80   Py_RETURN_NONE;
81 }
82
83 /* An Objfile method which returns the objfile's build id, or None.  */
84
85 static PyObject *
86 objfpy_get_build_id (PyObject *self, void *closure)
87 {
88   objfile_object *obj = (objfile_object *) self;
89   struct objfile *objfile = obj->objfile;
90   const struct elf_build_id *build_id = NULL;
91   volatile struct gdb_exception except;
92
93   OBJFPY_REQUIRE_VALID (obj);
94
95   TRY_CATCH (except, RETURN_MASK_ALL)
96     {
97       build_id = build_id_bfd_get (objfile->obfd);
98     }
99   GDB_PY_HANDLE_EXCEPTION (except);
100
101   if (build_id != NULL)
102     {
103       char *hex_form = make_hex_string (build_id->data, build_id->size);
104       PyObject *result;
105
106       result = PyString_Decode (hex_form, strlen (hex_form),
107                                 host_charset (), NULL);
108       xfree (hex_form);
109       return result;
110     }
111
112   Py_RETURN_NONE;
113 }
114
115 /* An Objfile method which returns the objfile's progspace, or None.  */
116
117 static PyObject *
118 objfpy_get_progspace (PyObject *self, void *closure)
119 {
120   objfile_object *obj = (objfile_object *) self;
121
122   if (obj->objfile)
123     {
124       PyObject *pspace =  pspace_to_pspace_object (obj->objfile->pspace);
125
126       Py_XINCREF (pspace);
127       return pspace;
128     }
129
130   Py_RETURN_NONE;
131 }
132
133 static void
134 objfpy_dealloc (PyObject *o)
135 {
136   objfile_object *self = (objfile_object *) o;
137
138   Py_XDECREF (self->dict);
139   Py_XDECREF (self->printers);
140   Py_XDECREF (self->frame_filters);
141   Py_XDECREF (self->type_printers);
142   Py_XDECREF (self->xmethods);
143   Py_TYPE (self)->tp_free (self);
144 }
145
146 /* Initialize an objfile_object.
147    The result is a boolean indicating success.  */
148
149 static int
150 objfpy_initialize (objfile_object *self)
151 {
152   self->objfile = NULL;
153   self->dict = NULL;
154
155   self->printers = PyList_New (0);
156   if (self->printers == NULL)
157     return 0;
158
159   self->frame_filters = PyDict_New ();
160   if (self->frame_filters == NULL)
161     return 0;
162
163   self->type_printers = PyList_New (0);
164   if (self->type_printers == NULL)
165     return 0;
166
167   self->xmethods = PyList_New (0);
168   if (self->xmethods == NULL)
169     return 0;
170
171   return 1;
172 }
173
174 static PyObject *
175 objfpy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
176 {
177   objfile_object *self = (objfile_object *) type->tp_alloc (type, 0);
178
179   if (self)
180     {
181       if (!objfpy_initialize (self))
182         {
183           Py_DECREF (self);
184           return NULL;
185         }
186     }
187
188   return (PyObject *) self;
189 }
190
191 PyObject *
192 objfpy_get_printers (PyObject *o, void *ignore)
193 {
194   objfile_object *self = (objfile_object *) o;
195
196   Py_INCREF (self->printers);
197   return self->printers;
198 }
199
200 static int
201 objfpy_set_printers (PyObject *o, PyObject *value, void *ignore)
202 {
203   PyObject *tmp;
204   objfile_object *self = (objfile_object *) o;
205
206   if (! value)
207     {
208       PyErr_SetString (PyExc_TypeError,
209                        _("Cannot delete the pretty_printers attribute."));
210       return -1;
211     }
212
213   if (! PyList_Check (value))
214     {
215       PyErr_SetString (PyExc_TypeError,
216                        _("The pretty_printers attribute must be a list."));
217       return -1;
218     }
219
220   /* Take care in case the LHS and RHS are related somehow.  */
221   tmp = self->printers;
222   Py_INCREF (value);
223   self->printers = value;
224   Py_XDECREF (tmp);
225
226   return 0;
227 }
228
229 /* Return the Python dictionary attribute containing frame filters for
230    this object file.  */
231 PyObject *
232 objfpy_get_frame_filters (PyObject *o, void *ignore)
233 {
234   objfile_object *self = (objfile_object *) o;
235
236   Py_INCREF (self->frame_filters);
237   return self->frame_filters;
238 }
239
240 /* Set this object file's frame filters dictionary to FILTERS.  */
241 static int
242 objfpy_set_frame_filters (PyObject *o, PyObject *filters, void *ignore)
243 {
244   PyObject *tmp;
245   objfile_object *self = (objfile_object *) o;
246
247   if (! filters)
248     {
249       PyErr_SetString (PyExc_TypeError,
250                        _("Cannot delete the frame filters attribute."));
251       return -1;
252     }
253
254   if (! PyDict_Check (filters))
255     {
256       PyErr_SetString (PyExc_TypeError,
257                        _("The frame_filters attribute must be a dictionary."));
258       return -1;
259     }
260
261   /* Take care in case the LHS and RHS are related somehow.  */
262   tmp = self->frame_filters;
263   Py_INCREF (filters);
264   self->frame_filters = filters;
265   Py_XDECREF (tmp);
266
267   return 0;
268 }
269
270 /* Get the 'type_printers' attribute.  */
271
272 static PyObject *
273 objfpy_get_type_printers (PyObject *o, void *ignore)
274 {
275   objfile_object *self = (objfile_object *) o;
276
277   Py_INCREF (self->type_printers);
278   return self->type_printers;
279 }
280
281 /* Get the 'xmethods' attribute.  */
282
283 PyObject *
284 objfpy_get_xmethods (PyObject *o, void *ignore)
285 {
286   objfile_object *self = (objfile_object *) o;
287
288   Py_INCREF (self->xmethods);
289   return self->xmethods;
290 }
291
292 /* Set the 'type_printers' attribute.  */
293
294 static int
295 objfpy_set_type_printers (PyObject *o, PyObject *value, void *ignore)
296 {
297   PyObject *tmp;
298   objfile_object *self = (objfile_object *) o;
299
300   if (! value)
301     {
302       PyErr_SetString (PyExc_TypeError,
303                        _("Cannot delete the type_printers attribute."));
304       return -1;
305     }
306
307   if (! PyList_Check (value))
308     {
309       PyErr_SetString (PyExc_TypeError,
310                        _("The type_printers attribute must be a list."));
311       return -1;
312     }
313
314   /* Take care in case the LHS and RHS are related somehow.  */
315   tmp = self->type_printers;
316   Py_INCREF (value);
317   self->type_printers = value;
318   Py_XDECREF (tmp);
319
320   return 0;
321 }
322
323 /* Implementation of gdb.Objfile.is_valid (self) -> Boolean.
324    Returns True if this object file still exists in GDB.  */
325
326 static PyObject *
327 objfpy_is_valid (PyObject *self, PyObject *args)
328 {
329   objfile_object *obj = (objfile_object *) self;
330
331   if (! obj->objfile)
332     Py_RETURN_FALSE;
333
334   Py_RETURN_TRUE;
335 }
336
337 /* Implementation of gdb.Objfile.add_separate_debug_file (self) -> Boolean.  */
338
339 static PyObject *
340 objfpy_add_separate_debug_file (PyObject *self, PyObject *args, PyObject *kw)
341 {
342   static char *keywords[] = { "file_name", NULL };
343   objfile_object *obj = (objfile_object *) self;
344   const char *file_name;
345   int symfile_flags = 0;
346   volatile struct gdb_exception except;
347
348   OBJFPY_REQUIRE_VALID (obj);
349
350   if (!PyArg_ParseTupleAndKeywords (args, kw, "s", keywords, &file_name))
351     return NULL;
352
353   TRY_CATCH (except, RETURN_MASK_ALL)
354     {
355       bfd *abfd = symfile_bfd_open (file_name);
356
357       symbol_file_add_separate (abfd, file_name, symfile_flags, obj->objfile);
358     }
359   GDB_PY_HANDLE_EXCEPTION (except);
360
361   Py_RETURN_NONE;
362 }
363
364 \f
365
366 /* Clear the OBJFILE pointer in an Objfile object and remove the
367    reference.  */
368 static void
369 py_free_objfile (struct objfile *objfile, void *datum)
370 {
371   struct cleanup *cleanup;
372   objfile_object *object = datum;
373
374   cleanup = ensure_python_env (get_objfile_arch (objfile), current_language);
375   object->objfile = NULL;
376   Py_DECREF ((PyObject *) object);
377   do_cleanups (cleanup);
378 }
379
380 /* Return a borrowed reference to the Python object of type Objfile
381    representing OBJFILE.  If the object has already been created,
382    return it.  Otherwise, create it.  Return NULL and set the Python
383    error on failure.  */
384
385 PyObject *
386 objfile_to_objfile_object (struct objfile *objfile)
387 {
388   objfile_object *object;
389
390   object = objfile_data (objfile, objfpy_objfile_data_key);
391   if (!object)
392     {
393       object = PyObject_New (objfile_object, &objfile_object_type);
394       if (object)
395         {
396           if (!objfpy_initialize (object))
397             {
398               Py_DECREF (object);
399               return NULL;
400             }
401
402           object->objfile = objfile;
403           set_objfile_data (objfile, objfpy_objfile_data_key, object);
404         }
405     }
406
407   return (PyObject *) object;
408 }
409
410 int
411 gdbpy_initialize_objfile (void)
412 {
413   objfpy_objfile_data_key
414     = register_objfile_data_with_cleanup (NULL, py_free_objfile);
415
416   if (PyType_Ready (&objfile_object_type) < 0)
417     return -1;
418
419   return gdb_pymodule_addobject (gdb_module, "Objfile",
420                                  (PyObject *) &objfile_object_type);
421 }
422
423 \f
424
425 static PyMethodDef objfile_object_methods[] =
426 {
427   { "is_valid", objfpy_is_valid, METH_NOARGS,
428     "is_valid () -> Boolean.\n\
429 Return true if this object file is valid, false if not." },
430
431   { "add_separate_debug_file", (PyCFunction) objfpy_add_separate_debug_file,
432     METH_VARARGS | METH_KEYWORDS,
433     "add_separate_debug_file (file_name).\n\
434 Add FILE_NAME to the list of files containing debug info for the objfile." },
435
436   { NULL }
437 };
438
439 static PyGetSetDef objfile_getset[] =
440 {
441   { "__dict__", gdb_py_generic_dict, NULL,
442     "The __dict__ for this objfile.", &objfile_object_type },
443   { "filename", objfpy_get_filename, NULL,
444     "The objfile's filename, or None.", NULL },
445   { "build_id", objfpy_get_build_id, NULL,
446     "The objfile's build id, or None.", NULL },
447   { "progspace", objfpy_get_progspace, NULL,
448     "The objfile's progspace, or None.", NULL },
449   { "pretty_printers", objfpy_get_printers, objfpy_set_printers,
450     "Pretty printers.", NULL },
451   { "frame_filters", objfpy_get_frame_filters,
452     objfpy_set_frame_filters, "Frame Filters.", NULL },
453   { "type_printers", objfpy_get_type_printers, objfpy_set_type_printers,
454     "Type printers.", NULL },
455   { "xmethods", objfpy_get_xmethods, NULL,
456     "Debug methods.", NULL },
457   { NULL }
458 };
459
460 static PyTypeObject objfile_object_type =
461 {
462   PyVarObject_HEAD_INIT (NULL, 0)
463   "gdb.Objfile",                  /*tp_name*/
464   sizeof (objfile_object),        /*tp_basicsize*/
465   0,                              /*tp_itemsize*/
466   objfpy_dealloc,                 /*tp_dealloc*/
467   0,                              /*tp_print*/
468   0,                              /*tp_getattr*/
469   0,                              /*tp_setattr*/
470   0,                              /*tp_compare*/
471   0,                              /*tp_repr*/
472   0,                              /*tp_as_number*/
473   0,                              /*tp_as_sequence*/
474   0,                              /*tp_as_mapping*/
475   0,                              /*tp_hash */
476   0,                              /*tp_call*/
477   0,                              /*tp_str*/
478   0,                              /*tp_getattro*/
479   0,                              /*tp_setattro*/
480   0,                              /*tp_as_buffer*/
481   Py_TPFLAGS_DEFAULT,             /*tp_flags*/
482   "GDB objfile object",           /* tp_doc */
483   0,                              /* tp_traverse */
484   0,                              /* tp_clear */
485   0,                              /* tp_richcompare */
486   0,                              /* tp_weaklistoffset */
487   0,                              /* tp_iter */
488   0,                              /* tp_iternext */
489   objfile_object_methods,         /* tp_methods */
490   0,                              /* tp_members */
491   objfile_getset,                 /* tp_getset */
492   0,                              /* tp_base */
493   0,                              /* tp_dict */
494   0,                              /* tp_descr_get */
495   0,                              /* tp_descr_set */
496   offsetof (objfile_object, dict), /* tp_dictoffset */
497   0,                              /* tp_init */
498   0,                              /* tp_alloc */
499   objfpy_new,                     /* tp_new */
500 };