How to set Thread specific breakpoints in Xcode? How to set Thread specific breakpoints in Xcode? multithreading multithreading

How to set Thread specific breakpoints in Xcode?


You can set per-thread breakpoints using the gdb console. For example:

b -[UIView layoutSubviews] thread 2

(You use the gdb "info threads" commands to see what threads exist and what identifier gdb uses for them).

I don't think there's a way to set a breakpoint for every thread except the main thread (thread 1), but if you have a reasonable number of threads you could set breakpoints for each of them individually if necessary.


update:

If the per-thread thing isn't working out because of GCD, another approach you could take would be to just set a regular breakpoint, and set gdb commands for that breakpoint to dump out a backtrace ("where") and then "continue".


There is a very useful bit of code that accomplishes exactly what you are asking. It's also much faster than using breakpoints. It does this by method swizzling a few UIKit methods (setNeedsLayout, setNeedsDisplay, etc.) and throwing an assertion if any are called on a thread other than the main thread.

It works great for catching these types of flaws during development, but be sure to remember to remove it from production code. It's also trivial to adapt to AppKit if need be.