Skip to content

Commit

Permalink
src: print backtrace on fatal error
Browse files Browse the repository at this point in the history
Print a C backtrace on fatal errors to make it easier to debug issues
like #6727.

PR-URL: #6734
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
  • Loading branch information
bnoordhuis authored and Fishrock123 committed Jul 5, 2016
1 parent 6de80fc commit 6cec90a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@

[ 'OS=="win"', {
'sources': [
'src/backtrace_win32.cc',
'src/res/node.rc',
],
'defines!': [
Expand All @@ -484,6 +485,7 @@
'libraries': [ '-lpsapi.lib' ]
}, { # POSIX
'defines': [ '__POSIX__' ],
'sources': [ 'src/backtrace_posix.cc' ],
}],
[ 'OS=="mac"', {
# linking Corefoundation is needed since certain OSX debugging tools
Expand Down
50 changes: 50 additions & 0 deletions src/backtrace_posix.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "node.h"

#if defined(__linux__)
#include <features.h>
#endif

#if defined(__linux__) && !defined(__GLIBC__)
#define HAVE_EXECINFO_H 0
#else
#define HAVE_EXECINFO_H 1
#endif

#if HAVE_EXECINFO_H
#include <cxxabi.h>
#include <dlfcn.h>
#include <execinfo.h>
#include <stdio.h>
#endif

namespace node {

void DumpBacktrace(FILE* fp) {
#if HAVE_EXECINFO_H
void* frames[256];
const int size = backtrace(frames, arraysize(frames));
if (size <= 0) {
return;
}
for (int i = 1; i < size; i += 1) {
void* frame = frames[i];
fprintf(fp, "%2d: ", i);
Dl_info info;
const bool have_info = dladdr(frame, &info);
if (!have_info || info.dli_sname == nullptr) {
fprintf(fp, "%p", frame);
} else if (char* demangled = abi::__cxa_demangle(info.dli_sname, 0, 0, 0)) {
fprintf(fp, "%s", demangled);
free(demangled);
} else {
fprintf(fp, "%s", info.dli_sname);
}
if (have_info && info.dli_fname != nullptr) {
fprintf(fp, " [%s]", info.dli_fname);
}
fprintf(fp, "\n");
}
#endif // HAVE_EXECINFO_H
}

} // namespace node
8 changes: 8 additions & 0 deletions src/backtrace_win32.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "node.h"

namespace node {

void DumpBacktrace(FILE* fp) {
}

} // namespace node
1 change: 1 addition & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2452,6 +2452,7 @@ static void OnFatalError(const char* location, const char* message) {
} else {
PrintErrorString("FATAL ERROR: %s\n", message);
}
DumpBacktrace(stderr);
fflush(stderr);
ABORT();
}
Expand Down
3 changes: 3 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <assert.h>
#include <signal.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

#ifdef __APPLE__
Expand All @@ -18,6 +19,8 @@

namespace node {

void DumpBacktrace(FILE* fp);

#ifdef __APPLE__
template <typename T> using remove_reference = std::tr1::remove_reference<T>;
#else
Expand Down

0 comments on commit 6cec90a

Please sign in to comment.