Creating a native stack trace in Linux

Creating a native stack trace in Linux

book

Article ID: KB0085294

calendar_today

Updated On:

Products Versions
TIBCO DataSynapse GridServer -
Not Applicable -

Description

Resolution:
Sometimes when you are troubleshooting native C/C++ code on linux, you want to generate a stack trace, for example when a SIGSEGV is thrown.

When running under Java 1.5 and higher, you can use the native stack trace in the hotspot error file (hs_err_*).

But on Java 1.4, the JVM on the Engine already traps SIGSEGV and prints out a Java (not native) stack trace, so you need to override the actions of the JVM and install your own SIGSEGV handler for debugging. The backtrace_fd() and backtrace_symbols_fd() methods from glibc can be used for this purpose.

To install your own SIGSEGV handler for debugging, add code to your service wrapper initialization method similar to this:

#include &ltexecinfo.h>
#include &ltstdio.h>
#include &ltsignal.h>

#define TRACE_DEPTH 50

void segv_handler(int signum) { void *trace[TRACE_DEPTH]; int depth; FILE *fp; depth = backtrace(trace, TRACE_DEPTH); fp = fopen("trace.log", "w"); backtrace_symbols_fd(trace, depth, fileno(fp)); fclose(fp); abort(); }

void MyServiceWrapper::init() { signal(SIGSEGV, segv_handler); signal(SIGBUS, segv_handler); ...rest of wrapper initialization… }

Issue/Introduction

Creating a native stack trace in Linux