Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
backtrace_posix.cc
Go to the documentation of this file.
1 #include "node.h"
2 
3 #if defined(__linux__)
4 #include <features.h>
5 #endif
6 
7 #if defined(__linux__) && !defined(__GLIBC__) || \
8  defined(__UCLIBC__) || \
9  defined(_AIX)
10 #define HAVE_EXECINFO_H 0
11 #else
12 #define HAVE_EXECINFO_H 1
13 #endif
14 
15 #if HAVE_EXECINFO_H
16 #include <cxxabi.h>
17 #include <dlfcn.h>
18 #include <execinfo.h>
19 #include <stdio.h>
20 #endif
21 
22 namespace node {
23 
24 void DumpBacktrace(FILE* fp) {
25 #if HAVE_EXECINFO_H
26  void* frames[256];
27  const int size = backtrace(frames, arraysize(frames));
28  if (size <= 0) {
29  return;
30  }
31  for (int i = 1; i < size; i += 1) {
32  void* frame = frames[i];
33  fprintf(fp, "%2d: ", i);
34  Dl_info info;
35  const bool have_info = dladdr(frame, &info);
36  if (!have_info || info.dli_sname == nullptr) {
37  fprintf(fp, "%p", frame);
38  } else if (char* demangled = abi::__cxa_demangle(info.dli_sname, 0, 0, 0)) {
39  fprintf(fp, "%s", demangled);
40  free(demangled);
41  } else {
42  fprintf(fp, "%s", info.dli_sname);
43  }
44  if (have_info && info.dli_fname != nullptr) {
45  fprintf(fp, " [%s]", info.dli_fname);
46  }
47  fprintf(fp, "\n");
48  }
49 #endif // HAVE_EXECINFO_H
50 }
51 
52 } // namespace node
void DumpBacktrace(FILE *fp)