오랫만에 쓰려니 다 까먹어서 다시 조사

 

빌드

-g 옵션을 통해 디버깅 심볼을 넣어준다.

$ gcc -g tt.c

 

이거 안하면 소스코드가 안나옴

Reading symbols from ./a.out...
(No debugging symbols found in ./a.out)
(gdb) l
No symbol table is loaded.  Use the "file" command.

 

gdb 실행 (인자 없이)

$ gdb ./a.out

$ gdb
(gdb) file ./a.out

 

gdb 실행 (인자 필요시)

$ gdb --args ./a.out arg1 arg2

[링크 : https://stackoverflow.com/questions/6121094/how-do-i-run-a-program-with-commandline-arguments-using-gdb-within-a-bash-script]

 

브레이크 포인트 설정 및 소스 보기

소스는 list (혹은 소문자 l) 과 라인수를 넣고

브레이크 포인트는 b로 추가한다. b 뒤에는 라인수 혹은 함수 명을 넣으면 된다.

브레이크 포인트 목록 확인은 info b를 통해 가능하다.

clear 를 통해 b 명령을 통해 추가하는 것과 정반대로 삭제가 가능하고

delete(소문자 d) 명령은 info b의 num 을 이용하여 삭제한다.

(gdb) l
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 void main()
5 {
6 int *arr = NULL;
7 arr = (int*)malloc(10 * sizeof(int));
8 int idx = 0;
9 for(idx = 0; idx < 10000; idx++)
10 arr[idx] = idx;
(gdb) l 10
5 {
6 int *arr = NULL;
7 arr = (int*)malloc(10 * sizeof(int));
8 int idx = 0;
9 for(idx = 0; idx < 10000; idx++)
10 arr[idx] = idx;
11
12 // iprintf("before\n");
13 // fflush(stdout);
14
(gdb) l 14
9 for(idx = 0; idx < 10000; idx++)
10 arr[idx] = idx;
11
12 // iprintf("before\n");
13 // fflush(stdout);
14
15 free(arr);
16
17 printf("after\n");
18 fflush(stdout);
(gdb) b 15
Breakpoint 1 at 0x1201: file t2.c, line 15.
(gdb) b
No default breakpoint address now.
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000001201 in main at t2.c:15
(gdb) b main
Breakpoint 2 at 0x11b5: file t2.c, line 6.
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000001201 in main at t2.c:15
2       breakpoint     keep y   0x00000000000011b5 in main at t2.c:6
(gdb) d
Delete all breakpoints? (y or n) n
(gdb) clear main
Deleted breakpoint 2 
(gdb) d 1
(gdb) info b
No breakpoints or watchpoints.
(gdb) b main
Breakpoint 3 at 0x11b5: file t2.c, line 6.
(gdb) b 15
Breakpoint 4 at 0x1201: file t2.c, line 15.
(gdb) b 10 if idx = 200
Breakpoint 5 at 0x11db: file t2.c, line 10.
(gdb) info b
Num     Type           Disp Enb Address            What
3       breakpoint     keep y   0x00000000000011b5 in main at t2.c:6
4       breakpoint     keep y   0x0000000000001201 in main at t2.c:15
5       breakpoint     keep y   0x00000000000011db in main at t2.c:10
stop only if idx = 200

[링크 : https://dining-developer.tistory.com/13]

 

디버깅 시작, 디버깅 제어

run을 통해 시작하고 인자가 필요할땐 

(gdb) r 1 2 3 

처럼 인자를 공백으로 띄워서 넣어준다.

c는 break 포인트 까지 실행(continue)

print 를 이용하여 변수 값을 출력할수 있다.

r을 눌러 실행하나 main() 함수에서 한번 브레이크 걸리고

c를 눌러 진행하지만 10 라인에 브레이크가 걸려있어 한번 멈추고

c를 눌러 진행하지만 조건부 브레이크에 의해 idx가 200인 경우에 멈춘다.

(gdb) r
Starting program: /home/minimonk/work/src/malloc/a.out 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Breakpoint 3, main () at t2.c:6
6 int *arr = NULL;
(gdb) c
Continuing.

Breakpoint 5, main () at t2.c:10
10 arr[idx] = idx;
(gdb) c
Continuing.

Breakpoint 5, main () at t2.c:10
10 arr[idx] = idx;
(gdb) print idx
$1 = 200

 

next는 한줄 실행 (함수 진입 x)

step은 한줄 실행 (함수 진입)

finish는 함수 종료까지 실행(함수에서 벗어날때 까지)

 

[링크 : https://lacti.github.io/2008/04/01/gdb-simple-help/]

'프로그램 사용 > gdb & insight' 카테고리의 다른 글

gdb attach  (0) 2025.11.11
gdbserver taget  (0) 2023.07.19
gdb conditional break  (0) 2023.07.19
gdb 디버깅 타겟을 인자와 함께 실행하기  (0) 2022.10.17
gdb break  (0) 2021.04.09
Posted by 구차니