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