如何在 Xcode 中设置线程特定断点?

发布于 2024-10-07 01:58:41 字数 106 浏览 0 评论 0原文

我正在调试非主线程使用 UIKit 绘图方法的崩溃。我想在 -[UIView layoutSubviews] 上设置一个条件断点,只有在非主线程中执行时才会触发。这可能吗?

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 this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗喜 2024-10-14 01:58:41

您可以使用 gdb 控制台设置每个线程断点。例如:(

b -[UIView layoutSubviews] thread 2

您使用 gdb“infothreads”命令来查看存在哪些线程以及 gdb 对它们使用什么标识符)。

我认为没有办法为除主线程(线程 1)之外的每个线程设置断点,但是如果您有合理数量的线程,则可以为每个线程单独设置断点,如果必要的。


更新:

如果由于 GCD,每个线程的问题无法解决,您可以采取的另一种方法是设置一个常规断点,并为该断点设置 gdb 命令以转储回溯(“where”),然后“继续”。

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".

冷默言语 2024-10-14 01:58:41

一段非常有用的代码可以完全满足您的要求。它也比使用断点快得多。它通过混合一些 UIKit 方法(setNeedsLayout、setNeedsDisplay 等)并在主线程以外的线程上调用任何方法时抛出断言来实现此目的。

它非常适合在开发过程中捕获这些类型的缺陷,但请务必记住将其从生产代码中删除。如果需要的话,适应 AppKit 也很简单。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文