1 /* Python interface to symbols. 2 3 Copyright (C) 2008-2015 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 class; 151 152 SYMPY_REQUIRE_VALID (self, symbol); 153 154 class = SYMBOL_CLASS (symbol); 155 156 return PyBool_FromLong (class == LOC_CONST || class == 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 class; 164 165 SYMPY_REQUIRE_VALID (self, symbol); 166 167 class = SYMBOL_CLASS (symbol); 168 169 return PyBool_FromLong (class == 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 class; 177 178 SYMPY_REQUIRE_VALID (self, symbol); 179 180 class = SYMBOL_CLASS (symbol); 181 182 return PyBool_FromLong (!SYMBOL_IS_ARGUMENT (symbol) 183 && (class == LOC_LOCAL || class == LOC_REGISTER 184 || class == LOC_STATIC || class == LOC_COMPUTED 185 || class == 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 volatile struct gdb_exception except; 196 int result = 0; 197 198 SYMPY_REQUIRE_VALID (self, symbol); 199 200 TRY_CATCH (except, RETURN_MASK_ALL) 201 { 202 result = symbol_read_needs_frame (symbol); 203 } 204 GDB_PY_HANDLE_EXCEPTION (except); 205 206 if (result) 207 Py_RETURN_TRUE; 208 Py_RETURN_FALSE; 209 } 210 211 /* Implementation of gdb.Symbol.line -> int. 212 Returns the line number at which the symbol was defined. */ 213 214 static PyObject * 215 sympy_line (PyObject *self, void *closure) 216 { 217 struct symbol *symbol = NULL; 218 219 SYMPY_REQUIRE_VALID (self, symbol); 220 221 return PyInt_FromLong (SYMBOL_LINE (symbol)); 222 } 223 224 /* Implementation of gdb.Symbol.is_valid (self) -> Boolean. 225 Returns True if this Symbol still exists in GDB. */ 226 227 static PyObject * 228 sympy_is_valid (PyObject *self, PyObject *args) 229 { 230 struct symbol *symbol = NULL; 231 232 symbol = symbol_object_to_symbol (self); 233 if (symbol == NULL) 234 Py_RETURN_FALSE; 235 236 Py_RETURN_TRUE; 237 } 238 239 /* Implementation of gdb.Symbol.value (self[, frame]) -> gdb.Value. Returns 240 the value of the symbol, or an error in various circumstances. */ 241 242 static PyObject * 243 sympy_value (PyObject *self, PyObject *args) 244 { 245 struct symbol *symbol = NULL; 246 struct frame_info *frame_info = NULL; 247 PyObject *frame_obj = NULL; 248 struct value *value = NULL; 249 volatile struct gdb_exception except; 250 251 if (!PyArg_ParseTuple (args, "|O", &frame_obj)) 252 return NULL; 253 254 if (frame_obj != NULL && !PyObject_TypeCheck (frame_obj, &frame_object_type)) 255 { 256 PyErr_SetString (PyExc_TypeError, "argument is not a frame"); 257 return NULL; 258 } 259 260 SYMPY_REQUIRE_VALID (self, symbol); 261 if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF) 262 { 263 PyErr_SetString (PyExc_TypeError, "cannot get the value of a typedef"); 264 return NULL; 265 } 266 267 TRY_CATCH (except, RETURN_MASK_ALL) 268 { 269 if (frame_obj != NULL) 270 { 271 frame_info = frame_object_to_frame_info (frame_obj); 272 if (frame_info == NULL) 273 error (_("invalid frame")); 274 } 275 276 if (symbol_read_needs_frame (symbol) && frame_info == NULL) 277 error (_("symbol requires a frame to compute its value")); 278 279 value = read_var_value (symbol, frame_info); 280 } 281 GDB_PY_HANDLE_EXCEPTION (except); 282 283 return value_to_value_object (value); 284 } 285 286 /* Given a symbol, and a symbol_object that has previously been 287 allocated and initialized, populate the symbol_object with the 288 struct symbol data. Also, register the symbol_object life-cycle 289 with the life-cycle of the object file associated with this 290 symbol, if needed. */ 291 static void 292 set_symbol (symbol_object *obj, struct symbol *symbol) 293 { 294 obj->symbol = symbol; 295 obj->prev = NULL; 296 if (SYMBOL_OBJFILE_OWNED (symbol) 297 && symbol_symtab (symbol) != NULL) 298 { 299 struct objfile *objfile = symbol_objfile (symbol); 300 301 obj->next = objfile_data (objfile, sympy_objfile_data_key); 302 if (obj->next) 303 obj->next->prev = obj; 304 set_objfile_data (objfile, sympy_objfile_data_key, obj); 305 } 306 else 307 obj->next = NULL; 308 } 309 310 /* Create a new symbol object (gdb.Symbol) that encapsulates the struct 311 symbol object from GDB. */ 312 PyObject * 313 symbol_to_symbol_object (struct symbol *sym) 314 { 315 symbol_object *sym_obj; 316 317 sym_obj = PyObject_New (symbol_object, &symbol_object_type); 318 if (sym_obj) 319 set_symbol (sym_obj, sym); 320 321 return (PyObject *) sym_obj; 322 } 323 324 /* Return the symbol that is wrapped by this symbol object. */ 325 struct symbol * 326 symbol_object_to_symbol (PyObject *obj) 327 { 328 if (! PyObject_TypeCheck (obj, &symbol_object_type)) 329 return NULL; 330 return ((symbol_object *) obj)->symbol; 331 } 332 333 static void 334 sympy_dealloc (PyObject *obj) 335 { 336 symbol_object *sym_obj = (symbol_object *) obj; 337 338 if (sym_obj->prev) 339 sym_obj->prev->next = sym_obj->next; 340 else if (sym_obj->symbol != NULL 341 && SYMBOL_OBJFILE_OWNED (sym_obj->symbol) 342 && symbol_symtab (sym_obj->symbol) != NULL) 343 { 344 set_objfile_data (symbol_objfile (sym_obj->symbol), 345 sympy_objfile_data_key, sym_obj->next); 346 } 347 if (sym_obj->next) 348 sym_obj->next->prev = sym_obj->prev; 349 sym_obj->symbol = NULL; 350 } 351 352 /* Implementation of 353 gdb.lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this) 354 A tuple with 2 elements is always returned. The first is the symbol 355 object or None, the second is a boolean with the value of 356 is_a_field_of_this (see comment in lookup_symbol_in_language). */ 357 358 PyObject * 359 gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw) 360 { 361 int domain = VAR_DOMAIN; 362 struct field_of_this_result is_a_field_of_this; 363 const char *name; 364 static char *keywords[] = { "name", "block", "domain", NULL }; 365 struct symbol *symbol = NULL; 366 PyObject *block_obj = NULL, *ret_tuple, *sym_obj, *bool_obj; 367 const struct block *block = NULL; 368 volatile struct gdb_exception except; 369 370 if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O!i", keywords, &name, 371 &block_object_type, &block_obj, &domain)) 372 return NULL; 373 374 if (block_obj) 375 block = block_object_to_block (block_obj); 376 else 377 { 378 struct frame_info *selected_frame; 379 volatile struct gdb_exception except; 380 381 TRY_CATCH (except, RETURN_MASK_ALL) 382 { 383 selected_frame = get_selected_frame (_("No frame selected.")); 384 block = get_frame_block (selected_frame, NULL); 385 } 386 GDB_PY_HANDLE_EXCEPTION (except); 387 } 388 389 TRY_CATCH (except, RETURN_MASK_ALL) 390 { 391 symbol = lookup_symbol (name, block, domain, &is_a_field_of_this); 392 } 393 GDB_PY_HANDLE_EXCEPTION (except); 394 395 ret_tuple = PyTuple_New (2); 396 if (!ret_tuple) 397 return NULL; 398 399 if (symbol) 400 { 401 sym_obj = symbol_to_symbol_object (symbol); 402 if (!sym_obj) 403 { 404 Py_DECREF (ret_tuple); 405 return NULL; 406 } 407 } 408 else 409 { 410 sym_obj = Py_None; 411 Py_INCREF (Py_None); 412 } 413 PyTuple_SET_ITEM (ret_tuple, 0, sym_obj); 414 415 bool_obj = (is_a_field_of_this.type != NULL) ? Py_True : Py_False; 416 Py_INCREF (bool_obj); 417 PyTuple_SET_ITEM (ret_tuple, 1, bool_obj); 418 419 return ret_tuple; 420 } 421 422 /* Implementation of 423 gdb.lookup_global_symbol (name [, domain]) -> symbol or None. */ 424 425 PyObject * 426 gdbpy_lookup_global_symbol (PyObject *self, PyObject *args, PyObject *kw) 427 { 428 int domain = VAR_DOMAIN; 429 const char *name; 430 static char *keywords[] = { "name", "domain", NULL }; 431 struct symbol *symbol = NULL; 432 PyObject *sym_obj; 433 volatile struct gdb_exception except; 434 435 if (! PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name, 436 &domain)) 437 return NULL; 438 439 TRY_CATCH (except, RETURN_MASK_ALL) 440 { 441 symbol = lookup_global_symbol (name, NULL, domain); 442 } 443 GDB_PY_HANDLE_EXCEPTION (except); 444 445 if (symbol) 446 { 447 sym_obj = symbol_to_symbol_object (symbol); 448 if (!sym_obj) 449 return NULL; 450 } 451 else 452 { 453 sym_obj = Py_None; 454 Py_INCREF (Py_None); 455 } 456 457 return sym_obj; 458 } 459 460 /* This function is called when an objfile is about to be freed. 461 Invalidate the symbol as further actions on the symbol would result 462 in bad data. All access to obj->symbol should be gated by 463 SYMPY_REQUIRE_VALID which will raise an exception on invalid 464 symbols. */ 465 static void 466 del_objfile_symbols (struct objfile *objfile, void *datum) 467 { 468 symbol_object *obj = datum; 469 while (obj) 470 { 471 symbol_object *next = obj->next; 472 473 obj->symbol = NULL; 474 obj->next = NULL; 475 obj->prev = NULL; 476 477 obj = next; 478 } 479 } 480 481 int 482 gdbpy_initialize_symbols (void) 483 { 484 if (PyType_Ready (&symbol_object_type) < 0) 485 return -1; 486 487 /* Register an objfile "free" callback so we can properly 488 invalidate symbol when an object file that is about to be 489 deleted. */ 490 sympy_objfile_data_key 491 = register_objfile_data_with_cleanup (NULL, del_objfile_symbols); 492 493 if (PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNDEF", LOC_UNDEF) < 0 494 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST", 495 LOC_CONST) < 0 496 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_STATIC", 497 LOC_STATIC) < 0 498 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGISTER", 499 LOC_REGISTER) < 0 500 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_ARG", 501 LOC_ARG) < 0 502 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REF_ARG", 503 LOC_REF_ARG) < 0 504 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LOCAL", 505 LOC_LOCAL) < 0 506 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_TYPEDEF", 507 LOC_TYPEDEF) < 0 508 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LABEL", 509 LOC_LABEL) < 0 510 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_BLOCK", 511 LOC_BLOCK) < 0 512 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST_BYTES", 513 LOC_CONST_BYTES) < 0 514 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNRESOLVED", 515 LOC_UNRESOLVED) < 0 516 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_OPTIMIZED_OUT", 517 LOC_OPTIMIZED_OUT) < 0 518 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_COMPUTED", 519 LOC_COMPUTED) < 0 520 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGPARM_ADDR", 521 LOC_REGPARM_ADDR) < 0 522 || PyModule_AddIntConstant (gdb_module, "SYMBOL_UNDEF_DOMAIN", 523 UNDEF_DOMAIN) < 0 524 || PyModule_AddIntConstant (gdb_module, "SYMBOL_VAR_DOMAIN", 525 VAR_DOMAIN) < 0 526 || PyModule_AddIntConstant (gdb_module, "SYMBOL_STRUCT_DOMAIN", 527 STRUCT_DOMAIN) < 0 528 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LABEL_DOMAIN", 529 LABEL_DOMAIN) < 0 530 || PyModule_AddIntConstant (gdb_module, "SYMBOL_VARIABLES_DOMAIN", 531 VARIABLES_DOMAIN) < 0 532 || PyModule_AddIntConstant (gdb_module, "SYMBOL_FUNCTIONS_DOMAIN", 533 FUNCTIONS_DOMAIN) < 0 534 || PyModule_AddIntConstant (gdb_module, "SYMBOL_TYPES_DOMAIN", 535 TYPES_DOMAIN) < 0) 536 return -1; 537 538 return gdb_pymodule_addobject (gdb_module, "Symbol", 539 (PyObject *) &symbol_object_type); 540 } 541 542 543 544 static PyGetSetDef symbol_object_getset[] = { 545 { "type", sympy_get_type, NULL, 546 "Type of the symbol.", NULL }, 547 { "symtab", sympy_get_symtab, NULL, 548 "Symbol table in which the symbol appears.", NULL }, 549 { "name", sympy_get_name, NULL, 550 "Name of the symbol, as it appears in the source code.", NULL }, 551 { "linkage_name", sympy_get_linkage_name, NULL, 552 "Name of the symbol, as used by the linker (i.e., may be mangled).", 553 NULL }, 554 { "print_name", sympy_get_print_name, NULL, 555 "Name of the symbol in a form suitable for output.\n\ 556 This is either name or linkage_name, depending on whether the user asked GDB\n\ 557 to display demangled or mangled names.", NULL }, 558 { "addr_class", sympy_get_addr_class, NULL, "Address class of the symbol." }, 559 { "is_argument", sympy_is_argument, NULL, 560 "True if the symbol is an argument of a function." }, 561 { "is_constant", sympy_is_constant, NULL, 562 "True if the symbol is a constant." }, 563 { "is_function", sympy_is_function, NULL, 564 "True if the symbol is a function or method." }, 565 { "is_variable", sympy_is_variable, NULL, 566 "True if the symbol is a variable." }, 567 { "needs_frame", sympy_needs_frame, NULL, 568 "True if the symbol requires a frame for evaluation." }, 569 { "line", sympy_line, NULL, 570 "The source line number at which the symbol was defined." }, 571 { NULL } /* Sentinel */ 572 }; 573 574 static PyMethodDef symbol_object_methods[] = { 575 { "is_valid", sympy_is_valid, METH_NOARGS, 576 "is_valid () -> Boolean.\n\ 577 Return true if this symbol is valid, false if not." }, 578 { "value", sympy_value, METH_VARARGS, 579 "value ([frame]) -> gdb.Value\n\ 580 Return the value of the symbol." }, 581 {NULL} /* Sentinel */ 582 }; 583 584 PyTypeObject symbol_object_type = { 585 PyVarObject_HEAD_INIT (NULL, 0) 586 "gdb.Symbol", /*tp_name*/ 587 sizeof (symbol_object), /*tp_basicsize*/ 588 0, /*tp_itemsize*/ 589 sympy_dealloc, /*tp_dealloc*/ 590 0, /*tp_print*/ 591 0, /*tp_getattr*/ 592 0, /*tp_setattr*/ 593 0, /*tp_compare*/ 594 0, /*tp_repr*/ 595 0, /*tp_as_number*/ 596 0, /*tp_as_sequence*/ 597 0, /*tp_as_mapping*/ 598 0, /*tp_hash */ 599 0, /*tp_call*/ 600 sympy_str, /*tp_str*/ 601 0, /*tp_getattro*/ 602 0, /*tp_setattro*/ 603 0, /*tp_as_buffer*/ 604 Py_TPFLAGS_DEFAULT, /*tp_flags*/ 605 "GDB symbol object", /*tp_doc */ 606 0, /*tp_traverse */ 607 0, /*tp_clear */ 608 0, /*tp_richcompare */ 609 0, /*tp_weaklistoffset */ 610 0, /*tp_iter */ 611 0, /*tp_iternext */ 612 symbol_object_methods, /*tp_methods */ 613 0, /*tp_members */ 614 symbol_object_getset /*tp_getset */ 615 }; 616