* Cast to result of whatis expression
@ 2010-09-07 22:04 Kenneth Rose
2010-09-07 22:16 ` Tom Tromey
0 siblings, 1 reply; 5+ messages in thread
From: Kenneth Rose @ 2010-09-07 22:04 UTC (permalink / raw)
To: gdb
Hi all,
I'm writing something similar to gdb-stl-views for some custom container
classes that we have. As opposed to the STL containers though, our
containers cast the datatype to void*. A minimalistic example looks like:
class ContainerBase
{
protected:
ContainerBase( void *x ) : m_x( x ) {}
void *m_x;
};
template<typename T>
class Container : public ContainerBase
{
public:
Container() : ContainerBase( new T ) {}
T& get() { return *static_cast<T*>( m_x ); }
};
Say I have the following code somewhere:
Container<int> foo;
I'd like to be able to have a gdb command like "pcontainer foo" that shows
me something like "The value in foo is 0". In terms of implementing such
a command, I cannot just call get() because I get the dreaded "Cannot
evaluate function -- may be inlined" message. I need to explicitly cast
foo.m_x to the appropriate type, but I'm at a loss as to how to get that
type from gdb. "whatis foo" gives me back "Container<int>". Is there
anyway of extracting the "int" part of that result and using that as the
type to cast to? The following is not valid, but this is what I'm going
for....
set $type = regex 'whatis foo' Container< \1 >
set $result = *(($type *) foo.m_x)
I'm using the stock GDB that ships with Mac OS X 10.6 (GNU gdb
6.3.50-20050815 (Apple version gdb-1461). I know... it's old.
Thanks for your help.
-- kenrose
P.S. - I could have something like "pcontainer foo int", but I'm trying to
avoid that since gdb already knows the type of foo (as Container<int>).
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: Cast to result of whatis expression
2010-09-07 22:04 Cast to result of whatis expression Kenneth Rose
@ 2010-09-07 22:16 ` Tom Tromey
2010-09-10 18:09 ` Kenneth Rose
0 siblings, 1 reply; 5+ messages in thread
From: Tom Tromey @ 2010-09-07 22:16 UTC (permalink / raw)
To: Kenneth Rose; +Cc: gdb
>>>>> "Kenneth" == Kenneth Rose <kenrose@kenrose.org> writes:
Kenneth> I'm writing something similar to gdb-stl-views for some custom
Kenneth> container classes that we have.
I think that these days it is better to use the Python pretty-printing
infrastructure instead. It has a number of advantages -- it works no
matter how the object is printed (in a stack trace, embedded in some
other structure), and it works with MI.
Kenneth> I need to explicitly cast foo.m_x to the appropriate type, but
Kenneth> I'm at a loss as to how to get that type from gdb. "whatis
Kenneth> foo" gives me back "Container<int>". Is there anyway of
Kenneth> extracting the "int" part of that result and using that as the
Kenneth> type to cast to?
You can do this in Python :-)
You could probably do it (with some difficulty) using a combination of
logging, "shell", and "source".
Kenneth> I'm using the stock GDB that ships with Mac OS X 10.6 (GNU gdb
Kenneth> 6.3.50-20050815 (Apple version gdb-1461). I know... it's old.
Ah, Apple's fork. Unfortunately it is Python-free.
You could try GDB 7.2. It has some known bugs on Mac OS, but I have
also heard reports of it work.
Tom
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Cast to result of whatis expression
2010-09-07 22:16 ` Tom Tromey
@ 2010-09-10 18:09 ` Kenneth Rose
2010-09-10 19:57 ` Tom Tromey
0 siblings, 1 reply; 5+ messages in thread
From: Kenneth Rose @ 2010-09-10 18:09 UTC (permalink / raw)
To: Tom Tromey; +Cc: gdb
>>>>>> "Kenneth" == Kenneth Rose <gdb@kenrose.org> writes:
>
> Kenneth> I'm writing something similar to gdb-stl-views for some custom
> Kenneth> container classes that we have.
>
> I think that these days it is better to use the Python pretty-printing
> infrastructure instead. It has a number of advantages -- it works no
> matter how the object is printed (in a stack trace, embedded in some
> other structure), and it works with MI.
>
> Kenneth> I need to explicitly cast foo.m_x to the appropriate type, but
> Kenneth> I'm at a loss as to how to get that type from gdb. "whatis
> Kenneth> foo" gives me back "Container<int>". Is there anyway of
> Kenneth> extracting the "int" part of that result and using that as the
> Kenneth> type to cast to?
>
> You can do this in Python :-)
>
> You could probably do it (with some difficulty) using a combination of
> logging, "shell", and "source".
>
> Kenneth> I'm using the stock GDB that ships with Mac OS X 10.6 (GNU gdb
> Kenneth> 6.3.50-20050815 (Apple version gdb-1461). I know... it's old.
>
> Ah, Apple's fork. Unfortunately it is Python-free.
>
> You could try GDB 7.2. It has some known bugs on Mac OS, but I have
> also heard reports of it work.
>
> Tom
>
Thanks Tom. Unfortunately, this is for a bunch of developers and I can't
make everyone upgrade to GDB 7.2. That said, when I was researching how
to do this, the Python stuff looked uber sexy (and uber practical).
I played around with sending the result of "whatis foo" to a shell
command, but couldn't figure out how to actually pass an argument to the
inferior shell with gdb. The shell command seems to literally interpret
its arguments. I tried playing with 'set environment', but the inferior
shell didn't seem to pick up any of the env. variable declarations.
Anyways, I've ended up just passing the type to my pvector command. For
example, "pvector myContainer int". It's not so bad because I can get gdb
to tell me the container's value type with whatis. I just need to
manually copy and paste it myself as an argument to pvector. Since I run
gdb in emacs, this is pretty easy.
Thanks again,
-- kenrose
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Cast to result of whatis expression
2010-09-10 18:09 ` Kenneth Rose
@ 2010-09-10 19:57 ` Tom Tromey
0 siblings, 0 replies; 5+ messages in thread
From: Tom Tromey @ 2010-09-10 19:57 UTC (permalink / raw)
To: Kenneth Rose; +Cc: gdb
Kenneth> Thanks Tom. Unfortunately, this is for a bunch of developers
Kenneth> and I can't make everyone upgrade to GDB 7.2. That said, when
Kenneth> I was researching how to do this, the Python stuff looked uber
Kenneth> sexy (and uber practical).
Yeah. Try harder to upgrade!
Kenneth> I played around with sending the result of "whatis foo" to a shell
Kenneth> command, but couldn't figure out how to actually pass an
Kenneth> argument to the inferior shell with gdb.
The way to do complicated scripting in pre-python gdb is horrible:
1. Use set logging to write to a temporary file.
2. Run a gdb command that generates the output you need.
3. Use "shell" (and the scripting of your choice: sed, awk, perl...)
to rewrite the temp file to a second temp file that is in
gdb CLI syntax.
4. "source" the second temp file
Kenneth> I just need to manually copy and paste it myself as an argument
Kenneth> to pvector. Since I run gdb in emacs, this is pretty easy.
Of course, you could hack gud-mode to do this automatically :-)
Tom
^ permalink raw reply [flat|nested] 5+ messages in thread
* Cast to result of whatis expression
@ 2010-09-07 22:04 Kenneth Rose
0 siblings, 0 replies; 5+ messages in thread
From: Kenneth Rose @ 2010-09-07 22:04 UTC (permalink / raw)
To: gdb
Hi all,
I'm writing something similar to gdb-stl-views for some custom container
classes that we have. As opposed to the STL containers though, our
containers cast the datatype to void*. A minimalistic example looks like:
class ContainerBase
{
protected:
ContainerBase( void *x ) : m_x( x ) {}
void *m_x;
};
template<typename T>
class Container : public ContainerBase
{
public:
Container() : ContainerBase( new T ) {}
T& get() { return *static_cast<T*>( m_x ); }
};
Say I have the following code somewhere:
Container<int> foo;
I'd like to be able to have a gdb command like "pcontainer foo" that shows
me something like "The value in foo is 0". In terms of implementing such
a command, I cannot just call get() because I get the dreaded "Cannot
evaluate function -- may be inlined" message. I need to explicitly cast
foo.m_x to the appropriate type, but I'm at a loss as to how to get that
type from gdb. "whatis foo" gives me back "Container<int>". Is there
anyway of extracting the "int" part of that result and using that as the
type to cast to? The following is not valid, but this is what I'm going
for....
set $type = regex 'whatis foo' Container< \1 >
set $result = *(($type *) foo.m_x)
I'm using the stock GDB that ships with Mac OS X 10.6 (GNU gdb
6.3.50-20050815 (Apple version gdb-1461). I know... it's old.
Thanks for your help.
-- kenrose
P.S. - I could have something like "pcontainer foo int", but I'm trying to
avoid that since gdb already knows the type of foo (as Container<int>).
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2010-09-10 19:57 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-09-07 22:04 Cast to result of whatis expression Kenneth Rose
2010-09-07 22:16 ` Tom Tromey
2010-09-10 18:09 ` Kenneth Rose
2010-09-10 19:57 ` Tom Tromey
-- strict thread matches above, loose matches on Subject: below --
2010-09-07 22:04 Kenneth Rose
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox