| |   |
| 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 | 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 | struct block * |
| 213 | block_object_to_block (PyObject *obj) |
| 214 | { |
| 215 | return ((block_object *) obj)->block; |
| 216 | } |
| 217 | |
| 218 | static PyObject * |
| 219 | blpy_block_syms_iter (PyObject *self) |
| 220 | { |
| 221 | return self; |
| 222 | } |
| 223 | |
| 224 | static PyObject * |
| 225 | blpy_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 | |
| 241 | void |
| 242 | gdbpy_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 |
--- /dev/null
+++ b/gdb/python/block.c
@@ -0,0 +1,258 @@
+/* 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 */
+};
+
+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;
+}
+
+struct block *
+block_object_to_block (PyObject *obj)
+{
+ return ((block_object *) obj)->block;
+}
+
+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);
+} |
| |   |
| 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_natural_name (PyObject *self, PyObject *args); |
| 32 | static PyObject *sympy_get_linkage_name (PyObject *self, PyObject *args); |
| 33 | static PyObject *sympy_get_print_name (PyObject *self, PyObject *args); |
| 34 | static PyObject *sympy_get_class (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_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 | |
| 50 | 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_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 | |
| 125 | static PyObject * |
| 126 | sympy_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 | |
| 133 | static PyObject * |
| 134 | sympy_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 | |
| 141 | static PyObject * |
| 142 | sympy_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 | |
| 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 | struct symbol * |
| 167 | symbol_object_to_symbol (PyObject *obj) |
| 168 | { |
| 169 | return ((symbol_object *) obj)->symbol; |
| 170 | } |
| 171 | |
| 172 | void |
| 173 | gdbpy_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 |
--- /dev/null
+++ b/gdb/python/symbol.c
@@ -0,0 +1,224 @@
+/* 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_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 PyObject *sympy_get_class (PyObject *self, PyObject *args);
+
+static PyMethodDef symbol_object_methods[] = {
+ { "get_value", sympy_get_value, 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." },
+ { "get_class", sympy_get_class, METH_NOARGS,
+ "Return the class of the symbol." },
+ {NULL} /* Sentinel */
+};
+
+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_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));
+}
+
+static PyObject *
+sympy_get_class (PyObject *self, PyObject *args)
+{
+ symbol_object *self_sym = (symbol_object *) self;
+
+ return PyInt_FromLong (SYMBOL_CLASS (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;
+}
+
+struct symbol *
+symbol_object_to_symbol (PyObject *obj)
+{
+ return ((symbol_object *) obj)->symbol;
+}
+
+void
+gdbpy_initialize_symbols (void)
+{
+ symbol_object_type.tp_new = PyType_GenericNew;
+ if (PyType_Ready (&symbol_object_type) < 0)
+ return;
+
+ /* FIXME: These would probably be best exposed as class attributes of Symbol,
+ but I don't know how to do it except by messing with the type's dictionary.
+ That seems too messy. */
+ PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNDEF", LOC_UNDEF);
+ PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST", LOC_CONST);
+ PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_STATIC", LOC_STATIC);
+ PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGISTER", LOC_REGISTER);
+ PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_ARG", LOC_ARG);
+ PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REF_ARG", LOC_REF_ARG);
+ PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGPARM", LOC_REGPARM);
+ PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGPARM_ADDR",
+ LOC_REGPARM_ADDR);
+ PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LOCAL", LOC_LOCAL);
+ PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_TYPEDEF", LOC_TYPEDEF);
+ PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LABEL", LOC_LABEL);
+ PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_BLOCK", LOC_BLOCK);
+ PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST_BYTES",
+ LOC_CONST_BYTES);
+ PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LOCAL_ARG", LOC_LOCAL_ARG);
+ PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_BASEREG", LOC_BASEREG);
+ PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_BASEREG_ARG",
+ LOC_BASEREG_ARG);
+ PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNRESOLVED", LOC_UNRESOLVED);
+ PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_HP_THREAD_LOCAL_STATIC",
+ LOC_HP_THREAD_LOCAL_STATIC);
+ PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_OPTIMIZED_OUT",
+ LOC_OPTIMIZED_OUT);
+ PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_INDIRECT", LOC_INDIRECT);
+ PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_COMPUTED", LOC_COMPUTED);
+ PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_COMPUTED_ARG",
+ LOC_COMPUTED_ARG);
+
+ PyModule_AddIntConstant (gdb_module, "SYMBOL_UNDEF_DOMAIN", UNDEF_DOMAIN);
+ PyModule_AddIntConstant (gdb_module, "SYMBOL_VAR_DOMAIN", VAR_DOMAIN);
+ PyModule_AddIntConstant (gdb_module, "SYMBOL_STRUCT_DOMAIN", STRUCT_DOMAIN);
+ PyModule_AddIntConstant (gdb_module, "SYMBOL_LABEL_DOMAIN", LABEL_DOMAIN);
+ PyModule_AddIntConstant (gdb_module, "SYMBOL_VARIABLES_DOMAIN",
+ VARIABLES_DOMAIN);
+ PyModule_AddIntConstant (gdb_module, "SYMBOL_FUNCTIONS_DOMAIN",
+ FUNCTIONS_DOMAIN);
+ PyModule_AddIntConstant (gdb_module, "SYMBOL_TYPES_DOMAIN", TYPES_DOMAIN);
+ PyModule_AddIntConstant (gdb_module, "SYMBOL_METHODS_DOMAIN", METHODS_DOMAIN);
+
+ Py_INCREF (&symbol_object_type);
+ PyModule_AddObject (gdb_module, "Symbol", (PyObject *) &symbol_object_type);
+} |