12

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?

bornbnid
  • 383
  • 4
  • 9

2 Answers2

26

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:

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

landonf
  • 641
  • 1
  • 5
  • 9
  • Thx man so much for pointing out; But can you help answer my related questions? https://stackoverflow.com/questions/47071265/how-to-analyze-stack-info-of-a-thread – Paradise Nov 02 '17 at 09:21
  • what a great answer. links to the APIs. Links to example code. Bang on! – rustyMagnet Dec 29 '20 at 15:37
4

This seems fraught with peril, doubly so if you need to ask here for help. May I suggest PLCrashReporter? Its listed features are:

  • Implemented as a in-process fully async-safe signal handler.
  • Does not interfere with debugging in gdb
  • Handles both uncaught Objective-C exceptions and fatal signals (SIGSEGV, SIGBUS, etc)
  • Backtraces for all active threads are provided. (emphasis my own)
  • Provides full register state for the crashed thread.

Better yet, have a look at QuincyKit, a very handy wrapper around PLCrashReporter.

Aidan Steele
  • 10,999
  • 6
  • 38
  • 59
  • 4
    While this is a good third party solution, I'd still rather learn about looping through active threads. Even if you could point me in the right direction as far as documentation goes, I would find that more helpful. – bornbnid Jun 17 '11 at 15:47
  • Thx so much for reference PLC; But can you help answer my related questions? https://stackoverflow.com/questions/47071265/how-to-analyze-stack-info-of-a-thread – Paradise Nov 02 '17 at 09:22