Skip to content
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

Use thread local for libunwind GetStackTrace() reentrancy protection #161

Merged
merged 1 commit into from
Oct 31, 2019
Merged
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
9 changes: 6 additions & 3 deletions src/stacktrace_libunwind-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ _START_GOOGLE_NAMESPACE_
// recursive request, we'd end up with infinite recursion or deadlock.
// Luckily, it's safe to ignore those subsequent traces. In such
// cases, we return 0 to indicate the situation.
static bool g_now_entering = false;
// We can use the GCC __thread syntax here since libunwind is not supported on
// Windows.
static __thread bool g_tl_entered; // Initialized to false.

// If you change this function, also change GetStackFrames below.
int GetStackTrace(void** result, int max_depth, int skip_count) {
Expand All @@ -58,9 +60,10 @@ int GetStackTrace(void** result, int max_depth, int skip_count) {
unw_cursor_t cursor;
unw_context_t uc;

if (sync_val_compare_and_swap(&g_now_entering, false, true)) {
if (g_tl_entered) {
return 0;
}
g_tl_entered = true;

unw_getcontext(&uc);
RAW_CHECK(unw_init_local(&cursor, &uc) >= 0, "unw_init_local failed");
Expand All @@ -80,7 +83,7 @@ int GetStackTrace(void** result, int max_depth, int skip_count) {
break;
}

g_now_entering = false;
g_tl_entered = false;
return n;
}

Expand Down