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