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.
44 static dtrace_hdl_t
*g_dtp
;
45 struct ps_prochandle
*g_pr
;
52 * For hold times we use a global associative array since for mutexes, in
53 * user-land, it's not invalid to release a sychonization primitive that
54 * another thread acquired; rwlocks require a thread-local associative array
55 * since multiple thread can hold the same lock for reading. Note that we
56 * ignore recursive mutex acquisitions and releases as they don't truly
57 * affect lock contention.
59 static const char *g_hold_init
=
60 "plockstat$target:::rw-acquire\n"
62 " self->rwhold[arg0] = timestamp;\n"
64 "plockstat$target:::mutex-acquire\n"
67 " mtxhold[arg0] = timestamp;\n"
70 static const char *g_hold_histogram
=
71 "plockstat$target:::rw-release\n"
72 "/self->rwhold[arg0] && arg1 == 1/\n"
74 " @rw_w_hold[arg0, ustack()] =\n"
75 " quantize(timestamp - self->rwhold[arg0]);\n"
76 " self->rwhold[arg0] = 0;\n"
77 " rw_w_hold_found = 1;\n"
79 "plockstat$target:::rw-release\n"
80 "/self->rwhold[arg0]/\n"
82 " @rw_r_hold[arg0, ustack()] =\n"
83 " quantize(timestamp - self->rwhold[arg0]);\n"
84 " self->rwhold[arg0] = 0;\n"
85 " rw_r_hold_found = 1;\n"
87 "plockstat$target:::mutex-release\n"
88 "/mtxhold[arg0] && arg1 == 0/\n"
90 " @mtx_hold[arg0, ustack()] = quantize(timestamp - mtxhold[arg0]);\n"
91 " mtxhold[arg0] = 0;\n"
92 " mtx_hold_found = 1;\n"
98 " trace(\"Mutex hold\");\n"
99 " printa(@mtx_hold);\n"
102 "/rw_r_hold_found/\n"
104 " trace(\"R/W reader hold\");\n"
105 " printa(@rw_r_hold);\n"
108 "/rw_w_hold_found/\n"
110 " trace(\"R/W writer hold\");\n"
111 " printa(@rw_w_hold);\n"
114 static const char *g_hold_times
=
115 "plockstat$target:::rw-release\n"
116 "/self->rwhold[arg0] && arg1 == 1/\n"
118 " @rw_w_hold[arg0, ustack(5)] = sum(timestamp - self->rwhold[arg0]);\n"
119 " @rw_w_hold_count[arg0, ustack(5)] = count();\n"
120 " self->rwhold[arg0] = 0;\n"
121 " rw_w_hold_found = 1;\n"
123 "plockstat$target:::rw-release\n"
124 "/self->rwhold[arg0]/\n"
126 " @rw_r_hold[arg0, ustack(5)] = sum(timestamp - self->rwhold[arg0]);\n"
127 " @rw_r_hold_count[arg0, ustack(5)] = count();\n"
128 " self->rwhold[arg0] = 0;\n"
129 " rw_r_hold_found = 1;\n"
131 "plockstat$target:::mutex-release\n"
132 "/mtxhold[arg0] && arg1 == 0/\n"
134 " @mtx_hold[arg0, ustack(5)] = sum(timestamp - mtxhold[arg0]);\n"
135 " @mtx_hold_count[arg0, ustack(5)] = count();\n"
136 " mtxhold[arg0] = 0;\n"
137 " mtx_hold_found = 1;\n"
143 " trace(\"Mutex hold\");\n"
144 " printa(@mtx_hold, @mtx_hold_count);\n"
147 "/rw_r_hold_found/\n"
149 " trace(\"R/W reader hold\");\n"
150 " printa(@rw_r_hold, @rw_r_hold_count);\n"
153 "/rw_w_hold_found/\n"
155 " trace(\"R/W writer hold\");\n"
156 " printa(@rw_w_hold, @rw_w_hold_count);\n"
161 * For contention, we use thread-local associative arrays since we're tracing
162 * a single thread's activity in libc and multiple threads can be blocking or
163 * spinning on the same sychonization primitive.
165 static const char *g_ctnd_init
=
166 "plockstat$target:::rw-block\n"
168 " self->rwblock[arg0] = timestamp;\n"
170 "plockstat$target:::mutex-block\n"
172 " self->mtxblock[arg0] = timestamp;\n"
174 "plockstat$target:::mutex-spin\n"
176 " self->mtxspin[arg0] = timestamp;\n"
179 static const char *g_ctnd_histogram
=
180 "plockstat$target:::rw-blocked\n"
181 "/self->rwblock[arg0] && arg1 == 1 && arg2 != 0/\n"
183 " @rw_w_block[arg0, ustack()] =\n"
184 " quantize(timestamp - self->rwblock[arg0]);\n"
185 " self->rwblock[arg0] = 0;\n"
186 " rw_w_block_found = 1;\n"
188 "plockstat$target:::rw-blocked\n"
189 "/self->rwblock[arg0] && arg2 != 0/\n"
191 " @rw_r_block[arg0, ustack()] =\n"
192 " quantize(timestamp - self->rwblock[arg0]);\n"
193 " self->rwblock[arg0] = 0;\n"
194 " rw_r_block_found = 1;\n"
196 "plockstat$target:::rw-blocked\n"
197 "/self->rwblock[arg0]/\n"
199 " self->rwblock[arg0] = 0;\n"
201 "plockstat$target:::mutex-spun\n"
202 "/self->mtxspin[arg0] && arg1 != 0/\n"
204 " @mtx_spin[arg0, ustack()] =\n"
205 " quantize(timestamp - self->mtxspin[arg0]);\n"
206 " self->mtxspin[arg0] = 0;\n"
207 " mtx_spin_found = 1;\n"
209 "plockstat$target:::mutex-spun\n"
210 "/self->mtxspin[arg0]/\n"
212 " @mtx_vain_spin[arg0, ustack()] =\n"
213 " quantize(timestamp - self->mtxspin[arg0]);\n"
214 " self->mtxspin[arg0] = 0;\n"
215 " mtx_vain_spin_found = 1;\n"
217 "plockstat$target:::mutex-blocked\n"
218 "/self->mtxblock[arg0] && arg1 != 0/\n"
220 " @mtx_block[arg0, ustack()] =\n"
221 " quantize(timestamp - self->mtxblock[arg0]);\n"
222 " self->mtxblock[arg0] = 0;\n"
223 " mtx_block_found = 1;\n"
225 "plockstat$target:::mutex-blocked\n"
226 "/self->mtxblock[arg0]/\n"
228 " self->mtxblock[arg0] = 0;\n"
232 "/mtx_block_found/\n"
234 " trace(\"Mutex block\");\n"
235 " printa(@mtx_block);\n"
240 " trace(\"Mutex spin\");\n"
241 " printa(@mtx_spin);\n"
244 "/mtx_vain_spin_found/\n"
246 " trace(\"Mutex unsuccessful spin\");\n"
247 " printa(@mtx_vain_spin);\n"
250 "/rw_r_block_found/\n"
252 " trace(\"R/W reader block\");\n"
253 " printa(@rw_r_block);\n"
256 "/rw_w_block_found/\n"
258 " trace(\"R/W writer block\");\n"
259 " printa(@rw_w_block);\n"
263 static const char *g_ctnd_times
=
264 "plockstat$target:::rw-blocked\n"
265 "/self->rwblock[arg0] && arg1 == 1 && arg2 != 0/\n"
267 " @rw_w_block[arg0, ustack(5)] =\n"
268 " sum(timestamp - self->rwblock[arg0]);\n"
269 " @rw_w_block_count[arg0, ustack(5)] = count();\n"
270 " self->rwblock[arg0] = 0;\n"
271 " rw_w_block_found = 1;\n"
273 "plockstat$target:::rw-blocked\n"
274 "/self->rwblock[arg0] && arg2 != 0/\n"
276 " @rw_r_block[arg0, ustack(5)] =\n"
277 " sum(timestamp - self->rwblock[arg0]);\n"
278 " @rw_r_block_count[arg0, ustack(5)] = count();\n"
279 " self->rwblock[arg0] = 0;\n"
280 " rw_r_block_found = 1;\n"
282 "plockstat$target:::rw-blocked\n"
283 "/self->rwblock[arg0]/\n"
285 " self->rwblock[arg0] = 0;\n"
287 "plockstat$target:::mutex-spun\n"
288 "/self->mtxspin[arg0] && arg1 != 0/\n"
290 " @mtx_spin[arg0, ustack(5)] =\n"
291 " sum(timestamp - self->mtxspin[arg0]);\n"
292 " @mtx_spin_count[arg0, ustack(5)] = count();\n"
293 " self->mtxspin[arg0] = 0;\n"
294 " mtx_spin_found = 1;\n"
296 "plockstat$target:::mutex-spun\n"
297 "/self->mtxspin[arg0]/\n"
299 " @mtx_vain_spin[arg0, ustack(5)] =\n"
300 " sum(timestamp - self->mtxspin[arg0]);\n"
301 " @mtx_vain_spin_count[arg0, ustack(5)] = count();\n"
302 " self->mtxspin[arg0] = 0;\n"
303 " mtx_vain_spin_found = 1;\n"
305 "plockstat$target:::mutex-blocked\n"
306 "/self->mtxblock[arg0] && arg1 != 0/\n"
308 " @mtx_block[arg0, ustack(5)] =\n"
309 " sum(timestamp - self->mtxblock[arg0]);\n"
310 " @mtx_block_count[arg0, ustack(5)] = count();\n"
311 " self->mtxblock[arg0] = 0;\n"
312 " mtx_block_found = 1;\n"
314 "plockstat$target:::mutex-blocked\n"
315 "/self->mtxblock[arg0]/\n"
317 " self->mtxblock[arg0] = 0;\n"
321 "/mtx_block_found/\n"
323 " trace(\"Mutex block\");\n"
324 " printa(@mtx_block, @mtx_block_count);\n"
329 " trace(\"Mutex spin\");\n"
330 " printa(@mtx_spin, @mtx_spin_count);\n"
333 "/mtx_vain_spin_found/\n"
335 " trace(\"Mutex unsuccessful spin\");\n"
336 " printa(@mtx_vain_spin, @mtx_vain_spin_count);\n"
339 "/rw_r_block_found/\n"
341 " trace(\"R/W reader block\");\n"
342 " printa(@rw_r_block, @rw_r_block_count);\n"
345 "/rw_w_block_found/\n"
347 " trace(\"R/W writer block\");\n"
348 " printa(@rw_w_block, @rw_w_block_count);\n"
351 static char g_prog
[4096];
352 static size_t g_proglen
;
353 static int g_opt_V
, g_opt_s
;
356 static dtrace_optval_t g_nframes
;
357 static ulong_t g_nent
= ULONG_MAX
;
359 #define PLOCKSTAT_OPTSTR "n:ps:e:vx:ACHV"
364 (void) fprintf(stderr
, "Usage:\n"
365 "\t%s [-vACHV] [-n count] [-s depth] [-e secs] [-x opt[=val]]\n"
366 "\t command [arg...]\n"
367 "\t%s [-vACHV] [-n count] [-s depth] [-e secs] [-x opt[=val]]\n"
368 "\t -p pid\n", g_pname
, g_pname
);
374 verror(const char *fmt
, va_list ap
)
378 (void) fprintf(stderr
, "%s: ", g_pname
);
379 (void) vfprintf(stderr
, fmt
, ap
);
381 if (fmt
[strlen(fmt
) - 1] != '\n')
382 (void) fprintf(stderr
, ": %s\n", strerror(error
));
387 fatal(const char *fmt
, ...)
395 if (g_pr
!= NULL
&& g_dtp
!= NULL
)
396 dtrace_proc_release(g_dtp
, g_pr
);
403 dfatal(const char *fmt
, ...)
409 (void) fprintf(stderr
, "%s: ", g_pname
);
411 (void) vfprintf(stderr
, fmt
, ap
);
415 if (fmt
!= NULL
&& fmt
[strlen(fmt
) - 1] != '\n') {
416 (void) fprintf(stderr
, ": %s\n",
417 dtrace_errmsg(g_dtp
, dtrace_errno(g_dtp
)));
418 } else if (fmt
== NULL
) {
419 (void) fprintf(stderr
, "%s\n",
420 dtrace_errmsg(g_dtp
, dtrace_errno(g_dtp
)));
424 dtrace_proc_continue(g_dtp
, g_pr
);
425 dtrace_proc_release(g_dtp
, g_pr
);
433 notice(const char *fmt
, ...)
443 dprog_add(const char *prog
)
445 size_t len
= strlen(prog
);
446 bcopy(prog
, g_prog
+ g_proglen
, len
+ 1);
448 assert(g_proglen
< sizeof (g_prog
));
455 dtrace_proginfo_t info
;
458 (void) fprintf(stderr
, "%s: vvvv D program vvvv\n", g_pname
);
459 (void) fputs(g_prog
, stderr
);
460 (void) fprintf(stderr
, "%s: ^^^^ D program ^^^^\n", g_pname
);
463 if ((prog
= dtrace_program_strcompile(g_dtp
, g_prog
,
464 DTRACE_PROBESPEC_NAME
, 0, 0, NULL
)) == NULL
)
465 dfatal("failed to compile program");
467 if (dtrace_program_exec(g_dtp
, prog
, &info
) == -1)
468 dfatal("failed to enable probes");
474 (void) printf("%5s %8s %-28s %s\n", "Count", "nsec", "Lock", "Caller");
480 (void) printf("---------------------------------------"
481 "----------------------------------------\n");
485 print_histogram_header(void)
487 (void) printf("\n%10s ---- Time Distribution --- %5s %s\n",
488 "nsec", "count", "Stack");
492 * Convert an address to a symbolic string or a numeric string. If nolocks
493 * is set, we return an error code if this symbol appears to be a mutex- or
494 * rwlock-related symbol in libc so the caller has a chance to find a more
498 getsym(struct ps_prochandle
*P
, uintptr_t addr
, char *buf
, size_t size
,
506 if (P
== NULL
|| Pxlookup_by_addr(P
, addr
, name
, sizeof (name
),
508 (void) snprintf(buf
, size
, "%#lx", addr
);
511 if (info
.prs_object
== NULL
)
512 info
.prs_object
= "<unknown>";
514 if (info
.prs_lmid
!= LM_ID_BASE
) {
515 len
= snprintf(buf
, size
, "LM%lu`", info
.prs_lmid
);
520 len
= snprintf(buf
, size
, "%s`%s", info
.prs_object
, info
.prs_name
);
524 if (sym
.st_value
!= addr
)
525 len
= snprintf(buf
, size
, "+%#lx", addr
- sym
.st_value
);
527 if (nolocks
&& strcmp("libc.so.1", info
.prs_object
) == 0 &&
528 (strstr("mutex", info
.prs_name
) == 0 ||
529 strstr("rw", info
.prs_name
) == 0))
537 process_aggregate(const dtrace_aggdata_t
**aggsdata
, int naggvars
, void *arg
)
539 const dtrace_recdesc_t
*rec
;
544 struct ps_prochandle
*P
;
547 uint64_t sum
, count
, avg
;
549 if ((*(uint_t
*)arg
)++ >= g_nent
)
550 return (DTRACE_AGGWALK_NEXT
);
552 rec
= aggsdata
[0]->dtada_desc
->dtagd_rec
;
553 data
= aggsdata
[0]->dtada_data
;
555 /*LINTED - alignment*/
556 lock
= (uintptr_t)*(uint64_t *)(data
+ rec
[1].dtrd_offset
);
557 /*LINTED - alignment*/
558 stack
= (uint64_t *)(data
+ rec
[2].dtrd_offset
);
561 /*LINTED - alignment*/
562 sum
= *(uint64_t *)(aggsdata
[1]->dtada_data
+
563 aggsdata
[1]->dtada_desc
->dtagd_rec
[3].dtrd_offset
);
564 /*LINTED - alignment*/
565 count
= *(uint64_t *)(aggsdata
[2]->dtada_data
+
566 aggsdata
[2]->dtada_desc
->dtagd_rec
[3].dtrd_offset
);
570 /*LINTED - alignment*/
571 a
= (uint64_t *)(aggsdata
[1]->dtada_data
+
572 aggsdata
[1]->dtada_desc
->dtagd_rec
[3].dtrd_offset
);
577 for (count
= sum
= 0, i
= DTRACE_QUANTIZE_ZEROBUCKET
, j
= 0;
578 i
< DTRACE_QUANTIZE_NBUCKETS
; i
++, j
++) {
580 sum
+= a
[i
] << (j
- 64);
585 (void) printf("%5llu %8llu ", (u_longlong_t
)count
, (u_longlong_t
)avg
);
588 P
= dtrace_proc_grab(g_dtp
, pid
, PGRAB_RDONLY
);
590 (void) getsym(P
, lock
, buf
, sizeof (buf
), 0);
591 (void) printf("%-28s ", buf
);
593 for (i
= 2; i
<= 5; i
++) {
594 if (getsym(P
, stack
[i
], buf
, sizeof (buf
), 1) == 0)
597 (void) printf("%s\n", buf
);
602 int first_bin
, last_bin
;
603 uint64_t bin_size
, *a
;
605 /*LINTED - alignment*/
606 a
= (uint64_t *)(aggsdata
[1]->dtada_data
+
607 aggsdata
[1]->dtada_desc
->dtagd_rec
[3].dtrd_offset
);
609 print_histogram_header();
611 for (first_bin
= DTRACE_QUANTIZE_ZEROBUCKET
;
612 a
[first_bin
] == 0; first_bin
++)
614 for (last_bin
= DTRACE_QUANTIZE_ZEROBUCKET
+ 63;
615 a
[last_bin
] == 0; last_bin
--)
618 for (i
= 0; !stack_done
|| !quant_done
; i
++) {
620 (void) getsym(P
, stack
[i
+ 2], buf
,
627 bin_size
= a
[first_bin
];
629 (void) printf("%10llu |%-24.*s| %5llu %s\n",
631 (first_bin
- DTRACE_QUANTIZE_ZEROBUCKET
),
632 (int)(24.0 * bin_size
/ count
),
633 "@@@@@@@@@@@@@@@@@@@@@@@@@@",
634 (u_longlong_t
)bin_size
, buf
);
636 (void) printf("%43s %s\n", "", buf
);
639 if (i
+ 1 >= g_nframes
|| stack
[i
+ 3] == 0)
642 if (first_bin
++ == last_bin
)
647 dtrace_proc_release(g_dtp
, P
);
649 return (DTRACE_AGGWALK_NEXT
);
654 prochandler(struct ps_prochandle
*P
, const char *msg
, void *arg
)
656 const psinfo_t
*prp
= Ppsinfo(P
);
657 int pid
= Pstatus(P
)->pr_pid
;
658 char name
[SIG2STR_MAX
];
661 notice("pid %d: %s\n", pid
, msg
);
668 * Ideally we would like to always report pr_wstat here, but it
669 * isn't possible given current /proc semantics. If we grabbed
670 * the process, Ppsinfo() will either fail or return a zeroed
671 * psinfo_t depending on how far the parent is in reaping it.
672 * When /proc provides a stable pr_wstat in the status file,
673 * this code can be improved by examining this new pr_wstat.
675 if (prp
!= NULL
&& WIFSIGNALED(prp
->pr_wstat
)) {
676 notice("pid %d terminated by %s\n", pid
,
677 proc_signame(WTERMSIG(prp
->pr_wstat
),
678 name
, sizeof (name
)));
679 } else if (prp
!= NULL
&& WEXITSTATUS(prp
->pr_wstat
) != 0) {
680 notice("pid %d exited with status %d\n",
681 pid
, WEXITSTATUS(prp
->pr_wstat
));
683 notice("pid %d has exited\n", pid
);
689 notice("pid %d exec'd a set-id or unobservable program\n", pid
);
697 chewrec(const dtrace_probedata_t
*data
, const dtrace_recdesc_t
*rec
, void *arg
)
699 dtrace_eprobedesc_t
*epd
= data
->dtpda_edesc
;
700 dtrace_aggvarid_t aggvars
[2];
705 * A NULL rec indicates that we've processed the last record.
708 return (DTRACE_CONSUME_NEXT
);
710 buf
= data
->dtpda_data
- rec
->dtrd_offset
;
712 switch (rec
->dtrd_action
) {
713 case DTRACEACT_DIFEXPR
:
714 (void) printf("\n%s\n\n", (char *)buf
+ rec
->dtrd_offset
);
719 return (DTRACE_CONSUME_NEXT
);
721 case DTRACEACT_PRINTA
:
722 for (nagv
= 0, i
= 0; i
< epd
->dtepd_nrecs
- 1; i
++) {
723 const dtrace_recdesc_t
*nrec
= &rec
[i
];
725 if (nrec
->dtrd_uarg
!= rec
->dtrd_uarg
)
728 /*LINTED - alignment*/
729 aggvars
[nagv
++] = *(dtrace_aggvarid_t
*)((caddr_t
)buf
+
733 if (nagv
== (g_opt_s
? 1 : 2)) {
735 if (dtrace_aggregate_walk_joined(g_dtp
, aggvars
, nagv
,
736 process_aggregate
, &nent
) != 0)
737 dfatal("failed to walk aggregate");
740 return (DTRACE_CONSUME_NEXT
);
743 return (DTRACE_CONSUME_THIS
);
754 main(int argc
, char **argv
)
758 int opt_C
= 0, opt_H
= 0, opt_p
= 0, opt_v
= 0;
760 struct sigaction act
;
763 g_pname
= basename(argv
[0]);
764 argv
[0] = g_pname
; /* rewrite argv[0] for getopt errors */
767 * Make sure we have the required dtrace_proc privilege.
769 if ((ucp
= ucred_get(getpid())) != NULL
) {
770 const priv_set_t
*psp
;
771 if ((psp
= ucred_getprivset(ucp
, PRIV_EFFECTIVE
)) != NULL
&&
772 !priv_ismember(psp
, PRIV_DTRACE_PROC
)) {
773 fatal("dtrace_proc privilege required\n");
779 while ((c
= getopt(argc
, argv
, PLOCKSTAT_OPTSTR
)) != EOF
) {
783 g_nent
= strtoul(optarg
, &end
, 10);
784 if (*end
!= '\0' || errno
!= 0) {
785 (void) fprintf(stderr
, "%s: invalid count "
786 "'%s'\n", g_pname
, optarg
);
816 if (strchr(PLOCKSTAT_OPTSTR
, c
) == NULL
)
822 * We need a command or at least one pid.
827 if (opt_C
== 0 && opt_H
== 0)
830 if ((g_dtp
= dtrace_open(DTRACE_VERSION
, 0, &err
)) == NULL
)
831 fatal("failed to initialize dtrace: %s\n",
832 dtrace_errmsg(NULL
, err
));
835 * The longest string we trace is 23 bytes long -- so 32 is plenty.
837 if (dtrace_setopt(g_dtp
, "strsize", "32") == -1)
838 dfatal("failed to set 'strsize'");
841 * 1k should be more than enough for all trace() and printa() actions.
843 if (dtrace_setopt(g_dtp
, "bufsize", "1k") == -1)
844 dfatal("failed to set 'bufsize'");
847 * The table we produce has the hottest locks at the top.
849 if (dtrace_setopt(g_dtp
, "aggsortrev", NULL
) == -1)
850 dfatal("failed to set 'aggsortrev'");
853 * These are two reasonable defaults which should suffice.
855 if (dtrace_setopt(g_dtp
, "aggsize", "256k") == -1)
856 dfatal("failed to set 'aggsize'");
857 if (dtrace_setopt(g_dtp
, "aggrate", "1sec") == -1)
858 dfatal("failed to set 'aggrate'");
861 * Take a second pass through to look for options that set options now
862 * that we have an open dtrace handle.
865 while ((c
= getopt(argc
, argv
, PLOCKSTAT_OPTSTR
)) != EOF
) {
869 if (dtrace_setopt(g_dtp
, "ustackframes", optarg
) == -1)
870 dfatal("failed to set 'ustackframes'");
874 if ((p
= strchr(optarg
, '=')) != NULL
)
877 if (dtrace_setopt(g_dtp
, optarg
, p
) != 0)
878 dfatal("failed to set -x %s", optarg
);
883 (void) strtoul(optarg
, &end
, 10);
884 if (*optarg
== '-' || *end
!= '\0' || errno
!= 0) {
885 (void) fprintf(stderr
, "%s: invalid timeout "
886 "'%s'\n", g_pname
, optarg
);
891 * Construct a DTrace enabling that will exit after
892 * the specified number of seconds.
894 dprog_add("BEGIN\n{\n\tend = timestamp + ");
896 dprog_add(" * 1000000000;\n}\n");
897 dprog_add("tick-10hz\n/timestamp >= end/\n");
898 dprog_add("{\n\texit(0);\n}\n");
907 dprog_add(g_hold_init
);
909 dprog_add(g_hold_times
);
911 dprog_add(g_hold_histogram
);
915 dprog_add(g_ctnd_init
);
917 dprog_add(g_ctnd_times
);
919 dprog_add(g_ctnd_histogram
);
926 (void) fprintf(stderr
, "%s: only one pid is allowed\n",
932 pid
= strtoul(argv
[0], &end
, 10);
933 if (*end
!= '\0' || errno
!= 0 || (pid_t
)pid
!= pid
) {
934 (void) fprintf(stderr
, "%s: invalid pid '%s'\n",
939 if ((g_pr
= dtrace_proc_grab(g_dtp
, (pid_t
)pid
, 0)) == NULL
)
942 if ((g_pr
= dtrace_proc_create(g_dtp
, argv
[0], argv
)) == NULL
)
948 if (dtrace_handle_proc(g_dtp
, &prochandler
, NULL
) == -1)
949 dfatal("failed to establish proc handler");
951 (void) sigemptyset(&act
.sa_mask
);
953 act
.sa_handler
= intr
;
954 (void) sigaction(SIGINT
, &act
, NULL
);
955 (void) sigaction(SIGTERM
, &act
, NULL
);
957 if (dtrace_go(g_dtp
) != 0)
958 dfatal("dtrace_go()");
960 if (dtrace_getopt(g_dtp
, "ustackframes", &g_nframes
) != 0)
961 dfatal("failed to get 'ustackframes'");
963 dtrace_proc_continue(g_dtp
, g_pr
);
966 (void) printf("%s: tracing enabled for pid %d\n", g_pname
,
967 (int)Pstatus(g_pr
)->pr_pid
);
970 if (!g_intr
&& !done
)
973 if (done
|| g_intr
|| g_exited
) {
975 if (dtrace_stop(g_dtp
) == -1)
976 dfatal("couldn't stop tracing");
979 switch (dtrace_work(g_dtp
, stdout
, NULL
, chewrec
, NULL
)) {
980 case DTRACE_WORKSTATUS_DONE
:
983 case DTRACE_WORKSTATUS_OKAY
:
986 dfatal("processing aborted");