*** empty log message ***
[binutils-gdb.git] / gdb / signame.c
1 /* Convert between signal names and numbers.
2 Copyright (C) 1990 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 1, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #include <stdio.h>
19 #include <signal.h>
20 #include "signame.h"
21
22 /* GDB-specific, FIXME. (This is for the SYS_SIGLIST_MISSING define). */
23 #include "defs.h"
24 #include "param.h"
25
26 #ifdef __STDC__
27 #define CONST const
28 #else
29 #define CONST
30 #endif
31
32 #if SYS_SIGLIST_MISSING
33 /* There is too much variation in Sys V signal numbers and names, so
34 we must initialize them at runtime. */
35
36 static CONST char undoc[] = "unknown signal";
37
38 /* We'd like to make this const char*[], but whoever's using it might
39 want to assign from it to a char*. */
40 char *sys_siglist[NSIG];
41 #endif /* SYS_SIGLIST_MISSING */
42
43 /* Table of abbreviations for signals. Note: A given number can
44 appear more than once with different abbreviations. */
45 typedef struct
46 {
47 int number;
48 CONST char *abbrev;
49 } num_abbrev;
50 static num_abbrev sig_table[NSIG*2];
51 /* Number of elements of sig_table used. */
52 static int sig_table_nelts = 0;
53
54 /* Enter signal number NUMBER into the tables with ABBREV and NAME. */
55 static void
56 init_sig (number, abbrev, name)
57 int number;
58 CONST char *abbrev;
59 CONST char *name;
60 {
61 #if SYS_SIGLIST_MISSING
62 sys_siglist[number] = (char *) name;
63 #endif
64 sig_table[sig_table_nelts].number = number;
65 sig_table[sig_table_nelts++].abbrev = abbrev;
66 }
67
68 static void init_sigs ()
69 {
70 #if SYS_SIGLIST_MISSING
71 int i;
72
73 /* Initialize signal names. */
74 for (i = 0; i < NSIG; i++)
75 sys_siglist[i] = (char *) undoc;
76 #endif /* SYS_SIGLIST_MISSING */
77
78 /* Initialize signal names. */
79 #if defined (SIGHUP)
80 init_sig (SIGHUP, "HUP", "Hangup");
81 #endif
82 #if defined (SIGINT)
83 init_sig (SIGINT, "INT", "Interrupt");
84 #endif
85 #if defined (SIGQUIT)
86 init_sig (SIGQUIT, "QUIT", "Quit");
87 #endif
88 #if defined (SIGILL)
89 init_sig (SIGILL, "ILL", "Illegal Instruction");
90 #endif
91 #if defined (SIGTRAP)
92 init_sig (SIGTRAP, "TRAP", "Trace/breakpoint trap");
93 #endif
94 /* If SIGIOT == SIGABRT, we want to print it as SIGABRT because
95 SIGABRT is in ANSI and POSIX.1 and SIGIOT isn't. */
96 #if defined (SIGABRT)
97 init_sig (SIGABRT, "ABRT", "Aborted");
98 #endif
99 #if defined (SIGIOT)
100 init_sig (SIGIOT, "IOT", "IOT trap");
101 #endif
102 #if defined (SIGEMT)
103 init_sig (SIGEMT, "EMT", "EMT trap");
104 #endif
105 #if defined (SIGFPE)
106 init_sig (SIGFPE, "FPE", "Floating point exception");
107 #endif
108 #if defined (SIGKILL)
109 init_sig (SIGKILL, "KILL", "Killed");
110 #endif
111 #if defined (SIGBUS)
112 init_sig (SIGBUS, "BUS", "Bus error");
113 #endif
114 #if defined (SIGSEGV)
115 init_sig (SIGSEGV, "SEGV", "Segmentation fault");
116 #endif
117 #if defined (SIGSYS)
118 init_sig (SIGSYS, "SYS", "Bad system call");
119 #endif
120 #if defined (SIGPIPE)
121 init_sig (SIGPIPE, "PIPE", "Broken pipe");
122 #endif
123 #if defined (SIGALRM)
124 init_sig (SIGALRM, "ALRM", "Alarm clock");
125 #endif
126 #if defined (SIGTERM)
127 init_sig (SIGTERM, "TERM", "Terminated");
128 #endif
129 #if defined (SIGUSR1)
130 init_sig (SIGUSR1, "USR1", "User defined signal 1");
131 #endif
132 #if defined (SIGUSR2)
133 init_sig (SIGUSR2, "USR2", "User defined signal 2");
134 #endif
135 /* If SIGCLD == SIGCHLD, we want to print it as SIGCHLD because that
136 is what is in POSIX.1. */
137 #if defined (SIGCHLD)
138 init_sig (SIGCHLD, "CHLD", "Child exited");
139 #endif
140 #if defined (SIGCLD)
141 init_sig (SIGCLD, "CLD", "Child exited");
142 #endif
143 #if defined (SIGPWR)
144 init_sig (SIGPWR, "PWR", "Power failure");
145 #endif
146 #if defined (SIGTSTP)
147 init_sig (SIGTSTP, "TSTP", "Stopped");
148 #endif
149 #if defined (SIGTTIN)
150 init_sig (SIGTTIN, "TTIN", "Stopped (tty input)");
151 #endif
152 #if defined (SIGTTOU)
153 init_sig (SIGTTOU, "TTOU", "Stopped (tty output)");
154 #endif
155 #if defined (SIGSTOP)
156 init_sig (SIGSTOP, "STOP", "Stopped (signal)");
157 #endif
158 #if defined (SIGXCPU)
159 init_sig (SIGXCPU, "XCPU", "CPU time limit exceeded");
160 #endif
161 #if defined (SIGXFSZ)
162 init_sig (SIGXFSZ, "XFSZ", "File size limit exceeded");
163 #endif
164 #if defined (SIGVTALRM)
165 init_sig (SIGVTALRM, "VTALRM", "Virtual timer expired");
166 #endif
167 #if defined (SIGPROF)
168 init_sig (SIGPROF, "PROF", "Profiling timer expired");
169 #endif
170 #if defined (SIGWINCH)
171 /* "Window size changed" might be more accurate, but even if that
172 is all that it means now, perhaps in the future it will be
173 extended to cover other kinds of window changes. */
174 init_sig (SIGWINCH, "WINCH", "Window changed");
175 #endif
176 #if defined (SIGCONT)
177 init_sig (SIGCONT, "CONT", "Continued");
178 #endif
179 #if defined (SIGURG)
180 init_sig (SIGURG, "URG", "Urgent I/O condition");
181 #endif
182 #if defined (SIGIO)
183 /* "I/O pending" has also been suggested. A disadvantage is
184 that signal only happens when the process has
185 asked for it, not everytime I/O is pending. Another disadvantage
186 is the confusion from giving it a different name than under Unix. */
187 init_sig (SIGIO, "IO", "I/O possible");
188 #endif
189 #if defined (SIGWIND)
190 init_sig (SIGWIND, "WIND", "SIGWIND");
191 #endif
192 #if defined (SIGPHONE)
193 init_sig (SIGPHONE, "PHONE", "SIGPHONE");
194 #endif
195 #if defined (SIGPOLL)
196 init_sig (SIGPOLL, "POLL", "I/O possible");
197 #endif
198 #if defined (SIGLOST)
199 init_sig (SIGLOST, "LOST", "Resource lost");
200 #endif
201 }
202
203 /* Return the abbreviation for signal NUMBER. */
204 char *
205 sig_abbrev (number)
206 int number;
207 {
208 int i;
209
210 for (i = 0; i < sig_table_nelts; i++)
211 if (sig_table[i].number == number)
212 return (char *)sig_table[i].abbrev;
213 return NULL;
214 }
215
216 /* Return the signal number for an ABBREV, or -1 if there is no
217 signal by that name. */
218 int
219 sig_number (abbrev)
220 CONST char *abbrev;
221 {
222 int i;
223
224 /* Skip over "SIG" if present. */
225 if (abbrev[0] == 'S' && abbrev[1] == 'I' && abbrev[2] == 'G')
226 abbrev += 3;
227
228 for (i = 0; i < sig_table_nelts; i++)
229 if (abbrev[0] == sig_table[i].abbrev[0]
230 && strcmp (abbrev, sig_table[i].abbrev) == 0)
231 return sig_table[i].number;
232 return -1;
233 }
234
235 #if SYS_SIGLIST_MISSING
236 /* Print to standard error the name of SIGNAL, preceded by MESSAGE and
237 a colon, and followed by a newline. */
238 void
239 psignal (signal, message)
240 unsigned signal;
241 CONST char *message;
242 {
243 if (signal <= 0 || signal >= NSIG)
244 fprintf (stderr, "%s: unknown signal", message);
245 else
246 fprintf (stderr, "%s: %s\n", message, sys_siglist[signal]);
247 }
248 #endif /* SYS_SIGLIST_MISSING */
249
250 void
251 _initialize_signame ()
252 {
253 init_sigs ();
254 }