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