1

the main structure is

struct my_struct 
{
   int x; 
   void* md_template;
   void* md_capture_buff;
   ....
};

When i am doing

(gdb) p ((struct my_struct *)dev_base->next->priv)

The output is like this

$1 = {
 x= 15 '\017'
 md_template = ,
 md_capture_buff =
}

And when i am doing it with p/x:

(gdb) p/x ((struct my_struct *)dev_base->next->priv)

The output is like this

$1 = {
 x= 0xf;
 md_template =0x410027001a50 ,
 md_capture_buff = 0x41002c0c5490
}

In gdb-python:

python val = gdb.parse_and_eval('((struct my_struct *)dev_base->next->priv)')

python print val

The output is:

$1 = {
 x= 15 '\017'
 md_template = ,
 md_capture_buff =
}  

So how to write equivalent to p/x in gdb-python? or how to get the address of 'md_capture_buff' in python script as python val = gdb.parse_and_eval('((struct my_struct *)dev_base->next->priv)').address is not prining the address?

Baijnath Jaiswal
  • 377
  • 5
  • 17

1 Answers1

0

You probably have "set print address" off. I wouldn't do this. It's arguably a bug that the str operator is respecting this setting.

There isn't a good way to reproduce "p/x" other than just invoking it directly. See gdb.execute.

You can get the value of any field using []. Like

val = something['fieldname']

It's usually better, IMO, to use the API rather than parse_and_eval. That is, you can cast, look up fields, etc, directly from Python.

Tom Tromey
  • 21,507
  • 2
  • 45
  • 63
  • @tromey, Thanks. But while traversing a structure if an array occurs as a member, then how to print the values of whole array in hex (more readable form). it is possible only with `p/x`. but we can not use `gdb.execute('p/x ')` in the middle of traversal. I want to implement this in this script http://stackoverflow.com/questions/16787289/gdb-python-parsing-structures-each-field-and-print-them-with-proper-value-if . Please let me know if there is a way to do this. can we control printing arrays as the pointers are done in this script ? – Baijnath Jaiswal Jun 06 '13 at 05:29
  • I'm trying to avoid parse_and_eval() but I'm not sure how to read a structure pointer directly without it. What API can I use to avoid using parse_and_eval(), assuming the OP's question? – hesham_EE Oct 04 '16 at 18:08
  • There are a number of methods on `gdb.Value` that you'd use. You can use `dereference` to dereference a pointer, use `["fieldname"]` to get a field from a structure (or a pointer-to-structure -- so often you don't even need `dereference`), and use `cast` to change the type. – Tom Tromey Oct 04 '16 at 20:34