如何在Android上打印的堆栈跟踪(用符号函数名)?堆栈、函数、符号、如何在

2023-09-06 18:19:05 作者:一曲琵琶倾城梦

这个问题已经被问了很多次,但从未真正得到回答。我花了5到6个小时今天试图港口一些库到Android(libunwind,liwdfl),这可能从来没有打算工作在ARM。要无果的,当然。

This question has been asked a number of times, but it has never really been answered. I've spent 5 to 6 hours today trying to port some libraries to Android (libunwind, liwdfl) that probably have never been intended to work on ARM. To no avail, of course.

问题是,Android的缺乏回溯 / backtrace_symbols 功能。有没有人找到了一种方法来打印堆栈跟踪,比如说,在收到SIGSEGV?

The problem is that Android lacks backtrace / backtrace_symbols functions. Has anyone found a way to print stacktrace, say, upon receiving SIGSEGV?

推荐答案

的方式这是内部完成与的调用堆栈类。例如:

The way this is done internally is with the CallStack class. For example:

CallStack cs("logtag");

将转储当前本地调用堆栈的日志。不幸的是,这是不是在NDK使用,并且该类不从的版本改为版本。

will dump the current native call stack to the log. Unfortunately this is not available in the NDK, and the class does change from version to version.

在Android的KitKit(4.4),该实施使用的内部 libcorkscrew 库。在当前的master分支,在实施使用 libbacktrace ,也是一个内部库。然而,这种新的图书馆是建立在 libunwind ,一个的开源项目。

In Android KitKit (4.4), the implementation used the internal libcorkscrew library. In the current "master" branch, the implementation uses libbacktrace, also an internal library. However, this new library is built on libunwind, an open-source project.

所以,你应该能够建立libunwind到您的应用程序,并使用这种方式。由于该库已经在Android上使用,移植应该是远远低于一个问题(刚开始与AOSP的外部/ libunwind 源)。

So you should be able to build libunwind into your app and use it that way. Since the libraries are already in use on Android, porting should be far less of a problem (just start with the AOSP external/libunwind sources).