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