xref: /titanic_51/usr/src/cmd/ptools/psig/psig.c (revision 1e49577a7fcde812700ded04431b49d67cc57d6d)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <stdio.h>
29 #include <stdio_ext.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <ctype.h>
33 #include <fcntl.h>
34 #include <string.h>
35 #include <signal.h>
36 #include <stddef.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <libproc.h>
40 
41 /* evil knowledge of libc internals */
42 #include "../../../lib/libc/inc/thr_uberdata.h"
43 
44 #define	MAX_SYMNAMLEN	1024	/* Recommended max symbol name length */
45 
46 static	char	*sigflags(int, int);
47 static	int	look(char *);
48 static	void	perr(char *);
49 static	int	usage(void);
50 static	uintptr_t deinterpose(int, void *, psinfo_t *, struct sigaction *);
51 
52 static	char	*command;
53 static	char	*procname;
54 static	int	all_flag = 0;
55 static	int	lookuphandlers_flag = 1;
56 
57 int
58 main(int argc, char **argv)
59 {
60 	int rc = 0;
61 	int c;
62 	struct rlimit rlim;
63 
64 	if ((command = strrchr(argv[0], '/')) != NULL)
65 		command++;
66 	else
67 		command = argv[0];
68 
69 	while ((c = getopt(argc, argv, "an")) != EOF) {
70 		switch (c) {
71 		case 'a':
72 			all_flag = 1;
73 			break;
74 		case 'n':
75 			lookuphandlers_flag = 0;
76 			break;
77 		default:
78 			return (usage());
79 		}
80 	}
81 
82 	if (argc - optind < 1) {
83 		return (usage());
84 	}
85 
86 	/*
87 	 * Make sure we'll have enough file descriptors to handle a target
88 	 * that has many many mappings.
89 	 */
90 	if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
91 		rlim.rlim_cur = rlim.rlim_max;
92 		(void) setrlimit(RLIMIT_NOFILE, &rlim);
93 		(void) enable_extended_FILE_stdio(-1, -1);
94 	}
95 
96 	for (; optind != argc; optind++) {
97 		rc += look(argv[optind]);
98 	}
99 
100 	return (rc);
101 }
102 
103 static int
104 usage(void)
105 {
106 	(void) fprintf(stderr, "usage:\t%s [-n] pid ...\n", command);
107 	(void) fprintf(stderr, "  (report process signal actions)\n");
108 
109 	return (2);
110 }
111 
112 static uintptr_t
113 uberdata_addr(struct ps_prochandle *Pr, char dmodel)
114 {
115 	GElf_Sym sym;
116 
117 	if (Plookup_by_name(Pr, "libc.so", "_tdb_bootstrap", &sym) < 0)
118 		return (NULL);
119 #ifdef _LP64
120 	if (dmodel != PR_MODEL_NATIVE) {
121 		caddr32_t uaddr;
122 		caddr32_t addr;
123 
124 		if (Pread(Pr, &addr, sizeof (addr), sym.st_value)
125 		    == sizeof (addr) &&
126 		    addr != 0 &&
127 		    Pread(Pr, &uaddr, sizeof (uaddr), (uintptr_t)addr)
128 		    == sizeof (uaddr) &&
129 		    uaddr != 0)
130 			return ((uintptr_t)uaddr);
131 	}
132 #endif
133 	if (dmodel == PR_MODEL_NATIVE) {
134 		uintptr_t uaddr;
135 		uintptr_t addr;
136 
137 		if (Pread(Pr, &addr, sizeof (addr), sym.st_value)
138 		    == sizeof (addr) &&
139 		    addr != 0 &&
140 		    Pread(Pr, &uaddr, sizeof (uaddr), addr)
141 		    == sizeof (uaddr) &&
142 		    uaddr != 0)
143 			return (uaddr);
144 	}
145 	if (Plookup_by_name(Pr, "libc.so", "_uberdata", &sym) < 0)
146 		return (0);
147 	return (sym.st_value);
148 }
149 
150 /*
151  * Iterator function used to generate the process sigmask
152  * from the individual lwp sigmasks.
153  */
154 static int
155 lwp_iter(void *cd, const lwpstatus_t *lwpstatus)
156 {
157 	sigset_t *ssp = cd;
158 
159 	ssp->__sigbits[0] &= lwpstatus->pr_lwphold.__sigbits[0];
160 	ssp->__sigbits[1] &= lwpstatus->pr_lwphold.__sigbits[1];
161 	ssp->__sigbits[2] &= lwpstatus->pr_lwphold.__sigbits[2];
162 	ssp->__sigbits[3] &= lwpstatus->pr_lwphold.__sigbits[3];
163 
164 	/*
165 	 * Return non-zero to terminate the iteration
166 	 * if the sigmask has become all zeros.
167 	 */
168 	return ((ssp->__sigbits[0] | ssp->__sigbits[1] |
169 	    ssp->__sigbits[2] | ssp->__sigbits[3]) == 0);
170 }
171 
172 static int
173 look(char *arg)
174 {
175 	char pathname[100];
176 	struct stat statb;
177 	int fd = -1;
178 	int sig, gcode;
179 	sigset_t holdmask;
180 	int maxsig;
181 	struct sigaction *action = NULL;
182 	psinfo_t psinfo;
183 	const psinfo_t *psinfop;
184 	struct ps_prochandle *Pr = NULL;
185 	uintptr_t uberaddr;
186 	uintptr_t aharraddr;
187 	uintptr_t intfnaddr;
188 	size_t aharrlen;
189 	void *aharr = NULL;
190 	int error = 1;
191 
192 	procname = arg;		/* for perr() */
193 	if ((Pr = proc_arg_grab(arg, PR_ARG_PIDS, PGRAB_RDONLY|PGRAB_FORCE,
194 	    &gcode)) == NULL || (psinfop = Ppsinfo(Pr)) == NULL) {
195 		(void) fprintf(stderr, "%s: cannot examine %s: %s\n",
196 		    command, arg, Pgrab_error(gcode));
197 		goto look_error;
198 	}
199 	(void) memcpy(&psinfo, psinfop, sizeof (psinfo_t));
200 	proc_unctrl_psinfo(&psinfo);
201 
202 	(void) sprintf(pathname, "/proc/%d/sigact", (int)psinfo.pr_pid);
203 	if ((fd = open(pathname, O_RDONLY)) < 0) {
204 		perr("open sigact");
205 		goto look_error;
206 	}
207 
208 	if (fstat(fd, &statb) != 0) {
209 		perr("fstat sigact");
210 		goto look_error;
211 	}
212 	maxsig = statb.st_size / sizeof (struct sigaction);
213 	action = malloc(maxsig * sizeof (struct sigaction));
214 	if (action == NULL) {
215 		(void) fprintf(stderr,
216 		"%s: cannot malloc() space for %d sigaction structures\n",
217 			command, maxsig);
218 		goto look_error;
219 	}
220 	if (read(fd, (char *)action, maxsig * sizeof (struct sigaction)) !=
221 	    maxsig * sizeof (struct sigaction)) {
222 		perr("read sigact");
223 		goto look_error;
224 	}
225 	(void) close(fd);
226 	fd = -1;
227 
228 	(void) printf("%d:\t%.70s\n", (int)psinfo.pr_pid, psinfo.pr_psargs);
229 
230 	(void) sigfillset(&holdmask);
231 	(void) Plwp_iter(Pr, lwp_iter, &holdmask);
232 
233 	if ((uberaddr = uberdata_addr(Pr, psinfo.pr_dmodel)) == 0) {
234 		aharraddr = 0;
235 		aharrlen = 0;
236 		intfnaddr = 0;
237 	} else {
238 #ifdef _LP64
239 		if (psinfo.pr_dmodel != PR_MODEL_NATIVE) {
240 			caddr32_t addr;
241 			aharraddr = uberaddr +
242 				offsetof(uberdata32_t, siguaction);
243 			aharrlen = sizeof (siguaction32_t) * NSIG;
244 			(void) Pread(Pr, &addr, sizeof (addr),
245 			    uberaddr + offsetof(uberdata32_t, sigacthandler));
246 			intfnaddr = (uintptr_t)addr;
247 		} else
248 #endif
249 		{
250 			aharraddr = uberaddr +
251 				offsetof(uberdata_t, siguaction);
252 			aharrlen = sizeof (siguaction_t) * NSIG;
253 			(void) Pread(Pr, &intfnaddr, sizeof (intfnaddr),
254 			    uberaddr + offsetof(uberdata_t, sigacthandler));
255 		}
256 	}
257 
258 	if (aharraddr) {
259 		aharr = malloc(aharrlen);
260 		if (aharr == NULL) {
261 			(void) fprintf(stderr,
262 			"%s: cannot malloc() space for actual handler array\n",
263 			    command);
264 			goto look_error;
265 		}
266 
267 		if (Pread(Pr, aharr, aharrlen, aharraddr) != aharrlen) {
268 			(void) fprintf(stderr,
269 			    "%s: signal handler data at %p cannot be read.\n",
270 			    command, (void *)aharraddr);
271 			free(aharr);
272 			aharr = NULL;
273 		}
274 	}
275 
276 	for (sig = 1; sig <= maxsig; sig++) {
277 		struct sigaction *sp = &action[sig - 1];
278 		int caught = 0;
279 		char buf[SIG2STR_MAX];
280 		char *s;
281 
282 		/* proc_signame() returns "SIG..."; skip the "SIG" part */
283 		(void) printf("%s\t", proc_signame(sig, buf, sizeof (buf)) + 3);
284 
285 		if (prismember(&holdmask, sig))
286 			(void) printf("blocked,");
287 
288 		if (sp->sa_handler == SIG_DFL)
289 			(void) printf("default");
290 		else if (sp->sa_handler == SIG_IGN)
291 			(void) printf("ignored");
292 		else
293 			caught = 1;
294 
295 		if (caught || all_flag) {
296 			uintptr_t haddr;
297 			GElf_Sym hsym;
298 			char hname[MAX_SYMNAMLEN];
299 			char buf[PRSIGBUFSZ];
300 
301 			haddr = (uintptr_t)sp->sa_handler;
302 
303 			if (aharr && intfnaddr && haddr == intfnaddr)
304 				haddr = deinterpose(sig, aharr, &psinfo, sp);
305 
306 			if (haddr == (uintptr_t)SIG_DFL) {
307 				if (caught)
308 					(void) printf("default");
309 				caught = 0;
310 			} else if (haddr == (uintptr_t)SIG_IGN) {
311 				if (caught)
312 					(void) printf("ignored");
313 				caught = 0;
314 			} else {
315 				if (caught)
316 					(void) printf("caught");
317 			}
318 
319 			if (caught || all_flag) {
320 				if (lookuphandlers_flag && haddr > 1 &&
321 				    Plookup_by_addr(Pr, haddr, hname,
322 				    sizeof (hname), &hsym) == 0)
323 					(void) printf("\t%-8s", hname);
324 				else
325 					(void) printf("\t0x%-8lx",
326 						(ulong_t)haddr);
327 
328 				s = sigflags(sig, sp->sa_flags);
329 				(void) printf("%s", (*s != '\0')? s : "\t0");
330 				(void) proc_sigset2str(&sp->sa_mask, ",", 1,
331 				    buf, sizeof (buf));
332 				if (buf[0] != '\0')
333 					(void) printf("\t%s", buf);
334 			}
335 		} else if (sig == SIGCLD) {
336 			s = sigflags(sig,
337 				sp->sa_flags & (SA_NOCLDWAIT|SA_NOCLDSTOP));
338 			if (*s != '\0')
339 				(void) printf("\t\t%s", s);
340 		}
341 		(void) printf("\n");
342 	}
343 
344 	error = 0;
345 
346 look_error:
347 	if (fd >= 0)
348 		(void) close(fd);
349 	if (aharr)
350 		free(aharr);
351 	if (action)
352 		free(action);
353 	if (Pr)
354 		Prelease(Pr, 0);
355 	return (error);
356 }
357 
358 static void
359 perr(char *s)
360 {
361 	if (s)
362 		(void) fprintf(stderr, "%s: ", procname);
363 	else
364 		s = procname;
365 	perror(s);
366 }
367 
368 static char *
369 sigflags(int sig, int flags)
370 {
371 	static char code_buf[100];
372 	char *str = code_buf;
373 	int flagmask =
374 		(SA_ONSTACK|SA_RESETHAND|SA_RESTART|SA_SIGINFO|SA_NODEFER);
375 
376 	if (sig == SIGCLD)
377 		flagmask |= (SA_NOCLDSTOP|SA_NOCLDWAIT);
378 
379 	*str = '\0';
380 	if (flags & ~flagmask)
381 		(void) sprintf(str, ",0x%x,", flags & ~flagmask);
382 	else if (flags == 0)
383 		return (str);
384 
385 	if (flags & SA_RESTART)
386 		(void) strcat(str, ",RESTART");
387 	if (flags & SA_RESETHAND)
388 		(void) strcat(str, ",RESETHAND");
389 	if (flags & SA_ONSTACK)
390 		(void) strcat(str, ",ONSTACK");
391 	if (flags & SA_SIGINFO)
392 		(void) strcat(str, ",SIGINFO");
393 	if (flags & SA_NODEFER)
394 		(void) strcat(str, ",NODEFER");
395 
396 	if (sig == SIGCLD) {
397 		if (flags & SA_NOCLDWAIT)
398 			(void) strcat(str, ",NOCLDWAIT");
399 		if (flags & SA_NOCLDSTOP)
400 			(void) strcat(str, ",NOCLDSTOP");
401 	}
402 
403 	*str = '\t';
404 
405 	return (str);
406 }
407 
408 /*ARGSUSED2*/
409 static uintptr_t
410 deinterpose(int sig, void *aharr, psinfo_t *psinfo, struct sigaction *sp)
411 {
412 	if (sp->sa_handler == SIG_DFL || sp->sa_handler == SIG_IGN)
413 		return ((uintptr_t)sp->sa_handler);
414 #ifdef _LP64
415 	if (psinfo->pr_dmodel != PR_MODEL_NATIVE) {
416 		struct sigaction32 *sa32 = (struct sigaction32 *)
417 			((uintptr_t)aharr + sig * sizeof (siguaction32_t) +
418 			offsetof(siguaction32_t, sig_uaction));
419 
420 		sp->sa_flags = sa32->sa_flags;
421 		sp->sa_handler = (void (*)())(uintptr_t)sa32->sa_handler;
422 		(void) memcpy(&sp->sa_mask, &sa32->sa_mask,
423 			sizeof (sp->sa_mask));
424 	} else
425 #endif
426 	{
427 		struct sigaction *sa = (struct sigaction *)
428 			((uintptr_t)aharr + sig * sizeof (siguaction_t) +
429 			offsetof(siguaction_t, sig_uaction));
430 
431 		sp->sa_flags = sa->sa_flags;
432 		sp->sa_handler = sa->sa_handler;
433 		sp->sa_mask = sa->sa_mask;
434 	}
435 	return ((uintptr_t)sp->sa_handler);
436 }
437