[gdb] c object file debugger
gdb C Object File Debugger.
C 코드 분석중 Debugger를 돌려서 확인 해 볼 필요가 생겨서 예전에 사용해보고 한동안 잊고 지냈던 gdb에 대해 다시한번
재조명 하고자 글을 남겨본다.
select.c라는 파일을 컴파일한다.
Step 1. GCC Compile
-g옵션을 사용해 줘야 gdb debugging 이 가능하다.
$ gcc -g -o [object 명] [소스파일 명]
Step 2. gdb 실행
$ gdb [object 명]
Step 3. Break Point.
gdb debugger에서 프로그램 실행 및 break point를 설정하여 좀더 수월한 debugging이 가능하도록 해주는 명령에 대한
소개.
- List
기본적으로 10라인만 출력되며 set listsize 50 등으로 출력사이즈 조절이 가능하다.
(gdb) l
1 #include <sys/time.h>
2 #include <sys/types.h>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <stdio.h>
8
9 int main()
10 {
- Break Point 설정
편의상 b 나 i 같은 시작 initial로만 명령 수행이 가능하다. (b = breakpoint, i = infomation )
(gdb) b 10
Breakpoint 1 at 0x40071a: file select.c, line 10.
(gdb) i b
Num Type Disp Enb Address What
1 breakpoint keep y 0x000000000040071a in main at select.c:10
- Process Run
위에서 설정한 break point 10line에 정확시 걸려서 멈춘 상태이다.
(gdb) r
Starting program: /home/rocksea/study/c/select
Breakpoint 1, main () at select.c:10
10 {
- Next
n 을 이용하여 다음라인의 Process의 실행을 진행 할 수 있다.
(gdb) n
22 if ((fd[0] = open("/tmp/testfile", O_RDONLY)) == -1)
print를 통해 변수에 할당된 값, 주소값 등을 볼 수 있다.
(gdb) p state
$1 = -5856
(gdb) p &state
$3 = (int *) 0x7fffffffe768
일단 이정도만 알아도 간단한 Debugging정도는 할 수 있을 것이다.
사용하면서 필요한 부분은 더 보충 할 것이다.
.by rocksea