Printing a stack trace from another thread

后端 未结 2 645
慢半拍i
慢半拍i 2020-12-14 04:57

I know I can get the stack trace of the current thread using backtrace() or [NSThread callStackSymbols], but how would I get at the stack trace of a DIFFERENT thread (assumi

2条回答
  •  情书的邮戳
    2020-12-14 05:16

    Here is some safer way to get the callstack from another thread: Implementation and some background information. It uses signal handling and spawns a signal handler in the target thread. It has also the advantage that it is more cross-platform than your solution, i.e. it should work anywhere where you have and .

    For the printing, you can use backtrace_symbols as you do in your own suggestion. But you might be interested in an extended version of that as implemented here. It uses libbfd (from binutils; the recent version also mostly works on MacOSX, see here for a small limitation which might not be relevant for you) to read the debugging information and to add line-number and other information (it also falls back to dladdr if all else fails; that is what backtrace_symbols is doing).

提交回复
热议问题