< prev index next >

src/hotspot/share/runtime/frame.cpp

Print this page

   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/moduleEntry.hpp"
  27 #include "code/codeCache.hpp"

  28 #include "code/vmreg.inline.hpp"
  29 #include "compiler/abstractCompiler.hpp"
  30 #include "compiler/disassembler.hpp"
  31 #include "compiler/oopMap.hpp"
  32 #include "gc/shared/collectedHeap.inline.hpp"
  33 #include "interpreter/interpreter.hpp"
  34 #include "interpreter/oopMapCache.hpp"

  35 #include "memory/resourceArea.hpp"
  36 #include "memory/universe.hpp"

  37 #include "oops/markWord.hpp"
  38 #include "oops/method.hpp"
  39 #include "oops/methodData.hpp"
  40 #include "oops/oop.inline.hpp"
  41 #include "oops/verifyOopClosure.hpp"
  42 #include "prims/methodHandles.hpp"

  43 #include "runtime/frame.inline.hpp"
  44 #include "runtime/handles.inline.hpp"
  45 #include "runtime/javaCalls.hpp"
  46 #include "runtime/monitorChunk.hpp"
  47 #include "runtime/os.hpp"
  48 #include "runtime/sharedRuntime.hpp"
  49 #include "runtime/signature.hpp"

  50 #include "runtime/stubCodeGenerator.hpp"
  51 #include "runtime/stubRoutines.hpp"
  52 #include "runtime/thread.inline.hpp"
  53 #include "utilities/debug.hpp"
  54 #include "utilities/decoder.hpp"
  55 #include "utilities/formatBuffer.hpp"
  56 
  57 RegisterMap::RegisterMap(JavaThread *thread, bool update_map, bool process_frames) {
  58   _thread         = thread;
  59   _update_map     = update_map;


  60   _process_frames = process_frames;
  61   clear();
  62   debug_only(_update_for_id = NULL;)





















  63 #ifndef PRODUCT
  64   for (int i = 0; i < reg_count ; i++ ) _location[i] = NULL;
  65 #endif /* PRODUCT */
  66 }
  67 
  68 RegisterMap::RegisterMap(const RegisterMap* map) {
  69   assert(map != this, "bad initialization parameter");
  70   assert(map != NULL, "RegisterMap must be present");
  71   _thread                = map->thread();
  72   _update_map            = map->update_map();
  73   _process_frames        = map->process_frames();
  74   _include_argument_oops = map->include_argument_oops();
  75   debug_only(_update_for_id = map->_update_for_id;)






  76   pd_initialize_from(map);
  77   if (update_map()) {
  78     for(int i = 0; i < location_valid_size; i++) {
  79       LocationValidType bits = !update_map() ? 0 : map->_location_valid[i];
  80       _location_valid[i] = bits;
  81       // for whichever bits are set, pull in the corresponding map->_location
  82       int j = i*location_valid_type_size;
  83       while (bits != 0) {
  84         if ((bits & 1) != 0) {
  85           assert(0 <= j && j < reg_count, "range check");
  86           _location[j] = map->_location[j];
  87         }
  88         bits >>= 1;
  89         j += 1;
  90       }
  91     }
  92   }
  93 }
  94 













  95 void RegisterMap::clear() {
  96   set_include_argument_oops(true);
  97   if (_update_map) {
  98     for(int i = 0; i < location_valid_size; i++) {
  99       _location_valid[i] = 0;
 100     }
 101     pd_clear();
 102   } else {
 103     pd_initialize();
 104   }
 105 }
 106 
 107 #ifndef PRODUCT
 108 








 109 void RegisterMap::print_on(outputStream* st) const {
 110   st->print_cr("Register map");
 111   for(int i = 0; i < reg_count; i++) {
 112 
 113     VMReg r = VMRegImpl::as_VMReg(i);
 114     intptr_t* src = (intptr_t*) location(r);
 115     if (src != NULL) {
 116 
 117       r->print_on(st);
 118       st->print(" [" INTPTR_FORMAT "] = ", p2i(src));
 119       if (((uintptr_t)src & (sizeof(*src)-1)) != 0) {
 120         st->print_cr("<misaligned>");
 121       } else {
 122         st->print_cr(INTPTR_FORMAT, *src);
 123       }
 124     }
 125   }
 126 }
 127 
 128 void RegisterMap::print() const {
 129   print_on(tty);
 130 }
 131 
 132 #endif
 133 // This returns the pc that if you were in the debugger you'd see. Not
 134 // the idealized value in the frame object. This undoes the magic conversion

 148   }
 149 }
 150 
 151 // Change the pc in a frame object. This does not change the actual pc in
 152 // actual frame. To do that use patch_pc.
 153 //
 154 void frame::set_pc(address   newpc ) {
 155 #ifdef ASSERT
 156   if (_cb != NULL && _cb->is_nmethod()) {
 157     assert(!((nmethod*)_cb)->is_deopt_pc(_pc), "invariant violation");
 158   }
 159 #endif // ASSERT
 160 
 161   // Unsafe to use the is_deoptimized tester after changing pc
 162   _deopt_state = unknown;
 163   _pc = newpc;
 164   _cb = CodeCache::find_blob_unsafe(_pc);
 165 
 166 }
 167 















 168 // type testers
 169 bool frame::is_ignored_frame() const {
 170   return false;  // FIXME: some LambdaForm frames should be ignored
 171 }
 172 bool frame::is_deoptimized_frame() const {
 173   assert(_deopt_state != unknown, "not answerable");
 174   return _deopt_state == is_deoptimized;
 175 }
 176 
 177 bool frame::is_native_frame() const {
 178   return (_cb != NULL &&
 179           _cb->is_nmethod() &&
 180           ((nmethod*)_cb)->is_native_method());
 181 }
 182 
 183 bool frame::is_java_frame() const {
 184   if (is_interpreted_frame()) return true;
 185   if (is_compiled_frame())    return true;
 186   return false;
 187 }
 188 
 189 
 190 bool frame::is_compiled_frame() const {
 191   if (_cb != NULL &&
 192       _cb->is_compiled() &&
 193       ((CompiledMethod*)_cb)->is_java_method()) {
 194     return true;
 195   }
 196   return false;
 197 }
 198 
 199 
 200 bool frame::is_runtime_frame() const {
 201   return (_cb != NULL && _cb->is_runtime_stub());
 202 }
 203 
 204 bool frame::is_safepoint_blob_frame() const {
 205   return (_cb != NULL && _cb->is_safepoint_stub());
 206 }
 207 
 208 // testers
 209 
 210 bool frame::is_first_java_frame() const {
 211   RegisterMap map(JavaThread::current(), false); // No update
 212   frame s;
 213   for (s = sender(&map); !(s.is_java_frame() || s.is_first_frame()); s = s.sender(&map));
 214   return s.is_first_frame();
 215 }
 216 
 217 
 218 bool frame::entry_frame_is_first() const {
 219   return entry_frame_call_wrapper()->is_first_frame();

 249   assert(_cb != NULL && _cb->is_compiled(), "must be an nmethod");
 250   CompiledMethod* nm = (CompiledMethod *)_cb;
 251   if (TraceDependencies) {
 252     tty->print("checking (%s) ", nm->is_marked_for_deoptimization() ? "true" : "false");
 253     nm->print_value_on(tty);
 254     tty->cr();
 255   }
 256 
 257   if( !nm->is_marked_for_deoptimization() )
 258     return false;
 259 
 260   // If at the return point, then the frame has already been popped, and
 261   // only the return needs to be executed. Don't deoptimize here.
 262   return !nm->is_at_poll_return(pc());
 263 }
 264 
 265 bool frame::can_be_deoptimized() const {
 266   if (!is_compiled_frame()) return false;
 267   CompiledMethod* nm = (CompiledMethod*)_cb;
 268 
 269   if( !nm->can_be_deoptimized() )
 270     return false;
 271 
 272   return !nm->is_at_poll_return(pc());
 273 }
 274 
 275 void frame::deoptimize(JavaThread* thread) {
 276   assert(thread->frame_anchor()->has_last_Java_frame() &&
 277          thread->frame_anchor()->walkable(), "must be");



 278   // Schedule deoptimization of an nmethod activation with this frame.
 279   assert(_cb != NULL && _cb->is_compiled(), "must be");
 280 



 281   // If the call site is a MethodHandle call site use the MH deopt
 282   // handler.
 283   CompiledMethod* cm = (CompiledMethod*) _cb;
 284   address deopt = cm->is_method_handle_return(pc()) ?
 285                         cm->deopt_mh_handler_begin() :
 286                         cm->deopt_handler_begin();
 287 


 288   // Save the original pc before we patch in the new one
 289   cm->set_original_pc(this, pc());
 290   patch_pc(thread, deopt);

 291 
 292 #ifdef ASSERT
 293   {
 294     RegisterMap map(thread, false);
 295     frame check = thread->last_frame();
 296     while (id() != check.id()) {
 297       check = check.sender(&map);




 298     }
 299     assert(check.is_deoptimized_frame(), "missed deopt");
 300   }
 301 #endif // ASSERT
 302 }
 303 
 304 frame frame::java_sender() const {
 305   RegisterMap map(JavaThread::current(), false);
 306   frame s;
 307   for (s = sender(&map); !(s.is_java_frame() || s.is_first_frame()); s = s.sender(&map)) ;
 308   guarantee(s.is_java_frame(), "tried to get caller of first java frame");
 309   return s;
 310 }
 311 
 312 frame frame::real_sender(RegisterMap* map) const {
 313   frame result = sender(map);
 314   while (result.is_runtime_frame() ||
 315          result.is_ignored_frame()) {
 316     result = result.sender(map);
 317   }
 318   return result;
 319 }

 355   return interpreter_frame_method()->bcp_from(bcp);
 356 }
 357 
 358 void frame::interpreter_frame_set_bcp(address bcp) {
 359   assert(is_interpreted_frame(), "interpreted frame expected");
 360   *interpreter_frame_bcp_addr() = (intptr_t)bcp;
 361 }
 362 
 363 address frame::interpreter_frame_mdp() const {
 364   assert(ProfileInterpreter, "must be profiling interpreter");
 365   assert(is_interpreted_frame(), "interpreted frame expected");
 366   return (address)*interpreter_frame_mdp_addr();
 367 }
 368 
 369 void frame::interpreter_frame_set_mdp(address mdp) {
 370   assert(is_interpreted_frame(), "interpreted frame expected");
 371   assert(ProfileInterpreter, "must be profiling interpreter");
 372   *interpreter_frame_mdp_addr() = (intptr_t)mdp;
 373 }
 374 




 375 BasicObjectLock* frame::next_monitor_in_interpreter_frame(BasicObjectLock* current) const {
 376   assert(is_interpreted_frame(), "Not an interpreted frame");
 377 #ifdef ASSERT
 378   interpreter_frame_verify_monitor(current);
 379 #endif
 380   BasicObjectLock* next = (BasicObjectLock*) (((intptr_t*) current) + interpreter_frame_monitor_size());
 381   return next;
 382 }
 383 
 384 BasicObjectLock* frame::previous_monitor_in_interpreter_frame(BasicObjectLock* current) const {
 385   assert(is_interpreted_frame(), "Not an interpreted frame");
 386 #ifdef ASSERT
 387 //   // This verification needs to be checked before being enabled
 388 //   interpreter_frame_verify_monitor(current);
 389 #endif
 390   BasicObjectLock* previous = (BasicObjectLock*) (((intptr_t*) current) - interpreter_frame_monitor_size());
 391   return previous;
 392 }
 393 
 394 // Interpreter locals and expression stack locations.
 395 




 396 intptr_t* frame::interpreter_frame_local_at(int index) const {
 397   const int n = Interpreter::local_offset_in_bytes(index)/wordSize;
 398   return &((*interpreter_frame_locals_addr())[n]);


 399 }
 400 




 401 intptr_t* frame::interpreter_frame_expression_stack_at(jint offset) const {
 402   const int i = offset * interpreter_frame_expression_stack_direction();
 403   const int n = i * Interpreter::stackElementWords;
 404   return &(interpreter_frame_expression_stack()[n]);
 405 }
 406 




 407 jint frame::interpreter_frame_expression_stack_size() const {
 408   // Number of elements on the interpreter expression stack
 409   // Callers should span by stackElementWords
 410   int element_size = Interpreter::stackElementWords;
 411   size_t stack_size = 0;
 412   if (frame::interpreter_frame_expression_stack_direction() < 0) {
 413     stack_size = (interpreter_frame_expression_stack() -
 414                   interpreter_frame_tos_address() + 1)/element_size;
 415   } else {
 416     stack_size = (interpreter_frame_tos_address() -
 417                   interpreter_frame_expression_stack() + 1)/element_size;
 418   }
 419   assert( stack_size <= (size_t)max_jint, "stack size too big");
 420   return ((jint)stack_size);
 421 }
 422 
 423 
 424 // (frame::interpreter_frame_sender_sp accessor is in frame_<arch>.cpp)
 425 
 426 const char* frame::print_name() const {
 427   if (is_native_frame())      return "Native";
 428   if (is_interpreted_frame()) return "Interpreted";
 429   if (is_compiled_frame()) {
 430     if (is_deoptimized_frame()) return "Deoptimized";
 431     return "Compiled";
 432   }
 433   if (sp() == NULL)            return "Empty";
 434   return "C";
 435 }
 436 
 437 void frame::print_value_on(outputStream* st, JavaThread *thread) const {

 461       st->print("~interpreter");
 462     }
 463   }
 464   st->print_cr(")");
 465 
 466   if (_cb != NULL) {
 467     st->print("     ");
 468     _cb->print_value_on(st);
 469     st->cr();
 470 #ifndef PRODUCT
 471     if (end == NULL) {
 472       begin = _cb->code_begin();
 473       end   = _cb->code_end();
 474     }
 475 #endif
 476   }
 477   NOT_PRODUCT(if (WizardMode && Verbose) Disassembler::decode(begin, end);)
 478 }
 479 
 480 




 481 void frame::print_on(outputStream* st) const {
 482   print_value_on(st,NULL);
 483   if (is_interpreted_frame()) {
 484     interpreter_frame_print_on(st);
 485   }
 486 }
 487 
 488 
 489 void frame::interpreter_frame_print_on(outputStream* st) const {
 490 #ifndef PRODUCT
 491   assert(is_interpreted_frame(), "Not an interpreted frame");
 492   jint i;
 493   for (i = 0; i < interpreter_frame_method()->max_locals(); i++ ) {
 494     intptr_t x = *interpreter_frame_local_at(i);
 495     st->print(" - local  [" INTPTR_FORMAT "]", x);
 496     st->fill_to(23);
 497     st->print_cr("; #%d", i);
 498   }
 499   for (i = interpreter_frame_expression_stack_size() - 1; i >= 0; --i ) {
 500     intptr_t x = *interpreter_frame_expression_stack_at(i);
 501     st->print(" - stack  [" INTPTR_FORMAT "]", x);
 502     st->fill_to(23);
 503     st->print_cr("; #%d", i);
 504   }
 505   // locks for synchronization
 506   for (BasicObjectLock* current = interpreter_frame_monitor_end();
 507        current < interpreter_frame_monitor_begin();
 508        current = next_monitor_in_interpreter_frame(current)) {
 509     st->print(" - obj    [");
 510     current->obj()->print_value_on(st);
 511     st->print_cr("]");
 512     st->print(" - lock   [");
 513     current->lock()->print_on(st, current->obj());
 514     st->print_cr("]");
 515   }
 516   // monitor
 517   st->print_cr(" - monitor[" INTPTR_FORMAT "]", p2i(interpreter_frame_monitor_begin()));
 518   // bcp
 519   st->print(" - bcp    [" INTPTR_FORMAT "]", p2i(interpreter_frame_bcp()));
 520   st->fill_to(23);
 521   st->print_cr("; @%d", interpreter_frame_bci());
 522   // locals
 523   st->print_cr(" - locals [" INTPTR_FORMAT "]", p2i(interpreter_frame_local_at(0)));
 524   // method
 525   st->print(" - method [" INTPTR_FORMAT "]", p2i(interpreter_frame_method()));
 526   st->fill_to(23);
 527   st->print("; ");
 528   interpreter_frame_method()->print_name(st);
 529   st->cr();
 530 #endif
 531 }
 532 
 533 // Print whether the frame is in the VM or OS indicating a HotSpot problem.
 534 // Otherwise, it's likely a bug in the native library that the Java code calls,
 535 // hopefully indicating where to submit bugs.
 536 void frame::print_C_frame(outputStream* st, char* buf, int buflen, address pc) {
 537   // C/C++ frame
 538   bool in_vm = os::address_is_in_vm(pc);
 539   st->print(in_vm ? "V" : "C");
 540 
 541   int offset;
 542   bool found;
 543 

 582       Method* m = this->interpreter_frame_method();
 583       if (m != NULL) {
 584         m->name_and_sig_as_C_string(buf, buflen);
 585         st->print("j  %s", buf);
 586         st->print("+%d", this->interpreter_frame_bci());
 587         ModuleEntry* module = m->method_holder()->module();
 588         if (module->is_named()) {
 589           module->name()->as_C_string(buf, buflen);
 590           st->print(" %s", buf);
 591           if (module->version() != NULL) {
 592             module->version()->as_C_string(buf, buflen);
 593             st->print("@%s", buf);
 594           }
 595         }
 596       } else {
 597         st->print("j  " PTR_FORMAT, p2i(pc()));
 598       }
 599     } else if (StubRoutines::contains(pc())) {
 600       StubCodeDesc* desc = StubCodeDesc::desc_for(pc());
 601       if (desc != NULL) {
 602         st->print("v  ~StubRoutines::%s", desc->name());
 603       } else {
 604         st->print("v  ~StubRoutines::" PTR_FORMAT, p2i(pc()));
 605       }
 606     } else if (_cb->is_buffer_blob()) {
 607       st->print("v  ~BufferBlob::%s", ((BufferBlob *)_cb)->name());
 608     } else if (_cb->is_compiled()) {
 609       CompiledMethod* cm = (CompiledMethod*)_cb;
 610       Method* m = cm->method();
 611       if (m != NULL) {
 612         if (cm->is_nmethod()) {
 613           nmethod* nm = cm->as_nmethod();
 614           st->print("J %d%s", nm->compile_id(), (nm->is_osr_method() ? "%" : ""));
 615           st->print(" %s", nm->compiler_name());
 616         }
 617         m->name_and_sig_as_C_string(buf, buflen);
 618         st->print(" %s", buf);
 619         ModuleEntry* module = m->method_holder()->module();
 620         if (module->is_named()) {
 621           module->name()->as_C_string(buf, buflen);
 622           st->print(" %s", buf);
 623           if (module->version() != NULL) {
 624             module->version()->as_C_string(buf, buflen);
 625             st->print("@%s", buf);
 626           }
 627         }
 628         st->print(" (%d bytes) @ " PTR_FORMAT " [" PTR_FORMAT "+" INTPTR_FORMAT "]",
 629                   m->code_size(), p2i(_pc), p2i(_cb->code_begin()), _pc - _cb->code_begin());
 630 #if INCLUDE_JVMCI
 631         if (cm->is_nmethod()) {
 632           nmethod* nm = cm->as_nmethod();
 633           const char* jvmciName = nm->jvmci_name();
 634           if (jvmciName != NULL) {
 635             st->print(" (%s)", jvmciName);
 636           }
 637         }
 638 #endif
 639       } else {
 640         st->print("J  " PTR_FORMAT, p2i(pc()));
 641       }
 642     } else if (_cb->is_runtime_stub()) {
 643       st->print("v  ~RuntimeStub::%s", ((RuntimeStub *)_cb)->name());
 644     } else if (_cb->is_deoptimization_stub()) {
 645       st->print("v  ~DeoptimizationBlob");
 646     } else if (_cb->is_exception_stub()) {
 647       st->print("v  ~ExceptionBlob");
 648     } else if (_cb->is_safepoint_stub()) {
 649       st->print("v  ~SafepointBlob");
 650     } else if (_cb->is_adapter_blob()) {
 651       st->print("v  ~AdapterBlob");
 652     } else if (_cb->is_vtable_blob()) {
 653       st->print("v  ~VtableBlob");
 654     } else if (_cb->is_method_handles_adapter_blob()) {
 655       st->print("v  ~MethodHandlesAdapterBlob");
 656     } else if (_cb->is_uncommon_trap_stub()) {
 657       st->print("v  ~UncommonTrapBlob");
 658     } else {
 659       st->print("v  blob " PTR_FORMAT, p2i(pc()));
 660     }
 661   } else {
 662     print_C_frame(st, buf, buflen, pc());
 663   }
 664 }
 665 
 666 
 667 /*
 668   The interpreter_frame_expression_stack_at method in the case of SPARC needs the
 669   max_stack value of the method in order to compute the expression stack address.
 670   It uses the Method* in order to get the max_stack value but during GC this
 671   Method* value saved on the frame is changed by reverse_and_push and hence cannot
 672   be used. So we save the max_stack value in the FrameClosure object and pass it
 673   down to the interpreter_frame_expression_stack_at method
 674 */

 675 class InterpreterFrameClosure : public OffsetClosure {
 676  private:
 677   const frame* _fr;
 678   OopClosure*  _f;
 679   int          _max_locals;
 680   int          _max_stack;
 681 
 682  public:
 683   InterpreterFrameClosure(const frame* fr, int max_locals, int max_stack,
 684                           OopClosure* f) {
 685     _fr         = fr;
 686     _max_locals = max_locals;
 687     _max_stack  = max_stack;
 688     _f          = f;
 689   }
 690 
 691   void offset_do(int offset) {
 692     oop* addr;
 693     if (offset < _max_locals) {
 694       addr = (oop*) _fr->interpreter_frame_local_at(offset);
 695       assert((intptr_t*)addr >= _fr->sp(), "must be inside the frame");

 696       _f->do_oop(addr);
 697     } else {
 698       addr = (oop*) _fr->interpreter_frame_expression_stack_at((offset - _max_locals));
 699       // In case of exceptions, the expression stack is invalid and the esp will be reset to express
 700       // this condition. Therefore, we call f only if addr is 'inside' the stack (i.e., addr >= esp for Intel).
 701       bool in_stack;
 702       if (frame::interpreter_frame_expression_stack_direction() > 0) {
 703         in_stack = (intptr_t*)addr <= _fr->interpreter_frame_tos_address();
 704       } else {
 705         in_stack = (intptr_t*)addr >= _fr->interpreter_frame_tos_address();
 706       }
 707       if (in_stack) {

 708         _f->do_oop(addr);
 709       }
 710     }
 711   }
 712 
 713   int max_locals()  { return _max_locals; }
 714 };
 715 
 716 
 717 class InterpretedArgumentOopFinder: public SignatureIterator {
 718  private:
 719   OopClosure*  _f;             // Closure to invoke
 720   int          _offset;        // TOS-relative offset, decremented with each argument
 721   bool         _has_receiver;  // true if the callee has a receiver
 722   const frame* _fr;
 723 
 724   friend class SignatureIterator;  // so do_parameters_on can call do_type
 725   void do_type(BasicType type) {
 726     _offset -= parameter_type_word_count(type);
 727     if (is_reference_type(type)) oop_offset_do();

 794     _f = NULL; // will be set later
 795     _fr = frame;
 796     _is_static = is_static;
 797     _offset = ArgumentSizeComputer(signature).size();  // pre-decremented down to zero
 798   }
 799 
 800   void arguments_do(OopClosure* f) {
 801     _f = f;
 802     if (!_is_static)  oop_at_offset_do(_offset); // do the receiver
 803     do_parameters_on(this);
 804   }
 805 
 806 };
 807 
 808 oop* frame::interpreter_callee_receiver_addr(Symbol* signature) {
 809   ArgumentSizeComputer asc(signature);
 810   int size = asc.size();
 811   return (oop *)interpreter_frame_tos_at(size);
 812 }
 813 






 814 

 815 void frame::oops_interpreted_do(OopClosure* f, const RegisterMap* map, bool query_oop_map_cache) const {
 816   assert(is_interpreted_frame(), "Not an interpreted frame");
 817   assert(map != NULL, "map must be set");
 818   Thread *thread = Thread::current();
 819   methodHandle m (thread, interpreter_frame_method());
 820   jint      bci = interpreter_frame_bci();























 821 



 822   assert(!Universe::heap()->is_in(m()),
 823           "must be valid oop");
 824   assert(m->is_method(), "checking frame value");
 825   assert((m->is_native() && bci == 0)  ||
 826          (!m->is_native() && bci >= 0 && bci < m->code_size()),
 827          "invalid bci value");
 828 
 829   // Handle the monitor elements in the activation
 830   for (
 831     BasicObjectLock* current = interpreter_frame_monitor_end();
 832     current < interpreter_frame_monitor_begin();
 833     current = next_monitor_in_interpreter_frame(current)
 834   ) {
 835 #ifdef ASSERT
 836     interpreter_frame_verify_monitor(current);
 837 #endif
 838     current->oops_do(f);
 839   }
 840 
 841   if (m->is_native()) {
 842     f->do_oop(interpreter_frame_temp_oop_addr());
 843   }
 844 
 845   // The method pointer in the frame might be the only path to the method's
 846   // klass, and the klass needs to be kept alive while executing. The GCs
 847   // don't trace through method pointers, so the mirror of the method's klass
 848   // is installed as a GC root.
 849   f->do_oop(interpreter_frame_mirror_addr());
 850 
 851   int max_locals = m->is_native() ? m->size_of_parameters() : m->max_locals();
 852 
 853   Symbol* signature = NULL;
 854   bool has_receiver = false;
 855 
 856   // Process a callee's arguments if we are at a call site
 857   // (i.e., if we are at an invoke bytecode)
 858   // This is used sometimes for calling into the VM, not for another
 859   // interpreted or compiled frame.
 860   if (!m->is_native()) {
 861     Bytecode_invoke call = Bytecode_invoke_check(m, bci);
 862     if (call.is_valid()) {
 863       signature = call.signature();
 864       has_receiver = call.has_receiver();
 865       if (map->include_argument_oops() &&
 866           interpreter_frame_expression_stack_size() > 0) {
 867         ResourceMark rm(thread);  // is this right ???
 868         // we are at a call site & the expression stack is not empty
 869         // => process callee's arguments
 870         //
 871         // Note: The expression stack can be empty if an exception
 872         //       occurred during method resolution/execution. In all
 873         //       cases we empty the expression stack completely be-
 874         //       fore handling the exception (the exception handling
 875         //       code in the interpreter calls a blocking runtime
 876         //       routine which can cause this code to be executed).
 877         //       (was bug gri 7/27/98)
 878         oops_interpreted_arguments_do(signature, has_receiver, f);
 879       }
 880     }
 881   }
 882 
 883   InterpreterFrameClosure blk(this, max_locals, m->max_stack(), f);
 884 
 885   // process locals & expression stack
 886   InterpreterOopMap mask;
 887   if (query_oop_map_cache) {
 888     m->mask_for(bci, &mask);
 889   } else {
 890     OopMapCache::compute_one_oop_map(m, bci, &mask);
 891   }
 892   mask.iterate_oop(&blk);
 893 }
 894 
 895 
 896 void frame::oops_interpreted_arguments_do(Symbol* signature, bool has_receiver, OopClosure* f) const {
 897   InterpretedArgumentOopFinder finder(signature, has_receiver, this, f);
 898   finder.oops_do();
 899 }
 900 
 901 void frame::oops_code_blob_do(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* reg_map,
 902                               DerivedPointerIterationMode derived_mode) const {
 903   assert(_cb != NULL, "sanity check");
 904   if (_cb->oop_maps() != NULL) {
 905     OopMapSet::oops_do(this, reg_map, f, derived_mode);





 906 
 907     // Preserve potential arguments for a callee. We handle this by dispatching
 908     // on the codeblob. For c2i, we do
 909     if (reg_map->include_argument_oops()) {
 910       _cb->preserve_callee_argument_oops(*this, reg_map, f);
 911     }
 912   }
 913   // In cases where perm gen is collected, GC will want to mark
 914   // oops referenced from nmethods active on thread stacks so as to
 915   // prevent them from being collected. However, this visit should be
 916   // restricted to certain phases of the collection only. The
 917   // closure decides how it wants nmethods to be traced.
 918   if (cf != NULL)
 919     cf->do_code_blob(_cb);
 920 }
 921 
 922 class CompiledArgumentOopFinder: public SignatureIterator {
 923  protected:
 924   OopClosure*     _f;
 925   int             _offset;        // the current offset, incremented with each argument
 926   bool            _has_receiver;  // true if the callee has a receiver
 927   bool            _has_appendix;  // true if the call has an appendix
 928   frame           _fr;
 929   RegisterMap*    _reg_map;
 930   int             _arg_size;
 931   VMRegPair*      _regs;        // VMReg list of arguments
 932 
 933   friend class SignatureIterator;  // so do_parameters_on can call do_type
 934   void do_type(BasicType type) {
 935     if (is_reference_type(type))  handle_oop_offset();
 936     _offset += parameter_type_word_count(type);
 937   }
 938 
 939   virtual void handle_oop_offset() {
 940     // Extract low order register number from register array.
 941     // In LP64-land, the high-order bits are valid but unhelpful.
 942     VMReg reg = _regs[_offset].first();
 943     oop *loc = _fr.oopmapreg_to_oop_location(reg, _reg_map);
 944     assert(loc != NULL, "missing register map entry");








 945     _f->do_oop(loc);
 946   }
 947 
 948  public:
 949   CompiledArgumentOopFinder(Symbol* signature, bool has_receiver, bool has_appendix, OopClosure* f, frame fr, const RegisterMap* reg_map)
 950     : SignatureIterator(signature) {
 951 
 952     // initialize CompiledArgumentOopFinder
 953     _f         = f;
 954     _offset    = 0;
 955     _has_receiver = has_receiver;
 956     _has_appendix = has_appendix;
 957     _fr        = fr;
 958     _reg_map   = (RegisterMap*)reg_map;
 959     _arg_size  = ArgumentSizeComputer(signature).size() + (has_receiver ? 1 : 0) + (has_appendix ? 1 : 0);
 960 
 961     int arg_size;
 962     _regs = SharedRuntime::find_callee_arguments(signature, has_receiver, has_appendix, &arg_size);
 963     assert(arg_size == _arg_size, "wrong arg size");
 964   }
 965 
 966   void oops_do() {
 967     if (_has_receiver) {
 968       handle_oop_offset();
 969       _offset++;
 970     }
 971     do_parameters_on(this);
 972     if (_has_appendix) {
 973       handle_oop_offset();
 974       _offset++;
 975     }
 976   }
 977 };
 978 
 979 void frame::oops_compiled_arguments_do(Symbol* signature, bool has_receiver, bool has_appendix,
 980                                        const RegisterMap* reg_map, OopClosure* f) const {
 981   ResourceMark rm;
 982   CompiledArgumentOopFinder finder(signature, has_receiver, has_appendix, f, *this, reg_map);
 983   finder.oops_do();
 984 }
 985 
 986 
 987 // Get receiver out of callers frame, i.e. find parameter 0 in callers
 988 // frame.  Consult ADLC for where parameter 0 is to be found.  Then
 989 // check local reg_map for it being a callee-save register or argument
 990 // register, both of which are saved in the local frame.  If not found
 991 // there, it must be an in-stack argument of the caller.
 992 // Note: caller.sp() points to callee-arguments
 993 oop frame::retrieve_receiver(RegisterMap* reg_map) {
 994   frame caller = *this;
 995 
 996   // First consult the ADLC on where it puts parameter 0 for this signature.
 997   VMReg reg = SharedRuntime::name_for_receiver();
 998   oop* oop_adr = caller.oopmapreg_to_oop_location(reg, reg_map);
 999   if (oop_adr == NULL) {
1000     guarantee(oop_adr != NULL, "bad register save location");
1001     return NULL;
1002   }
1003   oop r = *oop_adr;


1004   assert(Universe::heap()->is_in_or_null(r), "bad receiver: " INTPTR_FORMAT " (" INTX_FORMAT ")", p2i(r), p2i(r));
1005   return r;
1006 }
1007 
1008 
1009 BasicLock* frame::get_native_monitor() {
1010   nmethod* nm = (nmethod*)_cb;
1011   assert(_cb != NULL && _cb->is_nmethod() && nm->method()->is_native(),
1012          "Should not call this unless it's a native nmethod");
1013   int byte_offset = in_bytes(nm->native_basic_lock_sp_offset());
1014   assert(byte_offset >= 0, "should not see invalid offset");
1015   return (BasicLock*) &sp()[byte_offset / wordSize];
1016 }
1017 
1018 oop frame::get_native_receiver() {
1019   nmethod* nm = (nmethod*)_cb;
1020   assert(_cb != NULL && _cb->is_nmethod() && nm->method()->is_native(),
1021          "Should not call this unless it's a native nmethod");
1022   int byte_offset = in_bytes(nm->native_receiver_sp_offset());
1023   assert(byte_offset >= 0, "should not see invalid offset");
1024   oop owner = ((oop*) sp())[byte_offset / wordSize];


1025   assert( Universe::heap()->is_in(owner), "bad receiver" );
1026   return owner;
1027 }
1028 
1029 void frame::oops_entry_do(OopClosure* f, const RegisterMap* map) const {
1030   assert(map != NULL, "map must be set");
1031   if (map->include_argument_oops()) {
1032     // must collect argument oops, as nobody else is doing it
1033     Thread *thread = Thread::current();
1034     methodHandle m (thread, entry_frame_call_wrapper()->callee_method());
1035     EntryFrameOopFinder finder(this, m->signature(), m->is_static());
1036     finder.arguments_do(f);
1037   }
1038   // Traverse the Handle Block saved in the entry frame
1039   entry_frame_call_wrapper()->oops_do(f);
1040 }
1041 
1042 void frame::oops_do(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* map,
1043                     DerivedPointerIterationMode derived_mode) const {
1044   oops_do_internal(f, cf, map, true, derived_mode);
1045 }

