arm, objdump: print obsolote warning when 26-bit set in instructions
[binutils-gdb.git] / gdb / python / py-symtab.c
blob99a5094ba6075d913dd90861014164434adbf980
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/>. */
20 #include "charset.h"
21 #include "symtab.h"
22 #include "source.h"
23 #include "python-internal.h"
24 #include "objfiles.h"
25 #include "block.h"
27 struct symtab_object {
28 PyObject_HEAD
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
34 deleted. */
35 symtab_object *prev;
36 symtab_object *next;
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. */
44 struct stpy_deleter
46 void operator() (symtab_object *obj)
48 while (obj)
50 symtab_object *next = obj->next;
52 obj->symtab = NULL;
53 obj->next = NULL;
54 obj->prev = NULL;
55 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) \
68 do { \
69 symtab = symtab_object_to_symtab (symtab_obj); \
70 if (symtab == NULL) \
71 { \
72 PyErr_SetString (PyExc_RuntimeError, \
73 _("Symbol Table is invalid.")); \
74 return NULL; \
75 } \
76 } while (0)
78 struct sal_object {
79 PyObject_HEAD
80 /* The GDB Symbol table structure. */
81 PyObject *symtab;
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. */
88 sal_object *prev;
89 sal_object *next;
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. */
97 struct salpy_deleter
99 void operator() (sal_object *obj)
101 gdbpy_enter enter_py;
103 while (obj)
105 sal_object *next = obj->next;
107 gdbpy_ref<> tmp (obj->symtab);
108 obj->symtab = Py_None;
109 Py_INCREF (Py_None);
111 obj->next = NULL;
112 obj->prev = NULL;
113 xfree (obj->sal);
114 obj->sal = NULL;
116 obj = next;
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) \
129 do { \
130 sal = sal_object_to_symtab_and_line (sal_obj); \
131 if (sal == NULL) \
133 PyErr_SetString (PyExc_RuntimeError, \
134 _("Symbol Table and Line is invalid.")); \
135 return NULL; \
137 } while (0)
139 static PyObject *
140 stpy_str (PyObject *self)
142 PyObject *result;
143 struct symtab *symtab = NULL;
145 STPY_REQUIRE_VALID (self, symtab);
147 result = PyUnicode_FromString (symtab_to_filename_for_display (symtab));
149 return result;
152 static PyObject *
153 stpy_get_filename (PyObject *self, void *closure)
155 PyObject *str_obj;
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 ();
163 return str_obj;
166 static PyObject *
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. */
178 static PyObject *
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 ();
193 Py_RETURN_NONE;
196 static PyObject *
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. */
212 static PyObject *
213 stpy_is_valid (PyObject *self, PyObject *args)
215 struct symtab *symtab = NULL;
217 symtab = symtab_object_to_symtab (self);
218 if (symtab == NULL)
219 Py_RETURN_FALSE;
221 Py_RETURN_TRUE;
224 /* Return the GLOBAL_BLOCK of the underlying symtab. */
226 static PyObject *
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. */
242 static PyObject *
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
258 table. */
260 static PyObject *
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);
270 static PyObject *
271 salpy_str (PyObject *self)
273 const char *filename;
274 sal_object *sal_obj;
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>";
282 else
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,
289 sal->line);
292 static void
293 stpy_dealloc (PyObject *obj)
295 symtab_object *symtab = (symtab_object *) obj;
297 if (symtab->prev)
298 symtab->prev->next = symtab->next;
299 else if (symtab->symtab)
300 stpy_objfile_data_key.set (symtab->symtab->compunit ()->objfile (),
301 symtab->next);
302 if (symtab->next)
303 symtab->next->prev = symtab->prev;
304 symtab->symtab = NULL;
305 Py_TYPE (obj)->tp_free (obj);
309 static PyObject *
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. */
322 static PyObject *
323 salpy_get_last (PyObject *self, void *closure)
325 struct symtab_and_line *sal = NULL;
327 SALPY_REQUIRE_VALID (self, sal);
329 if (sal->end > 0)
330 return gdb_py_object_from_ulongest (sal->end - 1).release ();
331 else
332 Py_RETURN_NONE;
335 static PyObject *
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 ();
345 static PyObject *
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. */
361 static PyObject *
362 salpy_is_valid (PyObject *self, PyObject *args)
364 struct symtab_and_line *sal;
366 sal = sal_object_to_symtab_and_line (self);
367 if (sal == NULL)
368 Py_RETURN_FALSE;
370 Py_RETURN_TRUE;
373 static void
374 salpy_dealloc (PyObject *self)
376 sal_object *self_sal = (sal_object *) self;
378 if (self_sal->prev)
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 (),
383 self_sal->next);
385 if (self_sal->next)
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;
403 if (sal.symtab)
405 symtab_obj = symtab_to_symtab_object (sal.symtab);
406 /* If a symtab existed in the sal, but it cannot be duplicated,
407 we exit. */
408 if (symtab_obj == NULL)
409 return -1;
411 else
413 symtab_obj = Py_None;
414 Py_INCREF (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);
429 sal_obj->next
430 = salpy_objfile_data_key.get (symtab->compunit ()->objfile ());
431 if (sal_obj->next)
432 sal_obj->next->prev = sal_obj;
434 salpy_objfile_data_key.set (symtab->compunit ()->objfile (), sal_obj);
436 else
437 sal_obj->next = NULL;
439 return 0;
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. */
447 static void
448 set_symtab (symtab_object *obj, struct symtab *symtab)
450 obj->symtab = symtab;
451 obj->prev = NULL;
452 if (symtab)
454 obj->next = stpy_objfile_data_key.get (symtab->compunit ()->objfile ());
455 if (obj->next)
456 obj->next->prev = obj;
457 stpy_objfile_data_key.set (symtab->compunit ()->objfile (), obj);
459 else
460 obj->next = NULL;
463 /* Create a new symbol table (gdb.Symtab) object that encapsulates the
464 symtab structure from GDB. */
465 PyObject *
466 symtab_to_symtab_object (struct symtab *symtab)
468 symtab_object *symtab_obj;
470 symtab_obj = PyObject_New (symtab_object, &symtab_object_type);
471 if (symtab_obj)
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. */
479 PyObject *
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));
483 if (sal_obj != NULL)
485 if (set_sal (sal_obj.get (), sal) < 0)
486 return NULL;
489 return (PyObject *) sal_obj.release ();
492 /* Return struct symtab_and_line reference that is wrapped by this
493 object. */
494 struct symtab_and_line *
495 sal_object_to_symtab_and_line (PyObject *obj)
497 if (! PyObject_TypeCheck (obj, &sal_object_type))
498 return NULL;
499 return ((sal_object *) obj)->sal;
502 /* Return struct symtab reference that is wrapped by this object. */
503 struct symtab *
504 symtab_object_to_symtab (PyObject *obj)
506 if (! PyObject_TypeCheck (obj, &symtab_object_type))
507 return NULL;
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)
516 return -1;
518 sal_object_type.tp_new = PyType_GenericNew;
519 if (gdbpy_type_ready (&sal_object_type) < 0)
520 return -1;
522 return 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.",
533 NULL },
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*/
562 0, /*tp_itemsize*/
563 stpy_dealloc, /*tp_dealloc*/
564 0, /*tp_print*/
565 0, /*tp_getattr*/
566 0, /*tp_setattr*/
567 0, /*tp_compare*/
568 0, /*tp_repr*/
569 0, /*tp_as_number*/
570 0, /*tp_as_sequence*/
571 0, /*tp_as_mapping*/
572 0, /*tp_hash */
573 0, /*tp_call*/
574 stpy_str, /*tp_str*/
575 0, /*tp_getattro*/
576 0, /*tp_setattro*/
577 0, /*tp_as_buffer*/
578 Py_TPFLAGS_DEFAULT, /*tp_flags*/
579 "GDB symtab object", /*tp_doc */
580 0, /*tp_traverse */
581 0, /*tp_clear */
582 0, /*tp_richcompare */
583 0, /*tp_weaklistoffset */
584 0, /*tp_iter */
585 0, /*tp_iternext */
586 symtab_object_methods, /*tp_methods */
587 0, /*tp_members */
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*/
612 0, /*tp_itemsize*/
613 salpy_dealloc, /*tp_dealloc*/
614 0, /*tp_print*/
615 0, /*tp_getattr*/
616 0, /*tp_setattr*/
617 0, /*tp_compare*/
618 0, /*tp_repr*/
619 0, /*tp_as_number*/
620 0, /*tp_as_sequence*/
621 0, /*tp_as_mapping*/
622 0, /*tp_hash */
623 0, /*tp_call*/
624 salpy_str, /*tp_str*/
625 0, /*tp_getattro*/
626 0, /*tp_setattro*/
627 0, /*tp_as_buffer*/
628 Py_TPFLAGS_DEFAULT, /*tp_flags*/
629 "GDB symtab_and_line object", /*tp_doc */
630 0, /*tp_traverse */
631 0, /*tp_clear */
632 0, /*tp_richcompare */
633 0, /*tp_weaklistoffset */
634 0, /*tp_iter */
635 0, /*tp_iternext */
636 sal_object_methods, /*tp_methods */
637 0, /*tp_members */
638 sal_object_getset /*tp_getset */