diff --git a/src/DebugMgr.cpp b/src/DebugMgr.cpp index 65f2382..2423d35 100644 --- a/src/DebugMgr.cpp +++ b/src/DebugMgr.cpp @@ -53,6 +53,7 @@ void DebugMgr::registerSignalHandlers() { #ifndef _WIN32 signal(SIGSEGV, &DebugMgr::signal_handler); signal(SIGBUS, &DebugMgr::signal_handler); + signal(SIGILL, &DebugMgr::signal_handler); #endif } } @@ -103,6 +104,10 @@ void DebugMgr::fatal(IDebug *dbg, const char *fmt, va_list ap) { throw std::runtime_error(""); } +void DebugMgr::crashClose() { + +} + void DebugMgr::flush() { m_out->flush(); } @@ -113,7 +118,7 @@ void DebugMgr::signal_handler(int sigid) { void DebugMgr::crash_handler() { m_dbg->error("Application Crashed"); - flush(); + m_out->crashClose(); _exit(1); } diff --git a/src/DebugMgr.h b/src/DebugMgr.h index 1bc7c70..74eefdd 100644 --- a/src/DebugMgr.h +++ b/src/DebugMgr.h @@ -57,6 +57,8 @@ class DebugMgr : public IDebugMgr { virtual void fatal(IDebug *dbg, const char *fmt, va_list ap) override; + virtual void crashClose() override; + virtual void flush() override; private: diff --git a/src/DebugOutFile.cpp b/src/DebugOutFile.cpp index b700e1b..caab5c0 100644 --- a/src/DebugOutFile.cpp +++ b/src/DebugOutFile.cpp @@ -18,12 +18,24 @@ * Created on: * Author: */ +#include +#include +#include #include "dmgr/IDebug.h" #include "DebugOutFile.h" +#ifndef _WIN32 +#include +#include +#endif namespace dmgr { +DebugOutFile::DebugOutFile(const std::string &name) : + m_path(name), m_fp(0), m_close_fp(true) { + + m_fp = fopen(name.c_str(), "w"); +} DebugOutFile::DebugOutFile(FILE *fp, bool close_fp) : m_fp(fp), m_close_fp(close_fp), m_flush(false) { @@ -78,6 +90,27 @@ void DebugOutFile::fatal(IDebug *dbg, const char *fmt, va_list ap) { fflush(m_fp); } +void DebugOutFile::crashClose() { + fflush(m_fp); +#ifndef _WIN32 + if (m_path.size()) { + static void *bt_buffer[64]; + char *rename_path = (char *)alloca(m_path.size()+64); + int32_t bt_size; + + bt_size = backtrace(bt_buffer, sizeof(bt_buffer)/sizeof(void *)); + + backtrace_symbols_fd(bt_buffer, bt_size, fileno(m_fp)); + + // Rename the file + fclose(m_fp); + + sprintf(rename_path, "%s.%d", m_path.c_str(), getpid()); + ::rename(m_path.c_str(), rename_path); + } +#endif +} + void DebugOutFile::flush() { fflush(m_fp); } diff --git a/src/DebugOutFile.h b/src/DebugOutFile.h index 5bafc1d..69e95fd 100644 --- a/src/DebugOutFile.h +++ b/src/DebugOutFile.h @@ -28,6 +28,8 @@ namespace dmgr { class DebugOutFile : public virtual IDebugOut { public: + DebugOutFile(const std::string &name); + DebugOutFile(FILE *fp, bool close_fp); virtual ~DebugOutFile(); @@ -37,9 +39,13 @@ class DebugOutFile : public virtual IDebugOut { virtual void debug(IDebug *dbg, const char *fmt, va_list ap) override; virtual void error(IDebug *dbg, const char *fmt, va_list ap) override; virtual void fatal(IDebug *dbg, const char *fmt, va_list ap) override; + + virtual void crashClose() override; + virtual void flush() override; private: + std::string m_path; FILE *m_fp; bool m_close_fp; bool m_flush; diff --git a/src/DebugOutList.cpp b/src/DebugOutList.cpp index fb990b6..a746a49 100644 --- a/src/DebugOutList.cpp +++ b/src/DebugOutList.cpp @@ -72,6 +72,14 @@ void DebugOutList::fatal(IDebug *dbg, const char *fmt, va_list ap) { } } +void DebugOutList::crashClose() { + for (std::vector::const_iterator + it=m_outputs.begin(); + it!=m_outputs.end(); it++) { + (*it)->crashClose(); + } +} + void DebugOutList::flush() { for (std::vector::const_iterator it=m_outputs.begin(); diff --git a/src/DebugOutList.h b/src/DebugOutList.h index 33c2316..089c67e 100644 --- a/src/DebugOutList.h +++ b/src/DebugOutList.h @@ -42,6 +42,9 @@ class DebugOutList : public virtual IDebugOutList { virtual void debug(IDebug *dbg, const char *fmt, va_list ap) override; virtual void error(IDebug *dbg, const char *fmt, va_list ap) override; virtual void fatal(IDebug *dbg, const char *fmt, va_list ap) override; + + virtual void crashClose() override; + virtual void flush() override; private: diff --git a/src/Factory.cpp b/src/Factory.cpp index af26d8a..7b8be86 100644 --- a/src/Factory.cpp +++ b/src/Factory.cpp @@ -44,6 +44,10 @@ IDebugOut *Factory::mkDebugOutFile(FILE *fp, bool close_fp) { return new DebugOutFile(fp, close_fp); } +IDebugOut *Factory::mkDebugOutPath(const std::string &name) { + return new DebugOutFile(name); +} + IDebugOutList *Factory::mkDebugOutList() { return new DebugOutList(); } diff --git a/src/Factory.h b/src/Factory.h index 6ced3da..efc0d6b 100644 --- a/src/Factory.h +++ b/src/Factory.h @@ -37,6 +37,8 @@ class Factory : public virtual IFactory { virtual IDebugOut *mkDebugOutFile(FILE *fp, bool close_fp) override; + virtual IDebugOut *mkDebugOutPath(const std::string &name) override; + virtual IDebugOutList *mkDebugOutList() override; static IFactory *inst(); diff --git a/src/include/dmgr/IDebugOut.h b/src/include/dmgr/IDebugOut.h index 843697a..208cf3a 100644 --- a/src/include/dmgr/IDebugOut.h +++ b/src/include/dmgr/IDebugOut.h @@ -39,6 +39,8 @@ class IDebugOut { virtual void error(IDebug *dbg, const char *fmt, va_list ap) = 0; virtual void fatal(IDebug *dbg, const char *fmt, va_list ap) = 0; + virtual void crashClose() = 0; + virtual void flush() = 0; }; diff --git a/src/include/dmgr/IFactory.h b/src/include/dmgr/IFactory.h index 20f72a9..deb96a6 100644 --- a/src/include/dmgr/IFactory.h +++ b/src/include/dmgr/IFactory.h @@ -36,6 +36,8 @@ class IFactory { virtual IDebugOut *mkDebugOutFile(FILE *fp, bool close_fp) = 0; + virtual IDebugOut *mkDebugOutPath(const std::string &name) = 0; + virtual IDebugOutList *mkDebugOutList() = 0; };