<div dir="ltr">I dug a little deeper, inspecting the GDB remote packets send by LLDB to perform the stepping. It appears when sending memory breakpoint commands used for stepping, the size of the instruction being replaced isn't taken into account, or writing back the original instruction isn't done properly. The following log shows what happens when stepping into the previously mentioned function:<div><br></div><div><div>(lldb) s</div><div>Process 166 stopped</div><div>* thread #1: tid = 0x0fd9, 0x002602e0 AttachTestIOSDev`[J]java.lang.Object.<init>(__$env=0x016bffc8, __$this=0x017864b0)V + 12 at Object.java:136, queue = 'com.apple.main-thread', stop reason = step in</div><div> frame #0: 0x002602e0 AttachTestIOSDev`[J]java.lang.Object.<init>(__$env=0x016bffc8, __$this=0x017864b0)V + 12 at Object.java:136</div><div>(lldb) disassemble -p</div><div>AttachTestIOSDev`[J]java.lang.Object.<init>()V + 12 at Object.java:136:</div><div>-> 0x2602e0: ldr r2, [r1]</div><div> 0x2602e2: ldr r2, [r2, #0x30]</div><div> 0x2602e4: tst.w r2, #0x100000</div><div> 0x2602e8: it ne</div><div>(lldb) s</div><div>Process 166 stopped</div><div>* thread #1: tid = 0x0fd9, 0x002602ec AttachTestIOSDev`[J]java.lang.Object.<init>(__$env=0x016bffc8, __$this=0x017864b0)V + 24 at Object.java:136, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=EXC_ARM_UNDEFINED, subcode=0xffd1b001)</div><div> frame #0: 0x002602ec AttachTestIOSDev`[J]java.lang.Object.<init>(__$env=0x016bffc8, __$this=0x017864b0)V + 24 at Object.java:136</div><div>(lldb) disassemble -p</div><div>AttachTestIOSDev`[J]java.lang.Object.<init>()V + 24 at Object.java:136:</div><div>-> 0x2602ec: .long 0xb001ffd1 ; unknown opcode</div><div> 0x2602f0: pop {r7, pc}</div><div><br></div><div>AttachTestIOSDev`[J]java.lang.Object.<init>()V + 30:</div><div> 0x2602f2: nop </div><div><br></div><div>AttachTestIOSDev`[J]java.lang.Object.clone()Ljava/lang/Object; at Object.java:154:</div><div> 0x2602f4: push {r4, r5, r7, lr}</div><div>(lldb) disassemble -f</div><div>AttachTestIOSDev`[J]java.lang.Object.<init>()V at Object.java:136:</div><div> 0x2602d4: push {r7, lr}</div><div> 0x2602d6: mov r7, sp</div><div> 0x2602d8: sub sp, #0x4</div><div> 0x2602da: movs r2, #0x0</div><div> 0x2602dc: str r2, [sp]</div><div> 0x2602de: str r1, [sp]</div><div> 0x2602e0: ldr r2, [r1]</div><div> 0x2602e2: ldr r2, [r2, #0x30]</div><div> 0x2602e4: tst.w r2, #0x100000</div><div> 0x2602e8: it ne</div><div> 0x2602ea: blne 0x44b290 ; _bcRegisterFinalizer</div><div> 0x2602ee: add sp, #0x4</div><div> 0x2602f0: pop {r7, pc}</div><div><br></div><div>AttachTestIOSDev`[J]java.lang.Object.<init>()V + 30:</div><div> 0x2602f2: nop </div></div><div><br></div><div>The first step succeeds and ends up right after the prologue, at 0x2602e0: ldr r2, [r1]. The next step ends up at 0x2602ec: .long 0xb001ffd1 which is wrong, it should be 0x2602ea: blne 0x44b290.</div><div><br></div><div>The GDB remote conversation between lldb and the debugserver on the device (only relevant parts):</div><div><br></div><div><div># First step</div><div>lldb->debugserver: $Z0,2602e0,2#73</div><div>debugserver->lldb: $OK#00</div><div>lldb->debugserver: $vCont;c:0fd9#15</div><div>debugserver->lldb: (320) $T05thread:fd9;qaddr:37ebfad0;threads:fd9,ffa,ffb,ffd,fff,1009,100a,100b;00:c8ff6b01;01:b0647801;02:00000000;03:c87d6a00;04:00000000;05:c8ff6b01;06:fc6a6501;07:0c6a6501;08:90e96b01;09:28000000;0a:74a0ea37;0b:c8ff6b01;0c:b09e5b00;0d:086a6501;0e:d1b22000;0f:</div><div><br></div><div># Second step</div><div>lldb->debugserver: $Z0,2602ea,2#a4</div><div>debugserver->lldb: $OK#00</div><div>lldb->debugserver: $vCont;c:0fd9#15</div><div>debugserver->lldb: (324) $T92thread:fd9;qaddr:37ebfad0;threads:fd9,ffa,ffb,ffd,fff,1009,100a,100b;00:c8ff6b01;01:b0647801;02:01004300;03:c87d6a00;04:00000000;05:c8ff6b01;06:fc6a6501;07:0c6a6501;08:90e96b01;09:28000000;0a:74a0ea37;0b:c8ff6b01;0c:b09e5b00;0d:086a6501;0e:d1b22000;0f:</div></div><div><br></div><div>For the first step, a 2 byte memory breakpoint is written to 0x2602e0 ($Z0,2602e0,2#73), which is where the first step ended up. The instruction that got replaced is 2 bytes long. The GDB command wrote a 2 bytes memory breakpoint to the address, so all is good.</div><div><br></div><div>For the second step, a 2 byte memory breakpoint is written to 0x2602ea ($Z0,2602ea,2#a4). But instead of ending up at 0x2602ec, which is in the middle of the 4-byte blne instruction.</div><div><br></div><div>Is it correct for LLDB to set a 2 byte memory breakpoint instead of a 4-byte memory breakpoint in this case? The PC will be set to an invalid address, which then causes the EXC_BAD_INSTRUCTION.</div><div><br></div><div>Am i understanding this correctly? Is there a way for me to fix this?</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Nov 26, 2014 at 5:26 PM, Mario Zechner <span dir="ltr"><<a href="mailto:badlogicgames@gmail.com" target="_blank">badlogicgames@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi,<div><br></div><div>we generate thumbv7 binaries for iOS devices. We deploy, launch and debug those via LLDB. Stepping into functions seems to almost always generate a EXC_BAD_INSTRUCTION signal. The signal is not generated when running the app without the debugger attached. It is also not generated when we attach a debugger, but simply let the app run without breakpoints or any stepping.</div><div><br></div><div>Here's one of these function's LLVM IR:</div><div><br></div><div>=======================</div><div><div>define external void @"[J]java.lang.Object.<init>()V"(%Env* %p0, %Object* %p1) nounwind noinline optsize {</div><div>label0:</div><div> call void @"llvm.dbg.declare"(metadata !{%Env* %p0}, metadata !19), !dbg !{i32 136, i32 0, metadata !{i32 786478, metadata !0, metadata !1, metadata !"[J]java.lang.Object.<init>()V", metadata !"[J]java.lang.Object.<init>()V", metadata !"", i32 136, metadata !15, i1 false, i1 true, i32 0, i32 0, null, i32 256, i1 false, void (%Env*, %Object*)* @"[J]java.lang.Object.<init>()V", null, null, metadata !17, i32 136}, null}</div><div> %r0 = alloca %Object*</div><div> store %Object* null, %Object** %r0</div><div> call void @"llvm.dbg.declare"(metadata !{%Object** %r0}, metadata !21), !dbg !{i32 136, i32 0, metadata !14, null}</div><div> store %Object* %p1, %Object** %r0</div><div> call void @"register_finalizable"(%Env* %p0, %Object* %p1), !dbg !{i32 136, i32 0, metadata !18, null}</div><div> ret void, !dbg !{i32 136, i32 0, metadata !18, null}</div><div>}</div></div><div>=======================<br></div><div><br></div><div>The corresponding thumbv7 assembler code as generated by LLVM:</div><div><br></div><div>=======================<br></div><div><div><span style="white-space:pre-wrap"> </span>.globl<span style="white-space:pre-wrap"> </span>"_[J]java.lang.Object.<init>()V"</div><div><span style="white-space:pre-wrap"> </span>.align<span style="white-space:pre-wrap"> </span>2</div><div><span style="white-space:pre-wrap"> </span>.code<span style="white-space:pre-wrap"> </span>16 @ @"[J]java.lang.Object.<init>()V"</div><div><span style="white-space:pre-wrap"> </span>.thumb_func<span style="white-space:pre-wrap"> </span>"_[J]java.lang.Object.<init>()V"</div><div>"_[J]java.lang.Object.<init>()V":</div><div><span style="white-space:pre-wrap"> </span>.cfi_startproc</div><div>Lfunc_begin18:</div><div><span style="white-space:pre-wrap"> </span>.loc<span style="white-space:pre-wrap"> </span>1 136 0 @ Object.java:136:0</div><div>@ BB#0: @ %label0</div><div><span style="white-space:pre-wrap"> </span>.loc<span style="white-space:pre-wrap"> </span>1 136 0 @ Object.java:136:0</div><div><span style="white-space:pre-wrap"> </span>push<span style="white-space:pre-wrap"> </span>{r7, lr}</div><div><span style="white-space:pre-wrap"> </span>mov<span style="white-space:pre-wrap"> </span>r7, sp</div><div><span style="white-space:pre-wrap"> </span>sub<span style="white-space:pre-wrap"> </span>sp, #4</div><div><span style="white-space:pre-wrap"> </span>@DEBUG_VALUE: [J]java.lang.Object.<init>()V:__$env <- R0</div><div><span style="white-space:pre-wrap"> </span>movs<span style="white-space:pre-wrap"> </span>r2, #0</div><div><span style="white-space:pre-wrap"> </span>str<span style="white-space:pre-wrap"> </span>r2, [sp]</div><div><span style="white-space:pre-wrap"> </span>str<span style="white-space:pre-wrap"> </span>r1, [sp]</div><div><span style="white-space:pre-wrap"> </span>.loc<span style="white-space:pre-wrap"> </span>1 136 0 prologue_end @ Object.java:136:0</div><div>Ltmp6:</div><div><span style="white-space:pre-wrap"> </span>ldr<span style="white-space:pre-wrap"> </span>r2, [r1]</div><div><span style="white-space:pre-wrap"> </span>ldr<span style="white-space:pre-wrap"> </span>r2, [r2, #48]</div><div><span style="white-space:pre-wrap"> </span>tst.w<span style="white-space:pre-wrap"> </span>r2, #1048576</div><div>Ltmp7:</div><div><span style="white-space:pre-wrap"> </span>@DEBUG_VALUE: [J]java.lang.Object.<init>()V:__$env <- R0</div><div><span style="white-space:pre-wrap"> </span>it<span style="white-space:pre-wrap"> </span>ne</div><div><span style="white-space:pre-wrap"> </span>blxne<span style="white-space:pre-wrap"> </span>__bcRegisterFinalizer</div><div><span style="white-space:pre-wrap"> </span>add<span style="white-space:pre-wrap"> </span>sp, #4</div><div><span style="white-space:pre-wrap"> </span>pop<span style="white-space:pre-wrap"> </span>{r7, pc}</div><div>Ltmp8:</div><div>Lfunc_end18:</div><div>"L_[J]java.lang.Object.<init>()V_end":</div><div><br></div><div><span style="white-space:pre-wrap"> </span>.cfi_endproc</div></div><div>=======================<br></div><div><br></div><div>Now, when stepping into this function, LLDB receives a signal from the debug server:</div><div><br></div><div><div>=======================<br></div><div>(lldb) s</div><div>Process 176 stopped</div><div>* thread #1: tid = 0x11f5, 0x0023e2ec AttachTestIOSDev`[J]java.lang.Object.<init>(__$env=0x0169efc8, __$this=0x0174cd10)V + 24 at Object.java:136, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=EXC_ARM_UNDEFINED, subcode=0xffd1b001)</div><div> frame #0: 0x0023e2ec AttachTestIOSDev`[J]java.lang.Object.<init>(__$env=0x0169efc8, __$this=0x0174cd10)V + 24 at Object.java:136</div></div><div>=======================<br></div><div><br></div><div>Disassembling around the PC gives:</div><div><br></div><div>=======================<br></div><div><div>(lldb) disassemble --pc</div><div>AttachTestIOSDev`[J]java.lang.Object.<init>()V + 24 at Object.java:136:</div><div>-> 0x23e2ec: .long 0xb001ffd1 ; unknown opcode</div><div> 0x23e2f0: pop {r7, pc}</div><div><br></div><div>AttachTestIOSDev`[J]java.lang.Object.<init>()V + 30:</div><div> 0x23e2f2: nop</div></div><div><br></div><div>Disassembling until the beginning of the frame gives:</div><div><br></div><div><div>(lldb) disassemble -f</div><div>AttachTestIOSDev`[J]java.lang.Object.<init>()V at Object.java:136:</div><div> 0x23e2d4: push {r7, lr}</div><div> 0x23e2d6: mov r7, sp</div><div> 0x23e2d8: sub sp, #0x4</div><div> 0x23e2da: movs r2, #0x0</div><div> 0x23e2dc: str r2, [sp]</div><div> 0x23e2de: str r1, [sp]</div><div> 0x23e2e0: ldr r2, [r1]</div><div> 0x23e2e2: ldr r2, [r2, #0x30]</div><div> 0x23e2e4: tst.w r2, #0x100000</div><div> 0x23e2e8: it ne</div><div> 0x23e2ea: blne 0x429290 ; _bcRegisterFinalizer</div><div> 0x23e2ee: add sp, #0x4</div><div> 0x23e2f0: pop {r7, pc}</div></div><div><br></div><div>Accprding to this, execution should never end up at address 0x23e2ec. That's right in the middle of the blne and add instructions in the second disassembly. I have a hunch that the debugserver on the device may interfere here, e.g. add a trap instruction to implement the stepping. I'm not quite sure what to make of it.</div><div><br></div><div>I'd appreciate any hints. If you require more information, i got plenty of logs :)</div><div><br></div><div>Thanks,</div><div>Mario</div></div>
</blockquote></div><br></div>