gdb_wait.cc 2.8 KB

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