1 /* 2 * Copyright (c) 1997, 2024, 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/moduleEntry.hpp" 27 #include "code/codeCache.hpp" 28 #include "code/scopeDesc.hpp" 29 #include "code/vmreg.inline.hpp" 30 #include "compiler/abstractCompiler.hpp" 31 #include "compiler/disassembler.hpp" 32 #include "compiler/oopMap.hpp" 33 #include "gc/shared/collectedHeap.inline.hpp" 34 #include "interpreter/interpreter.hpp" 35 #include "interpreter/oopMapCache.hpp" 36 #include "logging/log.hpp" 37 #include "memory/resourceArea.hpp" 38 #include "memory/universe.hpp" 39 #include "oops/markWord.hpp" 40 #include "oops/method.inline.hpp" 41 #include "oops/methodData.hpp" 42 #include "oops/oop.inline.hpp" 43 #include "oops/inlineKlass.hpp" 44 #include "oops/stackChunkOop.inline.hpp" 45 #include "oops/verifyOopClosure.hpp" 46 #include "prims/methodHandles.hpp" 47 #include "runtime/continuation.hpp" 48 #include "runtime/continuationEntry.inline.hpp" 49 #include "runtime/frame.inline.hpp" 50 #include "runtime/handles.inline.hpp" 51 #include "runtime/javaCalls.hpp" 52 #include "runtime/javaThread.hpp" 53 #include "runtime/monitorChunk.hpp" 54 #include "runtime/os.hpp" 55 #include "runtime/sharedRuntime.hpp" 56 #include "runtime/safefetch.hpp" 57 #include "runtime/signature.hpp" 58 #include "runtime/stackValue.hpp" 59 #include "runtime/stubCodeGenerator.hpp" 60 #include "runtime/stubRoutines.hpp" 61 #include "utilities/debug.hpp" 62 #include "utilities/decoder.hpp" 63 #include "utilities/formatBuffer.hpp" 64 #ifdef COMPILER1 65 #include "c1/c1_Runtime1.hpp" 66 #endif 67 68 RegisterMap::RegisterMap(JavaThread *thread, UpdateMap update_map, ProcessFrames process_frames, WalkContinuation walk_cont) { 69 _thread = thread; 70 _update_map = update_map == UpdateMap::include; 71 _process_frames = process_frames == ProcessFrames::include; 72 _walk_cont = walk_cont == WalkContinuation::include; 73 clear(); 74 DEBUG_ONLY (_update_for_id = nullptr;) 75 NOT_PRODUCT(_skip_missing = false;) 76 NOT_PRODUCT(_async = false;) 77 78 if (walk_cont == WalkContinuation::include && thread != nullptr && thread->last_continuation() != nullptr) { 79 _chunk = stackChunkHandle(Thread::current()->handle_area()->allocate_null_handle(), true /* dummy */); 80 } 81 _chunk_index = -1; 82 83 #ifndef PRODUCT 84 for (int i = 0; i < reg_count ; i++ ) _location[i] = nullptr; 85 #endif /* PRODUCT */ 86 } 87 88 RegisterMap::RegisterMap(oop continuation, UpdateMap update_map) { 89 _thread = nullptr; 90 _update_map = update_map == UpdateMap::include; 91 _process_frames = false; 92 _walk_cont = true; 93 clear(); 94 DEBUG_ONLY (_update_for_id = nullptr;) 95 NOT_PRODUCT(_skip_missing = false;) 96 NOT_PRODUCT(_async = false;) 97 98 _chunk = stackChunkHandle(Thread::current()->handle_area()->allocate_null_handle(), true /* dummy */); 99 _chunk_index = -1; 100 101 #ifndef PRODUCT 102 for (int i = 0; i < reg_count ; i++ ) _location[i] = nullptr; 103 #endif /* PRODUCT */ 104 } 105 106 RegisterMap::RegisterMap(const RegisterMap* map) { 107 assert(map != this, "bad initialization parameter"); 108 assert(map != nullptr, "RegisterMap must be present"); 109 _thread = map->thread(); 110 _update_map = map->update_map(); 111 _process_frames = map->process_frames(); 112 _walk_cont = map->_walk_cont; 113 _include_argument_oops = map->include_argument_oops(); 114 DEBUG_ONLY (_update_for_id = map->_update_for_id;) 115 NOT_PRODUCT(_skip_missing = map->_skip_missing;) 116 NOT_PRODUCT(_async = map->_async;) 117 118 // only the original RegisterMap's handle lives long enough for StackWalker; this is bound to cause trouble with nested continuations. 119 _chunk = map->_chunk; 120 _chunk_index = map->_chunk_index; 121 122 pd_initialize_from(map); 123 if (update_map()) { 124 for(int i = 0; i < location_valid_size; i++) { 125 LocationValidType bits = map->_location_valid[i]; 126 _location_valid[i] = bits; 127 // for whichever bits are set, pull in the corresponding map->_location 128 int j = i*location_valid_type_size; 129 while (bits != 0) { 130 if ((bits & 1) != 0) { 131 assert(0 <= j && j < reg_count, "range check"); 132 _location[j] = map->_location[j]; 133 } 134 bits >>= 1; 135 j += 1; 136 } 137 } 138 } 139 } 140 141 oop RegisterMap::cont() const { 142 return _chunk() != nullptr ? _chunk()->cont() : (oop)nullptr; 143 } 144 145 void RegisterMap::set_stack_chunk(stackChunkOop chunk) { 146 assert(chunk == nullptr || _walk_cont, ""); 147 assert(chunk == nullptr || _chunk.not_null(), ""); 148 if (_chunk.is_null()) return; 149 log_trace(continuations)("set_stack_chunk: " INTPTR_FORMAT " this: " INTPTR_FORMAT, p2i((oopDesc*)chunk), p2i(this)); 150 _chunk.replace(chunk); // reuse handle. see comment above in the constructor 151 if (chunk == nullptr) { 152 _chunk_index = -1; 153 } else { 154 _chunk_index++; 155 } 156 } 157 158 void RegisterMap::clear() { 159 set_include_argument_oops(true); 160 if (update_map()) { 161 for(int i = 0; i < location_valid_size; i++) { 162 _location_valid[i] = 0; 163 } 164 pd_clear(); 165 } else { 166 pd_initialize(); 167 } 168 } 169 170 #ifndef PRODUCT 171 172 VMReg RegisterMap::find_register_spilled_here(void* p, intptr_t* sp) { 173 for(int i = 0; i < RegisterMap::reg_count; i++) { 174 VMReg r = VMRegImpl::as_VMReg(i); 175 if (p == location(r, sp)) return r; 176 } 177 return nullptr; 178 } 179 180 void RegisterMap::print_on(outputStream* st) const { 181 st->print_cr("Register map"); 182 for(int i = 0; i < reg_count; i++) { 183 184 VMReg r = VMRegImpl::as_VMReg(i); 185 intptr_t* src = (intptr_t*) location(r, nullptr); 186 if (src != nullptr) { 187 188 r->print_on(st); 189 st->print(" [" INTPTR_FORMAT "] = ", p2i(src)); 190 if (((uintptr_t)src & (sizeof(*src)-1)) != 0) { 191 st->print_cr("<misaligned>"); 192 } else { 193 st->print_cr(INTPTR_FORMAT, *src); 194 } 195 } 196 } 197 } 198 199 void RegisterMap::print() const { 200 print_on(tty); 201 } 202 203 #endif 204 // This returns the pc that if you were in the debugger you'd see. Not 205 // the idealized value in the frame object. This undoes the magic conversion 206 // that happens for deoptimized frames. In addition it makes the value the 207 // hardware would want to see in the native frame. The only user (at this point) 208 // is deoptimization. It likely no one else should ever use it. 209 210 address frame::raw_pc() const { 211 if (is_deoptimized_frame()) { 212 nmethod* nm = cb()->as_nmethod_or_null(); 213 assert(nm != nullptr, "only nmethod is expected here"); 214 if (nm->is_method_handle_return(pc())) 215 return nm->deopt_mh_handler_begin() - pc_return_offset; 216 else 217 return nm->deopt_handler_begin() - pc_return_offset; 218 } else { 219 return (pc() - pc_return_offset); 220 } 221 } 222 223 // Change the pc in a frame object. This does not change the actual pc in 224 // actual frame. To do that use patch_pc. 225 // 226 void frame::set_pc(address newpc) { 227 #ifdef ASSERT 228 if (_cb != nullptr && _cb->is_nmethod()) { 229 assert(!((nmethod*)_cb)->is_deopt_pc(_pc), "invariant violation"); 230 } 231 #endif // ASSERT 232 233 // Unsafe to use the is_deoptimized tester after changing pc 234 _deopt_state = unknown; 235 _pc = newpc; 236 _cb = CodeCache::find_blob(_pc); 237 238 } 239 240 // type testers 241 bool frame::is_ignored_frame() const { 242 return false; // FIXME: some LambdaForm frames should be ignored 243 } 244 245 bool frame::is_native_frame() const { 246 return (_cb != nullptr && 247 _cb->is_nmethod() && 248 ((nmethod*)_cb)->is_native_method()); 249 } 250 251 bool frame::is_java_frame() const { 252 if (is_interpreted_frame()) return true; 253 if (is_compiled_frame()) return true; 254 return false; 255 } 256 257 bool frame::is_runtime_frame() const { 258 return (_cb != nullptr && _cb->is_runtime_stub()); 259 } 260 261 bool frame::is_safepoint_blob_frame() const { 262 return (_cb != nullptr && _cb->is_safepoint_stub()); 263 } 264 265 // testers 266 267 bool frame::is_first_java_frame() const { 268 RegisterMap map(JavaThread::current(), 269 RegisterMap::UpdateMap::skip, 270 RegisterMap::ProcessFrames::include, 271 RegisterMap::WalkContinuation::skip); // No update 272 frame s; 273 for (s = sender(&map); !(s.is_java_frame() || s.is_first_frame()); s = s.sender(&map)); 274 return s.is_first_frame(); 275 } 276 277 bool frame::is_first_vthread_frame(JavaThread* thread) const { 278 return Continuation::is_continuation_enterSpecial(*this) 279 && Continuation::get_continuation_entry_for_entry_frame(thread, *this)->is_virtual_thread(); 280 } 281 282 bool frame::entry_frame_is_first() const { 283 return entry_frame_call_wrapper()->is_first_frame(); 284 } 285 286 JavaCallWrapper* frame::entry_frame_call_wrapper_if_safe(JavaThread* thread) const { 287 JavaCallWrapper** jcw = entry_frame_call_wrapper_addr(); 288 address addr = (address) jcw; 289 290 // addr must be within the usable part of the stack 291 if (thread->is_in_usable_stack(addr)) { 292 return *jcw; 293 } 294 295 return nullptr; 296 } 297 298 bool frame::is_entry_frame_valid(JavaThread* thread) const { 299 // Validate the JavaCallWrapper an entry frame must have 300 address jcw = (address)entry_frame_call_wrapper(); 301 if (!thread->is_in_stack_range_excl(jcw, (address)fp())) { 302 return false; 303 } 304 305 // Validate sp saved in the java frame anchor 306 JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor(); 307 return (jfa->last_Java_sp() > sp()); 308 } 309 310 Method* frame::safe_interpreter_frame_method() const { 311 Method** m_addr = interpreter_frame_method_addr(); 312 if (m_addr == nullptr) { 313 return nullptr; 314 } 315 return (Method*) SafeFetchN((intptr_t*) m_addr, 0); 316 } 317 318 bool frame::should_be_deoptimized() const { 319 if (_deopt_state == is_deoptimized || 320 !is_compiled_frame() ) return false; 321 assert(_cb != nullptr && _cb->is_nmethod(), "must be an nmethod"); 322 nmethod* nm = _cb->as_nmethod(); 323 LogTarget(Debug, dependencies) lt; 324 if (lt.is_enabled()) { 325 LogStream ls(<); 326 ls.print("checking (%s) ", nm->is_marked_for_deoptimization() ? "true" : "false"); 327 nm->print_value_on(&ls); 328 ls.cr(); 329 } 330 331 if( !nm->is_marked_for_deoptimization() ) 332 return false; 333 334 // If at the return point, then the frame has already been popped, and 335 // only the return needs to be executed. Don't deoptimize here. 336 return !nm->is_at_poll_return(pc()); 337 } 338 339 bool frame::can_be_deoptimized() const { 340 if (!is_compiled_frame()) return false; 341 nmethod* nm = _cb->as_nmethod(); 342 343 if(!nm->can_be_deoptimized()) 344 return false; 345 346 return !nm->is_at_poll_return(pc()); 347 } 348 349 void frame::deoptimize(JavaThread* thread) { 350 assert(thread == nullptr 351 || (thread->frame_anchor()->has_last_Java_frame() && 352 thread->frame_anchor()->walkable()), "must be"); 353 // Schedule deoptimization of an nmethod activation with this frame. 354 assert(_cb != nullptr && _cb->is_nmethod(), "must be"); 355 356 // If the call site is a MethodHandle call site use the MH deopt handler. 357 nmethod* nm = _cb->as_nmethod(); 358 address deopt = nm->is_method_handle_return(pc()) ? 359 nm->deopt_mh_handler_begin() : 360 nm->deopt_handler_begin(); 361 362 NativePostCallNop* inst = nativePostCallNop_at(pc()); 363 364 // Save the original pc before we patch in the new one 365 nm->set_original_pc(this, pc()); 366 367 #ifdef COMPILER1 368 if (nm->is_compiled_by_c1() && nm->method()->has_scalarized_args() && 369 pc() < nm->verified_inline_entry_point()) { 370 // The VEP and VIEP(RO) of C1-compiled methods call into the runtime to buffer scalarized value 371 // type args. We can't deoptimize at that point because the buffers have not yet been initialized. 372 // Also, if the method is synchronized, we first need to acquire the lock. 373 // Don't patch the return pc to delay deoptimization until we enter the method body (the check 374 // added in LIRGenerator::do_Base will detect the pending deoptimization by checking the original_pc). 375 #if defined ASSERT && !defined AARCH64 // Stub call site does not look like NativeCall on AArch64 376 NativeCall* call = nativeCall_before(this->pc()); 377 address dest = call->destination(); 378 assert(dest == Runtime1::entry_for(C1StubId::buffer_inline_args_no_receiver_id) || 379 dest == Runtime1::entry_for(C1StubId::buffer_inline_args_id), "unexpected safepoint in entry point"); 380 #endif 381 return; 382 } 383 #endif 384 385 patch_pc(thread, deopt); 386 assert(is_deoptimized_frame(), "must be"); 387 388 #ifdef ASSERT 389 if (thread != nullptr) { 390 frame check = thread->last_frame(); 391 if (is_older(check.id())) { 392 RegisterMap map(thread, 393 RegisterMap::UpdateMap::skip, 394 RegisterMap::ProcessFrames::include, 395 RegisterMap::WalkContinuation::skip); 396 while (id() != check.id()) { 397 check = check.sender(&map); 398 } 399 assert(check.is_deoptimized_frame(), "missed deopt"); 400 } 401 } 402 #endif // ASSERT 403 } 404 405 frame frame::java_sender() const { 406 RegisterMap map(JavaThread::current(), 407 RegisterMap::UpdateMap::skip, 408 RegisterMap::ProcessFrames::include, 409 RegisterMap::WalkContinuation::skip); 410 frame s; 411 for (s = sender(&map); !(s.is_java_frame() || s.is_first_frame()); s = s.sender(&map)) ; 412 guarantee(s.is_java_frame(), "tried to get caller of first java frame"); 413 return s; 414 } 415 416 frame frame::real_sender(RegisterMap* map) const { 417 frame result = sender(map); 418 while (result.is_runtime_frame() || 419 result.is_ignored_frame()) { 420 result = result.sender(map); 421 } 422 return result; 423 } 424 425 // Interpreter frames 426 427 428 Method* frame::interpreter_frame_method() const { 429 assert(is_interpreted_frame(), "interpreted frame expected"); 430 Method* m = *interpreter_frame_method_addr(); 431 assert(m->is_method(), "not a Method*"); 432 return m; 433 } 434 435 void frame::interpreter_frame_set_method(Method* method) { 436 assert(is_interpreted_frame(), "interpreted frame expected"); 437 *interpreter_frame_method_addr() = method; 438 } 439 440 void frame::interpreter_frame_set_mirror(oop mirror) { 441 assert(is_interpreted_frame(), "interpreted frame expected"); 442 *interpreter_frame_mirror_addr() = mirror; 443 } 444 445 jint frame::interpreter_frame_bci() const { 446 assert(is_interpreted_frame(), "interpreted frame expected"); 447 address bcp = interpreter_frame_bcp(); 448 return interpreter_frame_method()->bci_from(bcp); 449 } 450 451 address frame::interpreter_frame_bcp() const { 452 assert(is_interpreted_frame(), "interpreted frame expected"); 453 address bcp = (address)*interpreter_frame_bcp_addr(); 454 return interpreter_frame_method()->bcp_from(bcp); 455 } 456 457 void frame::interpreter_frame_set_bcp(address bcp) { 458 assert(is_interpreted_frame(), "interpreted frame expected"); 459 *interpreter_frame_bcp_addr() = (intptr_t)bcp; 460 } 461 462 address frame::interpreter_frame_mdp() const { 463 assert(ProfileInterpreter, "must be profiling interpreter"); 464 assert(is_interpreted_frame(), "interpreted frame expected"); 465 return (address)*interpreter_frame_mdp_addr(); 466 } 467 468 void frame::interpreter_frame_set_mdp(address mdp) { 469 assert(is_interpreted_frame(), "interpreted frame expected"); 470 assert(ProfileInterpreter, "must be profiling interpreter"); 471 *interpreter_frame_mdp_addr() = (intptr_t)mdp; 472 } 473 474 BasicObjectLock* frame::next_monitor_in_interpreter_frame(BasicObjectLock* current) const { 475 assert(is_interpreted_frame(), "Not an interpreted frame"); 476 #ifdef ASSERT 477 interpreter_frame_verify_monitor(current); 478 #endif 479 BasicObjectLock* next = (BasicObjectLock*) (((intptr_t*) current) + interpreter_frame_monitor_size()); 480 return next; 481 } 482 483 BasicObjectLock* frame::previous_monitor_in_interpreter_frame(BasicObjectLock* current) const { 484 assert(is_interpreted_frame(), "Not an interpreted frame"); 485 #ifdef ASSERT 486 // // This verification needs to be checked before being enabled 487 // interpreter_frame_verify_monitor(current); 488 #endif 489 BasicObjectLock* previous = (BasicObjectLock*) (((intptr_t*) current) - interpreter_frame_monitor_size()); 490 return previous; 491 } 492 493 // Interpreter locals and expression stack locations. 494 495 intptr_t* frame::interpreter_frame_local_at(int index) const { 496 const int n = Interpreter::local_offset_in_bytes(index)/wordSize; 497 intptr_t* first = interpreter_frame_locals(); 498 return &(first[n]); 499 } 500 501 intptr_t* frame::interpreter_frame_expression_stack_at(jint offset) const { 502 const int i = offset * interpreter_frame_expression_stack_direction(); 503 const int n = i * Interpreter::stackElementWords; 504 return &(interpreter_frame_expression_stack()[n]); 505 } 506 507 jint frame::interpreter_frame_expression_stack_size() const { 508 // Number of elements on the interpreter expression stack 509 // Callers should span by stackElementWords 510 int element_size = Interpreter::stackElementWords; 511 size_t stack_size = 0; 512 if (frame::interpreter_frame_expression_stack_direction() < 0) { 513 stack_size = (interpreter_frame_expression_stack() - 514 interpreter_frame_tos_address() + 1)/element_size; 515 } else { 516 stack_size = (interpreter_frame_tos_address() - 517 interpreter_frame_expression_stack() + 1)/element_size; 518 } 519 assert(stack_size <= (size_t)max_jint, "stack size too big"); 520 return (jint)stack_size; 521 } 522 523 524 // (frame::interpreter_frame_sender_sp accessor is in frame_<arch>.cpp) 525 526 const char* frame::print_name() const { 527 if (is_native_frame()) return "Native"; 528 if (is_interpreted_frame()) return "Interpreted"; 529 if (is_compiled_frame()) { 530 if (is_deoptimized_frame()) return "Deoptimized"; 531 return "Compiled"; 532 } 533 if (sp() == nullptr) return "Empty"; 534 return "C"; 535 } 536 537 void frame::print_value_on(outputStream* st) const { 538 NOT_PRODUCT(address begin = pc()-40;) 539 NOT_PRODUCT(address end = nullptr;) 540 541 st->print("%s frame (sp=" INTPTR_FORMAT " unextended sp=" INTPTR_FORMAT, print_name(), p2i(sp()), p2i(unextended_sp())); 542 if (sp() != nullptr) 543 st->print(", fp=" INTPTR_FORMAT ", real_fp=" INTPTR_FORMAT ", pc=" INTPTR_FORMAT, 544 p2i(fp()), p2i(real_fp()), p2i(pc())); 545 st->print_cr(")"); 546 547 if (StubRoutines::contains(pc())) { 548 StubCodeDesc* desc = StubCodeDesc::desc_for(pc()); 549 st->print("~Stub::%s", desc->name()); 550 NOT_PRODUCT(begin = desc->begin(); end = desc->end();) 551 } else if (Interpreter::contains(pc())) { 552 InterpreterCodelet* desc = Interpreter::codelet_containing(pc()); 553 if (desc != nullptr) { 554 st->print("~"); 555 desc->print_on(st); 556 NOT_PRODUCT(begin = desc->code_begin(); end = desc->code_end();) 557 } else { 558 st->print("~interpreter"); 559 } 560 } 561 562 #ifndef PRODUCT 563 if (_cb != nullptr) { 564 st->print(" "); 565 _cb->print_value_on(st); 566 if (end == nullptr) { 567 begin = _cb->code_begin(); 568 end = _cb->code_end(); 569 } 570 } 571 if (WizardMode && Verbose) Disassembler::decode(begin, end); 572 #endif 573 } 574 575 void frame::print_on(outputStream* st) const { 576 print_value_on(st); 577 if (is_interpreted_frame()) { 578 interpreter_frame_print_on(st); 579 } 580 } 581 582 void frame::interpreter_frame_print_on(outputStream* st) const { 583 #ifndef PRODUCT 584 assert(is_interpreted_frame(), "Not an interpreted frame"); 585 jint i; 586 for (i = 0; i < interpreter_frame_method()->max_locals(); i++ ) { 587 intptr_t x = *interpreter_frame_local_at(i); 588 st->print(" - local [" INTPTR_FORMAT "]", x); 589 st->fill_to(23); 590 st->print_cr("; #%d", i); 591 } 592 for (i = interpreter_frame_expression_stack_size() - 1; i >= 0; --i ) { 593 intptr_t x = *interpreter_frame_expression_stack_at(i); 594 st->print(" - stack [" INTPTR_FORMAT "]", x); 595 st->fill_to(23); 596 st->print_cr("; #%d", i); 597 } 598 // locks for synchronization 599 for (BasicObjectLock* current = interpreter_frame_monitor_end(); 600 current < interpreter_frame_monitor_begin(); 601 current = next_monitor_in_interpreter_frame(current)) { 602 st->print(" - obj [%s", current->obj() == nullptr ? "null" : ""); 603 if (current->obj() != nullptr) current->obj()->print_value_on(st); 604 st->print_cr("]"); 605 st->print(" - lock ["); 606 current->lock()->print_on(st, current->obj()); 607 st->print_cr("]"); 608 } 609 // monitor 610 st->print_cr(" - monitor[" INTPTR_FORMAT "]", p2i(interpreter_frame_monitor_begin())); 611 // bcp 612 st->print(" - bcp [" INTPTR_FORMAT "]", p2i(interpreter_frame_bcp())); 613 st->fill_to(23); 614 st->print_cr("; @%d", interpreter_frame_bci()); 615 // locals 616 st->print_cr(" - locals [" INTPTR_FORMAT "]", p2i(interpreter_frame_local_at(0))); 617 // method 618 st->print(" - method [" INTPTR_FORMAT "]", p2i(interpreter_frame_method())); 619 st->fill_to(23); 620 st->print("; "); 621 interpreter_frame_method()->print_name(st); 622 st->cr(); 623 #endif 624 } 625 626 // Print whether the frame is in the VM or OS indicating a HotSpot problem. 627 // Otherwise, it's likely a bug in the native library that the Java code calls, 628 // hopefully indicating where to submit bugs. 629 void frame::print_C_frame(outputStream* st, char* buf, int buflen, address pc) { 630 // C/C++ frame 631 bool in_vm = os::address_is_in_vm(pc); 632 st->print(in_vm ? "V" : "C"); 633 634 int offset; 635 bool found; 636 637 if (buf == nullptr || buflen < 1) return; 638 // libname 639 buf[0] = '\0'; 640 found = os::dll_address_to_library_name(pc, buf, buflen, &offset); 641 if (found && buf[0] != '\0') { 642 // skip directory names 643 const char *p1, *p2; 644 p1 = buf; 645 int len = (int)strlen(os::file_separator()); 646 while ((p2 = strstr(p1, os::file_separator())) != nullptr) p1 = p2 + len; 647 st->print(" [%s+0x%x]", p1, offset); 648 } else { 649 st->print(" " PTR_FORMAT, p2i(pc)); 650 } 651 652 found = os::dll_address_to_function_name(pc, buf, buflen, &offset); 653 if (found) { 654 st->print(" %s+0x%x", buf, offset); 655 } 656 } 657 658 // frame::print_on_error() is called by fatal error handler. Notice that we may 659 // crash inside this function if stack frame is corrupted. The fatal error 660 // handler can catch and handle the crash. Here we assume the frame is valid. 661 // 662 // First letter indicates type of the frame: 663 // J: Java frame (compiled) 664 // j: Java frame (interpreted) 665 // V: VM frame (C/C++) 666 // v: Other frames running VM generated code (e.g. stubs, adapters, etc.) 667 // C: C/C++ frame 668 // 669 // We don't need detailed frame type as that in frame::print_name(). "C" 670 // suggests the problem is in user lib; everything else is likely a VM bug. 671 672 void frame::print_on_error(outputStream* st, char* buf, int buflen, bool verbose) const { 673 if (_cb != nullptr) { 674 if (Interpreter::contains(pc())) { 675 Method* m = this->interpreter_frame_method(); 676 if (m != nullptr) { 677 m->name_and_sig_as_C_string(buf, buflen); 678 st->print("j %s", buf); 679 st->print("+%d", this->interpreter_frame_bci()); 680 ModuleEntry* module = m->method_holder()->module(); 681 if (module->is_named()) { 682 module->name()->as_C_string(buf, buflen); 683 st->print(" %s", buf); 684 if (module->version() != nullptr) { 685 module->version()->as_C_string(buf, buflen); 686 st->print("@%s", buf); 687 } 688 } 689 } else { 690 st->print("j " PTR_FORMAT, p2i(pc())); 691 } 692 } else if (StubRoutines::contains(pc())) { 693 StubCodeDesc* desc = StubCodeDesc::desc_for(pc()); 694 if (desc != nullptr) { 695 st->print("v ~StubRoutines::%s " PTR_FORMAT, desc->name(), p2i(pc())); 696 } else { 697 st->print("v ~StubRoutines::" PTR_FORMAT, p2i(pc())); 698 } 699 } else if (_cb->is_buffer_blob()) { 700 st->print("v ~BufferBlob::%s " PTR_FORMAT, ((BufferBlob *)_cb)->name(), p2i(pc())); 701 } else if (_cb->is_nmethod()) { 702 nmethod* nm = _cb->as_nmethod(); 703 Method* m = nm->method(); 704 if (m != nullptr) { 705 st->print("J %d%s", nm->compile_id(), (nm->is_osr_method() ? "%" : "")); 706 st->print(" %s", nm->compiler_name()); 707 m->name_and_sig_as_C_string(buf, buflen); 708 st->print(" %s", buf); 709 ModuleEntry* module = m->method_holder()->module(); 710 if (module->is_named()) { 711 module->name()->as_C_string(buf, buflen); 712 st->print(" %s", buf); 713 if (module->version() != nullptr) { 714 module->version()->as_C_string(buf, buflen); 715 st->print("@%s", buf); 716 } 717 } 718 st->print(" (%d bytes) @ " PTR_FORMAT " [" PTR_FORMAT "+" INTPTR_FORMAT "]", 719 m->code_size(), p2i(_pc), p2i(_cb->code_begin()), _pc - _cb->code_begin()); 720 #if INCLUDE_JVMCI 721 const char* jvmciName = nm->jvmci_name(); 722 if (jvmciName != nullptr) { 723 st->print(" (%s)", jvmciName); 724 } 725 #endif 726 } else { 727 st->print("J " PTR_FORMAT, p2i(pc())); 728 } 729 } else if (_cb->is_runtime_stub()) { 730 st->print("v ~RuntimeStub::%s " PTR_FORMAT, ((RuntimeStub *)_cb)->name(), p2i(pc())); 731 } else if (_cb->is_deoptimization_stub()) { 732 st->print("v ~DeoptimizationBlob " PTR_FORMAT, p2i(pc())); 733 } else if (_cb->is_exception_stub()) { 734 st->print("v ~ExceptionBlob " PTR_FORMAT, p2i(pc())); 735 } else if (_cb->is_safepoint_stub()) { 736 st->print("v ~SafepointBlob " PTR_FORMAT, p2i(pc())); 737 } else if (_cb->is_adapter_blob()) { 738 st->print("v ~AdapterBlob " PTR_FORMAT, p2i(pc())); 739 } else if (_cb->is_vtable_blob()) { 740 st->print("v ~VtableBlob " PTR_FORMAT, p2i(pc())); 741 } else if (_cb->is_method_handles_adapter_blob()) { 742 st->print("v ~MethodHandlesAdapterBlob " PTR_FORMAT, p2i(pc())); 743 } else if (_cb->is_uncommon_trap_stub()) { 744 st->print("v ~UncommonTrapBlob " PTR_FORMAT, p2i(pc())); 745 } else if (_cb->is_upcall_stub()) { 746 st->print("v ~UpcallStub::%s " PTR_FORMAT, _cb->name(), p2i(pc())); 747 } else { 748 st->print("v blob " PTR_FORMAT, p2i(pc())); 749 } 750 } else { 751 print_C_frame(st, buf, buflen, pc()); 752 } 753 } 754 755 756 /* 757 The interpreter_frame_expression_stack_at method in the case of SPARC needs the 758 max_stack value of the method in order to compute the expression stack address. 759 It uses the Method* in order to get the max_stack value but during GC this 760 Method* value saved on the frame is changed by reverse_and_push and hence cannot 761 be used. So we save the max_stack value in the FrameClosure object and pass it 762 down to the interpreter_frame_expression_stack_at method 763 */ 764 class InterpreterFrameClosure : public OffsetClosure { 765 private: 766 const frame* _fr; 767 OopClosure* _f; 768 int _max_locals; 769 int _max_stack; 770 771 public: 772 InterpreterFrameClosure(const frame* fr, int max_locals, int max_stack, 773 OopClosure* f, BufferedValueClosure* bvt_f) { 774 _fr = fr; 775 _max_locals = max_locals; 776 _max_stack = max_stack; 777 _f = f; 778 } 779 780 void offset_do(int offset) { 781 oop* addr; 782 if (offset < _max_locals) { 783 addr = (oop*) _fr->interpreter_frame_local_at(offset); 784 assert((intptr_t*)addr >= _fr->sp(), "must be inside the frame"); 785 if (_f != nullptr) { 786 _f->do_oop(addr); 787 } 788 } else { 789 addr = (oop*) _fr->interpreter_frame_expression_stack_at((offset - _max_locals)); 790 // In case of exceptions, the expression stack is invalid and the esp will be reset to express 791 // this condition. Therefore, we call f only if addr is 'inside' the stack (i.e., addr >= esp for Intel). 792 bool in_stack; 793 if (frame::interpreter_frame_expression_stack_direction() > 0) { 794 in_stack = (intptr_t*)addr <= _fr->interpreter_frame_tos_address(); 795 } else { 796 in_stack = (intptr_t*)addr >= _fr->interpreter_frame_tos_address(); 797 } 798 if (in_stack) { 799 if (_f != nullptr) { 800 _f->do_oop(addr); 801 } 802 } 803 } 804 } 805 }; 806 807 808 class InterpretedArgumentOopFinder: public SignatureIterator { 809 private: 810 OopClosure* _f; // Closure to invoke 811 int _offset; // TOS-relative offset, decremented with each argument 812 bool _has_receiver; // true if the callee has a receiver 813 const frame* _fr; 814 815 friend class SignatureIterator; // so do_parameters_on can call do_type 816 void do_type(BasicType type) { 817 _offset -= parameter_type_word_count(type); 818 if (is_reference_type(type)) oop_offset_do(); 819 } 820 821 void oop_offset_do() { 822 oop* addr; 823 addr = (oop*)_fr->interpreter_frame_tos_at(_offset); 824 _f->do_oop(addr); 825 } 826 827 public: 828 InterpretedArgumentOopFinder(Symbol* signature, bool has_receiver, const frame* fr, OopClosure* f) : SignatureIterator(signature), _has_receiver(has_receiver) { 829 // compute size of arguments 830 int args_size = ArgumentSizeComputer(signature).size() + (has_receiver ? 1 : 0); 831 assert(!fr->is_interpreted_frame() || 832 args_size <= fr->interpreter_frame_expression_stack_size(), 833 "args cannot be on stack anymore"); 834 // initialize InterpretedArgumentOopFinder 835 _f = f; 836 _fr = fr; 837 _offset = args_size; 838 } 839 840 void oops_do() { 841 if (_has_receiver) { 842 --_offset; 843 oop_offset_do(); 844 } 845 do_parameters_on(this); 846 } 847 }; 848 849 850 // Entry frame has following form (n arguments) 851 // +-----------+ 852 // sp -> | last arg | 853 // +-----------+ 854 // : ::: : 855 // +-----------+ 856 // (sp+n)->| first arg| 857 // +-----------+ 858 859 860 861 // visits and GC's all the arguments in entry frame 862 class EntryFrameOopFinder: public SignatureIterator { 863 private: 864 bool _is_static; 865 int _offset; 866 const frame* _fr; 867 OopClosure* _f; 868 869 friend class SignatureIterator; // so do_parameters_on can call do_type 870 void do_type(BasicType type) { 871 // decrement offset before processing the type 872 _offset -= parameter_type_word_count(type); 873 assert (_offset >= 0, "illegal offset"); 874 if (is_reference_type(type)) oop_at_offset_do(_offset); 875 } 876 877 void oop_at_offset_do(int offset) { 878 assert (offset >= 0, "illegal offset"); 879 oop* addr = (oop*) _fr->entry_frame_argument_at(offset); 880 _f->do_oop(addr); 881 } 882 883 public: 884 EntryFrameOopFinder(const frame* frame, Symbol* signature, bool is_static) : SignatureIterator(signature) { 885 _f = nullptr; // will be set later 886 _fr = frame; 887 _is_static = is_static; 888 _offset = ArgumentSizeComputer(signature).size(); // pre-decremented down to zero 889 } 890 891 void arguments_do(OopClosure* f) { 892 _f = f; 893 if (!_is_static) oop_at_offset_do(_offset); // do the receiver 894 do_parameters_on(this); 895 } 896 897 }; 898 899 oop* frame::interpreter_callee_receiver_addr(Symbol* signature) { 900 ArgumentSizeComputer asc(signature); 901 int size = asc.size(); 902 return (oop *)interpreter_frame_tos_at(size); 903 } 904 905 oop frame::interpreter_callee_receiver(Symbol* signature) { 906 return *interpreter_callee_receiver_addr(signature); 907 } 908 909 void frame::oops_interpreted_do(OopClosure* f, const RegisterMap* map, bool query_oop_map_cache) const { 910 assert(is_interpreted_frame(), "Not an interpreted frame"); 911 Thread *thread = Thread::current(); 912 methodHandle m (thread, interpreter_frame_method()); 913 jint bci = interpreter_frame_bci(); 914 915 assert(!Universe::heap()->is_in(m()), 916 "must be valid oop"); 917 assert(m->is_method(), "checking frame value"); 918 assert((m->is_native() && bci == 0) || 919 (!m->is_native() && bci >= 0 && bci < m->code_size()), 920 "invalid bci value"); 921 922 // Handle the monitor elements in the activation 923 for ( 924 BasicObjectLock* current = interpreter_frame_monitor_end(); 925 current < interpreter_frame_monitor_begin(); 926 current = next_monitor_in_interpreter_frame(current) 927 ) { 928 #ifdef ASSERT 929 interpreter_frame_verify_monitor(current); 930 #endif 931 current->oops_do(f); 932 } 933 934 if (m->is_native()) { 935 f->do_oop(interpreter_frame_temp_oop_addr()); 936 } 937 938 // The method pointer in the frame might be the only path to the method's 939 // klass, and the klass needs to be kept alive while executing. The GCs 940 // don't trace through method pointers, so the mirror of the method's klass 941 // is installed as a GC root. 942 f->do_oop(interpreter_frame_mirror_addr()); 943 944 int max_locals = m->is_native() ? m->size_of_parameters() : m->max_locals(); 945 946 Symbol* signature = nullptr; 947 bool has_receiver = false; 948 949 // Process a callee's arguments if we are at a call site 950 // (i.e., if we are at an invoke bytecode) 951 // This is used sometimes for calling into the VM, not for another 952 // interpreted or compiled frame. 953 if (!m->is_native()) { 954 Bytecode_invoke call = Bytecode_invoke_check(m, bci); 955 if (map != nullptr && call.is_valid()) { 956 signature = call.signature(); 957 has_receiver = call.has_receiver(); 958 if (map->include_argument_oops() && 959 interpreter_frame_expression_stack_size() > 0) { 960 ResourceMark rm(thread); // is this right ??? 961 // we are at a call site & the expression stack is not empty 962 // => process callee's arguments 963 // 964 // Note: The expression stack can be empty if an exception 965 // occurred during method resolution/execution. In all 966 // cases we empty the expression stack completely be- 967 // fore handling the exception (the exception handling 968 // code in the interpreter calls a blocking runtime 969 // routine which can cause this code to be executed). 970 // (was bug gri 7/27/98) 971 oops_interpreted_arguments_do(signature, has_receiver, f); 972 } 973 } 974 } 975 976 InterpreterFrameClosure blk(this, max_locals, m->max_stack(), f, nullptr); 977 978 // process locals & expression stack 979 InterpreterOopMap mask; 980 if (query_oop_map_cache) { 981 m->mask_for(m, bci, &mask); 982 } else { 983 OopMapCache::compute_one_oop_map(m, bci, &mask); 984 } 985 mask.iterate_oop(&blk); 986 } 987 988 void frame::buffered_values_interpreted_do(BufferedValueClosure* f) { 989 assert(is_interpreted_frame(), "Not an interpreted frame"); 990 Thread *thread = Thread::current(); 991 methodHandle m (thread, interpreter_frame_method()); 992 jint bci = interpreter_frame_bci(); 993 994 assert(m->is_method(), "checking frame value"); 995 assert(!m->is_native() && bci >= 0 && bci < m->code_size(), 996 "invalid bci value"); 997 998 InterpreterFrameClosure blk(this, m->max_locals(), m->max_stack(), nullptr, f); 999 1000 // process locals & expression stack 1001 InterpreterOopMap mask; 1002 m->mask_for(bci, &mask); 1003 mask.iterate_oop(&blk); 1004 } 1005 1006 void frame::oops_interpreted_arguments_do(Symbol* signature, bool has_receiver, OopClosure* f) const { 1007 InterpretedArgumentOopFinder finder(signature, has_receiver, this, f); 1008 finder.oops_do(); 1009 } 1010 1011 void frame::oops_nmethod_do(OopClosure* f, NMethodClosure* cf, DerivedOopClosure* df, DerivedPointerIterationMode derived_mode, const RegisterMap* reg_map) const { 1012 assert(_cb != nullptr, "sanity check"); 1013 assert((oop_map() == nullptr) == (_cb->oop_maps() == nullptr), "frame and _cb must agree that oopmap is set or not"); 1014 if (oop_map() != nullptr) { 1015 if (df != nullptr) { 1016 _oop_map->oops_do(this, reg_map, f, df); 1017 } else { 1018 _oop_map->oops_do(this, reg_map, f, derived_mode); 1019 } 1020 1021 // Preserve potential arguments for a callee. We handle this by dispatching 1022 // on the codeblob. For c2i, we do 1023 if (reg_map->include_argument_oops() && _cb->is_nmethod()) { 1024 // Only nmethod preserves outgoing arguments at call. 1025 _cb->as_nmethod()->preserve_callee_argument_oops(*this, reg_map, f); 1026 } 1027 } 1028 // In cases where perm gen is collected, GC will want to mark 1029 // oops referenced from nmethods active on thread stacks so as to 1030 // prevent them from being collected. However, this visit should be 1031 // restricted to certain phases of the collection only. The 1032 // closure decides how it wants nmethods to be traced. 1033 if (cf != nullptr && _cb->is_nmethod()) 1034 cf->do_nmethod(_cb->as_nmethod()); 1035 } 1036 1037 class CompiledArgumentOopFinder: public SignatureIterator { 1038 protected: 1039 OopClosure* _f; 1040 int _offset; // the current offset, incremented with each argument 1041 bool _has_receiver; // true if the callee has a receiver 1042 bool _has_appendix; // true if the call has an appendix 1043 frame _fr; 1044 RegisterMap* _reg_map; 1045 int _arg_size; 1046 VMRegPair* _regs; // VMReg list of arguments 1047 1048 friend class SignatureIterator; // so do_parameters_on can call do_type 1049 void do_type(BasicType type) { 1050 if (is_reference_type(type)) handle_oop_offset(); 1051 _offset += parameter_type_word_count(type); 1052 } 1053 1054 virtual void handle_oop_offset() { 1055 // Extract low order register number from register array. 1056 // In LP64-land, the high-order bits are valid but unhelpful. 1057 assert(_offset < _arg_size, "out of bounds"); 1058 VMReg reg = _regs[_offset].first(); 1059 oop *loc = _fr.oopmapreg_to_oop_location(reg, _reg_map); 1060 #ifdef ASSERT 1061 if (loc == nullptr) { 1062 if (_reg_map->should_skip_missing()) { 1063 return; 1064 } 1065 tty->print_cr("Error walking frame oops:"); 1066 _fr.print_on(tty); 1067 assert(loc != nullptr, "missing register map entry reg: %d %s loc: " INTPTR_FORMAT, reg->value(), reg->name(), p2i(loc)); 1068 } 1069 #endif 1070 _f->do_oop(loc); 1071 } 1072 1073 public: 1074 CompiledArgumentOopFinder(Symbol* signature, bool has_receiver, bool has_appendix, OopClosure* f, frame fr, const RegisterMap* reg_map) 1075 : SignatureIterator(signature) { 1076 1077 // initialize CompiledArgumentOopFinder 1078 _f = f; 1079 _offset = 0; 1080 _has_receiver = has_receiver; 1081 _has_appendix = has_appendix; 1082 _fr = fr; 1083 _reg_map = (RegisterMap*)reg_map; 1084 _regs = SharedRuntime::find_callee_arguments(signature, has_receiver, has_appendix, &_arg_size); 1085 } 1086 1087 void oops_do() { 1088 if (_has_receiver) { 1089 handle_oop_offset(); 1090 _offset++; 1091 } 1092 do_parameters_on(this); 1093 if (_has_appendix) { 1094 handle_oop_offset(); 1095 _offset++; 1096 } 1097 } 1098 }; 1099 1100 void frame::oops_compiled_arguments_do(Symbol* signature, bool has_receiver, bool has_appendix, 1101 const RegisterMap* reg_map, OopClosure* f) const { 1102 // ResourceMark rm; 1103 CompiledArgumentOopFinder finder(signature, has_receiver, has_appendix, f, *this, reg_map); 1104 finder.oops_do(); 1105 } 1106 1107 // Get receiver out of callers frame, i.e. find parameter 0 in callers 1108 // frame. Consult ADLC for where parameter 0 is to be found. Then 1109 // check local reg_map for it being a callee-save register or argument 1110 // register, both of which are saved in the local frame. If not found 1111 // there, it must be an in-stack argument of the caller. 1112 // Note: caller.sp() points to callee-arguments 1113 oop frame::retrieve_receiver(RegisterMap* reg_map) { 1114 frame caller = *this; 1115 1116 // First consult the ADLC on where it puts parameter 0 for this signature. 1117 VMReg reg = SharedRuntime::name_for_receiver(); 1118 oop* oop_adr = caller.oopmapreg_to_oop_location(reg, reg_map); 1119 if (oop_adr == nullptr) { 1120 guarantee(oop_adr != nullptr, "bad register save location"); 1121 return nullptr; 1122 } 1123 oop r = *oop_adr; 1124 assert(Universe::heap()->is_in_or_null(r), "bad receiver: " INTPTR_FORMAT " (" INTX_FORMAT ")", p2i(r), p2i(r)); 1125 return r; 1126 } 1127 1128 1129 BasicLock* frame::get_native_monitor() { 1130 nmethod* nm = (nmethod*)_cb; 1131 assert(_cb != nullptr && _cb->is_nmethod() && nm->method()->is_native(), 1132 "Should not call this unless it's a native nmethod"); 1133 int byte_offset = in_bytes(nm->native_basic_lock_sp_offset()); 1134 assert(byte_offset >= 0, "should not see invalid offset"); 1135 return (BasicLock*) &sp()[byte_offset / wordSize]; 1136 } 1137 1138 oop frame::get_native_receiver() { 1139 nmethod* nm = (nmethod*)_cb; 1140 assert(_cb != nullptr && _cb->is_nmethod() && nm->method()->is_native(), 1141 "Should not call this unless it's a native nmethod"); 1142 int byte_offset = in_bytes(nm->native_receiver_sp_offset()); 1143 assert(byte_offset >= 0, "should not see invalid offset"); 1144 oop owner = ((oop*) sp())[byte_offset / wordSize]; 1145 assert( Universe::heap()->is_in(owner), "bad receiver" ); 1146 return owner; 1147 } 1148 1149 void frame::oops_entry_do(OopClosure* f, const RegisterMap* map) const { 1150 assert(map != nullptr, "map must be set"); 1151 if (map->include_argument_oops()) { 1152 // must collect argument oops, as nobody else is doing it 1153 Thread *thread = Thread::current(); 1154 methodHandle m (thread, entry_frame_call_wrapper()->callee_method()); 1155 EntryFrameOopFinder finder(this, m->signature(), m->is_static()); 1156 finder.arguments_do(f); 1157 } 1158 // Traverse the Handle Block saved in the entry frame 1159 entry_frame_call_wrapper()->oops_do(f); 1160 } 1161 1162 void frame::oops_upcall_do(OopClosure* f, const RegisterMap* map) const { 1163 assert(map != nullptr, "map must be set"); 1164 if (map->include_argument_oops()) { 1165 // Upcall stubs call a MethodHandle impl method of which only the receiver 1166 // is ever an oop. 1167 // Currently we should not be able to get here, since there are no 1168 // safepoints in the one resolve stub we can get into (handle_wrong_method) 1169 // Leave this here as a trap in case we ever do: 1170 ShouldNotReachHere(); // not implemented 1171 } 1172 _cb->as_upcall_stub()->oops_do(f, *this); 1173 } 1174 1175 bool frame::is_deoptimized_frame() const { 1176 assert(_deopt_state != unknown, "not answerable"); 1177 if (_deopt_state == is_deoptimized) { 1178 return true; 1179 } 1180 1181 /* This method only checks if the frame is deoptimized 1182 * as in return address being patched. 1183 * It doesn't care if the OP that we return to is a 1184 * deopt instruction */ 1185 /*if (_cb != nullptr && _cb->is_nmethod()) { 1186 return NativeDeoptInstruction::is_deopt_at(_pc); 1187 }*/ 1188 return false; 1189 } 1190 1191 void frame::oops_do_internal(OopClosure* f, NMethodClosure* cf, 1192 DerivedOopClosure* df, DerivedPointerIterationMode derived_mode, 1193 const RegisterMap* map, bool use_interpreter_oop_map_cache) const { 1194 #ifndef PRODUCT 1195 // simulate GC crash here to dump java thread in error report 1196 if (CrashGCForDumpingJavaThread) { 1197 char *t = nullptr; 1198 *t = 'c'; 1199 } 1200 #endif 1201 if (is_interpreted_frame()) { 1202 oops_interpreted_do(f, map, use_interpreter_oop_map_cache); 1203 } else if (is_entry_frame()) { 1204 oops_entry_do(f, map); 1205 } else if (is_upcall_stub_frame()) { 1206 oops_upcall_do(f, map); 1207 } else if (CodeCache::contains(pc())) { 1208 oops_nmethod_do(f, cf, df, derived_mode, map); 1209 } else { 1210 ShouldNotReachHere(); 1211 } 1212 } 1213 1214 void frame::nmethod_do(NMethodClosure* cf) const { 1215 if (_cb != nullptr && _cb->is_nmethod()) { 1216 cf->do_nmethod(_cb->as_nmethod()); 1217 } 1218 } 1219 1220 1221 // Call f closure on the interpreted Method*s in the stack. 1222 void frame::metadata_do(MetadataClosure* f) const { 1223 ResourceMark rm; 1224 if (is_interpreted_frame()) { 1225 Method* m = this->interpreter_frame_method(); 1226 assert(m != nullptr, "expecting a method in this frame"); 1227 f->do_metadata(m); 1228 } 1229 } 1230 1231 void frame::verify(const RegisterMap* map) const { 1232 #ifndef PRODUCT 1233 if (TraceCodeBlobStacks) { 1234 tty->print_cr("*** verify"); 1235 print_on(tty); 1236 } 1237 #endif 1238 1239 // for now make sure receiver type is correct 1240 if (is_interpreted_frame()) { 1241 Method* method = interpreter_frame_method(); 1242 guarantee(method->is_method(), "method is wrong in frame::verify"); 1243 if (!method->is_static()) { 1244 // fetch the receiver 1245 oop* p = (oop*) interpreter_frame_local_at(0); 1246 // make sure we have the right receiver type 1247 } 1248 } 1249 #if COMPILER2_OR_JVMCI 1250 assert(DerivedPointerTable::is_empty(), "must be empty before verify"); 1251 #endif 1252 1253 if (map->update_map()) { // The map has to be up-to-date for the current frame 1254 oops_do_internal(&VerifyOopClosure::verify_oop, nullptr, nullptr, DerivedPointerIterationMode::_ignore, map, false); 1255 } 1256 } 1257 1258 1259 #ifdef ASSERT 1260 bool frame::verify_return_pc(address x) { 1261 #ifdef TARGET_ARCH_aarch64 1262 if (!pauth_ptr_is_raw(x)) { 1263 return false; 1264 } 1265 #endif 1266 if (StubRoutines::returns_to_call_stub(x)) { 1267 return true; 1268 } 1269 if (CodeCache::contains(x)) { 1270 return true; 1271 } 1272 if (Interpreter::contains(x)) { 1273 return true; 1274 } 1275 return false; 1276 } 1277 #endif 1278 1279 #ifdef ASSERT 1280 void frame::interpreter_frame_verify_monitor(BasicObjectLock* value) const { 1281 assert(is_interpreted_frame(), "Not an interpreted frame"); 1282 // verify that the value is in the right part of the frame 1283 address low_mark = (address) interpreter_frame_monitor_end(); 1284 address high_mark = (address) interpreter_frame_monitor_begin(); 1285 address current = (address) value; 1286 1287 const int monitor_size = frame::interpreter_frame_monitor_size(); 1288 guarantee((high_mark - current) % monitor_size == 0 , "Misaligned top of BasicObjectLock*"); 1289 guarantee( high_mark > current , "Current BasicObjectLock* higher than high_mark"); 1290 1291 guarantee((current - low_mark) % monitor_size == 0 , "Misaligned bottom of BasicObjectLock*"); 1292 guarantee( current >= low_mark , "Current BasicObjectLock* below than low_mark"); 1293 } 1294 #endif 1295 1296 #ifndef PRODUCT 1297 1298 // Returns true iff the address p is readable and *(intptr_t*)p != errvalue 1299 extern "C" bool dbg_is_safe(const void* p, intptr_t errvalue); 1300 1301 class FrameValuesOopClosure: public OopClosure, public DerivedOopClosure { 1302 private: 1303 GrowableArray<oop*>* _oops; 1304 GrowableArray<narrowOop*>* _narrow_oops; 1305 GrowableArray<derived_base*>* _base; 1306 GrowableArray<derived_pointer*>* _derived; 1307 NoSafepointVerifier nsv; 1308 1309 public: 1310 FrameValuesOopClosure() { 1311 _oops = new (mtThread) GrowableArray<oop*>(100, mtThread); 1312 _narrow_oops = new (mtThread) GrowableArray<narrowOop*>(100, mtThread); 1313 _base = new (mtThread) GrowableArray<derived_base*>(100, mtThread); 1314 _derived = new (mtThread) GrowableArray<derived_pointer*>(100, mtThread); 1315 } 1316 ~FrameValuesOopClosure() { 1317 delete _oops; 1318 delete _narrow_oops; 1319 delete _base; 1320 delete _derived; 1321 } 1322 1323 virtual void do_oop(oop* p) override { _oops->push(p); } 1324 virtual void do_oop(narrowOop* p) override { _narrow_oops->push(p); } 1325 virtual void do_derived_oop(derived_base* base_loc, derived_pointer* derived_loc) override { 1326 _base->push(base_loc); 1327 _derived->push(derived_loc); 1328 } 1329 1330 bool is_good(oop* p) { 1331 return *p == nullptr || (dbg_is_safe(*p, -1) && dbg_is_safe((*p)->klass(), -1) && oopDesc::is_oop_or_null(*p)); 1332 } 1333 void describe(FrameValues& values, int frame_no) { 1334 for (int i = 0; i < _oops->length(); i++) { 1335 oop* p = _oops->at(i); 1336 values.describe(frame_no, (intptr_t*)p, err_msg("oop%s for #%d", is_good(p) ? "" : " (BAD)", frame_no)); 1337 } 1338 for (int i = 0; i < _narrow_oops->length(); i++) { 1339 narrowOop* p = _narrow_oops->at(i); 1340 // we can't check for bad compressed oops, as decoding them might crash 1341 values.describe(frame_no, (intptr_t*)p, err_msg("narrow oop for #%d", frame_no)); 1342 } 1343 assert(_base->length() == _derived->length(), "should be the same"); 1344 for (int i = 0; i < _base->length(); i++) { 1345 derived_base* base = _base->at(i); 1346 derived_pointer* derived = _derived->at(i); 1347 values.describe(frame_no, (intptr_t*)derived, err_msg("derived pointer (base: " INTPTR_FORMAT ") for #%d", p2i(base), frame_no)); 1348 } 1349 } 1350 }; 1351 1352 class FrameValuesOopMapClosure: public OopMapClosure { 1353 private: 1354 const frame* _fr; 1355 const RegisterMap* _reg_map; 1356 FrameValues& _values; 1357 int _frame_no; 1358 1359 public: 1360 FrameValuesOopMapClosure(const frame* fr, const RegisterMap* reg_map, FrameValues& values, int frame_no) 1361 : _fr(fr), _reg_map(reg_map), _values(values), _frame_no(frame_no) {} 1362 1363 virtual void do_value(VMReg reg, OopMapValue::oop_types type) override { 1364 intptr_t* p = (intptr_t*)_fr->oopmapreg_to_location(reg, _reg_map); 1365 if (p != nullptr && (((intptr_t)p & WordAlignmentMask) == 0)) { 1366 const char* type_name = nullptr; 1367 switch(type) { 1368 case OopMapValue::oop_value: type_name = "oop"; break; 1369 case OopMapValue::narrowoop_value: type_name = "narrow oop"; break; 1370 case OopMapValue::callee_saved_value: type_name = "callee-saved"; break; 1371 case OopMapValue::derived_oop_value: type_name = "derived"; break; 1372 // case OopMapValue::live_value: type_name = "live"; break; 1373 default: break; 1374 } 1375 if (type_name != nullptr) { 1376 _values.describe(_frame_no, p, err_msg("%s for #%d", type_name, _frame_no)); 1377 } 1378 } 1379 } 1380 }; 1381 1382 // callers need a ResourceMark because of name_and_sig_as_C_string() usage, 1383 // RA allocated string is returned to the caller 1384 void frame::describe(FrameValues& values, int frame_no, const RegisterMap* reg_map) { 1385 // boundaries: sp and the 'real' frame pointer 1386 values.describe(-1, sp(), err_msg("sp for #%d", frame_no), 0); 1387 intptr_t* frame_pointer = real_fp(); // Note: may differ from fp() 1388 1389 // print frame info at the highest boundary 1390 intptr_t* info_address = MAX2(sp(), frame_pointer); 1391 1392 if (info_address != frame_pointer) { 1393 // print frame_pointer explicitly if not marked by the frame info 1394 values.describe(-1, frame_pointer, err_msg("frame pointer for #%d", frame_no), 1); 1395 } 1396 1397 if (is_entry_frame() || is_compiled_frame() || is_interpreted_frame() || is_native_frame()) { 1398 // Label values common to most frames 1399 values.describe(-1, unextended_sp(), err_msg("unextended_sp for #%d", frame_no), 0); 1400 } 1401 1402 if (is_interpreted_frame()) { 1403 Method* m = interpreter_frame_method(); 1404 int bci = interpreter_frame_bci(); 1405 InterpreterCodelet* desc = Interpreter::codelet_containing(pc()); 1406 1407 // Label the method and current bci 1408 values.describe(-1, info_address, 1409 FormatBuffer<1024>("#%d method %s @ %d", frame_no, m->name_and_sig_as_C_string(), bci), 3); 1410 if (desc != nullptr) { 1411 values.describe(-1, info_address, err_msg("- %s codelet: %s", 1412 desc->bytecode() >= 0 ? Bytecodes::name(desc->bytecode()) : "", 1413 desc->description() != nullptr ? desc->description() : "?"), 2); 1414 } 1415 values.describe(-1, info_address, 1416 err_msg("- %d locals %d max stack", m->max_locals(), m->max_stack()), 2); 1417 // return address will be emitted by caller in describe_pd 1418 // values.describe(frame_no, (intptr_t*)sender_pc_addr(), Continuation::is_return_barrier_entry(*sender_pc_addr()) ? "return address (return barrier)" : "return address"); 1419 1420 if (m->max_locals() > 0) { 1421 intptr_t* l0 = interpreter_frame_local_at(0); 1422 intptr_t* ln = interpreter_frame_local_at(m->max_locals() - 1); 1423 values.describe(-1, MAX2(l0, ln), err_msg("locals for #%d", frame_no), 2); 1424 // Report each local and mark as owned by this frame 1425 for (int l = 0; l < m->max_locals(); l++) { 1426 intptr_t* l0 = interpreter_frame_local_at(l); 1427 values.describe(frame_no, l0, err_msg("local %d", l), 1); 1428 } 1429 } 1430 1431 if (interpreter_frame_monitor_begin() != interpreter_frame_monitor_end()) { 1432 values.describe(frame_no, (intptr_t*)interpreter_frame_monitor_begin(), "monitors begin"); 1433 values.describe(frame_no, (intptr_t*)interpreter_frame_monitor_end(), "monitors end"); 1434 } 1435 1436 // Compute the actual expression stack size 1437 InterpreterOopMap mask; 1438 OopMapCache::compute_one_oop_map(methodHandle(Thread::current(), m), bci, &mask); 1439 intptr_t* tos = nullptr; 1440 // Report each stack element and mark as owned by this frame 1441 for (int e = 0; e < mask.expression_stack_size(); e++) { 1442 tos = MAX2(tos, interpreter_frame_expression_stack_at(e)); 1443 values.describe(frame_no, interpreter_frame_expression_stack_at(e), 1444 err_msg("stack %d", e), 1); 1445 } 1446 if (tos != nullptr) { 1447 values.describe(-1, tos, err_msg("expression stack for #%d", frame_no), 2); 1448 } 1449 1450 if (reg_map != nullptr) { 1451 FrameValuesOopClosure oopsFn; 1452 oops_do(&oopsFn, nullptr, &oopsFn, reg_map); 1453 oopsFn.describe(values, frame_no); 1454 } 1455 } else if (is_entry_frame()) { 1456 // For now just label the frame 1457 values.describe(-1, info_address, err_msg("#%d entry frame", frame_no), 2); 1458 } else if (cb()->is_nmethod()) { 1459 // For now just label the frame 1460 nmethod* nm = cb()->as_nmethod(); 1461 values.describe(-1, info_address, 1462 FormatBuffer<1024>("#%d nmethod " INTPTR_FORMAT " for method J %s%s", frame_no, 1463 p2i(nm), 1464 nm->method()->name_and_sig_as_C_string(), 1465 (_deopt_state == is_deoptimized) ? 1466 " (deoptimized)" : 1467 ((_deopt_state == unknown) ? " (state unknown)" : "")), 1468 3); 1469 1470 { // mark arguments (see nmethod::print_nmethod_labels) 1471 Method* m = nm->method(); 1472 1473 int stack_slot_offset = nm->frame_size() * wordSize; // offset, in bytes, to caller sp 1474 int sizeargs = m->size_of_parameters(); 1475 1476 BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sizeargs); 1477 VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair, sizeargs); 1478 { 1479 int sig_index = 0; 1480 if (!m->is_static()) { 1481 sig_bt[sig_index++] = T_OBJECT; // 'this' 1482 } 1483 for (SignatureStream ss(m->signature()); !ss.at_return_type(); ss.next()) { 1484 BasicType t = ss.type(); 1485 assert(type2size[t] == 1 || type2size[t] == 2, "size is 1 or 2"); 1486 sig_bt[sig_index++] = t; 1487 if (type2size[t] == 2) { 1488 sig_bt[sig_index++] = T_VOID; 1489 } 1490 } 1491 assert(sig_index == sizeargs, ""); 1492 } 1493 int stack_arg_slots = SharedRuntime::java_calling_convention(sig_bt, regs, sizeargs); 1494 assert(stack_arg_slots == nm->as_nmethod()->num_stack_arg_slots(false /* rounded */) || nm->is_osr_method(), ""); 1495 int out_preserve = SharedRuntime::out_preserve_stack_slots(); 1496 int sig_index = 0; 1497 int arg_index = (m->is_static() ? 0 : -1); 1498 for (SignatureStream ss(m->signature()); !ss.at_return_type(); ) { 1499 bool at_this = (arg_index == -1); 1500 bool at_old_sp = false; 1501 BasicType t = (at_this ? T_OBJECT : ss.type()); 1502 assert(t == sig_bt[sig_index], "sigs in sync"); 1503 VMReg fst = regs[sig_index].first(); 1504 if (fst->is_stack()) { 1505 assert(((int)fst->reg2stack()) >= 0, "reg2stack: %d", fst->reg2stack()); 1506 int offset = (fst->reg2stack() + out_preserve) * VMRegImpl::stack_slot_size + stack_slot_offset; 1507 intptr_t* stack_address = (intptr_t*)((address)unextended_sp() + offset); 1508 if (at_this) { 1509 values.describe(frame_no, stack_address, err_msg("this for #%d", frame_no), 1); 1510 } else { 1511 values.describe(frame_no, stack_address, err_msg("param %d %s for #%d", arg_index, type2name(t), frame_no), 1); 1512 } 1513 } 1514 sig_index += type2size[t]; 1515 arg_index += 1; 1516 if (!at_this) { 1517 ss.next(); 1518 } 1519 } 1520 } 1521 1522 if (reg_map != nullptr && is_java_frame()) { 1523 int scope_no = 0; 1524 for (ScopeDesc* scope = nm->scope_desc_at(pc()); scope != nullptr; scope = scope->sender(), scope_no++) { 1525 Method* m = scope->method(); 1526 int bci = scope->bci(); 1527 values.describe(-1, info_address, err_msg("- #%d scope %s @ %d", scope_no, m->name_and_sig_as_C_string(), bci), 2); 1528 1529 { // mark locals 1530 GrowableArray<ScopeValue*>* scvs = scope->locals(); 1531 int scvs_length = scvs != nullptr ? scvs->length() : 0; 1532 for (int i = 0; i < scvs_length; i++) { 1533 intptr_t* stack_address = (intptr_t*)StackValue::stack_value_address(this, reg_map, scvs->at(i)); 1534 if (stack_address != nullptr) { 1535 values.describe(frame_no, stack_address, err_msg("local %d for #%d (scope %d)", i, frame_no, scope_no), 1); 1536 } 1537 } 1538 } 1539 { // mark expression stack 1540 GrowableArray<ScopeValue*>* scvs = scope->expressions(); 1541 int scvs_length = scvs != nullptr ? scvs->length() : 0; 1542 for (int i = 0; i < scvs_length; i++) { 1543 intptr_t* stack_address = (intptr_t*)StackValue::stack_value_address(this, reg_map, scvs->at(i)); 1544 if (stack_address != nullptr) { 1545 values.describe(frame_no, stack_address, err_msg("stack %d for #%d (scope %d)", i, frame_no, scope_no), 1); 1546 } 1547 } 1548 } 1549 } 1550 1551 FrameValuesOopClosure oopsFn; 1552 oops_do(&oopsFn, nullptr, &oopsFn, reg_map); 1553 oopsFn.describe(values, frame_no); 1554 1555 if (oop_map() != nullptr) { 1556 FrameValuesOopMapClosure valuesFn(this, reg_map, values, frame_no); 1557 // also OopMapValue::live_value ?? 1558 oop_map()->all_type_do(this, OopMapValue::callee_saved_value, &valuesFn); 1559 } 1560 } 1561 1562 if (nm->method()->is_continuation_enter_intrinsic()) { 1563 ContinuationEntry* ce = Continuation::get_continuation_entry_for_entry_frame(reg_map->thread(), *this); // (ContinuationEntry*)unextended_sp(); 1564 ce->describe(values, frame_no); 1565 } 1566 } else if (is_native_frame()) { 1567 // For now just label the frame 1568 nmethod* nm = cb()->as_nmethod_or_null(); 1569 values.describe(-1, info_address, 1570 FormatBuffer<1024>("#%d nmethod " INTPTR_FORMAT " for native method %s", frame_no, 1571 p2i(nm), nm->method()->name_and_sig_as_C_string()), 2); 1572 } else { 1573 // provide default info if not handled before 1574 char *info = (char *) "special frame"; 1575 if ((_cb != nullptr) && 1576 (_cb->name() != nullptr)) { 1577 info = (char *)_cb->name(); 1578 } 1579 values.describe(-1, info_address, err_msg("#%d <%s>", frame_no, info), 2); 1580 } 1581 1582 // platform dependent additional data 1583 describe_pd(values, frame_no); 1584 } 1585 1586 #endif 1587 1588 #ifndef PRODUCT 1589 1590 void FrameValues::describe(int owner, intptr_t* location, const char* description, int priority) { 1591 FrameValue fv; 1592 fv.location = location; 1593 fv.owner = owner; 1594 fv.priority = priority; 1595 fv.description = NEW_RESOURCE_ARRAY(char, strlen(description) + 1); 1596 strcpy(fv.description, description); 1597 _values.append(fv); 1598 } 1599 1600 1601 #ifdef ASSERT 1602 void FrameValues::validate() { 1603 _values.sort(compare); 1604 bool error = false; 1605 FrameValue prev; 1606 prev.owner = -1; 1607 for (int i = _values.length() - 1; i >= 0; i--) { 1608 FrameValue fv = _values.at(i); 1609 if (fv.owner == -1) continue; 1610 if (prev.owner == -1) { 1611 prev = fv; 1612 continue; 1613 } 1614 if (prev.location == fv.location) { 1615 if (fv.owner != prev.owner) { 1616 tty->print_cr("overlapping storage"); 1617 tty->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", p2i(prev.location), *prev.location, prev.description); 1618 tty->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", p2i(fv.location), *fv.location, fv.description); 1619 error = true; 1620 } 1621 } else { 1622 prev = fv; 1623 } 1624 } 1625 // if (error) { tty->cr(); print_on(static_cast<JavaThread*>(nullptr), tty); } 1626 assert(!error, "invalid layout"); 1627 } 1628 #endif // ASSERT 1629 1630 void FrameValues::print_on(JavaThread* thread, outputStream* st) { 1631 _values.sort(compare); 1632 1633 // Sometimes values like the fp can be invalid values if the 1634 // register map wasn't updated during the walk. Trim out values 1635 // that aren't actually in the stack of the thread. 1636 int min_index = 0; 1637 int max_index = _values.length() - 1; 1638 intptr_t* v0 = _values.at(min_index).location; 1639 intptr_t* v1 = _values.at(max_index).location; 1640 1641 if (thread != nullptr) { 1642 if (thread == Thread::current()) { 1643 while (!thread->is_in_live_stack((address)v0)) v0 = _values.at(++min_index).location; 1644 while (!thread->is_in_live_stack((address)v1)) v1 = _values.at(--max_index).location; 1645 } else { 1646 while (!thread->is_in_full_stack((address)v0)) v0 = _values.at(++min_index).location; 1647 while (!thread->is_in_full_stack((address)v1)) v1 = _values.at(--max_index).location; 1648 } 1649 } 1650 1651 print_on(st, min_index, max_index, v0, v1); 1652 } 1653 1654 void FrameValues::print_on(stackChunkOop chunk, outputStream* st) { 1655 _values.sort(compare); 1656 1657 intptr_t* start = chunk->start_address(); 1658 intptr_t* end = chunk->end_address() + 1; 1659 1660 int min_index = 0; 1661 int max_index = _values.length() - 1; 1662 intptr_t* v0 = _values.at(min_index).location; 1663 intptr_t* v1 = _values.at(max_index).location; 1664 while (!(start <= v0 && v0 <= end)) v0 = _values.at(++min_index).location; 1665 while (!(start <= v1 && v1 <= end)) v1 = _values.at(--max_index).location; 1666 1667 print_on(st, min_index, max_index, v0, v1); 1668 } 1669 1670 void FrameValues::print_on(outputStream* st, int min_index, int max_index, intptr_t* v0, intptr_t* v1) { 1671 intptr_t* min = MIN2(v0, v1); 1672 intptr_t* max = MAX2(v0, v1); 1673 intptr_t* cur = max; 1674 intptr_t* last = nullptr; 1675 intptr_t* fp = nullptr; 1676 for (int i = max_index; i >= min_index; i--) { 1677 FrameValue fv = _values.at(i); 1678 while (cur > fv.location) { 1679 st->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT, p2i(cur), *cur); 1680 cur--; 1681 } 1682 if (last == fv.location) { 1683 const char* spacer = " " LP64_ONLY(" "); 1684 st->print_cr(" %s %s %s", spacer, spacer, fv.description); 1685 } else { 1686 if (*fv.description == '#' && isdigit(fv.description[1])) { 1687 // The fv.description string starting with a '#' is the line for the 1688 // saved frame pointer eg. "#10 method java.lang.invoke.LambdaForm..." 1689 // basicaly means frame 10. 1690 fp = fv.location; 1691 } 1692 // To print a fp-relative value: 1693 // 1. The content of *fv.location must be such that we think it's a 1694 // fp-relative number, i.e [-100..100]. 1695 // 2. We must have found the frame pointer. 1696 // 3. The line can not be the line for the saved frame pointer. 1697 // 4. Recognize it as being part of the "fixed frame". 1698 if (*fv.location != 0 && *fv.location > -100 && *fv.location < 100 1699 && fp != nullptr && *fv.description != '#' 1700 #if !defined(PPC64) 1701 && (strncmp(fv.description, "interpreter_frame_", 18) == 0 || strstr(fv.description, " method ")) 1702 #else // !defined(PPC64) 1703 && (strcmp(fv.description, "sender_sp") == 0 || strcmp(fv.description, "top_frame_sp") == 0 || 1704 strcmp(fv.description, "esp") == 0 || strcmp(fv.description, "monitors") == 0 || 1705 strcmp(fv.description, "locals") == 0 || strstr(fv.description, " method ")) 1706 #endif //!defined(PPC64) 1707 ) { 1708 st->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %-32s (relativized: fp%+d)", 1709 p2i(fv.location), p2i(&fp[*fv.location]), fv.description, (int)*fv.location); 1710 } else { 1711 st->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", p2i(fv.location), *fv.location, fv.description); 1712 } 1713 last = fv.location; 1714 cur--; 1715 } 1716 } 1717 } 1718 1719 #endif // ndef PRODUCT