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 97e902a07fcb31d5b2e0118069680cc80102d367

Expose block and symbol to Python.

Commit diff

gdb/Makefile.in

 
262262#
263263SUBDIR_PYTHON_OBS = \
264264 python.o \
265 python-block.o \
265266 python-breakpoint.o \
266267 python-cmd.o \
267268 python-frame.o \
268269 python-hooks.o \
270 python-symbol.o \
269271 python-value.o
270272SUBDIR_PYTHON_SRCS = \
271273 python/python.c \
274 python/block.c \
272275 python/breakpoint.c \
273276 python/cmd.c \
274277 python/frame.c \
275278 python/hooks.c \
279 python/symbol.c \
276280 python/value.c
277281SUBDIR_PYTHON_DEPS =
278282SUBDIR_PYTHON_LDFLAGS=
34253425 $(command_h) $(libiberty_h) $(cli_decode_h) $(charset_h) $(top_h) \
34263426 $(exceptions_h) $(python_internal_h) $(version_h) $(solib_h)
34273427 $(CC) -c $(INTERNAL_CFLAGS) $(PYTHON_CFLAGS) $(srcdir)/python/python.c
3428python-block.o: $(srcdir)/python/block.c $(defs_h) $(block_h) $(dictionary_h) \
3429 $(symtab_h) $(python_h) $(python_internal_h)
3430 $(CC) -c $(INTERNAL_CFLAGS) $(PYTHON_CFLAGS) \
3431 $(srcdir)/python/block.c -o python-block.o
34283432python-breakpoint.o: $(srcdir)/python/breakpoint.c $(defs_h) $(python_h) \
34293433 $(value_h) $(exceptions_h) $(python_internal_h) $(charset_h) \
34303434 $(breakpoint_h) $(gdbcmd_h)
34483448 $(charset_h) $(gdb_events_h) $(python_h) $(python_internal_h)
34493449 $(CC) -c $(INTERNAL_CFLAGS) $(PYTHON_CFLAGS) \
34503450 $(srcdir)/python/hooks.c -o python-hooks.o
3451python-symbol.o: $(srcdir)/python/symbol.c $(defs_h) $(symtab_h) $(python_h) \
3452 $(python_internal_h)
3453 $(CC) -c $(INTERNAL_CFLAGS) $(PYTHON_CFLAGS) \
3454 $(srcdir)/python/symbol.c -o python-symbol.o
34513455python-value.o: $(srcdir)/python/value.c $(defs_h) $(exceptions_h) \
34523456 $(python_internal_h) $(value_h)
34533457 $(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
52PyTypeObject 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
212struct block *
213block_object_to_block (PyObject *obj)
214{
215 return ((block_object *) obj)->block;
216}
217
218static PyObject *
219blpy_block_syms_iter (PyObject *self)
220{
221 return self;
222}
223
224static PyObject *
225blpy_block_syms_iternext (PyObject *self)
226{
227 block_syms_iterator_object *iter_obj = (block_syms_iterator_object *) self;
228 struct symbol *sym;
229
230 if (!iter_obj->initialized_p)
231 {
232 sym = dict_iterator_first (iter_obj->dict, &(iter_obj->iter));
233 iter_obj->initialized_p = 1;
234 }
235 else
236 sym = dict_iterator_next (&(iter_obj->iter));
237
238 return (sym == NULL)? NULL : symbol_to_symbol_object (sym);
239}
240
241void
242gdbpy_initialize_blocks (void)
243{
244 block_object_type.tp_new = PyType_GenericNew;
245 if (PyType_Ready (&block_object_type) < 0)
246 return;
247
248 block_syms_iterator_object_type.tp_new = PyType_GenericNew;
249 if (PyType_Ready (&block_syms_iterator_object_type) < 0)
250 return;
251
252 Py_INCREF (&block_object_type);
253 PyModule_AddObject (gdb_module, "Block", (PyObject *) &block_object_type);
254
255 Py_INCREF (&block_syms_iterator_object_type);
256 PyModule_AddObject (gdb_module, "BlockIterator",
257 (PyObject *) &block_syms_iterator_object_type);
258}
toggle raw diff

gdb/python/python-internal.h

 
2727#endif
2828
2929extern PyObject *gdb_module;
30extern PyTypeObject block_object_type;
3031extern PyTypeObject value_object_type;
32extern PyTypeObject symbol_object_type;
3133
3234PyObject *gdbpy_make_value_from_int (PyObject *self, PyObject *args);
3335PyObject *gdbpy_get_value_from_history (PyObject *self, PyObject *args);
4040PyObject *gdbpy_get_selected_frame (PyObject *self, PyObject *args);
4141
4242PyObject *variable_to_python (struct cmd_list_element *);
43PyObject *symbol_to_symbol_object (struct symbol *sym);
44PyObject *block_to_block_object (struct block *block);
4345PyObject *value_to_value_object (struct value *v);
4446PyObject *gdb_owned_value_to_value_object (struct value *v);
4547
48struct block *block_object_to_block (PyObject *obj);
49struct symbol *symbol_object_to_symbol (PyObject *obj);
4650struct value *value_object_to_value (PyObject *self);
4751
4852PyObject *gdbpy_get_hook_function (const char *);
5656void gdbpy_initialize_breakpoints (void);
5757void gdbpy_initialize_frames (void);
5858void gdbpy_initialize_commands (void);
59void gdbpy_initialize_symbols (void);
60void gdbpy_initialize_blocks (void);
5961
6062/* Use this after a TRY_EXCEPT to throw the appropriate Python
6163 exception. FIXME: throw different errors depending on
toggle raw diff

gdb/python/python.c

 
3636static PyObject *get_show_variable (PyObject *, PyObject *);
3737static PyObject *execute_gdb_command (PyObject *, PyObject *);
3838static PyObject *gdbpy_solib_address (PyObject *, PyObject *);
39static PyObject *gdbpy_find_pc_function (PyObject *, PyObject *);
3940
4041
4142void
7171 { "solib_address", gdbpy_solib_address, METH_VARARGS,
7272 "Return shared library holding a given address, or None." },
7373
74 { "find_pc_function", gdbpy_find_pc_function, METH_VARARGS,
75 "Return the function containing the given pc value, or None." },
76
7477 {NULL, NULL, 0, NULL}
7578 };
7679
9090 gdbpy_initialize_breakpoints ();
9191 gdbpy_initialize_frames ();
9292 gdbpy_initialize_commands ();
93 gdbpy_initialize_symbols ();
94 gdbpy_initialize_blocks ();
9395
9496 PyRun_SimpleString ("import gdb");
9597
371371 return str_obj;
372372}
373373
374PyObject *
375gdbpy_find_pc_function (PyObject *self, PyObject *args)
376{
377 unsigned long long pc;
378 struct symbol *sym;
379 PyObject *sym_obj;
380
381 if (!PyArg_ParseTuple (args, "K", &pc))
382 return NULL;
383
384 sym = find_pc_function (pc);
385 if (sym)
386 return symbol_to_symbol_object (sym);
387
388 Py_RETURN_NONE;
389}
390
374391
375392
376393void
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_natural_name (PyObject *self, PyObject *args);
32static PyObject *sympy_get_linkage_name (PyObject *self, PyObject *args);
33static PyObject *sympy_get_print_name (PyObject *self, PyObject *args);
34static PyObject *sympy_get_class (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_natural_name", sympy_get_natural_name, METH_NOARGS,
40 "Return the \"natural\" name of the symbol." },
41 { "get_linkage_name", sympy_get_linkage_name, METH_NOARGS,
42 "Return the name of the symbol as used by the linker." },
43 { "get_print_name", sympy_get_print_name, METH_NOARGS,
44 "Return the name of the symbol in a form suitable for output." },
45 { "get_class", sympy_get_class, METH_NOARGS,
46 "Return the class of the symbol." },
47 {NULL} /* Sentinel */
48};
49
50PyTypeObject 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_natural_name (PyObject *self, PyObject *args)
119{
120 symbol_object *self_sym = (symbol_object *) self;
121
122 return PyString_FromString (SYMBOL_NATURAL_NAME (self_sym->symbol));
123}
124
125static PyObject *
126sympy_get_linkage_name (PyObject *self, PyObject *args)
127{
128 symbol_object *self_sym = (symbol_object *) self;
129
130 return PyString_FromString (SYMBOL_LINKAGE_NAME (self_sym->symbol));
131}
132
133static PyObject *
134sympy_get_print_name (PyObject *self, PyObject *args)
135{
136 symbol_object *self_sym = (symbol_object *) self;
137
138 return PyString_FromString (SYMBOL_PRINT_NAME (self_sym->symbol));
139}
140
141static PyObject *
142sympy_get_class (PyObject *self, PyObject *args)
143{
144 symbol_object *self_sym = (symbol_object *) self;
145
146 return PyInt_FromLong (SYMBOL_CLASS (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
166struct symbol *
167symbol_object_to_symbol (PyObject *obj)
168{
169 return ((symbol_object *) obj)->symbol;
170}
171
172void
173gdbpy_initialize_symbols (void)
174{
175 symbol_object_type.tp_new = PyType_GenericNew;
176 if (PyType_Ready (&symbol_object_type) < 0)
177 return;
178
179 /* FIXME: These would probably be best exposed as class attributes of Symbol,
180 but I don't know how to do it except by messing with the type's dictionary.
181 That seems too messy. */
182 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNDEF", LOC_UNDEF);
183 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST", LOC_CONST);
184 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_STATIC", LOC_STATIC);
185 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGISTER", LOC_REGISTER);
186 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_ARG", LOC_ARG);
187 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REF_ARG", LOC_REF_ARG);
188 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGPARM", LOC_REGPARM);
189 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGPARM_ADDR",
190 LOC_REGPARM_ADDR);
191 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LOCAL", LOC_LOCAL);
192 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_TYPEDEF", LOC_TYPEDEF);
193 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LABEL", LOC_LABEL);
194 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_BLOCK", LOC_BLOCK);
195 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST_BYTES",
196 LOC_CONST_BYTES);
197 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LOCAL_ARG", LOC_LOCAL_ARG);
198 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_BASEREG", LOC_BASEREG);
199 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_BASEREG_ARG",
200 LOC_BASEREG_ARG);
201 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNRESOLVED", LOC_UNRESOLVED);
202 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_HP_THREAD_LOCAL_STATIC",
203 LOC_HP_THREAD_LOCAL_STATIC);
204 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_OPTIMIZED_OUT",
205 LOC_OPTIMIZED_OUT);
206 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_INDIRECT", LOC_INDIRECT);
207 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_COMPUTED", LOC_COMPUTED);
208 PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_COMPUTED_ARG",
209 LOC_COMPUTED_ARG);
210
211 PyModule_AddIntConstant (gdb_module, "SYMBOL_UNDEF_DOMAIN", UNDEF_DOMAIN);
212 PyModule_AddIntConstant (gdb_module, "SYMBOL_VAR_DOMAIN", VAR_DOMAIN);
213 PyModule_AddIntConstant (gdb_module, "SYMBOL_STRUCT_DOMAIN", STRUCT_DOMAIN);
214 PyModule_AddIntConstant (gdb_module, "SYMBOL_LABEL_DOMAIN", LABEL_DOMAIN);
215 PyModule_AddIntConstant (gdb_module, "SYMBOL_VARIABLES_DOMAIN",
216 VARIABLES_DOMAIN);
217 PyModule_AddIntConstant (gdb_module, "SYMBOL_FUNCTIONS_DOMAIN",
218 FUNCTIONS_DOMAIN);
219 PyModule_AddIntConstant (gdb_module, "SYMBOL_TYPES_DOMAIN", TYPES_DOMAIN);
220 PyModule_AddIntConstant (gdb_module, "SYMBOL_METHODS_DOMAIN", METHODS_DOMAIN);
221
222 Py_INCREF (&symbol_object_type);
223 PyModule_AddObject (gdb_module, "Symbol", (PyObject *) &symbol_object_type);
224}
toggle raw diff