C언어
GDB 를 이용한 구조체 파싱 방법
헬로르르
2019. 9. 14. 16:57
임베디드 세상에서 놀다 보니 hex memory (byte) 데이터를 구조체로 변경해서 봐야 할 일이 아주 많습니다.
디버거를 물려서 보면 가장 베스트인데, 임베디드 개발중에 디버거는 아주 사치인 경우가 많죠 ....
맨날 눈으로 파싱해서 볼려니 머리가 아파가지고, GDB 연동해서 바로 구조체 파싱 하는 법을 보도록 하겠습니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
#include <stdio.h>
#include <string.h>
typedef struct testa
{
int a;
int b;
}testa;
typedef struct testb
{
short int c;
short int d;
}testb;
typedef struct testc
{
testa a;
testb b;
}testc;
void testaaaa(int a);
testc parse(void * p)
{
testc d;
memcpy(&d, p, sizeof(d));
return d;
}
testc c;
unsigned char bf[100];
int main(void)
{
printf("hello\n");
}
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs |
이렇게 nested struct 으로 된 구조체들을 임베디드 하시는분들은 아주 많이 볼 거에요.
testc 같은 경우는 12byte인데, testa / testb 로 되어있어서 아주쉽게 파싱이 잘 안됩니다.
이걸 GCC로 다시 빌드를 한 후에, GDB 를 통해 구조체를 파싱하려고 합니다.
1. gcc test.c -g
2. gdb a.exe
gdb 안에서는,
b main
r
set print pretty on
set bf={1,2,3}
p/x *(struct testc *)&bf
를 하시면 아주 이쁘게 파싱이 되서 보이는걸 보실 수 있습니다!
pointer같은 경우는 어차피 memory 덤프떠서 볼 이유가 없기때문에 해당 부분에선 고려하지 않겠습니다.