New python attribute gdb.Objfile.build_id.
[deliverable/binutils-gdb.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 \f
338
339 /* Clear the OBJFILE pointer in an Objfile object and remove the
340 reference. */
341 static void
342 py_free_objfile (struct objfile *objfile, void *datum)
343 {
344 struct cleanup *cleanup;
345 objfile_object *object = datum;
346
347 cleanup = ensure_python_env (get_objfile_arch (objfile), current_language);
348 object->objfile = NULL;
349 Py_DECREF ((PyObject *) object);
350 do_cleanups (cleanup);
351 }
352
353 /* Return a borrowed reference to the Python object of type Objfile
354 representing OBJFILE. If the object has already been created,
355 return it. Otherwise, create it. Return NULL and set the Python
356 error on failure. */
357
358 PyObject *
359 objfile_to_objfile_object (struct objfile *objfile)
360 {
361 objfile_object *object;
362
363 object = objfile_data (objfile, objfpy_objfile_data_key);
364 if (!object)
365 {
366 object = PyObject_New (objfile_object, &objfile_object_type);
367 if (object)
368 {
369 if (!objfpy_initialize (object))
370 {
371 Py_DECREF (object);
372 return NULL;
373 }
374
375 object->objfile = objfile;
376 set_objfile_data (objfile, objfpy_objfile_data_key, object);
377 }
378 }
379
380 return (PyObject *) object;
381 }
382
383 int
384 gdbpy_initialize_objfile (void)
385 {
386 objfpy_objfile_data_key
387 = register_objfile_data_with_cleanup (NULL, py_free_objfile);
388
389 if (PyType_Ready (&objfile_object_type) < 0)
390 return -1;
391
392 return gdb_pymodule_addobject (gdb_module, "Objfile",
393 (PyObject *) &objfile_object_type);
394 }
395
396 \f
397
398 static PyMethodDef objfile_object_methods[] =
399 {
400 { "is_valid", objfpy_is_valid, METH_NOARGS,
401 "is_valid () -> Boolean.\n\
402 Return true if this object file is valid, false if not." },
403
404 { NULL }
405 };
406
407 static PyGetSetDef objfile_getset[] =
408 {
409 { "__dict__", gdb_py_generic_dict, NULL,
410 "The __dict__ for this objfile.", &objfile_object_type },
411 { "filename", objfpy_get_filename, NULL,
412 "The objfile's filename, or None.", NULL },
413 { "build_id", objfpy_get_build_id, NULL,
414 "The objfile's build id, or None.", NULL },
415 { "progspace", objfpy_get_progspace, NULL,
416 "The objfile's progspace, or None.", NULL },
417 { "pretty_printers", objfpy_get_printers, objfpy_set_printers,
418 "Pretty printers.", NULL },
419 { "frame_filters", objfpy_get_frame_filters,
420 objfpy_set_frame_filters, "Frame Filters.", NULL },
421 { "type_printers", objfpy_get_type_printers, objfpy_set_type_printers,
422 "Type printers.", NULL },
423 { "xmethods", objfpy_get_xmethods, NULL,
424 "Debug methods.", NULL },
425 { NULL }
426 };
427
428 static PyTypeObject objfile_object_type =
429 {
430 PyVarObject_HEAD_INIT (NULL, 0)
431 "gdb.Objfile", /*tp_name*/
432 sizeof (objfile_object), /*tp_basicsize*/
433 0, /*tp_itemsize*/
434 objfpy_dealloc, /*tp_dealloc*/
435 0, /*tp_print*/
436 0, /*tp_getattr*/
437 0, /*tp_setattr*/
438 0, /*tp_compare*/
439 0, /*tp_repr*/
440 0, /*tp_as_number*/
441 0, /*tp_as_sequence*/
442 0, /*tp_as_mapping*/
443 0, /*tp_hash */
444 0, /*tp_call*/
445 0, /*tp_str*/
446 0, /*tp_getattro*/
447 0, /*tp_setattro*/
448 0, /*tp_as_buffer*/
449 Py_TPFLAGS_DEFAULT, /*tp_flags*/
450 "GDB objfile object", /* tp_doc */
451 0, /* tp_traverse */
452 0, /* tp_clear */
453 0, /* tp_richcompare */
454 0, /* tp_weaklistoffset */
455 0, /* tp_iter */
456 0, /* tp_iternext */
457 objfile_object_methods, /* tp_methods */
458 0, /* tp_members */
459 objfile_getset, /* tp_getset */
460 0, /* tp_base */
461 0, /* tp_dict */
462 0, /* tp_descr_get */
463 0, /* tp_descr_set */
464 offsetof (objfile_object, dict), /* tp_dictoffset */
465 0, /* tp_init */
466 0, /* tp_alloc */
467 objfpy_new, /* tp_new */
468 };
This page took 0.040053 seconds and 5 git commands to generate.