Xcode で快適なデバッグライフを追い求める

Xcode で快適なデバッグライフを追い求める

iOSDC https://iosdc.jp/2016/c/node/116

僕は怠惰な人間です。プログラミングの大半はデバッグに時間を費やすと思っているので、なるべく早く原因に辿りついたり効率のよいデバッグライフを送りたいと常々思っています。
プリントデバッグもいいのですが Xcode には便利な機能が色々とあります。それらを使うことで簡単に原因を特定できるケースがあります。
visualize されるのは分かりやすいですよね。
それらを tips で紹介できたらなと思います。

0a86666ca90016b2a28289dda921b0d7?s=128

Toshihiro Morimoto

August 20, 2016
Tweet

Transcript

  1. 2016/08/20 iOS Developers Conference Japan @dealforest Toshihiro Morimoto XcodeͰշదͳσόοάϥΠϑΛ௥͍ٻΊΔ

  2. Έͳ͞Μ ͜Μͳܦݧ͋Γ·ͤΜ͔ʁ ࣗݾ঺հ

  3. 8PSLT

  4. ͍͟ϦϦʔε͞ΕΔͱ... 9DPEF1MVHJO

  5. None
  6. ͦ͏Ͱ͢ɺצͷ͍͍Έͳ͞Μ͸͓෼͔ΓͰ͢Ͷ

  7. Xcode 8 ͔Β͸ Xcode Plugin ͕ಈ͔ͳ͘ͳΓ·ͨ͠

  8. RIP Xcode Plugin

  9. ͸͍ɺ͜Ε͔Βຊ୊Ͱ͢

  10. XcodeͰշదͳσόοάϥΠϑΛ௥͍ٻΊΔ

  11. iOS ΤϯδχΞʹͱͬͯ Xcode ͱ͸

  12. ෢࢜ʹͱͬͯͷ౛

  13. ྉཧਓʹͱͬͯͷแஸ

  14. Xcode ͳͯ͘͠ iOS ։ൃ͸Ͱ͖·ͤΜ

  15. Xcode ͷػೳΛ׆༻͢Ε͹ σόοά͕վળ͞ΕΔ৔߹͕ଟʑ͋Γ·͢

  16. ϓϩάϥϛϯάͷେ൒͸σόοάͩͱ ๻͸ࢥ͍ͬͯ·͢

  17. ݪҼʹͨͲΓͭͨ͘Ίʹ ৘ใ͕ଟ͍ʹӽͨ͜͠ͱ͸͋Γ·ͤΜ

  18. ͱ͍͏͜ͱͰ ීஈ͍ͯ͠Δσόοάͷ tips Λ ঺հ͍͖͍ͯͨ͠ͱࢥ͍·͢

  19. • Ϋϥογϡͨ͠Β AppDelegate ͩͬͨ໰୊ • Ͳͷ ViewController ͔Θ͔Βͳ͍໰୊ • ىಈ࣌ʹಛఆͷ

    ViewController ʹ͍ͨ͠໰୊ • ࣮ػͷϩά΍ϑΝΠϧΛऔಘ͍ͨ͠໰୊ ຊ೔ͷ͓͠ͳ͕͖
  20. Ϋϥογϡͨ͠Β AppDelegate ͩͬͨ໰୊

  21. Ϋϥογϡͨ͠Β AppDelegate ͩͬͨ໰୊

  22. ΋͘͠͸Ξηϯϒϥͩͬͨ໰୊

  23. • Exception Breakpoint Λ௥Ճ • Diagonostics Λઃఆ ղܾํ๏

  24. Exception Breakpoint Λ௥Ճ

  25. Exception Breakpoint Λઃఆ͓ͯ͘͠ͱɹɹ exception ͕ൃੜͨ͠λΠϛϯάͰ Break ͯ͘͠ΕΔ Exception Breakpoint Λ௥Ճ

  26. None
  27. None
  28. None
  29. None
  30. ্ͨͩ͠ख͘࢖͑ͳ͍έʔε΋͋ͬͯ Exception લఏͷίʔυ͕ॻ͔Ε͍ͯΔͱ ࠓճͷҙਤͱ͸ҧ͏λΠϛϯάͰ Break ͯ͠͠·͏ Exception Breakpoint Λ௥Ճ

  31. ্ͨͩ͠ख͘࢖͑ͳ͍έʔε΋͋ͬͯ Exception લఏͷίʔυ͕ॻ͔Ε͍ͯΔͱ ࠓճͷҙਤͱ͸ҧ͏λΠϛϯάͰ Break ͯ͠͠·͏ ex) .storyboard ͕ͳ͚Ε͹ .nib

    ʹϑΥʔϧόοΫ͢Δ try ͰΩϟον͍ͯͯ͠΋ break ͯ͠͠·͏ Exception Breakpoint Λ௥Ճ
  32. Diagonostics Λઃఆ

  33. •ෆਖ਼ͳϝϞϦૢ࡞Λݕ஌ •dynamic library ؔ࿈ͷϩΪϯά (dlopen, dlsym ౳) Diagonostics Λઃఆ

  34. None
  35. None
  36. None
  37. Ͳ͏ઃఆ͍͍͔ͯ͠෼͔Βͳ͍…

  38. ͦΜͳΈͳ͞ΜͷͨΊͷΦεεϝͷઃఆͰ͢ ɹɹ ʢXcode 7.3.1ʣ

  39. None
  40. ύϑΥʔϚϯε͕ؾʹͳΔ৔߹͸ Enable Address Sanitizer ΛΦϑʹͯ͠Լ͍͞

  41. (lldb) po self <ViewController: 0x61800000e080> (lldb) memory history 0x61800000e080 thread

    ... name = 'Memory allocated at' frame #0: 0x00000001051bba97 libclang_rt.asan_iossim_dynamic.dylib`wrap_calloc + 199 frame #1: 0x00000001064362fd libobjc.A.dylib`class_createInstance + 84 frame #2: 0x0000000106440dc7 libobjc.A.dylib`_objc_rootAlloc + 41 frame #3: 0x00000001072d6d25 UIKit`-[UIClassSwapper initWithCoder:] + 175 frame #4: 0x00000001074c731b UIKit`UINibDecoderDecodeObjectForValue + 683 ... Enable Address Sanitizer Λ༗ޮʹ͍ͯ͠Δͱ…
  42. Ͳͷ ViewController ͔Θ͔Βͳ͍໰୊

  43. मਖ਼ͨ͠Γػೳ௥Ճ͠Α͏ͱࢥͬͨ࣌ʹ Ͳͷ ViewController ͔Θ͔Βͳ͍ Ͳͷ ViewController ͔Θ͔Βͳ͍໰୊

  44. ͦΜͳܦݧ͋Γ·ͤΜ͔ʁʁ Ͳͷ ViewController ͔Θ͔Βͳ͍໰୊

  45. Ͱ͸ɺͲ͏͍͏ঢ়گͰى͜Δͷͱ͔ Ͳͷ ViewController ͔Θ͔Βͳ͍໰୊

  46. • ৽͍͠ϓϩδΣΫτʹؔΘΓ࢝Ίͨ࣌ • స৬ͨ͠͹͔ͬΓͰΑ͘෼͔Βͳ͍࣌ ɹ ͳͲͳͲ… Ͳͷ ViewController ͔Θ͔Βͳ͍໰୊

  47. ͱ͸͍͑ϦϞʔτͰ࡞ۀ͍ͯ͠Δͱ Ӧۀ࣌ؒ֎ʹɺΘ͟Θ͟ฉ͘ͷ΋ͳͱ… Ͳͷ ViewController ͔Θ͔Βͳ͍໰୊

  48. Symbolic Breakpoint with action Λ࢖ͬͯ viewWillAppear / viewDidLoad ͕ ࣮ߦ͞ΕͨλΠϛϯάͰϩάʹදࣔ͢Δ

    ղܾํ๏
  49. None
  50. None
  51. None
  52. Symbolic Breakpoint ObjCͷ৔߹ viewWillApper: viewDidLoad

  53. (lldb) bt 0 // suggest * thread #1: tid =

    0x10503c9, 0x00000001088569f4 dealforest`ViewController.viewWillAppear(animated=false, self=0x000061800000e080) -> () + 20 at ViewController.swift:19, queue = 'com.apple.main-thread', stop reason = breakpoint 6.1 (lldb) po "[viewWillAppear] - \(self)”// custom format "[viewWillAppear] - <dealforest.ViewController: 0x61800000e080>" Action - Debugger Command
  54. viewDidLoad ͱ viewWillAppear Λ ઃఆ͠ͱ͚͹େମϩάʹग़ྗ͞ΕΔ͸ͣ

  55. ཧ༝ͱͯ͠͸ ViewController Ͱ ࣮૷͓͔ͯ͠ͳ͍ͱ Symbolic Breakpoint ͕ݺ͹Εͳ͍ͨΊͰ͢

  56. viewDidLoad ͕ͳ͍͔΋͠Εͳ͍͠ viewWillAppear ͕ͳ͍͔΋͠Εͳ͍ Ͱ΋྆ํͳ͍ͷ͸গͳ͍͸ͣ

  57. ϩά͕ᓔಃ͍͠ͱײͨ࣌͡͸ Breakpoint Λແޮʹ͢Δ͚ͩͰ͢

  58. νʔϜͰڞ༗͍ͨ͠৔߹͸ Breakpoint Λڞ༗͢Δ͜ͱ͕Ͱ͖·͢

  59. None
  60. ͓·͚

  61. ͳͥ bt 0 ͕͓͢͢Ίͳͷ͔ͱ͍͏ͱ

  62. Xcode Plugin - KZLinkedConsole ͱ ૬ੑ͕ͱͯ΋͍͍͔ΒͰ͢

  63. krzysztofzablocki / KZLinkedConsole

  64. (lldb) bt 0 * thread #1: tid = 0x10503c9, 0x00000001088569f4

    dealforest`ViewController.viewWillAppear(animated=false, self=0x000061800000e080) -> () + 20 at ViewController.swift:19, queue = 'com.apple.main-thread', stop reason = breakpoint 6.1 Action - Debugger Command ϦϯΫʹͳΓλοϓ͢Δͱ։͚Δ
  65. ىಈ࣌ʹಛఆͷ ViewController ʹ͍ͨ͠ ໰୊

  66. ભҠ͕໘౗͍͘͞ը໘ͷػೳΛ ։ൃ͢Δ͜ͱʹͳΓ·ͨ͠ ىಈ࣌ʹಛఆͷ ViewController ʹ͍ͨ͠໰୊

  67. ىಈ࣌ʹಛఆͷ ViewController ʹ͍ͨ͠໰୊

  68. σόοά͢Δͱ͖ʹमਖ਼ͯ͠࠶ىಈ ͦͷޙɺը໘·ͰભҠͯ͠… ͜ΕΛ܁Γฦ͢ ىಈ࣌ʹಛఆͷ ViewController ʹ͍ͨ͠໰୊

  69. ΊΜͲ͘͘͞ͳ͍Ͱ͔͢ʁʁ ىಈ࣌ʹಛఆͷ ViewController ʹ͍ͨ͠໰୊

  70. Environment Variables Λ࢖͍ɹɹɹɹɹɹɹɹɹɹɹɹ ֘౰͢Δ ViewController Λදࣔ͢Δ ղܾํ๏

  71. None
  72. DF_STORYBOARD_NAME = Main:settings

  73. දࣔ͢ΔॲཧΛdidFinishLaunchingWithOptions ʹ௥Ճ if let env = NSProcessInfo().environment["DF_STORYBOARD_NAME"] { let names

    = env.componentsSeparatedByString(":") if let storyboardName = names.first { let storyboard = UIStoryboard(name: storyboardName, bundle: nil) let controller: UIViewController if let identifier = names.last where identifier != storyboardName { controller = storyboard .instantiateViewControllerWithIdentifier(identifier) } else { controller = storyboard.instantiateInitialViewController()! } window?.rootViewController = controller } }
  74. Xcode ͔Βىಈͨ࣌͠ͷΈ༗ޮ App Switcher Ͱ kill ͯ͠ىಈ͠ͳ͓ͤ͹ແޮʹͳΔ Environment Variables Λ࢖͏ϝϦοτ

  75. Ұ࣌తͳ෼ذΛ࡞Γ͍ͨ࣌ʹ ͱͯ΋ศར

  76. • ڧ੍తʹ logout • API ͷϦΫΤετઌΛมߋ • localhost:3000 • proxy

    • ϩάϨϕϧͷ੾Γସ͑ Environment Variables ͷ࢖͍ॴ
  77. ࣮ػͷϩά΍ϑΝΠϧΛऔಘ͍ͨ͠໰୊

  78. ࣗલͰग़ྗ͍ͯ͠Δϩά΋͘͠͸ DBϑΝΠϧ(Realm, CoreData, SQLite…)Λ Έ͍ͨ࣌͋Γ·͢ΑͶʁ ࣮ػͷϩά΍ϑΝΠϧΛऔಘ͍ͨ͠໰୊

  79. ͱ͸͍͑औಘ͠Α͏ͱࢥ͏ͱ ݁ߏΊΜͲ͍͘͞ ࣮ػͷϩά΍ϑΝΠϧΛऔಘ͍ͨ͠໰୊

  80. • iExproler Λ࢖͍ iPhone ΛϚ΢ϯτ͠औಘ • PC ʹܨ͗ Xcode Ͱ

    Container Λऔಘɹɹɹɹ (͜ͷ৔߹ɺࣗ෼͕Ճೖ͍ͯ͠ΔνʔϜͷΞϓϦͷΈͰ͢) ࣮ػͷϩά΍ϑΝΠϧΛऔಘ͍ͨ͠໰୊
  81. ໘౗͍͘͞Ͱ͢Ͷ… ΋ͬͱؾܰʹ͍ͨ͠΋ͷͰ͢ ࣮ػͷϩά΍ϑΝΠϧΛऔಘ͍ͨ͠໰୊

  82. LLDB Λ࢖ͬͯ֘౰͢ΔϑΝΠϧΛ Slack ΁ૹ৴ ղܾํ๏

  83. DEMO

  84. (lldb) slack “Documents/default.realm” Please run ‘continue’ (lldb) continue Process 25152

    resuming upload success Slack ʹϑΝΠϧΛૹ৴
  85. Slack Ͱड͚औͬͨ࣌ͷը૾

  86. ศརͰ͠ΐʁʁ

  87. ࢖͍͍ͨͰ͢ΑͶʁʁ

  88. https://gist.github.com/dealforest/702a848ba0a7a64b139985f5cad885f0

  89. ίϚϯυΛಡΈࠐΉ $ cat ~/.lldbinit … command script import <path_to>/slack.py

  90. And more…

  91. • Xcode Functions • memory visualizer • Xcode Plugin •

    LLDB • p, po, frame variable • watchpoint • facebook/chisel And more
  92. • Xcode Settings - Diagonostics - Environment Variables • Breakpoint

    - Exception Breakpoint - Symbolic Breakpoint • LLDB Recap
  93. গ͠Ͱ΋Έͳ͞ΜͷσόοάαΠΫϧ͕ɹɹɹɹ շదʹͳΕ͹޾͍Ͱ͢

  94. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠

  95. ͜ͷΑ͏ͳσόοάʹ·ͭΘΔ࿩Λɹɹɹɹ ମܥཱͯͯձࣾͰ࿩͢Α͏ͳ͜ͱ΋ ɹɹɹɹ ͍ͯ͠ΔͷͰڵຯ͋Δํ͸੠͕͚Լ͍͞# એ఻