From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 62191 invoked by alias); 7 May 2015 23:30:57 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 62176 invoked by uid 89); 7 May 2015 23:30:56 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.6 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_LOW,SPF_PASS,T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: mail-qg0-f74.google.com Received: from mail-qg0-f74.google.com (HELO mail-qg0-f74.google.com) (209.85.192.74) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Thu, 07 May 2015 23:30:54 +0000 Received: by qgdq107 with SMTP id q107so1834132qgd.1 for ; Thu, 07 May 2015 16:30:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:message-id:date:subject:from:to:cc :content-type; bh=mcxGTt83nd+8FnF8NIb9JOccWn3kQae3fKQM+7gekaY=; b=Z3QbNRX4sUGEwy0pkKeLAQGmSnu9syMsHbLgb4nPOnfRvswpPztWPSju/mBb2odJHc qlHYwP8U6YvkK7pD2MjX2+oQtCGr5ld/cf9uIULWKVeP+1Uyzoxjh/hmfXG/7VQG/nkd M58rEfhQmPE1Q/Wiy1ljgPMZ6scIDoH83KxsM47vqIT9WTAhtdJH0YUplS+mKP0/64k5 A+D9bpmxxjrw8HgHw1+CN6RZIxj6E9YG8kk0VMlptbdYd9a7LEelxI3OVWfTINoACdMp Y7RDqE8y+alDmdw1BKzjA/ky141UKFczO0n4e0cz4+ztQv9BM0BC1iL6CBn/0lVDdes2 vorA== X-Gm-Message-State: ALoCoQlXGP3IFV0gRU4tEu3D9xc2xruHMNtGp+IArkcwoRayBaTZqfOBTreLXK5elzKmq24JUpO7 MIME-Version: 1.0 X-Received: by 10.140.236.23 with SMTP id h23mr1615299qhc.12.1431041451803; Thu, 07 May 2015 16:30:51 -0700 (PDT) Message-ID: <001a1135a3d865051b0515864d6a@google.com> Date: Thu, 07 May 2015 23:30:00 -0000 Subject: Re: [Python] Add methods reference_value and const_value to gdb.Value From: Doug Evans To: Siva Chandra Cc: gdb-patches Content-Type: text/plain; charset=UTF-8; format=flowed; delsp=yes X-IsSubscribed: yes X-SW-Source: 2015-05/txt/msg00172.txt.bz2 Siva Chandra writes: > This will help address libstdc++/65840: > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65840 > > gdb/ChangeLog: > > 2015-04-27 Siva Chandra Reddy > > * NEWS (Python Scripting): Mention the new gdb.Value methods. > * python/py-value.c (valpy_reference_value): New function. > (valpy_const_value): Likewise. > (value_object_methods): Add new methods. > * value.c (make_cv_value): New function. > * value.h (make_cv_value): Declare. > > gdb/doc/ChangeLog: > > 2015-04-27 Siva Chandra Reddy > > * python.texi (Values From Inferior): Add descriptions of new > methods gdb.Value.reference_value and gdb.Value.const_value. > > gdb/testsuite/ChangeLog: > > 2015-04-27 Siva Chandra Reddy > > * gdb.python/py-xmethods.cc: Enhance test case. > * gdb.python/py-xmethods.exp: New tests. > * gdb.python/py-xmethods.py (A_indexoper): New xmethod worker > function. > (B_indexoper): Likewise. > (global_dm_list) : Add new xmethod worker functions. Thanks again for the ping. LGTM with two nits below. grep for ====. One more nit might be that we already have gdb.Value.referenced_value, and now we'll also have gdb.Value.reference_value. I'm ok with it. > diff --git a/gdb/NEWS b/gdb/NEWS > index b711553..627afd0 100644 > --- a/gdb/NEWS > +++ b/gdb/NEWS > @@ -52,6 +52,9 @@ > which is the name of the objfile as specified by the user, > without, for example, resolving symlinks. > ** You can now write frame unwinders in Python. > + ** gdb.Value objects have new methods "reference_value" and > + "const_value" which return a reference to the value and a > + "const" version of the value respectively. > > * New commands > > diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi > index 448fa8b2..1b1079d 100644 > --- a/gdb/doc/python.texi > +++ b/gdb/doc/python.texi > @@ -781,6 +781,16 @@ The @code{gdb.Value} object @code{py_val} is identical to that > corresponding to @code{val}. > @end defun > > +@defun Value.reference_value () > +Return a @code{gdb.Value} object which is a reference to the value > +encapsulated by this instance. > +@end defun > + > +@defun Value.const_value () > +Return a @code{gdb.Value} object which is a @code{const} version of the > +value encapsulated by this instance. > +@end defun > + > @defun Value.dynamic_cast (type) > Like @code{Value.cast}, but works as if the C@t{++} @code{dynamic_cast} > operator were used. Consult a C@t{++} reference for details. > diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c > index 6622d11..94ff9ba 100644 > --- a/gdb/python/py-value.c > +++ b/gdb/python/py-value.c > @@ -236,6 +236,60 @@ valpy_referenced_value (PyObject *self, PyObject *args) > return result; > } > > +/* Return a value which is a reference to the value. */ > + > +static PyObject * > +valpy_reference_value (PyObject *self, PyObject *args) > +{ > + PyObject *result = NULL; > + > + TRY > + { > + struct value *self_val; > + struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ()); > + > + self_val = ((value_object *) self)->value; > + result = value_to_value_object (value_ref (self_val)); > + > + do_cleanups (cleanup); > + } > + CATCH (except, RETURN_MASK_ALL) > + { > + GDB_PY_HANDLE_EXCEPTION (except); > + } > + END_CATCH > + > + return result; > +} > + > +/* Return a "const" qualified version of the value. */ > + > +static PyObject * > +valpy_const_value (PyObject *self, PyObject *args) > +{ > + PyObject *result = NULL; > + > + TRY > + { > + struct value *self_val, *res_val; > + struct type *const_type; ==== const_type can be deleted. > + struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ()); > + > + self_val = ((value_object *) self)->value; > + res_val = make_cv_value (1, 0, self_val); > + result = value_to_value_object (res_val); > + > + do_cleanups (cleanup); > + } > + CATCH (except, RETURN_MASK_ALL) > + { > + GDB_PY_HANDLE_EXCEPTION (except); > + } > + END_CATCH > + > + return result; > +} > + > /* Return "&value". */ > static PyObject * > valpy_get_address (PyObject *self, void *closure) > @@ -1692,6 +1746,10 @@ 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." }, > + { "reference_value", valpy_reference_value, METH_NOARGS, > + "Return a value of type TYPE_CODE_REF referencing this value." }, > + { "const_value", valpy_const_value, METH_NOARGS, > + "Return a 'const' qualied version of the same value." }, > { "lazy_string", (PyCFunction) valpy_lazy_string, > METH_VARARGS | METH_KEYWORDS, > "lazy_string ([encoding] [, length]) -> lazy_string\n\ > diff --git a/gdb/testsuite/gdb.python/py-xmethods.cc b/gdb/testsuite/gdb.python/py-xmethods.cc > index aedd1de..98bdb72 100644 > --- a/gdb/testsuite/gdb.python/py-xmethods.cc > +++ b/gdb/testsuite/gdb.python/py-xmethods.cc > @@ -173,7 +173,7 @@ int main(void) > > for (int i = 0; i < 10; i++) > { > - a1.array[i] = a2.array[i] = i; > + a1.array[i] = a2.array[i] = b1.array[i] = i; > } > > return 0; /* Break here. */ > diff --git a/gdb/testsuite/gdb.python/py-xmethods.exp b/gdb/testsuite/gdb.python/py-xmethods.exp > index a83b14d..eacfa0c 100644 > --- a/gdb/testsuite/gdb.python/py-xmethods.exp > +++ b/gdb/testsuite/gdb.python/py-xmethods.exp > @@ -100,6 +100,8 @@ gdb_test "p a1.geta()" "From Python .*5" "After: a1.geta()" > gdb_test "p ++a1" "From Python .*6" "After: ++a1" > gdb_test "p a1.getarrayind(5)" "From Python .*5" \ > "After: a1.getarrayind(5)" > +gdb_test "P a1\[6\]" ".*int &.*6" "After a1\[\]" > +gdb_test "P b1\[7\]" ".*const int &.*7" "After b1\[\]" > # Note the following test. Xmethods on dynamc types are not looked up > # currently. Hence, even though a_ptr points to a B object, the xmethod > # defined for A objects is invoked. > diff --git a/gdb/testsuite/gdb.python/py-xmethods.py b/gdb/testsuite/gdb.python/py-xmethods.py > index 78935e1..3194e89 100644 > --- a/gdb/testsuite/gdb.python/py-xmethods.py > +++ b/gdb/testsuite/gdb.python/py-xmethods.py > @@ -43,6 +43,12 @@ def A_getarrayind(obj, index): > print('From Python :') > return obj['array'][index] > > +def A_indexoper(obj, index): > + return obj['array'][index].reference_value() > + > +def B_indexoper(obj, index): > + return obj['array'][index].const_value().reference_value() > + > > type_A = gdb.parse_and_eval('(dop::A *) 0').type.target() > type_B = gdb.parse_and_eval('(dop::B *) 0').type.target() > @@ -208,6 +214,16 @@ global_dm_list = [ > '^getarrayind$', > A_getarrayind, > type_int), > + SimpleXMethodMatcher('A_indexoper', > + '^dop::A$', > + 'operator\\[\\]', > + A_indexoper, > + type_int), > + SimpleXMethodMatcher('B_indexoper', > + '^dop::B$', > + 'operator\\[\\]', > + B_indexoper, > + type_int) > ] > > for matcher in global_dm_list: > diff --git a/gdb/value.c b/gdb/value.c > index cb56849..242b46c 100644 > --- a/gdb/value.c > +++ b/gdb/value.c > @@ -1704,6 +1704,22 @@ value_copy (struct value *arg) > return val; > } > > +/* Return a "const" and/or "volatile" qualified version of the value V. > + If CNST is true, then the returned value will be qualified with > + "const". > + if VOLTL is true, then the returned value will be qualified with > + "volatile". */ > + > +struct value * > +make_cv_value (int cnst, int voltl, struct value *v) > +{ > + struct value *cv_val = value_copy (v); > + > + cv_val->type = make_cv_type (cnst, voltl, value_type (v), NULL); > + > + return cv_val; > +} ==== I'm not sure the call to value_copy will always produce the desired result, but I don't have anything specific to say. So let's go with it. I'd replace the assignment to cv_val->type with a call to deprecated_set_value_type though. Do we need to call set_value_enclosing_type too? For consistency I'd think so. > + > /* Return a version of ARG that is non-lvalue. */ > > struct value * > diff --git a/gdb/value.h b/gdb/value.h > index 21baa32..9aeaa4a 100644 > --- a/gdb/value.h > +++ b/gdb/value.h > @@ -1040,6 +1040,8 @@ extern struct value *value_non_lval (struct value *); > > extern void value_force_lval (struct value *, CORE_ADDR); > > +extern struct value *make_cv_value (int, int, struct value *); > + > extern void preserve_one_value (struct value *, struct objfile *, htab_t); > > /* From valops.c */