jeremy-rifkin / cpptrace

Simple, portable, and self-contained stacktrace library for C++11 and newer
MIT License
621 stars 64 forks source link

[documentation] Signal-Safe Tracing demo #111

Closed sigidagi closed 4 months ago

sigidagi commented 4 months ago

Hi, I'm using Linux Ubuntu 22.04

Because signal-safe stack unwinding is only possible with libunwind, which must be manually enabled, I've applied such configuration:

cmake -B build -S . -DCMAKE_BUILD_TYPE=Debug -DCPPTRACE_UNWIND_WITH_LIBUNWIND=On -DCPPTRACE_BUILD_TESTING=On -DBUILD_SHARED_LIBS=On

also checked that libunwind-dev is installed on my machine:

sigis:cpptrace$ sudo apt-cache policy libunwind-dev
libunwind-dev:
  Installed: 1.6.2-3
  Candidate: 1.6.2-3
  Version table:
 *** 1.6.2-3 500
        500 http://de.archive.ubuntu.com/ubuntu lunar/main amd64 Packages
        100 /var/lib/dpkg/status
sigis:cpptrace$

I can build library and tests without any issues, but running demo, there is no debug output in the console.

sigis:cpptrace$ ./build/signal_demo
SIGSEGV occurred:
sigis:cpptrace$

What I am missing?

jeremy-rifkin commented 4 months ago

Thanks for opening this, sorry to see you’re running into issues. It’s probably some edge case I didn’t handle in the demo (unfortunately even for demo purposes this is super tricky). I’ll look into this this evening.

sigidagi commented 4 months ago

Hi Jeremy, It seems that I needed pipe to signal_tracer program (After investigating signal_demo source code).

sigis:app-debug$ ./signal_demo | ./signal_tracer
SIGSEGV occurred:
Stack trace (most recent call first):
#0 0x5647635202cd0000 in handler(int, siginfo_t*, void*) at /home/sigis/prg/cpp/cpptrace/test/signal_demo.cpp:42:25
#1 0x7f05c483c45f0000 at /lib/x86_64-linux-gnu/libc.so.6
#2 0x5647635202450000 in trace() at /home/sigis/prg/cpp/cpptrace/test/signal_demo.cpp:16:24
#3 0x5647635202580000 in bar() at /home/sigis/prg/cpp/cpptrace/test/signal_demo.cpp:20:5
#4 0x5647635202680000 in foo() at /home/sigis/prg/cpp/cpptrace/test/signal_demo.cpp:24:5
#5 0x5647635204ac0000 in main at /home/sigis/prg/cpp/cpptrace/test/signal_demo.cpp:98:5
#6 0x7f05c4823a8f0000 in __libc_start_call_main at ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#7 0x7f05c4823b480000 in __libc_start_main_impl at ./csu/../csu/libc-start.c:360:3
#8 0x5647635201740000 at /home/sigis/prg/cpp/cpptrace/build/app-debug/signal_demo
Stack trace (most recent call first):
<empty trace>

Best regards.

jeremy-rifkin commented 4 months ago

Hi, sorry I didn’t look lat night. You shouldn’t have to pipe like that, one of the points of signal_demo is that it handles the forking and piping to the signal_tracer. I’ll look tonight. As a side note I have no idea how that snippet you sent worked since it should never write to stdout.

jeremy-rifkin commented 4 months ago

Ah, I see the problem. It needs to be ./signal_demo not ./build/signal_demo because the program forks + exec's signal_tracer expecting that program to be in the current working directory.

./signal_demo | ./signal_tracer works because it's not ./build/ but also notice the double Stack trace (most recent call first):: The first one, the populated trace, is from the process's own exec'd signal_tracer process while the empty trace is from the | ./signal_tracer which gets no input from stdin because signal_demo doesn't write to stdout.