Creating a Native Stack Trace in Linux
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. Since the JVM on the Engine already traps SIGSEGV and prints out a Java (not native) stack trace, you must 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 initialization method similar to this:
#include <execinfo.h>
#include <stdio.h>
#include <signal.h>
#define TRACE_DEPTH 50
void MyService::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 MyService::init() {
signal(SIGSEGV, segv_handler);
signal(SIGBUS, segv_handler);
}