Commit b4b2280c5280573783b390e6d330eaaf3a2a2411

Expose symtab to Python.

Commit diff

gdb/Makefile.in

 
266266 python-function.o \
267267 python-hooks.o \
268268 python-symbol.o \
269 python-symtab.o \
269270 python-utils.o \
270271 python-value.o
271272SUBDIR_PYTHON_SRCS = \
278278 python/python-function.c \
279279 python/python-hooks.c \
280280 python/python-symbol.c \
281 python/python-symtab.c \
281282 python/python-utils.c \
282283 python/python-value.c
283284SUBDIR_PYTHON_DEPS =
34763476 $(symtab_h) $(python_h) $(python_internal_h)
34773477 $(CC) -c $(INTERNAL_CFLAGS) $(PYTHON_CFLAGS) \
34783478 $(srcdir)/python/python-symbol.c -o python-symbol.o
3479python-symtab.o: $(srcdir)/python/python-symtab.c $(defs_h) \
3480 $(charset_h) $(source_h) $(symtab_h) $(python_h) $(python_internal_h)
3481 $(CC) -c $(INTERNAL_CFLAGS) $(PYTHON_CFLAGS) \
3482 $(srcdir)/python/python-symtab.c -o python-symtab.o
34793483python-utils.o: $(srcdir)/python/python-utils.c $(defs_h) $(python_internal_h)
34803484 $(CC) -c $(INTERNAL_CFLAGS) $(PYTHON_CFLAGS) \
34813485 $(srcdir)/python/python-utils.c -o python-utils.o
toggle raw diff

