2016-12-24 01:03:10 +00:00
|
|
|
#if !defined(__APPLE__) && !defined(__FreeBSD__)
|
2015-10-05 01:11:12 +00:00
|
|
|
#include <malloc.h>
|
2016-10-26 22:27:38 +00:00
|
|
|
#endif
|
2015-10-05 01:11:12 +00:00
|
|
|
#include <execinfo.h>
|
|
|
|
#include <cxxabi.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include <DB/Common/StackTrace.h>
|
|
|
|
|
|
|
|
|
|
|
|
StackTrace::StackTrace()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
frames_size = backtrace(frames, STACK_TRACE_MAX_DEPTH);
|
2015-10-05 01:11:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string StackTrace::toString() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
char ** symbols = backtrace_symbols(frames, frames_size);
|
|
|
|
std::stringstream res;
|
2015-10-05 01:11:12 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!symbols)
|
|
|
|
return "Cannot get symbols for stack trace.\n";
|
2015-10-05 01:11:12 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
for (size_t i = 0, size = frames_size; i < size; ++i)
|
|
|
|
{
|
|
|
|
/// We do "demangling" of names. The name is in parenthesis, before the '+' character.
|
2015-12-23 07:16:44 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
char * name_start = nullptr;
|
|
|
|
char * name_end = nullptr;
|
|
|
|
char * demangled_name = nullptr;
|
|
|
|
int status = 0;
|
2015-12-23 07:16:44 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (nullptr != (name_start = strchr(symbols[i], '('))
|
|
|
|
&& nullptr != (name_end = strchr(name_start, '+')))
|
|
|
|
{
|
|
|
|
++name_start;
|
|
|
|
*name_end = '\0';
|
|
|
|
demangled_name = abi::__cxa_demangle(name_start, 0, 0, &status);
|
|
|
|
*name_end = '+';
|
|
|
|
}
|
2015-10-05 01:11:12 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
res << i << ". ";
|
2015-12-23 07:16:44 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (nullptr != demangled_name && 0 == status)
|
|
|
|
{
|
|
|
|
res.write(symbols[i], name_start - symbols[i]);
|
|
|
|
res << demangled_name << name_end;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
res << symbols[i];
|
2015-10-05 01:11:12 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
res << std::endl;
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
free(demangled_name);
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
free(demangled_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
free(symbols);
|
|
|
|
throw;
|
|
|
|
}
|
2015-12-23 07:16:44 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
free(symbols);
|
|
|
|
return res.str();
|
2015-10-05 01:11:12 +00:00
|
|
|
}
|