如何在 GDB 中显示结构的字段?

我在 GDB (调试器)会话中遇到一个 struct (称为 ngx_http_variable_value_t) ,我想打印它在控制台中的字段。

这可能吗?

116232 次浏览

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.

I've only done this through graphic front ends for gdb. Found this entry in gdb online docs. Hope it helps. I believe this will require that the code was created with debugging info included.

ptype [arg] ptype accepts the same arguments as whatis, but prints a detailed description of the type, instead of just the name of the type.

Debugging with GDB:Symbols

I would have a look at the Data Display Debugger.

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).

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.

screenshot

Disclaimer: I am the developer of gdbgui

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"
},
}
  p *((struct my_struct*) variable)

This will help you to print the details of struct in gdb

Assume yout variable is in address X. You can do the following:

set $i1 = (ngx_http_variable_value_t *) = X
print *$i1
print *$i1->name_field

Here an example assuming this is my struct and a variable of type strcut internet has been defined within the address 0x804a008:

struct internet {
int priority;
char *name;
};


(gdb) set $i1 = (struct internet *)0x804a008
(gdb) print *$i1
$17 = {priority = 1, name = 0x804a018 "AAAABBBBCCCCDDDDEEEEt\227\004\b"}
(gdb) print $i1->name
$18 = 0x804a018 "AAAABBBBCCCCDDDDEEEEt\227\004\b"
(gdb) print $i1->priority
$19 = 1
(gdb)