Stack trace in FogLAMPProcess
#include <logger.h>
#include <process.h>
#include <service_record.h>
+#include <signal.h>
+#include <execinfo.h>
+#include <cxxabi.h>
#define LOG_SERVICE_NAME "FogLAMP Process"
using namespace std;
+/**
+ * Signal handker to log stack trqaces on fatal signals
+ */
+static void handler(int sig)
+{
+Logger *logger = Logger::getLogger();
+void *array[20];
+char buf[1024];
+int size;
+
+ // get void*'s for all entries on the stack
+ size = backtrace(array, 20);
+
+ // print out all the frames to stderr
+ logger->fatal("Signal %d (%s) trapped:\n", sig, strsignal(sig));
+ char **messages = backtrace_symbols(array, size);
+ for (int i = 0; i < size; i++)
+ {
+ Dl_info info;
+ if (dladdr(array[i], &info) && info.dli_sname)
+ {
+ char *demangled = NULL;
+ int status = -1;
+ if (info.dli_sname[0] == '_')
+ demangled = abi::__cxa_demangle(info.dli_sname, NULL, 0, &status);
+ snprintf(buf, sizeof(buf), "%-3d %*p %s + %zd---------",
+ i, int(2 + sizeof(void*) * 2), array[i],
+ status == 0 ? demangled :
+ info.dli_sname == 0 ? messages[i] : info.dli_sname,
+ (char *)array[i] - (char *)info.dli_saddr);
+ free(demangled);
+ }
+ else
+ {
+ snprintf(buf, sizeof(buf), "%-3d %*p %s---------",
+ i, int(2 + sizeof(void*) * 2), array[i], messages[i]);
+ }
+ logger->fatal("(%d) %s", i, buf);
+ }
+ free(messages);
+ exit(1);
+}
+
// Destructor
FogLampProcess::~FogLampProcess()
{
@@ -34,6 +80,12 @@ FogLampProcess::FogLampProcess(int argc, char** argv) :
m_argc(argc),
m_arg_vals((const char**) argv)
{
+ signal(SIGSEGV, handler);
+ signal(SIGILL, handler);
+ signal(SIGBUS, handler);
+ signal(SIGFPE, handler);
+ signal(SIGABRT, handler);
+
string myName = LOG_SERVICE_NAME;
m_logger = new Logger(myName);