1046 
1047 void frame::oops_do(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* map) const {
1048 #if COMPILER2_OR_JVMCI
1049   oops_do_internal(f, cf, map, true, DerivedPointerTable::is_active() ?
1050                                      DerivedPointerIterationMode::_with_table :
1051                                      DerivedPointerIterationMode::_ignore);
1052 #else
1053   oops_do_internal(f, cf, map, true, DerivedPointerIterationMode::_ignore);
1054 #endif
1055 }
1056 
1057 void frame::oops_do_internal(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* map,
1058                              bool use_interpreter_oop_map_cache, DerivedPointerIterationMode derived_mode) const {
1059 #ifndef PRODUCT
1060   // simulate GC crash here to dump java thread in error report
1061   if (CrashGCForDumpingJavaThread) {
1062     char *t = NULL;
1063     *t = 'c';
1064   }
1065 #endif
1066   if (is_interpreted_frame()) {
1067     oops_interpreted_do(f, map, use_interpreter_oop_map_cache);

1068   } else if (is_entry_frame()) {
1069     oops_entry_do(f, map);
1070   } else if (is_optimized_entry_frame()) {
1071     _cb->as_optimized_entry_blob()->oops_do(f, *this);
1072   } else if (CodeCache::contains(pc())) {
1073     oops_code_blob_do(f, cf, map, derived_mode);
1074   } else {
1075     ShouldNotReachHere();
1076   }
1077 }
1078 
1079 void frame::nmethods_do(CodeBlobClosure* cf) const {
1080   if (_cb != NULL && _cb->is_nmethod()) {
1081     cf->do_code_blob(_cb);
1082   }
1083 }
1084 
1085 
1086 // Call f closure on the interpreted Method*s in the stack.
1087 void frame::metadata_do(MetadataClosure* f) const {
1088   ResourceMark rm;
1089   if (is_interpreted_frame()) {
1090     Method* m = this->interpreter_frame_method();
1091     assert(m != NULL, "expecting a method in this frame");
1092     f->do_metadata(m);
1093   }
1094 }
1095 
1096 void frame::verify(const RegisterMap* map) const {







1097   // for now make sure receiver type is correct
1098   if (is_interpreted_frame()) {
1099     Method* method = interpreter_frame_method();
1100     guarantee(method->is_method(), "method is wrong in frame::verify");
1101     if (!method->is_static()) {
1102       // fetch the receiver
1103       oop* p = (oop*) interpreter_frame_local_at(0);
1104       // make sure we have the right receiver type
1105     }
1106   }
1107 #if COMPILER2_OR_JVMCI
1108   assert(DerivedPointerTable::is_empty(), "must be empty before verify");
1109 #endif

1110   if (map->update_map()) { // The map has to be up-to-date for the current frame
1111     oops_do_internal(&VerifyOopClosure::verify_oop, NULL, map, false, DerivedPointerIterationMode::_ignore);
1112   }
1113 }
1114 
1115 
1116 #ifdef ASSERT
1117 bool frame::verify_return_pc(address x) {
1118   if (StubRoutines::returns_to_call_stub(x)) {
1119     return true;
1120   }
1121   if (CodeCache::contains(x)) {
1122     return true;
1123   }
1124   if (Interpreter::contains(x)) {
1125     return true;
1126   }
1127   return false;
1128 }
1129 #endif
1130 
1131 #ifdef ASSERT

