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