Native code - how to get function call stack (backtrace) programmatically
Asked Answered
H

2

23

I have C++ codebase running on Android, and want to have crash reports sent by users.

I'm using ACRA library which works fine for Java code, but when something crashes in native code, I don't get enough information. Actually I'd like to receive stack trace of native function calls. I know crash info is printed into logcat after my process ends, and I can configure ACRA to read/send logcat. I've setup my code to detect native crash using signal handlers and calling back to Java for reporting by ACRA. It works also fine.

However there's bad timing with this approach - ACRA reads logs while crashing process is still alive, and Android (don't know exactly which part) writes crash report to logcat after crashed process completely ends. So I don't receive stack traces when using ACRA.

So I'm looking for a way to programatically read current stack trace from C++ code, and feed this info to ACRA (or maybe other crash reporting tool) myself.

All I need is some kind of this report written to logcat:

10-10 08:29:13.868: INFO/DEBUG(1121):          #00  pc 0003fc7c  /data/data/com.ex.lib/libapp.so
10-10 08:29:13.891: INFO/DEBUG(1121):          #04  pc 00016df4  /system/lib/libdvm.so
10-10 08:29:13.891: INFO/DEBUG(1121):          #05  pc 00045284  /system/lib/libdvm.so
10-10 08:29:13.899: INFO/DEBUG(1121):          #15  pc 00047c56  /system/lib/libdvm.so
10-10 08:29:13.922: INFO/DEBUG(1121):          #16  pc 00030e4c  /system/lib/libandroid_runtime.so

Is there any way to get this stack trace from my code?

Heaviness answered 10/10, 2011 at 8:24 Comment(0)
L
8

I have done this in my game base project - you can see the JNI code which handles this here:

https://bitbucket.org/xg/android-game-base/src/c0d969d44a55/jni/NativeActivityJNI.cpp#cl-40

which calls the Java method defined here:

https://bitbucket.org/xg/android-game-base/src/c0d969d44a55/src/com/gmail/whittock/tom/Util/NativeActivity.java#cl-91

The overall solution is based on handling signals, then in the signal handler firing a call up to java to dump the stack trace etc, in my code I start another activity to get the logcat information and email it to me.

Liqueur answered 24/11, 2011 at 14:15 Comment(1)
Isn't it a bit scary to call java method inside the signal handler?Booby
A
1

ACRA can trap the application crashing. You could then instantiate a second process that would execute the logcat ( see this question ) command, filtering by your application name, and then have the process to send the dumped file to you. This is far from optimal because:

  • The Application that would span logcat has to have the WRITE_EXTERNAL_STORAGE and READ_LOGS permissions
  • Probably the user would be annoyed to having to install a new program

But I didn't found another alternative to do this.

Advection answered 24/11, 2011 at 12:29 Comment(2)
It can be a second activity defined within your apk - there's no need for a second app to be installedLiqueur
Yes, but this would require the READ_LOGS, witch could cause some suspicion to usersAdvection

© 2022 - 2024 — McMap. All rights reserved.