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.
27 #pragma ident "%Z%%M% %I% %E% SMI"
46 static dtrace_hdl_t
*g_dtp
;
47 struct ps_prochandle
*g_pr
;
54 * For hold times we use a global associative array since for mutexes, in
55 * user-land, it's not invalid to release a sychonization primitive that
56 * another thread acquired; rwlocks require a thread-local associative array
57 * since multiple thread can hold the same lock for reading. Note that we
58 * ignore recursive mutex acquisitions and releases as they don't truly
59 * affect lock contention.
61 static const char *g_hold_init
=
62 "plockstat$target:::rw-acquire\n"
64 " self->rwhold[arg0] = timestamp;\n"
66 "plockstat$target:::mutex-acquire\n"
69 " mtxhold[arg0] = timestamp;\n"
72 static const char *g_hold_histogram
=
73 "plockstat$target:::rw-release\n"
74 "/self->rwhold[arg0] && arg1 == 1/\n"
76 " @rw_w_hold[arg0, ustack()] =\n"
77 " quantize(timestamp - self->rwhold[arg0]);\n"
78 " self->rwhold[arg0] = 0;\n"
79 " rw_w_hold_found = 1;\n"
81 "plockstat$target:::rw-release\n"
82 "/self->rwhold[arg0]/\n"
84 " @rw_r_hold[arg0, ustack()] =\n"
85 " quantize(timestamp - self->rwhold[arg0]);\n"
86 " self->rwhold[arg0] = 0;\n"
87 " rw_r_hold_found = 1;\n"
89 "plockstat$target:::mutex-release\n"
90 "/mtxhold[arg0] && arg1 == 0/\n"
92 " @mtx_hold[arg0, ustack()] = quantize(timestamp - mtxhold[arg0]);\n"
93 " mtxhold[arg0] = 0;\n"
94 " mtx_hold_found = 1;\n"
100 " trace(\"Mutex hold\");\n"
101 " printa(@mtx_hold);\n"
104 "/rw_r_hold_found/\n"
106 " trace(\"R/W reader hold\");\n"
107 " printa(@rw_r_hold);\n"
110 "/rw_w_hold_found/\n"
112 " trace(\"R/W writer hold\");\n"
113 " printa(@rw_w_hold);\n"
116 static const char *g_hold_times
=
117 "plockstat$target:::rw-release\n"
118 "/self->rwhold[arg0] && arg1 == 1/\n"
120 " @rw_w_hold[arg0, ustack(5)] = sum(timestamp - self->rwhold[arg0]);\n"
121 " @rw_w_hold_count[arg0, ustack(5)] = count();\n"
122 " self->rwhold[arg0] = 0;\n"
123 " rw_w_hold_found = 1;\n"
125 "plockstat$target:::rw-release\n"
126 "/self->rwhold[arg0]/\n"
128 " @rw_r_hold[arg0, ustack(5)] = sum(timestamp - self->rwhold[arg0]);\n"
129 " @rw_r_hold_count[arg0, ustack(5)] = count();\n"
130 " self->rwhold[arg0] = 0;\n"
131 " rw_r_hold_found = 1;\n"
133 "plockstat$target:::mutex-release\n"
134 "/mtxhold[arg0] && arg1 == 0/\n"
136 " @mtx_hold[arg0, ustack(5)] = sum(timestamp - mtxhold[arg0]);\n"
137 " @mtx_hold_count[arg0, ustack(5)] = count();\n"
138 " mtxhold[arg0] = 0;\n"
139 " mtx_hold_found = 1;\n"
145 " trace(\"Mutex hold\");\n"
146 " printa(@mtx_hold, @mtx_hold_count);\n"
149 "/rw_r_hold_found/\n"
151 " trace(\"R/W reader hold\");\n"
152 " printa(@rw_r_hold, @rw_r_hold_count);\n"
155 "/rw_w_hold_found/\n"
157 " trace(\"R/W writer hold\");\n"
158 " printa(@rw_w_hold, @rw_w_hold_count);\n"
163 * For contention, we use thread-local associative arrays since we're tracing
164 * a single thread's activity in libc and multiple threads can be blocking or
165 * spinning on the same sychonization primitive.
167 static const char *g_ctnd_init
=
168 "plockstat$target:::rw-block\n"
170 " self->rwblock[arg0] = timestamp;\n"
172 "plockstat$target:::mutex-block\n"
174 " self->mtxblock[arg0] = timestamp;\n"
176 "plockstat$target:::mutex-spin\n"
178 " self->mtxspin[arg0] = timestamp;\n"
181 static const char *g_ctnd_histogram
=
182 "plockstat$target:::rw-blocked\n"
183 "/self->rwblock[arg0] && arg1 == 1 && arg2 != 0/\n"
185 " @rw_w_block[arg0, ustack()] =\n"
186 " quantize(timestamp - self->rwblock[arg0]);\n"
187 " self->rwblock[arg0] = 0;\n"
188 " rw_w_block_found = 1;\n"
190 "plockstat$target:::rw-blocked\n"
191 "/self->rwblock[arg0] && arg2 != 0/\n"
193 " @rw_r_block[arg0, ustack()] =\n"
194 " quantize(timestamp - self->rwblock[arg0]);\n"
195 " self->rwblock[arg0] = 0;\n"
196 " rw_r_block_found = 1;\n"
198 "plockstat$target:::rw-blocked\n"
199 "/self->rwblock[arg0]/\n"
201 " self->rwblock[arg0] = 0;\n"
203 "plockstat$target:::mutex-spun\n"
204 "/self->mtxspin[arg0] && arg1 != 0/\n"
206 " @mtx_spin[arg0, ustack()] =\n"
207 " quantize(timestamp - self->mtxspin[arg0]);\n"
208 " self->mtxspin[arg0] = 0;\n"
209 " mtx_spin_found = 1;\n"
211 "plockstat$target:::mutex-spun\n"
212 "/self->mtxspin[arg0]/\n"
214 " @mtx_vain_spin[arg0, ustack()] =\n"
215 " quantize(timestamp - self->mtxspin[arg0]);\n"
216 " self->mtxspin[arg0] = 0;\n"
217 " mtx_vain_spin_found = 1;\n"
219 "plockstat$target:::mutex-blocked\n"
220 "/self->mtxblock[arg0] && arg1 != 0/\n"
222 " @mtx_block[arg0, ustack()] =\n"
223 " quantize(timestamp - self->mtxblock[arg0]);\n"
224 " self->mtxblock[arg0] = 0;\n"
225 " mtx_block_found = 1;\n"
227 "plockstat$target:::mutex-blocked\n"
228 "/self->mtxblock[arg0]/\n"
230 " self->mtxblock[arg0] = 0;\n"
234 "/mtx_block_found/\n"
236 " trace(\"Mutex block\");\n"
237 " printa(@mtx_block);\n"
242 " trace(\"Mutex spin\");\n"
243 " printa(@mtx_spin);\n"
246 "/mtx_vain_spin_found/\n"
248 " trace(\"Mutex unsuccessful spin\");\n"
249 " printa(@mtx_vain_spin);\n"
252 "/rw_r_block_found/\n"
254 " trace(\"R/W reader block\");\n"
255 " printa(@rw_r_block);\n"
258 "/rw_w_block_found/\n"
260 " trace(\"R/W writer block\");\n"
261 " printa(@rw_w_block);\n"
265 static const char *g_ctnd_times
=
266 "plockstat$target:::rw-blocked\n"
267 "/self->rwblock[arg0] && arg1 == 1 && arg2 != 0/\n"
269 " @rw_w_block[arg0, ustack(5)] =\n"
270 " sum(timestamp - self->rwblock[arg0]);\n"
271 " @rw_w_block_count[arg0, ustack(5)] = count();\n"
272 " self->rwblock[arg0] = 0;\n"
273 " rw_w_block_found = 1;\n"
275 "plockstat$target:::rw-blocked\n"
276 "/self->rwblock[arg0] && arg2 != 0/\n"
278 " @rw_r_block[arg0, ustack(5)] =\n"
279 " sum(timestamp - self->rwblock[arg0]);\n"
280 " @rw_r_block_count[arg0, ustack(5)] = count();\n"
281 " self->rwblock[arg0] = 0;\n"
282 " rw_r_block_found = 1;\n"
284 "plockstat$target:::rw-blocked\n"
285 "/self->rwblock[arg0]/\n"
287 " self->rwblock[arg0] = 0;\n"
289 "plockstat$target:::mutex-spun\n"
290 "/self->mtxspin[arg0] && arg1 != 0/\n"
292 " @mtx_spin[arg0, ustack(5)] =\n"
293 " sum(timestamp - self->mtxspin[arg0]);\n"
294 " @mtx_spin_count[arg0, ustack(5)] = count();\n"
295 " self->mtxspin[arg0] = 0;\n"
296 " mtx_spin_found = 1;\n"
298 "plockstat$target:::mutex-spun\n"
299 "/self->mtxspin[arg0]/\n"
301 " @mtx_vain_spin[arg0, ustack(5)] =\n"
302 " sum(timestamp - self->mtxspin[arg0]);\n"
303 " @mtx_vain_spin_count[arg0, ustack(5)] = count();\n"
304 " self->mtxspin[arg0] = 0;\n"
305 " mtx_vain_spin_found = 1;\n"
307 "plockstat$target:::mutex-blocked\n"
308 "/self->mtxblock[arg0] && arg1 != 0/\n"
310 " @mtx_block[arg0, ustack(5)] =\n"
311 " sum(timestamp - self->mtxblock[arg0]);\n"
312 " @mtx_block_count[arg0, ustack(5)] = count();\n"
313 " self->mtxblock[arg0] = 0;\n"
314 " mtx_block_found = 1;\n"
316 "plockstat$target:::mutex-blocked\n"
317 "/self->mtxblock[arg0]/\n"
319 " self->mtxblock[arg0] = 0;\n"
323 "/mtx_block_found/\n"
325 " trace(\"Mutex block\");\n"
326 " printa(@mtx_block, @mtx_block_count);\n"
331 " trace(\"Mutex spin\");\n"
332 " printa(@mtx_spin, @mtx_spin_count);\n"
335 "/mtx_vain_spin_found/\n"
337 " trace(\"Mutex unsuccessful spin\");\n"
338 " printa(@mtx_vain_spin, @mtx_vain_spin_count);\n"
341 "/rw_r_block_found/\n"
343 " trace(\"R/W reader block\");\n"
344 " printa(@rw_r_block, @rw_r_block_count);\n"
347 "/rw_w_block_found/\n"
349 " trace(\"R/W writer block\");\n"
350 " printa(@rw_w_block, @rw_w_block_count);\n"
353 static char g_prog
[4096];
354 static size_t g_proglen
;
355 static int g_opt_V
, g_opt_s
;
358 static dtrace_optval_t g_nframes
;
359 static ulong_t g_nent
= ULONG_MAX
;
361 #define PLOCKSTAT_OPTSTR "n:ps:e:vx:ACHV"
366 (void) fprintf(stderr
, "Usage:\n"
367 "\t%s [-vACHV] [-n count] [-s depth] [-e secs] [-x opt[=val]]\n"
368 "\t command [arg...]\n"
369 "\t%s [-vACHV] [-n count] [-s depth] [-e secs] [-x opt[=val]]\n"
370 "\t -p pid\n", g_pname
, g_pname
);
376 verror(const char *fmt
, va_list ap
)
380 (void) fprintf(stderr
, "%s: ", g_pname
);
381 (void) vfprintf(stderr
, fmt
, ap
);
383 if (fmt
[strlen(fmt
) - 1] != '\n')
384 (void) fprintf(stderr
, ": %s\n", strerror(error
));
389 fatal(const char *fmt
, ...)
397 if (g_pr
!= NULL
&& g_dtp
!= NULL
)
398 dtrace_proc_release(g_dtp
, g_pr
);
405 dfatal(const char *fmt
, ...)
411 (void) fprintf(stderr
, "%s: ", g_pname
);
413 (void) vfprintf(stderr
, fmt
, ap
);
417 if (fmt
!= NULL
&& fmt
[strlen(fmt
) - 1] != '\n') {
418 (void) fprintf(stderr
, ": %s\n",
419 dtrace_errmsg(g_dtp
, dtrace_errno(g_dtp
)));
420 } else if (fmt
== NULL
) {
421 (void) fprintf(stderr
, "%s\n",
422 dtrace_errmsg(g_dtp
, dtrace_errno(g_dtp
)));
426 dtrace_proc_continue(g_dtp
, g_pr
);
427 dtrace_proc_release(g_dtp
, g_pr
);
435 notice(const char *fmt
, ...)
445 dprog_add(const char *prog
)
447 size_t len
= strlen(prog
);
448 bcopy(prog
, g_prog
+ g_proglen
, len
+ 1);
450 assert(g_proglen
< sizeof (g_prog
));
457 dtrace_proginfo_t info
;
460 (void) fprintf(stderr
, "%s: vvvv D program vvvv\n", g_pname
);
461 (void) fputs(g_prog
, stderr
);
462 (void) fprintf(stderr
, "%s: ^^^^ D program ^^^^\n", g_pname
);
465 if ((prog
= dtrace_program_strcompile(g_dtp
, g_prog
,
466 DTRACE_PROBESPEC_NAME
, 0, 0, NULL
)) == NULL
)
467 dfatal("failed to compile program");
469 if (dtrace_program_exec(g_dtp
, prog
, &info
) == -1)
470 dfatal("failed to enable probes");
476 (void) printf("%5s %8s %-28s %s\n", "Count", "nsec", "Lock", "Caller");
482 (void) printf("---------------------------------------"
483 "----------------------------------------\n");
487 print_histogram_header(void)
489 (void) printf("\n%10s ---- Time Distribution --- %5s %s\n",
490 "nsec", "count", "Stack");
494 * Convert an address to a symbolic string or a numeric string. If nolocks
495 * is set, we return an error code if this symbol appears to be a mutex- or
496 * rwlock-related symbol in libc so the caller has a chance to find a more
500 getsym(struct ps_prochandle
*P
, uintptr_t addr
, char *buf
, size_t size
,
508 if (P
== NULL
|| Pxlookup_by_addr(P
, addr
, name
, sizeof (name
),
510 (void) snprintf(buf
, size
, "%#lx", addr
);
513 if (info
.prs_object
== NULL
)
514 info
.prs_object
= "<unknown>";
516 if (info
.prs_lmid
!= LM_ID_BASE
) {
517 len
= snprintf(buf
, size
, "LM%lu`", info
.prs_lmid
);
522 len
= snprintf(buf
, size
, "%s`%s", info
.prs_object
, info
.prs_name
);
526 if (sym
.st_value
!= addr
)
527 len
= snprintf(buf
, size
, "+%#lx", addr
- sym
.st_value
);
529 if (nolocks
&& strcmp("libc.so.1", info
.prs_object
) == 0 &&
530 (strstr("mutex", info
.prs_name
) == 0 ||
531 strstr("rw", info
.prs_name
) == 0))
539 process_aggregate(const dtrace_aggdata_t
**aggsdata
, int naggvars
, void *arg
)
541 const dtrace_recdesc_t
*rec
;
546 struct ps_prochandle
*P
;
549 uint64_t sum
, count
, avg
;
551 if ((*(uint_t
*)arg
)++ >= g_nent
)
552 return (DTRACE_AGGWALK_NEXT
);
554 rec
= aggsdata
[0]->dtada_desc
->dtagd_rec
;
555 data
= aggsdata
[0]->dtada_data
;
557 /*LINTED - alignment*/
558 lock
= (uintptr_t)*(uint64_t *)(data
+ rec
[1].dtrd_offset
);
559 /*LINTED - alignment*/
560 stack
= (uint64_t *)(data
+ rec
[2].dtrd_offset
);
563 /*LINTED - alignment*/
564 sum
= *(uint64_t *)(aggsdata
[1]->dtada_data
+
565 aggsdata
[1]->dtada_desc
->dtagd_rec
[3].dtrd_offset
);
566 /*LINTED - alignment*/
567 count
= *(uint64_t *)(aggsdata
[2]->dtada_data
+
568 aggsdata
[2]->dtada_desc
->dtagd_rec
[3].dtrd_offset
);
572 /*LINTED - alignment*/
573 a
= (uint64_t *)(aggsdata
[1]->dtada_data
+
574 aggsdata
[1]->dtada_desc
->dtagd_rec
[3].dtrd_offset
);
579 for (count
= sum
= 0, i
= DTRACE_QUANTIZE_ZEROBUCKET
, j
= 0;
580 i
< DTRACE_QUANTIZE_NBUCKETS
; i
++, j
++) {
582 sum
+= a
[i
] << (j
- 64);
587 (void) printf("%5llu %8llu ", (u_longlong_t
)count
, (u_longlong_t
)avg
);
590 P
= dtrace_proc_grab(g_dtp
, pid
, PGRAB_RDONLY
);
592 (void) getsym(P
, lock
, buf
, sizeof (buf
), 0);
593 (void) printf("%-28s ", buf
);
595 for (i
= 2; i
<= 5; i
++) {
596 if (getsym(P
, stack
[i
], buf
, sizeof (buf
), 1) == 0)
599 (void) printf("%s\n", buf
);
604 int first_bin
, last_bin
;
605 uint64_t bin_size
, *a
;
607 /*LINTED - alignment*/
608 a
= (uint64_t *)(aggsdata
[1]->dtada_data
+
609 aggsdata
[1]->dtada_desc
->dtagd_rec
[3].dtrd_offset
);
611 print_histogram_header();
613 for (first_bin
= DTRACE_QUANTIZE_ZEROBUCKET
;
614 a
[first_bin
] == 0; first_bin
++)
616 for (last_bin
= DTRACE_QUANTIZE_ZEROBUCKET
+ 63;
617 a
[last_bin
] == 0; last_bin
--)
620 for (i
= 0; !stack_done
|| !quant_done
; i
++) {
622 (void) getsym(P
, stack
[i
+ 2], buf
,
629 bin_size
= a
[first_bin
];
631 (void) printf("%10llu |%-24.*s| %5llu %s\n",
633 (first_bin
- DTRACE_QUANTIZE_ZEROBUCKET
),
634 (int)(24.0 * bin_size
/ count
),
635 "@@@@@@@@@@@@@@@@@@@@@@@@@@",
636 (u_longlong_t
)bin_size
, buf
);
638 (void) printf("%43s %s\n", "", buf
);
641 if (i
+ 1 >= g_nframes
|| stack
[i
+ 3] == 0)
644 if (first_bin
++ == last_bin
)
649 dtrace_proc_release(g_dtp
, P
);
651 return (DTRACE_AGGWALK_NEXT
);
656 prochandler(struct ps_prochandle
*P
, const char *msg
, void *arg
)
658 const psinfo_t
*prp
= Ppsinfo(P
);
659 int pid
= Pstatus(P
)->pr_pid
;
660 char name
[SIG2STR_MAX
];
663 notice("pid %d: %s\n", pid
, msg
);
670 * Ideally we would like to always report pr_wstat here, but it
671 * isn't possible given current /proc semantics. If we grabbed
672 * the process, Ppsinfo() will either fail or return a zeroed
673 * psinfo_t depending on how far the parent is in reaping it.
674 * When /proc provides a stable pr_wstat in the status file,
675 * this code can be improved by examining this new pr_wstat.
677 if (prp
!= NULL
&& WIFSIGNALED(prp
->pr_wstat
)) {
678 notice("pid %d terminated by %s\n", pid
,
679 proc_signame(WTERMSIG(prp
->pr_wstat
),
680 name
, sizeof (name
)));
681 } else if (prp
!= NULL
&& WEXITSTATUS(prp
->pr_wstat
) != 0) {
682 notice("pid %d exited with status %d\n",
683 pid
, WEXITSTATUS(prp
->pr_wstat
));
685 notice("pid %d has exited\n", pid
);
691 notice("pid %d exec'd a set-id or unobservable program\n", pid
);
699 chewrec(const dtrace_probedata_t
*data
, const dtrace_recdesc_t
*rec
, void *arg
)
701 dtrace_eprobedesc_t
*epd
= data
->dtpda_edesc
;
702 dtrace_aggvarid_t aggvars
[2];
707 * A NULL rec indicates that we've processed the last record.
710 return (DTRACE_CONSUME_NEXT
);
712 buf
= data
->dtpda_data
- rec
->dtrd_offset
;
714 switch (rec
->dtrd_action
) {
715 case DTRACEACT_DIFEXPR
:
716 (void) printf("\n%s\n\n", (char *)buf
+ rec
->dtrd_offset
);
721 return (DTRACE_CONSUME_NEXT
);
723 case DTRACEACT_PRINTA
:
724 for (nagv
= 0, i
= 0; i
< epd
->dtepd_nrecs
- 1; i
++) {
725 const dtrace_recdesc_t
*nrec
= &rec
[i
];
727 if (nrec
->dtrd_uarg
!= rec
->dtrd_uarg
)
730 /*LINTED - alignment*/
731 aggvars
[nagv
++] = *(dtrace_aggvarid_t
*)((caddr_t
)buf
+
735 if (nagv
== (g_opt_s
? 1 : 2)) {
737 if (dtrace_aggregate_walk_joined(g_dtp
, aggvars
, nagv
,
738 process_aggregate
, &nent
) != 0)
739 dfatal("failed to walk aggregate");
742 return (DTRACE_CONSUME_NEXT
);
745 return (DTRACE_CONSUME_THIS
);
756 main(int argc
, char **argv
)
760 int opt_C
= 0, opt_H
= 0, opt_p
= 0, opt_v
= 0;
762 struct sigaction act
;
765 g_pname
= basename(argv
[0]);
766 argv
[0] = g_pname
; /* rewrite argv[0] for getopt errors */
769 * Make sure we have the required dtrace_proc privilege.
771 if ((ucp
= ucred_get(getpid())) != NULL
) {
772 const priv_set_t
*psp
;
773 if ((psp
= ucred_getprivset(ucp
, PRIV_EFFECTIVE
)) != NULL
&&
774 !priv_ismember(psp
, PRIV_DTRACE_PROC
)) {
775 fatal("dtrace_proc privilege required\n");
781 while ((c
= getopt(argc
, argv
, PLOCKSTAT_OPTSTR
)) != EOF
) {
785 g_nent
= strtoul(optarg
, &end
, 10);
786 if (*end
!= '\0' || errno
!= 0) {
787 (void) fprintf(stderr
, "%s: invalid count "
788 "'%s'\n", g_pname
, optarg
);
818 if (strchr(PLOCKSTAT_OPTSTR
, c
) == NULL
)
824 * We need a command or at least one pid.
829 if (opt_C
== 0 && opt_H
== 0)
832 if ((g_dtp
= dtrace_open(DTRACE_VERSION
, 0, &err
)) == NULL
)
833 fatal("failed to initialize dtrace: %s\n",
834 dtrace_errmsg(NULL
, err
));
837 * The longest string we trace is 23 bytes long -- so 32 is plenty.
839 if (dtrace_setopt(g_dtp
, "strsize", "32") == -1)
840 dfatal("failed to set 'strsize'");
843 * 1k should be more than enough for all trace() and printa() actions.
845 if (dtrace_setopt(g_dtp
, "bufsize", "1k") == -1)
846 dfatal("failed to set 'bufsize'");
849 * The table we produce has the hottest locks at the top.
851 if (dtrace_setopt(g_dtp
, "aggsortrev", NULL
) == -1)
852 dfatal("failed to set 'aggsortrev'");
855 * These are two reasonable defaults which should suffice.
857 if (dtrace_setopt(g_dtp
, "aggsize", "256k") == -1)
858 dfatal("failed to set 'aggsize'");
859 if (dtrace_setopt(g_dtp
, "aggrate", "1sec") == -1)
860 dfatal("failed to set 'aggrate'");
863 * Take a second pass through to look for options that set options now
864 * that we have an open dtrace handle.
867 while ((c
= getopt(argc
, argv
, PLOCKSTAT_OPTSTR
)) != EOF
) {
871 if (dtrace_setopt(g_dtp
, "ustackframes", optarg
) == -1)
872 dfatal("failed to set 'ustackframes'");
876 if ((p
= strchr(optarg
, '=')) != NULL
)
879 if (dtrace_setopt(g_dtp
, optarg
, p
) != 0)
880 dfatal("failed to set -x %s", optarg
);
885 (void) strtoul(optarg
, &end
, 10);
886 if (*optarg
== '-' || *end
!= '\0' || errno
!= 0) {
887 (void) fprintf(stderr
, "%s: invalid timeout "
888 "'%s'\n", g_pname
, optarg
);
893 * Construct a DTrace enabling that will exit after
894 * the specified number of seconds.
896 dprog_add("BEGIN\n{\n\tend = timestamp + ");
898 dprog_add(" * 1000000000;\n}\n");
899 dprog_add("tick-10hz\n/timestamp >= end/\n");
900 dprog_add("{\n\texit(0);\n}\n");
909 dprog_add(g_hold_init
);
911 dprog_add(g_hold_times
);
913 dprog_add(g_hold_histogram
);
917 dprog_add(g_ctnd_init
);
919 dprog_add(g_ctnd_times
);
921 dprog_add(g_ctnd_histogram
);
928 (void) fprintf(stderr
, "%s: only one pid is allowed\n",
934 pid
= strtoul(argv
[0], &end
, 10);
935 if (*end
!= '\0' || errno
!= 0 || (pid_t
)pid
!= pid
) {
936 (void) fprintf(stderr
, "%s: invalid pid '%s'\n",
941 if ((g_pr
= dtrace_proc_grab(g_dtp
, (pid_t
)pid
, 0)) == NULL
)
944 if ((g_pr
= dtrace_proc_create(g_dtp
, argv
[0], argv
)) == NULL
)
950 if (dtrace_handle_proc(g_dtp
, &prochandler
, NULL
) == -1)
951 dfatal("failed to establish proc handler");
953 (void) sigemptyset(&act
.sa_mask
);
955 act
.sa_handler
= intr
;
956 (void) sigaction(SIGINT
, &act
, NULL
);
957 (void) sigaction(SIGTERM
, &act
, NULL
);
959 if (dtrace_go(g_dtp
) != 0)
960 dfatal("dtrace_go()");
962 if (dtrace_getopt(g_dtp
, "ustackframes", &g_nframes
) != 0)
963 dfatal("failed to get 'ustackframes'");
965 dtrace_proc_continue(g_dtp
, g_pr
);
968 (void) printf("%s: tracing enabled for pid %d\n", g_pname
,
969 (int)Pstatus(g_pr
)->pr_pid
);
972 if (!g_intr
&& !done
)
975 if (done
|| g_intr
|| g_exited
) {
977 if (dtrace_stop(g_dtp
) == -1)
978 dfatal("couldn't stop tracing");
981 switch (dtrace_work(g_dtp
, stdout
, NULL
, chewrec
, NULL
)) {
982 case DTRACE_WORKSTATUS_DONE
:
985 case DTRACE_WORKSTATUS_OKAY
:
988 dfatal("processing aborted");