debug mutex third_party/socket_wrapper/socket_wrapper.c
authorStefan Metzmacher <metze@samba.org>
Tue, 16 Feb 2021 18:19:45 +0000 (19:19 +0100)
committerStefan Metzmacher <metze@samba.org>
Tue, 23 Apr 2024 16:16:46 +0000 (18:16 +0200)
third_party/socket_wrapper/socket_wrapper.c

index c759d350fb1816bd1c7de26adac7fd034d01476e..c5d939f28ee16e60aca4d1c441c9b51bbdfff34c 100644 (file)
@@ -222,7 +222,7 @@ enum swrap_dbglvl_e {
        swrap_mutex_lock(&sockets_mutex); \
        swrap_mutex_lock(&socket_reset_mutex); \
        swrap_mutex_lock(&first_free_mutex); \
-       swrap_mutex_lock(&sockets_si_global); \
+       swrap_mutex_lock_trace(&sockets_si_global); \
        swrap_mutex_lock(&autobind_start_mutex); \
        swrap_mutex_lock(&pcap_dump_mutex); \
        swrap_mutex_lock(&mtu_update_mutex); \
@@ -232,7 +232,7 @@ enum swrap_dbglvl_e {
        swrap_mutex_unlock(&mtu_update_mutex); \
        swrap_mutex_unlock(&pcap_dump_mutex); \
        swrap_mutex_unlock(&autobind_start_mutex); \
-       swrap_mutex_unlock(&sockets_si_global); \
+       swrap_mutex_unlock_trace(&sockets_si_global); \
        swrap_mutex_unlock(&first_free_mutex); \
        swrap_mutex_unlock(&socket_reset_mutex); \
        swrap_mutex_unlock(&sockets_mutex); \
@@ -244,7 +244,7 @@ enum swrap_dbglvl_e {
 #define SWRAP_LOCK_SI(si) do { \
        struct socket_info_container *sic = SOCKET_INFO_CONTAINER(si); \
        if (sic != NULL) { \
-               swrap_mutex_lock(&sockets_si_global); \
+               swrap_mutex_lock_trace(&sockets_si_global); \
        } else { \
                abort(); \
        } \
@@ -253,7 +253,7 @@ enum swrap_dbglvl_e {
 #define SWRAP_UNLOCK_SI(si) do { \
        struct socket_info_container *sic = SOCKET_INFO_CONTAINER(si); \
        if (sic != NULL) { \
-               swrap_mutex_unlock(&sockets_si_global); \
+               swrap_mutex_unlock_trace(&sockets_si_global); \
        } else { \
                abort(); \
        } \
@@ -851,30 +851,54 @@ static void *_swrap_bind_symbol(enum swrap_lib lib, const char *fn_name)
        return func;
 }
 
-#define swrap_mutex_lock(m) _swrap_mutex_lock(m, #m, __func__, __LINE__)
-static void _swrap_mutex_lock(pthread_mutex_t *mutex, const char *name, const char *caller, unsigned line)
+#define swrap_mutex_lock(m) _swrap_mutex_lock(m, #m, __func__, __LINE__, false)
+#define swrap_mutex_lock_trace(m) _swrap_mutex_lock(m, #m, __func__, __LINE__, true)
+static void _swrap_mutex_lock(pthread_mutex_t *mutex, const char *name, const char *caller, unsigned line, bool trace)
 {
        int ret;
 
+       if (trace) {
+               SWRAP_LOG(SWRAP_LOG_ERROR, "PID(%d):TID(%lld):PPID(%d): %s(%u): Locking pthread mutex(%s) ...",
+                         getpid(), (unsigned long long)gettid(), getppid(),
+                         caller, line, name);
+       }
        ret = pthread_mutex_lock(mutex);
        if (ret != 0) {
-               SWRAP_LOG(SWRAP_LOG_ERROR, "PID(%d):PPID(%d): %s(%u): Couldn't lock pthread mutex(%s) - %s",
-                         getpid(), getppid(), caller, line, name, strerror(ret));
+               SWRAP_LOG(SWRAP_LOG_ERROR, "PID(%d):TID(%lld):PPID(%d): %s(%u): Couldn't lock pthread mutex(%s) - %d %s",
+                         getpid(), (unsigned long long)gettid(), getppid(),
+                         caller, line, name, ret, strerror(ret));
                abort();
        }
+       if (trace) {
+               SWRAP_LOG(SWRAP_LOG_ERROR, "PID(%d):TID(%lld):PPID(%d): %s(%u): Locked pthread mutex(%s) - %d %s",
+                         getpid(), (unsigned long long)gettid(), getppid(),
+                         caller, line, name, ret, strerror(ret));
+       }
 }
 
-#define swrap_mutex_unlock(m) _swrap_mutex_unlock(m, #m, __func__, __LINE__)
-static void _swrap_mutex_unlock(pthread_mutex_t *mutex, const char *name, const char *caller, unsigned line)
+#define swrap_mutex_unlock(m) _swrap_mutex_unlock(m, #m, __func__, __LINE__, false)
+#define swrap_mutex_unlock_trace(m) _swrap_mutex_unlock(m, #m, __func__, __LINE__, true)
+static void _swrap_mutex_unlock(pthread_mutex_t *mutex, const char *name, const char *caller, unsigned line, bool trace)
 {
        int ret;
 
+       if (trace) {
+               SWRAP_LOG(SWRAP_LOG_ERROR, "PID(%d):TID(%lld):PPID(%d): %s(%u): Unlocking pthread mutex(%s) ...",
+                         getpid(), (unsigned long long)gettid(), getppid(),
+                         caller, line, name);
+       }
        ret = pthread_mutex_unlock(mutex);
        if (ret != 0) {
-               SWRAP_LOG(SWRAP_LOG_ERROR, "PID(%d):PPID(%d): %s(%u): Couldn't unlock pthread mutex(%s) - %s",
-                         getpid(), getppid(), caller, line, name, strerror(ret));
+               SWRAP_LOG(SWRAP_LOG_ERROR, "PID(%d):TID(%lld):PPID(%d): %s(%u): Couldn't unlock pthread mutex(%s) - %d %s",
+                         getpid(), (unsigned long long)gettid(), getppid(),
+                         caller, line, name, ret, strerror(ret));
                abort();
        }
+       if (trace) {
+               SWRAP_LOG(SWRAP_LOG_ERROR, "PID(%d):TID(%lld):PPID(%d): %s(%u): Unlocked pthread mutex(%s) - %d %s",
+                         getpid(), (unsigned long long)gettid(), getppid(),
+                         caller, line, name, ret, strerror(ret));
+       }
 }
 
 /*
@@ -2045,7 +2069,7 @@ static void socket_wrapper_init_sockets(void)
        }
 
        swrap_mutex_lock(&first_free_mutex);
-       swrap_mutex_lock(&sockets_si_global);
+       swrap_mutex_lock_trace(&sockets_si_global);
 
        first_free = 0;
 
@@ -2056,7 +2080,7 @@ static void socket_wrapper_init_sockets(void)
        /* mark the end of the free list */
        swrap_set_next_free(&sockets[max_sockets-1].info, -1);
 
-       swrap_mutex_unlock(&sockets_si_global);
+       swrap_mutex_unlock_trace(&sockets_si_global);
        swrap_mutex_unlock(&first_free_mutex);
        swrap_mutex_unlock(&sockets_mutex);
        if (ret != 0) {