debug: support %m output format again

Use thread local storage to avoid the previous dead lock issues.
pull/7242/head
Victor Julien 3 years ago
parent ce4e543719
commit 1c8559b3ab

@ -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 */
/**

@ -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 <sys/prctl.h>
#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

@ -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;

Loading…
Cancel
Save