LLDB の世界からみた Swift

LLDB の世界からみた Swift

AKIBA.swift×Swift愛好会

0a86666ca90016b2a28289dda921b0d7?s=128

Toshihiro Morimoto

August 29, 2016
Tweet

Transcript

  1. 2016/08/29 AKIBA.swift×SwiftѪ޷ձ @dealforest Toshihiro Morimoto LLDB ͷੈք͔ΒΈͨ Swift

  2. ࣗݾ঺հ

  3. Works

  4. ࠓ೔͸ iOSDC Ͱ͓࿩ͨ͠͠ ಺༰ͷҰ෦ΛਂງΓ͍͖ͯ͠·͢

  5. ͦͷલʹେࣄͳ͓ئ͍͕͋Γ·͢

  6. LLDB ͷ࿩͸ϙΧʔʔϯͱͳͬͯ ൓Ԡ͕ͳ͘൵͘͠ͳΔͷͰ 3ׂΓ૿͠Ͱ͓ئ͍͠·͢!

  7. Έͳ͞Μ Swift ͸޷͖Ͱ͔͢ʁʁ

  8. ͸ʔʔʔ͍

  9. Ͳ͜Ͱ࢖͏ Swift ͕޷͖Ͱ͔͢ʁʁ

  10. • iOS Application, Playground, etc.. • Server Side Swift •

    Swift Compiler • Swift Interpliter Language (SIL) • LLDB
  11. • iOS Application, Playground, etc.. • Server Side Swift •

    Swift Compiler • Swift Interpliter Language (SIL) • LLDB
  12. LLDB ͷੈք͔ΒΈͨ Swift

  13. ࣮ߦ͢Δ (lldb) p “AKIBA.swift×SwiftѪ޷ձ” (String) $R1 = “AKIBA.swift×SwiftѪ޷ձ" (lldb) po

    “AKIBA.swift×SwiftѪ޷ձ” “AKIBA.swift×SwiftѪ޷ձ” (lldb) po $R1 “AKIBA.swift×SwiftѪ޷ձ”
  14. ม਺Λఆٛ͢Δ (lldb) po let $text = “AKIBA.swift×SwiftѪ޷ձ" (lldb) po $text

    “AKIBA.swift×SwiftѪ޷ձ”
  15. ؔ਺Λఆٛ͢Δ // վߦ͢ΔͱΤϥʔʹͳΔͷͰҰߦͰॻ͘͜ͱ (lldb) po func $getText() -> String {

    return “AKIBA.swift×SwiftѪ޷ձ” ɹ} (lldb) po $getText() “AKIBA.swift×SwiftѪ޷ձ”
  16. UIImage ΛϑΝΠϧʹग़ྗ͢Δ (lldb) po let $path = “/Users/dealforest/lldb.png” (lldb) po

    let $image = UIImage(named: “sample”) (lldb) po let $data = UIImagePNGRepresentation($image)! (lldb) po $data.writeToFile($path, atomically: true)
  17. LLDB ͷੈք͔ΒݟΔͱ Կ͕ҧ͏ͷ͔

  18. ҧ͏఺ • 1໋ྩ1ߦ • ຖճ po Λ͚ͭΔඞཁ͕͋Δ • ࣗ෼Ͱఆٛͨ͠ม਺Λఆٛɺࢀর͢Δͱ͖ʹ $

    Λɹɹɹ ͚ͭΔඞཁ͕͋Δ
  19. طଘͷ Swift ίʔυΛ ͦͷ··ͩͱಈ͖·ͤΜ

  20. ΊΜͲ͍͘͞Ͱ͢Ͷ

  21. repl Ͱ࣮ߦͯ͠΍Ε͹ ͜ͷ͋ͨΓͷ໰୊͸େମղܾ͠·͢

  22. ͕

  23. LLDB ͷੈքʹ Swift ͷੈքΛ ࡞Γ্͛ΔͷͰগ͠ҧ͏ؾ΋͠·͢

  24. ͦΕʹ͜ͷ··Ͱ͸ Έͳ͞Μ࢖͏ؾ΋ى͖ͳ͍Ͱ͢ΑͶ

  25. Ͳ͏ʹ͔͍͖ͯ͠·͠ΐ͏

  26. ؔ਺ʹ͢Δ - before (lldb) po let $path = “/Users/dealforest/lldb.png” (lldb)

    po let $image = UIImage(named: “sample”) (lldb) po let $data = UIImagePNGRepresentation($image)! (lldb) po $data.writeToFile($path, atomically: true)
  27. ؔ਺ʹ͢Δ - after // վߦ͢ΔͱΤϥʔʹͳΔͷͰҰߦͰॻ͘͜ͱ (lldb) po func $saveImage(image: UImage)

    { let path = “/Users/dealforest/lldb.png”; let data = UIImagePNGRepresentation(image)!; data.writeToFile(path, atomically: true); } (lldb) po $saveImage(UIImage(named: “sample”))
  28. ؔ਺ʹ͢Δ // վߦ͢ΔͱΤϥʔʹͳΔͷͰҰߦͰॻ͘͜ͱ (lldb) po func $saveImage(image: UImage) { let

    path = “/Users/dealforest/lldb.png”; let data = UIImagePNGRepresentation(image)!; data.writeToFile(path, atomically: true); } (lldb) po $saveImage(UIImage(named: “sample”))
  29. ͦΕʹɺ͜ͷ··Ͱ͸ ͜ͷؔ਺͸ӬଓԽ͞Ε·ͤΜ

  30. ຖճఆٛͯ͠࢖͏ͱ͔…

  31. LLDB Plugin ʹ͢Δ #!/usr/bin/env python import lldb def save_image(debugger, command,

    result, internal_dict): frame = lldb.debugger.GetSelectedTarget().GetProcess().GetSelectedThread().GetSelectedFrame() result = frame.EvaluateExpression(command) target = result.GetLocation() expr = "expr -l objc++ -- (NSData *)UIImagePNGRepresentation((id)" + target + ")" frame.EvaluateExpression(expr) imagePath = '/Users/dealforest/lldb.png' saveImageTemplate = 'expr -l objc++ -- (void) [(NSData *){} writeToFile:@"{}" atomically:YES];' saveImageExpr = createImageFormatStr.format(result.GetName(), imagePath) lldb.debugger.HandleCommand(saveImageExpr) def __lldb_init_module(debugger,internal_dict): debugger.HandleCommand("command script add -f save_image.save_image save_image") print"save_image command enabled." save_image.py
  32. LLDB Plugin ʹ͢Δ command script import <path_to>/save_image.py // ͜ΕͰىಈ࣌ʹಡΈࠐ·ΕΔͨΊӬଓԽ͞ΕΔ ~/.lldbinit

  33. LLDB Plugin ʹ͢Δ #!/usr/bin/env python import lldb def save_image(debugger, command,

    result, internal_dict): frame = lldb.debugger.GetSelectedTarget().GetProcess().GetSelectedThread().GetSelectedFrame() result = frame.EvaluateExpression(command) target = result.GetLocation() expr = "expr -l objc++ -- (NSData *)UIImagePNGRepresentation((id)" + target + ")" frame.EvaluateExpression(expr) imagePath = '/Users/dealforest/lldb.png' saveImageTemplate = 'expr -l objc++ -- (void) [(NSData *){} writeToFile:@"{}" atomically:YES];' saveImageExpr = createImageFormatStr.format(result.GetName(), imagePath) lldb.debugger.HandleCommand(saveImageExpr) def __lldb_init_module(debugger,internal_dict): debugger.HandleCommand("command script add -f save_image.save_image save_image") print"save_image command enabled." ίϚϯυͷఆٛ save_image.py
  34. LLDB Plugin ʹ͢Δ #!/usr/bin/env python import lldb def save_image(debugger, command,

    result, internal_dict): frame = lldb.debugger.GetSelectedTarget().GetProcess().GetSelectedThread().GetSelectedFrame() result = frame.EvaluateExpression(command) target = result.GetLocation() expr = "expr -l objc++ -- (NSData *)UIImagePNGRepresentation((id)" + target + ")" frame.EvaluateExpression(expr) imagePath = '/Users/dealforest/lldb.png' saveImageTemplate = 'expr -l objc++ -- (void) [(NSData *){} writeToFile:@"{}" atomically:YES];' saveImageExpr = createImageFormatStr.format(result.GetName(), imagePath) lldb.debugger.HandleCommand(saveImageExpr) def __lldb_init_module(debugger,internal_dict): debugger.HandleCommand("command script add -f save_image.save_image save_image") print"save_image command enabled." UIImage ͷ pointer Λऔಘ save_image.py
  35. LLDB Plugin ʹ͢Δ #!/usr/bin/env python import lldb def save_image(debugger, command,

    result, internal_dict): frame = lldb.debugger.GetSelectedTarget().GetProcess().GetSelectedThread().GetSelectedFrame() result = frame.EvaluateExpression(command) target = result.GetLocation() expr = "expr -l objc++ -- (NSData *)UIImagePNGRepresentation((id)" + target + ")" frame.EvaluateExpression(expr) imagePath = '/Users/dealforest/lldb.png' saveImageTemplate = 'expr -l objc++ -- (void) [(NSData *){} writeToFile:@"{}" atomically:YES];' saveImageExpr = createImageFormatStr.format(result.GetName(), imagePath) lldb.debugger.HandleCommand(saveImageExpr) def __lldb_init_module(debugger,internal_dict): debugger.HandleCommand("command script add -f save_image.save_image save_image") print"save_image command enabled." UIImage Λ NSData ʹม׵ save_image.py
  36. LLDB Plugin ʹ͢Δ #!/usr/bin/env python import lldb def save_image(debugger, command,

    result, internal_dict): frame = lldb.debugger.GetSelectedTarget().GetProcess().GetSelectedThread().GetSelectedFrame() result = frame.EvaluateExpression(command) target = result.GetLocation() expr = "expr -l objc++ -- (NSData *)UIImagePNGRepresentation((id)" + target + ")" frame.EvaluateExpression(expr) imagePath = '/Users/dealforest/lldb.png' saveImageTemplate = 'expr -l objc++ -- (void) [(NSData *){} writeToFile:@"{}" atomically:YES];' saveImageExpr = createImageFormatStr.format(result.GetName(), imagePath) lldb.debugger.HandleCommand(saveImageExpr) def __lldb_init_module(debugger,internal_dict): debugger.HandleCommand("command script add -f save_image.save_image save_image") print"save_image command enabled." NSData ΛϑΝΠϧʹอଘ save_image.py
  37. ͡Ό͊͜ΕΛؔ਺Ͱॻ͘ͱ ͜͏ͳΓ·͢

  38. LLDB Plugin ʹ͢Δ #!/usr/bin/env python import lldb def save_image(debugger, command,

    result, internal_dict): lldb.debugger.HandleCommand(""" expr -l swift -- func $saveImage(image: UIImage) { let $data = UIImagePNGRepresentation($image)! $data.writeToFile("/Users/dealforest/lldb.png", atomically: true) } """.strip()) expr = 'expr -l swift -- $saveImage(' + command + ')' lldb.debugger.HandleCommand(expr) def __lldb_init_module(debugger,internal_dict): debugger.HandleCommand("command script add -f save_image.save_image save_image") print"save_image command enabled." save_image.py
  39. LLDB Plugin ʹ͢Δ lldb.debugger.HandleCommand(""" expr -l swift -- func $saveImage(image:

    UIImage) { ɹɹɹɹɹɹɹɹɹ ɹ let $path = “/Users/dealforest/lldb.png” let $data = UIImagePNGRepresentation($image)! $data.writeToFile(path, atomically: true) } """.strip()) save_image.py
  40. ͜ͷؔ਺ͷதͰ͸ ݟ׳Εͨ Swift Ͱ͢Ͷ

  41. ޻෉࣍ୈͰී௨ʹ Swift ͕ ॻ͚ΔΑ͏ʹͳΓ·͢

  42. ؆୯ʹσόοά༻ͷίϚϯυΛ ࡞Δ͜ͱ΋Ͱ͖ΔͷͰΦεεϝͰ͢Α

  43. ͍͔͕Ͱ͔ͨ͠ʁʁ

  44. Swift ͰΘ͍Θ͍͢Δωλʹ ͳΕΕ͹޾͍Ͱ͢

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