Skip to content

fix include of <execinfo.h> #1047

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2020-03-15 Kevin Ushey <kevinushey@gmail.com>

* inst/include/Rcpp/exceptions_impl.h: Ensure <execinfo.h> is included
into global namespace; refactor detection of demangler support

2020-03-13 Dirk Eddelbuettel <edd@debian.org>

* DESCRIPTION (Date, Version): Release 1.0.4
Expand Down
41 changes: 28 additions & 13 deletions inst/include/Rcpp/exceptions_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,33 @@
// You should have received a copy of the GNU General Public License
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.

// disable demangler on platforms where we have no support
#ifndef RCPP_DEMANGLER_ENABLED
# if defined(_WIN32) || \
defined(__FreeBSD__) || \
defined(__NetBSD__) || \
defined(__OpenBSD__) || \
defined(__CYGWIN__) || \
defined(__sun) || \
defined(_AIX) || \
defined(__MUSL__) || \
defined(__HAIKU__) || \
defined(__ANDROID__)
# define RCPP_DEMANGLER_ENABLED 0
# elif defined(__GNUC__) || defined(__clang__)
# include <execinfo.h>
# define RCPP_DEMANGLER_ENABLED 1
# endif
#endif

namespace Rcpp {
#if defined(__GNUC__) || defined(__clang__)
#if defined(_WIN32) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__CYGWIN__) || defined(__sun) || defined(_AIX) || defined(__MUSL__) || defined(__HAIKU__) || defined(__ANDROID__)
// do nothing
#else
#include <execinfo.h>

// Extract mangled name e.g. ./test(baz+0x14)[0x400962]
#if RCPP_DEMANGLER_ENABLED
static std::string demangler_one(const char* input) { // #nocov start

static std::string buffer;

buffer = input;
size_t last_open = buffer.find_last_of('(');
size_t last_close = buffer.find_last_of(')');
Expand All @@ -42,19 +59,16 @@ namespace Rcpp {
function_name.resize(function_plus);
}
buffer.replace(last_open + 1, function_name.size(), demangle(function_name));

return buffer;

}
#endif
#endif

// thread-safe; invoked prior to throwing the exception
inline void exception::record_stack_trace()
{
#if defined(__GNUC__) || defined(__clang__)
#if defined(_WIN32) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__CYGWIN__) || defined(__sun) || defined(_AIX) || defined(__MUSL__) || defined(__HAIKU__) || defined(__ANDROID__)
// C++ stack not available on this system
#else // ! (defined(_WIN32) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__CYGWIN__) || defined(__sun) || defined(_AIX) || defined(__ANDROID__)

#if RCPP_DEMANGLER_ENABLED
/* inspired from http://tombarta.wordpress.com/2008/08/01/c-stack-traces-with-gcc/ */
const size_t max_depth = 100;
int stack_depth;
Expand All @@ -66,8 +80,7 @@ namespace Rcpp {
std::transform(stack_strings + 1, stack_strings + stack_depth,
std::back_inserter(stack), demangler_one);
free(stack_strings); // malloc()ed by backtrace_symbols
#endif
#endif
#endif
}

// not thread-safe; invoked after catching the exception
Expand All @@ -86,4 +99,6 @@ namespace Rcpp {
trace.attr("class") = "Rcpp_stack_trace";
rcpp_set_stack_trace(trace); // #nocov end
}

}