System notice: In light of the Debian OpenSSL security issue we've regenerated the server keys. See this thread for instructions and the new key fingerprints.

Commit b762eb212c3630895fa515e3a860e29fcef63f5a

Expose struct block and struct symbol to Python.

Also add find_pc_function and Frame.get_address_in_block.

Commit diff

gdb/Makefile.in

 
254254#
255255SUBDIR_PYTHON_OBS = \
256256 python.o \
257 python-block.o \
257258 python-breakpoint.o \
258259 python-cmd.o \
259260 python-frame.o \
260261 python-hooks.o \
261262 python-symtab.o \
263 python-symbol.o \
262264 python-value.o
263265SUBDIR_PYTHON_SRCS = \
264266 python/python.c \
267 python/block.c \
265268 python/breakpoint.c \
266269 python/cmd.c \
267270 python/frame.c \
268271 python/hooks.c \
269272 python/symtab.c \
273 python/symbol.c \
270274 python/value.c
271275SUBDIR_PYTHON_DEPS =
272276SUBDIR_PYTHON_LDFLAGS=
33993399 $(command_h) $(libiberty_h) $(cli_decode_h) $(charset_h) $(top_h) \
34003400 $(exceptions_h) $(python_internal_h) $(solib_h)
34013401 $(CC) -c $(INTERNAL_CFLAGS) $(PYTHON_CFLAGS) $(srcdir)/python/python.c
3402python-block.o: $(srcdir)/python/block.c $(defs_h) $(block_h) $(dictionary_h) \
3403 $(symtab_h) $(python_h) $(python_internal_h)
3404 $(CC) -c $(INTERNAL_CFLAGS) $(PYTHON_CFLAGS) \
3405 $(srcdir)/python/block.c -o python-block.o
34023406python-breakpoint.o: $(srcdir)/python/breakpoint.c $(defs_h) $(python_h) \
34033407 $(value_h) $(exceptions_h) $(python_internal_h) $(charset_h) \
34043408 $(breakpoint_h) $(gdbcmd_h)
34223422 $(charset_h) $(gdb_events_h) $(python_h) $(python_internal_h)
34233423 $(CC) -c $(INTERNAL_CFLAGS) $(PYTHON_CFLAGS) \
34243424 $(srcdir)/python/hooks.c -o python-hooks.o
3425python-symbol.o: $(srcdir)/python/symbol.c $(defs_h) $(symtab_h) $(python_h) \
3426 $(python_internal_h)
3427 $(CC) -c $(INTERNAL_CFLAGS) $(PYTHON_CFLAGS) \
3428 $(srcdir)/python/symbol.c -o python-symbol.o
34253429python-symtab.o: $(srcdir)/python/symtab.c $(defs_h) $(charset_h) $(source_h) \
34263430 $(symtab_h) $(python_h) $(python_internal_h)
34273431 $(CC) -c $(INTERNAL_CFLAGS) $(PYTHON_CFLAGS) \
toggle raw diff

