1 /* 2 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "classfile/systemDictionary.hpp" 27 #include "interpreter/interpreter.hpp" 28 #include "jvmtifiles/jvmtiEnv.hpp" 29 #include "memory/resourceArea.hpp" 30 #include "oops/instanceKlass.hpp" 31 #include "prims/jvmtiAgentThread.hpp" 32 #include "prims/jvmtiEventController.inline.hpp" 33 #include "prims/jvmtiImpl.hpp" 34 #include "prims/jvmtiRedefineClasses.hpp" 35 #include "runtime/handles.hpp" 36 #include "runtime/handles.inline.hpp" 37 #include "runtime/interfaceSupport.hpp" 38 #include "runtime/javaCalls.hpp" 39 #include "runtime/signature.hpp" 40 #include "runtime/vframe.hpp" 41 #include "runtime/vframe_hp.hpp" 42 #include "runtime/vm_operations.hpp" 43 #include "utilities/exceptions.hpp" 44 #ifdef TARGET_OS_FAMILY_linux 45 # include "thread_linux.inline.hpp" 46 #endif 47 #ifdef TARGET_OS_FAMILY_solaris 48 # include "thread_solaris.inline.hpp" 49 #endif 50 #ifdef TARGET_OS_FAMILY_windows 51 # include "thread_windows.inline.hpp" 52 #endif 53 54 GrowableArray<JvmtiRawMonitor*> *JvmtiPendingMonitors::_monitors = new (ResourceObj::C_HEAP) GrowableArray<JvmtiRawMonitor*>(1,true); 55 56 void JvmtiPendingMonitors::transition_raw_monitors() { 57 assert((Threads::number_of_threads()==1), 58 "Java thread has not created yet or more than one java thread \ 59 is running. Raw monitor transition will not work"); 60 JavaThread *current_java_thread = JavaThread::current(); 61 assert(current_java_thread->thread_state() == _thread_in_vm, "Must be in vm"); 62 { 63 ThreadBlockInVM __tbivm(current_java_thread); 64 for(int i=0; i< count(); i++) { 65 JvmtiRawMonitor *rmonitor = monitors()->at(i); 66 int r = rmonitor->raw_enter(current_java_thread); 67 assert(r == ObjectMonitor::OM_OK, "raw_enter should have worked"); 68 } 69 } 70 // pending monitors are converted to real monitor so delete them all. 71 dispose(); 72 } 73 74 // 75 // class JvmtiAgentThread 76 // 77 // JavaThread used to wrap a thread started by an agent 78 // using the JVMTI method RunAgentThread. 79 // 80 81 JvmtiAgentThread::JvmtiAgentThread(JvmtiEnv* env, jvmtiStartFunction start_fn, const void *start_arg) 82 : JavaThread(start_function_wrapper) { 83 _env = env; 84 _start_fn = start_fn; 85 _start_arg = start_arg; 86 } 87 88 void 89 JvmtiAgentThread::start_function_wrapper(JavaThread *thread, TRAPS) { 90 // It is expected that any Agent threads will be created as 91 // Java Threads. If this is the case, notification of the creation 92 // of the thread is given in JavaThread::thread_main(). 93 assert(thread->is_Java_thread(), "debugger thread should be a Java Thread"); 94 assert(thread == JavaThread::current(), "sanity check"); 95 96 JvmtiAgentThread *dthread = (JvmtiAgentThread *)thread; 97 dthread->call_start_function(); 98 } 99 100 void 101 JvmtiAgentThread::call_start_function() { 102 ThreadToNativeFromVM transition(this); 103 _start_fn(_env->jvmti_external(), jni_environment(), (void*)_start_arg); 104 } 105 106 107 // 108 // class GrowableCache - private methods 109 // 110 111 void GrowableCache::recache() { 112 int len = _elements->length(); 113 114 FREE_C_HEAP_ARRAY(address, _cache); 115 _cache = NEW_C_HEAP_ARRAY(address,len+1); 116 117 for (int i=0; i<len; i++) { 118 _cache[i] = _elements->at(i)->getCacheValue(); 119 // 120 // The cache entry has gone bad. Without a valid frame pointer 121 // value, the entry is useless so we simply delete it in product 122 // mode. The call to remove() will rebuild the cache again 123 // without the bad entry. 124 // 125 if (_cache[i] == NULL) { 126 assert(false, "cannot recache NULL elements"); 127 remove(i); 128 return; 129 } 130 } 131 _cache[len] = NULL; 132 133 _listener_fun(_this_obj,_cache); 134 } 135 136 bool GrowableCache::equals(void* v, GrowableElement *e2) { 137 GrowableElement *e1 = (GrowableElement *) v; 138 assert(e1 != NULL, "e1 != NULL"); 139 assert(e2 != NULL, "e2 != NULL"); 140 141 return e1->equals(e2); 142 } 143 144 // 145 // class GrowableCache - public methods 146 // 147 148 GrowableCache::GrowableCache() { 149 _this_obj = NULL; 150 _listener_fun = NULL; 151 _elements = NULL; 152 _cache = NULL; 153 } 154 155 GrowableCache::~GrowableCache() { 156 clear(); 157 delete _elements; 158 FREE_C_HEAP_ARRAY(address, _cache); 159 } 160 161 void GrowableCache::initialize(void *this_obj, void listener_fun(void *, address*) ) { 162 _this_obj = this_obj; 163 _listener_fun = listener_fun; 164 _elements = new (ResourceObj::C_HEAP) GrowableArray<GrowableElement*>(5,true); 165 recache(); 166 } 167 168 // number of elements in the collection 169 int GrowableCache::length() { 170 return _elements->length(); 171 } 172 173 // get the value of the index element in the collection 174 GrowableElement* GrowableCache::at(int index) { 175 GrowableElement *e = (GrowableElement *) _elements->at(index); 176 assert(e != NULL, "e != NULL"); 177 return e; 178 } 179 180 int GrowableCache::find(GrowableElement* e) { 181 return _elements->find(e, GrowableCache::equals); 182 } 183 184 // append a copy of the element to the end of the collection 185 void GrowableCache::append(GrowableElement* e) { 186 GrowableElement *new_e = e->clone(); 187 _elements->append(new_e); 188 recache(); 189 } 190 191 // insert a copy of the element using lessthan() 192 void GrowableCache::insert(GrowableElement* e) { 193 GrowableElement *new_e = e->clone(); 194 _elements->append(new_e); 195 196 int n = length()-2; 197 for (int i=n; i>=0; i--) { 198 GrowableElement *e1 = _elements->at(i); 199 GrowableElement *e2 = _elements->at(i+1); 200 if (e2->lessThan(e1)) { 201 _elements->at_put(i+1, e1); 202 _elements->at_put(i, e2); 203 } 204 } 205 206 recache(); 207 } 208 209 // remove the element at index 210 void GrowableCache::remove (int index) { 211 GrowableElement *e = _elements->at(index); 212 assert(e != NULL, "e != NULL"); 213 _elements->remove(e); 214 delete e; 215 recache(); 216 } 217 218 // clear out all elements, release all heap space and 219 // let our listener know that things have changed. 220 void GrowableCache::clear() { 221 int len = _elements->length(); 222 for (int i=0; i<len; i++) { 223 delete _elements->at(i); 224 } 225 _elements->clear(); 226 recache(); 227 } 228 229 void GrowableCache::oops_do(OopClosure* f) { 230 int len = _elements->length(); 231 for (int i=0; i<len; i++) { 232 GrowableElement *e = _elements->at(i); 233 e->oops_do(f); 234 } 235 } 236 237 void GrowableCache::gc_epilogue() { 238 int len = _elements->length(); 239 // recompute the new cache value after GC 240 for (int i=0; i<len; i++) { 241 _cache[i] = _elements->at(i)->getCacheValue(); 242 } 243 } 244 245 246 // 247 // class JvmtiRawMonitor 248 // 249 250 JvmtiRawMonitor::JvmtiRawMonitor(const char *name) { 251 #ifdef ASSERT 252 _name = strcpy(NEW_C_HEAP_ARRAY(char, strlen(name) + 1), name); 253 #else 254 _name = NULL; 255 #endif 256 _magic = JVMTI_RM_MAGIC; 257 } 258 259 JvmtiRawMonitor::~JvmtiRawMonitor() { 260 #ifdef ASSERT 261 FreeHeap(_name); 262 #endif 263 _magic = 0; 264 } 265 266 267 bool 268 JvmtiRawMonitor::is_valid() { 269 int value = 0; 270 271 // This object might not be a JvmtiRawMonitor so we can't assume 272 // the _magic field is properly aligned. Get the value in a safe 273 // way and then check against JVMTI_RM_MAGIC. 274 275 switch (sizeof(_magic)) { 276 case 2: 277 value = Bytes::get_native_u2((address)&_magic); 278 break; 279 280 case 4: 281 value = Bytes::get_native_u4((address)&_magic); 282 break; 283 284 case 8: 285 value = Bytes::get_native_u8((address)&_magic); 286 break; 287 288 default: 289 guarantee(false, "_magic field is an unexpected size"); 290 } 291 292 return value == JVMTI_RM_MAGIC; 293 } 294 295 296 // 297 // class JvmtiBreakpoint 298 // 299 300 JvmtiBreakpoint::JvmtiBreakpoint() { 301 _method = NULL; 302 _bci = 0; 303 #ifdef CHECK_UNHANDLED_OOPS 304 // This one is always allocated with new, but check it just in case. 305 Thread *thread = Thread::current(); 306 if (thread->is_in_stack((address)&_method)) { 307 thread->allow_unhandled_oop((oop*)&_method); 308 } 309 #endif // CHECK_UNHANDLED_OOPS 310 } 311 312 JvmtiBreakpoint::JvmtiBreakpoint(methodOop m_method, jlocation location) { 313 _method = m_method; 314 assert(_method != NULL, "_method != NULL"); 315 _bci = (int) location; 316 #ifdef CHECK_UNHANDLED_OOPS 317 // Could be allocated with new and wouldn't be on the unhandled oop list. 318 Thread *thread = Thread::current(); 319 if (thread->is_in_stack((address)&_method)) { 320 thread->allow_unhandled_oop(&_method); 321 } 322 #endif // CHECK_UNHANDLED_OOPS 323 324 assert(_bci >= 0, "_bci >= 0"); 325 } 326 327 void JvmtiBreakpoint::copy(JvmtiBreakpoint& bp) { 328 _method = bp._method; 329 _bci = bp._bci; 330 } 331 332 bool JvmtiBreakpoint::lessThan(JvmtiBreakpoint& bp) { 333 Unimplemented(); 334 return false; 335 } 336 337 bool JvmtiBreakpoint::equals(JvmtiBreakpoint& bp) { 338 return _method == bp._method 339 && _bci == bp._bci; 340 } 341 342 bool JvmtiBreakpoint::is_valid() { 343 return _method != NULL && 344 _bci >= 0; 345 } 346 347 address JvmtiBreakpoint::getBcp() { 348 return _method->bcp_from(_bci); 349 } 350 351 void JvmtiBreakpoint::each_method_version_do(method_action meth_act) { 352 ((methodOopDesc*)_method->*meth_act)(_bci); 353 354 // add/remove breakpoint to/from versions of the method that 355 // are EMCP. Directly or transitively obsolete methods are 356 // not saved in the PreviousVersionInfo. 357 Thread *thread = Thread::current(); 358 instanceKlassHandle ikh = instanceKlassHandle(thread, _method->method_holder()); 359 symbolOop m_name = _method->name(); 360 symbolOop m_signature = _method->signature(); 361 362 { 363 ResourceMark rm(thread); 364 // PreviousVersionInfo objects returned via PreviousVersionWalker 365 // contain a GrowableArray of handles. We have to clean up the 366 // GrowableArray _after_ the PreviousVersionWalker destructor 367 // has destroyed the handles. 368 { 369 // search previous versions if they exist 370 PreviousVersionWalker pvw((instanceKlass *)ikh()->klass_part()); 371 for (PreviousVersionInfo * pv_info = pvw.next_previous_version(); 372 pv_info != NULL; pv_info = pvw.next_previous_version()) { 373 GrowableArray<methodHandle>* methods = 374 pv_info->prev_EMCP_method_handles(); 375 376 if (methods == NULL) { 377 // We have run into a PreviousVersion generation where 378 // all methods were made obsolete during that generation's 379 // RedefineClasses() operation. At the time of that 380 // operation, all EMCP methods were flushed so we don't 381 // have to go back any further. 382 // 383 // A NULL methods array is different than an empty methods 384 // array. We cannot infer any optimizations about older 385 // generations from an empty methods array for the current 386 // generation. 387 break; 388 } 389 390 for (int i = methods->length() - 1; i >= 0; i--) { 391 methodHandle method = methods->at(i); 392 if (method->name() == m_name && method->signature() == m_signature) { 393 RC_TRACE(0x00000800, ("%sing breakpoint in %s(%s)", 394 meth_act == &methodOopDesc::set_breakpoint ? "sett" : "clear", 395 method->name()->as_C_string(), 396 method->signature()->as_C_string())); 397 assert(!method->is_obsolete(), "only EMCP methods here"); 398 399 ((methodOopDesc*)method()->*meth_act)(_bci); 400 break; 401 } 402 } 403 } 404 } // pvw is cleaned up 405 } // rm is cleaned up 406 } 407 408 void JvmtiBreakpoint::set() { 409 each_method_version_do(&methodOopDesc::set_breakpoint); 410 } 411 412 void JvmtiBreakpoint::clear() { 413 each_method_version_do(&methodOopDesc::clear_breakpoint); 414 } 415 416 void JvmtiBreakpoint::print() { 417 #ifndef PRODUCT 418 const char *class_name = (_method == NULL) ? "NULL" : _method->klass_name()->as_C_string(); 419 const char *method_name = (_method == NULL) ? "NULL" : _method->name()->as_C_string(); 420 421 tty->print("Breakpoint(%s,%s,%d,%p)",class_name, method_name, _bci, getBcp()); 422 #endif 423 } 424 425 426 // 427 // class VM_ChangeBreakpoints 428 // 429 // Modify the Breakpoints data structure at a safepoint 430 // 431 432 void VM_ChangeBreakpoints::doit() { 433 switch (_operation) { 434 case SET_BREAKPOINT: 435 _breakpoints->set_at_safepoint(*_bp); 436 break; 437 case CLEAR_BREAKPOINT: 438 _breakpoints->clear_at_safepoint(*_bp); 439 break; 440 case CLEAR_ALL_BREAKPOINT: 441 _breakpoints->clearall_at_safepoint(); 442 break; 443 default: 444 assert(false, "Unknown operation"); 445 } 446 } 447 448 void VM_ChangeBreakpoints::oops_do(OopClosure* f) { 449 // This operation keeps breakpoints alive 450 if (_breakpoints != NULL) { 451 _breakpoints->oops_do(f); 452 } 453 if (_bp != NULL) { 454 _bp->oops_do(f); 455 } 456 } 457 458 // 459 // class JvmtiBreakpoints 460 // 461 // a JVMTI internal collection of JvmtiBreakpoint 462 // 463 464 JvmtiBreakpoints::JvmtiBreakpoints(void listener_fun(void *,address *)) { 465 _bps.initialize(this,listener_fun); 466 } 467 468 JvmtiBreakpoints:: ~JvmtiBreakpoints() {} 469 470 void JvmtiBreakpoints::oops_do(OopClosure* f) { 471 _bps.oops_do(f); 472 } 473 474 void JvmtiBreakpoints::gc_epilogue() { 475 _bps.gc_epilogue(); 476 } 477 478 void JvmtiBreakpoints::print() { 479 #ifndef PRODUCT 480 ResourceMark rm; 481 482 int n = _bps.length(); 483 for (int i=0; i<n; i++) { 484 JvmtiBreakpoint& bp = _bps.at(i); 485 tty->print("%d: ", i); 486 bp.print(); 487 tty->print_cr(""); 488 } 489 #endif 490 } 491 492 493 void JvmtiBreakpoints::set_at_safepoint(JvmtiBreakpoint& bp) { 494 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 495 496 int i = _bps.find(bp); 497 if (i == -1) { 498 _bps.append(bp); 499 bp.set(); 500 } 501 } 502 503 void JvmtiBreakpoints::clear_at_safepoint(JvmtiBreakpoint& bp) { 504 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 505 506 int i = _bps.find(bp); 507 if (i != -1) { 508 _bps.remove(i); 509 bp.clear(); 510 } 511 } 512 513 void JvmtiBreakpoints::clearall_at_safepoint() { 514 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 515 516 int len = _bps.length(); 517 for (int i=0; i<len; i++) { 518 _bps.at(i).clear(); 519 } 520 _bps.clear(); 521 } 522 523 int JvmtiBreakpoints::length() { return _bps.length(); } 524 525 int JvmtiBreakpoints::set(JvmtiBreakpoint& bp) { 526 if ( _bps.find(bp) != -1) { 527 return JVMTI_ERROR_DUPLICATE; 528 } 529 VM_ChangeBreakpoints set_breakpoint(this,VM_ChangeBreakpoints::SET_BREAKPOINT, &bp); 530 VMThread::execute(&set_breakpoint); 531 return JVMTI_ERROR_NONE; 532 } 533 534 int JvmtiBreakpoints::clear(JvmtiBreakpoint& bp) { 535 if ( _bps.find(bp) == -1) { 536 return JVMTI_ERROR_NOT_FOUND; 537 } 538 539 VM_ChangeBreakpoints clear_breakpoint(this,VM_ChangeBreakpoints::CLEAR_BREAKPOINT, &bp); 540 VMThread::execute(&clear_breakpoint); 541 return JVMTI_ERROR_NONE; 542 } 543 544 void JvmtiBreakpoints::clearall_in_class_at_safepoint(klassOop klass) { 545 bool changed = true; 546 // We are going to run thru the list of bkpts 547 // and delete some. This deletion probably alters 548 // the list in some implementation defined way such 549 // that when we delete entry i, the next entry might 550 // no longer be at i+1. To be safe, each time we delete 551 // an entry, we'll just start again from the beginning. 552 // We'll stop when we make a pass thru the whole list without 553 // deleting anything. 554 while (changed) { 555 int len = _bps.length(); 556 changed = false; 557 for (int i = 0; i < len; i++) { 558 JvmtiBreakpoint& bp = _bps.at(i); 559 if (bp.method()->method_holder() == klass) { 560 bp.clear(); 561 _bps.remove(i); 562 // This changed 'i' so we have to start over. 563 changed = true; 564 break; 565 } 566 } 567 } 568 } 569 570 void JvmtiBreakpoints::clearall() { 571 VM_ChangeBreakpoints clearall_breakpoint(this,VM_ChangeBreakpoints::CLEAR_ALL_BREAKPOINT); 572 VMThread::execute(&clearall_breakpoint); 573 } 574 575 // 576 // class JvmtiCurrentBreakpoints 577 // 578 579 JvmtiBreakpoints *JvmtiCurrentBreakpoints::_jvmti_breakpoints = NULL; 580 address * JvmtiCurrentBreakpoints::_breakpoint_list = NULL; 581 582 583 JvmtiBreakpoints& JvmtiCurrentBreakpoints::get_jvmti_breakpoints() { 584 if (_jvmti_breakpoints != NULL) return (*_jvmti_breakpoints); 585 _jvmti_breakpoints = new JvmtiBreakpoints(listener_fun); 586 assert(_jvmti_breakpoints != NULL, "_jvmti_breakpoints != NULL"); 587 return (*_jvmti_breakpoints); 588 } 589 590 void JvmtiCurrentBreakpoints::listener_fun(void *this_obj, address *cache) { 591 JvmtiBreakpoints *this_jvmti = (JvmtiBreakpoints *) this_obj; 592 assert(this_jvmti != NULL, "this_jvmti != NULL"); 593 594 debug_only(int n = this_jvmti->length();); 595 assert(cache[n] == NULL, "cache must be NULL terminated"); 596 597 set_breakpoint_list(cache); 598 } 599 600 601 void JvmtiCurrentBreakpoints::oops_do(OopClosure* f) { 602 if (_jvmti_breakpoints != NULL) { 603 _jvmti_breakpoints->oops_do(f); 604 } 605 } 606 607 void JvmtiCurrentBreakpoints::gc_epilogue() { 608 if (_jvmti_breakpoints != NULL) { 609 _jvmti_breakpoints->gc_epilogue(); 610 } 611 } 612 613 614 /////////////////////////////////////////////////////////////// 615 // 616 // class VM_GetOrSetLocal 617 // 618 619 // Constructor for non-object getter 620 VM_GetOrSetLocal::VM_GetOrSetLocal(JavaThread* thread, jint depth, int index, BasicType type) 621 : _thread(thread) 622 , _calling_thread(NULL) 623 , _depth(depth) 624 , _index(index) 625 , _type(type) 626 , _set(false) 627 , _jvf(NULL) 628 , _result(JVMTI_ERROR_NONE) 629 { 630 } 631 632 // Constructor for object or non-object setter 633 VM_GetOrSetLocal::VM_GetOrSetLocal(JavaThread* thread, jint depth, int index, BasicType type, jvalue value) 634 : _thread(thread) 635 , _calling_thread(NULL) 636 , _depth(depth) 637 , _index(index) 638 , _type(type) 639 , _value(value) 640 , _set(true) 641 , _jvf(NULL) 642 , _result(JVMTI_ERROR_NONE) 643 { 644 } 645 646 // Constructor for object getter 647 VM_GetOrSetLocal::VM_GetOrSetLocal(JavaThread* thread, JavaThread* calling_thread, jint depth, int index) 648 : _thread(thread) 649 , _calling_thread(calling_thread) 650 , _depth(depth) 651 , _index(index) 652 , _type(T_OBJECT) 653 , _set(false) 654 , _jvf(NULL) 655 , _result(JVMTI_ERROR_NONE) 656 { 657 } 658 659 660 vframe *VM_GetOrSetLocal::get_vframe() { 661 if (!_thread->has_last_Java_frame()) { 662 return NULL; 663 } 664 RegisterMap reg_map(_thread); 665 vframe *vf = _thread->last_java_vframe(®_map); 666 int d = 0; 667 while ((vf != NULL) && (d < _depth)) { 668 vf = vf->java_sender(); 669 d++; 670 } 671 return vf; 672 } 673 674 javaVFrame *VM_GetOrSetLocal::get_java_vframe() { 675 vframe* vf = get_vframe(); 676 if (vf == NULL) { 677 _result = JVMTI_ERROR_NO_MORE_FRAMES; 678 return NULL; 679 } 680 javaVFrame *jvf = (javaVFrame*)vf; 681 682 if (!vf->is_java_frame() || jvf->method()->is_native()) { 683 _result = JVMTI_ERROR_OPAQUE_FRAME; 684 return NULL; 685 } 686 return jvf; 687 } 688 689 // Check that the klass is assignable to a type with the given signature. 690 // Another solution could be to use the function Klass::is_subtype_of(type). 691 // But the type class can be forced to load/initialize eagerly in such a case. 692 // This may cause unexpected consequences like CFLH or class-init JVMTI events. 693 // It is better to avoid such a behavior. 694 bool VM_GetOrSetLocal::is_assignable(const char* ty_sign, Klass* klass, Thread* thread) { 695 assert(ty_sign != NULL, "type signature must not be NULL"); 696 assert(thread != NULL, "thread must not be NULL"); 697 assert(klass != NULL, "klass must not be NULL"); 698 699 int len = (int) strlen(ty_sign); 700 if (ty_sign[0] == 'L' && ty_sign[len-1] == ';') { // Need pure class/interface name 701 ty_sign++; 702 len -= 2; 703 } 704 symbolHandle ty_sym = oopFactory::new_symbol_handle(ty_sign, len, thread); 705 if (klass->name() == ty_sym()) { 706 return true; 707 } 708 // Compare primary supers 709 int super_depth = klass->super_depth(); 710 int idx; 711 for (idx = 0; idx < super_depth; idx++) { 712 if (Klass::cast(klass->primary_super_of_depth(idx))->name() == ty_sym()) { 713 return true; 714 } 715 } 716 // Compare secondary supers 717 objArrayOop sec_supers = klass->secondary_supers(); 718 for (idx = 0; idx < sec_supers->length(); idx++) { 719 if (Klass::cast((klassOop) sec_supers->obj_at(idx))->name() == ty_sym()) { 720 return true; 721 } 722 } 723 return false; 724 } 725 726 // Checks error conditions: 727 // JVMTI_ERROR_INVALID_SLOT 728 // JVMTI_ERROR_TYPE_MISMATCH 729 // Returns: 'true' - everything is Ok, 'false' - error code 730 731 bool VM_GetOrSetLocal::check_slot_type(javaVFrame* jvf) { 732 methodOop method_oop = jvf->method(); 733 if (!method_oop->has_localvariable_table()) { 734 // Just to check index boundaries 735 jint extra_slot = (_type == T_LONG || _type == T_DOUBLE) ? 1 : 0; 736 if (_index < 0 || _index + extra_slot >= method_oop->max_locals()) { 737 _result = JVMTI_ERROR_INVALID_SLOT; 738 return false; 739 } 740 return true; 741 } 742 743 jint num_entries = method_oop->localvariable_table_length(); 744 if (num_entries == 0) { 745 _result = JVMTI_ERROR_INVALID_SLOT; 746 return false; // There are no slots 747 } 748 int signature_idx = -1; 749 int vf_bci = jvf->bci(); 750 LocalVariableTableElement* table = method_oop->localvariable_table_start(); 751 for (int i = 0; i < num_entries; i++) { 752 int start_bci = table[i].start_bci; 753 int end_bci = start_bci + table[i].length; 754 755 // Here we assume that locations of LVT entries 756 // with the same slot number cannot be overlapped 757 if (_index == (jint) table[i].slot && start_bci <= vf_bci && vf_bci <= end_bci) { 758 signature_idx = (int) table[i].descriptor_cp_index; 759 break; 760 } 761 } 762 if (signature_idx == -1) { 763 _result = JVMTI_ERROR_INVALID_SLOT; 764 return false; // Incorrect slot index 765 } 766 symbolOop sign_sym = method_oop->constants()->symbol_at(signature_idx); 767 const char* signature = (const char *) sign_sym->as_utf8(); 768 BasicType slot_type = char2type(signature[0]); 769 770 switch (slot_type) { 771 case T_BYTE: 772 case T_SHORT: 773 case T_CHAR: 774 case T_BOOLEAN: 775 slot_type = T_INT; 776 break; 777 case T_ARRAY: 778 slot_type = T_OBJECT; 779 break; 780 }; 781 if (_type != slot_type) { 782 _result = JVMTI_ERROR_TYPE_MISMATCH; 783 return false; 784 } 785 786 jobject jobj = _value.l; 787 if (_set && slot_type == T_OBJECT && jobj != NULL) { // NULL reference is allowed 788 // Check that the jobject class matches the return type signature. 789 JavaThread* cur_thread = JavaThread::current(); 790 HandleMark hm(cur_thread); 791 792 Handle obj = Handle(cur_thread, JNIHandles::resolve_external_guard(jobj)); 793 NULL_CHECK(obj, (_result = JVMTI_ERROR_INVALID_OBJECT, false)); 794 KlassHandle ob_kh = KlassHandle(cur_thread, obj->klass()); 795 NULL_CHECK(ob_kh, (_result = JVMTI_ERROR_INVALID_OBJECT, false)); 796 797 if (!is_assignable(signature, Klass::cast(ob_kh()), cur_thread)) { 798 _result = JVMTI_ERROR_TYPE_MISMATCH; 799 return false; 800 } 801 } 802 return true; 803 } 804 805 static bool can_be_deoptimized(vframe* vf) { 806 return (vf->is_compiled_frame() && vf->fr().can_be_deoptimized()); 807 } 808 809 bool VM_GetOrSetLocal::doit_prologue() { 810 _jvf = get_java_vframe(); 811 NULL_CHECK(_jvf, false); 812 813 if (!check_slot_type(_jvf)) { 814 return false; 815 } 816 return true; 817 } 818 819 void VM_GetOrSetLocal::doit() { 820 if (_set) { 821 // Force deoptimization of frame if compiled because it's 822 // possible the compiler emitted some locals as constant values, 823 // meaning they are not mutable. 824 if (can_be_deoptimized(_jvf)) { 825 826 // Schedule deoptimization so that eventually the local 827 // update will be written to an interpreter frame. 828 VM_DeoptimizeFrame deopt(_jvf->thread(), _jvf->fr().id()); 829 VMThread::execute(&deopt); 830 831 // Now store a new value for the local which will be applied 832 // once deoptimization occurs. Note however that while this 833 // write is deferred until deoptimization actually happens 834 // can vframe created after this point will have its locals 835 // reflecting this update so as far as anyone can see the 836 // write has already taken place. 837 838 // If we are updating an oop then get the oop from the handle 839 // since the handle will be long gone by the time the deopt 840 // happens. The oop stored in the deferred local will be 841 // gc'd on its own. 842 if (_type == T_OBJECT) { 843 _value.l = (jobject) (JNIHandles::resolve_external_guard(_value.l)); 844 } 845 // Re-read the vframe so we can see that it is deoptimized 846 // [ Only need because of assert in update_local() ] 847 _jvf = get_java_vframe(); 848 ((compiledVFrame*)_jvf)->update_local(_type, _index, _value); 849 return; 850 } 851 StackValueCollection *locals = _jvf->locals(); 852 HandleMark hm; 853 854 switch (_type) { 855 case T_INT: locals->set_int_at (_index, _value.i); break; 856 case T_LONG: locals->set_long_at (_index, _value.j); break; 857 case T_FLOAT: locals->set_float_at (_index, _value.f); break; 858 case T_DOUBLE: locals->set_double_at(_index, _value.d); break; 859 case T_OBJECT: { 860 Handle ob_h(JNIHandles::resolve_external_guard(_value.l)); 861 locals->set_obj_at (_index, ob_h); 862 break; 863 } 864 default: ShouldNotReachHere(); 865 } 866 _jvf->set_locals(locals); 867 } else { 868 StackValueCollection *locals = _jvf->locals(); 869 870 if (locals->at(_index)->type() == T_CONFLICT) { 871 memset(&_value, 0, sizeof(_value)); 872 _value.l = NULL; 873 return; 874 } 875 876 switch (_type) { 877 case T_INT: _value.i = locals->int_at (_index); break; 878 case T_LONG: _value.j = locals->long_at (_index); break; 879 case T_FLOAT: _value.f = locals->float_at (_index); break; 880 case T_DOUBLE: _value.d = locals->double_at(_index); break; 881 case T_OBJECT: { 882 // Wrap the oop to be returned in a local JNI handle since 883 // oops_do() no longer applies after doit() is finished. 884 oop obj = locals->obj_at(_index)(); 885 _value.l = JNIHandles::make_local(_calling_thread, obj); 886 break; 887 } 888 default: ShouldNotReachHere(); 889 } 890 } 891 } 892 893 894 bool VM_GetOrSetLocal::allow_nested_vm_operations() const { 895 return true; // May need to deoptimize 896 } 897 898 899 ///////////////////////////////////////////////////////////////////////////////////////// 900 901 // 902 // class JvmtiSuspendControl - see comments in jvmtiImpl.hpp 903 // 904 905 bool JvmtiSuspendControl::suspend(JavaThread *java_thread) { 906 // external suspend should have caught suspending a thread twice 907 908 // Immediate suspension required for JPDA back-end so JVMTI agent threads do 909 // not deadlock due to later suspension on transitions while holding 910 // raw monitors. Passing true causes the immediate suspension. 911 // java_suspend() will catch threads in the process of exiting 912 // and will ignore them. 913 java_thread->java_suspend(); 914 915 // It would be nice to have the following assertion in all the time, 916 // but it is possible for a racing resume request to have resumed 917 // this thread right after we suspended it. Temporarily enable this 918 // assertion if you are chasing a different kind of bug. 919 // 920 // assert(java_lang_Thread::thread(java_thread->threadObj()) == NULL || 921 // java_thread->is_being_ext_suspended(), "thread is not suspended"); 922 923 if (java_lang_Thread::thread(java_thread->threadObj()) == NULL) { 924 // check again because we can get delayed in java_suspend(): 925 // the thread is in process of exiting. 926 return false; 927 } 928 929 return true; 930 } 931 932 bool JvmtiSuspendControl::resume(JavaThread *java_thread) { 933 // external suspend should have caught resuming a thread twice 934 assert(java_thread->is_being_ext_suspended(), "thread should be suspended"); 935 936 // resume thread 937 { 938 // must always grab Threads_lock, see JVM_SuspendThread 939 MutexLocker ml(Threads_lock); 940 java_thread->java_resume(); 941 } 942 943 return true; 944 } 945 946 947 void JvmtiSuspendControl::print() { 948 #ifndef PRODUCT 949 MutexLocker mu(Threads_lock); 950 ResourceMark rm; 951 952 tty->print("Suspended Threads: ["); 953 for (JavaThread *thread = Threads::first(); thread != NULL; thread = thread->next()) { 954 #if JVMTI_TRACE 955 const char *name = JvmtiTrace::safe_get_thread_name(thread); 956 #else 957 const char *name = ""; 958 #endif /*JVMTI_TRACE */ 959 tty->print("%s(%c ", name, thread->is_being_ext_suspended() ? 'S' : '_'); 960 if (!thread->has_last_Java_frame()) { 961 tty->print("no stack"); 962 } 963 tty->print(") "); 964 } 965 tty->print_cr("]"); 966 #endif 967 }