Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed invalid backtrace #247

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/lib/graft/backtrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#include <signal.h>

#ifdef __cplusplus

#include <sstream>
std::string graft_bt_str();

extern "C" {
#endif
void graft_bt();
Expand Down
66 changes: 66 additions & 0 deletions src/lib/graft/backtrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,72 @@ typedef struct {
sigset_t uc_sigmask;
} sig_ucontext_t;

#ifdef __cplusplus

std::string graft_bt_str()
{
void *trace[TRACE_SIZE_MAX];
char **messages = (char **) NULL;

char *funcname = (char*) malloc(FUNC_NAME_SIZE_MAX);
int i, trace_size = backtrace(trace, TRACE_SIZE_MAX);

if (trace_size == 0)
{
return "\t<stack trace is possibly corrupt>\n";
}

std::ostringstream oss;

messages = backtrace_symbols(trace, trace_size);
for (i = 1; i < trace_size && messages; ++i)
{
char *begin_name = NULL, *begin_offset = NULL, *end_offset = NULL, *p;
for (p = messages[i]; *p; ++p)
{
if (*p == '(')
begin_name = p;
else if (*p == '+')
begin_offset = p;
else if (*p == ')' && begin_offset) {
end_offset = p;
break;
}
}

if (begin_name && begin_offset && end_offset && begin_name < begin_offset)
{
int status;
size_t funcnamesize = FUNC_NAME_SIZE_MAX;

*begin_name++ = '\0';
*begin_offset++ = '\0';
*end_offset = '\0';

p = abi::__cxa_demangle(begin_name, funcname, &funcnamesize, &status);
if (status == 0)
{
funcname = p;
oss << "\t#" << i << " " << messages[i] << " : " << funcname << "+" << begin_offset << "\n";
}
else
{
oss << "\t#" << i << " " << messages[i] << " : " << begin_name << "()+" << begin_offset << "\n";
}
}
else
{
oss << "\t#" << i << " " << messages[i] << "\n";
}
}
free(messages);
free(funcname);

return oss.str();
}

#endif //__cplusplus

void graft_bt()
{
void *trace[TRACE_SIZE_MAX];
Expand Down
18 changes: 8 additions & 10 deletions src/supernode/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ std::terminate_handler prev_terminate = nullptr;
// the workflow, the error propagates back to the client.
void terminate()
{
std::cerr << "\nTerminate called, dump stack:\n";
graft_bt();
std::ostringstream oss;
oss << "\nTerminate called, dump stack:\n";
oss << graft_bt_str();

//dump exception info
std::exception_ptr eptr = std::current_exception();
Expand All @@ -28,14 +29,17 @@ void terminate()
}
catch(std::exception& ex)
{
std::cerr << "\nTerminate caused by exception : '" << ex.what() << "'\n";
oss << "\nTerminate caused by exception : '" << ex.what() << "'\n";
}
catch(...)
{
std::cerr << "\nTerminate caused by unknown exception.\n";
oss << "\nTerminate caused by unknown exception.\n";
}
}

LOG_ERROR("") << oss.str();
std::cerr << oss.str();

prev_terminate();
}

Expand All @@ -54,12 +58,6 @@ int main(int argc, const char** argv)
} catch (const graft::exit_error& e) {
LOG_ERROR("The program is terminated because of error: ") << e.what();
return -1;
} catch (const std::exception & e) {
LOG_ERROR("Exception thrown: ") << e.what();
throw;
} catch(...) {
LOG_ERROR("Exception of unknown type!");
throw;
}

return 0;
Expand Down