-
-
Notifications
You must be signed in to change notification settings - Fork 219
Make Rcpp::exception thread-safe #1043
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2815ae8
Make Rcpp::exception thread-safe
jpritikin 8f5cd61
Respond to commit review
jpritikin 614e72f
Change copyright authorship per at Dirk's direction
jpritikin 23709a2
Append to instead of replace authorship
jpritikin caa35fb
Update ChangeLog per latest PR revision
jpritikin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// exceptions_impl.h: Rcpp R/C++ interface class library -- exceptions | ||
// | ||
// Copyright (C) 2020 Dirk Eddelbuettel, Romain Francois, and Joshua N. Pritikin | ||
// | ||
// This file is part of Rcpp. | ||
// | ||
// Rcpp is free software: you can redistribute it and/or modify it | ||
// under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 2 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Rcpp is distributed in the hope that it will be useful, but | ||
// WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
namespace Rcpp { | ||
kevinushey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#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] | ||
static std::string demangler_one(const char* input) { | ||
static std::string buffer; | ||
buffer = input; | ||
size_t last_open = buffer.find_last_of('('); | ||
size_t last_close = buffer.find_last_of(')'); | ||
if (last_open == std::string::npos || | ||
last_close == std::string::npos) { | ||
return input; // #nocov | ||
} | ||
std::string function_name = buffer.substr(last_open + 1, last_close - last_open - 1); | ||
// Strip the +0x14 (if it exists, which it does not in earlier versions of gcc) | ||
size_t function_plus = function_name.find_last_of('+'); | ||
if (function_plus != std::string::npos) { | ||
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() | ||
kevinushey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
#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__) | ||
|
||
/* inspired from http://tombarta.wordpress.com/2008/08/01/c-stack-traces-with-gcc/ */ | ||
const size_t max_depth = 100; | ||
int stack_depth; | ||
void *stack_addrs[max_depth]; | ||
|
||
stack_depth = backtrace(stack_addrs, max_depth); | ||
char **stack_strings = backtrace_symbols(stack_addrs, stack_depth); | ||
|
||
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 | ||
} | ||
|
||
// not thread-safe; invoked after catching the exception | ||
inline void exception::copy_stack_trace_to_r() const | ||
{ | ||
if (!stack.size()) { | ||
rcpp_set_stack_trace(R_NilValue); | ||
return; | ||
} | ||
|
||
CharacterVector res(stack.size()); | ||
std::copy(stack.begin(), stack.end(), res.begin()); | ||
List trace = List::create(_["file" ] = "", | ||
_["line" ] = -1, | ||
_["stack"] = res); | ||
trace.attr("class") = "Rcpp_stack_trace"; | ||
rcpp_set_stack_trace(trace); | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.