1 /* 2 * Copyright (c) 2003, 2015, 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 "interpreter/oopMapCache.hpp" 29 #include "jvmtifiles/jvmtiEnv.hpp" 30 #include "memory/resourceArea.hpp" 31 #include "oops/instanceKlass.hpp" 32 #include "oops/oop.inline.hpp" 33 #include "prims/jvmtiAgentThread.hpp" 34 #include "prims/jvmtiEventController.inline.hpp" 35 #include "prims/jvmtiImpl.hpp" 36 #include "prims/jvmtiRedefineClasses.hpp" 37 #include "runtime/atomic.inline.hpp" 38 #include "runtime/deoptimization.hpp" 39 #include "runtime/handles.hpp" 40 #include "runtime/handles.inline.hpp" 41 #include "runtime/interfaceSupport.hpp" 42 #include "runtime/javaCalls.hpp" 43 #include "runtime/os.hpp" 44 #include "runtime/serviceThread.hpp" 45 #include "runtime/signature.hpp" 46 #include "runtime/thread.inline.hpp" 47 #include "runtime/vframe.hpp" 48 #include "runtime/vframe_hp.hpp" 49 #include "runtime/vm_operations.hpp" 50 #include "utilities/exceptions.hpp" 51 52 // 53 // class JvmtiAgentThread 54 // 55 // JavaThread used to wrap a thread started by an agent 56 // using the JVMTI method RunAgentThread. 57 // 58 59 JvmtiAgentThread::JvmtiAgentThread(JvmtiEnv* env, jvmtiStartFunction start_fn, const void *start_arg) 60 : JavaThread(start_function_wrapper) { 61 _env = env; 62 _start_fn = start_fn; 63 _start_arg = start_arg; 64 } 65 66 void 67 JvmtiAgentThread::start_function_wrapper(JavaThread *thread, TRAPS) { 68 // It is expected that any Agent threads will be created as 69 // Java Threads. If this is the case, notification of the creation 70 // of the thread is given in JavaThread::thread_main(). 71 assert(thread->is_Java_thread(), "debugger thread should be a Java Thread"); 72 assert(thread == JavaThread::current(), "sanity check"); 73 74 JvmtiAgentThread *dthread = (JvmtiAgentThread *)thread; 75 dthread->call_start_function(); 76 } 77 78 void 79 JvmtiAgentThread::call_start_function() { 80 ThreadToNativeFromVM transition(this); 81 _start_fn(_env->jvmti_external(), jni_environment(), (void*)_start_arg); 82 } 83 84 85 // 86 // class GrowableCache - private methods 87 // 88 89 void GrowableCache::recache() { 90 int len = _elements->length(); 91 92 FREE_C_HEAP_ARRAY(address, _cache); 93 _cache = NEW_C_HEAP_ARRAY(address,len+1, mtInternal); 94 95 for (int i=0; i<len; i++) { 96 _cache[i] = _elements->at(i)->getCacheValue(); 97 // 98 // The cache entry has gone bad. Without a valid frame pointer 99 // value, the entry is useless so we simply delete it in product 100 // mode. The call to remove() will rebuild the cache again 101 // without the bad entry. 102 // 103 if (_cache[i] == NULL) { 104 assert(false, "cannot recache NULL elements"); 105 remove(i); 106 return; 107 } 108 } 109 _cache[len] = NULL; 110 111 _listener_fun(_this_obj,_cache); 112 } 113 114 bool GrowableCache::equals(void* v, GrowableElement *e2) { 115 GrowableElement *e1 = (GrowableElement *) v; 116 assert(e1 != NULL, "e1 != NULL"); 117 assert(e2 != NULL, "e2 != NULL"); 118 119 return e1->equals(e2); 120 } 121 122 // 123 // class GrowableCache - public methods 124 // 125 126 GrowableCache::GrowableCache() { 127 _this_obj = NULL; 128 _listener_fun = NULL; 129 _elements = NULL; 130 _cache = NULL; 131 } 132 133 GrowableCache::~GrowableCache() { 134 clear(); 135 delete _elements; 136 FREE_C_HEAP_ARRAY(address, _cache); 137 } 138 139 void GrowableCache::initialize(void *this_obj, void listener_fun(void *, address*) ) { 140 _this_obj = this_obj; 141 _listener_fun = listener_fun; 142 _elements = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<GrowableElement*>(5,true); 143 recache(); 144 } 145 146 // number of elements in the collection 147 int GrowableCache::length() { 148 return _elements->length(); 149 } 150 151 // get the value of the index element in the collection 152 GrowableElement* GrowableCache::at(int index) { 153 GrowableElement *e = (GrowableElement *) _elements->at(index); 154 assert(e != NULL, "e != NULL"); 155 return e; 156 } 157 158 int GrowableCache::find(GrowableElement* e) { 159 return _elements->find(e, GrowableCache::equals); 160 } 161 162 // append a copy of the element to the end of the collection 163 void GrowableCache::append(GrowableElement* e) { 164 GrowableElement *new_e = e->clone(); 165 _elements->append(new_e); 166 recache(); 167 } 168 169 // insert a copy of the element using lessthan() 170 void GrowableCache::insert(GrowableElement* e) { 171 GrowableElement *new_e = e->clone(); 172 _elements->append(new_e); 173 174 int n = length()-2; 175 for (int i=n; i>=0; i--) { 176 GrowableElement *e1 = _elements->at(i); 177 GrowableElement *e2 = _elements->at(i+1); 178 if (e2->lessThan(e1)) { 179 _elements->at_put(i+1, e1); 180 _elements->at_put(i, e2); 181 } 182 } 183 184 recache(); 185 } 186 187 // remove the element at index 188 void GrowableCache::remove (int index) { 189 GrowableElement *e = _elements->at(index); 190 assert(e != NULL, "e != NULL"); 191 _elements->remove(e); 192 delete e; 193 recache(); 194 } 195 196 // clear out all elements, release all heap space and 197 // let our listener know that things have changed. 198 void GrowableCache::clear() { 199 int len = _elements->length(); 200 for (int i=0; i<len; i++) { 201 delete _elements->at(i); 202 } 203 _elements->clear(); 204 recache(); 205 } 206 207 void GrowableCache::oops_do(OopClosure* f) { 208 int len = _elements->length(); 209 for (int i=0; i<len; i++) { 210 GrowableElement *e = _elements->at(i); 211 e->oops_do(f); 212 } 213 } 214 215 void GrowableCache::metadata_do(void f(Metadata*)) { 216 int len = _elements->length(); 217 for (int i=0; i<len; i++) { 218 GrowableElement *e = _elements->at(i); 219 e->metadata_do(f); 220 } 221 } 222 223 void GrowableCache::gc_epilogue() { 224 int len = _elements->length(); 225 for (int i=0; i<len; i++) { 226 _cache[i] = _elements->at(i)->getCacheValue(); 227 } 228 } 229 230 // 231 // class JvmtiBreakpoint 232 // 233 234 JvmtiBreakpoint::JvmtiBreakpoint() { 235 _method = NULL; 236 _bci = 0; 237 _class_holder = NULL; 238 } 239 240 JvmtiBreakpoint::JvmtiBreakpoint(Method* m_method, jlocation location) { 241 _method = m_method; 242 _class_holder = _method->method_holder()->klass_holder(); 243 #ifdef CHECK_UNHANDLED_OOPS 244 // _class_holder can't be wrapped in a Handle, because JvmtiBreakpoints are 245 // sometimes allocated on the heap. 246 // 247 // The code handling JvmtiBreakpoints allocated on the stack can't be 248 // interrupted by a GC until _class_holder is reachable by the GC via the 249 // oops_do method. 250 Thread::current()->allow_unhandled_oop(&_class_holder); 251 #endif // CHECK_UNHANDLED_OOPS 252 assert(_method != NULL, "_method != NULL"); 253 _bci = (int) location; 254 assert(_bci >= 0, "_bci >= 0"); 255 } 256 257 void JvmtiBreakpoint::copy(JvmtiBreakpoint& bp) { 258 _method = bp._method; 259 _bci = bp._bci; 260 _class_holder = bp._class_holder; 261 } 262 263 bool JvmtiBreakpoint::lessThan(JvmtiBreakpoint& bp) { 264 Unimplemented(); 265 return false; 266 } 267 268 bool JvmtiBreakpoint::equals(JvmtiBreakpoint& bp) { 269 return _method == bp._method 270 && _bci == bp._bci; 271 } 272 273 bool JvmtiBreakpoint::is_valid() { 274 // class loader can be NULL 275 return _method != NULL && 276 _bci >= 0; 277 } 278 279 address JvmtiBreakpoint::getBcp() { 280 return _method->bcp_from(_bci); 281 } 282 283 void JvmtiBreakpoint::each_method_version_do(method_action meth_act) { 284 ((Method*)_method->*meth_act)(_bci); 285 286 // add/remove breakpoint to/from versions of the method that are EMCP. 287 Thread *thread = Thread::current(); 288 instanceKlassHandle ikh = instanceKlassHandle(thread, _method->method_holder()); 289 Symbol* m_name = _method->name(); 290 Symbol* m_signature = _method->signature(); 291 292 // search previous versions if they exist 293 for (InstanceKlass* pv_node = ikh->previous_versions(); 294 pv_node != NULL; 295 pv_node = pv_node->previous_versions()) { 296 Array<Method*>* methods = pv_node->methods(); 297 298 for (int i = methods->length() - 1; i >= 0; i--) { 299 Method* method = methods->at(i); 300 // Only set breakpoints in running EMCP methods. 301 if (method->is_running_emcp() && 302 method->name() == m_name && 303 method->signature() == m_signature) { 304 RC_TRACE(0x00000800, ("%sing breakpoint in %s(%s)", 305 meth_act == &Method::set_breakpoint ? "sett" : "clear", 306 method->name()->as_C_string(), 307 method->signature()->as_C_string())); 308 309 (method->*meth_act)(_bci); 310 break; 311 } 312 } 313 } 314 } 315 316 void JvmtiBreakpoint::set() { 317 each_method_version_do(&Method::set_breakpoint); 318 } 319 320 void JvmtiBreakpoint::clear() { 321 each_method_version_do(&Method::clear_breakpoint); 322 } 323 324 void JvmtiBreakpoint::print() { 325 #ifndef PRODUCT 326 const char *class_name = (_method == NULL) ? "NULL" : _method->klass_name()->as_C_string(); 327 const char *method_name = (_method == NULL) ? "NULL" : _method->name()->as_C_string(); 328 329 tty->print("Breakpoint(%s,%s,%d,%p)",class_name, method_name, _bci, getBcp()); 330 #endif 331 } 332 333 334 // 335 // class VM_ChangeBreakpoints 336 // 337 // Modify the Breakpoints data structure at a safepoint 338 // 339 340 void VM_ChangeBreakpoints::doit() { 341 switch (_operation) { 342 case SET_BREAKPOINT: 343 _breakpoints->set_at_safepoint(*_bp); 344 break; 345 case CLEAR_BREAKPOINT: 346 _breakpoints->clear_at_safepoint(*_bp); 347 break; 348 default: 349 assert(false, "Unknown operation"); 350 } 351 } 352 353 void VM_ChangeBreakpoints::oops_do(OopClosure* f) { 354 // The JvmtiBreakpoints in _breakpoints will be visited via 355 // JvmtiExport::oops_do. 356 if (_bp != NULL) { 357 _bp->oops_do(f); 358 } 359 } 360 361 void VM_ChangeBreakpoints::metadata_do(void f(Metadata*)) { 362 // Walk metadata in breakpoints to keep from being deallocated with RedefineClasses 363 if (_bp != NULL) { 364 _bp->metadata_do(f); 365 } 366 } 367 368 // 369 // class JvmtiBreakpoints 370 // 371 // a JVMTI internal collection of JvmtiBreakpoint 372 // 373 374 JvmtiBreakpoints::JvmtiBreakpoints(void listener_fun(void *,address *)) { 375 _bps.initialize(this,listener_fun); 376 } 377 378 JvmtiBreakpoints:: ~JvmtiBreakpoints() {} 379 380 void JvmtiBreakpoints::oops_do(OopClosure* f) { 381 _bps.oops_do(f); 382 } 383 384 void JvmtiBreakpoints::metadata_do(void f(Metadata*)) { 385 _bps.metadata_do(f); 386 } 387 388 void JvmtiBreakpoints::gc_epilogue() { 389 _bps.gc_epilogue(); 390 } 391 392 void JvmtiBreakpoints::print() { 393 #ifndef PRODUCT 394 ResourceMark rm; 395 396 int n = _bps.length(); 397 for (int i=0; i<n; i++) { 398 JvmtiBreakpoint& bp = _bps.at(i); 399 tty->print("%d: ", i); 400 bp.print(); 401 tty->cr(); 402 } 403 #endif 404 } 405 406 407 void JvmtiBreakpoints::set_at_safepoint(JvmtiBreakpoint& bp) { 408 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 409 410 int i = _bps.find(bp); 411 if (i == -1) { 412 _bps.append(bp); 413 bp.set(); 414 } 415 } 416 417 void JvmtiBreakpoints::clear_at_safepoint(JvmtiBreakpoint& bp) { 418 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 419 420 int i = _bps.find(bp); 421 if (i != -1) { 422 _bps.remove(i); 423 bp.clear(); 424 } 425 } 426 427 int JvmtiBreakpoints::length() { return _bps.length(); } 428 429 int JvmtiBreakpoints::set(JvmtiBreakpoint& bp) { 430 if ( _bps.find(bp) != -1) { 431 return JVMTI_ERROR_DUPLICATE; 432 } 433 VM_ChangeBreakpoints set_breakpoint(VM_ChangeBreakpoints::SET_BREAKPOINT, &bp); 434 VMThread::execute(&set_breakpoint); 435 return JVMTI_ERROR_NONE; 436 } 437 438 int JvmtiBreakpoints::clear(JvmtiBreakpoint& bp) { 439 if ( _bps.find(bp) == -1) { 440 return JVMTI_ERROR_NOT_FOUND; 441 } 442 443 VM_ChangeBreakpoints clear_breakpoint(VM_ChangeBreakpoints::CLEAR_BREAKPOINT, &bp); 444 VMThread::execute(&clear_breakpoint); 445 return JVMTI_ERROR_NONE; 446 } 447 448 void JvmtiBreakpoints::clearall_in_class_at_safepoint(Klass* klass) { 449 bool changed = true; 450 // We are going to run thru the list of bkpts 451 // and delete some. This deletion probably alters 452 // the list in some implementation defined way such 453 // that when we delete entry i, the next entry might 454 // no longer be at i+1. To be safe, each time we delete 455 // an entry, we'll just start again from the beginning. 456 // We'll stop when we make a pass thru the whole list without 457 // deleting anything. 458 while (changed) { 459 int len = _bps.length(); 460 changed = false; 461 for (int i = 0; i < len; i++) { 462 JvmtiBreakpoint& bp = _bps.at(i); 463 if (bp.method()->method_holder() == klass) { 464 bp.clear(); 465 _bps.remove(i); 466 // This changed 'i' so we have to start over. 467 changed = true; 468 break; 469 } 470 } 471 } 472 } 473 474 // 475 // class JvmtiCurrentBreakpoints 476 // 477 478 JvmtiBreakpoints *JvmtiCurrentBreakpoints::_jvmti_breakpoints = NULL; 479 address * JvmtiCurrentBreakpoints::_breakpoint_list = NULL; 480 481 482 JvmtiBreakpoints& JvmtiCurrentBreakpoints::get_jvmti_breakpoints() { 483 if (_jvmti_breakpoints != NULL) return (*_jvmti_breakpoints); 484 _jvmti_breakpoints = new JvmtiBreakpoints(listener_fun); 485 assert(_jvmti_breakpoints != NULL, "_jvmti_breakpoints != NULL"); 486 return (*_jvmti_breakpoints); 487 } 488 489 void JvmtiCurrentBreakpoints::listener_fun(void *this_obj, address *cache) { 490 JvmtiBreakpoints *this_jvmti = (JvmtiBreakpoints *) this_obj; 491 assert(this_jvmti != NULL, "this_jvmti != NULL"); 492 493 debug_only(int n = this_jvmti->length();); 494 assert(cache[n] == NULL, "cache must be NULL terminated"); 495 496 set_breakpoint_list(cache); 497 } 498 499 500 void JvmtiCurrentBreakpoints::oops_do(OopClosure* f) { 501 if (_jvmti_breakpoints != NULL) { 502 _jvmti_breakpoints->oops_do(f); 503 } 504 } 505 506 void JvmtiCurrentBreakpoints::metadata_do(void f(Metadata*)) { 507 if (_jvmti_breakpoints != NULL) { 508 _jvmti_breakpoints->metadata_do(f); 509 } 510 } 511 512 void JvmtiCurrentBreakpoints::gc_epilogue() { 513 if (_jvmti_breakpoints != NULL) { 514 _jvmti_breakpoints->gc_epilogue(); 515 } 516 } 517 518 /////////////////////////////////////////////////////////////// 519 // 520 // class VM_GetOrSetLocal 521 // 522 523 // Constructor for non-object getter 524 VM_GetOrSetLocal::VM_GetOrSetLocal(JavaThread* thread, jint depth, int index, BasicType type) 525 : _thread(thread) 526 , _calling_thread(NULL) 527 , _depth(depth) 528 , _index(index) 529 , _type(type) 530 , _set(false) 531 , _jvf(NULL) 532 , _result(JVMTI_ERROR_NONE) 533 { 534 } 535 536 // Constructor for object or non-object setter 537 VM_GetOrSetLocal::VM_GetOrSetLocal(JavaThread* thread, jint depth, int index, BasicType type, jvalue value) 538 : _thread(thread) 539 , _calling_thread(NULL) 540 , _depth(depth) 541 , _index(index) 542 , _type(type) 543 , _value(value) 544 , _set(true) 545 , _jvf(NULL) 546 , _result(JVMTI_ERROR_NONE) 547 { 548 } 549 550 // Constructor for object getter 551 VM_GetOrSetLocal::VM_GetOrSetLocal(JavaThread* thread, JavaThread* calling_thread, jint depth, int index) 552 : _thread(thread) 553 , _calling_thread(calling_thread) 554 , _depth(depth) 555 , _index(index) 556 , _type(T_OBJECT) 557 , _set(false) 558 , _jvf(NULL) 559 , _result(JVMTI_ERROR_NONE) 560 { 561 } 562 563 vframe *VM_GetOrSetLocal::get_vframe() { 564 if (!_thread->has_last_Java_frame()) { 565 return NULL; 566 } 567 RegisterMap reg_map(_thread); 568 vframe *vf = _thread->last_java_vframe(®_map); 569 int d = 0; 570 while ((vf != NULL) && (d < _depth)) { 571 vf = vf->java_sender(); 572 d++; 573 } 574 return vf; 575 } 576 577 javaVFrame *VM_GetOrSetLocal::get_java_vframe() { 578 vframe* vf = get_vframe(); 579 if (vf == NULL) { 580 _result = JVMTI_ERROR_NO_MORE_FRAMES; 581 return NULL; 582 } 583 javaVFrame *jvf = (javaVFrame*)vf; 584 585 if (!vf->is_java_frame()) { 586 _result = JVMTI_ERROR_OPAQUE_FRAME; 587 return NULL; 588 } 589 return jvf; 590 } 591 592 // Check that the klass is assignable to a type with the given signature. 593 // Another solution could be to use the function Klass::is_subtype_of(type). 594 // But the type class can be forced to load/initialize eagerly in such a case. 595 // This may cause unexpected consequences like CFLH or class-init JVMTI events. 596 // It is better to avoid such a behavior. 597 bool VM_GetOrSetLocal::is_assignable(const char* ty_sign, Klass* klass, Thread* thread) { 598 assert(ty_sign != NULL, "type signature must not be NULL"); 599 assert(thread != NULL, "thread must not be NULL"); 600 assert(klass != NULL, "klass must not be NULL"); 601 602 int len = (int) strlen(ty_sign); 603 if (ty_sign[0] == 'L' && ty_sign[len-1] == ';') { // Need pure class/interface name 604 ty_sign++; 605 len -= 2; 606 } 607 TempNewSymbol ty_sym = SymbolTable::new_symbol(ty_sign, len, thread); 608 if (klass->name() == ty_sym) { 609 return true; 610 } 611 // Compare primary supers 612 int super_depth = klass->super_depth(); 613 int idx; 614 for (idx = 0; idx < super_depth; idx++) { 615 if (klass->primary_super_of_depth(idx)->name() == ty_sym) { 616 return true; 617 } 618 } 619 // Compare secondary supers 620 Array<Klass*>* sec_supers = klass->secondary_supers(); 621 for (idx = 0; idx < sec_supers->length(); idx++) { 622 if (((Klass*) sec_supers->at(idx))->name() == ty_sym) { 623 return true; 624 } 625 } 626 return false; 627 } 628 629 // Checks error conditions: 630 // JVMTI_ERROR_INVALID_SLOT 631 // JVMTI_ERROR_TYPE_MISMATCH 632 // Returns: 'true' - everything is Ok, 'false' - error code 633 634 bool VM_GetOrSetLocal::check_slot_type(javaVFrame* jvf) { 635 Method* method_oop = jvf->method(); 636 if (!method_oop->has_localvariable_table()) { 637 // Just to check index boundaries 638 jint extra_slot = (_type == T_LONG || _type == T_DOUBLE) ? 1 : 0; 639 if (_index < 0 || _index + extra_slot >= method_oop->max_locals()) { 640 _result = JVMTI_ERROR_INVALID_SLOT; 641 return false; 642 } 643 return true; 644 } 645 646 jint num_entries = method_oop->localvariable_table_length(); 647 if (num_entries == 0) { 648 _result = JVMTI_ERROR_INVALID_SLOT; 649 return false; // There are no slots 650 } 651 int signature_idx = -1; 652 int vf_bci = jvf->bci(); 653 LocalVariableTableElement* table = method_oop->localvariable_table_start(); 654 for (int i = 0; i < num_entries; i++) { 655 int start_bci = table[i].start_bci; 656 int end_bci = start_bci + table[i].length; 657 658 // Here we assume that locations of LVT entries 659 // with the same slot number cannot be overlapped 660 if (_index == (jint) table[i].slot && start_bci <= vf_bci && vf_bci <= end_bci) { 661 signature_idx = (int) table[i].descriptor_cp_index; 662 break; 663 } 664 } 665 if (signature_idx == -1) { 666 _result = JVMTI_ERROR_INVALID_SLOT; 667 return false; // Incorrect slot index 668 } 669 Symbol* sign_sym = method_oop->constants()->symbol_at(signature_idx); 670 const char* signature = (const char *) sign_sym->as_utf8(); 671 BasicType slot_type = char2type(signature[0]); 672 673 switch (slot_type) { 674 case T_BYTE: 675 case T_SHORT: 676 case T_CHAR: 677 case T_BOOLEAN: 678 slot_type = T_INT; 679 break; 680 case T_ARRAY: 681 slot_type = T_OBJECT; 682 break; 683 }; 684 if (_type != slot_type) { 685 _result = JVMTI_ERROR_TYPE_MISMATCH; 686 return false; 687 } 688 689 jobject jobj = _value.l; 690 if (_set && slot_type == T_OBJECT && jobj != NULL) { // NULL reference is allowed 691 // Check that the jobject class matches the return type signature. 692 JavaThread* cur_thread = JavaThread::current(); 693 HandleMark hm(cur_thread); 694 695 Handle obj = Handle(cur_thread, JNIHandles::resolve_external_guard(jobj)); 696 NULL_CHECK(obj, (_result = JVMTI_ERROR_INVALID_OBJECT, false)); 697 KlassHandle ob_kh = KlassHandle(cur_thread, obj->klass()); 698 NULL_CHECK(ob_kh, (_result = JVMTI_ERROR_INVALID_OBJECT, false)); 699 700 if (!is_assignable(signature, ob_kh(), cur_thread)) { 701 _result = JVMTI_ERROR_TYPE_MISMATCH; 702 return false; 703 } 704 } 705 return true; 706 } 707 708 static bool can_be_deoptimized(vframe* vf) { 709 return (vf->is_compiled_frame() && vf->fr().can_be_deoptimized()); 710 } 711 712 bool VM_GetOrSetLocal::doit_prologue() { 713 _jvf = get_java_vframe(); 714 NULL_CHECK(_jvf, false); 715 716 if (_jvf->method()->is_native()) { 717 if (getting_receiver() && !_jvf->method()->is_static()) { 718 return true; 719 } else { 720 _result = JVMTI_ERROR_OPAQUE_FRAME; 721 return false; 722 } 723 } 724 725 if (!check_slot_type(_jvf)) { 726 return false; 727 } 728 return true; 729 } 730 731 void VM_GetOrSetLocal::doit() { 732 InterpreterOopMap oop_mask; 733 _jvf->method()->mask_for(_jvf->bci(), &oop_mask); 734 if (oop_mask.is_dead(_index)) { 735 // The local can be invalid and uninitialized in the scope of current bci 736 _result = JVMTI_ERROR_INVALID_SLOT; 737 return; 738 } 739 if (_set) { 740 // Force deoptimization of frame if compiled because it's 741 // possible the compiler emitted some locals as constant values, 742 // meaning they are not mutable. 743 if (can_be_deoptimized(_jvf)) { 744 745 // Schedule deoptimization so that eventually the local 746 // update will be written to an interpreter frame. 747 Deoptimization::deoptimize_frame(_jvf->thread(), _jvf->fr().id()); 748 749 // Now store a new value for the local which will be applied 750 // once deoptimization occurs. Note however that while this 751 // write is deferred until deoptimization actually happens 752 // can vframe created after this point will have its locals 753 // reflecting this update so as far as anyone can see the 754 // write has already taken place. 755 756 // If we are updating an oop then get the oop from the handle 757 // since the handle will be long gone by the time the deopt 758 // happens. The oop stored in the deferred local will be 759 // gc'd on its own. 760 if (_type == T_OBJECT) { 761 _value.l = (jobject) (JNIHandles::resolve_external_guard(_value.l)); 762 } 763 // Re-read the vframe so we can see that it is deoptimized 764 // [ Only need because of assert in update_local() ] 765 _jvf = get_java_vframe(); 766 ((compiledVFrame*)_jvf)->update_local(_type, _index, _value); 767 return; 768 } 769 StackValueCollection *locals = _jvf->locals(); 770 HandleMark hm; 771 772 switch (_type) { 773 case T_INT: locals->set_int_at (_index, _value.i); break; 774 case T_LONG: locals->set_long_at (_index, _value.j); break; 775 case T_FLOAT: locals->set_float_at (_index, _value.f); break; 776 case T_DOUBLE: locals->set_double_at(_index, _value.d); break; 777 case T_OBJECT: { 778 Handle ob_h(JNIHandles::resolve_external_guard(_value.l)); 779 locals->set_obj_at (_index, ob_h); 780 break; 781 } 782 default: ShouldNotReachHere(); 783 } 784 _jvf->set_locals(locals); 785 } else { 786 if (_jvf->method()->is_native() && _jvf->is_compiled_frame()) { 787 assert(getting_receiver(), "Can only get here when getting receiver"); 788 oop receiver = _jvf->fr().get_native_receiver(); 789 _value.l = JNIHandles::make_local(_calling_thread, receiver); 790 } else { 791 StackValueCollection *locals = _jvf->locals(); 792 793 if (locals->at(_index)->type() == T_CONFLICT) { 794 memset(&_value, 0, sizeof(_value)); 795 _value.l = NULL; 796 return; 797 } 798 799 switch (_type) { 800 case T_INT: _value.i = locals->int_at (_index); break; 801 case T_LONG: _value.j = locals->long_at (_index); break; 802 case T_FLOAT: _value.f = locals->float_at (_index); break; 803 case T_DOUBLE: _value.d = locals->double_at(_index); break; 804 case T_OBJECT: { 805 // Wrap the oop to be returned in a local JNI handle since 806 // oops_do() no longer applies after doit() is finished. 807 oop obj = locals->obj_at(_index)(); 808 _value.l = JNIHandles::make_local(_calling_thread, obj); 809 break; 810 } 811 default: ShouldNotReachHere(); 812 } 813 } 814 } 815 } 816 817 818 bool VM_GetOrSetLocal::allow_nested_vm_operations() const { 819 return true; // May need to deoptimize 820 } 821 822 823 VM_GetReceiver::VM_GetReceiver( 824 JavaThread* thread, JavaThread* caller_thread, jint depth) 825 : VM_GetOrSetLocal(thread, caller_thread, depth, 0) {} 826 827 ///////////////////////////////////////////////////////////////////////////////////////// 828 829 // 830 // class JvmtiSuspendControl - see comments in jvmtiImpl.hpp 831 // 832 833 bool JvmtiSuspendControl::suspend(JavaThread *java_thread) { 834 // external suspend should have caught suspending a thread twice 835 836 // Immediate suspension required for JPDA back-end so JVMTI agent threads do 837 // not deadlock due to later suspension on transitions while holding 838 // raw monitors. Passing true causes the immediate suspension. 839 // java_suspend() will catch threads in the process of exiting 840 // and will ignore them. 841 java_thread->java_suspend(); 842 843 // It would be nice to have the following assertion in all the time, 844 // but it is possible for a racing resume request to have resumed 845 // this thread right after we suspended it. Temporarily enable this 846 // assertion if you are chasing a different kind of bug. 847 // 848 // assert(java_lang_Thread::thread(java_thread->threadObj()) == NULL || 849 // java_thread->is_being_ext_suspended(), "thread is not suspended"); 850 851 if (java_lang_Thread::thread(java_thread->threadObj()) == NULL) { 852 // check again because we can get delayed in java_suspend(): 853 // the thread is in process of exiting. 854 return false; 855 } 856 857 return true; 858 } 859 860 bool JvmtiSuspendControl::resume(JavaThread *java_thread) { 861 // external suspend should have caught resuming a thread twice 862 assert(java_thread->is_being_ext_suspended(), "thread should be suspended"); 863 864 // resume thread 865 { 866 // must always grab Threads_lock, see JVM_SuspendThread 867 MutexLocker ml(Threads_lock); 868 java_thread->java_resume(); 869 } 870 871 return true; 872 } 873 874 875 void JvmtiSuspendControl::print() { 876 #ifndef PRODUCT 877 MutexLocker mu(Threads_lock); 878 ResourceMark rm; 879 880 tty->print("Suspended Threads: ["); 881 for (JavaThread *thread = Threads::first(); thread != NULL; thread = thread->next()) { 882 #ifdef JVMTI_TRACE 883 const char *name = JvmtiTrace::safe_get_thread_name(thread); 884 #else 885 const char *name = ""; 886 #endif /*JVMTI_TRACE */ 887 tty->print("%s(%c ", name, thread->is_being_ext_suspended() ? 'S' : '_'); 888 if (!thread->has_last_Java_frame()) { 889 tty->print("no stack"); 890 } 891 tty->print(") "); 892 } 893 tty->print_cr("]"); 894 #endif 895 } 896 897 JvmtiDeferredEvent JvmtiDeferredEvent::compiled_method_load_event( 898 nmethod* nm) { 899 JvmtiDeferredEvent event = JvmtiDeferredEvent(TYPE_COMPILED_METHOD_LOAD); 900 event._event_data.compiled_method_load = nm; 901 // Keep the nmethod alive until the ServiceThread can process 902 // this deferred event. 903 nmethodLocker::lock_nmethod(nm); 904 return event; 905 } 906 907 JvmtiDeferredEvent JvmtiDeferredEvent::compiled_method_unload_event( 908 nmethod* nm, jmethodID id, const void* code) { 909 JvmtiDeferredEvent event = JvmtiDeferredEvent(TYPE_COMPILED_METHOD_UNLOAD); 910 event._event_data.compiled_method_unload.nm = nm; 911 event._event_data.compiled_method_unload.method_id = id; 912 event._event_data.compiled_method_unload.code_begin = code; 913 // Keep the nmethod alive until the ServiceThread can process 914 // this deferred event. This will keep the memory for the 915 // generated code from being reused too early. We pass 916 // zombie_ok == true here so that our nmethod that was just 917 // made into a zombie can be locked. 918 nmethodLocker::lock_nmethod(nm, true /* zombie_ok */); 919 return event; 920 } 921 922 JvmtiDeferredEvent JvmtiDeferredEvent::dynamic_code_generated_event( 923 const char* name, const void* code_begin, const void* code_end) { 924 JvmtiDeferredEvent event = JvmtiDeferredEvent(TYPE_DYNAMIC_CODE_GENERATED); 925 // Need to make a copy of the name since we don't know how long 926 // the event poster will keep it around after we enqueue the 927 // deferred event and return. strdup() failure is handled in 928 // the post() routine below. 929 event._event_data.dynamic_code_generated.name = os::strdup(name); 930 event._event_data.dynamic_code_generated.code_begin = code_begin; 931 event._event_data.dynamic_code_generated.code_end = code_end; 932 return event; 933 } 934 935 void JvmtiDeferredEvent::post() { 936 assert(ServiceThread::is_service_thread(Thread::current()), 937 "Service thread must post enqueued events"); 938 switch(_type) { 939 case TYPE_COMPILED_METHOD_LOAD: { 940 nmethod* nm = _event_data.compiled_method_load; 941 JvmtiExport::post_compiled_method_load(nm); 942 // done with the deferred event so unlock the nmethod 943 nmethodLocker::unlock_nmethod(nm); 944 break; 945 } 946 case TYPE_COMPILED_METHOD_UNLOAD: { 947 nmethod* nm = _event_data.compiled_method_unload.nm; 948 JvmtiExport::post_compiled_method_unload( 949 _event_data.compiled_method_unload.method_id, 950 _event_data.compiled_method_unload.code_begin); 951 // done with the deferred event so unlock the nmethod 952 nmethodLocker::unlock_nmethod(nm); 953 break; 954 } 955 case TYPE_DYNAMIC_CODE_GENERATED: { 956 JvmtiExport::post_dynamic_code_generated_internal( 957 // if strdup failed give the event a default name 958 (_event_data.dynamic_code_generated.name == NULL) 959 ? "unknown_code" : _event_data.dynamic_code_generated.name, 960 _event_data.dynamic_code_generated.code_begin, 961 _event_data.dynamic_code_generated.code_end); 962 if (_event_data.dynamic_code_generated.name != NULL) { 963 // release our copy 964 os::free((void *)_event_data.dynamic_code_generated.name); 965 } 966 break; 967 } 968 default: 969 ShouldNotReachHere(); 970 } 971 } 972 973 JvmtiDeferredEventQueue::QueueNode* JvmtiDeferredEventQueue::_queue_tail = NULL; 974 JvmtiDeferredEventQueue::QueueNode* JvmtiDeferredEventQueue::_queue_head = NULL; 975 976 volatile JvmtiDeferredEventQueue::QueueNode* 977 JvmtiDeferredEventQueue::_pending_list = NULL; 978 979 bool JvmtiDeferredEventQueue::has_events() { 980 assert(Service_lock->owned_by_self(), "Must own Service_lock"); 981 return _queue_head != NULL || _pending_list != NULL; 982 } 983 984 void JvmtiDeferredEventQueue::enqueue(const JvmtiDeferredEvent& event) { 985 assert(Service_lock->owned_by_self(), "Must own Service_lock"); 986 987 process_pending_events(); 988 989 // Events get added to the end of the queue (and are pulled off the front). 990 QueueNode* node = new QueueNode(event); 991 if (_queue_tail == NULL) { 992 _queue_tail = _queue_head = node; 993 } else { 994 assert(_queue_tail->next() == NULL, "Must be the last element in the list"); 995 _queue_tail->set_next(node); 996 _queue_tail = node; 997 } 998 999 Service_lock->notify_all(); 1000 assert((_queue_head == NULL) == (_queue_tail == NULL), 1001 "Inconsistent queue markers"); 1002 } 1003 1004 JvmtiDeferredEvent JvmtiDeferredEventQueue::dequeue() { 1005 assert(Service_lock->owned_by_self(), "Must own Service_lock"); 1006 1007 process_pending_events(); 1008 1009 assert(_queue_head != NULL, "Nothing to dequeue"); 1010 1011 if (_queue_head == NULL) { 1012 // Just in case this happens in product; it shouldn't but let's not crash 1013 return JvmtiDeferredEvent(); 1014 } 1015 1016 QueueNode* node = _queue_head; 1017 _queue_head = _queue_head->next(); 1018 if (_queue_head == NULL) { 1019 _queue_tail = NULL; 1020 } 1021 1022 assert((_queue_head == NULL) == (_queue_tail == NULL), 1023 "Inconsistent queue markers"); 1024 1025 JvmtiDeferredEvent event = node->event(); 1026 delete node; 1027 return event; 1028 } 1029 1030 void JvmtiDeferredEventQueue::add_pending_event( 1031 const JvmtiDeferredEvent& event) { 1032 1033 QueueNode* node = new QueueNode(event); 1034 1035 bool success = false; 1036 QueueNode* prev_value = (QueueNode*)_pending_list; 1037 do { 1038 node->set_next(prev_value); 1039 prev_value = (QueueNode*)Atomic::cmpxchg_ptr( 1040 (void*)node, (volatile void*)&_pending_list, (void*)node->next()); 1041 } while (prev_value != node->next()); 1042 } 1043 1044 // This method transfers any events that were added by someone NOT holding 1045 // the lock into the mainline queue. 1046 void JvmtiDeferredEventQueue::process_pending_events() { 1047 assert(Service_lock->owned_by_self(), "Must own Service_lock"); 1048 1049 if (_pending_list != NULL) { 1050 QueueNode* head = 1051 (QueueNode*)Atomic::xchg_ptr(NULL, (volatile void*)&_pending_list); 1052 1053 assert((_queue_head == NULL) == (_queue_tail == NULL), 1054 "Inconsistent queue markers"); 1055 1056 if (head != NULL) { 1057 // Since we've treated the pending list as a stack (with newer 1058 // events at the beginning), we need to join the bottom of the stack 1059 // with the 'tail' of the queue in order to get the events in the 1060 // right order. We do this by reversing the pending list and appending 1061 // it to the queue. 1062 1063 QueueNode* new_tail = head; 1064 QueueNode* new_head = NULL; 1065 1066 // This reverses the list 1067 QueueNode* prev = new_tail; 1068 QueueNode* node = new_tail->next(); 1069 new_tail->set_next(NULL); 1070 while (node != NULL) { 1071 QueueNode* next = node->next(); 1072 node->set_next(prev); 1073 prev = node; 1074 node = next; 1075 } 1076 new_head = prev; 1077 1078 // Now append the new list to the queue 1079 if (_queue_tail != NULL) { 1080 _queue_tail->set_next(new_head); 1081 } else { // _queue_head == NULL 1082 _queue_head = new_head; 1083 } 1084 _queue_tail = new_tail; 1085 } 1086 } 1087 }