gdb/python/block.c

 
1/* Python interface to blocks.
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 "block.h"
22#include "dictionary.h"
23#include "symtab.h"
24#include "python-internal.h"
25
26typedef struct {
27 PyObject_HEAD
28 struct block *block;
29} block_object;
30
31static PyObject *blpy_iter (PyObject *self);
32static PyObject *blpy_itersymbols (PyObject *self, PyObject *args);
33static PyObject *blpy_get_start (PyObject *self, PyObject *args);
34static PyObject *blpy_get_end (PyObject *self, PyObject *args);
35static PyObject *blpy_get_function (PyObject *self, PyObject *args);
36static PyObject *blpy_get_superblock (PyObject *self, PyObject *args);
37
38static PyMethodDef block_object_methods[] = {
39 { "itersymbols", blpy_itersymbols, METH_NOARGS,
40 "Return an iterator to walk through the symbols in the block." },
41 { "get_start", blpy_get_start, METH_NOARGS,
42 "Return the start address of this block." },
43 { "get_end", blpy_get_end, METH_NOARGS,
44 "Return the end address of this block." },
45 { "get_function", blpy_get_function, METH_NOARGS,
46 "Return the symbol that names this block, or None." },
47 { "get_superblock", blpy_get_end, METH_NOARGS,
48 "Return the block containing this block, or None." },
49 {NULL} /* Sentinel */
50};
51
52static PyTypeObject block_object_type = {
53 PyObject_HEAD_INIT (NULL)
54 0, /*ob_size*/
55 "gdb.Block", /*tp_name*/
56 sizeof (block_object), /*tp_basicsize*/
57 0, /*tp_itemsize*/
58 0, /*tp_dealloc*/
59 0, /*tp_print*/
60 0, /*tp_getattr*/
61 0, /*tp_setattr*/
62 0, /*tp_compare*/
63 0, /*tp_repr*/
64 0, /*tp_as_number*/
65 0, /*tp_as_sequence*/
66 0, /*tp_as_mapping*/
67 0, /*tp_hash */
68 0, /*tp_call*/
69 0, /*tp_str*/
70 0, /*tp_getattro*/
71 0, /*tp_setattro*/
72 0, /*tp_as_buffer*/
73 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /*tp_flags*/
74 "GDB block object", /* tp_doc */
75 0, /* tp_traverse */
76 0, /* tp_clear */
77 0, /* tp_richcompare */
78 0, /* tp_weaklistoffset */
79 blpy_iter, /* tp_iter */
80 0, /* tp_iternext */
81 block_object_methods /* tp_methods */
82};
83
84typedef struct {
85 PyObject_HEAD
86 struct dictionary *dict;
87 struct dict_iterator iter;
88 int initialized_p;
89} block_syms_iterator_object;
90
91static PyObject *blpy_block_syms_iter (PyObject *self);
92static PyObject *blpy_block_syms_iternext (PyObject *self);
93
94static PyTypeObject block_syms_iterator_object_type = {
95 PyObject_HEAD_INIT (NULL)
96 0, /*ob_size*/
97 "gdb.BlockIterator", /*tp_name*/
98 sizeof (block_syms_iterator_object), /*tp_basicsize*/
99 0, /*tp_itemsize*/
100 0, /*tp_dealloc*/
101 0, /*tp_print*/
102 0, /*tp_getattr*/
103 0, /*tp_setattr*/
104 0, /*tp_compare*/
105 0, /*tp_repr*/
106 0, /*tp_as_number*/
107 0, /*tp_as_sequence*/
108 0, /*tp_as_mapping*/
109 0, /*tp_hash */
110 0, /*tp_call*/
111 0, /*tp_str*/
112 0, /*tp_getattro*/
113 0, /*tp_setattro*/
114 0, /*tp_as_buffer*/
115 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /*tp_flags*/
116 "GDB block syms iterator object", /* tp_doc */
117 0, /* tp_traverse */
118 0, /* tp_clear */
119 0, /* tp_richcompare */
120 0, /* tp_weaklistoffset */
121 blpy_block_syms_iter, /* tp_iter */
122 blpy_block_syms_iternext, /* tp_iternext */
123 0 /* tp_methods */
124};
125
126
127static PyObject *
128blpy_iter (PyObject *self)
129{
130 block_syms_iterator_object *block_iter_obj;
131
132 block_iter_obj = PyObject_New (block_syms_iterator_object,
133 &block_syms_iterator_object_type);
134 if (block_iter_obj == NULL)
135 {
136 PyErr_SetString (PyExc_MemoryError,
137 "Could not allocate iterator object.");
138 return NULL;
139 }
140
141 block_iter_obj->dict = BLOCK_DICT (((block_object *) self)->block);
142 block_iter_obj->initialized_p = 0;
143
144 return (PyObject *) block_iter_obj;
145}
146
147static PyObject *
148blpy_itersymbols (PyObject *self, PyObject *args)
149{
150 return blpy_iter (self);
151}
152
153static PyObject *
154blpy_get_start (PyObject *self, PyObject *args)
155{
156 block_object *self_block = (block_object *) self;
157
158 return PyLong_FromUnsignedLongLong (BLOCK_START (self_block->block));
159}
160
161static PyObject *
162blpy_get_end (PyObject *self, PyObject *args)
163{
164 block_object *self_block = (block_object *) self;
165
166 return PyLong_FromUnsignedLongLong (BLOCK_END (self_block->block));
167}
168
169static PyObject *
170blpy_get_function (PyObject *self, PyObject *args)
171{
172 block_object *self_block = (block_object *) self;
173 struct symbol *sym;
174
175 sym = BLOCK_FUNCTION (self_block->block);
176 if (sym)
177 return symbol_to_symbol_object (sym);
178
179 Py_RETURN_NONE;
180}
181
182static PyObject *
183blpy_get_superblock (PyObject *self, PyObject *args)
184{
185 block_object *self_block = (block_object *) self;
186 struct block *block;
187
188 block = BLOCK_SUPERBLOCK (self_block->block);
189 if (block)
190 return block_to_block_object (block);
191
192 Py_RETURN_NONE;
193}
194
195PyObject *
196block_to_block_object (struct block *block)
197{
198 block_object *block_obj;
199
200 block_obj = PyObject_New (block_object, &block_object_type);
201 if (block_obj == NULL)
202 {
203 PyErr_SetString (PyExc_MemoryError, "Could not allocate block object.");
204 return NULL;
205 }
206
207 block_obj->block = block;
208
209 return (PyObject *) block_obj;
210}
211
212static PyObject *
213blpy_block_syms_iter (PyObject *self)
214{
215 return self;
216}
217
218static PyObject *
219blpy_block_syms_iternext (PyObject *self)
220{
221 block_syms_iterator_object *iter_obj = (block_syms_iterator_object *) self;
222 struct symbol *sym;
223
224 if (!iter_obj->initialized_p)
225 {
226 sym = dict_iterator_first (iter_obj->dict, &(iter_obj->iter));
227 iter_obj->initialized_p = 1;
228 }
229 else
230 sym = dict_iterator_next (&(iter_obj->iter));
231
232 return (sym == NULL)? NULL : symbol_to_symbol_object (sym);
233}
234
235void
236gdbpy_initialize_blocks (void)
237{
238 block_object_type.tp_new = PyType_GenericNew;
239 if (PyType_Ready (&block_object_type) < 0)
240 return;
241
242 block_syms_iterator_object_type.tp_new = PyType_GenericNew;
243 if (PyType_Ready (&block_syms_iterator_object_type) < 0)
244 return;
245
246 Py_INCREF (&block_object_type);
247 PyModule_AddObject (gdb_module, "Block", (PyObject *) &block_object_type);
248
249 Py_INCREF (&block_syms_iterator_object_type);
250 PyModule_AddObject (gdb_module, "BlockIterator",
251 (PyObject *) &block_syms_iterator_object_type);
252}
toggle raw diff

