Get the style color and number just once
[LibreOffice.git] / sal / osl / w32 / signal.cxx
blob2d04e59269dee8466521c80615ef700d72083147
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <stdlib.h>
24 #include <config_features.h>
26 #include <signalshared.hxx>
28 #include <systools/win32/uwinapi.h>
29 #include <errorrep.h>
30 #include <werapi.h>
32 namespace
34 long WINAPI signalHandlerFunction(LPEXCEPTION_POINTERS lpEP);
36 // Similar to SIGINT handler in sal/osl/unx/signal.cxx
37 BOOL WINAPI CtrlHandlerFunction(DWORD dwCtrlType)
39 switch (dwCtrlType)
41 case CTRL_C_EVENT:
42 case CTRL_BREAK_EVENT:
43 case CTRL_CLOSE_EVENT:
44 switch (oslSignalInfo Info{ osl_Signal_Terminate, 0, nullptr };
45 callSignalHandler(&Info))
47 case osl_Signal_ActCallNextHdl:
48 break; // Fall through to call the next handler
50 case osl_Signal_ActAbortApp:
51 abort();
52 break;
54 case osl_Signal_ActKillApp:
55 _exit(255);
56 break;
58 default:
59 return TRUE; // do not call the next handler
61 [[fallthrough]];
62 default:
63 return FALSE; // call the next handler
67 LPTOP_LEVEL_EXCEPTION_FILTER pPreviousHandler = nullptr;
70 bool onInitSignal()
72 pPreviousHandler = SetUnhandledExceptionFilter(signalHandlerFunction);
73 SetConsoleCtrlHandler(CtrlHandlerFunction, TRUE);
75 WerAddExcludedApplication(L"SOFFICE.EXE", FALSE);
77 return true;
80 bool onDeInitSignal()
82 SetConsoleCtrlHandler(CtrlHandlerFunction, FALSE);
83 SetUnhandledExceptionFilter(pPreviousHandler);
85 return false;
88 namespace
90 /* magic Microsoft C++ compiler exception constant */
91 #define EXCEPTION_MSC_CPP_EXCEPTION 0xe06d7363
93 long WINAPI signalHandlerFunction(LPEXCEPTION_POINTERS lpEP)
95 #if HAVE_FEATURE_BREAKPAD
96 // we should make sure to call the breakpad handler as
97 // first step when we hit a problem
98 if (pPreviousHandler)
99 pPreviousHandler(lpEP);
100 #endif
102 static bool bNested = false;
104 oslSignalInfo info;
106 info.UserSignal = lpEP->ExceptionRecord->ExceptionCode;
107 info.UserData = nullptr;
109 switch (lpEP->ExceptionRecord->ExceptionCode)
111 /* Transform unhandled exceptions into access violations.
112 Microsoft C++ compiler (add more for other compilers if necessary).
114 case EXCEPTION_MSC_CPP_EXCEPTION:
115 case EXCEPTION_ACCESS_VIOLATION:
116 info.Signal = osl_Signal_AccessViolation;
117 break;
119 case EXCEPTION_INT_DIVIDE_BY_ZERO:
120 info.Signal = osl_Signal_IntegerDivideByZero;
121 break;
123 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
124 info.Signal = osl_Signal_FloatDivideByZero;
125 break;
127 case EXCEPTION_BREAKPOINT:
128 info.Signal = osl_Signal_DebugBreak;
129 break;
131 default:
132 info.Signal = osl_Signal_System;
133 break;
136 oslSignalAction action;
138 if (!bNested)
140 bNested = true;
141 action = callSignalHandler(&info);
143 else
144 action = osl_Signal_ActKillApp;
146 switch (action)
148 case osl_Signal_ActCallNextHdl:
149 return EXCEPTION_CONTINUE_SEARCH;
151 case osl_Signal_ActAbortApp:
152 return EXCEPTION_EXECUTE_HANDLER;
154 case osl_Signal_ActKillApp:
155 SetErrorMode(SEM_NOGPFAULTERRORBOX);
156 exit(255);
157 break;
158 default:
159 break;
162 return EXCEPTION_CONTINUE_EXECUTION;
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */