1#!/usr/sbin/dtrace -Zs
2/*
3 * j_syscolors.d - trace Java method flow plus syscalls, in color.
4 *                 Written for the Java hotspot DTrace provider.
5 *
6 * $Id: j_syscolors.d 58 2007-10-01 13:36:29Z brendan $
7 *
8 * This traces Java methods if the hotspot provider exists (1.6.0) and
9 * the flag "+ExtendedDTraceProbes" is used. eg,
10 * java -XX:+ExtendedDTraceProbes classfile
11 *
12 * USAGE: j_syscolors.d { -p PID | -c cmd }	# hit Ctrl-C to end
13 *
14 * This watches Java method entries and returns, and indents child
15 * method calls.
16 *
17 * FIELDS:
18 *		C		CPU-id
19 *		PID		Process ID
20 *		TID		Thread ID
21 *		DELTA(us)	Elapsed time from previous line to this line
22 *		TYPE		Type of call (func/syscall)
23 *		NAME		Java method or syscall name
24 *
25 * If the flow appears to jump, check the TID column - the JVM may have
26 * switched to another thread.
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 * Changes in TID will appear to shuffle output, as we change from one thread
31 * depth to the next. See Docs/Notes/ALLjavaflow.txt for additional notes.
32 *
33 * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
34 *
35 * CDDL HEADER START
36 *
37 *  The contents of this file are subject to the terms of the
38 *  Common Development and Distribution License, Version 1.0 only
39 *  (the "License").  You may not use this file except in compliance
40 *  with the License.
41 *
42 *  You can obtain a copy of the license at Docs/cddl1.txt
43 *  or http://www.opensolaris.org/os/licensing.
44 *  See the License for the specific language governing permissions
45 *  and limitations under the License.
46 *
47 * CDDL HEADER END
48 *
49 * 09-Sep-2007	Brendan Gregg	Created this.
50 */
51
52/* increasing bufsize can reduce drops */
53#pragma D option bufsize=32m
54#pragma D option quiet
55#pragma D option switchrate=10
56
57self int depth[int];
58
59dtrace:::BEGIN
60{
61        color_java = "\033[2;35m";		/* violet, faint */
62        color_line = "\033[1;35m";		/* violet, bold */
63        color_syscall = "\033[2;32m";		/* green, faint */
64        color_off = "\033[0m";			/* default */
65
66	printf("%3s %6s/%-5s %9s %-8s -- %s\n", "C", "PID", "TID", "DELTA(us)",
67	    "TYPE", "NAME");
68}
69
70hotspot$target:::method-entry,
71hotspot$target:::method-return,
72syscall:::entry,
73syscall:::return
74/self->last == 0 && pid == $target/
75{
76	self->last = timestamp;
77}
78
79hotspot$target:::method-entry
80{
81	this->delta = (timestamp - self->last) / 1000;
82	this->class = (char *)copyin(arg1, arg2 + 1);
83	this->class[arg2] = '\0';
84	this->method = (char *)copyin(arg3, arg4 + 1);
85	this->method[arg4] = '\0';
86
87	printf("%s%3d %6d/%-5d %9d %-8s %*s-> %s.%s%s\n", color_java, cpu,
88	    pid, tid, this->delta, "method", self->depth[arg0] * 2, "",
89	    stringof(this->class), stringof(this->method), color_off);
90	self->depth[arg0]++;
91	self->depthlast = self->depth[arg0];
92	self->last = timestamp;
93}
94
95hotspot$target:::method-return
96{
97	this->delta = (timestamp - self->last) / 1000;
98	this->class = (char *)copyin(arg1, arg2 + 1);
99	this->class[arg2] = '\0';
100	this->method = (char *)copyin(arg3, arg4 + 1);
101	this->method[arg4] = '\0';
102
103	self->depth[arg0] -= self->depth[arg0] > 0 ? 1 : 0;
104	printf("%s%3d %6d/%-5d %9d %-8s %*s<- %s.%s%s\n", color_java, cpu,
105	    pid, tid, this->delta, "method", self->depth[arg0] * 2, "",
106	    stringof(this->class), stringof(this->method), color_off);
107	self->depthlast = self->depth[arg0];
108	self->last = timestamp;
109}
110
111syscall:::entry
112/pid == $target/
113{
114	this->delta = (timestamp - self->last) / 1000;
115	printf("%s%3d %6d/%-5d %9d %-8s %*s-> %s%s\n", color_syscall,
116	    cpu, pid, tid, this->delta, "syscall", self->depthlast * 2, "",
117	    probefunc, color_off);
118	self->last = timestamp;
119}
120
121syscall:::return
122/pid == $target/
123{
124	this->delta = (timestamp - self->last) / 1000;
125	printf("%s%3d %6d/%-5d %9d %-8s %*s<- %s%s\n", color_syscall,
126	    cpu, pid, tid, this->delta, "syscall", self->depthlast * 2, "",
127	    probefunc, color_off);
128	self->last = timestamp;
129}
130
131proc:::exit
132/pid == $target/
133{
134	exit(0);
135}
136