dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / ptools / psig / psig.c
blob6e3cf16a80589471ed32276c2db0e24f3a8e6504
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <stdio.h>
27 #include <stdio_ext.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <ctype.h>
31 #include <fcntl.h>
32 #include <string.h>
33 #include <signal.h>
34 #include <stddef.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <libproc.h>
39 /* evil knowledge of libc internals */
40 #include "../../../lib/libc/inc/thr_uberdata.h"
42 #define MAX_SYMNAMLEN 1024 /* Recommended max symbol name length */
44 static char *sigflags(int, int);
45 static int look(char *);
46 static void perr(char *);
47 static int usage(void);
48 static uintptr_t deinterpose(int, void *, psinfo_t *, struct sigaction *);
50 static char *command;
51 static char *procname;
52 static int all_flag = 0;
53 static int lookuphandlers_flag = 1;
55 int
56 main(int argc, char **argv)
58 int rc = 0;
59 int c;
60 struct rlimit rlim;
62 if ((command = strrchr(argv[0], '/')) != NULL)
63 command++;
64 else
65 command = argv[0];
67 while ((c = getopt(argc, argv, "an")) != EOF) {
68 switch (c) {
69 case 'a':
70 all_flag = 1;
71 break;
72 case 'n':
73 lookuphandlers_flag = 0;
74 break;
75 default:
76 return (usage());
80 if (argc - optind < 1) {
81 return (usage());
85 * Make sure we'll have enough file descriptors to handle a target
86 * that has many many mappings.
88 if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
89 rlim.rlim_cur = rlim.rlim_max;
90 (void) setrlimit(RLIMIT_NOFILE, &rlim);
91 (void) enable_extended_FILE_stdio(-1, -1);
94 for (; optind != argc; optind++) {
95 rc += look(argv[optind]);
98 return (rc);
101 static int
102 usage(void)
104 (void) fprintf(stderr, "usage:\t%s [-n] pid ...\n", command);
105 (void) fprintf(stderr, " (report process signal actions)\n");
107 return (2);
110 static uintptr_t
111 uberdata_addr(struct ps_prochandle *Pr, char dmodel)
113 GElf_Sym sym;
115 if (Plookup_by_name(Pr, "libc.so", "_tdb_bootstrap", &sym) < 0)
116 return ((uintptr_t)NULL);
117 #ifdef _LP64
118 if (dmodel != PR_MODEL_NATIVE) {
119 caddr32_t uaddr;
120 caddr32_t addr;
122 if (Pread(Pr, &addr, sizeof (addr), sym.st_value)
123 == sizeof (addr) &&
124 addr != 0 &&
125 Pread(Pr, &uaddr, sizeof (uaddr), (uintptr_t)addr)
126 == sizeof (uaddr) &&
127 uaddr != 0)
128 return ((uintptr_t)uaddr);
130 #endif
131 if (dmodel == PR_MODEL_NATIVE) {
132 uintptr_t uaddr;
133 uintptr_t addr;
135 if (Pread(Pr, &addr, sizeof (addr), sym.st_value)
136 == sizeof (addr) &&
137 addr != 0 &&
138 Pread(Pr, &uaddr, sizeof (uaddr), addr)
139 == sizeof (uaddr) &&
140 uaddr != 0)
141 return (uaddr);
143 if (Plookup_by_name(Pr, "libc.so", "_uberdata", &sym) < 0)
144 return (0);
145 return (sym.st_value);
149 * Iterator function used to generate the process sigmask
150 * from the individual lwp sigmasks.
152 static int
153 lwp_iter(void *cd, const lwpstatus_t *lwpstatus)
155 sigset_t *ssp = cd;
157 ssp->__sigbits[0] &= lwpstatus->pr_lwphold.__sigbits[0];
158 ssp->__sigbits[1] &= lwpstatus->pr_lwphold.__sigbits[1];
159 ssp->__sigbits[2] &= lwpstatus->pr_lwphold.__sigbits[2];
160 ssp->__sigbits[3] &= lwpstatus->pr_lwphold.__sigbits[3];
163 * Return non-zero to terminate the iteration
164 * if the sigmask has become all zeros.
166 return ((ssp->__sigbits[0] | ssp->__sigbits[1] |
167 ssp->__sigbits[2] | ssp->__sigbits[3]) == 0);
170 static int
171 look(char *arg)
173 char pathname[100];
174 struct stat statb;
175 int fd = -1;
176 int sig, gcode;
177 sigset_t holdmask;
178 int maxsig;
179 struct sigaction *action = NULL;
180 psinfo_t psinfo;
181 const psinfo_t *psinfop;
182 struct ps_prochandle *Pr = NULL;
183 uintptr_t uberaddr;
184 uintptr_t aharraddr;
185 uintptr_t intfnaddr;
186 size_t aharrlen;
187 void *aharr = NULL;
188 int error = 1;
190 procname = arg; /* for perr() */
191 if ((Pr = proc_arg_grab(arg, PR_ARG_PIDS, PGRAB_RDONLY|PGRAB_FORCE,
192 &gcode)) == NULL || (psinfop = Ppsinfo(Pr)) == NULL) {
193 (void) fprintf(stderr, "%s: cannot examine %s: %s\n",
194 command, arg, Pgrab_error(gcode));
195 goto look_error;
197 (void) memcpy(&psinfo, psinfop, sizeof (psinfo_t));
198 proc_unctrl_psinfo(&psinfo);
200 (void) sprintf(pathname, "/proc/%d/sigact", (int)psinfo.pr_pid);
201 if ((fd = open(pathname, O_RDONLY)) < 0) {
202 perr("open sigact");
203 goto look_error;
206 if (fstat(fd, &statb) != 0) {
207 perr("fstat sigact");
208 goto look_error;
210 maxsig = statb.st_size / sizeof (struct sigaction);
211 action = malloc(maxsig * sizeof (struct sigaction));
212 if (action == NULL) {
213 (void) fprintf(stderr,
214 "%s: cannot malloc() space for %d sigaction structures\n",
215 command, maxsig);
216 goto look_error;
218 if (read(fd, (char *)action, maxsig * sizeof (struct sigaction)) !=
219 maxsig * sizeof (struct sigaction)) {
220 perr("read sigact");
221 goto look_error;
223 (void) close(fd);
224 fd = -1;
226 (void) printf("%d:\t%.70s\n", (int)psinfo.pr_pid, psinfo.pr_psargs);
228 (void) sigfillset(&holdmask);
229 (void) Plwp_iter(Pr, lwp_iter, &holdmask);
231 if ((uberaddr = uberdata_addr(Pr, psinfo.pr_dmodel)) == 0) {
232 aharraddr = 0;
233 aharrlen = 0;
234 intfnaddr = 0;
235 } else {
236 #ifdef _LP64
237 if (psinfo.pr_dmodel != PR_MODEL_NATIVE) {
238 caddr32_t addr;
239 aharraddr = uberaddr +
240 offsetof(uberdata32_t, siguaction);
241 aharrlen = sizeof (siguaction32_t) * NSIG;
242 (void) Pread(Pr, &addr, sizeof (addr),
243 uberaddr + offsetof(uberdata32_t, sigacthandler));
244 intfnaddr = (uintptr_t)addr;
245 } else
246 #endif
248 aharraddr = uberaddr +
249 offsetof(uberdata_t, siguaction);
250 aharrlen = sizeof (siguaction_t) * NSIG;
251 (void) Pread(Pr, &intfnaddr, sizeof (intfnaddr),
252 uberaddr + offsetof(uberdata_t, sigacthandler));
256 if (aharraddr) {
257 aharr = malloc(aharrlen);
258 if (aharr == NULL) {
259 (void) fprintf(stderr,
260 "%s: cannot malloc() space for actual handler array\n",
261 command);
262 goto look_error;
265 if (Pread(Pr, aharr, aharrlen, aharraddr) != aharrlen) {
266 (void) fprintf(stderr,
267 "%s: signal handler data at %p cannot be read.\n",
268 command, (void *)aharraddr);
269 free(aharr);
270 aharr = NULL;
274 for (sig = 1; sig <= maxsig; sig++) {
275 struct sigaction *sp = &action[sig - 1];
276 int caught = 0;
277 char buf[SIG2STR_MAX];
278 char *s;
280 /* proc_signame() returns "SIG..."; skip the "SIG" part */
281 (void) printf("%s\t", proc_signame(sig, buf, sizeof (buf)) + 3);
283 if (prismember(&holdmask, sig))
284 (void) printf("blocked,");
286 if (sp->sa_handler == SIG_DFL)
287 (void) printf("default");
288 else if (sp->sa_handler == SIG_IGN)
289 (void) printf("ignored");
290 else
291 caught = 1;
293 if (caught || all_flag) {
294 uintptr_t haddr;
295 GElf_Sym hsym;
296 char hname[MAX_SYMNAMLEN];
297 char buf[PRSIGBUFSZ];
299 haddr = (uintptr_t)sp->sa_handler;
301 if (aharr && intfnaddr && haddr == intfnaddr)
302 haddr = deinterpose(sig, aharr, &psinfo, sp);
304 if (haddr == (uintptr_t)SIG_DFL) {
305 if (caught)
306 (void) printf("default");
307 caught = 0;
308 } else if (haddr == (uintptr_t)SIG_IGN) {
309 if (caught)
310 (void) printf("ignored");
311 caught = 0;
312 } else {
313 if (caught)
314 (void) printf("caught");
317 if (caught || all_flag) {
318 if (lookuphandlers_flag && haddr > 1 &&
319 Plookup_by_addr(Pr, haddr, hname,
320 sizeof (hname), &hsym) == 0)
321 (void) printf("\t%-8s", hname);
322 else
323 (void) printf("\t0x%-8lx",
324 (ulong_t)haddr);
326 s = sigflags(sig, sp->sa_flags);
327 (void) printf("%s", (*s != '\0')? s : "\t0");
328 (void) proc_sigset2str(&sp->sa_mask, ",", 1,
329 buf, sizeof (buf));
330 if (buf[0] != '\0')
331 (void) printf("\t%s", buf);
333 } else if (sig == SIGCLD) {
334 s = sigflags(sig,
335 sp->sa_flags & (SA_NOCLDWAIT|SA_NOCLDSTOP));
336 if (*s != '\0')
337 (void) printf("\t\t%s", s);
339 (void) printf("\n");
342 error = 0;
344 look_error:
345 if (fd >= 0)
346 (void) close(fd);
347 free(aharr);
348 free(action);
349 if (Pr)
350 Prelease(Pr, 0);
351 return (error);
354 static void
355 perr(char *s)
357 if (s)
358 (void) fprintf(stderr, "%s: ", procname);
359 else
360 s = procname;
361 perror(s);
364 static char *
365 sigflags(int sig, int flags)
367 static char code_buf[100];
368 char *str = code_buf;
369 int flagmask =
370 (SA_ONSTACK|SA_RESETHAND|SA_RESTART|SA_SIGINFO|SA_NODEFER);
372 if (sig == SIGCLD)
373 flagmask |= (SA_NOCLDSTOP|SA_NOCLDWAIT);
375 *str = '\0';
376 if (flags & ~flagmask)
377 (void) sprintf(str, ",0x%x,", flags & ~flagmask);
378 else if (flags == 0)
379 return (str);
381 if (flags & SA_RESTART)
382 (void) strcat(str, ",RESTART");
383 if (flags & SA_RESETHAND)
384 (void) strcat(str, ",RESETHAND");
385 if (flags & SA_ONSTACK)
386 (void) strcat(str, ",ONSTACK");
387 if (flags & SA_SIGINFO)
388 (void) strcat(str, ",SIGINFO");
389 if (flags & SA_NODEFER)
390 (void) strcat(str, ",NODEFER");
392 if (sig == SIGCLD) {
393 if (flags & SA_NOCLDWAIT)
394 (void) strcat(str, ",NOCLDWAIT");
395 if (flags & SA_NOCLDSTOP)
396 (void) strcat(str, ",NOCLDSTOP");
399 *str = '\t';
401 return (str);
404 /*ARGSUSED2*/
405 static uintptr_t
406 deinterpose(int sig, void *aharr, psinfo_t *psinfo, struct sigaction *sp)
408 if (sp->sa_handler == SIG_DFL || sp->sa_handler == SIG_IGN)
409 return ((uintptr_t)sp->sa_handler);
410 #ifdef _LP64
411 if (psinfo->pr_dmodel != PR_MODEL_NATIVE) {
412 struct sigaction32 *sa32 = (struct sigaction32 *)
413 ((uintptr_t)aharr + sig * sizeof (siguaction32_t) +
414 offsetof(siguaction32_t, sig_uaction));
416 sp->sa_flags = sa32->sa_flags;
417 sp->sa_handler = (void (*)())(uintptr_t)sa32->sa_handler;
418 (void) memcpy(&sp->sa_mask, &sa32->sa_mask,
419 sizeof (sp->sa_mask));
420 } else
421 #endif
423 struct sigaction *sa = (struct sigaction *)
424 ((uintptr_t)aharr + sig * sizeof (siguaction_t) +
425 offsetof(siguaction_t, sig_uaction));
427 sp->sa_flags = sa->sa_flags;
428 sp->sa_handler = sa->sa_handler;
429 sp->sa_mask = sa->sa_mask;
431 return ((uintptr_t)sp->sa_handler);