如何在iPad应用程序中遍历所有活动线程 [英] How To Loop Through All Active Thread in iPad app
问题描述
在我正在创建的iPad应用中,我正在尝试通过输出异常的callStackSymbols来处理未捕获的异常。这可以通过 [NSException 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]
完成。但是,我希望能够看到callStackSymbols在所有其他活动线程上也是如此。我知道我可以在任何线程上使用 [NSThread 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.
这可能吗?
推荐答案
这是一个复杂的区域,正如Sedate Alien所说。您需要实现自己的堆栈遍历以从其他活动线程中检索堆栈跟踪;诸如backtrace(3)和+ [NSThread callStackSymbols]之类的API只会为当前线程产生一个回溯。
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.
由于我熟悉PLCrashReporter代码,我'我只是用它作为例子:
Since I'm familiar with the PLCrashReporter code, I'll just use it for examples:
-
要获取线程列表,你需要使用 task_threads(): https://opensource.plausible.coop/ stash / projects / PLCR / repos / plcrashreporter / browse / Source / PLCrashLogWriter.m?at = refs%2Ftags%2F1.0#694
获得线程列表后,可以通过 thread_get_state(): https://opensource.plausible.coop/stash/projects/PLCR/repos/plcrashreporter/browse/ Source / PLCrashFrameWalker_arm.c?at = refs%2Ftags%2F1.0#73
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
使用线程状态,您可以遍历目标的堆栈:
https://opensource.plausible.coop/stash/projects/PLCR/repos/plcrashreporter/browse/Source/PLCrashFrameWalker_arm.c?at=refs%2Ftags%2F1.0#104
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
请注意,所有这些代码都有点不寻常,因为它在信号处理程序中执行并被写为异步 - 安全;如果您不熟悉信号处理的复杂性,这是一个很好的起点: http://www.mikeash.com/pyblog/friday-qa-2011-04-01-signal-handling.html
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
正确实施这类事情是一个巨大的麻烦;我真的建议您使用PLCrashReporter或其上构建的产品之一( QuincyKit , HockeyApp , Atlassian JMC 等。
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).
这篇关于如何在iPad应用程序中遍历所有活动线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!