d3e95dbf6fdc49fa4ba15e6fb9230675f2725a04
[deliverable/binutils-gdb.git] / gdb / python / py-symtab.c
1 /* Python interface to symbol tables.
2
3 Copyright (C) 2008-2012 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->filename);
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
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
113 static PyObject *
114 stpy_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
126 static PyObject *
127 stpy_fullname (PyObject *self, PyObject *args)
128 {
129 const 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
142 /* Implementation of gdb.Symtab.is_valid (self) -> Boolean.
143 Returns True if this Symbol table still exists in GDB. */
144
145 static PyObject *
146 stpy_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
157 /* Return the GLOBAL_BLOCK of the underlying symtab. */
158
159 static PyObject *
160 stpy_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
175 static PyObject *
176 stpy_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
189 static PyObject *
190 salpy_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
212 static void
213 stpy_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
230 static PyObject *
231 salpy_get_pc (PyObject *self, void *closure)
232 {
233 struct symtab_and_line *sal = NULL;
234
235 SALPY_REQUIRE_VALID (self, sal);
236
237 return gdb_py_long_from_ulongest (sal->pc);
238 }
239
240 /* Implementation of the get method for the 'last' attribute of
241 gdb.Symtab_and_line. */
242
243 static PyObject *
244 salpy_get_last (PyObject *self, void *closure)
245 {
246 struct symtab_and_line *sal = NULL;
247
248 SALPY_REQUIRE_VALID (self, sal);
249
250 if (sal->end > 0)
251 return gdb_py_long_from_ulongest (sal->end - 1);
252 else
253 Py_RETURN_NONE;
254 }
255
256 static PyObject *
257 salpy_get_line (PyObject *self, void *closure)
258 {
259 struct symtab_and_line *sal = NULL;
260
261 SALPY_REQUIRE_VALID (self, sal);
262
263 return PyInt_FromLong (sal->line);
264 }
265
266 static PyObject *
267 salpy_get_symtab (PyObject *self, void *closure)
268 {
269 struct symtab_and_line *sal;
270 sal_object *self_sal = (sal_object *) self;
271
272 SALPY_REQUIRE_VALID (self, sal);
273
274 Py_INCREF (self_sal->symtab);
275
276 return (PyObject *) self_sal->symtab;
277 }
278
279 /* Implementation of gdb.Symtab_and_line.is_valid (self) -> Boolean.
280 Returns True if this Symbol table and line object still exists GDB. */
281
282 static PyObject *
283 salpy_is_valid (PyObject *self, PyObject *args)
284 {
285 struct symtab_and_line *sal;
286
287 sal = sal_object_to_symtab_and_line (self);
288 if (sal == NULL)
289 Py_RETURN_FALSE;
290
291 Py_RETURN_TRUE;
292 }
293
294 static void
295 salpy_dealloc (PyObject *self)
296 {
297 sal_object *self_sal = (sal_object *) self;
298
299 if (self_sal->prev)
300 self_sal->prev->next = self_sal->next;
301 else if (self_sal->symtab != (symtab_object * ) Py_None)
302 set_objfile_data (self_sal->symtab->symtab->objfile,
303 salpy_objfile_data_key, self_sal->next);
304
305 if (self_sal->next)
306 self_sal->next->prev = self_sal->prev;
307
308 Py_DECREF (self_sal->symtab);
309 xfree (self_sal->sal);
310 Py_TYPE (self)->tp_free (self);
311 }
312
313 /* Given a sal, and a sal_object that has previously been allocated
314 and initialized, populate the sal_object with the struct sal data.
315 Also, register the sal_object life-cycle with the life-cycle of the
316 object file associated with this sal, if needed. If a failure
317 occurs during the sal population, this function will return
318 NULL. */
319 static int
320 set_sal (sal_object *sal_obj, struct symtab_and_line sal)
321 {
322 symtab_object *symtab_obj;
323
324 if (sal.symtab)
325 {
326 symtab_obj = (symtab_object *) symtab_to_symtab_object (sal.symtab);
327 /* If a symtab existed in the sal, but it cannot be duplicated,
328 we exit. */
329 if (symtab_obj == NULL)
330 return 0;
331 }
332 else
333 {
334 symtab_obj = (symtab_object *) Py_None;
335 Py_INCREF (Py_None);
336 }
337
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;
342
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)
346 {
347 sal_obj->next = objfile_data (sal_obj->symtab->symtab->objfile,
348 salpy_objfile_data_key);
349 if (sal_obj->next)
350 sal_obj->next->prev = sal_obj;
351
352 set_objfile_data (sal_obj->symtab->symtab->objfile,
353 salpy_objfile_data_key, sal_obj);
354 }
355 else
356 sal_obj->next = NULL;
357
358 return 1;
359 }
360
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. */
366 static void
367 set_symtab (symtab_object *obj, struct symtab *symtab)
368 {
369 obj->symtab = symtab;
370 obj->prev = NULL;
371 if (symtab)
372 {
373 obj->next = objfile_data (symtab->objfile, stpy_objfile_data_key);
374 if (obj->next)
375 obj->next->prev = obj;
376 set_objfile_data (symtab->objfile, stpy_objfile_data_key, obj);
377 }
378 else
379 obj->next = NULL;
380 }
381
382 /* Create a new symbol table (gdb.Symtab) object that encapsulates the
383 symtab structure from GDB. */
384 PyObject *
385 symtab_to_symtab_object (struct symtab *symtab)
386 {
387 symtab_object *symtab_obj;
388
389 symtab_obj = PyObject_New (symtab_object, &symtab_object_type);
390 if (symtab_obj)
391 set_symtab (symtab_obj, symtab);
392
393 return (PyObject *) symtab_obj;
394 }
395
396 /* Create a new symtab and line (gdb.Symtab_and_line) object
397 that encapsulates the symtab_and_line structure from GDB. */
398 PyObject *
399 symtab_and_line_to_sal_object (struct symtab_and_line sal)
400
401 {
402 sal_object *sal_obj;
403 int success = 0;
404
405 sal_obj = PyObject_New (sal_object, &sal_object_type);
406 if (sal_obj)
407 {
408 success = set_sal (sal_obj, sal);
409 if (!success)
410 {
411 Py_DECREF (sal_obj);
412 return NULL;
413 }
414 }
415
416 return (PyObject *) sal_obj;
417 }
418
419 /* Return struct symtab_and_line reference that is wrapped by this
420 object. */
421 struct symtab_and_line *
422 sal_object_to_symtab_and_line (PyObject *obj)
423 {
424 if (! PyObject_TypeCheck (obj, &sal_object_type))
425 return NULL;
426 return ((sal_object *) obj)->sal;
427 }
428
429 /* Return struct symtab reference that is wrapped by this object. */
430 struct symtab *
431 symtab_object_to_symtab (PyObject *obj)
432 {
433 if (! PyObject_TypeCheck (obj, &symtab_object_type))
434 return NULL;
435 return ((symtab_object *) obj)->symtab;
436 }
437
438 /* This function is called when an objfile is about to be freed.
439 Invalidate the symbol table as further actions on the symbol table
440 would result in bad data. All access to obj->symtab should be
441 gated by STPY_REQUIRE_VALID which will raise an exception on
442 invalid symbol tables. */
443 static void
444 del_objfile_symtab (struct objfile *objfile, void *datum)
445 {
446 symtab_object *obj = datum;
447
448 while (obj)
449 {
450 symtab_object *next = obj->next;
451
452 obj->symtab = NULL;
453 obj->next = NULL;
454 obj->prev = NULL;
455 obj = next;
456 }
457 }
458
459 /* This function is called when an objfile is about to be freed.
460 Invalidate the sal object as further actions on the sal
461 would result in bad data. All access to obj->sal should be
462 gated by SALPY_REQUIRE_VALID which will raise an exception on
463 invalid symbol table and line objects. */
464 static void
465 del_objfile_sal (struct objfile *objfile, void *datum)
466 {
467 sal_object *obj = datum;
468
469 while (obj)
470 {
471 sal_object *next = obj->next;
472
473 Py_DECREF (obj->symtab);
474 obj->symtab = (symtab_object *) Py_None;
475 Py_INCREF (Py_None);
476
477 obj->next = NULL;
478 obj->prev = NULL;
479 xfree (obj->sal);
480 obj->sal = NULL;
481
482 obj = next;
483 }
484 }
485
486 void
487 gdbpy_initialize_symtabs (void)
488 {
489 symtab_object_type.tp_new = PyType_GenericNew;
490 if (PyType_Ready (&symtab_object_type) < 0)
491 return;
492
493 sal_object_type.tp_new = PyType_GenericNew;
494 if (PyType_Ready (&sal_object_type) < 0)
495 return;
496
497 /* Register an objfile "free" callback so we can properly
498 invalidate symbol tables, and symbol table and line data
499 structures when an object file that is about to be
500 deleted. */
501 stpy_objfile_data_key
502 = register_objfile_data_with_cleanup (NULL, del_objfile_symtab);
503 salpy_objfile_data_key
504 = register_objfile_data_with_cleanup (NULL, del_objfile_sal);
505
506 Py_INCREF (&symtab_object_type);
507 PyModule_AddObject (gdb_module, "Symtab",
508 (PyObject *) &symtab_object_type);
509
510 Py_INCREF (&sal_object_type);
511 PyModule_AddObject (gdb_module, "Symtab_and_line",
512 (PyObject *) &sal_object_type);
513 }
514
515 \f
516
517 static PyGetSetDef symtab_object_getset[] = {
518 { "filename", stpy_get_filename, NULL,
519 "The symbol table's source filename.", NULL },
520 { "objfile", stpy_get_objfile, NULL, "The symtab's objfile.",
521 NULL },
522 {NULL} /* Sentinel */
523 };
524
525 static PyMethodDef symtab_object_methods[] = {
526 { "is_valid", stpy_is_valid, METH_NOARGS,
527 "is_valid () -> Boolean.\n\
528 Return true if this symbol table is valid, false if not." },
529 { "fullname", stpy_fullname, METH_NOARGS,
530 "fullname () -> String.\n\
531 Return the symtab's full source filename." },
532 { "global_block", stpy_global_block, METH_NOARGS,
533 "global_block () -> gdb.Block.\n\
534 Return the global block of the symbol table." },
535 { "static_block", stpy_static_block, METH_NOARGS,
536 "static_block () -> gdb.Block.\n\
537 Return the static block of the symbol table." },
538 {NULL} /* Sentinel */
539 };
540
541 static PyTypeObject symtab_object_type = {
542 PyVarObject_HEAD_INIT (NULL, 0)
543 "gdb.Symtab", /*tp_name*/
544 sizeof (symtab_object), /*tp_basicsize*/
545 0, /*tp_itemsize*/
546 stpy_dealloc, /*tp_dealloc*/
547 0, /*tp_print*/
548 0, /*tp_getattr*/
549 0, /*tp_setattr*/
550 0, /*tp_compare*/
551 0, /*tp_repr*/
552 0, /*tp_as_number*/
553 0, /*tp_as_sequence*/
554 0, /*tp_as_mapping*/
555 0, /*tp_hash */
556 0, /*tp_call*/
557 stpy_str, /*tp_str*/
558 0, /*tp_getattro*/
559 0, /*tp_setattro*/
560 0, /*tp_as_buffer*/
561 Py_TPFLAGS_DEFAULT, /*tp_flags*/
562 "GDB symtab object", /*tp_doc */
563 0, /*tp_traverse */
564 0, /*tp_clear */
565 0, /*tp_richcompare */
566 0, /*tp_weaklistoffset */
567 0, /*tp_iter */
568 0, /*tp_iternext */
569 symtab_object_methods, /*tp_methods */
570 0, /*tp_members */
571 symtab_object_getset /*tp_getset */
572 };
573
574 static PyGetSetDef sal_object_getset[] = {
575 { "symtab", salpy_get_symtab, NULL, "Symtab object.", NULL },
576 { "pc", salpy_get_pc, NULL, "Return the symtab_and_line's pc.", NULL },
577 { "last", salpy_get_last, NULL,
578 "Return the symtab_and_line's last address.", NULL },
579 { "line", salpy_get_line, NULL,
580 "Return the symtab_and_line's line.", NULL },
581 {NULL} /* Sentinel */
582 };
583
584 static PyMethodDef sal_object_methods[] = {
585 { "is_valid", salpy_is_valid, METH_NOARGS,
586 "is_valid () -> Boolean.\n\
587 Return true if this symbol table and line is valid, false if not." },
588 {NULL} /* Sentinel */
589 };
590
591 static PyTypeObject sal_object_type = {
592 PyVarObject_HEAD_INIT (NULL, 0)
593 "gdb.Symtab_and_line", /*tp_name*/
594 sizeof (sal_object), /*tp_basicsize*/
595 0, /*tp_itemsize*/
596 salpy_dealloc, /*tp_dealloc*/
597 0, /*tp_print*/
598 0, /*tp_getattr*/
599 0, /*tp_setattr*/
600 0, /*tp_compare*/
601 0, /*tp_repr*/
602 0, /*tp_as_number*/
603 0, /*tp_as_sequence*/
604 0, /*tp_as_mapping*/
605 0, /*tp_hash */
606 0, /*tp_call*/
607 salpy_str, /*tp_str*/
608 0, /*tp_getattro*/
609 0, /*tp_setattro*/
610 0, /*tp_as_buffer*/
611 Py_TPFLAGS_DEFAULT, /*tp_flags*/
612 "GDB symtab_and_line object", /*tp_doc */
613 0, /*tp_traverse */
614 0, /*tp_clear */
615 0, /*tp_richcompare */
616 0, /*tp_weaklistoffset */
617 0, /*tp_iter */
618 0, /*tp_iternext */
619 sal_object_methods, /*tp_methods */
620 0, /*tp_members */
621 sal_object_getset /*tp_getset */
622 };
This page took 0.074281 seconds and 4 git commands to generate.