1235368Sgnn#!/usr/sbin/dtrace -Zs
2235368Sgnn/*
3235368Sgnn * rb_syscolors.d - trace Ruby method flow plus syscalls, in color.
4235368Sgnn *                  Written for the Ruby DTrace provider.
5235368Sgnn *
6235368Sgnn * $Id: rb_syscolors.d 27 2007-09-13 09:26:01Z brendan $
7235368Sgnn *
8235368Sgnn * USAGE: rb_syscolors.d { -p PID | -c cmd }	# hit Ctrl-C to end
9235368Sgnn *
10235368Sgnn * This watches Ruby method entries and returns, and indents child
11235368Sgnn * function calls.
12235368Sgnn *
13235368Sgnn * FIELDS:
14235368Sgnn *		C		CPU-id
15235368Sgnn *		PID		Process ID
16235368Sgnn *		DELTA(us)	Elapsed time from previous line to this line
17235368Sgnn *		FILE		Filename of the Ruby program
18235368Sgnn *		LINE		Line number of filename
19235368Sgnn *		TYPE		Type of call (method/line/syscall)
20235368Sgnn *		NAME		Ruby method or syscall name
21235368Sgnn *
22235368Sgnn * Filename and method names are printed if available.
23235368Sgnn *
24235368Sgnn * WARNING: Watch the first column carefully, it prints the CPU-id. If it
25235368Sgnn * changes, then it is very likely that the output has been shuffled.
26235368Sgnn *
27235368Sgnn * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
28235368Sgnn *
29235368Sgnn * CDDL HEADER START
30235368Sgnn *
31235368Sgnn *  The contents of this file are subject to the terms of the
32235368Sgnn *  Common Development and Distribution License, Version 1.0 only
33235368Sgnn *  (the "License").  You may not use this file except in compliance
34235368Sgnn *  with the License.
35235368Sgnn *
36235368Sgnn *  You can obtain a copy of the license at Docs/cddl1.txt
37235368Sgnn *  or http://www.opensolaris.org/os/licensing.
38235368Sgnn *  See the License for the specific language governing permissions
39235368Sgnn *  and limitations under the License.
40235368Sgnn *
41235368Sgnn * CDDL HEADER END
42235368Sgnn *
43235368Sgnn * 09-Sep-2007	Brendan Gregg	Created this.
44235368Sgnn */
45235368Sgnn
46235368Sgnn#pragma D option quiet
47235368Sgnn#pragma D option switchrate=10
48235368Sgnn
49235368Sgnnself int depth;
50235368Sgnn
51235368Sgnndtrace:::BEGIN
52235368Sgnn{
53235368Sgnn	/*
54235368Sgnn	 * The following are terminal color escape sequences.
55235368Sgnn	 * Change them to whatever you prefer, eg HTML font tags.
56235368Sgnn	 */
57235368Sgnn        color_ruby = "\033[2;35m";		/* violet, faint */
58235368Sgnn        color_line = "\033[1;35m";		/* violet, bold */
59235368Sgnn        color_syscall = "\033[2;32m";		/* green, faint */
60235368Sgnn        color_off = "\033[0m";			/* default */
61235368Sgnn
62235368Sgnn	printf("%s %6s %10s  %16s:%-4s %-8s -- %s\n", "C", "PID", "DELTA(us)",
63235368Sgnn	    "FILE", "LINE", "TYPE", "NAME");
64235368Sgnn}
65235368Sgnn
66235368Sgnnruby$target:::function-entry,
67235368Sgnnruby$target:::function-return,
68235368Sgnnruby$target:::line,
69235368Sgnnsyscall:::entry,
70235368Sgnnsyscall:::return
71235368Sgnn/self->last == 0 && pid == $target/
72235368Sgnn{
73235368Sgnn	self->last = timestamp;
74235368Sgnn}
75235368Sgnn
76235368Sgnnruby$target:::function-entry
77235368Sgnn{
78235368Sgnn	this->delta = (timestamp - self->last) / 1000;
79235368Sgnn	this->name = strjoin(strjoin(copyinstr(arg0), "::"), copyinstr(arg1));
80235368Sgnn	printf("%s%d %6d %10d  %16s:%-4d %-8s %*s-> %s%s\n", color_ruby,
81235368Sgnn	    cpu, pid, this->delta, basename(copyinstr(arg2)), arg3, "method",
82235368Sgnn	    self->depth * 2, "", this->name, color_off);
83235368Sgnn	self->depth++;
84235368Sgnn	self->last = timestamp;
85235368Sgnn}
86235368Sgnn
87235368Sgnnruby$target:::function-return
88235368Sgnn{
89235368Sgnn	this->delta = (timestamp - self->last) / 1000;
90235368Sgnn	this->name = strjoin(strjoin(copyinstr(arg0), "::"), copyinstr(arg1));
91235368Sgnn	self->depth--;
92235368Sgnn	printf("%s%d %6d %10d  %16s:%-4d %-8s %*s<- %s%s\n", color_ruby,
93235368Sgnn	    cpu, pid, this->delta, basename(copyinstr(arg2)), arg3, "method",
94235368Sgnn	    self->depth * 2, "", this->name, color_off);
95235368Sgnn	self->last = timestamp;
96235368Sgnn}
97235368Sgnn
98235368Sgnnruby$target:::line
99235368Sgnn{
100235368Sgnn	this->delta = (timestamp - self->last) / 1000;
101235368Sgnn	printf("%s%d %6d %10d  %16s:%-4d %-8s %*s-- %s\n", color_line,
102235368Sgnn	    cpu, pid, this->delta, basename(copyinstr(arg0)), arg1, "line",
103235368Sgnn	    self->depth * 2, "", color_off);
104235368Sgnn	self->last = timestamp;
105235368Sgnn}
106235368Sgnn
107235368Sgnnsyscall:::entry
108235368Sgnn/pid == $target/
109235368Sgnn{
110235368Sgnn	this->delta = (timestamp - self->last) / 1000;
111235368Sgnn	printf("%s%d %6d %10d  %16s:-    %-8s %*s-> %s%s\n", color_syscall,
112235368Sgnn	    cpu, pid, this->delta, "\"", "syscall", self->depth * 2, "",
113235368Sgnn	    probefunc, color_off);
114235368Sgnn	self->depth++;
115235368Sgnn	self->last = timestamp;
116235368Sgnn}
117235368Sgnn
118235368Sgnnsyscall:::return
119235368Sgnn/pid == $target/
120235368Sgnn{
121235368Sgnn	this->delta = (timestamp - self->last) / 1000;
122235368Sgnn	self->depth--;
123235368Sgnn	printf("%s%d %6d %10d  %16s:-    %-8s %*s<- %s%s\n", color_syscall,
124235368Sgnn	    cpu, pid, this->delta, "\"", "syscall", self->depth * 2, "",
125235368Sgnn	    probefunc, color_off);
126235368Sgnn	self->last = timestamp;
127235368Sgnn}
128235368Sgnn
129235368Sgnnproc:::exit
130235368Sgnn/pid == $target/
131235368Sgnn{
132235368Sgnn	exit(0);
133235368Sgnn}
134