Presentation is loading. Please wait.

Presentation is loading. Please wait.

GDB - GNU Debugger 김진용.

Similar presentations


Presentation on theme: "GDB - GNU Debugger 김진용."— Presentation transcript:

1 GDB - GNU Debugger 김진용

2 GDB 개요 GDB GNU debugger GNU에서 만든 디버깅 유틸 실행 # gdb [ENTER]

3 GDB 명령어 실행 및 트레이스 run : 현재의 인수를 사용하여 프로그램을 실행
run <args> : 새로운 <인수>를 가지고 프로그램을 실행 continue : 현재 위치에서 프로그램을 계속 실행 (약자 c)

4 GDB 명령어 next : 한 줄씩 실행 시킨다. 이 때 함수를 포함하고 있으면 함수를 수행시킨다. (약자) n
next <n> : <n>줄을 실행시킨다. step : 한 줄씩 실행 시킨다. 이 때 함수를 포함하고 있으면 함수 내부로 들어가서 한 줄씩 실행한다. (약자) s step <n> : <n>줄을 실행시킨다. (주의) step 사용시 원치않은 함수로 들어가지 않도록 주의해야함

5 GDB 명령어 break <line number> : 라인 번호에서 프로그램 실행을 멈추게 한다.
quit : gdb를 종료 시킨다.

6 GDB 명령어 데이타에 관련된 명령들 whatis <expr> : 지정한 <변수>에 관련된 정보를 보여준다. print <expr> : <expr>에 지정된 식의 값을 보여준다. (약자) p display : 현재 지정된 display 명령의 목록을 보여준다. display <expr> : 새로운 <expr>을 display목록에 추가

7 GDB 명령어 list : 현재 위치에서 소스 파일의 내용을 10줄 보여준다.
list <first>,<last> : <시작줄>과 <끝줄>사이의 소스파일 내용을 보여준다.

8 GDB 명령어 기타 명령어 help [name] : 명령어의 사용방법 안내 quit : gdb의 종료

9 GDB의 사용예 예제프로그램 버그(bug)가 눈에 보이나요? 13 strcpy(bug,"hi");
printf("bug is %s \n", bug); 15 return; } <끝> 파일 이름 : test.c 버그(bug)가 눈에 보이나요? 예제프로그램 1 #include <stdio.h> 2 3 main() 4 { int i; double j; char *bug = NULL; 8 for( i = 0; i < 5; i++) { j = i/2 + i; printf(" j is %lf \n", j ); }

10 GDB의 사용예 컴파일과 실행 [sugar@hussein gdb]$ cc -g test.c
gdb]$ ./a.out j is j is j is j is j is Segmentation fault (core dumped) gdb]$ gdb]$ ls a.out core test.c

11 GDB의 사용예 GDB의 시작 $ gdb <실행화일> [sugar@hussein gdb]$ gdb a.out
GNU gdb with Linux support Copyright 1998 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-redhat-linux"... (gdb)

