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