xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/python/py-symtab.c (revision 200d779b75dbeafa7bc01fd0f60bc61185f6967b)
1 /* Python interface to symbol tables.
2 
3    Copyright (C) 2008-2014 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 "charset.h"
22 #include "symtab.h"
23 #include "source.h"
24 #include "python-internal.h"
25 #include "objfiles.h"
26 #include "block.h"
27 
28 typedef struct stpy_symtab_object {
29   PyObject_HEAD
30   /* The GDB Symbol table structure.  */
31   struct symtab *symtab;
32   /* A symtab object is associated with an objfile, so keep track with
33      a doubly-linked list, rooted in the objfile.  This allows
34      invalidation of the underlying struct symtab when the objfile is
35      deleted.  */
36   struct stpy_symtab_object *prev;
37   struct stpy_symtab_object *next;
38 } symtab_object;
39 
40 static PyTypeObject symtab_object_type
41     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("symtab_object");
42 static const struct objfile_data *stpy_objfile_data_key;
43 
44 /* Require a valid symbol table.  All access to symtab_object->symtab
45    should be gated by this call.  */
46 #define STPY_REQUIRE_VALID(symtab_obj, symtab)		 \
47   do {							 \
48     symtab = symtab_object_to_symtab (symtab_obj);	 \
49     if (symtab == NULL)					 \
50       {							 \
51 	PyErr_SetString (PyExc_RuntimeError,		 \
52 			 _("Symbol Table is invalid.")); \
53 	return NULL;					 \
54       }							 \
55   } while (0)
56 
57 typedef struct salpy_sal_object {
58   PyObject_HEAD
59   /* The GDB Symbol table structure.  */
60   symtab_object *symtab;
61   /* The GDB Symbol table and line structure.  */
62   struct symtab_and_line *sal;
63   /* A Symtab and line object is associated with an objfile, so keep
64      track with a doubly-linked list, rooted in the objfile.  This
65      allows invalidation of the underlying struct symtab_and_line
66      when the objfile is deleted.  */
67   struct salpy_sal_object *prev;
68   struct salpy_sal_object *next;
69 } sal_object;
70 
71 static PyTypeObject sal_object_type
72     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("sal_object");
73 static const struct objfile_data *salpy_objfile_data_key;
74 
75 /* Require a valid symbol table and line object.  All access to
76    sal_object->sal should be gated by this call.  */
77 #define SALPY_REQUIRE_VALID(sal_obj, sal)				\
78   do {									\
79     sal = sal_object_to_symtab_and_line (sal_obj);			\
80     if (sal == NULL)							\
81       {									\
82 	  PyErr_SetString (PyExc_RuntimeError,				\
83 			   _("Symbol Table and Line is invalid."));	\
84 	  return NULL;							\
85 	}								\
86   } while (0)
87 
88 static PyObject *
89 stpy_str (PyObject *self)
90 {
91   PyObject *result;
92   struct symtab *symtab = NULL;
93 
94   STPY_REQUIRE_VALID (self, symtab);
95 
96   result = PyString_FromString (symtab_to_filename_for_display (symtab));
97 
98   return result;
99 }
100 
101 static PyObject *
102 stpy_get_filename (PyObject *self, void *closure)
103 {
104   PyObject *str_obj;
105   struct symtab *symtab = NULL;
106   const char *filename;
107 
108   STPY_REQUIRE_VALID (self, symtab);
109   filename = symtab_to_filename_for_display (symtab);
110 
111   str_obj = PyString_Decode (filename, strlen (filename),
112 			     host_charset (), NULL);
113   return str_obj;
114 }
115 
116 static PyObject *
117 stpy_get_objfile (PyObject *self, void *closure)
118 {
119   struct symtab *symtab = NULL;
120   PyObject *result;
121 
122   STPY_REQUIRE_VALID (self, symtab);
123 
124   result = objfile_to_objfile_object (symtab->objfile);
125   Py_XINCREF (result);
126   return result;
127 }
128 
129 static PyObject *
130 stpy_fullname (PyObject *self, PyObject *args)
131 {
132   const char *fullname;
133   struct symtab *symtab = NULL;
134 
135   STPY_REQUIRE_VALID (self, symtab);
136 
137   fullname = symtab_to_fullname (symtab);
138 
139   return PyString_Decode (fullname, strlen (fullname), host_charset (), NULL);
140 }
141 
142 /* Implementation of gdb.Symtab.is_valid (self) -> Boolean.
143    Returns True if this Symbol table still exists in GDB.  */
144 
145 static PyObject *
146 stpy_is_valid (PyObject *self, PyObject *args)
147 {
148   struct symtab *symtab = NULL;
149 
150   symtab = symtab_object_to_symtab (self);
151   if (symtab == NULL)
152     Py_RETURN_FALSE;
153 
154   Py_RETURN_TRUE;
155 }
156 
157 /* Return the GLOBAL_BLOCK of the underlying symtab.  */
158 
159 static PyObject *
160 stpy_global_block (PyObject *self, PyObject *args)
161 {
162   struct symtab *symtab = NULL;
163   struct block *block = NULL;
164   struct blockvector *blockvector;
165 
166   STPY_REQUIRE_VALID (self, symtab);
167 
168   blockvector = BLOCKVECTOR (symtab);
169   block = BLOCKVECTOR_BLOCK (blockvector, GLOBAL_BLOCK);
170   return block_to_block_object (block, symtab->objfile);
171 }
172 
173 /* Return the STATIC_BLOCK of the underlying symtab.  */
174 
175 static PyObject *
176 stpy_static_block (PyObject *self, PyObject *args)
177 {
178   struct symtab *symtab = NULL;
179   struct block *block = NULL;
180   struct blockvector *blockvector;
181 
182   STPY_REQUIRE_VALID (self, symtab);
183 
184   blockvector = BLOCKVECTOR (symtab);
185   block = BLOCKVECTOR_BLOCK (blockvector, STATIC_BLOCK);
186   return block_to_block_object (block, symtab->objfile);
187 }
188 
189 /* Implementation of gdb.Symtab.linetable (self) -> gdb.Linetable.
190    Returns a gdb.Linetable object corresponding to this symbol
191    table.  */
192 
193 static PyObject *
194 stpy_get_linetable (PyObject *self, PyObject *args)
195 {
196   struct symtab *symtab = NULL;
197 
198   STPY_REQUIRE_VALID (self, symtab);
199 
200   return symtab_to_linetable_object (self);
201 }
202 
203 static PyObject *
204 salpy_str (PyObject *self)
205 {
206   char *s;
207   const char *filename;
208   sal_object *sal_obj;
209   PyObject *result;
210   struct symtab_and_line *sal = NULL;
211 
212   SALPY_REQUIRE_VALID (self, sal);
213 
214   sal_obj = (sal_object *) self;
215   filename = (sal_obj->symtab == (symtab_object *) Py_None)
216     ? "<unknown>" : symtab_to_filename_for_display (sal_obj->symtab->symtab);
217 
218   s = xstrprintf ("symbol and line for %s, line %d", filename,
219 		  sal->line);
220 
221   result = PyString_FromString (s);
222   xfree (s);
223 
224   return result;
225 }
226 
227 static void
228 stpy_dealloc (PyObject *obj)
229 {
230   symtab_object *symtab = (symtab_object *) obj;
231 
232   if (symtab->prev)
233     symtab->prev->next = symtab->next;
234   else if (symtab->symtab)
235     {
236       set_objfile_data (symtab->symtab->objfile,
237 			stpy_objfile_data_key, symtab->next);
238     }
239   if (symtab->next)
240     symtab->next->prev = symtab->prev;
241   symtab->symtab = NULL;
242 }
243 
244 
245 static PyObject *
246 salpy_get_pc (PyObject *self, void *closure)
247 {
248   struct symtab_and_line *sal = NULL;
249 
250   SALPY_REQUIRE_VALID (self, sal);
251 
252   return gdb_py_long_from_ulongest (sal->pc);
253 }
254 
255 /* Implementation of the get method for the 'last' attribute of
256    gdb.Symtab_and_line.  */
257 
258 static PyObject *
259 salpy_get_last (PyObject *self, void *closure)
260 {
261   struct symtab_and_line *sal = NULL;
262 
263   SALPY_REQUIRE_VALID (self, sal);
264 
265   if (sal->end > 0)
266     return gdb_py_long_from_ulongest (sal->end - 1);
267   else
268     Py_RETURN_NONE;
269 }
270 
271 static PyObject *
272 salpy_get_line (PyObject *self, void *closure)
273 {
274   struct symtab_and_line *sal = NULL;
275 
276   SALPY_REQUIRE_VALID (self, sal);
277 
278   return PyInt_FromLong (sal->line);
279 }
280 
281 static PyObject *
282 salpy_get_symtab (PyObject *self, void *closure)
283 {
284   struct symtab_and_line *sal;
285   sal_object *self_sal = (sal_object *) self;
286 
287   SALPY_REQUIRE_VALID (self, sal);
288 
289   Py_INCREF (self_sal->symtab);
290 
291   return (PyObject *) self_sal->symtab;
292 }
293 
294 /* Implementation of gdb.Symtab_and_line.is_valid (self) -> Boolean.
295    Returns True if this Symbol table and line object still exists GDB.  */
296 
297 static PyObject *
298 salpy_is_valid (PyObject *self, PyObject *args)
299 {
300   struct symtab_and_line *sal;
301 
302   sal = sal_object_to_symtab_and_line (self);
303   if (sal == NULL)
304     Py_RETURN_FALSE;
305 
306   Py_RETURN_TRUE;
307 }
308 
309 static void
310 salpy_dealloc (PyObject *self)
311 {
312   sal_object *self_sal = (sal_object *) self;
313 
314   if (self_sal->prev)
315     self_sal->prev->next = self_sal->next;
316   else if (self_sal->symtab != (symtab_object * ) Py_None)
317     set_objfile_data (self_sal->symtab->symtab->objfile,
318 		      salpy_objfile_data_key, self_sal->next);
319 
320   if (self_sal->next)
321     self_sal->next->prev = self_sal->prev;
322 
323   Py_DECREF (self_sal->symtab);
324   xfree (self_sal->sal);
325   Py_TYPE (self)->tp_free (self);
326 }
327 
328 /* Given a sal, and a sal_object that has previously been allocated
329    and initialized, populate the sal_object with the struct sal data.
330    Also, register the sal_object life-cycle with the life-cycle of the
331    object file associated with this sal, if needed.  If a failure
332    occurs during the sal population, this function will return -1.  */
333 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
334 set_sal (sal_object *sal_obj, struct symtab_and_line sal)
335 {
336   symtab_object *symtab_obj;
337 
338   if (sal.symtab)
339     {
340       symtab_obj = (symtab_object *) symtab_to_symtab_object  (sal.symtab);
341       /* If a symtab existed in the sal, but it cannot be duplicated,
342 	 we exit.  */
343       if (symtab_obj == NULL)
344 	return -1;
345     }
346   else
347     {
348       symtab_obj = (symtab_object *) Py_None;
349       Py_INCREF (Py_None);
350     }
351 
352   sal_obj->sal = xmemdup (&sal, sizeof (struct symtab_and_line),
353 			  sizeof (struct symtab_and_line));
354   sal_obj->symtab = symtab_obj;
355   sal_obj->prev = NULL;
356 
357   /* If the SAL does not have a symtab, we do not add it to the
358      objfile cleanup observer linked list.  */
359   if (sal_obj->symtab != (symtab_object *)Py_None)
360     {
361       sal_obj->next = objfile_data (sal_obj->symtab->symtab->objfile,
362 				    salpy_objfile_data_key);
363       if (sal_obj->next)
364 	sal_obj->next->prev = sal_obj;
365 
366       set_objfile_data (sal_obj->symtab->symtab->objfile,
367 			salpy_objfile_data_key, sal_obj);
368     }
369   else
370     sal_obj->next = NULL;
371 
372   return 0;
373 }
374 
375 /* Given a symtab, and a symtab_object that has previously been
376    allocated and initialized, populate the symtab_object with the
377    struct symtab data.  Also, register the symtab_object life-cycle
378    with the life-cycle of the object file associated with this
379    symtab, if needed.  */
380 static void
381 set_symtab (symtab_object *obj, struct symtab *symtab)
382 {
383   obj->symtab = symtab;
384   obj->prev = NULL;
385   if (symtab)
386     {
387       obj->next = objfile_data (symtab->objfile, stpy_objfile_data_key);
388       if (obj->next)
389 	obj->next->prev = obj;
390       set_objfile_data (symtab->objfile, stpy_objfile_data_key, obj);
391     }
392   else
393     obj->next = NULL;
394 }
395 
396 /* Create a new symbol table (gdb.Symtab) object that encapsulates the
397    symtab structure from GDB.  */
398 PyObject *
399 symtab_to_symtab_object (struct symtab *symtab)
400 {
401   symtab_object *symtab_obj;
402 
403   symtab_obj = PyObject_New (symtab_object, &symtab_object_type);
404   if (symtab_obj)
405     set_symtab (symtab_obj, symtab);
406 
407   return (PyObject *) symtab_obj;
408 }
409 
410 /* Create a new symtab and line (gdb.Symtab_and_line) object
411    that encapsulates the symtab_and_line structure from GDB.  */
412 PyObject *
413 symtab_and_line_to_sal_object (struct symtab_and_line sal)
414 {
415   sal_object *sal_obj;
416   int success = 0;
417 
418   sal_obj = PyObject_New (sal_object, &sal_object_type);
419   if (sal_obj)
420     {
421       if (set_sal (sal_obj, sal) < 0)
422 	{
423 	  Py_DECREF (sal_obj);
424 	  return NULL;
425 	}
426     }
427 
428   return (PyObject *) sal_obj;
429 }
430 
431 /* Return struct symtab_and_line reference that is wrapped by this
432    object.  */
433 struct symtab_and_line *
434 sal_object_to_symtab_and_line (PyObject *obj)
435 {
436   if (! PyObject_TypeCheck (obj, &sal_object_type))
437     return NULL;
438   return ((sal_object *) obj)->sal;
439 }
440 
441 /* Return struct symtab reference that is wrapped by this object.  */
442 struct symtab *
443 symtab_object_to_symtab (PyObject *obj)
444 {
445   if (! PyObject_TypeCheck (obj, &symtab_object_type))
446     return NULL;
447   return ((symtab_object *) obj)->symtab;
448 }
449 
450 /* This function is called when an objfile is about to be freed.
451    Invalidate the symbol table as further actions on the symbol table
452    would result in bad data.  All access to obj->symtab should be
453    gated by STPY_REQUIRE_VALID which will raise an exception on
454    invalid symbol tables.  */
455 static void
456 del_objfile_symtab (struct objfile *objfile, void *datum)
457 {
458   symtab_object *obj = datum;
459 
460   while (obj)
461     {
462       symtab_object *next = obj->next;
463 
464       obj->symtab = NULL;
465       obj->next = NULL;
466       obj->prev = NULL;
467       obj = next;
468     }
469 }
470 
471 /* This function is called when an objfile is about to be freed.
472    Invalidate the sal object as further actions on the sal
473    would result in bad data.  All access to obj->sal should be
474    gated by SALPY_REQUIRE_VALID which will raise an exception on
475    invalid symbol table and line objects.  */
476 static void
477 del_objfile_sal (struct objfile *objfile, void *datum)
478 {
479   sal_object *obj = datum;
480 
481   while (obj)
482     {
483       sal_object *next = obj->next;
484 
485       Py_DECREF (obj->symtab);
486       obj->symtab = (symtab_object *) Py_None;
487       Py_INCREF (Py_None);
488 
489       obj->next = NULL;
490       obj->prev = NULL;
491       xfree (obj->sal);
492       obj->sal = NULL;
493 
494       obj = next;
495     }
496 }
497 
498 int
499 gdbpy_initialize_symtabs (void)
500 {
501   symtab_object_type.tp_new = PyType_GenericNew;
502   if (PyType_Ready (&symtab_object_type) < 0)
503     return -1;
504 
505   sal_object_type.tp_new = PyType_GenericNew;
506   if (PyType_Ready (&sal_object_type) < 0)
507     return -1;
508 
509   /* Register an objfile "free" callback so we can properly
510      invalidate symbol tables, and symbol table and line data
511      structures when an object file that is about to be
512      deleted.  */
513   stpy_objfile_data_key
514     = register_objfile_data_with_cleanup (NULL, del_objfile_symtab);
515   salpy_objfile_data_key
516     = register_objfile_data_with_cleanup (NULL, del_objfile_sal);
517 
518   if (gdb_pymodule_addobject (gdb_module, "Symtab",
519 			      (PyObject *) &symtab_object_type) < 0)
520     return -1;
521 
522   return gdb_pymodule_addobject (gdb_module, "Symtab_and_line",
523 				 (PyObject *) &sal_object_type);
524 }
525 
526 
527 
528 static PyGetSetDef symtab_object_getset[] = {
529   { "filename", stpy_get_filename, NULL,
530     "The symbol table's source filename.", NULL },
531   { "objfile", stpy_get_objfile, NULL, "The symtab's objfile.",
532     NULL },
533   {NULL}  /* Sentinel */
534 };
535 
536 static PyMethodDef symtab_object_methods[] = {
537   { "is_valid", stpy_is_valid, METH_NOARGS,
538     "is_valid () -> Boolean.\n\
539 Return true if this symbol table is valid, false if not." },
540   { "fullname", stpy_fullname, METH_NOARGS,
541     "fullname () -> String.\n\
542 Return the symtab's full source filename." },
543   { "global_block", stpy_global_block, METH_NOARGS,
544     "global_block () -> gdb.Block.\n\
545 Return the global block of the symbol table." },
546   { "static_block", stpy_static_block, METH_NOARGS,
547     "static_block () -> gdb.Block.\n\
548 Return the static block of the symbol table." },
549     { "linetable", stpy_get_linetable, METH_NOARGS,
550     "linetable () -> gdb.Linetable.\n\
551 Return the Linetable associated with this symbol table" },
552   {NULL}  /* Sentinel */
553 };
554 
555 static PyTypeObject symtab_object_type = {
556   PyVarObject_HEAD_INIT (NULL, 0)
557   "gdb.Symtab",			  /*tp_name*/
558   sizeof (symtab_object),	  /*tp_basicsize*/
559   0,				  /*tp_itemsize*/
560   stpy_dealloc,			  /*tp_dealloc*/
561   0,				  /*tp_print*/
562   0,				  /*tp_getattr*/
563   0,				  /*tp_setattr*/
564   0,				  /*tp_compare*/
565   0,				  /*tp_repr*/
566   0,				  /*tp_as_number*/
567   0,				  /*tp_as_sequence*/
568   0,				  /*tp_as_mapping*/
569   0,				  /*tp_hash */
570   0,				  /*tp_call*/
571   stpy_str,			  /*tp_str*/
572   0,				  /*tp_getattro*/
573   0,				  /*tp_setattro*/
574   0,				  /*tp_as_buffer*/
575   Py_TPFLAGS_DEFAULT,		  /*tp_flags*/
576   "GDB symtab object",		  /*tp_doc */
577   0,				  /*tp_traverse */
578   0,				  /*tp_clear */
579   0,				  /*tp_richcompare */
580   0,				  /*tp_weaklistoffset */
581   0,				  /*tp_iter */
582   0,				  /*tp_iternext */
583   symtab_object_methods,	  /*tp_methods */
584   0,				  /*tp_members */
585   symtab_object_getset		  /*tp_getset */
586 };
587 
588 static PyGetSetDef sal_object_getset[] = {
589   { "symtab", salpy_get_symtab, NULL, "Symtab object.", NULL },
590   { "pc", salpy_get_pc, NULL, "Return the symtab_and_line's pc.", NULL },
591   { "last", salpy_get_last, NULL,
592     "Return the symtab_and_line's last address.", NULL },
593   { "line", salpy_get_line, NULL,
594     "Return the symtab_and_line's line.", NULL },
595   {NULL}  /* Sentinel */
596 };
597 
598 static PyMethodDef sal_object_methods[] = {
599   { "is_valid", salpy_is_valid, METH_NOARGS,
600     "is_valid () -> Boolean.\n\
601 Return true if this symbol table and line is valid, false if not." },
602   {NULL}  /* Sentinel */
603 };
604 
605 static PyTypeObject sal_object_type = {
606   PyVarObject_HEAD_INIT (NULL, 0)
607   "gdb.Symtab_and_line",	  /*tp_name*/
608   sizeof (sal_object),		  /*tp_basicsize*/
609   0,				  /*tp_itemsize*/
610   salpy_dealloc,		  /*tp_dealloc*/
611   0,				  /*tp_print*/
612   0,				  /*tp_getattr*/
613   0,				  /*tp_setattr*/
614   0,				  /*tp_compare*/
615   0,				  /*tp_repr*/
616   0,				  /*tp_as_number*/
617   0,				  /*tp_as_sequence*/
618   0,				  /*tp_as_mapping*/
619   0,				  /*tp_hash */
620   0,				  /*tp_call*/
621   salpy_str,			  /*tp_str*/
622   0,				  /*tp_getattro*/
623   0,				  /*tp_setattro*/
624   0,				  /*tp_as_buffer*/
625   Py_TPFLAGS_DEFAULT,		  /*tp_flags*/
626   "GDB symtab_and_line object",	  /*tp_doc */
627   0,				  /*tp_traverse */
628   0,				  /*tp_clear */
629   0,				  /*tp_richcompare */
630   0,				  /*tp_weaklistoffset */
631   0,				  /*tp_iter */
632   0,				  /*tp_iternext */
633   sal_object_methods,		  /*tp_methods */
634   0,				  /*tp_members */
635   sal_object_getset		  /*tp_getset */
636 };
637