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

CGdb

C Problem Overview


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?

C Solutions


Solution 1 - C

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 - C

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 - C

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 - C

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

Solution 5 - C

  p *((struct my_struct*) variable)

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

Solution 6 - C

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

Solution 7 - C

I would have a look at the Data Display Debugger.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionprismofeverythingView Question on Stackoverflow
Solution 1 - CNateView Answer on Stackoverflow
Solution 2 - CLnxPrgr3View Answer on Stackoverflow
Solution 3 - CCiro Santilli Путлер Капут 六四事View Answer on Stackoverflow
Solution 4 - Ccs01View Answer on Stackoverflow
Solution 5 - CPragadeesh waranView Answer on Stackoverflow
Solution 6 - CDave L DelaneyView Answer on Stackoverflow
Solution 7 - CgnudView Answer on Stackoverflow