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