Rcpp Version 1.0.9
exceptions_impl.h
Go to the documentation of this file.
1 // exceptions_impl.h: Rcpp R/C++ interface class library -- exceptions
2 //
3 // Copyright (C) 2012 - 2019 Dirk Eddelbuettel and Romain Francois
4 // Copyright (C) 2020 Dirk Eddelbuettel, Romain Francois, and Joshua N. Pritikin
5 //
6 // This file is part of Rcpp.
7 //
8 // Rcpp is free software: you can redistribute it and/or modify it
9 // under the terms of the GNU General Public License as published by
10 // the Free Software Foundation, either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // Rcpp is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
20 
21 #ifndef Rcpp__exceptions_impl__h
22 #define Rcpp__exceptions_impl__h
23 
24 // disable demangler on platforms where we have no support
25 #ifndef RCPP_DEMANGLER_ENABLED
26 # if defined(_WIN32) || \
27  defined(__FreeBSD__) || \
28  defined(__NetBSD__) || \
29  defined(__OpenBSD__) || \
30  defined(__CYGWIN__) || \
31  defined(__sun) || \
32  defined(_AIX) || \
33  defined(__MUSL__) || \
34  defined(__HAIKU__) || \
35  defined(__ANDROID__)
36 # define RCPP_DEMANGLER_ENABLED 0
37 # elif defined(__GNUC__) || defined(__clang__)
38 # include <execinfo.h>
39 # define RCPP_DEMANGLER_ENABLED 1
40 # else
41 # define RCPP_DEMANGLER_ENABLED 0
42 # endif
43 #endif
44 
45 namespace Rcpp {
46 
47  // Extract mangled name e.g. ./test(baz+0x14)[0x400962]
48 #if RCPP_DEMANGLER_ENABLED
49  static inline std::string demangler_one(const char* input) { // #nocov start
50 
51  static std::string buffer;
52 
53  buffer = input;
54  size_t last_open = buffer.find_last_of('(');
55  size_t last_close = buffer.find_last_of(')');
56  if (last_open == std::string::npos ||
57  last_close == std::string::npos) {
58  return input; // #nocov
59  }
60  std::string function_name = buffer.substr(last_open + 1, last_close - last_open - 1);
61  // Strip the +0x14 (if it exists, which it does not in earlier versions of gcc)
62  size_t function_plus = function_name.find_last_of('+');
63  if (function_plus != std::string::npos) {
64  function_name.resize(function_plus);
65  }
66  buffer.replace(last_open + 1, function_name.size(), demangle(function_name));
67 
68  return buffer;
69 
70  }
71 #endif
72 
73  // thread-safe; invoked prior to throwing the exception
75  {
76 #if RCPP_DEMANGLER_ENABLED
77  /* inspired from http://tombarta.wordpress.com/2008/08/01/c-stack-traces-with-gcc/ */
78  const size_t max_depth = 100;
79  int stack_depth;
80  void *stack_addrs[max_depth];
81 
82  stack_depth = backtrace(stack_addrs, max_depth);
83  char **stack_strings = backtrace_symbols(stack_addrs, stack_depth);
84 
85  std::transform(stack_strings + 1, stack_strings + stack_depth,
86  std::back_inserter(stack), demangler_one);
87  free(stack_strings); // malloc()ed by backtrace_symbols
88 #endif
89  }
90 
91  // not thread-safe; invoked after catching the exception
93  {
94  if (!stack.size()) {
95  rcpp_set_stack_trace(R_NilValue);
96  return;
97  }
98 
99  CharacterVector res(stack.size());
100  std::copy(stack.begin(), stack.end(), res.begin());
101  List trace = List::create(_["file" ] = "",
102  _["line" ] = -1,
103  _["stack"] = res);
104  trace.attr("class") = "Rcpp_stack_trace";
105  rcpp_set_stack_trace(trace); // #nocov end
106  }
107 
108 }
109 
110 #endif
AttributeProxy attr(const std::string &name)
iterator begin()
Definition: Vector.h:334
static Vector create()
Definition: Vector.h:1122
std::vector< std::string > stack
Definition: exceptions.h:59
void copy_stack_trace_to_r() const
void record_stack_trace()
std::string demangle(const std::string &name)
Definition: routines.h:192
Rcpp API.
Definition: algo.h:28
static internal::NamedPlaceHolder _
Definition: Named.h:64
attribute_hidden SEXP rcpp_set_stack_trace(SEXP e)
Definition: routines.h:186