3 * sh_syscolors.d - trace Bourne shell flow plus syscalls, in color.
4 * Written for the sh DTrace provider.
6 * $Id: sh_syscolors.d 27 2007-09-13 09:26:01Z brendan $
8 * USAGE: sh_syscolors.d { -p PID | -c cmd } # hit Ctrl-C to end
10 * This watches shell function entries and returns, and indents child
11 * function calls. Shell builtins, commands and lines are also printed.
16 * DELTA(us) Elapsed time from previous line to this line
17 * FILE Filename of the shell script
18 * LINE Line number of filename
19 * TYPE Type of call (func/builtin/cmd/line/shell)
20 * NAME Shell function, builtin or command name
22 * The filename for syscalls may be printed as the shell name, if the
23 * script was invoked using the form "shell filename" rather than running
24 * the script with an interpreter line.
26 * WARNING: Watch the first column carefully, it prints the CPU-id. If it
27 * changes, then it is very likely that the output has been shuffled.
29 * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
33 * The contents of this file are subject to the terms of the
34 * Common Development and Distribution License, Version 1.0 only
35 * (the "License"). You may not use this file except in compliance
38 * You can obtain a copy of the license at Docs/cddl1.txt
39 * or http://www.opensolaris.org/os/licensing.
40 * See the License for the specific language governing permissions
41 * and limitations under the License.
45 * 09-Sep-2007 Brendan Gregg Created this.
48 #pragma D option quiet
49 #pragma D option switchrate=10
55 color_shell = "\033[2;35m"; /* violet, faint */
56 color_line = "\033[1;35m"; /* violet, bold */
57 color_syscall = "\033[2;32m"; /* green, faint */
58 color_off = "\033[0m"; /* default */
60 printf("%s %6s %10s %16s:%-4s %-8s -- %s\n", "C", "PID", "DELTA(us)",
61 "FILE", "LINE", "TYPE", "NAME");
64 sh$target:::function-entry,
65 sh$target:::function-return,
66 sh$target:::builtin-entry,
67 sh$target:::command-entry,
71 /self->last == 0 && pid == $target/
73 self->last = timestamp;
76 sh$target:::function-entry
78 this->delta = (timestamp - self->last) / 1000;
79 printf("%s%d %6d %10d %16s:%-4d %-8s %*s-> %s%s\n", color_shell,
80 cpu, pid, this->delta, basename(copyinstr(arg0)), arg2, "func",
81 self->depth * 2, "", copyinstr(arg1), color_off);
83 self->last = timestamp;
86 sh$target:::function-return
88 this->delta = (timestamp - self->last) / 1000;
89 self->depth -= self->depth > 0 ? 1 : 0;
90 printf("%s%d %6d %10d %16s:- %-8s %*s<- %s%s\n", color_shell,
91 cpu, pid, this->delta, basename(copyinstr(arg0)), "func",
92 self->depth * 2, "", copyinstr(arg1), color_off);
93 self->last = timestamp;
96 sh$target:::builtin-entry
98 this->delta = (timestamp - self->last) / 1000;
99 printf("%s%d %6d %10d %16s:%-4d %-8s %*s-> %s%s\n", color_shell,
100 cpu, pid, this->delta, basename(copyinstr(arg0)), arg2, "builtin",
101 self->depth * 2, "", copyinstr(arg1), color_off);
103 self->last = timestamp;
106 sh$target:::builtin-return
108 this->delta = (timestamp - self->last) / 1000;
109 self->depth -= self->depth > 0 ? 1 : 0;
110 printf("%s%d %6d %10d %16s:%-4d %-8s %*s<- %s%s\n", color_shell,
111 cpu, pid, this->delta, basename(copyinstr(arg0)), arg2, "builtin",
112 self->depth * 2, "", copyinstr(arg1), color_off);
113 self->last = timestamp;
116 sh$target:::command-entry
118 this->delta = (timestamp - self->last) / 1000;
119 printf("%s%d %6d %10d %16s:%-4d %-8s %*s-> %s%s\n", color_shell,
120 cpu, pid, this->delta, basename(copyinstr(arg0)), arg2, "cmd",
121 self->depth * 2, "", copyinstr(arg1), color_off);
123 self->last = timestamp;
126 sh$target:::command-return
128 this->delta = (timestamp - self->last) / 1000;
129 self->depth -= self->depth > 0 ? 1 : 0;
130 printf("%s%d %6d %10d %16s:%-4d %-8s %*s<- %s%s\n", color_shell,
131 cpu, pid, this->delta, basename(copyinstr(arg0)), arg2, "cmd",
132 self->depth * 2, "", copyinstr(arg1), color_off);
133 self->last = timestamp;
138 this->delta = (timestamp - self->last) / 1000;
139 printf("%s%d %6d %10d %16s:%-4d %-8s %*s-- %s\n", color_line,
140 cpu, pid, this->delta, basename(copyinstr(arg0)), arg1, "line",
141 self->depth * 2, "", color_off);
142 self->last = timestamp;
148 this->delta = (timestamp - self->last) / 1000;
149 printf("%s%d %6d %10d %16s:- %-8s %*s-> %s%s\n", color_syscall,
150 cpu, pid, this->delta, basename(execname), "syscall",
151 self->depth * 2, "", probefunc, color_off);
152 self->last = timestamp;
158 this->delta = (timestamp - self->last) / 1000;
159 printf("%s%d %6d %10d %16s:- %-8s %*s<- %s%s\n", color_syscall,
160 cpu, pid, this->delta, basename(execname), "syscall",
161 self->depth * 2, "", probefunc, color_off);
162 self->last = timestamp;