xref: /netbsd-src/external/gpl3/gdb/dist/gdbsupport/gdb_wait.cc (revision 5ba1f45f2a09259cc846f20c7c5501604d633c90)
18dffb485Schristos /* Support code for standard wait macros in gdb_wait.h.
28dffb485Schristos 
3*5ba1f45fSchristos    Copyright (C) 2019-2024 Free Software Foundation, Inc.
48dffb485Schristos 
58dffb485Schristos    This file is part of GDB.
68dffb485Schristos 
78dffb485Schristos    This program is free software; you can redistribute it and/or modify
88dffb485Schristos    it under the terms of the GNU General Public License as published by
98dffb485Schristos    the Free Software Foundation; either version 3 of the License, or
108dffb485Schristos    (at your option) any later version.
118dffb485Schristos 
128dffb485Schristos    This program is distributed in the hope that it will be useful,
138dffb485Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
148dffb485Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
158dffb485Schristos    GNU General Public License for more details.
168dffb485Schristos 
178dffb485Schristos    You should have received a copy of the GNU General Public License
188dffb485Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
198dffb485Schristos 
208dffb485Schristos 
218dffb485Schristos #include "gdb_wait.h"
228dffb485Schristos 
238dffb485Schristos #ifdef __MINGW32__
248dffb485Schristos 
258dffb485Schristos /* The underlying idea is that when a Windows program is terminated by
268dffb485Schristos    a fatal exception, its exit code is the value of that exception, as
278dffb485Schristos    defined by the various EXCEPTION_* symbols in the Windows API
288dffb485Schristos    headers.  We thus emulate WTERMSIG etc. by translating the fatal
298dffb485Schristos    exception codes to more-or-less equivalent Posix signals.
308dffb485Schristos 
318dffb485Schristos    The translation below is not perfect, because a program could
328dffb485Schristos    legitimately exit normally with a status whose value happens to
338dffb485Schristos    have the high bits set, but that's extremely rare, to say the
348dffb485Schristos    least, and it is deemed such a negligibly small probability of
358dffb485Schristos    false positives is justified by the utility of reporting the
368dffb485Schristos    terminating signal in the "normal" cases.  */
378dffb485Schristos 
388dffb485Schristos # include <signal.h>
398dffb485Schristos 
408dffb485Schristos # define WIN32_LEAN_AND_MEAN
418dffb485Schristos # include <windows.h>		/* for EXCEPTION_* constants */
428dffb485Schristos 
438dffb485Schristos struct xlate_status
448dffb485Schristos {
458dffb485Schristos   /* The exit status (actually, fatal exception code).  */
468dffb485Schristos   DWORD status;
478dffb485Schristos 
488dffb485Schristos   /* The corresponding signal value.  */
498dffb485Schristos   int sig;
508dffb485Schristos };
518dffb485Schristos 
528dffb485Schristos int
538dffb485Schristos windows_status_to_termsig (unsigned long status)
548dffb485Schristos {
558dffb485Schristos   static const xlate_status status_xlate_tbl[] =
568dffb485Schristos     {
578dffb485Schristos      {EXCEPTION_ACCESS_VIOLATION,	  SIGSEGV},
588dffb485Schristos      {EXCEPTION_IN_PAGE_ERROR,		  SIGSEGV},
598dffb485Schristos      {EXCEPTION_INVALID_HANDLE,		  SIGSEGV},
608dffb485Schristos      {EXCEPTION_ILLEGAL_INSTRUCTION,	  SIGILL},
618dffb485Schristos      {EXCEPTION_NONCONTINUABLE_EXCEPTION, SIGILL},
628dffb485Schristos      {EXCEPTION_ARRAY_BOUNDS_EXCEEDED,	  SIGSEGV},
638dffb485Schristos      {EXCEPTION_FLT_DENORMAL_OPERAND,	  SIGFPE},
648dffb485Schristos      {EXCEPTION_FLT_DIVIDE_BY_ZERO,	  SIGFPE},
658dffb485Schristos      {EXCEPTION_FLT_INEXACT_RESULT,	  SIGFPE},
668dffb485Schristos      {EXCEPTION_FLT_INVALID_OPERATION,	  SIGFPE},
678dffb485Schristos      {EXCEPTION_FLT_OVERFLOW,		  SIGFPE},
688dffb485Schristos      {EXCEPTION_FLT_STACK_CHECK,	  SIGFPE},
698dffb485Schristos      {EXCEPTION_FLT_UNDERFLOW,		  SIGFPE},
708dffb485Schristos      {EXCEPTION_INT_DIVIDE_BY_ZERO,	  SIGFPE},
718dffb485Schristos      {EXCEPTION_INT_OVERFLOW,		  SIGFPE},
728dffb485Schristos      {EXCEPTION_PRIV_INSTRUCTION,	  SIGILL},
738dffb485Schristos      {EXCEPTION_STACK_OVERFLOW,		  SIGSEGV},
748dffb485Schristos      {CONTROL_C_EXIT,			  SIGTERM}
758dffb485Schristos     };
768dffb485Schristos 
778dffb485Schristos   for (const xlate_status &x : status_xlate_tbl)
788dffb485Schristos     if (x.status == status)
798dffb485Schristos       return x.sig;
808dffb485Schristos 
818dffb485Schristos   return -1;
828dffb485Schristos }
838dffb485Schristos 
848dffb485Schristos #endif	/* __MINGW32__ */
85