Stack backtrace from signal handler?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Stack backtrace from signal handler?

John Dallman
Preface: I am not producing an Android app. I work for a software component business, creating shared libraries, compiled from C and C++ code, for use in third-party customers' apps. I test my libraries in a command-line test harness, which I run in the ADB shell. I am only producing software for 64-bit ARM, because none of the customers want 32-bit code. 

I would like to be able to print the call stack from my test harness' signal handler. This is not vital - I can get them easily enough using GDB - but if I can get stack tracebacks into my test harness' log file (this goes nowhere near logcat) it makes it easier to tell if multiple problems have related causes. 

On Linux, macOS and iOS, I can use backtrace() and backtrace_symbol() to do this. I can't find anything quite so straightforward on Android, but I may be missing something. 

The closest thing I've found is an example at https://github.com/alexeikh/android-ndk-backtrace-test, but my Linux NDK 21b doesn't seem to have a libunwind.a for 64-bit ARM, only for 32-bit. That's at../android-ndk-r21b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/arm-linux-androideabi/libunwind.a. 

I have a ../android-ndk-r21b/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/9.0.8/include/unwind.h, but no __unwind_config.h. 

Help?

Thanks, 

John Dallman

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CAH1xqg%3DfazpEqrGirObhBuGi0wvu9RgjW1c4fWW%3DF1%2BRy-9xtg%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Stack backtrace from signal handler?

Dan Albert
I don't think we have an async safe unwinder currently. +cferris +rprichard

On Mon, Jun 8, 2020, 09:24 John Dallman <[hidden email]> wrote:
Preface: I am not producing an Android app. I work for a software component business, creating shared libraries, compiled from C and C++ code, for use in third-party customers' apps. I test my libraries in a command-line test harness, which I run in the ADB shell. I am only producing software for 64-bit ARM, because none of the customers want 32-bit code. 

I would like to be able to print the call stack from my test harness' signal handler. This is not vital - I can get them easily enough using GDB - but if I can get stack tracebacks into my test harness' log file (this goes nowhere near logcat) it makes it easier to tell if multiple problems have related causes. 

On Linux, macOS and iOS, I can use backtrace() and backtrace_symbol() to do this. I can't find anything quite so straightforward on Android, but I may be missing something. 

The closest thing I've found is an example at https://github.com/alexeikh/android-ndk-backtrace-test, but my Linux NDK 21b doesn't seem to have a libunwind.a for 64-bit ARM, only for 32-bit. That's at../android-ndk-r21b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/arm-linux-androideabi/libunwind.a. 

I have a ../android-ndk-r21b/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/9.0.8/include/unwind.h, but no __unwind_config.h. 

Help?

Thanks, 

John Dallman

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CAH1xqg%3DfazpEqrGirObhBuGi0wvu9RgjW1c4fWW%3DF1%2BRy-9xtg%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CAFVaGhtsjxX4GqSfQJQvGOQZ5u4%2B3ZtnFKyjTDpymDJqO-LRmg%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Stack backtrace from signal handler?

Christopher Ferris
As Dan mentioned, we don't have an async safe unwinder. Even libunwind, which claims to be async safe, isn't. I'm not sure if the llvm unwinder is async safe since I haven't worked as close with that one.

Christopher

On Mon, Jun 8, 2020 at 11:27 AM Dan Albert <[hidden email]> wrote:
I don't think we have an async safe unwinder currently. +cferris +rprichard

On Mon, Jun 8, 2020, 09:24 John Dallman <[hidden email]> wrote:
Preface: I am not producing an Android app. I work for a software component business, creating shared libraries, compiled from C and C++ code, for use in third-party customers' apps. I test my libraries in a command-line test harness, which I run in the ADB shell. I am only producing software for 64-bit ARM, because none of the customers want 32-bit code. 

I would like to be able to print the call stack from my test harness' signal handler. This is not vital - I can get them easily enough using GDB - but if I can get stack tracebacks into my test harness' log file (this goes nowhere near logcat) it makes it easier to tell if multiple problems have related causes. 

On Linux, macOS and iOS, I can use backtrace() and backtrace_symbol() to do this. I can't find anything quite so straightforward on Android, but I may be missing something. 

The closest thing I've found is an example at https://github.com/alexeikh/android-ndk-backtrace-test, but my Linux NDK 21b doesn't seem to have a libunwind.a for 64-bit ARM, only for 32-bit. That's at../android-ndk-r21b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/arm-linux-androideabi/libunwind.a. 

I have a ../android-ndk-r21b/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/9.0.8/include/unwind.h, but no __unwind_config.h. 

Help?

Thanks, 

John Dallman

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CAH1xqg%3DfazpEqrGirObhBuGi0wvu9RgjW1c4fWW%3DF1%2BRy-9xtg%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CANtHk4m-0acjUD52y_CaqGo_-OtCqsayzoQwbiDCY42rV5Lt2Q%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Stack backtrace from signal handler?

Ryan Prichard
LLVM's libunwind uses malloc/free for:
 - DW_CFA_remember_state / DW_CFA_restore_state CFA instructions
 - the DwarfFDECache

This heap usage can be turned off using _LIBUNWIND_NO_HEAP, but then I think DW_CFA_remember_state / DW_CFA_restore_state become unrecognized CFA opcodes.

It looks like libgcc uses alloca to allocate a pool of frame-state records for DW_CFA_remember_state / DW_CFA_restore_state.

