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