4

Given an address of a struct type variable let's say 0x61b2e0.
I know that this address points to a struct of type s_a, which is defined as

typedef struct {
    int n;
    char *v;
} s_a;

How can I print this struct in gdb using the address above?

1
  • 5
    p *((s_a*)(0x61b2e0)) Commented Feb 27, 2019 at 16:13

2 Answers 2

8

(gdb) p *((s_a *)(<address>)) should help you to print a content of the structure referenced by <address> virtual address i.e. in you case it would be: (gdb) p *((s_a *)(0x61b2e0))

Sign up to request clarification or add additional context in comments.

Comments

0

To examine memory without dependence on the program's data type gdb provides the x command with follow syntax,

x/nfu addr

Here the /nfu is optional, where

n - how much memory to display in count according to the option u. Negative sign at front will cause the read with decremented address

f - display format

u - unit size. For E.g., 'h' correspond to halfwords

For the given structure, read for two integer sizes according to your machine will give the structure content. The first integer for the value of n and second for the pointer address. So here for two word read,

x/2uw 0x61b2e0

2 Comments

While this works, using a cast in the print as @meowgoesthedog mentioned is a better answer.
I agree as well.

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.