1 /* Python interface to symbol tables.
3 Copyright (C) 2008-2023 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 struct 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
40 /* This function is called when an objfile is about to be freed.
41 Invalidate the symbol table as further actions on the symbol table
42 would result in bad data. All access to obj->symtab should be
43 gated by STPY_REQUIRE_VALID which will raise an exception on
44 invalid symbol tables. */
47 void operator() (symtab_object
*obj
)
51 symtab_object
*next
= obj
->next
;
61 extern PyTypeObject symtab_object_type
62 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("symtab_object");
63 static const registry
<objfile
>::key
<symtab_object
, stpy_deleter
>
64 stpy_objfile_data_key
;
66 /* Require a valid symbol table. All access to symtab_object->symtab
67 should be gated by this call. */
68 #define STPY_REQUIRE_VALID(symtab_obj, symtab) \
70 symtab = symtab_object_to_symtab (symtab_obj); \
73 PyErr_SetString (PyExc_RuntimeError, \
74 _("Symbol Table is invalid.")); \
81 /* The GDB Symbol table structure. */
83 /* The GDB Symbol table and line structure. */
84 struct symtab_and_line
*sal
;
85 /* A Symtab and line object is associated with an objfile, so keep
86 track with a doubly-linked list, rooted in the objfile. This
87 allows invalidation of the underlying struct symtab_and_line
88 when the objfile is deleted. */
93 /* This is called when an objfile is about to be freed. Invalidate
94 the sal object as further actions on the sal would result in bad
95 data. All access to obj->sal should be gated by
96 SALPY_REQUIRE_VALID which will raise an exception on invalid symbol
97 table and line objects. */
100 void operator() (sal_object
*obj
)
102 gdbpy_enter enter_py
;
106 sal_object
*next
= obj
->next
;
108 gdbpy_ref
<> tmp (obj
->symtab
);
109 obj
->symtab
= Py_None
;
122 extern PyTypeObject sal_object_type
123 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("sal_object");
124 static const registry
<objfile
>::key
<sal_object
, salpy_deleter
>
125 salpy_objfile_data_key
;
127 /* Require a valid symbol table and line object. All access to
128 sal_object->sal should be gated by this call. */
129 #define SALPY_REQUIRE_VALID(sal_obj, sal) \
131 sal = sal_object_to_symtab_and_line (sal_obj); \
134 PyErr_SetString (PyExc_RuntimeError, \
135 _("Symbol Table and Line is invalid.")); \
141 stpy_str (PyObject
*self
)
144 struct symtab
*symtab
= NULL
;
146 STPY_REQUIRE_VALID (self
, symtab
);
148 result
= PyUnicode_FromString (symtab_to_filename_for_display (symtab
));
154 stpy_get_filename (PyObject
*self
, void *closure
)
157 struct symtab
*symtab
= NULL
;
158 const char *filename
;
160 STPY_REQUIRE_VALID (self
, symtab
);
161 filename
= symtab_to_filename_for_display (symtab
);
163 str_obj
= host_string_to_python_string (filename
).release ();
168 stpy_get_objfile (PyObject
*self
, void *closure
)
170 struct symtab
*symtab
= NULL
;
172 STPY_REQUIRE_VALID (self
, symtab
);
174 return objfile_to_objfile_object (symtab
->compunit ()->objfile ()).release ();
177 /* Getter function for symtab.producer. */
180 stpy_get_producer (PyObject
*self
, void *closure
)
182 struct symtab
*symtab
= NULL
;
183 struct compunit_symtab
*cust
;
185 STPY_REQUIRE_VALID (self
, symtab
);
186 cust
= symtab
->compunit ();
187 if (cust
->producer () != nullptr)
189 const char *producer
= cust
->producer ();
191 return host_string_to_python_string (producer
).release ();
198 stpy_fullname (PyObject
*self
, PyObject
*args
)
200 const char *fullname
;
201 struct symtab
*symtab
= NULL
;
203 STPY_REQUIRE_VALID (self
, symtab
);
205 fullname
= symtab_to_fullname (symtab
);
207 return host_string_to_python_string (fullname
).release ();
210 /* Implementation of gdb.Symtab.is_valid (self) -> Boolean.
211 Returns True if this Symbol table still exists in GDB. */
214 stpy_is_valid (PyObject
*self
, PyObject
*args
)
216 struct symtab
*symtab
= NULL
;
218 symtab
= symtab_object_to_symtab (self
);
225 /* Return the GLOBAL_BLOCK of the underlying symtab. */
228 stpy_global_block (PyObject
*self
, PyObject
*args
)
230 struct symtab
*symtab
= NULL
;
231 const struct blockvector
*blockvector
;
233 STPY_REQUIRE_VALID (self
, symtab
);
235 blockvector
= symtab
->compunit ()->blockvector ();
236 const struct block
*block
= blockvector
->global_block ();
238 return block_to_block_object (block
, symtab
->compunit ()->objfile ());
241 /* Return the STATIC_BLOCK of the underlying symtab. */
244 stpy_static_block (PyObject
*self
, PyObject
*args
)
246 struct symtab
*symtab
= NULL
;
247 const struct blockvector
*blockvector
;
249 STPY_REQUIRE_VALID (self
, symtab
);
251 blockvector
= symtab
->compunit ()->blockvector ();
252 const struct block
*block
= blockvector
->static_block ();
254 return block_to_block_object (block
, symtab
->compunit ()->objfile ());
257 /* Implementation of gdb.Symtab.linetable (self) -> gdb.LineTable.
258 Returns a gdb.LineTable object corresponding to this symbol
262 stpy_get_linetable (PyObject
*self
, PyObject
*args
)
264 struct symtab
*symtab
= NULL
;
266 STPY_REQUIRE_VALID (self
, symtab
);
268 return symtab_to_linetable_object (self
);
272 salpy_str (PyObject
*self
)
274 const char *filename
;
276 struct symtab_and_line
*sal
= NULL
;
278 SALPY_REQUIRE_VALID (self
, sal
);
280 sal_obj
= (sal_object
*) self
;
281 if (sal_obj
->symtab
== Py_None
)
282 filename
= "<unknown>";
285 symtab
*symtab
= symtab_object_to_symtab (sal_obj
->symtab
);
286 filename
= symtab_to_filename_for_display (symtab
);
289 return PyUnicode_FromFormat ("symbol and line for %s, line %d", filename
,
294 stpy_dealloc (PyObject
*obj
)
296 symtab_object
*symtab
= (symtab_object
*) obj
;
299 symtab
->prev
->next
= symtab
->next
;
300 else if (symtab
->symtab
)
301 stpy_objfile_data_key
.set (symtab
->symtab
->compunit ()->objfile (),
304 symtab
->next
->prev
= symtab
->prev
;
305 symtab
->symtab
= NULL
;
306 Py_TYPE (obj
)->tp_free (obj
);
311 salpy_get_pc (PyObject
*self
, void *closure
)
313 struct symtab_and_line
*sal
= NULL
;
315 SALPY_REQUIRE_VALID (self
, sal
);
317 return gdb_py_object_from_ulongest (sal
->pc
).release ();
320 /* Implementation of the get method for the 'last' attribute of
321 gdb.Symtab_and_line. */
324 salpy_get_last (PyObject
*self
, void *closure
)
326 struct symtab_and_line
*sal
= NULL
;
328 SALPY_REQUIRE_VALID (self
, sal
);
331 return gdb_py_object_from_ulongest (sal
->end
- 1).release ();
337 salpy_get_line (PyObject
*self
, void *closure
)
339 struct symtab_and_line
*sal
= NULL
;
341 SALPY_REQUIRE_VALID (self
, sal
);
343 return gdb_py_object_from_longest (sal
->line
).release ();
347 salpy_get_symtab (PyObject
*self
, void *closure
)
349 struct symtab_and_line
*sal
;
350 sal_object
*self_sal
= (sal_object
*) self
;
352 SALPY_REQUIRE_VALID (self
, sal
);
354 Py_INCREF (self_sal
->symtab
);
356 return (PyObject
*) self_sal
->symtab
;
359 /* Implementation of gdb.Symtab_and_line.is_valid (self) -> Boolean.
360 Returns True if this Symbol table and line object still exists GDB. */
363 salpy_is_valid (PyObject
*self
, PyObject
*args
)
365 struct symtab_and_line
*sal
;
367 sal
= sal_object_to_symtab_and_line (self
);
375 salpy_dealloc (PyObject
*self
)
377 sal_object
*self_sal
= (sal_object
*) self
;
380 self_sal
->prev
->next
= self_sal
->next
;
381 else if (self_sal
->symtab
!= Py_None
)
382 salpy_objfile_data_key
.set
383 (symtab_object_to_symtab (self_sal
->symtab
)->compunit ()->objfile (),
387 self_sal
->next
->prev
= self_sal
->prev
;
389 Py_DECREF (self_sal
->symtab
);
390 xfree (self_sal
->sal
);
391 Py_TYPE (self
)->tp_free (self
);
394 /* Given a sal, and a sal_object that has previously been allocated
395 and initialized, populate the sal_object with the struct sal data.
396 Also, register the sal_object life-cycle with the life-cycle of the
397 object file associated with this sal, if needed. If a failure
398 occurs during the sal population, this function will return -1. */
399 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
400 set_sal (sal_object
*sal_obj
, struct symtab_and_line sal
)
402 PyObject
*symtab_obj
;
406 symtab_obj
= symtab_to_symtab_object (sal
.symtab
);
407 /* If a symtab existed in the sal, but it cannot be duplicated,
409 if (symtab_obj
== NULL
)
414 symtab_obj
= Py_None
;
418 sal_obj
->sal
= ((struct symtab_and_line
*)
419 xmemdup (&sal
, sizeof (struct symtab_and_line
),
420 sizeof (struct symtab_and_line
)));
421 sal_obj
->symtab
= symtab_obj
;
422 sal_obj
->prev
= NULL
;
424 /* If the SAL does not have a symtab, we do not add it to the
425 objfile cleanup observer linked list. */
426 if (sal_obj
->symtab
!= Py_None
)
428 symtab
*symtab
= symtab_object_to_symtab (sal_obj
->symtab
);
431 = salpy_objfile_data_key
.get (symtab
->compunit ()->objfile ());
433 sal_obj
->next
->prev
= sal_obj
;
435 salpy_objfile_data_key
.set (symtab
->compunit ()->objfile (), sal_obj
);
438 sal_obj
->next
= NULL
;
443 /* Given a symtab, and a symtab_object that has previously been
444 allocated and initialized, populate the symtab_object with the
445 struct symtab data. Also, register the symtab_object life-cycle
446 with the life-cycle of the object file associated with this
447 symtab, if needed. */
449 set_symtab (symtab_object
*obj
, struct symtab
*symtab
)
451 obj
->symtab
= symtab
;
455 obj
->next
= stpy_objfile_data_key
.get (symtab
->compunit ()->objfile ());
457 obj
->next
->prev
= obj
;
458 stpy_objfile_data_key
.set (symtab
->compunit ()->objfile (), obj
);
464 /* Create a new symbol table (gdb.Symtab) object that encapsulates the
465 symtab structure from GDB. */
467 symtab_to_symtab_object (struct symtab
*symtab
)
469 symtab_object
*symtab_obj
;
471 symtab_obj
= PyObject_New (symtab_object
, &symtab_object_type
);
473 set_symtab (symtab_obj
, symtab
);
475 return (PyObject
*) symtab_obj
;
478 /* Create a new symtab and line (gdb.Symtab_and_line) object
479 that encapsulates the symtab_and_line structure from GDB. */
481 symtab_and_line_to_sal_object (struct symtab_and_line sal
)
483 gdbpy_ref
<sal_object
> sal_obj (PyObject_New (sal_object
, &sal_object_type
));
486 if (set_sal (sal_obj
.get (), sal
) < 0)
490 return (PyObject
*) sal_obj
.release ();
493 /* Return struct symtab_and_line reference that is wrapped by this
495 struct symtab_and_line
*
496 sal_object_to_symtab_and_line (PyObject
*obj
)
498 if (! PyObject_TypeCheck (obj
, &sal_object_type
))
500 return ((sal_object
*) obj
)->sal
;
503 /* Return struct symtab reference that is wrapped by this object. */
505 symtab_object_to_symtab (PyObject
*obj
)
507 if (! PyObject_TypeCheck (obj
, &symtab_object_type
))
509 return ((symtab_object
*) obj
)->symtab
;
513 gdbpy_initialize_symtabs (void)
515 symtab_object_type
.tp_new
= PyType_GenericNew
;
516 if (PyType_Ready (&symtab_object_type
) < 0)
519 sal_object_type
.tp_new
= PyType_GenericNew
;
520 if (PyType_Ready (&sal_object_type
) < 0)
523 if (gdb_pymodule_addobject (gdb_module
, "Symtab",
524 (PyObject
*) &symtab_object_type
) < 0)
527 return gdb_pymodule_addobject (gdb_module
, "Symtab_and_line",
528 (PyObject
*) &sal_object_type
);
533 static gdb_PyGetSetDef symtab_object_getset
[] = {
534 { "filename", stpy_get_filename
, NULL
,
535 "The symbol table's source filename.", NULL
},
536 { "objfile", stpy_get_objfile
, NULL
, "The symtab's objfile.",
538 { "producer", stpy_get_producer
, NULL
,
539 "The name/version of the program that compiled this symtab.", NULL
},
540 {NULL
} /* Sentinel */
543 static PyMethodDef symtab_object_methods
[] = {
544 { "is_valid", stpy_is_valid
, METH_NOARGS
,
545 "is_valid () -> Boolean.\n\
546 Return true if this symbol table is valid, false if not." },
547 { "fullname", stpy_fullname
, METH_NOARGS
,
548 "fullname () -> String.\n\
549 Return the symtab's full source filename." },
550 { "global_block", stpy_global_block
, METH_NOARGS
,
551 "global_block () -> gdb.Block.\n\
552 Return the global block of the symbol table." },
553 { "static_block", stpy_static_block
, METH_NOARGS
,
554 "static_block () -> gdb.Block.\n\
555 Return the static block of the symbol table." },
556 { "linetable", stpy_get_linetable
, METH_NOARGS
,
557 "linetable () -> gdb.LineTable.\n\
558 Return the LineTable associated with this symbol table" },
559 {NULL
} /* Sentinel */
562 PyTypeObject symtab_object_type
= {
563 PyVarObject_HEAD_INIT (NULL
, 0)
564 "gdb.Symtab", /*tp_name*/
565 sizeof (symtab_object
), /*tp_basicsize*/
567 stpy_dealloc
, /*tp_dealloc*/
574 0, /*tp_as_sequence*/
582 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
583 "GDB symtab object", /*tp_doc */
586 0, /*tp_richcompare */
587 0, /*tp_weaklistoffset */
590 symtab_object_methods
, /*tp_methods */
592 symtab_object_getset
/*tp_getset */
595 static gdb_PyGetSetDef sal_object_getset
[] = {
596 { "symtab", salpy_get_symtab
, NULL
, "Symtab object.", NULL
},
597 { "pc", salpy_get_pc
, NULL
, "Return the symtab_and_line's pc.", NULL
},
598 { "last", salpy_get_last
, NULL
,
599 "Return the symtab_and_line's last address.", NULL
},
600 { "line", salpy_get_line
, NULL
,
601 "Return the symtab_and_line's line.", NULL
},
602 {NULL
} /* Sentinel */
605 static PyMethodDef sal_object_methods
[] = {
606 { "is_valid", salpy_is_valid
, METH_NOARGS
,
607 "is_valid () -> Boolean.\n\
608 Return true if this symbol table and line is valid, false if not." },
609 {NULL
} /* Sentinel */
612 PyTypeObject sal_object_type
= {
613 PyVarObject_HEAD_INIT (NULL
, 0)
614 "gdb.Symtab_and_line", /*tp_name*/
615 sizeof (sal_object
), /*tp_basicsize*/
617 salpy_dealloc
, /*tp_dealloc*/
624 0, /*tp_as_sequence*/
628 salpy_str
, /*tp_str*/
632 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
633 "GDB symtab_and_line object", /*tp_doc */
636 0, /*tp_richcompare */
637 0, /*tp_weaklistoffset */
640 sal_object_methods
, /*tp_methods */
642 sal_object_getset
/*tp_getset */