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