如何在 iPad 应用程序中循环所有活动线程
在我正在创建的 iPad 应用程序中,我尝试通过输出异常的 callStackSymbols 来处理未捕获的异常。这可以通过 [NSException callStackSymbols] 来完成。
但是,我希望也能够在所有其他活动线程上看到 callStackSymbols。我知道我可以在任何线程上使用 [NSThread callStackSymbols],但我需要循环遍历所有活动线程才能执行此操作。
这可能吗?
In the iPad app that I'm creating, I'm trying to handle the uncaught Exceptions by outputting the callStackSymbols of the exception. This can be done with [NSException callStackSymbols]
However, I'd like to be able to see the callStackSymbols on all the other active threads as well. I know I can use [NSThread callStackSymbols]
on any thread, but I need to loop through all the active threads to do so.
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如沉静的外星人所指出的,这是一个复杂的领域。您将需要实现自己的堆栈遍历以从其他活动线程检索堆栈跟踪;像 backtrace(3) 和 +[NSThread callStackSymbols] 这样的 API 只会产生当前线程的回溯。
由于我熟悉 PLCrashReporter 代码,因此我将仅使用它作为示例:
要获取线程列表,您需要使用 task_threads(): https://opensource.plausible.coop/stash/projects/PLCR/repos/plcrashreporter/browse/Source/PLCrashLogWriter.m?at=refs%2Ftags%2F1.0#694< /p>
https://opensource.plausible.coop/stash/projects/PLCR/repos/plcrashreporter/browse/Source/PLCrashFrameWalker_arm.c?at=refs%2Ftags%2F1.0#104< /p>
注意所有这些代码都有点不寻常,因为它在信号处理程序中执行并且被编写为异步安全的;如果您不熟悉信号处理的复杂性,这是一个很好的起点:http://www.mikeash.com/pyblog/friday-qa-2011-04-01-signal-handling.html
实现这种正确的事情是一个巨大的头痛;我真的建议您使用 PLCrashReporter,或在其之上构建的产品之一(QuincyKit, HockeyApp, Atlassian JMC 等)。
This is a complex area, as Sedate Alien noted. You will need to implement your own stack walking to retrieve a stack trace from the other active threads; APIs such as backtrace(3) and +[NSThread callStackSymbols] will only produce a backtrace for the current thread.
Since I'm familiar with the PLCrashReporter code, I'll just use it for examples:
To fetch the thread list, you'll need to use task_threads(): https://opensource.plausible.coop/stash/projects/PLCR/repos/plcrashreporter/browse/Source/PLCrashLogWriter.m?at=refs%2Ftags%2F1.0#694
Once you have the thread list, you can fetch the thread state via thread_get_state(): https://opensource.plausible.coop/stash/projects/PLCR/repos/plcrashreporter/browse/Source/PLCrashFrameWalker_arm.c?at=refs%2Ftags%2F1.0#73
Using the thread state, you can walk the target's stack:
https://opensource.plausible.coop/stash/projects/PLCR/repos/plcrashreporter/browse/Source/PLCrashFrameWalker_arm.c?at=refs%2Ftags%2F1.0#104
Note that all this code is a bit unusual, as it executes within a signal handler and is written to be async-safe; if you're unfamiliar with the complications around signal handling, this is a good starting point: http://www.mikeash.com/pyblog/friday-qa-2011-04-01-signal-handling.html
Implementing this kind of thing correctly is a gigantic headache; I would really recommend that you make use of PLCrashReporter, or one of the products built on top of it (QuincyKit, HockeyApp, Atlassian JMC, etc).
这似乎充满危险,如果您需要在这里寻求帮助,则更是如此。我可以推荐 PLCrashReporter 吗?其列出的功能包括:
更好的是,看看 QuincyKit,它是 PLCrashReporter 的一个非常方便的包装器。
This seems fraught with peril, doubly so if you need to ask here for help. May I suggest PLCrashReporter? Its listed features are:
Better yet, have a look at QuincyKit, a very handy wrapper around PLCrashReporter.