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