1 /* Python interface to symbol tables. 2 3 Copyright (C) 2008-2017 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 "charset.h" 22 #include "symtab.h" 23 #include "source.h" 24 #include "python-internal.h" 25 #include "objfiles.h" 26 #include "block.h" 27 #include "py-ref.h" 28 29 typedef struct stpy_symtab_object { 30 PyObject_HEAD 31 /* The GDB Symbol table structure. */ 32 struct symtab *symtab; 33 /* A symtab object is associated with an objfile, so keep track with 34 a doubly-linked list, rooted in the objfile. This allows 35 invalidation of the underlying struct symtab when the objfile is 36 deleted. */ 37 struct stpy_symtab_object *prev; 38 struct stpy_symtab_object *next; 39 } symtab_object; 40 41 extern PyTypeObject symtab_object_type 42 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("symtab_object"); 43 static const struct objfile_data *stpy_objfile_data_key; 44 45 /* Require a valid symbol table. All access to symtab_object->symtab 46 should be gated by this call. */ 47 #define STPY_REQUIRE_VALID(symtab_obj, symtab) \ 48 do { \ 49 symtab = symtab_object_to_symtab (symtab_obj); \ 50 if (symtab == NULL) \ 51 { \ 52 PyErr_SetString (PyExc_RuntimeError, \ 53 _("Symbol Table is invalid.")); \ 54 return NULL; \ 55 } \ 56 } while (0) 57 58 typedef struct salpy_sal_object { 59 PyObject_HEAD 60 /* The GDB Symbol table structure. */ 61 symtab_object *symtab; 62 /* The GDB Symbol table and line structure. */ 63 struct symtab_and_line *sal; 64 /* A Symtab and line object is associated with an objfile, so keep 65 track with a doubly-linked list, rooted in the objfile. This 66 allows invalidation of the underlying struct symtab_and_line 67 when the objfile is deleted. */ 68 struct salpy_sal_object *prev; 69 struct salpy_sal_object *next; 70 } sal_object; 71 72 extern PyTypeObject sal_object_type 73 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("sal_object"); 74 static const struct objfile_data *salpy_objfile_data_key; 75 76 /* Require a valid symbol table and line object. All access to 77 sal_object->sal should be gated by this call. */ 78 #define SALPY_REQUIRE_VALID(sal_obj, sal) \ 79 do { \ 80 sal = sal_object_to_symtab_and_line (sal_obj); \ 81 if (sal == NULL) \ 82 { \ 83 PyErr_SetString (PyExc_RuntimeError, \ 84 _("Symbol Table and Line is invalid.")); \ 85 return NULL; \ 86 } \ 87 } while (0) 88 89 static PyObject * 90 stpy_str (PyObject *self) 91 { 92 PyObject *result; 93 struct symtab *symtab = NULL; 94 95 STPY_REQUIRE_VALID (self, symtab); 96 97 result = PyString_FromString (symtab_to_filename_for_display (symtab)); 98 99 return result; 100 } 101 102 static PyObject * 103 stpy_get_filename (PyObject *self, void *closure) 104 { 105 PyObject *str_obj; 106 struct symtab *symtab = NULL; 107 const char *filename; 108 109 STPY_REQUIRE_VALID (self, symtab); 110 filename = symtab_to_filename_for_display (symtab); 111 112 str_obj = host_string_to_python_string (filename); 113 return str_obj; 114 } 115 116 static PyObject * 117 stpy_get_objfile (PyObject *self, void *closure) 118 { 119 struct symtab *symtab = NULL; 120 PyObject *result; 121 122 STPY_REQUIRE_VALID (self, symtab); 123 124 result = objfile_to_objfile_object (SYMTAB_OBJFILE (symtab)); 125 Py_XINCREF (result); 126 return result; 127 } 128 129 /* Getter function for symtab.producer. */ 130 131 static PyObject * 132 stpy_get_producer (PyObject *self, void *closure) 133 { 134 struct symtab *symtab = NULL; 135 struct compunit_symtab *cust; 136 137 STPY_REQUIRE_VALID (self, symtab); 138 cust = SYMTAB_COMPUNIT (symtab); 139 if (COMPUNIT_PRODUCER (cust) != NULL) 140 { 141 const char *producer = COMPUNIT_PRODUCER (cust); 142 143 return host_string_to_python_string (producer); 144 } 145 146 Py_RETURN_NONE; 147 } 148 149 static PyObject * 150 stpy_fullname (PyObject *self, PyObject *args) 151 { 152 const char *fullname; 153 struct symtab *symtab = NULL; 154 155 STPY_REQUIRE_VALID (self, symtab); 156 157 fullname = symtab_to_fullname (symtab); 158 159 return host_string_to_python_string (fullname); 160 } 161 162 /* Implementation of gdb.Symtab.is_valid (self) -> Boolean. 163 Returns True if this Symbol table still exists in GDB. */ 164 165 static PyObject * 166 stpy_is_valid (PyObject *self, PyObject *args) 167 { 168 struct symtab *symtab = NULL; 169 170 symtab = symtab_object_to_symtab (self); 171 if (symtab == NULL) 172 Py_RETURN_FALSE; 173 174 Py_RETURN_TRUE; 175 } 176 177 /* Return the GLOBAL_BLOCK of the underlying symtab. */ 178 179 static PyObject * 180 stpy_global_block (PyObject *self, PyObject *args) 181 { 182 struct symtab *symtab = NULL; 183 struct block *block = NULL; 184 const struct blockvector *blockvector; 185 186 STPY_REQUIRE_VALID (self, symtab); 187 188 blockvector = SYMTAB_BLOCKVECTOR (symtab); 189 block = BLOCKVECTOR_BLOCK (blockvector, GLOBAL_BLOCK); 190 return block_to_block_object (block, SYMTAB_OBJFILE (symtab)); 191 } 192 193 /* Return the STATIC_BLOCK of the underlying symtab. */ 194 195 static PyObject * 196 stpy_static_block (PyObject *self, PyObject *args) 197 { 198 struct symtab *symtab = NULL; 199 struct block *block = NULL; 200 const struct blockvector *blockvector; 201 202 STPY_REQUIRE_VALID (self, symtab); 203 204 blockvector = SYMTAB_BLOCKVECTOR (symtab); 205 block = BLOCKVECTOR_BLOCK (blockvector, STATIC_BLOCK); 206 return block_to_block_object (block, SYMTAB_OBJFILE (symtab)); 207 } 208 209 /* Implementation of gdb.Symtab.linetable (self) -> gdb.LineTable. 210 Returns a gdb.LineTable object corresponding to this symbol 211 table. */ 212 213 static PyObject * 214 stpy_get_linetable (PyObject *self, PyObject *args) 215 { 216 struct symtab *symtab = NULL; 217 218 STPY_REQUIRE_VALID (self, symtab); 219 220 return symtab_to_linetable_object (self); 221 } 222 223 static PyObject * 224 salpy_str (PyObject *self) 225 { 226 char *s; 227 const char *filename; 228 sal_object *sal_obj; 229 PyObject *result; 230 struct symtab_and_line *sal = NULL; 231 232 SALPY_REQUIRE_VALID (self, sal); 233 234 sal_obj = (sal_object *) self; 235 filename = (sal_obj->symtab == (symtab_object *) Py_None) 236 ? "<unknown>" : symtab_to_filename_for_display (sal_obj->symtab->symtab); 237 238 s = xstrprintf ("symbol and line for %s, line %d", filename, 239 sal->line); 240 241 result = PyString_FromString (s); 242 xfree (s); 243 244 return result; 245 } 246 247 static void 248 stpy_dealloc (PyObject *obj) 249 { 250 symtab_object *symtab = (symtab_object *) obj; 251 252 if (symtab->prev) 253 symtab->prev->next = symtab->next; 254 else if (symtab->symtab) 255 { 256 set_objfile_data (SYMTAB_OBJFILE (symtab->symtab), 257 stpy_objfile_data_key, symtab->next); 258 } 259 if (symtab->next) 260 symtab->next->prev = symtab->prev; 261 symtab->symtab = NULL; 262 } 263 264 265 static PyObject * 266 salpy_get_pc (PyObject *self, void *closure) 267 { 268 struct symtab_and_line *sal = NULL; 269 270 SALPY_REQUIRE_VALID (self, sal); 271 272 return gdb_py_long_from_ulongest (sal->pc); 273 } 274 275 /* Implementation of the get method for the 'last' attribute of 276 gdb.Symtab_and_line. */ 277 278 static PyObject * 279 salpy_get_last (PyObject *self, void *closure) 280 { 281 struct symtab_and_line *sal = NULL; 282 283 SALPY_REQUIRE_VALID (self, sal); 284 285 if (sal->end > 0) 286 return gdb_py_long_from_ulongest (sal->end - 1); 287 else 288 Py_RETURN_NONE; 289 } 290 291 static PyObject * 292 salpy_get_line (PyObject *self, void *closure) 293 { 294 struct symtab_and_line *sal = NULL; 295 296 SALPY_REQUIRE_VALID (self, sal); 297 298 return PyInt_FromLong (sal->line); 299 } 300 301 static PyObject * 302 salpy_get_symtab (PyObject *self, void *closure) 303 { 304 struct symtab_and_line *sal; 305 sal_object *self_sal = (sal_object *) self; 306 307 SALPY_REQUIRE_VALID (self, sal); 308 309 Py_INCREF (self_sal->symtab); 310 311 return (PyObject *) self_sal->symtab; 312 } 313 314 /* Implementation of gdb.Symtab_and_line.is_valid (self) -> Boolean. 315 Returns True if this Symbol table and line object still exists GDB. */ 316 317 static PyObject * 318 salpy_is_valid (PyObject *self, PyObject *args) 319 { 320 struct symtab_and_line *sal; 321 322 sal = sal_object_to_symtab_and_line (self); 323 if (sal == NULL) 324 Py_RETURN_FALSE; 325 326 Py_RETURN_TRUE; 327 } 328 329 static void 330 salpy_dealloc (PyObject *self) 331 { 332 sal_object *self_sal = (sal_object *) self; 333 334 if (self_sal->prev) 335 self_sal->prev->next = self_sal->next; 336 else if (self_sal->symtab != (symtab_object * ) Py_None) 337 set_objfile_data (SYMTAB_OBJFILE (self_sal->symtab->symtab), 338 salpy_objfile_data_key, self_sal->next); 339 340 if (self_sal->next) 341 self_sal->next->prev = self_sal->prev; 342 343 Py_DECREF (self_sal->symtab); 344 xfree (self_sal->sal); 345 Py_TYPE (self)->tp_free (self); 346 } 347 348 /* Given a sal, and a sal_object that has previously been allocated 349 and initialized, populate the sal_object with the struct sal data. 350 Also, register the sal_object life-cycle with the life-cycle of the 351 object file associated with this sal, if needed. If a failure 352 occurs during the sal population, this function will return -1. */ 353 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION 354 set_sal (sal_object *sal_obj, struct symtab_and_line sal) 355 { 356 symtab_object *symtab_obj; 357 358 if (sal.symtab) 359 { 360 symtab_obj = (symtab_object *) symtab_to_symtab_object (sal.symtab); 361 /* If a symtab existed in the sal, but it cannot be duplicated, 362 we exit. */ 363 if (symtab_obj == NULL) 364 return -1; 365 } 366 else 367 { 368 symtab_obj = (symtab_object *) Py_None; 369 Py_INCREF (Py_None); 370 } 371 372 sal_obj->sal = ((struct symtab_and_line *) 373 xmemdup (&sal, sizeof (struct symtab_and_line), 374 sizeof (struct symtab_and_line))); 375 sal_obj->symtab = symtab_obj; 376 sal_obj->prev = NULL; 377 378 /* If the SAL does not have a symtab, we do not add it to the 379 objfile cleanup observer linked list. */ 380 if (sal_obj->symtab != (symtab_object *)Py_None) 381 { 382 sal_obj->next 383 = ((struct salpy_sal_object *) 384 objfile_data (SYMTAB_OBJFILE (sal_obj->symtab->symtab), 385 salpy_objfile_data_key)); 386 if (sal_obj->next) 387 sal_obj->next->prev = sal_obj; 388 389 set_objfile_data (SYMTAB_OBJFILE (sal_obj->symtab->symtab), 390 salpy_objfile_data_key, sal_obj); 391 } 392 else 393 sal_obj->next = NULL; 394 395 return 0; 396 } 397 398 /* Given a symtab, and a symtab_object that has previously been 399 allocated and initialized, populate the symtab_object with the 400 struct symtab data. Also, register the symtab_object life-cycle 401 with the life-cycle of the object file associated with this 402 symtab, if needed. */ 403 static void 404 set_symtab (symtab_object *obj, struct symtab *symtab) 405 { 406 obj->symtab = symtab; 407 obj->prev = NULL; 408 if (symtab) 409 { 410 obj->next 411 = ((struct stpy_symtab_object *) 412 objfile_data (SYMTAB_OBJFILE (symtab), stpy_objfile_data_key)); 413 if (obj->next) 414 obj->next->prev = obj; 415 set_objfile_data (SYMTAB_OBJFILE (symtab), stpy_objfile_data_key, obj); 416 } 417 else 418 obj->next = NULL; 419 } 420 421 /* Create a new symbol table (gdb.Symtab) object that encapsulates the 422 symtab structure from GDB. */ 423 PyObject * 424 symtab_to_symtab_object (struct symtab *symtab) 425 { 426 symtab_object *symtab_obj; 427 428 symtab_obj = PyObject_New (symtab_object, &symtab_object_type); 429 if (symtab_obj) 430 set_symtab (symtab_obj, symtab); 431 432 return (PyObject *) symtab_obj; 433 } 434 435 /* Create a new symtab and line (gdb.Symtab_and_line) object 436 that encapsulates the symtab_and_line structure from GDB. */ 437 PyObject * 438 symtab_and_line_to_sal_object (struct symtab_and_line sal) 439 { 440 gdbpy_ref<sal_object> sal_obj (PyObject_New (sal_object, &sal_object_type)); 441 if (sal_obj != NULL) 442 { 443 if (set_sal (sal_obj.get (), sal) < 0) 444 return NULL; 445 } 446 447 return (PyObject *) sal_obj.release (); 448 } 449 450 /* Return struct symtab_and_line reference that is wrapped by this 451 object. */ 452 struct symtab_and_line * 453 sal_object_to_symtab_and_line (PyObject *obj) 454 { 455 if (! PyObject_TypeCheck (obj, &sal_object_type)) 456 return NULL; 457 return ((sal_object *) obj)->sal; 458 } 459 460 /* Return struct symtab reference that is wrapped by this object. */ 461 struct symtab * 462 symtab_object_to_symtab (PyObject *obj) 463 { 464 if (! PyObject_TypeCheck (obj, &symtab_object_type)) 465 return NULL; 466 return ((symtab_object *) obj)->symtab; 467 } 468 469 /* This function is called when an objfile is about to be freed. 470 Invalidate the symbol table as further actions on the symbol table 471 would result in bad data. All access to obj->symtab should be 472 gated by STPY_REQUIRE_VALID which will raise an exception on 473 invalid symbol tables. */ 474 static void 475 del_objfile_symtab (struct objfile *objfile, void *datum) 476 { 477 symtab_object *obj = (symtab_object *) datum; 478 479 while (obj) 480 { 481 symtab_object *next = obj->next; 482 483 obj->symtab = NULL; 484 obj->next = NULL; 485 obj->prev = NULL; 486 obj = next; 487 } 488 } 489 490 /* This function is called when an objfile is about to be freed. 491 Invalidate the sal object as further actions on the sal 492 would result in bad data. All access to obj->sal should be 493 gated by SALPY_REQUIRE_VALID which will raise an exception on 494 invalid symbol table and line objects. */ 495 static void 496 del_objfile_sal (struct objfile *objfile, void *datum) 497 { 498 sal_object *obj = (sal_object *) datum; 499 500 while (obj) 501 { 502 sal_object *next = obj->next; 503 504 Py_DECREF (obj->symtab); 505 obj->symtab = (symtab_object *) Py_None; 506 Py_INCREF (Py_None); 507 508 obj->next = NULL; 509 obj->prev = NULL; 510 xfree (obj->sal); 511 obj->sal = NULL; 512 513 obj = next; 514 } 515 } 516 517 int 518 gdbpy_initialize_symtabs (void) 519 { 520 symtab_object_type.tp_new = PyType_GenericNew; 521 if (PyType_Ready (&symtab_object_type) < 0) 522 return -1; 523 524 sal_object_type.tp_new = PyType_GenericNew; 525 if (PyType_Ready (&sal_object_type) < 0) 526 return -1; 527 528 /* Register an objfile "free" callback so we can properly 529 invalidate symbol tables, and symbol table and line data 530 structures when an object file that is about to be 531 deleted. */ 532 stpy_objfile_data_key 533 = register_objfile_data_with_cleanup (NULL, del_objfile_symtab); 534 salpy_objfile_data_key 535 = register_objfile_data_with_cleanup (NULL, del_objfile_sal); 536 537 if (gdb_pymodule_addobject (gdb_module, "Symtab", 538 (PyObject *) &symtab_object_type) < 0) 539 return -1; 540 541 return gdb_pymodule_addobject (gdb_module, "Symtab_and_line", 542 (PyObject *) &sal_object_type); 543 } 544 545 546 547 static gdb_PyGetSetDef symtab_object_getset[] = { 548 { "filename", stpy_get_filename, NULL, 549 "The symbol table's source filename.", NULL }, 550 { "objfile", stpy_get_objfile, NULL, "The symtab's objfile.", 551 NULL }, 552 { "producer", stpy_get_producer, NULL, 553 "The name/version of the program that compiled this symtab.", NULL }, 554 {NULL} /* Sentinel */ 555 }; 556 557 static PyMethodDef symtab_object_methods[] = { 558 { "is_valid", stpy_is_valid, METH_NOARGS, 559 "is_valid () -> Boolean.\n\ 560 Return true if this symbol table is valid, false if not." }, 561 { "fullname", stpy_fullname, METH_NOARGS, 562 "fullname () -> String.\n\ 563 Return the symtab's full source filename." }, 564 { "global_block", stpy_global_block, METH_NOARGS, 565 "global_block () -> gdb.Block.\n\ 566 Return the global block of the symbol table." }, 567 { "static_block", stpy_static_block, METH_NOARGS, 568 "static_block () -> gdb.Block.\n\ 569 Return the static block of the symbol table." }, 570 { "linetable", stpy_get_linetable, METH_NOARGS, 571 "linetable () -> gdb.LineTable.\n\ 572 Return the LineTable associated with this symbol table" }, 573 {NULL} /* Sentinel */ 574 }; 575 576 PyTypeObject symtab_object_type = { 577 PyVarObject_HEAD_INIT (NULL, 0) 578 "gdb.Symtab", /*tp_name*/ 579 sizeof (symtab_object), /*tp_basicsize*/ 580 0, /*tp_itemsize*/ 581 stpy_dealloc, /*tp_dealloc*/ 582 0, /*tp_print*/ 583 0, /*tp_getattr*/ 584 0, /*tp_setattr*/ 585 0, /*tp_compare*/ 586 0, /*tp_repr*/ 587 0, /*tp_as_number*/ 588 0, /*tp_as_sequence*/ 589 0, /*tp_as_mapping*/ 590 0, /*tp_hash */ 591 0, /*tp_call*/ 592 stpy_str, /*tp_str*/ 593 0, /*tp_getattro*/ 594 0, /*tp_setattro*/ 595 0, /*tp_as_buffer*/ 596 Py_TPFLAGS_DEFAULT, /*tp_flags*/ 597 "GDB symtab object", /*tp_doc */ 598 0, /*tp_traverse */ 599 0, /*tp_clear */ 600 0, /*tp_richcompare */ 601 0, /*tp_weaklistoffset */ 602 0, /*tp_iter */ 603 0, /*tp_iternext */ 604 symtab_object_methods, /*tp_methods */ 605 0, /*tp_members */ 606 symtab_object_getset /*tp_getset */ 607 }; 608 609 static gdb_PyGetSetDef sal_object_getset[] = { 610 { "symtab", salpy_get_symtab, NULL, "Symtab object.", NULL }, 611 { "pc", salpy_get_pc, NULL, "Return the symtab_and_line's pc.", NULL }, 612 { "last", salpy_get_last, NULL, 613 "Return the symtab_and_line's last address.", NULL }, 614 { "line", salpy_get_line, NULL, 615 "Return the symtab_and_line's line.", NULL }, 616 {NULL} /* Sentinel */ 617 }; 618 619 static PyMethodDef sal_object_methods[] = { 620 { "is_valid", salpy_is_valid, METH_NOARGS, 621 "is_valid () -> Boolean.\n\ 622 Return true if this symbol table and line is valid, false if not." }, 623 {NULL} /* Sentinel */ 624 }; 625 626 PyTypeObject sal_object_type = { 627 PyVarObject_HEAD_INIT (NULL, 0) 628 "gdb.Symtab_and_line", /*tp_name*/ 629 sizeof (sal_object), /*tp_basicsize*/ 630 0, /*tp_itemsize*/ 631 salpy_dealloc, /*tp_dealloc*/ 632 0, /*tp_print*/ 633 0, /*tp_getattr*/ 634 0, /*tp_setattr*/ 635 0, /*tp_compare*/ 636 0, /*tp_repr*/ 637 0, /*tp_as_number*/ 638 0, /*tp_as_sequence*/ 639 0, /*tp_as_mapping*/ 640 0, /*tp_hash */ 641 0, /*tp_call*/ 642 salpy_str, /*tp_str*/ 643 0, /*tp_getattro*/ 644 0, /*tp_setattro*/ 645 0, /*tp_as_buffer*/ 646 Py_TPFLAGS_DEFAULT, /*tp_flags*/ 647 "GDB symtab_and_line object", /*tp_doc */ 648 0, /*tp_traverse */ 649 0, /*tp_clear */ 650 0, /*tp_richcompare */ 651 0, /*tp_weaklistoffset */ 652 0, /*tp_iter */ 653 0, /*tp_iternext */ 654 sal_object_methods, /*tp_methods */ 655 0, /*tp_members */ 656 sal_object_getset /*tp_getset */ 657 }; 658