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