Home | History | Annotate | Line # | Download | only in gdbsupport
      1 /* Support code for standard wait macros in gdb_wait.h.
      2 
      3    Copyright (C) 2019-2024 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 
     21 #include "gdb_wait.h"
     22 
     23 #ifdef __MINGW32__
     24 
     25 /* The underlying idea is that when a Windows program is terminated by
     26    a fatal exception, its exit code is the value of that exception, as
     27    defined by the various EXCEPTION_* symbols in the Windows API
     28    headers.  We thus emulate WTERMSIG etc. by translating the fatal
     29    exception codes to more-or-less equivalent Posix signals.
     30 
     31    The translation below is not perfect, because a program could
     32    legitimately exit normally with a status whose value happens to
     33    have the high bits set, but that's extremely rare, to say the
     34    least, and it is deemed such a negligibly small probability of
     35    false positives is justified by the utility of reporting the
     36    terminating signal in the "normal" cases.  */
     37 
     38 # include <signal.h>
     39 
     40 # define WIN32_LEAN_AND_MEAN
     41 # include <windows.h>		/* for EXCEPTION_* constants */
     42 
     43 struct xlate_status
     44 {
     45   /* The exit status (actually, fatal exception code).  */
     46   DWORD status;
     47 
     48   /* The corresponding signal value.  */
     49   int sig;
     50 };
     51 
     52 int
     53 windows_status_to_termsig (unsigned long status)
     54 {
     55   static const xlate_status status_xlate_tbl[] =
     56     {
     57      {EXCEPTION_ACCESS_VIOLATION,	  SIGSEGV},
     58      {EXCEPTION_IN_PAGE_ERROR,		  SIGSEGV},
     59      {EXCEPTION_INVALID_HANDLE,		  SIGSEGV},
     60      {EXCEPTION_ILLEGAL_INSTRUCTION,	  SIGILL},
     61      {EXCEPTION_NONCONTINUABLE_EXCEPTION, SIGILL},
     62      {EXCEPTION_ARRAY_BOUNDS_EXCEEDED,	  SIGSEGV},
     63      {EXCEPTION_FLT_DENORMAL_OPERAND,	  SIGFPE},
     64      {EXCEPTION_FLT_DIVIDE_BY_ZERO,	  SIGFPE},
     65      {EXCEPTION_FLT_INEXACT_RESULT,	  SIGFPE},
     66      {EXCEPTION_FLT_INVALID_OPERATION,	  SIGFPE},
     67      {EXCEPTION_FLT_OVERFLOW,		  SIGFPE},
     68      {EXCEPTION_FLT_STACK_CHECK,	  SIGFPE},
     69      {EXCEPTION_FLT_UNDERFLOW,		  SIGFPE},
     70      {EXCEPTION_INT_DIVIDE_BY_ZERO,	  SIGFPE},
     71      {EXCEPTION_INT_OVERFLOW,		  SIGFPE},
     72      {EXCEPTION_PRIV_INSTRUCTION,	  SIGILL},
     73      {EXCEPTION_STACK_OVERFLOW,		  SIGSEGV},
     74      {CONTROL_C_EXIT,			  SIGTERM}
     75     };
     76 
     77   for (const xlate_status &x : status_xlate_tbl)
     78     if (x.status == status)
     79       return x.sig;
     80 
     81   return -1;
     82 }
     83 
     84 #endif	/* __MINGW32__ */
     85