1#!/usr/sbin/dtrace -Zs
2/*
3 * sh_flowtime.d - snoop Bourne shell execution with flow and delta times.
4 *                 Written for the sh DTrace provider.
5 *
6 * $Id: sh_flowtime.d 45 2007-09-17 08:54:56Z brendan $
7 *
8 * This traces shell activity from all Bourne shells on the system that are
9 * running with sh provider support.
10 *
11 * USAGE: sh_flowtime.d			# hit Ctrl-C to end
12 *
13 * This watches shell function entries and returns, and indents child
14 * function calls. Shell builtins are also printed.
15 *
16 * FIELDS:
17 *		C		CPU-id
18 *		TIME(us)	Time since boot, us
19 *		FILE		Filename that this function belongs to
20 *		NAME		Shell function or builtin name
21 *
22 * LEGEND:
23 *		->		function entry
24 *		<-		function return
25 *		 >		builtin
26 *		 |		external command
27 *
28 * DELTAs:
29 *		->		previous line to the start of this function
30 *		<-		previous line to the end of this function
31 *		>		previous line to the end of this builtin
32 *		|		previous line to the end of this command
33 *
34 * See sh_flowinfo.d for more verbose and more straightforward delta times.
35 *
36 * Filename and function names are printed if available.
37 *
38 * WARNING: Watch the first column carefully, it prints the CPU-id. If it
39 * changes, then it is very likely that the output has been shuffled.
40 *
41 * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
42 *
43 * CDDL HEADER START
44 *
45 *  The contents of this file are subject to the terms of the
46 *  Common Development and Distribution License, Version 1.0 only
47 *  (the "License").  You may not use this file except in compliance
48 *  with the License.
49 *
50 *  You can obtain a copy of the license at Docs/cddl1.txt
51 *  or http://www.opensolaris.org/os/licensing.
52 *  See the License for the specific language governing permissions
53 *  and limitations under the License.
54 *
55 * CDDL HEADER END
56 *
57 * 09-Sep-2007	Brendan Gregg	Created this.
58 */
59
60#pragma D option quiet
61#pragma D option switchrate=10
62
63self int depth;
64
65self uint64_t last;
66
67dtrace:::BEGIN
68{
69	printf("%3s %-16s %-16s %9s -- %s\n", "C", "TIME(us)", "FILE",
70	    "DELTA(us)", "NAME");
71}
72
73sh*:::function-entry,
74sh*:::function-return,
75sh*:::builtin-return,
76sh*:::command-return
77/self->last == 0/
78{
79	self->last = timestamp;
80}
81
82sh*:::function-entry
83{
84	this->elapsed = (timestamp - self->last) / 1000;
85	printf("%3d %-16d %-16s %9d %*s-> %s\n", cpu, timestamp / 1000,
86	    basename(copyinstr(arg0)), this->elapsed, self->depth * 2, "",
87	    copyinstr(arg1));
88	self->depth++;
89	self->last = timestamp;
90}
91
92sh*:::function-return
93{
94	this->elapsed = (timestamp - self->last) / 1000;
95	self->depth -= self->depth > 0 ? 1 : 0;
96	printf("%3d %-16d %-16s %9d %*s<- %s\n", cpu, timestamp / 1000,
97	    basename(copyinstr(arg0)), this->elapsed, self->depth * 2, "",
98	    copyinstr(arg1));
99	self->last = timestamp;
100}
101
102sh*:::builtin-return
103{
104	this->elapsed = (timestamp - self->last) / 1000;
105	printf("%3d %-16d %-16s %9d %*s> %s\n", cpu, timestamp / 1000,
106	    basename(copyinstr(arg0)), this->elapsed, self->depth * 2, "",
107	    copyinstr(arg1));
108	self->last = timestamp;
109}
110
111sh*:::command-return
112{
113	this->elapsed = (timestamp - self->last) / 1000;
114	printf("%3d %-16d %-16s %9d %*s| %s\n", cpu, timestamp / 1000,
115	    basename(copyinstr(arg0)), this->elapsed, self->depth * 2, "",
116	    copyinstr(arg1));
117	self->last = timestamp;
118}
119