问题
Is there a way to programmatically enable a breakpoint? Normally I'd use a breakpoint condition, but I need to use a symbolic breakpoint.
Specifically, something is changing the contentOffset
of my UIScrollView between a call to scrollToIndexPath
and a call to reloadData
. I'd like to enable the symbolic breakpoint only after the call to scrollToIndexPath
.
回答1:
Jim Ingham's answer works on a x86 system. For an iOS device I was able to use the method described in this answer to programmatically create a breakpoint. Inserting the line
asm("svc 0");
Causes the debugger to stop on the following instruction in XCode.
Interestingly if you're not connected to XCode the program continues to execute normally.
回答2:
You can sort of do this by using the __builtin_trap(). On x86 systems, you will have to manually adjust the pc after hitting the trap. But that's pretty easy to do, and is sometimes useful if you have code that gets hit a lot and you need to only trap in some complex situation. Something like:
> lldb trapit
(lldb) target create "trapit"
Current executable set to 'trapit' (x86_64).
(lldb) run
Process 63363 launched: '/private/tmp/trapit' (x86_64)
About to trap.
Process 63363 stopped
* thread #1: tid = 0x2054b7, function: main , stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
frame #0: 0x0000000100000f4d trapit`main at trapit.c:6
3 int main()
4 {
5 printf ("About to trap.\n");
-> 6 __builtin_trap();
7 printf("Trapped.\n");
8 }
(lldb) dis -f
trapit`main:
0x100000f30 <+0>: pushq %rbp
0x100000f31 <+1>: movq %rsp, %rbp
0x100000f34 <+4>: subq $0x10, %rsp
0x100000f38 <+8>: leaq 0x47(%rip), %rdi ; "About to trap.\n"
0x100000f3f <+15>: movb $0x0, %al
0x100000f41 <+17>: callq 0x100000f66 ; symbol stub for: printf
0x100000f46 <+22>: leaq 0x49(%rip), %rdi ; "Trapped.\n"
-> 0x100000f4d <+29>: ud2
0x100000f4f <+31>: movl %eax, -0x4(%rbp)
0x100000f52 <+34>: movb $0x0, %al
0x100000f54 <+36>: callq 0x100000f66 ; symbol stub for: printf
0x100000f59 <+41>: xorl %ecx, %ecx
0x100000f5b <+43>: movl %eax, -0x8(%rbp)
0x100000f5e <+46>: movl %ecx, %eax
0x100000f60 <+48>: addq $0x10, %rsp
0x100000f64 <+52>: popq %rbp
0x100000f65 <+53>: retq
(lldb) reg write pc 0x100000f4f
(lldb) c
Process 63363 resuming
Trapped.
Process 63363 exited with status = 0 (0x00000000)
来源:https://stackoverflow.com/questions/34031957/xcode-programmatically-enable-a-symbolic-breakpoint