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