1 /* Python interface to symbol tables.
3 Copyright (C) 2008, 2009, 2010, 2011 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
);
141 /* Implementation of gdb.Symtab.is_valid (self) -> Boolean.
142 Returns True if this Symbol table still exists in GDB. */
145 stpy_is_valid (PyObject
*self
, PyObject
*args
)
147 struct symtab
*symtab
= NULL
;
149 symtab
= symtab_object_to_symtab (self
);
157 salpy_str (PyObject
*self
)
162 struct symtab_and_line
*sal
= NULL
;
164 SALPY_REQUIRE_VALID (self
, sal
);
166 sal_obj
= (sal_object
*) self
;
167 filename
= (sal_obj
->symtab
== (symtab_object
*) Py_None
)
168 ? "<unknown>" : sal_obj
->symtab
->symtab
->filename
;
170 s
= xstrprintf ("symbol and line for %s, line %d", filename
,
173 result
= PyString_FromString (s
);
180 stpy_dealloc (PyObject
*obj
)
182 symtab_object
*symtab
= (symtab_object
*) obj
;
185 symtab
->prev
->next
= symtab
->next
;
186 else if (symtab
->symtab
)
188 set_objfile_data (symtab
->symtab
->objfile
,
189 stpy_objfile_data_key
, symtab
->next
);
192 symtab
->next
->prev
= symtab
->prev
;
193 symtab
->symtab
= NULL
;
198 salpy_get_pc (PyObject
*self
, void *closure
)
200 struct symtab_and_line
*sal
= NULL
;
202 SALPY_REQUIRE_VALID (self
, sal
);
204 return gdb_py_long_from_ulongest (sal
->pc
);
208 salpy_get_line (PyObject
*self
, void *closure
)
210 struct symtab_and_line
*sal
= NULL
;
212 SALPY_REQUIRE_VALID (self
, sal
);
214 return PyInt_FromLong (sal
->line
);
218 salpy_get_symtab (PyObject
*self
, void *closure
)
220 struct symtab_and_line
*sal
;
221 sal_object
*self_sal
= (sal_object
*) self
;
223 SALPY_REQUIRE_VALID (self
, sal
);
225 Py_INCREF (self_sal
->symtab
);
227 return (PyObject
*) self_sal
->symtab
;
230 /* Implementation of gdb.Symtab_and_line.is_valid (self) -> Boolean.
231 Returns True if this Symbol table and line object still exists GDB. */
234 salpy_is_valid (PyObject
*self
, PyObject
*args
)
236 struct symtab_and_line
*sal
;
238 sal
= sal_object_to_symtab_and_line (self
);
246 salpy_dealloc (PyObject
*self
)
248 sal_object
*self_sal
= (sal_object
*) self
;
251 self_sal
->prev
->next
= self_sal
->next
;
252 else if (self_sal
->symtab
!= (symtab_object
* ) Py_None
)
253 set_objfile_data (self_sal
->symtab
->symtab
->objfile
,
254 salpy_objfile_data_key
, self_sal
->next
);
257 self_sal
->next
->prev
= self_sal
->prev
;
259 Py_DECREF (self_sal
->symtab
);
260 xfree (self_sal
->sal
);
261 self_sal
->ob_type
->tp_free (self
);
264 /* Given a sal, and a sal_object that has previously been allocated
265 and initialized, populate the sal_object with the struct sal data.
266 Also, register the sal_object life-cycle with the life-cycle of the
267 object file associated with this sal, if needed. If a failure
268 occurs during the sal population, this function will return
271 set_sal (sal_object
*sal_obj
, struct symtab_and_line sal
)
273 symtab_object
*symtab_obj
;
277 symtab_obj
= (symtab_object
*) symtab_to_symtab_object (sal
.symtab
);
278 /* If a symtab existed in the sal, but it cannot be duplicated,
280 if (symtab_obj
== NULL
)
285 symtab_obj
= (symtab_object
*) Py_None
;
289 sal_obj
->sal
= xmemdup (&sal
, sizeof (struct symtab_and_line
),
290 sizeof (struct symtab_and_line
));
291 sal_obj
->symtab
= symtab_obj
;
292 sal_obj
->prev
= NULL
;
294 /* If the SAL does not have a symtab, we do not add it to the
295 objfile cleanup observer linked list. */
296 if (sal_obj
->symtab
!= (symtab_object
*)Py_None
)
298 sal_obj
->next
= objfile_data (sal_obj
->symtab
->symtab
->objfile
,
299 salpy_objfile_data_key
);
301 sal_obj
->next
->prev
= sal_obj
;
303 set_objfile_data (sal_obj
->symtab
->symtab
->objfile
,
304 salpy_objfile_data_key
, sal_obj
);
307 sal_obj
->next
= NULL
;
312 /* Given a symtab, and a symtab_object that has previously been
313 allocated and initialized, populate the symtab_object with the
314 struct symtab data. Also, register the symtab_object life-cycle
315 with the life-cycle of the object file associated with this
316 symtab, if needed. */
318 set_symtab (symtab_object
*obj
, struct symtab
*symtab
)
320 obj
->symtab
= symtab
;
324 obj
->next
= objfile_data (symtab
->objfile
, stpy_objfile_data_key
);
326 obj
->next
->prev
= obj
;
327 set_objfile_data (symtab
->objfile
, stpy_objfile_data_key
, obj
);
333 /* Create a new symbol table (gdb.Symtab) object that encapsulates the
334 symtab structure from GDB. */
336 symtab_to_symtab_object (struct symtab
*symtab
)
338 symtab_object
*symtab_obj
;
340 symtab_obj
= PyObject_New (symtab_object
, &symtab_object_type
);
342 set_symtab (symtab_obj
, symtab
);
344 return (PyObject
*) symtab_obj
;
347 /* Create a new symtab and line (gdb.Symtab_and_line) object
348 that encapsulates the symtab_and_line structure from GDB. */
350 symtab_and_line_to_sal_object (struct symtab_and_line sal
)
356 sal_obj
= PyObject_New (sal_object
, &sal_object_type
);
359 success
= set_sal (sal_obj
, sal
);
367 return (PyObject
*) sal_obj
;
370 /* Return struct symtab_and_line reference that is wrapped by this
372 struct symtab_and_line
*
373 sal_object_to_symtab_and_line (PyObject
*obj
)
375 if (! PyObject_TypeCheck (obj
, &sal_object_type
))
377 return ((sal_object
*) obj
)->sal
;
380 /* Return struct symtab reference that is wrapped by this object. */
382 symtab_object_to_symtab (PyObject
*obj
)
384 if (! PyObject_TypeCheck (obj
, &symtab_object_type
))
386 return ((symtab_object
*) obj
)->symtab
;
389 /* This function is called when an objfile is about to be freed.
390 Invalidate the symbol table as further actions on the symbol table
391 would result in bad data. All access to obj->symtab should be
392 gated by STPY_REQUIRE_VALID which will raise an exception on
393 invalid symbol tables. */
395 del_objfile_symtab (struct objfile
*objfile
, void *datum
)
397 symtab_object
*obj
= datum
;
401 symtab_object
*next
= obj
->next
;
410 /* This function is called when an objfile is about to be freed.
411 Invalidate the sal object as further actions on the sal
412 would result in bad data. All access to obj->sal should be
413 gated by SALPY_REQUIRE_VALID which will raise an exception on
414 invalid symbol table and line objects. */
416 del_objfile_sal (struct objfile
*objfile
, void *datum
)
418 sal_object
*obj
= datum
;
422 sal_object
*next
= obj
->next
;
435 gdbpy_initialize_symtabs (void)
437 symtab_object_type
.tp_new
= PyType_GenericNew
;
438 if (PyType_Ready (&symtab_object_type
) < 0)
441 sal_object_type
.tp_new
= PyType_GenericNew
;
442 if (PyType_Ready (&sal_object_type
) < 0)
445 /* Register an objfile "free" callback so we can properly
446 invalidate symbol tables, and symbol table and line data
447 structures when an object file that is about to be
449 stpy_objfile_data_key
450 = register_objfile_data_with_cleanup (NULL
, del_objfile_symtab
);
451 salpy_objfile_data_key
452 = register_objfile_data_with_cleanup (NULL
, del_objfile_sal
);
454 Py_INCREF (&symtab_object_type
);
455 PyModule_AddObject (gdb_module
, "Symtab",
456 (PyObject
*) &symtab_object_type
);
458 Py_INCREF (&sal_object_type
);
459 PyModule_AddObject (gdb_module
, "Symtab_and_line",
460 (PyObject
*) &sal_object_type
);
465 static PyGetSetDef symtab_object_getset
[] = {
466 { "filename", stpy_get_filename
, NULL
,
467 "The symbol table's source filename.", NULL
},
468 { "objfile", stpy_get_objfile
, NULL
, "The symtab's objfile.",
470 {NULL
} /* Sentinel */
473 static PyMethodDef symtab_object_methods
[] = {
474 { "is_valid", stpy_is_valid
, METH_NOARGS
,
475 "is_valid () -> Boolean.\n\
476 Return true if this symbol table is valid, false if not." },
477 { "fullname", stpy_fullname
, METH_NOARGS
,
478 "fullname () -> String.\n\
479 Return the symtab's full source filename." },
480 {NULL
} /* Sentinel */
483 static PyTypeObject symtab_object_type
= {
484 PyObject_HEAD_INIT (NULL
)
486 "gdb.Symtab", /*tp_name*/
487 sizeof (symtab_object
), /*tp_basicsize*/
489 stpy_dealloc
, /*tp_dealloc*/
496 0, /*tp_as_sequence*/
504 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
505 "GDB symtab object", /*tp_doc */
508 0, /*tp_richcompare */
509 0, /*tp_weaklistoffset */
512 symtab_object_methods
, /*tp_methods */
514 symtab_object_getset
/*tp_getset */
517 static PyGetSetDef sal_object_getset
[] = {
518 { "symtab", salpy_get_symtab
, NULL
, "Symtab object.", NULL
},
519 { "pc", salpy_get_pc
, NULL
, "Return the symtab_and_line's pc.", NULL
},
520 { "line", salpy_get_line
, NULL
,
521 "Return the symtab_and_line's line.", NULL
},
522 {NULL
} /* Sentinel */
525 static PyMethodDef sal_object_methods
[] = {
526 { "is_valid", salpy_is_valid
, METH_NOARGS
,
527 "is_valid () -> Boolean.\n\
528 Return true if this symbol table and line is valid, false if not." },
529 {NULL
} /* Sentinel */
532 static PyTypeObject sal_object_type
= {
533 PyObject_HEAD_INIT (NULL
)
535 "gdb.Symtab_and_line", /*tp_name*/
536 sizeof (sal_object
), /*tp_basicsize*/
538 salpy_dealloc
, /*tp_dealloc*/
545 0, /*tp_as_sequence*/
549 salpy_str
, /*tp_str*/
553 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
554 "GDB symtab_and_line object", /*tp_doc */
557 0, /*tp_richcompare */
558 0, /*tp_weaklistoffset */
561 sal_object_methods
, /*tp_methods */
563 sal_object_getset
/*tp_getset */