tcl_syscolors.d revision 235368
1#!/usr/sbin/dtrace -Zs
2/*
3 * tcl_syscolors.d - trace Tcl program flow plus syscalls, in color.
4 *                   Written for the Tcl DTrace provider.
5 *
6 * $Id: tcl_syscolors.d 63 2007-10-04 04:34:38Z brendan $
7 *
8 * This traces activity from all Tcl processes on the system with DTrace
9 * provider support (tcl8.4.16).
10 *
11 * USAGE: tcl_syscolors.d { -p PID | -c cmd }	# hit Ctrl-C to end
12 *
13 * This watches Tcl method entries and returns, and indents child
14 * method calls.
15 *
16 * FIELDS:
17 *		C		CPU-id
18 *		PID		Process ID
19 *		TID		Thread ID
20 *		DELTA(us)	Elapsed time from previous line to this line
21 *		TYPE		Type of call (proc/cmd/syscall)
22 *		NAME		Tcl proc/cmd or syscall name
23 *
24 * WARNING: Watch the first column carefully, it prints the CPU-id. If it
25 * changes, then it is very likely that the output has been shuffled.
26 *
27 * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
28 *
29 * CDDL HEADER START
30 *
31 *  The contents of this file are subject to the terms of the
32 *  Common Development and Distribution License, Version 1.0 only
33 *  (the "License").  You may not use this file except in compliance
34 *  with the License.
35 *
36 *  You can obtain a copy of the license at Docs/cddl1.txt
37 *  or http://www.opensolaris.org/os/licensing.
38 *  See the License for the specific language governing permissions
39 *  and limitations under the License.
40 *
41 * CDDL HEADER END
42 *
43 * 09-Sep-2007	Brendan Gregg	Created this.
44 */
45
46#pragma D option quiet
47#pragma D option switchrate=10
48
49self int depth;
50
51dtrace:::BEGIN
52{
53        color_tcl = "\033[2;35m";		/* violet, faint */
54        color_line = "\033[1;35m";		/* violet, bold */
55        color_syscall = "\033[2;32m";		/* green, faint */
56        color_off = "\033[0m";			/* default */
57
58	printf("%3s %6s %9s %-8s -- %s\n", "C", "PID", "DELTA(us)", "TYPE",
59	    "NAME");
60}
61
62tcl$target:::method-entry,
63tcl$target:::method-return,
64syscall:::entry,
65syscall:::return
66/self->last == 0 && pid == $target/
67{
68	self->last = timestamp;
69}
70
71tcl$target:::proc-entry
72{
73	this->delta = (timestamp - self->last) / 1000;
74	printf("%s%3d %6d %9d %-8s %*s-> %s%s\n", color_tcl, cpu,
75	    pid, this->delta, "proc", self->depth * 2, "", copyinstr(arg0),
76	    color_off);
77	self->depth++;
78	self->depthlast = self->depth;
79	self->last = timestamp;
80}
81
82tcl$target:::proc-return
83{
84	this->delta = (timestamp - self->last) / 1000;
85	self->depth -= self->depth > 0 ? 1 : 0;
86	printf("%s%3d %6d %9d %-8s %*s<- %s%s\n", color_tcl, cpu,
87	    pid, this->delta, "proc", self->depth * 2, "", copyinstr(arg0),
88	    color_off);
89	self->depthlast = self->depth;
90	self->last = timestamp;
91}
92
93tcl$target:::cmd-entry
94{
95	this->delta = (timestamp - self->last) / 1000;
96	printf("%s%3d %6d %9d %-8s %*s-> %s%s\n", color_tcl, cpu,
97	    pid, this->delta, "cmd", self->depth * 2, "", copyinstr(arg0),
98	    color_off);
99	self->depth++;
100	self->depthlast = self->depth;
101	self->last = timestamp;
102}
103
104tcl$target:::cmd-return
105{
106	this->delta = (timestamp - self->last) / 1000;
107	self->depth -= self->depth > 0 ? 1 : 0;
108	printf("%s%3d %6d %9d %-8s %*s<- %s%s\n", color_tcl, cpu,
109	    pid, this->delta, "cmd", self->depth * 2, "", copyinstr(arg0),
110	    color_off);
111	self->depthlast = self->depth;
112	self->last = timestamp;
113}
114
115syscall:::entry
116/pid == $target/
117{
118	this->delta = (timestamp - self->last) / 1000;
119	printf("%s%3d %6d %9d %-8s %*s-> %s%s\n", color_syscall,
120	    cpu, pid, this->delta, "syscall", self->depthlast * 2, "",
121	    probefunc, color_off);
122	self->last = timestamp;
123}
124
125syscall:::return
126/pid == $target/
127{
128	this->delta = (timestamp - self->last) / 1000;
129	printf("%s%3d %6d %9d %-8s %*s<- %s%s\n", color_syscall,
130	    cpu, pid, this->delta, "syscall", self->depthlast * 2, "",
131	    probefunc, color_off);
132	self->last = timestamp;
133}
134
135proc:::exit
136/pid == $target/
137{
138	exit(0);
139}
140