gdb/python/python-frame.c

 
5656static PyObject *frapy_get_address_in_block (PyObject *self, PyObject *args);
5757static PyObject *frapy_get_prev (PyObject *self, PyObject *args);
5858static PyObject *frapy_get_next (PyObject *self, PyObject *args);
59static PyObject *frapy_find_sal (PyObject *self, PyObject *args);
60static PyObject *frapy_read_var_value (PyObject *self, PyObject *args);
5961
6062#define FRAPY_REQUIRE_VALID(frame_obj, frame) \
6163 do { \
8686 { "get_prev", frapy_get_prev, METH_NOARGS,
8787 "Return the previous (outer) frame." },
8888 { "get_next", frapy_get_next, METH_NOARGS, "Return the next (inner) frame." },
89 { "find_sal", frapy_find_sal, METH_NOARGS,
90 "Return the frame's symtab and line." },
91 { "read_var_value", frapy_read_var_value, METH_VARARGS,
92 "Return the value of the variable in this frame." },
8993 {NULL} /* Sentinel */
9094};
9195
408408 return next_obj;
409409}
410410
411static PyObject *
412frapy_find_sal (PyObject *self, PyObject *args)
413{
414 struct frame_info *frame;
415 struct symtab_and_line sal;
416 volatile struct gdb_exception except;
417 PyObject *sal_obj = NULL; /* Initialize to appease gcc warning. */
418
419 TRY_CATCH (except, RETURN_MASK_ALL)
420 {
421 FRAPY_REQUIRE_VALID ((frame_object *) self, frame);
422
423 find_frame_sal (frame, &sal);
424 sal_obj = symtab_and_line_to_sal_object (sal);
425 }
426 GDB_PY_HANDLE_EXCEPTION (except);
427
428 return sal_obj;
429}
430
431static PyObject *
432frapy_read_var_value (PyObject *self, PyObject *args)
433{
434 struct frame_info *frame;
435 PyObject *sym_obj;
436 struct symbol *var;
437 struct value *val = NULL;
438 volatile struct gdb_exception except;
439
440 if (!PyArg_ParseTuple (args, "O!", &symbol_object_type, &sym_obj))
441 return NULL;
442
443 var = symbol_object_to_symbol (sym_obj);
444
445 TRY_CATCH (except, RETURN_MASK_ALL)
446 {
447 FRAPY_REQUIRE_VALID ((frame_object *) self, frame);
448
449 val = read_var_value (var, frame);
450 }
451 GDB_PY_HANDLE_EXCEPTION (except);
452
453 if (val)
454 return value_to_value_object (val);
455
456 Py_RETURN_NONE;
457}
458
411459PyObject *
412460gdbpy_get_frames (PyObject *self, PyObject *args)
413461{
toggle raw diff

gdb/python/python-internal.h

 
4545
4646struct block;
4747struct symbol;
48struct symtab_and_line;
4849struct value;
4950
5051extern PyObject *gdb_module;
5858PyObject *gdbpy_get_frames (PyObject *, PyObject *);
5959PyObject *gdbpy_get_current_frame (PyObject *, PyObject *);
6060PyObject *gdbpy_frame_stop_reason_string (PyObject *, PyObject *);
61PyObject *gdbpy_lookup_symbol (PyObject *self, PyObject *args);
6162PyObject *gdbpy_get_selected_frame (PyObject *self, PyObject *args);
6263
64PyObject *symtab_and_line_to_sal_object (struct symtab_and_line sal);
65PyObject *symtab_to_symtab_object (struct symtab *symtab);
6366PyObject *symbol_to_symbol_object (struct symbol *sym);
6467PyObject *block_to_block_object (struct block *block);
6568PyObject *value_to_value_object (struct value *v);
7777void gdbpy_initialize_values (void);
7878void gdbpy_initialize_breakpoints (void);
7979void gdbpy_initialize_frames (void);
80void gdbpy_initialize_symtabs (void);
8081void gdbpy_initialize_commands (void);
8182void gdbpy_initialize_symbols (void);
8283void gdbpy_initialize_blocks (void);
toggle raw diff

gdb/python/python-symbol.c

 
2828
2929static PyObject *sympy_str (PyObject *self);
3030static PyObject *sympy_get_value (PyObject *self, PyObject *args);
31static PyObject *sympy_get_symtab (PyObject *self, PyObject *args);
3132static PyObject *sympy_get_natural_name (PyObject *self, PyObject *args);
3233static PyObject *sympy_get_linkage_name (PyObject *self, PyObject *args);
3334static PyObject *sympy_get_print_name (PyObject *self, PyObject *args);
3737static PyMethodDef symbol_object_methods[] = {
3838 { "get_value", sympy_get_value, METH_NOARGS,
3939 "Return the value of the symbol." },
40 { "get_symtab", sympy_get_symtab, METH_NOARGS,
41 "Return the value of the symbol." },
4042 { "get_natural_name", sympy_get_natural_name, METH_NOARGS,
4143 "Return the \"natural\" name of the symbol." },
4244 { "get_linkage_name", sympy_get_linkage_name, METH_NOARGS,
118118}
119119
120120static PyObject *
121sympy_get_symtab (PyObject *self, PyObject *args)
122{
123 symbol_object *self_sym = (symbol_object *) self;
124
125 return symtab_to_symtab_object (SYMBOL_SYMTAB (self_sym->symbol));
126}
127
128static PyObject *
121129sympy_get_natural_name (PyObject *self, PyObject *args)
122130{
123131 symbol_object *self_sym = (symbol_object *) self;
toggle raw diff

gdb/python/python-symtab.c

 
1/* Python interface to symbol tables.
2
3 Copyright (C) 2008 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
26typedef struct {
27 PyObject_HEAD
28 struct symtab *symtab;
29} symtab_object;
30
31static PyObject *stpy_str (PyObject *self);
32static PyObject *stpy_filename (PyObject *self, PyObject *args);
33static PyObject *stpy_to_fullname (PyObject *self, PyObject *args);
34
35static PyMethodDef symtab_object_methods[] = {
36 { "get_filename", stpy_filename, METH_NOARGS,
37 "Return the symtab's source filename." },
38 { "to_fullname", stpy_to_fullname, METH_NOARGS,
39 "Return the symtab's full source filename." },
40 {NULL} /* Sentinel */
41};
42
43static PyTypeObject symtab_object_type = {
44 PyObject_HEAD_INIT (NULL)
45 0, /*ob_size*/
46 "gdb.Symtab", /*tp_name*/
47 sizeof (symtab_object), /*tp_basicsize*/
48 0, /*tp_itemsize*/
49 0, /*tp_dealloc*/
50 0, /*tp_print*/
51 0, /*tp_getattr*/
52 0, /*tp_setattr*/
53 0, /*tp_compare*/
54 0, /*tp_repr*/
55 0, /*tp_as_number*/
56 0, /*tp_as_sequence*/
57 0, /*tp_as_mapping*/
58 0, /*tp_hash */
59 0, /*tp_call*/
60 stpy_str, /*tp_str*/
61 0, /*tp_getattro*/
62 0, /*tp_setattro*/
63 0, /*tp_as_buffer*/
64 Py_TPFLAGS_DEFAULT, /*tp_flags*/
65 "GDB symtab object", /* tp_doc */
66 0, /* tp_traverse */
67 0, /* tp_clear */
68 0, /* tp_richcompare */
69 0, /* tp_weaklistoffset */
70 0, /* tp_iter */
71 0, /* tp_iternext */
72 symtab_object_methods /* tp_methods */
73};
74
75typedef struct {
76 PyObject_HEAD
77 symtab_object *symtab;
78 struct symtab_and_line *sal;
79} sal_object;
80
81static void salpy_dealloc (PyObject *self);
82static int salpy_setsymtab (PyObject *self, PyObject *value, void *closure);
83static PyObject *salpy_str (PyObject *self);
84static PyObject *salpy_getsymtab (PyObject *self, void *closure);
85static PyObject *salpy_pc (PyObject *self, PyObject *args);
86static PyObject *salpy_line (PyObject *self, PyObject *args);
87
88static PyGetSetDef sal_object_getseters[] = {
89 { "symtab", salpy_getsymtab, salpy_setsymtab, "Symtab object.", NULL },
90 {NULL} /* Sentinel */
91};
92
93static PyMethodDef sal_object_methods[] = {
94 { "get_pc", salpy_pc, METH_NOARGS,
95 "Return the symtab_and_line's pc." },
96 { "get_line", salpy_line, METH_NOARGS,
97 "Return the symtab_and_line's line." },
98 {NULL} /* Sentinel */
99};
100
101static PyTypeObject sal_object_type = {
102 PyObject_HEAD_INIT (NULL)
103 0, /*ob_size*/
104 "gdb.Symtab_and_line", /*tp_name*/
105 sizeof (sal_object), /*tp_basicsize*/
106 0, /*tp_itemsize*/
107 salpy_dealloc, /*tp_dealloc*/
108 0, /*tp_print*/
109 0, /*tp_getattr*/
110 0, /*tp_setattr*/
111 0, /*tp_compare*/
112 0, /*tp_repr*/
113 0, /*tp_as_number*/
114 0, /*tp_as_sequence*/
115 0, /*tp_as_mapping*/
116 0, /*tp_hash */
117 0, /*tp_call*/
118 salpy_str, /*tp_str*/
119 0, /*tp_getattro*/
120 0, /*tp_setattro*/
121 0, /*tp_as_buffer*/
122 Py_TPFLAGS_DEFAULT, /*tp_flags*/
123 "GDB symtab_and_line object", /* tp_doc */
124 0, /* tp_traverse */
125 0, /* tp_clear */
126 0, /* tp_richcompare */
127 0, /* tp_weaklistoffset */
128 0, /* tp_iter */
129 0, /* tp_iternext */
130 sal_object_methods, /* tp_methods */
131 0, /* tp_members */
132 sal_object_getseters /* tp_getset */
133};
134
135
136static PyObject *
137stpy_str (PyObject *self)
138{
139 int ret;
140 char *s;
141 PyObject *result;
142
143 ret = asprintf (&s, "symbol table for %s",
144 ((symtab_object *) self)->symtab->filename);
145 if (ret < 0)
146 Py_RETURN_NONE;
147
148 result = PyString_FromString (s);
149 xfree (s);
150
151 return result;
152}
153
154/* FIXME: maybe this should be an attribute instead of a method? */
155static PyObject *
156stpy_filename (PyObject *self, PyObject *args)
157{
158 symtab_object *self_symtab = (symtab_object *) self;
159 PyObject *str_obj;
160
161 /* FIXME: Can symtab->filename really be NULL? */
162 if (self_symtab->symtab->filename)
163 str_obj = PyString_Decode (self_symtab->symtab->filename,
164 strlen (self_symtab->symtab->filename),
165 host_charset (), NULL);
166 else
167 {
168 str_obj = Py_None;
169 Py_INCREF (Py_None);
170 }
171
172 return str_obj;
173}
174
175static PyObject *
176stpy_to_fullname (PyObject *self, PyObject *args)
177{
178 char *fullname;
179
180 fullname = symtab_to_fullname (((symtab_object *) self)->symtab);
181 if (fullname)
182 return PyString_Decode (fullname, strlen (fullname), host_charset (), NULL);
183
184 Py_RETURN_NONE;
185}
186
187static PyObject *
188salpy_str (PyObject *self)
189{
190 int ret;
191 char *s, *filename;
192 sal_object *sal_obj;
193 PyObject *result;
194
195 sal_obj = (sal_object *) self;
196 filename = (sal_obj->symtab == (symtab_object *) Py_None)? "<unknown>" :
197 sal_obj->symtab->symtab->filename;
198 ret = asprintf (&s, "symbol and line for %s, line %d", filename,
199 sal_obj->sal->line);
200 if (ret < 0)
201 Py_RETURN_NONE;
202
203 result = PyString_FromString (s);
204 xfree (s);
205
206 return result;
207}
208
209static PyObject *
210salpy_pc (PyObject *self, PyObject *args)
211{
212 return PyLong_FromUnsignedLongLong (((sal_object *) self)->sal->pc);
213}
214
215static PyObject *
216salpy_line (PyObject *self, PyObject *args)
217{
218 return PyLong_FromUnsignedLongLong (((sal_object *) self)->sal->line);
219}
220
221static PyObject *
222salpy_getsymtab (PyObject *self, void *closure)
223{
224 sal_object *self_sal = (sal_object *) self;
225
226 Py_INCREF (self_sal->symtab);
227
228 return (PyObject *) self_sal->symtab;
229}
230
231static int
232salpy_setsymtab (PyObject *self, PyObject *value, void *closure)
233{
234 PyErr_SetString (PyExc_TypeError, "The symtab attribute can't be modified.");
235
236 return -1;
237}
238
239static void
240salpy_dealloc (PyObject *self)
241{
242 sal_object *self_sal = (sal_object *) self;
243
244 Py_DECREF (self_sal->symtab);
245 xfree (self_sal->sal);
246 self_sal->ob_type->tp_free (self);
247}
248
249PyObject *
250symtab_and_line_to_sal_object (struct symtab_and_line sal)
251{
252 sal_object *sal_obj;
253 symtab_object *symtab_obj;
254
255 sal_obj = PyObject_New (sal_object, &sal_object_type);
256 if (sal_obj == NULL)
257 {
258 PyErr_SetString (PyExc_MemoryError,
259 "Could not allocate Symtab_and_line object.");
260 return NULL;
261 }
262
263 if (sal.symtab)
264 {
265 symtab_obj = (symtab_object *) symtab_to_symtab_object (sal.symtab);
266 if (symtab_obj == NULL)
267 {
268 Py_DECREF (sal_obj);
269 return NULL;
270 }
271
272 symtab_obj->symtab = sal.symtab;
273 }
274 else
275 {
276 symtab_obj = (symtab_object *) Py_None;
277 Py_INCREF (Py_None);
278 }
279
280 sal_obj->sal = (struct symtab_and_line *)
281 xmalloc (sizeof (struct symtab_and_line));
282 *(sal_obj->sal) = sal;
283 sal_obj->symtab = symtab_obj;
284
285 return (PyObject *) sal_obj;
286}
287
288PyObject *
289symtab_to_symtab_object (struct symtab *symtab)
290{
291 symtab_object *symtab_obj;
292
293 symtab_obj = PyObject_New (symtab_object, &symtab_object_type);
294 if (symtab_obj == NULL)
295 {
296 PyErr_SetString (PyExc_MemoryError,
297 "Could not allocate Symtab object.");
298
299 return NULL;
300 }
301
302 symtab_obj->symtab = symtab;
303
304 return (PyObject *) symtab_obj;
305}
306
307void
308gdbpy_initialize_symtabs (void)
309{
310 symtab_object_type.tp_new = PyType_GenericNew;
311 if (PyType_Ready (&symtab_object_type) < 0)
312 return;
313
314 sal_object_type.tp_new = PyType_GenericNew;
315 if (PyType_Ready (&sal_object_type) < 0)
316 return;
317
318 Py_INCREF (&symtab_object_type);
319 PyModule_AddObject (gdb_module, "Symtab", (PyObject *) &symtab_object_type);
320
321 Py_INCREF (&sal_object_type);
322 PyModule_AddObject (gdb_module, "Symtab_and_line",
323 (PyObject *) &sal_object_type);
324}
toggle raw diff

gdb/python/python.c

 
3939#include "solib.h"
4040#include "exceptions.h"
4141#include "python-internal.h"
42#include "linespec.h"
43#include "symtab.h"
44#include "source.h"
4245#include "version.h"
4346#include "target.h"
4447#include "gdbthread.h"
5252static PyObject *get_show_variable (PyObject *, PyObject *);
5353static PyObject *execute_gdb_command (PyObject *, PyObject *);
5454static PyObject *gdbpy_solib_address (PyObject *, PyObject *);
55static PyObject *gdbpy_decode_line (PyObject *, PyObject *);
5556static PyObject *gdbpy_find_pc_function (PyObject *, PyObject *);
5657static PyObject *gdbpy_write (PyObject *, PyObject *);
5758static PyObject *gdbpy_flush (PyObject *, PyObject *);
7878 { "frame_stop_reason_string", gdbpy_frame_stop_reason_string,
7979 METH_VARARGS, "Return a string explaining unwind stop reason" },
8080
81 { "lookup_symbol", gdbpy_lookup_symbol, METH_VARARGS,
82 "Return the symbol corresponding to the given name, or None." },
8183 { "solib_address", gdbpy_solib_address, METH_VARARGS,
8284 "Return shared library holding a given address, or None." },
8385
8486 { "find_pc_function", gdbpy_find_pc_function, METH_VARARGS,
8587 "Return the function containing the given pc value, or None." },
8688
89 { "decode_line", gdbpy_decode_line, METH_VARARGS,
90 "Decode a string argument the way that 'break' or 'edit' does.\n\
91Return a tuple holding the file name (or None) and line number (or None).\n\
92Note: may later change to return an object." },
93
8794 { "write", gdbpy_write, METH_VARARGS,
8895 "Write a string using gdb's filtered stream." },
8996 { "flush", gdbpy_flush, METH_NOARGS,
339339 Py_RETURN_NONE;
340340}
341341
342/* A Python function which is a wrapper for decode_line_1. */
343
344static PyObject *
345gdbpy_decode_line (PyObject *self, PyObject *args)
346{
347 struct symtabs_and_lines sals = { NULL, 0 }; /* Initialize to appease gcc. */
348 struct symtab_and_line sal;
349 char *arg = NULL;
350 int free_sals = 0, i;
351 PyObject *result = NULL;
352 volatile struct gdb_exception except;
353
354 if (! PyArg_ParseTuple (args, "|s", &arg))
355 return NULL;
356
357 TRY_CATCH (except, RETURN_MASK_ALL)
358 {
359 if (arg)
360 {
361 char *copy;
362
363 arg = strdup (arg);
364 copy = arg;
365
366 sals = decode_line_1 (&copy, 0, 0, 0, 0, 0);
367 free_sals = 1;
368 }
369 else
370 {
371 set_default_source_symtab_and_line ();
372 sal = get_current_source_symtab_and_line ();
373 sals.sals = &sal;
374 sals.nelts = 1;
375 }
376 }
377 if (arg)
378 xfree (arg);
379
380 if (except.reason < 0)
381 {
382 if (free_sals)
383 xfree (sals.sals);
384 /* We know this will always throw. */
385 GDB_PY_HANDLE_EXCEPTION (except);
386 }
387
388 if (sals.nelts)
389 {
390 result = PyTuple_New (sals.nelts);
391 for (i = 0; i < sals.nelts; ++i)
392 {
393 PyObject *obj;
394 char *str;
395
396 obj = symtab_and_line_to_sal_object (sals.sals[i]);
397 if (! obj)
398 {
399 Py_DECREF (result);
400 result = NULL;
401 break;
402 }
403
404 PyTuple_SetItem (result, i, obj);
405 }
406 }
407
408 if (free_sals)
409 xfree (sals.sals);
410
411 if (result)
412 return result;
413 Py_RETURN_NONE;
414}
415
342416
343417
344418/* Printing. */
557557 gdbpy_initialize_values ();
558558 gdbpy_initialize_breakpoints ();
559559 gdbpy_initialize_frames ();
560 gdbpy_initialize_symtabs ();
560561 gdbpy_initialize_commands ();
561562 gdbpy_initialize_symbols ();
562563 gdbpy_initialize_blocks ();
toggle raw diff