
GDB를 이용해 간간히 디버깅 하긴 했지만, 자주 사용하지 않아서 익숙하지 않았다. 앞으로 사용할 때마다 검색시간을 줄이기 위해 내가 사용하는/했던 gdb 명령어를 정리해놓기로 했다.
명령어를 정리하다가 마스코트를 찾아보았는데, gdb 마스코트는 위 그림의 금붕어이고, 이유를 찾아보니 벌레(버그)를 잡아먹고 물을 쏘아 벌레를 죽인다나 뭐라나.. 아무튼 그렇다고 한다.
급한 분들을 위한 cheat sheet


사전 조건
컴파일 시 -g 옵션을 주어야 함.
GDB 실행
디버깅하기 위해 가장 먼저 해야 할 것은 해당 프로세스를 대상으로 gdb를 실행해야 한다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gdb <프로그램명> | |
gdb <프로그램명> <core파일명> | |
gdb <프로그램명> <실행중인 프로세스 pid> |
여기서부터 설명되는 명령어는 대부분 단축어가 있다.
단축이 되는 글자를 파란색으로 표시하겠다.
중단점 설정/확인/해제
break : 중단점을 설정한다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
break <함수이름> | |
break <라인번호> | |
break <파일이름:라인번호> | |
break <파일이름:함수이름> | |
break +<offset> // 현재 위치에서 오프셋 라인 뒤에 설정 | |
break -<offset> // 현재 위치에서 오프셋 라인 뒤에 설정 | |
break *address // 이미지의 주소 영역을 breakpoint로 설정 | |
break <...> if <condition> // condition이 만족할때만 중단 |
info break : 중단점을 확인한다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
info break | |
info b | |
i b | |
>>> | |
Num Type Disp Enb Address What | |
2 breakpoint keep y <PENDING> a | |
3 breakpoint keep y <PENDING> b | |
4 breakpoint keep y <PENDING> c |
clear : 중단점을 지정해 지운다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
clear <설정된 함수이름> | |
clear <설정된 라인번호> | |
clear <설정된 파일이름:라인번호> | |
clear <설정된 파일이름:함수이름> |
delete : 중단점을 지운다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
delete // 설정된 모든 브레이크포인트를 지운다. | |
delete <breakpoint 번호> //번호에 해당하는 중단점을 지운다. | |
delete <breakpoint 번호> <breakpoint 번호> // 번호에 해당하는 중단점 모두 지운다. |
disable / enable : 중단점을 활성화/비활성화한다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
en // 모든 중단점 활성화 | |
enable // 모든 중단점 활성화 | |
enable <중단점 번호> // 해당 중단점 활성화 | |
enable <중단점 번호> <중단점 번호> // 해당 중단점 모두 활성화 | |
dis // 모든 중단점 활성화 | |
disable // 모든 중단점 활성화 | |
disable <중단점 번호> // 해당 중단점 비활성화 | |
disable <중단점 번호> <중단점 번호> // 해당 중단점 모두 비활성화 |
<ctrl + c> : 프로세스 가 실행 중 중단점에 도달하지 않을 때 사용하여 일시 중단 가능하다.
프로세스 실행
run : 프로세스를 새로 실행한다. 이미 실행 중이라면 재시작한다.
continue : 마지막 중단 지점에서 다음 중단점 까지 프로세스를 재개한다.
next : 실행 중인 프로세스를 한 줄 실행한다. 함수 실행 시 내부로 진입 X
step : 실행 중인 프로세스를 한 줄 실행한다. 함수 실행 시 내부로 진입
finish : 현재 함수를 수행하고 빠져나간 후 리턴 값을 출력한다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
run // 프로세스를 새로 실행한다. 이미 실행중이라면 재시작한다. | |
continue // 다음 중단점까지 프로그램을 재개한다. | |
conitnue n // 프로그램을 재개한다. 중단점을 n번 건너뛴다. | |
next // 실행중인 프로세스를 한줄 실행한다. 함수 실행시 내부로 진입 X | |
next n // 실행중인 프로세스를 n줄 실행한다. 함수 실행시 내부로 진입 X | |
step // 실행중인 프로세스를 한줄 실행한다. 함수 실행시 내부로 진입 | |
step n // 실행중인 프로세스를 n줄 실행한다. 함수 실행시 내부로 진입 | |
finish // 현재 함수를 수행하고 빠져나간후 리턴값을 출력한다. | |
Call Stack(콜 스택) 확인
backtrace : 현재 위치의 함수 call stack을 출력한다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
backtrace // 현재 실행 위치의 함수 call stack을 출력한다. | |
bt // 현재 실행 위치의 함수 call stack을 출력한다. | |
bt N // 현재 실행 위치의 함수 call stack 중 처음 N개 출력 | |
bt -N // 현재 실행 위치의 함수 call stack 중 마지막 N개 출력 | |
bt full // local 변수들도 함께 출력 |
값 출력 / 변경

print : 변수/주소 등을 출력한다
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print <val> // 변수 출력 | |
p <val> // 변수 출력 | |
p <func::val> // 해당 함수의 변수를 출력한다. | |
p *<ptr> // 포인터의 값 | |
p <addr> // 주소에 있는 값 | |
p arr[n] // arr 배열의 n번째 값 출력 | |
p -pretty *<구조체> // 예쁘게 출력한다. 구조체를 출력할 때 용이하다. | |
p/x <val> // x(16진수) 형식으로 변수 출력 |
display : 매 실행(step, next, continue 등) 마다 출력한다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
display expr // 매 실행시 마다 출력한다. | |
display/fmt expr // 매 실행시 마다 fmt 포멧 형식으로 출력한다. | |
info dis // 설정된 display를 출력한다. | |
info display // 설정된 display를 출력한다. | |
enbale display // 모든 display를 활성화 시킨다. | |
en dis // 모든 display를 활성화 시킨다. | |
en dis <번호> <번호> // 번호에 해당하는 display를 활성화 시킨다. | |
disbale display // 모든 display를 비활성화 시킨다. | |
dis dis // 모든 display를 비활성화 시킨다. | |
dis dis <번호> <번호> // 번호에 해당하는 display를 비활성화 시킨다. |
set : 변수/주소 등에 값을 할당한다.
기타
return : 현재 함수를 수행하지 않고 빠져나간다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return // 현재 함수를 수행하지 않고 빠져나간다. | |
return -1 // 현재 함수를 수행하지 않고 빠져나간다. 리턴값은 -1 |
list : 소스 파일을 출력한다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
list // 현재 위치의 소스 출력 | |
list 100 // 100번째 라인 주변 소스 출력 | |
list funtion // 지정 함수의 소스 출력 | |
list - // 직전에 출력한 소스 출력 | |
set listsize count // 출력하는 라인수를 count로 설정한다. | |
set listsize unlimited // 출력하는 라인수 무제한 |
Reference
이상 많이 썼던 것만 정리해서 모아 보았다. 종종 업데이트 해야겠다.
'Programming > Debug' 카테고리의 다른 글
Cachegrind - cache, branch prediction 측정/분석/프로파일링 (0) | 2021.04.15 |
---|
댓글