|
1 /* |
|
2 * Copyright (c) 2003, 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 /* |
|
33 * This source code is provided to illustrate the usage of a given feature |
|
34 * or technique and has been deliberately simplified. Additional steps |
|
35 * required for a production-quality application, such as security checks, |
|
36 * input validation and proper error handling, might not be present in |
|
37 * this sample code. |
|
38 */ |
|
39 |
|
40 |
|
41 /* This file contains all class, method and allocation event support functions, |
|
42 * both JVMTI and BCI events. |
|
43 * (See hprof_monitor.c for the monitor event handlers). |
|
44 */ |
|
45 |
|
46 #include "hprof.h" |
|
47 |
|
48 /* Private internal functions. */ |
|
49 |
|
50 /* Return a TraceIndex for the given thread. */ |
|
51 static TraceIndex |
|
52 get_current(TlsIndex tls_index, JNIEnv *env, jboolean skip_init) |
|
53 { |
|
54 TraceIndex trace_index; |
|
55 |
|
56 trace_index = tls_get_trace(tls_index, env, gdata->max_trace_depth, skip_init); |
|
57 return trace_index; |
|
58 } |
|
59 |
|
60 /* Return a ClassIndex for the given jclass, loader supplied or looked up. */ |
|
61 static ClassIndex |
|
62 find_cnum(JNIEnv *env, jclass klass, jobject loader) |
|
63 { |
|
64 LoaderIndex loader_index; |
|
65 ClassIndex cnum; |
|
66 char * signature; |
|
67 |
|
68 HPROF_ASSERT(klass!=NULL); |
|
69 |
|
70 /* Get the loader index */ |
|
71 loader_index = loader_find_or_create(env, loader); |
|
72 |
|
73 /* Get the signature for this class */ |
|
74 getClassSignature(klass, &signature, NULL); |
|
75 |
|
76 /* Find the ClassIndex for this class */ |
|
77 cnum = class_find_or_create(signature, loader_index); |
|
78 |
|
79 /* Free the signature space */ |
|
80 jvmtiDeallocate(signature); |
|
81 |
|
82 /* Make sure we save a global reference to this class in the table */ |
|
83 HPROF_ASSERT(cnum!=0); |
|
84 (void)class_new_classref(env, cnum, klass); |
|
85 return cnum; |
|
86 } |
|
87 |
|
88 /* Get the ClassIndex for the superClass of this jclass. */ |
|
89 static ClassIndex |
|
90 get_super(JNIEnv *env, jclass klass) |
|
91 { |
|
92 ClassIndex super_cnum; |
|
93 |
|
94 super_cnum = 0; |
|
95 WITH_LOCAL_REFS(env, 1) { |
|
96 jclass super_klass; |
|
97 |
|
98 super_klass = getSuperclass(env, klass); |
|
99 if ( super_klass != NULL ) { |
|
100 super_cnum = find_cnum(env, super_klass, |
|
101 getClassLoader(super_klass)); |
|
102 } |
|
103 } END_WITH_LOCAL_REFS; |
|
104 return super_cnum; |
|
105 } |
|
106 |
|
107 /* Record an allocation. Could be jobject, jclass, jarray or primitive type. */ |
|
108 static void |
|
109 any_allocation(JNIEnv *env, SerialNumber thread_serial_num, |
|
110 TraceIndex trace_index, jobject object) |
|
111 { |
|
112 SiteIndex site_index; |
|
113 ClassIndex cnum; |
|
114 jint size; |
|
115 jclass klass; |
|
116 |
|
117 /* NOTE: Normally the getObjectClass() and getClassLoader() |
|
118 * would require a |
|
119 * WITH_LOCAL_REFS(env, 1) { |
|
120 * } END_WITH_LOCAL_REFS; |
|
121 * but for performance reasons we skip it here. |
|
122 */ |
|
123 |
|
124 /* Get and tag the klass */ |
|
125 klass = getObjectClass(env, object); |
|
126 cnum = find_cnum(env, klass, getClassLoader(klass)); |
|
127 site_index = site_find_or_create(cnum, trace_index); |
|
128 tag_class(env, klass, cnum, thread_serial_num, site_index); |
|
129 |
|
130 /* Tag the object */ |
|
131 size = (jint)getObjectSize(object); |
|
132 tag_new_object(object, OBJECT_NORMAL, thread_serial_num, size, site_index); |
|
133 } |
|
134 |
|
135 /* Handle a java.lang.Object.<init> object allocation. */ |
|
136 void |
|
137 event_object_init(JNIEnv *env, jthread thread, jobject object) |
|
138 { |
|
139 /* Called via BCI Tracker class */ |
|
140 |
|
141 /* Be very careful what is called here, watch out for recursion. */ |
|
142 |
|
143 jint *pstatus; |
|
144 TraceIndex trace_index; |
|
145 SerialNumber thread_serial_num; |
|
146 |
|
147 HPROF_ASSERT(env!=NULL); |
|
148 HPROF_ASSERT(thread!=NULL); |
|
149 HPROF_ASSERT(object!=NULL); |
|
150 |
|
151 /* Prevent recursion into any BCI function for this thread (pstatus). */ |
|
152 if ( tls_get_tracker_status(env, thread, JNI_TRUE, |
|
153 &pstatus, NULL, &thread_serial_num, &trace_index) == 0 ) { |
|
154 (*pstatus) = 1; |
|
155 any_allocation(env, thread_serial_num, trace_index, object); |
|
156 (*pstatus) = 0; |
|
157 } |
|
158 } |
|
159 |
|
160 /* Handle any newarray opcode allocation. */ |
|
161 void |
|
162 event_newarray(JNIEnv *env, jthread thread, jobject object) |
|
163 { |
|
164 /* Called via BCI Tracker class */ |
|
165 |
|
166 /* Be very careful what is called here, watch out for recursion. */ |
|
167 |
|
168 jint *pstatus; |
|
169 TraceIndex trace_index; |
|
170 SerialNumber thread_serial_num; |
|
171 |
|
172 HPROF_ASSERT(env!=NULL); |
|
173 HPROF_ASSERT(thread!=NULL); |
|
174 HPROF_ASSERT(object!=NULL); |
|
175 |
|
176 /* Prevent recursion into any BCI function for this thread (pstatus). */ |
|
177 if ( tls_get_tracker_status(env, thread, JNI_FALSE, |
|
178 &pstatus, NULL, &thread_serial_num, &trace_index) == 0 ) { |
|
179 (*pstatus) = 1; |
|
180 any_allocation(env, thread_serial_num, trace_index, object); |
|
181 (*pstatus) = 0; |
|
182 } |
|
183 } |
|
184 |
|
185 /* Handle tracking of a method call. */ |
|
186 void |
|
187 event_call(JNIEnv *env, jthread thread, ClassIndex cnum, MethodIndex mnum) |
|
188 { |
|
189 /* Called via BCI Tracker class */ |
|
190 |
|
191 /* Be very careful what is called here, watch out for recursion. */ |
|
192 |
|
193 TlsIndex tls_index; |
|
194 jint *pstatus; |
|
195 |
|
196 HPROF_ASSERT(env!=NULL); |
|
197 HPROF_ASSERT(thread!=NULL); |
|
198 if (cnum == 0 || cnum == gdata->tracker_cnum) { |
|
199 jclass newExcCls = (*env)->FindClass(env, "java/lang/IllegalArgumentException"); |
|
200 (*env)->ThrowNew(env, newExcCls, "Illegal cnum."); |
|
201 |
|
202 return; |
|
203 } |
|
204 |
|
205 /* Prevent recursion into any BCI function for this thread (pstatus). */ |
|
206 if ( tls_get_tracker_status(env, thread, JNI_FALSE, |
|
207 &pstatus, &tls_index, NULL, NULL) == 0 ) { |
|
208 jmethodID method; |
|
209 |
|
210 (*pstatus) = 1; |
|
211 method = class_get_methodID(env, cnum, mnum); |
|
212 if (method != NULL) { |
|
213 tls_push_method(tls_index, method); |
|
214 } |
|
215 |
|
216 (*pstatus) = 0; |
|
217 } |
|
218 } |
|
219 |
|
220 /* Handle tracking of an exception catch */ |
|
221 void |
|
222 event_exception_catch(JNIEnv *env, jthread thread, jmethodID method, |
|
223 jlocation location, jobject exception) |
|
224 { |
|
225 /* Called via JVMTI_EVENT_EXCEPTION_CATCH callback */ |
|
226 |
|
227 /* Be very careful what is called here, watch out for recursion. */ |
|
228 |
|
229 TlsIndex tls_index; |
|
230 jint *pstatus; |
|
231 |
|
232 HPROF_ASSERT(env!=NULL); |
|
233 HPROF_ASSERT(thread!=NULL); |
|
234 HPROF_ASSERT(method!=NULL); |
|
235 |
|
236 /* Prevent recursion into any BCI function for this thread (pstatus). */ |
|
237 if ( tls_get_tracker_status(env, thread, JNI_FALSE, |
|
238 &pstatus, &tls_index, NULL, NULL) == 0 ) { |
|
239 (*pstatus) = 1; |
|
240 tls_pop_exception_catch(tls_index, thread, method); |
|
241 (*pstatus) = 0; |
|
242 } |
|
243 } |
|
244 |
|
245 /* Handle tracking of a method return pop one (maybe more) methods. */ |
|
246 void |
|
247 event_return(JNIEnv *env, jthread thread, ClassIndex cnum, MethodIndex mnum) |
|
248 { |
|
249 /* Called via BCI Tracker class */ |
|
250 |
|
251 /* Be very careful what is called here, watch out for recursion. */ |
|
252 |
|
253 TlsIndex tls_index; |
|
254 jint *pstatus; |
|
255 |
|
256 HPROF_ASSERT(env!=NULL); |
|
257 HPROF_ASSERT(thread!=NULL); |
|
258 |
|
259 if (cnum == 0 || cnum == gdata->tracker_cnum) { |
|
260 jclass newExcCls = (*env)->FindClass(env, "java/lang/IllegalArgumentException"); |
|
261 (*env)->ThrowNew(env, newExcCls, "Illegal cnum."); |
|
262 |
|
263 return; |
|
264 } |
|
265 |
|
266 /* Prevent recursion into any BCI function for this thread (pstatus). */ |
|
267 if ( tls_get_tracker_status(env, thread, JNI_FALSE, |
|
268 &pstatus, &tls_index, NULL, NULL) == 0 ) { |
|
269 jmethodID method; |
|
270 |
|
271 (*pstatus) = 1; |
|
272 method = class_get_methodID(env, cnum, mnum); |
|
273 if (method != NULL) { |
|
274 tls_pop_method(tls_index, thread, method); |
|
275 } |
|
276 |
|
277 (*pstatus) = 0; |
|
278 } |
|
279 } |
|
280 |
|
281 /* Handle a class prepare (should have been already loaded) */ |
|
282 void |
|
283 event_class_prepare(JNIEnv *env, jthread thread, jclass klass, jobject loader) |
|
284 { |
|
285 /* Called via JVMTI_EVENT_CLASS_PREPARE event */ |
|
286 |
|
287 ClassIndex cnum; |
|
288 |
|
289 HPROF_ASSERT(env!=NULL); |
|
290 HPROF_ASSERT(thread!=NULL); |
|
291 HPROF_ASSERT(klass!=NULL); |
|
292 |
|
293 /* Find the ClassIndex for this class */ |
|
294 cnum = find_cnum(env, klass, loader); |
|
295 class_add_status(cnum, CLASS_PREPARED); |
|
296 |
|
297 } |
|
298 |
|
299 /* Handle a class load (could have been already loaded) */ |
|
300 void |
|
301 event_class_load(JNIEnv *env, jthread thread, jclass klass, jobject loader) |
|
302 { |
|
303 /* Called via JVMTI_EVENT_CLASS_LOAD event or reset_class_load_status() */ |
|
304 |
|
305 ClassIndex cnum; |
|
306 |
|
307 HPROF_ASSERT(env!=NULL); |
|
308 HPROF_ASSERT(klass!=NULL); |
|
309 |
|
310 /* Find the ClassIndex for this class */ |
|
311 cnum = find_cnum(env, klass, loader); |
|
312 |
|
313 /* Always mark it as being in the load list */ |
|
314 class_add_status(cnum, CLASS_IN_LOAD_LIST); |
|
315 |
|
316 /* If we are seeing this as a new loaded class, extra work */ |
|
317 if ( ! ( class_get_status(cnum) & CLASS_LOADED ) ) { |
|
318 TraceIndex trace_index; |
|
319 SiteIndex site_index; |
|
320 ClassIndex super; |
|
321 SerialNumber class_serial_num; |
|
322 SerialNumber trace_serial_num; |
|
323 SerialNumber thread_serial_num; |
|
324 ObjectIndex class_object_index; |
|
325 char *signature; |
|
326 |
|
327 /* Get the TlsIndex and a TraceIndex for this location */ |
|
328 if ( thread == NULL ) { |
|
329 /* This should be very rare, but if this class load was simulated |
|
330 * from hprof_init.c due to a reset of the class load status, |
|
331 * and it originated from a pre-VM_INIT event, the jthread |
|
332 * would be NULL, or it was a jclass created that didn't get |
|
333 * reported to us, like an array class or a primitive class? |
|
334 */ |
|
335 trace_index = gdata->system_trace_index; |
|
336 thread_serial_num = gdata->unknown_thread_serial_num; |
|
337 } else { |
|
338 TlsIndex tls_index; |
|
339 |
|
340 tls_index = tls_find_or_create(env, thread); |
|
341 trace_index = get_current(tls_index, env, JNI_FALSE); |
|
342 thread_serial_num = tls_get_thread_serial_number(tls_index); |
|
343 } |
|
344 |
|
345 /* Get the SiteIndex for this location and a java.lang.Class object */ |
|
346 /* Note that the target cnum, not the cnum for java.lang.Class. */ |
|
347 site_index = site_find_or_create(cnum, trace_index); |
|
348 |
|
349 /* Tag this java.lang.Class object */ |
|
350 tag_class(env, klass, cnum, thread_serial_num, site_index); |
|
351 |
|
352 class_add_status(cnum, CLASS_LOADED); |
|
353 |
|
354 class_serial_num = class_get_serial_number(cnum); |
|
355 class_object_index = class_get_object_index(cnum); |
|
356 trace_serial_num = trace_get_serial_number(trace_index); |
|
357 signature = string_get(class_get_signature(cnum)); |
|
358 |
|
359 rawMonitorEnter(gdata->data_access_lock); { |
|
360 io_write_class_load(class_serial_num, class_object_index, |
|
361 trace_serial_num, signature); |
|
362 } rawMonitorExit(gdata->data_access_lock); |
|
363 |
|
364 super = get_super(env, klass); |
|
365 class_set_super(cnum, super); |
|
366 } |
|
367 |
|
368 } |
|
369 |
|
370 /* Handle a thread start event */ |
|
371 void |
|
372 event_thread_start(JNIEnv *env, jthread thread) |
|
373 { |
|
374 /* Called via JVMTI_EVENT_THREAD_START event */ |
|
375 |
|
376 TlsIndex tls_index; |
|
377 ObjectIndex object_index; |
|
378 TraceIndex trace_index; |
|
379 jlong tag; |
|
380 SerialNumber thread_serial_num; |
|
381 |
|
382 HPROF_ASSERT(env!=NULL); |
|
383 HPROF_ASSERT(thread!=NULL); |
|
384 |
|
385 tls_index = tls_find_or_create(env, thread); |
|
386 thread_serial_num = tls_get_thread_serial_number(tls_index); |
|
387 trace_index = get_current(tls_index, env, JNI_FALSE); |
|
388 |
|
389 tag = getTag(thread); |
|
390 if ( tag == (jlong)0 ) { |
|
391 SiteIndex site_index; |
|
392 jint size; |
|
393 |
|
394 size = (jint)getObjectSize(thread); |
|
395 site_index = site_find_or_create(gdata->thread_cnum, trace_index); |
|
396 /* We create a new object with this thread's serial number */ |
|
397 object_index = object_new(site_index, size, OBJECT_NORMAL, |
|
398 thread_serial_num); |
|
399 } else { |
|
400 object_index = tag_extract(tag); |
|
401 /* Normally the Thread object is created and tagged before we get |
|
402 * here, but the thread_serial_number on this object isn't what |
|
403 * we want. So we update it to the serial number of this thread. |
|
404 */ |
|
405 object_set_thread_serial_number(object_index, thread_serial_num); |
|
406 } |
|
407 tls_set_thread_object_index(tls_index, object_index); |
|
408 |
|
409 WITH_LOCAL_REFS(env, 1) { |
|
410 jvmtiThreadInfo threadInfo; |
|
411 jvmtiThreadGroupInfo threadGroupInfo; |
|
412 jvmtiThreadGroupInfo parentGroupInfo; |
|
413 |
|
414 getThreadInfo(thread, &threadInfo); |
|
415 getThreadGroupInfo(threadInfo.thread_group, &threadGroupInfo); |
|
416 if ( threadGroupInfo.parent != NULL ) { |
|
417 getThreadGroupInfo(threadGroupInfo.parent, &parentGroupInfo); |
|
418 } else { |
|
419 (void)memset(&parentGroupInfo, 0, sizeof(parentGroupInfo)); |
|
420 } |
|
421 |
|
422 rawMonitorEnter(gdata->data_access_lock); { |
|
423 io_write_thread_start(thread_serial_num, |
|
424 object_index, trace_get_serial_number(trace_index), |
|
425 threadInfo.name, threadGroupInfo.name, parentGroupInfo.name); |
|
426 } rawMonitorExit(gdata->data_access_lock); |
|
427 |
|
428 jvmtiDeallocate(threadInfo.name); |
|
429 jvmtiDeallocate(threadGroupInfo.name); |
|
430 jvmtiDeallocate(parentGroupInfo.name); |
|
431 |
|
432 } END_WITH_LOCAL_REFS; |
|
433 } |
|
434 |
|
435 void |
|
436 event_thread_end(JNIEnv *env, jthread thread) |
|
437 { |
|
438 /* Called via JVMTI_EVENT_THREAD_END event */ |
|
439 TlsIndex tls_index; |
|
440 |
|
441 HPROF_ASSERT(env!=NULL); |
|
442 HPROF_ASSERT(thread!=NULL); |
|
443 |
|
444 tls_index = tls_find_or_create(env, thread); |
|
445 rawMonitorEnter(gdata->data_access_lock); { |
|
446 io_write_thread_end(tls_get_thread_serial_number(tls_index)); |
|
447 } rawMonitorExit(gdata->data_access_lock); |
|
448 tls_thread_ended(env, tls_index); |
|
449 setThreadLocalStorage(thread, (void*)NULL); |
|
450 } |