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