GDB (API)
/home/stan/gdb/src/gdb/python/py-symtab.c
Go to the documentation of this file.
00001 /* Python interface to symbol tables.
00002 
00003    Copyright (C) 2008-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "charset.h"
00022 #include "symtab.h"
00023 #include "source.h"
00024 #include "python-internal.h"
00025 #include "objfiles.h"
00026 #include "block.h"
00027 
00028 typedef struct stpy_symtab_object {
00029   PyObject_HEAD
00030   /* The GDB Symbol table structure.  */
00031   struct symtab *symtab;
00032   /* A symtab object is associated with an objfile, so keep track with
00033      a doubly-linked list, rooted in the objfile.  This allows
00034      invalidation of the underlying struct symtab when the objfile is
00035      deleted.  */
00036   struct stpy_symtab_object *prev;
00037   struct stpy_symtab_object *next;
00038 } symtab_object;
00039 
00040 static PyTypeObject symtab_object_type
00041     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("symtab_object");
00042 static const struct objfile_data *stpy_objfile_data_key;
00043 
00044 /* Require a valid symbol table.  All access to symtab_object->symtab
00045    should be gated by this call.  */
00046 #define STPY_REQUIRE_VALID(symtab_obj, symtab)           \
00047   do {                                                   \
00048     symtab = symtab_object_to_symtab (symtab_obj);       \
00049     if (symtab == NULL)                                  \
00050       {                                                  \
00051         PyErr_SetString (PyExc_RuntimeError,             \
00052                          _("Symbol Table is invalid.")); \
00053         return NULL;                                     \
00054       }                                                  \
00055   } while (0)
00056 
00057 typedef struct salpy_sal_object {
00058   PyObject_HEAD
00059   /* The GDB Symbol table structure.  */
00060   symtab_object *symtab;
00061   /* The GDB Symbol table and line structure.  */
00062   struct symtab_and_line *sal;
00063   /* A Symtab and line object is associated with an objfile, so keep
00064      track with a doubly-linked list, rooted in the objfile.  This
00065      allows invalidation of the underlying struct symtab_and_line
00066      when the objfile is deleted.  */
00067   struct salpy_sal_object *prev;
00068   struct salpy_sal_object *next;
00069 } sal_object;
00070 
00071 static PyTypeObject sal_object_type
00072     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("sal_object");
00073 static const struct objfile_data *salpy_objfile_data_key;
00074 
00075 /* Require a valid symbol table and line object.  All access to
00076    sal_object->sal should be gated by this call.  */
00077 #define SALPY_REQUIRE_VALID(sal_obj, sal)                               \
00078   do {                                                                  \
00079     sal = sal_object_to_symtab_and_line (sal_obj);                      \
00080     if (sal == NULL)                                                    \
00081       {                                                                 \
00082           PyErr_SetString (PyExc_RuntimeError,                          \
00083                            _("Symbol Table and Line is invalid."));     \
00084           return NULL;                                                  \
00085         }                                                               \
00086   } while (0)
00087 
00088 static PyObject *
00089 stpy_str (PyObject *self)
00090 {
00091   PyObject *result;
00092   struct symtab *symtab = NULL;
00093 
00094   STPY_REQUIRE_VALID (self, symtab);
00095 
00096   result = PyString_FromString (symtab_to_filename_for_display (symtab));
00097 
00098   return result;
00099 }
00100 
00101 static PyObject *
00102 stpy_get_filename (PyObject *self, void *closure)
00103 {
00104   PyObject *str_obj;
00105   struct symtab *symtab = NULL;
00106   const char *filename;
00107 
00108   STPY_REQUIRE_VALID (self, symtab);
00109   filename = symtab_to_filename_for_display (symtab);
00110 
00111   str_obj = PyString_Decode (filename, strlen (filename),
00112                              host_charset (), NULL);
00113   return str_obj;
00114 }
00115 
00116 static PyObject *
00117 stpy_get_objfile (PyObject *self, void *closure)
00118 {
00119   struct symtab *symtab = NULL;
00120   PyObject *result;
00121 
00122   STPY_REQUIRE_VALID (self, symtab);
00123 
00124   result = objfile_to_objfile_object (symtab->objfile);
00125   Py_XINCREF (result);
00126   return result;
00127 }
00128 
00129 static PyObject *
00130 stpy_fullname (PyObject *self, PyObject *args)
00131 {
00132   const char *fullname;
00133   struct symtab *symtab = NULL;
00134 
00135   STPY_REQUIRE_VALID (self, symtab);
00136 
00137   fullname = symtab_to_fullname (symtab);
00138 
00139   return PyString_Decode (fullname, strlen (fullname), host_charset (), NULL);
00140 }
00141 
00142 /* Implementation of gdb.Symtab.is_valid (self) -> Boolean.
00143    Returns True if this Symbol table still exists in GDB.  */
00144 
00145 static PyObject *
00146 stpy_is_valid (PyObject *self, PyObject *args)
00147 {
00148   struct symtab *symtab = NULL;
00149 
00150   symtab = symtab_object_to_symtab (self);
00151   if (symtab == NULL)
00152     Py_RETURN_FALSE;
00153 
00154   Py_RETURN_TRUE;
00155 }
00156 
00157 /* Return the GLOBAL_BLOCK of the underlying symtab.  */
00158 
00159 static PyObject *
00160 stpy_global_block (PyObject *self, PyObject *args)
00161 {
00162   struct symtab *symtab = NULL;
00163   struct block *block = NULL;
00164   struct blockvector *blockvector;
00165 
00166   STPY_REQUIRE_VALID (self, symtab);
00167 
00168   blockvector = BLOCKVECTOR (symtab);
00169   block = BLOCKVECTOR_BLOCK (blockvector, GLOBAL_BLOCK);
00170   return block_to_block_object (block, symtab->objfile);
00171 }
00172 
00173 /* Return the STATIC_BLOCK of the underlying symtab.  */
00174 
00175 static PyObject *
00176 stpy_static_block (PyObject *self, PyObject *args)
00177 {
00178   struct symtab *symtab = NULL;
00179   struct block *block = NULL;
00180   struct blockvector *blockvector;
00181 
00182   STPY_REQUIRE_VALID (self, symtab);
00183 
00184   blockvector = BLOCKVECTOR (symtab);
00185   block = BLOCKVECTOR_BLOCK (blockvector, STATIC_BLOCK);
00186   return block_to_block_object (block, symtab->objfile);
00187 }
00188 
00189 static PyObject *
00190 salpy_str (PyObject *self)
00191 {
00192   char *s;
00193   const char *filename;
00194   sal_object *sal_obj;
00195   PyObject *result;
00196   struct symtab_and_line *sal = NULL;
00197 
00198   SALPY_REQUIRE_VALID (self, sal);
00199 
00200   sal_obj = (sal_object *) self;
00201   filename = (sal_obj->symtab == (symtab_object *) Py_None)
00202     ? "<unknown>" : symtab_to_filename_for_display (sal_obj->symtab->symtab);
00203 
00204   s = xstrprintf ("symbol and line for %s, line %d", filename,
00205                   sal->line);
00206 
00207   result = PyString_FromString (s);
00208   xfree (s);
00209 
00210   return result;
00211 }
00212 
00213 static void
00214 stpy_dealloc (PyObject *obj)
00215 {
00216   symtab_object *symtab = (symtab_object *) obj;
00217 
00218   if (symtab->prev)
00219     symtab->prev->next = symtab->next;
00220   else if (symtab->symtab)
00221     {
00222       set_objfile_data (symtab->symtab->objfile,
00223                         stpy_objfile_data_key, symtab->next);
00224     }
00225   if (symtab->next)
00226     symtab->next->prev = symtab->prev;
00227   symtab->symtab = NULL;
00228 }
00229 
00230 
00231 static PyObject *
00232 salpy_get_pc (PyObject *self, void *closure)
00233 {
00234   struct symtab_and_line *sal = NULL;
00235 
00236   SALPY_REQUIRE_VALID (self, sal);
00237 
00238   return gdb_py_long_from_ulongest (sal->pc);
00239 }
00240 
00241 /* Implementation of the get method for the 'last' attribute of
00242    gdb.Symtab_and_line.  */
00243 
00244 static PyObject *
00245 salpy_get_last (PyObject *self, void *closure)
00246 {
00247   struct symtab_and_line *sal = NULL;
00248 
00249   SALPY_REQUIRE_VALID (self, sal);
00250 
00251   if (sal->end > 0)
00252     return gdb_py_long_from_ulongest (sal->end - 1);
00253   else
00254     Py_RETURN_NONE;
00255 }
00256 
00257 static PyObject *
00258 salpy_get_line (PyObject *self, void *closure)
00259 {
00260   struct symtab_and_line *sal = NULL;
00261 
00262   SALPY_REQUIRE_VALID (self, sal);
00263 
00264   return PyInt_FromLong (sal->line);
00265 }
00266 
00267 static PyObject *
00268 salpy_get_symtab (PyObject *self, void *closure)
00269 {
00270   struct symtab_and_line *sal;
00271   sal_object *self_sal = (sal_object *) self;
00272 
00273   SALPY_REQUIRE_VALID (self, sal);
00274 
00275   Py_INCREF (self_sal->symtab);
00276 
00277   return (PyObject *) self_sal->symtab;
00278 }
00279 
00280 /* Implementation of gdb.Symtab_and_line.is_valid (self) -> Boolean.
00281    Returns True if this Symbol table and line object still exists GDB.  */
00282 
00283 static PyObject *
00284 salpy_is_valid (PyObject *self, PyObject *args)
00285 {
00286   struct symtab_and_line *sal;
00287 
00288   sal = sal_object_to_symtab_and_line (self);
00289   if (sal == NULL)
00290     Py_RETURN_FALSE;
00291 
00292   Py_RETURN_TRUE;
00293 }
00294 
00295 static void
00296 salpy_dealloc (PyObject *self)
00297 {
00298   sal_object *self_sal = (sal_object *) self;
00299 
00300   if (self_sal->prev)
00301     self_sal->prev->next = self_sal->next;
00302   else if (self_sal->symtab != (symtab_object * ) Py_None)
00303     set_objfile_data (self_sal->symtab->symtab->objfile,
00304                       salpy_objfile_data_key, self_sal->next);
00305 
00306   if (self_sal->next)
00307     self_sal->next->prev = self_sal->prev;
00308 
00309   Py_DECREF (self_sal->symtab);
00310   xfree (self_sal->sal);
00311   Py_TYPE (self)->tp_free (self);
00312 }
00313 
00314 /* Given a sal, and a sal_object that has previously been allocated
00315    and initialized, populate the sal_object with the struct sal data.
00316    Also, register the sal_object life-cycle with the life-cycle of the
00317    object file associated with this sal, if needed.  If a failure
00318    occurs during the sal population, this function will return -1.  */
00319 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
00320 set_sal (sal_object *sal_obj, struct symtab_and_line sal)
00321 {
00322   symtab_object *symtab_obj;
00323 
00324   if (sal.symtab)
00325     {
00326       symtab_obj = (symtab_object *) symtab_to_symtab_object  (sal.symtab);
00327       /* If a symtab existed in the sal, but it cannot be duplicated,
00328          we exit.  */
00329       if (symtab_obj == NULL)
00330         return -1;
00331     }
00332   else
00333     {
00334       symtab_obj = (symtab_object *) Py_None;
00335       Py_INCREF (Py_None);
00336     }
00337 
00338   sal_obj->sal = xmemdup (&sal, sizeof (struct symtab_and_line),
00339                           sizeof (struct symtab_and_line));
00340   sal_obj->symtab = symtab_obj;
00341   sal_obj->prev = NULL;
00342 
00343   /* If the SAL does not have a symtab, we do not add it to the
00344      objfile cleanup observer linked list.  */
00345   if (sal_obj->symtab != (symtab_object *)Py_None)
00346     {
00347       sal_obj->next = objfile_data (sal_obj->symtab->symtab->objfile,
00348                                     salpy_objfile_data_key);
00349       if (sal_obj->next)
00350         sal_obj->next->prev = sal_obj;
00351 
00352       set_objfile_data (sal_obj->symtab->symtab->objfile,
00353                         salpy_objfile_data_key, sal_obj);
00354     }
00355   else
00356     sal_obj->next = NULL;
00357 
00358   return 0;
00359 }
00360 
00361 /* Given a symtab, and a symtab_object that has previously been
00362    allocated and initialized, populate the symtab_object with the
00363    struct symtab data.  Also, register the symtab_object life-cycle
00364    with the life-cycle of the object file associated with this
00365    symtab, if needed.  */
00366 static void
00367 set_symtab (symtab_object *obj, struct symtab *symtab)
00368 {
00369   obj->symtab = symtab;
00370   obj->prev = NULL;
00371   if (symtab)
00372     {
00373       obj->next = objfile_data (symtab->objfile, stpy_objfile_data_key);
00374       if (obj->next)
00375         obj->next->prev = obj;
00376       set_objfile_data (symtab->objfile, stpy_objfile_data_key, obj);
00377     }
00378   else
00379     obj->next = NULL;
00380 }
00381 
00382 /* Create a new symbol table (gdb.Symtab) object that encapsulates the
00383    symtab structure from GDB.  */
00384 PyObject *
00385 symtab_to_symtab_object (struct symtab *symtab)
00386 {
00387   symtab_object *symtab_obj;
00388 
00389   symtab_obj = PyObject_New (symtab_object, &symtab_object_type);
00390   if (symtab_obj)
00391     set_symtab (symtab_obj, symtab);
00392 
00393   return (PyObject *) symtab_obj;
00394 }
00395 
00396 /* Create a new symtab and line (gdb.Symtab_and_line) object
00397    that encapsulates the symtab_and_line structure from GDB.  */
00398 PyObject *
00399 symtab_and_line_to_sal_object (struct symtab_and_line sal)
00400 {
00401   sal_object *sal_obj;
00402   int success = 0;
00403 
00404   sal_obj = PyObject_New (sal_object, &sal_object_type);
00405   if (sal_obj)
00406     {
00407       if (set_sal (sal_obj, sal) < 0)
00408         {
00409           Py_DECREF (sal_obj);
00410           return NULL;
00411         }
00412     }
00413 
00414   return (PyObject *) sal_obj;
00415 }
00416 
00417 /* Return struct symtab_and_line reference that is wrapped by this
00418    object.  */
00419 struct symtab_and_line *
00420 sal_object_to_symtab_and_line (PyObject *obj)
00421 {
00422   if (! PyObject_TypeCheck (obj, &sal_object_type))
00423     return NULL;
00424   return ((sal_object *) obj)->sal;
00425 }
00426 
00427 /* Return struct symtab reference that is wrapped by this object.  */
00428 struct symtab *
00429 symtab_object_to_symtab (PyObject *obj)
00430 {
00431   if (! PyObject_TypeCheck (obj, &symtab_object_type))
00432     return NULL;
00433   return ((symtab_object *) obj)->symtab;
00434 }
00435 
00436 /* This function is called when an objfile is about to be freed.
00437    Invalidate the symbol table as further actions on the symbol table
00438    would result in bad data.  All access to obj->symtab should be
00439    gated by STPY_REQUIRE_VALID which will raise an exception on
00440    invalid symbol tables.  */
00441 static void
00442 del_objfile_symtab (struct objfile *objfile, void *datum)
00443 {
00444   symtab_object *obj = datum;
00445 
00446   while (obj)
00447     {
00448       symtab_object *next = obj->next;
00449 
00450       obj->symtab = NULL;
00451       obj->next = NULL;
00452       obj->prev = NULL;
00453       obj = next;
00454     }
00455 }
00456 
00457 /* This function is called when an objfile is about to be freed.
00458    Invalidate the sal object as further actions on the sal
00459    would result in bad data.  All access to obj->sal should be
00460    gated by SALPY_REQUIRE_VALID which will raise an exception on
00461    invalid symbol table and line objects.  */
00462 static void
00463 del_objfile_sal (struct objfile *objfile, void *datum)
00464 {
00465   sal_object *obj = datum;
00466 
00467   while (obj)
00468     {
00469       sal_object *next = obj->next;
00470 
00471       Py_DECREF (obj->symtab);
00472       obj->symtab = (symtab_object *) Py_None;
00473       Py_INCREF (Py_None);
00474 
00475       obj->next = NULL;
00476       obj->prev = NULL;
00477       xfree (obj->sal);
00478       obj->sal = NULL;
00479 
00480       obj = next;
00481     }
00482 }
00483 
00484 int
00485 gdbpy_initialize_symtabs (void)
00486 {
00487   symtab_object_type.tp_new = PyType_GenericNew;
00488   if (PyType_Ready (&symtab_object_type) < 0)
00489     return -1;
00490 
00491   sal_object_type.tp_new = PyType_GenericNew;
00492   if (PyType_Ready (&sal_object_type) < 0)
00493     return -1;
00494 
00495   /* Register an objfile "free" callback so we can properly
00496      invalidate symbol tables, and symbol table and line data
00497      structures when an object file that is about to be
00498      deleted.  */
00499   stpy_objfile_data_key
00500     = register_objfile_data_with_cleanup (NULL, del_objfile_symtab);
00501   salpy_objfile_data_key
00502     = register_objfile_data_with_cleanup (NULL, del_objfile_sal);
00503 
00504   if (gdb_pymodule_addobject (gdb_module, "Symtab",
00505                               (PyObject *) &symtab_object_type) < 0)
00506     return -1;
00507 
00508   return gdb_pymodule_addobject (gdb_module, "Symtab_and_line",
00509                                  (PyObject *) &sal_object_type);
00510 }
00511 
00512 
00513 
00514 static PyGetSetDef symtab_object_getset[] = {
00515   { "filename", stpy_get_filename, NULL,
00516     "The symbol table's source filename.", NULL },
00517   { "objfile", stpy_get_objfile, NULL, "The symtab's objfile.",
00518     NULL },
00519   {NULL}  /* Sentinel */
00520 };
00521 
00522 static PyMethodDef symtab_object_methods[] = {
00523   { "is_valid", stpy_is_valid, METH_NOARGS,
00524     "is_valid () -> Boolean.\n\
00525 Return true if this symbol table is valid, false if not." },
00526   { "fullname", stpy_fullname, METH_NOARGS,
00527     "fullname () -> String.\n\
00528 Return the symtab's full source filename." },
00529   { "global_block", stpy_global_block, METH_NOARGS,
00530     "global_block () -> gdb.Block.\n\
00531 Return the global block of the symbol table." },
00532   { "static_block", stpy_static_block, METH_NOARGS,
00533     "static_block () -> gdb.Block.\n\
00534 Return the static block of the symbol table." },
00535   {NULL}  /* Sentinel */
00536 };
00537 
00538 static PyTypeObject symtab_object_type = {
00539   PyVarObject_HEAD_INIT (NULL, 0)
00540   "gdb.Symtab",                   /*tp_name*/
00541   sizeof (symtab_object),         /*tp_basicsize*/
00542   0,                              /*tp_itemsize*/
00543   stpy_dealloc,                   /*tp_dealloc*/
00544   0,                              /*tp_print*/
00545   0,                              /*tp_getattr*/
00546   0,                              /*tp_setattr*/
00547   0,                              /*tp_compare*/
00548   0,                              /*tp_repr*/
00549   0,                              /*tp_as_number*/
00550   0,                              /*tp_as_sequence*/
00551   0,                              /*tp_as_mapping*/
00552   0,                              /*tp_hash */
00553   0,                              /*tp_call*/
00554   stpy_str,                       /*tp_str*/
00555   0,                              /*tp_getattro*/
00556   0,                              /*tp_setattro*/
00557   0,                              /*tp_as_buffer*/
00558   Py_TPFLAGS_DEFAULT,             /*tp_flags*/
00559   "GDB symtab object",            /*tp_doc */
00560   0,                              /*tp_traverse */
00561   0,                              /*tp_clear */
00562   0,                              /*tp_richcompare */
00563   0,                              /*tp_weaklistoffset */
00564   0,                              /*tp_iter */
00565   0,                              /*tp_iternext */
00566   symtab_object_methods,          /*tp_methods */
00567   0,                              /*tp_members */
00568   symtab_object_getset            /*tp_getset */
00569 };
00570 
00571 static PyGetSetDef sal_object_getset[] = {
00572   { "symtab", salpy_get_symtab, NULL, "Symtab object.", NULL },
00573   { "pc", salpy_get_pc, NULL, "Return the symtab_and_line's pc.", NULL },
00574   { "last", salpy_get_last, NULL,
00575     "Return the symtab_and_line's last address.", NULL },
00576   { "line", salpy_get_line, NULL,
00577     "Return the symtab_and_line's line.", NULL },
00578   {NULL}  /* Sentinel */
00579 };
00580 
00581 static PyMethodDef sal_object_methods[] = {
00582   { "is_valid", salpy_is_valid, METH_NOARGS,
00583     "is_valid () -> Boolean.\n\
00584 Return true if this symbol table and line is valid, false if not." },
00585   {NULL}  /* Sentinel */
00586 };
00587 
00588 static PyTypeObject sal_object_type = {
00589   PyVarObject_HEAD_INIT (NULL, 0)
00590   "gdb.Symtab_and_line",          /*tp_name*/
00591   sizeof (sal_object),            /*tp_basicsize*/
00592   0,                              /*tp_itemsize*/
00593   salpy_dealloc,                  /*tp_dealloc*/
00594   0,                              /*tp_print*/
00595   0,                              /*tp_getattr*/
00596   0,                              /*tp_setattr*/
00597   0,                              /*tp_compare*/
00598   0,                              /*tp_repr*/
00599   0,                              /*tp_as_number*/
00600   0,                              /*tp_as_sequence*/
00601   0,                              /*tp_as_mapping*/
00602   0,                              /*tp_hash */
00603   0,                              /*tp_call*/
00604   salpy_str,                      /*tp_str*/
00605   0,                              /*tp_getattro*/
00606   0,                              /*tp_setattro*/
00607   0,                              /*tp_as_buffer*/
00608   Py_TPFLAGS_DEFAULT,             /*tp_flags*/
00609   "GDB symtab_and_line object",   /*tp_doc */
00610   0,                              /*tp_traverse */
00611   0,                              /*tp_clear */
00612   0,                              /*tp_richcompare */
00613   0,                              /*tp_weaklistoffset */
00614   0,                              /*tp_iter */
00615   0,                              /*tp_iternext */
00616   sal_object_methods,             /*tp_methods */
00617   0,                              /*tp_members */
00618   sal_object_getset               /*tp_getset */
00619 };
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines