lsst.utils  13.0-5-g109db22
 All Classes Namespaces Files Functions Variables Groups Pages
Backtrace.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008-2017 AURA/LSST.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <https://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 #if defined(LSST_UTILS_BACKTRACE_ENABLE) && (defined(__clang__) || defined(__GNUC__))
26 
27 #include <cerrno>
28 #include <cstdio>
29 #include <cstdlib>
30 #include <csignal>
31 
32 #include <cxxabi.h>
33 #include <execinfo.h>
34 
35 #include <regex>
36 
37 #include "lsst/utils/Backtrace.h"
38 
39 namespace lsst {
40 namespace utils {
41 
42 namespace {
43 
45 static constexpr size_t MAX_FRAMES = 128;
47 static constexpr size_t NAME_SIZE_ESTIMATE = 1024;
48 
61 char *demangleAndPrint(char *input, char *buffer, size_t *bufferSize) {
62  int status = 1;
63 
64  std::cmatch matches;
65  std::regex rgx(
66  "(.*[\\s|\\(])" // before
67  "(_\\w+)" // mangled name
68  "(.*\\+.*)" // after
69  );
70 
71  if (std::regex_match(input, matches, rgx)) {
72  buffer = abi::__cxa_demangle(matches.str(2).c_str(), buffer, bufferSize, &status);
73  fprintf(stderr, "%s%s%s\n", matches.str(1).c_str(), buffer, matches.str(3).c_str());
74  }
75  if (status != 0) {
76  // no name found to demangle or name demangling failed
77  fprintf(stderr, "%s\n", input);
78  }
79  return buffer;
80 }
81 
89 void raiseWithDefaultHandler(int signum) noexcept {
90  signal(signum, SIG_DFL);
91  raise(signum);
92 }
93 
99 void signalHandler(int signum) {
100  fprintf(stderr, "Caught signal %d, backtrace follows:\n", signum);
101 
102  // retrieve current stack addresses
103  void *addressList[MAX_FRAMES + 1];
104  size_t addressLength = backtrace(addressList, MAX_FRAMES);
105  if (addressLength == 0) {
106  fprintf(stderr, " \n");
107  return;
108  }
109 
110  // translate the addresses into an array of strings
111  // that describe the addresses symbolically.
112  // return value symbolList is malloc'ed and must be free'd
113  char **symbolList = backtrace_symbols(addressList, addressLength);
114  if (symbolList == NULL) {
115  fprintf(stderr, "[backtrace_symbols] cannot dump trace, probably out of memory\n");
116  raiseWithDefaultHandler(signum);
117  }
118 
119  // allocate storage for demangled name
120  // this will be reallocated by abi::__cxa_demangle in demangleAndPrint
121  // if storage is insufficient, but if it is sufficient
122  // this reduces the number of allocations
123  size_t nameSize = NAME_SIZE_ESTIMATE;
124  char *name = (char *)malloc(nameSize * sizeof(char));
125  if (name == NULL) {
126  fprintf(stderr, "[malloc] cannot dump trace, probably out of memory\n");
127  raiseWithDefaultHandler(signum);
128  }
129 
130  for (size_t i = 0; i < addressLength; ++i) {
131  if (char *buffer = demangleAndPrint(symbolList[i], name, &nameSize)) {
132  // buffer may have been realloc'ed
133  name = buffer;
134  }
135  }
136 
137  free(name);
138  free(symbolList);
139  raiseWithDefaultHandler(signum);
140 }
141 
142 } // namespace
143 
144 Backtrace::Backtrace() noexcept : enabled(true) {
145  // Register abort handlers
146  signal(SIGABRT, signalHandler);
147  signal(SIGSEGV, signalHandler);
148  signal(SIGILL, signalHandler);
149  signal(SIGFPE, signalHandler);
150 }
151 
152 } // namespace utils
153 } // namespace lsst
154 
155 #else
156 
157 #include "lsst/utils/Backtrace.h"
158 
159 namespace lsst {
160 namespace utils {
161 
162 Backtrace::Backtrace() noexcept : enabled(false) {}
163 
164 } // namespace utils
165 } // namespace lsst
166 
167 #endif
168