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