* [RFC][Python] Re: any expression to tell whether a variable was optimized out? [not found] ` <m3hc1rluz1.fsf@fleche.redhat.com> @ 2009-03-23 2:09 ` Thiago Jung Bauermann 2009-03-23 4:24 ` Eli Zaretskii 2009-03-23 17:47 ` Tom Tromey 0 siblings, 2 replies; 10+ messages in thread From: Thiago Jung Bauermann @ 2009-03-23 2:09 UTC (permalink / raw) To: Tom Tromey; +Cc: Alexandre Oliva, gdb-patches [ Moving thread from the gdb@ ml. ] El mar, 17-03-2009 a las 15:06 -0600, Tom Tromey escribió: > >>>>> "Thiago" == Thiago Jung Bauermann <bauerman@br.ibm.com> writes: > Thiago> You can also do the above by checking out the archer-tromey-python > Thiago> branch, and applying the patch below. I didn't commit this yet because I > Thiago> don't know if it would be better to have is_optimized_out function as a > Thiago> method or attribute of gdb.Value... > > I think attribute, because (AFAIK) it is immutable. > > I'm a little surprised that this is an attribute of values at all. > Doesn't that seem weird? Is there anything useful that can be done > with a value like this? Perhaps just fetching its type? I guess Alexandre just found a use for the attribute. :-) This patch implements the attribute in gdb.Value, what do you think? By the way, when writing this patch I realised that probably the "address" method in Value would better be turned into an attribute. Also, in the python branch, the "type" method also could be converted to an attribute. -- []'s Thiago Jung Bauermann IBM Linux Technology Center gdb/ Add gdb.Value.is_optimized_out attribute. * python/python-value.c (valpy_get_is_optimized_out): New function. (value_object_getset): New variable. (value_object_type): Initialize tp_getset element. gdb/doc/ * gdb.texinfo (Values From Inferior): Document is_optimized_out attribute. gdb/testsuite/ * gdb.python/python-value.exp (test_value_in_inferior): Test gdb.Value.is_optimized_out attribute. Index: gdb.git/gdb/python/python-value.c =================================================================== --- gdb.git.orig/gdb/python/python-value.c 2009-03-22 22:19:11.000000000 -0300 +++ gdb.git/gdb/python/python-value.c 2009-03-22 22:19:13.000000000 -0300 @@ -272,6 +272,18 @@ valpy_str (PyObject *self) return result; } +/* Implements gdb.Value.is_optimized_out. */ +static PyObject * +valpy_get_is_optimized_out (PyObject *self, void *closure) +{ + struct value *value = ((value_object *) self)->value; + + if (value_optimized_out (value)) + Py_RETURN_TRUE; + + Py_RETURN_FALSE; +} + enum valpy_opcode { VALPY_ADD, @@ -825,6 +837,13 @@ gdbpy_initialize_values (void) values_in_python = NULL; } +static PyGetSetDef value_object_getset[] = { + { "is_optimized_out", valpy_get_is_optimized_out, NULL, + "Boolean telling whether the value is optimized out (i.e., not available).", + NULL }, + {NULL} /* Sentinel */ +}; + static PyMethodDef value_object_methods[] = { { "address", valpy_address, METH_NOARGS, "Return the address of the value." }, { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." }, @@ -897,7 +916,7 @@ PyTypeObject value_object_type = { 0, /* tp_iternext */ value_object_methods, /* tp_methods */ 0, /* tp_members */ - 0, /* tp_getset */ + value_object_getset, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ 0, /* tp_descr_get */ Index: gdb.git/gdb/testsuite/gdb.python/python-value.exp =================================================================== --- gdb.git.orig/gdb/testsuite/gdb.python/python-value.exp 2009-03-22 22:19:11.000000000 -0300 +++ gdb.git/gdb/testsuite/gdb.python/python-value.exp 2009-03-22 22:19:13.000000000 -0300 @@ -225,6 +225,9 @@ proc test_value_in_inferior {} { # Check that the dereferenced value is sane gdb_test "python print arg0" "0x.*$testfile\"" "verify dereferenced value" + + # Smoke-test is_optimized_out attribute + gdb_test "python print 'result =', arg0.is_optimized_out" "= False" "Test is_optimized_out attribute" } Index: gdb.git/gdb/doc/gdb.texinfo =================================================================== --- gdb.git.orig/gdb/doc/gdb.texinfo 2009-03-22 22:19:11.000000000 -0300 +++ gdb.git/gdb/doc/gdb.texinfo 2009-03-22 22:24:46.000000000 -0300 @@ -18325,13 +18325,22 @@ bar = some_val['foo'] Again, @code{bar} will also be a @code{gdb.Value} object. -For pointer data types, @code{gdb.Value} provides a method for -dereferencing the pointer to obtain the object it points to. +The following attribute is provided: +@table @code +@defmethod Value is_optimized_out +This read-only boolean attribute is true if the compiler optimized out +this value, thus it is not available for fetching from the inferior. +@end defmethod +@end table + +The following methods are provided: + +@table @code @defmethod Value dereference -This method returns a new @code{gdb.Value} object whose contents is -the object pointed to by the pointer. For example, if @code{foo} is -a C pointer to an @code{int}, declared in your C program as +For pointer data types, this method returns a new @code{gdb.Value} object +whose contents is the object pointed to by the pointer. For example, if +@code{foo} is a C pointer to an @code{int}, declared in your C program as @smallexample int *foo; @@ -18375,6 +18384,7 @@ will be used, if the current language is The optional @var{errors} argument is the same as the corresponding argument to Python's @code{string.decode} method. @end defmethod +@end table @node Commands In Python @subsubsection Commands In Python ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [RFC][Python] Re: any expression to tell whether a variable was optimized out? 2009-03-23 2:09 ` [RFC][Python] Re: any expression to tell whether a variable was optimized out? Thiago Jung Bauermann @ 2009-03-23 4:24 ` Eli Zaretskii 2009-03-26 21:21 ` Thiago Jung Bauermann 2009-03-23 17:47 ` Tom Tromey 1 sibling, 1 reply; 10+ messages in thread From: Eli Zaretskii @ 2009-03-23 4:24 UTC (permalink / raw) To: Thiago Jung Bauermann; +Cc: tromey, aoliva, gdb-patches > From: Thiago Jung Bauermann <bauerman@br.ibm.com> > Cc: Alexandre Oliva <aoliva@redhat.com>, gdb-patches@sourceware.org > Date: Sun, 22 Mar 2009 22:31:02 -0300 > > +@table @code > +@defmethod Value is_optimized_out > +This read-only boolean attribute is true if the compiler optimized out > +this value, thus it is not available for fetching from the inferior. > +@end defmethod > +@end table This is OK, but please add a @cindex entry for this that starts with "optimized", so that readers will be able to find this easier. Thanks. ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [RFC][Python] Re: any expression to tell whether a variable was optimized out? 2009-03-23 4:24 ` Eli Zaretskii @ 2009-03-26 21:21 ` Thiago Jung Bauermann 0 siblings, 0 replies; 10+ messages in thread From: Thiago Jung Bauermann @ 2009-03-26 21:21 UTC (permalink / raw) To: Eli Zaretskii; +Cc: tromey, aoliva, gdb-patches El lun, 23-03-2009 a las 06:24 +0200, Eli Zaretskii escribió: > > From: Thiago Jung Bauermann <bauerman@br.ibm.com> > > Cc: Alexandre Oliva <aoliva@redhat.com>, gdb-patches@sourceware.org > > Date: Sun, 22 Mar 2009 22:31:02 -0300 > > > > +@table @code > > +@defmethod Value is_optimized_out > > +This read-only boolean attribute is true if the compiler optimized out > > +this value, thus it is not available for fetching from the inferior. > > +@end defmethod > > +@end table > > This is OK, but please add a @cindex entry for this that starts with > "optimized", so that readers will be able to find this easier. Here's the @cindex entry I added: +@table @code +@cindex optimized out value in Python +@defmethod Value is_optimized_out +This read-only boolean attribute is true if the compiler optimized out +this value, thus it is not available for fetching from the inferior. +@end defmethod +@end table I committed the following patch. Thanks for the review. -- []'s Thiago Jung Bauermann IBM Linux Technology Center gdb/ Add gdb.Value.is_optimized_out attribute. * python/python-value.c (valpy_get_is_optimized_out): New function. (value_object_getset): New variable. (value_object_type): Initialize tp_getset element. gdb/doc/ * gdb.texinfo (Values From Inferior): Document is_optimized_out attribute. gdb/testsuite/ * gdb.python/python-value.exp (test_value_in_inferior): Test gdb.Value.is_optimized_out attribute. Index: src/gdb/python/python-value.c =================================================================== --- src.orig/gdb/python/python-value.c 2009-03-21 00:06:27.000000000 -0300 +++ src/gdb/python/python-value.c 2009-03-26 17:48:45.000000000 -0300 @@ -272,6 +272,18 @@ valpy_str (PyObject *self) return result; } +/* Implements gdb.Value.is_optimized_out. */ +static PyObject * +valpy_get_is_optimized_out (PyObject *self, void *closure) +{ + struct value *value = ((value_object *) self)->value; + + if (value_optimized_out (value)) + Py_RETURN_TRUE; + + Py_RETURN_FALSE; +} + enum valpy_opcode { VALPY_ADD, @@ -825,6 +837,13 @@ gdbpy_initialize_values (void) values_in_python = NULL; } +static PyGetSetDef value_object_getset[] = { + { "is_optimized_out", valpy_get_is_optimized_out, NULL, + "Boolean telling whether the value is optimized out (i.e., not available).", + NULL }, + {NULL} /* Sentinel */ +}; + static PyMethodDef value_object_methods[] = { { "address", valpy_address, METH_NOARGS, "Return the address of the value." }, { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." }, @@ -897,7 +916,7 @@ PyTypeObject value_object_type = { 0, /* tp_iternext */ value_object_methods, /* tp_methods */ 0, /* tp_members */ - 0, /* tp_getset */ + value_object_getset, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ 0, /* tp_descr_get */ Index: src/gdb/testsuite/gdb.python/python-value.exp =================================================================== --- src.orig/gdb/testsuite/gdb.python/python-value.exp 2009-02-04 19:49:10.000000000 -0200 +++ src/gdb/testsuite/gdb.python/python-value.exp 2009-03-26 17:48:45.000000000 -0300 @@ -225,6 +225,9 @@ proc test_value_in_inferior {} { # Check that the dereferenced value is sane gdb_test "python print arg0" "0x.*$testfile\"" "verify dereferenced value" + + # Smoke-test is_optimized_out attribute + gdb_test "python print 'result =', arg0.is_optimized_out" "= False" "Test is_optimized_out attribute" } Index: src/gdb/doc/gdb.texinfo =================================================================== --- src.orig/gdb/doc/gdb.texinfo 2009-03-25 17:54:17.000000000 -0300 +++ src/gdb/doc/gdb.texinfo 2009-03-26 17:49:33.000000000 -0300 @@ -18325,13 +18325,23 @@ bar = some_val['foo'] Again, @code{bar} will also be a @code{gdb.Value} object. -For pointer data types, @code{gdb.Value} provides a method for -dereferencing the pointer to obtain the object it points to. +The following attribute is provided: +@table @code +@cindex optimized out value in Python +@defmethod Value is_optimized_out +This read-only boolean attribute is true if the compiler optimized out +this value, thus it is not available for fetching from the inferior. +@end defmethod +@end table + +The following methods are provided: + +@table @code @defmethod Value dereference -This method returns a new @code{gdb.Value} object whose contents is -the object pointed to by the pointer. For example, if @code{foo} is -a C pointer to an @code{int}, declared in your C program as +For pointer data types, this method returns a new @code{gdb.Value} object +whose contents is the object pointed to by the pointer. For example, if +@code{foo} is a C pointer to an @code{int}, declared in your C program as @smallexample int *foo; @@ -18375,6 +18385,7 @@ will be used, if the current language is The optional @var{errors} argument is the same as the corresponding argument to Python's @code{string.decode} method. @end defmethod +@end table @node Commands In Python @subsubsection Commands In Python ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [RFC][Python] Re: any expression to tell whether a variable was optimized out? 2009-03-23 2:09 ` [RFC][Python] Re: any expression to tell whether a variable was optimized out? Thiago Jung Bauermann 2009-03-23 4:24 ` Eli Zaretskii @ 2009-03-23 17:47 ` Tom Tromey 2009-03-28 21:38 ` [RFA][Python] Change gdb.Value.address from a method to an attribute Thiago Jung Bauermann 1 sibling, 1 reply; 10+ messages in thread From: Tom Tromey @ 2009-03-23 17:47 UTC (permalink / raw) To: Thiago Jung Bauermann; +Cc: Alexandre Oliva, gdb-patches >>>>> "Thiago" == Thiago Jung Bauermann <bauerman@br.ibm.com> writes: Tom> I'm a little surprised that this is an attribute of values at all. Tom> Doesn't that seem weird? Is there anything useful that can be done Tom> with a value like this? Perhaps just fetching its type? Thiago> I guess Alexandre just found a use for the attribute. :-) :) Funnily enough, a couple of PRs related to this came in over the weekend. And, those make it clear that we actually do want this as an attribute of the value -- a future GCC may generate a value where some components are defined and some are not. In this case we would want to track the undefined bits and let value operations apply to defined ones but not undefined ones. I think. Thiago> This patch implements the attribute in gdb.Value, what do you think? The code bits are ok. Thiago> By the way, when writing this patch I realised that probably the Thiago> "address" method in Value would better be turned into an attribute. Do it :) Thiago> Also, in the python branch, the "type" method also could be converted to Thiago> an attribute. The type method is still funny because, due to memory management oddities, we always allocate a fresh Type. This has already bit a user, though, and we ought to figure out how to fix it. Tom ^ permalink raw reply [flat|nested] 10+ messages in thread
* [RFA][Python] Change gdb.Value.address from a method to an attribute. 2009-03-23 17:47 ` Tom Tromey @ 2009-03-28 21:38 ` Thiago Jung Bauermann 2009-03-29 4:21 ` Eli Zaretskii 2009-03-29 15:42 ` Tom Tromey 0 siblings, 2 replies; 10+ messages in thread From: Thiago Jung Bauermann @ 2009-03-28 21:38 UTC (permalink / raw) To: Tom Tromey; +Cc: gdb-patches El lun, 23-03-2009 a las 11:45 -0600, Tom Tromey escribió: > >>>>> "Thiago" == Thiago Jung Bauermann <bauerman@br.ibm.com> writes: > Thiago> By the way, when writing this patch I realised that probably the > Thiago> "address" method in Value would better be turned into an attribute. > > Do it :) Just did itâ¢. Ok to commit? -- []'s Thiago Jung Bauermann IBM Linux Technology Center gdb/ Change gdb.Value.address from a method to an attribute. * python/python-value.c (value_object): Add `address' element. (valpy_dealloc): Decrement reference to self->address if set. (valpy_new): Initialize val_obj->address. (valpy_address): Rename to ... (valpy_get_address): ... this. Change signature from method to attribute. Update self->address if not set. (value_to_value_object): Initialize val_obj->address. (value_object_getset): Add `address' element. (value_object_methods): Remove `address' element. gdb/testsuite/ * gdb.python/python-value.exp: Add tests for the address attribute. gdb/doc/ * gdb.texinfo (Values From Inferior): Change gdb.Value.address from a method to an attribute. Index: src/gdb/doc/gdb.texinfo =================================================================== --- src.orig/gdb/doc/gdb.texinfo 2009-03-26 17:50:36.000000000 -0300 +++ src/gdb/doc/gdb.texinfo 2009-03-28 17:45:17.000000000 -0300 @@ -18325,9 +18325,15 @@ bar = some_val['foo'] Again, @code{bar} will also be a @code{gdb.Value} object. -The following attribute is provided: +The following attributes are provided: @table @code +@defmethod Value address +If the @code{gdb.Value} object is addressable, this read-only attribute +holds a @code{gdb.Value} object representing the address. Otherwise, +this attribute holds @code{None}. +@end defmethod + @cindex optimized out value in Python @defmethod Value is_optimized_out This read-only boolean attribute is true if the compiler optimized out Index: src/gdb/python/python-value.c =================================================================== --- src.orig/gdb/python/python-value.c 2009-03-26 17:50:36.000000000 -0300 +++ src/gdb/python/python-value.c 2009-03-28 17:45:17.000000000 -0300 @@ -59,6 +59,7 @@ typedef struct { PyObject_HEAD struct value *value; int owned_by_gdb; + PyObject *address; } value_object; /* Called by the Python interpreter when deallocating a value object. */ @@ -71,6 +72,13 @@ valpy_dealloc (PyObject *obj) if (!self->owned_by_gdb) value_free (self->value); + + if (self->address) + /* Use braces to appease gcc warning. *sigh* */ + { + Py_DECREF (self->address); + } + self->ob_type->tp_free (self); } @@ -105,6 +113,7 @@ valpy_new (PyTypeObject *subtype, PyObje value_obj->value = value; value_obj->owned_by_gdb = 0; + value_obj->address = NULL; release_value (value); value_prepend_to_list (&values_in_python, value); @@ -129,18 +138,30 @@ valpy_dereference (PyObject *self, PyObj /* Return "&value". */ static PyObject * -valpy_address (PyObject *self, PyObject *args) +valpy_get_address (PyObject *self, void *closure) { struct value *res_val = NULL; /* Initialize to appease gcc warning. */ + value_object *val_obj = (value_object *) self; volatile struct gdb_exception except; - TRY_CATCH (except, RETURN_MASK_ALL) + if (!val_obj->address) { - res_val = value_addr (((value_object *) self)->value); + TRY_CATCH (except, RETURN_MASK_ALL) + { + res_val = value_addr (val_obj->value); + } + if (except.reason < 0) + { + val_obj->address = Py_None; + Py_INCREF (Py_None); + } + else + val_obj->address = value_to_value_object (res_val); } - GDB_PY_HANDLE_EXCEPTION (except); - return value_to_value_object (res_val); + Py_INCREF (val_obj->address); + + return val_obj->address; } /* Implementation of gdb.Value.string ([encoding] [, errors]) -> string @@ -726,6 +747,7 @@ value_to_value_object (struct value *val { val_obj->value = val; val_obj->owned_by_gdb = 0; + val_obj->address = NULL; release_value (val); value_prepend_to_list (&values_in_python, val); } @@ -838,6 +860,8 @@ gdbpy_initialize_values (void) } static PyGetSetDef value_object_getset[] = { + { "address", valpy_get_address, NULL, "The address of the value.", + NULL }, { "is_optimized_out", valpy_get_is_optimized_out, NULL, "Boolean telling whether the value is optimized out (i.e., not available).", NULL }, @@ -845,7 +869,6 @@ static PyGetSetDef value_object_getset[] }; static PyMethodDef value_object_methods[] = { - { "address", valpy_address, METH_NOARGS, "Return the address of the value." }, { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." }, { "string", (PyCFunction) valpy_string, METH_VARARGS | METH_KEYWORDS, "string ([encoding] [, errors]) -> string\n\ Index: src/gdb/testsuite/gdb.python/python-value.exp =================================================================== --- src.orig/gdb/testsuite/gdb.python/python-value.exp 2009-03-26 17:50:36.000000000 -0300 +++ src/gdb/testsuite/gdb.python/python-value.exp 2009-03-28 17:45:17.000000000 -0300 @@ -70,6 +70,9 @@ proc test_value_creation {} { gdb_py_test_silent_cmd "python a = gdb.Value (u'unicode test')" "create unicode value" 1 gdb_test "python print a" "\"unicode test\"" "print Unicode string" gdb_test "python print a.__class__" "<type 'gdb.Value'>" "verify type of unicode string" + + # Test address attribute is None in a non-addressable value + gdb_test "python print 'result =', i.address" "= None" "Test address attribute in non-addressable value" } proc test_value_numeric_ops {} { @@ -228,6 +231,9 @@ proc test_value_in_inferior {} { # Smoke-test is_optimized_out attribute gdb_test "python print 'result =', arg0.is_optimized_out" "= False" "Test is_optimized_out attribute" + + # Test address attribute + gdb_test "python print 'result =', arg0.address" "= 0x\[\[:xdigit:\]\]+" "Test address attribute" } ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [RFA][Python] Change gdb.Value.address from a method to an attribute. 2009-03-28 21:38 ` [RFA][Python] Change gdb.Value.address from a method to an attribute Thiago Jung Bauermann @ 2009-03-29 4:21 ` Eli Zaretskii 2009-03-29 18:12 ` Tom Tromey 2009-03-29 15:42 ` Tom Tromey 1 sibling, 1 reply; 10+ messages in thread From: Eli Zaretskii @ 2009-03-29 4:21 UTC (permalink / raw) To: Thiago Jung Bauermann; +Cc: tromey, gdb-patches > From: Thiago Jung Bauermann <bauerman@br.ibm.com> > Cc: gdb-patches@sourceware.org > Date: Sat, 28 Mar 2009 18:18:57 -0300 > > +@defmethod Value address > +If the @code{gdb.Value} object is addressable, this read-only attribute > +holds a @code{gdb.Value} object representing the address. Otherwise, > +this attribute holds @code{None}. > +@end defmethod The two uses of `gdb.Value' so close to one another and meaning two different things might confuse the reader. Is it really important to make the point that the address attribute is itself a `gdb.Value'? If not, I'd suggest to rephrase: If the @code{gdb.Value} object is addressable, this read-only attribute holds its address. Thanks. ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [RFA][Python] Change gdb.Value.address from a method to an attribute. 2009-03-29 4:21 ` Eli Zaretskii @ 2009-03-29 18:12 ` Tom Tromey 2009-03-29 18:56 ` Eli Zaretskii 0 siblings, 1 reply; 10+ messages in thread From: Tom Tromey @ 2009-03-29 18:12 UTC (permalink / raw) To: Eli Zaretskii; +Cc: Thiago Jung Bauermann, gdb-patches >>>>> "Eli" == Eli Zaretskii <eliz@gnu.org> writes: >> +@defmethod Value address >> +If the @code{gdb.Value} object is addressable, this read-only attribute >> +holds a @code{gdb.Value} object representing the address. Otherwise, >> +this attribute holds @code{None}. >> +@end defmethod Eli> The two uses of `gdb.Value' so close to one another and meaning two Eli> different things might confuse the reader. Our readers are smarter than that. But perhaps the first "the @code{}" can be changed to "this". Eli> Is it really important to make the point that the address Eli> attribute is itself a `gdb.Value'? Yes. It is valuable for readers to know the possible types of method return values and of attributes. This gives users an idea of what operations can be done with the result. Tom ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [RFA][Python] Change gdb.Value.address from a method to an attribute. 2009-03-29 18:12 ` Tom Tromey @ 2009-03-29 18:56 ` Eli Zaretskii 2009-03-29 21:22 ` Thiago Jung Bauermann 0 siblings, 1 reply; 10+ messages in thread From: Eli Zaretskii @ 2009-03-29 18:56 UTC (permalink / raw) To: Tom Tromey; +Cc: bauerman, gdb-patches > Cc: Thiago Jung Bauermann <bauerman@br.ibm.com>, gdb-patches@sourceware.org > From: Tom Tromey <tromey@redhat.com> > Date: Sun, 29 Mar 2009 09:41:48 -0600 > > >>>>> "Eli" == Eli Zaretskii <eliz@gnu.org> writes: > > >> +@defmethod Value address > >> +If the @code{gdb.Value} object is addressable, this read-only attribute > >> +holds a @code{gdb.Value} object representing the address. Otherwise, > >> +this attribute holds @code{None}. > >> +@end defmethod > > Eli> The two uses of `gdb.Value' so close to one another and meaning two > Eli> different things might confuse the reader. > > Our readers are smarter than that. Including non-native English speakers? I doubt that. > But perhaps the first "the @code{}" can be changed to "this". Fine with me, thanks. ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [RFA][Python] Change gdb.Value.address from a method to an attribute. 2009-03-29 18:56 ` Eli Zaretskii @ 2009-03-29 21:22 ` Thiago Jung Bauermann 0 siblings, 0 replies; 10+ messages in thread From: Thiago Jung Bauermann @ 2009-03-29 21:22 UTC (permalink / raw) To: Eli Zaretskii; +Cc: Tom Tromey, gdb-patches El dom, 29-03-2009 a las 21:10 +0300, Eli Zaretskii escribió: > > From: Tom Tromey <tromey@redhat.com> > > Date: Sun, 29 Mar 2009 09:41:48 -0600 > > But perhaps the first "the @code{}" can be changed to "this". > > Fine with me, thanks. Ok, I committed the following. -- []'s Thiago Jung Bauermann IBM Linux Technology Center gdb/ Change gdb.Value.address from a method to an attribute. * python/python-value.c (value_object): Add `address' element. (valpy_dealloc): Decrement reference to self->address if set. (valpy_new): Initialize val_obj->address. (valpy_address): Rename to ... (valpy_get_address): ... this. Change signature from method to attribute. Update self->address if not set. (value_to_value_object): Initialize val_obj->address. (value_object_getset): Add `address' element. (value_object_methods): Remove `address' element. gdb/testsuite/ * gdb.python/python-value.exp: Add tests for the address attribute. gdb/doc/ * gdb.texinfo (Values From Inferior): Change gdb.Value.address from a method to an attribute. Index: src/gdb/doc/gdb.texinfo =================================================================== --- src.orig/gdb/doc/gdb.texinfo 2009-03-28 17:55:49.000000000 -0300 +++ src/gdb/doc/gdb.texinfo 2009-03-29 18:03:09.000000000 -0300 @@ -18325,9 +18325,15 @@ bar = some_val['foo'] Again, @code{bar} will also be a @code{gdb.Value} object. -The following attribute is provided: +The following attributes are provided: @table @code +@defmethod Value address +If this object is addressable, this read-only attribute holds a +@code{gdb.Value} object representing the address. Otherwise, +this attribute holds @code{None}. +@end defmethod + @cindex optimized out value in Python @defmethod Value is_optimized_out This read-only boolean attribute is true if the compiler optimized out Index: src/gdb/python/python-value.c =================================================================== --- src.orig/gdb/python/python-value.c 2009-03-28 17:55:49.000000000 -0300 +++ src/gdb/python/python-value.c 2009-03-28 17:55:51.000000000 -0300 @@ -59,6 +59,7 @@ typedef struct { PyObject_HEAD struct value *value; int owned_by_gdb; + PyObject *address; } value_object; /* Called by the Python interpreter when deallocating a value object. */ @@ -71,6 +72,13 @@ valpy_dealloc (PyObject *obj) if (!self->owned_by_gdb) value_free (self->value); + + if (self->address) + /* Use braces to appease gcc warning. *sigh* */ + { + Py_DECREF (self->address); + } + self->ob_type->tp_free (self); } @@ -105,6 +113,7 @@ valpy_new (PyTypeObject *subtype, PyObje value_obj->value = value; value_obj->owned_by_gdb = 0; + value_obj->address = NULL; release_value (value); value_prepend_to_list (&values_in_python, value); @@ -129,18 +138,30 @@ valpy_dereference (PyObject *self, PyObj /* Return "&value". */ static PyObject * -valpy_address (PyObject *self, PyObject *args) +valpy_get_address (PyObject *self, void *closure) { struct value *res_val = NULL; /* Initialize to appease gcc warning. */ + value_object *val_obj = (value_object *) self; volatile struct gdb_exception except; - TRY_CATCH (except, RETURN_MASK_ALL) + if (!val_obj->address) { - res_val = value_addr (((value_object *) self)->value); + TRY_CATCH (except, RETURN_MASK_ALL) + { + res_val = value_addr (val_obj->value); + } + if (except.reason < 0) + { + val_obj->address = Py_None; + Py_INCREF (Py_None); + } + else + val_obj->address = value_to_value_object (res_val); } - GDB_PY_HANDLE_EXCEPTION (except); - return value_to_value_object (res_val); + Py_INCREF (val_obj->address); + + return val_obj->address; } /* Implementation of gdb.Value.string ([encoding] [, errors]) -> string @@ -726,6 +747,7 @@ value_to_value_object (struct value *val { val_obj->value = val; val_obj->owned_by_gdb = 0; + val_obj->address = NULL; release_value (val); value_prepend_to_list (&values_in_python, val); } @@ -838,6 +860,8 @@ gdbpy_initialize_values (void) } static PyGetSetDef value_object_getset[] = { + { "address", valpy_get_address, NULL, "The address of the value.", + NULL }, { "is_optimized_out", valpy_get_is_optimized_out, NULL, "Boolean telling whether the value is optimized out (i.e., not available).", NULL }, @@ -845,7 +869,6 @@ static PyGetSetDef value_object_getset[] }; static PyMethodDef value_object_methods[] = { - { "address", valpy_address, METH_NOARGS, "Return the address of the value." }, { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." }, { "string", (PyCFunction) valpy_string, METH_VARARGS | METH_KEYWORDS, "string ([encoding] [, errors]) -> string\n\ Index: src/gdb/testsuite/gdb.python/python-value.exp =================================================================== --- src.orig/gdb/testsuite/gdb.python/python-value.exp 2009-03-28 17:55:49.000000000 -0300 +++ src/gdb/testsuite/gdb.python/python-value.exp 2009-03-28 17:55:51.000000000 -0300 @@ -70,6 +70,9 @@ proc test_value_creation {} { gdb_py_test_silent_cmd "python a = gdb.Value (u'unicode test')" "create unicode value" 1 gdb_test "python print a" "\"unicode test\"" "print Unicode string" gdb_test "python print a.__class__" "<type 'gdb.Value'>" "verify type of unicode string" + + # Test address attribute is None in a non-addressable value + gdb_test "python print 'result =', i.address" "= None" "Test address attribute in non-addressable value" } proc test_value_numeric_ops {} { @@ -228,6 +231,9 @@ proc test_value_in_inferior {} { # Smoke-test is_optimized_out attribute gdb_test "python print 'result =', arg0.is_optimized_out" "= False" "Test is_optimized_out attribute" + + # Test address attribute + gdb_test "python print 'result =', arg0.address" "= 0x\[\[:xdigit:\]\]+" "Test address attribute" } ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [RFA][Python] Change gdb.Value.address from a method to an attribute. 2009-03-28 21:38 ` [RFA][Python] Change gdb.Value.address from a method to an attribute Thiago Jung Bauermann 2009-03-29 4:21 ` Eli Zaretskii @ 2009-03-29 15:42 ` Tom Tromey 1 sibling, 0 replies; 10+ messages in thread From: Tom Tromey @ 2009-03-29 15:42 UTC (permalink / raw) To: Thiago Jung Bauermann; +Cc: gdb-patches >>>>> "Thiago" == Thiago Jung Bauermann <bauerman@br.ibm.com> writes: Thiago> By the way, when writing this patch I realised that probably the Thiago> "address" method in Value would better be turned into an attribute. Tom> Do it :) Thiago> Just did it™. Ok to commit? Yes, thanks. Tom ^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2009-03-29 21:16 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <orskllhfo3.fsf@oliva.athome.lsd.ic.unicamp.br>
[not found] ` <20090310125945.GA4376@caradoc.them.org>
[not found] ` <1236700079.11106.1.camel@localhost.localdomain>
[not found] ` <orzlftfhu1.fsf@oliva.athome.lsd.ic.unicamp.br>
[not found] ` <1236710732.11106.30.camel@localhost.localdomain>
[not found] ` <m3hc1rluz1.fsf@fleche.redhat.com>
2009-03-23 2:09 ` [RFC][Python] Re: any expression to tell whether a variable was optimized out? Thiago Jung Bauermann
2009-03-23 4:24 ` Eli Zaretskii
2009-03-26 21:21 ` Thiago Jung Bauermann
2009-03-23 17:47 ` Tom Tromey
2009-03-28 21:38 ` [RFA][Python] Change gdb.Value.address from a method to an attribute Thiago Jung Bauermann
2009-03-29 4:21 ` Eli Zaretskii
2009-03-29 18:12 ` Tom Tromey
2009-03-29 18:56 ` Eli Zaretskii
2009-03-29 21:22 ` Thiago Jung Bauermann
2009-03-29 15:42 ` Tom Tromey
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox