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