replace hexify with bin2hex
[platform/upstream/binutils.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 defined (SIGINT)
135   if (hostsig == SIGINT)
136     return GDB_SIGNAL_INT;
137 #endif
138 #if defined (SIGQUIT)
139   if (hostsig == SIGQUIT)
140     return GDB_SIGNAL_QUIT;
141 #endif
142 #if defined (SIGILL)
143   if (hostsig == SIGILL)
144     return GDB_SIGNAL_ILL;
145 #endif
146 #if defined (SIGTRAP)
147   if (hostsig == SIGTRAP)
148     return GDB_SIGNAL_TRAP;
149 #endif
150 #if defined (SIGABRT)
151   if (hostsig == SIGABRT)
152     return GDB_SIGNAL_ABRT;
153 #endif
154 #if defined (SIGEMT)
155   if (hostsig == SIGEMT)
156     return GDB_SIGNAL_EMT;
157 #endif
158 #if defined (SIGFPE)
159   if (hostsig == SIGFPE)
160     return GDB_SIGNAL_FPE;
161 #endif
162 #if defined (SIGKILL)
163   if (hostsig == SIGKILL)
164     return GDB_SIGNAL_KILL;
165 #endif
166 #if defined (SIGBUS)
167   if (hostsig == SIGBUS)
168     return GDB_SIGNAL_BUS;
169 #endif
170 #if defined (SIGSEGV)
171   if (hostsig == SIGSEGV)
172     return GDB_SIGNAL_SEGV;
173 #endif
174 #if defined (SIGSYS)
175   if (hostsig == SIGSYS)
176     return GDB_SIGNAL_SYS;
177 #endif
178 #if defined (SIGPIPE)
179   if (hostsig == SIGPIPE)
180     return GDB_SIGNAL_PIPE;
181 #endif
182 #if defined (SIGALRM)
183   if (hostsig == SIGALRM)
184     return GDB_SIGNAL_ALRM;
185 #endif
186 #if defined (SIGTERM)
187   if (hostsig == SIGTERM)
188     return GDB_SIGNAL_TERM;
189 #endif
190 #if defined (SIGUSR1)
191   if (hostsig == SIGUSR1)
192     return GDB_SIGNAL_USR1;
193 #endif
194 #if defined (SIGUSR2)
195   if (hostsig == SIGUSR2)
196     return GDB_SIGNAL_USR2;
197 #endif
198 #if defined (SIGCLD)
199   if (hostsig == SIGCLD)
200     return GDB_SIGNAL_CHLD;
201 #endif
202 #if defined (SIGCHLD)
203   if (hostsig == SIGCHLD)
204     return GDB_SIGNAL_CHLD;
205 #endif
206 #if defined (SIGPWR)
207   if (hostsig == SIGPWR)
208     return GDB_SIGNAL_PWR;
209 #endif
210 #if defined (SIGWINCH)
211   if (hostsig == SIGWINCH)
212     return GDB_SIGNAL_WINCH;
213 #endif
214 #if defined (SIGURG)
215   if (hostsig == SIGURG)
216     return GDB_SIGNAL_URG;
217 #endif
218 #if defined (SIGIO)
219   if (hostsig == SIGIO)
220     return GDB_SIGNAL_IO;
221 #endif
222 #if defined (SIGPOLL)
223   if (hostsig == SIGPOLL)
224     return GDB_SIGNAL_POLL;
225 #endif
226 #if defined (SIGSTOP)
227   if (hostsig == SIGSTOP)
228     return GDB_SIGNAL_STOP;
229 #endif
230 #if defined (SIGTSTP)
231   if (hostsig == SIGTSTP)
232     return GDB_SIGNAL_TSTP;
233 #endif
234 #if defined (SIGCONT)
235   if (hostsig == SIGCONT)
236     return GDB_SIGNAL_CONT;
237 #endif
238 #if defined (SIGTTIN)
239   if (hostsig == SIGTTIN)
240     return GDB_SIGNAL_TTIN;
241 #endif
242 #if defined (SIGTTOU)
243   if (hostsig == SIGTTOU)
244     return GDB_SIGNAL_TTOU;
245 #endif
246 #if defined (SIGVTALRM)
247   if (hostsig == SIGVTALRM)
248     return GDB_SIGNAL_VTALRM;
249 #endif
250 #if defined (SIGPROF)
251   if (hostsig == SIGPROF)
252     return GDB_SIGNAL_PROF;
253 #endif
254 #if defined (SIGXCPU)
255   if (hostsig == SIGXCPU)
256     return GDB_SIGNAL_XCPU;
257 #endif
258 #if defined (SIGXFSZ)
259   if (hostsig == SIGXFSZ)
260     return GDB_SIGNAL_XFSZ;
261 #endif
262 #if defined (SIGWIND)
263   if (hostsig == SIGWIND)
264     return GDB_SIGNAL_WIND;
265 #endif
266 #if defined (SIGPHONE)
267   if (hostsig == SIGPHONE)
268     return GDB_SIGNAL_PHONE;
269 #endif
270 #if defined (SIGLOST)
271   if (hostsig == SIGLOST)
272     return GDB_SIGNAL_LOST;
273 #endif
274 #if defined (SIGWAITING)
275   if (hostsig == SIGWAITING)
276     return GDB_SIGNAL_WAITING;
277 #endif
278 #if defined (SIGCANCEL)
279   if (hostsig == SIGCANCEL)
280     return GDB_SIGNAL_CANCEL;
281 #endif
282 #if defined (SIGLWP)
283   if (hostsig == SIGLWP)
284     return GDB_SIGNAL_LWP;
285 #endif
286 #if defined (SIGDANGER)
287   if (hostsig == SIGDANGER)
288     return GDB_SIGNAL_DANGER;
289 #endif
290 #if defined (SIGGRANT)
291   if (hostsig == SIGGRANT)
292     return GDB_SIGNAL_GRANT;
293 #endif
294 #if defined (SIGRETRACT)
295   if (hostsig == SIGRETRACT)
296     return GDB_SIGNAL_RETRACT;
297 #endif
298 #if defined (SIGMSG)
299   if (hostsig == SIGMSG)
300     return GDB_SIGNAL_MSG;
301 #endif
302 #if defined (SIGSOUND)
303   if (hostsig == SIGSOUND)
304     return GDB_SIGNAL_SOUND;
305 #endif
306 #if defined (SIGSAK)
307   if (hostsig == SIGSAK)
308     return GDB_SIGNAL_SAK;
309 #endif
310 #if defined (SIGPRIO)
311   if (hostsig == SIGPRIO)
312     return GDB_SIGNAL_PRIO;
313 #endif
314
315   /* Mach exceptions.  Assumes that the values for EXC_ are positive! */
316 #if defined (EXC_BAD_ACCESS) && defined (_NSIG)
317   if (hostsig == _NSIG + EXC_BAD_ACCESS)
318     return GDB_EXC_BAD_ACCESS;
319 #endif
320 #if defined (EXC_BAD_INSTRUCTION) && defined (_NSIG)
321   if (hostsig == _NSIG + EXC_BAD_INSTRUCTION)
322     return GDB_EXC_BAD_INSTRUCTION;
323 #endif
324 #if defined (EXC_ARITHMETIC) && defined (_NSIG)
325   if (hostsig == _NSIG + EXC_ARITHMETIC)
326     return GDB_EXC_ARITHMETIC;
327 #endif
328 #if defined (EXC_EMULATION) && defined (_NSIG)
329   if (hostsig == _NSIG + EXC_EMULATION)
330     return GDB_EXC_EMULATION;
331 #endif
332 #if defined (EXC_SOFTWARE) && defined (_NSIG)
333   if (hostsig == _NSIG + EXC_SOFTWARE)
334     return GDB_EXC_SOFTWARE;
335 #endif
336 #if defined (EXC_BREAKPOINT) && defined (_NSIG)
337   if (hostsig == _NSIG + EXC_BREAKPOINT)
338     return GDB_EXC_BREAKPOINT;
339 #endif
340
341 #if defined (SIGINFO)
342   if (hostsig == SIGINFO)
343     return GDB_SIGNAL_INFO;
344 #endif
345
346 #if defined (REALTIME_LO)
347   if (hostsig >= REALTIME_LO && hostsig < REALTIME_HI)
348     {
349       /* This block of GDB_SIGNAL_REALTIME value is in order.  */
350       if (33 <= hostsig && hostsig <= 63)
351         return (enum gdb_signal)
352           (hostsig - 33 + (int) GDB_SIGNAL_REALTIME_33);
353       else if (hostsig == 32)
354         return GDB_SIGNAL_REALTIME_32;
355       else if (64 <= hostsig && hostsig <= 127)
356         return (enum gdb_signal)
357           (hostsig - 64 + (int) GDB_SIGNAL_REALTIME_64);
358       else
359         error (_("GDB bug: target.c (gdb_signal_from_host): "
360                "unrecognized real-time signal"));
361     }
362 #endif
363
364   return GDB_SIGNAL_UNKNOWN;
365 }
366
367 /* Convert a OURSIG (an enum gdb_signal) to the form used by the
368    target operating system (refered to as the ``host'') or zero if the
369    equivalent host signal is not available.  Set/clear OURSIG_OK
370    accordingly. */
371
372 static int
373 do_gdb_signal_to_host (enum gdb_signal oursig,
374                           int *oursig_ok)
375 {
376   int retsig;
377   /* Silence the 'not used' warning, for targets that
378      do not support signals.  */
379   (void) retsig;
380
381   *oursig_ok = 1;
382   switch (oursig)
383     {
384     case GDB_SIGNAL_0:
385       return 0;
386
387 #if defined (SIGHUP)
388     case GDB_SIGNAL_HUP:
389       return SIGHUP;
390 #endif
391 #if defined (SIGINT)
392     case GDB_SIGNAL_INT:
393       return SIGINT;
394 #endif
395 #if defined (SIGQUIT)
396     case GDB_SIGNAL_QUIT:
397       return SIGQUIT;
398 #endif
399 #if defined (SIGILL)
400     case GDB_SIGNAL_ILL:
401       return SIGILL;
402 #endif
403 #if defined (SIGTRAP)
404     case GDB_SIGNAL_TRAP:
405       return SIGTRAP;
406 #endif
407 #if defined (SIGABRT)
408     case GDB_SIGNAL_ABRT:
409       return SIGABRT;
410 #endif
411 #if defined (SIGEMT)
412     case GDB_SIGNAL_EMT:
413       return SIGEMT;
414 #endif
415 #if defined (SIGFPE)
416     case GDB_SIGNAL_FPE:
417       return SIGFPE;
418 #endif
419 #if defined (SIGKILL)
420     case GDB_SIGNAL_KILL:
421       return SIGKILL;
422 #endif
423 #if defined (SIGBUS)
424     case GDB_SIGNAL_BUS:
425       return SIGBUS;
426 #endif
427 #if defined (SIGSEGV)
428     case GDB_SIGNAL_SEGV:
429       return SIGSEGV;
430 #endif
431 #if defined (SIGSYS)
432     case GDB_SIGNAL_SYS:
433       return SIGSYS;
434 #endif
435 #if defined (SIGPIPE)
436     case GDB_SIGNAL_PIPE:
437       return SIGPIPE;
438 #endif
439 #if defined (SIGALRM)
440     case GDB_SIGNAL_ALRM:
441       return SIGALRM;
442 #endif
443 #if defined (SIGTERM)
444     case GDB_SIGNAL_TERM:
445       return SIGTERM;
446 #endif
447 #if defined (SIGUSR1)
448     case GDB_SIGNAL_USR1:
449       return SIGUSR1;
450 #endif
451 #if defined (SIGUSR2)
452     case GDB_SIGNAL_USR2:
453       return SIGUSR2;
454 #endif
455 #if defined (SIGCHLD) || defined (SIGCLD)
456     case GDB_SIGNAL_CHLD:
457 #if defined (SIGCHLD)
458       return SIGCHLD;
459 #else
460       return SIGCLD;
461 #endif
462 #endif /* SIGCLD or SIGCHLD */
463 #if defined (SIGPWR)
464     case GDB_SIGNAL_PWR:
465       return SIGPWR;
466 #endif
467 #if defined (SIGWINCH)
468     case GDB_SIGNAL_WINCH:
469       return SIGWINCH;
470 #endif
471 #if defined (SIGURG)
472     case GDB_SIGNAL_URG:
473       return SIGURG;
474 #endif
475 #if defined (SIGIO)
476     case GDB_SIGNAL_IO:
477       return SIGIO;
478 #endif
479 #if defined (SIGPOLL)
480     case GDB_SIGNAL_POLL:
481       return SIGPOLL;
482 #endif
483 #if defined (SIGSTOP)
484     case GDB_SIGNAL_STOP:
485       return SIGSTOP;
486 #endif
487 #if defined (SIGTSTP)
488     case GDB_SIGNAL_TSTP:
489       return SIGTSTP;
490 #endif
491 #if defined (SIGCONT)
492     case GDB_SIGNAL_CONT:
493       return SIGCONT;
494 #endif
495 #if defined (SIGTTIN)
496     case GDB_SIGNAL_TTIN:
497       return SIGTTIN;
498 #endif
499 #if defined (SIGTTOU)
500     case GDB_SIGNAL_TTOU:
501       return SIGTTOU;
502 #endif
503 #if defined (SIGVTALRM)
504     case GDB_SIGNAL_VTALRM:
505       return SIGVTALRM;
506 #endif
507 #if defined (SIGPROF)
508     case GDB_SIGNAL_PROF:
509       return SIGPROF;
510 #endif
511 #if defined (SIGXCPU)
512     case GDB_SIGNAL_XCPU:
513       return SIGXCPU;
514 #endif
515 #if defined (SIGXFSZ)
516     case GDB_SIGNAL_XFSZ:
517       return SIGXFSZ;
518 #endif
519 #if defined (SIGWIND)
520     case GDB_SIGNAL_WIND:
521       return SIGWIND;
522 #endif
523 #if defined (SIGPHONE)
524     case GDB_SIGNAL_PHONE:
525       return SIGPHONE;
526 #endif
527 #if defined (SIGLOST)
528     case GDB_SIGNAL_LOST:
529       return SIGLOST;
530 #endif
531 #if defined (SIGWAITING)
532     case GDB_SIGNAL_WAITING:
533       return SIGWAITING;
534 #endif
535 #if defined (SIGCANCEL)
536     case GDB_SIGNAL_CANCEL:
537       return SIGCANCEL;
538 #endif
539 #if defined (SIGLWP)
540     case GDB_SIGNAL_LWP:
541       return SIGLWP;
542 #endif
543 #if defined (SIGDANGER)
544     case GDB_SIGNAL_DANGER:
545       return SIGDANGER;
546 #endif
547 #if defined (SIGGRANT)
548     case GDB_SIGNAL_GRANT:
549       return SIGGRANT;
550 #endif
551 #if defined (SIGRETRACT)
552     case GDB_SIGNAL_RETRACT:
553       return SIGRETRACT;
554 #endif
555 #if defined (SIGMSG)
556     case GDB_SIGNAL_MSG:
557       return SIGMSG;
558 #endif
559 #if defined (SIGSOUND)
560     case GDB_SIGNAL_SOUND:
561       return SIGSOUND;
562 #endif
563 #if defined (SIGSAK)
564     case GDB_SIGNAL_SAK:
565       return SIGSAK;
566 #endif
567 #if defined (SIGPRIO)
568     case GDB_SIGNAL_PRIO:
569       return SIGPRIO;
570 #endif
571
572       /* Mach exceptions.  Assumes that the values for EXC_ are positive! */
573 #if defined (EXC_BAD_ACCESS) && defined (_NSIG)
574     case GDB_EXC_BAD_ACCESS:
575       return _NSIG + EXC_BAD_ACCESS;
576 #endif
577 #if defined (EXC_BAD_INSTRUCTION) && defined (_NSIG)
578     case GDB_EXC_BAD_INSTRUCTION:
579       return _NSIG + EXC_BAD_INSTRUCTION;
580 #endif
581 #if defined (EXC_ARITHMETIC) && defined (_NSIG)
582     case GDB_EXC_ARITHMETIC:
583       return _NSIG + EXC_ARITHMETIC;
584 #endif
585 #if defined (EXC_EMULATION) && defined (_NSIG)
586     case GDB_EXC_EMULATION:
587       return _NSIG + EXC_EMULATION;
588 #endif
589 #if defined (EXC_SOFTWARE) && defined (_NSIG)
590     case GDB_EXC_SOFTWARE:
591       return _NSIG + EXC_SOFTWARE;
592 #endif
593 #if defined (EXC_BREAKPOINT) && defined (_NSIG)
594     case GDB_EXC_BREAKPOINT:
595       return _NSIG + EXC_BREAKPOINT;
596 #endif
597
598 #if defined (SIGINFO)
599     case GDB_SIGNAL_INFO:
600       return SIGINFO;
601 #endif
602
603     default:
604 #if defined (REALTIME_LO)
605       retsig = 0;
606
607       if (oursig >= GDB_SIGNAL_REALTIME_33
608           && oursig <= GDB_SIGNAL_REALTIME_63)
609         {
610           /* This block of signals is continuous, and
611              GDB_SIGNAL_REALTIME_33 is 33 by definition.  */
612           retsig = (int) oursig - (int) GDB_SIGNAL_REALTIME_33 + 33;
613         }
614       else if (oursig == GDB_SIGNAL_REALTIME_32)
615         {
616           /* GDB_SIGNAL_REALTIME_32 isn't contiguous with
617              GDB_SIGNAL_REALTIME_33.  It is 32 by definition.  */
618           retsig = 32;
619         }
620       else if (oursig >= GDB_SIGNAL_REALTIME_64
621           && oursig <= GDB_SIGNAL_REALTIME_127)
622         {
623           /* This block of signals is continuous, and
624              GDB_SIGNAL_REALTIME_64 is 64 by definition.  */
625           retsig = (int) oursig - (int) GDB_SIGNAL_REALTIME_64 + 64;
626         }
627
628       if (retsig >= REALTIME_LO && retsig < REALTIME_HI)
629         return retsig;
630 #endif
631
632       *oursig_ok = 0;
633       return 0;
634     }
635 }
636
637 int
638 gdb_signal_to_host_p (enum gdb_signal oursig)
639 {
640   int oursig_ok;
641   do_gdb_signal_to_host (oursig, &oursig_ok);
642   return oursig_ok;
643 }
644
645 int
646 gdb_signal_to_host (enum gdb_signal oursig)
647 {
648   int oursig_ok;
649   int targ_signo = do_gdb_signal_to_host (oursig, &oursig_ok);
650   if (!oursig_ok)
651     {
652       /* The user might be trying to do "signal SIGSAK" where this system
653          doesn't have SIGSAK.  */
654       warning (_("Signal %s does not exist on this system."),
655                gdb_signal_to_name (oursig));
656       return 0;
657     }
658   else
659     return targ_signo;
660 }