ALPSCore reference
stacktrace.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1998-2018 ALPS Collaboration. See COPYRIGHT.TXT
3  * All rights reserved. Use is subject to license terms. See LICENSE.TXT
4  * For use in publications, see ACKNOWLEDGE.TXT
5  */
6 
8 
9 #ifndef ALPS_UTILITY_NO_STACKTRACE
10 
11 #include <sstream>
12 
13 #include <cxxabi.h>
14 #include <stdlib.h>
15 #include <execinfo.h>
16 
17 #endif
18 
19 namespace alps {
20 
21 #ifndef ALPS_UTILITY_NO_STACKTRACE
22 
23  // TODO: use boost::units::detail::demangle
24  // in #include <boost/units/detail/utility.hpp>
25  std::string stacktrace() {
26  std::ostringstream buffer;
27  void * stack[ALPS_MAX_FRAMES + 1];
28  std::size_t depth = backtrace(stack, ALPS_MAX_FRAMES + 1);
29  if (!depth)
30  buffer << " <empty, possibly corrupt>" << std::endl;
31  else {
32  char * * symbols = backtrace_symbols(stack, depth);
33  for (std::size_t i = 1; i < depth; ++i) {
34  std::string symbol = symbols[i];
35  // TODO: use alps::stacktrace to find the position of the demangling name
36  if (symbol.find_first_of(' ', 59) != std::string::npos) {
37  std::string name = symbol.substr(59, symbol.find_first_of(' ', 59) - 59);
38  int status;
39  char * demangled = abi::__cxa_demangle(name.c_str(), NULL, NULL, &status);
40  if (!status) {
41  buffer << " "
42  << symbol.substr(0, 59)
43  << demangled
44  << symbol.substr(59 + name.size())
45  << std::endl;
46  free(demangled);
47  } else
48  buffer << " " << symbol << std::endl;
49  } else
50  buffer << " " << symbol << std::endl;
51  }
52  free(symbols);
53  }
54  return buffer.str();
55  }
56 
57 #else
58 
59  std::string stacktrace() {
60  return "";
61  }
62 
63 #endif
64 
65 }
std::string stacktrace()
Definition: stacktrace.cpp:25
#define ALPS_MAX_FRAMES
Definition: stacktrace.hpp:17