36

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?

1

1 Answer 1

82
print *variable

If you do that it will display the value of that variable in GDB.
You also have an option to display the struct with indentation and new lines:

$1 = {
  next = 0x0,
  flags = {
    sweet = 1,
    sour = 1
  },
  meat = 0x54 "Pork"
}

For that you need to set the pretty print:

set print pretty on

If you want to print an array of values you do:

print *array@len
Sign up to request clarification or add additional context in comments.

4 Comments

Yeah it prints the raw data but that still does not recognize the values as a struct. If i got it right, the question was about recognizing as a struct (i.e. recognize fields in the struct) which i don't think is possible
Your initial post (before edit) was only about print *array@len which can only print members of an array of same type. chemie.fu-berlin.de/chemnet/use/info/gdb/gdb_9.html. However, the latter edit to the answer might be a solution. Thanks for adding that to my knowledge
Is it possible to print the struct with the typenames of each member as well?
So many years without knowing that cool feature

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.