1 /* Python interface to symbol tables.
3 Copyright (C) 2008-2013 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"
28 typedef struct stpy_symtab_object
{
30 /* The GDB Symbol table structure. */
31 struct symtab
*symtab
;
32 /* A symtab object is associated with an objfile, so keep track with
33 a doubly-linked list, rooted in the objfile. This allows
34 invalidation of the underlying struct symtab when the objfile is
36 struct stpy_symtab_object
*prev
;
37 struct stpy_symtab_object
*next
;
40 static PyTypeObject symtab_object_type
41 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("symtab_object");
42 static const struct objfile_data
*stpy_objfile_data_key
;
44 /* Require a valid symbol table. All access to symtab_object->symtab
45 should be gated by this call. */
46 #define STPY_REQUIRE_VALID(symtab_obj, symtab) \
48 symtab = symtab_object_to_symtab (symtab_obj); \
51 PyErr_SetString (PyExc_RuntimeError, \
52 _("Symbol Table is invalid.")); \
57 typedef struct salpy_sal_object
{
59 /* The GDB Symbol table structure. */
60 symtab_object
*symtab
;
61 /* The GDB Symbol table and line structure. */
62 struct symtab_and_line
*sal
;
63 /* A Symtab and line object is associated with an objfile, so keep
64 track with a doubly-linked list, rooted in the objfile. This
65 allows invalidation of the underlying struct symtab_and_line
66 when the objfile is deleted. */
67 struct salpy_sal_object
*prev
;
68 struct salpy_sal_object
*next
;
71 static PyTypeObject sal_object_type
72 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("sal_object");
73 static const struct objfile_data
*salpy_objfile_data_key
;
75 /* Require a valid symbol table and line object. All access to
76 sal_object->sal should be gated by this call. */
77 #define SALPY_REQUIRE_VALID(sal_obj, sal) \
79 sal = sal_object_to_symtab_and_line (sal_obj); \
82 PyErr_SetString (PyExc_RuntimeError, \
83 _("Symbol Table and Line is invalid.")); \
89 stpy_str (PyObject
*self
)
92 struct symtab
*symtab
= NULL
;
94 STPY_REQUIRE_VALID (self
, symtab
);
96 result
= PyString_FromString (symtab_to_filename_for_display (symtab
));
102 stpy_get_filename (PyObject
*self
, void *closure
)
105 struct symtab
*symtab
= NULL
;
106 const char *filename
;
108 STPY_REQUIRE_VALID (self
, symtab
);
109 filename
= symtab_to_filename_for_display (symtab
);
111 str_obj
= PyString_Decode (filename
, strlen (filename
),
112 host_charset (), NULL
);
117 stpy_get_objfile (PyObject
*self
, void *closure
)
119 struct symtab
*symtab
= NULL
;
122 STPY_REQUIRE_VALID (self
, symtab
);
124 result
= objfile_to_objfile_object (symtab
->objfile
);
130 stpy_fullname (PyObject
*self
, PyObject
*args
)
132 const char *fullname
;
133 struct symtab
*symtab
= NULL
;
135 STPY_REQUIRE_VALID (self
, symtab
);
137 fullname
= symtab_to_fullname (symtab
);
139 return PyString_Decode (fullname
, strlen (fullname
), host_charset (), NULL
);
142 /* Implementation of gdb.Symtab.is_valid (self) -> Boolean.
143 Returns True if this Symbol table still exists in GDB. */
146 stpy_is_valid (PyObject
*self
, PyObject
*args
)
148 struct symtab
*symtab
= NULL
;
150 symtab
= symtab_object_to_symtab (self
);
157 /* Return the GLOBAL_BLOCK of the underlying symtab. */
160 stpy_global_block (PyObject
*self
, PyObject
*args
)
162 struct symtab
*symtab
= NULL
;
163 struct block
*block
= NULL
;
164 struct blockvector
*blockvector
;
166 STPY_REQUIRE_VALID (self
, symtab
);
168 blockvector
= BLOCKVECTOR (symtab
);
169 block
= BLOCKVECTOR_BLOCK (blockvector
, GLOBAL_BLOCK
);
170 return block_to_block_object (block
, symtab
->objfile
);
173 /* Return the STATIC_BLOCK of the underlying symtab. */
176 stpy_static_block (PyObject
*self
, PyObject
*args
)
178 struct symtab
*symtab
= NULL
;
179 struct block
*block
= NULL
;
180 struct blockvector
*blockvector
;
182 STPY_REQUIRE_VALID (self
, symtab
);
184 blockvector
= BLOCKVECTOR (symtab
);
185 block
= BLOCKVECTOR_BLOCK (blockvector
, STATIC_BLOCK
);
186 return block_to_block_object (block
, symtab
->objfile
);
190 salpy_str (PyObject
*self
)
193 const char *filename
;
196 struct symtab_and_line
*sal
= NULL
;
198 SALPY_REQUIRE_VALID (self
, sal
);
200 sal_obj
= (sal_object
*) self
;
201 filename
= (sal_obj
->symtab
== (symtab_object
*) Py_None
)
202 ? "<unknown>" : symtab_to_filename_for_display (sal_obj
->symtab
->symtab
);
204 s
= xstrprintf ("symbol and line for %s, line %d", filename
,
207 result
= PyString_FromString (s
);
214 stpy_dealloc (PyObject
*obj
)
216 symtab_object
*symtab
= (symtab_object
*) obj
;
219 symtab
->prev
->next
= symtab
->next
;
220 else if (symtab
->symtab
)
222 set_objfile_data (symtab
->symtab
->objfile
,
223 stpy_objfile_data_key
, symtab
->next
);
226 symtab
->next
->prev
= symtab
->prev
;
227 symtab
->symtab
= NULL
;
232 salpy_get_pc (PyObject
*self
, void *closure
)
234 struct symtab_and_line
*sal
= NULL
;
236 SALPY_REQUIRE_VALID (self
, sal
);
238 return gdb_py_long_from_ulongest (sal
->pc
);
241 /* Implementation of the get method for the 'last' attribute of
242 gdb.Symtab_and_line. */
245 salpy_get_last (PyObject
*self
, void *closure
)
247 struct symtab_and_line
*sal
= NULL
;
249 SALPY_REQUIRE_VALID (self
, sal
);
252 return gdb_py_long_from_ulongest (sal
->end
- 1);
258 salpy_get_line (PyObject
*self
, void *closure
)
260 struct symtab_and_line
*sal
= NULL
;
262 SALPY_REQUIRE_VALID (self
, sal
);
264 return PyInt_FromLong (sal
->line
);
268 salpy_get_symtab (PyObject
*self
, void *closure
)
270 struct symtab_and_line
*sal
;
271 sal_object
*self_sal
= (sal_object
*) self
;
273 SALPY_REQUIRE_VALID (self
, sal
);
275 Py_INCREF (self_sal
->symtab
);
277 return (PyObject
*) self_sal
->symtab
;
280 /* Implementation of gdb.Symtab_and_line.is_valid (self) -> Boolean.
281 Returns True if this Symbol table and line object still exists GDB. */
284 salpy_is_valid (PyObject
*self
, PyObject
*args
)
286 struct symtab_and_line
*sal
;
288 sal
= sal_object_to_symtab_and_line (self
);
296 salpy_dealloc (PyObject
*self
)
298 sal_object
*self_sal
= (sal_object
*) self
;
301 self_sal
->prev
->next
= self_sal
->next
;
302 else if (self_sal
->symtab
!= (symtab_object
* ) Py_None
)
303 set_objfile_data (self_sal
->symtab
->symtab
->objfile
,
304 salpy_objfile_data_key
, self_sal
->next
);
307 self_sal
->next
->prev
= self_sal
->prev
;
309 Py_DECREF (self_sal
->symtab
);
310 xfree (self_sal
->sal
);
311 Py_TYPE (self
)->tp_free (self
);
314 /* Given a sal, and a sal_object that has previously been allocated
315 and initialized, populate the sal_object with the struct sal data.
316 Also, register the sal_object life-cycle with the life-cycle of the
317 object file associated with this sal, if needed. If a failure
318 occurs during the sal population, this function will return -1. */
319 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
320 set_sal (sal_object
*sal_obj
, struct symtab_and_line sal
)
322 symtab_object
*symtab_obj
;
326 symtab_obj
= (symtab_object
*) symtab_to_symtab_object (sal
.symtab
);
327 /* If a symtab existed in the sal, but it cannot be duplicated,
329 if (symtab_obj
== NULL
)
334 symtab_obj
= (symtab_object
*) Py_None
;
338 sal_obj
->sal
= xmemdup (&sal
, sizeof (struct symtab_and_line
),
339 sizeof (struct symtab_and_line
));
340 sal_obj
->symtab
= symtab_obj
;
341 sal_obj
->prev
= NULL
;
343 /* If the SAL does not have a symtab, we do not add it to the
344 objfile cleanup observer linked list. */
345 if (sal_obj
->symtab
!= (symtab_object
*)Py_None
)
347 sal_obj
->next
= objfile_data (sal_obj
->symtab
->symtab
->objfile
,
348 salpy_objfile_data_key
);
350 sal_obj
->next
->prev
= sal_obj
;
352 set_objfile_data (sal_obj
->symtab
->symtab
->objfile
,
353 salpy_objfile_data_key
, sal_obj
);
356 sal_obj
->next
= NULL
;
361 /* Given a symtab, and a symtab_object that has previously been
362 allocated and initialized, populate the symtab_object with the
363 struct symtab data. Also, register the symtab_object life-cycle
364 with the life-cycle of the object file associated with this
365 symtab, if needed. */
367 set_symtab (symtab_object
*obj
, struct symtab
*symtab
)
369 obj
->symtab
= symtab
;
373 obj
->next
= objfile_data (symtab
->objfile
, stpy_objfile_data_key
);
375 obj
->next
->prev
= obj
;
376 set_objfile_data (symtab
->objfile
, stpy_objfile_data_key
, obj
);
382 /* Create a new symbol table (gdb.Symtab) object that encapsulates the
383 symtab structure from GDB. */
385 symtab_to_symtab_object (struct symtab
*symtab
)
387 symtab_object
*symtab_obj
;
389 symtab_obj
= PyObject_New (symtab_object
, &symtab_object_type
);
391 set_symtab (symtab_obj
, symtab
);
393 return (PyObject
*) symtab_obj
;
396 /* Create a new symtab and line (gdb.Symtab_and_line) object
397 that encapsulates the symtab_and_line structure from GDB. */
399 symtab_and_line_to_sal_object (struct symtab_and_line sal
)
404 sal_obj
= PyObject_New (sal_object
, &sal_object_type
);
407 if (set_sal (sal_obj
, sal
) < 0)
414 return (PyObject
*) sal_obj
;
417 /* Return struct symtab_and_line reference that is wrapped by this
419 struct symtab_and_line
*
420 sal_object_to_symtab_and_line (PyObject
*obj
)
422 if (! PyObject_TypeCheck (obj
, &sal_object_type
))
424 return ((sal_object
*) obj
)->sal
;
427 /* Return struct symtab reference that is wrapped by this object. */
429 symtab_object_to_symtab (PyObject
*obj
)
431 if (! PyObject_TypeCheck (obj
, &symtab_object_type
))
433 return ((symtab_object
*) obj
)->symtab
;
436 /* This function is called when an objfile is about to be freed.
437 Invalidate the symbol table as further actions on the symbol table
438 would result in bad data. All access to obj->symtab should be
439 gated by STPY_REQUIRE_VALID which will raise an exception on
440 invalid symbol tables. */
442 del_objfile_symtab (struct objfile
*objfile
, void *datum
)
444 symtab_object
*obj
= datum
;
448 symtab_object
*next
= obj
->next
;
457 /* This function is called when an objfile is about to be freed.
458 Invalidate the sal object as further actions on the sal
459 would result in bad data. All access to obj->sal should be
460 gated by SALPY_REQUIRE_VALID which will raise an exception on
461 invalid symbol table and line objects. */
463 del_objfile_sal (struct objfile
*objfile
, void *datum
)
465 sal_object
*obj
= datum
;
469 sal_object
*next
= obj
->next
;
471 Py_DECREF (obj
->symtab
);
472 obj
->symtab
= (symtab_object
*) Py_None
;
485 gdbpy_initialize_symtabs (void)
487 symtab_object_type
.tp_new
= PyType_GenericNew
;
488 if (PyType_Ready (&symtab_object_type
) < 0)
491 sal_object_type
.tp_new
= PyType_GenericNew
;
492 if (PyType_Ready (&sal_object_type
) < 0)
495 /* Register an objfile "free" callback so we can properly
496 invalidate symbol tables, and symbol table and line data
497 structures when an object file that is about to be
499 stpy_objfile_data_key
500 = register_objfile_data_with_cleanup (NULL
, del_objfile_symtab
);
501 salpy_objfile_data_key
502 = register_objfile_data_with_cleanup (NULL
, del_objfile_sal
);
504 if (gdb_pymodule_addobject (gdb_module
, "Symtab",
505 (PyObject
*) &symtab_object_type
) < 0)
508 return gdb_pymodule_addobject (gdb_module
, "Symtab_and_line",
509 (PyObject
*) &sal_object_type
);
514 static PyGetSetDef symtab_object_getset
[] = {
515 { "filename", stpy_get_filename
, NULL
,
516 "The symbol table's source filename.", NULL
},
517 { "objfile", stpy_get_objfile
, NULL
, "The symtab's objfile.",
519 {NULL
} /* Sentinel */
522 static PyMethodDef symtab_object_methods
[] = {
523 { "is_valid", stpy_is_valid
, METH_NOARGS
,
524 "is_valid () -> Boolean.\n\
525 Return true if this symbol table is valid, false if not." },
526 { "fullname", stpy_fullname
, METH_NOARGS
,
527 "fullname () -> String.\n\
528 Return the symtab's full source filename." },
529 { "global_block", stpy_global_block
, METH_NOARGS
,
530 "global_block () -> gdb.Block.\n\
531 Return the global block of the symbol table." },
532 { "static_block", stpy_static_block
, METH_NOARGS
,
533 "static_block () -> gdb.Block.\n\
534 Return the static block of the symbol table." },
535 {NULL
} /* Sentinel */
538 static PyTypeObject symtab_object_type
= {
539 PyVarObject_HEAD_INIT (NULL
, 0)
540 "gdb.Symtab", /*tp_name*/
541 sizeof (symtab_object
), /*tp_basicsize*/
543 stpy_dealloc
, /*tp_dealloc*/
550 0, /*tp_as_sequence*/
558 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
559 "GDB symtab object", /*tp_doc */
562 0, /*tp_richcompare */
563 0, /*tp_weaklistoffset */
566 symtab_object_methods
, /*tp_methods */
568 symtab_object_getset
/*tp_getset */
571 static PyGetSetDef sal_object_getset
[] = {
572 { "symtab", salpy_get_symtab
, NULL
, "Symtab object.", NULL
},
573 { "pc", salpy_get_pc
, NULL
, "Return the symtab_and_line's pc.", NULL
},
574 { "last", salpy_get_last
, NULL
,
575 "Return the symtab_and_line's last address.", NULL
},
576 { "line", salpy_get_line
, NULL
,
577 "Return the symtab_and_line's line.", NULL
},
578 {NULL
} /* Sentinel */
581 static PyMethodDef sal_object_methods
[] = {
582 { "is_valid", salpy_is_valid
, METH_NOARGS
,
583 "is_valid () -> Boolean.\n\
584 Return true if this symbol table and line is valid, false if not." },
585 {NULL
} /* Sentinel */
588 static PyTypeObject sal_object_type
= {
589 PyVarObject_HEAD_INIT (NULL
, 0)
590 "gdb.Symtab_and_line", /*tp_name*/
591 sizeof (sal_object
), /*tp_basicsize*/
593 salpy_dealloc
, /*tp_dealloc*/
600 0, /*tp_as_sequence*/
604 salpy_str
, /*tp_str*/
608 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
609 "GDB symtab_and_line object", /*tp_doc */
612 0, /*tp_richcompare */
613 0, /*tp_weaklistoffset */
616 sal_object_methods
, /*tp_methods */
618 sal_object_getset
/*tp_getset */