* [RFC/Patch v4] Call overloaded operators to perform valid Python operations on struct/class values.
@ 2014-02-02 20:06 Siva Chandra
2014-02-02 20:59 ` Eli Zaretskii
2014-02-07 6:53 ` Doug Evans
0 siblings, 2 replies; 5+ messages in thread
From: Siva Chandra @ 2014-02-02 20:06 UTC (permalink / raw)
To: Doug Evans, Eli Zaretskii; +Cc: gdb-patches, Tom Tromey
[-- Attachment #1: Type: text/plain, Size: 1206 bytes --]
On Sat, Jan 25, 2014 at 10:45 AM, Doug Evans <dje@google.com> wrote:
> IWBN if the docs listed exactly which operations are supported.
I have added it now in the attached patch. Eli should probably take
another look.
> [I realize this patch relies on the user-defined method patch, so it
> can't go in just yet.]
Are you referring to the debug methods patch? If yes, I do think this
patch relies on that patch. This is an independent feature which would
make writing debug methods simpler (IMO).
2014-02-02 Siva Chandra Reddy <sivachandra@google.com>
Call overloaded operators to perform valid Python operations on
struct/class values.
* NEWS (Python Scripting): Add entry for this new feature.
* python/py-value.c (valpy_binop): Call value_x_binop for struct
and class values.
testsuite/
* gdb.python/py-value-cc.cc: Improve test case to enable testing
operations on gdb.Value objects.
* gdb.python/py-value-cc.exp: Add new test to test operations on
gdb.Value objects.
doc/
* gdb.texinfo (Values From Inferior): Add description about
performing valid Python operations on gdb.Value objects.
[-- Attachment #2: python_op_patch_v4.txt --]
[-- Type: text/plain, Size: 6297 bytes --]
diff --git a/gdb/NEWS b/gdb/NEWS
index 5062e02..c669fd4 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -51,6 +51,12 @@ qXfer:btrace:read's annex
The qXfer:btrace:read packet supports a new annex 'delta' to read
branch trace incrementally.
+* Python Scripting
+
+ ** Valid Python operations on gdb.Value objects representing
+ structs/classes invoke the corresponding overloaded operators if
+ available.
+
*** Changes in GDB 7.7
* Improved support for process record-replay and reverse debugging on
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index af14286..dbeac6a 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -24038,7 +24038,26 @@ bar = some_val + 2
@noindent
As result of this, @code{bar} will also be a @code{gdb.Value} object
-whose values are of the same type as those of @code{some_val}.
+whose values are of the same type as those of @code{some_val}. Valid
+Python operations can also be performed on @code{gdb.Value} objects
+representing a @code{struct} or @code{class} object. For such cases,
+the overloaded operator (if present), is used to perform the operation.
+For example, if @code{val1}and @code{val2} are @code{gdb.Value} objects
+representing instances of a @code{class} which overloads the @code{+}
+operator, then one can use the @code{+} operator in their Python script
+as follows:
+
+@smallexample
+val3 = val1 + val2
+@end smallexample
+
+@noindent
+The result of the operation @code{val3} is also a @code{gdb.Value}
+object corresponding to the value returned by the overloaded @code{+}
+operator. In general, overloaded operators are invoked for the
+following operators: @code{+} (binary addition), @code{-} (binary
+subtraction), @code{*} (multiplication), @code{/}, @code{%}, @code{<<},
+@code{>>}, @code{|}, @code{&}, @code{^}.
Inferior values that are structures or instances of some class can
be accessed using the Python @dfn{dictionary syntax}. For example, if
diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index 67e6c4e..f382ec5 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -934,6 +934,8 @@ valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other)
struct value *arg1, *arg2;
struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
struct value *res_val = NULL;
+ enum exp_opcode op = OP_NULL;
+ int handled = 0;
/* If the gdb.Value object is the second operand, then it will be passed
to us as the OTHER argument, and SELF will be an entirely different
@@ -965,6 +967,7 @@ valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other)
CHECK_TYPEDEF (rtype);
rtype = STRIP_REFERENCE (rtype);
+ handled = 1;
if (TYPE_CODE (ltype) == TYPE_CODE_PTR
&& is_integral_type (rtype))
res_val = value_ptradd (arg1, value_as_long (arg2));
@@ -972,7 +975,10 @@ valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other)
&& is_integral_type (ltype))
res_val = value_ptradd (arg2, value_as_long (arg1));
else
- res_val = value_binop (arg1, arg2, BINOP_ADD);
+ {
+ handled = 0;
+ op = BINOP_ADD;
+ }
}
break;
case VALPY_SUB:
@@ -985,6 +991,7 @@ valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other)
CHECK_TYPEDEF (rtype);
rtype = STRIP_REFERENCE (rtype);
+ handled = 1;
if (TYPE_CODE (ltype) == TYPE_CODE_PTR
&& TYPE_CODE (rtype) == TYPE_CODE_PTR)
/* A ptrdiff_t for the target would be preferable here. */
@@ -994,38 +1001,49 @@ valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other)
&& is_integral_type (rtype))
res_val = value_ptradd (arg1, - value_as_long (arg2));
else
- res_val = value_binop (arg1, arg2, BINOP_SUB);
+ {
+ handled = 0;
+ op = BINOP_SUB;
+ }
}
break;
case VALPY_MUL:
- res_val = value_binop (arg1, arg2, BINOP_MUL);
+ op = BINOP_MUL;
break;
case VALPY_DIV:
- res_val = value_binop (arg1, arg2, BINOP_DIV);
+ op = BINOP_DIV;
break;
case VALPY_REM:
- res_val = value_binop (arg1, arg2, BINOP_REM);
+ op = BINOP_REM;
break;
case VALPY_POW:
- res_val = value_binop (arg1, arg2, BINOP_EXP);
+ op = BINOP_EXP;
break;
case VALPY_LSH:
- res_val = value_binop (arg1, arg2, BINOP_LSH);
+ op = BINOP_LSH;
break;
case VALPY_RSH:
- res_val = value_binop (arg1, arg2, BINOP_RSH);
+ op = BINOP_RSH;
break;
case VALPY_BITAND:
- res_val = value_binop (arg1, arg2, BINOP_BITWISE_AND);
+ op = BINOP_BITWISE_AND;
break;
case VALPY_BITOR:
- res_val = value_binop (arg1, arg2, BINOP_BITWISE_IOR);
+ op = BINOP_BITWISE_IOR;
break;
case VALPY_BITXOR:
- res_val = value_binop (arg1, arg2, BINOP_BITWISE_XOR);
+ op = BINOP_BITWISE_XOR;
break;
}
+ if (!handled)
+ {
+ if (binop_user_defined_p (op, arg1, arg2))
+ res_val = value_x_binop (arg1, arg2, op, OP_NULL, EVAL_NORMAL);
+ else
+ res_val = value_binop (arg1, arg2, op);
+ }
+
if (res_val)
result = value_to_value_object (res_val);
diff --git a/gdb/testsuite/gdb.python/py-value-cc.cc b/gdb/testsuite/gdb.python/py-value-cc.cc
index ace957a..7ea4f5d 100644
--- a/gdb/testsuite/gdb.python/py-value-cc.cc
+++ b/gdb/testsuite/gdb.python/py-value-cc.cc
@@ -17,9 +17,18 @@
class A {
public:
+ int operator+ (const int a1);
+
+ public:
int a;
};
+int
+A::operator+ (const int a1)
+{
+ return a + a1;
+}
+
union U {
int a;
char c;
@@ -88,5 +97,7 @@ main ()
{
A obj;
+ obj.a = 5;
+
return func (obj);
}
diff --git a/gdb/testsuite/gdb.python/py-value-cc.exp b/gdb/testsuite/gdb.python/py-value-cc.exp
index e6351f6..949f04f 100644
--- a/gdb/testsuite/gdb.python/py-value-cc.exp
+++ b/gdb/testsuite/gdb.python/py-value-cc.exp
@@ -99,3 +99,7 @@ gdb_test "python print xtd\[x_fields\[1\]\]\['a'\]" "102" "xtd->a via field"
gdb_test "python print len(uu_fields)" "2" "number of fields in uu"
gdb_test "python print uu\[uu_fields\[0\]\]\['x'\]" "1000" "uu.x via field"
gdb_test "python print uu\[uu_fields\[1\]\]\['a'\]" "1000" "uu.a via field"
+
+# Test overloaded operators.
+gdb_test_no_output "python a = gdb.parse_and_eval('a')" "init a"
+gdb_test "python print a + 5" "10" "a + 5"
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [RFC/Patch v4] Call overloaded operators to perform valid Python operations on struct/class values.
2014-02-02 20:06 [RFC/Patch v4] Call overloaded operators to perform valid Python operations on struct/class values Siva Chandra
@ 2014-02-02 20:59 ` Eli Zaretskii
2014-02-02 21:03 ` Eli Zaretskii
2014-02-07 6:53 ` Doug Evans
1 sibling, 1 reply; 5+ messages in thread
From: Eli Zaretskii @ 2014-02-02 20:59 UTC (permalink / raw)
To: Siva Chandra; +Cc: dje, gdb-patches, tromey
> Date: Sun, 2 Feb 2014 12:06:31 -0800
> From: Siva Chandra <sivachandra@google.com>
> Cc: gdb-patches <gdb-patches@sourceware.org>, Tom Tromey <tromey@redhat.com>
>
> +operator. In general, overloaded operators are invoked for the
> +following operators: @code{+} (binary addition), @code{-} (binary
^^^^^^^^^
I'd suggest "operations" here.
> +subtraction), @code{*} (multiplication), @code{/}, @code{%}, @code{<<},
> +@code{>>}, @code{|}, @code{&}, @code{^}.
OK with those changes.
Thanks.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [RFC/Patch v4] Call overloaded operators to perform valid Python operations on struct/class values.
2014-02-02 20:59 ` Eli Zaretskii
@ 2014-02-02 21:03 ` Eli Zaretskii
0 siblings, 0 replies; 5+ messages in thread
From: Eli Zaretskii @ 2014-02-02 21:03 UTC (permalink / raw)
To: sivachandra; +Cc: dje, gdb-patches, tromey
> Date: Sun, 02 Feb 2014 22:59:01 +0200
> From: Eli Zaretskii <eliz@gnu.org>
> Cc: dje@google.com, gdb-patches@sourceware.org, tromey@redhat.com
>
> > Date: Sun, 2 Feb 2014 12:06:31 -0800
> > From: Siva Chandra <sivachandra@google.com>
> > Cc: gdb-patches <gdb-patches@sourceware.org>, Tom Tromey <tromey@redhat.com>
> >
> > +operator. In general, overloaded operators are invoked for the
> > +following operators: @code{+} (binary addition), @code{-} (binary
> ^^^^^^^^^
> I'd suggest "operations" here.
>
> > +subtraction), @code{*} (multiplication), @code{/}, @code{%}, @code{<<},
> > +@code{>>}, @code{|}, @code{&}, @code{^}.
>
> OK with those changes.
Another gotcha that somehow got deleted from the mail I prepared:
> +For example, if @code{val1}and @code{val2} are @code{gdb.Value} objects
^^
Missing space.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [RFC/Patch v4] Call overloaded operators to perform valid Python operations on struct/class values.
2014-02-02 20:06 [RFC/Patch v4] Call overloaded operators to perform valid Python operations on struct/class values Siva Chandra
2014-02-02 20:59 ` Eli Zaretskii
@ 2014-02-07 6:53 ` Doug Evans
2014-02-20 0:04 ` Siva Chandra
1 sibling, 1 reply; 5+ messages in thread
From: Doug Evans @ 2014-02-07 6:53 UTC (permalink / raw)
To: Siva Chandra; +Cc: Eli Zaretskii, gdb-patches, Tom Tromey
On Sun, Feb 2, 2014 at 12:06 PM, Siva Chandra <sivachandra@google.com> wrote:
> On Sat, Jan 25, 2014 at 10:45 AM, Doug Evans <dje@google.com> wrote:
>> IWBN if the docs listed exactly which operations are supported.
>
> I have added it now in the attached patch. Eli should probably take
> another look.
>
>> [I realize this patch relies on the user-defined method patch, so it
>> can't go in just yet.]
>
> Are you referring to the debug methods patch? If yes, I do think this
> patch relies on that patch. This is an independent feature which would
> make writing debug methods simpler (IMO).
>
> 2014-02-02 Siva Chandra Reddy <sivachandra@google.com>
>
> Call overloaded operators to perform valid Python operations on
> struct/class values.
> * NEWS (Python Scripting): Add entry for this new feature.
> * python/py-value.c (valpy_binop): Call value_x_binop for struct
> and class values.
>
> testsuite/
> * gdb.python/py-value-cc.cc: Improve test case to enable testing
> operations on gdb.Value objects.
> * gdb.python/py-value-cc.exp: Add new test to test operations on
> gdb.Value objects.
>
> doc/
> * gdb.texinfo (Values From Inferior): Add description about
> performing valid Python operations on gdb.Value objects.
Hi.
The patch is fine with me.
I put in some time to play with the patch.
One thing I wondered about is what happens if the user does string1 + string2?
We do leak memory. That's not a problem with this patch of course,
though by making this scriptable the problem gets worse.
I filed https://sourceware.org/bugzilla/show_bug.cgi?id=16537
btw, Just writing this down since it's on my mind:
If anything needs to be done it should be addressed in a separate
patch. And maybe leaving things as-is is ok, and just a doc addition
is all that's needed. Anyways, hand called functions (which includes
c++ operators) run the inferior (setting aside using debug methods).
The more we make it easy to invoke c++ operators the more users are
going to run into the issues of hand called functions (e.g. what if
they have a breakpoint set that the c++ operator happens to trip?).
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [RFC/Patch v4] Call overloaded operators to perform valid Python operations on struct/class values.
2014-02-07 6:53 ` Doug Evans
@ 2014-02-20 0:04 ` Siva Chandra
0 siblings, 0 replies; 5+ messages in thread
From: Siva Chandra @ 2014-02-20 0:04 UTC (permalink / raw)
To: Doug Evans; +Cc: Eli Zaretskii, gdb-patches, Tom Tromey
[-- Attachment #1: Type: text/plain, Size: 1058 bytes --]
I have pushed the attached patch (which includes the corrections
suggested by Eli).
On Thu, Feb 6, 2014 at 10:53 PM, Doug Evans <dje@google.com> wrote:
> Hi.
>
> The patch is fine with me.
>
> I put in some time to play with the patch.
> One thing I wondered about is what happens if the user does string1 + string2?
> We do leak memory. That's not a problem with this patch of course,
> though by making this scriptable the problem gets worse.
> I filed https://sourceware.org/bugzilla/show_bug.cgi?id=16537
>
> btw, Just writing this down since it's on my mind:
> If anything needs to be done it should be addressed in a separate
> patch. And maybe leaving things as-is is ok, and just a doc addition
> is all that's needed. Anyways, hand called functions (which includes
> c++ operators) run the inferior (setting aside using debug methods).
> The more we make it easy to invoke c++ operators the more users are
> going to run into the issues of hand called functions (e.g. what if
> they have a breakpoint set that the c++ operator happens to trip?).
[-- Attachment #2: python_op_patch_v5.txt --]
[-- Type: text/plain, Size: 6271 bytes --]
diff --git a/gdb/NEWS b/gdb/NEWS
index b54a414..135858e 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -82,6 +82,12 @@ qXfer:btrace:read's annex
The qXfer:btrace:read packet supports a new annex 'delta' to read
branch trace incrementally.
+* Python Scripting
+
+ ** Valid Python operations on gdb.Value objects representing
+ structs/classes invoke the corresponding overloaded operators if
+ available.
+
* New targets
PowerPC64 GNU/Linux little-endian powerpc64le-*-linux*
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index 62636a4..90b7074 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -528,7 +528,26 @@ bar = some_val + 2
@noindent
As result of this, @code{bar} will also be a @code{gdb.Value} object
-whose values are of the same type as those of @code{some_val}.
+whose values are of the same type as those of @code{some_val}. Valid
+Python operations can also be performed on @code{gdb.Value} objects
+representing a @code{struct} or @code{class} object. For such cases,
+the overloaded operator (if present), is used to perform the operation.
+For example, if @code{val1} and @code{val2} are @code{gdb.Value} objects
+representing instances of a @code{class} which overloads the @code{+}
+operator, then one can use the @code{+} operator in their Python script
+as follows:
+
+@smallexample
+val3 = val1 + val2
+@end smallexample
+
+@noindent
+The result of the operation @code{val3} is also a @code{gdb.Value}
+object corresponding to the value returned by the overloaded @code{+}
+operator. In general, overloaded operators are invoked for the
+following operations: @code{+} (binary addition), @code{-} (binary
+subtraction), @code{*} (multiplication), @code{/}, @code{%}, @code{<<},
+@code{>>}, @code{|}, @code{&}, @code{^}.
Inferior values that are structures or instances of some class can
be accessed using the Python @dfn{dictionary syntax}. For example, if
diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index f8a8a98..75aa642 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -933,6 +933,8 @@ valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other)
struct value *arg1, *arg2;
struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
struct value *res_val = NULL;
+ enum exp_opcode op = OP_NULL;
+ int handled = 0;
/* If the gdb.Value object is the second operand, then it will be passed
to us as the OTHER argument, and SELF will be an entirely different
@@ -964,6 +966,7 @@ valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other)
CHECK_TYPEDEF (rtype);
rtype = STRIP_REFERENCE (rtype);
+ handled = 1;
if (TYPE_CODE (ltype) == TYPE_CODE_PTR
&& is_integral_type (rtype))
res_val = value_ptradd (arg1, value_as_long (arg2));
@@ -971,7 +974,10 @@ valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other)
&& is_integral_type (ltype))
res_val = value_ptradd (arg2, value_as_long (arg1));
else
- res_val = value_binop (arg1, arg2, BINOP_ADD);
+ {
+ handled = 0;
+ op = BINOP_ADD;
+ }
}
break;
case VALPY_SUB:
@@ -984,6 +990,7 @@ valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other)
CHECK_TYPEDEF (rtype);
rtype = STRIP_REFERENCE (rtype);
+ handled = 1;
if (TYPE_CODE (ltype) == TYPE_CODE_PTR
&& TYPE_CODE (rtype) == TYPE_CODE_PTR)
/* A ptrdiff_t for the target would be preferable here. */
@@ -993,38 +1000,49 @@ valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other)
&& is_integral_type (rtype))
res_val = value_ptradd (arg1, - value_as_long (arg2));
else
- res_val = value_binop (arg1, arg2, BINOP_SUB);
+ {
+ handled = 0;
+ op = BINOP_SUB;
+ }
}
break;
case VALPY_MUL:
- res_val = value_binop (arg1, arg2, BINOP_MUL);
+ op = BINOP_MUL;
break;
case VALPY_DIV:
- res_val = value_binop (arg1, arg2, BINOP_DIV);
+ op = BINOP_DIV;
break;
case VALPY_REM:
- res_val = value_binop (arg1, arg2, BINOP_REM);
+ op = BINOP_REM;
break;
case VALPY_POW:
- res_val = value_binop (arg1, arg2, BINOP_EXP);
+ op = BINOP_EXP;
break;
case VALPY_LSH:
- res_val = value_binop (arg1, arg2, BINOP_LSH);
+ op = BINOP_LSH;
break;
case VALPY_RSH:
- res_val = value_binop (arg1, arg2, BINOP_RSH);
+ op = BINOP_RSH;
break;
case VALPY_BITAND:
- res_val = value_binop (arg1, arg2, BINOP_BITWISE_AND);
+ op = BINOP_BITWISE_AND;
break;
case VALPY_BITOR:
- res_val = value_binop (arg1, arg2, BINOP_BITWISE_IOR);
+ op = BINOP_BITWISE_IOR;
break;
case VALPY_BITXOR:
- res_val = value_binop (arg1, arg2, BINOP_BITWISE_XOR);
+ op = BINOP_BITWISE_XOR;
break;
}
+ if (!handled)
+ {
+ if (binop_user_defined_p (op, arg1, arg2))
+ res_val = value_x_binop (arg1, arg2, op, OP_NULL, EVAL_NORMAL);
+ else
+ res_val = value_binop (arg1, arg2, op);
+ }
+
if (res_val)
result = value_to_value_object (res_val);
diff --git a/gdb/testsuite/gdb.python/py-value-cc.cc b/gdb/testsuite/gdb.python/py-value-cc.cc
index ace957a..7ea4f5d 100644
--- a/gdb/testsuite/gdb.python/py-value-cc.cc
+++ b/gdb/testsuite/gdb.python/py-value-cc.cc
@@ -17,9 +17,18 @@
class A {
public:
+ int operator+ (const int a1);
+
+ public:
int a;
};
+int
+A::operator+ (const int a1)
+{
+ return a + a1;
+}
+
union U {
int a;
char c;
@@ -88,5 +97,7 @@ main ()
{
A obj;
+ obj.a = 5;
+
return func (obj);
}
diff --git a/gdb/testsuite/gdb.python/py-value-cc.exp b/gdb/testsuite/gdb.python/py-value-cc.exp
index e6351f6..949f04f 100644
--- a/gdb/testsuite/gdb.python/py-value-cc.exp
+++ b/gdb/testsuite/gdb.python/py-value-cc.exp
@@ -99,3 +99,7 @@ gdb_test "python print xtd\[x_fields\[1\]\]\['a'\]" "102" "xtd->a via field"
gdb_test "python print len(uu_fields)" "2" "number of fields in uu"
gdb_test "python print uu\[uu_fields\[0\]\]\['x'\]" "1000" "uu.x via field"
gdb_test "python print uu\[uu_fields\[1\]\]\['a'\]" "1000" "uu.a via field"
+
+# Test overloaded operators.
+gdb_test_no_output "python a = gdb.parse_and_eval('a')" "init a"
+gdb_test "python print a + 5" "10" "a + 5"
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2014-02-20 0:04 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-02-02 20:06 [RFC/Patch v4] Call overloaded operators to perform valid Python operations on struct/class values Siva Chandra
2014-02-02 20:59 ` Eli Zaretskii
2014-02-02 21:03 ` Eli Zaretskii
2014-02-07 6:53 ` Doug Evans
2014-02-20 0:04 ` Siva Chandra
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox