Mirror of the gdb mailing list
 help / color / mirror / Atom feed
* getting subclass type from base class pointer
@ 2015-10-29 19:41 Paul Marquess
  2015-10-29 23:38 ` Paul Marquess
  0 siblings, 1 reply; 4+ messages in thread
From: Paul Marquess @ 2015-10-29 19:41 UTC (permalink / raw)
  To: gdb

I must be missing something obvious, so apologies upfront.

Consider this simple snippet of C++


class SuperClass 
{
};

class SubClass : public SuperClass 
{
};

int main()
{
    SuperClass* pTest = new SubClass;

    delete pTest; // << Break here in GDB
}

Using the python API I'm trying to determine the type of object that pTest points to. 


gdb) python x = gdb.parse_and_eval("pTest")
(gdb) python print x.type
SuperClass *
(gdb) python print x.dereference().type
SuperClass

How do I get at the SubClass object using the Python API?

cheers
Paul

PS running GDB 7.10

$ gdb -v
GNU gdb (GDB) 7.10




^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: getting subclass type from base class pointer
  2015-10-29 19:41 getting subclass type from base class pointer Paul Marquess
@ 2015-10-29 23:38 ` Paul Marquess
  2015-10-30  7:20   ` André Pönitz
  0 siblings, 1 reply; 4+ messages in thread
From: Paul Marquess @ 2015-10-29 23:38 UTC (permalink / raw)
  To: gdb

> From: gdb-owner@sourceware.org [mailto:gdb-owner@sourceware.org] On Behalf Of Paul Marquess
> 
> I must be missing something obvious, so apologies upfront.
> 
> Consider this simple snippet of C++
> 
> 
> class SuperClass
> {
> };
> 
> class SubClass : public SuperClass
> {
> };
> 
> int main()
> {
>     SuperClass* pTest = new SubClass;
> 
>     delete pTest; // << Break here in GDB }
> 
> Using the python API I'm trying to determine the type of object that pTest points to. 
> 
> 
> gdb) python x = gdb.parse_and_eval("pTest")
> (gdb) python print x.type
> SuperClass *
> (gdb) python print x.dereference().type
> SuperClass
> 
> How do I get at the SubClass object using the Python API?


Quick follow-up to my own post, the actual code I'm working with is more like shown below, so I there should be a vtable in play. 
Same question - how get at the SubClass object if I have a pointer to a SuperClass object using the Python API?


class SuperClass 
{
    public:
    virtual int fred() { return 1; }
    SuperClass() {}
    virtual ~SuperClass() {}
};

class SubClass : public SuperClass 
{
    public:
    virtual int fred() { return 2 ;}
    SubClass() {}
    virtual ~SubClass() {}
};

int main()
{
    SuperClass* pTest = new SubClass;

    delete pTest;
}


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: getting subclass type from base class pointer
  2015-10-29 23:38 ` Paul Marquess
@ 2015-10-30  7:20   ` André Pönitz
  2015-10-30 11:26     ` Paul Marquess
  0 siblings, 1 reply; 4+ messages in thread
From: André Pönitz @ 2015-10-30  7:20 UTC (permalink / raw)
  To: Paul Marquess; +Cc: gdb

On Thu, Oct 29, 2015 at 11:38:27PM +0000, Paul Marquess wrote:
> > gdb) python x = gdb.parse_and_eval("pTest")
> > (gdb) python print x.type
> > SuperClass *

(gdb) python print(x.type)
SuperClass *
(gdb) python print(x.dynamic_type)
SubClass *

Andre'


^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: getting subclass type from base class pointer
  2015-10-30  7:20   ` André Pönitz
@ 2015-10-30 11:26     ` Paul Marquess
  0 siblings, 0 replies; 4+ messages in thread
From: Paul Marquess @ 2015-10-30 11:26 UTC (permalink / raw)
  To: André Pönitz; +Cc: gdb

From: André Pönitz [mailto:apoenitz@t-online.de] 
 
> On Thu, Oct 29, 2015 at 11:38:27PM +0000, Paul Marquess wrote:
> > > gdb) python x = gdb.parse_and_eval("pTest")
> > > (gdb) python print x.type
> > > SuperClass *
> 
> (gdb) python print(x.type)
> SuperClass *
> (gdb) python print(x.dynamic_type)
> SubClass *

Bingo! That did it.

Hmm, could have sworn that I tried that myself (but obviously I'm deluding myself).

Thanks for the help
Paul


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-10-30 11:26 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-10-29 19:41 getting subclass type from base class pointer Paul Marquess
2015-10-29 23:38 ` Paul Marquess
2015-10-30  7:20   ` André Pönitz
2015-10-30 11:26     ` Paul Marquess

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox