Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Is there any way to display struct values in gdb?


Asked by Deacon Dunn on Dec 12, 2021 FAQ



In GDB, given a variable that points to a struct, print will display the raw pointer value and x will display the raw bytes pointed to. Is there any way to display the data pointed to as that struct, i.e. a list of fields and their values?
Similarly,
You can use the GDB command ptypeto print out the definition of a struct or class. Additionally, use ptype /oto print offsets and sizes of all fields in a struct (like pahole).
In addition, These settings are useful for debugging programs in any language: GDB prints memory addresses showing the location of stack traces, structure values, pointer values, breakpoints, and so forth, even when it also displays the contents of those addresses. The default is on.
In fact,
GDB can't print struct net_device, struct pci_dev because it doesn't have debug info describing the definition of those structures. Your userspace struct my_struct is declared to have opaque pointers to those structures.
Also Know,
In particular, whenever you ask GDB for the value of a variable in your program, the value is found in the selected frame. There are special GDB commands to select whichever frame you are interested in.