1 /* Support code for standard wait macros in gdb_wait.h.
2 
3    Copyright (C) 2019-2021 Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19 
20 #include "common-defs.h"
21 
22 #include "gdb_wait.h"
23 
24 #ifdef __MINGW32__
25 
26 /* The underlying idea is that when a Windows program is terminated by
27    a fatal exception, its exit code is the value of that exception, as
28    defined by the various EXCEPTION_* symbols in the Windows API
29    headers.  We thus emulate WTERMSIG etc. by translating the fatal
30    exception codes to more-or-less equivalent Posix signals.
31 
32    The translation below is not perfect, because a program could
33    legitimately exit normally with a status whose value happens to
34    have the high bits set, but that's extremely rare, to say the
35    least, and it is deemed such a negligibly small probability of
36    false positives is justified by the utility of reporting the
37    terminating signal in the "normal" cases.  */
38 
39 # include <signal.h>
40 
41 # define WIN32_LEAN_AND_MEAN
42 # include <windows.h>		/* for EXCEPTION_* constants */
43 
44 struct xlate_status
45 {
46   /* The exit status (actually, fatal exception code).  */
47   DWORD status;
48 
49   /* The corresponding signal value.  */
50   int sig;
51 };
52 
53 int
windows_status_to_termsig(unsigned long status)54 windows_status_to_termsig (unsigned long status)
55 {
56   static const xlate_status status_xlate_tbl[] =
57     {
58      {EXCEPTION_ACCESS_VIOLATION,	  SIGSEGV},
59      {EXCEPTION_IN_PAGE_ERROR,		  SIGSEGV},
60      {EXCEPTION_INVALID_HANDLE,		  SIGSEGV},
61      {EXCEPTION_ILLEGAL_INSTRUCTION,	  SIGILL},
62      {EXCEPTION_NONCONTINUABLE_EXCEPTION, SIGILL},
63      {EXCEPTION_ARRAY_BOUNDS_EXCEEDED,	  SIGSEGV},
64      {EXCEPTION_FLT_DENORMAL_OPERAND,	  SIGFPE},
65      {EXCEPTION_FLT_DIVIDE_BY_ZERO,	  SIGFPE},
66      {EXCEPTION_FLT_INEXACT_RESULT,	  SIGFPE},
67      {EXCEPTION_FLT_INVALID_OPERATION,	  SIGFPE},
68      {EXCEPTION_FLT_OVERFLOW,		  SIGFPE},
69      {EXCEPTION_FLT_STACK_CHECK,	  SIGFPE},
70      {EXCEPTION_FLT_UNDERFLOW,		  SIGFPE},
71      {EXCEPTION_INT_DIVIDE_BY_ZERO,	  SIGFPE},
72      {EXCEPTION_INT_OVERFLOW,		  SIGFPE},
73      {EXCEPTION_PRIV_INSTRUCTION,	  SIGILL},
74      {EXCEPTION_STACK_OVERFLOW,		  SIGSEGV},
75      {CONTROL_C_EXIT,			  SIGTERM}
76     };
77 
78   for (const xlate_status &x : status_xlate_tbl)
79     if (x.status == status)
80       return x.sig;
81 
82   return -1;
83 }
84 
85 #endif	/* __MINGW32__ */
86