1132 void frame::interpreter_frame_verify_monitor(BasicObjectLock* value) const {
1133   assert(is_interpreted_frame(), "Not an interpreted frame");
1134   // verify that the value is in the right part of the frame
1135   address low_mark  = (address) interpreter_frame_monitor_end();
1136   address high_mark = (address) interpreter_frame_monitor_begin();
1137   address current   = (address) value;
1138 
1139   const int monitor_size = frame::interpreter_frame_monitor_size();
1140   guarantee((high_mark - current) % monitor_size  ==  0         , "Misaligned top of BasicObjectLock*");
1141   guarantee( high_mark > current                                , "Current BasicObjectLock* higher than high_mark");
1142 
1143   guarantee((current - low_mark) % monitor_size  ==  0         , "Misaligned bottom of BasicObjectLock*");
1144   guarantee( current >= low_mark                               , "Current BasicObjectLock* below than low_mark");
1145 }
1146 #endif
1147 
1148 #ifndef PRODUCT



















































1149 // callers need a ResourceMark because of name_and_sig_as_C_string() usage,
1150 // RA allocated string is returned to the caller
1151 void frame::describe(FrameValues& values, int frame_no) {

1152   // boundaries: sp and the 'real' frame pointer
1153   values.describe(-1, sp(), err_msg("sp for #%d", frame_no), 1);
1154   intptr_t* frame_pointer = real_fp(); // Note: may differ from fp()
1155 
1156   // print frame info at the highest boundary
1157   intptr_t* info_address = MAX2(sp(), frame_pointer);
1158 
1159   if (info_address != frame_pointer) {
1160     // print frame_pointer explicitly if not marked by the frame info
1161     values.describe(-1, frame_pointer, err_msg("frame pointer for #%d", frame_no), 1);
1162   }
1163 
1164   if (is_entry_frame() || is_compiled_frame() || is_interpreted_frame() || is_native_frame()) {
1165     // Label values common to most frames
1166     values.describe(-1, unextended_sp(), err_msg("unextended_sp for #%d", frame_no));
1167   }
1168 
1169   if (is_interpreted_frame()) {
1170     Method* m = interpreter_frame_method();
1171     int bci = interpreter_frame_bci();

1172 
1173     // Label the method and current bci
1174     values.describe(-1, info_address,
1175                     FormatBuffer<1024>("#%d method %s @ %d", frame_no, m->name_and_sig_as_C_string(), bci), 2);





1176     values.describe(-1, info_address,
1177                     err_msg("- %d locals %d max stack", m->max_locals(), m->max_stack()), 1);


1178     if (m->max_locals() > 0) {
1179       intptr_t* l0 = interpreter_frame_local_at(0);
1180       intptr_t* ln = interpreter_frame_local_at(m->max_locals() - 1);
1181       values.describe(-1, MAX2(l0, ln), err_msg("locals for #%d", frame_no), 1);
1182       // Report each local and mark as owned by this frame
1183       for (int l = 0; l < m->max_locals(); l++) {
1184         intptr_t* l0 = interpreter_frame_local_at(l);
1185         values.describe(frame_no, l0, err_msg("local %d", l));
1186       }
1187     }
1188 





1189     // Compute the actual expression stack size
1190     InterpreterOopMap mask;
1191     OopMapCache::compute_one_oop_map(methodHandle(Thread::current(), m), bci, &mask);
1192     intptr_t* tos = NULL;
1193     // Report each stack element and mark as owned by this frame
1194     for (int e = 0; e < mask.expression_stack_size(); e++) {
1195       tos = MAX2(tos, interpreter_frame_expression_stack_at(e));
1196       values.describe(frame_no, interpreter_frame_expression_stack_at(e),
1197                       err_msg("stack %d", e));
1198     }
1199     if (tos != NULL) {
1200       values.describe(-1, tos, err_msg("expression stack for #%d", frame_no), 1);
1201     }
1202     if (interpreter_frame_monitor_begin() != interpreter_frame_monitor_end()) {
1203       values.describe(frame_no, (intptr_t*)interpreter_frame_monitor_begin(), "monitors begin");
1204       values.describe(frame_no, (intptr_t*)interpreter_frame_monitor_end(), "monitors end");

1205     }
1206   } else if (is_entry_frame()) {
1207     // For now just label the frame
1208     values.describe(-1, info_address, err_msg("#%d entry frame", frame_no), 2);
1209   } else if (is_compiled_frame()) {
1210     // For now just label the frame
1211     CompiledMethod* cm = (CompiledMethod*)cb();
1212     values.describe(-1, info_address,
1213                     FormatBuffer<1024>("#%d nmethod " INTPTR_FORMAT " for method J %s%s", frame_no,
1214                                        p2i(cm),
1215                                        cm->method()->name_and_sig_as_C_string(),
1216                                        (_deopt_state == is_deoptimized) ?
1217                                        " (deoptimized)" :
1218                                        ((_deopt_state == unknown) ? " (state unknown)" : "")),
1219                     2);



























































































1220   } else if (is_native_frame()) {
1221     // For now just label the frame
1222     nmethod* nm = cb()->as_nmethod_or_null();
1223     values.describe(-1, info_address,
1224                     FormatBuffer<1024>("#%d nmethod " INTPTR_FORMAT " for native method %s", frame_no,
1225                                        p2i(nm), nm->method()->name_and_sig_as_C_string()), 2);
1226   } else {
1227     // provide default info if not handled before
1228     char *info = (char *) "special frame";
1229     if ((_cb != NULL) &&
1230         (_cb->name() != NULL)) {
1231       info = (char *)_cb->name();
1232     }
1233     values.describe(-1, info_address, err_msg("#%d <%s>", frame_no, info), 2);
1234   }
1235 
1236   // platform dependent additional data
1237   describe_pd(values, frame_no);
1238 }
1239 



