psiginfo relied too much on sys_siglist.
[glibc.git] / stdio-common / psiginfo.c
blob627c21c6403dfd91ffc79ca4ccd4f95ba5efcf88
1 /* Copyright (C) 2009, 2010 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library 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 GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <libintl.h>
21 #include <signal.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <not-cancel.h>
30 /* Defined in sys_siglist.c. */
31 extern const char *const _sys_siglist[];
32 extern const char *const _sys_siglist_internal[] attribute_hidden;
35 #define MF(l) MF1 (l)
36 #define MF1(l) str_##l
37 #define C(s1, s2) C1 (s1, s2)
38 #define C1(s1, s2) s1##s2
40 #define NOW SIGILL
41 #include "psiginfo-define.h"
43 #define NOW SIGFPE
44 #include "psiginfo-define.h"
46 #define NOW SIGSEGV
47 #include "psiginfo-define.h"
49 #define NOW SIGBUS
50 #include "psiginfo-define.h"
52 #define NOW SIGTRAP
53 #include "psiginfo-define.h"
55 #define NOW SIGCLD
56 #include "psiginfo-define.h"
58 #define NOW SIGPOLL
59 #include "psiginfo-define.h"
62 /* Print out on stderr a line consisting of the test in S, a colon, a space,
63 a message describing the meaning of the signal number PINFO and a newline.
64 If S is NULL or "", the colon and space are omitted. */
65 void
66 psiginfo (const siginfo_t *pinfo, const char *s)
68 char buf[512];
69 FILE *fp = fmemopen (buf, sizeof (buf), "w");
70 if (fp == NULL)
72 const char *colon;
74 if (s == NULL || *s == '\0')
75 s = colon = "";
76 else
77 colon = ": ";
79 __fxprintf (NULL, "%s%ssignal %d\n", s, colon, pinfo->si_signo);
80 return;
83 if (s != NULL && *s != '\0')
84 fprintf (fp, "%s: ", s);
86 const char *desc;
87 if (pinfo->si_signo >= 0 && pinfo->si_signo < NSIG
88 && ((desc = INTUSE(_sys_siglist)[pinfo->si_signo]) != NULL
89 || (pinfo->si_signo >= SIGRTMIN && pinfo->si_signo < SIGRTMAX)))
91 if (desc == NULL)
93 if (pinfo->si_signo - SIGRTMIN < SIGRTMAX - pinfo->si_signo)
95 if (pinfo->si_signo == SIGRTMIN)
96 fprintf (fp, "SIGRTMIN (");
97 else
98 fprintf (fp, "SIGRTMIN+%d (", pinfo->si_signo - SIGRTMIN);
100 else
102 if (pinfo->si_signo == SIGRTMAX)
103 fprintf (fp, "SIGRTMAX (");
104 else
105 fprintf (fp, "SIGRTMAX-%d (", SIGRTMAX - pinfo->si_signo);
108 else
109 fprintf (fp, "%s (", _(desc));
111 const char *base = NULL;
112 const uint8_t *offarr = NULL;
113 size_t offarr_len = 0;
114 switch (pinfo->si_signo)
116 #define H(sig) \
117 case sig: \
118 base = C(codestrs_, sig).str; \
119 offarr = C (codes_, sig); \
120 offarr_len = sizeof (C (codes_, sig)) / sizeof (C (codes_, sig)[0]);\
121 break
123 H (SIGILL);
124 H (SIGFPE);
125 H (SIGSEGV);
126 H (SIGBUS);
127 H (SIGTRAP);
128 H (SIGCHLD);
129 H (SIGPOLL);
132 const char *str = NULL;
133 if (offarr != NULL
134 && pinfo->si_code >= 1 && pinfo->si_code <= offarr_len)
135 str = base + offarr[pinfo->si_code - 1];
136 else
137 switch (pinfo->si_code)
139 case SI_USER:
140 str = N_("Signal sent by kill()");
141 break;
142 case SI_QUEUE:
143 str = N_("Signal sent by sigqueue()");
144 break;
145 case SI_TIMER:
146 str = N_("Signal generated by the expiration of a timer");
147 break;
148 case SI_ASYNCIO:
149 str = N_("\
150 Signal generated by the completion of an asynchronous I/O request");
151 break;
152 case SI_MESGQ:
153 str = N_("\
154 Signal generated by the arrival of a message on an empty message queue");
155 break;
156 #ifdef SI_TKILL
157 case SI_TKILL:
158 str = N_("Signal sent by tkill()");
159 break;
160 #endif
161 #ifdef SI_ASYNCNL
162 case SI_ASYNCNL:
163 str = N_("\
164 Signal generated by the completion of an asynchronous name lookup request");
165 break;
166 #endif
167 #ifdef SI_SIGIO
168 case SI_SIGIO:
169 str = N_("\
170 Signal generated by the completion of an I/O request");
171 break;
172 #endif
173 #ifdef SI_KERNEL
174 case SI_KERNEL:
175 str = N_("Signal sent by the kernel");
176 break;
177 #endif
180 if (str != NULL)
181 fprintf (fp, "%s ", _(str));
182 else
183 fprintf (fp, "%d ", pinfo->si_code);
185 if (pinfo->si_signo == SIGILL || pinfo->si_signo == SIGFPE
186 || pinfo->si_signo == SIGSEGV || pinfo->si_signo == SIGBUS)
187 fprintf (fp, "[%p])\n", pinfo->si_addr);
188 else if (pinfo->si_signo == SIGCHLD)
189 fprintf (fp, "%ld %d %ld)\n",
190 (long int) pinfo->si_pid, pinfo->si_status,
191 (long int) pinfo->si_uid);
192 else if (pinfo->si_signo == SIGPOLL)
193 fprintf (fp, "%ld)\n", (long int) pinfo->si_band);
194 else
195 fprintf (fp, "%ld %ld)\n",
196 (long int) pinfo->si_pid, (long int) pinfo->si_uid);
198 else
199 fprintf (fp, _("Unknown signal %d\n"), pinfo->si_signo);
201 fclose (fp);
203 write_not_cancel (STDERR_FILENO, buf, strlen (buf));