]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - cddl/contrib/dtracetoolkit/Shell/sh_flow.d
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.git] / cddl / contrib / dtracetoolkit / Shell / sh_flow.d
1 #!/usr/sbin/dtrace -Zs
2 /*
3  * sh_flow.d - snoop Bourne shell execution showing function flow using DTrace.
4  *             Written for the sh DTrace provider.
5  *
6  * $Id: sh_flow.d 41 2007-09-17 02:20:10Z 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_flow.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, builtin or command name
21  *
22  * LEGEND:
23  *              ->              function entry
24  *              <-              function return
25  *              >               builtin
26  *              |               external command
27  *
28  * WARNING: Watch the first column carefully, it prints the CPU-id. If it
29  * changes, then it is very likely that the output has been shuffled.
30  *
31  * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
32  *
33  * CDDL HEADER START
34  *
35  *  The contents of this file are subject to the terms of the
36  *  Common Development and Distribution License, Version 1.0 only
37  *  (the "License").  You may not use this file except in compliance
38  *  with the License.
39  *
40  *  You can obtain a copy of the license at Docs/cddl1.txt
41  *  or http://www.opensolaris.org/os/licensing.
42  *  See the License for the specific language governing permissions
43  *  and limitations under the License.
44  *
45  * CDDL HEADER END
46  *
47  * 09-Sep-2007  Brendan Gregg   Created this.
48  */
49
50 #pragma D option quiet
51 #pragma D option switchrate=10
52
53 self int depth;
54
55 dtrace:::BEGIN
56 {
57         self->depth = 0;
58         printf("%3s %-16s %-16s -- %s\n", "C", "TIME(us)", "FILE", "NAME");
59 }
60
61 sh*:::function-entry
62 {
63         printf("%3d %-16d %-16s %*s-> %s\n", cpu, timestamp / 1000, 
64             basename(copyinstr(arg0)), self->depth * 2, "", copyinstr(arg1));
65         self->depth++;
66 }
67
68 sh*:::function-return
69 {
70         self->depth -= self->depth > 0 ? 1 : 0;
71         printf("%3d %-16d %-16s %*s<- %s\n", cpu, timestamp / 1000,
72             basename(copyinstr(arg0)), self->depth * 2, "", copyinstr(arg1));
73 }
74
75 sh*:::builtin-entry
76 {
77         printf("%3d %-16d %-16s %*s> %s\n", cpu, timestamp / 1000, 
78             basename(copyinstr(arg0)), self->depth * 2, "", copyinstr(arg1));
79 }
80
81 sh*:::command-entry
82 {
83         printf("%3d %-16d %-16s %*s| %s\n", cpu, timestamp / 1000, 
84             basename(copyinstr(arg0)), self->depth * 2, "", copyinstr(arg1));
85 }