1#!/usr/sbin/dtrace -Zs
2/*
3 * j_classflow.d - trace a Java class method flow using DTrace.
4 *                 Written for the Java hotspot DTrace provider.
5 *
6 * $Id: j_classflow.d 63 2007-10-04 04:34:38Z brendan $
7 *
8 * This traces activity from all Java processes on the system with hotspot
9 * provider support (1.6.0) and the flag "+ExtendedDTraceProbes". eg,
10 * java -XX:+ExtendedDTraceProbes classfile
11 *
12 * USAGE: j_classflow.d	classname	# 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 *		TIME(us)	Time since boot, us
20 *		PID		Process ID
21 *		CLASS.METHOD	Java class and method name
22 *
23 * LEGEND:
24 *		->		method entry
25 *		<-		method return
26 *
27 * WARNING: Watch the first column carefully, it prints the CPU-id. If it
28 * changes, then it is very likely that the output has been shuffled.
29 * Changes in TID will appear to shuffle output, as we change from one thread
30 * depth to the next. See Docs/Notes/ALLjavaflow.txt for additional notes.
31 *
32 * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
33 *
34 * CDDL HEADER START
35 *
36 *  The contents of this file are subject to the terms of the
37 *  Common Development and Distribution License, Version 1.0 only
38 *  (the "License").  You may not use this file except in compliance
39 *  with the License.
40 *
41 *  You can obtain a copy of the license at Docs/cddl1.txt
42 *  or http://www.opensolaris.org/os/licensing.
43 *  See the License for the specific language governing permissions
44 *  and limitations under the License.
45 *
46 * CDDL HEADER END
47 *
48 * 09-Sep-2007	Brendan Gregg	Created this.
49 */
50
51/* increasing bufsize can reduce drops */
52#pragma D option bufsize=16m
53#pragma D option quiet
54#pragma D option defaultargs
55#pragma D option switchrate=10
56
57self int depth[int];
58
59dtrace:::BEGIN
60/$$1 == ""/
61{
62	printf("USAGE: j_classflow.d classname\n");
63	exit(1);
64}
65
66dtrace:::BEGIN
67{
68	printf("%3s %6s %-16s -- %s\n", "C", "PID", "TIME(us)", "CLASS.METHOD");
69}
70
71hotspot*:::method-entry,
72hotspot*:::method-return
73{
74	this->class = stringof((char *)copyin(arg1, arg2 + 1));
75	this->class[arg2] = '\0';
76}
77
78hotspot*:::method-entry
79/this->class == $$1/
80{
81	this->method = (char *)copyin(arg3, arg4 + 1);
82	this->method[arg4] = '\0';
83
84	printf("%3d %6d %-16d %*s-> %s.%s\n", cpu, pid, timestamp / 1000,
85	    self->depth[arg0] * 2, "", stringof(this->class),
86	    stringof(this->method));
87	self->depth[arg0]++;
88}
89
90hotspot*:::method-return
91/this->class == $$1/
92{
93	this->method = (char *)copyin(arg3, arg4 + 1);
94	this->method[arg4] = '\0';
95
96	self->depth[arg0] -= self->depth[arg0] > 0 ? 1 : 0;
97	printf("%3d %6d %-16d %*s<- %s.%s\n", cpu, pid, timestamp / 1000,
98	    self->depth[arg0] * 2, "", stringof(this->class),
99	    stringof(this->method));
100}
101