1 /* Python interface to symbol tables.
3 Copyright (C) 2008-2016 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"
28 typedef struct stpy_symtab_object
{
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
36 struct stpy_symtab_object
*prev
;
37 struct stpy_symtab_object
*next
;
40 extern PyTypeObject symtab_object_type
41 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("symtab_object");
42 static const struct objfile_data
*stpy_objfile_data_key
;
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) \
48 symtab = symtab_object_to_symtab (symtab_obj); \
51 PyErr_SetString (PyExc_RuntimeError, \
52 _("Symbol Table is invalid.")); \
57 typedef struct salpy_sal_object
{
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
;
71 extern PyTypeObject sal_object_type
72 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("sal_object");
73 static const struct objfile_data
*salpy_objfile_data_key
;
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) \
79 sal = sal_object_to_symtab_and_line (sal_obj); \
82 PyErr_SetString (PyExc_RuntimeError, \
83 _("Symbol Table and Line is invalid.")); \
89 stpy_str (PyObject
*self
)
92 struct symtab
*symtab
= NULL
;
94 STPY_REQUIRE_VALID (self
, symtab
);
96 result
= PyString_FromString (symtab_to_filename_for_display (symtab
));
102 stpy_get_filename (PyObject
*self
, void *closure
)
105 struct symtab
*symtab
= NULL
;
106 const char *filename
;
108 STPY_REQUIRE_VALID (self
, symtab
);
109 filename
= symtab_to_filename_for_display (symtab
);
111 str_obj
= host_string_to_python_string (filename
);
116 stpy_get_objfile (PyObject
*self
, void *closure
)
118 struct symtab
*symtab
= NULL
;
121 STPY_REQUIRE_VALID (self
, symtab
);
123 result
= objfile_to_objfile_object (SYMTAB_OBJFILE (symtab
));
128 /* Getter function for symtab.producer. */
131 stpy_get_producer (PyObject
*self
, void *closure
)
133 struct symtab
*symtab
= NULL
;
134 struct compunit_symtab
*cust
;
136 STPY_REQUIRE_VALID (self
, symtab
);
137 cust
= SYMTAB_COMPUNIT (symtab
);
138 if (COMPUNIT_PRODUCER (cust
) != NULL
)
140 const char *producer
= COMPUNIT_PRODUCER (cust
);
142 return host_string_to_python_string (producer
);
149 stpy_fullname (PyObject
*self
, PyObject
*args
)
151 const char *fullname
;
152 struct symtab
*symtab
= NULL
;
154 STPY_REQUIRE_VALID (self
, symtab
);
156 fullname
= symtab_to_fullname (symtab
);
158 return host_string_to_python_string (fullname
);
161 /* Implementation of gdb.Symtab.is_valid (self) -> Boolean.
162 Returns True if this Symbol table still exists in GDB. */
165 stpy_is_valid (PyObject
*self
, PyObject
*args
)
167 struct symtab
*symtab
= NULL
;
169 symtab
= symtab_object_to_symtab (self
);
176 /* Return the GLOBAL_BLOCK of the underlying symtab. */
179 stpy_global_block (PyObject
*self
, PyObject
*args
)
181 struct symtab
*symtab
= NULL
;
182 struct block
*block
= NULL
;
183 const struct blockvector
*blockvector
;
185 STPY_REQUIRE_VALID (self
, symtab
);
187 blockvector
= SYMTAB_BLOCKVECTOR (symtab
);
188 block
= BLOCKVECTOR_BLOCK (blockvector
, GLOBAL_BLOCK
);
189 return block_to_block_object (block
, SYMTAB_OBJFILE (symtab
));
192 /* Return the STATIC_BLOCK of the underlying symtab. */
195 stpy_static_block (PyObject
*self
, PyObject
*args
)
197 struct symtab
*symtab
= NULL
;
198 struct block
*block
= NULL
;
199 const struct blockvector
*blockvector
;
201 STPY_REQUIRE_VALID (self
, symtab
);
203 blockvector
= SYMTAB_BLOCKVECTOR (symtab
);
204 block
= BLOCKVECTOR_BLOCK (blockvector
, STATIC_BLOCK
);
205 return block_to_block_object (block
, SYMTAB_OBJFILE (symtab
));
208 /* Implementation of gdb.Symtab.linetable (self) -> gdb.LineTable.
209 Returns a gdb.LineTable object corresponding to this symbol
213 stpy_get_linetable (PyObject
*self
, PyObject
*args
)
215 struct symtab
*symtab
= NULL
;
217 STPY_REQUIRE_VALID (self
, symtab
);
219 return symtab_to_linetable_object (self
);
223 salpy_str (PyObject
*self
)
226 const char *filename
;
229 struct symtab_and_line
*sal
= NULL
;
231 SALPY_REQUIRE_VALID (self
, sal
);
233 sal_obj
= (sal_object
*) self
;
234 filename
= (sal_obj
->symtab
== (symtab_object
*) Py_None
)
235 ? "<unknown>" : symtab_to_filename_for_display (sal_obj
->symtab
->symtab
);
237 s
= xstrprintf ("symbol and line for %s, line %d", filename
,
240 result
= PyString_FromString (s
);
247 stpy_dealloc (PyObject
*obj
)
249 symtab_object
*symtab
= (symtab_object
*) obj
;
252 symtab
->prev
->next
= symtab
->next
;
253 else if (symtab
->symtab
)
255 set_objfile_data (SYMTAB_OBJFILE (symtab
->symtab
),
256 stpy_objfile_data_key
, symtab
->next
);
259 symtab
->next
->prev
= symtab
->prev
;
260 symtab
->symtab
= NULL
;
265 salpy_get_pc (PyObject
*self
, void *closure
)
267 struct symtab_and_line
*sal
= NULL
;
269 SALPY_REQUIRE_VALID (self
, sal
);
271 return gdb_py_long_from_ulongest (sal
->pc
);
274 /* Implementation of the get method for the 'last' attribute of
275 gdb.Symtab_and_line. */
278 salpy_get_last (PyObject
*self
, void *closure
)
280 struct symtab_and_line
*sal
= NULL
;
282 SALPY_REQUIRE_VALID (self
, sal
);
285 return gdb_py_long_from_ulongest (sal
->end
- 1);
291 salpy_get_line (PyObject
*self
, void *closure
)
293 struct symtab_and_line
*sal
= NULL
;
295 SALPY_REQUIRE_VALID (self
, sal
);
297 return PyInt_FromLong (sal
->line
);
301 salpy_get_symtab (PyObject
*self
, void *closure
)
303 struct symtab_and_line
*sal
;
304 sal_object
*self_sal
= (sal_object
*) self
;
306 SALPY_REQUIRE_VALID (self
, sal
);
308 Py_INCREF (self_sal
->symtab
);
310 return (PyObject
*) self_sal
->symtab
;
313 /* Implementation of gdb.Symtab_and_line.is_valid (self) -> Boolean.
314 Returns True if this Symbol table and line object still exists GDB. */
317 salpy_is_valid (PyObject
*self
, PyObject
*args
)
319 struct symtab_and_line
*sal
;
321 sal
= sal_object_to_symtab_and_line (self
);
329 salpy_dealloc (PyObject
*self
)
331 sal_object
*self_sal
= (sal_object
*) self
;
334 self_sal
->prev
->next
= self_sal
->next
;
335 else if (self_sal
->symtab
!= (symtab_object
* ) Py_None
)
336 set_objfile_data (SYMTAB_OBJFILE (self_sal
->symtab
->symtab
),
337 salpy_objfile_data_key
, self_sal
->next
);
340 self_sal
->next
->prev
= self_sal
->prev
;
342 Py_DECREF (self_sal
->symtab
);
343 xfree (self_sal
->sal
);
344 Py_TYPE (self
)->tp_free (self
);
347 /* Given a sal, and a sal_object that has previously been allocated
348 and initialized, populate the sal_object with the struct sal data.
349 Also, register the sal_object life-cycle with the life-cycle of the
350 object file associated with this sal, if needed. If a failure
351 occurs during the sal population, this function will return -1. */
352 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
353 set_sal (sal_object
*sal_obj
, struct symtab_and_line sal
)
355 symtab_object
*symtab_obj
;
359 symtab_obj
= (symtab_object
*) symtab_to_symtab_object (sal
.symtab
);
360 /* If a symtab existed in the sal, but it cannot be duplicated,
362 if (symtab_obj
== NULL
)
367 symtab_obj
= (symtab_object
*) Py_None
;
371 sal_obj
->sal
= ((struct symtab_and_line
*)
372 xmemdup (&sal
, sizeof (struct symtab_and_line
),
373 sizeof (struct symtab_and_line
)));
374 sal_obj
->symtab
= symtab_obj
;
375 sal_obj
->prev
= NULL
;
377 /* If the SAL does not have a symtab, we do not add it to the
378 objfile cleanup observer linked list. */
379 if (sal_obj
->symtab
!= (symtab_object
*)Py_None
)
382 = ((struct salpy_sal_object
*)
383 objfile_data (SYMTAB_OBJFILE (sal_obj
->symtab
->symtab
),
384 salpy_objfile_data_key
));
386 sal_obj
->next
->prev
= sal_obj
;
388 set_objfile_data (SYMTAB_OBJFILE (sal_obj
->symtab
->symtab
),
389 salpy_objfile_data_key
, sal_obj
);
392 sal_obj
->next
= NULL
;
397 /* Given a symtab, and a symtab_object that has previously been
398 allocated and initialized, populate the symtab_object with the
399 struct symtab data. Also, register the symtab_object life-cycle
400 with the life-cycle of the object file associated with this
401 symtab, if needed. */
403 set_symtab (symtab_object
*obj
, struct symtab
*symtab
)
405 obj
->symtab
= symtab
;
410 = ((struct stpy_symtab_object
*)
411 objfile_data (SYMTAB_OBJFILE (symtab
), stpy_objfile_data_key
));
413 obj
->next
->prev
= obj
;
414 set_objfile_data (SYMTAB_OBJFILE (symtab
), stpy_objfile_data_key
, obj
);
420 /* Create a new symbol table (gdb.Symtab) object that encapsulates the
421 symtab structure from GDB. */
423 symtab_to_symtab_object (struct symtab
*symtab
)
425 symtab_object
*symtab_obj
;
427 symtab_obj
= PyObject_New (symtab_object
, &symtab_object_type
);
429 set_symtab (symtab_obj
, symtab
);
431 return (PyObject
*) symtab_obj
;
434 /* Create a new symtab and line (gdb.Symtab_and_line) object
435 that encapsulates the symtab_and_line structure from GDB. */
437 symtab_and_line_to_sal_object (struct symtab_and_line sal
)
441 sal_obj
= PyObject_New (sal_object
, &sal_object_type
);
444 if (set_sal (sal_obj
, sal
) < 0)
451 return (PyObject
*) sal_obj
;
454 /* Return struct symtab_and_line reference that is wrapped by this
456 struct symtab_and_line
*
457 sal_object_to_symtab_and_line (PyObject
*obj
)
459 if (! PyObject_TypeCheck (obj
, &sal_object_type
))
461 return ((sal_object
*) obj
)->sal
;
464 /* Return struct symtab reference that is wrapped by this object. */
466 symtab_object_to_symtab (PyObject
*obj
)
468 if (! PyObject_TypeCheck (obj
, &symtab_object_type
))
470 return ((symtab_object
*) obj
)->symtab
;
473 /* This function is called when an objfile is about to be freed.
474 Invalidate the symbol table as further actions on the symbol table
475 would result in bad data. All access to obj->symtab should be
476 gated by STPY_REQUIRE_VALID which will raise an exception on
477 invalid symbol tables. */
479 del_objfile_symtab (struct objfile
*objfile
, void *datum
)
481 symtab_object
*obj
= (symtab_object
*) datum
;
485 symtab_object
*next
= obj
->next
;
494 /* This function is called when an objfile is about to be freed.
495 Invalidate the sal object as further actions on the sal
496 would result in bad data. All access to obj->sal should be
497 gated by SALPY_REQUIRE_VALID which will raise an exception on
498 invalid symbol table and line objects. */
500 del_objfile_sal (struct objfile
*objfile
, void *datum
)
502 sal_object
*obj
= (sal_object
*) datum
;
506 sal_object
*next
= obj
->next
;
508 Py_DECREF (obj
->symtab
);
509 obj
->symtab
= (symtab_object
*) Py_None
;
522 gdbpy_initialize_symtabs (void)
524 symtab_object_type
.tp_new
= PyType_GenericNew
;
525 if (PyType_Ready (&symtab_object_type
) < 0)
528 sal_object_type
.tp_new
= PyType_GenericNew
;
529 if (PyType_Ready (&sal_object_type
) < 0)
532 /* Register an objfile "free" callback so we can properly
533 invalidate symbol tables, and symbol table and line data
534 structures when an object file that is about to be
536 stpy_objfile_data_key
537 = register_objfile_data_with_cleanup (NULL
, del_objfile_symtab
);
538 salpy_objfile_data_key
539 = register_objfile_data_with_cleanup (NULL
, del_objfile_sal
);
541 if (gdb_pymodule_addobject (gdb_module
, "Symtab",
542 (PyObject
*) &symtab_object_type
) < 0)
545 return gdb_pymodule_addobject (gdb_module
, "Symtab_and_line",
546 (PyObject
*) &sal_object_type
);
551 static PyGetSetDef symtab_object_getset
[] = {
552 { "filename", stpy_get_filename
, NULL
,
553 "The symbol table's source filename.", NULL
},
554 { "objfile", stpy_get_objfile
, NULL
, "The symtab's objfile.",
556 { "producer", stpy_get_producer
, NULL
,
557 "The name/version of the program that compiled this symtab.", NULL
},
558 {NULL
} /* Sentinel */
561 static PyMethodDef symtab_object_methods
[] = {
562 { "is_valid", stpy_is_valid
, METH_NOARGS
,
563 "is_valid () -> Boolean.\n\
564 Return true if this symbol table is valid, false if not." },
565 { "fullname", stpy_fullname
, METH_NOARGS
,
566 "fullname () -> String.\n\
567 Return the symtab's full source filename." },
568 { "global_block", stpy_global_block
, METH_NOARGS
,
569 "global_block () -> gdb.Block.\n\
570 Return the global block of the symbol table." },
571 { "static_block", stpy_static_block
, METH_NOARGS
,
572 "static_block () -> gdb.Block.\n\
573 Return the static block of the symbol table." },
574 { "linetable", stpy_get_linetable
, METH_NOARGS
,
575 "linetable () -> gdb.LineTable.\n\
576 Return the LineTable associated with this symbol table" },
577 {NULL
} /* Sentinel */
580 PyTypeObject symtab_object_type
= {
581 PyVarObject_HEAD_INIT (NULL
, 0)
582 "gdb.Symtab", /*tp_name*/
583 sizeof (symtab_object
), /*tp_basicsize*/
585 stpy_dealloc
, /*tp_dealloc*/
592 0, /*tp_as_sequence*/
600 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
601 "GDB symtab object", /*tp_doc */
604 0, /*tp_richcompare */
605 0, /*tp_weaklistoffset */
608 symtab_object_methods
, /*tp_methods */
610 symtab_object_getset
/*tp_getset */
613 static PyGetSetDef sal_object_getset
[] = {
614 { "symtab", salpy_get_symtab
, NULL
, "Symtab object.", NULL
},
615 { "pc", salpy_get_pc
, NULL
, "Return the symtab_and_line's pc.", NULL
},
616 { "last", salpy_get_last
, NULL
,
617 "Return the symtab_and_line's last address.", NULL
},
618 { "line", salpy_get_line
, NULL
,
619 "Return the symtab_and_line's line.", NULL
},
620 {NULL
} /* Sentinel */
623 static PyMethodDef sal_object_methods
[] = {
624 { "is_valid", salpy_is_valid
, METH_NOARGS
,
625 "is_valid () -> Boolean.\n\
626 Return true if this symbol table and line is valid, false if not." },
627 {NULL
} /* Sentinel */
630 PyTypeObject sal_object_type
= {
631 PyVarObject_HEAD_INIT (NULL
, 0)
632 "gdb.Symtab_and_line", /*tp_name*/
633 sizeof (sal_object
), /*tp_basicsize*/
635 salpy_dealloc
, /*tp_dealloc*/
642 0, /*tp_as_sequence*/
646 salpy_str
, /*tp_str*/
650 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
651 "GDB symtab_and_line object", /*tp_doc */
654 0, /*tp_richcompare */
655 0, /*tp_weaklistoffset */
658 sal_object_methods
, /*tp_methods */
660 sal_object_getset
/*tp_getset */