#1-2. LLDB(Low-Level Debugger) 디버거 [Swift]

인생노잼시기·2021년 4월 9일
0

🦅 스위프트 문법

목록 보기
4/13

LLDB(Low-Level Debugger)

LLVM은 컴파일 프로젝트이고 그 서브 프로젝트인 LLDB는 디버깅을 맡고 있다.
LLDB는 LLVM의 Debugger Component를 개발하는 서브 프로젝트이다.
C, C++, Objective-C, Swift를 지원하며, 현재 Xcode의 기본 디버거로 내장되어 있다.

기본 문법

(lldb) command [subcommand] -option "this is argument"

구성요소: Command, Subcommand, Option, Argument

예시:
(lldb) breakpoint set --file test.c --line 12

breakpoint(Command)와 set(Subcommand)을 이용하며
--file option을 통해 test.c 파일 내
--line option을 통해 12번째 라인에
중단점을 set 해줍니다.

Help

// LLDB에서 제공하는 Command가 궁금하다면,
(lldb) help

// 특정 Command의 Subcommand나, Option이 궁금하다면,
(lldb) help breakpoint
(lldb) help breakpoint set

Apropos

//referent count를 체크할 수 있는 명령어가 있을까? 궁금하다면,
(lldb) apropos "refernce count"
// 결과
# The following commands may relate to 'reference count':
#    refcount -- Inspect the reference count data for a Swift object

breakpoint 걸기

그냥 왼쪽 행 번호 클릭해서 사용 중...
명령어를 줄여서 사용할 수도 있구나

함수명, 파일 라인(행)에 걸 수 있다

Condtion 조건에 따라

//viewWillAppear 호출시, animated가 true인 경우에만 break
(lldb) breakpoint set --name "viewWillAppear" --condition animated
(lldb) br s -n "viewWillAppear" -c animated

Command 실행 & AutoContinue

-command (-C) option을 이용하면 break시 원하는 lldb command를 실행 할 수 있습니다.
–auto-continue (-G) option의 기능은 auto continue로, command 실행 후 break에 걸린채로 있지 않고 프로그램을 자동 진행하게 해줍니다`

(lldb) breakpoint set -n "viewDidLoad" --command "po $arg1" -G1
(lldb) br s -n "viewDidLoad" -C "po $arg1" -G1

b (_regexp-break) Command

# 특정 이름을 가진 function에서 break
(lldb) b viewDidLoad
# 현재 파일 20번째 line에서 break
(lldb) b 20
# 특정 파일 20번째 line에서 break
(lldb) b ViewController.swift:12
# 현재 파일 내 특정 text를 포함한 line에서 break
(lldb) b /stop here/
# 특정 주소값에서 break
(lldb) b 0x1234000

Stepping

run
next(n)
step-in(s)
finish
버튼을 눌러서 사용하자

Expression

멈춰있는 동안 새로운 동작를 실행시킬 수 있는 (lldb) expression Command

po

(lldb) help po를 치면
po는 (lldb) expression -O -- 의 줄임말이라고 나온다.
여기서 -O option은 object의 description을 출력하겠다는 뜻입니다.

po가 출력하는 description은 NSObject의 debugDescription입니다.??? 무슨 말이지???ㅋㅋㅋ

(lldb) expression -l objc -O -- "[오브젝티브C 표현]"

variable

(lldb) expression self.view

self.view를 $R0이라는 변수에 저장한다
(lldb) expression $R0.backgroundColor = UIColor.blue

코드를 실행시킨다
(lldb) continue
(lldb) c (줄여서 가능)
(lldb) expr let $someNumber = 10
(lldb) expr var $someString = "some string"

#  실행 도중 breakpoint를 만나도 그냥 진행
(lldb) expression --ignore-breakpoints true --
(lldb) ex -i 1 --
#  실행 도중 breakpoint를 만나면 멈춤
(lldb) expression --ignore-breakpoints false --
(lldb) ex -i 0 --  

unsafeBitCast
객체의 주소값과 타입을 알면 객체의 정보를 알아낼 수 있다

expr -l Swift -- import UIKit
expr -l Swift -- let $pin = unsafeBitCast(0x7df67c50, to: MKPinAnnotationView.self)
expr -l Swift -- print($pin.alpha)

객체의 주소값은 어디에 있는데? 못찾겠는데?

Image

https://yagom.net/courses/start-lldb/lessons/image/

출처: https://yagom.net/courses/start-lldb/lessons/lldblow-level-debugger란/

profile
인생노잼

0개의 댓글