add backtrace generator utility to platform API

This commit is contained in:
Eduardo Bart 2011-10-26 20:11:46 -02:00
parent fb6f07bfc4
commit c08bbf828b
2 changed files with 31 additions and 1 deletions

View File

@ -81,6 +81,7 @@ public:
/// Get the app user directory, the place to save files configurations files /// Get the app user directory, the place to save files configurations files
std::string getAppUserDir(); std::string getAppUserDir();
std::string generateBacktrace(int maxLevel);
private: private:
PlatformListener* m_listener; PlatformListener* m_listener;

View File

@ -33,6 +33,7 @@
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <X11/Xatom.h> #include <X11/Xatom.h>
#include <execinfo.h>
struct X11PlatformPrivate { struct X11PlatformPrivate {
Display *display; Display *display;
@ -866,3 +867,31 @@ std::string Platform::getAppUserDir()
logError("Couldn't create directory for saving configuration file. (",sdir.str(),")"); logError("Couldn't create directory for saving configuration file. (",sdir.str(),")");
return sdir.str(); return sdir.str();
} }
std::string Platform::generateBacktrace(int maxLevel = 100)
{
std::stringstream ss;
std::vector<void*> buffer(maxLevel);
int numLevels = backtrace(&buffer[0], maxLevel);
char **tracebackBuffer = backtrace_symbols(&buffer[0], numLevels);
if(tracebackBuffer) {
for(int i = 1; i < numLevels; i++) {
std::string line = tracebackBuffer[i];
if(line.find("__libc_start_main") != std::string::npos)
break;
std::size_t demanglePos = line.find("(_Z");
if(demanglePos != std::string::npos) {
demanglePos++;
int len = std::min(line.find_first_of("+", demanglePos), line.find_first_of(")", demanglePos)) - demanglePos;
std::string funcName = line.substr(demanglePos, len);
line.replace(demanglePos, len, Fw::demangleName(funcName.c_str()));
}
if(i > 1)
ss << "\n";
ss << line;
}
free(tracebackBuffer);
}
return ss.str();
}