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