[Patch] PR python/15464 and python/16113
Siva Chandra
sivachandra@google.com
Mon Dec 30 14:27:00 GMT 2013
Attached is a newer version of the patch which an 'xfree' moved to a
more appropriate place.
2013-12-30 Siva Chandra Reddy <sivachandra@google.com>
PR python/15464
PR python/16133
* valops.c (value_struct_elt_bitpos): New function
* python/py-value.c (valpy_getitem): Use 'bitpos' attribute to
look for a field when 'name' is 'None' or empty.
testsuite/
* gdb.python/py-value-cc.cc: Enhance test case.
* gdb.python/py-value-cc.exp: Add new tests.
-------------- next part --------------
diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index df25179..69194b0 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -573,6 +573,7 @@ valpy_getitem (PyObject *self, PyObject *key)
value_object *self_value = (value_object *) self;
char *field = NULL;
PyObject *base_class_type_object = NULL;
+ long bitpos = -1;
volatile struct gdb_exception except;
PyObject *result = NULL;
@@ -614,10 +615,41 @@ valpy_getitem (PyObject *self, PyObject *key)
if (name_obj == NULL)
return NULL;
- field = python_string_to_host_string (name_obj);
- Py_DECREF (name_obj);
- if (field == NULL)
- return NULL;
+ if (name_obj != Py_None)
+ {
+ field = python_string_to_host_string (name_obj);
+ Py_DECREF (name_obj);
+ name_obj = NULL;
+ if (field == NULL)
+ return NULL;
+ }
+
+ if (name_obj == Py_None || field[0] == '\0')
+ {
+ PyObject *bitpos_obj;
+ int valid;
+
+ Py_XDECREF (name_obj);
+ xfree (field);
+ field = NULL;
+
+ if (!PyObject_HasAttrString (key, "bitpos"))
+ {
+ PyErr_SetString (PyExc_AttributeError,
+ _("gdb.Field object has no name and no "
+ "'bitpos' attribute."));
+
+ return NULL;
+ }
+ bitpos_obj = PyObject_GetAttrString (key, "bitpos");
+ if (bitpos_obj == NULL)
+ return NULL;
+
+ valid = gdb_py_int_as_long (bitpos_obj, &bitpos);
+ Py_DECREF (bitpos_obj);
+ if (!valid)
+ return NULL;
+ }
}
}
@@ -629,6 +661,8 @@ valpy_getitem (PyObject *self, PyObject *key)
if (field)
res_val = value_struct_elt (&tmp, NULL, field, 0, NULL);
+ else if (bitpos >= 0)
+ res_val = value_struct_elt_bitpos (&tmp, bitpos, "struct/class/union");
else if (base_class_type_object != NULL)
{
struct type *base_class_type, *val_type;
diff --git a/gdb/testsuite/gdb.python/py-value-cc.cc b/gdb/testsuite/gdb.python/py-value-cc.cc
index 80094ec..8b9aa5a 100644
--- a/gdb/testsuite/gdb.python/py-value-cc.cc
+++ b/gdb/testsuite/gdb.python/py-value-cc.cc
@@ -30,8 +30,15 @@ class B : public A {
char a;
};
+struct X
+{
+ union { int x; char y; };
+ union { int a; char b; };
+};
+
typedef B Btd;
typedef int *int_ptr;
+typedef X Xtd;
int
func (const A &a)
@@ -57,6 +64,13 @@ func (const A &a)
U u;
u.a = 99;
+ X x;
+ x.x = 101;
+ x.a = 102;
+
+ X *x_ptr = &x;
+ Xtd *xtd = &x;
+
return 0; /* Break here. */
}
diff --git a/gdb/testsuite/gdb.python/py-value-cc.exp b/gdb/testsuite/gdb.python/py-value-cc.exp
index eacaf2e..08dc462 100644
--- a/gdb/testsuite/gdb.python/py-value-cc.exp
+++ b/gdb/testsuite/gdb.python/py-value-cc.exp
@@ -53,6 +53,12 @@ gdb_test_no_output "python b_ref = gdb.parse_and_eval('b_ref')" "init b_ref"
gdb_test_no_output "python b_td = gdb.parse_and_eval('b_td')" "init b_td"
gdb_test_no_output "python u = gdb.parse_and_eval('u')" "init u"
gdb_test_no_output "python u_fields = u.type.fields()" "init u_fields"
+gdb_test_no_output "python x = gdb.parse_and_eval('x')" "init x"
+gdb_test_no_output "python x_fields = x.type.fields()" "init x_fields"
+gdb_test_no_output "python x_ptr = gdb.parse_and_eval('x_ptr')" "init x_ptr"
+gdb_test_no_output "python xtd = gdb.parse_and_eval('xtd')" "init xtd"
+
+gdb_test "python print(b\[b_fields\[1\]\])" "97 'a'" "b.a via field"
gdb_test "python print(b\[b_fields\[1\]\])" "97 'a'" "b.a via field"
gdb_test "python print(b\[b_fields\[0\]\].type)" "A" \
@@ -79,3 +85,11 @@ gdb_test "python print(b_td\[b_fields\[0\]\]\['a'\])" "100" \
gdb_test "python print(u\[u_fields\[0\]\])" "99.*" "u's first field via field"
gdb_test "python print(u\[u_fields\[1\]\])" "99.*" "u's second field via field"
+
+gdb_test "python print len(x_fields)" "2" "number for fields in u"
+gdb_test "python print x\[x_fields\[0\]\]\['x'\]" "101" "x.x via field"
+gdb_test "python print x\[x_fields\[1\]\]\['a'\]" "102" "x.a via field"
+gdb_test "python print x_ptr\[x_fields\[0\]\]\['x'\]" "101" "x_ptr->x via field"
+gdb_test "python print x_ptr\[x_fields\[1\]\]\['a'\]" "102" "x_ptr->a via field"
+gdb_test "python print xtd\[x_fields\[0\]\]\['x'\]" "101" "xtd->x via field"
+gdb_test "python print xtd\[x_fields\[1\]\]\['a'\]" "102" "xtd->a via field"
diff --git a/gdb/valops.c b/gdb/valops.c
index d43c758..f147854 100644
--- a/gdb/valops.c
+++ b/gdb/valops.c
@@ -2248,6 +2248,49 @@ value_struct_elt (struct value **argp, struct value **args,
return v;
}
+/* Given *ARGP, a value of type structure or union, or a pointer/reference
+ to a structure or union, extract and return its component (field) at the
+ specified BITPOS.
+ Returns NULL if BITPOS is invalid. */
+
+struct value *
+value_struct_elt_bitpos (struct value **argp, int bitpos, const char *err)
+{
+ struct type *t;
+ struct value *v;
+ int i;
+ int nbases;
+
+ *argp = coerce_array (*argp);
+
+ t = check_typedef (value_type (*argp));
+
+ while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
+ {
+ *argp = value_ind (*argp);
+ if (TYPE_CODE (check_typedef (value_type (*argp))) != TYPE_CODE_FUNC)
+ *argp = coerce_array (*argp);
+ t = check_typedef (value_type (*argp));
+ }
+
+ if (TYPE_CODE (t) != TYPE_CODE_STRUCT
+ && TYPE_CODE (t) != TYPE_CODE_UNION)
+ error (_("Attempt to extract a component of a value that is not a %s."),
+ err);
+
+ for (i = TYPE_N_BASECLASSES (t); i < TYPE_NFIELDS (t); i++)
+ {
+ if (!field_is_static (&TYPE_FIELD (t, i))
+ && bitpos == TYPE_FIELD_BITPOS (t, i))
+ return value_primitive_field (*argp, 0, i, t);
+ }
+
+ error (_("Attempt to extract a component with an invalid bitpos."));
+
+ /* Never hit. */
+ return NULL;
+}
+
/* Search through the methods of an object (and its bases) to find a
specified method. Return the pointer to the fn_field list of
overloaded instances.
diff --git a/gdb/value.h b/gdb/value.h
index 6b158df..a279803 100644
--- a/gdb/value.h
+++ b/gdb/value.h
@@ -670,6 +670,10 @@ extern struct value *value_struct_elt (struct value **argp,
const char *name, int *static_memfuncp,
const char *err);
+extern struct value *value_struct_elt_bitpos (struct value **argp,
+ int bitpos,
+ const char *err);
+
extern struct value *value_aggregate_elt (struct type *curtype,
char *name,
struct type *expect_type,
More information about the Gdb-patches
mailing list