From f668b85ecea96218c24524d91b7d4f1e065fcd74 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Thu, 17 Apr 2025 10:28:37 +0200 Subject: [PATCH] BUG/MINOR debug: fix !USE_THREAD_DUMP in ha_thread_dump_fill() The function must make sure to return NULL for foreign threads and the local buffer for the current thread in this case, otherwise panics (and sometimes even warnings) will segfault when USE_THREAD_DUMP is disabled. Let's slightly re-arrange the function to reduce the #if/else since we have to specifically handle the case of !USE_THREAD_DUMP anyway. This needs to be backported wherever b8adef065d ("MEDIUM: debug: on panic, make the target thread automatically allocate its buf") was backported (at least 2.8). (cherry picked from commit 0b56839455c7f45ae58954e1eb3873a3725899dc) Signed-off-by: Willy Tarreau --- src/debug.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/debug.c b/src/debug.c index eea2a09..49ab62b 100644 --- a/src/debug.c +++ b/src/debug.c @@ -384,6 +384,7 @@ struct buffer *ha_thread_dump_fill(struct buffer *buf, int thr) { struct buffer *old = NULL; +#ifdef USE_THREAD_DUMP /* A thread that's currently dumping other threads cannot be dumped, or * it will very likely cause a deadlock. */ @@ -402,14 +403,12 @@ struct buffer *ha_thread_dump_fill(struct buffer *buf, int thr) old = NULL; } while (!HA_ATOMIC_CAS(&ha_thread_ctx[thr].thread_dump_buffer, &old, buf)); -#ifdef USE_THREAD_DUMP /* asking the remote thread to dump itself allows to get more details * including a backtrace. */ if (thr != tid) ha_tkill(thr, DEBUGSIG); else -#endif ha_thread_dump_one(thr, thr != tid); /* now wait for the dump to be done (or cancelled) */ @@ -424,6 +423,20 @@ struct buffer *ha_thread_dump_fill(struct buffer *buf, int thr) } ha_thread_relax(); } +#else /* !USE_THREAD_DUMP below, we're on the target thread */ + /* when thread-dump is not supported, we can only dump our own thread */ + if (thr != tid) + return NULL; + + /* the buffer might not be valid in case of a panic, since we + * have to allocate it ourselves in this case. + */ + if ((ulong)buf == 0x2UL) + buf = get_trash_chunk(); + HA_ATOMIC_STORE(&th_ctx->thread_dump_buffer, buf); + old = buf; + ha_thread_dump_one(tid, 0); +#endif return (struct buffer *)((ulong)old & ~0x1UL); } -- 1.7.10.4