Bug 11792 - Provide access to the dynamic type of an object
Summary: Provide access to the dynamic type of an object
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: python (show other bugs)
Version: 7.1
: P2 enhancement
Target Milestone: 7.3
Assignee: Tom Tromey
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-07-06 16:00 UTC by Andre'
Modified: 2010-08-30 20:30 UTC (History)
2 users (show)

See Also:
Host: i486-linux-gnu
Target: i486-linux-gnu
Build: i486-linux-gnu
Last reconfirmed: 2010-08-23 17:58:14


Attachments
proof-of-concept code, mainly copied code from a path triggered by 'set print object on' (950 bytes, text/plain)
2010-07-07 10:26 UTC, Andre'
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Andre' 2010-07-06 16:00:15 UTC
There seems to be no easy way to get access to the dynamic type of a C++ object.
The problem is visible in the following script:

=================== snip ======================

rm -rf s && mkdir s && cd s

echo -e "
    class Base { public: virtual ~Base() {} };
    class Derived : public Base {};
    int main() {
        Base *b = new Derived;
        return (int)b;
    }" | g++ -g -xc++ -

gdb -ex 'file a.out' \
    -ex 'start' \
    -ex 'next' \
    -ex 'print b' \
    -ex 'set print object on' \
    -ex 'print b' \
    -ex 'python import gdb' \
    -ex 'python print "Python says: '%s'" % gdb.parse_and_eval("b").type' \
    -ex 'set confirm off' \
    -ex 'quit' 

=================== snip ======================


The result is:

  $1 = (Base *) 0x804b008
  $2 = (Derived *) 0x804b008
  Python says: Base *



It would be nice if there was a method accessible by Python that produces
"Derived *"
Comment 1 Andre' 2010-07-07 10:26:47 UTC
Created attachment 4872 [details]
proof-of-concept code, mainly copied code from a path triggered by 'set print object on'

With the attached patch and adding 

  -ex 'python print "Python says: '%s'" % gdb.parse_and_eval("b").dynamic_type'


to the test script I get

Temporary breakpoint 1, main () at <stdin>:5
5	<stdin>: No such file or directory.
	in <stdin>
6	in <stdin>
$1 = (Base *) 0x804b008
$2 = (Derived *) 0x804b008
Python says: Base *
Python says: Derived *
Comment 2 Tom Tromey 2010-08-20 21:56:55 UTC
This patch looks pretty good.
Do you have paperwork in place?  If not, can you get it?
Comment 3 Andre' 2010-08-23 09:02:19 UTC
(In reply to comment #2)
> This patch looks pretty good.

It's mostly original gdb code.

> Do you have paperwork in place?  

Yes, there's one for the company.


Comment 4 Sourceware Commits 2010-08-30 20:28:49 UTC
Subject: Bug 11792

CVSROOT:	/cvs/src
Module name:	src
Changes by:	tromey@sourceware.org	2010-08-30 20:28:32

Modified files:
	gdb            : ChangeLog 
	gdb/doc        : ChangeLog gdb.texinfo 
	gdb/python     : py-value.c 
	gdb/testsuite  : ChangeLog 
	gdb/testsuite/gdb.python: py-value.exp 

Log message:
	gdb
	2010-08-30  Andre Poenitz  <andre.poenitz@nokia.com>
	Tom Tromey  <tromey@redhat.com>
	
	PR python/11792:
	* python/py-value.c (valpy_get_dynamic_type): New function.
	(value_object_getset): Add "dynamic_type".
	(valpy_get_type): Fail on error.
	gdb/doc
	PR python/11792:
	* gdb.texinfo (Values From Inferior): Document dynamic_type.
	gdb/testsuite
	PR python/11792:
	* gdb.python/py-value.exp (test_subscript_regression): Add
	dynamic_type test.

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/ChangeLog.diff?cvsroot=src&r1=1.12126&r2=1.12127
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/doc/ChangeLog.diff?cvsroot=src&r1=1.1106&r2=1.1107
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/doc/gdb.texinfo.diff?cvsroot=src&r1=1.759&r2=1.760
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/python/py-value.c.diff?cvsroot=src&r1=1.15&r2=1.16
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/testsuite/ChangeLog.diff?cvsroot=src&r1=1.2432&r2=1.2433
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/testsuite/gdb.python/py-value.exp.diff?cvsroot=src&r1=1.15&r2=1.16

Comment 5 Tom Tromey 2010-08-30 20:30:27 UTC
Fix checked in.