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