1 /*
   2  * Copyright (c) 1997, 2021, 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/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 "memory/resourceArea.hpp"
  36 #include "memory/universe.hpp"
  37 #include "oops/markWord.hpp"
  38 #include "oops/method.hpp"
  39 #include "oops/methodData.hpp"
  40 #include "oops/oop.inline.hpp"
  41 #include "oops/verifyOopClosure.hpp"
  42 #include "prims/methodHandles.hpp"
  43 #include "runtime/frame.inline.hpp"
  44 #include "runtime/handles.inline.hpp"
  45 #include "runtime/javaCalls.hpp"
  46 #include "runtime/monitorChunk.hpp"
  47 #include "runtime/os.hpp"
  48 #include "runtime/sharedRuntime.hpp"
  49 #include "runtime/signature.hpp"
  50 #include "runtime/stubCodeGenerator.hpp"
  51 #include "runtime/stubRoutines.hpp"
  52 #include "runtime/thread.inline.hpp"
  53 #include "utilities/debug.hpp"
  54 #include "utilities/decoder.hpp"
  55 #include "utilities/formatBuffer.hpp"
  56 
  57 RegisterMap::RegisterMap(JavaThread *thread, bool update_map, bool process_frames) {
  58   _thread         = thread;
  59   _update_map     = update_map;
  60   _process_frames = process_frames;
  61   clear();
  62   debug_only(_update_for_id = NULL;)
  63 #ifndef PRODUCT
  64   for (int i = 0; i < reg_count ; i++ ) _location[i] = NULL;
  65 #endif /* PRODUCT */
  66 }
  67 
  68 RegisterMap::RegisterMap(const RegisterMap* map) {
  69   assert(map != this, "bad initialization parameter");
  70   assert(map != NULL, "RegisterMap must be present");
  71   _thread                = map->thread();
  72   _update_map            = map->update_map();
  73   _process_frames        = map->process_frames();
  74   _include_argument_oops = map->include_argument_oops();
  75   debug_only(_update_for_id = map->_update_for_id;)
  76   pd_initialize_from(map);
  77   if (update_map()) {
  78     for(int i = 0; i < location_valid_size; i++) {
  79       LocationValidType bits = !update_map() ? 0 : map->_location_valid[i];
  80       _location_valid[i] = bits;
  81       // for whichever bits are set, pull in the corresponding map->_location
  82       int j = i*location_valid_type_size;
  83       while (bits != 0) {
  84         if ((bits & 1) != 0) {
  85           assert(0 <= j && j < reg_count, "range check");
  86           _location[j] = map->_location[j];
  87         }
  88         bits >>= 1;
  89         j += 1;
  90       }
  91     }
  92   }
  93 }
  94 
  95 void RegisterMap::clear() {
  96   set_include_argument_oops(true);
  97   if (_update_map) {
  98     for(int i = 0; i < location_valid_size; i++) {
  99       _location_valid[i] = 0;
 100     }
 101     pd_clear();
 102   } else {
 103     pd_initialize();
 104   }
 105 }
 106 
 107 #ifndef PRODUCT
 108 
 109 void RegisterMap::print_on(outputStream* st) const {
 110   st->print_cr("Register map");
 111   for(int i = 0; i < reg_count; i++) {
 112 
 113     VMReg r = VMRegImpl::as_VMReg(i);
 114     intptr_t* src = (intptr_t*) location(r);
 115     if (src != NULL) {
 116 
 117       r->print_on(st);
 118       st->print(" [" INTPTR_FORMAT "] = ", p2i(src));
 119       if (((uintptr_t)src & (sizeof(*src)-1)) != 0) {
 120         st->print_cr("<misaligned>");
 121       } else {
 122         st->print_cr(INTPTR_FORMAT, *src);
 123       }
 124     }
 125   }
 126 }
 127 
 128 void RegisterMap::print() const {
 129   print_on(tty);
 130 }
 131 
 132 #endif
 133 // This returns the pc that if you were in the debugger you'd see. Not
 134 // the idealized value in the frame object. This undoes the magic conversion
 135 // that happens for deoptimized frames. In addition it makes the value the
 136 // hardware would want to see in the native frame. The only user (at this point)
 137 // is deoptimization. It likely no one else should ever use it.
 138 
 139 address frame::raw_pc() const {
 140   if (is_deoptimized_frame()) {
 141     CompiledMethod* cm = cb()->as_compiled_method_or_null();
 142     if (cm->is_method_handle_return(pc()))
 143       return cm->deopt_mh_handler_begin() - pc_return_offset;
 144     else
 145       return cm->deopt_handler_begin() - pc_return_offset;
 146   } else {
 147     return (pc() - pc_return_offset);
 148   }
 149 }
 150 
 151 // Change the pc in a frame object. This does not change the actual pc in
 152 // actual frame. To do that use patch_pc.
 153 //
 154 void frame::set_pc(address   newpc ) {
 155 #ifdef ASSERT
 156   if (_cb != NULL && _cb->is_nmethod()) {
 157     assert(!((nmethod*)_cb)->is_deopt_pc(_pc), "invariant violation");
 158   }
 159 #endif // ASSERT
 160 
 161   // Unsafe to use the is_deoptimized tester after changing pc
 162   _deopt_state = unknown;
 163   _pc = newpc;
 164   _cb = CodeCache::find_blob_unsafe(_pc);
 165 
 166 }
 167 
 168 // type testers
 169 bool frame::is_ignored_frame() const {
 170   return false;  // FIXME: some LambdaForm frames should be ignored
 171 }
 172 bool frame::is_deoptimized_frame() const {
 173   assert(_deopt_state != unknown, "not answerable");
 174   return _deopt_state == is_deoptimized;
 175 }
 176 
 177 bool frame::is_native_frame() const {
 178   return (_cb != NULL &&
 179           _cb->is_nmethod() &&
 180           ((nmethod*)_cb)->is_native_method());
 181 }
 182 
 183 bool frame::is_java_frame() const {
 184   if (is_interpreted_frame()) return true;
 185   if (is_compiled_frame())    return true;
 186   return false;
 187 }
 188 
 189 
 190 bool frame::is_compiled_frame() const {
 191   if (_cb != NULL &&
 192       _cb->is_compiled() &&
 193       ((CompiledMethod*)_cb)->is_java_method()) {
 194     return true;
 195   }
 196   return false;
 197 }
 198 
 199 
 200 bool frame::is_runtime_frame() const {
 201   return (_cb != NULL && _cb->is_runtime_stub());
 202 }
 203 
 204 bool frame::is_safepoint_blob_frame() const {
 205   return (_cb != NULL && _cb->is_safepoint_stub());
 206 }
 207 
 208 // testers
 209 
 210 bool frame::is_first_java_frame() const {
 211   RegisterMap map(JavaThread::current(), false); // No update
 212   frame s;
 213   for (s = sender(&map); !(s.is_java_frame() || s.is_first_frame()); s = s.sender(&map));
 214   return s.is_first_frame();
 215 }
 216 
 217 
 218 bool frame::entry_frame_is_first() const {
 219   return entry_frame_call_wrapper()->is_first_frame();
 220 }
 221 
 222 JavaCallWrapper* frame::entry_frame_call_wrapper_if_safe(JavaThread* thread) const {
 223   JavaCallWrapper** jcw = entry_frame_call_wrapper_addr();
 224   address addr = (address) jcw;
 225 
 226   // addr must be within the usable part of the stack
 227   if (thread->is_in_usable_stack(addr)) {
 228     return *jcw;
 229   }
 230 
 231   return NULL;
 232 }
 233 
 234 bool frame::is_entry_frame_valid(JavaThread* thread) const {
 235   // Validate the JavaCallWrapper an entry frame must have
 236   address jcw = (address)entry_frame_call_wrapper();
 237   if (!thread->is_in_stack_range_excl(jcw, (address)fp())) {
 238     return false;
 239   }
 240 
 241   // Validate sp saved in the java frame anchor
 242   JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
 243   return (jfa->last_Java_sp() > sp());
 244 }
 245 
 246 bool frame::should_be_deoptimized() const {
 247   if (_deopt_state == is_deoptimized ||
 248       !is_compiled_frame() ) return false;
 249   assert(_cb != NULL && _cb->is_compiled(), "must be an nmethod");
 250   CompiledMethod* nm = (CompiledMethod *)_cb;
 251   if (TraceDependencies) {
 252     tty->print("checking (%s) ", nm->is_marked_for_deoptimization() ? "true" : "false");
 253     nm->print_value_on(tty);
 254     tty->cr();
 255   }
 256 
 257   if( !nm->is_marked_for_deoptimization() )
 258     return false;
 259 
 260   // If at the return point, then the frame has already been popped, and
 261   // only the return needs to be executed. Don't deoptimize here.
 262   return !nm->is_at_poll_return(pc());
 263 }
 264 
 265 bool frame::can_be_deoptimized() const {
 266   if (!is_compiled_frame()) return false;
 267   CompiledMethod* nm = (CompiledMethod*)_cb;
 268 
 269   if( !nm->can_be_deoptimized() )
 270     return false;
 271 
 272   return !nm->is_at_poll_return(pc());
 273 }
 274 
 275 void frame::deoptimize(JavaThread* thread) {
 276   assert(thread->frame_anchor()->has_last_Java_frame() &&
 277          thread->frame_anchor()->walkable(), "must be");
 278   // Schedule deoptimization of an nmethod activation with this frame.
 279   assert(_cb != NULL && _cb->is_compiled(), "must be");
 280 
 281   // If the call site is a MethodHandle call site use the MH deopt
 282   // handler.
 283   CompiledMethod* cm = (CompiledMethod*) _cb;
 284   address deopt = cm->is_method_handle_return(pc()) ?
 285                         cm->deopt_mh_handler_begin() :
 286                         cm->deopt_handler_begin();
 287 
 288   // Save the original pc before we patch in the new one
 289   cm->set_original_pc(this, pc());
 290   patch_pc(thread, deopt);
 291 
 292 #ifdef ASSERT
 293   {
 294     RegisterMap map(thread, false);
 295     frame check = thread->last_frame();
 296     while (id() != check.id()) {
 297       check = check.sender(&map);
 298     }
 299     assert(check.is_deoptimized_frame(), "missed deopt");
 300   }
 301 #endif // ASSERT
 302 }
 303 
 304 frame frame::java_sender() const {
 305   RegisterMap map(JavaThread::current(), false);
 306   frame s;
 307   for (s = sender(&map); !(s.is_java_frame() || s.is_first_frame()); s = s.sender(&map)) ;
 308   guarantee(s.is_java_frame(), "tried to get caller of first java frame");
 309   return s;
 310 }
 311 
 312 frame frame::real_sender(RegisterMap* map) const {
 313   frame result = sender(map);
 314   while (result.is_runtime_frame() ||
 315          result.is_ignored_frame()) {
 316     result = result.sender(map);
 317   }
 318   return result;
 319 }
 320 
 321 // Interpreter frames
 322 
 323 
 324 void frame::interpreter_frame_set_locals(intptr_t* locs)  {
 325   assert(is_interpreted_frame(), "Not an interpreted frame");
 326   *interpreter_frame_locals_addr() = locs;
 327 }
 328 
 329 Method* frame::interpreter_frame_method() const {
 330   assert(is_interpreted_frame(), "interpreted frame expected");
 331   Method* m = *interpreter_frame_method_addr();
 332   assert(m->is_method(), "not a Method*");
 333   return m;
 334 }
 335 
 336 void frame::interpreter_frame_set_method(Method* method) {
 337   assert(is_interpreted_frame(), "interpreted frame expected");
 338   *interpreter_frame_method_addr() = method;
 339 }
 340 
 341 void frame::interpreter_frame_set_mirror(oop mirror) {
 342   assert(is_interpreted_frame(), "interpreted frame expected");
 343   *interpreter_frame_mirror_addr() = mirror;
 344 }
 345 
 346 jint frame::interpreter_frame_bci() const {
 347   assert(is_interpreted_frame(), "interpreted frame expected");
 348   address bcp = interpreter_frame_bcp();
 349   return interpreter_frame_method()->bci_from(bcp);
 350 }
 351 
 352 address frame::interpreter_frame_bcp() const {
 353   assert(is_interpreted_frame(), "interpreted frame expected");
 354   address bcp = (address)*interpreter_frame_bcp_addr();
 355   return interpreter_frame_method()->bcp_from(bcp);
 356 }
 357 
 358 void frame::interpreter_frame_set_bcp(address bcp) {
 359   assert(is_interpreted_frame(), "interpreted frame expected");
 360   *interpreter_frame_bcp_addr() = (intptr_t)bcp;
 361 }
 362 
 363 address frame::interpreter_frame_mdp() const {
 364   assert(ProfileInterpreter, "must be profiling interpreter");
 365   assert(is_interpreted_frame(), "interpreted frame expected");
 366   return (address)*interpreter_frame_mdp_addr();
 367 }
 368 
 369 void frame::interpreter_frame_set_mdp(address mdp) {
 370   assert(is_interpreted_frame(), "interpreted frame expected");
 371   assert(ProfileInterpreter, "must be profiling interpreter");
 372   *interpreter_frame_mdp_addr() = (intptr_t)mdp;
 373 }
 374 
 375 BasicObjectLock* frame::next_monitor_in_interpreter_frame(BasicObjectLock* current) const {
 376   assert(is_interpreted_frame(), "Not an interpreted frame");
 377 #ifdef ASSERT
 378   interpreter_frame_verify_monitor(current);
 379 #endif
 380   BasicObjectLock* next = (BasicObjectLock*) (((intptr_t*) current) + interpreter_frame_monitor_size());
 381   return next;
 382 }
 383 
 384 BasicObjectLock* frame::previous_monitor_in_interpreter_frame(BasicObjectLock* current) const {
 385   assert(is_interpreted_frame(), "Not an interpreted frame");
 386 #ifdef ASSERT
 387 //   // This verification needs to be checked before being enabled
 388 //   interpreter_frame_verify_monitor(current);
 389 #endif
 390   BasicObjectLock* previous = (BasicObjectLock*) (((intptr_t*) current) - interpreter_frame_monitor_size());
 391   return previous;
 392 }
 393 
 394 // Interpreter locals and expression stack locations.
 395 
 396 intptr_t* frame::interpreter_frame_local_at(int index) const {
 397   const int n = Interpreter::local_offset_in_bytes(index)/wordSize;
 398   return &((*interpreter_frame_locals_addr())[n]);
 399 }
 400 
 401 intptr_t* frame::interpreter_frame_expression_stack_at(jint offset) const {
 402   const int i = offset * interpreter_frame_expression_stack_direction();
 403   const int n = i * Interpreter::stackElementWords;
 404   return &(interpreter_frame_expression_stack()[n]);
 405 }
 406 
 407 jint frame::interpreter_frame_expression_stack_size() const {
 408   // Number of elements on the interpreter expression stack
 409   // Callers should span by stackElementWords
 410   int element_size = Interpreter::stackElementWords;
 411   size_t stack_size = 0;
 412   if (frame::interpreter_frame_expression_stack_direction() < 0) {
 413     stack_size = (interpreter_frame_expression_stack() -
 414                   interpreter_frame_tos_address() + 1)/element_size;
 415   } else {
 416     stack_size = (interpreter_frame_tos_address() -
 417                   interpreter_frame_expression_stack() + 1)/element_size;
 418   }
 419   assert( stack_size <= (size_t)max_jint, "stack size too big");
 420   return ((jint)stack_size);
 421 }
 422 
 423 
 424 // (frame::interpreter_frame_sender_sp accessor is in frame_<arch>.cpp)
 425 
 426 const char* frame::print_name() const {
 427   if (is_native_frame())      return "Native";
 428   if (is_interpreted_frame()) return "Interpreted";
 429   if (is_compiled_frame()) {
 430     if (is_deoptimized_frame()) return "Deoptimized";
 431     return "Compiled";
 432   }
 433   if (sp() == NULL)            return "Empty";
 434   return "C";
 435 }
 436 
 437 void frame::print_value_on(outputStream* st, JavaThread *thread) const {
 438   NOT_PRODUCT(address begin = pc()-40;)
 439   NOT_PRODUCT(address end   = NULL;)
 440 
 441   st->print("%s frame (sp=" INTPTR_FORMAT " unextended sp=" INTPTR_FORMAT, print_name(), p2i(sp()), p2i(unextended_sp()));
 442   if (sp() != NULL)
 443     st->print(", fp=" INTPTR_FORMAT ", real_fp=" INTPTR_FORMAT ", pc=" INTPTR_FORMAT,
 444               p2i(fp()), p2i(real_fp()), p2i(pc()));
 445 
 446   if (StubRoutines::contains(pc())) {
 447     st->print_cr(")");
 448     st->print("(");
 449     StubCodeDesc* desc = StubCodeDesc::desc_for(pc());
 450     st->print("~Stub::%s", desc->name());
 451     NOT_PRODUCT(begin = desc->begin(); end = desc->end();)
 452   } else if (Interpreter::contains(pc())) {
 453     st->print_cr(")");
 454     st->print("(");
 455     InterpreterCodelet* desc = Interpreter::codelet_containing(pc());
 456     if (desc != NULL) {
 457       st->print("~");
 458       desc->print_on(st);
 459       NOT_PRODUCT(begin = desc->code_begin(); end = desc->code_end();)
 460     } else {
 461       st->print("~interpreter");
 462     }
 463   }
 464   st->print_cr(")");
 465 
 466   if (_cb != NULL) {
 467     st->print("     ");
 468     _cb->print_value_on(st);
 469     st->cr();
 470 #ifndef PRODUCT
 471     if (end == NULL) {
 472       begin = _cb->code_begin();
 473       end   = _cb->code_end();
 474     }
 475 #endif
 476   }
 477   NOT_PRODUCT(if (WizardMode && Verbose) Disassembler::decode(begin, end);)
 478 }
 479 
 480 
 481 void frame::print_on(outputStream* st) const {
 482   print_value_on(st,NULL);
 483   if (is_interpreted_frame()) {
 484     interpreter_frame_print_on(st);
 485   }
 486 }
 487 
 488 
 489 void frame::interpreter_frame_print_on(outputStream* st) const {
 490 #ifndef PRODUCT
 491   assert(is_interpreted_frame(), "Not an interpreted frame");
 492   jint i;
 493   for (i = 0; i < interpreter_frame_method()->max_locals(); i++ ) {
 494     intptr_t x = *interpreter_frame_local_at(i);
 495     st->print(" - local  [" INTPTR_FORMAT "]", x);
 496     st->fill_to(23);
 497     st->print_cr("; #%d", i);
 498   }
 499   for (i = interpreter_frame_expression_stack_size() - 1; i >= 0; --i ) {
 500     intptr_t x = *interpreter_frame_expression_stack_at(i);
 501     st->print(" - stack  [" INTPTR_FORMAT "]", x);
 502     st->fill_to(23);
 503     st->print_cr("; #%d", i);
 504   }
 505   // locks for synchronization
 506   for (BasicObjectLock* current = interpreter_frame_monitor_end();
 507        current < interpreter_frame_monitor_begin();
 508        current = next_monitor_in_interpreter_frame(current)) {
 509     st->print(" - obj    [");
 510     current->obj()->print_value_on(st);
 511     st->print_cr("]");
 512     st->print(" - lock   [");
 513     current->lock()->print_on(st, current->obj());
 514     st->print_cr("]");
 515   }
 516   // monitor
 517   st->print_cr(" - monitor[" INTPTR_FORMAT "]", p2i(interpreter_frame_monitor_begin()));
 518   // bcp
 519   st->print(" - bcp    [" INTPTR_FORMAT "]", p2i(interpreter_frame_bcp()));
 520   st->fill_to(23);
 521   st->print_cr("; @%d", interpreter_frame_bci());
 522   // locals
 523   st->print_cr(" - locals [" INTPTR_FORMAT "]", p2i(interpreter_frame_local_at(0)));
 524   // method
 525   st->print(" - method [" INTPTR_FORMAT "]", p2i(interpreter_frame_method()));
 526   st->fill_to(23);
 527   st->print("; ");
 528   interpreter_frame_method()->print_name(st);
 529   st->cr();
 530 #endif
 531 }
 532 
 533 // Print whether the frame is in the VM or OS indicating a HotSpot problem.
 534 // Otherwise, it's likely a bug in the native library that the Java code calls,
 535 // hopefully indicating where to submit bugs.
 536 void frame::print_C_frame(outputStream* st, char* buf, int buflen, address pc) {
 537   // C/C++ frame
 538   bool in_vm = os::address_is_in_vm(pc);
 539   st->print(in_vm ? "V" : "C");
 540 
 541   int offset;
 542   bool found;
 543 
 544   if (buf == NULL || buflen < 1) return;
 545   // libname
 546   buf[0] = '\0';
 547   found = os::dll_address_to_library_name(pc, buf, buflen, &offset);
 548   if (found && buf[0] != '\0') {
 549     // skip directory names
 550     const char *p1, *p2;
 551     p1 = buf;
 552     int len = (int)strlen(os::file_separator());
 553     while ((p2 = strstr(p1, os::file_separator())) != NULL) p1 = p2 + len;
 554     st->print("  [%s+0x%x]", p1, offset);
 555   } else {
 556     st->print("  " PTR_FORMAT, p2i(pc));
 557   }
 558 
 559   found = os::dll_address_to_function_name(pc, buf, buflen, &offset);
 560   if (found) {
 561     st->print("  %s+0x%x", buf, offset);
 562   }
 563 }
 564 
 565 // frame::print_on_error() is called by fatal error handler. Notice that we may
 566 // crash inside this function if stack frame is corrupted. The fatal error
 567 // handler can catch and handle the crash. Here we assume the frame is valid.
 568 //
 569 // First letter indicates type of the frame:
 570 //    J: Java frame (compiled)
 571 //    j: Java frame (interpreted)
 572 //    V: VM frame (C/C++)
 573 //    v: Other frames running VM generated code (e.g. stubs, adapters, etc.)
 574 //    C: C/C++ frame
 575 //
 576 // We don't need detailed frame type as that in frame::print_name(). "C"
 577 // suggests the problem is in user lib; everything else is likely a VM bug.
 578 
 579 void frame::print_on_error(outputStream* st, char* buf, int buflen, bool verbose) const {
 580   if (_cb != NULL) {
 581     if (Interpreter::contains(pc())) {
 582       Method* m = this->interpreter_frame_method();
 583       if (m != NULL) {
 584         m->name_and_sig_as_C_string(buf, buflen);
 585         st->print("j  %s", buf);
 586         st->print("+%d", this->interpreter_frame_bci());
 587         ModuleEntry* module = m->method_holder()->module();
 588         if (module->is_named()) {
 589           module->name()->as_C_string(buf, buflen);
 590           st->print(" %s", buf);
 591           if (module->version() != NULL) {
 592             module->version()->as_C_string(buf, buflen);
 593             st->print("@%s", buf);
 594           }
 595         }
 596       } else {
 597         st->print("j  " PTR_FORMAT, p2i(pc()));
 598       }
 599     } else if (StubRoutines::contains(pc())) {
 600       StubCodeDesc* desc = StubCodeDesc::desc_for(pc());
 601       if (desc != NULL) {
 602         st->print("v  ~StubRoutines::%s", desc->name());
 603       } else {
 604         st->print("v  ~StubRoutines::" PTR_FORMAT, p2i(pc()));
 605       }
 606     } else if (_cb->is_buffer_blob()) {
 607       st->print("v  ~BufferBlob::%s", ((BufferBlob *)_cb)->name());
 608     } else if (_cb->is_compiled()) {
 609       CompiledMethod* cm = (CompiledMethod*)_cb;
 610       Method* m = cm->method();
 611       if (m != NULL) {
 612         if (cm->is_nmethod()) {
 613           nmethod* nm = cm->as_nmethod();
 614           st->print("J %d%s", nm->compile_id(), (nm->is_osr_method() ? "%" : ""));
 615           st->print(" %s", nm->compiler_name());
 616         }
 617         m->name_and_sig_as_C_string(buf, buflen);
 618         st->print(" %s", buf);
 619         ModuleEntry* module = m->method_holder()->module();
 620         if (module->is_named()) {
 621           module->name()->as_C_string(buf, buflen);
 622           st->print(" %s", buf);
 623           if (module->version() != NULL) {
 624             module->version()->as_C_string(buf, buflen);
 625             st->print("@%s", buf);
 626           }
 627         }
 628         st->print(" (%d bytes) @ " PTR_FORMAT " [" PTR_FORMAT "+" INTPTR_FORMAT "]",
 629                   m->code_size(), p2i(_pc), p2i(_cb->code_begin()), _pc - _cb->code_begin());
 630 #if INCLUDE_JVMCI
 631         if (cm->is_nmethod()) {
 632           nmethod* nm = cm->as_nmethod();
 633           const char* jvmciName = nm->jvmci_name();
 634           if (jvmciName != NULL) {
 635             st->print(" (%s)", jvmciName);
 636           }
 637         }
 638 #endif
 639       } else {
 640         st->print("J  " PTR_FORMAT, p2i(pc()));
 641       }
 642     } else if (_cb->is_runtime_stub()) {
 643       st->print("v  ~RuntimeStub::%s", ((RuntimeStub *)_cb)->name());
 644     } else if (_cb->is_deoptimization_stub()) {
 645       st->print("v  ~DeoptimizationBlob");
 646     } else if (_cb->is_exception_stub()) {
 647       st->print("v  ~ExceptionBlob");
 648     } else if (_cb->is_safepoint_stub()) {
 649       st->print("v  ~SafepointBlob");
 650     } else if (_cb->is_adapter_blob()) {
 651       st->print("v  ~AdapterBlob");
 652     } else if (_cb->is_vtable_blob()) {
 653       st->print("v  ~VtableBlob");
 654     } else if (_cb->is_method_handles_adapter_blob()) {
 655       st->print("v  ~MethodHandlesAdapterBlob");
 656     } else if (_cb->is_uncommon_trap_stub()) {
 657       st->print("v  ~UncommonTrapBlob");
 658     } else {
 659       st->print("v  blob " PTR_FORMAT, p2i(pc()));
 660     }
 661   } else {
 662     print_C_frame(st, buf, buflen, pc());
 663   }
 664 }
 665 
 666 
 667 /*
 668   The interpreter_frame_expression_stack_at method in the case of SPARC needs the
 669   max_stack value of the method in order to compute the expression stack address.
 670   It uses the Method* in order to get the max_stack value but during GC this
 671   Method* value saved on the frame is changed by reverse_and_push and hence cannot
 672   be used. So we save the max_stack value in the FrameClosure object and pass it
 673   down to the interpreter_frame_expression_stack_at method
 674 */
 675 class InterpreterFrameClosure : public OffsetClosure {
 676  private:
 677   const frame* _fr;
 678   OopClosure*  _f;
 679   int          _max_locals;
 680   int          _max_stack;
 681 
 682  public:
 683   InterpreterFrameClosure(const frame* fr, int max_locals, int max_stack,
 684                           OopClosure* f) {
 685     _fr         = fr;
 686     _max_locals = max_locals;
 687     _max_stack  = max_stack;
 688     _f          = f;
 689   }
 690 
 691   void offset_do(int offset) {
 692     oop* addr;
 693     if (offset < _max_locals) {
 694       addr = (oop*) _fr->interpreter_frame_local_at(offset);
 695       assert((intptr_t*)addr >= _fr->sp(), "must be inside the frame");
 696       _f->do_oop(addr);
 697     } else {
 698       addr = (oop*) _fr->interpreter_frame_expression_stack_at((offset - _max_locals));
 699       // In case of exceptions, the expression stack is invalid and the esp will be reset to express
 700       // this condition. Therefore, we call f only if addr is 'inside' the stack (i.e., addr >= esp for Intel).
 701       bool in_stack;
 702       if (frame::interpreter_frame_expression_stack_direction() > 0) {
 703         in_stack = (intptr_t*)addr <= _fr->interpreter_frame_tos_address();
 704       } else {
 705         in_stack = (intptr_t*)addr >= _fr->interpreter_frame_tos_address();
 706       }
 707       if (in_stack) {
 708         _f->do_oop(addr);
 709       }
 710     }
 711   }
 712 
 713   int max_locals()  { return _max_locals; }
 714 };
 715 
 716 
 717 class InterpretedArgumentOopFinder: public SignatureIterator {
 718  private:
 719   OopClosure*  _f;             // Closure to invoke
 720   int          _offset;        // TOS-relative offset, decremented with each argument
 721   bool         _has_receiver;  // true if the callee has a receiver
 722   const frame* _fr;
 723 
 724   friend class SignatureIterator;  // so do_parameters_on can call do_type
 725   void do_type(BasicType type) {
 726     _offset -= parameter_type_word_count(type);
 727     if (is_reference_type(type)) oop_offset_do();
 728    }
 729 
 730   void oop_offset_do() {
 731     oop* addr;
 732     addr = (oop*)_fr->interpreter_frame_tos_at(_offset);
 733     _f->do_oop(addr);
 734   }
 735 
 736  public:
 737   InterpretedArgumentOopFinder(Symbol* signature, bool has_receiver, const frame* fr, OopClosure* f) : SignatureIterator(signature), _has_receiver(has_receiver) {
 738     // compute size of arguments
 739     int args_size = ArgumentSizeComputer(signature).size() + (has_receiver ? 1 : 0);
 740     assert(!fr->is_interpreted_frame() ||
 741            args_size <= fr->interpreter_frame_expression_stack_size(),
 742             "args cannot be on stack anymore");
 743     // initialize InterpretedArgumentOopFinder
 744     _f         = f;
 745     _fr        = fr;
 746     _offset    = args_size;
 747   }
 748 
 749   void oops_do() {
 750     if (_has_receiver) {
 751       --_offset;
 752       oop_offset_do();
 753     }
 754     do_parameters_on(this);
 755   }
 756 };
 757 
 758 
 759 // Entry frame has following form (n arguments)
 760 //         +-----------+
 761 //   sp -> |  last arg |
 762 //         +-----------+
 763 //         :    :::    :
 764 //         +-----------+
 765 // (sp+n)->|  first arg|
 766 //         +-----------+
 767 
 768 
 769 
 770 // visits and GC's all the arguments in entry frame
 771 class EntryFrameOopFinder: public SignatureIterator {
 772  private:
 773   bool         _is_static;
 774   int          _offset;
 775   const frame* _fr;
 776   OopClosure*  _f;
 777 
 778   friend class SignatureIterator;  // so do_parameters_on can call do_type
 779   void do_type(BasicType type) {
 780     // decrement offset before processing the type
 781     _offset -= parameter_type_word_count(type);
 782     assert (_offset >= 0, "illegal offset");
 783     if (is_reference_type(type))  oop_at_offset_do(_offset);
 784  }
 785 
 786   void oop_at_offset_do(int offset) {
 787     assert (offset >= 0, "illegal offset");
 788     oop* addr = (oop*) _fr->entry_frame_argument_at(offset);
 789     _f->do_oop(addr);
 790   }
 791 
 792  public:
 793   EntryFrameOopFinder(const frame* frame, Symbol* signature, bool is_static) : SignatureIterator(signature) {
 794     _f = NULL; // will be set later
 795     _fr = frame;
 796     _is_static = is_static;
 797     _offset = ArgumentSizeComputer(signature).size();  // pre-decremented down to zero
 798   }
 799 
 800   void arguments_do(OopClosure* f) {
 801     _f = f;
 802     if (!_is_static)  oop_at_offset_do(_offset); // do the receiver
 803     do_parameters_on(this);
 804   }
 805 
 806 };
 807 
 808 oop* frame::interpreter_callee_receiver_addr(Symbol* signature) {
 809   ArgumentSizeComputer asc(signature);
 810   int size = asc.size();
 811   return (oop *)interpreter_frame_tos_at(size);
 812 }
 813 
 814 
 815 void frame::oops_interpreted_do(OopClosure* f, const RegisterMap* map, bool query_oop_map_cache) const {
 816   assert(is_interpreted_frame(), "Not an interpreted frame");
 817   assert(map != NULL, "map must be set");
 818   Thread *thread = Thread::current();
 819   methodHandle m (thread, interpreter_frame_method());
 820   jint      bci = interpreter_frame_bci();
 821 
 822   assert(!Universe::heap()->is_in(m()),
 823           "must be valid oop");
 824   assert(m->is_method(), "checking frame value");
 825   assert((m->is_native() && bci == 0)  ||
 826          (!m->is_native() && bci >= 0 && bci < m->code_size()),
 827          "invalid bci value");
 828 
 829   // Handle the monitor elements in the activation
 830   for (
 831     BasicObjectLock* current = interpreter_frame_monitor_end();
 832     current < interpreter_frame_monitor_begin();
 833     current = next_monitor_in_interpreter_frame(current)
 834   ) {
 835 #ifdef ASSERT
 836     interpreter_frame_verify_monitor(current);
 837 #endif
 838     current->oops_do(f);
 839   }
 840 
 841   if (m->is_native()) {
 842     f->do_oop(interpreter_frame_temp_oop_addr());
 843   }
 844 
 845   // The method pointer in the frame might be the only path to the method's
 846   // klass, and the klass needs to be kept alive while executing. The GCs
 847   // don't trace through method pointers, so the mirror of the method's klass
 848   // is installed as a GC root.
 849   f->do_oop(interpreter_frame_mirror_addr());
 850 
 851   int max_locals = m->is_native() ? m->size_of_parameters() : m->max_locals();
 852 
 853   Symbol* signature = NULL;
 854   bool has_receiver = false;
 855 
 856   // Process a callee's arguments if we are at a call site
 857   // (i.e., if we are at an invoke bytecode)
 858   // This is used sometimes for calling into the VM, not for another
 859   // interpreted or compiled frame.
 860   if (!m->is_native()) {
 861     Bytecode_invoke call = Bytecode_invoke_check(m, bci);
 862     if (call.is_valid()) {
 863       signature = call.signature();
 864       has_receiver = call.has_receiver();
 865       if (map->include_argument_oops() &&
 866           interpreter_frame_expression_stack_size() > 0) {
 867         ResourceMark rm(thread);  // is this right ???
 868         // we are at a call site & the expression stack is not empty
 869         // => process callee's arguments
 870         //
 871         // Note: The expression stack can be empty if an exception
 872         //       occurred during method resolution/execution. In all
 873         //       cases we empty the expression stack completely be-
 874         //       fore handling the exception (the exception handling
 875         //       code in the interpreter calls a blocking runtime
 876         //       routine which can cause this code to be executed).
 877         //       (was bug gri 7/27/98)
 878         oops_interpreted_arguments_do(signature, has_receiver, f);
 879       }
 880     }
 881   }
 882 
 883   InterpreterFrameClosure blk(this, max_locals, m->max_stack(), f);
 884 
 885   // process locals & expression stack
 886   InterpreterOopMap mask;
 887   if (query_oop_map_cache) {
 888     m->mask_for(bci, &mask);
 889   } else {
 890     OopMapCache::compute_one_oop_map(m, bci, &mask);
 891   }
 892   mask.iterate_oop(&blk);
 893 }
 894 
 895 
 896 void frame::oops_interpreted_arguments_do(Symbol* signature, bool has_receiver, OopClosure* f) const {
 897   InterpretedArgumentOopFinder finder(signature, has_receiver, this, f);
 898   finder.oops_do();
 899 }
 900 
 901 void frame::oops_code_blob_do(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* reg_map,
 902                               DerivedPointerIterationMode derived_mode) const {
 903   assert(_cb != NULL, "sanity check");
 904   if (_cb->oop_maps() != NULL) {
 905     OopMapSet::oops_do(this, reg_map, f, derived_mode);
 906 
 907     // Preserve potential arguments for a callee. We handle this by dispatching
 908     // on the codeblob. For c2i, we do
 909     if (reg_map->include_argument_oops()) {
 910       _cb->preserve_callee_argument_oops(*this, reg_map, f);
 911     }
 912   }
 913   // In cases where perm gen is collected, GC will want to mark
 914   // oops referenced from nmethods active on thread stacks so as to
 915   // prevent them from being collected. However, this visit should be
 916   // restricted to certain phases of the collection only. The
 917   // closure decides how it wants nmethods to be traced.
 918   if (cf != NULL)
 919     cf->do_code_blob(_cb);
 920 }
 921 
 922 class CompiledArgumentOopFinder: public SignatureIterator {
 923  protected:
 924   OopClosure*     _f;
 925   int             _offset;        // the current offset, incremented with each argument
 926   bool            _has_receiver;  // true if the callee has a receiver
 927   bool            _has_appendix;  // true if the call has an appendix
 928   frame           _fr;
 929   RegisterMap*    _reg_map;
 930   int             _arg_size;
 931   VMRegPair*      _regs;        // VMReg list of arguments
 932 
 933   friend class SignatureIterator;  // so do_parameters_on can call do_type
 934   void do_type(BasicType type) {
 935     if (is_reference_type(type))  handle_oop_offset();
 936     _offset += parameter_type_word_count(type);
 937   }
 938 
 939   virtual void handle_oop_offset() {
 940     // Extract low order register number from register array.
 941     // In LP64-land, the high-order bits are valid but unhelpful.
 942     VMReg reg = _regs[_offset].first();
 943     oop *loc = _fr.oopmapreg_to_oop_location(reg, _reg_map);
 944     assert(loc != NULL, "missing register map entry");
 945     _f->do_oop(loc);
 946   }
 947 
 948  public:
 949   CompiledArgumentOopFinder(Symbol* signature, bool has_receiver, bool has_appendix, OopClosure* f, frame fr, const RegisterMap* reg_map)
 950     : SignatureIterator(signature) {
 951 
 952     // initialize CompiledArgumentOopFinder
 953     _f         = f;
 954     _offset    = 0;
 955     _has_receiver = has_receiver;
 956     _has_appendix = has_appendix;
 957     _fr        = fr;
 958     _reg_map   = (RegisterMap*)reg_map;
 959     _arg_size  = ArgumentSizeComputer(signature).size() + (has_receiver ? 1 : 0) + (has_appendix ? 1 : 0);
 960 
 961     int arg_size;
 962     _regs = SharedRuntime::find_callee_arguments(signature, has_receiver, has_appendix, &arg_size);
 963     assert(arg_size == _arg_size, "wrong arg size");
 964   }
 965 
 966   void oops_do() {
 967     if (_has_receiver) {
 968       handle_oop_offset();
 969       _offset++;
 970     }
 971     do_parameters_on(this);
 972     if (_has_appendix) {
 973       handle_oop_offset();
 974       _offset++;
 975     }
 976   }
 977 };
 978 
 979 void frame::oops_compiled_arguments_do(Symbol* signature, bool has_receiver, bool has_appendix,
 980                                        const RegisterMap* reg_map, OopClosure* f) const {
 981   ResourceMark rm;
 982   CompiledArgumentOopFinder finder(signature, has_receiver, has_appendix, f, *this, reg_map);
 983   finder.oops_do();
 984 }
 985 
 986 
 987 // Get receiver out of callers frame, i.e. find parameter 0 in callers
 988 // frame.  Consult ADLC for where parameter 0 is to be found.  Then
 989 // check local reg_map for it being a callee-save register or argument
 990 // register, both of which are saved in the local frame.  If not found
 991 // there, it must be an in-stack argument of the caller.
 992 // Note: caller.sp() points to callee-arguments
 993 oop frame::retrieve_receiver(RegisterMap* reg_map) {
 994   frame caller = *this;
 995 
 996   // First consult the ADLC on where it puts parameter 0 for this signature.
 997   VMReg reg = SharedRuntime::name_for_receiver();
 998   oop* oop_adr = caller.oopmapreg_to_oop_location(reg, reg_map);
 999   if (oop_adr == NULL) {
1000     guarantee(oop_adr != NULL, "bad register save location");
1001     return NULL;
1002   }
1003   oop r = *oop_adr;
1004   assert(Universe::heap()->is_in_or_null(r), "bad receiver: " INTPTR_FORMAT " (" INTX_FORMAT ")", p2i(r), p2i(r));
1005   return r;
1006 }
1007 
1008 
1009 BasicLock* frame::get_native_monitor() {
1010   nmethod* nm = (nmethod*)_cb;
1011   assert(_cb != NULL && _cb->is_nmethod() && nm->method()->is_native(),
1012          "Should not call this unless it's a native nmethod");
1013   int byte_offset = in_bytes(nm->native_basic_lock_sp_offset());
1014   assert(byte_offset >= 0, "should not see invalid offset");
1015   return (BasicLock*) &sp()[byte_offset / wordSize];
1016 }
1017 
1018 oop frame::get_native_receiver() {
1019   nmethod* nm = (nmethod*)_cb;
1020   assert(_cb != NULL && _cb->is_nmethod() && nm->method()->is_native(),
1021          "Should not call this unless it's a native nmethod");
1022   int byte_offset = in_bytes(nm->native_receiver_sp_offset());
1023   assert(byte_offset >= 0, "should not see invalid offset");
1024   oop owner = ((oop*) sp())[byte_offset / wordSize];
1025   assert( Universe::heap()->is_in(owner), "bad receiver" );
1026   return owner;
1027 }
1028 
1029 void frame::oops_entry_do(OopClosure* f, const RegisterMap* map) const {
1030   assert(map != NULL, "map must be set");
1031   if (map->include_argument_oops()) {
1032     // must collect argument oops, as nobody else is doing it
1033     Thread *thread = Thread::current();
1034     methodHandle m (thread, entry_frame_call_wrapper()->callee_method());
1035     EntryFrameOopFinder finder(this, m->signature(), m->is_static());
1036     finder.arguments_do(f);
1037   }
1038   // Traverse the Handle Block saved in the entry frame
1039   entry_frame_call_wrapper()->oops_do(f);
1040 }
1041 
1042 void frame::oops_do(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* map,
1043                     DerivedPointerIterationMode derived_mode) const {
1044   oops_do_internal(f, cf, map, true, derived_mode);
1045 }
1046 
1047 void frame::oops_do(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* map) const {
1048 #if COMPILER2_OR_JVMCI
1049   oops_do_internal(f, cf, map, true, DerivedPointerTable::is_active() ?
1050                                      DerivedPointerIterationMode::_with_table :
1051                                      DerivedPointerIterationMode::_ignore);
1052 #else
1053   oops_do_internal(f, cf, map, true, DerivedPointerIterationMode::_ignore);
1054 #endif
1055 }
1056 
1057 void frame::oops_do_internal(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* map,
1058                              bool use_interpreter_oop_map_cache, DerivedPointerIterationMode derived_mode) const {
1059 #ifndef PRODUCT
1060   // simulate GC crash here to dump java thread in error report
1061   if (CrashGCForDumpingJavaThread) {
1062     char *t = NULL;
1063     *t = 'c';
1064   }
1065 #endif
1066   if (is_interpreted_frame()) {
1067     oops_interpreted_do(f, map, use_interpreter_oop_map_cache);
1068   } else if (is_entry_frame()) {
1069     oops_entry_do(f, map);
1070   } else if (is_optimized_entry_frame()) {
1071     _cb->as_optimized_entry_blob()->oops_do(f, *this);
1072   } else if (CodeCache::contains(pc())) {
1073     oops_code_blob_do(f, cf, map, derived_mode);
1074   } else {
1075     ShouldNotReachHere();
1076   }
1077 }
1078 
1079 void frame::nmethods_do(CodeBlobClosure* cf) const {
1080   if (_cb != NULL && _cb->is_nmethod()) {
1081     cf->do_code_blob(_cb);
1082   }
1083 }
1084 
1085 
1086 // Call f closure on the interpreted Method*s in the stack.
1087 void frame::metadata_do(MetadataClosure* f) const {
1088   ResourceMark rm;
1089   if (is_interpreted_frame()) {
1090     Method* m = this->interpreter_frame_method();
1091     assert(m != NULL, "expecting a method in this frame");
1092     f->do_metadata(m);
1093   }
1094 }
1095 
1096 void frame::verify(const RegisterMap* map) const {
1097   // for now make sure receiver type is correct
1098   if (is_interpreted_frame()) {
1099     Method* method = interpreter_frame_method();
1100     guarantee(method->is_method(), "method is wrong in frame::verify");
1101     if (!method->is_static()) {
1102       // fetch the receiver
1103       oop* p = (oop*) interpreter_frame_local_at(0);
1104       // make sure we have the right receiver type
1105     }
1106   }
1107 #if COMPILER2_OR_JVMCI
1108   assert(DerivedPointerTable::is_empty(), "must be empty before verify");
1109 #endif
1110   if (map->update_map()) { // The map has to be up-to-date for the current frame
1111     oops_do_internal(&VerifyOopClosure::verify_oop, NULL, map, false, DerivedPointerIterationMode::_ignore);
1112   }
1113 }
1114 
1115 
1116 #ifdef ASSERT
1117 bool frame::verify_return_pc(address x) {
1118   if (StubRoutines::returns_to_call_stub(x)) {
1119     return true;
1120   }
1121   if (CodeCache::contains(x)) {
1122     return true;
1123   }
1124   if (Interpreter::contains(x)) {
1125     return true;
1126   }
1127   return false;
1128 }
1129 #endif
1130 
1131 #ifdef ASSERT
1132 void frame::interpreter_frame_verify_monitor(BasicObjectLock* value) const {
1133   assert(is_interpreted_frame(), "Not an interpreted frame");
1134   // verify that the value is in the right part of the frame
1135   address low_mark  = (address) interpreter_frame_monitor_end();
1136   address high_mark = (address) interpreter_frame_monitor_begin();
1137   address current   = (address) value;
1138 
1139   const int monitor_size = frame::interpreter_frame_monitor_size();
1140   guarantee((high_mark - current) % monitor_size  ==  0         , "Misaligned top of BasicObjectLock*");
1141   guarantee( high_mark > current                                , "Current BasicObjectLock* higher than high_mark");
1142 
1143   guarantee((current - low_mark) % monitor_size  ==  0         , "Misaligned bottom of BasicObjectLock*");
1144   guarantee( current >= low_mark                               , "Current BasicObjectLock* below than low_mark");
1145 }
1146 #endif
1147 
1148 #ifndef PRODUCT
1149 // callers need a ResourceMark because of name_and_sig_as_C_string() usage,
1150 // RA allocated string is returned to the caller
1151 void frame::describe(FrameValues& values, int frame_no) {
1152   // boundaries: sp and the 'real' frame pointer
1153   values.describe(-1, sp(), err_msg("sp for #%d", frame_no), 1);
1154   intptr_t* frame_pointer = real_fp(); // Note: may differ from fp()
1155 
1156   // print frame info at the highest boundary
1157   intptr_t* info_address = MAX2(sp(), frame_pointer);
1158 
1159   if (info_address != frame_pointer) {
1160     // print frame_pointer explicitly if not marked by the frame info
1161     values.describe(-1, frame_pointer, err_msg("frame pointer for #%d", frame_no), 1);
1162   }
1163 
1164   if (is_entry_frame() || is_compiled_frame() || is_interpreted_frame() || is_native_frame()) {
1165     // Label values common to most frames
1166     values.describe(-1, unextended_sp(), err_msg("unextended_sp for #%d", frame_no));
1167   }
1168 
1169   if (is_interpreted_frame()) {
1170     Method* m = interpreter_frame_method();
1171     int bci = interpreter_frame_bci();
1172 
1173     // Label the method and current bci
1174     values.describe(-1, info_address,
1175                     FormatBuffer<1024>("#%d method %s @ %d", frame_no, m->name_and_sig_as_C_string(), bci), 2);
1176     values.describe(-1, info_address,
1177                     err_msg("- %d locals %d max stack", m->max_locals(), m->max_stack()), 1);
1178     if (m->max_locals() > 0) {
1179       intptr_t* l0 = interpreter_frame_local_at(0);
1180       intptr_t* ln = interpreter_frame_local_at(m->max_locals() - 1);
1181       values.describe(-1, MAX2(l0, ln), err_msg("locals for #%d", frame_no), 1);
1182       // Report each local and mark as owned by this frame
1183       for (int l = 0; l < m->max_locals(); l++) {
1184         intptr_t* l0 = interpreter_frame_local_at(l);
1185         values.describe(frame_no, l0, err_msg("local %d", l));
1186       }
1187     }
1188 
1189     // Compute the actual expression stack size
1190     InterpreterOopMap mask;
1191     OopMapCache::compute_one_oop_map(methodHandle(Thread::current(), m), bci, &mask);
1192     intptr_t* tos = NULL;
1193     // Report each stack element and mark as owned by this frame
1194     for (int e = 0; e < mask.expression_stack_size(); e++) {
1195       tos = MAX2(tos, interpreter_frame_expression_stack_at(e));
1196       values.describe(frame_no, interpreter_frame_expression_stack_at(e),
1197                       err_msg("stack %d", e));
1198     }
1199     if (tos != NULL) {
1200       values.describe(-1, tos, err_msg("expression stack for #%d", frame_no), 1);
1201     }
1202     if (interpreter_frame_monitor_begin() != interpreter_frame_monitor_end()) {
1203       values.describe(frame_no, (intptr_t*)interpreter_frame_monitor_begin(), "monitors begin");
1204       values.describe(frame_no, (intptr_t*)interpreter_frame_monitor_end(), "monitors end");
1205     }
1206   } else if (is_entry_frame()) {
1207     // For now just label the frame
1208     values.describe(-1, info_address, err_msg("#%d entry frame", frame_no), 2);
1209   } else if (is_compiled_frame()) {
1210     // For now just label the frame
1211     CompiledMethod* cm = (CompiledMethod*)cb();
1212     values.describe(-1, info_address,
1213                     FormatBuffer<1024>("#%d nmethod " INTPTR_FORMAT " for method J %s%s", frame_no,
1214                                        p2i(cm),
1215                                        cm->method()->name_and_sig_as_C_string(),
1216                                        (_deopt_state == is_deoptimized) ?
1217                                        " (deoptimized)" :
1218                                        ((_deopt_state == unknown) ? " (state unknown)" : "")),
1219                     2);
1220   } else if (is_native_frame()) {
1221     // For now just label the frame
1222     nmethod* nm = cb()->as_nmethod_or_null();
1223     values.describe(-1, info_address,
1224                     FormatBuffer<1024>("#%d nmethod " INTPTR_FORMAT " for native method %s", frame_no,
1225                                        p2i(nm), nm->method()->name_and_sig_as_C_string()), 2);
1226   } else {
1227     // provide default info if not handled before
1228     char *info = (char *) "special frame";
1229     if ((_cb != NULL) &&
1230         (_cb->name() != NULL)) {
1231       info = (char *)_cb->name();
1232     }
1233     values.describe(-1, info_address, err_msg("#%d <%s>", frame_no, info), 2);
1234   }
1235 
1236   // platform dependent additional data
1237   describe_pd(values, frame_no);
1238 }
1239 
1240 #endif
1241 
1242 #ifndef PRODUCT
1243 
1244 void FrameValues::describe(int owner, intptr_t* location, const char* description, int priority) {
1245   FrameValue fv;
1246   fv.location = location;
1247   fv.owner = owner;
1248   fv.priority = priority;
1249   fv.description = NEW_RESOURCE_ARRAY(char, strlen(description) + 1);
1250   strcpy(fv.description, description);
1251   _values.append(fv);
1252 }
1253 
1254 
1255 #ifdef ASSERT
1256 void FrameValues::validate() {
1257   _values.sort(compare);
1258   bool error = false;
1259   FrameValue prev;
1260   prev.owner = -1;
1261   for (int i = _values.length() - 1; i >= 0; i--) {
1262     FrameValue fv = _values.at(i);
1263     if (fv.owner == -1) continue;
1264     if (prev.owner == -1) {
1265       prev = fv;
1266       continue;
1267     }
1268     if (prev.location == fv.location) {
1269       if (fv.owner != prev.owner) {
1270         tty->print_cr("overlapping storage");
1271         tty->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", p2i(prev.location), *prev.location, prev.description);
1272         tty->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", p2i(fv.location), *fv.location, fv.description);
1273         error = true;
1274       }
1275     } else {
1276       prev = fv;
1277     }
1278   }
1279   assert(!error, "invalid layout");
1280 }
1281 #endif // ASSERT
1282 
1283 void FrameValues::print_on(JavaThread* thread, outputStream* st) {
1284   _values.sort(compare);
1285 
1286   // Sometimes values like the fp can be invalid values if the
1287   // register map wasn't updated during the walk.  Trim out values
1288   // that aren't actually in the stack of the thread.
1289   int min_index = 0;
1290   int max_index = _values.length() - 1;
1291   intptr_t* v0 = _values.at(min_index).location;
1292   intptr_t* v1 = _values.at(max_index).location;
1293 
1294   if (thread == Thread::current()) {
1295     while (!thread->is_in_live_stack((address)v0)) {
1296       v0 = _values.at(++min_index).location;
1297     }
1298     while (!thread->is_in_live_stack((address)v1)) {
1299       v1 = _values.at(--max_index).location;
1300     }
1301   } else {
1302     while (!thread->is_in_full_stack((address)v0)) {
1303       v0 = _values.at(++min_index).location;
1304     }
1305     while (!thread->is_in_full_stack((address)v1)) {
1306       v1 = _values.at(--max_index).location;
1307     }
1308   }
1309   intptr_t* min = MIN2(v0, v1);
1310   intptr_t* max = MAX2(v0, v1);
1311   intptr_t* cur = max;
1312   intptr_t* last = NULL;
1313   for (int i = max_index; i >= min_index; i--) {
1314     FrameValue fv = _values.at(i);
1315     while (cur > fv.location) {
1316       st->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT, p2i(cur), *cur);
1317       cur--;
1318     }
1319     if (last == fv.location) {
1320       const char* spacer = "          " LP64_ONLY("        ");
1321       st->print_cr(" %s  %s %s", spacer, spacer, fv.description);
1322     } else {
1323       st->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", p2i(fv.location), *fv.location, fv.description);
1324       last = fv.location;
1325       cur--;
1326     }
1327   }
1328 }
1329 
1330 #endif // ndef PRODUCT