Search
Duplicate

터미널에서 디버깅 with lldb (기본 명령어)

간단소개
팔만코딩경 컨트리뷰터
ContributorNotionAccount
주제 / 분류
debug
Scrap
태그
lldb
9 more properties

LLDB ?

LLDB → Low Level DeBugger
소프트웨어 / Command line 디버거
맥에는 따로 설치해주지 않아도 설치되어 있음

사용 방법

1.
gcc -g 옵션 으로 컴파일하여 실행 파일을 만듦
2.
$ lldb (실행파일)로 lldb를 실행
$ lldb (pid) 로 실행 중인 프로그램 디버깅도 가능
pid는 $ ps 명령어로 확인할 수 있음
lldb를 실행하시면 (lldb) help 로 명령어들을 확인할 수 있음

명령어

BreakPoint

함수를 Breakpoint로 설정
(lldb) b ft_printf # 함수 이름이 ft_printf인 모든 함수에 Breakpoint 설정 (lldb) breakpoint set --name ft_printf
Bash
복사
라인을 Breakpoint로 설정
(lldb) b main.c:42 (lldb) breakpoint set --file main.c --line 42
Bash
복사
현재 Breakpoint 확인
(lldb) b (lldb) breakpoint list
Bash
복사
Breakpoint 제거
특정 Breakpoint 제거
(lldb) br del [breakpoint 번호] (lldb) breakpoint delete [breakpoint 번호]
Bash
복사
모든 Breakpoint 제거
(lldb) br del (lldb) breakpoint delete
Bash
복사

Run

(lldb) r # 코드 실행 (lldb) run (lldb) run <args> # 인자를 넘겨주면서 실행 시킬 수 있음
Bash
복사

Next와 Step

두 명령어 모두 코드를 한 줄씩 실행시키지만 조금의 차이점이 있음
(lldb) n # 코드를 한줄 실행하지만, 함수 안으로 들어가지 않음 (stepping over calls) (lldb) next (lldb) s # 코드를 한줄 실행하고, 함수 안으로 들어감 (stepping into calls) (lldb) step
Bash
복사

Continue

(lldb) c # 다음 breakpoint까지 실행 재개 # continue -- Continue execution of all threads in the current process (lldb) continew
Bash
복사

finish

(lldb) finish # 현재 스택 프레임이 끝날 때가지 실행 (lldb) f
Bash
복사

var

(lldb) var # 현재 스택 프레임의 변수들을 출력 (lldb) var 변수이름 # 현재 스택 프레임의 특정 변수의 값을 출력 # 형 변환, 포인터, 구조체 연산도 가능
Bash
복사

env

(lldb) env # 현재 환경변수 값을 출력
Bash
복사

quit

(lldb) q # lldb 종료 (lldb) quit
Bash
복사

list

(lldb) l main.c # 코드 보기 (lldb) list main.c
Bash
복사

Register

(lldb) register read # 현재 레지스터 읽기 (lldb) register read rdi # 특정 레지스터만 읽기 (lldb) register write # 레지스터 쓰기
Bash
복사

Backtrace

(lldb) bt # 스택 추적 (lldb) backtrace (lldb) up # 상위 스택 피킹, 얕아짐 (lldb) down # 하위 스택 피킹, 깊어짐
Bash
복사

Disassemble

(lldb) d # Disassemble specified instructions in the current target. Defaults to the current function for the current thread and stack frame. (lldb) disassemble
Bash
복사

Stop reason

stop reason에 어떤 이유로 멈췄는지에 대한 정보가 표시 됨
Set breakpoint
EXC_BAD_ACCESS