How do I show what fields a struct has in GDB?
I came upon a struct (called ngx_http_variable_value_t
) in my GDB (debugger) session and I would like to print what fields it has in the console.
Is that possible?
Solution 1:
You can use the GDB command ptype
to print out the definition of a struct or class.
Additionally, use ptype /o
to print offsets and sizes of all fields in a struct (like pahole).
Solution 2:
If you have debugging symbols built in, you should just be able to print the value: print variable
or print *variable
if it's a pointer to a struct.
Solution 3:
set print pretty on
This option also gives newlines and indentation for p *my_struct_pointer
.
Which do you prefer:
$2 = {path = {mnt = 0xffff8800070ce1a0, dentry = 0xffff880006850600},last = {{{hash = 3537271320, len = 2}, hash_len = 12127205912}, name = 0xffff88000659501c "../b.out"}
or:
$3 = {
path = {
mnt = 0xffff8800070ce1a0,
dentry = 0xffff880006850600
},
last = {
{
{
hash = 3537271320,
len = 2
},
hash_len = 12127205912
},
name = 0xffff88000659501c "../b.out"
},
}
Solution 4:
In addition to using the command line option, you can also use graphical debuggers. I suggest gdbgui, but there are quite a few out there.
Disclaimer: I am the developer of gdbgui
Solution 5:
p *((struct my_struct*) variable)
This will help you to print the details of struct in gdb