From 7b282c5acc13f099b11a670de50fd52a0d81ea40 Mon Sep 17 00:00:00 2001 From: Siva Chandra Reddy Date: Thu, 22 Mar 2012 08:10:44 +0000 Subject: 2012-03-14 Siva Chandra Python scripting: Add new method Value.referenced_value to gdb.Value which can dereference pointer as well as reference values. * NEWS: Add entry under 'Python scripting' about the new method Value.referenced_value on gdb.Value objects. * python/py-value.c (valpy_referenced_value): New function defining a new method on gdb.Value objects which can dereference pointer and reference values. * testsuite/gdb.python/py-value.cc: Add test case for testing the methodology exposing C++ values to Python. * testsuite/gdb.python/py-value-cc.exp: Add tests testing the methodology exposing C++ values to Python. * testsuite/gdb.python/Makefile.in: Add py-value-cc to EXECUTABLES. * docs/gdb.texinfo (Python API/Values From Inferior): Add description about the new method Value.referenced_value. Add description on how Value.dereference is different (and similar) to Value.referenced_value. --- gdb/python/py-value.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'gdb/python') diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c index 44443e0..58513d8 100644 --- a/gdb/python/py-value.c +++ b/gdb/python/py-value.c @@ -192,6 +192,47 @@ valpy_dereference (PyObject *self, PyObject *args) return result; } +/* Given a value of a pointer type or a reference type, return the value + referenced. The difference between this function and valpy_dereference is + that the latter applies * unary operator to a value, which need not always + result in the value referenced. For example, for a value which is a reference + to an 'int' pointer ('int *'), valpy_dereference will result in a value of + type 'int' while valpy_referenced_value will result in a value of type + 'int *'. */ + +static PyObject * +valpy_referenced_value (PyObject *self, PyObject *args) +{ + volatile struct gdb_exception except; + PyObject *result = NULL; + + TRY_CATCH (except, RETURN_MASK_ALL) + { + struct value *self_val, *res_val; + struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ()); + + self_val = ((value_object *) self)->value; + switch (TYPE_CODE (check_typedef (value_type (self_val)))) + { + case TYPE_CODE_PTR: + res_val = value_ind (self_val); + break; + case TYPE_CODE_REF: + res_val = coerce_ref (self_val); + break; + default: + error(_("Trying to get the referenced value from a value which is " + "neither a pointer nor a reference.")); + } + + result = value_to_value_object (res_val); + do_cleanups (cleanup); + } + GDB_PY_HANDLE_EXCEPTION (except); + + return result; +} + /* Return "&value". */ static PyObject * valpy_get_address (PyObject *self, void *closure) @@ -1379,6 +1420,8 @@ Cast the value to the supplied type, as if by the C++\n\ reinterpret_cast operator." }, { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." }, + { "referenced_value", valpy_referenced_value, METH_NOARGS, + "Return the value referenced by a TYPE_CODE_REF or TYPE_CODE_PTR value." }, { "lazy_string", (PyCFunction) valpy_lazy_string, METH_VARARGS | METH_KEYWORDS, "lazy_string ([encoding] [, length]) -> lazy_string\n\ -- cgit v1.1