Post

g++ 컴파일 및 GDB 사용법

g++ 컴파일 및 GDB 사용법

설치

1
2
brew install gdb # mac
sudo apt-get install gdb # linux
1
2
gcc –g test.c –o test
gdb test

사전 준비

  • 컴파일시 -g 옵션 추가
    1
    2
    
    g++ -g test.c -o test	# 컴파일
    gdb test	# 실행
    

GDB 실행

1
gdb <프로그램명>

중단점 설정/확인/해제

break : 중단점 설정

1
2
3
4
5
break <함수이름>
break <라인번호>
break <파일이름:라인번호>
break <파일이름:함수이름>
break <...> if <condition>	// 조건에 맞는 경우 중단점 설정

info break : 중단점 확인

1
2
3
info break
info b
i b

clear : 특정 중단점 삭제

1
2
3
4
clear <함수이름>
clear <라인번호>
clear <파일이름:라인번호>
clear <파일이름:함수이름>

delete : 모든 중단점 삭제

1
2
3
delete	// 설정된 모든 중단점 삭제
delete <breakpoint 번호>	// 번호에 해당하는 중단점 삭제
delete <breakpoint 번호> <breakpoint 번호>	// 번호에 해당하는 모든\ 중단점 삭제

disable / enable : 중단점 활성화/비활성화

1
2
3
4
5
6
7
8
# 중단점 활성화
enable 
enable <breakpoint 번호>
enable <breakpoint 번호> <breakpoint 번호>
# 중단점 비활성화
disable
disable <breakpoint 번호>
disable <breakpoint 번호> <breakpoint 번호>

프로세스 실행

run : 프로세스 실행 또는 재실행

1
run

continue : 다음 중단점까지 프로세스 재채

1
2
continue
continue n

next : 실행 중인 프로세스를 한 줄 실행(함수 실행 시 내부로 진입x)

1
2
next
next n

step : 실행 중인 프로세스를 한 줄 실행(함수 실행 시 내부로 진입o)

1
2
step
step n

finish : 현재 함수를 수행하고 빠져나간 후 리턴 값 출력

1
finish

Call Stack 확인

backtrace

1
2
3
4
5
backtrace
bt
bt N
bt -N
bt full

값 출력/변경

1
2
3
4
5
6
7
8
print <val>
p <val>
p <func:val>
p *<ptr>
p <addr>
p arr[n]
p -pretty *<구조체>
p/x <val>

display : 매 실행(step, next, continue 등) 마다 출력

1
2
3
4
5
6
7
8
9
10
11
12
13
display expr
dsiplay/fmt expr 

info dis
info display

enable display
en dis
en dis <번호>< 번호>

disable display
dis dis
dis dis <번호><번호>

기타

return : 현재 함수를 수행하지 않고 빠져나감

1
2
return
return -1

list : 소스 파일을 출력

1
2
3
4
5
6
7
8
list
list 100
list function
list -
list start,end

set listsize count
set listsize unlimited

Reference

https://jangpd007.tistory.com/54 https://dining-developer.tistory.com/13

This post is licensed under CC BY 4.0 by the author.