[rhythmbox] debug: print thread names rather than handle addresses where available



commit 225e07841c7df06593b434443cc3ed69f69330c0
Author: Jonathan Matthew <jonathan d14n org>
Date:   Tue Jan 18 22:10:18 2022 +1000

    debug: print thread names rather than handle addresses where available
    
    This should make debug output from threaded sections of code a bit
    easier to deal with.  Most threads already have names, although threads
    created by glib thread pools are all called 'pool-rhythmbox', so we
    print the thread handle address for these too.

 config.h.meson |  6 ++++++
 lib/rb-debug.c | 18 ++++++++++++++++--
 meson.build    | 13 +++++++++++++
 3 files changed, 35 insertions(+), 2 deletions(-)
---
diff --git a/config.h.meson b/config.h.meson
index 8e7468504..811a501df 100644
--- a/config.h.meson
+++ b/config.h.meson
@@ -28,3 +28,9 @@
 
 /* Define to the version of this package. */
 #define VERSION "@VERSION@"
+
+/* Define if prctl is available. */
+#mesondefine HAVE_PRCTL
+
+/* Define if pthread_getname_np is available. */
+#mesondefine HAVE_PTHREAD_GETNAME_NP
diff --git a/lib/rb-debug.c b/lib/rb-debug.c
index dac751645..a5b4ccac6 100644
--- a/lib/rb-debug.c
+++ b/lib/rb-debug.c
@@ -36,6 +36,11 @@
 #include <stdarg.h>
 #include <signal.h>
 #include <time.h>
+#if defined(HAVE_PRCTL)
+#include <sys/prctl.h>
+#elif defined(HAVE_PTHREAD_GETNAME_NP)
+#include <pthread.h>
+#endif
 
 #include <glib.h>
 
@@ -95,12 +100,21 @@ _rb_debug_print (const char *func, const char *file, const int line, gboolean ne
 {
        char str_time[255];
        time_t the_time;
+       char thread_name[17] = {0,};
+
+#if defined(HAVE_PRCTL)
+       prctl(PR_GET_NAME, thread_name, 0, 0, 0);
+#elif defined(HAVE_PTHREAD_GETNAME_NP)
+       pthread_getname_np(pthread_self (), thread_name, sizeof(thread_name));
+#endif
+       if (thread_name[0] == '\0' || g_str_equal (thread_name, "pool-rhythmbox"))
+               snprintf (thread_name, sizeof (thread_name)-1, "%p", g_thread_self ());
 
        time (&the_time);
        strftime (str_time, 254, "%H:%M:%S", localtime (&the_time));
 
-       g_printerr (newline ? "(%s) [%p] [%s] %s:%d: %s\n" : "(%s) [%p] [%s] %s:%d: %s",
-                   str_time, g_thread_self (), func, file, line, buffer);
+       g_printerr (newline ? "(%s) <%s> [%s] %s:%d: %s\n" : "(%s) <%s> [%s] %s:%d: %s",
+                   str_time, thread_name, func, file, line, buffer);
 }
 
 /**
diff --git a/meson.build b/meson.build
index 882cd32f1..db5bb260c 100644
--- a/meson.build
+++ b/meson.build
@@ -145,6 +145,19 @@ cdata.set('HAVE_CFMAKERAW', have_cfmakeraw)
 
 gdk_targets = run_command(['pkg-config', '--variable', 'targets', 'gdk-3.0']).stdout().strip()
 
+have_prctl = cc.has_function('prctl', prefix: '#include <sys/prctl.h>')
+cdata.set('HAVE_PRCTL', have_prctl)
+
+have_pthread_getname_np = cc.links('''
+  #include <pthread.h>
+  int main() {
+    char nm[17];
+    pthread_getname_np(pthread_self(), nm, sizeof(nm));
+  }''', name: 'pthread_getname_np')
+cdata.set('HAVE_PTHREAD_GETNAME_NP', have_pthread_getname_np)
+
+
+
 totem_plparser_uselibcamel = run_command(['pkg-config', '--variable', 'uselibcamel', 
'totem-plparser']).stdout().strip()
 
 if gdk_targets.contains('x11')


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]