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