1 /* Python interface to symbol tables.
3 Copyright (C) 2008-2018 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
24 #include "python-internal.h"
29 typedef struct stpy_symtab_object {
31 /* The GDB Symbol table structure. */
32 struct symtab *symtab;
33 /* A symtab object is associated with an objfile, so keep track with
34 a doubly-linked list, rooted in the objfile. This allows
35 invalidation of the underlying struct symtab when the objfile is
37 struct stpy_symtab_object *prev;
38 struct stpy_symtab_object *next;
41 extern PyTypeObject symtab_object_type
42 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("symtab_object");
43 static const struct objfile_data *stpy_objfile_data_key;
45 /* Require a valid symbol table. All access to symtab_object->symtab
46 should be gated by this call. */
47 #define STPY_REQUIRE_VALID(symtab_obj, symtab) \
49 symtab = symtab_object_to_symtab (symtab_obj); \
52 PyErr_SetString (PyExc_RuntimeError, \
53 _("Symbol Table is invalid.")); \
58 typedef struct salpy_sal_object {
60 /* The GDB Symbol table structure. */
61 symtab_object *symtab;
62 /* The GDB Symbol table and line structure. */
63 struct symtab_and_line *sal;
64 /* A Symtab and line object is associated with an objfile, so keep
65 track with a doubly-linked list, rooted in the objfile. This
66 allows invalidation of the underlying struct symtab_and_line
67 when the objfile is deleted. */
68 struct salpy_sal_object *prev;
69 struct salpy_sal_object *next;
72 extern PyTypeObject sal_object_type
73 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("sal_object");
74 static const struct objfile_data *salpy_objfile_data_key;
76 /* Require a valid symbol table and line object. All access to
77 sal_object->sal should be gated by this call. */
78 #define SALPY_REQUIRE_VALID(sal_obj, sal) \
80 sal = sal_object_to_symtab_and_line (sal_obj); \
83 PyErr_SetString (PyExc_RuntimeError, \
84 _("Symbol Table and Line is invalid.")); \
90 stpy_str (PyObject *self)
93 struct symtab *symtab = NULL;
95 STPY_REQUIRE_VALID (self, symtab);
97 result = PyString_FromString (symtab_to_filename_for_display (symtab));
103 stpy_get_filename (PyObject *self, void *closure)
106 struct symtab *symtab = NULL;
107 const char *filename;
109 STPY_REQUIRE_VALID (self, symtab);
110 filename = symtab_to_filename_for_display (symtab);
112 str_obj = host_string_to_python_string (filename);
117 stpy_get_objfile (PyObject *self, void *closure)
119 struct symtab *symtab = NULL;
121 STPY_REQUIRE_VALID (self, symtab);
123 return objfile_to_objfile_object (SYMTAB_OBJFILE (symtab)).release ();
126 /* Getter function for symtab.producer. */
129 stpy_get_producer (PyObject *self, void *closure)
131 struct symtab *symtab = NULL;
132 struct compunit_symtab *cust;
134 STPY_REQUIRE_VALID (self, symtab);
135 cust = SYMTAB_COMPUNIT (symtab);
136 if (COMPUNIT_PRODUCER (cust) != NULL)
138 const char *producer = COMPUNIT_PRODUCER (cust);
140 return host_string_to_python_string (producer);
147 stpy_fullname (PyObject *self, PyObject *args)
149 const char *fullname;
150 struct symtab *symtab = NULL;
152 STPY_REQUIRE_VALID (self, symtab);
154 fullname = symtab_to_fullname (symtab);
156 return host_string_to_python_string (fullname);
159 /* Implementation of gdb.Symtab.is_valid (self) -> Boolean.
160 Returns True if this Symbol table still exists in GDB. */
163 stpy_is_valid (PyObject *self, PyObject *args)
165 struct symtab *symtab = NULL;
167 symtab = symtab_object_to_symtab (self);
174 /* Return the GLOBAL_BLOCK of the underlying symtab. */
177 stpy_global_block (PyObject *self, PyObject *args)
179 struct symtab *symtab = NULL;
180 struct block *block = NULL;
181 const struct blockvector *blockvector;
183 STPY_REQUIRE_VALID (self, symtab);
185 blockvector = SYMTAB_BLOCKVECTOR (symtab);
186 block = BLOCKVECTOR_BLOCK (blockvector, GLOBAL_BLOCK);
187 return block_to_block_object (block, SYMTAB_OBJFILE (symtab));
190 /* Return the STATIC_BLOCK of the underlying symtab. */
193 stpy_static_block (PyObject *self, PyObject *args)
195 struct symtab *symtab = NULL;
196 struct block *block = NULL;
197 const struct blockvector *blockvector;
199 STPY_REQUIRE_VALID (self, symtab);
201 blockvector = SYMTAB_BLOCKVECTOR (symtab);
202 block = BLOCKVECTOR_BLOCK (blockvector, STATIC_BLOCK);
203 return block_to_block_object (block, SYMTAB_OBJFILE (symtab));
206 /* Implementation of gdb.Symtab.linetable (self) -> gdb.LineTable.
207 Returns a gdb.LineTable object corresponding to this symbol
211 stpy_get_linetable (PyObject *self, PyObject *args)
213 struct symtab *symtab = NULL;
215 STPY_REQUIRE_VALID (self, symtab);
217 return symtab_to_linetable_object (self);
221 salpy_str (PyObject *self)
224 const char *filename;
227 struct symtab_and_line *sal = NULL;
229 SALPY_REQUIRE_VALID (self, sal);
231 sal_obj = (sal_object *) self;
232 filename = (sal_obj->symtab == (symtab_object *) Py_None)
233 ? "<unknown>" : symtab_to_filename_for_display (sal_obj->symtab->symtab);
235 s = xstrprintf ("symbol and line for %s, line %d", filename,
238 result = PyString_FromString (s);
245 stpy_dealloc (PyObject *obj)
247 symtab_object *symtab = (symtab_object *) obj;
250 symtab->prev->next = symtab->next;
251 else if (symtab->symtab)
253 set_objfile_data (SYMTAB_OBJFILE (symtab->symtab),
254 stpy_objfile_data_key, symtab->next);
257 symtab->next->prev = symtab->prev;
258 symtab->symtab = NULL;
263 salpy_get_pc (PyObject *self, void *closure)
265 struct symtab_and_line *sal = NULL;
267 SALPY_REQUIRE_VALID (self, sal);
269 return gdb_py_long_from_ulongest (sal->pc);
272 /* Implementation of the get method for the 'last' attribute of
273 gdb.Symtab_and_line. */
276 salpy_get_last (PyObject *self, void *closure)
278 struct symtab_and_line *sal = NULL;
280 SALPY_REQUIRE_VALID (self, sal);
283 return gdb_py_long_from_ulongest (sal->end - 1);
289 salpy_get_line (PyObject *self, void *closure)
291 struct symtab_and_line *sal = NULL;
293 SALPY_REQUIRE_VALID (self, sal);
295 return PyInt_FromLong (sal->line);
299 salpy_get_symtab (PyObject *self, void *closure)
301 struct symtab_and_line *sal;
302 sal_object *self_sal = (sal_object *) self;
304 SALPY_REQUIRE_VALID (self, sal);
306 Py_INCREF (self_sal->symtab);
308 return (PyObject *) self_sal->symtab;
311 /* Implementation of gdb.Symtab_and_line.is_valid (self) -> Boolean.
312 Returns True if this Symbol table and line object still exists GDB. */
315 salpy_is_valid (PyObject *self, PyObject *args)
317 struct symtab_and_line *sal;
319 sal = sal_object_to_symtab_and_line (self);
327 salpy_dealloc (PyObject *self)
329 sal_object *self_sal = (sal_object *) self;
332 self_sal->prev->next = self_sal->next;
333 else if (self_sal->symtab != (symtab_object * ) Py_None)
334 set_objfile_data (SYMTAB_OBJFILE (self_sal->symtab->symtab),
335 salpy_objfile_data_key, self_sal->next);
338 self_sal->next->prev = self_sal->prev;
340 Py_DECREF (self_sal->symtab);
341 xfree (self_sal->sal);
342 Py_TYPE (self)->tp_free (self);
345 /* Given a sal, and a sal_object that has previously been allocated
346 and initialized, populate the sal_object with the struct sal data.
347 Also, register the sal_object life-cycle with the life-cycle of the
348 object file associated with this sal, if needed. If a failure
349 occurs during the sal population, this function will return -1. */
350 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
351 set_sal (sal_object *sal_obj, struct symtab_and_line sal)
353 symtab_object *symtab_obj;
357 symtab_obj = (symtab_object *) symtab_to_symtab_object (sal.symtab);
358 /* If a symtab existed in the sal, but it cannot be duplicated,
360 if (symtab_obj == NULL)
365 symtab_obj = (symtab_object *) Py_None;
369 sal_obj->sal = ((struct symtab_and_line *)
370 xmemdup (&sal, sizeof (struct symtab_and_line),
371 sizeof (struct symtab_and_line)));
372 sal_obj->symtab = symtab_obj;
373 sal_obj->prev = NULL;
375 /* If the SAL does not have a symtab, we do not add it to the
376 objfile cleanup observer linked list. */
377 if (sal_obj->symtab != (symtab_object *)Py_None)
380 = ((struct salpy_sal_object *)
381 objfile_data (SYMTAB_OBJFILE (sal_obj->symtab->symtab),
382 salpy_objfile_data_key));
384 sal_obj->next->prev = sal_obj;
386 set_objfile_data (SYMTAB_OBJFILE (sal_obj->symtab->symtab),
387 salpy_objfile_data_key, sal_obj);
390 sal_obj->next = NULL;
395 /* Given a symtab, and a symtab_object that has previously been
396 allocated and initialized, populate the symtab_object with the
397 struct symtab data. Also, register the symtab_object life-cycle
398 with the life-cycle of the object file associated with this
399 symtab, if needed. */
401 set_symtab (symtab_object *obj, struct symtab *symtab)
403 obj->symtab = symtab;
408 = ((struct stpy_symtab_object *)
409 objfile_data (SYMTAB_OBJFILE (symtab), stpy_objfile_data_key));
411 obj->next->prev = obj;
412 set_objfile_data (SYMTAB_OBJFILE (symtab), stpy_objfile_data_key, obj);
418 /* Create a new symbol table (gdb.Symtab) object that encapsulates the
419 symtab structure from GDB. */
421 symtab_to_symtab_object (struct symtab *symtab)
423 symtab_object *symtab_obj;
425 symtab_obj = PyObject_New (symtab_object, &symtab_object_type);
427 set_symtab (symtab_obj, symtab);
429 return (PyObject *) symtab_obj;
432 /* Create a new symtab and line (gdb.Symtab_and_line) object
433 that encapsulates the symtab_and_line structure from GDB. */
435 symtab_and_line_to_sal_object (struct symtab_and_line sal)
437 gdbpy_ref<sal_object> sal_obj (PyObject_New (sal_object, &sal_object_type));
440 if (set_sal (sal_obj.get (), sal) < 0)
444 return (PyObject *) sal_obj.release ();
447 /* Return struct symtab_and_line reference that is wrapped by this
449 struct symtab_and_line *
450 sal_object_to_symtab_and_line (PyObject *obj)
452 if (! PyObject_TypeCheck (obj, &sal_object_type))
454 return ((sal_object *) obj)->sal;
457 /* Return struct symtab reference that is wrapped by this object. */
459 symtab_object_to_symtab (PyObject *obj)
461 if (! PyObject_TypeCheck (obj, &symtab_object_type))
463 return ((symtab_object *) obj)->symtab;
466 /* This function is called when an objfile is about to be freed.
467 Invalidate the symbol table as further actions on the symbol table
468 would result in bad data. All access to obj->symtab should be
469 gated by STPY_REQUIRE_VALID which will raise an exception on
470 invalid symbol tables. */
472 del_objfile_symtab (struct objfile *objfile, void *datum)
474 symtab_object *obj = (symtab_object *) datum;
478 symtab_object *next = obj->next;
487 /* This function is called when an objfile is about to be freed.
488 Invalidate the sal object as further actions on the sal
489 would result in bad data. All access to obj->sal should be
490 gated by SALPY_REQUIRE_VALID which will raise an exception on
491 invalid symbol table and line objects. */
493 del_objfile_sal (struct objfile *objfile, void *datum)
495 sal_object *obj = (sal_object *) datum;
499 sal_object *next = obj->next;
501 Py_DECREF (obj->symtab);
502 obj->symtab = (symtab_object *) Py_None;
515 gdbpy_initialize_symtabs (void)
517 symtab_object_type.tp_new = PyType_GenericNew;
518 if (PyType_Ready (&symtab_object_type) < 0)
521 sal_object_type.tp_new = PyType_GenericNew;
522 if (PyType_Ready (&sal_object_type) < 0)
525 /* Register an objfile "free" callback so we can properly
526 invalidate symbol tables, and symbol table and line data
527 structures when an object file that is about to be
529 stpy_objfile_data_key
530 = register_objfile_data_with_cleanup (NULL, del_objfile_symtab);
531 salpy_objfile_data_key
532 = register_objfile_data_with_cleanup (NULL, del_objfile_sal);
534 if (gdb_pymodule_addobject (gdb_module, "Symtab",
535 (PyObject *) &symtab_object_type) < 0)
538 return gdb_pymodule_addobject (gdb_module, "Symtab_and_line",
539 (PyObject *) &sal_object_type);
544 static gdb_PyGetSetDef symtab_object_getset[] = {
545 { "filename", stpy_get_filename, NULL,
546 "The symbol table's source filename.", NULL },
547 { "objfile", stpy_get_objfile, NULL, "The symtab's objfile.",
549 { "producer", stpy_get_producer, NULL,
550 "The name/version of the program that compiled this symtab.", NULL },
551 {NULL} /* Sentinel */
554 static PyMethodDef symtab_object_methods[] = {
555 { "is_valid", stpy_is_valid, METH_NOARGS,
556 "is_valid () -> Boolean.\n\
557 Return true if this symbol table is valid, false if not." },
558 { "fullname", stpy_fullname, METH_NOARGS,
559 "fullname () -> String.\n\
560 Return the symtab's full source filename." },
561 { "global_block", stpy_global_block, METH_NOARGS,
562 "global_block () -> gdb.Block.\n\
563 Return the global block of the symbol table." },
564 { "static_block", stpy_static_block, METH_NOARGS,
565 "static_block () -> gdb.Block.\n\
566 Return the static block of the symbol table." },
567 { "linetable", stpy_get_linetable, METH_NOARGS,
568 "linetable () -> gdb.LineTable.\n\
569 Return the LineTable associated with this symbol table" },
570 {NULL} /* Sentinel */
573 PyTypeObject symtab_object_type = {
574 PyVarObject_HEAD_INIT (NULL, 0)
575 "gdb.Symtab", /*tp_name*/
576 sizeof (symtab_object), /*tp_basicsize*/
578 stpy_dealloc, /*tp_dealloc*/
585 0, /*tp_as_sequence*/
593 Py_TPFLAGS_DEFAULT, /*tp_flags*/
594 "GDB symtab object", /*tp_doc */
597 0, /*tp_richcompare */
598 0, /*tp_weaklistoffset */
601 symtab_object_methods, /*tp_methods */
603 symtab_object_getset /*tp_getset */
606 static gdb_PyGetSetDef sal_object_getset[] = {
607 { "symtab", salpy_get_symtab, NULL, "Symtab object.", NULL },
608 { "pc", salpy_get_pc, NULL, "Return the symtab_and_line's pc.", NULL },
609 { "last", salpy_get_last, NULL,
610 "Return the symtab_and_line's last address.", NULL },
611 { "line", salpy_get_line, NULL,
612 "Return the symtab_and_line's line.", NULL },
613 {NULL} /* Sentinel */
616 static PyMethodDef sal_object_methods[] = {
617 { "is_valid", salpy_is_valid, METH_NOARGS,
618 "is_valid () -> Boolean.\n\
619 Return true if this symbol table and line is valid, false if not." },
620 {NULL} /* Sentinel */
623 PyTypeObject sal_object_type = {
624 PyVarObject_HEAD_INIT (NULL, 0)
625 "gdb.Symtab_and_line", /*tp_name*/
626 sizeof (sal_object), /*tp_basicsize*/
628 salpy_dealloc, /*tp_dealloc*/
635 0, /*tp_as_sequence*/
639 salpy_str, /*tp_str*/
643 Py_TPFLAGS_DEFAULT, /*tp_flags*/
644 "GDB symtab_and_line object", /*tp_doc */
647 0, /*tp_richcompare */
648 0, /*tp_weaklistoffset */
651 sal_object_methods, /*tp_methods */
653 sal_object_getset /*tp_getset */