public inbox for archer@sourceware.org
 help / color / mirror / Atom feed
* pretty printing of smart pointers and making function calls
@ 2012-01-25 13:20 Vladimir
  2012-01-31 18:26 ` Tom Tromey
  0 siblings, 1 reply; 4+ messages in thread
From: Vladimir @ 2012-01-25 13:20 UTC (permalink / raw)
  To: archer

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?

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

* Re: pretty printing of smart pointers and making function calls
  2012-01-25 13:20 pretty printing of smart pointers and making function calls Vladimir
@ 2012-01-31 18:26 ` Tom Tromey
  2012-02-08 16:36   ` Vladimir
  0 siblings, 1 reply; 4+ messages in thread
From: Tom Tromey @ 2012-01-31 18:26 UTC (permalink / raw)
  To: Vladimir; +Cc: archer

>>>>> "Vladimir" == Vladimir  <volo.zyko@gmail.com> writes:

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

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

* Re: pretty printing of smart pointers and making function calls
  2012-01-31 18:26 ` Tom Tromey
@ 2012-02-08 16:36   ` Vladimir
  2012-02-22 21:38     ` Tom Tromey
  0 siblings, 1 reply; 4+ messages in thread
From: Vladimir @ 2012-02-08 16:36 UTC (permalink / raw)
  To: archer

Hi,

> 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.

> 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

The link above mentions gdb.parse_and_eval() which does exactly what I need.

> 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

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

* Re: pretty printing of smart pointers and making function calls
  2012-02-08 16:36   ` Vladimir
@ 2012-02-22 21:38     ` Tom Tromey
  0 siblings, 0 replies; 4+ messages in thread
From: Tom Tromey @ 2012-02-22 21:38 UTC (permalink / raw)
  To: Vladimir; +Cc: archer

>>>>> "Vladimir" == Vladimir  <volo.zyko@gmail.com> writes:

Sorry about the long delay here.
I managed to forget about this thread somehow :(

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

Vladimir> I'm not sure I understand you correctly. Do you mean that if
Vladimir> the user asks for a value of variable a and pretty printer
Vladimir> knows that it's a pointer then along with pointer address it
Vladimir> should print the value the pointer points too? Anyway some
Vladimir> reference to document or example would be useful here.

E.g., the libstdc++ printers for iterators print the referred-to object
as well.  Here's a simple one:

class StdSlistIteratorPrinter:
    "Print __gnu_cxx::slist::iterator"

    def __init__(self, typename, val):
        self.val = val

    def to_string(self):
        nodetype = find_type(self.val.type, '_Node')
        nodetype = nodetype.strip_typedefs().pointer()
        return self.val['_M_node'].cast(nodetype).dereference()['_M_data']

This looks into the iterator representation, finds the pointer to the
iterated-over data, and prints that.

This is a hack, but often a useful one.

Vladimir> But you are right that this doesn't work with core-file. Is
Vladimir> there any way to check in Python whether I debug alive process
Vladimir> or see its dead body?

Nothing direct but as a workaround you can often find this stuff using
gdb.execute(..., to_string=True) and then parsing the result.
The output here might change between releases, though, so it is
generally better to get a real Python API.

Tom

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

end of thread, other threads:[~2012-02-22 21:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-01-25 13:20 pretty printing of smart pointers and making function calls Vladimir
2012-01-31 18:26 ` Tom Tromey
2012-02-08 16:36   ` Vladimir
2012-02-22 21:38     ` Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).