py-symbol.c 21 KB

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