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