1240 #endif
1241 
1242 #ifndef PRODUCT
1243 
1244 void FrameValues::describe(int owner, intptr_t* location, const char* description, int priority) {
1245   FrameValue fv;
1246   fv.location = location;
1247   fv.owner = owner;
1248   fv.priority = priority;
1249   fv.description = NEW_RESOURCE_ARRAY(char, strlen(description) + 1);
1250   strcpy(fv.description, description);
1251   _values.append(fv);
1252 }
1253 
1254 
1255 #ifdef ASSERT
1256 void FrameValues::validate() {
1257   _values.sort(compare);
1258   bool error = false;
1259   FrameValue prev;
1260   prev.owner = -1;
1261   for (int i = _values.length() - 1; i >= 0; i--) {
1262     FrameValue fv = _values.at(i);
1263     if (fv.owner == -1) continue;
1264     if (prev.owner == -1) {
1265       prev = fv;
1266       continue;
1267     }
1268     if (prev.location == fv.location) {
1269       if (fv.owner != prev.owner) {
1270         tty->print_cr("overlapping storage");
1271         tty->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", p2i(prev.location), *prev.location, prev.description);
1272         tty->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", p2i(fv.location), *fv.location, fv.description);
1273         error = true;
1274       }
1275     } else {
1276       prev = fv;
1277     }
1278   }

1279   assert(!error, "invalid layout");
1280 }
1281 #endif // ASSERT
1282 
1283 void FrameValues::print_on(JavaThread* thread, outputStream* st) {
1284   _values.sort(compare);
1285 
1286   // Sometimes values like the fp can be invalid values if the
1287   // register map wasn't updated during the walk.  Trim out values
1288   // that aren't actually in the stack of the thread.
1289   int min_index = 0;
1290   int max_index = _values.length() - 1;
1291   intptr_t* v0 = _values.at(min_index).location;
1292   intptr_t* v1 = _values.at(max_index).location;
1293 
1294   if (thread == Thread::current()) {
1295     while (!thread->is_in_live_stack((address)v0)) {
1296       v0 = _values.at(++min_index).location;
1297     }
1298     while (!thread->is_in_live_stack((address)v1)) {
1299       v1 = _values.at(--max_index).location;
1300     }
1301   } else {
1302     while (!thread->is_in_full_stack((address)v0)) {
1303       v0 = _values.at(++min_index).location;
1304     }
1305     while (!thread->is_in_full_stack((address)v1)) {
1306       v1 = _values.at(--max_index).location;
1307     }
1308   }























1309   intptr_t* min = MIN2(v0, v1);
1310   intptr_t* max = MAX2(v0, v1);
1311   intptr_t* cur = max;
1312   intptr_t* last = NULL;
1313   for (int i = max_index; i >= min_index; i--) {
1314     FrameValue fv = _values.at(i);
1315     while (cur > fv.location) {
1316       st->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT, p2i(cur), *cur);
1317       cur--;
1318     }
1319     if (last == fv.location) {
1320       const char* spacer = "          " LP64_ONLY("        ");
1321       st->print_cr(" %s  %s %s", spacer, spacer, fv.description);
1322     } else {
1323       st->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", p2i(fv.location), *fv.location, fv.description);






1324       last = fv.location;
1325       cur--;
1326     }
1327   }
1328 }
1329 
1330 #endif // ndef PRODUCT

   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/moduleEntry.hpp"
  27 #include "code/codeCache.hpp"
  28 #include "code/scopeDesc.hpp"
  29 #include "code/vmreg.inline.hpp"
  30 #include "compiler/abstractCompiler.hpp"
  31 #include "compiler/disassembler.hpp"
  32 #include "compiler/oopMap.hpp"
  33 #include "gc/shared/collectedHeap.inline.hpp"
  34 #include "interpreter/interpreter.hpp"
  35 #include "interpreter/oopMapCache.hpp"
  36 #include "logging/log.hpp"
  37 #include "memory/resourceArea.hpp"
  38 #include "memory/universe.hpp"
  39 #include "oops/instanceStackChunkKlass.inline.hpp"
  40 #include "oops/markWord.hpp"
  41 #include "oops/method.hpp"
  42 #include "oops/methodData.hpp"
  43 #include "oops/oop.inline.hpp"
  44 #include "oops/verifyOopClosure.hpp"
  45 #include "prims/methodHandles.hpp"
  46 #include "runtime/continuation.hpp"
  47 #include "runtime/frame.inline.hpp"
  48 #include "runtime/handles.inline.hpp"
  49 #include "runtime/javaCalls.hpp"
  50 #include "runtime/monitorChunk.hpp"
  51 #include "runtime/os.hpp"
  52 #include "runtime/sharedRuntime.hpp"
  53 #include "runtime/signature.hpp"
  54 #include "runtime/stackValue.hpp"
  55 #include "runtime/stubCodeGenerator.hpp"
  56 #include "runtime/stubRoutines.hpp"
  57 #include "runtime/thread.inline.hpp"
  58 #include "utilities/debug.hpp"
  59 #include "utilities/decoder.hpp"
  60 #include "utilities/formatBuffer.hpp"
  61 
  62 RegisterMap::RegisterMap(JavaThread *thread, bool update_map, bool process_frames, bool walk_cont) {
  63   _thread         = thread;
  64   _update_map     = update_map;
  65   _walk_cont      = walk_cont;
  66   DEBUG_ONLY(_skip_missing = false;)
  67   _process_frames = process_frames;
  68   clear();
  69   debug_only(_update_for_id = NULL;)
  70 
  71   if (walk_cont && thread != NULL && thread->last_continuation() != NULL) {
  72     _chunk = stackChunkHandle(Thread::current(), NULL, true);
  73   }
  74 
  75 #ifndef PRODUCT
  76   for (int i = 0; i < reg_count ; i++ ) _location[i] = NULL;
  77 #endif /* PRODUCT */
  78 }
  79 
  80 RegisterMap::RegisterMap(oop continuation, bool update_map) {
  81   _thread         = NULL;
  82   _update_map     = update_map;
  83   _walk_cont      = true;
  84   DEBUG_ONLY(_skip_missing = false;)
  85   _process_frames = false;
  86   clear();
  87   debug_only(_update_for_id = NULL;)
  88 
  89   _chunk = stackChunkHandle(Thread::current(), NULL, true);
  90 
  91 #ifndef PRODUCT
  92   for (int i = 0; i < reg_count ; i++ ) _location[i] = NULL;
  93 #endif /* PRODUCT */
  94 }
  95 
  96 RegisterMap::RegisterMap(const RegisterMap* map) {
  97   assert(map != this, "bad initialization parameter");
  98   assert(map != NULL, "RegisterMap must be present");
  99   _thread                = map->thread();
 100   _update_map            = map->update_map();
 101   _process_frames        = map->process_frames();
 102   _include_argument_oops = map->include_argument_oops();
 103   debug_only(_update_for_id = map->_update_for_id;)
 104   _walk_cont     = map->_walk_cont;
 105   DEBUG_ONLY(_skip_missing = map->_skip_missing;)
 106 
 107   // only the original RegisterMap's handle lives long enough for StackWalker; this is bound to cause trouble with nested continuations.
 108   _chunk = map->_chunk; // stackChunkHandle(Thread::current(), map->_chunk(), map->_chunk.not_null()); // 
 109 
 110   pd_initialize_from(map);
 111   if (update_map()) {
 112     for(int i = 0; i < location_valid_size; i++) {
 113       LocationValidType bits = map->_location_valid[i];
 114       _location_valid[i] = bits;
 115       // for whichever bits are set, pull in the corresponding map->_location
 116       int j = i*location_valid_type_size;
 117       while (bits != 0) {
 118         if ((bits & 1) != 0) {
 119           assert(0 <= j && j < reg_count, "range check");
 120           _location[j] = map->_location[j];
 121         }
 122         bits >>= 1;
 123         j += 1;
 124       }
 125     }
 126   }
 127 }
 128 
 129 oop RegisterMap::cont() const {
 130   return _chunk() != NULL ? _chunk()->cont() : (oop)NULL;
 131 }
 132 
 133 void RegisterMap::set_stack_chunk(stackChunkOop chunk) {
 134   assert (chunk == NULL || _walk_cont, "");
 135   assert (chunk == NULL || chunk->is_stackChunk(), "");
 136   assert (chunk == NULL || _chunk.not_null(), "");
 137   if (_chunk.is_null()) return;
 138   log_trace(jvmcont)("set_stack_chunk: " INTPTR_FORMAT " this: " INTPTR_FORMAT, p2i((oopDesc*)chunk), p2i(this));
 139   *(_chunk.raw_value()) = chunk; // reuse handle. see comment above in the constructor
 140 }
 141 
 142 void RegisterMap::clear() {
 143   set_include_argument_oops(true);
 144   if (update_map()) {
 145     for(int i = 0; i < location_valid_size; i++) {
 146       _location_valid[i] = 0;
 147     }
 148     pd_clear();
 149   } else {
 150     pd_initialize();
 151   }
 152 }
 153 
 154 #ifndef PRODUCT
 155 
 156 VMReg RegisterMap::find_register_spilled_here(void* p, intptr_t* sp) {
 157   for(int i = 0; i < RegisterMap::reg_count; i++) {
 158     VMReg r = VMRegImpl::as_VMReg(i);
 159     if (p == location(r, sp)) return r;
 160   }
 161   return NULL;
 162 }
 163 
 164 void RegisterMap::print_on(outputStream* st) const {
 165   st->print_cr("Register map");
 166   for(int i = 0; i < reg_count; i++) {
 167 
 168     VMReg r = VMRegImpl::as_VMReg(i);
 169     intptr_t* src = (intptr_t*) location(r, (intptr_t*)NULL);
 170     if (src != NULL) {
 171 
 172       r->print_on(st);
 173       st->print(" [" INTPTR_FORMAT "] = ", p2i(src));
 174       if (((uintptr_t)src & (sizeof(*src)-1)) != 0) {
 175         st->print_cr("<misaligned>");
 176       } else {
 177         st->print_cr(INTPTR_FORMAT, *src);
 178       }
 179     }
 180   }
 181 }
 182 
 183 void RegisterMap::print() const {
 184   print_on(tty);
 185 }
 186 
 187 #endif
 188 // This returns the pc that if you were in the debugger you'd see. Not
 189 // the idealized value in the frame object. This undoes the magic conversion

 203   }
 204 }
 205 
 206 // Change the pc in a frame object. This does not change the actual pc in
 207 // actual frame. To do that use patch_pc.
 208 //
 209 void frame::set_pc(address   newpc ) {
 210 #ifdef ASSERT
 211   if (_cb != NULL && _cb->is_nmethod()) {
 212     assert(!((nmethod*)_cb)->is_deopt_pc(_pc), "invariant violation");
 213   }
 214 #endif // ASSERT
 215 
 216   // Unsafe to use the is_deoptimized tester after changing pc
 217   _deopt_state = unknown;
 218   _pc = newpc;
 219   _cb = CodeCache::find_blob_unsafe(_pc);
 220 
 221 }
 222 
 223 void frame::set_pc_preserve_deopt(address newpc) {
 224   set_pc_preserve_deopt(newpc, CodeCache::find_blob_unsafe(newpc));
 225 }
 226 
 227 void frame::set_pc_preserve_deopt(address newpc, CodeBlob* cb) {
 228 #ifdef ASSERT
 229   if (_cb != NULL && _cb->is_nmethod()) {
 230     assert(!((nmethod*)_cb)->is_deopt_pc(_pc), "invariant violation");
 231   }
 232 #endif // ASSERT
 233 
 234   _pc = newpc;
 235   _cb = cb;
 236 }
 237 
 238 // type testers
 239 bool frame::is_ignored_frame() const {
 240   return false;  // FIXME: some LambdaForm frames should be ignored
 241 }




 242 
 243 bool frame::is_native_frame() const {
 244   return (_cb != NULL &&
 245           _cb->is_nmethod() &&
 246           ((nmethod*)_cb)->is_native_method());
 247 }
 248 
 249 bool frame::is_java_frame() const {
 250   if (is_interpreted_frame()) return true;
 251   if (is_compiled_frame())    return true;
 252   return false;
 253 }
 254 











 255 bool frame::is_runtime_frame() const {
 256   return (_cb != NULL && _cb->is_runtime_stub());
 257 }
 258 
 259 bool frame::is_safepoint_blob_frame() const {
 260   return (_cb != NULL && _cb->is_safepoint_stub());
 261 }
 262 
 263 // testers
 264 
 265 bool frame::is_first_java_frame() const {
 266   RegisterMap map(JavaThread::current(), false); // No update
 267   frame s;
 268   for (s = sender(&map); !(s.is_java_frame() || s.is_first_frame()); s = s.sender(&map));
 269   return s.is_first_frame();
 270 }
 271 
 272 
 273 bool frame::entry_frame_is_first() const {
 274   return entry_frame_call_wrapper()->is_first_frame();

 304   assert(_cb != NULL && _cb->is_compiled(), "must be an nmethod");
 305   CompiledMethod* nm = (CompiledMethod *)_cb;
 306   if (TraceDependencies) {
 307     tty->print("checking (%s) ", nm->is_marked_for_deoptimization() ? "true" : "false");
 308     nm->print_value_on(tty);
 309     tty->cr();
 310   }
 311 
 312   if( !nm->is_marked_for_deoptimization() )
 313     return false;
 314 
 315   // If at the return point, then the frame has already been popped, and
 316   // only the return needs to be executed. Don't deoptimize here.
 317   return !nm->is_at_poll_return(pc());
 318 }
 319 
 320 bool frame::can_be_deoptimized() const {
 321   if (!is_compiled_frame()) return false;
 322   CompiledMethod* nm = (CompiledMethod*)_cb;
 323 
 324   if(!nm->can_be_deoptimized())
 325     return false;
 326 
 327   return !nm->is_at_poll_return(pc());
 328 }
 329 
 330 void frame::deoptimize(JavaThread* thread) {
 331   // tty->print_cr(">>> frame::deoptimize");
 332   // print_on(tty);
 333   assert(thread == NULL
 334          || (thread->frame_anchor()->has_last_Java_frame() &&
 335              thread->frame_anchor()->walkable()), "must be");
 336   // Schedule deoptimization of an nmethod activation with this frame.
 337   assert(_cb != NULL && _cb->is_compiled(), "must be");
 338 
 339   // log_develop_trace(jvmcont)(">>>> frame::deoptimize %ld", os::current_thread_id());
 340   // tty->print_cr(">>>> frame::deoptimize: %ld", os::current_thread_id()); print_on(tty);
 341 
 342   // If the call site is a MethodHandle call site use the MH deopt
 343   // handler.
 344   CompiledMethod* cm = (CompiledMethod*) _cb;
 345   address deopt = cm->is_method_handle_return(pc()) ?
 346                         cm->deopt_mh_handler_begin() :
 347                         cm->deopt_handler_begin();
 348 
 349   NativePostCallNop* inst = nativePostCallNop_at(pc());
 350 
 351   // Save the original pc before we patch in the new one
 352   cm->set_original_pc(this, pc());
 353   patch_pc(thread, deopt);
 354   assert(is_deoptimized_frame(), "must be");
 355 
 356 #ifdef ASSERT
 357   if (thread != NULL) {

 358     frame check = thread->last_frame();
 359     if (is_older(check.id())) {
 360       RegisterMap map(thread, false);
 361       while (id() != check.id()) {
 362         check = check.sender(&map);
 363       }
 364       assert(check.is_deoptimized_frame(), "missed deopt");
 365     }

 366   }
 367 #endif // ASSERT
 368 }
 369 
 370 frame frame::java_sender() const {
 371   RegisterMap map(JavaThread::current(), false);
 372   frame s;
 373   for (s = sender(&map); !(s.is_java_frame() || s.is_first_frame()); s = s.sender(&map)) ;
 374   guarantee(s.is_java_frame(), "tried to get caller of first java frame");
 375   return s;
 376 }
 377 
 378 frame frame::real_sender(RegisterMap* map) const {
 379   frame result = sender(map);
 380   while (result.is_runtime_frame() ||
 381          result.is_ignored_frame()) {
 382     result = result.sender(map);
 383   }
 384   return result;
 385 }

 421   return interpreter_frame_method()->bcp_from(bcp);
 422 }
 423 
 424 void frame::interpreter_frame_set_bcp(address bcp) {
 425   assert(is_interpreted_frame(), "interpreted frame expected");
 426   *interpreter_frame_bcp_addr() = (intptr_t)bcp;
 427 }
 428 
 429 address frame::interpreter_frame_mdp() const {
 430   assert(ProfileInterpreter, "must be profiling interpreter");
 431   assert(is_interpreted_frame(), "interpreted frame expected");
 432   return (address)*interpreter_frame_mdp_addr();
 433 }
 434 
 435 void frame::interpreter_frame_set_mdp(address mdp) {
 436   assert(is_interpreted_frame(), "interpreted frame expected");
 437   assert(ProfileInterpreter, "must be profiling interpreter");
 438   *interpreter_frame_mdp_addr() = (intptr_t)mdp;
 439 }
 440 
 441 template BasicObjectLock* frame::next_monitor_in_interpreter_frame<true>(BasicObjectLock* current) const;
 442 template BasicObjectLock* frame::next_monitor_in_interpreter_frame<false>(BasicObjectLock* current) const;
 443 
 444 template <bool relative>
 445 BasicObjectLock* frame::next_monitor_in_interpreter_frame(BasicObjectLock* current) const {
 446   assert(is_interpreted_frame(), "Not an interpreted frame");
 447 #ifdef ASSERT
 448   interpreter_frame_verify_monitor<relative>(current);
 449 #endif
 450   BasicObjectLock* next = (BasicObjectLock*) (((intptr_t*) current) + interpreter_frame_monitor_size());
 451   return next;
 452 }
 453 
 454 BasicObjectLock* frame::previous_monitor_in_interpreter_frame(BasicObjectLock* current) const {
 455   assert(is_interpreted_frame(), "Not an interpreted frame");
 456 #ifdef ASSERT
 457 //   // This verification needs to be checked before being enabled
 458 //   interpreter_frame_verify_monitor(current);
 459 #endif
 460   BasicObjectLock* previous = (BasicObjectLock*) (((intptr_t*) current) - interpreter_frame_monitor_size());
 461   return previous;
 462 }
 463 
 464 // Interpreter locals and expression stack locations.
 465 
 466 template intptr_t* frame::interpreter_frame_local_at<true>(int index) const;
 467 template intptr_t* frame::interpreter_frame_local_at<false>(int index) const;
 468 
 469 template <bool relative>
 470 intptr_t* frame::interpreter_frame_local_at(int index) const {
 471   const int n = Interpreter::local_offset_in_bytes(index)/wordSize;
 472   intptr_t* first = relative ? fp() + (intptr_t)*interpreter_frame_locals_addr()
 473                              : *interpreter_frame_locals_addr();
 474   return &(first[n]);
 475 }
 476 
 477 template intptr_t* frame::interpreter_frame_expression_stack_at<true>(jint index) const;
 478 template intptr_t* frame::interpreter_frame_expression_stack_at<false>(jint index) const;
 479 
 480 template <bool relative>
 481 intptr_t* frame::interpreter_frame_expression_stack_at(jint offset) const {
 482   const int i = offset * interpreter_frame_expression_stack_direction();
 483   const int n = i * Interpreter::stackElementWords;
 484   return &(interpreter_frame_expression_stack<relative>()[n]);
 485 }
 486 
 487 template jint frame::interpreter_frame_expression_stack_size<true>() const;
 488 template jint frame::interpreter_frame_expression_stack_size<false>() const;
 489 
 490 template <bool relative>
 491 jint frame::interpreter_frame_expression_stack_size() const {
 492   // Number of elements on the interpreter expression stack
 493   // Callers should span by stackElementWords
 494   int element_size = Interpreter::stackElementWords;
 495   size_t stack_size = 0;
 496   if (frame::interpreter_frame_expression_stack_direction() < 0) {
 497     stack_size = (interpreter_frame_expression_stack<relative>() -
 498                   interpreter_frame_tos_address<relative>() + 1)/element_size;
 499   } else {
 500     stack_size = (interpreter_frame_tos_address<relative>() -
 501                   interpreter_frame_expression_stack<relative>() + 1)/element_size;
 502   }
 503   assert( stack_size <= (size_t)max_jint, "stack size too big");
 504   return ((jint)stack_size);
 505 }
 506 
 507 
 508 // (frame::interpreter_frame_sender_sp accessor is in frame_<arch>.cpp)
 509 
 510 const char* frame::print_name() const {
 511   if (is_native_frame())      return "Native";
 512   if (is_interpreted_frame()) return "Interpreted";
 513   if (is_compiled_frame()) {
 514     if (is_deoptimized_frame()) return "Deoptimized";
 515     return "Compiled";
 516   }
 517   if (sp() == NULL)            return "Empty";
 518   return "C";
 519 }
 520 
 521 void frame::print_value_on(outputStream* st, JavaThread *thread) const {

 545       st->print("~interpreter");
 546     }
 547   }
 548   st->print_cr(")");
 549 
 550   if (_cb != NULL) {
 551     st->print("     ");
 552     _cb->print_value_on(st);
 553     st->cr();
 554 #ifndef PRODUCT
 555     if (end == NULL) {
 556       begin = _cb->code_begin();
 557       end   = _cb->code_end();
 558     }
 559 #endif
 560   }
 561   NOT_PRODUCT(if (WizardMode && Verbose) Disassembler::decode(begin, end);)
 562 }
 563 
 564 
 565 template void frame::print_on<false>(outputStream* st) const;
 566 template void frame::print_on<true >(outputStream* st) const;
 567 
 568 template <bool relative>
 569 void frame::print_on(outputStream* st) const {
 570   print_value_on(st,NULL);
 571   if (is_interpreted_frame()) {
 572     interpreter_frame_print_on<relative>(st);
 573   }
 574 }
 575 
 576 template <bool relative>
 577 void frame::interpreter_frame_print_on(outputStream* st) const {
 578 #ifndef PRODUCT
 579   assert(is_interpreted_frame(), "Not an interpreted frame");
 580   jint i;
 581   for (i = 0; i < interpreter_frame_method()->max_locals(); i++ ) {
 582     intptr_t x = *interpreter_frame_local_at<relative>(i);
 583     st->print(" - local  [" INTPTR_FORMAT "]", x);
 584     st->fill_to(23);
 585     st->print_cr("; #%d", i);
 586   }
 587   for (i = interpreter_frame_expression_stack_size<relative>() - 1; i >= 0; --i ) {
 588     intptr_t x = *interpreter_frame_expression_stack_at<relative>(i);
 589     st->print(" - stack  [" INTPTR_FORMAT "]", x);
 590     st->fill_to(23);
 591     st->print_cr("; #%d", i);
 592   }
 593   // locks for synchronization
 594   for (BasicObjectLock* current = interpreter_frame_monitor_end<relative>();
 595        current < interpreter_frame_monitor_begin();
 596        current = next_monitor_in_interpreter_frame<relative>(current)) {
 597     st->print(" - obj    [");
 598     current->obj()->print_value_on(st);
 599     st->print_cr("]");
 600     st->print(" - lock   [");
 601     current->lock()->print_on(st, current->obj());
 602     st->print_cr("]");
 603   }
 604   // monitor
 605   st->print_cr(" - monitor[" INTPTR_FORMAT "]", p2i(interpreter_frame_monitor_begin()));
 606   // bcp
 607   st->print(" - bcp    [" INTPTR_FORMAT "]", p2i(interpreter_frame_bcp()));
 608   st->fill_to(23);
 609   st->print_cr("; @%d", interpreter_frame_bci());
 610   // locals
 611   st->print_cr(" - locals [" INTPTR_FORMAT "]", p2i(interpreter_frame_local_at<relative>(0)));
 612   // method
 613   st->print(" - method [" INTPTR_FORMAT "]", p2i(interpreter_frame_method()));
 614   st->fill_to(23);
 615   st->print("; ");
 616   interpreter_frame_method()->print_name(st);
 617   st->cr();
 618 #endif
 619 }
 620 
 621 // Print whether the frame is in the VM or OS indicating a HotSpot problem.
 622 // Otherwise, it's likely a bug in the native library that the Java code calls,
 623 // hopefully indicating where to submit bugs.
 624 void frame::print_C_frame(outputStream* st, char* buf, int buflen, address pc) {
 625   // C/C++ frame
 626   bool in_vm = os::address_is_in_vm(pc);
 627   st->print(in_vm ? "V" : "C");
 628 
 629   int offset;
 630   bool found;
 631 

 670       Method* m = this->interpreter_frame_method();
 671       if (m != NULL) {
 672         m->name_and_sig_as_C_string(buf, buflen);
 673         st->print("j  %s", buf);
 674         st->print("+%d", this->interpreter_frame_bci());
 675         ModuleEntry* module = m->method_holder()->module();
 676         if (module->is_named()) {
 677           module->name()->as_C_string(buf, buflen);
 678           st->print(" %s", buf);
 679           if (module->version() != NULL) {
 680             module->version()->as_C_string(buf, buflen);
 681             st->print("@%s", buf);
 682           }
 683         }
 684       } else {
 685         st->print("j  " PTR_FORMAT, p2i(pc()));
 686       }
 687     } else if (StubRoutines::contains(pc())) {
 688       StubCodeDesc* desc = StubCodeDesc::desc_for(pc());
 689       if (desc != NULL) {
 690         st->print("v  ~StubRoutines::%s " PTR_FORMAT, desc->name(), p2i(pc()));
 691       } else {
 692         st->print("v  ~StubRoutines::" PTR_FORMAT, p2i(pc()));
 693       }
 694     } else if (_cb->is_buffer_blob()) {
 695       st->print("v  ~BufferBlob::%s " PTR_FORMAT, ((BufferBlob *)_cb)->name(), p2i(pc()));
 696     } else if (_cb->is_compiled()) {
 697       CompiledMethod* cm = (CompiledMethod*)_cb;
 698       Method* m = cm->method();
 699       if (m != NULL) {
 700         if (cm->is_nmethod()) {
 701           nmethod* nm = cm->as_nmethod();
 702           st->print("J %d%s", nm->compile_id(), (nm->is_osr_method() ? "%" : ""));
 703           st->print(" %s", nm->compiler_name());
 704         }
 705         m->name_and_sig_as_C_string(buf, buflen);
 706         st->print(" %s", buf);
 707         ModuleEntry* module = m->method_holder()->module();
 708         if (module->is_named()) {
 709           module->name()->as_C_string(buf, buflen);
 710           st->print(" %s", buf);
 711           if (module->version() != NULL) {
 712             module->version()->as_C_string(buf, buflen);
 713             st->print("@%s", buf);
 714           }
 715         }
 716         st->print(" (%d bytes) @ " PTR_FORMAT " [" PTR_FORMAT "+" INTPTR_FORMAT "]",
 717                   m->code_size(), p2i(_pc), p2i(_cb->code_begin()), _pc - _cb->code_begin());
 718 #if INCLUDE_JVMCI
 719         if (cm->is_nmethod()) {
 720           nmethod* nm = cm->as_nmethod();
 721           const char* jvmciName = nm->jvmci_name();
 722           if (jvmciName != NULL) {
 723             st->print(" (%s)", jvmciName);
 724           }
 725         }
 726 #endif
 727       } else {
 728         st->print("J  " PTR_FORMAT, p2i(pc()));
 729       }
 730     } else if (_cb->is_runtime_stub()) {
 731       st->print("v  ~RuntimeStub::%s " PTR_FORMAT, ((RuntimeStub *)_cb)->name(), p2i(pc()));
 732     } else if (_cb->is_deoptimization_stub()) {
 733       st->print("v  ~DeoptimizationBlob " PTR_FORMAT, p2i(pc()));
 734     } else if (_cb->is_exception_stub()) {
 735       st->print("v  ~ExceptionBlob " PTR_FORMAT, p2i(pc()));
 736     } else if (_cb->is_safepoint_stub()) {
 737       st->print("v  ~SafepointBlob " PTR_FORMAT, p2i(pc()));
 738     } else if (_cb->is_adapter_blob()) {
 739       st->print("v  ~AdapterBlob " PTR_FORMAT, p2i(pc()));
 740     } else if (_cb->is_vtable_blob()) {
 741       st->print("v  ~VtableBlob " PTR_FORMAT, p2i(pc()));
 742     } else if (_cb->is_method_handles_adapter_blob()) {
 743       st->print("v  ~MethodHandlesAdapterBlob " PTR_FORMAT, p2i(pc()));
 744     } else if (_cb->is_uncommon_trap_stub()) {
 745       st->print("v  ~UncommonTrapBlob " PTR_FORMAT, p2i(pc()));
 746     } else {
 747       st->print("v  blob " PTR_FORMAT, p2i(pc()));
 748     }
 749   } else {
 750     print_C_frame(st, buf, buflen, pc());
 751   }
 752 }
 753 
 754 
 755 /*
 756   The interpreter_frame_expression_stack_at method in the case of SPARC needs the
 757   max_stack value of the method in order to compute the expression stack address.
 758   It uses the Method* in order to get the max_stack value but during GC this
 759   Method* value saved on the frame is changed by reverse_and_push and hence cannot
 760   be used. So we save the max_stack value in the FrameClosure object and pass it
 761   down to the interpreter_frame_expression_stack_at method
 762 */
 763 template <bool relative>
 764 class InterpreterFrameClosure : public OffsetClosure {
 765  private:
 766   const frame* _fr;
 767   OopClosure*  _f;
 768   int          _max_locals;
 769   int          _max_stack;
 770 
 771  public:
 772   InterpreterFrameClosure(const frame* fr, int max_locals, int max_stack,
 773                           OopClosure* f) {
 774     _fr         = fr;
 775     _max_locals = max_locals;
 776     _max_stack  = max_stack;
 777     _f          = f;
 778   }
 779 
 780   void offset_do(int offset) {
 781     oop* addr;
 782     if (offset < _max_locals) {
 783       addr = (oop*) _fr->interpreter_frame_local_at<relative>(offset);
 784       assert((intptr_t*)addr >= _fr->sp(), "must be inside the frame");
 785       DEBUG_ONLY(if (log_develop_is_enabled(Trace, jvmcont) && relative) log_develop_trace(jvmcont)("InterpreterFrameClosure::offset_do local p: " INTPTR_FORMAT, p2i(addr));)
 786       _f->do_oop(addr);
 787     } else {
 788       addr = (oop*) _fr->interpreter_frame_expression_stack_at<relative>((offset - _max_locals));
 789       // In case of exceptions, the expression stack is invalid and the esp will be reset to express
 790       // this condition. Therefore, we call f only if addr is 'inside' the stack (i.e., addr >= esp for Intel).
 791       bool in_stack;
 792       if (frame::interpreter_frame_expression_stack_direction() > 0) {
 793         in_stack = (intptr_t*)addr <= _fr->interpreter_frame_tos_address<relative>();
 794       } else {
 795         in_stack = (intptr_t*)addr >= _fr->interpreter_frame_tos_address<relative>();
 796       }
 797       if (in_stack) {
 798         DEBUG_ONLY(if (log_develop_is_enabled(Trace, jvmcont) && relative) log_develop_trace(jvmcont)("InterpreterFrameClosure::offset_do stack p: " INTPTR_FORMAT, p2i(addr));)
 799         _f->do_oop(addr);
 800       }
 801     }
 802   }
 803 
 804   int max_locals()  { return _max_locals; }
 805 };
 806 
 807 
 808 class InterpretedArgumentOopFinder: public SignatureIterator {
 809  private:
 810   OopClosure*  _f;             // Closure to invoke
 811   int          _offset;        // TOS-relative offset, decremented with each argument
 812   bool         _has_receiver;  // true if the callee has a receiver
 813   const frame* _fr;
 814 
 815   friend class SignatureIterator;  // so do_parameters_on can call do_type
 816   void do_type(BasicType type) {
 817     _offset -= parameter_type_word_count(type);
 818     if (is_reference_type(type)) oop_offset_do();

 885     _f = NULL; // will be set later
 886     _fr = frame;
 887     _is_static = is_static;
 888     _offset = ArgumentSizeComputer(signature).size();  // pre-decremented down to zero
 889   }
 890 
 891   void arguments_do(OopClosure* f) {
 892     _f = f;
 893     if (!_is_static)  oop_at_offset_do(_offset); // do the receiver
 894     do_parameters_on(this);
 895   }
 896 
 897 };
 898 
 899 oop* frame::interpreter_callee_receiver_addr(Symbol* signature) {
 900   ArgumentSizeComputer asc(signature);
 901   int size = asc.size();
 902   return (oop *)interpreter_frame_tos_at(size);
 903 }
 904 
 905 oop frame::interpreter_callee_receiver(Symbol* signature) {
 906   // TODO: Erik: remove after integration with concurrent stack scanning
 907   oop r = *interpreter_callee_receiver_addr(signature);
 908   r = NativeAccess<>::oop_load(&r);
 909   return r;
 910 }
 911 
 912 template <bool relative>
 913 void frame::oops_interpreted_do(OopClosure* f, const RegisterMap* map, bool query_oop_map_cache) const {


 914   Thread *thread = Thread::current();
 915   methodHandle m (thread, interpreter_frame_method());
 916   jint bci = interpreter_frame_bci();
 917 
 918   InterpreterOopMap mask;
 919   if (query_oop_map_cache) {
 920     m->mask_for(bci, &mask);
 921   } else {
 922     OopMapCache::compute_one_oop_map(m, bci, &mask);
 923   }
 924   
 925   oops_interpreted_do0<relative>(f, map, m, bci, mask);
 926 }
 927 
 928 // Initialize explicitly so that these can be used only with definitions.
 929 // TODO: Rectify as Loom stabilizes...
 930 template void frame::oops_interpreted_do<true> (OopClosure* f, const RegisterMap* map, bool query_oop_map_cache) const;
 931 template void frame::oops_interpreted_do<false>(OopClosure* f, const RegisterMap* map, bool query_oop_map_cache) const;
 932 
 933 template <bool relative>
 934 void frame::oops_interpreted_do(OopClosure* f, const RegisterMap* map, const InterpreterOopMap& mask) const {
 935   Thread *thread = Thread::current();
 936   methodHandle m (thread, interpreter_frame_method());
 937   jint bci = interpreter_frame_bci();
 938   oops_interpreted_do0<relative>(f, map, m, bci, mask);
 939 }
 940 
 941 template <bool relative>
 942 void frame::oops_interpreted_do0(OopClosure* f, const RegisterMap* map, methodHandle m, jint bci, const InterpreterOopMap& mask) const {
 943   assert(is_interpreted_frame(), "Not an interpreted frame");
 944   assert(!Universe::heap()->is_in(m()),
 945           "must be valid oop");
 946   assert(m->is_method(), "checking frame value");
 947   assert((m->is_native() && bci == 0)  ||
 948          (!m->is_native() && bci >= 0 && bci < m->code_size()),
 949          "invalid bci value");
 950 
 951   // Handle the monitor elements in the activation
 952   for (
 953     BasicObjectLock* current = interpreter_frame_monitor_end<relative>();
 954     current < interpreter_frame_monitor_begin();
 955     current = next_monitor_in_interpreter_frame<relative>(current)
 956   ) {
 957 #ifdef ASSERT
 958     interpreter_frame_verify_monitor<relative>(current);
 959 #endif
 960     current->oops_do(f);
 961   }
 962 
 963   if (m->is_native()) {
 964     f->do_oop(interpreter_frame_temp_oop_addr());
 965   }
 966 
 967   // The method pointer in the frame might be the only path to the method's
 968   // klass, and the klass needs to be kept alive while executing. The GCs
 969   // don't trace through method pointers, so the mirror of the method's klass
 970   // is installed as a GC root.
 971   f->do_oop(interpreter_frame_mirror_addr());
 972 
 973   int max_locals = m->is_native() ? m->size_of_parameters() : m->max_locals();
 974 
 975   Symbol* signature = NULL;
 976   bool has_receiver = false;
 977 
 978   // Process a callee's arguments if we are at a call site
 979   // (i.e., if we are at an invoke bytecode)
 980   // This is used sometimes for calling into the VM, not for another
 981   // interpreted or compiled frame.
 982   if (!m->is_native()) {
 983     Bytecode_invoke call = Bytecode_invoke_check(m, bci);
 984     if (call.is_valid()) {
 985       signature = call.signature();
 986       has_receiver = call.has_receiver();
 987       if (map != NULL && map->include_argument_oops() &&
 988           interpreter_frame_expression_stack_size<relative>() > 0) {
 989         // ResourceMark rm(thread);  // is this right ???
 990         // we are at a call site & the expression stack is not empty
 991         // => process callee's arguments
 992         //
 993         // Note: The expression stack can be empty if an exception
 994         //       occurred during method resolution/execution. In all
 995         //       cases we empty the expression stack completely be-
 996         //       fore handling the exception (the exception handling
 997         //       code in the interpreter calls a blocking runtime
 998         //       routine which can cause this code to be executed).
 999         //       (was bug gri 7/27/98)
1000         oops_interpreted_arguments_do(signature, has_receiver, f);
1001       }
1002     }
1003   }
1004 
1005   InterpreterFrameClosure<relative> blk(this, max_locals, m->max_stack(), f);
1006 
1007   // process locals & expression stack
1008   // mask.print();





1009   mask.iterate_oop(&blk);
1010 }
1011 
1012 
1013 void frame::oops_interpreted_arguments_do(Symbol* signature, bool has_receiver, OopClosure* f) const {
1014   InterpretedArgumentOopFinder finder(signature, has_receiver, this, f);
1015   finder.oops_do();
1016 }
1017 
1018 void frame::oops_code_blob_do(OopClosure* f, CodeBlobClosure* cf, DerivedOopClosure* df, DerivedPointerIterationMode derived_mode, const RegisterMap* reg_map) const {

1019   assert(_cb != NULL, "sanity check");
1020   assert((oop_map() == NULL) == (_cb->oop_maps() == NULL), "frame and _cb must agree that oopmap is set or not");
1021   if (oop_map() != NULL) {
1022     if (df != NULL) {
1023       _oop_map->oops_do(this, reg_map, f, df);
1024     } else {
1025       _oop_map->oops_do(this, reg_map, f, derived_mode);
1026     }
1027 
1028     // Preserve potential arguments for a callee. We handle this by dispatching
1029     // on the codeblob. For c2i, we do
1030     if (reg_map->include_argument_oops()) {
1031       _cb->preserve_callee_argument_oops(*this, reg_map, f);
1032     }
1033   }
1034   // In cases where perm gen is collected, GC will want to mark
1035   // oops referenced from nmethods active on thread stacks so as to
1036   // prevent them from being collected. However, this visit should be
1037   // restricted to certain phases of the collection only. The
1038   // closure decides how it wants nmethods to be traced.
1039   if (cf != NULL)
1040     cf->do_code_blob(_cb);
1041 }
1042 
1043 class CompiledArgumentOopFinder: public SignatureIterator {
1044  protected:
1045   OopClosure*     _f;
1046   int             _offset;        // the current offset, incremented with each argument
1047   bool            _has_receiver;  // true if the callee has a receiver
1048   bool            _has_appendix;  // true if the call has an appendix
1049   frame           _fr;
1050   RegisterMap*    _reg_map;
1051   int             _arg_size;
1052   VMRegPair*      _regs;        // VMReg list of arguments
1053 
1054   friend class SignatureIterator;  // so do_parameters_on can call do_type
1055   void do_type(BasicType type) {
1056     if (is_reference_type(type))  handle_oop_offset();
1057     _offset += parameter_type_word_count(type);
1058   }
1059 
1060   virtual void handle_oop_offset() {
1061     // Extract low order register number from register array.
1062     // In LP64-land, the high-order bits are valid but unhelpful.
1063     VMReg reg = _regs[_offset].first();
1064     oop *loc = _fr.oopmapreg_to_oop_location(reg, _reg_map);
1065   #ifdef ASSERT
1066     if (loc == NULL) {
1067       if (_reg_map->should_skip_missing())
1068         return;
1069       tty->print_cr("Error walking frame oops:");
1070       _fr.print_on(tty);
1071       assert(loc != NULL, "missing register map entry reg: " INTPTR_FORMAT " %s loc: " INTPTR_FORMAT, reg->value(), reg->name(), p2i(loc));
1072     }
1073   #endif
1074     _f->do_oop(loc);
1075   }
1076 
1077  public:
1078   CompiledArgumentOopFinder(Symbol* signature, bool has_receiver, bool has_appendix, OopClosure* f, frame fr, const RegisterMap* reg_map)
1079     : SignatureIterator(signature) {
1080 
1081     // initialize CompiledArgumentOopFinder
1082     _f         = f;
1083     _offset    = 0;
1084     _has_receiver = has_receiver;
1085     _has_appendix = has_appendix;
1086     _fr        = fr;
1087     _reg_map   = (RegisterMap*)reg_map;
1088     _arg_size  = ArgumentSizeComputer(signature).size() + (has_receiver ? 1 : 0) + (has_appendix ? 1 : 0);
1089 
1090     int arg_size;
1091     _regs = SharedRuntime::find_callee_arguments(signature, has_receiver, has_appendix, &arg_size);
1092     assert(arg_size == _arg_size, "wrong arg size");
1093   }
1094 
1095   void oops_do() {
1096     if (_has_receiver) {
1097       handle_oop_offset();
1098       _offset++;
1099     }
1100     do_parameters_on(this);
1101     if (_has_appendix) {
1102       handle_oop_offset();
1103       _offset++;
1104     }
1105   }
1106 };
1107 
1108 void frame::oops_compiled_arguments_do(Symbol* signature, bool has_receiver, bool has_appendix,
1109                                        const RegisterMap* reg_map, OopClosure* f) const {
1110   // ResourceMark rm;
1111   CompiledArgumentOopFinder finder(signature, has_receiver, has_appendix, f, *this, reg_map);
1112   finder.oops_do();
1113 }
1114 

