xref: /titanic_51/usr/src/cmd/plockstat/plockstat.c (revision a1b5e537933659371285214eae1db2603e6364b4)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <assert.h>
307c478bd9Sstevel@tonic-gate #include <dtrace.h>
317c478bd9Sstevel@tonic-gate #include <limits.h>
327c478bd9Sstevel@tonic-gate #include <link.h>
337c478bd9Sstevel@tonic-gate #include <priv.h>
347c478bd9Sstevel@tonic-gate #include <signal.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include <stdarg.h>
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate #include <strings.h>
407c478bd9Sstevel@tonic-gate #include <errno.h>
417c478bd9Sstevel@tonic-gate #include <sys/wait.h>
427c478bd9Sstevel@tonic-gate #include <libproc.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate static char *g_pname;
457c478bd9Sstevel@tonic-gate static dtrace_hdl_t *g_dtp;
467c478bd9Sstevel@tonic-gate struct ps_prochandle *g_pr;
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #define	E_SUCCESS	0
497c478bd9Sstevel@tonic-gate #define	E_ERROR		1
507c478bd9Sstevel@tonic-gate #define	E_USAGE		2
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * For hold times we use a global associative array since for mutexes, in
547c478bd9Sstevel@tonic-gate  * user-land, it's not invalid to release a sychonization primitive that
557c478bd9Sstevel@tonic-gate  * another thread acquired; rwlocks require a thread-local associative array
567c478bd9Sstevel@tonic-gate  * since multiple thread can hold the same lock for reading. Note that we
577c478bd9Sstevel@tonic-gate  * ignore recursive mutex acquisitions and releases as they don't truly
587c478bd9Sstevel@tonic-gate  * affect lock contention.
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate static const char *g_hold_init =
617c478bd9Sstevel@tonic-gate "plockstat$target:::rw-acquire\n"
627c478bd9Sstevel@tonic-gate "{\n"
637c478bd9Sstevel@tonic-gate "	self->rwhold[arg0] = timestamp;\n"
647c478bd9Sstevel@tonic-gate "}\n"
657c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-acquire\n"
667c478bd9Sstevel@tonic-gate "/arg1 == 0/\n"
677c478bd9Sstevel@tonic-gate "{\n"
687c478bd9Sstevel@tonic-gate "	mtxhold[arg0] = timestamp;\n"
697c478bd9Sstevel@tonic-gate "}\n";
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate static const char *g_hold_histogram =
727c478bd9Sstevel@tonic-gate "plockstat$target:::rw-release\n"
737c478bd9Sstevel@tonic-gate "/self->rwhold[arg0] && arg1 == 1/\n"
747c478bd9Sstevel@tonic-gate "{\n"
757c478bd9Sstevel@tonic-gate "	@rw_w_hold[arg0, ustack()] =\n"
767c478bd9Sstevel@tonic-gate "	    quantize(timestamp - self->rwhold[arg0]);\n"
777c478bd9Sstevel@tonic-gate "	self->rwhold[arg0] = 0;\n"
787c478bd9Sstevel@tonic-gate "}\n"
797c478bd9Sstevel@tonic-gate "plockstat$target:::rw-release\n"
807c478bd9Sstevel@tonic-gate "/self->rwhold[arg0]/\n"
817c478bd9Sstevel@tonic-gate "{\n"
827c478bd9Sstevel@tonic-gate "	@rw_r_hold[arg0, ustack()] =\n"
837c478bd9Sstevel@tonic-gate "	    quantize(timestamp - self->rwhold[arg0]);\n"
847c478bd9Sstevel@tonic-gate "	self->rwhold[arg0] = 0;\n"
857c478bd9Sstevel@tonic-gate "}\n"
867c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-release\n"
877c478bd9Sstevel@tonic-gate "/mtxhold[arg0] && arg1 == 0/\n"
887c478bd9Sstevel@tonic-gate "{\n"
897c478bd9Sstevel@tonic-gate "	@mtx_hold[arg0, ustack()] = quantize(timestamp - mtxhold[arg0]);\n"
907c478bd9Sstevel@tonic-gate "	mtxhold[arg0] = 0;\n"
917c478bd9Sstevel@tonic-gate "}\n";
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate static const char *g_hold_times =
947c478bd9Sstevel@tonic-gate "plockstat$target:::rw-release\n"
957c478bd9Sstevel@tonic-gate "/self->rwhold[arg0] && arg1 == 1/\n"
967c478bd9Sstevel@tonic-gate "{\n"
977c478bd9Sstevel@tonic-gate "	@rw_w_hold[arg0, ustack(5)] = avg(timestamp - self->rwhold[arg0]);\n"
987c478bd9Sstevel@tonic-gate "	self->rwhold[arg0] = 0;\n"
997c478bd9Sstevel@tonic-gate "}\n"
1007c478bd9Sstevel@tonic-gate "plockstat$target:::rw-release\n"
1017c478bd9Sstevel@tonic-gate "/self->rwhold[arg0]/\n"
1027c478bd9Sstevel@tonic-gate "{\n"
1037c478bd9Sstevel@tonic-gate "	@rw_r_hold[arg0, ustack(5)] = avg(timestamp - self->rwhold[arg0]);\n"
1047c478bd9Sstevel@tonic-gate "	self->rwhold[arg0] = 0;\n"
1057c478bd9Sstevel@tonic-gate "}\n"
1067c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-release\n"
1077c478bd9Sstevel@tonic-gate "/mtxhold[arg0] && arg1 == 0/\n"
1087c478bd9Sstevel@tonic-gate "{\n"
1097c478bd9Sstevel@tonic-gate "	@mtx_hold[arg0, ustack(5)] = avg(timestamp - mtxhold[arg0]);\n"
1107c478bd9Sstevel@tonic-gate "	mtxhold[arg0] = 0;\n"
1117c478bd9Sstevel@tonic-gate "}\n";
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate  * For contention, we use thread-local associative arrays since we're tracing
1167c478bd9Sstevel@tonic-gate  * a single thread's activity in libc and multiple threads can be blocking or
1177c478bd9Sstevel@tonic-gate  * spinning on the same sychonization primitive.
1187c478bd9Sstevel@tonic-gate  */
1197c478bd9Sstevel@tonic-gate static const char *g_ctnd_init =
1207c478bd9Sstevel@tonic-gate "plockstat$target:::rw-block\n"
1217c478bd9Sstevel@tonic-gate "{\n"
1227c478bd9Sstevel@tonic-gate "	self->rwblock[arg0] = timestamp;\n"
1237c478bd9Sstevel@tonic-gate "}\n"
1247c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-block\n"
1257c478bd9Sstevel@tonic-gate "{\n"
1267c478bd9Sstevel@tonic-gate "	self->mtxblock[arg0] = timestamp;\n"
1277c478bd9Sstevel@tonic-gate "}\n"
1287c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-spin\n"
1297c478bd9Sstevel@tonic-gate "{\n"
1307c478bd9Sstevel@tonic-gate "	self->mtxspin[arg0] = timestamp;\n"
1317c478bd9Sstevel@tonic-gate "}\n";
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate static const char *g_ctnd_histogram =
1347c478bd9Sstevel@tonic-gate "plockstat$target:::rw-blocked\n"
1357c478bd9Sstevel@tonic-gate "/self->rwblock[arg0] && arg1 == 1 && arg2 != 0/\n"
1367c478bd9Sstevel@tonic-gate "{\n"
1377c478bd9Sstevel@tonic-gate "	@rw_w_block[arg0, ustack()] =\n"
1387c478bd9Sstevel@tonic-gate "	    quantize(timestamp - self->rwblock[arg0]);\n"
1397c478bd9Sstevel@tonic-gate "	self->rwblock[arg0] = 0;\n"
1407c478bd9Sstevel@tonic-gate "}\n"
1417c478bd9Sstevel@tonic-gate "plockstat$target:::rw-blocked\n"
1427c478bd9Sstevel@tonic-gate "/self->rwblock[arg0] && arg2 != 0/\n"
1437c478bd9Sstevel@tonic-gate "{\n"
1447c478bd9Sstevel@tonic-gate "	@rw_r_block[arg0, ustack()] =\n"
1457c478bd9Sstevel@tonic-gate "	    quantize(timestamp - self->rwblock[arg0]);\n"
1467c478bd9Sstevel@tonic-gate "	self->rwblock[arg0] = 0;\n"
1477c478bd9Sstevel@tonic-gate "}\n"
1487c478bd9Sstevel@tonic-gate "plockstat$target:::rw-blocked\n"
1497c478bd9Sstevel@tonic-gate "/self->rwblock[arg0]/\n"
1507c478bd9Sstevel@tonic-gate "{\n"
1517c478bd9Sstevel@tonic-gate "	self->rwblock[arg0] = 0;\n"
1527c478bd9Sstevel@tonic-gate "}\n"
1537c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-spun\n"
1547c478bd9Sstevel@tonic-gate "/self->mtxspin[arg0] && arg1 != 0/\n"
1557c478bd9Sstevel@tonic-gate "{\n"
1567c478bd9Sstevel@tonic-gate "	@mtx_spin[arg0, ustack()] =\n"
1577c478bd9Sstevel@tonic-gate "	    quantize(timestamp - self->mtxspin[arg0]);\n"
1587c478bd9Sstevel@tonic-gate "	self->mtxspin[arg0] = 0;\n"
1597c478bd9Sstevel@tonic-gate "}\n"
1607c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-spun\n"
1617c478bd9Sstevel@tonic-gate "/self->mtxspin[arg0]/\n"
1627c478bd9Sstevel@tonic-gate "{\n"
1637c478bd9Sstevel@tonic-gate "	@mtx_vain_spin[arg0, ustack()] =\n"
1647c478bd9Sstevel@tonic-gate "	    quantize(timestamp - self->mtxspin[arg0]);\n"
1657c478bd9Sstevel@tonic-gate "	self->mtxspin[arg0] = 0;\n"
1667c478bd9Sstevel@tonic-gate "}\n"
1677c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-blocked\n"
1687c478bd9Sstevel@tonic-gate "/self->mtxblock[arg0] && arg1 != 0/\n"
1697c478bd9Sstevel@tonic-gate "{\n"
1707c478bd9Sstevel@tonic-gate "	@mtx_block[arg0, ustack()] =\n"
1717c478bd9Sstevel@tonic-gate "	    quantize(timestamp - self->mtxblock[arg0]);\n"
1727c478bd9Sstevel@tonic-gate "	self->mtxblock[arg0] = 0;\n"
1737c478bd9Sstevel@tonic-gate "}\n"
1747c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-blocked\n"
1757c478bd9Sstevel@tonic-gate "/self->mtxblock[arg0]/\n"
1767c478bd9Sstevel@tonic-gate "{\n"
1777c478bd9Sstevel@tonic-gate "	self->mtxblock[arg0] = 0;\n"
1787c478bd9Sstevel@tonic-gate "}\n";
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate static const char *g_ctnd_times =
1827c478bd9Sstevel@tonic-gate "plockstat$target:::rw-blocked\n"
1837c478bd9Sstevel@tonic-gate "/self->rwblock[arg0] && arg1 == 1 && arg2 != 0/\n"
1847c478bd9Sstevel@tonic-gate "{\n"
1857c478bd9Sstevel@tonic-gate "	@rw_w_block[arg0, ustack(5)] =\n"
1867c478bd9Sstevel@tonic-gate "	    avg(timestamp - self->rwblock[arg0]);\n"
1877c478bd9Sstevel@tonic-gate "	self->rwblock[arg0] = 0;\n"
1887c478bd9Sstevel@tonic-gate "}\n"
1897c478bd9Sstevel@tonic-gate "plockstat$target:::rw-blocked\n"
1907c478bd9Sstevel@tonic-gate "/self->rwblock[arg0] && arg2 != 0/\n"
1917c478bd9Sstevel@tonic-gate "{\n"
1927c478bd9Sstevel@tonic-gate "	@rw_r_block[arg0, ustack(5)] =\n"
1937c478bd9Sstevel@tonic-gate "	    avg(timestamp - self->rwblock[arg0]);\n"
1947c478bd9Sstevel@tonic-gate "	self->rwblock[arg0] = 0;\n"
1957c478bd9Sstevel@tonic-gate "}\n"
1967c478bd9Sstevel@tonic-gate "plockstat$target:::rw-blocked\n"
1977c478bd9Sstevel@tonic-gate "/self->rwblock[arg0]/\n"
1987c478bd9Sstevel@tonic-gate "{\n"
1997c478bd9Sstevel@tonic-gate "	self->rwblock[arg0] = 0;\n"
2007c478bd9Sstevel@tonic-gate "}\n"
2017c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-spun\n"
2027c478bd9Sstevel@tonic-gate "/self->mtxspin[arg0] && arg1 != 0/\n"
2037c478bd9Sstevel@tonic-gate "{\n"
2047c478bd9Sstevel@tonic-gate "	@mtx_spin[arg0, ustack(5)] =\n"
2057c478bd9Sstevel@tonic-gate "	    avg(timestamp - self->mtxspin[arg0]);\n"
2067c478bd9Sstevel@tonic-gate "	self->mtxspin[arg0] = 0;\n"
2077c478bd9Sstevel@tonic-gate "}\n"
2087c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-spun\n"
2097c478bd9Sstevel@tonic-gate "/self->mtxspin[arg0]/\n"
2107c478bd9Sstevel@tonic-gate "{\n"
2117c478bd9Sstevel@tonic-gate "	@mtx_vain_spin[arg0, ustack(5)] =\n"
2127c478bd9Sstevel@tonic-gate "	    avg(timestamp - self->mtxspin[arg0]);\n"
2137c478bd9Sstevel@tonic-gate "	self->mtxspin[arg0] = 0;\n"
2147c478bd9Sstevel@tonic-gate "}\n"
2157c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-blocked\n"
2167c478bd9Sstevel@tonic-gate "/self->mtxblock[arg0] && arg1 != 0/\n"
2177c478bd9Sstevel@tonic-gate "{\n"
2187c478bd9Sstevel@tonic-gate "	@mtx_block[arg0, ustack(5)] =\n"
2197c478bd9Sstevel@tonic-gate "	    avg(timestamp - self->mtxblock[arg0]);\n"
2207c478bd9Sstevel@tonic-gate "	self->mtxblock[arg0] = 0;\n"
2217c478bd9Sstevel@tonic-gate "}\n"
2227c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-blocked\n"
2237c478bd9Sstevel@tonic-gate "/self->mtxblock[arg0]/\n"
2247c478bd9Sstevel@tonic-gate "{\n"
2257c478bd9Sstevel@tonic-gate "	self->mtxblock[arg0] = 0;\n"
2267c478bd9Sstevel@tonic-gate "}\n";
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate static char g_prog[2048];
2297c478bd9Sstevel@tonic-gate static size_t g_proglen;
2307c478bd9Sstevel@tonic-gate static int g_opt_V, g_opt_s;
2317c478bd9Sstevel@tonic-gate static int g_intr;
2327c478bd9Sstevel@tonic-gate static dtrace_optval_t g_nframes;
2337c478bd9Sstevel@tonic-gate static ulong_t g_nent = ULONG_MAX;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate #define	PLOCKSTAT_OPTSTR	"n:ps:e:vx:ACHV"
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate static void
2387c478bd9Sstevel@tonic-gate usage(void)
2397c478bd9Sstevel@tonic-gate {
2407c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "Usage:\n"
2417c478bd9Sstevel@tonic-gate 	    "\t%s [-vACHV] [-n count] [-s depth] [-e secs] [-x opt[=val]]\n"
2427c478bd9Sstevel@tonic-gate 	    "\t    command [arg...]\n"
2437c478bd9Sstevel@tonic-gate 	    "\t%s [-vACHV] [-n count] [-s depth] [-e secs] [-x opt[=val]]\n"
2447c478bd9Sstevel@tonic-gate 	    "\t    -p pid\n", g_pname, g_pname);
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	exit(E_USAGE);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate static void
2507c478bd9Sstevel@tonic-gate verror(const char *fmt, va_list ap)
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate 	int error = errno;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", g_pname);
2557c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	if (fmt[strlen(fmt) - 1] != '\n')
2587c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, ": %s\n", strerror(error));
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
2627c478bd9Sstevel@tonic-gate static void
2637c478bd9Sstevel@tonic-gate fatal(const char *fmt, ...)
2647c478bd9Sstevel@tonic-gate {
2657c478bd9Sstevel@tonic-gate 	va_list ap;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
2687c478bd9Sstevel@tonic-gate 	verror(fmt, ap);
2697c478bd9Sstevel@tonic-gate 	va_end(ap);
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	if (g_pr != NULL && g_dtp != NULL)
2727c478bd9Sstevel@tonic-gate 		dtrace_proc_release(g_dtp, g_pr);
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	exit(E_ERROR);
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
2787c478bd9Sstevel@tonic-gate static void
2797c478bd9Sstevel@tonic-gate dfatal(const char *fmt, ...)
2807c478bd9Sstevel@tonic-gate {
2817c478bd9Sstevel@tonic-gate 	va_list ap;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", g_pname);
2867c478bd9Sstevel@tonic-gate 	if (fmt != NULL)
2877c478bd9Sstevel@tonic-gate 		(void) vfprintf(stderr, fmt, ap);
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	va_end(ap);
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	if (fmt != NULL && fmt[strlen(fmt) - 1] != '\n') {
2927c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, ": %s\n",
2937c478bd9Sstevel@tonic-gate 		    dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
2947c478bd9Sstevel@tonic-gate 	} else if (fmt == NULL) {
2957c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s\n",
2967c478bd9Sstevel@tonic-gate 		    dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	if (g_pr != NULL) {
3007c478bd9Sstevel@tonic-gate 		dtrace_proc_continue(g_dtp, g_pr);
3017c478bd9Sstevel@tonic-gate 		dtrace_proc_release(g_dtp, g_pr);
3027c478bd9Sstevel@tonic-gate 	}
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	exit(E_ERROR);
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
3087c478bd9Sstevel@tonic-gate static void
3097c478bd9Sstevel@tonic-gate notice(const char *fmt, ...)
3107c478bd9Sstevel@tonic-gate {
3117c478bd9Sstevel@tonic-gate 	va_list ap;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
3147c478bd9Sstevel@tonic-gate 	verror(fmt, ap);
3157c478bd9Sstevel@tonic-gate 	va_end(ap);
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate static void
3197c478bd9Sstevel@tonic-gate dprog_add(const char *prog)
3207c478bd9Sstevel@tonic-gate {
3217c478bd9Sstevel@tonic-gate 	size_t len = strlen(prog);
3227c478bd9Sstevel@tonic-gate 	bcopy(prog, g_prog + g_proglen, len + 1);
3237c478bd9Sstevel@tonic-gate 	g_proglen += len;
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate static void
3277c478bd9Sstevel@tonic-gate dprog_compile(void)
3287c478bd9Sstevel@tonic-gate {
3297c478bd9Sstevel@tonic-gate 	dtrace_prog_t *prog;
3307c478bd9Sstevel@tonic-gate 	dtrace_proginfo_t info;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	if (g_opt_V) {
3337c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: vvvv D program vvvv\n", g_pname);
3347c478bd9Sstevel@tonic-gate 		(void) fputs(g_prog, stderr);
3357c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: ^^^^ D program ^^^^\n", g_pname);
3367c478bd9Sstevel@tonic-gate 	}
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	if ((prog = dtrace_program_strcompile(g_dtp, g_prog,
3397c478bd9Sstevel@tonic-gate 	    DTRACE_PROBESPEC_NAME, 0, 0, NULL)) == NULL)
3407c478bd9Sstevel@tonic-gate 		dfatal("failed to compile program");
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	if (dtrace_program_exec(g_dtp, prog, &info) == -1)
3437c478bd9Sstevel@tonic-gate 		dfatal("failed to enable probes");
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate static void
3477c478bd9Sstevel@tonic-gate print_header(const char *aggname)
3487c478bd9Sstevel@tonic-gate {
3497c478bd9Sstevel@tonic-gate 	if (strcmp(aggname, "mtx_hold") == 0) {
3507c478bd9Sstevel@tonic-gate 		(void) printf("\nMutex hold\n\n");
3517c478bd9Sstevel@tonic-gate 	} else if (strcmp(aggname, "mtx_block") == 0) {
3527c478bd9Sstevel@tonic-gate 		(void) printf("\nMutex block\n\n");
3537c478bd9Sstevel@tonic-gate 	} else if (strcmp(aggname, "mtx_spin") == 0) {
3547c478bd9Sstevel@tonic-gate 		(void) printf("\nMutex spin\n\n");
3557c478bd9Sstevel@tonic-gate 	} else if (strcmp(aggname, "mtx_vain_spin") == 0) {
3567c478bd9Sstevel@tonic-gate 		(void) printf("\nMutex unsuccessful spin\n\n");
3577c478bd9Sstevel@tonic-gate 	} else if (strcmp(aggname, "rw_r_hold") == 0) {
3587c478bd9Sstevel@tonic-gate 		(void) printf("\nR/W reader hold\n\n");
3597c478bd9Sstevel@tonic-gate 	} else if (strcmp(aggname, "rw_w_hold") == 0) {
3607c478bd9Sstevel@tonic-gate 		(void) printf("\nR/W writer hold\n\n");
3617c478bd9Sstevel@tonic-gate 	} else if (strcmp(aggname, "rw_r_block") == 0) {
3627c478bd9Sstevel@tonic-gate 		(void) printf("\nR/W reader block\n\n");
3637c478bd9Sstevel@tonic-gate 	} else if (strcmp(aggname, "rw_w_block") == 0) {
3647c478bd9Sstevel@tonic-gate 		(void) printf("\nR/W writer block\n\n");
3657c478bd9Sstevel@tonic-gate 	}
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate void
3697c478bd9Sstevel@tonic-gate print_legend(void)
3707c478bd9Sstevel@tonic-gate {
3717c478bd9Sstevel@tonic-gate 	(void) printf("%5s %8s %-28s %s\n", "Count", "nsec", "Lock", "Caller");
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate void
3757c478bd9Sstevel@tonic-gate print_bar(void)
3767c478bd9Sstevel@tonic-gate {
3777c478bd9Sstevel@tonic-gate 	(void) printf("---------------------------------------"
3787c478bd9Sstevel@tonic-gate 	    "----------------------------------------\n");
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate void
3827c478bd9Sstevel@tonic-gate print_histogram_header(void)
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	(void) printf("\n%10s ---- Time Distribution --- %5s %s\n",
3857c478bd9Sstevel@tonic-gate 	    "nsec", "count", "Stack");
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate /*
3897c478bd9Sstevel@tonic-gate  * Convert an address to a symbolic string or a numeric string. If nolocks
3907c478bd9Sstevel@tonic-gate  * is set, we return an error code if this symbol appears to be a mutex- or
3917c478bd9Sstevel@tonic-gate  * rwlock-related symbol in libc so the caller has a chance to find a more
3927c478bd9Sstevel@tonic-gate  * helpful symbol.
3937c478bd9Sstevel@tonic-gate  */
3947c478bd9Sstevel@tonic-gate static int
3957c478bd9Sstevel@tonic-gate getsym(struct ps_prochandle *P, uintptr_t addr, char *buf, size_t size,
3967c478bd9Sstevel@tonic-gate     int nolocks)
3977c478bd9Sstevel@tonic-gate {
3987c478bd9Sstevel@tonic-gate 	char name[256];
3997c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
4007c478bd9Sstevel@tonic-gate 	prsyminfo_t info;
4017c478bd9Sstevel@tonic-gate 	size_t len;
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	if (P == NULL || Pxlookup_by_addr(P, addr, name, sizeof (name),
4047c478bd9Sstevel@tonic-gate 	    &sym, &info) != 0) {
4057c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, size, "%#lx", addr);
4067c478bd9Sstevel@tonic-gate 		return (0);
4077c478bd9Sstevel@tonic-gate 	}
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	if (info.prs_lmid != LM_ID_BASE) {
4107c478bd9Sstevel@tonic-gate 		len = snprintf(buf, size, "LM%lu`", info.prs_lmid);
4117c478bd9Sstevel@tonic-gate 		buf += len;
4127c478bd9Sstevel@tonic-gate 		size -= len;
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	len = snprintf(buf, size, "%s`%s", info.prs_object, info.prs_name);
4167c478bd9Sstevel@tonic-gate 	buf += len;
4177c478bd9Sstevel@tonic-gate 	size -= len;
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	if (sym.st_value != addr)
4207c478bd9Sstevel@tonic-gate 		len = snprintf(buf, size, "+%#lx", addr - sym.st_value);
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	if (nolocks && strcmp("libc.so.1", info.prs_object) == 0 &&
4237c478bd9Sstevel@tonic-gate 	    (strstr("mutex", info.prs_name) == 0 ||
4247c478bd9Sstevel@tonic-gate 	    strstr("rw", info.prs_name) == 0))
4257c478bd9Sstevel@tonic-gate 		return (-1);
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	return (0);
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4317c478bd9Sstevel@tonic-gate static int
432*a1b5e537Sbmc process_aggregate(const dtrace_aggdata_t *agg, void *arg)
4337c478bd9Sstevel@tonic-gate {
4347c478bd9Sstevel@tonic-gate 	static dtrace_aggid_t last = DTRACE_AGGIDNONE;
4357c478bd9Sstevel@tonic-gate 	static uint_t nent;
436*a1b5e537Sbmc 	const dtrace_aggdesc_t *aggdesc = agg->dtada_desc;
4377c478bd9Sstevel@tonic-gate 	caddr_t data = agg->dtada_data;
438*a1b5e537Sbmc 	const dtrace_recdesc_t *rec;
4397c478bd9Sstevel@tonic-gate 	uint64_t *a, count, avg;
4400b38a8bdSahl 	char buf[256];
4417c478bd9Sstevel@tonic-gate 	uintptr_t lock;
4427c478bd9Sstevel@tonic-gate 	pid_t pid;
4437c478bd9Sstevel@tonic-gate 	uint64_t *stack;
4447c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P;
4457c478bd9Sstevel@tonic-gate 	int i, j;
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	if (aggdesc->dtagd_id != last) {
4487c478bd9Sstevel@tonic-gate 		print_header(aggdesc->dtagd_name);
4497c478bd9Sstevel@tonic-gate 		nent = 0;
4507c478bd9Sstevel@tonic-gate 	}
4517c478bd9Sstevel@tonic-gate 	if (nent >= g_nent)
4527c478bd9Sstevel@tonic-gate 		goto out;
4537c478bd9Sstevel@tonic-gate 	nent++;
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	rec = aggdesc->dtagd_rec;
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	/*LINTED - alignment*/
4587c478bd9Sstevel@tonic-gate 	lock = (uintptr_t)*(uint64_t *)(data + rec[1].dtrd_offset);
4597c478bd9Sstevel@tonic-gate 	/*LINTED - alignment*/
4607c478bd9Sstevel@tonic-gate 	stack = (uint64_t *)(data + rec[2].dtrd_offset);
4617c478bd9Sstevel@tonic-gate 	/*LINTED - alignment*/
4627c478bd9Sstevel@tonic-gate 	a = (uint64_t *)(data + rec[aggdesc->dtagd_nrecs - 1].dtrd_offset);
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	if (!g_opt_s) {
4657c478bd9Sstevel@tonic-gate 		if (aggdesc->dtagd_id != last) {
4667c478bd9Sstevel@tonic-gate 			print_legend();
4677c478bd9Sstevel@tonic-gate 			print_bar();
4687c478bd9Sstevel@tonic-gate 		}
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 		count = a[0];
4717c478bd9Sstevel@tonic-gate 		avg = a[1] / a[0];
4727c478bd9Sstevel@tonic-gate 	} else {
4737c478bd9Sstevel@tonic-gate 		print_bar();
4747c478bd9Sstevel@tonic-gate 		print_legend();
4757c478bd9Sstevel@tonic-gate 		count = avg = 0;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 		for (i = DTRACE_QUANTIZE_ZEROBUCKET, j = 0;
4787c478bd9Sstevel@tonic-gate 		    i < DTRACE_QUANTIZE_NBUCKETS; i++, j++) {
4797c478bd9Sstevel@tonic-gate 			count += a[i];
4807c478bd9Sstevel@tonic-gate 			avg += a[i] << (j - 64);
4817c478bd9Sstevel@tonic-gate 		}
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 		avg /= count;
4847c478bd9Sstevel@tonic-gate 	}
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	(void) printf("%5llu %8llu ", (u_longlong_t)count, (u_longlong_t)avg);
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	pid = stack[0];
4897c478bd9Sstevel@tonic-gate 	P = dtrace_proc_grab(g_dtp, pid, PGRAB_RDONLY);
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	(void) getsym(P, lock, buf, sizeof (buf), 0);
4927c478bd9Sstevel@tonic-gate 	(void) printf("%-28s ", buf);
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	for (i = 2; i <= 5; i++) {
4957c478bd9Sstevel@tonic-gate 		if (getsym(P, stack[i], buf, sizeof (buf), 1) == 0)
4967c478bd9Sstevel@tonic-gate 			break;
4977c478bd9Sstevel@tonic-gate 	}
4987c478bd9Sstevel@tonic-gate 	(void) printf("%s\n", buf);
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	if (g_opt_s) {
5017c478bd9Sstevel@tonic-gate 		int stack_done = 0;
5027c478bd9Sstevel@tonic-gate 		int quant_done = 0;
5037c478bd9Sstevel@tonic-gate 		int first_bin, last_bin;
5047c478bd9Sstevel@tonic-gate 		uint64_t bin_size;
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 		print_histogram_header();
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 		for (first_bin = DTRACE_QUANTIZE_ZEROBUCKET;
5097c478bd9Sstevel@tonic-gate 		    a[first_bin] == 0; first_bin++)
5107c478bd9Sstevel@tonic-gate 			continue;
5117c478bd9Sstevel@tonic-gate 		for (last_bin = DTRACE_QUANTIZE_ZEROBUCKET + 63;
5127c478bd9Sstevel@tonic-gate 		    a[last_bin] == 0; last_bin--)
5137c478bd9Sstevel@tonic-gate 			continue;
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 		for (i = 0; !stack_done || !quant_done; i++) {
5167c478bd9Sstevel@tonic-gate 			if (!stack_done) {
5177c478bd9Sstevel@tonic-gate 				(void) getsym(P, stack[i + 2], buf,
5187c478bd9Sstevel@tonic-gate 				    sizeof (buf), 0);
5197c478bd9Sstevel@tonic-gate 			} else {
5207c478bd9Sstevel@tonic-gate 				buf[0] = '\0';
5217c478bd9Sstevel@tonic-gate 			}
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 			if (!quant_done) {
5247c478bd9Sstevel@tonic-gate 				bin_size = a[first_bin];
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 				(void) printf("%10llu |%-24.*s| %5llu %s\n",
5277c478bd9Sstevel@tonic-gate 				    1ULL <<
5287c478bd9Sstevel@tonic-gate 				    (first_bin - DTRACE_QUANTIZE_ZEROBUCKET),
5297c478bd9Sstevel@tonic-gate 				    (int)(24.0 * bin_size / count),
5307c478bd9Sstevel@tonic-gate 				    "@@@@@@@@@@@@@@@@@@@@@@@@@@",
5317c478bd9Sstevel@tonic-gate 				    (u_longlong_t)bin_size, buf);
5327c478bd9Sstevel@tonic-gate 			} else {
5337c478bd9Sstevel@tonic-gate 				(void) printf("%43s %s\n", "", buf);
5347c478bd9Sstevel@tonic-gate 			}
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 			if (i + 1 >= g_nframes || stack[i + 3] == 0)
5377c478bd9Sstevel@tonic-gate 				stack_done = 1;
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 			if (first_bin++ == last_bin)
5407c478bd9Sstevel@tonic-gate 				quant_done = 1;
5417c478bd9Sstevel@tonic-gate 		}
5427c478bd9Sstevel@tonic-gate 	}
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	dtrace_proc_release(g_dtp, P);
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate out:
5477c478bd9Sstevel@tonic-gate 	last = aggdesc->dtagd_id;
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	return (DTRACE_AGGWALK_NEXT);
5507c478bd9Sstevel@tonic-gate }
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate static int
5537c478bd9Sstevel@tonic-gate prochandler(struct ps_prochandle *P)
5547c478bd9Sstevel@tonic-gate {
5557c478bd9Sstevel@tonic-gate 	const psinfo_t *prp = Ppsinfo(P);
5567c478bd9Sstevel@tonic-gate 	int pid = Pstatus(P)->pr_pid;
5577c478bd9Sstevel@tonic-gate 	char name[SIG2STR_MAX];
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	switch (Pstate(P)) {
5607c478bd9Sstevel@tonic-gate 	case PS_UNDEAD:
5617c478bd9Sstevel@tonic-gate 		/*
5627c478bd9Sstevel@tonic-gate 		 * Ideally we would like to always report pr_wstat here, but it
5637c478bd9Sstevel@tonic-gate 		 * isn't possible given current /proc semantics.  If we grabbed
5647c478bd9Sstevel@tonic-gate 		 * the process, Ppsinfo() will either fail or return a zeroed
5657c478bd9Sstevel@tonic-gate 		 * psinfo_t depending on how far the parent is in reaping it.
5667c478bd9Sstevel@tonic-gate 		 * When /proc provides a stable pr_wstat in the status file,
5677c478bd9Sstevel@tonic-gate 		 * this code can be improved by examining this new pr_wstat.
5687c478bd9Sstevel@tonic-gate 		 */
5697c478bd9Sstevel@tonic-gate 		if (prp != NULL && WIFSIGNALED(prp->pr_wstat)) {
5707c478bd9Sstevel@tonic-gate 			notice("pid %d terminated by %s\n", pid,
5717c478bd9Sstevel@tonic-gate 			    proc_signame(WTERMSIG(prp->pr_wstat),
5727c478bd9Sstevel@tonic-gate 			    name, sizeof (name)));
5737c478bd9Sstevel@tonic-gate 		} else if (prp != NULL && WEXITSTATUS(prp->pr_wstat) != 0) {
5747c478bd9Sstevel@tonic-gate 			notice("pid %d exited with status %d\n",
5757c478bd9Sstevel@tonic-gate 			    pid, WEXITSTATUS(prp->pr_wstat));
5767c478bd9Sstevel@tonic-gate 		} else {
5777c478bd9Sstevel@tonic-gate 			notice("pid %d has exited\n", pid);
5787c478bd9Sstevel@tonic-gate 		}
5797c478bd9Sstevel@tonic-gate 		return (1);
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	case PS_LOST:
5827c478bd9Sstevel@tonic-gate 		notice("pid %d exec'd a set-id or unobservable program\n", pid);
5837c478bd9Sstevel@tonic-gate 		return (1);
5847c478bd9Sstevel@tonic-gate 	}
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	return (0);
5877c478bd9Sstevel@tonic-gate }
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5907c478bd9Sstevel@tonic-gate static void
5917c478bd9Sstevel@tonic-gate intr(int signo)
5927c478bd9Sstevel@tonic-gate {
5937c478bd9Sstevel@tonic-gate 	g_intr = 1;
5947c478bd9Sstevel@tonic-gate }
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate int
5977c478bd9Sstevel@tonic-gate main(int argc, char **argv)
5987c478bd9Sstevel@tonic-gate {
5997c478bd9Sstevel@tonic-gate 	ucred_t *ucp;
6007c478bd9Sstevel@tonic-gate 	int err;
6017c478bd9Sstevel@tonic-gate 	int opt_C = 0, opt_H = 0, opt_p = 0, opt_v = 0;
6027c478bd9Sstevel@tonic-gate 	char c, *p, *end;
6037c478bd9Sstevel@tonic-gate 	struct sigaction act;
6047c478bd9Sstevel@tonic-gate 	int done = 0;
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	if ((g_pname = strrchr(argv[0], '/')) == NULL)
6077c478bd9Sstevel@tonic-gate 		g_pname = argv[0];
6087c478bd9Sstevel@tonic-gate 	else
6097c478bd9Sstevel@tonic-gate 		argv[0] = ++g_pname;		/* for getopt() */
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	/*
6127c478bd9Sstevel@tonic-gate 	 * Make sure we have the required dtrace_proc privilege.
6137c478bd9Sstevel@tonic-gate 	 */
6147c478bd9Sstevel@tonic-gate 	if ((ucp = ucred_get(getpid())) != NULL) {
6157c478bd9Sstevel@tonic-gate 		const priv_set_t *psp;
6167c478bd9Sstevel@tonic-gate 		if ((psp = ucred_getprivset(ucp, PRIV_EFFECTIVE)) != NULL &&
6177c478bd9Sstevel@tonic-gate 		    !priv_ismember(psp, PRIV_DTRACE_PROC)) {
6187c478bd9Sstevel@tonic-gate 			fatal("dtrace_proc privilege required\n");
6197c478bd9Sstevel@tonic-gate 		}
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 		ucred_free(ucp);
6227c478bd9Sstevel@tonic-gate 	}
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, PLOCKSTAT_OPTSTR)) != EOF) {
6257c478bd9Sstevel@tonic-gate 		switch (c) {
6267c478bd9Sstevel@tonic-gate 		case 'n':
6277c478bd9Sstevel@tonic-gate 			errno = 0;
6287c478bd9Sstevel@tonic-gate 			g_nent = strtoul(optarg, &end, 10);
6297c478bd9Sstevel@tonic-gate 			if (*end != '\0' || errno != 0) {
6307c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "%s: invalid count "
6317c478bd9Sstevel@tonic-gate 				    "'%s'\n", g_pname, optarg);
6327c478bd9Sstevel@tonic-gate 				usage();
6337c478bd9Sstevel@tonic-gate 			}
6347c478bd9Sstevel@tonic-gate 			break;
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 		case 'p':
6377c478bd9Sstevel@tonic-gate 			opt_p = 1;
6387c478bd9Sstevel@tonic-gate 			break;
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 		case 'v':
6417c478bd9Sstevel@tonic-gate 			opt_v = 1;
6427c478bd9Sstevel@tonic-gate 			break;
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 		case 'A':
6457c478bd9Sstevel@tonic-gate 			opt_C = opt_H = 1;
6467c478bd9Sstevel@tonic-gate 			break;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 		case 'C':
6497c478bd9Sstevel@tonic-gate 			opt_C = 1;
6507c478bd9Sstevel@tonic-gate 			break;
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 		case 'H':
6537c478bd9Sstevel@tonic-gate 			opt_H = 1;
6547c478bd9Sstevel@tonic-gate 			break;
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 		case 'V':
6577c478bd9Sstevel@tonic-gate 			g_opt_V = 1;
6587c478bd9Sstevel@tonic-gate 			break;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 		default:
6617c478bd9Sstevel@tonic-gate 			if (strchr(PLOCKSTAT_OPTSTR, c) == NULL)
6627c478bd9Sstevel@tonic-gate 				usage();
6637c478bd9Sstevel@tonic-gate 		}
6647c478bd9Sstevel@tonic-gate 	}
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	/*
6677c478bd9Sstevel@tonic-gate 	 * We need a command or at least one pid.
6687c478bd9Sstevel@tonic-gate 	 */
6697c478bd9Sstevel@tonic-gate 	if (argc == optind)
6707c478bd9Sstevel@tonic-gate 		usage();
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	if (opt_C == 0 && opt_H == 0)
6737c478bd9Sstevel@tonic-gate 		opt_C = 1;
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	if ((g_dtp = dtrace_open(DTRACE_VERSION, 0, &err)) == NULL)
6767c478bd9Sstevel@tonic-gate 		fatal("failed to initialize dtrace: %s\n",
6777c478bd9Sstevel@tonic-gate 		    dtrace_errmsg(NULL, err));
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	if (dtrace_setopt(g_dtp, "aggsize", "256k") == -1)
6807c478bd9Sstevel@tonic-gate 		dfatal("failed to set 'aggsize'");
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	if (dtrace_setopt(g_dtp, "aggrate", "1sec") == -1)
6837c478bd9Sstevel@tonic-gate 		dfatal("failed to set 'aggrate'");
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	/*
6867c478bd9Sstevel@tonic-gate 	 * Take a second pass through to look for options that set options now
6877c478bd9Sstevel@tonic-gate 	 * that we have an open dtrace handle.
6887c478bd9Sstevel@tonic-gate 	 */
6897c478bd9Sstevel@tonic-gate 	optind = 1;
6907c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, PLOCKSTAT_OPTSTR)) != EOF) {
6917c478bd9Sstevel@tonic-gate 		switch (c) {
6927c478bd9Sstevel@tonic-gate 		case 's':
6937c478bd9Sstevel@tonic-gate 			g_opt_s = 1;
6947c478bd9Sstevel@tonic-gate 			if (dtrace_setopt(g_dtp, "ustackframes", optarg) == -1)
6957c478bd9Sstevel@tonic-gate 				dfatal("failed to set 'ustackframes'");
6967c478bd9Sstevel@tonic-gate 			break;
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 		case 'x':
6997c478bd9Sstevel@tonic-gate 			if ((p = strchr(optarg, '=')) != NULL)
7007c478bd9Sstevel@tonic-gate 				*p++ = '\0';
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 			if (dtrace_setopt(g_dtp, optarg, p) != 0)
7037c478bd9Sstevel@tonic-gate 				dfatal("failed to set -x %s", optarg);
7047c478bd9Sstevel@tonic-gate 			break;
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 		case 'e':
7077c478bd9Sstevel@tonic-gate 			errno = 0;
7087c478bd9Sstevel@tonic-gate 			(void) strtoul(optarg, &end, 10);
7097c478bd9Sstevel@tonic-gate 			if (*optarg == '-' || *end != '\0' || errno != 0) {
7107c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "%s: invalid timeout "
7117c478bd9Sstevel@tonic-gate 				    "'%s'\n", g_pname, optarg);
7127c478bd9Sstevel@tonic-gate 				usage();
7137c478bd9Sstevel@tonic-gate 			}
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 			/*
7167c478bd9Sstevel@tonic-gate 			 * Construct a DTrace enabling that will exit after
7177c478bd9Sstevel@tonic-gate 			 * the specified number of seconds.
7187c478bd9Sstevel@tonic-gate 			 */
7197c478bd9Sstevel@tonic-gate 			dprog_add("BEGIN\n{\n\tend = timestamp + ");
7207c478bd9Sstevel@tonic-gate 			dprog_add(optarg);
7217c478bd9Sstevel@tonic-gate 			dprog_add(" * 1000000000;\n}\n");
7227c478bd9Sstevel@tonic-gate 			dprog_add("tick-10hz\n/timestamp >= end/\n");
7237c478bd9Sstevel@tonic-gate 			dprog_add("{\n\texit(0);\n}\n");
7247c478bd9Sstevel@tonic-gate 			break;
7257c478bd9Sstevel@tonic-gate 		}
7267c478bd9Sstevel@tonic-gate 	}
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	argc -= optind;
7297c478bd9Sstevel@tonic-gate 	argv += optind;
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	if (opt_H) {
7327c478bd9Sstevel@tonic-gate 		dprog_add(g_hold_init);
7337c478bd9Sstevel@tonic-gate 		if (g_opt_s == NULL)
7347c478bd9Sstevel@tonic-gate 			dprog_add(g_hold_times);
7357c478bd9Sstevel@tonic-gate 		else
7367c478bd9Sstevel@tonic-gate 			dprog_add(g_hold_histogram);
7377c478bd9Sstevel@tonic-gate 	}
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	if (opt_C) {
7407c478bd9Sstevel@tonic-gate 		dprog_add(g_ctnd_init);
7417c478bd9Sstevel@tonic-gate 		if (g_opt_s == NULL)
7427c478bd9Sstevel@tonic-gate 			dprog_add(g_ctnd_times);
7437c478bd9Sstevel@tonic-gate 		else
7447c478bd9Sstevel@tonic-gate 			dprog_add(g_ctnd_histogram);
7457c478bd9Sstevel@tonic-gate 	}
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	if (opt_p) {
7487c478bd9Sstevel@tonic-gate 		ulong_t pid;
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 		if (argc > 1) {
7517c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: only one pid is allowed\n",
7527c478bd9Sstevel@tonic-gate 			    g_pname);
7537c478bd9Sstevel@tonic-gate 			usage();
7547c478bd9Sstevel@tonic-gate 		}
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 		errno = 0;
7577c478bd9Sstevel@tonic-gate 		pid = strtoul(argv[0], &end, 10);
7587c478bd9Sstevel@tonic-gate 		if (*end != '\0' || errno != 0 || (pid_t)pid != pid) {
7597c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: invalid pid '%s'\n",
7607c478bd9Sstevel@tonic-gate 			    g_pname, argv[0]);
7617c478bd9Sstevel@tonic-gate 			usage();
7627c478bd9Sstevel@tonic-gate 		}
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 		if ((g_pr = dtrace_proc_grab(g_dtp, (pid_t)pid, 0)) == NULL)
7657c478bd9Sstevel@tonic-gate 			dfatal(NULL);
7667c478bd9Sstevel@tonic-gate 	} else {
7677c478bd9Sstevel@tonic-gate 		if ((g_pr = dtrace_proc_create(g_dtp, argv[0], argv)) == NULL)
7687c478bd9Sstevel@tonic-gate 			dfatal(NULL);
7697c478bd9Sstevel@tonic-gate 	}
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 	dprog_compile();
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
7747c478bd9Sstevel@tonic-gate 	act.sa_flags = 0;
7757c478bd9Sstevel@tonic-gate 	act.sa_handler = intr;
7767c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGINT, &act, NULL);
7777c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGTERM, &act, NULL);
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	if (dtrace_go(g_dtp) != 0)
7807c478bd9Sstevel@tonic-gate 		dfatal("dtrace_go()");
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	if (dtrace_getopt(g_dtp, "ustackframes", &g_nframes) != 0)
7837c478bd9Sstevel@tonic-gate 		dfatal("failed to get 'ustackframes'");
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	dtrace_proc_continue(g_dtp, g_pr);
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	if (opt_v)
7887c478bd9Sstevel@tonic-gate 		(void) printf("%s: tracing enabled for pid %d\n", g_pname,
7897c478bd9Sstevel@tonic-gate 		    (int)Pstatus(g_pr)->pr_pid);
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	while (!done) {
7927c478bd9Sstevel@tonic-gate 		(void) sleep(1);
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 		if (dtrace_status(g_dtp) == DTRACE_STATUS_EXITED)
7957c478bd9Sstevel@tonic-gate 			done = 1;
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 		/* Done if the user hits control-C. */
7987c478bd9Sstevel@tonic-gate 		if (g_intr)
7997c478bd9Sstevel@tonic-gate 			done = 1;
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 		if (prochandler(g_pr) == 1)
8027c478bd9Sstevel@tonic-gate 			done = 1;
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 		if (dtrace_aggregate_snap(g_dtp) != 0)
8057c478bd9Sstevel@tonic-gate 			dfatal("failed to add to aggregate");
8067c478bd9Sstevel@tonic-gate 	}
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	if (dtrace_aggregate_snap(g_dtp) != 0)
8097c478bd9Sstevel@tonic-gate 		dfatal("failed to add to aggregate");
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	if (dtrace_aggregate_walk_valrevsorted(g_dtp,
8127c478bd9Sstevel@tonic-gate 	    process_aggregate, NULL) != 0)
8137c478bd9Sstevel@tonic-gate 		dfatal("failed to print aggregations");
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	dtrace_close(g_dtp);
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	return (0);
8187c478bd9Sstevel@tonic-gate }
819