*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / python / py-symbol.c
CommitLineData
f3e9a817
PM
1/* Python interface to symbols.
2
0b302171 3 Copyright (C) 2008-2012 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
20#include "defs.h"
21#include "block.h"
22#include "exceptions.h"
23#include "frame.h"
24#include "symtab.h"
25#include "python-internal.h"
26#include "objfiles.h"
27
28typedef struct sympy_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 struct sympy_symbol_object *prev;
37 struct sympy_symbol_object *next;
38} symbol_object;
39
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)
52
53static const struct objfile_data *sympy_objfile_data_key;
54
55static PyObject *
56sympy_str (PyObject *self)
57{
58 PyObject *result;
59 struct symbol *symbol = NULL;
60
61 SYMPY_REQUIRE_VALID (self, symbol);
62
63 result = PyString_FromString (SYMBOL_PRINT_NAME (symbol));
64
65 return result;
66}
67
457e09f0
DE
68static PyObject *
69sympy_get_type (PyObject *self, void *closure)
70{
71 struct symbol *symbol = NULL;
72
73 SYMPY_REQUIRE_VALID (self, symbol);
74
75 if (SYMBOL_TYPE (symbol) == NULL)
76 {
77 Py_INCREF (Py_None);
78 return Py_None;
79 }
80
81 return type_to_type_object (SYMBOL_TYPE (symbol));
82}
83
f3e9a817
PM
84static PyObject *
85sympy_get_symtab (PyObject *self, void *closure)
86{
87 struct symbol *symbol = NULL;
88
89 SYMPY_REQUIRE_VALID (self, symbol);
90
91 return symtab_to_symtab_object (SYMBOL_SYMTAB (symbol));
92}
93
94static PyObject *
95sympy_get_name (PyObject *self, void *closure)
96{
97 struct symbol *symbol = NULL;
98
99 SYMPY_REQUIRE_VALID (self, symbol);
100
101 return PyString_FromString (SYMBOL_NATURAL_NAME (symbol));
102}
103
104static PyObject *
105sympy_get_linkage_name (PyObject *self, void *closure)
106{
107 struct symbol *symbol = NULL;
108
109 SYMPY_REQUIRE_VALID (self, symbol);
110
111 return PyString_FromString (SYMBOL_LINKAGE_NAME (symbol));
112}
113
114static PyObject *
115sympy_get_print_name (PyObject *self, void *closure)
116{
117 struct symbol *symbol = NULL;
118
119 SYMPY_REQUIRE_VALID (self, symbol);
120
121 return sympy_str (self);
122}
123
124static PyObject *
125sympy_get_addr_class (PyObject *self, void *closure)
126{
127 struct symbol *symbol = NULL;
128
129 SYMPY_REQUIRE_VALID (self, symbol);
130
131 return PyInt_FromLong (SYMBOL_CLASS (symbol));
132}
133
134static PyObject *
135sympy_is_argument (PyObject *self, void *closure)
136{
137 struct symbol *symbol = NULL;
138
139 SYMPY_REQUIRE_VALID (self, symbol);
140
141 return PyBool_FromLong (SYMBOL_IS_ARGUMENT (symbol));
142}
143
144static PyObject *
145sympy_is_constant (PyObject *self, void *closure)
146{
147 struct symbol *symbol = NULL;
148 enum address_class class;
149
150 SYMPY_REQUIRE_VALID (self, symbol);
151
152 class = SYMBOL_CLASS (symbol);
153
154 return PyBool_FromLong (class == LOC_CONST || class == LOC_CONST_BYTES);
155}
156
157static PyObject *
158sympy_is_function (PyObject *self, void *closure)
159{
160 struct symbol *symbol = NULL;
161 enum address_class class;
162
163 SYMPY_REQUIRE_VALID (self, symbol);
164
165 class = SYMBOL_CLASS (symbol);
166
167 return PyBool_FromLong (class == LOC_BLOCK);
168}
169
170static PyObject *
171sympy_is_variable (PyObject *self, void *closure)
172{
173 struct symbol *symbol = NULL;
174 enum address_class class;
175
176 SYMPY_REQUIRE_VALID (self, symbol);
177
178 class = SYMBOL_CLASS (symbol);
179
180 return PyBool_FromLong (!SYMBOL_IS_ARGUMENT (symbol)
181 && (class == LOC_LOCAL || class == LOC_REGISTER
182 || class == LOC_STATIC || class == LOC_COMPUTED
183 || class == LOC_OPTIMIZED_OUT));
184}
185
f0823d2c
TT
186/* Implementation of gdb.Symbol.needs_frame -> Boolean.
187 Returns true iff the symbol needs a frame for evaluation. */
188
189static PyObject *
190sympy_needs_frame (PyObject *self, void *closure)
191{
192 struct symbol *symbol = NULL;
193 volatile struct gdb_exception except;
194 int result = 0;
195
196 SYMPY_REQUIRE_VALID (self, symbol);
197
198 TRY_CATCH (except, RETURN_MASK_ALL)
199 {
200 result = symbol_read_needs_frame (symbol);
201 }
202 GDB_PY_HANDLE_EXCEPTION (except);
203
204 if (result)
205 Py_RETURN_TRUE;
206 Py_RETURN_FALSE;
207}
208
64e7d9dd
TT
209/* Implementation of gdb.Symbol.line -> int.
210 Returns the line number at which the symbol was defined. */
211
212static PyObject *
213sympy_line (PyObject *self, void *closure)
214{
215 struct symbol *symbol = NULL;
216
217 SYMPY_REQUIRE_VALID (self, symbol);
218
219 return PyInt_FromLong (SYMBOL_LINE (symbol));
220}
221
29703da4
PM
222/* Implementation of gdb.Symbol.is_valid (self) -> Boolean.
223 Returns True if this Symbol still exists in GDB. */
224
225static PyObject *
226sympy_is_valid (PyObject *self, PyObject *args)
227{
228 struct symbol *symbol = NULL;
229
230 symbol = symbol_object_to_symbol (self);
231 if (symbol == NULL)
232 Py_RETURN_FALSE;
233
234 Py_RETURN_TRUE;
235}
236
f0823d2c
TT
237/* Implementation of gdb.Symbol.value (self[, frame]) -> gdb.Value. Returns
238 the value of the symbol, or an error in various circumstances. */
239
240static PyObject *
241sympy_value (PyObject *self, PyObject *args)
242{
243 struct symbol *symbol = NULL;
244 struct frame_info *frame_info = NULL;
245 PyObject *frame_obj = NULL;
246 struct value *value = NULL;
247 volatile struct gdb_exception except;
248
249 if (!PyArg_ParseTuple (args, "|O", &frame_obj))
250 return NULL;
251
252 if (frame_obj != NULL && !PyObject_TypeCheck (frame_obj, &frame_object_type))
253 {
254 PyErr_SetString (PyExc_TypeError, "argument is not a frame");
255 return NULL;
256 }
257
258 SYMPY_REQUIRE_VALID (self, symbol);
259 if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
260 {
261 PyErr_SetString (PyExc_TypeError, "cannot get the value of a typedef");
262 return NULL;
263 }
264
265 TRY_CATCH (except, RETURN_MASK_ALL)
266 {
267 if (frame_obj != NULL)
268 {
269 frame_info = frame_object_to_frame_info (frame_obj);
270 if (frame_info == NULL)
c6910659 271 error (_("invalid frame"));
f0823d2c
TT
272 }
273
274 if (symbol_read_needs_frame (symbol) && frame_info == NULL)
c6910659 275 error (_("symbol requires a frame to compute its value"));
f0823d2c
TT
276
277 value = read_var_value (symbol, frame_info);
278 }
279 GDB_PY_HANDLE_EXCEPTION (except);
280
281 return value_to_value_object (value);
282}
283
f3e9a817
PM
284/* Given a symbol, and a symbol_object that has previously been
285 allocated and initialized, populate the symbol_object with the
286 struct symbol data. Also, register the symbol_object life-cycle
b021a221 287 with the life-cycle of the object file associated with this
f3e9a817
PM
288 symbol, if needed. */
289static void
290set_symbol (symbol_object *obj, struct symbol *symbol)
291{
292 obj->symbol = symbol;
293 obj->prev = NULL;
294 if (SYMBOL_SYMTAB (symbol))
295 {
296 obj->next = objfile_data (SYMBOL_SYMTAB (symbol)->objfile,
297 sympy_objfile_data_key);
298
299 if (obj->next)
300 obj->next->prev = obj;
301 set_objfile_data (SYMBOL_SYMTAB (symbol)->objfile,
302 sympy_objfile_data_key, obj);
303 }
304 else
305 obj->next = NULL;
306}
307
308/* Create a new symbol object (gdb.Symbol) that encapsulates the struct
309 symbol object from GDB. */
310PyObject *
311symbol_to_symbol_object (struct symbol *sym)
312{
313 symbol_object *sym_obj;
314
315 sym_obj = PyObject_New (symbol_object, &symbol_object_type);
316 if (sym_obj)
317 set_symbol (sym_obj, sym);
318
319 return (PyObject *) sym_obj;
320}
321
322/* Return the symbol that is wrapped by this symbol object. */
323struct symbol *
324symbol_object_to_symbol (PyObject *obj)
325{
326 if (! PyObject_TypeCheck (obj, &symbol_object_type))
327 return NULL;
328 return ((symbol_object *) obj)->symbol;
329}
330
331static void
332sympy_dealloc (PyObject *obj)
333{
334 symbol_object *sym_obj = (symbol_object *) obj;
335
336 if (sym_obj->prev)
337 sym_obj->prev->next = sym_obj->next;
338 else if (SYMBOL_SYMTAB (sym_obj->symbol))
339 {
340 set_objfile_data (SYMBOL_SYMTAB (sym_obj->symbol)->objfile,
341 sympy_objfile_data_key, sym_obj->next);
342 }
343 if (sym_obj->next)
344 sym_obj->next->prev = sym_obj->prev;
345 sym_obj->symbol = NULL;
346}
347
348/* Implementation of
349 gdb.lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this)
350 A tuple with 2 elements is always returned. The first is the symbol
351 object or None, the second is a boolean with the value of
352 is_a_field_of_this (see comment in lookup_symbol_in_language). */
6e6fbe60 353
f3e9a817
PM
354PyObject *
355gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw)
356{
357 int domain = VAR_DOMAIN, is_a_field_of_this = 0;
358 const char *name;
359 static char *keywords[] = { "name", "block", "domain", NULL };
76dce0be 360 struct symbol *symbol = NULL;
f3e9a817 361 PyObject *block_obj = NULL, *ret_tuple, *sym_obj, *bool_obj;
9df2fbc4 362 const struct block *block = NULL;
76dce0be 363 volatile struct gdb_exception except;
f3e9a817
PM
364
365 if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O!i", keywords, &name,
366 &block_object_type, &block_obj, &domain))
367 return NULL;
368
369 if (block_obj)
370 block = block_object_to_block (block_obj);
371 else
372 {
373 struct frame_info *selected_frame;
374 volatile struct gdb_exception except;
375
376 TRY_CATCH (except, RETURN_MASK_ALL)
377 {
626e7282
JK
378 selected_frame = get_selected_frame (_("No frame selected."));
379 block = get_frame_block (selected_frame, NULL);
f3e9a817
PM
380 }
381 GDB_PY_HANDLE_EXCEPTION (except);
382 }
383
76dce0be
PM
384 TRY_CATCH (except, RETURN_MASK_ALL)
385 {
386 symbol = lookup_symbol (name, block, domain, &is_a_field_of_this);
387 }
388 GDB_PY_HANDLE_EXCEPTION (except);
f3e9a817
PM
389
390 ret_tuple = PyTuple_New (2);
391 if (!ret_tuple)
392 return NULL;
393
394 if (symbol)
395 {
396 sym_obj = symbol_to_symbol_object (symbol);
397 if (!sym_obj)
398 {
399 Py_DECREF (ret_tuple);
400 return NULL;
401 }
402 }
403 else
404 {
405 sym_obj = Py_None;
406 Py_INCREF (Py_None);
407 }
408 PyTuple_SET_ITEM (ret_tuple, 0, sym_obj);
409
410 bool_obj = is_a_field_of_this? Py_True : Py_False;
411 Py_INCREF (bool_obj);
412 PyTuple_SET_ITEM (ret_tuple, 1, bool_obj);
413
414 return ret_tuple;
415}
416
6e6fbe60
DE
417/* Implementation of
418 gdb.lookup_global_symbol (name [, domain]) -> symbol or None. */
419
420PyObject *
421gdbpy_lookup_global_symbol (PyObject *self, PyObject *args, PyObject *kw)
422{
423 int domain = VAR_DOMAIN;
424 const char *name;
425 static char *keywords[] = { "name", "domain", NULL };
76dce0be 426 struct symbol *symbol = NULL;
6e6fbe60 427 PyObject *sym_obj;
76dce0be 428 volatile struct gdb_exception except;
6e6fbe60
DE
429
430 if (! PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name,
431 &domain))
432 return NULL;
433
76dce0be
PM
434 TRY_CATCH (except, RETURN_MASK_ALL)
435 {
436 symbol = lookup_symbol_global (name, NULL, domain);
437 }
438 GDB_PY_HANDLE_EXCEPTION (except);
6e6fbe60
DE
439
440 if (symbol)
441 {
442 sym_obj = symbol_to_symbol_object (symbol);
443 if (!sym_obj)
444 return NULL;
445 }
446 else
447 {
448 sym_obj = Py_None;
449 Py_INCREF (Py_None);
450 }
451
452 return sym_obj;
453}
454
f3e9a817
PM
455/* This function is called when an objfile is about to be freed.
456 Invalidate the symbol as further actions on the symbol would result
457 in bad data. All access to obj->symbol should be gated by
458 SYMPY_REQUIRE_VALID which will raise an exception on invalid
459 symbols. */
460static void
461del_objfile_symbols (struct objfile *objfile, void *datum)
462{
463 symbol_object *obj = datum;
464 while (obj)
465 {
466 symbol_object *next = obj->next;
467
468 obj->symbol = NULL;
469 obj->next = NULL;
470 obj->prev = NULL;
471
472 obj = next;
473 }
474}
475
476void
477gdbpy_initialize_symbols (void)
478{
479 if (PyType_Ready (&symbol_object_type) < 0)
480 return;
481
482 /* Register an objfile "free" callback so we can properly
483 invalidate symbol when an object file that is about to be
484 deleted. */
485 sympy_objfile_data_key
486 = register_objfile_data_with_cleanup (NULL, del_objfile_symbols);
487
488 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNDEF", LOC_UNDEF);
489 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST", LOC_CONST);
490 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_STATIC", LOC_STATIC);
491 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGISTER", LOC_REGISTER);
492 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_ARG", LOC_ARG);
493 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REF_ARG", LOC_REF_ARG);
494 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LOCAL", LOC_LOCAL);
495 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_TYPEDEF", LOC_TYPEDEF);
496 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LABEL", LOC_LABEL);
497 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_BLOCK", LOC_BLOCK);
498 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST_BYTES",
499 LOC_CONST_BYTES);
9a2b4c1b
MS
500 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNRESOLVED",
501 LOC_UNRESOLVED);
f3e9a817
PM
502 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_OPTIMIZED_OUT",
503 LOC_OPTIMIZED_OUT);
504 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_COMPUTED", LOC_COMPUTED);
505 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGPARM_ADDR",
506 LOC_REGPARM_ADDR);
507 PyModule_AddIntConstant (gdb_module, "SYMBOL_UNDEF_DOMAIN", UNDEF_DOMAIN);
508 PyModule_AddIntConstant (gdb_module, "SYMBOL_VAR_DOMAIN", VAR_DOMAIN);
509 PyModule_AddIntConstant (gdb_module, "SYMBOL_STRUCT_DOMAIN", STRUCT_DOMAIN);
510 PyModule_AddIntConstant (gdb_module, "SYMBOL_LABEL_DOMAIN", LABEL_DOMAIN);
511 PyModule_AddIntConstant (gdb_module, "SYMBOL_VARIABLES_DOMAIN",
512 VARIABLES_DOMAIN);
513 PyModule_AddIntConstant (gdb_module, "SYMBOL_FUNCTIONS_DOMAIN",
514 FUNCTIONS_DOMAIN);
515 PyModule_AddIntConstant (gdb_module, "SYMBOL_TYPES_DOMAIN", TYPES_DOMAIN);
516
517 Py_INCREF (&symbol_object_type);
518 PyModule_AddObject (gdb_module, "Symbol", (PyObject *) &symbol_object_type);
519}
520
521\f
522
523static PyGetSetDef symbol_object_getset[] = {
457e09f0
DE
524 { "type", sympy_get_type, NULL,
525 "Type of the symbol.", NULL },
f3e9a817
PM
526 { "symtab", sympy_get_symtab, NULL,
527 "Symbol table in which the symbol appears.", NULL },
528 { "name", sympy_get_name, NULL,
529 "Name of the symbol, as it appears in the source code.", NULL },
530 { "linkage_name", sympy_get_linkage_name, NULL,
9a2b4c1b
MS
531 "Name of the symbol, as used by the linker (i.e., may be mangled).",
532 NULL },
f3e9a817
PM
533 { "print_name", sympy_get_print_name, NULL,
534 "Name of the symbol in a form suitable for output.\n\
535This is either name or linkage_name, depending on whether the user asked GDB\n\
536to display demangled or mangled names.", NULL },
537 { "addr_class", sympy_get_addr_class, NULL, "Address class of the symbol." },
538 { "is_argument", sympy_is_argument, NULL,
539 "True if the symbol is an argument of a function." },
540 { "is_constant", sympy_is_constant, NULL,
541 "True if the symbol is a constant." },
542 { "is_function", sympy_is_function, NULL,
543 "True if the symbol is a function or method." },
544 { "is_variable", sympy_is_variable, NULL,
545 "True if the symbol is a variable." },
f0823d2c
TT
546 { "needs_frame", sympy_needs_frame, NULL,
547 "True if the symbol requires a frame for evaluation." },
64e7d9dd
TT
548 { "line", sympy_line, NULL,
549 "The source line number at which the symbol was defined." },
f3e9a817
PM
550 { NULL } /* Sentinel */
551};
552
29703da4
PM
553static PyMethodDef symbol_object_methods[] = {
554 { "is_valid", sympy_is_valid, METH_NOARGS,
555 "is_valid () -> Boolean.\n\
556Return true if this symbol is valid, false if not." },
f0823d2c
TT
557 { "value", sympy_value, METH_VARARGS,
558 "value ([frame]) -> gdb.Value\n\
559Return the value of the symbol." },
29703da4
PM
560 {NULL} /* Sentinel */
561};
562
f3e9a817
PM
563PyTypeObject symbol_object_type = {
564 PyObject_HEAD_INIT (NULL)
565 0, /*ob_size*/
566 "gdb.Symbol", /*tp_name*/
567 sizeof (symbol_object), /*tp_basicsize*/
568 0, /*tp_itemsize*/
569 sympy_dealloc, /*tp_dealloc*/
570 0, /*tp_print*/
571 0, /*tp_getattr*/
572 0, /*tp_setattr*/
573 0, /*tp_compare*/
574 0, /*tp_repr*/
575 0, /*tp_as_number*/
576 0, /*tp_as_sequence*/
577 0, /*tp_as_mapping*/
578 0, /*tp_hash */
579 0, /*tp_call*/
580 sympy_str, /*tp_str*/
581 0, /*tp_getattro*/
582 0, /*tp_setattro*/
583 0, /*tp_as_buffer*/
584 Py_TPFLAGS_DEFAULT, /*tp_flags*/
585 "GDB symbol object", /*tp_doc */
586 0, /*tp_traverse */
587 0, /*tp_clear */
588 0, /*tp_richcompare */
589 0, /*tp_weaklistoffset */
590 0, /*tp_iter */
591 0, /*tp_iternext */
29703da4 592 symbol_object_methods, /*tp_methods */
f3e9a817
PM
593 0, /*tp_members */
594 symbol_object_getset /*tp_getset */
595};
This page took 0.275633 seconds and 4 git commands to generate.