From 0474bbcfb9395d53b3738fc3aff6fafed368f8cb Mon Sep 17 00:00:00 2001 From: Jesse Natalie Date: Tue, 26 Apr 2022 16:25:13 -0700 Subject: [PATCH] u_debug_stack: Lock around stack dumps to prevent interleaving Reviewed-by: Yonggang Luo Reviewed-by: Sil Vilerino Part-of: --- src/util/u_debug_stack.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/util/u_debug_stack.c b/src/util/u_debug_stack.c index 87f981f..a0d9499 100644 --- a/src/util/u_debug_stack.c +++ b/src/util/u_debug_stack.c @@ -36,6 +36,7 @@ #include "u_debug_symbol.h" #include "u_debug_stack.h" #include "pipe/p_config.h" +#include "c11/threads.h" #if defined(HAVE_LIBUNWIND) @@ -186,7 +187,6 @@ debug_backtrace_print(FILE *f, #include #endif - /** * Capture stack backtrace. * @@ -296,17 +296,32 @@ debug_backtrace_capture(struct debug_stack_frame *backtrace, } +static mtx_t backtrace_mutex; + +static void +initialize_backtrace_mutex() +{ + static bool first = true; + + if (first) { + (void)mtx_init(&backtrace_mutex, mtx_plain); + first = false; + } +} + void debug_backtrace_dump(const struct debug_stack_frame *backtrace, unsigned nr_frames) { unsigned i; - + initialize_backtrace_mutex(); + mtx_lock(&backtrace_mutex); for (i = 0; i < nr_frames; ++i) { if (!backtrace[i].function) break; debug_symbol_print(backtrace[i].function); } + mtx_unlock(&backtrace_mutex); } @@ -317,6 +332,8 @@ debug_backtrace_print(FILE *f, { unsigned i; + initialize_backtrace_mutex(); + mtx_lock(&backtrace_mutex); for (i = 0; i < nr_frames; ++i) { const char *symbol; if (!backtrace[i].function) @@ -325,6 +342,8 @@ debug_backtrace_print(FILE *f, if (symbol) fprintf(f, "%s\n", symbol); } + fflush(f); + mtx_unlock(&backtrace_mutex); } #endif /* HAVE_LIBUNWIND */ -- 2.7.4