]> git.sesse.net Git - casparcg/blob - common/os/linux/signal_handlers.cpp
e87cc69e7c4d535582c90f3178927c4bdeb3b914
[casparcg] / common / os / linux / signal_handlers.cpp
1 #include "../general_protection_fault.h"
2
3 #include "../../except.h"
4 #include "../../log.h"
5 #include "../../thread_info.h"
6
7 #include <signal.h>
8
9 namespace caspar {
10
11 struct floating_point_exception : virtual caspar_exception {};
12 struct segmentation_fault_exception : virtual caspar_exception {};
13
14 void catch_fpe(int signum)
15 {
16         try
17         {
18                 CASPAR_THROW_EXCEPTION(floating_point_exception());
19         }
20         catch (...)
21         {
22                 CASPAR_LOG_CURRENT_EXCEPTION();
23                 throw;
24         }
25 }
26
27 void catch_segv(int signum)
28 {
29         try
30         {
31                 CASPAR_THROW_EXCEPTION(segmentation_fault_exception());
32         }
33         catch (...)
34         {
35                 CASPAR_LOG_CURRENT_EXCEPTION();
36                 throw;
37         }
38 }
39
40 void do_install_handlers()
41 {
42         signal(SIGFPE, catch_fpe);
43         signal(SIGSEGV, catch_segv);
44 }
45
46 void install_gpf_handler()
47 {
48         ensure_gpf_handler_installed_for_thread();
49 }
50
51 void ensure_gpf_handler_installed_for_thread(
52                 const char* thread_description)
53 {
54         static auto install = []() { do_install_handlers(); return 0; } ();
55         
56         if (thread_description)
57                 get_thread_info().name = thread_description;
58 }
59
60 }