Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I show what fields a struct has in GDB?

Tags:

c

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?

like image 417
prismofeverything Avatar asked Nov 20 '09 06:11

prismofeverything


People also ask

Which command is used in gdb to find the type of variable in C?

The ptype [ARG] command will print the type. Show activity on this post. This question may be related: vtable in polymorphic class of C++ using gdb: (gdb) help set print object Set printing of object's derived type based on vtable info.

How do I get more information on gdb?

To get more information we use the "backtrace" command. The "backtrace" command tells gdb to list all the function calls (that leads to the crash) in the stack frame.

What does display do in gdb?

Enables automatic displaying of certain expressions each time GDB stops at a breakpoint or after a step.

What does list do in gdb?

In general, the list command expects you to supply zero, one or two locations. Locations specify source lines; there are several ways of writing them (see Specify Location), but the effect is always to specify some source line.


2 Answers

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

like image 140
Nate Avatar answered Sep 21 '22 09:09

Nate


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.

like image 32
LnxPrgr3 Avatar answered Sep 20 '22 09:09

LnxPrgr3