1235368Sgnn#!/usr/sbin/dtrace -Zs
2235368Sgnn/*
3235368Sgnn * j_classflow.d - trace a Java class method flow using DTrace.
4235368Sgnn *                 Written for the Java hotspot DTrace provider.
5235368Sgnn *
6235368Sgnn * $Id: j_classflow.d 63 2007-10-04 04:34:38Z brendan $
7235368Sgnn *
8235368Sgnn * This traces activity from all Java processes on the system with hotspot
9235368Sgnn * provider support (1.6.0) and the flag "+ExtendedDTraceProbes". eg,
10235368Sgnn * java -XX:+ExtendedDTraceProbes classfile
11235368Sgnn *
12235368Sgnn * USAGE: j_classflow.d	classname	# hit Ctrl-C to end
13235368Sgnn *
14235368Sgnn * This watches Java method entries and returns, and indents child
15235368Sgnn * method calls.
16235368Sgnn *
17235368Sgnn * FIELDS:
18235368Sgnn *		C		CPU-id
19235368Sgnn *		TIME(us)	Time since boot, us
20235368Sgnn *		PID		Process ID
21235368Sgnn *		CLASS.METHOD	Java class and method name
22235368Sgnn *
23235368Sgnn * LEGEND:
24235368Sgnn *		->		method entry
25235368Sgnn *		<-		method return
26235368Sgnn *
27235368Sgnn * WARNING: Watch the first column carefully, it prints the CPU-id. If it
28235368Sgnn * changes, then it is very likely that the output has been shuffled.
29235368Sgnn * Changes in TID will appear to shuffle output, as we change from one thread
30235368Sgnn * depth to the next. See Docs/Notes/ALLjavaflow.txt for additional notes.
31235368Sgnn *
32235368Sgnn * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
33235368Sgnn *
34235368Sgnn * CDDL HEADER START
35235368Sgnn *
36235368Sgnn *  The contents of this file are subject to the terms of the
37235368Sgnn *  Common Development and Distribution License, Version 1.0 only
38235368Sgnn *  (the "License").  You may not use this file except in compliance
39235368Sgnn *  with the License.
40235368Sgnn *
41235368Sgnn *  You can obtain a copy of the license at Docs/cddl1.txt
42235368Sgnn *  or http://www.opensolaris.org/os/licensing.
43235368Sgnn *  See the License for the specific language governing permissions
44235368Sgnn *  and limitations under the License.
45235368Sgnn *
46235368Sgnn * CDDL HEADER END
47235368Sgnn *
48235368Sgnn * 09-Sep-2007	Brendan Gregg	Created this.
49235368Sgnn */
50235368Sgnn
51235368Sgnn/* increasing bufsize can reduce drops */
52235368Sgnn#pragma D option bufsize=16m
53235368Sgnn#pragma D option quiet
54235368Sgnn#pragma D option defaultargs
55235368Sgnn#pragma D option switchrate=10
56235368Sgnn
57235368Sgnnself int depth[int];
58235368Sgnn
59235368Sgnndtrace:::BEGIN
60235368Sgnn/$$1 == ""/
61235368Sgnn{
62235368Sgnn	printf("USAGE: j_classflow.d classname\n");
63235368Sgnn	exit(1);
64235368Sgnn}
65235368Sgnn
66235368Sgnndtrace:::BEGIN
67235368Sgnn{
68235368Sgnn	printf("%3s %6s %-16s -- %s\n", "C", "PID", "TIME(us)", "CLASS.METHOD");
69235368Sgnn}
70235368Sgnn
71235368Sgnnhotspot*:::method-entry,
72235368Sgnnhotspot*:::method-return
73235368Sgnn{
74235368Sgnn	this->class = stringof((char *)copyin(arg1, arg2 + 1));
75235368Sgnn	this->class[arg2] = '\0';
76235368Sgnn}
77235368Sgnn
78235368Sgnnhotspot*:::method-entry
79235368Sgnn/this->class == $$1/
80235368Sgnn{
81235368Sgnn	this->method = (char *)copyin(arg3, arg4 + 1);
82235368Sgnn	this->method[arg4] = '\0';
83235368Sgnn
84235368Sgnn	printf("%3d %6d %-16d %*s-> %s.%s\n", cpu, pid, timestamp / 1000,
85235368Sgnn	    self->depth[arg0] * 2, "", stringof(this->class),
86235368Sgnn	    stringof(this->method));
87235368Sgnn	self->depth[arg0]++;
88235368Sgnn}
89235368Sgnn
90235368Sgnnhotspot*:::method-return
91235368Sgnn/this->class == $$1/
92235368Sgnn{
93235368Sgnn	this->method = (char *)copyin(arg3, arg4 + 1);
94235368Sgnn	this->method[arg4] = '\0';
95235368Sgnn
96235368Sgnn	self->depth[arg0] -= self->depth[arg0] > 0 ? 1 : 0;
97235368Sgnn	printf("%3d %6d %-16d %*s<- %s.%s\n", cpu, pid, timestamp / 1000,
98235368Sgnn	    self->depth[arg0] * 2, "", stringof(this->class),
99235368Sgnn	    stringof(this->method));
100235368Sgnn}
101