3

why i am getting wrong answer while doing arithmetic operation:

(gdb) python address = gdb.parse_and_eval('&(((struct my_struct *)next->priv).wait_list)')
(gdb) python print address
0x410027a00728
(gdb) python offset = gdb.parse_and_eval('&((struct wait_list_t *)0)->list')
(gdb) python print offset
0x0
(gdb) python diff = address - offset
gdb) python print diff
0x410027a0072

while output should be 0x410027a00728. i checked the type of address and offset by

(gdb) python print address.type
struct list_head *
(gdb) python print offset.type
struct list_head *

I tried this also

(gdb) python y = hex(long(address))
(gdb) python print y
0x410027A14FF0L
(gdb) python z = hex(long(offset))
(gdb) python print z
0x0L
(gdb) python diff = y - z
Traceback (most recent call last):
File "<string>", line 1, in ?
TypeError: unsupported operand type(s) for -: 'str' and 'str'
Error while executing Python code.

Is there any alternative to do this ?

Baijnath Jaiswal
  • 377
  • 5
  • 17

1 Answers1

1

You are subtracting two values of pointer type. This means that the result, as in C, is divided by the size of the object.

Instead, ensure that "offset" has an integral type, not a pointer type.

In the last example you are trying to subtract strings. You can't do that. Move the calls to "hex" from the computations to the prints and it will work.

Tom Tromey
  • 21,507
  • 2
  • 45
  • 63
  • tromey,in the script `print-struct-follow-pointers.py` written here http://stackoverflow.com/questions/16787289/gdb-python-parsing-structures-each-field-and-print-them-with-proper-value-if ,similar to `is_pointer(v)`condition in script, i am checking one more condition by `if is_typedef(v):` `type_ = v.type.strip_typedefs()` `print 'type = %s'%(type_)` in the case of some structs, it prints correct type, but for some structs it prints `type = struct {...}` . i am not able to find the exact reason.. – Baijnath Jaiswal Jul 03 '13 at 07:08
  • solved.:-) it was because of anonymous structure.. !! is there no support for python keyword `with` and `as` inside gdb? why is it not working ? code is here http://stackoverflow.com/questions/17448736/gdb-python-why-below-code-is-not-working-under-gdb – Baijnath Jaiswal Jul 04 '13 at 10:56