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