Mercurial > hg > truffle
annotate src/share/vm/prims/jvmtiEnvBase.cpp @ 1972:f95d63e2154a
6989984: Use standard include model for Hospot
Summary: Replaced MakeDeps and the includeDB files with more standardized solutions.
Reviewed-by: coleenp, kvn, kamg
author | stefank |
---|---|
date | Tue, 23 Nov 2010 13:22:55 -0800 |
parents | ce6848d0666d |
children | 684faacebf20 |
rev | line source |
---|---|
0 | 1 /* |
1972 | 2 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. |
0 | 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
1552
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1144
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1144
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1144
diff
changeset
|
21 * questions. |
0 | 22 * |
23 */ | |
24 | |
1972 | 25 #include "precompiled.hpp" |
26 #include "classfile/systemDictionary.hpp" | |
27 #include "jvmtifiles/jvmtiEnv.hpp" | |
28 #include "oops/objArrayKlass.hpp" | |
29 #include "oops/objArrayOop.hpp" | |
30 #include "prims/jvmtiEnvBase.hpp" | |
31 #include "prims/jvmtiEventController.inline.hpp" | |
32 #include "prims/jvmtiExtensions.hpp" | |
33 #include "prims/jvmtiImpl.hpp" | |
34 #include "prims/jvmtiManageCapabilities.hpp" | |
35 #include "prims/jvmtiTagMap.hpp" | |
36 #include "prims/jvmtiThreadState.inline.hpp" | |
37 #include "runtime/biasedLocking.hpp" | |
38 #include "runtime/deoptimization.hpp" | |
39 #include "runtime/interfaceSupport.hpp" | |
40 #include "runtime/jfieldIDWorkaround.hpp" | |
41 #include "runtime/objectMonitor.hpp" | |
42 #include "runtime/objectMonitor.inline.hpp" | |
43 #include "runtime/signature.hpp" | |
44 #include "runtime/vframe.hpp" | |
45 #include "runtime/vframe_hp.hpp" | |
46 #include "runtime/vmThread.hpp" | |
47 #include "runtime/vm_operations.hpp" | |
0 | 48 |
49 /////////////////////////////////////////////////////////////// | |
50 // | |
51 // JvmtiEnvBase | |
52 // | |
53 | |
54 JvmtiEnvBase* JvmtiEnvBase::_head_environment = NULL; | |
55 | |
56 bool JvmtiEnvBase::_globally_initialized = false; | |
57 volatile bool JvmtiEnvBase::_needs_clean_up = false; | |
58 | |
59 jvmtiPhase JvmtiEnvBase::_phase = JVMTI_PHASE_PRIMORDIAL; | |
60 | |
61 volatile int JvmtiEnvBase::_dying_thread_env_iteration_count = 0; | |
62 | |
63 extern jvmtiInterface_1_ jvmti_Interface; | |
64 extern jvmtiInterface_1_ jvmtiTrace_Interface; | |
65 | |
66 | |
67 // perform initializations that must occur before any JVMTI environments | |
68 // are released but which should only be initialized once (no matter | |
69 // how many environments are created). | |
70 void | |
71 JvmtiEnvBase::globally_initialize() { | |
72 assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check"); | |
73 assert(_globally_initialized == false, "bad call"); | |
74 | |
75 JvmtiManageCapabilities::initialize(); | |
76 | |
77 #ifndef JVMTI_KERNEL | |
78 // register extension functions and events | |
79 JvmtiExtensions::register_extensions(); | |
80 #endif // !JVMTI_KERNEL | |
81 | |
82 #ifdef JVMTI_TRACE | |
83 JvmtiTrace::initialize(); | |
84 #endif | |
85 | |
86 _globally_initialized = true; | |
87 } | |
88 | |
89 | |
90 void | |
91 JvmtiEnvBase::initialize() { | |
92 assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check"); | |
93 | |
94 // Add this environment to the end of the environment list (order is important) | |
95 { | |
96 // This block of code must not contain any safepoints, as list deallocation | |
97 // (which occurs at a safepoint) cannot occur simultaneously with this list | |
98 // addition. Note: No_Safepoint_Verifier cannot, currently, be used before | |
99 // threads exist. | |
100 JvmtiEnvIterator it; | |
101 JvmtiEnvBase *previous_env = NULL; | |
102 for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) { | |
103 previous_env = env; | |
104 } | |
105 if (previous_env == NULL) { | |
106 _head_environment = this; | |
107 } else { | |
108 previous_env->set_next_environment(this); | |
109 } | |
110 } | |
111 | |
112 if (_globally_initialized == false) { | |
113 globally_initialize(); | |
114 } | |
115 } | |
116 | |
117 | |
611
2f716c0acb64
6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents:
609
diff
changeset
|
118 bool |
2f716c0acb64
6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents:
609
diff
changeset
|
119 JvmtiEnvBase::is_valid() { |
2f716c0acb64
6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents:
609
diff
changeset
|
120 jint value = 0; |
2f716c0acb64
6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents:
609
diff
changeset
|
121 |
2f716c0acb64
6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents:
609
diff
changeset
|
122 // This object might not be a JvmtiEnvBase so we can't assume |
2f716c0acb64
6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents:
609
diff
changeset
|
123 // the _magic field is properly aligned. Get the value in a safe |
2f716c0acb64
6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents:
609
diff
changeset
|
124 // way and then check against JVMTI_MAGIC. |
2f716c0acb64
6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents:
609
diff
changeset
|
125 |
2f716c0acb64
6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents:
609
diff
changeset
|
126 switch (sizeof(_magic)) { |
2f716c0acb64
6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents:
609
diff
changeset
|
127 case 2: |
2f716c0acb64
6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents:
609
diff
changeset
|
128 value = Bytes::get_native_u2((address)&_magic); |
2f716c0acb64
6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents:
609
diff
changeset
|
129 break; |
2f716c0acb64
6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents:
609
diff
changeset
|
130 |
2f716c0acb64
6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents:
609
diff
changeset
|
131 case 4: |
2f716c0acb64
6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents:
609
diff
changeset
|
132 value = Bytes::get_native_u4((address)&_magic); |
2f716c0acb64
6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents:
609
diff
changeset
|
133 break; |
2f716c0acb64
6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents:
609
diff
changeset
|
134 |
2f716c0acb64
6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents:
609
diff
changeset
|
135 case 8: |
2f716c0acb64
6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents:
609
diff
changeset
|
136 value = Bytes::get_native_u8((address)&_magic); |
2f716c0acb64
6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents:
609
diff
changeset
|
137 break; |
2f716c0acb64
6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents:
609
diff
changeset
|
138 |
2f716c0acb64
6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents:
609
diff
changeset
|
139 default: |
2f716c0acb64
6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents:
609
diff
changeset
|
140 guarantee(false, "_magic field is an unexpected size"); |
2f716c0acb64
6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents:
609
diff
changeset
|
141 } |
2f716c0acb64
6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents:
609
diff
changeset
|
142 |
2f716c0acb64
6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents:
609
diff
changeset
|
143 return value == JVMTI_MAGIC; |
2f716c0acb64
6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents:
609
diff
changeset
|
144 } |
2f716c0acb64
6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents:
609
diff
changeset
|
145 |
2f716c0acb64
6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents:
609
diff
changeset
|
146 |
1122 | 147 bool |
1121 | 148 JvmtiEnvBase::use_version_1_0_semantics() { |
149 int major, minor, micro; | |
150 | |
151 JvmtiExport::decode_version_values(_version, &major, &minor, µ); | |
152 return major == 1 && minor == 0; // micro version doesn't matter here | |
153 } | |
154 | |
155 | |
156 bool | |
157 JvmtiEnvBase::use_version_1_1_semantics() { | |
158 int major, minor, micro; | |
159 | |
160 JvmtiExport::decode_version_values(_version, &major, &minor, µ); | |
161 return major == 1 && minor == 1; // micro version doesn't matter here | |
162 } | |
163 | |
164 | |
165 JvmtiEnvBase::JvmtiEnvBase(jint version) : _env_event_enable() { | |
166 _version = version; | |
0 | 167 _env_local_storage = NULL; |
168 _tag_map = NULL; | |
169 _native_method_prefix_count = 0; | |
170 _native_method_prefixes = NULL; | |
171 _next = NULL; | |
172 _class_file_load_hook_ever_enabled = false; | |
173 | |
174 // Moot since ClassFileLoadHook not yet enabled. | |
175 // But "true" will give a more predictable ClassFileLoadHook behavior | |
176 // for environment creation during ClassFileLoadHook. | |
177 _is_retransformable = true; | |
178 | |
179 // all callbacks initially NULL | |
180 memset(&_event_callbacks,0,sizeof(jvmtiEventCallbacks)); | |
181 | |
182 // all capabilities initially off | |
183 memset(&_current_capabilities, 0, sizeof(_current_capabilities)); | |
184 | |
185 // all prohibited capabilities initially off | |
186 memset(&_prohibited_capabilities, 0, sizeof(_prohibited_capabilities)); | |
187 | |
188 _magic = JVMTI_MAGIC; | |
189 | |
190 JvmtiEventController::env_initialize((JvmtiEnv*)this); | |
191 | |
192 #ifdef JVMTI_TRACE | |
371 | 193 _jvmti_external.functions = TraceJVMTI != NULL ? &jvmtiTrace_Interface : &jvmti_Interface; |
0 | 194 #else |
195 _jvmti_external.functions = &jvmti_Interface; | |
196 #endif | |
197 } | |
198 | |
199 | |
200 void | |
201 JvmtiEnvBase::dispose() { | |
202 | |
203 #ifdef JVMTI_TRACE | |
204 JvmtiTrace::shutdown(); | |
205 #endif | |
206 | |
207 // Dispose of event info and let the event controller call us back | |
208 // in a locked state (env_dispose, below) | |
209 JvmtiEventController::env_dispose(this); | |
210 } | |
211 | |
212 void | |
213 JvmtiEnvBase::env_dispose() { | |
214 assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check"); | |
215 | |
216 // We have been entered with all events disabled on this environment. | |
217 // A race to re-enable events (by setting callbacks) is prevented by | |
218 // checking for a valid environment when setting callbacks (while | |
219 // holding the JvmtiThreadState_lock). | |
220 | |
221 // Mark as invalid. | |
222 _magic = DISPOSED_MAGIC; | |
223 | |
224 // Relinquish all capabilities. | |
225 jvmtiCapabilities *caps = get_capabilities(); | |
226 JvmtiManageCapabilities::relinquish_capabilities(caps, caps, caps); | |
227 | |
228 // Same situation as with events (see above) | |
229 set_native_method_prefixes(0, NULL); | |
230 | |
231 #ifndef JVMTI_KERNEL | |
232 JvmtiTagMap* tag_map_to_deallocate = _tag_map; | |
233 set_tag_map(NULL); | |
234 // A tag map can be big, deallocate it now | |
235 if (tag_map_to_deallocate != NULL) { | |
236 delete tag_map_to_deallocate; | |
237 } | |
238 #endif // !JVMTI_KERNEL | |
239 | |
240 _needs_clean_up = true; | |
241 } | |
242 | |
243 | |
244 JvmtiEnvBase::~JvmtiEnvBase() { | |
245 assert(SafepointSynchronize::is_at_safepoint(), "sanity check"); | |
246 | |
247 // There is a small window of time during which the tag map of a | |
248 // disposed environment could have been reallocated. | |
249 // Make sure it is gone. | |
250 #ifndef JVMTI_KERNEL | |
251 JvmtiTagMap* tag_map_to_deallocate = _tag_map; | |
252 set_tag_map(NULL); | |
253 // A tag map can be big, deallocate it now | |
254 if (tag_map_to_deallocate != NULL) { | |
255 delete tag_map_to_deallocate; | |
256 } | |
257 #endif // !JVMTI_KERNEL | |
258 | |
259 _magic = BAD_MAGIC; | |
260 } | |
261 | |
262 | |
263 void | |
264 JvmtiEnvBase::periodic_clean_up() { | |
265 assert(SafepointSynchronize::is_at_safepoint(), "sanity check"); | |
266 | |
267 // JvmtiEnvBase reference is saved in JvmtiEnvThreadState. So | |
268 // clean up JvmtiThreadState before deleting JvmtiEnv pointer. | |
269 JvmtiThreadState::periodic_clean_up(); | |
270 | |
271 // Unlink all invalid environments from the list of environments | |
272 // and deallocate them | |
273 JvmtiEnvIterator it; | |
274 JvmtiEnvBase* previous_env = NULL; | |
275 JvmtiEnvBase* env = it.first(); | |
276 while (env != NULL) { | |
277 if (env->is_valid()) { | |
278 previous_env = env; | |
279 env = it.next(env); | |
280 } else { | |
281 // This one isn't valid, remove it from the list and deallocate it | |
282 JvmtiEnvBase* defunct_env = env; | |
283 env = it.next(env); | |
284 if (previous_env == NULL) { | |
285 _head_environment = env; | |
286 } else { | |
287 previous_env->set_next_environment(env); | |
288 } | |
289 delete defunct_env; | |
290 } | |
291 } | |
292 | |
293 } | |
294 | |
295 | |
296 void | |
297 JvmtiEnvBase::check_for_periodic_clean_up() { | |
298 assert(SafepointSynchronize::is_at_safepoint(), "sanity check"); | |
299 | |
300 class ThreadInsideIterationClosure: public ThreadClosure { | |
301 private: | |
302 bool _inside; | |
303 public: | |
304 ThreadInsideIterationClosure() : _inside(false) {}; | |
305 | |
306 void do_thread(Thread* thread) { | |
307 _inside |= thread->is_inside_jvmti_env_iteration(); | |
308 } | |
309 | |
310 bool is_inside_jvmti_env_iteration() { | |
311 return _inside; | |
312 } | |
313 }; | |
314 | |
315 if (_needs_clean_up) { | |
316 // Check if we are currently iterating environment, | |
317 // deallocation should not occur if we are | |
318 ThreadInsideIterationClosure tiic; | |
319 Threads::threads_do(&tiic); | |
320 if (!tiic.is_inside_jvmti_env_iteration() && | |
321 !is_inside_dying_thread_env_iteration()) { | |
322 _needs_clean_up = false; | |
323 JvmtiEnvBase::periodic_clean_up(); | |
324 } | |
325 } | |
326 } | |
327 | |
328 | |
329 void | |
330 JvmtiEnvBase::record_first_time_class_file_load_hook_enabled() { | |
331 assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), | |
332 "sanity check"); | |
333 | |
334 if (!_class_file_load_hook_ever_enabled) { | |
335 _class_file_load_hook_ever_enabled = true; | |
336 | |
337 if (get_capabilities()->can_retransform_classes) { | |
338 _is_retransformable = true; | |
339 } else { | |
340 _is_retransformable = false; | |
341 | |
342 // cannot add retransform capability after ClassFileLoadHook has been enabled | |
343 get_prohibited_capabilities()->can_retransform_classes = 1; | |
344 } | |
345 } | |
346 } | |
347 | |
348 | |
349 void | |
350 JvmtiEnvBase::record_class_file_load_hook_enabled() { | |
351 if (!_class_file_load_hook_ever_enabled) { | |
352 if (Threads::number_of_threads() == 0) { | |
353 record_first_time_class_file_load_hook_enabled(); | |
354 } else { | |
355 MutexLocker mu(JvmtiThreadState_lock); | |
356 record_first_time_class_file_load_hook_enabled(); | |
357 } | |
358 } | |
359 } | |
360 | |
361 | |
362 jvmtiError | |
363 JvmtiEnvBase::set_native_method_prefixes(jint prefix_count, char** prefixes) { | |
364 assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), | |
365 "sanity check"); | |
366 | |
367 int old_prefix_count = get_native_method_prefix_count(); | |
368 char **old_prefixes = get_native_method_prefixes(); | |
369 | |
370 // allocate and install the new prefixex | |
371 if (prefix_count == 0 || !is_valid()) { | |
372 _native_method_prefix_count = 0; | |
373 _native_method_prefixes = NULL; | |
374 } else { | |
375 // there are prefixes, allocate an array to hold them, and fill it | |
376 char** new_prefixes = (char**)os::malloc((prefix_count) * sizeof(char*)); | |
377 if (new_prefixes == NULL) { | |
378 return JVMTI_ERROR_OUT_OF_MEMORY; | |
379 } | |
380 for (int i = 0; i < prefix_count; i++) { | |
381 char* prefix = prefixes[i]; | |
382 if (prefix == NULL) { | |
383 for (int j = 0; j < (i-1); j++) { | |
384 os::free(new_prefixes[j]); | |
385 } | |
386 os::free(new_prefixes); | |
387 return JVMTI_ERROR_NULL_POINTER; | |
388 } | |
389 prefix = os::strdup(prefixes[i]); | |
390 if (prefix == NULL) { | |
391 for (int j = 0; j < (i-1); j++) { | |
392 os::free(new_prefixes[j]); | |
393 } | |
394 os::free(new_prefixes); | |
395 return JVMTI_ERROR_OUT_OF_MEMORY; | |
396 } | |
397 new_prefixes[i] = prefix; | |
398 } | |
399 _native_method_prefix_count = prefix_count; | |
400 _native_method_prefixes = new_prefixes; | |
401 } | |
402 | |
403 // now that we know the new prefixes have been successfully installed we can | |
404 // safely remove the old ones | |
405 if (old_prefix_count != 0) { | |
406 for (int i = 0; i < old_prefix_count; i++) { | |
407 os::free(old_prefixes[i]); | |
408 } | |
409 os::free(old_prefixes); | |
410 } | |
411 | |
412 return JVMTI_ERROR_NONE; | |
413 } | |
414 | |
415 | |
416 // Collect all the prefixes which have been set in any JVM TI environments | |
417 // by the SetNativeMethodPrefix(es) functions. Be sure to maintain the | |
418 // order of environments and the order of prefixes within each environment. | |
419 // Return in a resource allocated array. | |
420 char** | |
421 JvmtiEnvBase::get_all_native_method_prefixes(int* count_ptr) { | |
422 assert(Threads::number_of_threads() == 0 || | |
423 SafepointSynchronize::is_at_safepoint() || | |
424 JvmtiThreadState_lock->is_locked(), | |
425 "sanity check"); | |
426 | |
427 int total_count = 0; | |
428 GrowableArray<char*>* prefix_array =new GrowableArray<char*>(5); | |
429 | |
430 JvmtiEnvIterator it; | |
431 for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) { | |
432 int prefix_count = env->get_native_method_prefix_count(); | |
433 char** prefixes = env->get_native_method_prefixes(); | |
434 for (int j = 0; j < prefix_count; j++) { | |
435 // retrieve a prefix and so that it is safe against asynchronous changes | |
436 // copy it into the resource area | |
437 char* prefix = prefixes[j]; | |
438 char* prefix_copy = NEW_RESOURCE_ARRAY(char, strlen(prefix)+1); | |
439 strcpy(prefix_copy, prefix); | |
440 prefix_array->at_put_grow(total_count++, prefix_copy); | |
441 } | |
442 } | |
443 | |
444 char** all_prefixes = NEW_RESOURCE_ARRAY(char*, total_count); | |
445 char** p = all_prefixes; | |
446 for (int i = 0; i < total_count; ++i) { | |
447 *p++ = prefix_array->at(i); | |
448 } | |
449 *count_ptr = total_count; | |
450 return all_prefixes; | |
451 } | |
452 | |
453 void | |
454 JvmtiEnvBase::set_event_callbacks(const jvmtiEventCallbacks* callbacks, | |
455 jint size_of_callbacks) { | |
456 assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check"); | |
457 | |
458 size_t byte_cnt = sizeof(jvmtiEventCallbacks); | |
459 | |
460 // clear in either case to be sure we got any gap between sizes | |
461 memset(&_event_callbacks, 0, byte_cnt); | |
462 | |
463 // Now that JvmtiThreadState_lock is held, prevent a possible race condition where events | |
464 // are re-enabled by a call to set event callbacks where the DisposeEnvironment | |
465 // occurs after the boiler-plate environment check and before the lock is acquired. | |
466 if (callbacks != NULL && is_valid()) { | |
467 if (size_of_callbacks < (jint)byte_cnt) { | |
468 byte_cnt = size_of_callbacks; | |
469 } | |
470 memcpy(&_event_callbacks, callbacks, byte_cnt); | |
471 } | |
472 } | |
473 | |
474 // Called from JVMTI entry points which perform stack walking. If the | |
475 // associated JavaThread is the current thread, then wait_for_suspend | |
476 // is not used. Otherwise, it determines if we should wait for the | |
477 // "other" thread to complete external suspension. (NOTE: in future | |
478 // releases the suspension mechanism should be reimplemented so this | |
479 // is not necessary.) | |
480 // | |
481 bool | |
482 JvmtiEnvBase::is_thread_fully_suspended(JavaThread* thr, bool wait_for_suspend, uint32_t *bits) { | |
483 // "other" threads require special handling | |
484 if (thr != JavaThread::current()) { | |
485 if (wait_for_suspend) { | |
486 // We are allowed to wait for the external suspend to complete | |
487 // so give the other thread a chance to get suspended. | |
488 if (!thr->wait_for_ext_suspend_completion(SuspendRetryCount, | |
489 SuspendRetryDelay, bits)) { | |
490 // didn't make it so let the caller know | |
491 return false; | |
492 } | |
493 } | |
494 // We aren't allowed to wait for the external suspend to complete | |
495 // so if the other thread isn't externally suspended we need to | |
496 // let the caller know. | |
497 else if (!thr->is_ext_suspend_completed_with_lock(bits)) { | |
498 return false; | |
499 } | |
500 } | |
501 | |
502 return true; | |
503 } | |
504 | |
505 | |
506 // In the fullness of time, all users of the method should instead | |
507 // directly use allocate, besides being cleaner and faster, this will | |
508 // mean much better out of memory handling | |
509 unsigned char * | |
510 JvmtiEnvBase::jvmtiMalloc(jlong size) { | |
511 unsigned char* mem; | |
512 jvmtiError result = allocate(size, &mem); | |
513 assert(result == JVMTI_ERROR_NONE, "Allocate failed"); | |
514 return mem; | |
515 } | |
516 | |
517 | |
518 // | |
519 // Threads | |
520 // | |
521 | |
522 jobject * | |
523 JvmtiEnvBase::new_jobjectArray(int length, Handle *handles) { | |
524 if (length == 0) { | |
525 return NULL; | |
526 } | |
527 | |
528 jobject *objArray = (jobject *) jvmtiMalloc(sizeof(jobject) * length); | |
529 NULL_CHECK(objArray, NULL); | |
530 | |
531 for (int i=0; i<length; i++) { | |
532 objArray[i] = jni_reference(handles[i]); | |
533 } | |
534 return objArray; | |
535 } | |
536 | |
537 jthread * | |
538 JvmtiEnvBase::new_jthreadArray(int length, Handle *handles) { | |
539 return (jthread *) new_jobjectArray(length,handles); | |
540 } | |
541 | |
542 jthreadGroup * | |
543 JvmtiEnvBase::new_jthreadGroupArray(int length, Handle *handles) { | |
544 return (jthreadGroup *) new_jobjectArray(length,handles); | |
545 } | |
546 | |
547 | |
548 JavaThread * | |
549 JvmtiEnvBase::get_JavaThread(jthread jni_thread) { | |
550 oop t = JNIHandles::resolve_external_guard(jni_thread); | |
1142 | 551 if (t == NULL || !t->is_a(SystemDictionary::Thread_klass())) { |
0 | 552 return NULL; |
553 } | |
554 // The following returns NULL if the thread has not yet run or is in | |
555 // process of exiting | |
556 return java_lang_Thread::thread(t); | |
557 } | |
558 | |
559 | |
560 // update the access_flags for the field in the klass | |
561 void | |
562 JvmtiEnvBase::update_klass_field_access_flag(fieldDescriptor *fd) { | |
563 instanceKlass* ik = instanceKlass::cast(fd->field_holder()); | |
564 typeArrayOop fields = ik->fields(); | |
565 fields->ushort_at_put(fd->index(), (jushort)fd->access_flags().as_short()); | |
566 } | |
567 | |
568 | |
569 // return the vframe on the specified thread and depth, NULL if no such frame | |
570 vframe* | |
571 JvmtiEnvBase::vframeFor(JavaThread* java_thread, jint depth) { | |
572 if (!java_thread->has_last_Java_frame()) { | |
573 return NULL; | |
574 } | |
575 RegisterMap reg_map(java_thread); | |
576 vframe *vf = java_thread->last_java_vframe(®_map); | |
577 int d = 0; | |
578 while ((vf != NULL) && (d < depth)) { | |
579 vf = vf->java_sender(); | |
580 d++; | |
581 } | |
582 return vf; | |
583 } | |
584 | |
585 | |
586 // | |
587 // utilities: JNI objects | |
588 // | |
589 | |
590 | |
591 jclass | |
592 JvmtiEnvBase::get_jni_class_non_null(klassOop k) { | |
593 assert(k != NULL, "k != NULL"); | |
594 return (jclass)jni_reference(Klass::cast(k)->java_mirror()); | |
595 } | |
596 | |
597 #ifndef JVMTI_KERNEL | |
598 | |
599 // | |
600 // Field Information | |
601 // | |
602 | |
603 bool | |
604 JvmtiEnvBase::get_field_descriptor(klassOop k, jfieldID field, fieldDescriptor* fd) { | |
605 if (!jfieldIDWorkaround::is_valid_jfieldID(k, field)) { | |
606 return false; | |
607 } | |
608 bool found = false; | |
609 if (jfieldIDWorkaround::is_static_jfieldID(field)) { | |
610 JNIid* id = jfieldIDWorkaround::from_static_jfieldID(field); | |
611 int offset = id->offset(); | |
612 klassOop holder = id->holder(); | |
613 found = instanceKlass::cast(holder)->find_local_field_from_offset(offset, true, fd); | |
614 } else { | |
615 // Non-static field. The fieldID is really the offset of the field within the object. | |
616 int offset = jfieldIDWorkaround::from_instance_jfieldID(k, field); | |
617 found = instanceKlass::cast(k)->find_field_from_offset(offset, false, fd); | |
618 } | |
619 return found; | |
620 } | |
621 | |
622 // | |
623 // Object Monitor Information | |
624 // | |
625 | |
626 // | |
627 // Count the number of objects for a lightweight monitor. The hobj | |
628 // parameter is object that owns the monitor so this routine will | |
629 // count the number of times the same object was locked by frames | |
630 // in java_thread. | |
631 // | |
632 jint | |
633 JvmtiEnvBase::count_locked_objects(JavaThread *java_thread, Handle hobj) { | |
634 jint ret = 0; | |
635 if (!java_thread->has_last_Java_frame()) { | |
636 return ret; // no Java frames so no monitors | |
637 } | |
638 | |
639 ResourceMark rm; | |
640 HandleMark hm; | |
641 RegisterMap reg_map(java_thread); | |
642 | |
643 for(javaVFrame *jvf=java_thread->last_java_vframe(®_map); jvf != NULL; | |
644 jvf = jvf->java_sender()) { | |
645 GrowableArray<MonitorInfo*>* mons = jvf->monitors(); | |
646 if (!mons->is_empty()) { | |
647 for (int i = 0; i < mons->length(); i++) { | |
648 MonitorInfo *mi = mons->at(i); | |
818
b109e761e927
6837472: com/sun/jdi/MonitorFrameInfo.java fails with AggressiveOpts in 6u14
kvn
parents:
611
diff
changeset
|
649 if (mi->owner_is_scalar_replaced()) continue; |
0 | 650 |
651 // see if owner of the monitor is our object | |
652 if (mi->owner() != NULL && mi->owner() == hobj()) { | |
653 ret++; | |
654 } | |
655 } | |
656 } | |
657 } | |
658 return ret; | |
659 } | |
660 | |
661 | |
662 | |
663 jvmtiError | |
664 JvmtiEnvBase::get_current_contended_monitor(JavaThread *calling_thread, JavaThread *java_thread, jobject *monitor_ptr) { | |
665 #ifdef ASSERT | |
666 uint32_t debug_bits = 0; | |
667 #endif | |
668 assert((SafepointSynchronize::is_at_safepoint() || | |
669 is_thread_fully_suspended(java_thread, false, &debug_bits)), | |
670 "at safepoint or target thread is suspended"); | |
671 oop obj = NULL; | |
672 ObjectMonitor *mon = java_thread->current_waiting_monitor(); | |
673 if (mon == NULL) { | |
674 // thread is not doing an Object.wait() call | |
675 mon = java_thread->current_pending_monitor(); | |
676 if (mon != NULL) { | |
677 // The thread is trying to enter() or raw_enter() an ObjectMonitor. | |
678 obj = (oop)mon->object(); | |
679 // If obj == NULL, then ObjectMonitor is raw which doesn't count | |
680 // as contended for this API | |
681 } | |
682 // implied else: no contended ObjectMonitor | |
683 } else { | |
684 // thread is doing an Object.wait() call | |
685 obj = (oop)mon->object(); | |
686 assert(obj != NULL, "Object.wait() should have an object"); | |
687 } | |
688 | |
689 if (obj == NULL) { | |
690 *monitor_ptr = NULL; | |
691 } else { | |
692 HandleMark hm; | |
693 Handle hobj(obj); | |
694 *monitor_ptr = jni_reference(calling_thread, hobj); | |
695 } | |
696 return JVMTI_ERROR_NONE; | |
697 } | |
698 | |
699 | |
700 jvmtiError | |
701 JvmtiEnvBase::get_owned_monitors(JavaThread *calling_thread, JavaThread* java_thread, | |
702 GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list) { | |
703 jvmtiError err = JVMTI_ERROR_NONE; | |
704 #ifdef ASSERT | |
705 uint32_t debug_bits = 0; | |
706 #endif | |
707 assert((SafepointSynchronize::is_at_safepoint() || | |
708 is_thread_fully_suspended(java_thread, false, &debug_bits)), | |
709 "at safepoint or target thread is suspended"); | |
710 | |
711 if (java_thread->has_last_Java_frame()) { | |
712 ResourceMark rm; | |
713 HandleMark hm; | |
714 RegisterMap reg_map(java_thread); | |
715 | |
716 int depth = 0; | |
717 for (javaVFrame *jvf = java_thread->last_java_vframe(®_map); jvf != NULL; | |
718 jvf = jvf->java_sender()) { | |
719 if (depth++ < MaxJavaStackTraceDepth) { // check for stack too deep | |
720 // add locked objects for this frame into list | |
721 err = get_locked_objects_in_frame(calling_thread, java_thread, jvf, owned_monitors_list, depth-1); | |
722 if (err != JVMTI_ERROR_NONE) { | |
723 return err; | |
724 } | |
725 } | |
726 } | |
727 } | |
728 | |
729 // Get off stack monitors. (e.g. acquired via jni MonitorEnter). | |
730 JvmtiMonitorClosure jmc(java_thread, calling_thread, owned_monitors_list, this); | |
731 ObjectSynchronizer::monitors_iterate(&jmc); | |
732 err = jmc.error(); | |
733 | |
734 return err; | |
735 } | |
736 | |
737 // Save JNI local handles for any objects that this frame owns. | |
738 jvmtiError | |
739 JvmtiEnvBase::get_locked_objects_in_frame(JavaThread* calling_thread, JavaThread* java_thread, | |
740 javaVFrame *jvf, GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitors_list, int stack_depth) { | |
741 jvmtiError err = JVMTI_ERROR_NONE; | |
742 ResourceMark rm; | |
743 | |
744 GrowableArray<MonitorInfo*>* mons = jvf->monitors(); | |
745 if (mons->is_empty()) { | |
746 return err; // this javaVFrame holds no monitors | |
747 } | |
748 | |
749 HandleMark hm; | |
750 oop wait_obj = NULL; | |
751 { | |
752 // save object of current wait() call (if any) for later comparison | |
753 ObjectMonitor *mon = java_thread->current_waiting_monitor(); | |
754 if (mon != NULL) { | |
755 wait_obj = (oop)mon->object(); | |
756 } | |
757 } | |
758 oop pending_obj = NULL; | |
759 { | |
760 // save object of current enter() call (if any) for later comparison | |
761 ObjectMonitor *mon = java_thread->current_pending_monitor(); | |
762 if (mon != NULL) { | |
763 pending_obj = (oop)mon->object(); | |
764 } | |
765 } | |
766 | |
767 for (int i = 0; i < mons->length(); i++) { | |
768 MonitorInfo *mi = mons->at(i); | |
769 | |
818
b109e761e927
6837472: com/sun/jdi/MonitorFrameInfo.java fails with AggressiveOpts in 6u14
kvn
parents:
611
diff
changeset
|
770 if (mi->owner_is_scalar_replaced()) continue; |
b109e761e927
6837472: com/sun/jdi/MonitorFrameInfo.java fails with AggressiveOpts in 6u14
kvn
parents:
611
diff
changeset
|
771 |
0 | 772 oop obj = mi->owner(); |
773 if (obj == NULL) { | |
774 // this monitor doesn't have an owning object so skip it | |
775 continue; | |
776 } | |
777 | |
778 if (wait_obj == obj) { | |
779 // the thread is waiting on this monitor so it isn't really owned | |
780 continue; | |
781 } | |
782 | |
783 if (pending_obj == obj) { | |
784 // the thread is pending on this monitor so it isn't really owned | |
785 continue; | |
786 } | |
787 | |
788 if (owned_monitors_list->length() > 0) { | |
789 // Our list has at least one object on it so we have to check | |
790 // for recursive object locking | |
791 bool found = false; | |
792 for (int j = 0; j < owned_monitors_list->length(); j++) { | |
793 jobject jobj = ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(j))->monitor; | |
794 oop check = JNIHandles::resolve(jobj); | |
795 if (check == obj) { | |
796 found = true; // we found the object | |
797 break; | |
798 } | |
799 } | |
800 | |
801 if (found) { | |
802 // already have this object so don't include it | |
803 continue; | |
804 } | |
805 } | |
806 | |
807 // add the owning object to our list | |
808 jvmtiMonitorStackDepthInfo *jmsdi; | |
809 err = allocate(sizeof(jvmtiMonitorStackDepthInfo), (unsigned char **)&jmsdi); | |
810 if (err != JVMTI_ERROR_NONE) { | |
811 return err; | |
812 } | |
813 Handle hobj(obj); | |
814 jmsdi->monitor = jni_reference(calling_thread, hobj); | |
815 jmsdi->stack_depth = stack_depth; | |
816 owned_monitors_list->append(jmsdi); | |
817 } | |
818 | |
819 return err; | |
820 } | |
821 | |
822 jvmtiError | |
823 JvmtiEnvBase::get_stack_trace(JavaThread *java_thread, | |
824 jint start_depth, jint max_count, | |
825 jvmtiFrameInfo* frame_buffer, jint* count_ptr) { | |
826 #ifdef ASSERT | |
827 uint32_t debug_bits = 0; | |
828 #endif | |
829 assert((SafepointSynchronize::is_at_safepoint() || | |
830 is_thread_fully_suspended(java_thread, false, &debug_bits)), | |
831 "at safepoint or target thread is suspended"); | |
832 int count = 0; | |
833 if (java_thread->has_last_Java_frame()) { | |
834 RegisterMap reg_map(java_thread); | |
835 Thread* current_thread = Thread::current(); | |
836 ResourceMark rm(current_thread); | |
837 javaVFrame *jvf = java_thread->last_java_vframe(®_map); | |
838 HandleMark hm(current_thread); | |
839 if (start_depth != 0) { | |
840 if (start_depth > 0) { | |
841 for (int j = 0; j < start_depth && jvf != NULL; j++) { | |
842 jvf = jvf->java_sender(); | |
843 } | |
844 if (jvf == NULL) { | |
845 // start_depth is deeper than the stack depth | |
846 return JVMTI_ERROR_ILLEGAL_ARGUMENT; | |
847 } | |
848 } else { // start_depth < 0 | |
849 // we are referencing the starting depth based on the oldest | |
850 // part of the stack. | |
851 // optimize to limit the number of times that java_sender() is called | |
852 javaVFrame *jvf_cursor = jvf; | |
853 javaVFrame *jvf_prev = NULL; | |
854 javaVFrame *jvf_prev_prev; | |
855 int j = 0; | |
856 while (jvf_cursor != NULL) { | |
857 jvf_prev_prev = jvf_prev; | |
858 jvf_prev = jvf_cursor; | |
859 for (j = 0; j > start_depth && jvf_cursor != NULL; j--) { | |
860 jvf_cursor = jvf_cursor->java_sender(); | |
861 } | |
862 } | |
863 if (j == start_depth) { | |
864 // previous pointer is exactly where we want to start | |
865 jvf = jvf_prev; | |
866 } else { | |
867 // we need to back up further to get to the right place | |
868 if (jvf_prev_prev == NULL) { | |
869 // the -start_depth is greater than the stack depth | |
870 return JVMTI_ERROR_ILLEGAL_ARGUMENT; | |
871 } | |
872 // j now is the number of frames on the stack starting with | |
873 // jvf_prev, we start from jvf_prev_prev and move older on | |
874 // the stack that many, the result is -start_depth frames | |
875 // remaining. | |
876 jvf = jvf_prev_prev; | |
877 for (; j < 0; j++) { | |
878 jvf = jvf->java_sender(); | |
879 } | |
880 } | |
881 } | |
882 } | |
883 for (; count < max_count && jvf != NULL; count++) { | |
884 frame_buffer[count].method = jvf->method()->jmethod_id(); | |
885 frame_buffer[count].location = (jvf->method()->is_native() ? -1 : jvf->bci()); | |
886 jvf = jvf->java_sender(); | |
887 } | |
888 } else { | |
889 if (start_depth != 0) { | |
890 // no frames and there is a starting depth | |
891 return JVMTI_ERROR_ILLEGAL_ARGUMENT; | |
892 } | |
893 } | |
894 *count_ptr = count; | |
895 return JVMTI_ERROR_NONE; | |
896 } | |
897 | |
898 jvmtiError | |
899 JvmtiEnvBase::get_frame_count(JvmtiThreadState *state, jint *count_ptr) { | |
900 assert((state != NULL), | |
901 "JavaThread should create JvmtiThreadState before calling this method"); | |
902 *count_ptr = state->count_frames(); | |
903 return JVMTI_ERROR_NONE; | |
904 } | |
905 | |
906 jvmtiError | |
907 JvmtiEnvBase::get_frame_location(JavaThread *java_thread, jint depth, | |
908 jmethodID* method_ptr, jlocation* location_ptr) { | |
909 #ifdef ASSERT | |
910 uint32_t debug_bits = 0; | |
911 #endif | |
912 assert((SafepointSynchronize::is_at_safepoint() || | |
913 is_thread_fully_suspended(java_thread, false, &debug_bits)), | |
914 "at safepoint or target thread is suspended"); | |
915 Thread* current_thread = Thread::current(); | |
916 ResourceMark rm(current_thread); | |
917 | |
918 vframe *vf = vframeFor(java_thread, depth); | |
919 if (vf == NULL) { | |
920 return JVMTI_ERROR_NO_MORE_FRAMES; | |
921 } | |
922 | |
923 // vframeFor should return a java frame. If it doesn't | |
924 // it means we've got an internal error and we return the | |
925 // error in product mode. In debug mode we will instead | |
926 // attempt to cast the vframe to a javaVFrame and will | |
927 // cause an assertion/crash to allow further diagnosis. | |
928 #ifdef PRODUCT | |
929 if (!vf->is_java_frame()) { | |
930 return JVMTI_ERROR_INTERNAL; | |
931 } | |
932 #endif | |
933 | |
934 HandleMark hm(current_thread); | |
935 javaVFrame *jvf = javaVFrame::cast(vf); | |
936 methodOop method = jvf->method(); | |
937 if (method->is_native()) { | |
938 *location_ptr = -1; | |
939 } else { | |
940 *location_ptr = jvf->bci(); | |
941 } | |
942 *method_ptr = method->jmethod_id(); | |
943 | |
944 return JVMTI_ERROR_NONE; | |
945 } | |
946 | |
947 | |
948 jvmtiError | |
949 JvmtiEnvBase::get_object_monitor_usage(JavaThread* calling_thread, jobject object, jvmtiMonitorUsage* info_ptr) { | |
950 HandleMark hm; | |
951 Handle hobj; | |
952 | |
953 bool at_safepoint = SafepointSynchronize::is_at_safepoint(); | |
954 | |
955 // Check arguments | |
956 { | |
957 oop mirror = JNIHandles::resolve_external_guard(object); | |
958 NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT); | |
959 NULL_CHECK(info_ptr, JVMTI_ERROR_NULL_POINTER); | |
960 | |
961 hobj = Handle(mirror); | |
962 } | |
963 | |
964 JavaThread *owning_thread = NULL; | |
965 ObjectMonitor *mon = NULL; | |
966 jvmtiMonitorUsage ret = { | |
967 NULL, 0, 0, NULL, 0, NULL | |
968 }; | |
969 | |
970 uint32_t debug_bits = 0; | |
971 // first derive the object's owner and entry_count (if any) | |
972 { | |
973 // Revoke any biases before querying the mark word | |
974 if (SafepointSynchronize::is_at_safepoint()) { | |
975 BiasedLocking::revoke_at_safepoint(hobj); | |
976 } else { | |
977 BiasedLocking::revoke_and_rebias(hobj, false, calling_thread); | |
978 } | |
979 | |
980 address owner = NULL; | |
981 { | |
982 markOop mark = hobj()->mark(); | |
983 | |
984 if (!mark->has_monitor()) { | |
985 // this object has a lightweight monitor | |
986 | |
987 if (mark->has_locker()) { | |
988 owner = (address)mark->locker(); // save the address of the Lock word | |
989 } | |
990 // implied else: no owner | |
991 } else { | |
992 // this object has a heavyweight monitor | |
993 mon = mark->monitor(); | |
994 | |
995 // The owner field of a heavyweight monitor may be NULL for no | |
996 // owner, a JavaThread * or it may still be the address of the | |
997 // Lock word in a JavaThread's stack. A monitor can be inflated | |
998 // by a non-owning JavaThread, but only the owning JavaThread | |
999 // can change the owner field from the Lock word to the | |
1000 // JavaThread * and it may not have done that yet. | |
1001 owner = (address)mon->owner(); | |
1002 } | |
1003 } | |
1004 | |
1005 if (owner != NULL) { | |
1006 // This monitor is owned so we have to find the owning JavaThread. | |
1007 // Since owning_thread_from_monitor_owner() grabs a lock, GC can | |
1008 // move our object at this point. However, our owner value is safe | |
1009 // since it is either the Lock word on a stack or a JavaThread *. | |
1010 owning_thread = Threads::owning_thread_from_monitor_owner(owner, !at_safepoint); | |
1011 assert(owning_thread != NULL, "sanity check"); | |
1012 if (owning_thread != NULL) { // robustness | |
1013 // The monitor's owner either has to be the current thread, at safepoint | |
1014 // or it has to be suspended. Any of these conditions will prevent both | |
1015 // contending and waiting threads from modifying the state of | |
1016 // the monitor. | |
1017 if (!at_safepoint && !JvmtiEnv::is_thread_fully_suspended(owning_thread, true, &debug_bits)) { | |
1018 return JVMTI_ERROR_THREAD_NOT_SUSPENDED; | |
1019 } | |
1020 HandleMark hm; | |
1021 Handle th(owning_thread->threadObj()); | |
1022 ret.owner = (jthread)jni_reference(calling_thread, th); | |
1023 } | |
1024 // implied else: no owner | |
1025 } | |
1026 | |
1027 if (owning_thread != NULL) { // monitor is owned | |
1028 if ((address)owning_thread == owner) { | |
1029 // the owner field is the JavaThread * | |
1030 assert(mon != NULL, | |
1031 "must have heavyweight monitor with JavaThread * owner"); | |
1032 ret.entry_count = mon->recursions() + 1; | |
1033 } else { | |
1034 // The owner field is the Lock word on the JavaThread's stack | |
1035 // so the recursions field is not valid. We have to count the | |
1036 // number of recursive monitor entries the hard way. We pass | |
1037 // a handle to survive any GCs along the way. | |
1038 ResourceMark rm; | |
1039 ret.entry_count = count_locked_objects(owning_thread, hobj); | |
1040 } | |
1041 } | |
1042 // implied else: entry_count == 0 | |
1043 } | |
1044 | |
1045 int nWant,nWait; | |
1046 if (mon != NULL) { | |
1047 // this object has a heavyweight monitor | |
1048 nWant = mon->contentions(); // # of threads contending for monitor | |
1049 nWait = mon->waiters(); // # of threads in Object.wait() | |
1050 ret.waiter_count = nWant + nWait; | |
1051 ret.notify_waiter_count = nWait; | |
1052 } else { | |
1053 // this object has a lightweight monitor | |
1054 ret.waiter_count = 0; | |
1055 ret.notify_waiter_count = 0; | |
1056 } | |
1057 | |
1058 // Allocate memory for heavyweight and lightweight monitor. | |
1059 jvmtiError err; | |
1060 err = allocate(ret.waiter_count * sizeof(jthread *), (unsigned char**)&ret.waiters); | |
1061 if (err != JVMTI_ERROR_NONE) { | |
1062 return err; | |
1063 } | |
1064 err = allocate(ret.notify_waiter_count * sizeof(jthread *), | |
1065 (unsigned char**)&ret.notify_waiters); | |
1066 if (err != JVMTI_ERROR_NONE) { | |
1067 deallocate((unsigned char*)ret.waiters); | |
1068 return err; | |
1069 } | |
1070 | |
1071 // now derive the rest of the fields | |
1072 if (mon != NULL) { | |
1073 // this object has a heavyweight monitor | |
1074 | |
1075 // Number of waiters may actually be less than the waiter count. | |
1076 // So NULL out memory so that unused memory will be NULL. | |
1077 memset(ret.waiters, 0, ret.waiter_count * sizeof(jthread *)); | |
1078 memset(ret.notify_waiters, 0, ret.notify_waiter_count * sizeof(jthread *)); | |
1079 | |
1080 if (ret.waiter_count > 0) { | |
1081 // we have contending and/or waiting threads | |
1082 HandleMark hm; | |
1083 if (nWant > 0) { | |
1084 // we have contending threads | |
1085 ResourceMark rm; | |
1086 // get_pending_threads returns only java thread so we do not need to | |
1087 // check for non java threads. | |
1088 GrowableArray<JavaThread*>* wantList = Threads::get_pending_threads( | |
1089 nWant, (address)mon, !at_safepoint); | |
1090 if (wantList->length() < nWant) { | |
1091 // robustness: the pending list has gotten smaller | |
1092 nWant = wantList->length(); | |
1093 } | |
1094 for (int i = 0; i < nWant; i++) { | |
1095 JavaThread *pending_thread = wantList->at(i); | |
1096 // If the monitor has no owner, then a non-suspended contending | |
1097 // thread could potentially change the state of the monitor by | |
1098 // entering it. The JVM/TI spec doesn't allow this. | |
1099 if (owning_thread == NULL && !at_safepoint & | |
1100 !JvmtiEnv::is_thread_fully_suspended(pending_thread, true, &debug_bits)) { | |
1101 if (ret.owner != NULL) { | |
1102 destroy_jni_reference(calling_thread, ret.owner); | |
1103 } | |
1104 for (int j = 0; j < i; j++) { | |
1105 destroy_jni_reference(calling_thread, ret.waiters[j]); | |
1106 } | |
1107 deallocate((unsigned char*)ret.waiters); | |
1108 deallocate((unsigned char*)ret.notify_waiters); | |
1109 return JVMTI_ERROR_THREAD_NOT_SUSPENDED; | |
1110 } | |
1111 Handle th(pending_thread->threadObj()); | |
1112 ret.waiters[i] = (jthread)jni_reference(calling_thread, th); | |
1113 } | |
1114 } | |
1115 if (nWait > 0) { | |
1116 // we have threads in Object.wait() | |
1117 int offset = nWant; // add after any contending threads | |
1118 ObjectWaiter *waiter = mon->first_waiter(); | |
1119 for (int i = 0, j = 0; i < nWait; i++) { | |
1120 if (waiter == NULL) { | |
1121 // robustness: the waiting list has gotten smaller | |
1122 nWait = j; | |
1123 break; | |
1124 } | |
1125 Thread *t = mon->thread_of_waiter(waiter); | |
1126 if (t != NULL && t->is_Java_thread()) { | |
1127 JavaThread *wjava_thread = (JavaThread *)t; | |
1128 // If the thread was found on the ObjectWaiter list, then | |
1129 // it has not been notified. This thread can't change the | |
1130 // state of the monitor so it doesn't need to be suspended. | |
1131 Handle th(wjava_thread->threadObj()); | |
1132 ret.waiters[offset + j] = (jthread)jni_reference(calling_thread, th); | |
1133 ret.notify_waiters[j++] = (jthread)jni_reference(calling_thread, th); | |
1134 } | |
1135 waiter = mon->next_waiter(waiter); | |
1136 } | |
1137 } | |
1138 } | |
1139 | |
1140 // Adjust count. nWant and nWait count values may be less than original. | |
1141 ret.waiter_count = nWant + nWait; | |
1142 ret.notify_waiter_count = nWait; | |
1143 } else { | |
1144 // this object has a lightweight monitor and we have nothing more | |
1145 // to do here because the defaults are just fine. | |
1146 } | |
1147 | |
1148 // we don't update return parameter unless everything worked | |
1149 *info_ptr = ret; | |
1150 | |
1151 return JVMTI_ERROR_NONE; | |
1152 } | |
1153 | |
1154 ResourceTracker::ResourceTracker(JvmtiEnv* env) { | |
1155 _env = env; | |
1156 _allocations = new (ResourceObj::C_HEAP) GrowableArray<unsigned char*>(20, true); | |
1157 _failed = false; | |
1158 } | |
1159 ResourceTracker::~ResourceTracker() { | |
1160 if (_failed) { | |
1161 for (int i=0; i<_allocations->length(); i++) { | |
1162 _env->deallocate(_allocations->at(i)); | |
1163 } | |
1164 } | |
1165 delete _allocations; | |
1166 } | |
1167 | |
1168 jvmtiError ResourceTracker::allocate(jlong size, unsigned char** mem_ptr) { | |
1169 unsigned char *ptr; | |
1170 jvmtiError err = _env->allocate(size, &ptr); | |
1171 if (err == JVMTI_ERROR_NONE) { | |
1172 _allocations->append(ptr); | |
1173 *mem_ptr = ptr; | |
1174 } else { | |
1175 *mem_ptr = NULL; | |
1176 _failed = true; | |
1177 } | |
1178 return err; | |
1179 } | |
1180 | |
1181 unsigned char* ResourceTracker::allocate(jlong size) { | |
1182 unsigned char* ptr; | |
1183 allocate(size, &ptr); | |
1184 return ptr; | |
1185 } | |
1186 | |
1187 char* ResourceTracker::strdup(const char* str) { | |
1188 char *dup_str = (char*)allocate(strlen(str)+1); | |
1189 if (dup_str != NULL) { | |
1190 strcpy(dup_str, str); | |
1191 } | |
1192 return dup_str; | |
1193 } | |
1194 | |
1195 struct StackInfoNode { | |
1196 struct StackInfoNode *next; | |
1197 jvmtiStackInfo info; | |
1198 }; | |
1199 | |
1200 // Create a jvmtiStackInfo inside a linked list node and create a | |
1201 // buffer for the frame information, both allocated as resource objects. | |
1202 // Fill in both the jvmtiStackInfo and the jvmtiFrameInfo. | |
1203 // Note that either or both of thr and thread_oop | |
1204 // may be null if the thread is new or has exited. | |
1205 void | |
1206 VM_GetMultipleStackTraces::fill_frames(jthread jt, JavaThread *thr, oop thread_oop) { | |
1207 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); | |
1208 | |
1209 jint state = 0; | |
1210 struct StackInfoNode *node = NEW_RESOURCE_OBJ(struct StackInfoNode); | |
1211 jvmtiStackInfo *infop = &(node->info); | |
1212 node->next = head(); | |
1213 set_head(node); | |
1214 infop->frame_count = 0; | |
1215 infop->thread = jt; | |
1216 | |
1217 if (thread_oop != NULL) { | |
1218 // get most state bits | |
1219 state = (jint)java_lang_Thread::get_thread_status(thread_oop); | |
1220 } | |
1221 | |
1222 if (thr != NULL) { // add more state bits if there is a JavaThead to query | |
1223 // same as is_being_ext_suspended() but without locking | |
1224 if (thr->is_ext_suspended() || thr->is_external_suspend()) { | |
1225 state |= JVMTI_THREAD_STATE_SUSPENDED; | |
1226 } | |
1227 JavaThreadState jts = thr->thread_state(); | |
1228 if (jts == _thread_in_native) { | |
1229 state |= JVMTI_THREAD_STATE_IN_NATIVE; | |
1230 } | |
1231 OSThread* osThread = thr->osthread(); | |
1232 if (osThread != NULL && osThread->interrupted()) { | |
1233 state |= JVMTI_THREAD_STATE_INTERRUPTED; | |
1234 } | |
1235 } | |
1236 infop->state = state; | |
1237 | |
1238 if (thr != NULL || (state & JVMTI_THREAD_STATE_ALIVE) != 0) { | |
1239 infop->frame_buffer = NEW_RESOURCE_ARRAY(jvmtiFrameInfo, max_frame_count()); | |
1240 env()->get_stack_trace(thr, 0, max_frame_count(), | |
1241 infop->frame_buffer, &(infop->frame_count)); | |
1242 } else { | |
1243 infop->frame_buffer = NULL; | |
1244 infop->frame_count = 0; | |
1245 } | |
1246 _frame_count_total += infop->frame_count; | |
1247 } | |
1248 | |
1249 // Based on the stack information in the linked list, allocate memory | |
1250 // block to return and fill it from the info in the linked list. | |
1251 void | |
1252 VM_GetMultipleStackTraces::allocate_and_fill_stacks(jint thread_count) { | |
1253 // do I need to worry about alignment issues? | |
1254 jlong alloc_size = thread_count * sizeof(jvmtiStackInfo) | |
1255 + _frame_count_total * sizeof(jvmtiFrameInfo); | |
1256 env()->allocate(alloc_size, (unsigned char **)&_stack_info); | |
1257 | |
1258 // pointers to move through the newly allocated space as it is filled in | |
1259 jvmtiStackInfo *si = _stack_info + thread_count; // bottom of stack info | |
1260 jvmtiFrameInfo *fi = (jvmtiFrameInfo *)si; // is the top of frame info | |
1261 | |
1262 // copy information in resource area into allocated buffer | |
1263 // insert stack info backwards since linked list is backwards | |
1264 // insert frame info forwards | |
1265 // walk the StackInfoNodes | |
1266 for (struct StackInfoNode *sin = head(); sin != NULL; sin = sin->next) { | |
1267 jint frame_count = sin->info.frame_count; | |
1268 size_t frames_size = frame_count * sizeof(jvmtiFrameInfo); | |
1269 --si; | |
1270 memcpy(si, &(sin->info), sizeof(jvmtiStackInfo)); | |
1271 if (frames_size == 0) { | |
1272 si->frame_buffer = NULL; | |
1273 } else { | |
1274 memcpy(fi, sin->info.frame_buffer, frames_size); | |
1275 si->frame_buffer = fi; // point to the new allocated copy of the frames | |
1276 fi += frame_count; | |
1277 } | |
1278 } | |
1279 assert(si == _stack_info, "the last copied stack info must be the first record"); | |
1280 assert((unsigned char *)fi == ((unsigned char *)_stack_info) + alloc_size, | |
1281 "the last copied frame info must be the last record"); | |
1282 } | |
1283 | |
1284 | |
1285 void | |
1286 VM_GetThreadListStackTraces::doit() { | |
1287 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); | |
1288 | |
1289 ResourceMark rm; | |
1290 for (int i = 0; i < _thread_count; ++i) { | |
1291 jthread jt = _thread_list[i]; | |
1292 oop thread_oop = JNIHandles::resolve_external_guard(jt); | |
1142 | 1293 if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::Thread_klass())) { |
0 | 1294 set_result(JVMTI_ERROR_INVALID_THREAD); |
1295 return; | |
1296 } | |
1297 fill_frames(jt, java_lang_Thread::thread(thread_oop), thread_oop); | |
1298 } | |
1299 allocate_and_fill_stacks(_thread_count); | |
1300 } | |
1301 | |
1302 void | |
1303 VM_GetAllStackTraces::doit() { | |
1304 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); | |
1305 | |
1306 ResourceMark rm; | |
1307 _final_thread_count = 0; | |
1308 for (JavaThread *jt = Threads::first(); jt != NULL; jt = jt->next()) { | |
1309 oop thread_oop = jt->threadObj(); | |
1310 if (thread_oop != NULL && | |
1311 !jt->is_exiting() && | |
1312 java_lang_Thread::is_alive(thread_oop) && | |
1313 !jt->is_hidden_from_external_view()) { | |
1314 ++_final_thread_count; | |
1315 // Handle block of the calling thread is used to create local refs. | |
1316 fill_frames((jthread)JNIHandles::make_local(_calling_thread, thread_oop), | |
1317 jt, thread_oop); | |
1318 } | |
1319 } | |
1320 allocate_and_fill_stacks(_final_thread_count); | |
1321 } | |
1322 | |
1323 // Verifies that the top frame is a java frame in an expected state. | |
1324 // Deoptimizes frame if needed. | |
1325 // Checks that the frame method signature matches the return type (tos). | |
1326 // HandleMark must be defined in the caller only. | |
1327 // It is to keep a ret_ob_h handle alive after return to the caller. | |
1328 jvmtiError | |
1329 JvmtiEnvBase::check_top_frame(JavaThread* current_thread, JavaThread* java_thread, | |
1330 jvalue value, TosState tos, Handle* ret_ob_h) { | |
1331 ResourceMark rm(current_thread); | |
1332 | |
1333 vframe *vf = vframeFor(java_thread, 0); | |
1334 NULL_CHECK(vf, JVMTI_ERROR_NO_MORE_FRAMES); | |
1335 | |
1336 javaVFrame *jvf = (javaVFrame*) vf; | |
1337 if (!vf->is_java_frame() || jvf->method()->is_native()) { | |
1338 return JVMTI_ERROR_OPAQUE_FRAME; | |
1339 } | |
1340 | |
1341 // If the frame is a compiled one, need to deoptimize it. | |
1342 if (vf->is_compiled_frame()) { | |
1343 if (!vf->fr().can_be_deoptimized()) { | |
1344 return JVMTI_ERROR_OPAQUE_FRAME; | |
1345 } | |
1905
ce6848d0666d
6968367: can_post_on_exceptions is still using VM_DeoptimizeFrame in some places
never
parents:
1552
diff
changeset
|
1346 Deoptimization::deoptimize_frame(java_thread, jvf->fr().id()); |
0 | 1347 } |
1348 | |
1349 // Get information about method return type | |
1350 symbolHandle signature(current_thread, jvf->method()->signature()); | |
1351 | |
1352 ResultTypeFinder rtf(signature); | |
1353 TosState fr_tos = as_TosState(rtf.type()); | |
1354 if (fr_tos != tos) { | |
1355 if (tos != itos || (fr_tos != btos && fr_tos != ctos && fr_tos != stos)) { | |
1356 return JVMTI_ERROR_TYPE_MISMATCH; | |
1357 } | |
1358 } | |
1359 | |
1360 // Check that the jobject class matches the return type signature. | |
1361 jobject jobj = value.l; | |
1362 if (tos == atos && jobj != NULL) { // NULL reference is allowed | |
1363 Handle ob_h = Handle(current_thread, JNIHandles::resolve_external_guard(jobj)); | |
1364 NULL_CHECK(ob_h, JVMTI_ERROR_INVALID_OBJECT); | |
1365 KlassHandle ob_kh = KlassHandle(current_thread, ob_h()->klass()); | |
1366 NULL_CHECK(ob_kh, JVMTI_ERROR_INVALID_OBJECT); | |
1367 | |
1368 // Method return type signature. | |
1369 char* ty_sign = 1 + strchr(signature->as_C_string(), ')'); | |
1370 | |
1371 if (!VM_GetOrSetLocal::is_assignable(ty_sign, Klass::cast(ob_kh()), current_thread)) { | |
1372 return JVMTI_ERROR_TYPE_MISMATCH; | |
1373 } | |
1374 *ret_ob_h = ob_h; | |
1375 } | |
1376 return JVMTI_ERROR_NONE; | |
1377 } /* end check_top_frame */ | |
1378 | |
1379 | |
1380 // ForceEarlyReturn<type> follows the PopFrame approach in many aspects. | |
1381 // Main difference is on the last stage in the interpreter. | |
1382 // The PopFrame stops method execution to continue execution | |
1383 // from the same method call instruction. | |
1384 // The ForceEarlyReturn forces return from method so the execution | |
1385 // continues at the bytecode following the method call. | |
1386 | |
1387 // Threads_lock NOT held, java_thread not protected by lock | |
1388 // java_thread - pre-checked | |
1389 | |
1390 jvmtiError | |
1391 JvmtiEnvBase::force_early_return(JavaThread* java_thread, jvalue value, TosState tos) { | |
1392 JavaThread* current_thread = JavaThread::current(); | |
1393 HandleMark hm(current_thread); | |
1394 uint32_t debug_bits = 0; | |
1395 | |
609
ea20d7ce26b0
6800721: 3/4 JavaThread::jvmti_thread_state() and JvmtiThreadState::state_for() robustness
dcubed
parents:
470
diff
changeset
|
1396 // retrieve or create the state |
ea20d7ce26b0
6800721: 3/4 JavaThread::jvmti_thread_state() and JvmtiThreadState::state_for() robustness
dcubed
parents:
470
diff
changeset
|
1397 JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread); |
ea20d7ce26b0
6800721: 3/4 JavaThread::jvmti_thread_state() and JvmtiThreadState::state_for() robustness
dcubed
parents:
470
diff
changeset
|
1398 if (state == NULL) { |
ea20d7ce26b0
6800721: 3/4 JavaThread::jvmti_thread_state() and JvmtiThreadState::state_for() robustness
dcubed
parents:
470
diff
changeset
|
1399 return JVMTI_ERROR_THREAD_NOT_ALIVE; |
ea20d7ce26b0
6800721: 3/4 JavaThread::jvmti_thread_state() and JvmtiThreadState::state_for() robustness
dcubed
parents:
470
diff
changeset
|
1400 } |
ea20d7ce26b0
6800721: 3/4 JavaThread::jvmti_thread_state() and JvmtiThreadState::state_for() robustness
dcubed
parents:
470
diff
changeset
|
1401 |
0 | 1402 // Check if java_thread is fully suspended |
1403 if (!is_thread_fully_suspended(java_thread, | |
1404 true /* wait for suspend completion */, | |
1405 &debug_bits)) { | |
1406 return JVMTI_ERROR_THREAD_NOT_SUSPENDED; | |
1407 } | |
1408 | |
1409 // Check to see if a ForceEarlyReturn was already in progress | |
1410 if (state->is_earlyret_pending()) { | |
1411 // Probably possible for JVMTI clients to trigger this, but the | |
1412 // JPDA backend shouldn't allow this to happen | |
1413 return JVMTI_ERROR_INTERNAL; | |
1414 } | |
1415 { | |
1416 // The same as for PopFrame. Workaround bug: | |
1417 // 4812902: popFrame hangs if the method is waiting at a synchronize | |
1418 // Catch this condition and return an error to avoid hanging. | |
1419 // Now JVMTI spec allows an implementation to bail out with an opaque | |
1420 // frame error. | |
1421 OSThread* osThread = java_thread->osthread(); | |
1422 if (osThread->get_state() == MONITOR_WAIT) { | |
1423 return JVMTI_ERROR_OPAQUE_FRAME; | |
1424 } | |
1425 } | |
1426 Handle ret_ob_h = Handle(); | |
1427 jvmtiError err = check_top_frame(current_thread, java_thread, value, tos, &ret_ob_h); | |
1428 if (err != JVMTI_ERROR_NONE) { | |
1429 return err; | |
1430 } | |
1431 assert(tos != atos || value.l == NULL || ret_ob_h() != NULL, | |
1432 "return object oop must not be NULL if jobject is not NULL"); | |
1433 | |
1434 // Update the thread state to reflect that the top frame must be | |
1435 // forced to return. | |
1436 // The current frame will be returned later when the suspended | |
1437 // thread is resumed and right before returning from VM to Java. | |
1438 // (see call_VM_base() in assembler_<cpu>.cpp). | |
1439 | |
1440 state->set_earlyret_pending(); | |
1441 state->set_earlyret_oop(ret_ob_h()); | |
1442 state->set_earlyret_value(value, tos); | |
1443 | |
1444 // Set pending step flag for this early return. | |
1445 // It is cleared when next step event is posted. | |
1446 state->set_pending_step_for_earlyret(); | |
1447 | |
1448 return JVMTI_ERROR_NONE; | |
1449 } /* end force_early_return */ | |
1450 | |
1451 void | |
1452 JvmtiMonitorClosure::do_monitor(ObjectMonitor* mon) { | |
1453 if ( _error != JVMTI_ERROR_NONE) { | |
1454 // Error occurred in previous iteration so no need to add | |
1455 // to the list. | |
1456 return; | |
1457 } | |
1458 if (mon->owner() == _java_thread ) { | |
1459 // Filter out on stack monitors collected during stack walk. | |
1460 oop obj = (oop)mon->object(); | |
1461 bool found = false; | |
1462 for (int j = 0; j < _owned_monitors_list->length(); j++) { | |
1463 jobject jobj = ((jvmtiMonitorStackDepthInfo*)_owned_monitors_list->at(j))->monitor; | |
1464 oop check = JNIHandles::resolve(jobj); | |
1465 if (check == obj) { | |
1466 // On stack monitor already collected during the stack walk. | |
1467 found = true; | |
1468 break; | |
1469 } | |
1470 } | |
1471 if (found == false) { | |
1472 // This is off stack monitor (e.g. acquired via jni MonitorEnter). | |
1473 jvmtiError err; | |
1474 jvmtiMonitorStackDepthInfo *jmsdi; | |
1475 err = _env->allocate(sizeof(jvmtiMonitorStackDepthInfo), (unsigned char **)&jmsdi); | |
1476 if (err != JVMTI_ERROR_NONE) { | |
1477 _error = err; | |
1478 return; | |
1479 } | |
1480 Handle hobj(obj); | |
1481 jmsdi->monitor = _env->jni_reference(_calling_thread, hobj); | |
1482 // stack depth is unknown for this monitor. | |
1483 jmsdi->stack_depth = -1; | |
1484 _owned_monitors_list->append(jmsdi); | |
1485 } | |
1486 } | |
1487 } | |
1488 | |
1489 #endif // !JVMTI_KERNEL |