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. monitors.d -c "java ..."
39 *   2. monitors.d -p JAVA_PID
40 *
41 * The script traces monitor related probes.
42 *
43 * Notes:
44 *  - These probes are disabled by default since it incurs performance
45 *    overhead to the application. To trace the monitor-* probes, you need
46 *    to turn on the ExtendedDTraceProbes VM option.
47 *    You can either start the application with -XX:+ExtendedDTraceProbes
48 *    option or use the jinfo command to enable it at runtime as follows:
49 *
50 *       jinfo -flag +ExtendedDTraceProbes <java_pid>
51 *
52 */
53
54#pragma D option quiet
55#pragma D option destructive
56#pragma D option defaultargs
57#pragma D option aggrate=100ms
58
59
60self string thread_name;
61self char* str_ptr;
62
63:::BEGIN
64{
65    SAMPLE_NAME = "hotspot monitors tracing";
66
67    printf("BEGIN %s\n\n", SAMPLE_NAME);
68}
69
70/*
71 * hotspot:::thread-start, hotspot:::thread-stop probe arguments:
72 *  arg0: char*,        thread name passed as mUTF8 string
73 *  arg1: uintptr_t,    thread name length
74 *  arg2: uintptr_t,    Java thread id
75 *  arg3: uintptr_t,    native/OS thread id
76 *  arg4: uintptr_t,    is a daemon or not
77 */
78hotspot$target:::thread-start
79{
80    self->str_ptr = (char*) copyin(arg0, arg1+1);
81    self->str_ptr[arg1] = '\0';
82    self->thread_name = (string) self->str_ptr;
83
84    printf("thread-start: id=%d, is_daemon=%d, name=%s, os_id=%d\n",
85            arg2, arg4, self->thread_name, arg3);
86
87    threads[arg2] = self->thread_name;
88}
89
90
91hotspot$target:::thread-stop
92{
93    self->str_ptr = (char*) copyin(arg0, arg1+1);
94    self->str_ptr[arg1] = '\0';
95    self->thread_name = (string) self->str_ptr;
96
97
98    printf("thread-stop: id=%d, is_daemon=%d, name=%s, os_id=%d\n",
99            arg2, arg4, self->thread_name, arg3);
100}
101
102/*
103 *
104 * hotspot::monitor-contended-enter, hotspot::monitor-contended-entered
105 *
106 *  arg0: uintptr_t,    the Java thread identifier for the thread peforming
107 *                          the monitor operation
108 *  arg1: uintptr_t,    a unique, but opaque identifier for the specific
109 *                          monitor that the action is performed upon
110 *  arg2: char*,        a pointer to mUTF-8 string data which contains the
111 *                          name of the class of the object being acted upon
112 *  arg3: uintptr_t,    the length of the class name (in bytes)
113 */
114
115hotspot$target:::monitor-contended-enter
116{
117    /* (uintptr_t thread_id, uintptr_t monitor_id,
118       char* obj_class_name, uintptr_t obj_class_name_len) */
119
120    self->str_ptr = (char*) copyin(arg2, arg3+1);
121    self->str_ptr[arg3] = '\0';
122    self->class_name = (string) self->str_ptr;
123
124    monitors[arg1] = self->class_name;
125
126    monitors_enter[arg1] = arg0;
127    printf("%s: -> enter monitor (%d) %s\n",
128        threads[arg0], arg1, monitors[arg1]);
129}
130
131hotspot$target:::monitor-contended-entered
132{
133    /* (uintptr_t thread_id, uintptr_t monitor_id, char* obj_class_name,
134        uintptr_t obj_class_name_len) */
135
136    monitors_entered[arg1] = arg0;
137    printf("%s: <- entered monitor (%d) %s\n",
138        threads[arg0], arg1, monitors[arg1]);
139}
140
141
142:::END
143{
144    printf("\nEND of %s\n", SAMPLE_NAME);
145}
146
147syscall::rexit:entry,
148syscall::exit:entry
149/pid == $target/
150{
151   exit(0);
152}
153