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