1 #!/usr/sbin/dtrace -s
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  * Description:
38  * dtrace -c option launches the command specified in the -c argument and
39  * starts tracing the process. Typically, you can run a D script and trace
40  * a Java application as follows:
41  *    dscript.d -Zc "java HelloWorld"
42  *
43  * The -Z option is needed to permit probe descriptions that match zero
44  * probes because Hotspot probes definitions are located in libjvm.so which
45  * has not been yet loaded and thus can't be enabled until the application
46  * is started.
47  *
48  * Straightforward attempt to run D script may fail, e.g.:
49  *    dscript.d -c "java HelloWorld"
50  *    "probe description hotspotPID:::probename does not match any probes"
51  *
52  * This is because DTrace tries to enable probes before libjvm.so is loaded.
53  * The -Z option requires Solaris patch 118822-30 installed on your system.
54  *
55  * In case you don't have this Solaris patch use dtrace_helper.d script.
56  * This script waits until the Hotspot DTrace probes are loaded and then
57  * stops the Java process (passed as '-c' options). After the process is
58  * stopped, another D script (passed as first argument) is called to do real
59  * trace of Java process.
60  *
61  * Usage example:
62  *   dtrace_helper.d -c "java ..." ../hotspot/class_loading_stat.d
63  */
64 
65 #pragma D option quiet
66 #pragma D option destructive
67 
68 
69 pid$target::dlopen:entry
70 {
71     self->filename = arg0;
72 }
73 
74 
75 pid$target::dlopen:return
76 /self->filename && basename(copyinstr(self->filename)) == "libjvm.so"/
77 {
78     printf(" loaded %s\n", basename(copyinstr(self->filename)));
79     self->filename = 0;
80 
81     stop();
82     printf(" stopped java process with pid=%d \n", $target);
83 
84     printf(" run: %s -p %d &", $1, $target);
85     system("(%s -p %d) &", $1, $target);
86     exit(0);
87 }
88