xref: /netbsd-src/external/gpl3/gdb/dist/sim/common/sim-signal.c (revision 88241920d21b339bf319c0e979ffda80c49a2936)
14e98e3e1Schristos /* Simulator signal support
2*88241920Schristos    Copyright (C) 1997-2024 Free Software Foundation, Inc.
34e98e3e1Schristos    Contributed by Cygnus Support
44e98e3e1Schristos 
54e98e3e1Schristos This file is part of the GNU Simulators.
64e98e3e1Schristos 
74e98e3e1Schristos This program is free software; you can redistribute it and/or modify
84e98e3e1Schristos it under the terms of the GNU General Public License as published by
94e98e3e1Schristos the Free Software Foundation; either version 3 of the License, or
104e98e3e1Schristos (at your option) any later version.
114e98e3e1Schristos 
124e98e3e1Schristos This program is distributed in the hope that it will be useful,
134e98e3e1Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
144e98e3e1Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
154e98e3e1Schristos GNU General Public License for more details.
164e98e3e1Schristos 
174e98e3e1Schristos You should have received a copy of the GNU General Public License
184e98e3e1Schristos along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
194e98e3e1Schristos 
204b169a6bSchristos /* This must come before any other includes.  */
214b169a6bSchristos #include "defs.h"
224b169a6bSchristos 
234e98e3e1Schristos #include <signal.h>
244b169a6bSchristos 
25*88241920Schristos #include "sim/sim.h"
26*88241920Schristos #include "sim-io.h"
274b169a6bSchristos #include "sim-signal.h"
284e98e3e1Schristos 
294e98e3e1Schristos /* Convert SIM_SIGFOO to SIGFOO.
304e98e3e1Schristos    What to do when the host doesn't have SIGFOO is handled on a case by case
314e98e3e1Schristos    basis.  Generally, in the case of passing a value back to gdb, we want gdb
324e98e3e1Schristos    to not think the process has died (so it can be debugged at the point of
334e98e3e1Schristos    failure).  */
344e98e3e1Schristos 
354e98e3e1Schristos #ifdef _WIN32
364e98e3e1Schristos #ifndef SIGTRAP
374e98e3e1Schristos #define SIGTRAP 5
384e98e3e1Schristos #endif
394e98e3e1Schristos #ifndef SIGBUS
404e98e3e1Schristos #define SIGBUS 10
414e98e3e1Schristos #endif
424e98e3e1Schristos #ifndef SIGQUIT
434e98e3e1Schristos #define SIGQUIT 3
444e98e3e1Schristos #endif
454e98e3e1Schristos #endif
464e98e3e1Schristos 
474e98e3e1Schristos int
484e98e3e1Schristos sim_signal_to_host (SIM_DESC sd, SIM_SIGNAL sig)
494e98e3e1Schristos {
504e98e3e1Schristos   switch (sig)
514e98e3e1Schristos     {
524e98e3e1Schristos     case SIM_SIGINT :
534e98e3e1Schristos       return SIGINT;
544e98e3e1Schristos 
554e98e3e1Schristos     case SIM_SIGABRT :
564e98e3e1Schristos       return SIGABRT;
574e98e3e1Schristos 
584e98e3e1Schristos     case SIM_SIGILL :
594e98e3e1Schristos #ifdef SIGILL
604e98e3e1Schristos       return SIGILL;
614e98e3e1Schristos #else
624e98e3e1Schristos       return SIGSEGV;
634e98e3e1Schristos #endif
644e98e3e1Schristos 
654e98e3e1Schristos     case SIM_SIGTRAP :
664e98e3e1Schristos       return SIGTRAP;
674e98e3e1Schristos 
684e98e3e1Schristos     case SIM_SIGBUS :
694e98e3e1Schristos #ifdef SIGBUS
704e98e3e1Schristos       return SIGBUS;
714e98e3e1Schristos #else
724e98e3e1Schristos       return SIGSEGV;
734e98e3e1Schristos #endif
744e98e3e1Schristos 
754e98e3e1Schristos     case SIM_SIGSEGV :
764e98e3e1Schristos       return SIGSEGV;
774e98e3e1Schristos 
784e98e3e1Schristos     case SIM_SIGXCPU :
794e98e3e1Schristos #ifdef SIGXCPU
804e98e3e1Schristos       return SIGXCPU;
814e98e3e1Schristos #endif
824e98e3e1Schristos       break;
834e98e3e1Schristos 
844e98e3e1Schristos     case SIM_SIGFPE:
854e98e3e1Schristos #ifdef SIGFPE
864e98e3e1Schristos       return SIGFPE;
874e98e3e1Schristos #endif
884e98e3e1Schristos       break;
894e98e3e1Schristos 
904e98e3e1Schristos     case SIM_SIGNONE:
914e98e3e1Schristos       return 0;
924e98e3e1Schristos       break;
934e98e3e1Schristos     }
944e98e3e1Schristos 
954e98e3e1Schristos   sim_io_eprintf (sd, "sim_signal_to_host: unknown signal: %d\n", sig);
964e98e3e1Schristos #ifdef SIGHUP
974e98e3e1Schristos   return SIGHUP;  /* FIXME: Suggestions?  */
984e98e3e1Schristos #else
994e98e3e1Schristos   return 1;
1004e98e3e1Schristos #endif
1014e98e3e1Schristos }
1024e98e3e1Schristos 
103a2e2270fSchristos enum gdb_signal
104a2e2270fSchristos sim_signal_to_gdb_signal (SIM_DESC sd, SIM_SIGNAL sig)
1054e98e3e1Schristos {
1064e98e3e1Schristos   switch (sig)
1074e98e3e1Schristos     {
1084e98e3e1Schristos     case SIM_SIGINT :
109a2e2270fSchristos       return GDB_SIGNAL_INT;
1104e98e3e1Schristos 
1114e98e3e1Schristos     case SIM_SIGABRT :
112a2e2270fSchristos       return GDB_SIGNAL_ABRT;
1134e98e3e1Schristos 
1144e98e3e1Schristos     case SIM_SIGILL :
115a2e2270fSchristos       return GDB_SIGNAL_ILL;
1164e98e3e1Schristos 
1174e98e3e1Schristos     case SIM_SIGTRAP :
118a2e2270fSchristos       return GDB_SIGNAL_TRAP;
1194e98e3e1Schristos 
1204e98e3e1Schristos     case SIM_SIGBUS :
121a2e2270fSchristos       return GDB_SIGNAL_BUS;
1224e98e3e1Schristos 
1234e98e3e1Schristos     case SIM_SIGSEGV :
124a2e2270fSchristos       return GDB_SIGNAL_SEGV;
1254e98e3e1Schristos 
1264e98e3e1Schristos     case SIM_SIGXCPU :
127a2e2270fSchristos       return GDB_SIGNAL_XCPU;
1284e98e3e1Schristos 
1294e98e3e1Schristos     case SIM_SIGFPE:
130a2e2270fSchristos       return GDB_SIGNAL_FPE;
1314e98e3e1Schristos       break;
1324e98e3e1Schristos 
1334e98e3e1Schristos     case SIM_SIGNONE:
134a2e2270fSchristos       return GDB_SIGNAL_0;
1354e98e3e1Schristos       break;
1364e98e3e1Schristos     }
1374e98e3e1Schristos 
1384e98e3e1Schristos   sim_io_eprintf (sd, "sim_signal_to_host: unknown signal: %d\n", sig);
139a2e2270fSchristos   return GDB_SIGNAL_HUP;
1404e98e3e1Schristos }
141