1 #!/usr/sbin/dtrace -Zs
2 /*
3  * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  *   - Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  *
12  *   - Redistributions in binary form must reproduce the above copyright
13  *     notice, this list of conditions and the following disclaimer in the
14  *     documentation and/or other materials provided with the distribution.
15  *
16  *   - Neither the name of Oracle nor the names of its
17  *     contributors may be used to endorse or promote products derived
18  *     from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34 */
35 
36 /*
37  * Usage:
38  *   1. method_invocation_tree.d -c "java ..."
39  *   2. method_invocation_tree.d -p JAVA_PID
40  *
41  * This script prints tree of Java and JNI method invocations.
42  *
43  * Notes:
44  *  - These probes are disabled by default since it incurs performance
45  *    overhead to the application. To trace the method-entry and
46  *    method-exit probes, you need to turn on the ExtendedDTraceProbes VM
47  *    option.
48  *    You can either start the application with -XX:+ExtendedDTraceProbes
49  *    option or use the jinfo command to enable it at runtime as follows:
50  *
51  *       jinfo -flag +ExtendedDTraceProbes <java_pid>
52  *
53  */
54 
55 #pragma D option quiet
56 #pragma D option destructive
57 #pragma D option defaultargs
58 #pragma D option bufsize=16m
59 #pragma D option aggrate=100ms
60 
61 self char *str_ptr;
62 self string class_name;
63 self string method_name;
64 self string signature;
65 
66 self int indent;
67 
68 BEGIN
69 {
70     SAMPLE_NAME = "hotspot method invocation tracing";
71 
72     printf("BEGIN %s\n\n", SAMPLE_NAME);
73 }
74 
75 hotspot$target:::*
76 /!self->indent/
77 {
78     self->indent = 0;
79 }
80 
81 /*
82  * hotspot:::method-entry, hotspot:::method-return probe arguments:
83  *  arg0: uintptr_t,    Java thread id
84  *  arg1: char*,        a pointer to mUTF-8 string containing the name of
85  *                          the class of the method being entered
86  *  arg2: uintptr_t,    the length of the class name (in bytes)
87  *  arg3: char*,        a pointer to mUTF-8 string data which contains the
88  *                          name of the method being entered
89  *  arg4: uintptr_t,    the length of the method name (in bytes)
90  *  arg5: char*,        a pointer to mUTF-8 string data which contains the
91  *                          signature of the method being entered
92  *  arg6: uintptr_t,    the length of the signature(in bytes)
93  */
94 
95 hotspot$target:::method-return
96 {
97     self->indent --;
98     METHOD_RETURN_CNT ++
99 }
100 
101 hotspot$target:::method-entry
102 {
103     self->indent ++;
104     METHOD_ENTRY_CNT ++;
105 
106     self->str_ptr = (char*) copyin(arg1, arg2+1);
107     self->str_ptr[arg2] = '\0';
108     self->class_name = (string) self->str_ptr;
109 
110     self->str_ptr = (char*) copyin(arg3, arg4+1);
111     self->str_ptr[arg4] = '\0';
112     self->method_name = (string) self->str_ptr;
113 
114     self->str_ptr = (char*) copyin(arg5, arg6+1);
115     self->str_ptr[arg6] = '\0';
116     self->signature = (string) self->str_ptr;
117 
118     printf("%-10u%*s%s:%s:%s\n",
119         tid, self->indent, "", self->class_name,
120         self->method_name, self->signature);
121 
122 }
123 
124 hotspot_jni$target:::*_entry
125 {
126     printf("%-10u%*sJNI:%s\n", tid, self->indent+1, "", probename);
127 }
128 
129 :::END
130 {
131     printf("METHOD_ENTRY_CNT:  %10d\n", METHOD_ENTRY_CNT);
132     printf("METHOD_RETURN_CNT: %10d\n", METHOD_RETURN_CNT);
133 
134     printf("\nEND of %s\n", SAMPLE_NAME);
135 }
136 
137 syscall::rexit:entry,
138 syscall::exit:entry
139 /pid == $target/
140 {
141    exit(0);
142 }
143