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