1 /*
   2  * Copyright (c) 1997, 2014, 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 "compiler/abstractCompiler.hpp"
  27 #include "compiler/disassembler.hpp"
  28 #include "gc_interface/collectedHeap.inline.hpp"
  29 #include "interpreter/interpreter.hpp"
  30 #include "interpreter/oopMapCache.hpp"
  31 #include "memory/resourceArea.hpp"
  32 #include "memory/universe.inline.hpp"
  33 #include "oops/markOop.hpp"
  34 #include "oops/methodData.hpp"
  35 #include "oops/method.hpp"
  36 #include "oops/oop.inline.hpp"
  37 #include "oops/oop.inline2.hpp"
  38 #include "prims/methodHandles.hpp"
  39 #include "runtime/frame.inline.hpp"
  40 #include "runtime/handles.inline.hpp"
  41 #include "runtime/javaCalls.hpp"
  42 #include "runtime/monitorChunk.hpp"
  43 #include "runtime/sharedRuntime.hpp"
  44 #include "runtime/signature.hpp"
  45 #include "runtime/stubCodeGenerator.hpp"
  46 #include "runtime/stubRoutines.hpp"
  47 #include "utilities/decoder.hpp"
  48 
  49 #ifdef TARGET_ARCH_x86
  50 # include "nativeInst_x86.hpp"
  51 #endif
  52 #ifdef TARGET_ARCH_sparc
  53 # include "nativeInst_sparc.hpp"
  54 #endif
  55 #ifdef TARGET_ARCH_zero
  56 # include "nativeInst_zero.hpp"
  57 #endif
  58 #ifdef TARGET_ARCH_arm
  59 # include "nativeInst_arm.hpp"
  60 #endif
  61 #ifdef TARGET_ARCH_ppc
  62 # include "nativeInst_ppc.hpp"
  63 #endif
  64 
  65 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
  66 
  67 RegisterMap::RegisterMap(JavaThread *thread, bool update_map) {
  68   _thread         = thread;
  69   _update_map     = update_map;
  70   clear();
  71   debug_only(_update_for_id = NULL;)
  72 #ifndef PRODUCT
  73   for (int i = 0; i < reg_count ; i++ ) _location[i] = NULL;
  74 #endif /* PRODUCT */
  75 }
  76 
  77 RegisterMap::RegisterMap(const RegisterMap* map) {
  78   assert(map != this, "bad initialization parameter");
  79   assert(map != NULL, "RegisterMap must be present");
  80   _thread                = map->thread();
  81   _update_map            = map->update_map();
  82   _include_argument_oops = map->include_argument_oops();
  83   debug_only(_update_for_id = map->_update_for_id;)
  84   pd_initialize_from(map);
  85   if (update_map()) {
  86     for(int i = 0; i < location_valid_size; i++) {
  87       LocationValidType bits = !update_map() ? 0 : map->_location_valid[i];
  88       _location_valid[i] = bits;
  89       // for whichever bits are set, pull in the corresponding map->_location
  90       int j = i*location_valid_type_size;
  91       while (bits != 0) {
  92         if ((bits & 1) != 0) {
  93           assert(0 <= j && j < reg_count, "range check");
  94           _location[j] = map->_location[j];
  95         }
  96         bits >>= 1;
  97         j += 1;
  98       }
  99     }
 100   }
 101 }
 102 
 103 void RegisterMap::clear() {
 104   set_include_argument_oops(true);
 105   if (_update_map) {
 106     for(int i = 0; i < location_valid_size; i++) {
 107       _location_valid[i] = 0;
 108     }
 109     pd_clear();
 110   } else {
 111     pd_initialize();
 112   }
 113 }
 114 
 115 #ifndef PRODUCT
 116 
 117 void RegisterMap::print_on(outputStream* st) const {
 118   st->print_cr("Register map");
 119   for(int i = 0; i < reg_count; i++) {
 120 
 121     VMReg r = VMRegImpl::as_VMReg(i);
 122     intptr_t* src = (intptr_t*) location(r);
 123     if (src != NULL) {
 124 
 125       r->print_on(st);
 126       st->print(" [" INTPTR_FORMAT "] = ", src);
 127       if (((uintptr_t)src & (sizeof(*src)-1)) != 0) {
 128         st->print_cr("<misaligned>");
 129       } else {
 130         st->print_cr(INTPTR_FORMAT, *src);
 131       }
 132     }
 133   }
 134 }
 135 
 136 void RegisterMap::print() const {
 137   print_on(tty);
 138 }
 139 
 140 #endif
 141 // This returns the pc that if you were in the debugger you'd see. Not
 142 // the idealized value in the frame object. This undoes the magic conversion
 143 // that happens for deoptimized frames. In addition it makes the value the
 144 // hardware would want to see in the native frame. The only user (at this point)
 145 // is deoptimization. It likely no one else should ever use it.
 146 
 147 address frame::raw_pc() const {
 148   if (is_deoptimized_frame()) {
 149     nmethod* nm = cb()->as_nmethod_or_null();
 150     if (nm->is_method_handle_return(pc()))
 151       return nm->deopt_mh_handler_begin() - pc_return_offset;
 152     else
 153       return nm->deopt_handler_begin() - pc_return_offset;
 154   } else {
 155     return (pc() - pc_return_offset);
 156   }
 157 }
 158 
 159 // Change the pc in a frame object. This does not change the actual pc in
 160 // actual frame. To do that use patch_pc.
 161 //
 162 void frame::set_pc(address   newpc ) {
 163 #ifdef ASSERT
 164   if (_cb != NULL && _cb->is_nmethod()) {
 165     assert(!((nmethod*)_cb)->is_deopt_pc(_pc), "invariant violation");
 166   }
 167 #endif // ASSERT
 168 
 169   // Unsafe to use the is_deoptimzed tester after changing pc
 170   _deopt_state = unknown;
 171   _pc = newpc;
 172   _cb = CodeCache::find_blob_unsafe(_pc);
 173 
 174 }
 175 
 176 // type testers
 177 bool frame::is_ignored_frame() const {
 178   return false;  // FIXME: some LambdaForm frames should be ignored
 179 }
 180 bool frame::is_deoptimized_frame() const {
 181   assert(_deopt_state != unknown, "not answerable");
 182   return _deopt_state == is_deoptimized;
 183 }
 184 
 185 bool frame::is_native_frame() const {
 186   return (_cb != NULL &&
 187           _cb->is_nmethod() &&
 188           ((nmethod*)_cb)->is_native_method());
 189 }
 190 
 191 bool frame::is_java_frame() const {
 192   if (is_interpreted_frame()) return true;
 193   if (is_compiled_frame())    return true;
 194   return false;
 195 }
 196 
 197 
 198 bool frame::is_compiled_frame() const {
 199   if (_cb != NULL &&
 200       _cb->is_nmethod() &&
 201       ((nmethod*)_cb)->is_java_method()) {
 202     return true;
 203   }
 204   return false;
 205 }
 206 
 207 
 208 bool frame::is_runtime_frame() const {
 209   return (_cb != NULL && _cb->is_runtime_stub());
 210 }
 211 
 212 bool frame::is_safepoint_blob_frame() const {
 213   return (_cb != NULL && _cb->is_safepoint_stub());
 214 }
 215 
 216 // testers
 217 
 218 bool frame::is_first_java_frame() const {
 219   RegisterMap map(JavaThread::current(), false); // No update
 220   frame s;
 221   for (s = sender(&map); !(s.is_java_frame() || s.is_first_frame()); s = s.sender(&map));
 222   return s.is_first_frame();
 223 }
 224 
 225 
 226 bool frame::entry_frame_is_first() const {
 227   return entry_frame_call_wrapper()->is_first_frame();
 228 }
 229 
 230 JavaCallWrapper* frame::entry_frame_call_wrapper_if_safe(JavaThread* thread) const {
 231   JavaCallWrapper** jcw = entry_frame_call_wrapper_addr();
 232   address addr = (address) jcw;
 233 
 234   // addr must be within the usable part of the stack
 235   if (thread->is_in_usable_stack(addr)) {
 236     return *jcw;
 237   }
 238 
 239   return NULL;
 240 }
 241 
 242 bool frame::is_entry_frame_valid(JavaThread* thread) const {
 243   // Validate the JavaCallWrapper an entry frame must have
 244   address jcw = (address)entry_frame_call_wrapper();
 245   bool jcw_safe = (jcw < thread->stack_base()) && (jcw > (address)fp()); // less than stack base
 246   if (!jcw_safe) {
 247     return false;
 248   }
 249 
 250   // Validate sp saved in the java frame anchor
 251   JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
 252   return (jfa->last_Java_sp() > sp());
 253 }
 254 
 255 bool frame::should_be_deoptimized() const {
 256   if (_deopt_state == is_deoptimized ||
 257       !is_compiled_frame() ) return false;
 258   assert(_cb != NULL && _cb->is_nmethod(), "must be an nmethod");
 259   nmethod* nm = (nmethod *)_cb;
 260   if (TraceDependencies) {
 261     tty->print("checking (%s) ", nm->is_marked_for_deoptimization() ? "true" : "false");
 262     nm->print_value_on(tty);
 263     tty->cr();
 264   }
 265 
 266   if( !nm->is_marked_for_deoptimization() )
 267     return false;
 268 
 269   // If at the return point, then the frame has already been popped, and
 270   // only the return needs to be executed. Don't deoptimize here.
 271   return !nm->is_at_poll_return(pc());
 272 }
 273 
 274 bool frame::can_be_deoptimized() const {
 275   if (!is_compiled_frame()) return false;
 276   nmethod* nm = (nmethod*)_cb;
 277 
 278   if( !nm->can_be_deoptimized() )
 279     return false;
 280 
 281   return !nm->is_at_poll_return(pc());
 282 }
 283 
 284 void frame::deoptimize(JavaThread* thread) {
 285   // Schedule deoptimization of an nmethod activation with this frame.
 286   assert(_cb != NULL && _cb->is_nmethod(), "must be");
 287   nmethod* nm = (nmethod*)_cb;
 288 
 289   // This is a fix for register window patching race
 290   if (NeedsDeoptSuspend && Thread::current() != thread) {
 291     assert(SafepointSynchronize::is_at_safepoint(),
 292            "patching other threads for deopt may only occur at a safepoint");
 293 
 294     // It is possible especially with DeoptimizeALot/DeoptimizeRandom that
 295     // we could see the frame again and ask for it to be deoptimized since
 296     // it might move for a long time. That is harmless and we just ignore it.
 297     if (id() == thread->must_deopt_id()) {
 298       assert(thread->is_deopt_suspend(), "lost suspension");
 299       return;
 300     }
 301 
 302     // We are at a safepoint so the target thread can only be
 303     // in 4 states:
 304     //     blocked - no problem
 305     //     blocked_trans - no problem (i.e. could have woken up from blocked
 306     //                                 during a safepoint).
 307     //     native - register window pc patching race
 308     //     native_trans - momentary state
 309     //
 310     // We could just wait out a thread in native_trans to block.
 311     // Then we'd have all the issues that the safepoint code has as to
 312     // whether to spin or block. It isn't worth it. Just treat it like
 313     // native and be done with it.
 314     //
 315     // Examine the state of the thread at the start of safepoint since
 316     // threads that were in native at the start of the safepoint could
 317     // come to a halt during the safepoint, changing the current value
 318     // of the safepoint_state.
 319     JavaThreadState state = thread->safepoint_state()->orig_thread_state();
 320     if (state == _thread_in_native || state == _thread_in_native_trans) {
 321       // Since we are at a safepoint the target thread will stop itself
 322       // before it can return to java as long as we remain at the safepoint.
 323       // Therefore we can put an additional request for the thread to stop
 324       // no matter what no (like a suspend). This will cause the thread
 325       // to notice it needs to do the deopt on its own once it leaves native.
 326       //
 327       // The only reason we must do this is because on machine with register
 328       // windows we have a race with patching the return address and the
 329       // window coming live as the thread returns to the Java code (but still
 330       // in native mode) and then blocks. It is only this top most frame
 331       // that is at risk. So in truth we could add an additional check to
 332       // see if this frame is one that is at risk.
 333       RegisterMap map(thread, false);
 334       frame at_risk =  thread->last_frame().sender(&map);
 335       if (id() == at_risk.id()) {
 336         thread->set_must_deopt_id(id());
 337         thread->set_deopt_suspend();
 338         return;
 339       }
 340     }
 341   } // NeedsDeoptSuspend
 342 
 343 
 344   // If the call site is a MethodHandle call site use the MH deopt
 345   // handler.
 346   address deopt = nm->is_method_handle_return(pc()) ?
 347     nm->deopt_mh_handler_begin() :
 348     nm->deopt_handler_begin();
 349 
 350   // Save the original pc before we patch in the new one
 351   nm->set_original_pc(this, pc());
 352   patch_pc(thread, deopt);
 353 
 354 #ifdef ASSERT
 355   {
 356     RegisterMap map(thread, false);
 357     frame check = thread->last_frame();
 358     while (id() != check.id()) {
 359       check = check.sender(&map);
 360     }
 361     assert(check.is_deoptimized_frame(), "missed deopt");
 362   }
 363 #endif // ASSERT
 364 }
 365 
 366 frame frame::java_sender() const {
 367   RegisterMap map(JavaThread::current(), false);
 368   frame s;
 369   for (s = sender(&map); !(s.is_java_frame() || s.is_first_frame()); s = s.sender(&map)) ;
 370   guarantee(s.is_java_frame(), "tried to get caller of first java frame");
 371   return s;
 372 }
 373 
 374 frame frame::real_sender(RegisterMap* map) const {
 375   frame result = sender(map);
 376   while (result.is_runtime_frame() ||
 377          result.is_ignored_frame()) {
 378     result = result.sender(map);
 379   }
 380   return result;
 381 }
 382 
 383 // Note: called by profiler - NOT for current thread
 384 frame frame::profile_find_Java_sender_frame(JavaThread *thread) {
 385 // If we don't recognize this frame, walk back up the stack until we do
 386   RegisterMap map(thread, false);
 387   frame first_java_frame = frame();
 388 
 389   // Find the first Java frame on the stack starting with input frame
 390   if (is_java_frame()) {
 391     // top frame is compiled frame or deoptimized frame
 392     first_java_frame = *this;
 393   } else if (safe_for_sender(thread)) {
 394     for (frame sender_frame = sender(&map);
 395       sender_frame.safe_for_sender(thread) && !sender_frame.is_first_frame();
 396       sender_frame = sender_frame.sender(&map)) {
 397       if (sender_frame.is_java_frame()) {
 398         first_java_frame = sender_frame;
 399         break;
 400       }
 401     }
 402   }
 403   return first_java_frame;
 404 }
 405 
 406 // Interpreter frames
 407 
 408 
 409 void frame::interpreter_frame_set_locals(intptr_t* locs)  {
 410   assert(is_interpreted_frame(), "Not an interpreted frame");
 411   *interpreter_frame_locals_addr() = locs;
 412 }
 413 
 414 Method* frame::interpreter_frame_method() const {
 415   assert(is_interpreted_frame(), "interpreted frame expected");
 416   Method* m = *interpreter_frame_method_addr();
 417   assert(m->is_method(), "not a Method*");
 418   return m;
 419 }
 420 
 421 void frame::interpreter_frame_set_method(Method* method) {
 422   assert(is_interpreted_frame(), "interpreted frame expected");
 423   *interpreter_frame_method_addr() = method;
 424 }
 425 
 426 void frame::interpreter_frame_set_bcx(intptr_t bcx) {
 427   assert(is_interpreted_frame(), "Not an interpreted frame");
 428   if (ProfileInterpreter) {
 429     bool formerly_bci = is_bci(interpreter_frame_bcx());
 430     bool is_now_bci = is_bci(bcx);
 431     *interpreter_frame_bcx_addr() = bcx;
 432 
 433     intptr_t mdx = interpreter_frame_mdx();
 434 
 435     if (mdx != 0) {
 436       if (formerly_bci) {
 437         if (!is_now_bci) {
 438           // The bcx was just converted from bci to bcp.
 439           // Convert the mdx in parallel.
 440           MethodData* mdo = interpreter_frame_method()->method_data();
 441           assert(mdo != NULL, "");
 442           int mdi = mdx - 1; // We distinguish valid mdi from zero by adding one.
 443           address mdp = mdo->di_to_dp(mdi);
 444           interpreter_frame_set_mdx((intptr_t)mdp);
 445         }
 446       } else {
 447         if (is_now_bci) {
 448           // The bcx was just converted from bcp to bci.
 449           // Convert the mdx in parallel.
 450           MethodData* mdo = interpreter_frame_method()->method_data();
 451           assert(mdo != NULL, "");
 452           int mdi = mdo->dp_to_di((address)mdx);
 453           interpreter_frame_set_mdx((intptr_t)mdi + 1); // distinguish valid from 0.
 454         }
 455       }
 456     }
 457   } else {
 458     *interpreter_frame_bcx_addr() = bcx;
 459   }
 460 }
 461 
 462 jint frame::interpreter_frame_bci() const {
 463   assert(is_interpreted_frame(), "interpreted frame expected");
 464   intptr_t bcx = interpreter_frame_bcx();
 465   return is_bci(bcx) ? bcx : interpreter_frame_method()->bci_from((address)bcx);
 466 }
 467 
 468 void frame::interpreter_frame_set_bci(jint bci) {
 469   assert(is_interpreted_frame(), "interpreted frame expected");
 470   assert(!is_bci(interpreter_frame_bcx()), "should not set bci during GC");
 471   interpreter_frame_set_bcx((intptr_t)interpreter_frame_method()->bcp_from(bci));
 472 }
 473 
 474 address frame::interpreter_frame_bcp() const {
 475   assert(is_interpreted_frame(), "interpreted frame expected");
 476   intptr_t bcx = interpreter_frame_bcx();
 477   return is_bci(bcx) ? interpreter_frame_method()->bcp_from(bcx) : (address)bcx;
 478 }
 479 
 480 void frame::interpreter_frame_set_bcp(address bcp) {
 481   assert(is_interpreted_frame(), "interpreted frame expected");
 482   assert(!is_bci(interpreter_frame_bcx()), "should not set bcp during GC");
 483   interpreter_frame_set_bcx((intptr_t)bcp);
 484 }
 485 
 486 void frame::interpreter_frame_set_mdx(intptr_t mdx) {
 487   assert(is_interpreted_frame(), "Not an interpreted frame");
 488   assert(ProfileInterpreter, "must be profiling interpreter");
 489   *interpreter_frame_mdx_addr() = mdx;
 490 }
 491 
 492 address frame::interpreter_frame_mdp() const {
 493   assert(ProfileInterpreter, "must be profiling interpreter");
 494   assert(is_interpreted_frame(), "interpreted frame expected");
 495   intptr_t bcx = interpreter_frame_bcx();
 496   intptr_t mdx = interpreter_frame_mdx();
 497 
 498   assert(!is_bci(bcx), "should not access mdp during GC");
 499   return (address)mdx;
 500 }
 501 
 502 void frame::interpreter_frame_set_mdp(address mdp) {
 503   assert(is_interpreted_frame(), "interpreted frame expected");
 504   if (mdp == NULL) {
 505     // Always allow the mdp to be cleared.
 506     interpreter_frame_set_mdx((intptr_t)mdp);
 507   }
 508   intptr_t bcx = interpreter_frame_bcx();
 509   assert(!is_bci(bcx), "should not set mdp during GC");
 510   interpreter_frame_set_mdx((intptr_t)mdp);
 511 }
 512 
 513 BasicObjectLock* frame::next_monitor_in_interpreter_frame(BasicObjectLock* current) const {
 514   assert(is_interpreted_frame(), "Not an interpreted frame");
 515 #ifdef ASSERT
 516   interpreter_frame_verify_monitor(current);
 517 #endif
 518   BasicObjectLock* next = (BasicObjectLock*) (((intptr_t*) current) + interpreter_frame_monitor_size());
 519   return next;
 520 }
 521 
 522 BasicObjectLock* frame::previous_monitor_in_interpreter_frame(BasicObjectLock* current) const {
 523   assert(is_interpreted_frame(), "Not an interpreted frame");
 524 #ifdef ASSERT
 525 //   // This verification needs to be checked before being enabled
 526 //   interpreter_frame_verify_monitor(current);
 527 #endif
 528   BasicObjectLock* previous = (BasicObjectLock*) (((intptr_t*) current) - interpreter_frame_monitor_size());
 529   return previous;
 530 }
 531 
 532 // Interpreter locals and expression stack locations.
 533 
 534 intptr_t* frame::interpreter_frame_local_at(int index) const {
 535   const int n = Interpreter::local_offset_in_bytes(index)/wordSize;
 536   return &((*interpreter_frame_locals_addr())[n]);
 537 }
 538 
 539 intptr_t* frame::interpreter_frame_expression_stack_at(jint offset) const {
 540   const int i = offset * interpreter_frame_expression_stack_direction();
 541   const int n = i * Interpreter::stackElementWords;
 542   return &(interpreter_frame_expression_stack()[n]);
 543 }
 544 
 545 jint frame::interpreter_frame_expression_stack_size() const {
 546   // Number of elements on the interpreter expression stack
 547   // Callers should span by stackElementWords
 548   int element_size = Interpreter::stackElementWords;
 549   size_t stack_size = 0;
 550   if (frame::interpreter_frame_expression_stack_direction() < 0) {
 551     stack_size = (interpreter_frame_expression_stack() -
 552                   interpreter_frame_tos_address() + 1)/element_size;
 553   } else {
 554     stack_size = (interpreter_frame_tos_address() -
 555                   interpreter_frame_expression_stack() + 1)/element_size;
 556   }
 557   assert( stack_size <= (size_t)max_jint, "stack size too big");
 558   return ((jint)stack_size);
 559 }
 560 
 561 
 562 // (frame::interpreter_frame_sender_sp accessor is in frame_<arch>.cpp)
 563 
 564 const char* frame::print_name() const {
 565   if (is_native_frame())      return "Native";
 566   if (is_interpreted_frame()) return "Interpreted";
 567   if (is_compiled_frame()) {
 568     if (is_deoptimized_frame()) return "Deoptimized";
 569     return "Compiled";
 570   }
 571   if (sp() == NULL)            return "Empty";
 572   return "C";
 573 }
 574 
 575 void frame::print_value_on(outputStream* st, JavaThread *thread) const {
 576   NOT_PRODUCT(address begin = pc()-40;)
 577   NOT_PRODUCT(address end   = NULL;)
 578 
 579   st->print("%s frame (sp=" INTPTR_FORMAT " unextended sp=" INTPTR_FORMAT, print_name(), sp(), unextended_sp());
 580   if (sp() != NULL)
 581     st->print(", fp=" INTPTR_FORMAT ", real_fp=" INTPTR_FORMAT ", pc=" INTPTR_FORMAT, fp(), real_fp(), pc());
 582 
 583   if (StubRoutines::contains(pc())) {
 584     st->print_cr(")");
 585     st->print("(");
 586     StubCodeDesc* desc = StubCodeDesc::desc_for(pc());
 587     st->print("~Stub::%s", desc->name());
 588     NOT_PRODUCT(begin = desc->begin(); end = desc->end();)
 589   } else if (Interpreter::contains(pc())) {
 590     st->print_cr(")");
 591     st->print("(");
 592     InterpreterCodelet* desc = Interpreter::codelet_containing(pc());
 593     if (desc != NULL) {
 594       st->print("~");
 595       desc->print_on(st);
 596       NOT_PRODUCT(begin = desc->code_begin(); end = desc->code_end();)
 597     } else {
 598       st->print("~interpreter");
 599     }
 600   }
 601   st->print_cr(")");
 602 
 603   if (_cb != NULL) {
 604     st->print("     ");
 605     _cb->print_value_on(st);
 606     st->cr();
 607 #ifndef PRODUCT
 608     if (end == NULL) {
 609       begin = _cb->code_begin();
 610       end   = _cb->code_end();
 611     }
 612 #endif
 613   }
 614   NOT_PRODUCT(if (WizardMode && Verbose) Disassembler::decode(begin, end);)
 615 }
 616 
 617 
 618 void frame::print_on(outputStream* st) const {
 619   print_value_on(st,NULL);
 620   if (is_interpreted_frame()) {
 621     interpreter_frame_print_on(st);
 622   }
 623 }
 624 
 625 
 626 void frame::interpreter_frame_print_on(outputStream* st) const {
 627 #ifndef PRODUCT
 628   assert(is_interpreted_frame(), "Not an interpreted frame");
 629   jint i;
 630   for (i = 0; i < interpreter_frame_method()->max_locals(); i++ ) {
 631     intptr_t x = *interpreter_frame_local_at(i);
 632     st->print(" - local  [" INTPTR_FORMAT "]", x);
 633     st->fill_to(23);
 634     st->print_cr("; #%d", i);
 635   }
 636   for (i = interpreter_frame_expression_stack_size() - 1; i >= 0; --i ) {
 637     intptr_t x = *interpreter_frame_expression_stack_at(i);
 638     st->print(" - stack  [" INTPTR_FORMAT "]", x);
 639     st->fill_to(23);
 640     st->print_cr("; #%d", i);
 641   }
 642   // locks for synchronization
 643   for (BasicObjectLock* current = interpreter_frame_monitor_end();
 644        current < interpreter_frame_monitor_begin();
 645        current = next_monitor_in_interpreter_frame(current)) {
 646     st->print(" - obj    [");
 647     current->obj()->print_value_on(st);
 648     st->print_cr("]");
 649     st->print(" - lock   [");
 650     current->lock()->print_on(st);
 651     st->print_cr("]");
 652   }
 653   // monitor
 654   st->print_cr(" - monitor[" INTPTR_FORMAT "]", interpreter_frame_monitor_begin());
 655   // bcp
 656   st->print(" - bcp    [" INTPTR_FORMAT "]", interpreter_frame_bcp());
 657   st->fill_to(23);
 658   st->print_cr("; @%d", interpreter_frame_bci());
 659   // locals
 660   st->print_cr(" - locals [" INTPTR_FORMAT "]", interpreter_frame_local_at(0));
 661   // method
 662   st->print(" - method [" INTPTR_FORMAT "]", (address)interpreter_frame_method());
 663   st->fill_to(23);
 664   st->print("; ");
 665   interpreter_frame_method()->print_name(st);
 666   st->cr();
 667 #endif
 668 }
 669 
 670 // Return whether the frame is in the VM or os indicating a Hotspot problem.
 671 // Otherwise, it's likely a bug in the native library that the Java code calls,
 672 // hopefully indicating where to submit bugs.
 673 void frame::print_C_frame(outputStream* st, char* buf, int buflen, address pc) {
 674   // C/C++ frame
 675   bool in_vm = os::address_is_in_vm(pc);
 676   st->print(in_vm ? "V" : "C");
 677 
 678   int offset;
 679   bool found;
 680 
 681   // libname
 682   found = os::dll_address_to_library_name(pc, buf, buflen, &offset);
 683   if (found) {
 684     // skip directory names
 685     const char *p1, *p2;
 686     p1 = buf;
 687     int len = (int)strlen(os::file_separator());
 688     while ((p2 = strstr(p1, os::file_separator())) != NULL) p1 = p2 + len;
 689     st->print("  [%s+0x%x]", p1, offset);
 690   } else {
 691     st->print("  " PTR_FORMAT, pc);
 692   }
 693 
 694   // function name - os::dll_address_to_function_name() may return confusing
 695   // names if pc is within jvm.dll or libjvm.so, because JVM only has
 696   // JVM_xxxx and a few other symbols in the dynamic symbol table. Do this
 697   // only for native libraries.
 698   if (!in_vm || Decoder::can_decode_C_frame_in_vm()) {
 699     found = os::dll_address_to_function_name(pc, buf, buflen, &offset);
 700 
 701     if (found) {
 702       st->print("  %s+0x%x", buf, offset);
 703     }
 704   }
 705 }
 706 
 707 // frame::print_on_error() is called by fatal error handler. Notice that we may
 708 // crash inside this function if stack frame is corrupted. The fatal error
 709 // handler can catch and handle the crash. Here we assume the frame is valid.
 710 //
 711 // First letter indicates type of the frame:
 712 //    J: Java frame (compiled)
 713 //    j: Java frame (interpreted)
 714 //    V: VM frame (C/C++)
 715 //    v: Other frames running VM generated code (e.g. stubs, adapters, etc.)
 716 //    C: C/C++ frame
 717 //
 718 // We don't need detailed frame type as that in frame::print_name(). "C"
 719 // suggests the problem is in user lib; everything else is likely a VM bug.
 720 
 721 void frame::print_on_error(outputStream* st, char* buf, int buflen, bool verbose) const {
 722   if (_cb != NULL) {
 723     if (Interpreter::contains(pc())) {
 724       Method* m = this->interpreter_frame_method();
 725       if (m != NULL) {
 726         m->name_and_sig_as_C_string(buf, buflen);
 727         st->print("j  %s", buf);
 728         st->print("+%d", this->interpreter_frame_bci());
 729       } else {
 730         st->print("j  " PTR_FORMAT, pc());
 731       }
 732     } else if (StubRoutines::contains(pc())) {
 733       StubCodeDesc* desc = StubCodeDesc::desc_for(pc());
 734       if (desc != NULL) {
 735         st->print("v  ~StubRoutines::%s", desc->name());
 736       } else {
 737         st->print("v  ~StubRoutines::" PTR_FORMAT, pc());
 738       }
 739     } else if (_cb->is_buffer_blob()) {
 740       st->print("v  ~BufferBlob::%s", ((BufferBlob *)_cb)->name());
 741     } else if (_cb->is_nmethod()) {
 742       nmethod* nm = (nmethod*)_cb;
 743       Method* m = nm->method();
 744       if (m != NULL) {
 745         m->name_and_sig_as_C_string(buf, buflen);
 746         st->print("J %d%s %s %s (%d bytes) @ " PTR_FORMAT " [" PTR_FORMAT "+0x%x]",
 747                   nm->compile_id(), (nm->is_osr_method() ? "%" : ""),
 748                   ((nm->compiler() != NULL) ? nm->compiler()->name() : ""),
 749                   buf, m->code_size(), _pc, _cb->code_begin(), _pc - _cb->code_begin());
 750       } else {
 751         st->print("J  " PTR_FORMAT, pc());
 752       }
 753     } else if (_cb->is_runtime_stub()) {
 754       st->print("v  ~RuntimeStub::%s", ((RuntimeStub *)_cb)->name());
 755     } else if (_cb->is_deoptimization_stub()) {
 756       st->print("v  ~DeoptimizationBlob");
 757     } else if (_cb->is_exception_stub()) {
 758       st->print("v  ~ExceptionBlob");
 759     } else if (_cb->is_safepoint_stub()) {
 760       st->print("v  ~SafepointBlob");
 761     } else {
 762       st->print("v  blob " PTR_FORMAT, pc());
 763     }
 764   } else {
 765     print_C_frame(st, buf, buflen, pc());
 766   }
 767 }
 768 
 769 
 770 /*
 771   The interpreter_frame_expression_stack_at method in the case of SPARC needs the
 772   max_stack value of the method in order to compute the expression stack address.
 773   It uses the Method* in order to get the max_stack value but during GC this
 774   Method* value saved on the frame is changed by reverse_and_push and hence cannot
 775   be used. So we save the max_stack value in the FrameClosure object and pass it
 776   down to the interpreter_frame_expression_stack_at method
 777 */
 778 class InterpreterFrameClosure : public OffsetClosure {
 779  private:
 780   frame* _fr;
 781   OopClosure* _f;
 782   int    _max_locals;
 783   int    _max_stack;
 784 
 785  public:
 786   InterpreterFrameClosure(frame* fr, int max_locals, int max_stack,
 787                           OopClosure* f) {
 788     _fr         = fr;
 789     _max_locals = max_locals;
 790     _max_stack  = max_stack;
 791     _f          = f;
 792   }
 793 
 794   void offset_do(int offset) {
 795     oop* addr;
 796     if (offset < _max_locals) {
 797       addr = (oop*) _fr->interpreter_frame_local_at(offset);
 798       assert((intptr_t*)addr >= _fr->sp(), "must be inside the frame");
 799       _f->do_oop(addr);
 800     } else {
 801       addr = (oop*) _fr->interpreter_frame_expression_stack_at((offset - _max_locals));
 802       // In case of exceptions, the expression stack is invalid and the esp will be reset to express
 803       // this condition. Therefore, we call f only if addr is 'inside' the stack (i.e., addr >= esp for Intel).
 804       bool in_stack;
 805       if (frame::interpreter_frame_expression_stack_direction() > 0) {
 806         in_stack = (intptr_t*)addr <= _fr->interpreter_frame_tos_address();
 807       } else {
 808         in_stack = (intptr_t*)addr >= _fr->interpreter_frame_tos_address();
 809       }
 810       if (in_stack) {
 811         _f->do_oop(addr);
 812       }
 813     }
 814   }
 815 
 816   int max_locals()  { return _max_locals; }
 817   frame* fr()       { return _fr; }
 818 };
 819 
 820 
 821 class InterpretedArgumentOopFinder: public SignatureInfo {
 822  private:
 823   OopClosure* _f;        // Closure to invoke
 824   int    _offset;        // TOS-relative offset, decremented with each argument
 825   bool   _has_receiver;  // true if the callee has a receiver
 826   frame* _fr;
 827 
 828   void set(int size, BasicType type) {
 829     _offset -= size;
 830     if (type == T_OBJECT || type == T_ARRAY) oop_offset_do();
 831   }
 832 
 833   void oop_offset_do() {
 834     oop* addr;
 835     addr = (oop*)_fr->interpreter_frame_tos_at(_offset);
 836     _f->do_oop(addr);
 837   }
 838 
 839  public:
 840   InterpretedArgumentOopFinder(Symbol* signature, bool has_receiver, frame* fr, OopClosure* f) : SignatureInfo(signature), _has_receiver(has_receiver) {
 841     // compute size of arguments
 842     int args_size = ArgumentSizeComputer(signature).size() + (has_receiver ? 1 : 0);
 843     assert(!fr->is_interpreted_frame() ||
 844            args_size <= fr->interpreter_frame_expression_stack_size(),
 845             "args cannot be on stack anymore");
 846     // initialize InterpretedArgumentOopFinder
 847     _f         = f;
 848     _fr        = fr;
 849     _offset    = args_size;
 850   }
 851 
 852   void oops_do() {
 853     if (_has_receiver) {
 854       --_offset;
 855       oop_offset_do();
 856     }
 857     iterate_parameters();
 858   }
 859 };
 860 
 861 
 862 // Entry frame has following form (n arguments)
 863 //         +-----------+
 864 //   sp -> |  last arg |
 865 //         +-----------+
 866 //         :    :::    :
 867 //         +-----------+
 868 // (sp+n)->|  first arg|
 869 //         +-----------+
 870 
 871 
 872 
 873 // visits and GC's all the arguments in entry frame
 874 class EntryFrameOopFinder: public SignatureInfo {
 875  private:
 876   bool   _is_static;
 877   int    _offset;
 878   frame* _fr;
 879   OopClosure* _f;
 880 
 881   void set(int size, BasicType type) {
 882     assert (_offset >= 0, "illegal offset");
 883     if (type == T_OBJECT || type == T_ARRAY) oop_at_offset_do(_offset);
 884     _offset -= size;
 885   }
 886 
 887   void oop_at_offset_do(int offset) {
 888     assert (offset >= 0, "illegal offset");
 889     oop* addr = (oop*) _fr->entry_frame_argument_at(offset);
 890     _f->do_oop(addr);
 891   }
 892 
 893  public:
 894    EntryFrameOopFinder(frame* frame, Symbol* signature, bool is_static) : SignatureInfo(signature) {
 895      _f = NULL; // will be set later
 896      _fr = frame;
 897      _is_static = is_static;
 898      _offset = ArgumentSizeComputer(signature).size() - 1; // last parameter is at index 0
 899    }
 900 
 901   void arguments_do(OopClosure* f) {
 902     _f = f;
 903     if (!_is_static) oop_at_offset_do(_offset+1); // do the receiver
 904     iterate_parameters();
 905   }
 906 
 907 };
 908 
 909 oop* frame::interpreter_callee_receiver_addr(Symbol* signature) {
 910   ArgumentSizeComputer asc(signature);
 911   int size = asc.size();
 912   return (oop *)interpreter_frame_tos_at(size);
 913 }
 914 
 915 
 916 void frame::oops_interpreted_do(OopClosure* f, CLDClosure* cld_f,
 917     const RegisterMap* map, bool query_oop_map_cache) {
 918   assert(is_interpreted_frame(), "Not an interpreted frame");
 919   assert(map != NULL, "map must be set");
 920   Thread *thread = Thread::current();
 921   methodHandle m (thread, interpreter_frame_method());
 922   jint      bci = interpreter_frame_bci();
 923 
 924   assert(!Universe::heap()->is_in(m()),
 925           "must be valid oop");
 926   assert(m->is_method(), "checking frame value");
 927   assert((m->is_native() && bci == 0)  ||
 928          (!m->is_native() && bci >= 0 && bci < m->code_size()),
 929          "invalid bci value");
 930 
 931   // Handle the monitor elements in the activation
 932   for (
 933     BasicObjectLock* current = interpreter_frame_monitor_end();
 934     current < interpreter_frame_monitor_begin();
 935     current = next_monitor_in_interpreter_frame(current)
 936   ) {
 937 #ifdef ASSERT
 938     interpreter_frame_verify_monitor(current);
 939 #endif
 940     current->oops_do(f);
 941   }
 942 
 943   // process fixed part
 944   if (cld_f != NULL) {
 945     // The method pointer in the frame might be the only path to the method's
 946     // klass, and the klass needs to be kept alive while executing. The GCs
 947     // don't trace through method pointers, so typically in similar situations
 948     // the mirror or the class loader of the klass are installed as a GC root.
 949     // To minimze the overhead of doing that here, we ask the GC to pass down a
 950     // closure that knows how to keep klasses alive given a ClassLoaderData.
 951     cld_f->do_cld(m->method_holder()->class_loader_data());
 952   }
 953 
 954   if (m->is_native() PPC32_ONLY(&& m->is_static())) {
 955     f->do_oop(interpreter_frame_temp_oop_addr());
 956   }
 957 
 958   int max_locals = m->is_native() ? m->size_of_parameters() : m->max_locals();
 959 
 960   Symbol* signature = NULL;
 961   bool has_receiver = false;
 962 
 963   // Process a callee's arguments if we are at a call site
 964   // (i.e., if we are at an invoke bytecode)
 965   // This is used sometimes for calling into the VM, not for another
 966   // interpreted or compiled frame.
 967   if (!m->is_native()) {
 968     Bytecode_invoke call = Bytecode_invoke_check(m, bci);
 969     if (call.is_valid()) {
 970       signature = call.signature();
 971       has_receiver = call.has_receiver();
 972       if (map->include_argument_oops() &&
 973           interpreter_frame_expression_stack_size() > 0) {
 974         ResourceMark rm(thread);  // is this right ???
 975         // we are at a call site & the expression stack is not empty
 976         // => process callee's arguments
 977         //
 978         // Note: The expression stack can be empty if an exception
 979         //       occurred during method resolution/execution. In all
 980         //       cases we empty the expression stack completely be-
 981         //       fore handling the exception (the exception handling
 982         //       code in the interpreter calls a blocking runtime
 983         //       routine which can cause this code to be executed).
 984         //       (was bug gri 7/27/98)
 985         oops_interpreted_arguments_do(signature, has_receiver, f);
 986       }
 987     }
 988   }
 989 
 990   InterpreterFrameClosure blk(this, max_locals, m->max_stack(), f);
 991 
 992   // process locals & expression stack
 993   InterpreterOopMap mask;
 994   if (query_oop_map_cache) {
 995     m->mask_for(bci, &mask);
 996   } else {
 997     OopMapCache::compute_one_oop_map(m, bci, &mask);
 998   }
 999   mask.iterate_oop(&blk);
1000 }
1001 
1002 
1003 void frame::oops_interpreted_arguments_do(Symbol* signature, bool has_receiver, OopClosure* f) {
1004   InterpretedArgumentOopFinder finder(signature, has_receiver, this, f);
1005   finder.oops_do();
1006 }
1007 
1008 void frame::oops_code_blob_do(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* reg_map) {
1009   assert(_cb != NULL, "sanity check");
1010   if (_cb->oop_maps() != NULL) {
1011     OopMapSet::oops_do(this, reg_map, f);
1012 
1013     // Preserve potential arguments for a callee. We handle this by dispatching
1014     // on the codeblob. For c2i, we do
1015     if (reg_map->include_argument_oops()) {
1016       _cb->preserve_callee_argument_oops(*this, reg_map, f);
1017     }
1018   }
1019   // In cases where perm gen is collected, GC will want to mark
1020   // oops referenced from nmethods active on thread stacks so as to
1021   // prevent them from being collected. However, this visit should be
1022   // restricted to certain phases of the collection only. The
1023   // closure decides how it wants nmethods to be traced.
1024   if (cf != NULL)
1025     cf->do_code_blob(_cb);
1026 }
1027 
1028 class CompiledArgumentOopFinder: public SignatureInfo {
1029  protected:
1030   OopClosure*     _f;
1031   int             _offset;        // the current offset, incremented with each argument
1032   bool            _has_receiver;  // true if the callee has a receiver
1033   bool            _has_appendix;  // true if the call has an appendix
1034   frame           _fr;
1035   RegisterMap*    _reg_map;
1036   int             _arg_size;
1037   VMRegPair*      _regs;        // VMReg list of arguments
1038 
1039   void set(int size, BasicType type) {
1040     if (type == T_OBJECT || type == T_ARRAY) handle_oop_offset();
1041     _offset += size;
1042   }
1043 
1044   virtual void handle_oop_offset() {
1045     // Extract low order register number from register array.
1046     // In LP64-land, the high-order bits are valid but unhelpful.
1047     VMReg reg = _regs[_offset].first();
1048     oop *loc = _fr.oopmapreg_to_location(reg, _reg_map);
1049     _f->do_oop(loc);
1050   }
1051 
1052  public:
1053   CompiledArgumentOopFinder(Symbol* signature, bool has_receiver, bool has_appendix, OopClosure* f, frame fr,  const RegisterMap* reg_map)
1054     : SignatureInfo(signature) {
1055 
1056     // initialize CompiledArgumentOopFinder
1057     _f         = f;
1058     _offset    = 0;
1059     _has_receiver = has_receiver;
1060     _has_appendix = has_appendix;
1061     _fr        = fr;
1062     _reg_map   = (RegisterMap*)reg_map;
1063     _arg_size  = ArgumentSizeComputer(signature).size() + (has_receiver ? 1 : 0) + (has_appendix ? 1 : 0);
1064 
1065     int arg_size;
1066     _regs = SharedRuntime::find_callee_arguments(signature, has_receiver, has_appendix, &arg_size);
1067     assert(arg_size == _arg_size, "wrong arg size");
1068   }
1069 
1070   void oops_do() {
1071     if (_has_receiver) {
1072       handle_oop_offset();
1073       _offset++;
1074     }
1075     iterate_parameters();
1076     if (_has_appendix) {
1077       handle_oop_offset();
1078       _offset++;
1079     }
1080   }
1081 };
1082 
1083 void frame::oops_compiled_arguments_do(Symbol* signature, bool has_receiver, bool has_appendix, const RegisterMap* reg_map, OopClosure* f) {
1084   ResourceMark rm;
1085   CompiledArgumentOopFinder finder(signature, has_receiver, has_appendix, f, *this, reg_map);
1086   finder.oops_do();
1087 }
1088 
1089 
1090 // Get receiver out of callers frame, i.e. find parameter 0 in callers
1091 // frame.  Consult ADLC for where parameter 0 is to be found.  Then
1092 // check local reg_map for it being a callee-save register or argument
1093 // register, both of which are saved in the local frame.  If not found
1094 // there, it must be an in-stack argument of the caller.
1095 // Note: caller.sp() points to callee-arguments
1096 oop frame::retrieve_receiver(RegisterMap* reg_map) {
1097   frame caller = *this;
1098 
1099   // First consult the ADLC on where it puts parameter 0 for this signature.
1100   VMReg reg = SharedRuntime::name_for_receiver();
1101   oop* oop_adr = caller.oopmapreg_to_location(reg, reg_map);
1102   if (oop_adr == NULL) {
1103     guarantee(oop_adr != NULL, "bad register save location");
1104     return NULL;
1105   }
1106   oop r = *oop_adr;
1107   assert(Universe::heap()->is_in_or_null(r), err_msg("bad receiver: " INTPTR_FORMAT " (" INTX_FORMAT ")", (void *) r, (void *) r));
1108   return r;
1109 }
1110 
1111 
1112 oop* frame::oopmapreg_to_location(VMReg reg, const RegisterMap* reg_map) const {
1113   if(reg->is_reg()) {
1114     // If it is passed in a register, it got spilled in the stub frame.
1115     return (oop *)reg_map->location(reg);
1116   } else {
1117     int sp_offset_in_bytes = reg->reg2stack() * VMRegImpl::stack_slot_size;
1118     return (oop*)(((address)unextended_sp()) + sp_offset_in_bytes);
1119   }
1120 }
1121 
1122 BasicLock* frame::get_native_monitor() {
1123   nmethod* nm = (nmethod*)_cb;
1124   assert(_cb != NULL && _cb->is_nmethod() && nm->method()->is_native(),
1125          "Should not call this unless it's a native nmethod");
1126   int byte_offset = in_bytes(nm->native_basic_lock_sp_offset());
1127   assert(byte_offset >= 0, "should not see invalid offset");
1128   return (BasicLock*) &sp()[byte_offset / wordSize];
1129 }
1130 
1131 oop frame::get_native_receiver() {
1132   nmethod* nm = (nmethod*)_cb;
1133   assert(_cb != NULL && _cb->is_nmethod() && nm->method()->is_native(),
1134          "Should not call this unless it's a native nmethod");
1135   int byte_offset = in_bytes(nm->native_receiver_sp_offset());
1136   assert(byte_offset >= 0, "should not see invalid offset");
1137   oop owner = ((oop*) sp())[byte_offset / wordSize];
1138   assert( Universe::heap()->is_in(owner), "bad receiver" );
1139   return owner;
1140 }
1141 
1142 void frame::oops_entry_do(OopClosure* f, const RegisterMap* map) {
1143   assert(map != NULL, "map must be set");
1144   if (map->include_argument_oops()) {
1145     // must collect argument oops, as nobody else is doing it
1146     Thread *thread = Thread::current();
1147     methodHandle m (thread, entry_frame_call_wrapper()->callee_method());
1148     EntryFrameOopFinder finder(this, m->signature(), m->is_static());
1149     finder.arguments_do(f);
1150   }
1151   // Traverse the Handle Block saved in the entry frame
1152   entry_frame_call_wrapper()->oops_do(f);
1153 }
1154 
1155 
1156 void frame::oops_do_internal(OopClosure* f, CLDClosure* cld_f, CodeBlobClosure* cf, RegisterMap* map, bool use_interpreter_oop_map_cache) {
1157 #ifndef PRODUCT
1158   // simulate GC crash here to dump java thread in error report
1159   if (CrashGCForDumpingJavaThread) {
1160     char *t = NULL;
1161     *t = 'c';
1162   }
1163 #endif
1164   if (is_interpreted_frame()) {
1165     oops_interpreted_do(f, cld_f, map, use_interpreter_oop_map_cache);
1166   } else if (is_entry_frame()) {
1167     oops_entry_do(f, map);
1168   } else if (CodeCache::contains(pc())) {
1169     oops_code_blob_do(f, cf, map);
1170 #ifdef SHARK
1171   } else if (is_fake_stub_frame()) {
1172     // nothing to do
1173 #endif // SHARK
1174   } else {
1175     ShouldNotReachHere();
1176   }
1177 }
1178 
1179 void frame::nmethods_do(CodeBlobClosure* cf) {
1180   if (_cb != NULL && _cb->is_nmethod()) {
1181     cf->do_code_blob(_cb);
1182   }
1183 }
1184 
1185 
1186 // call f() on the interpreted Method*s in the stack.
1187 // Have to walk the entire code cache for the compiled frames Yuck.
1188 void frame::metadata_do(void f(Metadata*)) {
1189   if (_cb != NULL && Interpreter::contains(pc())) {
1190     Method* m = this->interpreter_frame_method();
1191     assert(m != NULL, "huh?");
1192     f(m);
1193   }
1194 }
1195 
1196 void frame::gc_prologue() {
1197   if (is_interpreted_frame()) {
1198     // set bcx to bci to become Method* position independent during GC
1199     interpreter_frame_set_bcx(interpreter_frame_bci());
1200   }
1201 }
1202 
1203 
1204 void frame::gc_epilogue() {
1205   if (is_interpreted_frame()) {
1206     // set bcx back to bcp for interpreter
1207     interpreter_frame_set_bcx((intptr_t)interpreter_frame_bcp());
1208   }
1209   // call processor specific epilog function
1210   pd_gc_epilog();
1211 }
1212 
1213 
1214 # ifdef ENABLE_ZAP_DEAD_LOCALS
1215 
1216 void frame::CheckValueClosure::do_oop(oop* p) {
1217   if (CheckOopishValues && Universe::heap()->is_in_reserved(*p)) {
1218     warning("value @ " INTPTR_FORMAT " looks oopish (" INTPTR_FORMAT ") (thread = " INTPTR_FORMAT ")", p, (address)*p, Thread::current());
1219   }
1220 }
1221 frame::CheckValueClosure frame::_check_value;
1222 
1223 
1224 void frame::CheckOopClosure::do_oop(oop* p) {
1225   if (*p != NULL && !(*p)->is_oop()) {
1226     warning("value @ " INTPTR_FORMAT " should be an oop (" INTPTR_FORMAT ") (thread = " INTPTR_FORMAT ")", p, (address)*p, Thread::current());
1227  }
1228 }
1229 frame::CheckOopClosure frame::_check_oop;
1230 
1231 void frame::check_derived_oop(oop* base, oop* derived) {
1232   _check_oop.do_oop(base);
1233 }
1234 
1235 
1236 void frame::ZapDeadClosure::do_oop(oop* p) {
1237   if (TraceZapDeadLocals) tty->print_cr("zapping @ " INTPTR_FORMAT " containing " INTPTR_FORMAT, p, (address)*p);
1238   *p = cast_to_oop<intptr_t>(0xbabebabe);
1239 }
1240 frame::ZapDeadClosure frame::_zap_dead;
1241 
1242 void frame::zap_dead_locals(JavaThread* thread, const RegisterMap* map) {
1243   assert(thread == Thread::current(), "need to synchronize to do this to another thread");
1244   // Tracing - part 1
1245   if (TraceZapDeadLocals) {
1246     ResourceMark rm(thread);
1247     tty->print_cr("--------------------------------------------------------------------------------");
1248     tty->print("Zapping dead locals in ");
1249     print_on(tty);
1250     tty->cr();
1251   }
1252   // Zapping
1253        if (is_entry_frame      ()) zap_dead_entry_locals      (thread, map);
1254   else if (is_interpreted_frame()) zap_dead_interpreted_locals(thread, map);
1255   else if (is_compiled_frame()) zap_dead_compiled_locals   (thread, map);
1256 
1257   else
1258     // could be is_runtime_frame
1259     // so remove error: ShouldNotReachHere();
1260     ;
1261   // Tracing - part 2
1262   if (TraceZapDeadLocals) {
1263     tty->cr();
1264   }
1265 }
1266 
1267 
1268 void frame::zap_dead_interpreted_locals(JavaThread *thread, const RegisterMap* map) {
1269   // get current interpreter 'pc'
1270   assert(is_interpreted_frame(), "Not an interpreted frame");
1271   Method* m   = interpreter_frame_method();
1272   int       bci = interpreter_frame_bci();
1273 
1274   int max_locals = m->is_native() ? m->size_of_parameters() : m->max_locals();
1275 
1276   // process dynamic part
1277   InterpreterFrameClosure value_blk(this, max_locals, m->max_stack(),
1278                                     &_check_value);
1279   InterpreterFrameClosure   oop_blk(this, max_locals, m->max_stack(),
1280                                     &_check_oop  );
1281   InterpreterFrameClosure  dead_blk(this, max_locals, m->max_stack(),
1282                                     &_zap_dead   );
1283 
1284   // get frame map
1285   InterpreterOopMap mask;
1286   m->mask_for(bci, &mask);
1287   mask.iterate_all( &oop_blk, &value_blk, &dead_blk);
1288 }
1289 
1290 
1291 void frame::zap_dead_compiled_locals(JavaThread* thread, const RegisterMap* reg_map) {
1292 
1293   ResourceMark rm(thread);
1294   assert(_cb != NULL, "sanity check");
1295   if (_cb->oop_maps() != NULL) {
1296     OopMapSet::all_do(this, reg_map, &_check_oop, check_derived_oop, &_check_value);
1297   }
1298 }
1299 
1300 
1301 void frame::zap_dead_entry_locals(JavaThread*, const RegisterMap*) {
1302   if (TraceZapDeadLocals) warning("frame::zap_dead_entry_locals unimplemented");
1303 }
1304 
1305 
1306 void frame::zap_dead_deoptimized_locals(JavaThread*, const RegisterMap*) {
1307   if (TraceZapDeadLocals) warning("frame::zap_dead_deoptimized_locals unimplemented");
1308 }
1309 
1310 # endif // ENABLE_ZAP_DEAD_LOCALS
1311 
1312 void frame::verify(const RegisterMap* map) {
1313   // for now make sure receiver type is correct
1314   if (is_interpreted_frame()) {
1315     Method* method = interpreter_frame_method();
1316     guarantee(method->is_method(), "method is wrong in frame::verify");
1317     if (!method->is_static()) {
1318       // fetch the receiver
1319       oop* p = (oop*) interpreter_frame_local_at(0);
1320       // make sure we have the right receiver type
1321     }
1322   }
1323   COMPILER2_PRESENT(assert(DerivedPointerTable::is_empty(), "must be empty before verify");)
1324   oops_do_internal(&VerifyOopClosure::verify_oop, NULL, NULL, (RegisterMap*)map, false);
1325 }
1326 
1327 
1328 #ifdef ASSERT
1329 bool frame::verify_return_pc(address x) {
1330   if (StubRoutines::returns_to_call_stub(x)) {
1331     return true;
1332   }
1333   if (CodeCache::contains(x)) {
1334     return true;
1335   }
1336   if (Interpreter::contains(x)) {
1337     return true;
1338   }
1339   return false;
1340 }
1341 #endif
1342 
1343 #ifdef ASSERT
1344 void frame::interpreter_frame_verify_monitor(BasicObjectLock* value) const {
1345   assert(is_interpreted_frame(), "Not an interpreted frame");
1346   // verify that the value is in the right part of the frame
1347   address low_mark  = (address) interpreter_frame_monitor_end();
1348   address high_mark = (address) interpreter_frame_monitor_begin();
1349   address current   = (address) value;
1350 
1351   const int monitor_size = frame::interpreter_frame_monitor_size();
1352   guarantee((high_mark - current) % monitor_size  ==  0         , "Misaligned top of BasicObjectLock*");
1353   guarantee( high_mark > current                                , "Current BasicObjectLock* higher than high_mark");
1354 
1355   guarantee((current - low_mark) % monitor_size  ==  0         , "Misaligned bottom of BasicObjectLock*");
1356   guarantee( current >= low_mark                               , "Current BasicObjectLock* below than low_mark");
1357 }
1358 #endif
1359 
1360 #ifndef PRODUCT
1361 void frame::describe(FrameValues& values, int frame_no) {
1362   // boundaries: sp and the 'real' frame pointer
1363   values.describe(-1, sp(), err_msg("sp for #%d", frame_no), 1);
1364   intptr_t* frame_pointer = real_fp(); // Note: may differ from fp()
1365 
1366   // print frame info at the highest boundary
1367   intptr_t* info_address = MAX2(sp(), frame_pointer);
1368 
1369   if (info_address != frame_pointer) {
1370     // print frame_pointer explicitly if not marked by the frame info
1371     values.describe(-1, frame_pointer, err_msg("frame pointer for #%d", frame_no), 1);
1372   }
1373 
1374   if (is_entry_frame() || is_compiled_frame() || is_interpreted_frame() || is_native_frame()) {
1375     // Label values common to most frames
1376     values.describe(-1, unextended_sp(), err_msg("unextended_sp for #%d", frame_no));
1377   }
1378 
1379   if (is_interpreted_frame()) {
1380     Method* m = interpreter_frame_method();
1381     int bci = interpreter_frame_bci();
1382 
1383     // Label the method and current bci
1384     values.describe(-1, info_address,
1385                     FormatBuffer<1024>("#%d method %s @ %d", frame_no, m->name_and_sig_as_C_string(), bci), 2);
1386     values.describe(-1, info_address,
1387                     err_msg("- %d locals %d max stack", m->max_locals(), m->max_stack()), 1);
1388     if (m->max_locals() > 0) {
1389       intptr_t* l0 = interpreter_frame_local_at(0);
1390       intptr_t* ln = interpreter_frame_local_at(m->max_locals() - 1);
1391       values.describe(-1, MAX2(l0, ln), err_msg("locals for #%d", frame_no), 1);
1392       // Report each local and mark as owned by this frame
1393       for (int l = 0; l < m->max_locals(); l++) {
1394         intptr_t* l0 = interpreter_frame_local_at(l);
1395         values.describe(frame_no, l0, err_msg("local %d", l));
1396       }
1397     }
1398 
1399     // Compute the actual expression stack size
1400     InterpreterOopMap mask;
1401     OopMapCache::compute_one_oop_map(m, bci, &mask);
1402     intptr_t* tos = NULL;
1403     // Report each stack element and mark as owned by this frame
1404     for (int e = 0; e < mask.expression_stack_size(); e++) {
1405       tos = MAX2(tos, interpreter_frame_expression_stack_at(e));
1406       values.describe(frame_no, interpreter_frame_expression_stack_at(e),
1407                       err_msg("stack %d", e));
1408     }
1409     if (tos != NULL) {
1410       values.describe(-1, tos, err_msg("expression stack for #%d", frame_no), 1);
1411     }
1412     if (interpreter_frame_monitor_begin() != interpreter_frame_monitor_end()) {
1413       values.describe(frame_no, (intptr_t*)interpreter_frame_monitor_begin(), "monitors begin");
1414       values.describe(frame_no, (intptr_t*)interpreter_frame_monitor_end(), "monitors end");
1415     }
1416   } else if (is_entry_frame()) {
1417     // For now just label the frame
1418     values.describe(-1, info_address, err_msg("#%d entry frame", frame_no), 2);
1419   } else if (is_compiled_frame()) {
1420     // For now just label the frame
1421     nmethod* nm = cb()->as_nmethod_or_null();
1422     values.describe(-1, info_address,
1423                     FormatBuffer<1024>("#%d nmethod " INTPTR_FORMAT " for method %s%s", frame_no,
1424                                        nm, nm->method()->name_and_sig_as_C_string(),
1425                                        (_deopt_state == is_deoptimized) ?
1426                                        " (deoptimized)" :
1427                                        ((_deopt_state == unknown) ? " (state unknown)" : "")),
1428                     2);
1429   } else if (is_native_frame()) {
1430     // For now just label the frame
1431     nmethod* nm = cb()->as_nmethod_or_null();
1432     values.describe(-1, info_address,
1433                     FormatBuffer<1024>("#%d nmethod " INTPTR_FORMAT " for native method %s", frame_no,
1434                                        nm, nm->method()->name_and_sig_as_C_string()), 2);
1435   } else {
1436     // provide default info if not handled before
1437     char *info = (char *) "special frame";
1438     if ((_cb != NULL) &&
1439         (_cb->name() != NULL)) {
1440       info = (char *)_cb->name();
1441     }
1442     values.describe(-1, info_address, err_msg("#%d <%s>", frame_no, info), 2);
1443   }
1444 
1445   // platform dependent additional data
1446   describe_pd(values, frame_no);
1447 }
1448 
1449 #endif
1450 
1451 
1452 //-----------------------------------------------------------------------------------
1453 // StackFrameStream implementation
1454 
1455 StackFrameStream::StackFrameStream(JavaThread *thread, bool update) : _reg_map(thread, update) {
1456   assert(thread->has_last_Java_frame(), "sanity check");
1457   _fr = thread->last_frame();
1458   _is_done = false;
1459 }
1460 
1461 
1462 #ifndef PRODUCT
1463 
1464 void FrameValues::describe(int owner, intptr_t* location, const char* description, int priority) {
1465   FrameValue fv;
1466   fv.location = location;
1467   fv.owner = owner;
1468   fv.priority = priority;
1469   fv.description = NEW_RESOURCE_ARRAY(char, strlen(description) + 1);
1470   strcpy(fv.description, description);
1471   _values.append(fv);
1472 }
1473 
1474 
1475 #ifdef ASSERT
1476 void FrameValues::validate() {
1477   _values.sort(compare);
1478   bool error = false;
1479   FrameValue prev;
1480   prev.owner = -1;
1481   for (int i = _values.length() - 1; i >= 0; i--) {
1482     FrameValue fv = _values.at(i);
1483     if (fv.owner == -1) continue;
1484     if (prev.owner == -1) {
1485       prev = fv;
1486       continue;
1487     }
1488     if (prev.location == fv.location) {
1489       if (fv.owner != prev.owner) {
1490         tty->print_cr("overlapping storage");
1491         tty->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", prev.location, *prev.location, prev.description);
1492         tty->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", fv.location, *fv.location, fv.description);
1493         error = true;
1494       }
1495     } else {
1496       prev = fv;
1497     }
1498   }
1499   assert(!error, "invalid layout");
1500 }
1501 #endif // ASSERT
1502 
1503 void FrameValues::print(JavaThread* thread) {
1504   _values.sort(compare);
1505 
1506   // Sometimes values like the fp can be invalid values if the
1507   // register map wasn't updated during the walk.  Trim out values
1508   // that aren't actually in the stack of the thread.
1509   int min_index = 0;
1510   int max_index = _values.length() - 1;
1511   intptr_t* v0 = _values.at(min_index).location;
1512   intptr_t* v1 = _values.at(max_index).location;
1513 
1514   if (thread == Thread::current()) {
1515     while (!thread->is_in_stack((address)v0)) {
1516       v0 = _values.at(++min_index).location;
1517     }
1518     while (!thread->is_in_stack((address)v1)) {
1519       v1 = _values.at(--max_index).location;
1520     }
1521   } else {
1522     while (!thread->on_local_stack((address)v0)) {
1523       v0 = _values.at(++min_index).location;
1524     }
1525     while (!thread->on_local_stack((address)v1)) {
1526       v1 = _values.at(--max_index).location;
1527     }
1528   }
1529   intptr_t* min = MIN2(v0, v1);
1530   intptr_t* max = MAX2(v0, v1);
1531   intptr_t* cur = max;
1532   intptr_t* last = NULL;
1533   for (int i = max_index; i >= min_index; i--) {
1534     FrameValue fv = _values.at(i);
1535     while (cur > fv.location) {
1536       tty->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT, cur, *cur);
1537       cur--;
1538     }
1539     if (last == fv.location) {
1540       const char* spacer = "          " LP64_ONLY("        ");
1541       tty->print_cr(" %s  %s %s", spacer, spacer, fv.description);
1542     } else {
1543       tty->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", fv.location, *fv.location, fv.description);
1544       last = fv.location;
1545       cur--;
1546     }
1547   }
1548 }
1549 
1550 #endif // ndef PRODUCT