_Unwind_Backtrace stops at __cxxabiv1::__terminate()

Asked by Thomas Willson on 2021-04-22

I'm trying to print out a stack trace when an unhandled exception occurs. I am using a termination handler registered through `std::set_terminate` and `_Unwind_Backtrace` to accomplish this. However, the last function that shows up is `__cxxabiv1::__terminate()`. When using GDB to retrieve a backtrace, the entire call stack (back to `main()`) is shown.

I've tested the same code using GCC on Linux (x86-64) and I get the full stack trace. Here's the code I'm using to test this function:

```
_Unwind_Reason_Code trace_fcn(_Unwind_Context *ctx, void *d) {
 int *depth = (int*) d;
 trace_printf("\t#%d: program counter at %08lx\n", *depth,
   _Unwind_GetIP(ctx));
 (*depth)++;
 return _URC_NO_REASON;
}

void print_backtrace_here() {
 int depth = 0;
 _Unwind_Backtrace(&trace_fcn, &depth);
}

[[noreturn]] void onTerminate() noexcept {
 trace_printf("\n!!!Termination Initiated!!!\n");
 print_backtrace_here();
 std::abort();
}

void test() {
 throw std::exception();
}

int main(int argc, char *argv[]) {
 std::set_terminate(onTerminate);
 test();

 while (1)
  ;
}
```

I'm using GNU Arm Embedded Toolchain 10-2020-q4-major provided binaries on Windows to test targeting a Cortex-M4. Compilation flags are:
```
-mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -std=c++17 -fexceptions -g -Og -mtpcs-frame -mtpcs-leaf-frame -fno-omit-frame-pointer -funwind-tables -MMD -MP -r -c -Wall
```

Linking flags:
```
-mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fexceptions -mtpcs-frame -mtpcs-leaf-frame -fno-omit-frame-pointer -funwind-tables -g -lsupc++ -lgcc -lnosys
```

Please let me know if there's any details I'm missing here that will help reproduce the problem!

Question information

Language:
English Edit question
Status:
Expired
For:
GNU Arm Embedded Toolchain Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Thomas Willson (willson556) said : #1

It seems this may be a general problem with GCC running on ARM as I've now reproduced it with GCC 8.3 on aarch64 on Debian.

Launchpad Janitor (janitor) said : #2

This question was expired because it remained in the 'Open' state without activity for the last 15 days.