1115 // Get receiver out of callers frame, i.e. find parameter 0 in callers
1116 // frame.  Consult ADLC for where parameter 0 is to be found.  Then
1117 // check local reg_map for it being a callee-save register or argument
1118 // register, both of which are saved in the local frame.  If not found
1119 // there, it must be an in-stack argument of the caller.
1120 // Note: caller.sp() points to callee-arguments
1121 oop frame::retrieve_receiver(RegisterMap* reg_map) {
1122   frame caller = *this;
1123 
1124   // First consult the ADLC on where it puts parameter 0 for this signature.
1125   VMReg reg = SharedRuntime::name_for_receiver();
1126   oop* oop_adr = caller.oopmapreg_to_oop_location(reg, reg_map);
1127   if (oop_adr == NULL) {
1128     guarantee(oop_adr != NULL, "bad register save location");
1129     return NULL;
1130   }
1131   oop r = *oop_adr;
1132   // TODO: Erik: remove after integration with concurrent stack scanning
1133   r = NativeAccess<>::oop_load(&r);
1134   assert(Universe::heap()->is_in_or_null(r), "bad receiver: " INTPTR_FORMAT " (" INTX_FORMAT ")", p2i(r), p2i(r));
1135   return r;
1136 }
1137 
1138 
1139 BasicLock* frame::get_native_monitor() {
1140   nmethod* nm = (nmethod*)_cb;
1141   assert(_cb != NULL && _cb->is_nmethod() && nm->method()->is_native(),
1142          "Should not call this unless it's a native nmethod");
1143   int byte_offset = in_bytes(nm->native_basic_lock_sp_offset());
1144   assert(byte_offset >= 0, "should not see invalid offset");
1145   return (BasicLock*) &sp()[byte_offset / wordSize];
1146 }
1147 
1148 oop frame::get_native_receiver() {
1149   nmethod* nm = (nmethod*)_cb;
1150   assert(_cb != NULL && _cb->is_nmethod() && nm->method()->is_native(),
1151          "Should not call this unless it's a native nmethod");
1152   int byte_offset = in_bytes(nm->native_receiver_sp_offset());
1153   assert(byte_offset >= 0, "should not see invalid offset");
1154   oop owner = ((oop*) sp())[byte_offset / wordSize];
1155   // TODO: Erik: remove after integration with concurrent stack scanning
1156   owner = NativeAccess<>::oop_load(&owner);
1157   assert( Universe::heap()->is_in(owner), "bad receiver" );
1158   return owner;
1159 }
1160 
1161 void frame::oops_entry_do(OopClosure* f, const RegisterMap* map) const {
1162   assert(map != NULL, "map must be set");
1163   if (map->include_argument_oops()) {
1164     // must collect argument oops, as nobody else is doing it
1165     Thread *thread = Thread::current();
1166     methodHandle m (thread, entry_frame_call_wrapper()->callee_method());
1167     EntryFrameOopFinder finder(this, m->signature(), m->is_static());
1168     finder.arguments_do(f);
1169   }
1170   // Traverse the Handle Block saved in the entry frame
1171   entry_frame_call_wrapper()->oops_do(f);
1172 }
1173 
1174 bool frame::is_deoptimized_frame() const {
1175   assert(_deopt_state != unknown, "not answerable");
1176   if (_deopt_state == is_deoptimized) {
1177     return true;
1178   }
1179 
1180   /* This method only checks if the frame is deoptimized
1181    * as in return address being patched. 
1182    * It doesn't care if the OP that we return to is a 
1183    * deopt instruction */
1184   /*if (_cb != NULL && _cb->is_nmethod()) {
1185     return NativeDeoptInstruction::is_deopt_at(_pc);
1186   }*/
1187   return false;
1188 }
1189 
1190 void frame::oops_do_internal(OopClosure* f, CodeBlobClosure* cf, DerivedOopClosure* df, DerivedPointerIterationMode derived_mode, const RegisterMap* map, bool use_interpreter_oop_map_cache) const {

1191 #ifndef PRODUCT
1192   // simulate GC crash here to dump java thread in error report
1193   if (CrashGCForDumpingJavaThread) {
1194     char *t = NULL;
1195     *t = 'c';
1196   }
1197 #endif
1198   if (is_interpreted_frame()) {
1199     map->thread() != NULL ? oops_interpreted_do<false>(f, map, use_interpreter_oop_map_cache)
1200                           : oops_interpreted_do<true >(f, map, use_interpreter_oop_map_cache);
1201   } else if (is_entry_frame()) {
1202     oops_entry_do(f, map);
1203   } else if (is_optimized_entry_frame()) {
1204     _cb->as_optimized_entry_blob()->oops_do(f, *this);
1205   } else if (CodeCache::contains(pc())) {
1206     oops_code_blob_do(f, cf, df, derived_mode, map);
1207   } else {
1208     ShouldNotReachHere();
1209   }
1210 }
1211 
1212 void frame::nmethods_do(CodeBlobClosure* cf) const {
1213   if (_cb != NULL && _cb->is_nmethod()) {
1214     cf->do_code_blob(_cb);
1215   }
1216 }
1217 
1218 
1219 // Call f closure on the interpreted Method*s in the stack.
1220 void frame::metadata_do(MetadataClosure* f) const {
1221   ResourceMark rm;
1222   if (is_interpreted_frame()) {
1223     Method* m = this->interpreter_frame_method();
1224     assert(m != NULL, "expecting a method in this frame");
1225     f->do_metadata(m);
1226   }
1227 }
1228 
1229 void frame::verify(const RegisterMap* map) const {
1230 #ifndef PRODUCT
1231   if (TraceCodeBlobStacks) {
1232     tty->print_cr("*** verify");
1233     print_on(tty);
1234   }
1235 #endif
1236 
1237   // for now make sure receiver type is correct
1238   if (is_interpreted_frame()) {
1239     Method* method = interpreter_frame_method();
1240     guarantee(method->is_method(), "method is wrong in frame::verify");
1241     if (!method->is_static()) {
1242       // fetch the receiver
1243       oop* p = (oop*) interpreter_frame_local_at(0);
1244       // make sure we have the right receiver type
1245     }
1246   }
1247 #if COMPILER2_OR_JVMCI
1248   assert(DerivedPointerTable::is_empty(), "must be empty before verify");
1249 #endif
1250 
1251   if (map->update_map()) { // The map has to be up-to-date for the current frame
1252     oops_do_internal(&VerifyOopClosure::verify_oop, NULL, NULL, DerivedPointerIterationMode::_ignore, map, false);
1253   }
1254 }
1255 
1256 
1257 #ifdef ASSERT
1258 bool frame::verify_return_pc(address x) {
1259   if (StubRoutines::returns_to_call_stub(x)) {
1260     return true;
1261   }
1262   if (CodeCache::contains(x)) {
1263     return true;
1264   }
1265   if (Interpreter::contains(x)) {
1266     return true;
1267   }
1268   return false;
1269 }
1270 #endif
1271 
1272 #ifdef ASSERT
1273 template <bool relative>
1274 void frame::interpreter_frame_verify_monitor(BasicObjectLock* value) const {
1275   assert(is_interpreted_frame(), "Not an interpreted frame");
1276   // verify that the value is in the right part of the frame
1277   address low_mark  = (address) interpreter_frame_monitor_end<relative>();
1278   address high_mark = (address) interpreter_frame_monitor_begin();
1279   address current   = (address) value;
1280 
1281   const int monitor_size = frame::interpreter_frame_monitor_size();
1282   guarantee((high_mark - current) % monitor_size  ==  0         , "Misaligned top of BasicObjectLock*");
1283   guarantee( high_mark > current                                , "Current BasicObjectLock* higher than high_mark");
1284 
1285   guarantee((current - low_mark) % monitor_size  ==  0         , "Misaligned bottom of BasicObjectLock*");
1286   guarantee( current >= low_mark                               , "Current BasicObjectLock* below than low_mark");
1287 }
1288 #endif
1289 
1290 #ifndef PRODUCT
1291 
1292 // Returns true iff the address p is readable and *(intptr_t*)p != errvalue
1293 extern "C" bool dbg_is_safe(const void* p, intptr_t errvalue);
1294 
1295 class FrameValuesOopClosure: public OopClosure, public DerivedOopClosure {
1296 private:
1297   FrameValues& _values;
1298   int _frame_no;
1299 public:
1300   FrameValuesOopClosure(FrameValues& values, int frame_no) : _values(values), _frame_no(frame_no) {}
1301   virtual void do_oop(oop* p) {
1302     bool good = *p == nullptr || (dbg_is_safe(*p, -1) && dbg_is_safe((*p)->klass(), -1) && oopDesc::is_oop_or_null(*p));
1303     _values.describe(_frame_no, (intptr_t*)p, err_msg("oop%s for #%d", good ? "" : " (BAD)", _frame_no)); 
1304   }
1305   virtual void do_oop(narrowOop* p) { _values.describe(_frame_no, (intptr_t*)p, err_msg("narrow oop for #%d", _frame_no)); }
1306   virtual void do_derived_oop(oop* base, derived_pointer* derived) { 
1307     _values.describe(_frame_no, (intptr_t*)derived, err_msg("derived pointer (base: " INTPTR_FORMAT ") for #%d", p2i(base), _frame_no));
1308   }
1309 };
1310 
1311 class FrameValuesOopMapClosure: public OopMapClosure {
1312 private:
1313   const frame* _fr;
1314   const RegisterMap* _reg_map;
1315   FrameValues& _values;
1316   int _frame_no;
1317 public:
1318   FrameValuesOopMapClosure(const frame* fr, const RegisterMap* reg_map, FrameValues& values, int frame_no)
1319    : _fr(fr), _reg_map(reg_map), _values(values), _frame_no(frame_no) {}
1320 
1321   virtual void do_value(VMReg reg, OopMapValue::oop_types type) {
1322     intptr_t* p = (intptr_t*)_fr->oopmapreg_to_location(reg, _reg_map);
1323     if (p != NULL && (((intptr_t)p & WordAlignmentMask) == 0)) {
1324       const char* type_name = NULL;
1325       switch(type) {
1326         case OopMapValue::oop_value:          type_name = "oop";          break;
1327         case OopMapValue::narrowoop_value:    type_name = "narrow oop";   break;
1328         case OopMapValue::callee_saved_value: type_name = "callee-saved"; break;
1329         case OopMapValue::derived_oop_value:  type_name = "derived";      break;
1330         // case OopMapValue::live_value:         type_name = "live";         break;
1331         default: break;
1332       }
1333       if (type_name != NULL)
1334         _values.describe(_frame_no, p, err_msg("%s for #%d", type_name, _frame_no));
1335     }
1336   }
1337 };
1338 
1339 template void frame::describe<false>(FrameValues& values, int frame_no, const RegisterMap* reg_map);
1340 template void frame::describe<true >(FrameValues& values, int frame_no, const RegisterMap* reg_map);
1341 
1342 // callers need a ResourceMark because of name_and_sig_as_C_string() usage,
1343 // RA allocated string is returned to the caller
1344 template <bool relative>
1345 void frame::describe(FrameValues& values, int frame_no, const RegisterMap* reg_map) {
1346   // boundaries: sp and the 'real' frame pointer
1347   values.describe(-1, sp(), err_msg("sp for #%d", frame_no), 0);
1348   intptr_t* frame_pointer = real_fp(); // Note: may differ from fp()
1349 
1350   // print frame info at the highest boundary
1351   intptr_t* info_address = MAX2(sp(), frame_pointer);
1352 
1353   if (info_address != frame_pointer) {
1354     // print frame_pointer explicitly if not marked by the frame info
1355     values.describe(-1, frame_pointer, err_msg("frame pointer for #%d", frame_no), 1);
1356   }
1357 
1358   if (is_entry_frame() || is_compiled_frame() || is_interpreted_frame() || is_native_frame()) {
1359     // Label values common to most frames
1360     values.describe(-1, unextended_sp(), err_msg("unextended_sp for #%d", frame_no), 0);
1361   }
1362 
1363   if (is_interpreted_frame()) {
1364     Method* m = interpreter_frame_method();
1365     int bci = interpreter_frame_bci();
1366     InterpreterCodelet* desc = Interpreter::codelet_containing(pc());
1367 
1368     // Label the method and current bci
1369     values.describe(-1, info_address,
1370                     FormatBuffer<1024>("#%d method %s @ %d", frame_no, m->name_and_sig_as_C_string(), bci), 3);
1371     if (desc != NULL) {
1372       values.describe(-1, info_address, err_msg("- %s codelet: %s", 
1373         desc->bytecode()    >= 0    ? Bytecodes::name(desc->bytecode()) : "",
1374         desc->description() != NULL ? desc->description()               : "?"), 2);
1375     }
1376     values.describe(-1, info_address,
1377                     err_msg("- %d locals %d max stack", m->max_locals(), m->max_stack()), 2);
1378     values.describe(frame_no, (intptr_t*)sender_pc_addr(), Continuation::is_return_barrier_entry(*sender_pc_addr()) ? "return address (return barrier)" : "return address");
1379 
1380     if (m->max_locals() > 0) {
1381       intptr_t* l0 = interpreter_frame_local_at<relative>(0);
1382       intptr_t* ln = interpreter_frame_local_at<relative>(m->max_locals() - 1);
1383       values.describe(-1, MAX2(l0, ln), err_msg("locals for #%d", frame_no), 2);
1384       // Report each local and mark as owned by this frame
1385       for (int l = 0; l < m->max_locals(); l++) {
1386         intptr_t* l0 = interpreter_frame_local_at<relative>(l);
1387         values.describe(frame_no, l0, err_msg("local %d", l), 1);
1388       }
1389     }
1390 
1391     if (interpreter_frame_monitor_begin() != interpreter_frame_monitor_end<relative>()) {
1392       values.describe(frame_no, (intptr_t*)interpreter_frame_monitor_begin(), "monitors begin");
1393       values.describe(frame_no, (intptr_t*)interpreter_frame_monitor_end<relative>(), "monitors end");
1394     }
1395 
1396     // Compute the actual expression stack size
1397     InterpreterOopMap mask;
1398     OopMapCache::compute_one_oop_map(methodHandle(Thread::current(), m), bci, &mask);
1399     intptr_t* tos = NULL;
1400     // Report each stack element and mark as owned by this frame
1401     for (int e = 0; e < mask.expression_stack_size(); e++) {
1402       tos = MAX2(tos, interpreter_frame_expression_stack_at<relative>(e));
1403       values.describe(frame_no, interpreter_frame_expression_stack_at<relative>(e),
1404                       err_msg("stack %d", e), 1);
1405     }
1406     if (tos != NULL) {
1407       values.describe(-1, tos, err_msg("expression stack for #%d", frame_no), 2);
1408     }
1409 
1410     if (reg_map != NULL) {
1411       FrameValuesOopClosure oopsFn(values, frame_no);
1412       oops_do(&oopsFn, NULL, &oopsFn, reg_map);
1413     }
1414   } else if (is_entry_frame()) {
1415     // For now just label the frame
1416     values.describe(-1, info_address, err_msg("#%d entry frame", frame_no), 2);
1417   } else if (cb()->is_compiled()) {
1418     // For now just label the frame
1419     CompiledMethod* cm = cb()->as_compiled_method();
1420     values.describe(-1, info_address,
1421                     FormatBuffer<1024>("#%d nmethod " INTPTR_FORMAT " for method J %s%s", frame_no,
1422                                        p2i(cm),
1423                                        cm->method()->name_and_sig_as_C_string(),
1424                                        (_deopt_state == is_deoptimized) ?
1425                                        " (deoptimized)" :
1426                                        ((_deopt_state == unknown) ? " (state unknown)" : "")),
1427                     3);
1428 
1429     { // mark arguments (see nmethod::print_nmethod_labels)
1430       Method* m = cm->method();
1431 
1432       int stack_slot_offset = cm->frame_size() * wordSize; // offset, in bytes, to caller sp
1433       int sizeargs = m->size_of_parameters();
1434 
1435       BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sizeargs);
1436       VMRegPair* regs   = NEW_RESOURCE_ARRAY(VMRegPair, sizeargs);
1437       {
1438         int sig_index = 0;
1439         if (!m->is_static()) sig_bt[sig_index++] = T_OBJECT; // 'this'
1440         for (SignatureStream ss(m->signature()); !ss.at_return_type(); ss.next()) {
1441           BasicType t = ss.type();
1442           assert(type2size[t] == 1 || type2size[t] == 2, "size is 1 or 2");
1443           sig_bt[sig_index++] = t;
1444           if (type2size[t] == 2) sig_bt[sig_index++] = T_VOID;
1445         }
1446         assert(sig_index == sizeargs, "");
1447       }
1448       int out_preserve = SharedRuntime::java_calling_convention(sig_bt, regs, sizeargs);
1449       assert (out_preserve ==  m->num_stack_arg_slots(), "");
1450       int sig_index = 0;
1451       int arg_index = (m->is_static() ? 0 : -1);
1452       for (SignatureStream ss(m->signature()); !ss.at_return_type(); ) {
1453         bool at_this = (arg_index == -1);
1454         bool at_old_sp = false;
1455         BasicType t = (at_this ? T_OBJECT : ss.type());
1456         assert(t == sig_bt[sig_index], "sigs in sync");
1457         VMReg fst = regs[sig_index].first();
1458         if (fst->is_stack()) {
1459           assert (((int)fst->reg2stack()) >= 0, "reg2stack: " INTPTR_FORMAT, fst->reg2stack());
1460           int offset = fst->reg2stack() * VMRegImpl::stack_slot_size + stack_slot_offset;
1461           intptr_t* stack_address = (intptr_t*)((address)sp() + offset);
1462           if (at_this)
1463             values.describe(frame_no, stack_address, err_msg("this for #%d", frame_no), 1);
1464           else
1465             values.describe(frame_no, stack_address, err_msg("param %d %s for #%d", arg_index, type2name(t), frame_no), 1);
1466         }
1467         sig_index += type2size[t];
1468         arg_index += 1;
1469         if (!at_this) ss.next();
1470       }
1471     }
1472 
1473     if (reg_map != NULL && is_java_frame()) {
1474       int scope_no = 0;
1475       for (ScopeDesc* scope = cm->scope_desc_at(pc()); scope != NULL; scope = scope->sender(), scope_no++) {
1476         Method* m = scope->method();
1477         int  bci = scope->bci();
1478         values.describe(-1, info_address, err_msg("- #%d scope %s @ %d", scope_no, m->name_and_sig_as_C_string(), bci), 2);
1479 
1480         { // mark locals
1481           GrowableArray<ScopeValue*>* scvs = scope->locals();
1482           int scvs_length = scvs != NULL ? scvs->length() : 0;
1483           for (int i = 0; i < scvs_length; i++) {
1484             intptr_t* stack_address = (intptr_t*)StackValue::stack_value_address(this, reg_map, scvs->at(i));
1485             if (stack_address != NULL)
1486               values.describe(frame_no, stack_address, err_msg("local %d for #%d (scope %d)", i, frame_no, scope_no), 1);
1487           }
1488         }
1489         { // mark expression stack
1490           GrowableArray<ScopeValue*>* scvs = scope->expressions();
1491           int scvs_length = scvs != NULL ? scvs->length() : 0;
1492           for (int i = 0; i < scvs_length; i++) {
1493             intptr_t* stack_address = (intptr_t*)StackValue::stack_value_address(this, reg_map, scvs->at(i));
1494             if (stack_address != NULL)
1495               values.describe(frame_no, stack_address, err_msg("stack %d for #%d (scope %d)", i, frame_no, scope_no), 1);
1496           }
1497         }
1498       }
1499 
1500       FrameValuesOopClosure oopsFn(values, frame_no);
1501       oops_do(&oopsFn, NULL, &oopsFn, reg_map);
1502 
1503       if (oop_map() != NULL) {
1504         FrameValuesOopMapClosure valuesFn(this, reg_map, values, frame_no);
1505         // also OopMapValue::live_value ??
1506         oop_map()->all_type_do(this, OopMapValue::callee_saved_value, &valuesFn);
1507       }
1508     }
1509 
1510     if (cm->method()->is_continuation_enter_intrinsic()) {
1511       address usp = (address)unextended_sp();
1512       values.describe(frame_no, (intptr_t*)(usp + in_bytes(ContinuationEntry::parent_offset())), "parent");
1513       values.describe(frame_no, (intptr_t*)(usp + in_bytes(ContinuationEntry::cont_offset())),   "continuation");
1514       values.describe(frame_no, (intptr_t*)(usp + in_bytes(ContinuationEntry::chunk_offset())),   "chunk");
1515       values.describe(frame_no, (intptr_t*)(usp + in_bytes(ContinuationEntry::argsize_offset())), "argsize");
1516       // values.describe(frame_no, (intptr_t*)(usp + in_bytes(ContinuationEntry::parent_cont_fastpath_offset())),      "parent fastpath");
1517       // values.describe(frame_no, (intptr_t*)(usp + in_bytes(ContinuationEntry::parent_held_monitor_count_offset())), "parent held monitor count");
1518     }
1519   } else if (is_native_frame()) {
1520     // For now just label the frame
1521     nmethod* nm = cb()->as_nmethod_or_null();
1522     values.describe(-1, info_address,
1523                     FormatBuffer<1024>("#%d nmethod " INTPTR_FORMAT " for native method %s", frame_no,
1524                                        p2i(nm), nm->method()->name_and_sig_as_C_string()), 2);
1525   } else {
1526     // provide default info if not handled before
1527     char *info = (char *) "special frame";
1528     if ((_cb != NULL) &&
1529         (_cb->name() != NULL)) {
1530       info = (char *)_cb->name();
1531     }
1532     values.describe(-1, info_address, err_msg("#%d <%s>", frame_no, info), 2);
1533   }
1534 
1535   // platform dependent additional data
1536   describe_pd(values, frame_no);
1537 }
1538 
1539 void frame::describe_top(FrameValues& values) {
1540   describe_top_pd(values);
1541 }
1542 #endif
1543 
1544 #ifndef PRODUCT
1545 
1546 void FrameValues::describe(int owner, intptr_t* location, const char* description, int priority) {
1547   FrameValue fv;
1548   fv.location = location;
1549   fv.owner = owner;
1550   fv.priority = priority;
1551   fv.description = NEW_RESOURCE_ARRAY(char, strlen(description) + 1);
1552   strcpy(fv.description, description);
1553   _values.append(fv);
1554 }
1555 
1556 
1557 #ifdef ASSERT
1558 void FrameValues::validate() {
1559   _values.sort(compare);
1560   bool error = false;
1561   FrameValue prev;
1562   prev.owner = -1;
1563   for (int i = _values.length() - 1; i >= 0; i--) {
1564     FrameValue fv = _values.at(i);
1565     if (fv.owner == -1) continue;
1566     if (prev.owner == -1) {
1567       prev = fv;
1568       continue;
1569     }
1570     if (prev.location == fv.location) {
1571       if (fv.owner != prev.owner) {
1572         tty->print_cr("overlapping storage");
1573         tty->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", p2i(prev.location), *prev.location, prev.description);
1574         tty->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", p2i(fv.location), *fv.location, fv.description);
1575         error = true;
1576       }
1577     } else {
1578       prev = fv;
1579     }
1580   }
1581   // if (error) { tty->cr(); print_on((JavaThread*)nullptr, tty); }
1582   assert(!error, "invalid layout");
1583 }
1584 #endif // ASSERT
1585 
1586 void FrameValues::print_on(JavaThread* thread, outputStream* st) {
1587   _values.sort(compare);
1588 
1589   // Sometimes values like the fp can be invalid values if the
1590   // register map wasn't updated during the walk.  Trim out values
1591   // that aren't actually in the stack of the thread.
1592   int min_index = 0;
1593   int max_index = _values.length() - 1;
1594   intptr_t* v0 = _values.at(min_index).location;
1595   intptr_t* v1 = _values.at(max_index).location;
1596 
1597   if (thread != NULL) {
1598     if (thread == Thread::current()) {
1599       while (!thread->is_in_live_stack((address)v0)) v0 = _values.at(++min_index).location;
1600       while (!thread->is_in_live_stack((address)v1)) v1 = _values.at(--max_index).location;
1601     } else {
1602       while (!thread->is_in_full_stack((address)v0)) v0 = _values.at(++min_index).location;
1603       while (!thread->is_in_full_stack((address)v1)) v1 = _values.at(--max_index).location;






1604     }
1605   }
1606   
1607   print_on(st, min_index, max_index, v0, v1);
1608 }
1609 
1610 void FrameValues::print_on(stackChunkOop chunk, outputStream* st) {
1611   assert (chunk->is_stackChunk(), "");
1612 
1613   _values.sort(compare);
1614 
1615   intptr_t* start = chunk->start_address();
1616   intptr_t* end = chunk->end_address() + 1;
1617 
1618   int min_index = 0;
1619   int max_index = _values.length() - 1;
1620   intptr_t* v0 = _values.at(min_index).location;
1621   intptr_t* v1 = _values.at(max_index).location;
1622   while (!(start <= v0 && v0 <= end)) v0 = _values.at(++min_index).location;
1623   while (!(start <= v1 && v1 <= end)) v1 = _values.at(--max_index).location;
1624 
1625   print_on(st, min_index, max_index, v0, v1, true);
1626 }
1627 
1628 void FrameValues::print_on(outputStream* st, int min_index, int max_index, intptr_t* v0, intptr_t* v1, bool relative) {
1629   intptr_t* min = MIN2(v0, v1);
1630   intptr_t* max = MAX2(v0, v1);
1631   intptr_t* cur = max;
1632   intptr_t* last = NULL;
1633   for (int i = max_index; i >= min_index; i--) {
1634     FrameValue fv = _values.at(i);
1635     while (cur > fv.location) {
1636       st->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT, p2i(cur), *cur);
1637       cur--;
1638     }
1639     if (last == fv.location) {
1640       const char* spacer = "          " LP64_ONLY("        ");
1641       st->print_cr(" %s  %s %s", spacer, spacer, fv.description);
1642     } else {
1643       if (relative
1644           && *fv.location != 0 && *fv.location > -100 && *fv.location < 100 
1645           && (strncmp(fv.description, "interpreter_frame_", 18) == 0 || strstr(fv.description, " method "))) {
1646         st->print_cr(" " INTPTR_FORMAT ": %18d %s", p2i(fv.location), (int)*fv.location, fv.description);
1647       } else {
1648         st->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", p2i(fv.location), *fv.location, fv.description);
1649       }
1650       last = fv.location;
1651       cur--;
1652     }
1653   }
1654 }
1655 
1656 #endif // ndef PRODUCT
< prev index next >