2b10e21d872a3ff36e29be68f3c05c637755aef9
[deliverable/binutils-gdb.git] / gdb / python / py-symbol.c
1 /* Python interface to symbols.
2
3 Copyright (C) 2008-2019 Free Software Foundation, Inc.
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
20 #include "defs.h"
21 #include "block.h"
22 #include "frame.h"
23 #include "symtab.h"
24 #include "python-internal.h"
25 #include "objfiles.h"
26
27 typedef struct sympy_symbol_object {
28 PyObject_HEAD
29 /* The GDB symbol structure this object is wrapping. */
30 struct symbol *symbol;
31 /* A symbol object is associated with an objfile, so keep track with
32 doubly-linked list, rooted in the objfile. This lets us
33 invalidate the underlying struct symbol when the objfile is
34 deleted. */
35 struct sympy_symbol_object *prev;
36 struct sympy_symbol_object *next;
37 } symbol_object;
38
39 /* Require a valid symbol. All access to symbol_object->symbol should be
40 gated by this call. */
41 #define SYMPY_REQUIRE_VALID(symbol_obj, symbol) \
42 do { \
43 symbol = symbol_object_to_symbol (symbol_obj); \
44 if (symbol == NULL) \
45 { \
46 PyErr_SetString (PyExc_RuntimeError, \
47 _("Symbol is invalid.")); \
48 return NULL; \
49 } \
50 } while (0)
51
52 static const struct objfile_data *sympy_objfile_data_key;
53
54 static PyObject *
55 sympy_str (PyObject *self)
56 {
57 PyObject *result;
58 struct symbol *symbol = NULL;
59
60 SYMPY_REQUIRE_VALID (self, symbol);
61
62 result = PyString_FromString (SYMBOL_PRINT_NAME (symbol));
63
64 return result;
65 }
66
67 static PyObject *
68 sympy_get_type (PyObject *self, void *closure)
69 {
70 struct symbol *symbol = NULL;
71
72 SYMPY_REQUIRE_VALID (self, symbol);
73
74 if (SYMBOL_TYPE (symbol) == NULL)
75 {
76 Py_INCREF (Py_None);
77 return Py_None;
78 }
79
80 return type_to_type_object (SYMBOL_TYPE (symbol));
81 }
82
83 static PyObject *
84 sympy_get_symtab (PyObject *self, void *closure)
85 {
86 struct symbol *symbol = NULL;
87
88 SYMPY_REQUIRE_VALID (self, symbol);
89
90 if (!SYMBOL_OBJFILE_OWNED (symbol))
91 Py_RETURN_NONE;
92
93 return symtab_to_symtab_object (symbol_symtab (symbol));
94 }
95
96 static PyObject *
97 sympy_get_name (PyObject *self, void *closure)
98 {
99 struct symbol *symbol = NULL;
100
101 SYMPY_REQUIRE_VALID (self, symbol);
102
103 return PyString_FromString (SYMBOL_NATURAL_NAME (symbol));
104 }
105
106 static PyObject *
107 sympy_get_linkage_name (PyObject *self, void *closure)
108 {
109 struct symbol *symbol = NULL;
110
111 SYMPY_REQUIRE_VALID (self, symbol);
112
113 return PyString_FromString (SYMBOL_LINKAGE_NAME (symbol));
114 }
115
116 static PyObject *
117 sympy_get_print_name (PyObject *self, void *closure)
118 {
119 struct symbol *symbol = NULL;
120
121 SYMPY_REQUIRE_VALID (self, symbol);
122
123 return sympy_str (self);
124 }
125
126 static PyObject *
127 sympy_get_addr_class (PyObject *self, void *closure)
128 {
129 struct symbol *symbol = NULL;
130
131 SYMPY_REQUIRE_VALID (self, symbol);
132
133 return PyInt_FromLong (SYMBOL_CLASS (symbol));
134 }
135
136 static PyObject *
137 sympy_is_argument (PyObject *self, void *closure)
138 {
139 struct symbol *symbol = NULL;
140
141 SYMPY_REQUIRE_VALID (self, symbol);
142
143 return PyBool_FromLong (SYMBOL_IS_ARGUMENT (symbol));
144 }
145
146 static PyObject *
147 sympy_is_constant (PyObject *self, void *closure)
148 {
149 struct symbol *symbol = NULL;
150 enum address_class theclass;
151
152 SYMPY_REQUIRE_VALID (self, symbol);
153
154 theclass = SYMBOL_CLASS (symbol);
155
156 return PyBool_FromLong (theclass == LOC_CONST || theclass == LOC_CONST_BYTES);
157 }
158
159 static PyObject *
160 sympy_is_function (PyObject *self, void *closure)
161 {
162 struct symbol *symbol = NULL;
163 enum address_class theclass;
164
165 SYMPY_REQUIRE_VALID (self, symbol);
166
167 theclass = SYMBOL_CLASS (symbol);
168
169 return PyBool_FromLong (theclass == LOC_BLOCK);
170 }
171
172 static PyObject *
173 sympy_is_variable (PyObject *self, void *closure)
174 {
175 struct symbol *symbol = NULL;
176 enum address_class theclass;
177
178 SYMPY_REQUIRE_VALID (self, symbol);
179
180 theclass = SYMBOL_CLASS (symbol);
181
182 return PyBool_FromLong (!SYMBOL_IS_ARGUMENT (symbol)
183 && (theclass == LOC_LOCAL || theclass == LOC_REGISTER
184 || theclass == LOC_STATIC || theclass == LOC_COMPUTED
185 || theclass == LOC_OPTIMIZED_OUT));
186 }
187
188 /* Implementation of gdb.Symbol.needs_frame -> Boolean.
189 Returns true iff the symbol needs a frame for evaluation. */
190
191 static PyObject *
192 sympy_needs_frame (PyObject *self, void *closure)
193 {
194 struct symbol *symbol = NULL;
195 int result = 0;
196
197 SYMPY_REQUIRE_VALID (self, symbol);
198
199 try
200 {
201 result = symbol_read_needs_frame (symbol);
202 }
203 catch (const gdb_exception &except)
204 {
205 GDB_PY_HANDLE_EXCEPTION (except);
206 }
207
208 if (result)
209 Py_RETURN_TRUE;
210 Py_RETURN_FALSE;
211 }
212
213 /* Implementation of gdb.Symbol.line -> int.
214 Returns the line number at which the symbol was defined. */
215
216 static PyObject *
217 sympy_line (PyObject *self, void *closure)
218 {
219 struct symbol *symbol = NULL;
220
221 SYMPY_REQUIRE_VALID (self, symbol);
222
223 return PyInt_FromLong (SYMBOL_LINE (symbol));
224 }
225
226 /* Implementation of gdb.Symbol.is_valid (self) -> Boolean.
227 Returns True if this Symbol still exists in GDB. */
228
229 static PyObject *
230 sympy_is_valid (PyObject *self, PyObject *args)
231 {
232 struct symbol *symbol = NULL;
233
234 symbol = symbol_object_to_symbol (self);
235 if (symbol == NULL)
236 Py_RETURN_FALSE;
237
238 Py_RETURN_TRUE;
239 }
240
241 /* Implementation of gdb.Symbol.value (self[, frame]) -> gdb.Value. Returns
242 the value of the symbol, or an error in various circumstances. */
243
244 static PyObject *
245 sympy_value (PyObject *self, PyObject *args)
246 {
247 struct symbol *symbol = NULL;
248 struct frame_info *frame_info = NULL;
249 PyObject *frame_obj = NULL;
250 struct value *value = NULL;
251
252 if (!PyArg_ParseTuple (args, "|O", &frame_obj))
253 return NULL;
254
255 if (frame_obj != NULL && !PyObject_TypeCheck (frame_obj, &frame_object_type))
256 {
257 PyErr_SetString (PyExc_TypeError, "argument is not a frame");
258 return NULL;
259 }
260
261 SYMPY_REQUIRE_VALID (self, symbol);
262 if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
263 {
264 PyErr_SetString (PyExc_TypeError, "cannot get the value of a typedef");
265 return NULL;
266 }
267
268 try
269 {
270 if (frame_obj != NULL)
271 {
272 frame_info = frame_object_to_frame_info (frame_obj);
273 if (frame_info == NULL)
274 error (_("invalid frame"));
275 }
276
277 if (symbol_read_needs_frame (symbol) && frame_info == NULL)
278 error (_("symbol requires a frame to compute its value"));
279
280 /* TODO: currently, we have no way to recover the block in which SYMBOL
281 was found, so we have no block to pass to read_var_value. This will
282 yield an incorrect value when symbol is not local to FRAME_INFO (this
283 can happen with nested functions). */
284 value = read_var_value (symbol, NULL, frame_info);
285 }
286 catch (const gdb_exception &except)
287 {
288 GDB_PY_HANDLE_EXCEPTION (except);
289 }
290
291 return value_to_value_object (value);
292 }
293
294 /* Given a symbol, and a symbol_object that has previously been
295 allocated and initialized, populate the symbol_object with the
296 struct symbol data. Also, register the symbol_object life-cycle
297 with the life-cycle of the object file associated with this
298 symbol, if needed. */
299 static void
300 set_symbol (symbol_object *obj, struct symbol *symbol)
301 {
302 obj->symbol = symbol;
303 obj->prev = NULL;
304 if (SYMBOL_OBJFILE_OWNED (symbol)
305 && symbol_symtab (symbol) != NULL)
306 {
307 struct objfile *objfile = symbol_objfile (symbol);
308
309 obj->next = ((struct sympy_symbol_object *)
310 objfile_data (objfile, sympy_objfile_data_key));
311 if (obj->next)
312 obj->next->prev = obj;
313 set_objfile_data (objfile, sympy_objfile_data_key, obj);
314 }
315 else
316 obj->next = NULL;
317 }
318
319 /* Create a new symbol object (gdb.Symbol) that encapsulates the struct
320 symbol object from GDB. */
321 PyObject *
322 symbol_to_symbol_object (struct symbol *sym)
323 {
324 symbol_object *sym_obj;
325
326 sym_obj = PyObject_New (symbol_object, &symbol_object_type);
327 if (sym_obj)
328 set_symbol (sym_obj, sym);
329
330 return (PyObject *) sym_obj;
331 }
332
333 /* Return the symbol that is wrapped by this symbol object. */
334 struct symbol *
335 symbol_object_to_symbol (PyObject *obj)
336 {
337 if (! PyObject_TypeCheck (obj, &symbol_object_type))
338 return NULL;
339 return ((symbol_object *) obj)->symbol;
340 }
341
342 static void
343 sympy_dealloc (PyObject *obj)
344 {
345 symbol_object *sym_obj = (symbol_object *) obj;
346
347 if (sym_obj->prev)
348 sym_obj->prev->next = sym_obj->next;
349 else if (sym_obj->symbol != NULL
350 && SYMBOL_OBJFILE_OWNED (sym_obj->symbol)
351 && symbol_symtab (sym_obj->symbol) != NULL)
352 {
353 set_objfile_data (symbol_objfile (sym_obj->symbol),
354 sympy_objfile_data_key, sym_obj->next);
355 }
356 if (sym_obj->next)
357 sym_obj->next->prev = sym_obj->prev;
358 sym_obj->symbol = NULL;
359 }
360
361 /* Implementation of
362 gdb.lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this)
363 A tuple with 2 elements is always returned. The first is the symbol
364 object or None, the second is a boolean with the value of
365 is_a_field_of_this (see comment in lookup_symbol_in_language). */
366
367 PyObject *
368 gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw)
369 {
370 int domain = VAR_DOMAIN;
371 struct field_of_this_result is_a_field_of_this;
372 const char *name;
373 static const char *keywords[] = { "name", "block", "domain", NULL };
374 struct symbol *symbol = NULL;
375 PyObject *block_obj = NULL, *sym_obj, *bool_obj;
376 const struct block *block = NULL;
377
378 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|O!i", keywords, &name,
379 &block_object_type, &block_obj,
380 &domain))
381 return NULL;
382
383 if (block_obj)
384 block = block_object_to_block (block_obj);
385 else
386 {
387 struct frame_info *selected_frame;
388
389 try
390 {
391 selected_frame = get_selected_frame (_("No frame selected."));
392 block = get_frame_block (selected_frame, NULL);
393 }
394 catch (const gdb_exception &except)
395 {
396 GDB_PY_HANDLE_EXCEPTION (except);
397 }
398 }
399
400 try
401 {
402 symbol = lookup_symbol (name, block, (domain_enum) domain,
403 &is_a_field_of_this).symbol;
404 }
405 catch (const gdb_exception &except)
406 {
407 GDB_PY_HANDLE_EXCEPTION (except);
408 }
409
410 gdbpy_ref<> ret_tuple (PyTuple_New (2));
411 if (ret_tuple == NULL)
412 return NULL;
413
414 if (symbol)
415 {
416 sym_obj = symbol_to_symbol_object (symbol);
417 if (!sym_obj)
418 return NULL;
419 }
420 else
421 {
422 sym_obj = Py_None;
423 Py_INCREF (Py_None);
424 }
425 PyTuple_SET_ITEM (ret_tuple.get (), 0, sym_obj);
426
427 bool_obj = (is_a_field_of_this.type != NULL) ? Py_True : Py_False;
428 Py_INCREF (bool_obj);
429 PyTuple_SET_ITEM (ret_tuple.get (), 1, bool_obj);
430
431 return ret_tuple.release ();
432 }
433
434 /* Implementation of
435 gdb.lookup_global_symbol (name [, domain]) -> symbol or None. */
436
437 PyObject *
438 gdbpy_lookup_global_symbol (PyObject *self, PyObject *args, PyObject *kw)
439 {
440 int domain = VAR_DOMAIN;
441 const char *name;
442 static const char *keywords[] = { "name", "domain", NULL };
443 struct symbol *symbol = NULL;
444 PyObject *sym_obj;
445
446 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name,
447 &domain))
448 return NULL;
449
450 try
451 {
452 symbol = lookup_global_symbol (name, NULL, (domain_enum) domain).symbol;
453 }
454 catch (const gdb_exception &except)
455 {
456 GDB_PY_HANDLE_EXCEPTION (except);
457 }
458
459 if (symbol)
460 {
461 sym_obj = symbol_to_symbol_object (symbol);
462 if (!sym_obj)
463 return NULL;
464 }
465 else
466 {
467 sym_obj = Py_None;
468 Py_INCREF (Py_None);
469 }
470
471 return sym_obj;
472 }
473
474 /* Implementation of
475 gdb.lookup_static_symbol (name [, domain) -> symbol or None. */
476
477 PyObject *
478 gdbpy_lookup_static_symbol (PyObject *self, PyObject *args, PyObject *kw)
479 {
480 const char *name;
481 int domain = VAR_DOMAIN;
482 static const char *keywords[] = { "name", "domain", NULL };
483 struct symbol *symbol = NULL;
484 PyObject *sym_obj;
485
486 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name,
487 &domain))
488 return NULL;
489
490 try
491 {
492 symbol = lookup_static_symbol (name, (domain_enum) domain).symbol;
493 }
494 catch (const gdb_exception &except)
495 {
496 GDB_PY_HANDLE_EXCEPTION (except);
497 }
498
499 if (symbol)
500 {
501 sym_obj = symbol_to_symbol_object (symbol);
502 if (!sym_obj)
503 return NULL;
504 }
505 else
506 {
507 sym_obj = Py_None;
508 Py_INCREF (Py_None);
509 }
510
511 return sym_obj;
512 }
513
514 /* This function is called when an objfile is about to be freed.
515 Invalidate the symbol as further actions on the symbol would result
516 in bad data. All access to obj->symbol should be gated by
517 SYMPY_REQUIRE_VALID which will raise an exception on invalid
518 symbols. */
519 static void
520 del_objfile_symbols (struct objfile *objfile, void *datum)
521 {
522 symbol_object *obj = (symbol_object *) datum;
523 while (obj)
524 {
525 symbol_object *next = obj->next;
526
527 obj->symbol = NULL;
528 obj->next = NULL;
529 obj->prev = NULL;
530
531 obj = next;
532 }
533 }
534
535 int
536 gdbpy_initialize_symbols (void)
537 {
538 if (PyType_Ready (&symbol_object_type) < 0)
539 return -1;
540
541 /* Register an objfile "free" callback so we can properly
542 invalidate symbol when an object file that is about to be
543 deleted. */
544 sympy_objfile_data_key
545 = register_objfile_data_with_cleanup (NULL, del_objfile_symbols);
546
547 if (PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNDEF", LOC_UNDEF) < 0
548 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST",
549 LOC_CONST) < 0
550 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_STATIC",
551 LOC_STATIC) < 0
552 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGISTER",
553 LOC_REGISTER) < 0
554 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_ARG",
555 LOC_ARG) < 0
556 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REF_ARG",
557 LOC_REF_ARG) < 0
558 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LOCAL",
559 LOC_LOCAL) < 0
560 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_TYPEDEF",
561 LOC_TYPEDEF) < 0
562 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LABEL",
563 LOC_LABEL) < 0
564 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_BLOCK",
565 LOC_BLOCK) < 0
566 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST_BYTES",
567 LOC_CONST_BYTES) < 0
568 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNRESOLVED",
569 LOC_UNRESOLVED) < 0
570 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_OPTIMIZED_OUT",
571 LOC_OPTIMIZED_OUT) < 0
572 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_COMPUTED",
573 LOC_COMPUTED) < 0
574 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_COMMON_BLOCK",
575 LOC_COMMON_BLOCK) < 0
576 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGPARM_ADDR",
577 LOC_REGPARM_ADDR) < 0
578 || PyModule_AddIntConstant (gdb_module, "SYMBOL_UNDEF_DOMAIN",
579 UNDEF_DOMAIN) < 0
580 || PyModule_AddIntConstant (gdb_module, "SYMBOL_VAR_DOMAIN",
581 VAR_DOMAIN) < 0
582 || PyModule_AddIntConstant (gdb_module, "SYMBOL_STRUCT_DOMAIN",
583 STRUCT_DOMAIN) < 0
584 || PyModule_AddIntConstant (gdb_module, "SYMBOL_MODULE_DOMAIN",
585 MODULE_DOMAIN) < 0
586 || PyModule_AddIntConstant (gdb_module, "SYMBOL_COMMON_BLOCK_DOMAIN",
587 COMMON_BLOCK_DOMAIN) < 0)
588 return -1;
589
590 /* These remain defined for compatibility, but as they were never
591 correct, they are no longer documented. Eventually we can remove
592 them. These exist because at one time, enum search_domain and
593 enum domain_enum_tag were combined -- but different values were
594 used differently. Here we try to give them values that will make
595 sense if they are passed to gdb.lookup_symbol. */
596 if (PyModule_AddIntConstant (gdb_module, "SYMBOL_VARIABLES_DOMAIN",
597 VAR_DOMAIN) < 0
598 || PyModule_AddIntConstant (gdb_module, "SYMBOL_FUNCTIONS_DOMAIN",
599 VAR_DOMAIN) < 0
600 || PyModule_AddIntConstant (gdb_module, "SYMBOL_TYPES_DOMAIN",
601 VAR_DOMAIN) < 0)
602 return -1;
603
604 return gdb_pymodule_addobject (gdb_module, "Symbol",
605 (PyObject *) &symbol_object_type);
606 }
607
608 \f
609
610 static gdb_PyGetSetDef symbol_object_getset[] = {
611 { "type", sympy_get_type, NULL,
612 "Type of the symbol.", NULL },
613 { "symtab", sympy_get_symtab, NULL,
614 "Symbol table in which the symbol appears.", NULL },
615 { "name", sympy_get_name, NULL,
616 "Name of the symbol, as it appears in the source code.", NULL },
617 { "linkage_name", sympy_get_linkage_name, NULL,
618 "Name of the symbol, as used by the linker (i.e., may be mangled).",
619 NULL },
620 { "print_name", sympy_get_print_name, NULL,
621 "Name of the symbol in a form suitable for output.\n\
622 This is either name or linkage_name, depending on whether the user asked GDB\n\
623 to display demangled or mangled names.", NULL },
624 { "addr_class", sympy_get_addr_class, NULL, "Address class of the symbol." },
625 { "is_argument", sympy_is_argument, NULL,
626 "True if the symbol is an argument of a function." },
627 { "is_constant", sympy_is_constant, NULL,
628 "True if the symbol is a constant." },
629 { "is_function", sympy_is_function, NULL,
630 "True if the symbol is a function or method." },
631 { "is_variable", sympy_is_variable, NULL,
632 "True if the symbol is a variable." },
633 { "needs_frame", sympy_needs_frame, NULL,
634 "True if the symbol requires a frame for evaluation." },
635 { "line", sympy_line, NULL,
636 "The source line number at which the symbol was defined." },
637 { NULL } /* Sentinel */
638 };
639
640 static PyMethodDef symbol_object_methods[] = {
641 { "is_valid", sympy_is_valid, METH_NOARGS,
642 "is_valid () -> Boolean.\n\
643 Return true if this symbol is valid, false if not." },
644 { "value", sympy_value, METH_VARARGS,
645 "value ([frame]) -> gdb.Value\n\
646 Return the value of the symbol." },
647 {NULL} /* Sentinel */
648 };
649
650 PyTypeObject symbol_object_type = {
651 PyVarObject_HEAD_INIT (NULL, 0)
652 "gdb.Symbol", /*tp_name*/
653 sizeof (symbol_object), /*tp_basicsize*/
654 0, /*tp_itemsize*/
655 sympy_dealloc, /*tp_dealloc*/
656 0, /*tp_print*/
657 0, /*tp_getattr*/
658 0, /*tp_setattr*/
659 0, /*tp_compare*/
660 0, /*tp_repr*/
661 0, /*tp_as_number*/
662 0, /*tp_as_sequence*/
663 0, /*tp_as_mapping*/
664 0, /*tp_hash */
665 0, /*tp_call*/
666 sympy_str, /*tp_str*/
667 0, /*tp_getattro*/
668 0, /*tp_setattro*/
669 0, /*tp_as_buffer*/
670 Py_TPFLAGS_DEFAULT, /*tp_flags*/
671 "GDB symbol object", /*tp_doc */
672 0, /*tp_traverse */
673 0, /*tp_clear */
674 0, /*tp_richcompare */
675 0, /*tp_weaklistoffset */
676 0, /*tp_iter */
677 0, /*tp_iternext */
678 symbol_object_methods, /*tp_methods */
679 0, /*tp_members */
680 symbol_object_getset /*tp_getset */
681 };
This page took 0.043583 seconds and 3 git commands to generate.