1 /* Python interface to symbol tables.
3 Copyright (C) 2008-2024 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/>. */
23 #include "python-internal.h"
27 struct symtab_object
{
29 /* The GDB Symbol table structure. */
30 struct symtab
*symtab
;
31 /* A symtab object is associated with an objfile, so keep track with
32 a doubly-linked list, rooted in the objfile. This allows
33 invalidation of the underlying struct symtab when the objfile is
39 /* This function is called when an objfile is about to be freed.
40 Invalidate the symbol table as further actions on the symbol table
41 would result in bad data. All access to obj->symtab should be
42 gated by STPY_REQUIRE_VALID which will raise an exception on
43 invalid symbol tables. */
46 void operator() (symtab_object
*obj
)
50 symtab_object
*next
= obj
->next
;
60 extern PyTypeObject symtab_object_type
61 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("symtab_object");
62 static const registry
<objfile
>::key
<symtab_object
, stpy_deleter
>
63 stpy_objfile_data_key
;
65 /* Require a valid symbol table. All access to symtab_object->symtab
66 should be gated by this call. */
67 #define STPY_REQUIRE_VALID(symtab_obj, symtab) \
69 symtab = symtab_object_to_symtab (symtab_obj); \
72 PyErr_SetString (PyExc_RuntimeError, \
73 _("Symbol Table is invalid.")); \
80 /* The GDB Symbol table structure. */
82 /* The GDB Symbol table and line structure. */
83 struct symtab_and_line
*sal
;
84 /* A Symtab and line object is associated with an objfile, so keep
85 track with a doubly-linked list, rooted in the objfile. This
86 allows invalidation of the underlying struct symtab_and_line
87 when the objfile is deleted. */
92 /* This is called when an objfile is about to be freed. Invalidate
93 the sal object as further actions on the sal would result in bad
94 data. All access to obj->sal should be gated by
95 SALPY_REQUIRE_VALID which will raise an exception on invalid symbol
96 table and line objects. */
99 void operator() (sal_object
*obj
)
101 gdbpy_enter enter_py
;
105 sal_object
*next
= obj
->next
;
107 gdbpy_ref
<> tmp (obj
->symtab
);
108 obj
->symtab
= Py_None
;
121 extern PyTypeObject sal_object_type
122 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("sal_object");
123 static const registry
<objfile
>::key
<sal_object
, salpy_deleter
>
124 salpy_objfile_data_key
;
126 /* Require a valid symbol table and line object. All access to
127 sal_object->sal should be gated by this call. */
128 #define SALPY_REQUIRE_VALID(sal_obj, sal) \
130 sal = sal_object_to_symtab_and_line (sal_obj); \
133 PyErr_SetString (PyExc_RuntimeError, \
134 _("Symbol Table and Line is invalid.")); \
140 stpy_str (PyObject
*self
)
143 struct symtab
*symtab
= NULL
;
145 STPY_REQUIRE_VALID (self
, symtab
);
147 result
= PyUnicode_FromString (symtab_to_filename_for_display (symtab
));
153 stpy_get_filename (PyObject
*self
, void *closure
)
156 struct symtab
*symtab
= NULL
;
157 const char *filename
;
159 STPY_REQUIRE_VALID (self
, symtab
);
160 filename
= symtab_to_filename_for_display (symtab
);
162 str_obj
= host_string_to_python_string (filename
).release ();
167 stpy_get_objfile (PyObject
*self
, void *closure
)
169 struct symtab
*symtab
= NULL
;
171 STPY_REQUIRE_VALID (self
, symtab
);
173 return objfile_to_objfile_object (symtab
->compunit ()->objfile ()).release ();
176 /* Getter function for symtab.producer. */
179 stpy_get_producer (PyObject
*self
, void *closure
)
181 struct symtab
*symtab
= NULL
;
182 struct compunit_symtab
*cust
;
184 STPY_REQUIRE_VALID (self
, symtab
);
185 cust
= symtab
->compunit ();
186 if (cust
->producer () != nullptr)
188 const char *producer
= cust
->producer ();
190 return host_string_to_python_string (producer
).release ();
197 stpy_fullname (PyObject
*self
, PyObject
*args
)
199 const char *fullname
;
200 struct symtab
*symtab
= NULL
;
202 STPY_REQUIRE_VALID (self
, symtab
);
204 fullname
= symtab_to_fullname (symtab
);
206 return host_string_to_python_string (fullname
).release ();
209 /* Implementation of gdb.Symtab.is_valid (self) -> Boolean.
210 Returns True if this Symbol table still exists in GDB. */
213 stpy_is_valid (PyObject
*self
, PyObject
*args
)
215 struct symtab
*symtab
= NULL
;
217 symtab
= symtab_object_to_symtab (self
);
224 /* Return the GLOBAL_BLOCK of the underlying symtab. */
227 stpy_global_block (PyObject
*self
, PyObject
*args
)
229 struct symtab
*symtab
= NULL
;
230 const struct blockvector
*blockvector
;
232 STPY_REQUIRE_VALID (self
, symtab
);
234 blockvector
= symtab
->compunit ()->blockvector ();
235 const struct block
*block
= blockvector
->global_block ();
237 return block_to_block_object (block
, symtab
->compunit ()->objfile ());
240 /* Return the STATIC_BLOCK of the underlying symtab. */
243 stpy_static_block (PyObject
*self
, PyObject
*args
)
245 struct symtab
*symtab
= NULL
;
246 const struct blockvector
*blockvector
;
248 STPY_REQUIRE_VALID (self
, symtab
);
250 blockvector
= symtab
->compunit ()->blockvector ();
251 const struct block
*block
= blockvector
->static_block ();
253 return block_to_block_object (block
, symtab
->compunit ()->objfile ());
256 /* Implementation of gdb.Symtab.linetable (self) -> gdb.LineTable.
257 Returns a gdb.LineTable object corresponding to this symbol
261 stpy_get_linetable (PyObject
*self
, PyObject
*args
)
263 struct symtab
*symtab
= NULL
;
265 STPY_REQUIRE_VALID (self
, symtab
);
267 return symtab_to_linetable_object (self
);
271 salpy_str (PyObject
*self
)
273 const char *filename
;
275 struct symtab_and_line
*sal
= NULL
;
277 SALPY_REQUIRE_VALID (self
, sal
);
279 sal_obj
= (sal_object
*) self
;
280 if (sal_obj
->symtab
== Py_None
)
281 filename
= "<unknown>";
284 symtab
*symtab
= symtab_object_to_symtab (sal_obj
->symtab
);
285 filename
= symtab_to_filename_for_display (symtab
);
288 return PyUnicode_FromFormat ("symbol and line for %s, line %d", filename
,
293 stpy_dealloc (PyObject
*obj
)
295 symtab_object
*symtab
= (symtab_object
*) obj
;
298 symtab
->prev
->next
= symtab
->next
;
299 else if (symtab
->symtab
)
300 stpy_objfile_data_key
.set (symtab
->symtab
->compunit ()->objfile (),
303 symtab
->next
->prev
= symtab
->prev
;
304 symtab
->symtab
= NULL
;
305 Py_TYPE (obj
)->tp_free (obj
);
310 salpy_get_pc (PyObject
*self
, void *closure
)
312 struct symtab_and_line
*sal
= NULL
;
314 SALPY_REQUIRE_VALID (self
, sal
);
316 return gdb_py_object_from_ulongest (sal
->pc
).release ();
319 /* Implementation of the get method for the 'last' attribute of
320 gdb.Symtab_and_line. */
323 salpy_get_last (PyObject
*self
, void *closure
)
325 struct symtab_and_line
*sal
= NULL
;
327 SALPY_REQUIRE_VALID (self
, sal
);
330 return gdb_py_object_from_ulongest (sal
->end
- 1).release ();
336 salpy_get_line (PyObject
*self
, void *closure
)
338 struct symtab_and_line
*sal
= NULL
;
340 SALPY_REQUIRE_VALID (self
, sal
);
342 return gdb_py_object_from_longest (sal
->line
).release ();
346 salpy_get_symtab (PyObject
*self
, void *closure
)
348 struct symtab_and_line
*sal
;
349 sal_object
*self_sal
= (sal_object
*) self
;
351 SALPY_REQUIRE_VALID (self
, sal
);
353 Py_INCREF (self_sal
->symtab
);
355 return (PyObject
*) self_sal
->symtab
;
358 /* Implementation of gdb.Symtab_and_line.is_valid (self) -> Boolean.
359 Returns True if this Symbol table and line object still exists GDB. */
362 salpy_is_valid (PyObject
*self
, PyObject
*args
)
364 struct symtab_and_line
*sal
;
366 sal
= sal_object_to_symtab_and_line (self
);
374 salpy_dealloc (PyObject
*self
)
376 sal_object
*self_sal
= (sal_object
*) self
;
379 self_sal
->prev
->next
= self_sal
->next
;
380 else if (self_sal
->symtab
!= Py_None
)
381 salpy_objfile_data_key
.set
382 (symtab_object_to_symtab (self_sal
->symtab
)->compunit ()->objfile (),
386 self_sal
->next
->prev
= self_sal
->prev
;
388 Py_DECREF (self_sal
->symtab
);
389 xfree (self_sal
->sal
);
390 Py_TYPE (self
)->tp_free (self
);
393 /* Given a sal, and a sal_object that has previously been allocated
394 and initialized, populate the sal_object with the struct sal data.
395 Also, register the sal_object life-cycle with the life-cycle of the
396 object file associated with this sal, if needed. If a failure
397 occurs during the sal population, this function will return -1. */
398 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
399 set_sal (sal_object
*sal_obj
, struct symtab_and_line sal
)
401 PyObject
*symtab_obj
;
405 symtab_obj
= symtab_to_symtab_object (sal
.symtab
);
406 /* If a symtab existed in the sal, but it cannot be duplicated,
408 if (symtab_obj
== NULL
)
413 symtab_obj
= Py_None
;
417 sal_obj
->sal
= ((struct symtab_and_line
*)
418 xmemdup (&sal
, sizeof (struct symtab_and_line
),
419 sizeof (struct symtab_and_line
)));
420 sal_obj
->symtab
= symtab_obj
;
421 sal_obj
->prev
= NULL
;
423 /* If the SAL does not have a symtab, we do not add it to the
424 objfile cleanup observer linked list. */
425 if (sal_obj
->symtab
!= Py_None
)
427 symtab
*symtab
= symtab_object_to_symtab (sal_obj
->symtab
);
430 = salpy_objfile_data_key
.get (symtab
->compunit ()->objfile ());
432 sal_obj
->next
->prev
= sal_obj
;
434 salpy_objfile_data_key
.set (symtab
->compunit ()->objfile (), sal_obj
);
437 sal_obj
->next
= NULL
;
442 /* Given a symtab, and a symtab_object that has previously been
443 allocated and initialized, populate the symtab_object with the
444 struct symtab data. Also, register the symtab_object life-cycle
445 with the life-cycle of the object file associated with this
446 symtab, if needed. */
448 set_symtab (symtab_object
*obj
, struct symtab
*symtab
)
450 obj
->symtab
= symtab
;
454 obj
->next
= stpy_objfile_data_key
.get (symtab
->compunit ()->objfile ());
456 obj
->next
->prev
= obj
;
457 stpy_objfile_data_key
.set (symtab
->compunit ()->objfile (), obj
);
463 /* Create a new symbol table (gdb.Symtab) object that encapsulates the
464 symtab structure from GDB. */
466 symtab_to_symtab_object (struct symtab
*symtab
)
468 symtab_object
*symtab_obj
;
470 symtab_obj
= PyObject_New (symtab_object
, &symtab_object_type
);
472 set_symtab (symtab_obj
, symtab
);
474 return (PyObject
*) symtab_obj
;
477 /* Create a new symtab and line (gdb.Symtab_and_line) object
478 that encapsulates the symtab_and_line structure from GDB. */
480 symtab_and_line_to_sal_object (struct symtab_and_line sal
)
482 gdbpy_ref
<sal_object
> sal_obj (PyObject_New (sal_object
, &sal_object_type
));
485 if (set_sal (sal_obj
.get (), sal
) < 0)
489 return (PyObject
*) sal_obj
.release ();
492 /* Return struct symtab_and_line reference that is wrapped by this
494 struct symtab_and_line
*
495 sal_object_to_symtab_and_line (PyObject
*obj
)
497 if (! PyObject_TypeCheck (obj
, &sal_object_type
))
499 return ((sal_object
*) obj
)->sal
;
502 /* Return struct symtab reference that is wrapped by this object. */
504 symtab_object_to_symtab (PyObject
*obj
)
506 if (! PyObject_TypeCheck (obj
, &symtab_object_type
))
508 return ((symtab_object
*) obj
)->symtab
;
511 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
512 gdbpy_initialize_symtabs (void)
514 symtab_object_type
.tp_new
= PyType_GenericNew
;
515 if (gdbpy_type_ready (&symtab_object_type
) < 0)
518 sal_object_type
.tp_new
= PyType_GenericNew
;
519 if (gdbpy_type_ready (&sal_object_type
) < 0)
525 GDBPY_INITIALIZE_FILE (gdbpy_initialize_symtabs
);
529 static gdb_PyGetSetDef symtab_object_getset
[] = {
530 { "filename", stpy_get_filename
, NULL
,
531 "The symbol table's source filename.", NULL
},
532 { "objfile", stpy_get_objfile
, NULL
, "The symtab's objfile.",
534 { "producer", stpy_get_producer
, NULL
,
535 "The name/version of the program that compiled this symtab.", NULL
},
536 {NULL
} /* Sentinel */
539 static PyMethodDef symtab_object_methods
[] = {
540 { "is_valid", stpy_is_valid
, METH_NOARGS
,
541 "is_valid () -> Boolean.\n\
542 Return true if this symbol table is valid, false if not." },
543 { "fullname", stpy_fullname
, METH_NOARGS
,
544 "fullname () -> String.\n\
545 Return the symtab's full source filename." },
546 { "global_block", stpy_global_block
, METH_NOARGS
,
547 "global_block () -> gdb.Block.\n\
548 Return the global block of the symbol table." },
549 { "static_block", stpy_static_block
, METH_NOARGS
,
550 "static_block () -> gdb.Block.\n\
551 Return the static block of the symbol table." },
552 { "linetable", stpy_get_linetable
, METH_NOARGS
,
553 "linetable () -> gdb.LineTable.\n\
554 Return the LineTable associated with this symbol table" },
555 {NULL
} /* Sentinel */
558 PyTypeObject symtab_object_type
= {
559 PyVarObject_HEAD_INIT (NULL
, 0)
560 "gdb.Symtab", /*tp_name*/
561 sizeof (symtab_object
), /*tp_basicsize*/
563 stpy_dealloc
, /*tp_dealloc*/
570 0, /*tp_as_sequence*/
578 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
579 "GDB symtab object", /*tp_doc */
582 0, /*tp_richcompare */
583 0, /*tp_weaklistoffset */
586 symtab_object_methods
, /*tp_methods */
588 symtab_object_getset
/*tp_getset */
591 static gdb_PyGetSetDef sal_object_getset
[] = {
592 { "symtab", salpy_get_symtab
, NULL
, "Symtab object.", NULL
},
593 { "pc", salpy_get_pc
, NULL
, "Return the symtab_and_line's pc.", NULL
},
594 { "last", salpy_get_last
, NULL
,
595 "Return the symtab_and_line's last address.", NULL
},
596 { "line", salpy_get_line
, NULL
,
597 "Return the symtab_and_line's line.", NULL
},
598 {NULL
} /* Sentinel */
601 static PyMethodDef sal_object_methods
[] = {
602 { "is_valid", salpy_is_valid
, METH_NOARGS
,
603 "is_valid () -> Boolean.\n\
604 Return true if this symbol table and line is valid, false if not." },
605 {NULL
} /* Sentinel */
608 PyTypeObject sal_object_type
= {
609 PyVarObject_HEAD_INIT (NULL
, 0)
610 "gdb.Symtab_and_line", /*tp_name*/
611 sizeof (sal_object
), /*tp_basicsize*/
613 salpy_dealloc
, /*tp_dealloc*/
620 0, /*tp_as_sequence*/
624 salpy_str
, /*tp_str*/
628 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
629 "GDB symtab_and_line object", /*tp_doc */
632 0, /*tp_richcompare */
633 0, /*tp_weaklistoffset */
636 sal_object_methods
, /*tp_methods */
638 sal_object_getset
/*tp_getset */