Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sal / osl / w32 / signal.cxx
blob52d47da86b6cc83f6ee34f71f89da7f4a5a91019
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 }; callSignalHandler(&Info))
46 case osl_Signal_ActCallNextHdl:
47 break; // Fall through to call the next handler
49 case osl_Signal_ActAbortApp:
50 abort();
51 break;
53 case osl_Signal_ActKillApp:
54 _exit(255);
55 break;
57 default:
58 return TRUE; // do not call the next handler
60 [[fallthrough]];
61 default:
62 return FALSE; // call the next handler
66 LPTOP_LEVEL_EXCEPTION_FILTER pPreviousHandler = nullptr;
69 bool onInitSignal()
71 pPreviousHandler = SetUnhandledExceptionFilter(signalHandlerFunction);
72 SetConsoleCtrlHandler(CtrlHandlerFunction, TRUE);
74 WerAddExcludedApplication(L"SOFFICE.EXE", FALSE);
76 return true;
79 bool onDeInitSignal()
81 SetConsoleCtrlHandler(CtrlHandlerFunction, FALSE);
82 SetUnhandledExceptionFilter(pPreviousHandler);
84 return false;
87 namespace
89 /* magic Microsoft C++ compiler exception constant */
90 #define EXCEPTION_MSC_CPP_EXCEPTION 0xe06d7363
92 long WINAPI signalHandlerFunction(LPEXCEPTION_POINTERS lpEP)
94 #if HAVE_FEATURE_BREAKPAD
95 // we should make sure to call the breakpad handler as
96 // first step when we hit a problem
97 if (pPreviousHandler)
98 pPreviousHandler(lpEP);
99 #endif
101 static bool bNested = false;
103 oslSignalInfo info;
105 info.UserSignal = lpEP->ExceptionRecord->ExceptionCode;
106 info.UserData = nullptr;
108 switch (lpEP->ExceptionRecord->ExceptionCode)
110 /* Transform unhandled exceptions into access violations.
111 Microsoft C++ compiler (add more for other compilers if necessary).
113 case EXCEPTION_MSC_CPP_EXCEPTION:
114 case EXCEPTION_ACCESS_VIOLATION:
115 info.Signal = osl_Signal_AccessViolation;
116 break;
118 case EXCEPTION_INT_DIVIDE_BY_ZERO:
119 info.Signal = osl_Signal_IntegerDivideByZero;
120 break;
122 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
123 info.Signal = osl_Signal_FloatDivideByZero;
124 break;
126 case EXCEPTION_BREAKPOINT:
127 info.Signal = osl_Signal_DebugBreak;
128 break;
130 default:
131 info.Signal = osl_Signal_System;
132 break;
135 oslSignalAction action;
137 if (!bNested)
139 bNested = true;
140 action = callSignalHandler(&info);
142 else
143 action = osl_Signal_ActKillApp;
145 switch (action)
147 case osl_Signal_ActCallNextHdl:
148 return EXCEPTION_CONTINUE_SEARCH;
150 case osl_Signal_ActAbortApp:
151 return EXCEPTION_EXECUTE_HANDLER;
153 case osl_Signal_ActKillApp:
154 SetErrorMode(SEM_NOGPFAULTERRORBOX);
155 exit(255);
156 break;
157 default:
158 break;
161 return EXCEPTION_CONTINUE_EXECUTION;
165 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */