poから始めるlldb

 poから始めるlldb

8db1f2958e24accef8412659656fc8dc?s=128

Atsuya Sato

July 14, 2018
Tweet

Transcript

  1. po͔Β࢝ΊΔlldb ະདྷେ×اۀΤϯδχΞ ՆͷLTେձ

  2. • ࠤ౻ ರ໵ (@natmark) • ະདྷେ ஌ೳγεςϜίʔε 4೥ • γεςϜιϑτ΢ΣΞݚ

    (127Lab)
  3. ProcessingίʔυΛiOS্Ͱ ར༻Մೳʹ͢ΔϥΠϒϥϦ Xcode File TemplateΛ؅ཧ ͢ΔCLIπʔϧ natmark/ProcessingKit natmark/Donut

  4. ʲએ఻ʳiOSDC Japan 2018ʹొஃ͠·͢ʂ

  5. Έͳ͞ΜLLDB࢖ͬͯ·͔͢

  6. ࠓ೔͸LLDBʹ͍ͭͯ࿩͠·͢ɻ ͱݴͬͯ΋ɺ5෼͔͠ͳ͍ͷͰ

  7. • LLDBʹ͍ͭͯ • ͳͥະདྷେੜʹLLDBͳͷ͔ • LLDBͷ؆୯ͳ࢖͍ํ • (͕࣌ؒ༨Ε͹) LLDBΛ࢖ͬͨগ࣮͠ફతͳσ όοά๏ʹ͍ͭͯ

    ΛૣޱͰ঺հ͠·͢ʂ
  8. LLDBͱ͸ʁ

  9. Low Level Debugger

  10. ࣍ੈ୅ߴੑೳσόοΨ

  11. LLDB • ιϑτ΢ΣΞσόοΨ • LLVMϓϩδΣΫτͷҰ෦ͱͯ͠։ൃ • Clang[Ϋϥϯ] දࣜղੳػ΍LLVMٯΞηϯϒϥͳͲ Λ׆༻ͨ͠࠶ར༻Մೳͳίϯϙʔωϯτͷू·Γ •

    XcodeͷσϑΥϧτσόοΨ • Swift/C/C++ /Objective-C,/Objective-C++ͳͲͷ ίʔυΛ࣮ߦՄೳ
  12. ͳͥະདྷେੜʹLLDBͳͷ͔

  13. • ະདྷେͷCݴޠͷߨٛ͸MacࣨͰߦ͏ • Macʹ͸ඪ४ͰLLDBೖͬͯΔʂ(※1) • ະདྷେͷߨٛͰσόοΨʔͷ࢖͍ํΛڭΘΒͳ͍… ※1 ΋͔ͨ͠͠ΒXcodeೖΕͨλΠϛϯά͔΋

  14. σόοάʹprintfΛͨ͘͞Μॻ͍ͯ ͍·ͤΜ͔…?

  15. σόοά༻ʹॻ͍ͨprintfͰɺ ppchkall(※1)͕௨Βͳ͔ͬͨܦݧ͋ Γ·ͤΜ͔…ʁ ※1 ppchkall: ະདྷେͷϓϩάϥϛϯάجૅͷ՝୊νΣοΫίϚϯυ ςετέʔεͷग़ྗͱඪ४ग़ྗΛൺ΂ͯdiffग़͢΍ͭ

  16. ๻͸͋Γ·͢ʂʂʂʂʂʂʂ

  17. σόοΨΛ࢖͑ΔΑ͏ʹͳΖ͏ʂ ͱ͍͏࿩

  18. LLDBΛ࢖ͬͯΈΔ

  19. address.c #include <stdio.h> void addrcat(char *a, char *d) { while(*a)

    { // ຤ඌ·ͰϙΠϯλΛਐΊΔ a++; } *a++ = '@'; // @Λ௥Ճ͢Δ while(*d) { // dͷࢦ͢ΞυϨεʹจࣈ͕֨ೲ͞Ε͍ͯΔؒ *a++ = *d++; // aʹίϐʔ } } int main(void) { char account[100]; char domain[100]; scanf("%s", account); scanf("%s", domain); addrcat(account, domain); printf("%s\n", account); return 0; }
  20. address.cͷίϯύΠϧ $ gcc -o address -g address.c -g Generate source-level

    debug information
  21. $ ls address address.c address.dSYM address.dSYM : Ϗϧυ࣌ʹग़ྗ͞ΕΔσόοάγϯϘϧϑΝΠϧ

  22. $ lldb address (lldb) target create “address” Current executable set

    to ‘address’ (x86_64). (lldb) run //ϓϩάϥϜΛ࣮ߦ͢Δ Process 21476 launched: '/Users/AtsuyaSato/Desktop/test-lldb/ address' (x86_64) 1015006 fun.ac.jp 1015006@fun.ac.jp Process 21476 exited with status = 0 (0x00000000)
  23. LLDBΛىಈ͢Δ͜ͱ͕Ͱ͖ͨ

  24. ϒϨʔΫϙΠϯτΛஔ͍ͯɺ్தͰ ॲཧΛࢭΊͯΈΔ

  25.  addrcat(account, domain); (lldb) breakpoint set --file address.c --line 20

    //address.cͷ20ߦ ໨ʹϒϨʔΫϙΠϯτΛுΔ Breakpoint 1: where = address`main + 91 at address.c:20, address = x0000000100000f1bb1015006
  26. (lldb) run Process 22308 launched: '/Users/AtsuyaSato/Desktop/test-lldb/address' (x86_64) 1015006 fun.ac.jp Process

    22308 stopped * thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1 frame #0: 0x0000000100000f1b address`main at address.c:20 17 scanf("%s", account); 18 scanf("%s", domain); 19 -> 20 addrcat(account, domain); 21 printf("%s\n", account); 22 return 0; 23 } Target 0: (address) stopped.  addrcat(account, domain);
  27. ม਺ͷ஋Λͷ͍ͧͯΈΔ

  28. (lldb) po account //po(expression -O --) ࣜΛධՁɺ໭Γ ஋ͷܕʹ͋ΘͤͨϑΥʔϚοτͰදࣔ “1015006”

  29. ࣮ߦதͷϓϩάϥϜͷม਺Λ೷͘͜ ͱ͕Ͱ͖ͨ

  30. Ͱ΋ɺϝʔϧΞυϨεؒҧ͑ͯೖྗ ͯ͠͠·ͬͨ… (ະདྷେͷϝʔϧΞυϨε͸ֶ੶൪߸ͷલʹb͕ඞཁ)

  31. ࣮ߦதͷม਺ͷத਎Λมߋͯ͠ΈΔ

  32. (lldb) po strcpy(account, "b1015006"); 0x00007ffeefbfeca0 (lldb) po account “b1015006

  33. ࣮ߦதͷม਺Λߋ৽Ͱ͖ͨ

  34. ؔ਺ΛεςοϓɾΦʔόʔͯ͠ɺ 1ߦ෼ॲཧΛਐΊͯΈΔ

  35. (lldb) next //1ߦ෼ॲཧΛਐΊΔɻؔ਺͸εςοϓΦʔόʔ Process 22308 stopped * thread #1, queue

    = 'com.apple.main-thread', stop reason = step over frame #0: 0x0000000100000f31 address`main at address.c:21 18 scanf("%s", domain); 19 20 addrcat(account, domain); -> 21 printf("%s\n", account); 22 return 0; 23 } Target 0: (address) stopped.
  36. (lldb) po account “b1015006@fun.ac.jp"

  37. addrcatؔ਺͕ͪΌΜͱಈ͍ͯΔ͜ ͱ΋֬ೝͰ͖ͨ

  38. ͜ΕͰCݴޠͷߨٛͰɺ LLDBΛ࢖ͬͨ؆୯ͳσόοά͕Ͱ ͖ΔΑ͏ʹͳͬͨ

  39. ΊͰͨ͠ΊͰͨ͠

  40. ͕࣌ؒ༨ͬͨͷͰɺͪΐͬͱ ࣮ફతͳσόοά๏Λ঺հ͠·͢

  41. Symbolic Breakpoint

  42. • Symbolic Breakpoint • ϒϨʔΫ͍ͤͨ͞Ϋϥεͱϝιου໊Λॻ ͘ͱɺͦͷϝιου͕࣮ߦ͞ΕͨλΠϛϯ άͰϒϨʔΫͰ͖Δ • ϓϥΠϕʔτϥΠϒϥϦͳͲɺϝιου໊ ͸෼͔͍ͬͯΔ΋ͷͷBreakpointΛுΕͳ

    ͍৔߹ʹศར
  43. (lldb) breakpoint set --name “addrcat" //addrcatͱ͍͏ؔ਺ʹϒϨʔΫϙΠϯτΛுΔ Breakpoint 5: where =

    address`addrcat + 12 at address.c:4, address = 0x0000000100000e4c (lldb) run Process 23416 launched: '/Users/AtsuyaSato/Desktop/test-lldb/address' (x86_64) b1015006 fun.ac.jp Process 23416 stopped * thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 5.1 frame #0: 0x0000000100000e4c address`addrcat(a="b1015006", d="fun.ac.jp") at address.c:4 1 #include <stdio.h> 2 3 void addrcat(char *a, char *d) { -> 4 while(*a) { // ຤ඌ·ͰϙΠϯλΛਐΊΔ 5 a++; 6 } 7 *a++ = '@'; // @Λ௥Ճ͢Δ Target 0: (address) stopped.
  44. Breakpoint Commands & Thread jump

  45. • commands • ϒϨʔΫϙΠϯτʹΑͬͯϓϩάϥϜ͕ఀ ࢭͨ࣌͠ɺϒϨʔΫϙΠϯτʹରͯ͠Ұ࿈ ͷίϚϯυͷىಈΛࢦఆ͢Δ͜ͱ͕Ͱ͖Δ • jump • ϓϩάϥϜΧ΢ϯλͷ஋Λ৽͍͠ΞυϨε

    ʹมߋͰ͖Δ
  46. (lldb) breakpoint set --line 20 Breakpoint 1: where = address`main

    + 91 at address.c:20, address = 0x0000000100000f1b (lldb) breakpoint command add 1 //1൪ͷBreakpointʹίϚ ϯυΛઃఆ Enter your debugger command(s). Type 'DONE' to end. > thread jump --by 1 // ݱࡏͷεϨουͰ1ߦεΩοϓ͢Δ (lldb) breakpoint modify --auto-continue 1 // 1൪ͷBreakpoint ͕ϒϨʔΫͨ͠ࡍʹࣗಈͰcontinue͢ΔΑ͏ʹมߋ  addrcat(account, domain);
  47. (lldb) run Process 23852 launched: '/Users/AtsuyaSato/Desktop/ test-lldb/address' (x86_64) b1015006 fun.ac.jp

    (lldb) thread jump --by 1 //commandʹΑͬͯίʔυΠϯ δΣΫγϣϯ͞Εͨ b1015006 //addrcat(account, domain); ͕εΩοϓ͞Εͨ ͨΊɺग़ྗ͞Εͨaccount͸domain͕݁߹͞Ε͍ͯͳ͍ Process 23852 exited with status = 0 (0x00000000)
  48. ָ͍͠LLDBϥΠϑΛʂʂ