From 1c8559b3ab2b90a8b90ef95fd942369574eb2af5 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Fri, 25 Feb 2022 15:40:41 +0100 Subject: [PATCH] debug: support %m output format again Use thread local storage to avoid the previous dead lock issues. --- src/threads.c | 1 + src/threads.h | 33 +++++++++++++++++++++------------ src/util-debug.c | 13 ++----------- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/threads.c b/src/threads.c index 1eb4dcb44c..8d671d3828 100644 --- a/src/threads.c +++ b/src/threads.c @@ -30,6 +30,7 @@ #include "util-debug.h" #include "threads.h" +thread_local char t_thread_name[THREAD_NAME_LEN + 1]; #ifdef UNITTESTS /* UNIT TESTS */ /** diff --git a/src/threads.h b/src/threads.h index d0cb125377..c11c054bc8 100644 --- a/src/threads.h +++ b/src/threads.h @@ -27,6 +27,10 @@ #ifndef __THREADS_H__ #define __THREADS_H__ +#ifndef THREAD_NAME_LEN +#define THREAD_NAME_LEN 16 +#endif + #if defined(TLS_C11) #define thread_local _Thread_local #elif defined(TLS_GNU) @@ -83,7 +87,6 @@ enum { #endif #if HAVE_SYS_PRCTL_H #include -#define THREAD_NAME_LEN 16 #endif enum { @@ -262,28 +265,31 @@ enum { }) #endif /* OS FREEBSD */ +extern thread_local char t_thread_name[THREAD_NAME_LEN + 1]; /* * OS specific macro's for setting the thread name. "top" can display * this name. */ #if defined OS_FREEBSD /* FreeBSD */ /** \todo Add implementation for FreeBSD */ -#define SCSetThreadName(n) ({ \ - char tname[16] = ""; \ - if (strlen(n) > 16) \ - SCLogDebug("Thread name is too long, truncating it..."); \ - strlcpy(tname, n, 16); \ - pthread_set_name_np(pthread_self(), tname); \ -}) +#define SCSetThreadName(n) \ + ({ \ + char tname[THREAD_NAME_LEN] = ""; \ + if (strlen(n) > THREAD_NAME_LEN) \ + SCLogDebug("Thread name is too long, truncating it..."); \ + strlcpy(tname, n, THREAD_NAME_LEN); \ + strlcpy(t_thread_name, n, sizeof(t_thread_name)); \ + pthread_set_name_np(pthread_self(), tname); \ + }) #elif defined __OpenBSD__ /* OpenBSD */ /** \todo Add implementation for OpenBSD */ -#define SCSetThreadName(n) (0) +#define SCSetThreadName(n) ({ strlcpy(t_thread_name, n, sizeof(t_thread_name)); }) #elif defined OS_WIN32 /* Windows */ /** \todo Add implementation for Windows */ -#define SCSetThreadName(n) (0) +#define SCSetThreadName(n) ({ strlcpy(t_thread_name, n, sizeof(t_thread_name)); }) #elif defined OS_DARWIN /* Mac OS X */ /** \todo Add implementation for MacOS */ -#define SCSetThreadName(n) (0) +#define SCSetThreadName(n) ({ strlcpy(t_thread_name, n, sizeof(t_thread_name)); }) #elif defined PR_SET_NAME /* PR_SET_NAME */ /** * \brief Set the threads name @@ -294,11 +300,14 @@ enum { if (strlen(n) > THREAD_NAME_LEN) \ SCLogDebug("Thread name is too long, truncating it..."); \ strlcpy(tname, n, THREAD_NAME_LEN); \ + strlcpy(t_thread_name, n, sizeof(t_thread_name)); \ if (prctl(PR_SET_NAME, tname, 0, 0, 0) < 0) \ SCLogDebug("Error setting thread name \"%s\": %s", tname, strerror(errno)); \ }) #else -#define SCSetThreadName(n) (0) +#define SCSetThreadName(n) ({ \ + strlcpy(t_thread_name, n, sizeof(t_thread_name)); \ +} #endif diff --git a/src/util-debug.c b/src/util-debug.c index 20d0709612..b96d75feef 100644 --- a/src/util-debug.c +++ b/src/util-debug.c @@ -367,17 +367,8 @@ static SCError SCLogMessageGetBuffer( case SC_LOG_FMT_TM: temp_fmt[0] = '\0'; -/* disabled to prevent dead lock: - * log or alloc (which calls log on error) can call TmThreadsGetCallingThread - * which will lock tv_root_lock. This can happen while we already hold this - * lock. */ -#if 0 - ThreadVars *tv = TmThreadsGetCallingThread(); - cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - *msg), - "%s%s", substr, ((tv != NULL)? tv->name: "UNKNOWN TM")); -#endif - cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), - "%s%s", substr, "N/A"); + cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), "%s%s%s%s", substr, + yellow, t_thread_name, reset); if (cw < 0) return SC_ERR_SPRINTF; temp += cw;