NEURON
backtrace_utils.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #if HAVE_CXXABI_H
3 #include <cxxabi.h>
4 #endif
5 #include <cstring>
6 #include <regex>
7 #include <string>
8 
9 #ifdef USE_BACKWARD
10 #include "backward.hpp"
11 #endif
12 
13 /*
14  * Small utility to bring C++ symbol demangling to C
15  */
16 
17 extern "C" {
18 
19 int parse_bt_symbol(char* backtrace_line, void** addr, char* symbol, char* offset) {
20 #ifndef _WIN32
21 #ifdef __APPLE__
22  std::regex btline("(\\d+)\\s+([\\w\\.]+)\\s+(0x[\\da-f]+)\\s+(\\w+)\\s+\\+\\s+(\\d+)");
23 #define ADDR 3
24 #define SYMBOL 4
25 #define OFFSET 5
26 #define OBJPOS 4
27 #elif __linux__
28  std::regex btline("([\\w\\.\\/]+)\\((\\w*)\\+(0x[\\da-f]+)\\)\\s+\\[(0x[\\da-f]+)\\]");
29 #define ADDR 4
30 #define SYMBOL 2
31 #define OFFSET 3
32 #define OBJPOS 2
33 #endif
34  std::cmatch backtrace_match;
35  if (std::regex_search(backtrace_line, backtrace_match, btline)) {
36  *addr = reinterpret_cast<void*>(std::stoul(backtrace_match[ADDR].str(), nullptr, 16));
37  std::strcpy(symbol, backtrace_match[SYMBOL].str().c_str());
38  std::strcpy(offset, backtrace_match[OFFSET].str().c_str());
39  backtrace_line[backtrace_match.position(OBJPOS)-1] = '\0';
40  return 1;
41  }
42 #endif
43  return 0;
44 }
45 
46 int cxx_demangle(char* symbol, char** funcname, size_t* funcname_sz) {
47 #if HAVE_CXXABI_H
48  int status = 0;
49  char* ret = abi::__cxa_demangle(symbol, *funcname, funcname_sz, &status);
50  *funcname = ret;
51  return status;
52 #else
53  // return something non-zero to indicate failure
54  // cxa_demangle can return 3 error codes: -1, -2, -3, we extend by one more error code
55  return -4;
56 #endif
57 }
58 
60 #ifdef USE_BACKWARD
61  backward::StackTrace st; st.load_here(12);
62  backward::Printer p; p.print(st);
63 #endif
64 }
65 
66 }
int cxx_demangle(char *symbol, char **funcname, size_t *funcname_sz)
size_t p
#define SYMBOL
Definition: model.h:102
return status
#define Printer
Definition: _defines.h:211
#define ret
Definition: redef.h:123
void backward_wrapper()
int parse_bt_symbol(char *backtrace_line, void **addr, char *symbol, char *offset)