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/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 
 469   if (StubRoutines::contains(pc())) {
 470     st->print_cr(")");
 471     st->print("(");
 472     StubCodeDesc* desc = StubCodeDesc::desc_for(pc());
 473     st->print("~Stub::%s", desc->name());
 474     NOT_PRODUCT(begin = desc->begin(); end = desc->end();)
 475   } else if (Interpreter::contains(pc())) {
 476     st->print_cr(")");
 477     st->print("(");
 478     InterpreterCodelet* desc = Interpreter::codelet_containing(pc());
 479     if (desc != NULL) {
 480       st->print("~");
 481       desc->print_on(st);
 482       NOT_PRODUCT(begin = desc->code_begin(); end = desc->code_end();)
 483     } else {
 484       st->print("~interpreter");
 485     }
 486   }
 487   st->print_cr(")");
 488 
 489   if (_cb != NULL) {
 490     st->print("     ");
 491     _cb->print_value_on(st);
 492     st->cr();
 493 #ifndef PRODUCT
 494     if (end == NULL) {
 495       begin = _cb->code_begin();
 496       end   = _cb->code_end();
 497     }
 498 #endif
 499   }
 500   NOT_PRODUCT(if (WizardMode && Verbose) Disassembler::decode(begin, end);)
 501 }
 502 
 503 
 504 void frame::print_on(outputStream* st) const {
 505   print_value_on(st,NULL);
 506   if (is_interpreted_frame()) {
 507     interpreter_frame_print_on(st);
 508   }
 509 }
 510 
 511 
 512 void frame::interpreter_frame_print_on(outputStream* st) const {
 513 #ifndef PRODUCT
 514   assert(is_interpreted_frame(), "Not an interpreted frame");
 515   jint i;
 516   for (i = 0; i < interpreter_frame_method()->max_locals(); i++ ) {
 517     intptr_t x = *interpreter_frame_local_at(i);
 518     st->print(" - local  [" INTPTR_FORMAT "]", x);
 519     st->fill_to(23);
 520     st->print_cr("; #%d", i);
 521   }
 522   for (i = interpreter_frame_expression_stack_size() - 1; i >= 0; --i ) {
 523     intptr_t x = *interpreter_frame_expression_stack_at(i);
 524     st->print(" - stack  [" INTPTR_FORMAT "]", x);
 525     st->fill_to(23);
 526     st->print_cr("; #%d", i);
 527   }
 528   // locks for synchronization
 529   for (BasicObjectLock* current = interpreter_frame_monitor_end();
 530        current < interpreter_frame_monitor_begin();
 531        current = next_monitor_in_interpreter_frame(current)) {
 532     st->print(" - obj    [");
 533     current->obj()->print_value_on(st);
 534     st->print_cr("]");
 535     st->print(" - lock   [");
 536     current->lock()->print_on(st, current->obj());
 537     st->print_cr("]");
 538   }
 539   // monitor
 540   st->print_cr(" - monitor[" INTPTR_FORMAT "]", p2i(interpreter_frame_monitor_begin()));
 541   // bcp
 542   st->print(" - bcp    [" INTPTR_FORMAT "]", p2i(interpreter_frame_bcp()));
 543   st->fill_to(23);
 544   st->print_cr("; @%d", interpreter_frame_bci());
 545   // locals
 546   st->print_cr(" - locals [" INTPTR_FORMAT "]", p2i(interpreter_frame_local_at(0)));
 547   // method
 548   st->print(" - method [" INTPTR_FORMAT "]", p2i(interpreter_frame_method()));
 549   st->fill_to(23);
 550   st->print("; ");
 551   interpreter_frame_method()->print_name(st);
 552   st->cr();
 553 #endif
 554 }
 555 
 556 // Print whether the frame is in the VM or OS indicating a HotSpot problem.
 557 // Otherwise, it's likely a bug in the native library that the Java code calls,
 558 // hopefully indicating where to submit bugs.
 559 void frame::print_C_frame(outputStream* st, char* buf, int buflen, address pc) {
 560   // C/C++ frame
 561   bool in_vm = os::address_is_in_vm(pc);
 562   st->print(in_vm ? "V" : "C");
 563 
 564   int offset;
 565   bool found;
 566 
 567   if (buf == NULL || buflen < 1) return;
 568   // libname
 569   buf[0] = '\0';
 570   found = os::dll_address_to_library_name(pc, buf, buflen, &offset);
 571   if (found && buf[0] != '\0') {
 572     // skip directory names
 573     const char *p1, *p2;
 574     p1 = buf;
 575     int len = (int)strlen(os::file_separator());
 576     while ((p2 = strstr(p1, os::file_separator())) != NULL) p1 = p2 + len;
 577     st->print("  [%s+0x%x]", p1, offset);
 578   } else {
 579     st->print("  " PTR_FORMAT, p2i(pc));
 580   }
 581 
 582   found = os::dll_address_to_function_name(pc, buf, buflen, &offset);
 583   if (found) {
 584     st->print("  %s+0x%x", buf, offset);
 585   }
 586 }
 587 
 588 // frame::print_on_error() is called by fatal error handler. Notice that we may
 589 // crash inside this function if stack frame is corrupted. The fatal error
 590 // handler can catch and handle the crash. Here we assume the frame is valid.
 591 //
 592 // First letter indicates type of the frame:
 593 //    J: Java frame (compiled)
 594 //    j: Java frame (interpreted)
 595 //    V: VM frame (C/C++)
 596 //    v: Other frames running VM generated code (e.g. stubs, adapters, etc.)
 597 //    C: C/C++ frame
 598 //
 599 // We don't need detailed frame type as that in frame::print_name(). "C"
 600 // suggests the problem is in user lib; everything else is likely a VM bug.
 601 
 602 void frame::print_on_error(outputStream* st, char* buf, int buflen, bool verbose) const {
 603   if (_cb != NULL) {
 604     if (Interpreter::contains(pc())) {
 605       Method* m = this->interpreter_frame_method();
 606       if (m != NULL) {
 607         m->name_and_sig_as_C_string(buf, buflen);
 608         st->print("j  %s", buf);
 609         st->print("+%d", this->interpreter_frame_bci());
 610         ModuleEntry* module = m->method_holder()->module();
 611         if (module->is_named()) {
 612           module->name()->as_C_string(buf, buflen);
 613           st->print(" %s", buf);
 614           if (module->version() != NULL) {
 615             module->version()->as_C_string(buf, buflen);
 616             st->print("@%s", buf);
 617           }
 618         }
 619       } else {
 620         st->print("j  " PTR_FORMAT, p2i(pc()));
 621       }
 622     } else if (StubRoutines::contains(pc())) {
 623       StubCodeDesc* desc = StubCodeDesc::desc_for(pc());
 624       if (desc != NULL) {
 625         st->print("v  ~StubRoutines::%s", desc->name());
 626       } else {
 627         st->print("v  ~StubRoutines::" PTR_FORMAT, p2i(pc()));
 628       }
 629     } else if (_cb->is_buffer_blob()) {
 630       st->print("v  ~BufferBlob::%s", ((BufferBlob *)_cb)->name());
 631     } else if (_cb->is_compiled()) {
 632       CompiledMethod* cm = (CompiledMethod*)_cb;
 633       Method* m = cm->method();
 634       if (m != NULL) {
 635         if (cm->is_nmethod()) {
 636           nmethod* nm = cm->as_nmethod();
 637           st->print("J %d%s", nm->compile_id(), (nm->is_osr_method() ? "%" : ""));
 638           st->print(" %s", nm->compiler_name());
 639         }
 640         m->name_and_sig_as_C_string(buf, buflen);
 641         st->print(" %s", buf);
 642         ModuleEntry* module = m->method_holder()->module();
 643         if (module->is_named()) {
 644           module->name()->as_C_string(buf, buflen);
 645           st->print(" %s", buf);
 646           if (module->version() != NULL) {
 647             module->version()->as_C_string(buf, buflen);
 648             st->print("@%s", buf);
 649           }
 650         }
 651         st->print(" (%d bytes) @ " PTR_FORMAT " [" PTR_FORMAT "+" INTPTR_FORMAT "]",
 652                   m->code_size(), p2i(_pc), p2i(_cb->code_begin()), _pc - _cb->code_begin());
 653 #if INCLUDE_JVMCI
 654         if (cm->is_nmethod()) {
 655           nmethod* nm = cm->as_nmethod();
 656           const char* jvmciName = nm->jvmci_name();
 657           if (jvmciName != NULL) {
 658             st->print(" (%s)", jvmciName);
 659           }
 660         }
 661 #endif
 662       } else {
 663         st->print("J  " PTR_FORMAT, p2i(pc()));
 664       }
 665     } else if (_cb->is_runtime_stub()) {
 666       st->print("v  ~RuntimeStub::%s", ((RuntimeStub *)_cb)->name());
 667     } else if (_cb->is_deoptimization_stub()) {
 668       st->print("v  ~DeoptimizationBlob");
 669     } else if (_cb->is_exception_stub()) {
 670       st->print("v  ~ExceptionBlob");
 671     } else if (_cb->is_safepoint_stub()) {
 672       st->print("v  ~SafepointBlob");
 673     } else if (_cb->is_adapter_blob()) {
 674       st->print("v  ~AdapterBlob");
 675     } else if (_cb->is_vtable_blob()) {
 676       st->print("v  ~VtableBlob");
 677     } else if (_cb->is_method_handles_adapter_blob()) {
 678       st->print("v  ~MethodHandlesAdapterBlob");
 679     } else if (_cb->is_uncommon_trap_stub()) {
 680       st->print("v  ~UncommonTrapBlob");
 681     } else {
 682       st->print("v  blob " PTR_FORMAT, p2i(pc()));
 683     }
 684   } else {
 685     print_C_frame(st, buf, buflen, pc());
 686   }
 687 }
 688 
 689 
 690 /*
 691   The interpreter_frame_expression_stack_at method in the case of SPARC needs the
 692   max_stack value of the method in order to compute the expression stack address.
 693   It uses the Method* in order to get the max_stack value but during GC this
 694   Method* value saved on the frame is changed by reverse_and_push and hence cannot
 695   be used. So we save the max_stack value in the FrameClosure object and pass it
 696   down to the interpreter_frame_expression_stack_at method
 697 */
 698 class InterpreterFrameClosure : public OffsetClosure {
 699  private:
 700   const frame* _fr;
 701   OopClosure*  _f;
 702   int          _max_locals;
 703   int          _max_stack;
 704 
 705  public:
 706   InterpreterFrameClosure(const frame* fr, int max_locals, int max_stack,
 707                           OopClosure* f, BufferedValueClosure* bvt_f) {
 708     _fr         = fr;
 709     _max_locals = max_locals;
 710     _max_stack  = max_stack;
 711     _f          = f;
 712   }
 713 
 714   void offset_do(int offset) {
 715     oop* addr;
 716     if (offset < _max_locals) {
 717       addr = (oop*) _fr->interpreter_frame_local_at(offset);
 718       assert((intptr_t*)addr >= _fr->sp(), "must be inside the frame");
 719       if (_f != NULL) {
 720         _f->do_oop(addr);
 721       }
 722     } else {
 723       addr = (oop*) _fr->interpreter_frame_expression_stack_at((offset - _max_locals));
 724       // In case of exceptions, the expression stack is invalid and the esp will be reset to express
 725       // this condition. Therefore, we call f only if addr is 'inside' the stack (i.e., addr >= esp for Intel).
 726       bool in_stack;
 727       if (frame::interpreter_frame_expression_stack_direction() > 0) {
 728         in_stack = (intptr_t*)addr <= _fr->interpreter_frame_tos_address();
 729       } else {
 730         in_stack = (intptr_t*)addr >= _fr->interpreter_frame_tos_address();
 731       }
 732       if (in_stack) {
 733         if (_f != NULL) {
 734           _f->do_oop(addr);
 735         }
 736       }
 737     }
 738   }
 739 
 740   int max_locals()  { return _max_locals; }
 741 };
 742 
 743 
 744 class InterpretedArgumentOopFinder: public SignatureIterator {
 745  private:
 746   OopClosure*  _f;             // Closure to invoke
 747   int          _offset;        // TOS-relative offset, decremented with each argument
 748   bool         _has_receiver;  // true if the callee has a receiver
 749   const frame* _fr;
 750 
 751   friend class SignatureIterator;  // so do_parameters_on can call do_type
 752   void do_type(BasicType type) {
 753     _offset -= parameter_type_word_count(type);
 754     if (is_reference_type(type)) oop_offset_do();
 755    }
 756 
 757   void oop_offset_do() {
 758     oop* addr;
 759     addr = (oop*)_fr->interpreter_frame_tos_at(_offset);
 760     _f->do_oop(addr);
 761   }
 762 
 763  public:
 764   InterpretedArgumentOopFinder(Symbol* signature, bool has_receiver, const frame* fr, OopClosure* f) : SignatureIterator(signature), _has_receiver(has_receiver) {
 765     // compute size of arguments
 766     int args_size = ArgumentSizeComputer(signature).size() + (has_receiver ? 1 : 0);
 767     assert(!fr->is_interpreted_frame() ||
 768            args_size <= fr->interpreter_frame_expression_stack_size(),
 769             "args cannot be on stack anymore");
 770     // initialize InterpretedArgumentOopFinder
 771     _f         = f;
 772     _fr        = fr;
 773     _offset    = args_size;
 774   }
 775 
 776   void oops_do() {
 777     if (_has_receiver) {
 778       --_offset;
 779       oop_offset_do();
 780     }
 781     do_parameters_on(this);
 782   }
 783 };
 784 
 785 
 786 // Entry frame has following form (n arguments)
 787 //         +-----------+
 788 //   sp -> |  last arg |
 789 //         +-----------+
 790 //         :    :::    :
 791 //         +-----------+
 792 // (sp+n)->|  first arg|
 793 //         +-----------+
 794 
 795 
 796 
 797 // visits and GC's all the arguments in entry frame
 798 class EntryFrameOopFinder: public SignatureIterator {
 799  private:
 800   bool         _is_static;
 801   int          _offset;
 802   const frame* _fr;
 803   OopClosure*  _f;
 804 
 805   friend class SignatureIterator;  // so do_parameters_on can call do_type
 806   void do_type(BasicType type) {
 807     // decrement offset before processing the type
 808     _offset -= parameter_type_word_count(type);
 809     assert (_offset >= 0, "illegal offset");
 810     if (is_reference_type(type))  oop_at_offset_do(_offset);
 811  }
 812 
 813   void oop_at_offset_do(int offset) {
 814     assert (offset >= 0, "illegal offset");
 815     oop* addr = (oop*) _fr->entry_frame_argument_at(offset);
 816     _f->do_oop(addr);
 817   }
 818 
 819  public:
 820   EntryFrameOopFinder(const frame* frame, Symbol* signature, bool is_static) : SignatureIterator(signature) {
 821     _f = NULL; // will be set later
 822     _fr = frame;
 823     _is_static = is_static;
 824     _offset = ArgumentSizeComputer(signature).size();  // pre-decremented down to zero
 825   }
 826 
 827   void arguments_do(OopClosure* f) {
 828     _f = f;
 829     if (!_is_static)  oop_at_offset_do(_offset); // do the receiver
 830     do_parameters_on(this);
 831   }
 832 
 833 };
 834 
 835 oop* frame::interpreter_callee_receiver_addr(Symbol* signature) {
 836   ArgumentSizeComputer asc(signature);
 837   int size = asc.size();
 838   return (oop *)interpreter_frame_tos_at(size);
 839 }
 840 
 841 
 842 void frame::oops_interpreted_do(OopClosure* f, const RegisterMap* map, bool query_oop_map_cache) const {
 843   assert(is_interpreted_frame(), "Not an interpreted frame");
 844   assert(map != NULL, "map must be set");
 845   Thread *thread = Thread::current();
 846   methodHandle m (thread, interpreter_frame_method());
 847   jint      bci = interpreter_frame_bci();
 848 
 849   assert(!Universe::heap()->is_in(m()),
 850           "must be valid oop");
 851   assert(m->is_method(), "checking frame value");
 852   assert((m->is_native() && bci == 0)  ||
 853          (!m->is_native() && bci >= 0 && bci < m->code_size()),
 854          "invalid bci value");
 855 
 856   // Handle the monitor elements in the activation
 857   for (
 858     BasicObjectLock* current = interpreter_frame_monitor_end();
 859     current < interpreter_frame_monitor_begin();
 860     current = next_monitor_in_interpreter_frame(current)
 861   ) {
 862 #ifdef ASSERT
 863     interpreter_frame_verify_monitor(current);
 864 #endif
 865     current->oops_do(f);
 866   }
 867 
 868   if (m->is_native()) {
 869     f->do_oop(interpreter_frame_temp_oop_addr());
 870   }
 871 
 872   // The method pointer in the frame might be the only path to the method's
 873   // klass, and the klass needs to be kept alive while executing. The GCs
 874   // don't trace through method pointers, so the mirror of the method's klass
 875   // is installed as a GC root.
 876   f->do_oop(interpreter_frame_mirror_addr());
 877 
 878   int max_locals = m->is_native() ? m->size_of_parameters() : m->max_locals();
 879 
 880   Symbol* signature = NULL;
 881   bool has_receiver = false;
 882 
 883   // Process a callee's arguments if we are at a call site
 884   // (i.e., if we are at an invoke bytecode)
 885   // This is used sometimes for calling into the VM, not for another
 886   // interpreted or compiled frame.
 887   if (!m->is_native()) {
 888     Bytecode_invoke call = Bytecode_invoke_check(m, bci);
 889     if (call.is_valid()) {
 890       signature = call.signature();
 891       has_receiver = call.has_receiver();
 892       if (map->include_argument_oops() &&
 893           interpreter_frame_expression_stack_size() > 0) {
 894         ResourceMark rm(thread);  // is this right ???
 895         // we are at a call site & the expression stack is not empty
 896         // => process callee's arguments
 897         //
 898         // Note: The expression stack can be empty if an exception
 899         //       occurred during method resolution/execution. In all
 900         //       cases we empty the expression stack completely be-
 901         //       fore handling the exception (the exception handling
 902         //       code in the interpreter calls a blocking runtime
 903         //       routine which can cause this code to be executed).
 904         //       (was bug gri 7/27/98)
 905         oops_interpreted_arguments_do(signature, has_receiver, f);
 906       }
 907     }
 908   }
 909 
 910   InterpreterFrameClosure blk(this, max_locals, m->max_stack(), f, NULL);
 911 
 912   // process locals & expression stack
 913   InterpreterOopMap mask;
 914   if (query_oop_map_cache) {
 915     m->mask_for(bci, &mask);
 916   } else {
 917     OopMapCache::compute_one_oop_map(m, bci, &mask);
 918   }
 919   mask.iterate_oop(&blk);
 920 }
 921 
 922 void frame::buffered_values_interpreted_do(BufferedValueClosure* f) {
 923   assert(is_interpreted_frame(), "Not an interpreted frame");
 924   Thread *thread = Thread::current();
 925   methodHandle m (thread, interpreter_frame_method());
 926   jint      bci = interpreter_frame_bci();
 927 
 928   assert(m->is_method(), "checking frame value");
 929   assert(!m->is_native() && bci >= 0 && bci < m->code_size(),
 930          "invalid bci value");
 931 
 932   InterpreterFrameClosure blk(this, m->max_locals(), m->max_stack(), NULL, f);
 933 
 934   // process locals & expression stack
 935   InterpreterOopMap mask;
 936   m->mask_for(bci, &mask);
 937   mask.iterate_oop(&blk);
 938 }
 939 
 940 void frame::oops_interpreted_arguments_do(Symbol* signature, bool has_receiver, OopClosure* f) const {
 941   InterpretedArgumentOopFinder finder(signature, has_receiver, this, f);
 942   finder.oops_do();
 943 }
 944 
 945 void frame::oops_code_blob_do(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* reg_map,
 946                               DerivedPointerIterationMode derived_mode) const {
 947   assert(_cb != NULL, "sanity check");
 948   if (_cb->oop_maps() != NULL) {
 949     OopMapSet::oops_do(this, reg_map, f, derived_mode);
 950 
 951     // Preserve potential arguments for a callee. We handle this by dispatching
 952     // on the codeblob. For c2i, we do
 953     if (reg_map->include_argument_oops()) {
 954       _cb->preserve_callee_argument_oops(*this, reg_map, f);
 955     }
 956   }
 957   // In cases where perm gen is collected, GC will want to mark
 958   // oops referenced from nmethods active on thread stacks so as to
 959   // prevent them from being collected. However, this visit should be
 960   // restricted to certain phases of the collection only. The
 961   // closure decides how it wants nmethods to be traced.
 962   if (cf != NULL)
 963     cf->do_code_blob(_cb);
 964 }
 965 
 966 class CompiledArgumentOopFinder: public SignatureIterator {
 967  protected:
 968   OopClosure*     _f;
 969   int             _offset;        // the current offset, incremented with each argument
 970   bool            _has_receiver;  // true if the callee has a receiver
 971   bool            _has_appendix;  // true if the call has an appendix
 972   frame           _fr;
 973   RegisterMap*    _reg_map;
 974   int             _arg_size;
 975   VMRegPair*      _regs;        // VMReg list of arguments
 976 
 977   friend class SignatureIterator;  // so do_parameters_on can call do_type
 978   void do_type(BasicType type) {
 979     if (is_reference_type(type))  handle_oop_offset();
 980     _offset += parameter_type_word_count(type);
 981   }
 982 
 983   virtual void handle_oop_offset() {
 984     // Extract low order register number from register array.
 985     // In LP64-land, the high-order bits are valid but unhelpful.
 986     assert(_offset < _arg_size, "out of bounds");
 987     VMReg reg = _regs[_offset].first();
 988     oop *loc = _fr.oopmapreg_to_oop_location(reg, _reg_map);
 989     assert(loc != NULL, "missing register map entry");
 990     _f->do_oop(loc);
 991   }
 992 
 993  public:
 994   CompiledArgumentOopFinder(Symbol* signature, bool has_receiver, bool has_appendix, OopClosure* f, frame fr, const RegisterMap* reg_map)
 995     : SignatureIterator(signature) {
 996 
 997     // initialize CompiledArgumentOopFinder
 998     _f         = f;
 999     _offset    = 0;
1000     _has_receiver = has_receiver;
1001     _has_appendix = has_appendix;
1002     _fr        = fr;
1003     _reg_map   = (RegisterMap*)reg_map;
1004     _regs = SharedRuntime::find_callee_arguments(signature, has_receiver, has_appendix, &_arg_size);
1005   }
1006 
1007   void oops_do() {
1008     if (_has_receiver) {
1009       handle_oop_offset();
1010       _offset++;
1011     }
1012     do_parameters_on(this);
1013     if (_has_appendix) {
1014       handle_oop_offset();
1015       _offset++;
1016     }
1017   }
1018 };
1019 
1020 void frame::oops_compiled_arguments_do(Symbol* signature, bool has_receiver, bool has_appendix,
1021                                        const RegisterMap* reg_map, OopClosure* f) const {
1022   ResourceMark rm;
1023   CompiledArgumentOopFinder finder(signature, has_receiver, has_appendix, f, *this, reg_map);
1024   finder.oops_do();
1025 }
1026 
1027 
1028 // Get receiver out of callers frame, i.e. find parameter 0 in callers
1029 // frame.  Consult ADLC for where parameter 0 is to be found.  Then
1030 // check local reg_map for it being a callee-save register or argument
1031 // register, both of which are saved in the local frame.  If not found
1032 // there, it must be an in-stack argument of the caller.
1033 // Note: caller.sp() points to callee-arguments
1034 oop frame::retrieve_receiver(RegisterMap* reg_map) {
1035   frame caller = *this;
1036 
1037   // First consult the ADLC on where it puts parameter 0 for this signature.
1038   VMReg reg = SharedRuntime::name_for_receiver();
1039   oop* oop_adr = caller.oopmapreg_to_oop_location(reg, reg_map);
1040   if (oop_adr == NULL) {
1041     guarantee(oop_adr != NULL, "bad register save location");
1042     return NULL;
1043   }
1044   oop r = *oop_adr;
1045   assert(Universe::heap()->is_in_or_null(r), "bad receiver: " INTPTR_FORMAT " (" INTX_FORMAT ")", p2i(r), p2i(r));
1046   return r;
1047 }
1048 
1049 
1050 BasicLock* frame::get_native_monitor() {
1051   nmethod* nm = (nmethod*)_cb;
1052   assert(_cb != NULL && _cb->is_nmethod() && nm->method()->is_native(),
1053          "Should not call this unless it's a native nmethod");
1054   int byte_offset = in_bytes(nm->native_basic_lock_sp_offset());
1055   assert(byte_offset >= 0, "should not see invalid offset");
1056   return (BasicLock*) &sp()[byte_offset / wordSize];
1057 }
1058 
1059 oop frame::get_native_receiver() {
1060   nmethod* nm = (nmethod*)_cb;
1061   assert(_cb != NULL && _cb->is_nmethod() && nm->method()->is_native(),
1062          "Should not call this unless it's a native nmethod");
1063   int byte_offset = in_bytes(nm->native_receiver_sp_offset());
1064   assert(byte_offset >= 0, "should not see invalid offset");
1065   oop owner = ((oop*) sp())[byte_offset / wordSize];
1066   assert( Universe::heap()->is_in(owner), "bad receiver" );
1067   return owner;
1068 }
1069 
1070 void frame::oops_entry_do(OopClosure* f, const RegisterMap* map) const {
1071   assert(map != NULL, "map must be set");
1072   if (map->include_argument_oops()) {
1073     // must collect argument oops, as nobody else is doing it
1074     Thread *thread = Thread::current();
1075     methodHandle m (thread, entry_frame_call_wrapper()->callee_method());
1076     EntryFrameOopFinder finder(this, m->signature(), m->is_static());
1077     finder.arguments_do(f);
1078   }
1079   // Traverse the Handle Block saved in the entry frame
1080   entry_frame_call_wrapper()->oops_do(f);
1081 }
1082 
1083 void frame::oops_do(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* map,
1084                     DerivedPointerIterationMode derived_mode) const {
1085   oops_do_internal(f, cf, map, true, derived_mode);
1086 }
1087 
1088 void frame::oops_do(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* map) const {
1089 #if COMPILER2_OR_JVMCI
1090   oops_do_internal(f, cf, map, true, DerivedPointerTable::is_active() ?
1091                                      DerivedPointerIterationMode::_with_table :
1092                                      DerivedPointerIterationMode::_ignore);
1093 #else
1094   oops_do_internal(f, cf, map, true, DerivedPointerIterationMode::_ignore);
1095 #endif
1096 }
1097 
1098 void frame::oops_do_internal(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* map,
1099                              bool use_interpreter_oop_map_cache, DerivedPointerIterationMode derived_mode) const {
1100 #ifndef PRODUCT
1101   // simulate GC crash here to dump java thread in error report
1102   if (CrashGCForDumpingJavaThread) {
1103     char *t = NULL;
1104     *t = 'c';
1105   }
1106 #endif
1107   if (is_interpreted_frame()) {
1108     oops_interpreted_do(f, map, use_interpreter_oop_map_cache);
1109   } else if (is_entry_frame()) {
1110     oops_entry_do(f, map);
1111   } else if (is_optimized_entry_frame()) {
1112     _cb->as_optimized_entry_blob()->oops_do(f, *this);
1113   } else if (CodeCache::contains(pc())) {
1114     oops_code_blob_do(f, cf, map, derived_mode);
1115   } else {
1116     ShouldNotReachHere();
1117   }
1118 }
1119 
1120 void frame::nmethods_do(CodeBlobClosure* cf) const {
1121   if (_cb != NULL && _cb->is_nmethod()) {
1122     cf->do_code_blob(_cb);
1123   }
1124 }
1125 
1126 
1127 // Call f closure on the interpreted Method*s in the stack.
1128 void frame::metadata_do(MetadataClosure* f) const {
1129   ResourceMark rm;
1130   if (is_interpreted_frame()) {
1131     Method* m = this->interpreter_frame_method();
1132     assert(m != NULL, "expecting a method in this frame");
1133     f->do_metadata(m);
1134   }
1135 }
1136 
1137 void frame::verify(const RegisterMap* map) const {
1138   // for now make sure receiver type is correct
1139   if (is_interpreted_frame()) {
1140     Method* method = interpreter_frame_method();
1141     guarantee(method->is_method(), "method is wrong in frame::verify");
1142     if (!method->is_static()) {
1143       // fetch the receiver
1144       oop* p = (oop*) interpreter_frame_local_at(0);
1145       // make sure we have the right receiver type
1146     }
1147   }
1148 #if COMPILER2_OR_JVMCI
1149   assert(DerivedPointerTable::is_empty(), "must be empty before verify");
1150 #endif
1151   if (map->update_map()) { // The map has to be up-to-date for the current frame
1152     oops_do_internal(&VerifyOopClosure::verify_oop, NULL, map, false, DerivedPointerIterationMode::_ignore);
1153   }
1154 }
1155 
1156 
1157 #ifdef ASSERT
1158 bool frame::verify_return_pc(address x) {
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