1 /* Python interface to symbol tables.
3 Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
24 #include "python-internal.h"
27 typedef struct stpy_symtab_object
{
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
35 struct stpy_symtab_object
*prev
;
36 struct stpy_symtab_object
*next
;
39 static PyTypeObject symtab_object_type
;
40 static const struct objfile_data
*stpy_objfile_data_key
;
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) \
46 symtab = symtab_object_to_symtab (symtab_obj); \
49 PyErr_SetString (PyExc_RuntimeError, \
50 _("Symbol Table is invalid.")); \
55 typedef struct salpy_sal_object
{
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
;
69 static PyTypeObject sal_object_type
;
70 static const struct objfile_data
*salpy_objfile_data_key
;
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) \
76 sal = sal_object_to_symtab_and_line (sal_obj); \
79 PyErr_SetString (PyExc_RuntimeError, \
80 _("Symbol Table and Line is invalid.")); \
86 stpy_str (PyObject
*self
)
89 struct symtab
*symtab
= NULL
;
91 STPY_REQUIRE_VALID (self
, symtab
);
93 result
= PyString_FromString (symtab
->filename
);
99 stpy_get_filename (PyObject
*self
, void *closure
)
102 struct symtab
*symtab
= NULL
;
104 STPY_REQUIRE_VALID (self
, symtab
);
106 str_obj
= PyString_Decode (symtab
->filename
,
107 strlen (symtab
->filename
),
108 host_charset (), NULL
);
113 stpy_get_objfile (PyObject
*self
, void *closure
)
115 struct symtab
*symtab
= NULL
;
118 STPY_REQUIRE_VALID (self
, symtab
);
120 result
= objfile_to_objfile_object (symtab
->objfile
);
126 stpy_fullname (PyObject
*self
, PyObject
*args
)
129 struct symtab
*symtab
= NULL
;
131 STPY_REQUIRE_VALID (self
, symtab
);
133 fullname
= symtab_to_fullname (symtab
);
135 return PyString_Decode (fullname
, strlen (fullname
),
136 host_charset (), NULL
);
142 salpy_str (PyObject
*self
)
147 struct symtab_and_line
*sal
= NULL
;
149 SALPY_REQUIRE_VALID (self
, sal
);
151 sal_obj
= (sal_object
*) self
;
152 filename
= (sal_obj
->symtab
== (symtab_object
*) Py_None
)
153 ? "<unknown>" : sal_obj
->symtab
->symtab
->filename
;
155 s
= xstrprintf ("symbol and line for %s, line %d", filename
,
158 result
= PyString_FromString (s
);
165 stpy_dealloc (PyObject
*obj
)
167 symtab_object
*symtab
= (symtab_object
*) obj
;
170 symtab
->prev
->next
= symtab
->next
;
171 else if (symtab
->symtab
)
173 set_objfile_data (symtab
->symtab
->objfile
,
174 stpy_objfile_data_key
, symtab
->next
);
177 symtab
->next
->prev
= symtab
->prev
;
178 symtab
->symtab
= NULL
;
183 salpy_get_pc (PyObject
*self
, void *closure
)
185 struct symtab_and_line
*sal
= NULL
;
187 SALPY_REQUIRE_VALID (self
, sal
);
189 return PyLong_FromUnsignedLongLong (sal
->pc
);
193 salpy_get_line (PyObject
*self
, void *closure
)
195 struct symtab_and_line
*sal
= NULL
;
197 SALPY_REQUIRE_VALID (self
, sal
);
199 return PyLong_FromUnsignedLongLong (sal
->line
);
203 salpy_get_symtab (PyObject
*self
, void *closure
)
205 struct symtab_and_line
*sal
;
206 sal_object
*self_sal
= (sal_object
*) self
;
208 SALPY_REQUIRE_VALID (self
, sal
);
210 Py_INCREF (self_sal
->symtab
);
212 return (PyObject
*) self_sal
->symtab
;
216 salpy_dealloc (PyObject
*self
)
218 sal_object
*self_sal
= (sal_object
*) self
;
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
);
227 self_sal
->next
->prev
= self_sal
->prev
;
229 Py_DECREF (self_sal
->symtab
);
230 xfree (self_sal
->sal
);
231 self_sal
->ob_type
->tp_free (self
);
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. */
241 set_sal (sal_object
*sal_obj
, struct symtab_and_line sal
)
243 symtab_object
*symtab_obj
;
247 symtab_obj
= (symtab_object
*) symtab_to_symtab_object (sal
.symtab
);
248 /* If a symtab existed in the sal, but it cannot be duplicated,
250 if (symtab_obj
== NULL
)
255 symtab_obj
= (symtab_object
*) Py_None
;
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
;
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
)
268 sal_obj
->next
= objfile_data (sal_obj
->symtab
->symtab
->objfile
,
269 salpy_objfile_data_key
);
271 sal_obj
->next
->prev
= sal_obj
;
273 set_objfile_data (sal_obj
->symtab
->symtab
->objfile
,
274 salpy_objfile_data_key
, sal_obj
);
277 sal_obj
->next
= NULL
;
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. */
288 set_symtab (symtab_object
*obj
, struct symtab
*symtab
)
290 obj
->symtab
= symtab
;
294 obj
->next
= objfile_data (symtab
->objfile
, stpy_objfile_data_key
);
296 obj
->next
->prev
= obj
;
297 set_objfile_data (symtab
->objfile
, stpy_objfile_data_key
, obj
);
303 /* Create a new symbol table (gdb.Symtab) object that encapsulates the
304 symtab structure from GDB. */
306 symtab_to_symtab_object (struct symtab
*symtab
)
308 symtab_object
*symtab_obj
;
310 symtab_obj
= PyObject_New (symtab_object
, &symtab_object_type
);
312 set_symtab (symtab_obj
, symtab
);
314 return (PyObject
*) symtab_obj
;
317 /* Create a new symtab and line (gdb.Symtab_and_line) object
318 that encapsulates the symtab_and_line structure from GDB. */
320 symtab_and_line_to_sal_object (struct symtab_and_line sal
)
324 symtab_object
*symtab_obj
;
326 sal_obj
= PyObject_New (sal_object
, &sal_object_type
);
330 success
= set_sal (sal_obj
, sal
);
338 return (PyObject
*) sal_obj
;
341 /* Return struct symtab_and_line reference that is wrapped by this
343 struct symtab_and_line
*
344 sal_object_to_symtab_and_line (PyObject
*obj
)
346 if (! PyObject_TypeCheck (obj
, &sal_object_type
))
348 return ((sal_object
*) obj
)->sal
;
351 /* Return struct symtab reference that is wrapped by this object. */
353 symtab_object_to_symtab (PyObject
*obj
)
355 if (! PyObject_TypeCheck (obj
, &symtab_object_type
))
357 return ((symtab_object
*) obj
)->symtab
;
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. */
366 del_objfile_symtab (struct objfile
*objfile
, void *datum
)
368 symtab_object
*obj
= datum
;
371 symtab_object
*next
= obj
->next
;
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. */
386 del_objfile_sal (struct objfile
*objfile
, void *datum
)
388 sal_object
*obj
= datum
;
391 sal_object
*next
= obj
->next
;
404 gdbpy_initialize_symtabs (void)
406 symtab_object_type
.tp_new
= PyType_GenericNew
;
407 if (PyType_Ready (&symtab_object_type
) < 0)
410 sal_object_type
.tp_new
= PyType_GenericNew
;
411 if (PyType_Ready (&sal_object_type
) < 0)
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
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
);
423 Py_INCREF (&symtab_object_type
);
424 PyModule_AddObject (gdb_module
, "Symtab",
425 (PyObject
*) &symtab_object_type
);
427 Py_INCREF (&sal_object_type
);
428 PyModule_AddObject (gdb_module
, "Symtab_and_line",
429 (PyObject
*) &sal_object_type
);
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.",
439 {NULL
} /* Sentinel */
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 */
449 static PyTypeObject symtab_object_type
= {
450 PyObject_HEAD_INIT (NULL
)
452 "gdb.Symtab", /*tp_name*/
453 sizeof (symtab_object
), /*tp_basicsize*/
455 stpy_dealloc
, /*tp_dealloc*/
462 0, /*tp_as_sequence*/
470 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
471 "GDB symtab object", /*tp_doc */
474 0, /*tp_richcompare */
475 0, /*tp_weaklistoffset */
478 symtab_object_methods
, /*tp_methods */
480 symtab_object_getset
/*tp_getset */
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 */
491 static PyTypeObject sal_object_type
= {
492 PyObject_HEAD_INIT (NULL
)
494 "gdb.Symtab_and_line", /*tp_name*/
495 sizeof (sal_object
), /*tp_basicsize*/
497 salpy_dealloc
, /*tp_dealloc*/
504 0, /*tp_as_sequence*/
508 salpy_str
, /*tp_str*/
512 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
513 "GDB symtab_and_line object", /*tp_doc */
516 0, /*tp_richcompare */
517 0, /*tp_weaklistoffset */
522 sal_object_getset
/*tp_getset */