]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/python/py-symtab.c
sim: ppc: use correct macros
[thirdparty/binutils-gdb.git] / gdb / python / py-symtab.c
CommitLineData
f3e9a817
PM
1/* Python interface to symbol tables.
2
d01e8234 3 Copyright (C) 2008-2025 Free Software Foundation, Inc.
f3e9a817
PM
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
f3e9a817
PM
20#include "charset.h"
21#include "symtab.h"
22#include "source.h"
23#include "python-internal.h"
24#include "objfiles.h"
a20ee7a4 25#include "block.h"
f3e9a817 26
f99b5177 27struct symtab_object {
f3e9a817
PM
28 PyObject_HEAD
29 /* The GDB Symbol table structure. */
30 struct symtab *symtab;
08b8a139
TT
31};
32
e36122e9 33extern PyTypeObject symtab_object_type
62eec1a5 34 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("symtab_object");
f55f9247
JV
35static const gdbpy_registry<gdbpy_memoizing_registry_storage<symtab_object,
36 symtab, &symtab_object::symtab>> stpy_registry;
f3e9a817
PM
37
38/* Require a valid symbol table. All access to symtab_object->symtab
39 should be gated by this call. */
40#define STPY_REQUIRE_VALID(symtab_obj, symtab) \
41 do { \
42 symtab = symtab_object_to_symtab (symtab_obj); \
43 if (symtab == NULL) \
44 { \
45 PyErr_SetString (PyExc_RuntimeError, \
46 _("Symbol Table is invalid.")); \
47 return NULL; \
48 } \
49 } while (0)
50
f99b5177 51struct sal_object {
f3e9a817 52 PyObject_HEAD
f3e9a817
PM
53 /* The GDB Symbol table and line structure. */
54 struct symtab_and_line *sal;
55 /* A Symtab and line object is associated with an objfile, so keep
56 track with a doubly-linked list, rooted in the objfile. This
57 allows invalidation of the underlying struct symtab_and_line
58 when the objfile is deleted. */
f99b5177
TT
59 sal_object *prev;
60 sal_object *next;
61};
f3e9a817 62
08b8a139
TT
63/* This is called when an objfile is about to be freed. Invalidate
64 the sal object as further actions on the sal would result in bad
65 data. All access to obj->sal should be gated by
66 SALPY_REQUIRE_VALID which will raise an exception on invalid symbol
67 table and line objects. */
d1da7bb4 68struct salpy_invalidator
08b8a139
TT
69{
70 void operator() (sal_object *obj)
71 {
d1da7bb4
JV
72 xfree (obj->sal);
73 obj->sal = nullptr;
08b8a139
TT
74 }
75};
76
e36122e9 77extern PyTypeObject sal_object_type
62eec1a5 78 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("sal_object");
d1da7bb4
JV
79static const gdbpy_registry<gdbpy_tracking_registry_storage<sal_object,
80 symtab_and_line, &sal_object::sal, salpy_invalidator>> salpy_registry;
f3e9a817
PM
81
82/* Require a valid symbol table and line object. All access to
83 sal_object->sal should be gated by this call. */
84#define SALPY_REQUIRE_VALID(sal_obj, sal) \
85 do { \
86 sal = sal_object_to_symtab_and_line (sal_obj); \
87 if (sal == NULL) \
88 { \
89 PyErr_SetString (PyExc_RuntimeError, \
90 _("Symbol Table and Line is invalid.")); \
91 return NULL; \
92 } \
93 } while (0)
94
95static PyObject *
96stpy_str (PyObject *self)
97{
98 PyObject *result;
99 struct symtab *symtab = NULL;
100
101 STPY_REQUIRE_VALID (self, symtab);
102
5aee4587 103 result = PyUnicode_FromString (symtab_to_filename_for_display (symtab));
f3e9a817
PM
104
105 return result;
106}
107
108static PyObject *
109stpy_get_filename (PyObject *self, void *closure)
110{
111 PyObject *str_obj;
112 struct symtab *symtab = NULL;
05cba821 113 const char *filename;
f3e9a817
PM
114
115 STPY_REQUIRE_VALID (self, symtab);
05cba821 116 filename = symtab_to_filename_for_display (symtab);
f3e9a817 117
833d985d 118 str_obj = host_string_to_python_string (filename).release ();
f3e9a817
PM
119 return str_obj;
120}
121
122static PyObject *
123stpy_get_objfile (PyObject *self, void *closure)
124{
125 struct symtab *symtab = NULL;
f3e9a817
PM
126
127 STPY_REQUIRE_VALID (self, symtab);
128
3c86fae3 129 return objfile_to_objfile_object (symtab->compunit ()->objfile ()).release ();
f3e9a817
PM
130}
131
2b4fd423
DE
132/* Getter function for symtab.producer. */
133
134static PyObject *
135stpy_get_producer (PyObject *self, void *closure)
136{
137 struct symtab *symtab = NULL;
43f3e411 138 struct compunit_symtab *cust;
2b4fd423
DE
139
140 STPY_REQUIRE_VALID (self, symtab);
c6159652 141 cust = symtab->compunit ();
ab5f850e 142 if (cust->producer () != nullptr)
2b4fd423 143 {
ab5f850e 144 const char *producer = cust->producer ();
2b4fd423 145
833d985d 146 return host_string_to_python_string (producer).release ();
2b4fd423
DE
147 }
148
149 Py_RETURN_NONE;
150}
151
f3e9a817
PM
152static PyObject *
153stpy_fullname (PyObject *self, PyObject *args)
154{
0b0865da 155 const char *fullname;
f3e9a817
PM
156 struct symtab *symtab = NULL;
157
158 STPY_REQUIRE_VALID (self, symtab);
159
160 fullname = symtab_to_fullname (symtab);
f3e9a817 161
833d985d 162 return host_string_to_python_string (fullname).release ();
f3e9a817
PM
163}
164
29703da4
PM
165/* Implementation of gdb.Symtab.is_valid (self) -> Boolean.
166 Returns True if this Symbol table still exists in GDB. */
167
168static PyObject *
169stpy_is_valid (PyObject *self, PyObject *args)
170{
171 struct symtab *symtab = NULL;
172
173 symtab = symtab_object_to_symtab (self);
174 if (symtab == NULL)
175 Py_RETURN_FALSE;
176
177 Py_RETURN_TRUE;
178}
179
a20ee7a4
SCR
180/* Return the GLOBAL_BLOCK of the underlying symtab. */
181
182static PyObject *
183stpy_global_block (PyObject *self, PyObject *args)
184{
185 struct symtab *symtab = NULL;
346d1dfe 186 const struct blockvector *blockvector;
a20ee7a4
SCR
187
188 STPY_REQUIRE_VALID (self, symtab);
189
44281e6c 190 blockvector = symtab->compunit ()->blockvector ();
63d609de
SM
191 const struct block *block = blockvector->global_block ();
192
3c86fae3 193 return block_to_block_object (block, symtab->compunit ()->objfile ());
a20ee7a4
SCR
194}
195
196/* Return the STATIC_BLOCK of the underlying symtab. */
197
198static PyObject *
199stpy_static_block (PyObject *self, PyObject *args)
200{
201 struct symtab *symtab = NULL;
346d1dfe 202 const struct blockvector *blockvector;
a20ee7a4
SCR
203
204 STPY_REQUIRE_VALID (self, symtab);
205
44281e6c 206 blockvector = symtab->compunit ()->blockvector ();
63d609de
SM
207 const struct block *block = blockvector->static_block ();
208
3c86fae3 209 return block_to_block_object (block, symtab->compunit ()->objfile ());
a20ee7a4
SCR
210}
211
4efd80aa
CS
212/* Implementation of gdb.Symtab.linetable (self) -> gdb.LineTable.
213 Returns a gdb.LineTable object corresponding to this symbol
bc79de95
PM
214 table. */
215
216static PyObject *
217stpy_get_linetable (PyObject *self, PyObject *args)
218{
219 struct symtab *symtab = NULL;
220
221 STPY_REQUIRE_VALID (self, symtab);
222
223 return symtab_to_linetable_object (self);
224}
225
f3e9a817
PM
226static PyObject *
227salpy_str (PyObject *self)
228{
05cba821 229 const char *filename;
f3e9a817 230 sal_object *sal_obj;
aaefa902 231 struct symtab_and_line *sal = nullptr;
f3e9a817
PM
232
233 SALPY_REQUIRE_VALID (self, sal);
234
235 sal_obj = (sal_object *) self;
aaefa902 236 if (sal_obj->sal->symtab == nullptr)
7c711119
TT
237 filename = "<unknown>";
238 else
aaefa902 239 filename = symtab_to_filename_for_display (sal_obj->sal->symtab);
f3e9a817 240
5aee4587
SM
241 return PyUnicode_FromFormat ("symbol and line for %s, line %d", filename,
242 sal->line);
f3e9a817
PM
243}
244
245static void
246stpy_dealloc (PyObject *obj)
247{
f55f9247
JV
248 symtab_object *symtab_obj = (symtab_object *) obj;
249
250 if (symtab_obj->symtab != nullptr)
251 stpy_registry.remove (symtab_obj->symtab->compunit ()->objfile(),
252 symtab_obj);
253
2e953aca 254 Py_TYPE (obj)->tp_free (obj);
f3e9a817
PM
255}
256
257
258static PyObject *
259salpy_get_pc (PyObject *self, void *closure)
260{
261 struct symtab_and_line *sal = NULL;
262
263 SALPY_REQUIRE_VALID (self, sal);
264
d1cab987 265 return gdb_py_object_from_ulongest (sal->pc).release ();
f3e9a817
PM
266}
267
ee0bf529
SCR
268/* Implementation of the get method for the 'last' attribute of
269 gdb.Symtab_and_line. */
270
271static PyObject *
272salpy_get_last (PyObject *self, void *closure)
273{
274 struct symtab_and_line *sal = NULL;
275
276 SALPY_REQUIRE_VALID (self, sal);
277
278 if (sal->end > 0)
d1cab987 279 return gdb_py_object_from_ulongest (sal->end - 1).release ();
ee0bf529
SCR
280 else
281 Py_RETURN_NONE;
282}
283
f3e9a817
PM
284static PyObject *
285salpy_get_line (PyObject *self, void *closure)
286{
287 struct symtab_and_line *sal = NULL;
288
289 SALPY_REQUIRE_VALID (self, sal);
290
47f0e2ff 291 return gdb_py_object_from_longest (sal->line).release ();
f3e9a817
PM
292}
293
294static PyObject *
295salpy_get_symtab (PyObject *self, void *closure)
296{
297 struct symtab_and_line *sal;
f3e9a817
PM
298
299 SALPY_REQUIRE_VALID (self, sal);
300
aaefa902
JV
301 if (sal->symtab == nullptr)
302 Py_RETURN_NONE;
303 else
304 return symtab_to_symtab_object (sal->symtab);
f3e9a817
PM
305}
306
29703da4
PM
307/* Implementation of gdb.Symtab_and_line.is_valid (self) -> Boolean.
308 Returns True if this Symbol table and line object still exists GDB. */
309
310static PyObject *
311salpy_is_valid (PyObject *self, PyObject *args)
312{
313 struct symtab_and_line *sal;
314
315 sal = sal_object_to_symtab_and_line (self);
316 if (sal == NULL)
317 Py_RETURN_FALSE;
318
319 Py_RETURN_TRUE;
320}
321
f3e9a817
PM
322static void
323salpy_dealloc (PyObject *self)
324{
325 sal_object *self_sal = (sal_object *) self;
326
d1da7bb4
JV
327 if (self_sal->sal != nullptr && self_sal->sal->symtab != nullptr)
328 salpy_registry.remove (self_sal->sal->symtab->compunit ()->objfile (),
329 self_sal);
f3e9a817 330
f3e9a817 331 xfree (self_sal->sal);
9a27f2c6 332 Py_TYPE (self)->tp_free (self);
f3e9a817
PM
333}
334
b021a221
MS
335/* Given a sal, and a sal_object that has previously been allocated
336 and initialized, populate the sal_object with the struct sal data.
337 Also, register the sal_object life-cycle with the life-cycle of the
338 object file associated with this sal, if needed. If a failure
33ee792f 339 occurs during the sal population, this function will return -1. */
aaefa902 340static void
f3e9a817
PM
341set_sal (sal_object *sal_obj, struct symtab_and_line sal)
342{
224c3ddb
SM
343 sal_obj->sal = ((struct symtab_and_line *)
344 xmemdup (&sal, sizeof (struct symtab_and_line),
345 sizeof (struct symtab_and_line)));
d1da7bb4
JV
346 sal_obj->prev = nullptr;
347 sal_obj->next = nullptr;
f3e9a817
PM
348
349 /* If the SAL does not have a symtab, we do not add it to the
350 objfile cleanup observer linked list. */
aaefa902
JV
351 symtab *symtab = sal_obj->sal->symtab;
352 if (symtab != nullptr)
d1da7bb4 353 salpy_registry.add (symtab->compunit ()->objfile (), sal_obj);
f3e9a817
PM
354}
355
356/* Given a symtab, and a symtab_object that has previously been
357 allocated and initialized, populate the symtab_object with the
358 struct symtab data. Also, register the symtab_object life-cycle
b021a221 359 with the life-cycle of the object file associated with this
f3e9a817
PM
360 symtab, if needed. */
361static void
362set_symtab (symtab_object *obj, struct symtab *symtab)
363{
364 obj->symtab = symtab;
14b8ef75 365 if (symtab != nullptr)
f55f9247 366 stpy_registry.add (symtab->compunit ()->objfile (), obj);
f3e9a817
PM
367}
368
369/* Create a new symbol table (gdb.Symtab) object that encapsulates the
370 symtab structure from GDB. */
371PyObject *
372symtab_to_symtab_object (struct symtab *symtab)
373{
374 symtab_object *symtab_obj;
375
14b8ef75
JV
376 /* Look if there's already a gdb.Symtab object for given SYMTAB
377 and if so, return it. */
378 if (symtab != nullptr)
379 {
f55f9247
JV
380 symtab_obj = stpy_registry.lookup (symtab->compunit ()->objfile (),
381 symtab);
382 if (symtab_obj != nullptr)
383 return (PyObject*)symtab_obj;
14b8ef75
JV
384 }
385
f3e9a817
PM
386 symtab_obj = PyObject_New (symtab_object, &symtab_object_type);
387 if (symtab_obj)
388 set_symtab (symtab_obj, symtab);
389
390 return (PyObject *) symtab_obj;
391}
392
393/* Create a new symtab and line (gdb.Symtab_and_line) object
394 that encapsulates the symtab_and_line structure from GDB. */
395PyObject *
396symtab_and_line_to_sal_object (struct symtab_and_line sal)
f3e9a817 397{
aaefa902
JV
398 sal_object *sal_obj;
399
400 sal_obj = PyObject_New (sal_object, &sal_object_type);
401 if (sal_obj != nullptr)
402 set_sal (sal_obj, sal);
f3e9a817 403
aaefa902 404 return (PyObject *) sal_obj;
f3e9a817
PM
405}
406
407/* Return struct symtab_and_line reference that is wrapped by this
408 object. */
409struct symtab_and_line *
410sal_object_to_symtab_and_line (PyObject *obj)
411{
412 if (! PyObject_TypeCheck (obj, &sal_object_type))
413 return NULL;
414 return ((sal_object *) obj)->sal;
415}
416
417/* Return struct symtab reference that is wrapped by this object. */
418struct symtab *
419symtab_object_to_symtab (PyObject *obj)
420{
421 if (! PyObject_TypeCheck (obj, &symtab_object_type))
422 return NULL;
423 return ((symtab_object *) obj)->symtab;
424}
425
3965bff5 426static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
f3e9a817
PM
427gdbpy_initialize_symtabs (void)
428{
429 symtab_object_type.tp_new = PyType_GenericNew;
336bb2a1 430 if (gdbpy_type_ready (&symtab_object_type) < 0)
999633ed 431 return -1;
f3e9a817
PM
432
433 sal_object_type.tp_new = PyType_GenericNew;
336bb2a1 434 if (gdbpy_type_ready (&sal_object_type) < 0)
999633ed 435 return -1;
f3e9a817 436
336bb2a1 437 return 0;
f3e9a817
PM
438}
439
3965bff5
AB
440GDBPY_INITIALIZE_FILE (gdbpy_initialize_symtabs);
441
f3e9a817
PM
442\f
443
0d1f4ceb 444static gdb_PyGetSetDef symtab_object_getset[] = {
f3e9a817
PM
445 { "filename", stpy_get_filename, NULL,
446 "The symbol table's source filename.", NULL },
447 { "objfile", stpy_get_objfile, NULL, "The symtab's objfile.",
448 NULL },
2b4fd423
DE
449 { "producer", stpy_get_producer, NULL,
450 "The name/version of the program that compiled this symtab.", NULL },
f3e9a817
PM
451 {NULL} /* Sentinel */
452};
453
454static PyMethodDef symtab_object_methods[] = {
29703da4
PM
455 { "is_valid", stpy_is_valid, METH_NOARGS,
456 "is_valid () -> Boolean.\n\
457Return true if this symbol table is valid, false if not." },
f3e9a817
PM
458 { "fullname", stpy_fullname, METH_NOARGS,
459 "fullname () -> String.\n\
460Return the symtab's full source filename." },
a20ee7a4
SCR
461 { "global_block", stpy_global_block, METH_NOARGS,
462 "global_block () -> gdb.Block.\n\
463Return the global block of the symbol table." },
464 { "static_block", stpy_static_block, METH_NOARGS,
465 "static_block () -> gdb.Block.\n\
466Return the static block of the symbol table." },
bc79de95 467 { "linetable", stpy_get_linetable, METH_NOARGS,
4efd80aa
CS
468 "linetable () -> gdb.LineTable.\n\
469Return the LineTable associated with this symbol table" },
f3e9a817
PM
470 {NULL} /* Sentinel */
471};
472
e36122e9 473PyTypeObject symtab_object_type = {
9a27f2c6 474 PyVarObject_HEAD_INIT (NULL, 0)
f3e9a817
PM
475 "gdb.Symtab", /*tp_name*/
476 sizeof (symtab_object), /*tp_basicsize*/
477 0, /*tp_itemsize*/
f55f9247 478 stpy_dealloc, /*tp_dealloc*/
f3e9a817
PM
479 0, /*tp_print*/
480 0, /*tp_getattr*/
481 0, /*tp_setattr*/
482 0, /*tp_compare*/
483 0, /*tp_repr*/
484 0, /*tp_as_number*/
485 0, /*tp_as_sequence*/
486 0, /*tp_as_mapping*/
487 0, /*tp_hash */
488 0, /*tp_call*/
489 stpy_str, /*tp_str*/
490 0, /*tp_getattro*/
491 0, /*tp_setattro*/
492 0, /*tp_as_buffer*/
493 Py_TPFLAGS_DEFAULT, /*tp_flags*/
494 "GDB symtab object", /*tp_doc */
495 0, /*tp_traverse */
496 0, /*tp_clear */
497 0, /*tp_richcompare */
498 0, /*tp_weaklistoffset */
499 0, /*tp_iter */
500 0, /*tp_iternext */
501 symtab_object_methods, /*tp_methods */
502 0, /*tp_members */
503 symtab_object_getset /*tp_getset */
504};
505
0d1f4ceb 506static gdb_PyGetSetDef sal_object_getset[] = {
f3e9a817
PM
507 { "symtab", salpy_get_symtab, NULL, "Symtab object.", NULL },
508 { "pc", salpy_get_pc, NULL, "Return the symtab_and_line's pc.", NULL },
ee0bf529
SCR
509 { "last", salpy_get_last, NULL,
510 "Return the symtab_and_line's last address.", NULL },
f3e9a817
PM
511 { "line", salpy_get_line, NULL,
512 "Return the symtab_and_line's line.", NULL },
513 {NULL} /* Sentinel */
514};
515
29703da4
PM
516static PyMethodDef sal_object_methods[] = {
517 { "is_valid", salpy_is_valid, METH_NOARGS,
518 "is_valid () -> Boolean.\n\
519Return true if this symbol table and line is valid, false if not." },
520 {NULL} /* Sentinel */
521};
522
e36122e9 523PyTypeObject sal_object_type = {
9a27f2c6 524 PyVarObject_HEAD_INIT (NULL, 0)
f3e9a817
PM
525 "gdb.Symtab_and_line", /*tp_name*/
526 sizeof (sal_object), /*tp_basicsize*/
527 0, /*tp_itemsize*/
528 salpy_dealloc, /*tp_dealloc*/
529 0, /*tp_print*/
530 0, /*tp_getattr*/
531 0, /*tp_setattr*/
532 0, /*tp_compare*/
533 0, /*tp_repr*/
534 0, /*tp_as_number*/
535 0, /*tp_as_sequence*/
536 0, /*tp_as_mapping*/
537 0, /*tp_hash */
538 0, /*tp_call*/
539 salpy_str, /*tp_str*/
540 0, /*tp_getattro*/
541 0, /*tp_setattro*/
542 0, /*tp_as_buffer*/
543 Py_TPFLAGS_DEFAULT, /*tp_flags*/
544 "GDB symtab_and_line object", /*tp_doc */
545 0, /*tp_traverse */
546 0, /*tp_clear */
547 0, /*tp_richcompare */
548 0, /*tp_weaklistoffset */
549 0, /*tp_iter */
550 0, /*tp_iternext */
29703da4 551 sal_object_methods, /*tp_methods */
f3e9a817
PM
552 0, /*tp_members */
553 sal_object_getset /*tp_getset */
554};