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