common/Util.cpp |   30 +++++++++++++++++++++---------
 1 file changed, 21 insertions(+), 9 deletions(-)

New commits:
commit 584870fd7526a9e84750b027a67d2755e8f887cc
Author:     Ashod Nakashian <ashod.nakash...@collabora.co.uk>
AuthorDate: Sat Sep 21 14:38:04 2019 -0400
Commit:     Jan Holesovsky <ke...@collabora.com>
CommitDate: Thu Oct 10 14:35:15 2019 +0200

    wsd: improved logging of thread renaming
    
    The thread name helps one track threads in logs.
    When renaming threads it's important to log the process
    and previous thread name (if any), so grepping is more
    fruitful and tracking is easier.
    
    Change-Id: I47a948d77629b387cc1e9fd58fdd88e1ae1168df
    Reviewed-on: https://gerrit.libreoffice.org/79327
    Reviewed-by: Ashod Nakashian <ashnak...@gmail.com>
    Tested-by: Ashod Nakashian <ashnak...@gmail.com>
    Reviewed-on: https://gerrit.libreoffice.org/79334
    Reviewed-by: Jan Holesovsky <ke...@collabora.com>
    Tested-by: Jan Holesovsky <ke...@collabora.com>

diff --git a/common/Util.cpp b/common/Util.cpp
index 048526a49..6caab8efb 100644
--- a/common/Util.cpp
+++ b/common/Util.cpp
@@ -498,19 +498,30 @@ namespace Util
         return replace(r, "\n", " / ");
     }
 
-    static __thread char ThreadName[32] = {0};
+    // prctl(2) supports names of up to 16 characters, including 
null-termination.
+    // Although in practice on linux more than 16 chars is supported.
+    static thread_local char ThreadName[32] = {0};
+    static_assert(sizeof(ThreadName) >= 16, "ThreadName should have a 
statically known size, and not be a pointer.");
 
     void setThreadName(const std::string& s)
     {
-        strncpy(ThreadName, s.c_str(), 31);
-        ThreadName[31] = '\0';
+        // Copy the current name.
+        const std::string knownAs
+            = ThreadName[0] ? "known as [" + std::string(ThreadName) + "]" : 
"unnamed";
+
+        // Set the new name.
+        strncpy(ThreadName, s.c_str(), sizeof(ThreadName) - 1);
+        ThreadName[sizeof(ThreadName) - 1] = '\0';
 #ifdef __linux
         if (prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(s.c_str()), 0, 
0, 0) != 0)
-            LOG_SYS("Cannot set thread name of " << getThreadId() << " (" << 
std::hex <<
-                    std::this_thread::get_id() << std::dec << ") to [" << s << 
"].");
+            LOG_SYS("Cannot set thread name of "
+                    << getThreadId() << " (" << std::hex << 
std::this_thread::get_id() << std::dec
+                    << ") of process " << getpid() << " currently " << knownAs 
<< " to [" << s
+                    << "].");
         else
-            LOG_INF("Thread " << getThreadId() << " (" << std::hex <<
-                    std::this_thread::get_id() << std::dec << ") is now called 
[" << s << "].");
+            LOG_INF("Thread " << getThreadId() << " (" << std::hex << 
std::this_thread::get_id()
+                              << std::dec << ") of process " << getpid() << " 
formerly " << knownAs
+                              << " is now called [" << s << "].");
 #elif defined IOS
         [[NSThread currentThread] setName:[NSString 
stringWithUTF8String:ThreadName]];
         LOG_INF("Thread " << getThreadId() <<
@@ -524,13 +535,14 @@ namespace Util
         if (ThreadName[0] == '\0')
         {
 #ifdef __linux
+            // prctl(2): The buffer should allow space for up to 16 bytes; the 
returned string will be null-terminated.
             if (prctl(PR_GET_NAME, reinterpret_cast<unsigned 
long>(ThreadName), 0, 0, 0) != 0)
                 strncpy(ThreadName, "<noid>", sizeof(ThreadName) - 1);
 #elif defined IOS
             const char *const name = [[[NSThread currentThread] name] 
UTF8String];
-            strncpy(ThreadName, name, 31);
-            ThreadName[31] = '\0';
+            strncpy(ThreadName, name, sizeof(ThreadName) - 1);
 #endif
+            ThreadName[sizeof(ThreadName) - 1] = '\0';
         }
 
         // Avoid so many redundant system calls
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to