testsuite: skip confirmation in 'gdb_reinitialize_dir'
[binutils-gdb.git] / gdbsupport / signals.cc
blob2cf641bd9e2fb0d996c23cd5aba23b857d815d95
1 /* Target signal translation functions for GDB.
2 Copyright (C) 1990-2024 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
5 This file is part of GDB.
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.
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.
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/>. */
21 #ifdef HAVE_SIGNAL_H
22 #include <signal.h>
23 #endif
25 #include "gdb_signals.h"
27 struct gdbarch;
29 /* Always use __SIGRTMIN if it's available. SIGRTMIN is the lowest
30 _available_ realtime signal, not the lowest supported; glibc takes
31 several for its own use. */
33 #if defined(__SIGRTMIN)
34 # define REALTIME_LO __SIGRTMIN
35 # define REALTIME_HI (__SIGRTMAX + 1)
36 #elif defined(SIGRTMIN)
37 # define REALTIME_LO SIGRTMIN
38 # define REALTIME_HI (SIGRTMAX + 1)
39 #endif
41 /* This table must match in order and size the signals in enum
42 gdb_signal. */
44 static const struct {
45 const char *symbol;
46 const char *name;
47 const char *string;
48 } signals [] =
50 #define SET(symbol, constant, name, string) { #symbol, name, string },
51 #include "gdb/signals.def"
52 #undef SET
55 const char *
56 gdb_signal_to_symbol_string (enum gdb_signal sig)
58 gdb_assert ((int) sig >= GDB_SIGNAL_FIRST && (int) sig <= GDB_SIGNAL_LAST);
60 return signals[sig].symbol;
63 /* Return the string for a signal. */
64 const char *
65 gdb_signal_to_string (enum gdb_signal sig)
67 if ((int) sig >= GDB_SIGNAL_FIRST && (int) sig <= GDB_SIGNAL_LAST)
68 return signals[sig].string;
69 else
70 return signals[GDB_SIGNAL_UNKNOWN].string;
73 /* Return the name for a signal. */
74 const char *
75 gdb_signal_to_name (enum gdb_signal sig)
77 if ((int) sig >= GDB_SIGNAL_FIRST && (int) sig <= GDB_SIGNAL_LAST
78 && signals[sig].name != NULL)
79 return signals[sig].name;
80 else
81 /* I think the code which prints this will always print it along
82 with the string, so no need to be verbose (very old comment). */
83 return "?";
86 /* Given a name, return its signal. */
87 enum gdb_signal
88 gdb_signal_from_name (const char *name)
90 enum gdb_signal sig;
92 /* It's possible we also should allow "SIGCLD" as well as "SIGCHLD"
93 for GDB_SIGNAL_SIGCHLD. SIGIOT, on the other hand, is more
94 questionable; seems like by now people should call it SIGABRT
95 instead. */
97 /* This ugly cast brought to you by the native VAX compiler. */
98 for (sig = GDB_SIGNAL_HUP;
99 sig < GDB_SIGNAL_LAST;
100 sig = (enum gdb_signal) ((int) sig + 1))
101 if (signals[sig].name != NULL
102 && strcmp (name, signals[sig].name) == 0)
103 return sig;
104 return GDB_SIGNAL_UNKNOWN;
107 /* The following functions are to help certain targets deal
108 with the signal/waitstatus stuff. They could just as well be in
109 a file called native-utils.c or unixwaitstatus-utils.c or whatever. */
111 /* Convert host signal to our signals. */
112 enum gdb_signal
113 gdb_signal_from_host (int hostsig)
115 /* A switch statement would make sense but would require special
116 kludges to deal with the cases where more than one signal has the
117 same number. Signals are ordered ANSI-standard signals first,
118 other signals second, with signals in each block ordered by their
119 numerical values on a typical POSIX platform. */
121 if (hostsig == 0)
122 return GDB_SIGNAL_0;
124 /* SIGINT, SIGILL, SIGABRT, SIGFPE, SIGSEGV and SIGTERM
125 are ANSI-standard signals and are always available. */
126 if (hostsig == SIGINT)
127 return GDB_SIGNAL_INT;
128 if (hostsig == SIGILL)
129 return GDB_SIGNAL_ILL;
130 if (hostsig == SIGABRT)
131 return GDB_SIGNAL_ABRT;
132 if (hostsig == SIGFPE)
133 return GDB_SIGNAL_FPE;
134 if (hostsig == SIGSEGV)
135 return GDB_SIGNAL_SEGV;
136 if (hostsig == SIGTERM)
137 return GDB_SIGNAL_TERM;
139 /* All other signals need preprocessor conditionals. */
140 #if defined (SIGHUP)
141 if (hostsig == SIGHUP)
142 return GDB_SIGNAL_HUP;
143 #endif
144 #if defined (SIGQUIT)
145 if (hostsig == SIGQUIT)
146 return GDB_SIGNAL_QUIT;
147 #endif
148 #if defined (SIGTRAP)
149 if (hostsig == SIGTRAP)
150 return GDB_SIGNAL_TRAP;
151 #endif
152 #if defined (SIGEMT)
153 if (hostsig == SIGEMT)
154 return GDB_SIGNAL_EMT;
155 #endif
156 #if defined (SIGKILL)
157 if (hostsig == SIGKILL)
158 return GDB_SIGNAL_KILL;
159 #endif
160 #if defined (SIGBUS)
161 if (hostsig == SIGBUS)
162 return GDB_SIGNAL_BUS;
163 #endif
164 #if defined (SIGSYS)
165 if (hostsig == SIGSYS)
166 return GDB_SIGNAL_SYS;
167 #endif
168 #if defined (SIGPIPE)
169 if (hostsig == SIGPIPE)
170 return GDB_SIGNAL_PIPE;
171 #endif
172 #if defined (SIGALRM)
173 if (hostsig == SIGALRM)
174 return GDB_SIGNAL_ALRM;
175 #endif
176 #if defined (SIGUSR1)
177 if (hostsig == SIGUSR1)
178 return GDB_SIGNAL_USR1;
179 #endif
180 #if defined (SIGUSR2)
181 if (hostsig == SIGUSR2)
182 return GDB_SIGNAL_USR2;
183 #endif
184 #if defined (SIGCLD)
185 if (hostsig == SIGCLD)
186 return GDB_SIGNAL_CHLD;
187 #endif
188 #if defined (SIGCHLD)
189 if (hostsig == SIGCHLD)
190 return GDB_SIGNAL_CHLD;
191 #endif
192 #if defined (SIGPWR)
193 if (hostsig == SIGPWR)
194 return GDB_SIGNAL_PWR;
195 #endif
196 #if defined (SIGWINCH)
197 if (hostsig == SIGWINCH)
198 return GDB_SIGNAL_WINCH;
199 #endif
200 #if defined (SIGURG)
201 if (hostsig == SIGURG)
202 return GDB_SIGNAL_URG;
203 #endif
204 #if defined (SIGIO)
205 if (hostsig == SIGIO)
206 return GDB_SIGNAL_IO;
207 #endif
208 #if defined (SIGPOLL)
209 if (hostsig == SIGPOLL)
210 return GDB_SIGNAL_POLL;
211 #endif
212 #if defined (SIGSTOP)
213 if (hostsig == SIGSTOP)
214 return GDB_SIGNAL_STOP;
215 #endif
216 #if defined (SIGTSTP)
217 if (hostsig == SIGTSTP)
218 return GDB_SIGNAL_TSTP;
219 #endif
220 #if defined (SIGCONT)
221 if (hostsig == SIGCONT)
222 return GDB_SIGNAL_CONT;
223 #endif
224 #if defined (SIGTTIN)
225 if (hostsig == SIGTTIN)
226 return GDB_SIGNAL_TTIN;
227 #endif
228 #if defined (SIGTTOU)
229 if (hostsig == SIGTTOU)
230 return GDB_SIGNAL_TTOU;
231 #endif
232 #if defined (SIGVTALRM)
233 if (hostsig == SIGVTALRM)
234 return GDB_SIGNAL_VTALRM;
235 #endif
236 #if defined (SIGPROF)
237 if (hostsig == SIGPROF)
238 return GDB_SIGNAL_PROF;
239 #endif
240 #if defined (SIGXCPU)
241 if (hostsig == SIGXCPU)
242 return GDB_SIGNAL_XCPU;
243 #endif
244 #if defined (SIGXFSZ)
245 if (hostsig == SIGXFSZ)
246 return GDB_SIGNAL_XFSZ;
247 #endif
248 #if defined (SIGWIND)
249 if (hostsig == SIGWIND)
250 return GDB_SIGNAL_WIND;
251 #endif
252 #if defined (SIGPHONE)
253 if (hostsig == SIGPHONE)
254 return GDB_SIGNAL_PHONE;
255 #endif
256 #if defined (SIGLOST)
257 if (hostsig == SIGLOST)
258 return GDB_SIGNAL_LOST;
259 #endif
260 #if defined (SIGWAITING)
261 if (hostsig == SIGWAITING)
262 return GDB_SIGNAL_WAITING;
263 #endif
264 #if defined (SIGCANCEL)
265 if (hostsig == SIGCANCEL)
266 return GDB_SIGNAL_CANCEL;
267 #endif
268 #if defined (SIGLWP)
269 if (hostsig == SIGLWP)
270 return GDB_SIGNAL_LWP;
271 #endif
272 #if defined (SIGDANGER)
273 if (hostsig == SIGDANGER)
274 return GDB_SIGNAL_DANGER;
275 #endif
276 #if defined (SIGGRANT)
277 if (hostsig == SIGGRANT)
278 return GDB_SIGNAL_GRANT;
279 #endif
280 #if defined (SIGRETRACT)
281 if (hostsig == SIGRETRACT)
282 return GDB_SIGNAL_RETRACT;
283 #endif
284 #if defined (SIGMSG)
285 if (hostsig == SIGMSG)
286 return GDB_SIGNAL_MSG;
287 #endif
288 #if defined (SIGSOUND)
289 if (hostsig == SIGSOUND)
290 return GDB_SIGNAL_SOUND;
291 #endif
292 #if defined (SIGSAK)
293 if (hostsig == SIGSAK)
294 return GDB_SIGNAL_SAK;
295 #endif
296 #if defined (SIGPRIO)
297 if (hostsig == SIGPRIO)
298 return GDB_SIGNAL_PRIO;
299 #endif
301 /* Mach exceptions. Assumes that the values for EXC_ are positive! */
302 #if defined (EXC_BAD_ACCESS) && defined (_NSIG)
303 if (hostsig == _NSIG + EXC_BAD_ACCESS)
304 return GDB_EXC_BAD_ACCESS;
305 #endif
306 #if defined (EXC_BAD_INSTRUCTION) && defined (_NSIG)
307 if (hostsig == _NSIG + EXC_BAD_INSTRUCTION)
308 return GDB_EXC_BAD_INSTRUCTION;
309 #endif
310 #if defined (EXC_ARITHMETIC) && defined (_NSIG)
311 if (hostsig == _NSIG + EXC_ARITHMETIC)
312 return GDB_EXC_ARITHMETIC;
313 #endif
314 #if defined (EXC_EMULATION) && defined (_NSIG)
315 if (hostsig == _NSIG + EXC_EMULATION)
316 return GDB_EXC_EMULATION;
317 #endif
318 #if defined (EXC_SOFTWARE) && defined (_NSIG)
319 if (hostsig == _NSIG + EXC_SOFTWARE)
320 return GDB_EXC_SOFTWARE;
321 #endif
322 #if defined (EXC_BREAKPOINT) && defined (_NSIG)
323 if (hostsig == _NSIG + EXC_BREAKPOINT)
324 return GDB_EXC_BREAKPOINT;
325 #endif
327 #if defined (SIGINFO)
328 if (hostsig == SIGINFO)
329 return GDB_SIGNAL_INFO;
330 #endif
331 #if defined (SIGLIBRT)
332 if (hostsig == SIGLIBRT)
333 return GDB_SIGNAL_LIBRT;
334 #endif
336 #if defined (REALTIME_LO)
337 if (hostsig >= REALTIME_LO && hostsig < REALTIME_HI)
339 /* This block of GDB_SIGNAL_REALTIME value is in order. */
340 if (33 <= hostsig && hostsig <= 63)
341 return (enum gdb_signal)
342 (hostsig - 33 + (int) GDB_SIGNAL_REALTIME_33);
343 else if (hostsig == 32)
344 return GDB_SIGNAL_REALTIME_32;
345 else if (64 <= hostsig && hostsig <= 127)
346 return (enum gdb_signal)
347 (hostsig - 64 + (int) GDB_SIGNAL_REALTIME_64);
348 else
349 error (_("GDB bug: target.c (gdb_signal_from_host): "
350 "unrecognized real-time signal"));
352 #endif
354 return GDB_SIGNAL_UNKNOWN;
357 /* Convert a OURSIG (an enum gdb_signal) to the form used by the
358 target operating system (referred to as the ``host'') or zero if the
359 equivalent host signal is not available. Set/clear OURSIG_OK
360 accordingly. */
362 static int
363 do_gdb_signal_to_host (enum gdb_signal oursig,
364 int *oursig_ok)
366 int retsig;
367 /* Silence the 'not used' warning, for targets that
368 do not support signals. */
369 (void) retsig;
371 /* Signals are ordered ANSI-standard signals first, other signals
372 second, with signals in each block ordered by their numerical
373 values on a typical POSIX platform. */
375 *oursig_ok = 1;
376 switch (oursig)
378 case GDB_SIGNAL_0:
379 return 0;
381 /* SIGINT, SIGILL, SIGABRT, SIGFPE, SIGSEGV and SIGTERM
382 are ANSI-standard signals and are always available. */
383 case GDB_SIGNAL_INT:
384 return SIGINT;
385 case GDB_SIGNAL_ILL:
386 return SIGILL;
387 case GDB_SIGNAL_ABRT:
388 return SIGABRT;
389 case GDB_SIGNAL_FPE:
390 return SIGFPE;
391 case GDB_SIGNAL_SEGV:
392 return SIGSEGV;
393 case GDB_SIGNAL_TERM:
394 return SIGTERM;
396 /* All other signals need preprocessor conditionals. */
397 #if defined (SIGHUP)
398 case GDB_SIGNAL_HUP:
399 return SIGHUP;
400 #endif
401 #if defined (SIGQUIT)
402 case GDB_SIGNAL_QUIT:
403 return SIGQUIT;
404 #endif
405 #if defined (SIGTRAP)
406 case GDB_SIGNAL_TRAP:
407 return SIGTRAP;
408 #endif
409 #if defined (SIGEMT)
410 case GDB_SIGNAL_EMT:
411 return SIGEMT;
412 #endif
413 #if defined (SIGKILL)
414 case GDB_SIGNAL_KILL:
415 return SIGKILL;
416 #endif
417 #if defined (SIGBUS)
418 case GDB_SIGNAL_BUS:
419 return SIGBUS;
420 #endif
421 #if defined (SIGSYS)
422 case GDB_SIGNAL_SYS:
423 return SIGSYS;
424 #endif
425 #if defined (SIGPIPE)
426 case GDB_SIGNAL_PIPE:
427 return SIGPIPE;
428 #endif
429 #if defined (SIGALRM)
430 case GDB_SIGNAL_ALRM:
431 return SIGALRM;
432 #endif
433 #if defined (SIGUSR1)
434 case GDB_SIGNAL_USR1:
435 return SIGUSR1;
436 #endif
437 #if defined (SIGUSR2)
438 case GDB_SIGNAL_USR2:
439 return SIGUSR2;
440 #endif
441 #if defined (SIGCHLD) || defined (SIGCLD)
442 case GDB_SIGNAL_CHLD:
443 #if defined (SIGCHLD)
444 return SIGCHLD;
445 #else
446 return SIGCLD;
447 #endif
448 #endif /* SIGCLD or SIGCHLD */
449 #if defined (SIGPWR)
450 case GDB_SIGNAL_PWR:
451 return SIGPWR;
452 #endif
453 #if defined (SIGWINCH)
454 case GDB_SIGNAL_WINCH:
455 return SIGWINCH;
456 #endif
457 #if defined (SIGURG)
458 case GDB_SIGNAL_URG:
459 return SIGURG;
460 #endif
461 #if defined (SIGIO)
462 case GDB_SIGNAL_IO:
463 return SIGIO;
464 #endif
465 #if defined (SIGPOLL)
466 case GDB_SIGNAL_POLL:
467 return SIGPOLL;
468 #endif
469 #if defined (SIGSTOP)
470 case GDB_SIGNAL_STOP:
471 return SIGSTOP;
472 #endif
473 #if defined (SIGTSTP)
474 case GDB_SIGNAL_TSTP:
475 return SIGTSTP;
476 #endif
477 #if defined (SIGCONT)
478 case GDB_SIGNAL_CONT:
479 return SIGCONT;
480 #endif
481 #if defined (SIGTTIN)
482 case GDB_SIGNAL_TTIN:
483 return SIGTTIN;
484 #endif
485 #if defined (SIGTTOU)
486 case GDB_SIGNAL_TTOU:
487 return SIGTTOU;
488 #endif
489 #if defined (SIGVTALRM)
490 case GDB_SIGNAL_VTALRM:
491 return SIGVTALRM;
492 #endif
493 #if defined (SIGPROF)
494 case GDB_SIGNAL_PROF:
495 return SIGPROF;
496 #endif
497 #if defined (SIGXCPU)
498 case GDB_SIGNAL_XCPU:
499 return SIGXCPU;
500 #endif
501 #if defined (SIGXFSZ)
502 case GDB_SIGNAL_XFSZ:
503 return SIGXFSZ;
504 #endif
505 #if defined (SIGWIND)
506 case GDB_SIGNAL_WIND:
507 return SIGWIND;
508 #endif
509 #if defined (SIGPHONE)
510 case GDB_SIGNAL_PHONE:
511 return SIGPHONE;
512 #endif
513 #if defined (SIGLOST)
514 case GDB_SIGNAL_LOST:
515 return SIGLOST;
516 #endif
517 #if defined (SIGWAITING)
518 case GDB_SIGNAL_WAITING:
519 return SIGWAITING;
520 #endif
521 #if defined (SIGCANCEL)
522 case GDB_SIGNAL_CANCEL:
523 return SIGCANCEL;
524 #endif
525 #if defined (SIGLWP)
526 case GDB_SIGNAL_LWP:
527 return SIGLWP;
528 #endif
529 #if defined (SIGDANGER)
530 case GDB_SIGNAL_DANGER:
531 return SIGDANGER;
532 #endif
533 #if defined (SIGGRANT)
534 case GDB_SIGNAL_GRANT:
535 return SIGGRANT;
536 #endif
537 #if defined (SIGRETRACT)
538 case GDB_SIGNAL_RETRACT:
539 return SIGRETRACT;
540 #endif
541 #if defined (SIGMSG)
542 case GDB_SIGNAL_MSG:
543 return SIGMSG;
544 #endif
545 #if defined (SIGSOUND)
546 case GDB_SIGNAL_SOUND:
547 return SIGSOUND;
548 #endif
549 #if defined (SIGSAK)
550 case GDB_SIGNAL_SAK:
551 return SIGSAK;
552 #endif
553 #if defined (SIGPRIO)
554 case GDB_SIGNAL_PRIO:
555 return SIGPRIO;
556 #endif
558 /* Mach exceptions. Assumes that the values for EXC_ are positive! */
559 #if defined (EXC_BAD_ACCESS) && defined (_NSIG)
560 case GDB_EXC_BAD_ACCESS:
561 return _NSIG + EXC_BAD_ACCESS;
562 #endif
563 #if defined (EXC_BAD_INSTRUCTION) && defined (_NSIG)
564 case GDB_EXC_BAD_INSTRUCTION:
565 return _NSIG + EXC_BAD_INSTRUCTION;
566 #endif
567 #if defined (EXC_ARITHMETIC) && defined (_NSIG)
568 case GDB_EXC_ARITHMETIC:
569 return _NSIG + EXC_ARITHMETIC;
570 #endif
571 #if defined (EXC_EMULATION) && defined (_NSIG)
572 case GDB_EXC_EMULATION:
573 return _NSIG + EXC_EMULATION;
574 #endif
575 #if defined (EXC_SOFTWARE) && defined (_NSIG)
576 case GDB_EXC_SOFTWARE:
577 return _NSIG + EXC_SOFTWARE;
578 #endif
579 #if defined (EXC_BREAKPOINT) && defined (_NSIG)
580 case GDB_EXC_BREAKPOINT:
581 return _NSIG + EXC_BREAKPOINT;
582 #endif
584 #if defined (SIGINFO)
585 case GDB_SIGNAL_INFO:
586 return SIGINFO;
587 #endif
588 #if defined (SIGLIBRT)
589 case GDB_SIGNAL_LIBRT:
590 return SIGLIBRT;
591 #endif
593 default:
594 #if defined (REALTIME_LO)
595 retsig = 0;
597 if (oursig >= GDB_SIGNAL_REALTIME_33
598 && oursig <= GDB_SIGNAL_REALTIME_63)
600 /* This block of signals is continuous, and
601 GDB_SIGNAL_REALTIME_33 is 33 by definition. */
602 retsig = (int) oursig - (int) GDB_SIGNAL_REALTIME_33 + 33;
604 else if (oursig == GDB_SIGNAL_REALTIME_32)
606 /* GDB_SIGNAL_REALTIME_32 isn't contiguous with
607 GDB_SIGNAL_REALTIME_33. It is 32 by definition. */
608 retsig = 32;
610 else if (oursig >= GDB_SIGNAL_REALTIME_64
611 && oursig <= GDB_SIGNAL_REALTIME_127)
613 /* This block of signals is continuous, and
614 GDB_SIGNAL_REALTIME_64 is 64 by definition. */
615 retsig = (int) oursig - (int) GDB_SIGNAL_REALTIME_64 + 64;
618 if (retsig >= REALTIME_LO && retsig < REALTIME_HI)
619 return retsig;
620 #endif
622 *oursig_ok = 0;
623 return 0;
628 gdb_signal_to_host_p (enum gdb_signal oursig)
630 int oursig_ok;
631 do_gdb_signal_to_host (oursig, &oursig_ok);
632 return oursig_ok;
636 gdb_signal_to_host (enum gdb_signal oursig)
638 int oursig_ok;
639 int targ_signo = do_gdb_signal_to_host (oursig, &oursig_ok);
640 if (!oursig_ok)
642 /* The user might be trying to do "signal SIGSAK" where this system
643 doesn't have SIGSAK. */
644 warning (_("Signal %s does not exist on this system."),
645 gdb_signal_to_name (oursig));
646 return 0;
648 else
649 return targ_signo;