Bug 14077 - Dynamic type of references cannot be accessed
Summary: Dynamic type of references cannot be accessed
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: python (show other bugs)
Version: HEAD
: P2 enhancement
Target Milestone: 7.8
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-05-08 10:21 UTC by Andre'
Modified: 2020-04-28 16:24 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andre' 2012-05-08 10:21:42 UTC
echo "
struct A { virtual ~A() {} int a; };
struct B : A {};

int main()
{
    B b;
    A *b1 = &b;
    A &b2 = b;
    return b1->a + b2.a;
}
" | g++ -g -xc++ -

~/debugger/gdb-git-i586/gdb/gdb \
    -ex "set confirm off" \
    -ex "file ./a.out" \
    -ex "b 10" \
    -ex run \
    -ex "python print 'Pointer   static  type : %s' % gdb.parse_and_eval('b1').type" \
    -ex "python print 'Pointer   dynamic type : %s' % gdb.parse_and_eval('b1').dynamic_type" \
    -ex "python print 'Reference static  type : %s' % gdb.parse_and_eval('b2').type" \
    -ex "python print 'Reference dynamic type : %s' % gdb.parse_and_eval('b2').dynamic_type" 



yields:

    Pointer   static  type : A *
    Pointer   dynamic type : B *
    Reference static  type : A &
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
    gdb.error: Attempt to take contents of a non-pointer value.
    Error while executing Python code.


It would be convenient to see   

    Reference dynamic type : B &

in the last line
Comment 1 Andre' 2012-05-08 17:01:31 UTC
The following seems to fix the issue for me:

diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index 6f67bdb..bfb7260 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -310,7 +310,11 @@ valpy_get_dynamic_type (PyObject *self, void *closure)
          struct value *target;
          int was_pointer = TYPE_CODE (type) == TYPE_CODE_PTR;
 
-         target = value_ind (val);
+         if (was_pointer)
+           target = value_ind (val);
+         else
+           target = coerce_ref (val);
+
          type = value_rtti_type (target, NULL, NULL, NULL);
 
          if (type)
Comment 2 Tom Tromey 2013-06-13 16:29:30 UTC
Thread is here:
http://sourceware.org/ml/gdb-patches/2012-05/msg00394.html
Comment 3 Andre' 2014-03-24 16:05:49 UTC
There has been another patch on the list, see https://sourceware.org/ml/gdb-patches/2014-03/msg00542.html
Comment 4 Hannes Domani 2020-04-28 16:24:30 UTC
Fixed since version 7.8, with this commit:
https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=7af389b892404edc76e1a60c59b354b785378fa5