12 GDB의 사용예 list명령 (gdb) list (gdb) list 4, 13 1 #include <stdio.h>
2 main() { int i; double j; char *bug = NULL; 8 for( i = 0; i < 5; i++) { j = i/2 + i; (gdb) (gdb) list 4, 13 { int i; double j; char *bug = NULL; 8 for( i = 0; i < 5; i++) { j = i/2 + i; printf(" j is %lf \n", j ); } strcpy(bug,"hi"); (gdb)

13 GDB의 사용예 break , run 명령 break point의 지정, run으로 break point까지 실행
Breakpoint 1 at 0x804840d: file test.c, line 9. (gdb) run Starting program: /home/users/phd/sugar/bit/gdb/a.out Breakpoint 1, main () at test.c:9 for( i = 0; i < 5; i++) { (gdb)

14 GDB의 사용예 next, print 명령 (gdb) n (gdb) p j 10 j = i/2 + i; $4 = 0
(gdb) p i $1 = 0 (gdb) p j $2 = e-270 printf(" j is %lf \n", j ); $3 = 0 (gdb) p j $4 = 0 (gdb) n j is for( i = 0; i < 5; i++) { (gdb)

15 GDB의 사용예 display 명령 (gdb) n j is 1.000000 (gdb) display i
(gdb) display j 2: j = 0 (gdb) n j = i/2 + i; 1: i = 1 printf(" j is %lf \n", j ); 2: j = 1 (gdb) n j is for( i = 0; i < 5; i++) { 2: j = 1 1: i = 1 j = i/2 + i; 1: i = 2 printf(" j is %lf \n", j ); 2: j = 3 (gdb)

16 GDB의 사용예 J값이 이상하다! …next명령어 계속... (gdb) n (gdb) n 13 strcpy(bug,"hi");
j = i/2 + i; 2: j = 4 1: i = 4 printf(" j is %lf \n", j ); 2: j = 6 j is for( i = 0; i < 5; i++) { (gdb) n strcpy(bug,"hi"); 2: j = 6 1: i = 5 Program received signal SIGSEGV, Segmentation fault. strcpy (dest=0x0, src=0x80484ec "hi") at ../sysdeps/generic/strcpy.c:38 ../sysdeps/generic/strcpy.c:38: No such file or directory. (gdb) q J값이 이상하다!

17 GDB의 사용예 bug의 값(주소)가 잘못되어 있다! [sugar@hussein gdb]$ gdb a.out
GNU gdb with Linux support …생략... (gdb) b 13 Breakpoint 1 at 0x : file test.c, line 13. (gdb) run Starting program: /home/users/phd/sugar/bit/gdb/a.out j is j is j is j is j is Breakpoint 1, main () at test.c:13 strcpy(bug,"hi"); (gdb) p bug $1 = 0x0 (gdb) bug의 값(주소)가 잘못되어 있다!

18 GDB의 사용예 예제프로그램 (vi debug.c에서 set number 명령 실행) _____10_main()
______1_#include <stdio.h> ______2_ ______3_void ______4_print_sum(sum) ______5_int sum; ______6_{ ______7_ printf("Total sum : %d\n", sum); ______8_} ______9_ _____10_main() _____11_{ _____12_ int i, sum; _____13_ _____14_ sum = 0; _____15_ for(i=0;i<5;i++) { _____16_ printf("%dth interation\n", i ); _____17_ sum += i; _____18_ } _____19_ print_sum(sum); _____20_}

19 GDB의 사용예 [sugar@hussein bit]$ gcc -g debug.c
bit]$ gdb a.out GNU gdb with Linux support Copyright 1998 Free Software Foundation, Inc. ....생략.... (gdb) break 14 Breakpoint 1 at 0x80483ee: file debug.c, line 14. (gdb) run Starting program: /home/users/phd/sugar/bit/a.out Breakpoint 1, main () at debug.c:14 14sum = 0; (gdb) next 15for(i=0;i<5;i++) { 16printf("%dth interation\n", i ); (gdb) n 0th interation 17sum += i; (gdb)

20 GDB의 사용예 (gdb) n (gdb) n 15for(i=0;i<5;i++) {
16printf("%dth interation\n", i ); 1th interation 17sum += i; 2th interation (gdb) n 15for(i=0;i<5;i++) { 16printf("%dth interation\n", i ); 3th interation 17sum += i; (gdb)

21 GDB의 사용예 (gdb) n (gdb) next 15for(i=0;i<5;i++) {
(gdb) print sum $1 = 6 (gdb) print i $2 = 3 (gdb) display sum 1: sum = 6 (gdb) display i 2: i = 3 (gdb) next 16printf("%dth interation\n", i ); 2: i = 4 1: sum = 6 4th interation 17sum += i; (gdb)

22 GDB의 사용예 버그가 없이 동작함을 확인! (gdb) next (gdb) next 15for(i=0;i<5;i++) {
1: sum = 10 19print_sum(sum); 2: i = 5 (gdb) next Total sum : 10 20} 2: i = 5 1: sum = 10 (gdb) c Program exited with code 017. (gdb) quit bit]$ 버그가 없이 동작함을 확인!


Download ppt "GDB - GNU Debugger 김진용."

Similar presentations


Ads by Google