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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@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 <array>') in the middle of traversal. I want to implement this in this script stackoverflow.com/questions/16787289/… . 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 ?
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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.