xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/python/py-objfile.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
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 "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 
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 PyString_Decode (objfile_name (obj->objfile),
82 			    strlen (objfile_name (obj->objfile)),
83 			    host_charset (), NULL);
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 PyString_Decode (username, strlen (username),
100 			      host_charset (), NULL);
101     }
102 
103   Py_RETURN_NONE;
104 }
105 
106 /* If SELF is a separate debug-info file, return the "backlink" field.
107    Otherwise return None.  */
108 
109 static PyObject *
110 objfpy_get_owner (PyObject *self, void *closure)
111 {
112   objfile_object *obj = (objfile_object *) self;
113   struct objfile *objfile = obj->objfile;
114   struct objfile *owner;
115 
116   OBJFPY_REQUIRE_VALID (obj);
117 
118   owner = objfile->separate_debug_objfile_backlink;
119   if (owner != NULL)
120     {
121       PyObject *result = objfile_to_objfile_object (owner);
122 
123       Py_XINCREF (result);
124       return result;
125     }
126   Py_RETURN_NONE;
127 }
128 
129 /* An Objfile method which returns the objfile's build id, or None.  */
130 
131 static PyObject *
132 objfpy_get_build_id (PyObject *self, void *closure)
133 {
134   objfile_object *obj = (objfile_object *) self;
135   struct objfile *objfile = obj->objfile;
136   const struct bfd_build_id *build_id = NULL;
137 
138   OBJFPY_REQUIRE_VALID (obj);
139 
140   TRY
141     {
142       build_id = build_id_bfd_get (objfile->obfd);
143     }
144   CATCH (except, RETURN_MASK_ALL)
145     {
146       GDB_PY_HANDLE_EXCEPTION (except);
147     }
148   END_CATCH
149 
150   if (build_id != NULL)
151     {
152       char *hex_form = make_hex_string (build_id->data, build_id->size);
153       PyObject *result;
154 
155       result = PyString_Decode (hex_form, strlen (hex_form),
156 				host_charset (), NULL);
157       xfree (hex_form);
158       return result;
159     }
160 
161   Py_RETURN_NONE;
162 }
163 
164 /* An Objfile method which returns the objfile's progspace, or None.  */
165 
166 static PyObject *
167 objfpy_get_progspace (PyObject *self, void *closure)
168 {
169   objfile_object *obj = (objfile_object *) self;
170 
171   if (obj->objfile)
172     {
173       PyObject *pspace =  pspace_to_pspace_object (obj->objfile->pspace);
174 
175       Py_XINCREF (pspace);
176       return pspace;
177     }
178 
179   Py_RETURN_NONE;
180 }
181 
182 static void
183 objfpy_dealloc (PyObject *o)
184 {
185   objfile_object *self = (objfile_object *) o;
186 
187   Py_XDECREF (self->dict);
188   Py_XDECREF (self->printers);
189   Py_XDECREF (self->frame_filters);
190   Py_XDECREF (self->frame_unwinders);
191   Py_XDECREF (self->type_printers);
192   Py_XDECREF (self->xmethods);
193   Py_TYPE (self)->tp_free (self);
194 }
195 
196 /* Initialize an objfile_object.
197    The result is a boolean indicating success.  */
198 
199 static int
200 objfpy_initialize (objfile_object *self)
201 {
202   self->objfile = NULL;
203   self->dict = NULL;
204 
205   self->printers = PyList_New (0);
206   if (self->printers == NULL)
207     return 0;
208 
209   self->frame_filters = PyDict_New ();
210   if (self->frame_filters == NULL)
211     return 0;
212 
213   self->frame_unwinders = PyList_New (0);
214   if (self->frame_unwinders == NULL)
215     return 0;
216 
217   self->type_printers = PyList_New (0);
218   if (self->type_printers == NULL)
219     return 0;
220 
221   self->xmethods = PyList_New (0);
222   if (self->xmethods == NULL)
223     return 0;
224 
225   return 1;
226 }
227 
228 static PyObject *
229 objfpy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
230 {
231   objfile_object *self = (objfile_object *) type->tp_alloc (type, 0);
232 
233   if (self)
234     {
235       if (!objfpy_initialize (self))
236 	{
237 	  Py_DECREF (self);
238 	  return NULL;
239 	}
240     }
241 
242   return (PyObject *) self;
243 }
244 
245 PyObject *
246 objfpy_get_printers (PyObject *o, void *ignore)
247 {
248   objfile_object *self = (objfile_object *) o;
249 
250   Py_INCREF (self->printers);
251   return self->printers;
252 }
253 
254 static int
255 objfpy_set_printers (PyObject *o, PyObject *value, void *ignore)
256 {
257   PyObject *tmp;
258   objfile_object *self = (objfile_object *) o;
259 
260   if (! value)
261     {
262       PyErr_SetString (PyExc_TypeError,
263 		       _("Cannot delete the pretty_printers attribute."));
264       return -1;
265     }
266 
267   if (! PyList_Check (value))
268     {
269       PyErr_SetString (PyExc_TypeError,
270 		       _("The pretty_printers attribute must be a list."));
271       return -1;
272     }
273 
274   /* Take care in case the LHS and RHS are related somehow.  */
275   tmp = self->printers;
276   Py_INCREF (value);
277   self->printers = value;
278   Py_XDECREF (tmp);
279 
280   return 0;
281 }
282 
283 /* Return the Python dictionary attribute containing frame filters for
284    this object file.  */
285 PyObject *
286 objfpy_get_frame_filters (PyObject *o, void *ignore)
287 {
288   objfile_object *self = (objfile_object *) o;
289 
290   Py_INCREF (self->frame_filters);
291   return self->frame_filters;
292 }
293 
294 /* Set this object file's frame filters dictionary to FILTERS.  */
295 static int
296 objfpy_set_frame_filters (PyObject *o, PyObject *filters, void *ignore)
297 {
298   PyObject *tmp;
299   objfile_object *self = (objfile_object *) o;
300 
301   if (! filters)
302     {
303       PyErr_SetString (PyExc_TypeError,
304 		       _("Cannot delete the frame filters attribute."));
305       return -1;
306     }
307 
308   if (! PyDict_Check (filters))
309     {
310       PyErr_SetString (PyExc_TypeError,
311 		       _("The frame_filters attribute must be a dictionary."));
312       return -1;
313     }
314 
315   /* Take care in case the LHS and RHS are related somehow.  */
316   tmp = self->frame_filters;
317   Py_INCREF (filters);
318   self->frame_filters = filters;
319   Py_XDECREF (tmp);
320 
321   return 0;
322 }
323 
324 /* Return the frame unwinders attribute for this object file.  */
325 
326 PyObject *
327 objfpy_get_frame_unwinders (PyObject *o, void *ignore)
328 {
329   objfile_object *self = (objfile_object *) o;
330 
331   Py_INCREF (self->frame_unwinders);
332   return self->frame_unwinders;
333 }
334 
335 /* Set this object file's frame unwinders list to UNWINDERS.  */
336 
337 static int
338 objfpy_set_frame_unwinders (PyObject *o, PyObject *unwinders, void *ignore)
339 {
340   PyObject *tmp;
341   objfile_object *self = (objfile_object *) o;
342 
343   if (!unwinders)
344     {
345       PyErr_SetString (PyExc_TypeError,
346 		       _("Cannot delete the frame unwinders attribute."));
347       return -1;
348     }
349 
350   if (!PyList_Check (unwinders))
351     {
352       PyErr_SetString (PyExc_TypeError,
353 		       _("The frame_unwinders attribute must be a list."));
354       return -1;
355     }
356 
357   /* Take care in case the LHS and RHS are related somehow.  */
358   tmp = self->frame_unwinders;
359   Py_INCREF (unwinders);
360   self->frame_unwinders = unwinders;
361   Py_XDECREF (tmp);
362 
363   return 0;
364 }
365 
366 /* Get the 'type_printers' attribute.  */
367 
368 static PyObject *
369 objfpy_get_type_printers (PyObject *o, void *ignore)
370 {
371   objfile_object *self = (objfile_object *) o;
372 
373   Py_INCREF (self->type_printers);
374   return self->type_printers;
375 }
376 
377 /* Get the 'xmethods' attribute.  */
378 
379 PyObject *
380 objfpy_get_xmethods (PyObject *o, void *ignore)
381 {
382   objfile_object *self = (objfile_object *) o;
383 
384   Py_INCREF (self->xmethods);
385   return self->xmethods;
386 }
387 
388 /* Set the 'type_printers' attribute.  */
389 
390 static int
391 objfpy_set_type_printers (PyObject *o, PyObject *value, void *ignore)
392 {
393   PyObject *tmp;
394   objfile_object *self = (objfile_object *) o;
395 
396   if (! value)
397     {
398       PyErr_SetString (PyExc_TypeError,
399 		       _("Cannot delete the type_printers attribute."));
400       return -1;
401     }
402 
403   if (! PyList_Check (value))
404     {
405       PyErr_SetString (PyExc_TypeError,
406 		       _("The type_printers attribute must be a list."));
407       return -1;
408     }
409 
410   /* Take care in case the LHS and RHS are related somehow.  */
411   tmp = self->type_printers;
412   Py_INCREF (value);
413   self->type_printers = value;
414   Py_XDECREF (tmp);
415 
416   return 0;
417 }
418 
419 /* Implementation of gdb.Objfile.is_valid (self) -> Boolean.
420    Returns True if this object file still exists in GDB.  */
421 
422 static PyObject *
423 objfpy_is_valid (PyObject *self, PyObject *args)
424 {
425   objfile_object *obj = (objfile_object *) self;
426 
427   if (! obj->objfile)
428     Py_RETURN_FALSE;
429 
430   Py_RETURN_TRUE;
431 }
432 
433 /* Implementation of gdb.Objfile.add_separate_debug_file (self) -> Boolean.  */
434 
435 static PyObject *
436 objfpy_add_separate_debug_file (PyObject *self, PyObject *args, PyObject *kw)
437 {
438   static char *keywords[] = { "file_name", NULL };
439   objfile_object *obj = (objfile_object *) self;
440   const char *file_name;
441   int symfile_flags = 0;
442 
443   OBJFPY_REQUIRE_VALID (obj);
444 
445   if (!PyArg_ParseTupleAndKeywords (args, kw, "s", keywords, &file_name))
446     return NULL;
447 
448   TRY
449     {
450       bfd *abfd = symfile_bfd_open (file_name);
451 
452       symbol_file_add_separate (abfd, file_name, symfile_flags, obj->objfile);
453     }
454   CATCH (except, RETURN_MASK_ALL)
455     {
456       GDB_PY_HANDLE_EXCEPTION (except);
457     }
458   END_CATCH
459 
460   Py_RETURN_NONE;
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 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 (! 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     {
600       PyObject *result = objfile_to_objfile_object (objfile);
601 
602       Py_XINCREF (result);
603       return result;
604     }
605 
606   PyErr_SetString (PyExc_ValueError, _("Objfile not found."));
607   return NULL;
608 }
609 
610 
611 
612 /* Clear the OBJFILE pointer in an Objfile object and remove the
613    reference.  */
614 static void
615 py_free_objfile (struct objfile *objfile, void *datum)
616 {
617   struct cleanup *cleanup;
618   objfile_object *object = datum;
619 
620   cleanup = ensure_python_env (get_objfile_arch (objfile), current_language);
621   object->objfile = NULL;
622   Py_DECREF ((PyObject *) object);
623   do_cleanups (cleanup);
624 }
625 
626 /* Return a borrowed reference to the Python object of type Objfile
627    representing OBJFILE.  If the object has already been created,
628    return it.  Otherwise, create it.  Return NULL and set the Python
629    error on failure.  */
630 
631 PyObject *
632 objfile_to_objfile_object (struct objfile *objfile)
633 {
634   objfile_object *object;
635 
636   object = objfile_data (objfile, objfpy_objfile_data_key);
637   if (!object)
638     {
639       object = PyObject_New (objfile_object, &objfile_object_type);
640       if (object)
641 	{
642 	  if (!objfpy_initialize (object))
643 	    {
644 	      Py_DECREF (object);
645 	      return NULL;
646 	    }
647 
648 	  object->objfile = objfile;
649 	  set_objfile_data (objfile, objfpy_objfile_data_key, object);
650 	}
651     }
652 
653   return (PyObject *) object;
654 }
655 
656 int
657 gdbpy_initialize_objfile (void)
658 {
659   objfpy_objfile_data_key
660     = register_objfile_data_with_cleanup (NULL, py_free_objfile);
661 
662   if (PyType_Ready (&objfile_object_type) < 0)
663     return -1;
664 
665   return gdb_pymodule_addobject (gdb_module, "Objfile",
666 				 (PyObject *) &objfile_object_type);
667 }
668 
669 
670 
671 static PyMethodDef objfile_object_methods[] =
672 {
673   { "is_valid", objfpy_is_valid, METH_NOARGS,
674     "is_valid () -> Boolean.\n\
675 Return true if this object file is valid, false if not." },
676 
677   { "add_separate_debug_file", (PyCFunction) objfpy_add_separate_debug_file,
678     METH_VARARGS | METH_KEYWORDS,
679     "add_separate_debug_file (file_name).\n\
680 Add FILE_NAME to the list of files containing debug info for the objfile." },
681 
682   { NULL }
683 };
684 
685 static PyGetSetDef objfile_getset[] =
686 {
687   { "__dict__", gdb_py_generic_dict, NULL,
688     "The __dict__ for this objfile.", &objfile_object_type },
689   { "filename", objfpy_get_filename, NULL,
690     "The objfile's filename, or None.", NULL },
691   { "username", objfpy_get_username, NULL,
692     "The name of the objfile as provided by the user, or None.", NULL },
693   { "owner", objfpy_get_owner, NULL,
694     "The objfile owner of separate debug info objfiles, or None.",
695     NULL },
696   { "build_id", objfpy_get_build_id, NULL,
697     "The objfile's build id, or None.", NULL },
698   { "progspace", objfpy_get_progspace, NULL,
699     "The objfile's progspace, or None.", NULL },
700   { "pretty_printers", objfpy_get_printers, objfpy_set_printers,
701     "Pretty printers.", NULL },
702   { "frame_filters", objfpy_get_frame_filters,
703     objfpy_set_frame_filters, "Frame Filters.", NULL },
704   { "frame_unwinders", objfpy_get_frame_unwinders,
705     objfpy_set_frame_unwinders, "Frame Unwinders", NULL },
706   { "type_printers", objfpy_get_type_printers, objfpy_set_type_printers,
707     "Type printers.", NULL },
708   { "xmethods", objfpy_get_xmethods, NULL,
709     "Debug methods.", NULL },
710   { NULL }
711 };
712 
713 PyTypeObject objfile_object_type =
714 {
715   PyVarObject_HEAD_INIT (NULL, 0)
716   "gdb.Objfile",		  /*tp_name*/
717   sizeof (objfile_object),	  /*tp_basicsize*/
718   0,				  /*tp_itemsize*/
719   objfpy_dealloc,		  /*tp_dealloc*/
720   0,				  /*tp_print*/
721   0,				  /*tp_getattr*/
722   0,				  /*tp_setattr*/
723   0,				  /*tp_compare*/
724   0,				  /*tp_repr*/
725   0,				  /*tp_as_number*/
726   0,				  /*tp_as_sequence*/
727   0,				  /*tp_as_mapping*/
728   0,				  /*tp_hash */
729   0,				  /*tp_call*/
730   0,				  /*tp_str*/
731   0,				  /*tp_getattro*/
732   0,				  /*tp_setattro*/
733   0,				  /*tp_as_buffer*/
734   Py_TPFLAGS_DEFAULT,		  /*tp_flags*/
735   "GDB objfile object",		  /* tp_doc */
736   0,				  /* tp_traverse */
737   0,				  /* tp_clear */
738   0,				  /* tp_richcompare */
739   0,				  /* tp_weaklistoffset */
740   0,				  /* tp_iter */
741   0,				  /* tp_iternext */
742   objfile_object_methods,	  /* tp_methods */
743   0,				  /* tp_members */
744   objfile_getset,		  /* tp_getset */
745   0,				  /* tp_base */
746   0,				  /* tp_dict */
747   0,				  /* tp_descr_get */
748   0,				  /* tp_descr_set */
749   offsetof (objfile_object, dict), /* tp_dictoffset */
750   0,				  /* tp_init */
751   0,				  /* tp_alloc */
752   objfpy_new,			  /* tp_new */
753 };
754