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