gdb/python/py-symbol.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Python interface to symbols.

  2.    Copyright (C) 2008-2015 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. typedef struct sympy_symbol_object {
  21.   PyObject_HEAD
  22.   /* The GDB symbol structure this object is wrapping.  */
  23.   struct symbol *symbol;
  24.   /* A symbol object is associated with an objfile, so keep track with
  25.      doubly-linked list, rooted in the objfile.  This lets us
  26.      invalidate the underlying struct symbol when the objfile is
  27.      deleted.  */
  28.   struct sympy_symbol_object *prev;
  29.   struct sympy_symbol_object *next;
  30. } symbol_object;

  31. /* Require a valid symbol.  All access to symbol_object->symbol should be
  32.    gated by this call.  */
  33. #define SYMPY_REQUIRE_VALID(symbol_obj, symbol)                \
  34.   do {                                                        \
  35.     symbol = symbol_object_to_symbol (symbol_obj);        \
  36.     if (symbol == NULL)                                        \
  37.       {                                                        \
  38.         PyErr_SetString (PyExc_RuntimeError,                \
  39.                          _("Symbol is invalid."));        \
  40.         return NULL;                                        \
  41.       }                                                        \
  42.   } while (0)

  43. static const struct objfile_data *sympy_objfile_data_key;

  44. static PyObject *
  45. sympy_str (PyObject *self)
  46. {
  47.   PyObject *result;
  48.   struct symbol *symbol = NULL;

  49.   SYMPY_REQUIRE_VALID (self, symbol);

  50.   result = PyString_FromString (SYMBOL_PRINT_NAME (symbol));

  51.   return result;
  52. }

  53. static PyObject *
  54. sympy_get_type (PyObject *self, void *closure)
  55. {
  56.   struct symbol *symbol = NULL;

  57.   SYMPY_REQUIRE_VALID (self, symbol);

  58.   if (SYMBOL_TYPE (symbol) == NULL)
  59.     {
  60.       Py_INCREF (Py_None);
  61.       return Py_None;
  62.     }

  63.   return type_to_type_object (SYMBOL_TYPE (symbol));
  64. }

  65. static PyObject *
  66. sympy_get_symtab (PyObject *self, void *closure)
  67. {
  68.   struct symbol *symbol = NULL;

  69.   SYMPY_REQUIRE_VALID (self, symbol);

  70.   if (!SYMBOL_OBJFILE_OWNED (symbol))
  71.     Py_RETURN_NONE;

  72.   return symtab_to_symtab_object (symbol_symtab (symbol));
  73. }

  74. static PyObject *
  75. sympy_get_name (PyObject *self, void *closure)
  76. {
  77.   struct symbol *symbol = NULL;

  78.   SYMPY_REQUIRE_VALID (self, symbol);

  79.   return PyString_FromString (SYMBOL_NATURAL_NAME (symbol));
  80. }

  81. static PyObject *
  82. sympy_get_linkage_name (PyObject *self, void *closure)
  83. {
  84.   struct symbol *symbol = NULL;

  85.   SYMPY_REQUIRE_VALID (self, symbol);

  86.   return PyString_FromString (SYMBOL_LINKAGE_NAME (symbol));
  87. }

  88. static PyObject *
  89. sympy_get_print_name (PyObject *self, void *closure)
  90. {
  91.   struct symbol *symbol = NULL;

  92.   SYMPY_REQUIRE_VALID (self, symbol);

  93.   return sympy_str (self);
  94. }

  95. static PyObject *
  96. sympy_get_addr_class (PyObject *self, void *closure)
  97. {
  98.   struct symbol *symbol = NULL;

  99.   SYMPY_REQUIRE_VALID (self, symbol);

  100.   return PyInt_FromLong (SYMBOL_CLASS (symbol));
  101. }

  102. static PyObject *
  103. sympy_is_argument (PyObject *self, void *closure)
  104. {
  105.   struct symbol *symbol = NULL;

  106.   SYMPY_REQUIRE_VALID (self, symbol);

  107.   return PyBool_FromLong (SYMBOL_IS_ARGUMENT (symbol));
  108. }

  109. static PyObject *
  110. sympy_is_constant (PyObject *self, void *closure)
  111. {
  112.   struct symbol *symbol = NULL;
  113.   enum address_class class;

  114.   SYMPY_REQUIRE_VALID (self, symbol);

  115.   class = SYMBOL_CLASS (symbol);

  116.   return PyBool_FromLong (class == LOC_CONST || class == LOC_CONST_BYTES);
  117. }

  118. static PyObject *
  119. sympy_is_function (PyObject *self, void *closure)
  120. {
  121.   struct symbol *symbol = NULL;
  122.   enum address_class class;

  123.   SYMPY_REQUIRE_VALID (self, symbol);

  124.   class = SYMBOL_CLASS (symbol);

  125.   return PyBool_FromLong (class == LOC_BLOCK);
  126. }

  127. static PyObject *
  128. sympy_is_variable (PyObject *self, void *closure)
  129. {
  130.   struct symbol *symbol = NULL;
  131.   enum address_class class;

  132.   SYMPY_REQUIRE_VALID (self, symbol);

  133.   class = SYMBOL_CLASS (symbol);

  134.   return PyBool_FromLong (!SYMBOL_IS_ARGUMENT (symbol)
  135.                           && (class == LOC_LOCAL || class == LOC_REGISTER
  136.                               || class == LOC_STATIC || class == LOC_COMPUTED
  137.                               || class == LOC_OPTIMIZED_OUT));
  138. }

  139. /* Implementation of gdb.Symbol.needs_frame -> Boolean.
  140.    Returns true iff the symbol needs a frame for evaluation.  */

  141. static PyObject *
  142. sympy_needs_frame (PyObject *self, void *closure)
  143. {
  144.   struct symbol *symbol = NULL;
  145.   volatile struct gdb_exception except;
  146.   int result = 0;

  147.   SYMPY_REQUIRE_VALID (self, symbol);

  148.   TRY_CATCH (except, RETURN_MASK_ALL)
  149.     {
  150.       result = symbol_read_needs_frame (symbol);
  151.     }
  152.   GDB_PY_HANDLE_EXCEPTION (except);

  153.   if (result)
  154.     Py_RETURN_TRUE;
  155.   Py_RETURN_FALSE;
  156. }

  157. /* Implementation of gdb.Symbol.line -> int.
  158.    Returns the line number at which the symbol was defined.  */

  159. static PyObject *
  160. sympy_line (PyObject *self, void *closure)
  161. {
  162.   struct symbol *symbol = NULL;

  163.   SYMPY_REQUIRE_VALID (self, symbol);

  164.   return PyInt_FromLong (SYMBOL_LINE (symbol));
  165. }

  166. /* Implementation of gdb.Symbol.is_valid (self) -> Boolean.
  167.    Returns True if this Symbol still exists in GDB.  */

  168. static PyObject *
  169. sympy_is_valid (PyObject *self, PyObject *args)
  170. {
  171.   struct symbol *symbol = NULL;

  172.   symbol = symbol_object_to_symbol (self);
  173.   if (symbol == NULL)
  174.     Py_RETURN_FALSE;

  175.   Py_RETURN_TRUE;
  176. }

  177. /* Implementation of gdb.Symbol.value (self[, frame]) -> gdb.Value.  Returns
  178.    the value of the symbol, or an error in various circumstances.  */

  179. static PyObject *
  180. sympy_value (PyObject *self, PyObject *args)
  181. {
  182.   struct symbol *symbol = NULL;
  183.   struct frame_info *frame_info = NULL;
  184.   PyObject *frame_obj = NULL;
  185.   struct value *value = NULL;
  186.   volatile struct gdb_exception except;

  187.   if (!PyArg_ParseTuple (args, "|O", &frame_obj))
  188.     return NULL;

  189.   if (frame_obj != NULL && !PyObject_TypeCheck (frame_obj, &frame_object_type))
  190.     {
  191.       PyErr_SetString (PyExc_TypeError, "argument is not a frame");
  192.       return NULL;
  193.     }

  194.   SYMPY_REQUIRE_VALID (self, symbol);
  195.   if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
  196.     {
  197.       PyErr_SetString (PyExc_TypeError, "cannot get the value of a typedef");
  198.       return NULL;
  199.     }

  200.   TRY_CATCH (except, RETURN_MASK_ALL)
  201.     {
  202.       if (frame_obj != NULL)
  203.         {
  204.           frame_info = frame_object_to_frame_info (frame_obj);
  205.           if (frame_info == NULL)
  206.             error (_("invalid frame"));
  207.         }

  208.       if (symbol_read_needs_frame (symbol) && frame_info == NULL)
  209.         error (_("symbol requires a frame to compute its value"));

  210.       value = read_var_value (symbol, frame_info);
  211.     }
  212.   GDB_PY_HANDLE_EXCEPTION (except);

  213.   return value_to_value_object (value);
  214. }

  215. /* Given a symbol, and a symbol_object that has previously been
  216.    allocated and initialized, populate the symbol_object with the
  217.    struct symbol data.  Also, register the symbol_object life-cycle
  218.    with the life-cycle of the object file associated with this
  219.    symbol, if needed.  */
  220. static void
  221. set_symbol (symbol_object *obj, struct symbol *symbol)
  222. {
  223.   obj->symbol = symbol;
  224.   obj->prev = NULL;
  225.   if (SYMBOL_OBJFILE_OWNED (symbol)
  226.       && symbol_symtab (symbol) != NULL)
  227.     {
  228.       struct objfile *objfile = symbol_objfile (symbol);

  229.       obj->next = objfile_data (objfile, sympy_objfile_data_key);
  230.       if (obj->next)
  231.         obj->next->prev = obj;
  232.       set_objfile_data (objfile, sympy_objfile_data_key, obj);
  233.     }
  234.   else
  235.     obj->next = NULL;
  236. }

  237. /* Create a new symbol object (gdb.Symbol) that encapsulates the struct
  238.    symbol object from GDB.  */
  239. PyObject *
  240. symbol_to_symbol_object (struct symbol *sym)
  241. {
  242.   symbol_object *sym_obj;

  243.   sym_obj = PyObject_New (symbol_object, &symbol_object_type);
  244.   if (sym_obj)
  245.     set_symbol (sym_obj, sym);

  246.   return (PyObject *) sym_obj;
  247. }

  248. /* Return the symbol that is wrapped by this symbol object.  */
  249. struct symbol *
  250. symbol_object_to_symbol (PyObject *obj)
  251. {
  252.   if (! PyObject_TypeCheck (obj, &symbol_object_type))
  253.     return NULL;
  254.   return ((symbol_object *) obj)->symbol;
  255. }

  256. static void
  257. sympy_dealloc (PyObject *obj)
  258. {
  259.   symbol_object *sym_obj = (symbol_object *) obj;

  260.   if (sym_obj->prev)
  261.     sym_obj->prev->next = sym_obj->next;
  262.   else if (sym_obj->symbol != NULL
  263.            && SYMBOL_OBJFILE_OWNED (sym_obj->symbol)
  264.            && symbol_symtab (sym_obj->symbol) != NULL)
  265.     {
  266.       set_objfile_data (symbol_objfile (sym_obj->symbol),
  267.                         sympy_objfile_data_key, sym_obj->next);
  268.     }
  269.   if (sym_obj->next)
  270.     sym_obj->next->prev = sym_obj->prev;
  271.   sym_obj->symbol = NULL;
  272. }

  273. /* Implementation of
  274.    gdb.lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this)
  275.    A tuple with 2 elements is always returned.  The first is the symbol
  276.    object or None, the second is a boolean with the value of
  277.    is_a_field_of_this (see comment in lookup_symbol_in_language).  */

  278. PyObject *
  279. gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw)
  280. {
  281.   int domain = VAR_DOMAIN;
  282.   struct field_of_this_result is_a_field_of_this;
  283.   const char *name;
  284.   static char *keywords[] = { "name", "block", "domain", NULL };
  285.   struct symbol *symbol = NULL;
  286.   PyObject *block_obj = NULL, *ret_tuple, *sym_obj, *bool_obj;
  287.   const struct block *block = NULL;
  288.   volatile struct gdb_exception except;

  289.   if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O!i", keywords, &name,
  290.                                      &block_object_type, &block_obj, &domain))
  291.     return NULL;

  292.   if (block_obj)
  293.     block = block_object_to_block (block_obj);
  294.   else
  295.     {
  296.       struct frame_info *selected_frame;
  297.       volatile struct gdb_exception except;

  298.       TRY_CATCH (except, RETURN_MASK_ALL)
  299.         {
  300.           selected_frame = get_selected_frame (_("No frame selected."));
  301.           block = get_frame_block (selected_frame, NULL);
  302.         }
  303.       GDB_PY_HANDLE_EXCEPTION (except);
  304.     }

  305.   TRY_CATCH (except, RETURN_MASK_ALL)
  306.     {
  307.       symbol = lookup_symbol (name, block, domain, &is_a_field_of_this);
  308.     }
  309.   GDB_PY_HANDLE_EXCEPTION (except);

  310.   ret_tuple = PyTuple_New (2);
  311.   if (!ret_tuple)
  312.     return NULL;

  313.   if (symbol)
  314.     {
  315.       sym_obj = symbol_to_symbol_object (symbol);
  316.       if (!sym_obj)
  317.         {
  318.           Py_DECREF (ret_tuple);
  319.           return NULL;
  320.         }
  321.     }
  322.   else
  323.     {
  324.       sym_obj = Py_None;
  325.       Py_INCREF (Py_None);
  326.     }
  327.   PyTuple_SET_ITEM (ret_tuple, 0, sym_obj);

  328.   bool_obj = (is_a_field_of_this.type != NULL) ? Py_True : Py_False;
  329.   Py_INCREF (bool_obj);
  330.   PyTuple_SET_ITEM (ret_tuple, 1, bool_obj);

  331.   return ret_tuple;
  332. }

  333. /* Implementation of
  334.    gdb.lookup_global_symbol (name [, domain]) -> symbol or None.  */

  335. PyObject *
  336. gdbpy_lookup_global_symbol (PyObject *self, PyObject *args, PyObject *kw)
  337. {
  338.   int domain = VAR_DOMAIN;
  339.   const char *name;
  340.   static char *keywords[] = { "name", "domain", NULL };
  341.   struct symbol *symbol = NULL;
  342.   PyObject *sym_obj;
  343.   volatile struct gdb_exception except;

  344.   if (! PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name,
  345.                                      &domain))
  346.     return NULL;

  347.   TRY_CATCH (except, RETURN_MASK_ALL)
  348.     {
  349.       symbol = lookup_global_symbol (name, NULL, domain);
  350.     }
  351.   GDB_PY_HANDLE_EXCEPTION (except);

  352.   if (symbol)
  353.     {
  354.       sym_obj = symbol_to_symbol_object (symbol);
  355.       if (!sym_obj)
  356.         return NULL;
  357.     }
  358.   else
  359.     {
  360.       sym_obj = Py_None;
  361.       Py_INCREF (Py_None);
  362.     }

  363.   return sym_obj;
  364. }

  365. /* This function is called when an objfile is about to be freed.
  366.    Invalidate the symbol as further actions on the symbol would result
  367.    in bad data.  All access to obj->symbol should be gated by
  368.    SYMPY_REQUIRE_VALID which will raise an exception on invalid
  369.    symbols.  */
  370. static void
  371. del_objfile_symbols (struct objfile *objfile, void *datum)
  372. {
  373.   symbol_object *obj = datum;
  374.   while (obj)
  375.     {
  376.       symbol_object *next = obj->next;

  377.       obj->symbol = NULL;
  378.       obj->next = NULL;
  379.       obj->prev = NULL;

  380.       obj = next;
  381.     }
  382. }

  383. int
  384. gdbpy_initialize_symbols (void)
  385. {
  386.   if (PyType_Ready (&symbol_object_type) < 0)
  387.     return -1;

  388.   /* Register an objfile "free" callback so we can properly
  389.      invalidate symbol when an object file that is about to be
  390.      deleted.  */
  391.   sympy_objfile_data_key
  392.     = register_objfile_data_with_cleanup (NULL, del_objfile_symbols);

  393.   if (PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNDEF", LOC_UNDEF) < 0
  394.       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST",
  395.                                   LOC_CONST) < 0
  396.       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_STATIC",
  397.                                   LOC_STATIC) < 0
  398.       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGISTER",
  399.                                   LOC_REGISTER) < 0
  400.       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_ARG",
  401.                                   LOC_ARG) < 0
  402.       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REF_ARG",
  403.                                   LOC_REF_ARG) < 0
  404.       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LOCAL",
  405.                                   LOC_LOCAL) < 0
  406.       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_TYPEDEF",
  407.                                   LOC_TYPEDEF) < 0
  408.       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LABEL",
  409.                                   LOC_LABEL) < 0
  410.       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_BLOCK",
  411.                                   LOC_BLOCK) < 0
  412.       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST_BYTES",
  413.                                   LOC_CONST_BYTES) < 0
  414.       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNRESOLVED",
  415.                                   LOC_UNRESOLVED) < 0
  416.       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_OPTIMIZED_OUT",
  417.                                   LOC_OPTIMIZED_OUT) < 0
  418.       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_COMPUTED",
  419.                                   LOC_COMPUTED) < 0
  420.       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGPARM_ADDR",
  421.                                   LOC_REGPARM_ADDR) < 0
  422.       || PyModule_AddIntConstant (gdb_module, "SYMBOL_UNDEF_DOMAIN",
  423.                                   UNDEF_DOMAIN) < 0
  424.       || PyModule_AddIntConstant (gdb_module, "SYMBOL_VAR_DOMAIN",
  425.                                   VAR_DOMAIN) < 0
  426.       || PyModule_AddIntConstant (gdb_module, "SYMBOL_STRUCT_DOMAIN",
  427.                                   STRUCT_DOMAIN) < 0
  428.       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LABEL_DOMAIN",
  429.                                   LABEL_DOMAIN) < 0
  430.       || PyModule_AddIntConstant (gdb_module, "SYMBOL_VARIABLES_DOMAIN",
  431.                                   VARIABLES_DOMAIN) < 0
  432.       || PyModule_AddIntConstant (gdb_module, "SYMBOL_FUNCTIONS_DOMAIN",
  433.                                   FUNCTIONS_DOMAIN) < 0
  434.       || PyModule_AddIntConstant (gdb_module, "SYMBOL_TYPES_DOMAIN",
  435.                                   TYPES_DOMAIN) < 0)
  436.     return -1;

  437.   return gdb_pymodule_addobject (gdb_module, "Symbol",
  438.                                  (PyObject *) &symbol_object_type);
  439. }



  440. static PyGetSetDef symbol_object_getset[] = {
  441.   { "type", sympy_get_type, NULL,
  442.     "Type of the symbol.", NULL },
  443.   { "symtab", sympy_get_symtab, NULL,
  444.     "Symbol table in which the symbol appears.", NULL },
  445.   { "name", sympy_get_name, NULL,
  446.     "Name of the symbol, as it appears in the source code.", NULL },
  447.   { "linkage_name", sympy_get_linkage_name, NULL,
  448.     "Name of the symbol, as used by the linker (i.e., may be mangled).",
  449.     NULL },
  450.   { "print_name", sympy_get_print_name, NULL,
  451.     "Name of the symbol in a form suitable for output.\n\
  452. This is either name or linkage_name, depending on whether the user asked GDB\n\
  453. to display demangled or mangled names.", NULL },
  454.   { "addr_class", sympy_get_addr_class, NULL, "Address class of the symbol." },
  455.   { "is_argument", sympy_is_argument, NULL,
  456.     "True if the symbol is an argument of a function." },
  457.   { "is_constant", sympy_is_constant, NULL,
  458.     "True if the symbol is a constant." },
  459.   { "is_function", sympy_is_function, NULL,
  460.     "True if the symbol is a function or method." },
  461.   { "is_variable", sympy_is_variable, NULL,
  462.     "True if the symbol is a variable." },
  463.   { "needs_frame", sympy_needs_frame, NULL,
  464.     "True if the symbol requires a frame for evaluation." },
  465.   { "line", sympy_line, NULL,
  466.     "The source line number at which the symbol was defined." },
  467.   { NULL/* Sentinel */
  468. };

  469. static PyMethodDef symbol_object_methods[] = {
  470.   { "is_valid", sympy_is_valid, METH_NOARGS,
  471.     "is_valid () -> Boolean.\n\
  472. Return true if this symbol is valid, false if not." },
  473.   { "value", sympy_value, METH_VARARGS,
  474.     "value ([frame]) -> gdb.Value\n\
  475. Return the value of the symbol." },
  476.   {NULL/* Sentinel */
  477. };

  478. PyTypeObject symbol_object_type = {
  479.   PyVarObject_HEAD_INIT (NULL, 0)
  480.   "gdb.Symbol",                          /*tp_name*/
  481.   sizeof (symbol_object),          /*tp_basicsize*/
  482.   0,                                  /*tp_itemsize*/
  483.   sympy_dealloc,                  /*tp_dealloc*/
  484.   0,                                  /*tp_print*/
  485.   0,                                  /*tp_getattr*/
  486.   0,                                  /*tp_setattr*/
  487.   0,                                  /*tp_compare*/
  488.   0,                                  /*tp_repr*/
  489.   0,                                  /*tp_as_number*/
  490.   0,                                  /*tp_as_sequence*/
  491.   0,                                  /*tp_as_mapping*/
  492.   0,                                  /*tp_hash */
  493.   0,                                  /*tp_call*/
  494.   sympy_str,                          /*tp_str*/
  495.   0,                                  /*tp_getattro*/
  496.   0,                                  /*tp_setattro*/
  497.   0,                                  /*tp_as_buffer*/
  498.   Py_TPFLAGS_DEFAULT,                  /*tp_flags*/
  499.   "GDB symbol object",                  /*tp_doc */
  500.   0,                                  /*tp_traverse */
  501.   0,                                  /*tp_clear */
  502.   0,                                  /*tp_richcompare */
  503.   0,                                  /*tp_weaklistoffset */
  504.   0,                                  /*tp_iter */
  505.   0,                                  /*tp_iternext */
  506.   symbol_object_methods,          /*tp_methods */
  507.   0,                                  /*tp_members */
  508.   symbol_object_getset                  /*tp_getset */
  509. };