From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 2521 invoked by alias); 23 Mar 2009 01:31:20 -0000 Received: (qmail 2512 invoked by uid 22791); 23 Mar 2009 01:31:19 -0000 X-SWARE-Spam-Status: No, hits=-2.5 required=5.0 tests=AWL,BAYES_00,J_CHICKENPOX_37,SPF_PASS X-Spam-Check-By: sourceware.org Received: from e24smtp04.br.ibm.com (HELO e24smtp04.br.ibm.com) (32.104.18.25) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 23 Mar 2009 01:31:11 +0000 Received: from mailhub1.br.ibm.com (mailhub1.br.ibm.com [9.18.232.109]) by e24smtp04.br.ibm.com (8.13.1/8.13.1) with ESMTP id n2N1RsFg016843 for ; Sun, 22 Mar 2009 22:27:54 -0300 Received: from d24av01.br.ibm.com (d24av01.br.ibm.com [9.18.232.46]) by mailhub1.br.ibm.com (8.13.8/8.13.8/NCO v9.2) with ESMTP id n2N1VPZ91323284 for ; Sun, 22 Mar 2009 22:31:25 -0300 Received: from d24av01.br.ibm.com (loopback [127.0.0.1]) by d24av01.br.ibm.com (8.12.11.20060308/8.13.3) with ESMTP id n2N1V7IC031938 for ; Sun, 22 Mar 2009 22:31:07 -0300 Received: from [9.8.1.116] ([9.8.1.116]) by d24av01.br.ibm.com (8.12.11.20060308/8.12.11) with ESMTP id n2N1V6X3031932; Sun, 22 Mar 2009 22:31:06 -0300 Subject: [RFC][Python] Re: any expression to tell whether a variable was optimized out? From: Thiago Jung Bauermann To: Tom Tromey Cc: Alexandre Oliva , gdb-patches@sourceware.org In-Reply-To: References: <20090310125945.GA4376@caradoc.them.org> <1236700079.11106.1.camel@localhost.localdomain> <1236710732.11106.30.camel@localhost.localdomain> Content-Type: text/plain; charset=UTF-8 Date: Mon, 23 Mar 2009 02:09:00 -0000 Message-Id: <1237771863.25721.17.camel@localhost.localdomain> Mime-Version: 1.0 Content-Transfer-Encoding: 8bit X-IsSubscribed: yes 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 X-SW-Source: 2009-03/txt/msg00474.txt.bz2 [ Moving thread from the gdb@ ml. ] El mar, 17-03-2009 a las 15:06 -0600, Tom Tromey escribió: > >>>>> "Thiago" == Thiago Jung Bauermann 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