I'm debugging a crash where a non-main thread uses UIKit drawing methods. I'd like to set a conditional breakpoint on -[UIView layoutSubviews]
that only triggers, if it's executed in a non-main thread. Is th开发者_Python百科is possible?
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.
精彩评论