]> Git Repo - binutils.git/blob - gdb/python/py-symtab.c
f91f32201dd122ca40a5ea542c534c74ea3a4007
[binutils.git] / gdb / python / py-symtab.c
1 /* Python interface to symbol tables.
2
3    Copyright (C) 2008, 2009, 2010 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
27 typedef struct stpy_symtab_object {
28   PyObject_HEAD
29   /* The GDB Symbol table structure.  */
30   struct symtab *symtab;
31   /* A symtab object is associated with an objfile, so keep track with
32      a doubly-linked list, rooted in the objfile.  This allows
33      invalidation of the underlying struct symtab when the objfile is
34      deleted.  */
35   struct stpy_symtab_object *prev;
36   struct stpy_symtab_object *next;
37 } symtab_object;
38
39 static PyTypeObject symtab_object_type;
40 static const struct objfile_data *stpy_objfile_data_key;
41
42 /* Require a valid symbol table.  All access to symtab_object->symtab
43    should be gated by this call.  */
44 #define STPY_REQUIRE_VALID(symtab_obj, symtab)           \
45   do {                                                   \
46     symtab = symtab_object_to_symtab (symtab_obj);       \
47     if (symtab == NULL)                                  \
48       {                                                  \
49         PyErr_SetString (PyExc_RuntimeError,             \
50                          _("Symbol Table is invalid.")); \
51         return NULL;                                     \
52       }                                                  \
53   } while (0)
54
55 typedef struct salpy_sal_object {
56   PyObject_HEAD
57   /* The GDB Symbol table structure.  */
58   symtab_object *symtab;
59   /* The GDB Symbol table and line structure.  */
60   struct symtab_and_line *sal;
61   /* A Symtab and line object is associated with an objfile, so keep
62      track with a doubly-linked list, rooted in the objfile.  This
63      allows invalidation of the underlying struct symtab_and_line
64      when the objfile is deleted.  */
65   struct salpy_sal_object *prev;
66   struct salpy_sal_object *next;
67 } sal_object;
68
69 static PyTypeObject sal_object_type;
70 static const struct objfile_data *salpy_objfile_data_key;
71
72 /* Require a valid symbol table and line object.  All access to
73    sal_object->sal should be gated by this call.  */
74 #define SALPY_REQUIRE_VALID(sal_obj, sal)                               \
75   do {                                                                  \
76     sal = sal_object_to_symtab_and_line (sal_obj);                      \
77     if (sal == NULL)                                                    \
78       {                                                                 \
79           PyErr_SetString (PyExc_RuntimeError,                          \
80                            _("Symbol Table and Line is invalid."));     \
81           return NULL;                                                  \
82         }                                                               \
83   } while (0)
84
85 static PyObject *
86 stpy_str (PyObject *self)
87 {
88   PyObject *result;
89   struct symtab *symtab = NULL;
90
91   STPY_REQUIRE_VALID (self, symtab);
92
93   result = PyString_FromString (symtab->filename);
94
95   return result;
96 }
97
98 static PyObject *
99 stpy_get_filename (PyObject *self, void *closure)
100 {
101   PyObject *str_obj;
102   struct symtab *symtab = NULL;
103
104   STPY_REQUIRE_VALID (self, symtab);
105
106   str_obj = PyString_Decode (symtab->filename,
107                              strlen (symtab->filename),
108                              host_charset (), NULL);
109   return str_obj;
110 }
111
112 static PyObject *
113 stpy_get_objfile (PyObject *self, void *closure)
114 {
115   struct symtab *symtab = NULL;
116   PyObject *result;
117
118   STPY_REQUIRE_VALID (self, symtab);
119
120   result = objfile_to_objfile_object (symtab->objfile);
121   Py_XINCREF (result);
122   return result;
123 }
124
125 static PyObject *
126 stpy_fullname (PyObject *self, PyObject *args)
127 {
128   char *fullname;
129   struct symtab *symtab = NULL;
130
131   STPY_REQUIRE_VALID (self, symtab);
132
133   fullname = symtab_to_fullname (symtab);
134   if (fullname)
135     return PyString_Decode (fullname, strlen (fullname),
136                             host_charset (), NULL);
137
138   Py_RETURN_NONE;
139 }
140
141 static PyObject *
142 salpy_str (PyObject *self)
143 {
144   char *s, *filename;
145   sal_object *sal_obj;
146   PyObject *result;
147   struct symtab_and_line *sal = NULL;
148
149   SALPY_REQUIRE_VALID (self, sal);
150
151   sal_obj = (sal_object *) self;
152   filename = (sal_obj->symtab == (symtab_object *) Py_None)
153     ? "<unknown>" : sal_obj->symtab->symtab->filename;
154
155   s = xstrprintf ("symbol and line for %s, line %d", filename,
156                   sal->line);
157
158   result = PyString_FromString (s);
159   xfree (s);
160
161   return result;
162 }
163
164 static void
165 stpy_dealloc (PyObject *obj)
166 {
167   symtab_object *symtab = (symtab_object *) obj;
168
169   if (symtab->prev)
170     symtab->prev->next = symtab->next;
171   else if (symtab->symtab)
172     {
173       set_objfile_data (symtab->symtab->objfile,
174                         stpy_objfile_data_key, symtab->next);
175     }
176   if (symtab->next)
177     symtab->next->prev = symtab->prev;
178   symtab->symtab = NULL;
179 }
180
181
182 static PyObject *
183 salpy_get_pc (PyObject *self, void *closure)
184 {
185   struct symtab_and_line *sal = NULL;
186
187   SALPY_REQUIRE_VALID (self, sal);
188
189   return PyLong_FromUnsignedLongLong (sal->pc);
190 }
191
192 static PyObject *
193 salpy_get_line (PyObject *self, void *closure)
194 {
195   struct symtab_and_line *sal = NULL;
196
197   SALPY_REQUIRE_VALID (self, sal);
198
199   return PyLong_FromUnsignedLongLong (sal->line);
200 }
201
202 static PyObject *
203 salpy_get_symtab (PyObject *self, void *closure)
204 {
205   struct symtab_and_line *sal;
206   sal_object *self_sal = (sal_object *) self;
207
208   SALPY_REQUIRE_VALID (self, sal);
209
210   Py_INCREF (self_sal->symtab);
211
212   return (PyObject *) self_sal->symtab;
213 }
214
215 static void
216 salpy_dealloc (PyObject *self)
217 {
218   sal_object *self_sal = (sal_object *) self;
219
220   if (self_sal->prev)
221     self_sal->prev->next = self_sal->next;
222   else if (self_sal->symtab != (symtab_object * ) Py_None)
223     set_objfile_data (self_sal->symtab->symtab->objfile,
224                       salpy_objfile_data_key, self_sal->next);
225
226   if (self_sal->next)
227     self_sal->next->prev = self_sal->prev;
228
229   Py_DECREF (self_sal->symtab);
230   xfree (self_sal->sal);
231   self_sal->ob_type->tp_free (self);
232 }
233
234 /* Given a sal, and a sal_object that has previously been
235    allocated and initialized, populate the sal_object with the
236    struct sal data.  Also, register the sal_object life-cycle with the
237    life-cycle of the the object file associated with this sal, if
238    needed.  If a failure occurs during the sal population,  this
239    function will return NULL.  */
240 static int
241 set_sal (sal_object *sal_obj, struct symtab_and_line sal)
242 {
243   symtab_object *symtab_obj;
244
245   if (sal.symtab)
246     {
247       symtab_obj = (symtab_object *) symtab_to_symtab_object  (sal.symtab);
248       /* If a symtab existed in the sal, but it cannot be duplicated,
249          we exit.  */
250       if (symtab_obj == NULL)
251         return 0;
252     }
253   else
254     {
255       symtab_obj = (symtab_object *) Py_None;
256       Py_INCREF (Py_None);
257     }
258
259   sal_obj->sal = xmemdup (&sal, sizeof (struct symtab_and_line),
260                           sizeof (struct symtab_and_line));
261   sal_obj->symtab = symtab_obj;
262   sal_obj->prev = NULL;
263
264   /* If the SAL does not have a symtab, we do not add it to the
265      objfile cleanup observer linked list.  */
266   if (sal_obj->symtab != (symtab_object *)Py_None)
267     {
268       sal_obj->next = objfile_data (sal_obj->symtab->symtab->objfile,
269                                     salpy_objfile_data_key);
270       if (sal_obj->next)
271         sal_obj->next->prev = sal_obj;
272
273       set_objfile_data (sal_obj->symtab->symtab->objfile,
274                         salpy_objfile_data_key, sal_obj);
275     }
276   else
277     sal_obj->next = NULL;
278
279   return 1;
280 }
281
282 /* Given a symtab, and a symtab_object that has previously been
283    allocated and initialized, populate the symtab_object with the
284    struct symtab data.  Also, register the symtab_object life-cycle
285    with the life-cycle of the the object file associated with this
286    symtab, if needed.  */
287 static void
288 set_symtab (symtab_object *obj, struct symtab *symtab)
289 {
290   obj->symtab = symtab;
291   obj->prev = NULL;
292   if (symtab)
293     {
294       obj->next = objfile_data (symtab->objfile, stpy_objfile_data_key);
295       if (obj->next)
296         obj->next->prev = obj;
297       set_objfile_data (symtab->objfile, stpy_objfile_data_key, obj);
298     }
299   else
300     obj->next = NULL;
301 }
302
303 /* Create a new symbol table (gdb.Symtab) object that encapsulates the
304    symtab structure from GDB.  */
305 PyObject *
306 symtab_to_symtab_object (struct symtab *symtab)
307 {
308   symtab_object *symtab_obj;
309
310   symtab_obj = PyObject_New (symtab_object, &symtab_object_type);
311   if (symtab_obj)
312     set_symtab (symtab_obj, symtab);
313
314   return (PyObject *) symtab_obj;
315 }
316
317 /* Create a new symtab and line (gdb.Symtab_and_line) object
318    that encapsulates the symtab_and_line structure from GDB.  */
319 PyObject *
320 symtab_and_line_to_sal_object (struct symtab_and_line sal)
321
322 {
323   sal_object *sal_obj;
324   symtab_object *symtab_obj;
325   int success = 0;
326   sal_obj = PyObject_New (sal_object, &sal_object_type);
327
328   if (sal_obj)
329     {
330       success = set_sal (sal_obj, sal);
331       if (!success)
332         {
333           Py_DECREF (sal_obj);
334           return NULL;
335         }
336     }
337
338   return (PyObject *) sal_obj;
339 }
340
341 /* Return struct symtab_and_line reference that is wrapped by this
342    object.  */
343 struct symtab_and_line *
344 sal_object_to_symtab_and_line (PyObject *obj)
345 {
346   if (! PyObject_TypeCheck (obj, &sal_object_type))
347     return NULL;
348   return ((sal_object *) obj)->sal;
349 }
350
351 /* Return struct symtab reference that is wrapped by this object.  */
352 struct symtab *
353 symtab_object_to_symtab (PyObject *obj)
354 {
355   if (! PyObject_TypeCheck (obj, &symtab_object_type))
356     return NULL;
357   return ((symtab_object *) obj)->symtab;
358 }
359
360 /* This function is called when an objfile is about to be freed.
361    Invalidate the symbol table as further actions on the symbol table
362    would result in bad data.  All access to obj->symtab should be
363    gated by STPY_REQUIRE_VALID which will raise an exception on
364    invalid symbol tables.  */
365 static void
366 del_objfile_symtab (struct objfile *objfile, void *datum)
367 {
368   symtab_object *obj = datum;
369   while (obj)
370     {
371       symtab_object *next = obj->next;
372
373       obj->symtab = NULL;
374       obj->next = NULL;
375       obj->prev = NULL;
376       obj = next;
377     }
378 }
379
380 /* This function is called when an objfile is about to be freed.
381    Invalidate the sal object as further actions on the sal
382    would result in bad data.  All access to obj->sal should be
383    gated by SALPY_REQUIRE_VALID which will raise an exception on
384    invalid symbol table and line objects.  */
385 static void
386 del_objfile_sal (struct objfile *objfile, void *datum)
387 {
388   sal_object *obj = datum;
389   while (obj)
390     {
391       sal_object *next = obj->next;
392
393       obj->symtab = NULL;
394       obj->next = NULL;
395       obj->prev = NULL;
396       xfree (obj->sal);
397       obj->sal = NULL;
398
399       obj = next;
400     }
401 }
402
403 void
404 gdbpy_initialize_symtabs (void)
405 {
406   symtab_object_type.tp_new = PyType_GenericNew;
407   if (PyType_Ready (&symtab_object_type) < 0)
408     return;
409
410   sal_object_type.tp_new = PyType_GenericNew;
411   if (PyType_Ready (&sal_object_type) < 0)
412     return;
413
414   /* Register an objfile "free" callback so we can properly
415      invalidate symbol tables, and symbol table and line data
416      structures when an object file that is about to be
417      deleted.  */
418   stpy_objfile_data_key
419     = register_objfile_data_with_cleanup (NULL, del_objfile_symtab);
420   salpy_objfile_data_key
421     = register_objfile_data_with_cleanup (NULL, del_objfile_sal);
422
423   Py_INCREF (&symtab_object_type);
424   PyModule_AddObject (gdb_module, "Symtab",
425                       (PyObject *) &symtab_object_type);
426
427   Py_INCREF (&sal_object_type);
428   PyModule_AddObject (gdb_module, "Symtab_and_line",
429                       (PyObject *) &sal_object_type);
430 }
431
432 \f
433
434 static PyGetSetDef symtab_object_getset[] = {
435   { "filename", stpy_get_filename, NULL,
436     "The symbol table's source filename.", NULL },
437   { "objfile", stpy_get_objfile, NULL, "The symtab's objfile.",
438     NULL },
439   {NULL}  /* Sentinel */
440 };
441
442 static PyMethodDef symtab_object_methods[] = {
443   { "fullname", stpy_fullname, METH_NOARGS,
444     "fullname () -> String.\n\
445 Return the symtab's full source filename." },
446   {NULL}  /* Sentinel */
447 };
448
449 static PyTypeObject symtab_object_type = {
450   PyObject_HEAD_INIT (NULL)
451   0,                              /*ob_size*/
452   "gdb.Symtab",                   /*tp_name*/
453   sizeof (symtab_object),         /*tp_basicsize*/
454   0,                              /*tp_itemsize*/
455   stpy_dealloc,                   /*tp_dealloc*/
456   0,                              /*tp_print*/
457   0,                              /*tp_getattr*/
458   0,                              /*tp_setattr*/
459   0,                              /*tp_compare*/
460   0,                              /*tp_repr*/
461   0,                              /*tp_as_number*/
462   0,                              /*tp_as_sequence*/
463   0,                              /*tp_as_mapping*/
464   0,                              /*tp_hash */
465   0,                              /*tp_call*/
466   stpy_str,                       /*tp_str*/
467   0,                              /*tp_getattro*/
468   0,                              /*tp_setattro*/
469   0,                              /*tp_as_buffer*/
470   Py_TPFLAGS_DEFAULT,             /*tp_flags*/
471   "GDB symtab object",            /*tp_doc */
472   0,                              /*tp_traverse */
473   0,                              /*tp_clear */
474   0,                              /*tp_richcompare */
475   0,                              /*tp_weaklistoffset */
476   0,                              /*tp_iter */
477   0,                              /*tp_iternext */
478   symtab_object_methods,          /*tp_methods */
479   0,                              /*tp_members */
480   symtab_object_getset            /*tp_getset */
481 };
482
483 static PyGetSetDef sal_object_getset[] = {
484   { "symtab", salpy_get_symtab, NULL, "Symtab object.", NULL },
485   { "pc", salpy_get_pc, NULL, "Return the symtab_and_line's pc.", NULL },
486   { "line", salpy_get_line, NULL,
487     "Return the symtab_and_line's line.", NULL },
488   {NULL}  /* Sentinel */
489 };
490
491 static PyTypeObject sal_object_type = {
492   PyObject_HEAD_INIT (NULL)
493   0,                              /*ob_size*/
494   "gdb.Symtab_and_line",          /*tp_name*/
495   sizeof (sal_object),            /*tp_basicsize*/
496   0,                              /*tp_itemsize*/
497   salpy_dealloc,                  /*tp_dealloc*/
498   0,                              /*tp_print*/
499   0,                              /*tp_getattr*/
500   0,                              /*tp_setattr*/
501   0,                              /*tp_compare*/
502   0,                              /*tp_repr*/
503   0,                              /*tp_as_number*/
504   0,                              /*tp_as_sequence*/
505   0,                              /*tp_as_mapping*/
506   0,                              /*tp_hash */
507   0,                              /*tp_call*/
508   salpy_str,                      /*tp_str*/
509   0,                              /*tp_getattro*/
510   0,                              /*tp_setattro*/
511   0,                              /*tp_as_buffer*/
512   Py_TPFLAGS_DEFAULT,             /*tp_flags*/
513   "GDB symtab_and_line object",   /*tp_doc */
514   0,                              /*tp_traverse */
515   0,                              /*tp_clear */
516   0,                              /*tp_richcompare */
517   0,                              /*tp_weaklistoffset */
518   0,                              /*tp_iter */
519   0,                              /*tp_iternext */
520   0,                              /*tp_methods */
521   0,                              /*tp_members */
522   sal_object_getset               /*tp_getset */
523 };
This page took 0.044383 seconds and 2 git commands to generate.