| |   |
| 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 | |
| 26 | typedef struct { |
| 27 | PyObject_HEAD |
| 28 | struct block *block; |
| 29 | } block_object; |
| 30 | |
| 31 | static PyObject *blpy_iter (PyObject *self); |
| 32 | static PyObject *blpy_itersymbols (PyObject *self, PyObject *args); |
| 33 | static PyObject *blpy_get_start (PyObject *self, PyObject *args); |
| 34 | static PyObject *blpy_get_end (PyObject *self, PyObject *args); |
| 35 | static PyObject *blpy_get_function (PyObject *self, PyObject *args); |
| 36 | static PyObject *blpy_get_superblock (PyObject *self, PyObject *args); |
| 37 | |
| 38 | static 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 | |
| 52 | static 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 | |
| 84 | typedef struct { |
| 85 | PyObject_HEAD |
| 86 | struct dictionary *dict; |
| 87 | struct dict_iterator iter; |
| 88 | int initialized_p; |
| 89 | } block_syms_iterator_object; |
| 90 | |
| 91 | static PyObject *blpy_block_syms_iter (PyObject *self); |
| 92 | static PyObject *blpy_block_syms_iternext (PyObject *self); |
| 93 | |
| 94 | static 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 | |
| 127 | static PyObject * |
| 128 | blpy_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 | |
| 147 | static PyObject * |
| 148 | blpy_itersymbols (PyObject *self, PyObject *args) |
| 149 | { |
| 150 | return blpy_iter (self); |
| 151 | } |
| 152 | |
| 153 | static PyObject * |
| 154 | blpy_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 | |
| 161 | static PyObject * |
| 162 | blpy_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 | |
| 169 | static PyObject * |
| 170 | blpy_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 | |
| 182 | static PyObject * |
| 183 | blpy_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 | |
| 195 | PyObject * |
| 196 | block_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 | |
| 212 | static PyObject * |
| 213 | blpy_block_syms_iter (PyObject *self) |
| 214 | { |
| 215 | return self; |
| 216 | } |
| 217 | |
| 218 | static PyObject * |
| 219 | blpy_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 | |
| 235 | void |
| 236 | gdbpy_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 |
--- /dev/null
+++ b/gdb/python/block.c
@@ -0,0 +1,252 @@
+/* Python interface to blocks.
+
+ Copyright (C) 2008 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "defs.h"
+#include "block.h"
+#include "dictionary.h"
+#include "symtab.h"
+#include "python-internal.h"
+
+typedef struct {
+ PyObject_HEAD
+ struct block *block;
+} block_object;
+
+static PyObject *blpy_iter (PyObject *self);
+static PyObject *blpy_itersymbols (PyObject *self, PyObject *args);
+static PyObject *blpy_get_start (PyObject *self, PyObject *args);
+static PyObject *blpy_get_end (PyObject *self, PyObject *args);
+static PyObject *blpy_get_function (PyObject *self, PyObject *args);
+static PyObject *blpy_get_superblock (PyObject *self, PyObject *args);
+
+static PyMethodDef block_object_methods[] = {
+ { "itersymbols", blpy_itersymbols, METH_NOARGS,
+ "Return an iterator to walk through the symbols in the block." },
+ { "get_start", blpy_get_start, METH_NOARGS,
+ "Return the start address of this block." },
+ { "get_end", blpy_get_end, METH_NOARGS,
+ "Return the end address of this block." },
+ { "get_function", blpy_get_function, METH_NOARGS,
+ "Return the symbol that names this block, or None." },
+ { "get_superblock", blpy_get_end, METH_NOARGS,
+ "Return the block containing this block, or None." },
+ {NULL} /* Sentinel */
+};
+
+static PyTypeObject block_object_type = {
+ PyObject_HEAD_INIT (NULL)
+ 0, /*ob_size*/
+ "gdb.Block", /*tp_name*/
+ sizeof (block_object), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ 0, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_compare*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash */
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ 0, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /*tp_flags*/
+ "GDB block object", /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ blpy_iter, /* tp_iter */
+ 0, /* tp_iternext */
+ block_object_methods /* tp_methods */
+};
+
+typedef struct {
+ PyObject_HEAD
+ struct dictionary *dict;
+ struct dict_iterator iter;
+ int initialized_p;
+} block_syms_iterator_object;
+
+static PyObject *blpy_block_syms_iter (PyObject *self);
+static PyObject *blpy_block_syms_iternext (PyObject *self);
+
+static PyTypeObject block_syms_iterator_object_type = {
+ PyObject_HEAD_INIT (NULL)
+ 0, /*ob_size*/
+ "gdb.BlockIterator", /*tp_name*/
+ sizeof (block_syms_iterator_object), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ 0, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_compare*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash */
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ 0, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /*tp_flags*/
+ "GDB block syms iterator object", /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ blpy_block_syms_iter, /* tp_iter */
+ blpy_block_syms_iternext, /* tp_iternext */
+ 0 /* tp_methods */
+};
+
+
+static PyObject *
+blpy_iter (PyObject *self)
+{
+ block_syms_iterator_object *block_iter_obj;
+
+ block_iter_obj = PyObject_New (block_syms_iterator_object,
+ &block_syms_iterator_object_type);
+ if (block_iter_obj == NULL)
+ {
+ PyErr_SetString (PyExc_MemoryError,
+ "Could not allocate iterator object.");
+ return NULL;
+ }
+
+ block_iter_obj->dict = BLOCK_DICT (((block_object *) self)->block);
+ block_iter_obj->initialized_p = 0;
+
+ return (PyObject *) block_iter_obj;
+}
+
+static PyObject *
+blpy_itersymbols (PyObject *self, PyObject *args)
+{
+ return blpy_iter (self);
+}
+
+static PyObject *
+blpy_get_start (PyObject *self, PyObject *args)
+{
+ block_object *self_block = (block_object *) self;
+
+ return PyLong_FromUnsignedLongLong (BLOCK_START (self_block->block));
+}
+
+static PyObject *
+blpy_get_end (PyObject *self, PyObject *args)
+{
+ block_object *self_block = (block_object *) self;
+
+ return PyLong_FromUnsignedLongLong (BLOCK_END (self_block->block));
+}
+
+static PyObject *
+blpy_get_function (PyObject *self, PyObject *args)
+{
+ block_object *self_block = (block_object *) self;
+ struct symbol *sym;
+
+ sym = BLOCK_FUNCTION (self_block->block);
+ if (sym)
+ return symbol_to_symbol_object (sym);
+
+ Py_RETURN_NONE;
+}
+
+static PyObject *
+blpy_get_superblock (PyObject *self, PyObject *args)
+{
+ block_object *self_block = (block_object *) self;
+ struct block *block;
+
+ block = BLOCK_SUPERBLOCK (self_block->block);
+ if (block)
+ return block_to_block_object (block);
+
+ Py_RETURN_NONE;
+}
+
+PyObject *
+block_to_block_object (struct block *block)
+{
+ block_object *block_obj;
+
+ block_obj = PyObject_New (block_object, &block_object_type);
+ if (block_obj == NULL)
+ {
+ PyErr_SetString (PyExc_MemoryError, "Could not allocate block object.");
+ return NULL;
+ }
+
+ block_obj->block = block;
+
+ return (PyObject *) block_obj;
+}
+
+static PyObject *
+blpy_block_syms_iter (PyObject *self)
+{
+ return self;
+}
+
+static PyObject *
+blpy_block_syms_iternext (PyObject *self)
+{
+ block_syms_iterator_object *iter_obj = (block_syms_iterator_object *) self;
+ struct symbol *sym;
+
+ if (!iter_obj->initialized_p)
+ {
+ sym = dict_iterator_first (iter_obj->dict, &(iter_obj->iter));
+ iter_obj->initialized_p = 1;
+ }
+ else
+ sym = dict_iterator_next (&(iter_obj->iter));
+
+ return (sym == NULL)? NULL : symbol_to_symbol_object (sym);
+}
+
+void
+gdbpy_initialize_blocks (void)
+{
+ block_object_type.tp_new = PyType_GenericNew;
+ if (PyType_Ready (&block_object_type) < 0)
+ return;
+
+ block_syms_iterator_object_type.tp_new = PyType_GenericNew;
+ if (PyType_Ready (&block_syms_iterator_object_type) < 0)
+ return;
+
+ Py_INCREF (&block_object_type);
+ PyModule_AddObject (gdb_module, "Block", (PyObject *) &block_object_type);
+
+ Py_INCREF (&block_syms_iterator_object_type);
+ PyModule_AddObject (gdb_module, "BlockIterator",
+ (PyObject *) &block_syms_iterator_object_type);
+} |
| |   |
| 52 | 52 | static PyObject *frapy_get_type (frame_object *self); |
| 53 | 53 | static PyObject *frapy_get_unwind_stop_reason (frame_object *self); |
| 54 | 54 | static PyObject *frapy_get_pc (frame_object *self); |
| 55 | static PyObject *frapy_get_address_in_block (PyObject *self, PyObject *args); |
| 55 | 56 | static PyObject *frapy_get_prev (frame_object *self); |
| 56 | 57 | static PyObject *frapy_get_next (frame_object *self); |
| 57 | 58 | static PyObject *frapy_find_sal (frame_object *self); |
| … | … | |
| 82 | 82 | METH_NOARGS, "Return the function name of the frame." }, |
| 83 | 83 | { "get_pc", (PyCFunction) frapy_get_pc, METH_NOARGS, |
| 84 | 84 | "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." }, |
| 85 | 87 | { "get_prev", (PyCFunction) frapy_get_prev, METH_NOARGS, |
| 86 | 88 | "Return the previous (outer) frame." }, |
| 87 | 89 | { "get_next", (PyCFunction) frapy_get_next, METH_NOARGS, |
| … | … | |
| 290 | 290 | return PyLong_FromUnsignedLongLong (pc); |
| 291 | 291 | } |
| 292 | 292 | |
| 293 | static PyObject * |
| 294 | frapy_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 | |
| 293 | 312 | static frame_object * |
| 294 | 313 | frame_info_to_frame_object (struct frame_info *frame) |
| 295 | 314 | { |
| toggle raw diff |
--- a/gdb/python/frame.c
+++ b/gdb/python/frame.c
@@ -52,6 +52,7 @@ static PyObject *frapy_get_name (frame_object *self);
static PyObject *frapy_get_type (frame_object *self);
static PyObject *frapy_get_unwind_stop_reason (frame_object *self);
static PyObject *frapy_get_pc (frame_object *self);
+static PyObject *frapy_get_address_in_block (PyObject *self, PyObject *args);
static PyObject *frapy_get_prev (frame_object *self);
static PyObject *frapy_get_next (frame_object *self);
static PyObject *frapy_find_sal (frame_object *self);
@@ -81,6 +82,8 @@ static PyMethodDef frame_object_methods[] = {
METH_NOARGS, "Return the function name of the frame." },
{ "get_pc", (PyCFunction) frapy_get_pc, METH_NOARGS,
"Return the frame's resume address." },
+ { "get_address_in_block", frapy_get_address_in_block, METH_NOARGS,
+ "Return an address which falls within the frame's code block." },
{ "get_prev", (PyCFunction) frapy_get_prev, METH_NOARGS,
"Return the previous (outer) frame." },
{ "get_next", (PyCFunction) frapy_get_next, METH_NOARGS,
@@ -287,6 +290,25 @@ frapy_get_pc (frame_object *self)
return PyLong_FromUnsignedLongLong (pc);
}
+static PyObject *
+frapy_get_address_in_block (PyObject *self, PyObject *args)
+{
+ frame_object *frame_obj = (frame_object *) self;
+ CORE_ADDR pc = 0; /* Initialize to appease gcc warning. */
+ struct frame_info *frame;
+ volatile struct gdb_exception except;
+
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ FRAPY_REQUIRE_VALID (frame_obj, frame);
+
+ pc = get_frame_address_in_block (frame);
+ }
+ GDB_PY_HANDLE_EXCEPTION (except);
+
+ return PyLong_FromUnsignedLongLong (pc);
+}
+
static frame_object *
frame_info_to_frame_object (struct frame_info *frame)
{ |
| |   |
| 44 | 44 | static PyObject *gdbpy_version (PyObject *, PyObject *); |
| 45 | 45 | static PyObject *gdbpy_host_conf (PyObject *, PyObject *); |
| 46 | 46 | static PyObject *gdbpy_target_conf (PyObject *, PyObject *); |
| 47 | static PyObject *gdbpy_find_pc_function (PyObject *, PyObject *); |
| 47 | 48 | |
| 48 | 49 | |
| 49 | 50 | void |
| … | … | |
| 77 | 77 | { "solib_address", gdbpy_solib_address, METH_VARARGS, |
| 78 | 78 | "Return shared library holding a given address, or None." }, |
| 79 | 79 | |
| 80 | { "find_pc_function", gdbpy_find_pc_function, METH_VARARGS, |
| 81 | "Return the function containing the given pc value, or None." }, |
| 82 | |
| 80 | 83 | { "decode_line", gdbpy_decode_line, METH_VARARGS, |
| 81 | 84 | "Decode a string argument the way that 'break' or 'edit' does.\n\ |
| 82 | 85 | Return a tuple holding the file name (or None) and line number (or None).\n\ |
| … | … | |
| 105 | 105 | gdbpy_initialize_frames (); |
| 106 | 106 | gdbpy_initialize_symtabs (); |
| 107 | 107 | gdbpy_initialize_commands (); |
| 108 | gdbpy_initialize_symbols (); |
| 109 | gdbpy_initialize_blocks (); |
| 108 | 110 | |
| 109 | 111 | PyRun_SimpleString ("import gdb"); |
| 110 | 112 | |
| … | … | |
| 495 | 495 | } |
| 496 | 496 | |
| 497 | 497 | PyObject * |
| 498 | gdbpy_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 | |
| 514 | PyObject * |
| 498 | 515 | gdbpy_decode_line (PyObject *self, PyObject *args) |
| 499 | 516 | { |
| 500 | 517 | struct symtabs_and_lines sals = { NULL, 0 }; /* Initialize to appease gcc. */ |
| toggle raw diff |
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -44,6 +44,7 @@ static PyObject *gdbpy_decode_line (PyObject *, PyObject *);
static PyObject *gdbpy_version (PyObject *, PyObject *);
static PyObject *gdbpy_host_conf (PyObject *, PyObject *);
static PyObject *gdbpy_target_conf (PyObject *, PyObject *);
+static PyObject *gdbpy_find_pc_function (PyObject *, PyObject *);
void
@@ -76,6 +77,9 @@ demand_python ()
{ "solib_address", gdbpy_solib_address, METH_VARARGS,
"Return shared library holding a given address, or None." },
+ { "find_pc_function", gdbpy_find_pc_function, METH_VARARGS,
+ "Return the function containing the given pc value, or None." },
+
{ "decode_line", gdbpy_decode_line, METH_VARARGS,
"Decode a string argument the way that 'break' or 'edit' does.\n\
Return a tuple holding the file name (or None) and line number (or None).\n\
@@ -101,6 +105,8 @@ Note: may later change to return an object." },
gdbpy_initialize_frames ();
gdbpy_initialize_symtabs ();
gdbpy_initialize_commands ();
+ gdbpy_initialize_symbols ();
+ gdbpy_initialize_blocks ();
PyRun_SimpleString ("import gdb");
@@ -489,6 +495,23 @@ gdbpy_solib_address (PyObject *self, PyObject *args)
}
PyObject *
+gdbpy_find_pc_function (PyObject *self, PyObject *args)
+{
+ unsigned long long pc;
+ struct symbol *sym;
+ PyObject *sym_obj;
+
+ if (!PyArg_ParseTuple (args, "K", &pc))
+ return NULL;
+
+ sym = find_pc_function (pc);
+ if (sym)
+ return symbol_to_symbol_object (sym);
+
+ Py_RETURN_NONE;
+}
+
+PyObject *
gdbpy_decode_line (PyObject *self, PyObject *args)
{
struct symtabs_and_lines sals = { NULL, 0 }; /* Initialize to appease gcc. */ |
| |   |
| 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 | |
| 24 | typedef struct { |
| 25 | PyObject_HEAD |
| 26 | struct symbol *symbol; |
| 27 | } symbol_object; |
| 28 | |
| 29 | static PyObject *sympy_str (PyObject *self); |
| 30 | static PyObject *sympy_get_value (PyObject *self, PyObject *args); |
| 31 | static PyObject *sympy_get_symtab (PyObject *self, PyObject *args); |
| 32 | static PyObject *sympy_get_natural_name (PyObject *self, PyObject *args); |
| 33 | static PyObject *sympy_get_linkage_name (PyObject *self, PyObject *args); |
| 34 | static PyObject *sympy_get_print_name (PyObject *self, PyObject *args); |
| 35 | |
| 36 | static 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 | |
| 50 | static 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 | |
| 83 | static PyObject * |
| 84 | sympy_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 | |
| 101 | static PyObject * |
| 102 | sympy_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 | |
| 117 | static PyObject * |
| 118 | sympy_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 | |
| 125 | static PyObject * |
| 126 | sympy_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 | |
| 133 | static PyObject * |
| 134 | sympy_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 | |
| 141 | static PyObject * |
| 142 | sympy_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 | |
| 149 | PyObject * |
| 150 | symbol_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 | |
| 166 | void |
| 167 | gdbpy_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 |
--- /dev/null
+++ b/gdb/python/symbol.c
@@ -0,0 +1,175 @@
+/* Python interface to symbols.
+
+ Copyright (C) 2008 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "defs.h"
+#include "symtab.h"
+#include "python-internal.h"
+
+typedef struct {
+ PyObject_HEAD
+ struct symbol *symbol;
+} symbol_object;
+
+static PyObject *sympy_str (PyObject *self);
+static PyObject *sympy_get_value (PyObject *self, PyObject *args);
+static PyObject *sympy_get_symtab (PyObject *self, PyObject *args);
+static PyObject *sympy_get_natural_name (PyObject *self, PyObject *args);
+static PyObject *sympy_get_linkage_name (PyObject *self, PyObject *args);
+static PyObject *sympy_get_print_name (PyObject *self, PyObject *args);
+
+static PyMethodDef symbol_object_methods[] = {
+ { "get_value", sympy_get_value, METH_NOARGS,
+ "Return the value of the symbol." },
+ { "get_symtab", sympy_get_symtab, METH_NOARGS,
+ "Return the value of the symbol." },
+ { "get_natural_name", sympy_get_natural_name, METH_NOARGS,
+ "Return the \"natural\" name of the symbol." },
+ { "get_linkage_name", sympy_get_linkage_name, METH_NOARGS,
+ "Return the name of the symbol as used by the linker." },
+ { "get_print_name", sympy_get_print_name, METH_NOARGS,
+ "Return the name of the symbol in a form suitable for output." },
+ {NULL} /* Sentinel */
+};
+
+static PyTypeObject symbol_object_type = {
+ PyObject_HEAD_INIT (NULL)
+ 0, /*ob_size*/
+ "gdb.Symbol", /*tp_name*/
+ sizeof (symbol_object), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ 0, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_compare*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash */
+ 0, /*tp_call*/
+ sympy_str, /*tp_str*/
+ 0, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT, /*tp_flags*/
+ "GDB symbol object", /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ symbol_object_methods /* tp_methods */
+};
+
+
+static PyObject *
+sympy_str (PyObject *self)
+{
+ int ret;
+ char *s;
+ PyObject *result;
+
+ ret = asprintf (&s, "symbol for %s",
+ SYMBOL_PRINT_NAME (((symbol_object *) self)->symbol));
+ if (ret < 0)
+ Py_RETURN_NONE;
+
+ result = PyString_FromString (s);
+ xfree (s);
+
+ return result;
+}
+
+static PyObject *
+sympy_get_value (PyObject *self, PyObject *args)
+{
+ symbol_object *self_sym = (symbol_object *) self;
+
+ switch (SYMBOL_CLASS (self_sym->symbol))
+ {
+ case LOC_BLOCK:
+ return block_to_block_object (SYMBOL_BLOCK_VALUE (self_sym->symbol));
+ }
+
+ PyErr_SetString (PyExc_NotImplementedError,
+ "Symbol type not yet supported in Python scripts.");
+ return NULL;
+}
+
+static PyObject *
+sympy_get_symtab (PyObject *self, PyObject *args)
+{
+ symbol_object *self_sym = (symbol_object *) self;
+
+ return symtab_to_symtab_object (SYMBOL_SYMTAB (self_sym->symbol));
+}
+
+static PyObject *
+sympy_get_natural_name (PyObject *self, PyObject *args)
+{
+ symbol_object *self_sym = (symbol_object *) self;
+
+ return PyString_FromString (SYMBOL_NATURAL_NAME (self_sym->symbol));
+}
+
+static PyObject *
+sympy_get_linkage_name (PyObject *self, PyObject *args)
+{
+ symbol_object *self_sym = (symbol_object *) self;
+
+ return PyString_FromString (SYMBOL_LINKAGE_NAME (self_sym->symbol));
+}
+
+static PyObject *
+sympy_get_print_name (PyObject *self, PyObject *args)
+{
+ symbol_object *self_sym = (symbol_object *) self;
+
+ return PyString_FromString (SYMBOL_PRINT_NAME (self_sym->symbol));
+}
+
+PyObject *
+symbol_to_symbol_object (struct symbol *sym)
+{
+ symbol_object *sym_obj;
+
+ sym_obj = PyObject_New (symbol_object, &symbol_object_type);
+ if (sym_obj == NULL)
+ {
+ PyErr_SetString (PyExc_MemoryError, "Could not allocate symbol object.");
+ return NULL;
+ }
+
+ sym_obj->symbol = sym;
+
+ return (PyObject *) sym_obj;
+}
+
+void
+gdbpy_initialize_symbols (void)
+{
+ symbol_object_type.tp_new = PyType_GenericNew;
+ if (PyType_Ready (&symbol_object_type) < 0)
+ return;
+
+ Py_INCREF (&symbol_object_type);
+ PyModule_AddObject (gdb_module, "Symbol", (PyObject *) &symbol_object_type);
+} |