1 /* |
|
2 * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved. |
|
3 * |
|
4 * Redistribution and use in source and binary forms, with or without |
|
5 * modification, are permitted provided that the following conditions |
|
6 * are met: |
|
7 * |
|
8 * - Redistributions of source code must retain the above copyright |
|
9 * notice, this list of conditions and the following disclaimer. |
|
10 * |
|
11 * - Redistributions in binary form must reproduce the above copyright |
|
12 * notice, this list of conditions and the following disclaimer in the |
|
13 * documentation and/or other materials provided with the distribution. |
|
14 * |
|
15 * - Neither the name of Oracle nor the names of its |
|
16 * contributors may be used to endorse or promote products derived |
|
17 * from this software without specific prior written permission. |
|
18 * |
|
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
|
20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
|
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
30 */ |
|
31 |
|
32 package com.sun.demo.jvmti.hprof; |
|
33 |
|
34 /* This class and it's methods are used by hprof when injecting bytecodes |
|
35 * into class file images. |
|
36 * See the directory src/share/demo/jvmti/hprof and the file README.txt |
|
37 * for more details. |
|
38 */ |
|
39 |
|
40 public class Tracker { |
|
41 |
|
42 /* Master switch that activates calls to native functions. */ |
|
43 |
|
44 private static int engaged = 0; |
|
45 |
|
46 /* To track memory allocated, we need to catch object init's and arrays. */ |
|
47 |
|
48 /* At the beginning of java.jang.Object.<init>(), a call to |
|
49 * Tracker.ObjectInit() is injected. |
|
50 */ |
|
51 |
|
52 private static native void nativeObjectInit(Object thr, Object obj); |
|
53 |
|
54 public static void ObjectInit(Object obj) |
|
55 { |
|
56 if ( engaged != 0) { |
|
57 if (obj == null) { |
|
58 throw new IllegalArgumentException("Null object."); |
|
59 } |
|
60 nativeObjectInit(Thread.currentThread(), obj); |
|
61 } |
|
62 } |
|
63 |
|
64 /* Immediately following any of the newarray bytecodes, a call to |
|
65 * Tracker.NewArray() is injected. |
|
66 */ |
|
67 |
|
68 private static native void nativeNewArray(Object thr, Object obj); |
|
69 |
|
70 public static void NewArray(Object obj) |
|
71 { |
|
72 if ( engaged != 0) { |
|
73 if (obj == null) { |
|
74 throw new IllegalArgumentException("Null object."); |
|
75 } |
|
76 nativeNewArray(Thread.currentThread(), obj); |
|
77 } |
|
78 } |
|
79 |
|
80 /* For cpu time spent in methods, we need to inject for every method. */ |
|
81 |
|
82 /* At the very beginning of every method, a call to |
|
83 * Tracker.CallSite() is injected. |
|
84 */ |
|
85 |
|
86 private static native void nativeCallSite(Object thr, int cnum, int mnum); |
|
87 |
|
88 public static void CallSite(int cnum, int mnum) |
|
89 { |
|
90 if ( engaged != 0 ) { |
|
91 if (cnum < 0) { |
|
92 throw new IllegalArgumentException("Negative class index"); |
|
93 } |
|
94 |
|
95 if (mnum < 0) { |
|
96 throw new IllegalArgumentException("Negative method index"); |
|
97 } |
|
98 |
|
99 nativeCallSite(Thread.currentThread(), cnum, mnum); |
|
100 } |
|
101 } |
|
102 |
|
103 /* Before any of the return bytecodes, a call to |
|
104 * Tracker.ReturnSite() is injected. |
|
105 */ |
|
106 |
|
107 private static native void nativeReturnSite(Object thr, int cnum, int mnum); |
|
108 |
|
109 public static void ReturnSite(int cnum, int mnum) |
|
110 { |
|
111 if ( engaged != 0 ) { |
|
112 if (cnum < 0) { |
|
113 throw new IllegalArgumentException("Negative class index"); |
|
114 } |
|
115 |
|
116 if (mnum < 0) { |
|
117 throw new IllegalArgumentException("Negative method index"); |
|
118 } |
|
119 |
|
120 nativeReturnSite(Thread.currentThread(), cnum, mnum); |
|
121 } |
|
122 } |
|
123 |
|
124 } |
|