summaryrefslogtreecommitdiffstats
path: root/libnetdata
diff options
context:
space:
mode:
authorMarkos Fountoulakis <44345837+mfundul@users.noreply.github.com>2020-06-20 22:13:22 +0300
committerGitHub <noreply@github.com>2020-06-20 22:13:22 +0300
commit431b1c85698feb33aee2e8cf972dcdf6fb26ba46 (patch)
treed4c81546538649529e7a303dbaf2134cde2d9996 /libnetdata
parentae944cd492ceeec4a466c2e976fbeea0fb18bc66 (diff)
Add libuv thread names support to FATAL log level (#9382)
* Add support for the fatal() family of calls to detect non-netdata thread names from the OS
Diffstat (limited to 'libnetdata')
-rw-r--r--libnetdata/log/log.c16
-rw-r--r--libnetdata/threads/threads.c18
-rw-r--r--libnetdata/threads/threads.h2
3 files changed, 32 insertions, 4 deletions
diff --git a/libnetdata/log/log.c b/libnetdata/log/log.c
index 52ea932aa3..95d3d98d46 100644
--- a/libnetdata/log/log.c
+++ b/libnetdata/log/log.c
@@ -796,6 +796,8 @@ void fatal_int( const char *file, const char *function, const unsigned long line
// save a copy of errno - just in case this function generates a new error
int __errno = errno;
va_list args;
+ const char *thread_tag;
+ char os_threadname[NETDATA_THREAD_NAME_MAX + 1];
if(error_log_syslog) {
va_start( args, fmt );
@@ -803,14 +805,22 @@ void fatal_int( const char *file, const char *function, const unsigned long line
va_end( args );
}
+ thread_tag = netdata_thread_tag();
+ if (!netdata_thread_tag_exists()) {
+ os_thread_get_current_name_np(os_threadname);
+ if ('\0' != os_threadname[0]) { /* If it is not an empty string replace "MAIN" thread_tag */
+ thread_tag = os_threadname;
+ }
+ }
+
char date[LOG_DATE_LENGTH];
log_date(date, LOG_DATE_LENGTH);
log_lock();
va_start( args, fmt );
- if(debug_flags) fprintf(stderr, "%s: %s FATAL : %s : (%04lu@%-10.10s:%-15.15s): ", date, program_name, netdata_thread_tag(), line, file, function);
- else fprintf(stderr, "%s: %s FATAL : %s :", date, program_name, netdata_thread_tag());
+ if(debug_flags) fprintf(stderr, "%s: %s FATAL : %s : (%04lu@%-10.10s:%-15.15s): ", date, program_name, thread_tag, line, file, function);
+ else fprintf(stderr, "%s: %s FATAL : %s : ", date, program_name, thread_tag);
vfprintf( stderr, fmt, args );
va_end( args );
@@ -823,7 +833,7 @@ void fatal_int( const char *file, const char *function, const unsigned long line
snprintfz(action_data, 70, "%04lu@%-10.10s:%-15.15s/%d", line, file, function, __errno);
char action_result[60+1];
- snprintfz(action_result, 60, "%s:%s", program_name, strncmp(netdata_thread_tag(), "STREAM_RECEIVER", strlen("STREAM_RECEIVER"))?netdata_thread_tag():"[x]");
+ snprintfz(action_result, 60, "%s:%s", program_name, strncmp(thread_tag, "STREAM_RECEIVER", strlen("STREAM_RECEIVER")) ? thread_tag : "[x]");
send_statistics("FATAL", action_result, action_data);
netdata_cleanup_and_exit(1);
diff --git a/libnetdata/threads/threads.c b/libnetdata/threads/threads.c
index 92e1678717..8a2e292439 100644
--- a/libnetdata/threads/threads.c
+++ b/libnetdata/threads/threads.c
@@ -18,8 +18,12 @@ typedef struct {
static __thread NETDATA_THREAD *netdata_thread = NULL;
+inline int netdata_thread_tag_exists(void) {
+ return (netdata_thread && netdata_thread->tag && *netdata_thread->tag);
+}
+
const char *netdata_thread_tag(void) {
- return ((netdata_thread && netdata_thread->tag && *netdata_thread->tag)?netdata_thread->tag:"MAIN");
+ return (netdata_thread_tag_exists() ? netdata_thread->tag : "MAIN");
}
// ----------------------------------------------------------------------------
@@ -151,6 +155,18 @@ void uv_thread_set_name_np(uv_thread_t ut, const char* name) {
info("cannot set libuv thread name to %s. Err: %d", threadname, ret);
}
+void os_thread_get_current_name_np(char threadname[NETDATA_THREAD_NAME_MAX + 1])
+{
+ int ret = 0;
+
+ threadname[0] = '\0';
+#if defined(__FreeBSD__)
+ pthread_get_name_np(pthread_self(), threadname, NETDATA_THREAD_NAME_MAX + 1);
+#elif defined(HAVE_PTHREAD_GETNAME_NP) /* Linux & macOS */
+ (void)pthread_getname_np(pthread_self(), threadname, NETDATA_THREAD_NAME_MAX + 1);
+#endif
+}
+
static void *thread_start(void *ptr) {
netdata_thread = (NETDATA_THREAD *)ptr;
diff --git a/libnetdata/threads/threads.h b/libnetdata/threads/threads.h
index b7cb0e15a0..e7d79d3283 100644
--- a/libnetdata/threads/threads.h
+++ b/libnetdata/threads/threads.h
@@ -22,6 +22,7 @@ typedef pthread_t netdata_thread_t;
#define NETDATA_THREAD_TAG_MAX 100
extern const char *netdata_thread_tag(void);
+extern int netdata_thread_tag_exists(void);
extern size_t netdata_threads_init(void);
extern void netdata_threads_init_after_fork(size_t stacksize);
@@ -33,6 +34,7 @@ extern int netdata_thread_detach(pthread_t thread);
#define NETDATA_THREAD_NAME_MAX 15
extern void uv_thread_set_name_np(uv_thread_t ut, const char* name);
+extern void os_thread_get_current_name_np(char threadname[NETDATA_THREAD_NAME_MAX + 1]);
#define netdata_thread_self pthread_self
#define netdata_thread_testcancel pthread_testcancel