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]
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 #pragma ident "%Z%%M% %I% %E% SMI"
46 #include <libproc_compat.h>
49 static dtrace_hdl_t *g_dtp;
50 struct ps_prochandle *g_pr;
57 * For hold times we use a global associative array since for mutexes, in
58 * user-land, it's not invalid to release a sychonization primitive that
59 * another thread acquired; rwlocks require a thread-local associative array
60 * since multiple thread can hold the same lock for reading. Note that we
61 * ignore recursive mutex acquisitions and releases as they don't truly
62 * affect lock contention.
64 static const char *g_hold_init =
65 "plockstat$target:::rw-acquire\n"
67 " self->rwhold[arg0] = timestamp;\n"
69 "plockstat$target:::mutex-acquire\n"
72 " mtxhold[arg0] = timestamp;\n"
75 static const char *g_hold_histogram =
76 "plockstat$target:::rw-release\n"
77 "/self->rwhold[arg0] && arg1 == 1/\n"
79 " @rw_w_hold[arg0, ustack()] =\n"
80 " quantize(timestamp - self->rwhold[arg0]);\n"
81 " self->rwhold[arg0] = 0;\n"
82 " rw_w_hold_found = 1;\n"
84 "plockstat$target:::rw-release\n"
85 "/self->rwhold[arg0]/\n"
87 " @rw_r_hold[arg0, ustack()] =\n"
88 " quantize(timestamp - self->rwhold[arg0]);\n"
89 " self->rwhold[arg0] = 0;\n"
90 " rw_r_hold_found = 1;\n"
92 "plockstat$target:::mutex-release\n"
93 "/mtxhold[arg0] && arg1 == 0/\n"
95 " @mtx_hold[arg0, ustack()] = quantize(timestamp - mtxhold[arg0]);\n"
96 " mtxhold[arg0] = 0;\n"
97 " mtx_hold_found = 1;\n"
103 " trace(\"Mutex hold\");\n"
104 " printa(@mtx_hold);\n"
107 "/rw_r_hold_found/\n"
109 " trace(\"R/W reader hold\");\n"
110 " printa(@rw_r_hold);\n"
113 "/rw_w_hold_found/\n"
115 " trace(\"R/W writer hold\");\n"
116 " printa(@rw_w_hold);\n"
119 static const char *g_hold_times =
120 "plockstat$target:::rw-release\n"
121 "/self->rwhold[arg0] && arg1 == 1/\n"
123 " @rw_w_hold[arg0, ustack(5)] = sum(timestamp - self->rwhold[arg0]);\n"
124 " @rw_w_hold_count[arg0, ustack(5)] = count();\n"
125 " self->rwhold[arg0] = 0;\n"
126 " rw_w_hold_found = 1;\n"
128 "plockstat$target:::rw-release\n"
129 "/self->rwhold[arg0]/\n"
131 " @rw_r_hold[arg0, ustack(5)] = sum(timestamp - self->rwhold[arg0]);\n"
132 " @rw_r_hold_count[arg0, ustack(5)] = count();\n"
133 " self->rwhold[arg0] = 0;\n"
134 " rw_r_hold_found = 1;\n"
136 "plockstat$target:::mutex-release\n"
137 "/mtxhold[arg0] && arg1 == 0/\n"
139 " @mtx_hold[arg0, ustack(5)] = sum(timestamp - mtxhold[arg0]);\n"
140 " @mtx_hold_count[arg0, ustack(5)] = count();\n"
141 " mtxhold[arg0] = 0;\n"
142 " mtx_hold_found = 1;\n"
148 " trace(\"Mutex hold\");\n"
149 " printa(@mtx_hold, @mtx_hold_count);\n"
152 "/rw_r_hold_found/\n"
154 " trace(\"R/W reader hold\");\n"
155 " printa(@rw_r_hold, @rw_r_hold_count);\n"
158 "/rw_w_hold_found/\n"
160 " trace(\"R/W writer hold\");\n"
161 " printa(@rw_w_hold, @rw_w_hold_count);\n"
166 * For contention, we use thread-local associative arrays since we're tracing
167 * a single thread's activity in libc and multiple threads can be blocking or
168 * spinning on the same sychonization primitive.
170 static const char *g_ctnd_init =
171 "plockstat$target:::rw-block\n"
173 " self->rwblock[arg0] = timestamp;\n"
175 "plockstat$target:::mutex-block\n"
177 " self->mtxblock[arg0] = timestamp;\n"
179 "plockstat$target:::mutex-spin\n"
181 " self->mtxspin[arg0] = timestamp;\n"
184 static const char *g_ctnd_histogram =
185 "plockstat$target:::rw-blocked\n"
186 "/self->rwblock[arg0] && arg1 == 1 && arg2 != 0/\n"
188 " @rw_w_block[arg0, ustack()] =\n"
189 " quantize(timestamp - self->rwblock[arg0]);\n"
190 " self->rwblock[arg0] = 0;\n"
191 " rw_w_block_found = 1;\n"
193 "plockstat$target:::rw-blocked\n"
194 "/self->rwblock[arg0] && arg2 != 0/\n"
196 " @rw_r_block[arg0, ustack()] =\n"
197 " quantize(timestamp - self->rwblock[arg0]);\n"
198 " self->rwblock[arg0] = 0;\n"
199 " rw_r_block_found = 1;\n"
201 "plockstat$target:::rw-blocked\n"
202 "/self->rwblock[arg0]/\n"
204 " self->rwblock[arg0] = 0;\n"
206 "plockstat$target:::mutex-spun\n"
207 "/self->mtxspin[arg0] && arg1 != 0/\n"
209 " @mtx_spin[arg0, ustack()] =\n"
210 " quantize(timestamp - self->mtxspin[arg0]);\n"
211 " self->mtxspin[arg0] = 0;\n"
212 " mtx_spin_found = 1;\n"
214 "plockstat$target:::mutex-spun\n"
215 "/self->mtxspin[arg0]/\n"
217 " @mtx_vain_spin[arg0, ustack()] =\n"
218 " quantize(timestamp - self->mtxspin[arg0]);\n"
219 " self->mtxspin[arg0] = 0;\n"
220 " mtx_vain_spin_found = 1;\n"
222 "plockstat$target:::mutex-blocked\n"
223 "/self->mtxblock[arg0] && arg1 != 0/\n"
225 " @mtx_block[arg0, ustack()] =\n"
226 " quantize(timestamp - self->mtxblock[arg0]);\n"
227 " self->mtxblock[arg0] = 0;\n"
228 " mtx_block_found = 1;\n"
230 "plockstat$target:::mutex-blocked\n"
231 "/self->mtxblock[arg0]/\n"
233 " self->mtxblock[arg0] = 0;\n"
237 "/mtx_block_found/\n"
239 " trace(\"Mutex block\");\n"
240 " printa(@mtx_block);\n"
245 " trace(\"Mutex spin\");\n"
246 " printa(@mtx_spin);\n"
249 "/mtx_vain_spin_found/\n"
251 " trace(\"Mutex unsuccessful spin\");\n"
252 " printa(@mtx_vain_spin);\n"
255 "/rw_r_block_found/\n"
257 " trace(\"R/W reader block\");\n"
258 " printa(@rw_r_block);\n"
261 "/rw_w_block_found/\n"
263 " trace(\"R/W writer block\");\n"
264 " printa(@rw_w_block);\n"
268 static const char *g_ctnd_times =
269 "plockstat$target:::rw-blocked\n"
270 "/self->rwblock[arg0] && arg1 == 1 && arg2 != 0/\n"
272 " @rw_w_block[arg0, ustack(5)] =\n"
273 " sum(timestamp - self->rwblock[arg0]);\n"
274 " @rw_w_block_count[arg0, ustack(5)] = count();\n"
275 " self->rwblock[arg0] = 0;\n"
276 " rw_w_block_found = 1;\n"
278 "plockstat$target:::rw-blocked\n"
279 "/self->rwblock[arg0] && arg2 != 0/\n"
281 " @rw_r_block[arg0, ustack(5)] =\n"
282 " sum(timestamp - self->rwblock[arg0]);\n"
283 " @rw_r_block_count[arg0, ustack(5)] = count();\n"
284 " self->rwblock[arg0] = 0;\n"
285 " rw_r_block_found = 1;\n"
287 "plockstat$target:::rw-blocked\n"
288 "/self->rwblock[arg0]/\n"
290 " self->rwblock[arg0] = 0;\n"
292 "plockstat$target:::mutex-spun\n"
293 "/self->mtxspin[arg0] && arg1 != 0/\n"
295 " @mtx_spin[arg0, ustack(5)] =\n"
296 " sum(timestamp - self->mtxspin[arg0]);\n"
297 " @mtx_spin_count[arg0, ustack(5)] = count();\n"
298 " self->mtxspin[arg0] = 0;\n"
299 " mtx_spin_found = 1;\n"
301 "plockstat$target:::mutex-spun\n"
302 "/self->mtxspin[arg0]/\n"
304 " @mtx_vain_spin[arg0, ustack(5)] =\n"
305 " sum(timestamp - self->mtxspin[arg0]);\n"
306 " @mtx_vain_spin_count[arg0, ustack(5)] = count();\n"
307 " self->mtxspin[arg0] = 0;\n"
308 " mtx_vain_spin_found = 1;\n"
310 "plockstat$target:::mutex-blocked\n"
311 "/self->mtxblock[arg0] && arg1 != 0/\n"
313 " @mtx_block[arg0, ustack(5)] =\n"
314 " sum(timestamp - self->mtxblock[arg0]);\n"
315 " @mtx_block_count[arg0, ustack(5)] = count();\n"
316 " self->mtxblock[arg0] = 0;\n"
317 " mtx_block_found = 1;\n"
319 "plockstat$target:::mutex-blocked\n"
320 "/self->mtxblock[arg0]/\n"
322 " self->mtxblock[arg0] = 0;\n"
326 "/mtx_block_found/\n"
328 " trace(\"Mutex block\");\n"
329 " printa(@mtx_block, @mtx_block_count);\n"
334 " trace(\"Mutex spin\");\n"
335 " printa(@mtx_spin, @mtx_spin_count);\n"
338 "/mtx_vain_spin_found/\n"
340 " trace(\"Mutex unsuccessful spin\");\n"
341 " printa(@mtx_vain_spin, @mtx_vain_spin_count);\n"
344 "/rw_r_block_found/\n"
346 " trace(\"R/W reader block\");\n"
347 " printa(@rw_r_block, @rw_r_block_count);\n"
350 "/rw_w_block_found/\n"
352 " trace(\"R/W writer block\");\n"
353 " printa(@rw_w_block, @rw_w_block_count);\n"
356 static char g_prog[4096];
357 static size_t g_proglen;
358 static int g_opt_V, g_opt_s;
361 static dtrace_optval_t g_nframes;
362 static ulong_t g_nent = ULONG_MAX;
364 #define PLOCKSTAT_OPTSTR "n:ps:e:vx:ACHV"
369 (void) fprintf(stderr, "Usage:\n"
370 "\t%s [-vACHV] [-n count] [-s depth] [-e secs] [-x opt[=val]]\n"
371 "\t command [arg...]\n"
372 "\t%s [-vACHV] [-n count] [-s depth] [-e secs] [-x opt[=val]]\n"
373 "\t -p pid\n", g_pname, g_pname);
379 verror(const char *fmt, va_list ap)
383 (void) fprintf(stderr, "%s: ", g_pname);
384 (void) vfprintf(stderr, fmt, ap);
386 if (fmt[strlen(fmt) - 1] != '\n')
387 (void) fprintf(stderr, ": %s\n", strerror(error));
392 fatal(const char *fmt, ...)
400 if (g_pr != NULL && g_dtp != NULL)
401 dtrace_proc_release(g_dtp, g_pr);
408 dfatal(const char *fmt, ...)
414 (void) fprintf(stderr, "%s: ", g_pname);
416 (void) vfprintf(stderr, fmt, ap);
420 if (fmt != NULL && fmt[strlen(fmt) - 1] != '\n') {
421 (void) fprintf(stderr, ": %s\n",
422 dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
423 } else if (fmt == NULL) {
424 (void) fprintf(stderr, "%s\n",
425 dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
429 dtrace_proc_continue(g_dtp, g_pr);
430 dtrace_proc_release(g_dtp, g_pr);
438 notice(const char *fmt, ...)
448 dprog_add(const char *prog)
450 size_t len = strlen(prog);
451 bcopy(prog, g_prog + g_proglen, len + 1);
453 assert(g_proglen < sizeof (g_prog));
460 dtrace_proginfo_t info;
463 (void) fprintf(stderr, "%s: vvvv D program vvvv\n", g_pname);
464 (void) fputs(g_prog, stderr);
465 (void) fprintf(stderr, "%s: ^^^^ D program ^^^^\n", g_pname);
468 if ((prog = dtrace_program_strcompile(g_dtp, g_prog,
469 DTRACE_PROBESPEC_NAME, 0, 0, NULL)) == NULL)
470 dfatal("failed to compile program");
472 if (dtrace_program_exec(g_dtp, prog, &info) == -1)
473 dfatal("failed to enable probes");
479 (void) printf("%5s %8s %-28s %s\n", "Count", "nsec", "Lock", "Caller");
485 (void) printf("---------------------------------------"
486 "----------------------------------------\n");
490 print_histogram_header(void)
492 (void) printf("\n%10s ---- Time Distribution --- %5s %s\n",
493 "nsec", "count", "Stack");
497 * Convert an address to a symbolic string or a numeric string. If nolocks
498 * is set, we return an error code if this symbol appears to be a mutex- or
499 * rwlock-related symbol in libc so the caller has a chance to find a more
503 getsym(struct ps_prochandle *P, uintptr_t addr, char *buf, size_t size,
512 int info; /* XXX unused */
516 if (P == NULL || Pxlookup_by_addr(P, addr, name, sizeof (name),
518 (void) snprintf(buf, size, "%#lx", addr);
522 if (info.prs_object == NULL)
523 info.prs_object = "<unknown>";
525 if (info.prs_lmid != LM_ID_BASE) {
526 len = snprintf(buf, size, "LM%lu`", info.prs_lmid);
531 len = snprintf(buf, size, "%s`%s", info.prs_object, info.prs_name);
533 map = proc_addr2map(P, addr);
534 len = snprintf(buf, size, "%s`%s", map->pr_mapname, name);
539 if (sym.st_value != addr)
540 len = snprintf(buf, size, "+%#lx", addr - sym.st_value);
542 if (nolocks && strcmp("libc.so.1", map->pr_mapname) == 0 &&
543 (strstr("mutex", name) == 0 ||
544 strstr("rw", name) == 0))
552 process_aggregate(const dtrace_aggdata_t **aggsdata, int naggvars, void *arg)
554 const dtrace_recdesc_t *rec;
559 struct ps_prochandle *P;
562 uint64_t sum, count, avg;
564 if ((*(uint_t *)arg)++ >= g_nent)
565 return (DTRACE_AGGWALK_NEXT);
567 rec = aggsdata[0]->dtada_desc->dtagd_rec;
568 data = aggsdata[0]->dtada_data;
570 /*LINTED - alignment*/
571 lock = (uintptr_t)*(uint64_t *)(data + rec[1].dtrd_offset);
572 /*LINTED - alignment*/
573 stack = (uint64_t *)(data + rec[2].dtrd_offset);
576 /*LINTED - alignment*/
577 sum = *(uint64_t *)(aggsdata[1]->dtada_data +
578 aggsdata[1]->dtada_desc->dtagd_rec[3].dtrd_offset);
579 /*LINTED - alignment*/
580 count = *(uint64_t *)(aggsdata[2]->dtada_data +
581 aggsdata[2]->dtada_desc->dtagd_rec[3].dtrd_offset);
585 /*LINTED - alignment*/
586 a = (uint64_t *)(aggsdata[1]->dtada_data +
587 aggsdata[1]->dtada_desc->dtagd_rec[3].dtrd_offset);
592 for (count = sum = 0, i = DTRACE_QUANTIZE_ZEROBUCKET, j = 0;
593 i < DTRACE_QUANTIZE_NBUCKETS; i++, j++) {
595 sum += a[i] << (j - 64);
600 (void) printf("%5llu %8llu ", (u_longlong_t)count, (u_longlong_t)avg);
603 P = dtrace_proc_grab(g_dtp, pid, PGRAB_RDONLY);
605 (void) getsym(P, lock, buf, sizeof (buf), 0);
606 (void) printf("%-28s ", buf);
608 for (i = 2; i <= 5; i++) {
609 if (getsym(P, stack[i], buf, sizeof (buf), 1) == 0)
612 (void) printf("%s\n", buf);
617 int first_bin, last_bin;
618 uint64_t bin_size, *a;
620 /*LINTED - alignment*/
621 a = (uint64_t *)(aggsdata[1]->dtada_data +
622 aggsdata[1]->dtada_desc->dtagd_rec[3].dtrd_offset);
624 print_histogram_header();
626 for (first_bin = DTRACE_QUANTIZE_ZEROBUCKET;
627 a[first_bin] == 0; first_bin++)
629 for (last_bin = DTRACE_QUANTIZE_ZEROBUCKET + 63;
630 a[last_bin] == 0; last_bin--)
633 for (i = 0; !stack_done || !quant_done; i++) {
635 (void) getsym(P, stack[i + 2], buf,
642 bin_size = a[first_bin];
644 (void) printf("%10llu |%-24.*s| %5llu %s\n",
646 (first_bin - DTRACE_QUANTIZE_ZEROBUCKET),
647 (int)(24.0 * bin_size / count),
648 "@@@@@@@@@@@@@@@@@@@@@@@@@@",
649 (u_longlong_t)bin_size, buf);
651 (void) printf("%43s %s\n", "", buf);
654 if (i + 1 >= g_nframes || stack[i + 3] == 0)
657 if (first_bin++ == last_bin)
662 dtrace_proc_release(g_dtp, P);
664 return (DTRACE_AGGWALK_NEXT);
669 prochandler(struct ps_prochandle *P, const char *msg, void *arg)
672 const psinfo_t *prp = Ppsinfo(P);
673 int pid = Pstatus(P)->pr_pid;
675 int pid = proc_getpid(P);
676 int wstat = proc_getwstat(P);
678 char name[SIG2STR_MAX];
681 notice("pid %d: %s\n", pid, msg);
688 * Ideally we would like to always report pr_wstat here, but it
689 * isn't possible given current /proc semantics. If we grabbed
690 * the process, Ppsinfo() will either fail or return a zeroed
691 * psinfo_t depending on how far the parent is in reaping it.
692 * When /proc provides a stable pr_wstat in the status file,
693 * this code can be improved by examining this new pr_wstat.
695 if (WIFSIGNALED(wstat)) {
696 notice("pid %d terminated by %s\n", pid,
697 proc_signame(WTERMSIG(wstat),
698 name, sizeof (name)));
699 } else if (WEXITSTATUS(wstat) != 0) {
700 notice("pid %d exited with status %d\n",
701 pid, WEXITSTATUS(wstat));
703 notice("pid %d has exited\n", pid);
709 notice("pid %d exec'd a set-id or unobservable program\n", pid);
717 chewrec(const dtrace_probedata_t *data, const dtrace_recdesc_t *rec, void *arg)
719 dtrace_eprobedesc_t *epd = data->dtpda_edesc;
720 dtrace_aggvarid_t aggvars[2];
725 * A NULL rec indicates that we've processed the last record.
728 return (DTRACE_CONSUME_NEXT);
730 buf = data->dtpda_data - rec->dtrd_offset;
732 switch (rec->dtrd_action) {
733 case DTRACEACT_DIFEXPR:
734 (void) printf("\n%s\n\n", (char *)buf + rec->dtrd_offset);
739 return (DTRACE_CONSUME_NEXT);
741 case DTRACEACT_PRINTA:
742 for (nagv = 0, i = 0; i < epd->dtepd_nrecs - 1; i++) {
743 const dtrace_recdesc_t *nrec = &rec[i];
745 if (nrec->dtrd_uarg != rec->dtrd_uarg)
748 /*LINTED - alignment*/
749 aggvars[nagv++] = *(dtrace_aggvarid_t *)((caddr_t)buf +
753 if (nagv == (g_opt_s ? 1 : 2)) {
755 if (dtrace_aggregate_walk_joined(g_dtp, aggvars, nagv,
756 process_aggregate, &nent) != 0)
757 dfatal("failed to walk aggregate");
760 return (DTRACE_CONSUME_NEXT);
763 return (DTRACE_CONSUME_THIS);
774 main(int argc, char **argv)
780 int opt_C = 0, opt_H = 0, opt_p = 0, opt_v = 0;
782 struct sigaction act;
785 g_pname = basename(argv[0]);
786 argv[0] = g_pname; /* rewrite argv[0] for getopt errors */
789 * Make sure we have the required dtrace_proc privilege.
791 if ((ucp = ucred_get(getpid())) != NULL) {
792 const priv_set_t *psp;
793 if ((psp = ucred_getprivset(ucp, PRIV_EFFECTIVE)) != NULL &&
794 !priv_ismember(psp, PRIV_DTRACE_PROC)) {
795 fatal("dtrace_proc privilege required\n");
802 while ((c = getopt(argc, argv, PLOCKSTAT_OPTSTR)) != EOF) {
806 g_nent = strtoul(optarg, &end, 10);
807 if (*end != '\0' || errno != 0) {
808 (void) fprintf(stderr, "%s: invalid count "
809 "'%s'\n", g_pname, optarg);
839 if (strchr(PLOCKSTAT_OPTSTR, c) == NULL)
845 * We need a command or at least one pid.
850 if (opt_C == 0 && opt_H == 0)
853 if ((g_dtp = dtrace_open(DTRACE_VERSION, 0, &err)) == NULL)
854 fatal("failed to initialize dtrace: %s\n",
855 dtrace_errmsg(NULL, err));
858 * The longest string we trace is 23 bytes long -- so 32 is plenty.
860 if (dtrace_setopt(g_dtp, "strsize", "32") == -1)
861 dfatal("failed to set 'strsize'");
864 * 1k should be more than enough for all trace() and printa() actions.
866 if (dtrace_setopt(g_dtp, "bufsize", "1k") == -1)
867 dfatal("failed to set 'bufsize'");
870 * The table we produce has the hottest locks at the top.
872 if (dtrace_setopt(g_dtp, "aggsortrev", NULL) == -1)
873 dfatal("failed to set 'aggsortrev'");
876 * These are two reasonable defaults which should suffice.
878 if (dtrace_setopt(g_dtp, "aggsize", "256k") == -1)
879 dfatal("failed to set 'aggsize'");
880 if (dtrace_setopt(g_dtp, "aggrate", "1sec") == -1)
881 dfatal("failed to set 'aggrate'");
884 * Take a second pass through to look for options that set options now
885 * that we have an open dtrace handle.
888 while ((c = getopt(argc, argv, PLOCKSTAT_OPTSTR)) != EOF) {
892 if (dtrace_setopt(g_dtp, "ustackframes", optarg) == -1)
893 dfatal("failed to set 'ustackframes'");
897 if ((p = strchr(optarg, '=')) != NULL)
900 if (dtrace_setopt(g_dtp, optarg, p) != 0)
901 dfatal("failed to set -x %s", optarg);
906 (void) strtoul(optarg, &end, 10);
907 if (*optarg == '-' || *end != '\0' || errno != 0) {
908 (void) fprintf(stderr, "%s: invalid timeout "
909 "'%s'\n", g_pname, optarg);
914 * Construct a DTrace enabling that will exit after
915 * the specified number of seconds.
917 dprog_add("BEGIN\n{\n\tend = timestamp + ");
919 dprog_add(" * 1000000000;\n}\n");
920 dprog_add("tick-10hz\n/timestamp >= end/\n");
921 dprog_add("{\n\texit(0);\n}\n");
930 dprog_add(g_hold_init);
932 dprog_add(g_hold_times);
934 dprog_add(g_hold_histogram);
938 dprog_add(g_ctnd_init);
940 dprog_add(g_ctnd_times);
942 dprog_add(g_ctnd_histogram);
949 (void) fprintf(stderr, "%s: only one pid is allowed\n",
955 pid = strtoul(argv[0], &end, 10);
956 if (*end != '\0' || errno != 0 || (pid_t)pid != pid) {
957 (void) fprintf(stderr, "%s: invalid pid '%s'\n",
962 if ((g_pr = dtrace_proc_grab(g_dtp, (pid_t)pid, 0)) == NULL)
965 if ((g_pr = dtrace_proc_create(g_dtp, argv[0], argv, NULL, NULL)) == NULL)
971 if (dtrace_handle_proc(g_dtp, &prochandler, NULL) == -1)
972 dfatal("failed to establish proc handler");
974 (void) sigemptyset(&act.sa_mask);
976 act.sa_handler = intr;
977 (void) sigaction(SIGINT, &act, NULL);
978 (void) sigaction(SIGTERM, &act, NULL);
980 if (dtrace_go(g_dtp) != 0)
981 dfatal("dtrace_go()");
983 if (dtrace_getopt(g_dtp, "ustackframes", &g_nframes) != 0)
984 dfatal("failed to get 'ustackframes'");
986 dtrace_proc_continue(g_dtp, g_pr);
989 (void) printf("%s: tracing enabled for pid %d\n", g_pname,
991 (int)Pstatus(g_pr)->pr_pid);
993 (int)proc_getpid(g_pr));
997 if (!g_intr && !done)
1000 if (done || g_intr || g_exited) {
1002 if (dtrace_stop(g_dtp) == -1)
1003 dfatal("couldn't stop tracing");
1006 switch (dtrace_work(g_dtp, stdout, NULL, chewrec, NULL)) {
1007 case DTRACE_WORKSTATUS_DONE:
1010 case DTRACE_WORKSTATUS_OKAY:
1013 dfatal("processing aborted");
1018 dtrace_close(g_dtp);