| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "libnetdata/libnetdata.h" |
| 4 | #include "stacktrace.h" |
| 5 | |
| 6 | // External variable needed by other modules |
| 7 | static bool nd_log_forked = false; |
| 8 | |
| 9 | void stacktrace_forked(void) { |
| 10 | nd_log_forked = true; |
| 11 | } |
| 12 | |
| 13 | // The stack trace formatter called by logger |
| 14 | NEVER_INLINE |
| 15 | bool stack_trace_formatter(BUFFER *wb, void *data __maybe_unused) { |
| 16 | static __thread bool in_stack_trace = false; |
| 17 | |
| 18 | extern bool nd_log_forked; |
| 19 | if (nd_log_forked) { |
| 20 | // libunwind freezes in forked children |
| 21 | buffer_strcat(wb, STACK_TRACE_INFO_PREFIX "stack trace is not available, stack trace after fork is disabled"); |
| 22 | return true; |
| 23 | } |
| 24 | |
| 25 | if (in_stack_trace) { |
| 26 | // Prevent recursion |
| 27 | buffer_strcat(wb, STACK_TRACE_INFO_PREFIX "stack trace is not available, stack trace recursion detected"); |
| 28 | return true; |
| 29 | } |
| 30 | |
| 31 | in_stack_trace = true; |
| 32 | |
| 33 | // Use the existing stacktrace_capture |
| 34 | stacktrace_capture(wb); |
| 35 | |
| 36 | in_stack_trace = false; // Ensure the flag is reset |
| 37 | return true; |
| 38 | } |