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