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