Discussion:
pretty printing of smart pointers and making function calls
Vladimir
2012-01-25 13:18:21 UTC
Permalink
Hi All,

I have a couple of question related to Python pretty printers. I tried to search
for the answer on the web but had no luck.

1. For example, I want to implement a pretty printer for variable a of type
std::auto_ptr<std::string>. It's rather straight forward so I will not add here
my implementation of such printer. Then I use it as follows:

(gdb) print a
$1 = std::auto_ptr<std::string> 0x804d030

Now I want to take a look at what is the value inside that pointer.

(gdb) print *a
Could not find operator*.

How can I implement a printer for the second case?

2) Another question is about how to call from pretty printer a function from my
currently debugged process. For example, I have an unsigned int variable which
contains a hash of a string and there is a function that can map the hash to the
original string. To make the unsigned int value a bit more descriptive I want to
call the function and print corresponding string. Actually I managed to make
such call but the result looks ugly:

def to_string(self):
crc = self.val
s = gdb.execute('call hash_to_string(%d)' % crc, to_string=True)
return ('crc = 0x%08X : \'%s\'' % (crc, s)))

(gdb) print crc
$1 = crc = 0x0024A837 : '$2 = "qerrrr"
'

Is there any way to obtain the result of the function call as gdb.Value?
Tom Tromey
2012-01-31 17:53:56 UTC
Permalink
Vladimir> I have a couple of question related to Python pretty
Vladimir> printers. I tried to search for the answer on the web but had
Vladimir> no luck.

Vladimir> (gdb) print *a
Vladimir> Could not find operator*.

Vladimir> How can I implement a printer for the second case?

gdb is giving an error here before it computes a value to print.

I forget if operator* is supposed to be working now or not.
I think some operators still aren't really working in gdb; but often
they will fail anyway because they were not compiled into your program.

You can work around this a little by having the base printer also print
the pointed-to value. This is what the libstdc++ iterator printers do
(for now).

There is a bug open about the missing operators problem:

http://sourceware.org/bugzilla/show_bug.cgi?id=12937

Vladimir> 2) Another question is about how to call from pretty printer a
Vladimir> function from my currently debugged process. For example, I
Vladimir> have an unsigned int variable which contains a hash of a
Vladimir> string and there is a function that can map the hash to the
Vladimir> original string. To make the unsigned int value a bit more
Vladimir> descriptive I want to call the function and print
Vladimir> corresponding string. Actually I managed to make such call but
Vladimir> the result looks ugly:

Vladimir> def to_string(self):
Vladimir> crc = self.val
Vladimir> s = gdb.execute('call hash_to_string(%d)' % crc, to_string=True)
Vladimir> return ('crc = 0x%08X : \'%s\'' % (crc, s)))

Vladimir> (gdb) print crc
Vladimir> $1 = crc = 0x0024A837 : '$2 = "qerrrr"
Vladimir> '

Vladimir> Is there any way to obtain the result of the function call as
Vladimir> gdb.Value?

Yes, it can be done. What you have to do is get the address of the
function as a value. There's no super way to do this right now:

http://sourceware.org/bugzilla/show_bug.cgi?id=12027

Once you have that you can call the Value, this is explained in the
docs:

A `gdb.Value' that represents a function can be executed via
inferior function call. Any arguments provided to the call must match
the function's prototype, and must be provided in the order specified
by that prototype.

For example, `some_val' is a `gdb.Value' instance representing a
function that takes two integers as arguments. To execute this
function, call it like so:

result = some_val (10,20)


However, you usually don't want to call functions when pretty-printing.
For one thing, this means core-file debugging won't work.
It is better, when possible, to do everything in Python instead.

Tom
Vladimir
2012-02-08 16:35:39 UTC
Permalink
Hi,
Post by Tom Tromey
You can work around this a little by having the base printer also print
the pointed-to value. This is what the libstdc++ iterator printers do
(for now).
I'm not sure I understand you correctly. Do you mean that if the user asks for a
value of variable a and pretty printer knows that it's a pointer then along with
pointer address it should print the value the pointer points too? Anyway some
reference to document or example would be useful here.
Post by Tom Tromey
Yes, it can be done. What you have to do is get the address of the
http://sourceware.org/bugzilla/show_bug.cgi?id=12027
The link above mentions gdb.parse_and_eval() which does exactly what I need.
Post by Tom Tromey
However, you usually don't want to call functions when pretty-printing.
For one thing, this means core-file debugging won't work.
It is better, when possible, to do everything in Python instead.
But you are right that this doesn't work with core-file. Is there any way to
check in Python whether I debug alive process or see its dead body?

Thanks,

Vladimir
Tom Tromey
2012-02-22 21:38:32 UTC
Permalink
This post might be inappropriate. Click to display it.
Loading...