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