1#!/usr/sbin/dtrace -Zs
2/*
3 * j_flow.d - snoop Java execution showing method flow using DTrace.
4 *            Written for the Java hotspot DTrace provider.
5 *
6 * $Id: j_flow.d 38 2007-09-16 08:17:41Z 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_flow.d		# 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 switchrate=10
55
56self int depth[int];
57
58dtrace:::BEGIN
59{
60	printf("%3s %6s %-16s -- %s\n", "C", "PID", "TIME(us)", "CLASS.METHOD");
61}
62
63hotspot*:::method-entry
64{
65	this->class = (char *)copyin(arg1, arg2 + 1);
66	this->class[arg2] = '\0';
67	this->method = (char *)copyin(arg3, arg4 + 1);
68	this->method[arg4] = '\0';
69
70	printf("%3d %6d %-16d %*s-> %s.%s\n", cpu, pid, timestamp / 1000,
71	    self->depth[arg0] * 2, "", stringof(this->class),
72	    stringof(this->method));
73	self->depth[arg0]++;
74}
75
76hotspot*:::method-return
77{
78	this->class = (char *)copyin(arg1, arg2 + 1);
79	this->class[arg2] = '\0';
80	this->method = (char *)copyin(arg3, arg4 + 1);
81	this->method[arg4] = '\0';
82
83	self->depth[arg0] -= self->depth[arg0] > 0 ? 1 : 0;
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}
88