Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a C structure recursively in gdb

Tags:

gdb

How can I make gdb print a structure's fields recursively,
i.e. follow pointers.

Right now, I have to go inside each field and either specify a '*' to print
the sub structure, or have to type cast to see what's inside the substructure.

e.g.

typedef struct {
int a;
}A;

typedef struct {
A *pA;
int b;
}B;

typedef struct {
B *pB;
int c;
}C;

C var_c;
C *pC = var_c;

... ...

Now, I would like to do "p *pc" on gdb prompt,
to see everything rather than just the address of pB.

like image 469
Aman Jain Avatar asked Nov 04 '22 12:11

Aman Jain


1 Answers

The best way to achieve what you want is to write a python pretty-printer for your structs. Documentation here.

like image 189
Employed Russian Avatar answered Nov 09 '22 10:11

Employed Russian