LoginSignup
24
13

More than 5 years have passed since last update.

Advanced Debugging with Xcode and LLDB

Posted at
1 / 22

GCC? LLVM? LLDB?


LLVM(Low Level Virtual Machine)

  • Xcode4からGCCを置き換えてデフォルトとして採用された
    • コンパイルが速い
    • コンパイルされたコードが速い
    • エラーメッセージがわかりやすい
    • 他のツールと連携しやすい

LLDB

  • 次世代の高性能デバッガ
  • Clang expressionパーサとかLLVM Disassemblerのような、より大きなLLVMプロジェクトにおいて再利用しやすいコンポーネントとしてビルドされる。
  • もっと詳しい情報, チュートリアルはこちらへ

LLDBコマンド

<noun> <verb> [-options [option-value]] [argument [argument...]]

  • 引数 (argument) が'-'から始まる場合、LLDBにオプションの終了位置を'--'で教えてやる必要がある

(lldb) process launch --stop-at-entry -- -program_arg value


Swift Debugging Reliability

  • Failed to get module from AST(abstract syntax tree) context
    スクリーンショット 2018-07-10 14.15.14.png

  • Swift Type Resolution
    スクリーンショット 2018-07-10 14.15.30.png


Advanced Debugging Tips and Tricks


普段どのようにやっているか?

  • 値の状態によって試してみたい
  • 新しいコードを追加してみたい
  • コード処理をスキップしてみたい
  • Obj-Cコードの値を変更してみたい

expression

  • デバッグ中に値の状態を切り替えることができる
    • LLDB expressions can modify program state
    • (lldb) expression didSelectedHeight = false WWDC 2018-07-10 14-20-13.png

inject code

  • デバッグ中に新しいコードを追加してみたい
    • Use auto-continuing breakpoints with debugger commands to inject code live
    • expression dynamicAnimator.delegate = self WWDC 2018-07-10 16-21-27.png

symbolic breakpoint & po

  • 特定のコードではなく指定されたメソットにブレークポイントを入れて使う
    • Create dependent breakpoints using breakpoint set --one-shot true

WWDC 2018-07-10 14-37-22.png
breakpoint set — one-shot true — name “-[UILabel setText:]”

  • po $arg1 ($arg2, etc) in assembly frames to print function arguments

breakpoint types

  • (place)breakpoint
  • exception breakpoint
  • conditional breakpoint
  • symbolic breakpoint

thread jump

  • コード処理をスキップできる (1)
    • Skip lines of code by dragging Instruction Pointer
    • WWDC 2018-07-10 14-41-24.png

thread jump

  • コード処理をスキップできる (2)
    • Skip lines of code by thread jump --by 1 WWDC 2018-07-10 14-42-41.png

custom debug string

  • extension : CustomDebugStringConvertible WWDC 2018-07-10 14-44-33.png WWDC 2018-07-10 14-44-53-2.png

Watchpoint

  • Pause when variables are modified by using watchpoints
    • 特定の値をwatchに設定する WWDC 2018-07-10 14-47-48.png
    • その値を参照し、アクションを実行する WWDC 2018-07-10 14-48-59.png

UIKitのようなObj-Cコードをいじる (1)

  • Evaluate Obj-C code in Swift frames with expression -l objc -O -- <expr> WWDC 2018-07-10 14-53-44.png expression -l objc -O -- [`self.view` recursiveDescription]

UIKitのようなObj-Cコードをいじる (2)

  • po unsafeBitCast(<memory addre ss>, to: <Object>) WWDC 2018-07-10 14-55-21.png

UIKitのようなObj-Cコードをいじる (3)

  • Flush view changes to the screen using expression CATransaction.flush() WWDC 2018-07-10 14-55-53.png

LLDBコマンドをカスタムしてもっと強力に使う

  • ~/.lldbinit
    • デバッグの起動時に読み込んでくれる。このファイル中は(Pythonではなく)LLDBのコマンドだけが実行できる。
    • その中でPythonスクリプトを読み込むことができる。
  • command alias

    • command alias poc expression -l objc -O --
  • command script import

    • command script import ~/nudge.py

LLDB Command Map - download

lldb-commands-map.png


Reference

"Debugging Swift code with LLDB"
https://medium.com/flawless-app-stories/debugging-swift-code-with-lldb-b30c5cf2fd49

"More than po: Debugging in LLDB"
https://www.slideshare.net/micheletitolo/more-than-po-debugging-in-lldb-78570000

"Video : Advanced Debugging with Xcode and LLDB / WWDC18 "
https://developer.apple.com/videos/play/wwdc2018/412/

24
13
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
13