ianlancetaylor / libbacktrace

A C library that may be linked into a C/C++ program to produce symbolic backtraces
Other
971 stars 228 forks source link

MUSL CRuntime used by Alpine Linux #104

Closed jlsantiago0 closed 1 year ago

jlsantiago0 commented 1 year ago

Hi,

Love this library, it is working well for me in most places. I am unable to see past the signal handler trampoline with MUSL runtime used by Alpine Linux. I tested with Alpine v3.9 and v3.16.

cat /etc/os-release
NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.16.3
PRETTY_NAME="Alpine Linux v3.16"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://gitlab.alpinelinux.org/alpine/aports/-/issues"

I have a test program that registers a signal handler for SIGABRT which uses static global pointer that was created before main started running as follows:

backtrace_create_state(NULL, 0, LibBackTraceErrorCB, NULL)

Notice that the stack dump wont get past the signal handler trampoline:

./stage/apps/hvmaf_unit_test/hvmaf_unit_test1

WARN:/mnt/jlsws2/dev/orthrus/master/src/apps/hvmaf_unit_test/hvmaf_unit_test1.cpp:101:StackTraceTest(): 
    Calling ::std::terminate() which should call abort(3).
terminate called without an active exception
HVMAF[484:169685:169685]SignalAbortHandler(): ProgramName: ./stage/apps/hvmaf_unit_test/hvmaf_unit_test1
HVMAF[484:169685:169685]SignalAbortHandler(): Received Signal '6'.
HVMAF[484:169685:169685]SignalAbortHandler(): Crash Dump Needed.
DoCrashDumpActual(): CrashDump
  Program:
    Name: './stage/apps/hvmaf_unit_test/hvmaf_unit_test1'
    Desc: 'HVMAF UnitTest1'
    StartTime: NS:1674070373889417704 2023:01:18:19.32.53(UTC)
    PProcessID: 484
    ProcessID:  169685
    ThreadID:   169685
  Frames:
    000: 0x566D5E97 hvmaf::DebugStackTrace::Internals::DoCrashDump()
      /mnt/jlsws2/dev/orthrus/master/src/hvmaf/common/hvmaf_debug_stacktrace.cpp:3768
    001: 0x566F136B SignalAbortHandler
      /mnt/jlsws2/dev/orthrus/master/src/hvmaf/common/hvmaf_signal.cpp:292
    002: 0xF7F10659
HVMAF[484:169685:169685]SignalAbortHandler(): Aborting Process.
Aborted (core dumped)

BTW: If I do a backtrace in normal operation it works fine. Jus t not in the signal handler.

ianlancetaylor commented 1 year ago

This actually isn't something that I can fix in libbacktrace. The libbacktrace library relies on _Unwind_Backtrace, which is provided by the compiler. That function is responsible for knowing how to unwind through a signal handler.

You didn't mention which compiler you are using, but this is a bug either in the compiler, or in MUSL, or in the interaction between them.

jlsantiago0 commented 1 year ago

Ah OK:

gcc --version
gcc (Alpine 11.2.1_git20220219) 11.2.1 20220219
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
jlsantiago0 commented 1 year ago

OK. Thank you for the quick reply. I appreciate this library quite a bit.

ianlancetaylor commented 1 year ago

When using GCC it unwinds through a signal handler by recognizing the code in the signal handler. For x86 see https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=libgcc/config/i386/linux-unwind.h;h=5486223d60272c73d5103b29ae592d2ee998e1cf;hb=HEAD

jlsantiago0 commented 1 year ago

I opened a ticket with Alpine. Hopefully they will be able to fix this:

https://gitlab.alpinelinux.org/alpine/aports/-/issues/14568