[sim] Run spellcheck.sh in sim (part 2)
[binutils-gdb.git] / gdb / python / py-symbol.c
blob24b53bbe38aa3dd80a5c6ce1e7f3cc6c20ea607b
1 /* Python interface to symbols.
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 "top.h"
21 #include "block.h"
22 #include "frame.h"
23 #include "symtab.h"
24 #include "python-internal.h"
25 #include "objfiles.h"
26 #include "symfile.h"
28 struct symbol_object {
29 PyObject_HEAD
30 /* The GDB symbol structure this object is wrapping. */
31 struct symbol *symbol;
32 /* A symbol object is associated with an objfile, so keep track with
33 doubly-linked list, rooted in the objfile. This lets us
34 invalidate the underlying struct symbol when the objfile is
35 deleted. */
36 symbol_object *prev;
37 symbol_object *next;
40 /* Require a valid symbol. All access to symbol_object->symbol should be
41 gated by this call. */
42 #define SYMPY_REQUIRE_VALID(symbol_obj, symbol) \
43 do { \
44 symbol = symbol_object_to_symbol (symbol_obj); \
45 if (symbol == NULL) \
46 { \
47 PyErr_SetString (PyExc_RuntimeError, \
48 _("Symbol is invalid.")); \
49 return NULL; \
50 } \
51 } while (0)
53 /* A deleter that is used when an objfile is about to be freed. */
54 struct symbol_object_deleter
56 void operator() (symbol_object *obj)
58 while (obj)
60 symbol_object *next = obj->next;
62 obj->symbol = NULL;
63 obj->next = NULL;
64 obj->prev = NULL;
66 obj = next;
71 static const registry<objfile>::key<symbol_object, symbol_object_deleter>
72 sympy_objfile_data_key;
74 static PyObject *
75 sympy_str (PyObject *self)
77 PyObject *result;
78 struct symbol *symbol = NULL;
80 SYMPY_REQUIRE_VALID (self, symbol);
82 result = PyUnicode_FromString (symbol->print_name ());
84 return result;
87 static PyObject *
88 sympy_get_type (PyObject *self, void *closure)
90 struct symbol *symbol = NULL;
92 SYMPY_REQUIRE_VALID (self, symbol);
94 if (symbol->type () == NULL)
96 Py_INCREF (Py_None);
97 return Py_None;
100 return type_to_type_object (symbol->type ());
103 static PyObject *
104 sympy_get_symtab (PyObject *self, void *closure)
106 struct symbol *symbol = NULL;
108 SYMPY_REQUIRE_VALID (self, symbol);
110 if (!symbol->is_objfile_owned ())
111 Py_RETURN_NONE;
113 return symtab_to_symtab_object (symbol->symtab ());
116 static PyObject *
117 sympy_get_name (PyObject *self, void *closure)
119 struct symbol *symbol = NULL;
121 SYMPY_REQUIRE_VALID (self, symbol);
123 return PyUnicode_FromString (symbol->natural_name ());
126 static PyObject *
127 sympy_get_linkage_name (PyObject *self, void *closure)
129 struct symbol *symbol = NULL;
131 SYMPY_REQUIRE_VALID (self, symbol);
133 return PyUnicode_FromString (symbol->linkage_name ());
136 static PyObject *
137 sympy_get_print_name (PyObject *self, void *closure)
139 struct symbol *symbol = NULL;
141 SYMPY_REQUIRE_VALID (self, symbol);
143 return sympy_str (self);
146 static PyObject *
147 sympy_get_addr_class (PyObject *self, void *closure)
149 struct symbol *symbol = NULL;
151 SYMPY_REQUIRE_VALID (self, symbol);
153 return gdb_py_object_from_longest (symbol->aclass ()).release ();
156 static PyObject *
157 sympy_is_argument (PyObject *self, void *closure)
159 struct symbol *symbol = NULL;
161 SYMPY_REQUIRE_VALID (self, symbol);
163 return PyBool_FromLong (symbol->is_argument ());
166 static PyObject *
167 sympy_is_constant (PyObject *self, void *closure)
169 struct symbol *symbol = NULL;
170 enum address_class theclass;
172 SYMPY_REQUIRE_VALID (self, symbol);
174 theclass = symbol->aclass ();
176 return PyBool_FromLong (theclass == LOC_CONST || theclass == LOC_CONST_BYTES);
179 static PyObject *
180 sympy_is_function (PyObject *self, void *closure)
182 struct symbol *symbol = NULL;
183 enum address_class theclass;
185 SYMPY_REQUIRE_VALID (self, symbol);
187 theclass = symbol->aclass ();
189 return PyBool_FromLong (theclass == LOC_BLOCK);
192 static PyObject *
193 sympy_is_variable (PyObject *self, void *closure)
195 struct symbol *symbol = NULL;
196 enum address_class theclass;
198 SYMPY_REQUIRE_VALID (self, symbol);
200 theclass = symbol->aclass ();
202 return PyBool_FromLong (!symbol->is_argument ()
203 && (theclass == LOC_LOCAL || theclass == LOC_REGISTER
204 || theclass == LOC_STATIC || theclass == LOC_COMPUTED
205 || theclass == LOC_OPTIMIZED_OUT));
208 /* Implementation of gdb.Symbol.needs_frame -> Boolean.
209 Returns true iff the symbol needs a frame for evaluation. */
211 static PyObject *
212 sympy_needs_frame (PyObject *self, void *closure)
214 struct symbol *symbol = NULL;
215 int result = 0;
217 SYMPY_REQUIRE_VALID (self, symbol);
221 result = symbol_read_needs_frame (symbol);
223 catch (const gdb_exception &except)
225 return gdbpy_handle_gdb_exception (nullptr, except);
228 if (result)
229 Py_RETURN_TRUE;
230 Py_RETURN_FALSE;
233 /* Implementation of gdb.Symbol.line -> int.
234 Returns the line number at which the symbol was defined. */
236 static PyObject *
237 sympy_line (PyObject *self, void *closure)
239 struct symbol *symbol = NULL;
241 SYMPY_REQUIRE_VALID (self, symbol);
243 return gdb_py_object_from_longest (symbol->line ()).release ();
246 /* Implementation of gdb.Symbol.is_valid (self) -> Boolean.
247 Returns True if this Symbol still exists in GDB. */
249 static PyObject *
250 sympy_is_valid (PyObject *self, PyObject *args)
252 struct symbol *symbol = NULL;
254 symbol = symbol_object_to_symbol (self);
255 if (symbol == NULL)
256 Py_RETURN_FALSE;
258 Py_RETURN_TRUE;
261 /* Implementation of gdb.Symbol.value (self[, frame]) -> gdb.Value. Returns
262 the value of the symbol, or an error in various circumstances. */
264 static PyObject *
265 sympy_value (PyObject *self, PyObject *args)
267 struct symbol *symbol = NULL;
268 frame_info_ptr frame_info = NULL;
269 PyObject *frame_obj = NULL;
271 if (!PyArg_ParseTuple (args, "|O", &frame_obj))
272 return NULL;
274 if (frame_obj != NULL && !PyObject_TypeCheck (frame_obj, &frame_object_type))
276 PyErr_SetString (PyExc_TypeError, "argument is not a frame");
277 return NULL;
280 SYMPY_REQUIRE_VALID (self, symbol);
281 if (symbol->aclass () == LOC_TYPEDEF)
283 PyErr_SetString (PyExc_TypeError, "cannot get the value of a typedef");
284 return NULL;
287 PyObject *result = nullptr;
290 if (frame_obj != NULL)
292 frame_info = frame_object_to_frame_info (frame_obj);
293 if (frame_info == NULL)
294 error (_("invalid frame"));
297 if (symbol_read_needs_frame (symbol) && frame_info == NULL)
298 error (_("symbol requires a frame to compute its value"));
300 /* TODO: currently, we have no way to recover the block in which SYMBOL
301 was found, so we have no block to pass to read_var_value. This will
302 yield an incorrect value when symbol is not local to FRAME_INFO (this
303 can happen with nested functions). */
304 scoped_value_mark free_values;
305 struct value *value = read_var_value (symbol, NULL, frame_info);
306 result = value_to_value_object (value);
308 catch (const gdb_exception &except)
310 return gdbpy_handle_gdb_exception (nullptr, except);
313 return result;
316 /* Given a symbol, and a symbol_object that has previously been
317 allocated and initialized, populate the symbol_object with the
318 struct symbol data. Also, register the symbol_object life-cycle
319 with the life-cycle of the object file associated with this
320 symbol, if needed. */
321 static void
322 set_symbol (symbol_object *obj, struct symbol *symbol)
324 obj->symbol = symbol;
325 obj->prev = NULL;
326 if (symbol->is_objfile_owned ()
327 && symbol->symtab () != NULL)
329 struct objfile *objfile = symbol->objfile ();
331 obj->next = sympy_objfile_data_key.get (objfile);
332 if (obj->next)
333 obj->next->prev = obj;
334 sympy_objfile_data_key.set (objfile, obj);
336 else
337 obj->next = NULL;
340 /* Create a new symbol object (gdb.Symbol) that encapsulates the struct
341 symbol object from GDB. */
342 PyObject *
343 symbol_to_symbol_object (struct symbol *sym)
345 symbol_object *sym_obj;
347 sym_obj = PyObject_New (symbol_object, &symbol_object_type);
348 if (sym_obj)
349 set_symbol (sym_obj, sym);
351 return (PyObject *) sym_obj;
354 /* Return the symbol that is wrapped by this symbol object. */
355 struct symbol *
356 symbol_object_to_symbol (PyObject *obj)
358 if (! PyObject_TypeCheck (obj, &symbol_object_type))
359 return NULL;
360 return ((symbol_object *) obj)->symbol;
363 static void
364 sympy_dealloc (PyObject *obj)
366 symbol_object *sym_obj = (symbol_object *) obj;
368 if (sym_obj->prev)
369 sym_obj->prev->next = sym_obj->next;
370 else if (sym_obj->symbol != NULL
371 && sym_obj->symbol->is_objfile_owned ()
372 && sym_obj->symbol->symtab () != NULL)
373 sympy_objfile_data_key.set (sym_obj->symbol->objfile (), sym_obj->next);
374 if (sym_obj->next)
375 sym_obj->next->prev = sym_obj->prev;
376 sym_obj->symbol = NULL;
377 Py_TYPE (obj)->tp_free (obj);
380 /* __repr__ implementation for gdb.Symbol. */
382 static PyObject *
383 sympy_repr (PyObject *self)
385 const auto symbol = symbol_object_to_symbol (self);
386 if (symbol == nullptr)
387 return gdb_py_invalid_object_repr (self);
389 return PyUnicode_FromFormat ("<%s print_name=%s>", Py_TYPE (self)->tp_name,
390 symbol->print_name ());
393 /* Implementation of
394 gdb.lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this)
395 A tuple with 2 elements is always returned. The first is the symbol
396 object or None, the second is a boolean with the value of
397 is_a_field_of_this (see comment in lookup_symbol_in_language). */
399 PyObject *
400 gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw)
402 int domain = VAR_DOMAIN;
403 struct field_of_this_result is_a_field_of_this;
404 const char *name;
405 static const char *keywords[] = { "name", "block", "domain", NULL };
406 struct symbol *symbol = NULL;
407 PyObject *block_obj = NULL, *sym_obj, *bool_obj;
408 const struct block *block = NULL;
410 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|O!i", keywords, &name,
411 &block_object_type, &block_obj,
412 &domain))
413 return NULL;
415 if (block_obj)
416 block = block_object_to_block (block_obj);
417 else
419 frame_info_ptr selected_frame;
423 selected_frame = get_selected_frame (_("No frame selected."));
424 block = get_frame_block (selected_frame, NULL);
426 catch (const gdb_exception &except)
428 return gdbpy_handle_gdb_exception (nullptr, except);
434 domain_search_flags flags = from_scripting_domain (domain);
435 symbol = lookup_symbol (name, block, flags, &is_a_field_of_this).symbol;
437 catch (const gdb_exception &except)
439 return gdbpy_handle_gdb_exception (nullptr, except);
442 gdbpy_ref<> ret_tuple (PyTuple_New (2));
443 if (ret_tuple == NULL)
444 return NULL;
446 if (symbol)
448 sym_obj = symbol_to_symbol_object (symbol);
449 if (!sym_obj)
450 return NULL;
452 else
454 sym_obj = Py_None;
455 Py_INCREF (Py_None);
457 PyTuple_SET_ITEM (ret_tuple.get (), 0, sym_obj);
459 bool_obj = PyBool_FromLong (is_a_field_of_this.type != NULL);
460 PyTuple_SET_ITEM (ret_tuple.get (), 1, bool_obj);
462 return ret_tuple.release ();
465 /* Implementation of
466 gdb.lookup_global_symbol (name [, domain]) -> symbol or None. */
468 PyObject *
469 gdbpy_lookup_global_symbol (PyObject *self, PyObject *args, PyObject *kw)
471 int domain = VAR_DOMAIN;
472 const char *name;
473 static const char *keywords[] = { "name", "domain", NULL };
474 struct symbol *symbol = NULL;
475 PyObject *sym_obj;
477 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name,
478 &domain))
479 return NULL;
483 domain_search_flags flags = from_scripting_domain (domain);
484 symbol = lookup_global_symbol (name, NULL, flags).symbol;
486 catch (const gdb_exception &except)
488 return gdbpy_handle_gdb_exception (nullptr, except);
491 if (symbol)
493 sym_obj = symbol_to_symbol_object (symbol);
494 if (!sym_obj)
495 return NULL;
497 else
499 sym_obj = Py_None;
500 Py_INCREF (Py_None);
503 return sym_obj;
506 /* Implementation of
507 gdb.lookup_static_symbol (name [, domain]) -> symbol or None. */
509 PyObject *
510 gdbpy_lookup_static_symbol (PyObject *self, PyObject *args, PyObject *kw)
512 const char *name;
513 int domain = VAR_DOMAIN;
514 static const char *keywords[] = { "name", "domain", NULL };
515 struct symbol *symbol = NULL;
516 PyObject *sym_obj;
518 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name,
519 &domain))
520 return NULL;
522 /* In order to find static symbols associated with the "current" object
523 file ahead of those from other object files, we first need to see if
524 we can acquire a current block. If this fails however, then we still
525 want to search all static symbols, so don't throw an exception just
526 yet. */
527 const struct block *block = NULL;
530 frame_info_ptr selected_frame
531 = get_selected_frame (_("No frame selected."));
532 block = get_frame_block (selected_frame, NULL);
534 catch (const gdb_exception_forced_quit &e)
536 quit_force (NULL, 0);
538 catch (const gdb_exception &except)
540 /* Nothing. */
545 domain_search_flags flags = from_scripting_domain (domain);
547 if (block != nullptr)
548 symbol
549 = lookup_symbol_in_static_block (name, block, flags).symbol;
551 if (symbol == nullptr)
552 symbol = lookup_static_symbol (name, flags).symbol;
554 catch (const gdb_exception &except)
556 return gdbpy_handle_gdb_exception (nullptr, except);
559 if (symbol)
561 sym_obj = symbol_to_symbol_object (symbol);
562 if (!sym_obj)
563 return NULL;
565 else
567 sym_obj = Py_None;
568 Py_INCREF (Py_None);
571 return sym_obj;
574 /* Implementation of
575 gdb.lookup_static_symbols (name [, domain]) -> symbol list.
577 Returns a list of all static symbols matching NAME in DOMAIN. */
579 PyObject *
580 gdbpy_lookup_static_symbols (PyObject *self, PyObject *args, PyObject *kw)
582 const char *name;
583 int domain = VAR_DOMAIN;
584 static const char *keywords[] = { "name", "domain", NULL };
586 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name,
587 &domain))
588 return NULL;
590 gdbpy_ref<> return_list (PyList_New (0));
591 if (return_list == NULL)
592 return NULL;
596 domain_search_flags flags = from_scripting_domain (domain);
598 /* Expand any symtabs that contain potentially matching symbols. */
599 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
600 expand_symtabs_matching (NULL, lookup_name, NULL, NULL,
601 SEARCH_GLOBAL_BLOCK | SEARCH_STATIC_BLOCK,
602 SEARCH_ALL_DOMAINS);
604 for (objfile *objfile : current_program_space->objfiles ())
606 for (compunit_symtab *cust : objfile->compunits ())
608 /* Skip included compunits to prevent including compunits from
609 being searched twice. */
610 if (cust->user != nullptr)
611 continue;
613 const struct blockvector *bv = cust->blockvector ();
614 const struct block *block = bv->static_block ();
616 if (block != nullptr)
618 symbol *symbol = lookup_symbol_in_static_block
619 (name, block, flags).symbol;
621 if (symbol != nullptr)
623 PyObject *sym_obj
624 = symbol_to_symbol_object (symbol);
625 if (PyList_Append (return_list.get (), sym_obj) == -1)
626 return NULL;
632 catch (const gdb_exception &except)
634 return gdbpy_handle_gdb_exception (nullptr, except);
637 return return_list.release ();
640 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
641 gdbpy_initialize_symbols (void)
643 if (gdbpy_type_ready (&symbol_object_type) < 0)
644 return -1;
646 if (PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNDEF", LOC_UNDEF) < 0
647 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST",
648 LOC_CONST) < 0
649 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_STATIC",
650 LOC_STATIC) < 0
651 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGISTER",
652 LOC_REGISTER) < 0
653 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_ARG",
654 LOC_ARG) < 0
655 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REF_ARG",
656 LOC_REF_ARG) < 0
657 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LOCAL",
658 LOC_LOCAL) < 0
659 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_TYPEDEF",
660 LOC_TYPEDEF) < 0
661 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LABEL",
662 LOC_LABEL) < 0
663 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_BLOCK",
664 LOC_BLOCK) < 0
665 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST_BYTES",
666 LOC_CONST_BYTES) < 0
667 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNRESOLVED",
668 LOC_UNRESOLVED) < 0
669 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_OPTIMIZED_OUT",
670 LOC_OPTIMIZED_OUT) < 0
671 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_COMPUTED",
672 LOC_COMPUTED) < 0
673 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_COMMON_BLOCK",
674 LOC_COMMON_BLOCK) < 0
675 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGPARM_ADDR",
676 LOC_REGPARM_ADDR) < 0)
677 return -1;
679 #define SYM_DOMAIN(X) \
680 if (PyModule_AddIntConstant (gdb_module, "SYMBOL_" #X "_DOMAIN", \
681 to_scripting_domain (X ## _DOMAIN)) < 0 \
682 || PyModule_AddIntConstant (gdb_module, "SEARCH_" #X "_DOMAIN", \
683 to_scripting_domain (SEARCH_ ## X ## _DOMAIN)) < 0) \
684 return -1;
685 #include "sym-domains.def"
686 #undef SYM_DOMAIN
688 return 0;
691 GDBPY_INITIALIZE_FILE (gdbpy_initialize_symbols);
695 static gdb_PyGetSetDef symbol_object_getset[] = {
696 { "type", sympy_get_type, NULL,
697 "Type of the symbol.", NULL },
698 { "symtab", sympy_get_symtab, NULL,
699 "Symbol table in which the symbol appears.", NULL },
700 { "name", sympy_get_name, NULL,
701 "Name of the symbol, as it appears in the source code.", NULL },
702 { "linkage_name", sympy_get_linkage_name, NULL,
703 "Name of the symbol, as used by the linker (i.e., may be mangled).",
704 NULL },
705 { "print_name", sympy_get_print_name, NULL,
706 "Name of the symbol in a form suitable for output.\n\
707 This is either name or linkage_name, depending on whether the user asked GDB\n\
708 to display demangled or mangled names.", NULL },
709 { "addr_class", sympy_get_addr_class, NULL, "Address class of the symbol." },
710 { "is_argument", sympy_is_argument, NULL,
711 "True if the symbol is an argument of a function." },
712 { "is_constant", sympy_is_constant, NULL,
713 "True if the symbol is a constant." },
714 { "is_function", sympy_is_function, NULL,
715 "True if the symbol is a function or method." },
716 { "is_variable", sympy_is_variable, NULL,
717 "True if the symbol is a variable." },
718 { "needs_frame", sympy_needs_frame, NULL,
719 "True if the symbol requires a frame for evaluation." },
720 { "line", sympy_line, NULL,
721 "The source line number at which the symbol was defined." },
722 { NULL } /* Sentinel */
725 static PyMethodDef symbol_object_methods[] = {
726 { "is_valid", sympy_is_valid, METH_NOARGS,
727 "is_valid () -> Boolean.\n\
728 Return true if this symbol is valid, false if not." },
729 { "value", sympy_value, METH_VARARGS,
730 "value ([frame]) -> gdb.Value\n\
731 Return the value of the symbol." },
732 {NULL} /* Sentinel */
735 PyTypeObject symbol_object_type = {
736 PyVarObject_HEAD_INIT (NULL, 0)
737 "gdb.Symbol", /*tp_name*/
738 sizeof (symbol_object), /*tp_basicsize*/
739 0, /*tp_itemsize*/
740 sympy_dealloc, /*tp_dealloc*/
741 0, /*tp_print*/
742 0, /*tp_getattr*/
743 0, /*tp_setattr*/
744 0, /*tp_compare*/
745 sympy_repr, /*tp_repr*/
746 0, /*tp_as_number*/
747 0, /*tp_as_sequence*/
748 0, /*tp_as_mapping*/
749 0, /*tp_hash */
750 0, /*tp_call*/
751 sympy_str, /*tp_str*/
752 0, /*tp_getattro*/
753 0, /*tp_setattro*/
754 0, /*tp_as_buffer*/
755 Py_TPFLAGS_DEFAULT, /*tp_flags*/
756 "GDB symbol object", /*tp_doc */
757 0, /*tp_traverse */
758 0, /*tp_clear */
759 0, /*tp_richcompare */
760 0, /*tp_weaklistoffset */
761 0, /*tp_iter */
762 0, /*tp_iternext */
763 symbol_object_methods, /*tp_methods */
764 0, /*tp_members */
765 symbol_object_getset /*tp_getset */