gdb/python/frame.c

 
5252static PyObject *frapy_get_type (frame_object *self);
5353static PyObject *frapy_get_unwind_stop_reason (frame_object *self);
5454static PyObject *frapy_get_pc (frame_object *self);
55static PyObject *frapy_get_address_in_block (PyObject *self, PyObject *args);
5556static PyObject *frapy_get_prev (frame_object *self);
5657static PyObject *frapy_get_next (frame_object *self);
5758static PyObject *frapy_find_sal (frame_object *self);
8282 METH_NOARGS, "Return the function name of the frame." },
8383 { "get_pc", (PyCFunction) frapy_get_pc, METH_NOARGS,
8484 "Return the frame's resume address." },
85 { "get_address_in_block", frapy_get_address_in_block, METH_NOARGS,
86 "Return an address which falls within the frame's code block." },
8587 { "get_prev", (PyCFunction) frapy_get_prev, METH_NOARGS,
8688 "Return the previous (outer) frame." },
8789 { "get_next", (PyCFunction) frapy_get_next, METH_NOARGS,
290290 return PyLong_FromUnsignedLongLong (pc);
291291}
292292
293static PyObject *
294frapy_get_address_in_block (PyObject *self, PyObject *args)
295{
296 frame_object *frame_obj = (frame_object *) self;
297 CORE_ADDR pc = 0; /* Initialize to appease gcc warning. */
298 struct frame_info *frame;
299 volatile struct gdb_exception except;
300
301 TRY_CATCH (except, RETURN_MASK_ALL)
302 {
303 FRAPY_REQUIRE_VALID (frame_obj, frame);
304
305 pc = get_frame_address_in_block (frame);
306 }
307 GDB_PY_HANDLE_EXCEPTION (except);
308
309 return PyLong_FromUnsignedLongLong (pc);
310}
311
293312static frame_object *
294313frame_info_to_frame_object (struct frame_info *frame)
295314{
toggle raw diff

gdb/python/python-internal.h

 
3737
3838PyObject *variable_to_python (struct cmd_list_element *);
3939PyObject *symtab_and_line_to_sal_object (struct symtab_and_line sal);
40PyObject *symtab_to_symtab_object (struct symtab *symtab);
41PyObject *symbol_to_symbol_object (struct symbol *sym);
42PyObject *block_to_block_object (struct block *block);
4043
4144PyObject *gdbpy_get_hook_function (const char *);
4245
4949void gdbpy_initialize_frames (void);
5050void gdbpy_initialize_symtabs (void);
5151void gdbpy_initialize_commands (void);
52void gdbpy_initialize_symbols (void);
53void gdbpy_initialize_blocks (void);
5254
5355/* Use this after a TRY_EXCEPT to throw the appropriate Python
5456 exception. FIXME: throw different errors depending on
toggle raw diff

gdb/python/python.c

 
4444static PyObject *gdbpy_version (PyObject *, PyObject *);
4545static PyObject *gdbpy_host_conf (PyObject *, PyObject *);
4646static PyObject *gdbpy_target_conf (PyObject *, PyObject *);
47static PyObject *gdbpy_find_pc_function (PyObject *, PyObject *);
4748
4849
4950void
7777 { "solib_address", gdbpy_solib_address, METH_VARARGS,
7878 "Return shared library holding a given address, or None." },
7979
80 { "find_pc_function", gdbpy_find_pc_function, METH_VARARGS,
81 "Return the function containing the given pc value, or None." },
82
8083 { "decode_line", gdbpy_decode_line, METH_VARARGS,
8184 "Decode a string argument the way that 'break' or 'edit' does.\n\
8285Return a tuple holding the file name (or None) and line number (or None).\n\
105105 gdbpy_initialize_frames ();
106106 gdbpy_initialize_symtabs ();
107107 gdbpy_initialize_commands ();
108 gdbpy_initialize_symbols ();
109 gdbpy_initialize_blocks ();
108110
109111 PyRun_SimpleString ("import gdb");
110112
495495}
496496
497497PyObject *
498gdbpy_find_pc_function (PyObject *self, PyObject *args)
499{
500 unsigned long long pc;
501 struct symbol *sym;
502 PyObject *sym_obj;
503
504 if (!PyArg_ParseTuple (args, "K", &pc))
505 return NULL;
506
507 sym = find_pc_function (pc);
508 if (sym)
509 return symbol_to_symbol_object (sym);
510
511 Py_RETURN_NONE;
512}
513
514PyObject *
498515gdbpy_decode_line (PyObject *self, PyObject *args)
499516{
500517 struct symtabs_and_lines sals = { NULL, 0 }; /* Initialize to appease gcc. */
toggle raw diff

gdb/python/symbol.c

 
1/* Python interface to symbols.
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 "symtab.h"
22#include "python-internal.h"
23
24typedef struct {
25 PyObject_HEAD
26 struct symbol *symbol;
27} symbol_object;
28
29static PyObject *sympy_str (PyObject *self);
30static PyObject *sympy_get_value (PyObject *self, PyObject *args);
31static PyObject *sympy_get_symtab (PyObject *self, PyObject *args);
32static PyObject *sympy_get_natural_name (PyObject *self, PyObject *args);
33static PyObject *sympy_get_linkage_name (PyObject *self, PyObject *args);
34static PyObject *sympy_get_print_name (PyObject *self, PyObject *args);
35
36static PyMethodDef symbol_object_methods[] = {
37 { "get_value", sympy_get_value, METH_NOARGS,
38 "Return the value of the symbol." },
39 { "get_symtab", sympy_get_symtab, METH_NOARGS,
40 "Return the value of the symbol." },
41 { "get_natural_name", sympy_get_natural_name, METH_NOARGS,
42 "Return the \"natural\" name of the symbol." },
43 { "get_linkage_name", sympy_get_linkage_name, METH_NOARGS,
44 "Return the name of the symbol as used by the linker." },
45 { "get_print_name", sympy_get_print_name, METH_NOARGS,
46 "Return the name of the symbol in a form suitable for output." },
47 {NULL} /* Sentinel */
48};
49
50static PyTypeObject symbol_object_type = {
51 PyObject_HEAD_INIT (NULL)
52 0, /*ob_size*/
53 "gdb.Symbol", /*tp_name*/
54 sizeof (symbol_object), /*tp_basicsize*/
55 0, /*tp_itemsize*/
56 0, /*tp_dealloc*/
57 0, /*tp_print*/
58 0, /*tp_getattr*/
59 0, /*tp_setattr*/
60 0, /*tp_compare*/
61 0, /*tp_repr*/
62 0, /*tp_as_number*/
63 0, /*tp_as_sequence*/
64 0, /*tp_as_mapping*/
65 0, /*tp_hash */
66 0, /*tp_call*/
67 sympy_str, /*tp_str*/
68 0, /*tp_getattro*/
69 0, /*tp_setattro*/
70 0, /*tp_as_buffer*/
71 Py_TPFLAGS_DEFAULT, /*tp_flags*/
72 "GDB symbol object", /* tp_doc */
73 0, /* tp_traverse */
74 0, /* tp_clear */
75 0, /* tp_richcompare */
76 0, /* tp_weaklistoffset */
77 0, /* tp_iter */
78 0, /* tp_iternext */
79 symbol_object_methods /* tp_methods */
80};
81
82
83static PyObject *
84sympy_str (PyObject *self)
85{
86 int ret;
87 char *s;
88 PyObject *result;
89
90 ret = asprintf (&s, "symbol for %s",
91 SYMBOL_PRINT_NAME (((symbol_object *) self)->symbol));
92 if (ret < 0)
93 Py_RETURN_NONE;
94
95 result = PyString_FromString (s);
96 xfree (s);
97
98 return result;
99}
100
101static PyObject *
102sympy_get_value (PyObject *self, PyObject *args)
103{
104 symbol_object *self_sym = (symbol_object *) self;
105
106 switch (SYMBOL_CLASS (self_sym->symbol))
107 {
108 case LOC_BLOCK:
109 return block_to_block_object (SYMBOL_BLOCK_VALUE (self_sym->symbol));
110 }
111
112 PyErr_SetString (PyExc_NotImplementedError,
113 "Symbol type not yet supported in Python scripts.");
114 return NULL;
115}
116
117static PyObject *
118sympy_get_symtab (PyObject *self, PyObject *args)
119{
120 symbol_object *self_sym = (symbol_object *) self;
121
122 return symtab_to_symtab_object (SYMBOL_SYMTAB (self_sym->symbol));
123}
124
125static PyObject *
126sympy_get_natural_name (PyObject *self, PyObject *args)
127{
128 symbol_object *self_sym = (symbol_object *) self;
129
130 return PyString_FromString (SYMBOL_NATURAL_NAME (self_sym->symbol));
131}
132
133static PyObject *
134sympy_get_linkage_name (PyObject *self, PyObject *args)
135{
136 symbol_object *self_sym = (symbol_object *) self;
137
138 return PyString_FromString (SYMBOL_LINKAGE_NAME (self_sym->symbol));
139}
140
141static PyObject *
142sympy_get_print_name (PyObject *self, PyObject *args)
143{
144 symbol_object *self_sym = (symbol_object *) self;
145
146 return PyString_FromString (SYMBOL_PRINT_NAME (self_sym->symbol));
147}
148
149PyObject *
150symbol_to_symbol_object (struct symbol *sym)
151{
152 symbol_object *sym_obj;
153
154 sym_obj = PyObject_New (symbol_object, &symbol_object_type);
155 if (sym_obj == NULL)
156 {
157 PyErr_SetString (PyExc_MemoryError, "Could not allocate symbol object.");
158 return NULL;
159 }
160
161 sym_obj->symbol = sym;
162
163 return (PyObject *) sym_obj;
164}
165
166void
167gdbpy_initialize_symbols (void)
168{
169 symbol_object_type.tp_new = PyType_GenericNew;
170 if (PyType_Ready (&symbol_object_type) < 0)
171 return;
172
173 Py_INCREF (&symbol_object_type);
174 PyModule_AddObject (gdb_module, "Symbol", (PyObject *) &symbol_object_type);
175}
toggle raw diff

gdb/python/symtab.c

 
259259
260260 if (sal.symtab)
261261 {
262 symtab_obj = PyObject_New (symtab_object, &symtab_object_type);
262 symtab_obj = (symtab_object *) symtab_to_symtab_object (sal.symtab);
263263 if (symtab_obj == NULL)
264264 {
265 PyErr_SetString (PyExc_MemoryError,
266 "Could not allocate Symtab object.");
267265 Py_DECREF (sal_obj);
268
269266 return NULL;
270267 }
271268
282282 return (PyObject *) sal_obj;
283283}
284284
285PyObject *
286symtab_to_symtab_object (struct symtab *symtab)
287{
288 symtab_object *symtab_obj;
289
290 symtab_obj = PyObject_New (symtab_object, &symtab_object_type);
291 if (symtab_obj == NULL)
292 {
293 PyErr_SetString (PyExc_MemoryError,
294 "Could not allocate Symtab object.");
295
296 return NULL;
297 }
298
299 symtab_obj->symtab = symtab;
300
301 return (PyObject *) symtab_obj;
302}
303
285304void
286305gdbpy_initialize_symtabs (void)
287306{
toggle raw diff