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