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