| |   |
| 56 | 56 | static PyObject *frapy_get_address_in_block (PyObject *self, PyObject *args); |
| 57 | 57 | static PyObject *frapy_get_prev (PyObject *self, PyObject *args); |
| 58 | 58 | static PyObject *frapy_get_next (PyObject *self, PyObject *args); |
| 59 | static PyObject *frapy_find_sal (PyObject *self, PyObject *args); |
| 60 | static PyObject *frapy_read_var_value (PyObject *self, PyObject *args); |
| 59 | 61 | |
| 60 | 62 | #define FRAPY_REQUIRE_VALID(frame_obj, frame) \ |
| 61 | 63 | do { \ |
| … | … | |
| 86 | 86 | { "get_prev", frapy_get_prev, METH_NOARGS, |
| 87 | 87 | "Return the previous (outer) frame." }, |
| 88 | 88 | { "get_next", frapy_get_next, METH_NOARGS, "Return the next (inner) frame." }, |
| 89 | { "find_sal", frapy_find_sal, METH_NOARGS, |
| 90 | "Return the frame's symtab and line." }, |
| 91 | { "read_var_value", frapy_read_var_value, METH_VARARGS, |
| 92 | "Return the value of the variable in this frame." }, |
| 89 | 93 | {NULL} /* Sentinel */ |
| 90 | 94 | }; |
| 91 | 95 | |
| … | … | |
| 409 | 409 | return next_obj; |
| 410 | 410 | } |
| 411 | 411 | |
| 412 | static PyObject * |
| 413 | frapy_find_sal (PyObject *self, PyObject *args) |
| 414 | { |
| 415 | struct frame_info *frame; |
| 416 | struct symtab_and_line sal; |
| 417 | volatile struct gdb_exception except; |
| 418 | PyObject *sal_obj = NULL; /* Initialize to appease gcc warning. */ |
| 419 | |
| 420 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 421 | { |
| 422 | FRAPY_REQUIRE_VALID ((frame_object *) self, frame); |
| 423 | |
| 424 | find_frame_sal (frame, &sal); |
| 425 | sal_obj = symtab_and_line_to_sal_object (sal); |
| 426 | } |
| 427 | GDB_PY_HANDLE_EXCEPTION (except); |
| 428 | |
| 429 | return sal_obj; |
| 430 | } |
| 431 | |
| 432 | static PyObject * |
| 433 | frapy_read_var_value (PyObject *self, PyObject *args) |
| 434 | { |
| 435 | struct frame_info *frame; |
| 436 | PyObject *sym_obj; |
| 437 | struct symbol *var; |
| 438 | struct value *val = NULL; |
| 439 | volatile struct gdb_exception except; |
| 440 | |
| 441 | if (!PyArg_ParseTuple (args, "O!", &symbol_object_type, &sym_obj)) |
| 442 | return NULL; |
| 443 | |
| 444 | var = symbol_object_to_symbol (sym_obj); |
| 445 | |
| 446 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 447 | { |
| 448 | FRAPY_REQUIRE_VALID ((frame_object *) self, frame); |
| 449 | |
| 450 | val = read_var_value (var, frame); |
| 451 | } |
| 452 | GDB_PY_HANDLE_EXCEPTION (except); |
| 453 | |
| 454 | if (val) |
| 455 | return value_to_value_object (val); |
| 456 | |
| 457 | Py_RETURN_NONE; |
| 458 | } |
| 459 | |
| 412 | 460 | PyObject * |
| 413 | 461 | gdbpy_get_frames (PyObject *self, PyObject *args) |
| 414 | 462 | { |
| toggle raw diff |
--- a/gdb/python/frame.c
+++ b/gdb/python/frame.c
@@ -56,6 +56,8 @@ static PyObject *frapy_get_pc (PyObject *self, PyObject *args);
static PyObject *frapy_get_address_in_block (PyObject *self, PyObject *args);
static PyObject *frapy_get_prev (PyObject *self, PyObject *args);
static PyObject *frapy_get_next (PyObject *self, PyObject *args);
+static PyObject *frapy_find_sal (PyObject *self, PyObject *args);
+static PyObject *frapy_read_var_value (PyObject *self, PyObject *args);
#define FRAPY_REQUIRE_VALID(frame_obj, frame) \
do { \
@@ -84,6 +86,10 @@ static PyMethodDef frame_object_methods[] = {
{ "get_prev", frapy_get_prev, METH_NOARGS,
"Return the previous (outer) frame." },
{ "get_next", frapy_get_next, METH_NOARGS, "Return the next (inner) frame." },
+ { "find_sal", frapy_find_sal, METH_NOARGS,
+ "Return the frame's symtab and line." },
+ { "read_var_value", frapy_read_var_value, METH_VARARGS,
+ "Return the value of the variable in this frame." },
{NULL} /* Sentinel */
};
@@ -403,6 +409,54 @@ frapy_get_next (PyObject *self, PyObject *args)
return next_obj;
}
+static PyObject *
+frapy_find_sal (PyObject *self, PyObject *args)
+{
+ struct frame_info *frame;
+ struct symtab_and_line sal;
+ volatile struct gdb_exception except;
+ PyObject *sal_obj = NULL; /* Initialize to appease gcc warning. */
+
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ FRAPY_REQUIRE_VALID ((frame_object *) self, frame);
+
+ find_frame_sal (frame, &sal);
+ sal_obj = symtab_and_line_to_sal_object (sal);
+ }
+ GDB_PY_HANDLE_EXCEPTION (except);
+
+ return sal_obj;
+}
+
+static PyObject *
+frapy_read_var_value (PyObject *self, PyObject *args)
+{
+ struct frame_info *frame;
+ PyObject *sym_obj;
+ struct symbol *var;
+ struct value *val = NULL;
+ volatile struct gdb_exception except;
+
+ if (!PyArg_ParseTuple (args, "O!", &symbol_object_type, &sym_obj))
+ return NULL;
+
+ var = symbol_object_to_symbol (sym_obj);
+
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ FRAPY_REQUIRE_VALID ((frame_object *) self, frame);
+
+ val = read_var_value (var, frame);
+ }
+ GDB_PY_HANDLE_EXCEPTION (except);
+
+ if (val)
+ return value_to_value_object (val);
+
+ Py_RETURN_NONE;
+}
+
PyObject *
gdbpy_get_frames (PyObject *self, PyObject *args)
{ |
| |   |
| 27 | 27 | #include "solib.h" |
| 28 | 28 | #include "exceptions.h" |
| 29 | 29 | #include "python-internal.h" |
| 30 | #include "linespec.h" |
| 31 | #include "symtab.h" |
| 32 | #include "source.h" |
| 30 | 33 | #include "version.h" |
| 31 | 34 | |
| 32 | 35 | #include <ctype.h> |
| … | … | |
| 39 | 39 | static PyObject *get_show_variable (PyObject *, PyObject *); |
| 40 | 40 | static PyObject *execute_gdb_command (PyObject *, PyObject *); |
| 41 | 41 | static PyObject *gdbpy_solib_address (PyObject *, PyObject *); |
| 42 | static PyObject *gdbpy_decode_line (PyObject *, PyObject *); |
| 42 | 43 | static PyObject *gdbpy_find_pc_function (PyObject *, PyObject *); |
| 43 | 44 | |
| 44 | 45 | |
| … | … | |
| 72 | 72 | { "frame_stop_reason_string", gdbpy_frame_stop_reason_string, |
| 73 | 73 | METH_VARARGS, "Return a string explaining unwind stop reason" }, |
| 74 | 74 | |
| 75 | { "lookup_symbol", gdbpy_lookup_symbol, METH_VARARGS, |
| 76 | "Return the symbol corresponding to the given name, or None." }, |
| 75 | 77 | { "solib_address", gdbpy_solib_address, METH_VARARGS, |
| 76 | 78 | "Return shared library holding a given address, or None." }, |
| 77 | 79 | |
| 78 | 80 | { "find_pc_function", gdbpy_find_pc_function, METH_VARARGS, |
| 79 | 81 | "Return the function containing the given pc value, or None." }, |
| 80 | 82 | |
| 83 | { "decode_line", gdbpy_decode_line, METH_VARARGS, |
| 84 | "Decode a string argument the way that 'break' or 'edit' does.\n\ |
| 85 | Return a tuple holding the file name (or None) and line number (or None).\n\ |
| 86 | Note: may later change to return an object." }, |
| 87 | |
| 81 | 88 | {NULL, NULL, 0, NULL} |
| 82 | 89 | }; |
| 83 | 90 | |
| … | … | |
| 100 | 100 | gdbpy_initialize_values (); |
| 101 | 101 | gdbpy_initialize_breakpoints (); |
| 102 | 102 | gdbpy_initialize_frames (); |
| 103 | gdbpy_initialize_symtabs (); |
| 103 | 104 | gdbpy_initialize_commands (); |
| 104 | 105 | gdbpy_initialize_symbols (); |
| 105 | 106 | gdbpy_initialize_blocks (); |
| … | … | |
| 400 | 400 | Py_RETURN_NONE; |
| 401 | 401 | } |
| 402 | 402 | |
| 403 | PyObject * |
| 404 | gdbpy_decode_line (PyObject *self, PyObject *args) |
| 405 | { |
| 406 | struct symtabs_and_lines sals = { NULL, 0 }; /* Initialize to appease gcc. */ |
| 407 | struct symtab_and_line sal; |
| 408 | char *arg = NULL; |
| 409 | int free_sals = 0, i; |
| 410 | PyObject *result = NULL; |
| 411 | volatile struct gdb_exception except; |
| 412 | |
| 413 | if (! PyArg_ParseTuple (args, "|s", &arg)) |
| 414 | return NULL; |
| 415 | |
| 416 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 417 | { |
| 418 | if (arg) |
| 419 | { |
| 420 | char *copy; |
| 421 | |
| 422 | arg = strdup (arg); |
| 423 | copy = arg; |
| 424 | |
| 425 | sals = decode_line_1 (©, 0, 0, 0, 0, 0); |
| 426 | free_sals = 1; |
| 427 | } |
| 428 | else |
| 429 | { |
| 430 | set_default_source_symtab_and_line (); |
| 431 | sal = get_current_source_symtab_and_line (); |
| 432 | sals.sals = &sal; |
| 433 | sals.nelts = 1; |
| 434 | } |
| 435 | } |
| 436 | if (arg) |
| 437 | xfree (arg); |
| 438 | |
| 439 | if (except.reason < 0) |
| 440 | { |
| 441 | if (free_sals) |
| 442 | xfree (sals.sals); |
| 443 | /* We know this will always throw. */ |
| 444 | GDB_PY_HANDLE_EXCEPTION (except); |
| 445 | } |
| 446 | |
| 447 | if (sals.nelts) |
| 448 | { |
| 449 | result = PyTuple_New (sals.nelts); |
| 450 | for (i = 0; i < sals.nelts; ++i) |
| 451 | { |
| 452 | PyObject *obj; |
| 453 | char *str; |
| 454 | |
| 455 | obj = symtab_and_line_to_sal_object (sals.sals[i]); |
| 456 | if (! obj) |
| 457 | { |
| 458 | Py_DECREF (result); |
| 459 | result = NULL; |
| 460 | break; |
| 461 | } |
| 462 | |
| 463 | PyTuple_SetItem (result, i, obj); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | if (free_sals) |
| 468 | xfree (sals.sals); |
| 469 | |
| 470 | if (result) |
| 471 | return result; |
| 472 | Py_RETURN_NONE; |
| 473 | } |
| 474 | |
| 403 | 475 | |
| 404 | 476 | |
| 405 | 477 | void |
| toggle raw diff |
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -27,6 +27,9 @@
#include "solib.h"
#include "exceptions.h"
#include "python-internal.h"
+#include "linespec.h"
+#include "symtab.h"
+#include "source.h"
#include "version.h"
#include <ctype.h>
@@ -36,6 +39,7 @@ PyObject *gdb_module;
static PyObject *get_show_variable (PyObject *, PyObject *);
static PyObject *execute_gdb_command (PyObject *, PyObject *);
static PyObject *gdbpy_solib_address (PyObject *, PyObject *);
+static PyObject *gdbpy_decode_line (PyObject *, PyObject *);
static PyObject *gdbpy_find_pc_function (PyObject *, PyObject *);
@@ -68,12 +72,19 @@ demand_python ()
{ "frame_stop_reason_string", gdbpy_frame_stop_reason_string,
METH_VARARGS, "Return a string explaining unwind stop reason" },
+ { "lookup_symbol", gdbpy_lookup_symbol, METH_VARARGS,
+ "Return the symbol corresponding to the given name, or None." },
{ "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\
+Note: may later change to return an object." },
+
{NULL, NULL, 0, NULL}
};
@@ -89,6 +100,7 @@ demand_python ()
gdbpy_initialize_values ();
gdbpy_initialize_breakpoints ();
gdbpy_initialize_frames ();
+ gdbpy_initialize_symtabs ();
gdbpy_initialize_commands ();
gdbpy_initialize_symbols ();
gdbpy_initialize_blocks ();
@@ -388,6 +400,78 @@ gdbpy_find_pc_function (PyObject *self, PyObject *args)
Py_RETURN_NONE;
}
+PyObject *
+gdbpy_decode_line (PyObject *self, PyObject *args)
+{
+ struct symtabs_and_lines sals = { NULL, 0 }; /* Initialize to appease gcc. */
+ struct symtab_and_line sal;
+ char *arg = NULL;
+ int free_sals = 0, i;
+ PyObject *result = NULL;
+ volatile struct gdb_exception except;
+
+ if (! PyArg_ParseTuple (args, "|s", &arg))
+ return NULL;
+
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ if (arg)
+ {
+ char *copy;
+
+ arg = strdup (arg);
+ copy = arg;
+
+ sals = decode_line_1 (©, 0, 0, 0, 0, 0);
+ free_sals = 1;
+ }
+ else
+ {
+ set_default_source_symtab_and_line ();
+ sal = get_current_source_symtab_and_line ();
+ sals.sals = &sal;
+ sals.nelts = 1;
+ }
+ }
+ if (arg)
+ xfree (arg);
+
+ if (except.reason < 0)
+ {
+ if (free_sals)
+ xfree (sals.sals);
+ /* We know this will always throw. */
+ GDB_PY_HANDLE_EXCEPTION (except);
+ }
+
+ if (sals.nelts)
+ {
+ result = PyTuple_New (sals.nelts);
+ for (i = 0; i < sals.nelts; ++i)
+ {
+ PyObject *obj;
+ char *str;
+
+ obj = symtab_and_line_to_sal_object (sals.sals[i]);
+ if (! obj)
+ {
+ Py_DECREF (result);
+ result = NULL;
+ break;
+ }
+
+ PyTuple_SetItem (result, i, obj);
+ }
+ }
+
+ if (free_sals)
+ xfree (sals.sals);
+
+ if (result)
+ return result;
+ Py_RETURN_NONE;
+}
+
void |
| |   |
| 28 | 28 | |
| 29 | 29 | static PyObject *sympy_str (PyObject *self); |
| 30 | 30 | static PyObject *sympy_get_value (PyObject *self, PyObject *args); |
| 31 | static PyObject *sympy_get_symtab (PyObject *self, PyObject *args); |
| 31 | 32 | static PyObject *sympy_get_natural_name (PyObject *self, PyObject *args); |
| 32 | 33 | static PyObject *sympy_get_linkage_name (PyObject *self, PyObject *args); |
| 33 | 34 | static PyObject *sympy_get_print_name (PyObject *self, PyObject *args); |
| … | … | |
| 37 | 37 | static PyMethodDef symbol_object_methods[] = { |
| 38 | 38 | { "get_value", sympy_get_value, METH_NOARGS, |
| 39 | 39 | "Return the value of the symbol." }, |
| 40 | { "get_symtab", sympy_get_symtab, METH_NOARGS, |
| 41 | "Return the value of the symbol." }, |
| 40 | 42 | { "get_natural_name", sympy_get_natural_name, METH_NOARGS, |
| 41 | 43 | "Return the \"natural\" name of the symbol." }, |
| 42 | 44 | { "get_linkage_name", sympy_get_linkage_name, METH_NOARGS, |
| … | … | |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | static PyObject * |
| 121 | sympy_get_symtab (PyObject *self, PyObject *args) |
| 122 | { |
| 123 | symbol_object *self_sym = (symbol_object *) self; |
| 124 | |
| 125 | return symtab_to_symtab_object (SYMBOL_SYMTAB (self_sym->symbol)); |
| 126 | } |
| 127 | |
| 128 | static PyObject * |
| 121 | 129 | sympy_get_natural_name (PyObject *self, PyObject *args) |
| 122 | 130 | { |
| 123 | 131 | symbol_object *self_sym = (symbol_object *) self; |
| … | … | |
| 180 | 180 | return ((symbol_object *) obj)->symbol; |
| 181 | 181 | } |
| 182 | 182 | |
| 183 | /* This function has less arguments than its C counterpart, to simplify the |
| 184 | Python interface: name, block and domain. The other two arguments are always |
| 185 | assumed to be set, and a tuple with 3 elements is always returned. The first |
| 186 | is the symbol object or None, the second is a boolean with the value of |
| 187 | is_a_field_of_this, and the third is the symbol object or None. */ |
| 188 | PyObject *gdbpy_lookup_symbol (PyObject *self, PyObject *args) |
| 189 | { |
| 190 | int domain, is_a_field_of_this = 0; |
| 191 | const char *name; |
| 192 | struct symbol *symbol; |
| 193 | struct symtab *symtab; |
| 194 | PyObject *block_obj, *ret_tuple, *sym_obj, *symtab_obj, *bool_obj; |
| 195 | |
| 196 | PyArg_ParseTuple (args, "sO!i", &name, &block_object_type, &block_obj, |
| 197 | &domain); |
| 198 | |
| 199 | symbol = lookup_symbol (name, block_object_to_block (block_obj), domain, |
| 200 | &is_a_field_of_this, &symtab); |
| 201 | |
| 202 | ret_tuple = PyTuple_New (3); |
| 203 | if (!ret_tuple) |
| 204 | { |
| 205 | PyErr_SetString (PyExc_MemoryError, "Could not allocate tuple object."); |
| 206 | return NULL; |
| 207 | } |
| 208 | |
| 209 | if (symbol) |
| 210 | { |
| 211 | sym_obj = symbol_to_symbol_object (symbol); |
| 212 | if (!sym_obj) |
| 213 | { |
| 214 | Py_DECREF (ret_tuple); |
| 215 | return NULL; |
| 216 | } |
| 217 | } |
| 218 | else |
| 219 | { |
| 220 | sym_obj = Py_None; |
| 221 | Py_INCREF (Py_None); |
| 222 | } |
| 223 | PyTuple_SET_ITEM (ret_tuple, 0, sym_obj); |
| 224 | |
| 225 | bool_obj = is_a_field_of_this? Py_True : Py_False; |
| 226 | Py_INCREF (bool_obj); |
| 227 | PyTuple_SET_ITEM (ret_tuple, 1, bool_obj); |
| 228 | |
| 229 | if (symtab) |
| 230 | { |
| 231 | symtab_obj = symtab_to_symtab_object (symtab); |
| 232 | if (!symtab_obj) |
| 233 | { |
| 234 | /* I *think* this will take care of decref'ing sym_obj and |
| 235 | bool_obj. */ |
| 236 | Py_DECREF (ret_tuple); |
| 237 | return NULL; |
| 238 | } |
| 239 | } |
| 240 | else |
| 241 | { |
| 242 | symtab_obj = Py_None; |
| 243 | Py_INCREF (Py_None); |
| 244 | } |
| 245 | PyTuple_SET_ITEM (ret_tuple, 2, symtab_obj); |
| 246 | |
| 247 | return ret_tuple; |
| 248 | } |
| 249 | |
| 183 | 250 | void |
| 184 | 251 | gdbpy_initialize_symbols (void) |
| 185 | 252 | { |
| toggle raw diff |
--- a/gdb/python/symbol.c
+++ b/gdb/python/symbol.c
@@ -28,6 +28,7 @@ typedef struct {
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);
@@ -36,6 +37,8 @@ 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_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,
@@ -115,6 +118,14 @@ sympy_get_value (PyObject *self, PyObject *args)
}
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;
@@ -169,6 +180,73 @@ symbol_object_to_symbol (PyObject *obj)
return ((symbol_object *) obj)->symbol;
}
+/* This function has less arguments than its C counterpart, to simplify the
+ Python interface: name, block and domain. The other two arguments are always
+ assumed to be set, and a tuple with 3 elements is always returned. The first
+ is the symbol object or None, the second is a boolean with the value of
+ is_a_field_of_this, and the third is the symbol object or None. */
+PyObject *gdbpy_lookup_symbol (PyObject *self, PyObject *args)
+{
+ int domain, is_a_field_of_this = 0;
+ const char *name;
+ struct symbol *symbol;
+ struct symtab *symtab;
+ PyObject *block_obj, *ret_tuple, *sym_obj, *symtab_obj, *bool_obj;
+
+ PyArg_ParseTuple (args, "sO!i", &name, &block_object_type, &block_obj,
+ &domain);
+
+ symbol = lookup_symbol (name, block_object_to_block (block_obj), domain,
+ &is_a_field_of_this, &symtab);
+
+ ret_tuple = PyTuple_New (3);
+ if (!ret_tuple)
+ {
+ PyErr_SetString (PyExc_MemoryError, "Could not allocate tuple object.");
+ return NULL;
+ }
+
+ if (symbol)
+ {
+ sym_obj = symbol_to_symbol_object (symbol);
+ if (!sym_obj)
+ {
+ Py_DECREF (ret_tuple);
+ return NULL;
+ }
+ }
+ else
+ {
+ sym_obj = Py_None;
+ Py_INCREF (Py_None);
+ }
+ PyTuple_SET_ITEM (ret_tuple, 0, sym_obj);
+
+ bool_obj = is_a_field_of_this? Py_True : Py_False;
+ Py_INCREF (bool_obj);
+ PyTuple_SET_ITEM (ret_tuple, 1, bool_obj);
+
+ if (symtab)
+ {
+ symtab_obj = symtab_to_symtab_object (symtab);
+ if (!symtab_obj)
+ {
+ /* I *think* this will take care of decref'ing sym_obj and
+ bool_obj. */
+ Py_DECREF (ret_tuple);
+ return NULL;
+ }
+ }
+ else
+ {
+ symtab_obj = Py_None;
+ Py_INCREF (Py_None);
+ }
+ PyTuple_SET_ITEM (ret_tuple, 2, symtab_obj);
+
+ return ret_tuple;
+}
+
void
gdbpy_initialize_symbols (void)
{ |
| |   |
| 1 | /* Python interface to symbol tables. |
| 2 | |
| 3 | Copyright (C) 2008 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GDB. |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include "defs.h" |
| 21 | #include "charset.h" |
| 22 | #include "symtab.h" |
| 23 | #include "source.h" |
| 24 | #include "python-internal.h" |
| 25 | |
| 26 | typedef struct { |
| 27 | PyObject_HEAD |
| 28 | struct symtab *symtab; |
| 29 | } symtab_object; |
| 30 | |
| 31 | static PyObject *stpy_str (PyObject *self); |
| 32 | static PyObject *stpy_filename (PyObject *self, PyObject *args); |
| 33 | static PyObject *stpy_to_fullname (PyObject *self, PyObject *args); |
| 34 | |
| 35 | static PyMethodDef symtab_object_methods[] = { |
| 36 | { "get_filename", stpy_filename, METH_NOARGS, |
| 37 | "Return the symtab's source filename." }, |
| 38 | { "to_fullname", stpy_to_fullname, METH_NOARGS, |
| 39 | "Return the symtab's full source filename." }, |
| 40 | {NULL} /* Sentinel */ |
| 41 | }; |
| 42 | |
| 43 | static PyTypeObject symtab_object_type = { |
| 44 | PyObject_HEAD_INIT (NULL) |
| 45 | 0, /*ob_size*/ |
| 46 | "gdb.Symtab", /*tp_name*/ |
| 47 | sizeof (symtab_object), /*tp_basicsize*/ |
| 48 | 0, /*tp_itemsize*/ |
| 49 | 0, /*tp_dealloc*/ |
| 50 | 0, /*tp_print*/ |
| 51 | 0, /*tp_getattr*/ |
| 52 | 0, /*tp_setattr*/ |
| 53 | 0, /*tp_compare*/ |
| 54 | 0, /*tp_repr*/ |
| 55 | 0, /*tp_as_number*/ |
| 56 | 0, /*tp_as_sequence*/ |
| 57 | 0, /*tp_as_mapping*/ |
| 58 | 0, /*tp_hash */ |
| 59 | 0, /*tp_call*/ |
| 60 | stpy_str, /*tp_str*/ |
| 61 | 0, /*tp_getattro*/ |
| 62 | 0, /*tp_setattro*/ |
| 63 | 0, /*tp_as_buffer*/ |
| 64 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 65 | "GDB symtab object", /* tp_doc */ |
| 66 | 0, /* tp_traverse */ |
| 67 | 0, /* tp_clear */ |
| 68 | 0, /* tp_richcompare */ |
| 69 | 0, /* tp_weaklistoffset */ |
| 70 | 0, /* tp_iter */ |
| 71 | 0, /* tp_iternext */ |
| 72 | symtab_object_methods /* tp_methods */ |
| 73 | }; |
| 74 | |
| 75 | typedef struct { |
| 76 | PyObject_HEAD |
| 77 | symtab_object *symtab; |
| 78 | struct symtab_and_line *sal; |
| 79 | } sal_object; |
| 80 | |
| 81 | static void salpy_dealloc (PyObject *self); |
| 82 | static int salpy_setsymtab (PyObject *self, PyObject *value, void *closure); |
| 83 | static PyObject *salpy_str (PyObject *self); |
| 84 | static PyObject *salpy_getsymtab (PyObject *self, void *closure); |
| 85 | static PyObject *salpy_pc (PyObject *self, PyObject *args); |
| 86 | static PyObject *salpy_line (PyObject *self, PyObject *args); |
| 87 | |
| 88 | static PyGetSetDef sal_object_getseters[] = { |
| 89 | { "symtab", salpy_getsymtab, salpy_setsymtab, "Symtab object.", NULL }, |
| 90 | {NULL} /* Sentinel */ |
| 91 | }; |
| 92 | |
| 93 | static PyMethodDef sal_object_methods[] = { |
| 94 | { "get_pc", salpy_pc, METH_NOARGS, |
| 95 | "Return the symtab_and_line's pc." }, |
| 96 | { "get_line", salpy_line, METH_NOARGS, |
| 97 | "Return the symtab_and_line's line." }, |
| 98 | {NULL} /* Sentinel */ |
| 99 | }; |
| 100 | |
| 101 | static PyTypeObject sal_object_type = { |
| 102 | PyObject_HEAD_INIT (NULL) |
| 103 | 0, /*ob_size*/ |
| 104 | "gdb.Symtab_and_line", /*tp_name*/ |
| 105 | sizeof (sal_object), /*tp_basicsize*/ |
| 106 | 0, /*tp_itemsize*/ |
| 107 | salpy_dealloc, /*tp_dealloc*/ |
| 108 | 0, /*tp_print*/ |
| 109 | 0, /*tp_getattr*/ |
| 110 | 0, /*tp_setattr*/ |
| 111 | 0, /*tp_compare*/ |
| 112 | 0, /*tp_repr*/ |
| 113 | 0, /*tp_as_number*/ |
| 114 | 0, /*tp_as_sequence*/ |
| 115 | 0, /*tp_as_mapping*/ |
| 116 | 0, /*tp_hash */ |
| 117 | 0, /*tp_call*/ |
| 118 | salpy_str, /*tp_str*/ |
| 119 | 0, /*tp_getattro*/ |
| 120 | 0, /*tp_setattro*/ |
| 121 | 0, /*tp_as_buffer*/ |
| 122 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 123 | "GDB symtab_and_line object", /* tp_doc */ |
| 124 | 0, /* tp_traverse */ |
| 125 | 0, /* tp_clear */ |
| 126 | 0, /* tp_richcompare */ |
| 127 | 0, /* tp_weaklistoffset */ |
| 128 | 0, /* tp_iter */ |
| 129 | 0, /* tp_iternext */ |
| 130 | sal_object_methods, /* tp_methods */ |
| 131 | 0, /* tp_members */ |
| 132 | sal_object_getseters /* tp_getset */ |
| 133 | }; |
| 134 | |
| 135 | |
| 136 | static PyObject * |
| 137 | stpy_str (PyObject *self) |
| 138 | { |
| 139 | int ret; |
| 140 | char *s; |
| 141 | PyObject *result; |
| 142 | |
| 143 | ret = asprintf (&s, "symbol table for %s", |
| 144 | ((symtab_object *) self)->symtab->filename); |
| 145 | if (ret < 0) |
| 146 | Py_RETURN_NONE; |
| 147 | |
| 148 | result = PyString_FromString (s); |
| 149 | xfree (s); |
| 150 | |
| 151 | return result; |
| 152 | } |
| 153 | |
| 154 | /* FIXME: maybe this should be an attribute instead of a method? */ |
| 155 | static PyObject * |
| 156 | stpy_filename (PyObject *self, PyObject *args) |
| 157 | { |
| 158 | symtab_object *self_symtab = (symtab_object *) self; |
| 159 | PyObject *str_obj; |
| 160 | |
| 161 | /* FIXME: Can symtab->filename really be NULL? */ |
| 162 | if (self_symtab->symtab->filename) |
| 163 | str_obj = PyString_Decode (self_symtab->symtab->filename, |
| 164 | strlen (self_symtab->symtab->filename), |
| 165 | host_charset (), NULL /* FIXME */); |
| 166 | else |
| 167 | { |
| 168 | str_obj = Py_None; |
| 169 | Py_INCREF (Py_None); |
| 170 | } |
| 171 | |
| 172 | return str_obj; |
| 173 | } |
| 174 | |
| 175 | static PyObject * |
| 176 | stpy_to_fullname (PyObject *self, PyObject *args) |
| 177 | { |
| 178 | char *fullname; |
| 179 | |
| 180 | fullname = symtab_to_fullname (((symtab_object *) self)->symtab); |
| 181 | if (fullname) |
| 182 | return PyString_Decode (fullname, strlen (fullname), host_charset (), |
| 183 | NULL /* FIXME */); |
| 184 | |
| 185 | Py_RETURN_NONE; |
| 186 | } |
| 187 | |
| 188 | static PyObject * |
| 189 | salpy_str (PyObject *self) |
| 190 | { |
| 191 | int ret; |
| 192 | char *s, *filename; |
| 193 | sal_object *sal_obj; |
| 194 | PyObject *result; |
| 195 | |
| 196 | sal_obj = (sal_object *) self; |
| 197 | filename = (sal_obj->symtab == (symtab_object *) Py_None)? "<unknown>" : |
| 198 | sal_obj->symtab->symtab->filename; |
| 199 | ret = asprintf (&s, "symbol and line for %s, line %d", filename, |
| 200 | sal_obj->sal->line); |
| 201 | if (ret < 0) |
| 202 | Py_RETURN_NONE; |
| 203 | |
| 204 | result = PyString_FromString (s); |
| 205 | xfree (s); |
| 206 | |
| 207 | return result; |
| 208 | } |
| 209 | |
| 210 | static PyObject * |
| 211 | salpy_pc (PyObject *self, PyObject *args) |
| 212 | { |
| 213 | return PyLong_FromUnsignedLongLong (((sal_object *) self)->sal->pc); |
| 214 | } |
| 215 | |
| 216 | static PyObject * |
| 217 | salpy_line (PyObject *self, PyObject *args) |
| 218 | { |
| 219 | return PyLong_FromUnsignedLongLong (((sal_object *) self)->sal->line); |
| 220 | } |
| 221 | |
| 222 | static PyObject * |
| 223 | salpy_getsymtab (PyObject *self, void *closure) |
| 224 | { |
| 225 | sal_object *self_sal = (sal_object *) self; |
| 226 | |
| 227 | Py_INCREF (self_sal->symtab); |
| 228 | |
| 229 | return (PyObject *) self_sal->symtab; |
| 230 | } |
| 231 | |
| 232 | static int |
| 233 | salpy_setsymtab (PyObject *self, PyObject *value, void *closure) |
| 234 | { |
| 235 | PyErr_SetString (PyExc_TypeError, "The symtab attribute can't be modified."); |
| 236 | |
| 237 | return -1; |
| 238 | } |
| 239 | |
| 240 | static void |
| 241 | salpy_dealloc (PyObject *self) |
| 242 | { |
| 243 | sal_object *self_sal = (sal_object *) self; |
| 244 | |
| 245 | Py_DECREF (self_sal->symtab); |
| 246 | xfree (self_sal->sal); |
| 247 | self_sal->ob_type->tp_free (self); |
| 248 | } |
| 249 | |
| 250 | PyObject * |
| 251 | symtab_and_line_to_sal_object (struct symtab_and_line sal) |
| 252 | { |
| 253 | sal_object *sal_obj; |
| 254 | symtab_object *symtab_obj; |
| 255 | |
| 256 | sal_obj = PyObject_New (sal_object, &sal_object_type); |
| 257 | if (sal_obj == NULL) |
| 258 | { |
| 259 | PyErr_SetString (PyExc_MemoryError, |
| 260 | "Could not allocate Symtab_and_line object."); |
| 261 | return NULL; |
| 262 | } |
| 263 | |
| 264 | if (sal.symtab) |
| 265 | { |
| 266 | symtab_obj = (symtab_object *) symtab_to_symtab_object (sal.symtab); |
| 267 | if (symtab_obj == NULL) |
| 268 | { |
| 269 | Py_DECREF (sal_obj); |
| 270 | return NULL; |
| 271 | } |
| 272 | |
| 273 | symtab_obj->symtab = sal.symtab; |
| 274 | } |
| 275 | else |
| 276 | { |
| 277 | symtab_obj = (symtab_object *) Py_None; |
| 278 | Py_INCREF (Py_None); |
| 279 | } |
| 280 | |
| 281 | sal_obj->sal = (struct symtab_and_line *) |
| 282 | xmalloc (sizeof (struct symtab_and_line)); |
| 283 | *(sal_obj->sal) = sal; |
| 284 | sal_obj->symtab = symtab_obj; |
| 285 | |
| 286 | return (PyObject *) sal_obj; |
| 287 | } |
| 288 | |
| 289 | PyObject * |
| 290 | symtab_to_symtab_object (struct symtab *symtab) |
| 291 | { |
| 292 | symtab_object *symtab_obj; |
| 293 | |
| 294 | symtab_obj = PyObject_New (symtab_object, &symtab_object_type); |
| 295 | if (symtab_obj == NULL) |
| 296 | { |
| 297 | PyErr_SetString (PyExc_MemoryError, |
| 298 | "Could not allocate Symtab object."); |
| 299 | |
| 300 | return NULL; |
| 301 | } |
| 302 | |
| 303 | symtab_obj->symtab = symtab; |
| 304 | |
| 305 | return (PyObject *) symtab_obj; |
| 306 | } |
| 307 | |
| 308 | void |
| 309 | gdbpy_initialize_symtabs (void) |
| 310 | { |
| 311 | symtab_object_type.tp_new = PyType_GenericNew; |
| 312 | if (PyType_Ready (&symtab_object_type) < 0) |
| 313 | return; |
| 314 | |
| 315 | sal_object_type.tp_new = PyType_GenericNew; |
| 316 | if (PyType_Ready (&sal_object_type) < 0) |
| 317 | return; |
| 318 | |
| 319 | Py_INCREF (&symtab_object_type); |
| 320 | PyModule_AddObject (gdb_module, "Symtab", (PyObject *) &symtab_object_type); |
| 321 | |
| 322 | Py_INCREF (&sal_object_type); |
| 323 | PyModule_AddObject (gdb_module, "Symtab_and_line", |
| 324 | (PyObject *) &sal_object_type); |
| 325 | } |
| toggle raw diff |
--- /dev/null
+++ b/gdb/python/symtab.c
@@ -0,0 +1,325 @@
+/* Python interface to symbol tables.
+
+ 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 "charset.h"
+#include "symtab.h"
+#include "source.h"
+#include "python-internal.h"
+
+typedef struct {
+ PyObject_HEAD
+ struct symtab *symtab;
+} symtab_object;
+
+static PyObject *stpy_str (PyObject *self);
+static PyObject *stpy_filename (PyObject *self, PyObject *args);
+static PyObject *stpy_to_fullname (PyObject *self, PyObject *args);
+
+static PyMethodDef symtab_object_methods[] = {
+ { "get_filename", stpy_filename, METH_NOARGS,
+ "Return the symtab's source filename." },
+ { "to_fullname", stpy_to_fullname, METH_NOARGS,
+ "Return the symtab's full source filename." },
+ {NULL} /* Sentinel */
+};
+
+static PyTypeObject symtab_object_type = {
+ PyObject_HEAD_INIT (NULL)
+ 0, /*ob_size*/
+ "gdb.Symtab", /*tp_name*/
+ sizeof (symtab_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*/
+ stpy_str, /*tp_str*/
+ 0, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT, /*tp_flags*/
+ "GDB symtab object", /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ symtab_object_methods /* tp_methods */
+};
+
+typedef struct {
+ PyObject_HEAD
+ symtab_object *symtab;
+ struct symtab_and_line *sal;
+} sal_object;
+
+static void salpy_dealloc (PyObject *self);
+static int salpy_setsymtab (PyObject *self, PyObject *value, void *closure);
+static PyObject *salpy_str (PyObject *self);
+static PyObject *salpy_getsymtab (PyObject *self, void *closure);
+static PyObject *salpy_pc (PyObject *self, PyObject *args);
+static PyObject *salpy_line (PyObject *self, PyObject *args);
+
+static PyGetSetDef sal_object_getseters[] = {
+ { "symtab", salpy_getsymtab, salpy_setsymtab, "Symtab object.", NULL },
+ {NULL} /* Sentinel */
+};
+
+static PyMethodDef sal_object_methods[] = {
+ { "get_pc", salpy_pc, METH_NOARGS,
+ "Return the symtab_and_line's pc." },
+ { "get_line", salpy_line, METH_NOARGS,
+ "Return the symtab_and_line's line." },
+ {NULL} /* Sentinel */
+};
+
+static PyTypeObject sal_object_type = {
+ PyObject_HEAD_INIT (NULL)
+ 0, /*ob_size*/
+ "gdb.Symtab_and_line", /*tp_name*/
+ sizeof (sal_object), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ salpy_dealloc, /*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*/
+ salpy_str, /*tp_str*/
+ 0, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT, /*tp_flags*/
+ "GDB symtab_and_line object", /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ sal_object_methods, /* tp_methods */
+ 0, /* tp_members */
+ sal_object_getseters /* tp_getset */
+};
+
+
+static PyObject *
+stpy_str (PyObject *self)
+{
+ int ret;
+ char *s;
+ PyObject *result;
+
+ ret = asprintf (&s, "symbol table for %s",
+ ((symtab_object *) self)->symtab->filename);
+ if (ret < 0)
+ Py_RETURN_NONE;
+
+ result = PyString_FromString (s);
+ xfree (s);
+
+ return result;
+}
+
+/* FIXME: maybe this should be an attribute instead of a method? */
+static PyObject *
+stpy_filename (PyObject *self, PyObject *args)
+{
+ symtab_object *self_symtab = (symtab_object *) self;
+ PyObject *str_obj;
+
+ /* FIXME: Can symtab->filename really be NULL? */
+ if (self_symtab->symtab->filename)
+ str_obj = PyString_Decode (self_symtab->symtab->filename,
+ strlen (self_symtab->symtab->filename),
+ host_charset (), NULL /* FIXME */);
+ else
+ {
+ str_obj = Py_None;
+ Py_INCREF (Py_None);
+ }
+
+ return str_obj;
+}
+
+static PyObject *
+stpy_to_fullname (PyObject *self, PyObject *args)
+{
+ char *fullname;
+
+ fullname = symtab_to_fullname (((symtab_object *) self)->symtab);
+ if (fullname)
+ return PyString_Decode (fullname, strlen (fullname), host_charset (),
+ NULL /* FIXME */);
+
+ Py_RETURN_NONE;
+}
+
+static PyObject *
+salpy_str (PyObject *self)
+{
+ int ret;
+ char *s, *filename;
+ sal_object *sal_obj;
+ PyObject *result;
+
+ sal_obj = (sal_object *) self;
+ filename = (sal_obj->symtab == (symtab_object *) Py_None)? "<unknown>" :
+ sal_obj->symtab->symtab->filename;
+ ret = asprintf (&s, "symbol and line for %s, line %d", filename,
+ sal_obj->sal->line);
+ if (ret < 0)
+ Py_RETURN_NONE;
+
+ result = PyString_FromString (s);
+ xfree (s);
+
+ return result;
+}
+
+static PyObject *
+salpy_pc (PyObject *self, PyObject *args)
+{
+ return PyLong_FromUnsignedLongLong (((sal_object *) self)->sal->pc);
+}
+
+static PyObject *
+salpy_line (PyObject *self, PyObject *args)
+{
+ return PyLong_FromUnsignedLongLong (((sal_object *) self)->sal->line);
+}
+
+static PyObject *
+salpy_getsymtab (PyObject *self, void *closure)
+{
+ sal_object *self_sal = (sal_object *) self;
+
+ Py_INCREF (self_sal->symtab);
+
+ return (PyObject *) self_sal->symtab;
+}
+
+static int
+salpy_setsymtab (PyObject *self, PyObject *value, void *closure)
+{
+ PyErr_SetString (PyExc_TypeError, "The symtab attribute can't be modified.");
+
+ return -1;
+}
+
+static void
+salpy_dealloc (PyObject *self)
+{
+ sal_object *self_sal = (sal_object *) self;
+
+ Py_DECREF (self_sal->symtab);
+ xfree (self_sal->sal);
+ self_sal->ob_type->tp_free (self);
+}
+
+PyObject *
+symtab_and_line_to_sal_object (struct symtab_and_line sal)
+{
+ sal_object *sal_obj;
+ symtab_object *symtab_obj;
+
+ sal_obj = PyObject_New (sal_object, &sal_object_type);
+ if (sal_obj == NULL)
+ {
+ PyErr_SetString (PyExc_MemoryError,
+ "Could not allocate Symtab_and_line object.");
+ return NULL;
+ }
+
+ if (sal.symtab)
+ {
+ symtab_obj = (symtab_object *) symtab_to_symtab_object (sal.symtab);
+ if (symtab_obj == NULL)
+ {
+ Py_DECREF (sal_obj);
+ return NULL;
+ }
+
+ symtab_obj->symtab = sal.symtab;
+ }
+ else
+ {
+ symtab_obj = (symtab_object *) Py_None;
+ Py_INCREF (Py_None);
+ }
+
+ sal_obj->sal = (struct symtab_and_line *)
+ xmalloc (sizeof (struct symtab_and_line));
+ *(sal_obj->sal) = sal;
+ sal_obj->symtab = symtab_obj;
+
+ return (PyObject *) sal_obj;
+}
+
+PyObject *
+symtab_to_symtab_object (struct symtab *symtab)
+{
+ symtab_object *symtab_obj;
+
+ symtab_obj = PyObject_New (symtab_object, &symtab_object_type);
+ if (symtab_obj == NULL)
+ {
+ PyErr_SetString (PyExc_MemoryError,
+ "Could not allocate Symt |