Mirror of the gdb mailing list
 help / color / mirror / Atom feed
From: Joachim Protze <joachim.protze@wh2.tu-dresden.de>
To: Andrew Oakley <andrew@ado.is-a-geek.net>
Cc: gdb@sourceware.org
Subject: Re: Python API - pretty printing complex types
Date: Wed, 09 Mar 2011 08:06:00 -0000	[thread overview]
Message-ID: <4D77352D.3010004@wh2.tu-dresden.de> (raw)
In-Reply-To: <20110309004619.7256b052@ado-gentoo>

My first approach makes use of the undocumented (online-doc) 
array-method of gdb.Type, that i put in a handy function -- the straight 
forward way.
For the second approach you have to put a typedef into your source -- 
the more flexible way for complex situations.

On 09.03.2011 01:46, Andrew Oakley wrote:
> struct value_type { ... };
>
> struct container {
>          int interesting_field1;
>          int interesting_field2;
>
>          size_t values_length1;
>          struct value_type * values1;
>
>          size_t values_length2;
>          struct value_type * values2;
> };
>
def cast_pointer_to_array(pointer, length):
     return 
pointer.cast(pointer.dereference().type.array(length-1).pointer()).dereference()

class container_printer:
     [...]
     def children(self):
         yield ("interesting_field1", self.val["interesting_field1"])
         yield ("interesting_field2", self.val["interesting_field2"])
         yield ("members1", cast_pointer_to_array(self.val["values1"], 
self.val["values_length1"])
         yield ("members2", cast_pointer_to_array(self.val["values2"], 
self.val["values_length2"])

     def display_hint (self):
         return "struct"

This way you get 2 Arrays of member-values
> Ideally my pretty printer would output something like this:
>
> container = {
>    interesting_field1 = 42,
>    interesting_field2 = 0,
>    members = {
>          { value1 },
>          { value2 },
>          { value3 }
>    }
> }
>
To get one single array use the second approach:


typedef struct container container_helper_type;

class container_printer:
     [...]
     def children(self):
         yield ("interesting_field1", self.val["interesting_field1"])
         yield ("interesting_field2", self.val["interesting_field2"])
         yield ("members", 
self.val.cast(gdb.lookup_type("container_helper_type")))

     def display_hint (self):
         return "struct"

class container_helper_type_printer:
     [...]
     def children(self):
         for i in range(self.val["values_length1"]):
             yield ("", self.val["values1"][i])
         for i in range(self.val["values_length2"]):
             yield ("", self.val["values2"][i])

     def display_hint (self):
         return "array"


  reply	other threads:[~2011-03-09  8:06 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-09  0:43 Andrew Oakley
2011-03-09  8:06 ` Joachim Protze [this message]
2011-03-10 21:08   ` Tom Tromey
     [not found] ` <201103090954.49355.andre.poenitz@nokia.com>
2011-03-09 19:28   ` Andrew Oakley
2011-03-10  9:07     ` André Pönitz
2011-03-10 21:25       ` Tom Tromey
2011-03-11  7:41         ` Joachim Protze
2011-03-11 11:25         ` André Pönitz
2011-03-10 21:11     ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4D77352D.3010004@wh2.tu-dresden.de \
    --to=joachim.protze@wh2.tu-dresden.de \
    --cc=andrew@ado.is-a-geek.net \
    --cc=gdb@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox