Skip to content

Commit

Permalink
Fix symbol demangling on stacktraces under Linux. (apache#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
larroy authored and piiswrong committed Jan 15, 2018
1 parent c087182 commit 6389c10
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions include/dmlc/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <string>
#include <vector>
#include <stdexcept>
#include <memory>
#include "./base.h"

#if DMLC_LOG_STACK_TRACE
Expand Down Expand Up @@ -264,17 +265,17 @@ inline std::string Demangle(char const *msg_str) {
size_t symbol_start = string::npos;
size_t symbol_end = string::npos;
if ( ((symbol_start = msg.find("_Z")) != string::npos)
&& (symbol_end = msg.find_first_of(" ", symbol_start)) ) {
&& (symbol_end = msg.find_first_of(" +", symbol_start)) ) {
string left_of_symbol(msg, 0, symbol_start);
string symbol(msg, symbol_start, symbol_end - symbol_start);
string right_of_symbol(msg, symbol_end);

int status = 0;
size_t length = string::npos;
char* demangled_symbol = abi::__cxa_demangle(symbol.c_str(), 0, &length, &status);
std::unique_ptr<char, decltype(&std::free)> demangled_symbol =
{abi::__cxa_demangle(symbol.c_str(), 0, &length, &status), &std::free};
if (demangled_symbol && status == 0 && length > 0) {
string symbol_str(demangled_symbol, length - 1);
free(demangled_symbol);
string symbol_str(demangled_symbol.get());
std::ostringstream os;
os << left_of_symbol << symbol_str << right_of_symbol;
return os.str();
Expand Down

0 comments on commit 6389c10

Please sign in to comment.