The unwinder uses dl_iterate_phdr or dl_unwind_find_exidx to locate the unwind information for a function, and AFAIK those APIs aren't async-safe, at least with current versions of Bionic. The APIs acquire a recursive lock, so:
 - the linker data structures could be inconsistent during a signal handler
 - a thread could acquire the lock, then disappear after fork

-Ryan


On Mon, Jun 8, 2020 at 11:37 AM Christopher Ferris <[hidden email]> wrote:
As Dan mentioned, we don't have an async safe unwinder. Even libunwind, which claims to be async safe, isn't. I'm not sure if the llvm unwinder is async safe since I haven't worked as close with that one.

Christopher

On Mon, Jun 8, 2020 at 11:27 AM Dan Albert <[hidden email]> wrote:
I don't think we have an async safe unwinder currently. +cferris +rprichard

On Mon, Jun 8, 2020, 09:24 John Dallman <[hidden email]> wrote:
Preface: I am not producing an Android app. I work for a software component business, creating shared libraries, compiled from C and C++ code, for use in third-party customers' apps. I test my libraries in a command-line test harness, which I run in the ADB shell. I am only producing software for 64-bit ARM, because none of the customers want 32-bit code. 

I would like to be able to print the call stack from my test harness' signal handler. This is not vital - I can get them easily enough using GDB - but if I can get stack tracebacks into my test harness' log file (this goes nowhere near logcat) it makes it easier to tell if multiple problems have related causes. 

On Linux, macOS and iOS, I can use backtrace() and backtrace_symbol() to do this. I can't find anything quite so straightforward on Android, but I may be missing something. 

The closest thing I've found is an example at https://github.com/alexeikh/android-ndk-backtrace-test, but my Linux NDK 21b doesn't seem to have a libunwind.a for 64-bit ARM, only for 32-bit. That's at../android-ndk-r21b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/arm-linux-androideabi/libunwind.a. 

I have a ../android-ndk-r21b/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/9.0.8/include/unwind.h, but no __unwind_config.h. 

Help?

Thanks, 

John Dallman

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CAH1xqg%3DfazpEqrGirObhBuGi0wvu9RgjW1c4fWW%3DF1%2BRy-9xtg%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CALgsJz%3D3s4DxXy2vzM0r_oh3iOUC54Z1dF8%3DmD68_bcbq_8cfg%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Stack backtrace from signal handler?

John Dallman
Thanks, guys. An equivalent to the glibc facilities would be a welcome addition, but I can;t claim it is crucial. 

John Dallman

On Tue, Jun 9, 2020 at 11:59 PM 'Ryan Prichard' via android-ndk <[hidden email]> wrote:
LLVM's libunwind uses malloc/free for:
 - DW_CFA_remember_state / DW_CFA_restore_state CFA instructions
 - the DwarfFDECache

This heap usage can be turned off using _LIBUNWIND_NO_HEAP, but then I think DW_CFA_remember_state / DW_CFA_restore_state become unrecognized CFA opcodes.

It looks like libgcc uses alloca to allocate a pool of frame-state records for DW_CFA_remember_state / DW_CFA_restore_state.

The unwinder uses dl_iterate_phdr or dl_unwind_find_exidx to locate the unwind information for a function, and AFAIK those APIs aren't async-safe, at least with current versions of Bionic. The APIs acquire a recursive lock, so:
 - the linker data structures could be inconsistent during a signal handler
 - a thread could acquire the lock, then disappear after fork

-Ryan


On Mon, Jun 8, 2020 at 11:37 AM Christopher Ferris <[hidden email]> wrote:
As Dan mentioned, we don't have an async safe unwinder. Even libunwind, which claims to be async safe, isn't. I'm not sure if the llvm unwinder is async safe since I haven't worked as close with that one.

Christopher

On Mon, Jun 8, 2020 at 11:27 AM Dan Albert <[hidden email]> wrote:
I don't think we have an async safe unwinder currently. +cferris +rprichard

On Mon, Jun 8, 2020, 09:24 John Dallman <[hidden email]> wrote:
Preface: I am not producing an Android app. I work for a software component business, creating shared libraries, compiled from C and C++ code, for use in third-party customers' apps. I test my libraries in a command-line test harness, which I run in the ADB shell. I am only producing software for 64-bit ARM, because none of the customers want 32-bit code. 

I would like to be able to print the call stack from my test harness' signal handler. This is not vital - I can get them easily enough using GDB - but if I can get stack tracebacks into my test harness' log file (this goes nowhere near logcat) it makes it easier to tell if multiple problems have related causes. 

On Linux, macOS and iOS, I can use backtrace() and backtrace_symbol() to do this. I can't find anything quite so straightforward on Android, but I may be missing something. 

The closest thing I've found is an example at https://github.com/alexeikh/android-ndk-backtrace-test, but my Linux NDK 21b doesn't seem to have a libunwind.a for 64-bit ARM, only for 32-bit. That's at../android-ndk-r21b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/arm-linux-androideabi/libunwind.a. 

I have a ../android-ndk-r21b/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/9.0.8/include/unwind.h, but no __unwind_config.h. 

Help?

Thanks, 

John Dallman

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CAH1xqg%3DfazpEqrGirObhBuGi0wvu9RgjW1c4fWW%3DF1%2BRy-9xtg%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CALgsJz%3D3s4DxXy2vzM0r_oh3iOUC54Z1dF8%3DmD68_bcbq_8cfg%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CAH1xqgnio3x65XoynzwxSw_huUt5D3UtyRtwsVU18_6YnkerSw%40mail.gmail.com.