]> git.sesse.net Git - casparcg/blobdiff - common/os/linux/signal_handlers.cpp
[linux] Don't overwrite thread name if it is already set.
[casparcg] / common / os / linux / signal_handlers.cpp
index 5df4f469b0138d149805719fc60cd04023796e6f..27d74b6efca9e9c33f8ab6a48bc3a7f42c501d75 100644 (file)
@@ -1,8 +1,11 @@
 #include "../general_protection_fault.h"
 
 #include "../../except.h"
+#include "../../log.h"
+#include "../../thread_info.h"
 
 #include <signal.h>
+#include <pthread.h>
 
 namespace caspar {
 
@@ -11,12 +14,28 @@ struct segmentation_fault_exception : virtual caspar_exception {};
 
 void catch_fpe(int signum)
 {
-       CASPAR_THROW_EXCEPTION(floating_point_exception());
+       try
+       {
+               CASPAR_THROW_EXCEPTION(floating_point_exception());
+       }
+       catch (...)
+       {
+               CASPAR_LOG_CURRENT_EXCEPTION();
+               throw;
+       }
 }
 
 void catch_segv(int signum)
 {
-       CASPAR_THROW_EXCEPTION(segmentation_fault_exception());
+       try
+       {
+               CASPAR_THROW_EXCEPTION(segmentation_fault_exception());
+       }
+       catch (...)
+       {
+               CASPAR_LOG_CURRENT_EXCEPTION();
+               throw;
+       }
 }
 
 void do_install_handlers()
@@ -33,7 +52,25 @@ void install_gpf_handler()
 void ensure_gpf_handler_installed_for_thread(
                const char* thread_description)
 {
+       static const int MAX_LINUX_THREAD_NAME_LEN = 15;
        static auto install = []() { do_install_handlers(); return 0; } ();
+
+       auto& for_thread = get_thread_info();
+       
+       if (thread_description && for_thread.name.empty())
+       {
+               for_thread.name = thread_description;
+
+               if (std::strlen(thread_description) > MAX_LINUX_THREAD_NAME_LEN)
+               {
+                       char truncated[MAX_LINUX_THREAD_NAME_LEN + 1];
+                       std::memcpy(truncated, thread_description, MAX_LINUX_THREAD_NAME_LEN);
+                       truncated[MAX_LINUX_THREAD_NAME_LEN] = 0;
+                       pthread_setname_np(pthread_self(), truncated);
+               }
+               else
+                       pthread_setname_np(pthread_self(), thread_description);
+       }
 }
 
 }