< 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 "gc/shared/collectedHeap.inline.hpp"
  32 #include "interpreter/interpreter.hpp"
  33 #include "interpreter/oopMapCache.hpp"

  34 #include "memory/resourceArea.hpp"
  35 #include "memory/universe.hpp"
  36 #include "oops/markOop.hpp"
  37 #include "oops/method.hpp"
  38 #include "oops/methodData.hpp"
  39 #include "oops/oop.inline.hpp"
  40 #include "oops/verifyOopClosure.hpp"
  41 #include "prims/methodHandles.hpp"

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

  49 #include "runtime/stubCodeGenerator.hpp"
  50 #include "runtime/stubRoutines.hpp"
  51 #include "runtime/thread.inline.hpp"
  52 #include "utilities/debug.hpp"
  53 #include "utilities/decoder.hpp"
  54 #include "utilities/formatBuffer.hpp"
  55 
  56 RegisterMap::RegisterMap(JavaThread *thread, bool update_map) {

  57   _thread         = thread;
  58   _update_map     = update_map;



  59   clear();
  60   debug_only(_update_for_id = NULL;)













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








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






















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








 106 void RegisterMap::print_on(outputStream* st) const {
 107   st->print_cr("Register map");
 108   for(int i = 0; i < reg_count; i++) {
 109 
 110     VMReg r = VMRegImpl::as_VMReg(i);
 111     intptr_t* src = (intptr_t*) location(r);
 112     if (src != NULL) {
 113 
 114       r->print_on(st);
 115       st->print(" [" INTPTR_FORMAT "] = ", p2i(src));
 116       if (((uintptr_t)src & (sizeof(*src)-1)) != 0) {
 117         st->print_cr("<misaligned>");
 118       } else {
 119         st->print_cr(INTPTR_FORMAT, *src);
 120       }
 121     }
 122   }
 123 }
 124 
 125 void RegisterMap::print() const {
 126   print_on(tty);
 127 }
 128 
 129 #endif
 130 // This returns the pc that if you were in the debugger you'd see. Not
 131 // the idealized value in the frame object. This undoes the magic conversion
 132 // that happens for deoptimized frames. In addition it makes the value the
 133 // hardware would want to see in the native frame. The only user (at this point)
 134 // is deoptimization. It likely no one else should ever use it.
 135 
 136 address frame::raw_pc() const {



 137   if (is_deoptimized_frame()) {
 138     CompiledMethod* cm = cb()->as_compiled_method_or_null();
 139     if (cm->is_method_handle_return(pc()))
 140       return cm->deopt_mh_handler_begin() - pc_return_offset;
 141     else
 142       return cm->deopt_handler_begin() - pc_return_offset;
 143   } else {
 144     return (pc() - pc_return_offset);
 145   }
 146 }
 147 
 148 // Change the pc in a frame object. This does not change the actual pc in
 149 // actual frame. To do that use patch_pc.
 150 //
 151 void frame::set_pc(address   newpc ) {
 152 #ifdef ASSERT
 153   if (_cb != NULL && _cb->is_nmethod()) {
 154     assert(!((nmethod*)_cb)->is_deopt_pc(_pc), "invariant violation");
 155   }
 156 #endif // ASSERT
 157 
 158   // Unsafe to use the is_deoptimzed tester after changing pc
 159   _deopt_state = unknown;
 160   _pc = newpc;
 161   _cb = CodeCache::find_blob_unsafe(_pc);
 162 
 163 }
 164 















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


 250     tty->print("checking (%s) ", nm->is_marked_for_deoptimization() ? "true" : "false");
 251     nm->print_value_on(tty);
 252     tty->cr();
 253   }
 254 
 255   if( !nm->is_marked_for_deoptimization() )
 256     return false;
 257 
 258   // If at the return point, then the frame has already been popped, and
 259   // only the return needs to be executed. Don't deoptimize here.
 260   return !nm->is_at_poll_return(pc());
 261 }
 262 
 263 bool frame::can_be_deoptimized() const {
 264   if (!is_compiled_frame()) return false;
 265   CompiledMethod* nm = (CompiledMethod*)_cb;
 266 
 267   if( !nm->can_be_deoptimized() )
 268     return false;
 269 









 270   return !nm->is_at_poll_return(pc());
 271 }
 272 
 273 void frame::deoptimize(JavaThread* thread) {


 274   assert(thread->frame_anchor()->has_last_Java_frame() &&
 275          thread->frame_anchor()->walkable(), "must be");
 276   // Schedule deoptimization of an nmethod activation with this frame.
 277   assert(_cb != NULL && _cb->is_compiled(), "must be");
 278 



 279   // If the call site is a MethodHandle call site use the MH deopt
 280   // handler.
 281   CompiledMethod* cm = (CompiledMethod*) _cb;
 282   address deopt = cm->is_method_handle_return(pc()) ?
 283                         cm->deopt_mh_handler_begin() :
 284                         cm->deopt_handler_begin();
 285 
 286   // Save the original pc before we patch in the new one
 287   cm->set_original_pc(this, pc());
 288   patch_pc(thread, deopt);

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




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


 791      _f = NULL; // will be set later
 792      _fr = frame;
 793      _is_static = is_static;
 794      _offset = ArgumentSizeComputer(signature).size() - 1; // last parameter is at index 0
 795    }
 796 
 797   void arguments_do(OopClosure* f) {
 798     _f = f;
 799     if (!_is_static) oop_at_offset_do(_offset+1); // do the receiver
 800     iterate_parameters();
 801   }
 802 
 803 };
 804 
 805 oop* frame::interpreter_callee_receiver_addr(Symbol* signature) {
 806   ArgumentSizeComputer asc(signature);
 807   int size = asc.size();
 808   return (oop *)interpreter_frame_tos_at(size);
 809 }
 810 
 811 
 812 void frame::oops_interpreted_do(OopClosure* f, const RegisterMap* map, bool query_oop_map_cache) {
 813   assert(is_interpreted_frame(), "Not an interpreted frame");
 814   assert(map != NULL, "map must be set");
 815   Thread *thread = Thread::current();
 816   methodHandle m (thread, interpreter_frame_method());
 817   jint      bci = interpreter_frame_bci();





















 818 
 819   assert(!Universe::heap()->is_in(m()),
 820           "must be valid oop");
 821   assert(m->is_method(), "checking frame value");
 822   assert((m->is_native() && bci == 0)  ||
 823          (!m->is_native() && bci >= 0 && bci < m->code_size()),
 824          "invalid bci value");
 825 
 826   // Handle the monitor elements in the activation
 827   for (
 828     BasicObjectLock* current = interpreter_frame_monitor_end();
 829     current < interpreter_frame_monitor_begin();
 830     current = next_monitor_in_interpreter_frame(current)
 831   ) {
 832 #ifdef ASSERT
 833     interpreter_frame_verify_monitor(current);
 834 #endif
 835     current->oops_do(f);
 836   }
 837 


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

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









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


1017   assert(byte_offset >= 0, "should not see invalid offset");
1018   oop owner = ((oop*) sp())[byte_offset / wordSize];
1019   assert( Universe::heap()->is_in(owner), "bad receiver" );
1020   return owner;
1021 }
1022 
1023 void frame::oops_entry_do(OopClosure* f, const RegisterMap* map) {
1024   assert(map != NULL, "map must be set");
1025   if (map->include_argument_oops()) {
1026     // must collect argument oops, as nobody else is doing it
1027     Thread *thread = Thread::current();
1028     methodHandle m (thread, entry_frame_call_wrapper()->callee_method());
1029     EntryFrameOopFinder finder(this, m->signature(), m->is_static());
1030     finder.arguments_do(f);
1031   }
1032   // Traverse the Handle Block saved in the entry frame
1033   entry_frame_call_wrapper()->oops_do(f);
1034 }
1035 
1036 
1037 void frame::oops_do_internal(OopClosure* f, CodeBlobClosure* cf, RegisterMap* map, bool use_interpreter_oop_map_cache) {
1038 #ifndef PRODUCT
1039 #if defined(__SUNPRO_CC) && __SUNPRO_CC >= 0x5140
1040 #pragma error_messages(off, SEC_NULL_PTR_DEREF)
1041 #endif
1042   // simulate GC crash here to dump java thread in error report
1043   if (CrashGCForDumpingJavaThread) {
1044     char *t = NULL;
1045     *t = 'c';
1046   }
1047 #endif
1048   if (is_interpreted_frame()) {
1049     oops_interpreted_do(f, map, use_interpreter_oop_map_cache);
1050   } else if (is_entry_frame()) {
1051     oops_entry_do(f, map);
1052   } else if (CodeCache::contains(pc())) {
1053     oops_code_blob_do(f, cf, map);
1054   } else {
1055     ShouldNotReachHere();
1056   }
1057 }
1058 
1059 void frame::nmethods_do(CodeBlobClosure* cf) {
1060   if (_cb != NULL && _cb->is_nmethod()) {
1061     cf->do_code_blob(_cb);
1062   }
1063 }
1064 
1065 
1066 // Call f closure on the interpreted Method*s in the stack.
1067 void frame::metadata_do(MetadataClosure* f) {
1068   ResourceMark rm;
1069   if (is_interpreted_frame()) {
1070     Method* m = this->interpreter_frame_method();
1071     assert(m != NULL, "expecting a method in this frame");
1072     f->do_metadata(m);
1073   }
1074 }
1075 
1076 void frame::verify(const RegisterMap* map) {







1077   // for now make sure receiver type is correct
1078   if (is_interpreted_frame()) {
1079     Method* method = interpreter_frame_method();
1080     guarantee(method->is_method(), "method is wrong in frame::verify");
1081     if (!method->is_static()) {
1082       // fetch the receiver
1083       oop* p = (oop*) interpreter_frame_local_at(0);
1084       // make sure we have the right receiver type
1085     }
1086   }
1087 #if COMPILER2_OR_JVMCI
1088   assert(DerivedPointerTable::is_empty(), "must be empty before verify");
1089 #endif
1090   oops_do_internal(&VerifyOopClosure::verify_oop, NULL, (RegisterMap*)map, false);
1091 }
1092 
1093 
1094 #ifdef ASSERT
1095 bool frame::verify_return_pc(address x) {
1096   if (StubRoutines::returns_to_call_stub(x)) {
1097     return true;
1098   }
1099   if (CodeCache::contains(x)) {
1100     return true;
1101   }
1102   if (Interpreter::contains(x)) {
1103     return true;
1104   }
1105   return false;
1106 }
1107 #endif
1108 
1109 #ifdef ASSERT
1110 void frame::interpreter_frame_verify_monitor(BasicObjectLock* value) const {
1111   assert(is_interpreted_frame(), "Not an interpreted frame");
1112   // verify that the value is in the right part of the frame
1113   address low_mark  = (address) interpreter_frame_monitor_end();
1114   address high_mark = (address) interpreter_frame_monitor_begin();
1115   address current   = (address) value;
1116 
1117   const int monitor_size = frame::interpreter_frame_monitor_size();
1118   guarantee((high_mark - current) % monitor_size  ==  0         , "Misaligned top of BasicObjectLock*");
1119   guarantee( high_mark > current                                , "Current BasicObjectLock* higher than high_mark");
1120 
1121   guarantee((current - low_mark) % monitor_size  ==  0         , "Misaligned bottom of BasicObjectLock*");
1122   guarantee( current >= low_mark                               , "Current BasicObjectLock* below than low_mark");
1123 }
1124 #endif
1125 
1126 #ifndef PRODUCT
1127 void frame::describe(FrameValues& values, int frame_no) {










































1128   // boundaries: sp and the 'real' frame pointer
1129   values.describe(-1, sp(), err_msg("sp for #%d", frame_no), 1);
1130   intptr_t* frame_pointer = real_fp(); // Note: may differ from fp()
1131 
1132   // print frame info at the highest boundary
1133   intptr_t* info_address = MAX2(sp(), frame_pointer);
1134 
1135   if (info_address != frame_pointer) {
1136     // print frame_pointer explicitly if not marked by the frame info
1137     values.describe(-1, frame_pointer, err_msg("frame pointer for #%d", frame_no), 1);
1138   }
1139 
1140   if (is_entry_frame() || is_compiled_frame() || is_interpreted_frame() || is_native_frame()) {
1141     // Label values common to most frames
1142     values.describe(-1, unextended_sp(), err_msg("unextended_sp for #%d", frame_no));
1143   }
1144 
1145   if (is_interpreted_frame()) {
1146     Method* m = interpreter_frame_method();
1147     int bci = interpreter_frame_bci();
1148 
1149     // Label the method and current bci
1150     values.describe(-1, info_address,
1151                     FormatBuffer<1024>("#%d method %s @ %d", frame_no, m->name_and_sig_as_C_string(), bci), 2);
1152     values.describe(-1, info_address,
1153                     err_msg("- %d locals %d max stack", m->max_locals(), m->max_stack()), 1);


1154     if (m->max_locals() > 0) {
1155       intptr_t* l0 = interpreter_frame_local_at(0);
1156       intptr_t* ln = interpreter_frame_local_at(m->max_locals() - 1);
1157       values.describe(-1, MAX2(l0, ln), err_msg("locals for #%d", frame_no), 1);
1158       // Report each local and mark as owned by this frame
1159       for (int l = 0; l < m->max_locals(); l++) {
1160         intptr_t* l0 = interpreter_frame_local_at(l);
1161         values.describe(frame_no, l0, err_msg("local %d", l));
1162       }
1163     }
1164 
1165     // Compute the actual expression stack size
1166     InterpreterOopMap mask;
1167     OopMapCache::compute_one_oop_map(m, bci, &mask);
1168     intptr_t* tos = NULL;
1169     // Report each stack element and mark as owned by this frame
1170     for (int e = 0; e < mask.expression_stack_size(); e++) {
1171       tos = MAX2(tos, interpreter_frame_expression_stack_at(e));
1172       values.describe(frame_no, interpreter_frame_expression_stack_at(e),
1173                       err_msg("stack %d", e));
1174     }
1175     if (tos != NULL) {
1176       values.describe(-1, tos, err_msg("expression stack for #%d", frame_no), 1);
1177     }
1178     if (interpreter_frame_monitor_begin() != interpreter_frame_monitor_end()) {
1179       values.describe(frame_no, (intptr_t*)interpreter_frame_monitor_begin(), "monitors begin");
1180       values.describe(frame_no, (intptr_t*)interpreter_frame_monitor_end(), "monitors end");
1181     }





1182   } else if (is_entry_frame()) {
1183     // For now just label the frame
1184     values.describe(-1, info_address, err_msg("#%d entry frame", frame_no), 2);
1185   } else if (is_compiled_frame()) {
1186     // For now just label the frame
1187     CompiledMethod* cm = (CompiledMethod*)cb();
1188     values.describe(-1, info_address,
1189                     FormatBuffer<1024>("#%d nmethod " INTPTR_FORMAT " for method %s%s%s", frame_no,
1190                                        p2i(cm),
1191                                        (cm->is_aot() ? "A ": "J "),
1192                                        cm->method()->name_and_sig_as_C_string(),
1193                                        (_deopt_state == is_deoptimized) ?
1194                                        " (deoptimized)" :
1195                                        ((_deopt_state == unknown) ? " (state unknown)" : "")),
1196                     2);
















































































1197   } else if (is_native_frame()) {
1198     // For now just label the frame
1199     nmethod* nm = cb()->as_nmethod_or_null();
1200     values.describe(-1, info_address,
1201                     FormatBuffer<1024>("#%d nmethod " INTPTR_FORMAT " for native method %s", frame_no,
1202                                        p2i(nm), nm->method()->name_and_sig_as_C_string()), 2);
1203   } else {
1204     // provide default info if not handled before
1205     char *info = (char *) "special frame";
1206     if ((_cb != NULL) &&
1207         (_cb->name() != NULL)) {
1208       info = (char *)_cb->name();
1209     }
1210     values.describe(-1, info_address, err_msg("#%d <%s>", frame_no, info), 2);
1211   }
1212 
1213   // platform dependent additional data
1214   describe_pd(values, frame_no);
1215 }
1216 
1217 #endif
1218 
1219 
1220 //-----------------------------------------------------------------------------------
1221 // StackFrameStream implementation
1222 
1223 StackFrameStream::StackFrameStream(JavaThread *thread, bool update) : _reg_map(thread, update) {
1224   assert(thread->has_last_Java_frame(), "sanity check");
1225   _fr = thread->last_frame();
1226   _is_done = false;





1227 }
1228 
1229 
1230 #ifndef PRODUCT
1231 
1232 void FrameValues::describe(int owner, intptr_t* location, const char* description, int priority) {
1233   FrameValue fv;
1234   fv.location = location;
1235   fv.owner = owner;
1236   fv.priority = priority;
1237   fv.description = NEW_RESOURCE_ARRAY(char, strlen(description) + 1);
1238   strcpy(fv.description, description);
1239   _values.append(fv);
1240 }
1241 
1242 
1243 #ifdef ASSERT
1244 void FrameValues::validate() {
1245   _values.sort(compare);
1246   bool error = false;




   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 "gc/shared/collectedHeap.inline.hpp"
  33 #include "interpreter/interpreter.hpp"
  34 #include "interpreter/oopMapCache.hpp"
  35 #include "logging/log.hpp"
  36 #include "memory/resourceArea.hpp"
  37 #include "memory/universe.hpp"
  38 #include "oops/markOop.hpp"
  39 #include "oops/method.hpp"
  40 #include "oops/methodData.hpp"
  41 #include "oops/oop.inline.hpp"
  42 #include "oops/verifyOopClosure.hpp"
  43 #include "prims/methodHandles.hpp"
  44 #include "runtime/continuation.hpp"
  45 #include "runtime/frame.inline.hpp"
  46 #include "runtime/handles.inline.hpp"
  47 #include "runtime/javaCalls.hpp"
  48 #include "runtime/monitorChunk.hpp"
  49 #include "runtime/os.hpp"
  50 #include "runtime/sharedRuntime.hpp"
  51 #include "runtime/signature.hpp"
  52 #include "runtime/stackValue.hpp"
  53 #include "runtime/stubCodeGenerator.hpp"
  54 #include "runtime/stubRoutines.hpp"
  55 #include "runtime/thread.inline.hpp"
  56 #include "utilities/debug.hpp"
  57 #include "utilities/decoder.hpp"
  58 #include "utilities/formatBuffer.hpp"
  59 
  60 RegisterMap::RegisterMap(JavaThread *thread, bool update_map, bool walk_cont, bool validate_oops) 
  61   : _cont(Handle()) {
  62   _thread         = thread;
  63   _update_map     = update_map;
  64   _validate_oops  = validate_oops;
  65   _walk_cont      = walk_cont;
  66   DEBUG_ONLY(_skip_missing = false;)
  67   clear();
  68   debug_only(_update_for_id = NULL;)
  69 
  70   _on_hstack = false;
  71   _last_vstack_fp = NULL;
  72   if (walk_cont) {
  73     // we allocate the handle now (rather than in set_cont) because sometimes (StackWalker) the handle must live across HandleMarks
  74     if (thread != NULL && thread->last_continuation() != NULL) {
  75       _cont = Handle(Thread::current(), thread->last_continuation());
  76       *(_cont.raw_value()) = NULL; // TODO UGLY : we just need to allocate a NULL handle
  77     } else {
  78       _cont = Handle();
  79     }
  80   }
  81 
  82 #ifndef PRODUCT
  83   for (int i = 0; i < reg_count ; i++ ) _location[i] = NULL;
  84 #endif /* PRODUCT */
  85 }
  86 
  87 RegisterMap::RegisterMap(const RegisterMap* map) {
  88   assert(map != this, "bad initialization parameter");
  89   assert(map != NULL, "RegisterMap must be present");
  90   _thread                = map->thread();
  91   _update_map            = map->update_map();
  92   _include_argument_oops = map->include_argument_oops();
  93   debug_only(_update_for_id = map->_update_for_id;)
  94   _validate_oops = map->_validate_oops;
  95   _walk_cont     = map->_walk_cont;
  96   DEBUG_ONLY(_skip_missing = map->_skip_missing;)
  97 
  98   _cont = map->_cont;
  99   _on_hstack = map->_on_hstack;
 100   _last_vstack_fp = map->_last_vstack_fp;
 101 
 102   pd_initialize_from(map);
 103   if (update_map()) {
 104     for(int i = 0; i < location_valid_size; i++) {
 105       LocationValidType bits = map->_location_valid[i];
 106       _location_valid[i] = bits;
 107       // for whichever bits are set, pull in the corresponding map->_location
 108       int j = i*location_valid_type_size;
 109       while (bits != 0) {
 110         if ((bits & 1) != 0) {
 111           assert(0 <= j && j < reg_count, "range check");
 112           _location[j] = map->_location[j];
 113         }
 114         bits >>= 1;
 115         j += 1;
 116       }
 117     }
 118   }
 119 }
 120 
 121 void RegisterMap::set_in_cont(bool on_hstack) {
 122    assert (_walk_cont, ""); 
 123    _on_hstack = on_hstack;
 124    if (!on_hstack)
 125     _last_vstack_fp = NULL;
 126 }
 127 
 128 void RegisterMap::set_cont(Handle cont) {
 129   assert (_walk_cont, "");
 130   _cont = cont;
 131 }
 132 
 133 void RegisterMap::set_cont(oop cont) {
 134   assert (_walk_cont, "");
 135   if (cont != NULL) {
 136     assert (_cont.not_null(), "");
 137     *(_cont.raw_value()) = cont; // reuse handle. see comment above in the constructor
 138   } else {
 139     _cont = Handle();
 140   }
 141 }
 142 
 143 void RegisterMap::clear() {
 144   set_include_argument_oops(true);
 145   if (update_map()) {
 146     for(int i = 0; i < location_valid_size; i++) {
 147       _location_valid[i] = 0;
 148     }
 149     pd_clear();
 150   } else {
 151     pd_initialize();
 152   }
 153 }
 154 
 155 #ifndef PRODUCT
 156 
 157 VMReg RegisterMap::find_register_spilled_here(void* p) {
 158   for(int i = 0; i < RegisterMap::reg_count; i++) {
 159     VMReg r = VMRegImpl::as_VMReg(i);
 160     if (p == location(r)) return r;
 161   }
 162   return NULL;
 163 }
 164 
 165 void RegisterMap::print_on(outputStream* st) const {
 166   st->print_cr("Register map");
 167   for(int i = 0; i < reg_count; i++) {
 168 
 169     VMReg r = VMRegImpl::as_VMReg(i);
 170     intptr_t* src = (intptr_t*) location(r);
 171     if (src != NULL) {
 172 
 173       r->print_on(st);
 174       st->print(" [" INTPTR_FORMAT "] = ", p2i(src));
 175       if (((uintptr_t)src & (sizeof(*src)-1)) != 0) {
 176         st->print_cr("<misaligned>");
 177       } else {
 178         st->print_cr(INTPTR_FORMAT, *src);
 179       }
 180     }
 181   }
 182 }
 183 
 184 void RegisterMap::print() const {
 185   print_on(tty);
 186 }
 187 
 188 #endif
 189 // This returns the pc that if you were in the debugger you'd see. Not
 190 // the idealized value in the frame object. This undoes the magic conversion
 191 // that happens for deoptimized frames. In addition it makes the value the
 192 // hardware would want to see in the native frame. The only user (at this point)
 193 // is deoptimization. It likely no one else should ever use it.
 194 
 195 address frame::raw_pc() const {
 196   // if (Continuation::is_continuation_entry_frame(*this)) {
 197   //   return StubRoutines::cont_returnBarrier();
 198   // }
 199   if (is_deoptimized_frame()) {
 200     CompiledMethod* cm = cb()->as_compiled_method_or_null();
 201     if (cm->is_method_handle_return(pc()))
 202       return cm->deopt_mh_handler_begin() - pc_return_offset;
 203     else
 204       return cm->deopt_handler_begin() - pc_return_offset;
 205   } else {
 206     return (pc() - pc_return_offset);
 207   }
 208 }
 209 
 210 // Change the pc in a frame object. This does not change the actual pc in
 211 // actual frame. To do that use patch_pc.
 212 //
 213 void frame::set_pc(address   newpc ) {
 214 #ifdef ASSERT
 215   if (_cb != NULL && _cb->is_nmethod()) {
 216     assert(!((nmethod*)_cb)->is_deopt_pc(_pc), "invariant violation");
 217   }
 218 #endif // ASSERT
 219 
 220   // Unsafe to use the is_deoptimzed tester after changing pc
 221   _deopt_state = unknown;
 222   _pc = newpc;
 223   _cb = CodeCache::find_blob_unsafe(_pc);
 224 
 225 }
 226 
 227 void frame::set_pc_preserve_deopt(address newpc) {
 228   set_pc_preserve_deopt(newpc, CodeCache::find_blob_unsafe(newpc));
 229 }
 230 
 231 void frame::set_pc_preserve_deopt(address newpc, CodeBlob* cb) {
 232 #ifdef ASSERT
 233   if (_cb != NULL && _cb->is_nmethod()) {
 234     assert(!((nmethod*)_cb)->is_deopt_pc(_pc), "invariant violation");
 235   }
 236 #endif // ASSERT
 237 
 238   _pc = newpc;
 239   _cb = cb;
 240 }
 241 
 242 // type testers
 243 bool frame::is_ignored_frame() const {
 244   return false;  // FIXME: some LambdaForm frames should be ignored
 245 }




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











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


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

 369     frame check = thread->last_frame();
 370     if (is_older(check.id())) {
 371       RegisterMap map(thread, false);
 372       while (id() != check.id()) {
 373         check = check.sender(&map);
 374       }
 375       assert(check.is_deoptimized_frame(), "missed deopt");
 376     }

 377   }
 378 #endif // ASSERT
 379 }
 380 
 381 frame frame::java_sender() const {
 382   RegisterMap map(JavaThread::current(), false);
 383   frame s;
 384   for (s = sender(&map); !(s.is_java_frame() || s.is_first_frame()); s = s.sender(&map)) ;
 385   guarantee(s.is_java_frame(), "tried to get caller of first java frame");
 386   return s;
 387 }
 388 
 389 frame frame::real_sender(RegisterMap* map) const {
 390   frame result = sender(map);
 391   while (result.is_runtime_frame() ||
 392          result.is_ignored_frame()) {
 393     result = result.sender(map);
 394   }
 395   return result;
 396 }


 870      _f = NULL; // will be set later
 871      _fr = frame;
 872      _is_static = is_static;
 873      _offset = ArgumentSizeComputer(signature).size() - 1; // last parameter is at index 0
 874    }
 875 
 876   void arguments_do(OopClosure* f) {
 877     _f = f;
 878     if (!_is_static) oop_at_offset_do(_offset+1); // do the receiver
 879     iterate_parameters();
 880   }
 881 
 882 };
 883 
 884 oop* frame::interpreter_callee_receiver_addr(Symbol* signature) {
 885   ArgumentSizeComputer asc(signature);
 886   int size = asc.size();
 887   return (oop *)interpreter_frame_tos_at(size);
 888 }
 889 

 890 void frame::oops_interpreted_do(OopClosure* f, const RegisterMap* map, bool query_oop_map_cache) {


 891   Thread *thread = Thread::current();
 892   methodHandle m (thread, interpreter_frame_method());
 893   jint bci = interpreter_frame_bci();
 894 
 895   InterpreterOopMap mask;
 896   if (query_oop_map_cache) {
 897     m->mask_for(bci, &mask);
 898   } else {
 899     OopMapCache::compute_one_oop_map(m, bci, &mask);
 900   }
 901   
 902   oops_interpreted_do0(f, map, m, bci, mask);
 903 }
 904 
 905 void frame::oops_interpreted_do(OopClosure* f, const RegisterMap* map, const InterpreterOopMap& mask) {
 906   Thread *thread = Thread::current();
 907   methodHandle m (thread, interpreter_frame_method());
 908   jint bci = interpreter_frame_bci();
 909   oops_interpreted_do0(f, map, m, bci, mask);
 910 }
 911 
 912 void frame::oops_interpreted_do0(OopClosure* f, const RegisterMap* map, methodHandle m, jint bci, const InterpreterOopMap& mask) {
 913   assert(is_interpreted_frame(), "Not an interpreted frame");
 914   // assert(map != NULL, "map must be set");
 915 
 916   assert(!Universe::heap()->is_in(m()),
 917           "must be valid oop");
 918   assert(m->is_method(), "checking frame value");
 919   assert((m->is_native() && bci == 0)  ||
 920          (!m->is_native() && bci >= 0 && bci < m->code_size()),
 921          "invalid bci value");
 922 
 923   // Handle the monitor elements in the activation
 924   for (
 925     BasicObjectLock* current = interpreter_frame_monitor_end();
 926     current < interpreter_frame_monitor_begin();
 927     current = next_monitor_in_interpreter_frame(current)
 928   ) {
 929 #ifdef ASSERT
 930     interpreter_frame_verify_monitor(current);
 931 #endif
 932     current->oops_do(f);
 933   }
 934 


 939   // The method pointer in the frame might be the only path to the method's
 940   // klass, and the klass needs to be kept alive while executing. The GCs
 941   // don't trace through method pointers, so the mirror of the method's klass
 942   // is installed as a GC root.
 943   f->do_oop(interpreter_frame_mirror_addr());
 944 
 945   int max_locals = m->is_native() ? m->size_of_parameters() : m->max_locals();
 946 
 947   Symbol* signature = NULL;
 948   bool has_receiver = false;
 949 
 950   // Process a callee's arguments if we are at a call site
 951   // (i.e., if we are at an invoke bytecode)
 952   // This is used sometimes for calling into the VM, not for another
 953   // interpreted or compiled frame.
 954   if (!m->is_native()) {
 955     Bytecode_invoke call = Bytecode_invoke_check(m, bci);
 956     if (call.is_valid()) {
 957       signature = call.signature();
 958       has_receiver = call.has_receiver();
 959       if (map != NULL && map->include_argument_oops() &&
 960           interpreter_frame_expression_stack_size() > 0) {
 961         // ResourceMark rm(thread);  // is this right ???
 962         // we are at a call site & the expression stack is not empty
 963         // => process callee's arguments
 964         //
 965         // Note: The expression stack can be empty if an exception
 966         //       occurred during method resolution/execution. In all
 967         //       cases we empty the expression stack completely be-
 968         //       fore handling the exception (the exception handling
 969         //       code in the interpreter calls a blocking runtime
 970         //       routine which can cause this code to be executed).
 971         //       (was bug gri 7/27/98)
 972         oops_interpreted_arguments_do(signature, has_receiver, f);
 973       }
 974     }
 975   }
 976 
 977   InterpreterFrameClosure blk(this, max_locals, m->max_stack(), f);
 978 
 979   // process locals & expression stack
 980   // mask.print();





 981   mask.iterate_oop(&blk);
 982 }
 983 
 984 
 985 void frame::oops_interpreted_arguments_do(Symbol* signature, bool has_receiver, OopClosure* f) {
 986   InterpretedArgumentOopFinder finder(signature, has_receiver, this, f);
 987   finder.oops_do();
 988 }
 989 
 990 void frame::oops_code_blob_do(OopClosure* f, CodeBlobClosure* cf, DerivedOopClosure* df, const RegisterMap* reg_map) {
 991   assert(_cb != NULL, "sanity check");
 992   assert((oop_map() == NULL) == (_cb->oop_maps() == NULL), "frame and _cb must agree that oopmap is set or not");
 993   if (oop_map() != NULL) {
 994     _oop_map->oops_do(this, reg_map, f, df);
 995 
 996     // Preserve potential arguments for a callee. We handle this by dispatching
 997     // on the codeblob. For c2i, we do
 998     if (reg_map->include_argument_oops()) {
 999       _cb->preserve_callee_argument_oops(*this, reg_map, f);
1000     }
1001   }
1002   // In cases where perm gen is collected, GC will want to mark
1003   // oops referenced from nmethods active on thread stacks so as to
1004   // prevent them from being collected. However, this visit should be
1005   // restricted to certain phases of the collection only. The
1006   // closure decides how it wants nmethods to be traced.
1007   if (cf != NULL)
1008     cf->do_code_blob(_cb);
1009 }
1010 
1011 class CompiledArgumentOopFinder: public SignatureInfo {
1012  protected:
1013   OopClosure*     _f;
1014   int             _offset;        // the current offset, incremented with each argument
1015   bool            _has_receiver;  // true if the callee has a receiver
1016   bool            _has_appendix;  // true if the call has an appendix
1017   frame           _fr;
1018   RegisterMap*    _reg_map;
1019   int             _arg_size;
1020   VMRegPair*      _regs;        // VMReg list of arguments
1021 
1022   void set(int size, BasicType type) {
1023     if (type == T_OBJECT || type == T_ARRAY) handle_oop_offset();
1024     _offset += size;
1025   }
1026 
1027   virtual void handle_oop_offset() {
1028     // Extract low order register number from register array.
1029     // In LP64-land, the high-order bits are valid but unhelpful.
1030     VMReg reg = _regs[_offset].first();
1031     oop *loc = _fr.oopmapreg_to_location(reg, _reg_map);
1032   #ifdef ASSERT
1033     if (loc == NULL) {
1034       if (_reg_map->should_skip_missing())
1035         return;
1036       tty->print_cr("Error walking frame oops:");
1037       _fr.print_on(tty);
1038       assert(loc != NULL, "reg: " INTPTR_FORMAT " %s loc: " INTPTR_FORMAT, reg->value(), reg->name(), p2i(loc));
1039     }
1040   #endif
1041     _f->do_oop(loc);
1042   }
1043 
1044  public:
1045   CompiledArgumentOopFinder(Symbol* signature, bool has_receiver, bool has_appendix, OopClosure* f, frame fr,  const RegisterMap* reg_map)
1046     : SignatureInfo(signature) {
1047 
1048     // initialize CompiledArgumentOopFinder
1049     _f         = f;
1050     _offset    = 0;
1051     _has_receiver = has_receiver;
1052     _has_appendix = has_appendix;
1053     _fr        = fr;
1054     _reg_map   = (RegisterMap*)reg_map;
1055     _arg_size  = ArgumentSizeComputer(signature).size() + (has_receiver ? 1 : 0) + (has_appendix ? 1 : 0);
1056 
1057     int arg_size;
1058     _regs = SharedRuntime::find_callee_arguments(signature, has_receiver, has_appendix, &arg_size);
1059     assert(arg_size == _arg_size, "wrong arg size");
1060   }
1061 
1062   void oops_do() {
1063     if (_has_receiver) {
1064       handle_oop_offset();
1065       _offset++;
1066     }
1067     iterate_parameters();
1068     if (_has_appendix) {
1069       handle_oop_offset();
1070       _offset++;
1071     }
1072   }
1073 };
1074 
1075 void frame::oops_compiled_arguments_do(Symbol* signature, bool has_receiver, bool has_appendix,
1076                                        const RegisterMap* reg_map, OopClosure* f) {
1077   // ResourceMark rm;
1078   CompiledArgumentOopFinder finder(signature, has_receiver, has_appendix, f, *this, reg_map);
1079   finder.oops_do();
1080 }
1081 
1082 
1083 // Get receiver out of callers frame, i.e. find parameter 0 in callers
1084 // frame.  Consult ADLC for where parameter 0 is to be found.  Then
1085 // check local reg_map for it being a callee-save register or argument
1086 // register, both of which are saved in the local frame.  If not found
1087 // there, it must be an in-stack argument of the caller.
1088 // Note: caller.sp() points to callee-arguments
1089 oop frame::retrieve_receiver(RegisterMap* reg_map) {
1090   frame caller = *this;
1091 
1092   // First consult the ADLC on where it puts parameter 0 for this signature.
1093   VMReg reg = SharedRuntime::name_for_receiver();
1094   oop* oop_adr = caller.oopmapreg_to_location(reg, reg_map);
1095   if (oop_adr == NULL) {
1096     guarantee(oop_adr != NULL, "bad register save location");
1097     return NULL;


1119   assert(byte_offset >= 0, "should not see invalid offset");
1120   oop owner = ((oop*) sp())[byte_offset / wordSize];
1121   assert( Universe::heap()->is_in(owner), "bad receiver" );
1122   return owner;
1123 }
1124 
1125 void frame::oops_entry_do(OopClosure* f, const RegisterMap* map) {
1126   assert(map != NULL, "map must be set");
1127   if (map->include_argument_oops()) {
1128     // must collect argument oops, as nobody else is doing it
1129     Thread *thread = Thread::current();
1130     methodHandle m (thread, entry_frame_call_wrapper()->callee_method());
1131     EntryFrameOopFinder finder(this, m->signature(), m->is_static());
1132     finder.arguments_do(f);
1133   }
1134   // Traverse the Handle Block saved in the entry frame
1135   entry_frame_call_wrapper()->oops_do(f);
1136 }
1137 
1138 
1139 void frame::oops_do_internal(OopClosure* f, CodeBlobClosure* cf, DerivedOopClosure* df, const RegisterMap* map, bool use_interpreter_oop_map_cache) {
1140 #ifndef PRODUCT
1141 #if defined(__SUNPRO_CC) && __SUNPRO_CC >= 0x5140
1142 #pragma error_messages(off, SEC_NULL_PTR_DEREF)
1143 #endif
1144   // simulate GC crash here to dump java thread in error report
1145   if (CrashGCForDumpingJavaThread) {
1146     char *t = NULL;
1147     *t = 'c';
1148   }
1149 #endif
1150   if (is_interpreted_frame()) {
1151     oops_interpreted_do(f, map, use_interpreter_oop_map_cache);
1152   } else if (is_entry_frame()) {
1153     oops_entry_do(f, map);
1154   } else if (CodeCache::contains(pc())) {
1155     oops_code_blob_do(f, cf, df, map);
1156   } else {
1157     ShouldNotReachHere();
1158   }
1159 }
1160 
1161 void frame::nmethods_do(CodeBlobClosure* cf) {
1162   if (_cb != NULL && _cb->is_nmethod()) {
1163     cf->do_code_blob(_cb);
1164   }
1165 }
1166 
1167 
1168 // Call f closure on the interpreted Method*s in the stack.
1169 void frame::metadata_do(MetadataClosure* f) {
1170   ResourceMark rm;
1171   if (is_interpreted_frame()) {
1172     Method* m = this->interpreter_frame_method();
1173     assert(m != NULL, "expecting a method in this frame");
1174     f->do_metadata(m);
1175   }
1176 }
1177 
1178 void frame::verify(const RegisterMap* map) {
1179 #ifndef PRODUCT
1180   if (TraceCodeBlobStacks) {
1181     tty->print_cr("*** verify");
1182     print_on(tty);
1183   }
1184 #endif
1185 
1186   // for now make sure receiver type is correct
1187   if (is_interpreted_frame()) {
1188     Method* method = interpreter_frame_method();
1189     guarantee(method->is_method(), "method is wrong in frame::verify");
1190     if (!method->is_static()) {
1191       // fetch the receiver
1192       oop* p = (oop*) interpreter_frame_local_at(0);
1193       // make sure we have the right receiver type
1194     }
1195   }
1196 #if COMPILER2_OR_JVMCI
1197   assert(DerivedPointerTable::is_empty(), "must be empty before verify");
1198 #endif
1199   oops_do_internal(&VerifyOopClosure::verify_oop, NULL, NULL, (RegisterMap*)map, false);
1200 }
1201 
1202 
1203 #ifdef ASSERT
1204 bool frame::verify_return_pc(address x) {
1205   if (StubRoutines::returns_to_call_stub(x)) {
1206     return true;
1207   }
1208   if (CodeCache::contains(x)) {
1209     return true;
1210   }
1211   if (Interpreter::contains(x)) {
1212     return true;
1213   }
1214   return false;
1215 }
1216 #endif
1217 
1218 #ifdef ASSERT
1219 void frame::interpreter_frame_verify_monitor(BasicObjectLock* value) const {
1220   assert(is_interpreted_frame(), "Not an interpreted frame");
1221   // verify that the value is in the right part of the frame
1222   address low_mark  = (address) interpreter_frame_monitor_end();
1223   address high_mark = (address) interpreter_frame_monitor_begin();
1224   address current   = (address) value;
1225 
1226   const int monitor_size = frame::interpreter_frame_monitor_size();
1227   guarantee((high_mark - current) % monitor_size  ==  0         , "Misaligned top of BasicObjectLock*");
1228   guarantee( high_mark > current                                , "Current BasicObjectLock* higher than high_mark");
1229 
1230   guarantee((current - low_mark) % monitor_size  ==  0         , "Misaligned bottom of BasicObjectLock*");
1231   guarantee( current >= low_mark                               , "Current BasicObjectLock* below than low_mark");
1232 }
1233 #endif
1234 
1235 #ifndef PRODUCT
1236 
1237 class FrameValuesOopClosure: public OopClosure, public DerivedOopClosure {
1238 private:
1239   FrameValues& _values;
1240   int _frame_no;
1241 public:
1242   FrameValuesOopClosure(FrameValues& values, int frame_no) : _values(values), _frame_no(frame_no) {}
1243   virtual void do_oop(oop* p)       { _values.describe(_frame_no, (intptr_t*)p, err_msg("oop for #%d", _frame_no)); }
1244   virtual void do_oop(narrowOop* p) { _values.describe(_frame_no, (intptr_t*)p, err_msg("narrow oop for #%d", _frame_no)); }
1245   virtual void do_derived_oop(oop *base, oop *derived) { 
1246     _values.describe(_frame_no, (intptr_t*)derived, err_msg("derived pointer (base: " INTPTR_FORMAT ") for #%d", p2i(base), _frame_no));
1247   }
1248 };
1249 
1250 class FrameValuesOopMapClosure: public OopMapClosure {
1251 private:
1252   const frame* _fr;
1253   const RegisterMap* _reg_map;
1254   FrameValues& _values;
1255   int _frame_no;
1256 public:
1257   FrameValuesOopMapClosure(const frame* fr, const RegisterMap* reg_map, FrameValues& values, int frame_no)
1258    : _fr(fr), _reg_map(reg_map), _values(values), _frame_no(frame_no) {}
1259 
1260   virtual void do_value(VMReg reg, OopMapValue::oop_types type) {
1261     intptr_t* p = (intptr_t*)_fr->oopmapreg_to_location(reg, _reg_map);
1262     if (p != NULL && (((intptr_t)p & WordAlignmentMask) == 0)) {
1263       const char* type_name = NULL;
1264       switch(type) {
1265         case OopMapValue::oop_value:          type_name = "oop";          break;
1266         case OopMapValue::narrowoop_value:    type_name = "narrow oop";   break;
1267         case OopMapValue::callee_saved_value: type_name = "callee-saved"; break;
1268         case OopMapValue::derived_oop_value:  type_name = "derived";      break;
1269         // case OopMapValue::live_value:         type_name = "live";         break;
1270         default: break;
1271       }
1272       if (type_name != NULL)
1273         _values.describe(_frame_no, p, err_msg("%s for #%d", type_name, _frame_no));
1274     }
1275   }
1276 };
1277 
1278 void frame::describe(FrameValues& values, int frame_no, const RegisterMap* reg_map) {
1279   // boundaries: sp and the 'real' frame pointer
1280   values.describe(-1, sp(), err_msg("sp for #%d", frame_no), 0);
1281   intptr_t* frame_pointer = real_fp(); // Note: may differ from fp()
1282 
1283   // print frame info at the highest boundary
1284   intptr_t* info_address = MAX2(sp(), frame_pointer);
1285 
1286   if (info_address != frame_pointer) {
1287     // print frame_pointer explicitly if not marked by the frame info
1288     values.describe(-1, frame_pointer, err_msg("frame pointer for #%d", frame_no), 1);
1289   }
1290 
1291   if (is_entry_frame() || is_compiled_frame() || is_interpreted_frame() || is_native_frame()) {
1292     // Label values common to most frames
1293     values.describe(-1, unextended_sp(), err_msg("unextended_sp for #%d", frame_no), 0);
1294   }
1295 
1296   if (is_interpreted_frame()) {
1297     Method* m = interpreter_frame_method();
1298     int bci = interpreter_frame_bci();
1299 
1300     // Label the method and current bci
1301     values.describe(-1, info_address,
1302                     FormatBuffer<1024>("#%d method %s @ %d", frame_no, m->name_and_sig_as_C_string(), bci), 3);
1303     values.describe(-1, info_address,
1304                     err_msg("- %d locals %d max stack", m->max_locals(), m->max_stack()), 2);
1305     values.describe(frame_no, (intptr_t*)sender_pc_addr(), "return address");
1306 
1307     if (m->max_locals() > 0) {
1308       intptr_t* l0 = interpreter_frame_local_at(0);
1309       intptr_t* ln = interpreter_frame_local_at(m->max_locals() - 1);
1310       values.describe(-1, MAX2(l0, ln), err_msg("locals for #%d", frame_no), 2);
1311       // Report each local and mark as owned by this frame
1312       for (int l = 0; l < m->max_locals(); l++) {
1313         intptr_t* l0 = interpreter_frame_local_at(l);
1314         values.describe(frame_no, l0, err_msg("local %d", l), 1);
1315       }
1316     }
1317 
1318     // Compute the actual expression stack size
1319     InterpreterOopMap mask;
1320     OopMapCache::compute_one_oop_map(m, bci, &mask);
1321     intptr_t* tos = NULL;
1322     // Report each stack element and mark as owned by this frame
1323     for (int e = 0; e < mask.expression_stack_size(); e++) {
1324       tos = MAX2(tos, interpreter_frame_expression_stack_at(e));
1325       values.describe(frame_no, interpreter_frame_expression_stack_at(e),
1326                       err_msg("stack %d", e), 1);
1327     }
1328     if (tos != NULL) {
1329       values.describe(-1, tos, err_msg("expression stack for #%d", frame_no), 2);
1330     }
1331     if (interpreter_frame_monitor_begin() != interpreter_frame_monitor_end()) {
1332       values.describe(frame_no, (intptr_t*)interpreter_frame_monitor_begin(), "monitors begin");
1333       values.describe(frame_no, (intptr_t*)interpreter_frame_monitor_end(), "monitors end");
1334     }
1335 
1336     if (reg_map != NULL) {
1337       FrameValuesOopClosure oopsFn(values, frame_no);
1338       oops_do(&oopsFn, NULL, &oopsFn, reg_map);
1339     }
1340   } else if (is_entry_frame()) {
1341     // For now just label the frame
1342     values.describe(-1, info_address, err_msg("#%d entry frame", frame_no), 2);
1343   } else if (is_compiled_frame()) {
1344     // For now just label the frame
1345     CompiledMethod* cm = cb()->as_compiled_method();
1346     values.describe(-1, info_address,
1347                     FormatBuffer<1024>("#%d nmethod " INTPTR_FORMAT " for method %s%s%s", frame_no,
1348                                        p2i(cm),
1349                                        (cm->is_aot() ? "A ": "J "),
1350                                        cm->method()->name_and_sig_as_C_string(),
1351                                        (_deopt_state == is_deoptimized) ?
1352                                        " (deoptimized)" :
1353                                        ((_deopt_state == unknown) ? " (state unknown)" : "")),
1354                     3);
1355 
1356     { // mark arguments (see nmethod::print_nmethod_labels)
1357       Method* m = cm->method();
1358 
1359       int stack_slot_offset = cm->frame_size() * wordSize; // offset, in bytes, to caller sp
1360       int sizeargs = m->size_of_parameters();
1361 
1362       BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sizeargs);
1363       VMRegPair* regs   = NEW_RESOURCE_ARRAY(VMRegPair, sizeargs);
1364       {
1365         int sig_index = 0;
1366         if (!m->is_static()) sig_bt[sig_index++] = T_OBJECT; // 'this'
1367         for (SignatureStream ss(m->signature()); !ss.at_return_type(); ss.next()) {
1368           BasicType t = ss.type();
1369           assert(type2size[t] == 1 || type2size[t] == 2, "size is 1 or 2");
1370           sig_bt[sig_index++] = t;
1371           if (type2size[t] == 2) sig_bt[sig_index++] = T_VOID;
1372         }
1373         assert(sig_index == sizeargs, "");
1374       }
1375       int out_preserve = SharedRuntime::java_calling_convention(sig_bt, regs, sizeargs, false);
1376       assert (out_preserve ==  m->num_stack_arg_slots(), "");
1377       int sig_index = 0;
1378       int arg_index = (m->is_static() ? 0 : -1);
1379       for (SignatureStream ss(m->signature()); !ss.at_return_type(); ) {
1380         bool at_this = (arg_index == -1);
1381         bool at_old_sp = false;
1382         BasicType t = (at_this ? T_OBJECT : ss.type());
1383         assert(t == sig_bt[sig_index], "sigs in sync");
1384         VMReg fst = regs[sig_index].first();
1385         if (fst->is_stack()) {
1386           int offset = fst->reg2stack() * VMRegImpl::stack_slot_size + stack_slot_offset;
1387           intptr_t* stack_address = (intptr_t*)((address)sp() + offset);
1388           if (at_this)
1389             values.describe(frame_no, stack_address, err_msg("this for #%d", frame_no), 1);
1390           else
1391             values.describe(frame_no, stack_address, err_msg("param %d %s for #%d", arg_index, type2name(t), frame_no), 1);
1392         }
1393         sig_index += type2size[t];
1394         arg_index += 1;
1395         if (!at_this) ss.next();
1396       }
1397     }
1398 
1399     if (reg_map != NULL) {
1400       int scope_no = 0;
1401       for (ScopeDesc* scope = cm->scope_desc_at(pc()); scope != NULL; scope = scope->sender(), scope_no++) {
1402         Method* m = scope->method();
1403         int  bci = scope->bci();
1404         values.describe(-1, info_address, err_msg("- #%d scope %s @ %d", scope_no, m->name_and_sig_as_C_string(), bci), 2);
1405 
1406         { // mark locals
1407           GrowableArray<ScopeValue*>* scvs = scope->locals();
1408           int scvs_length = scvs != NULL ? scvs->length() : 0;
1409           for (int i = 0; i < scvs_length; i++) {
1410             intptr_t* stack_address = (intptr_t*)StackValue::stack_value_address(this, reg_map, scvs->at(i));
1411             if (stack_address != NULL)
1412               values.describe(frame_no, stack_address, err_msg("local %d for #%d (scope %d)", i, frame_no, scope_no), 1);
1413           }
1414         }
1415         { // mark expression stack
1416           GrowableArray<ScopeValue*>* scvs = scope->expressions();
1417           int scvs_length = scvs != NULL ? scvs->length() : 0;
1418           for (int i = 0; i < scvs_length; i++) {
1419             intptr_t* stack_address = (intptr_t*)StackValue::stack_value_address(this, reg_map, scvs->at(i));
1420             if (stack_address != NULL)
1421               values.describe(frame_no, stack_address, err_msg("stack %d for #%d (scope %d)", i, frame_no, scope_no), 1);
1422           }
1423         }
1424       }
1425 
1426       FrameValuesOopClosure oopsFn(values, frame_no);
1427       oops_do(&oopsFn, NULL, &oopsFn, reg_map);
1428 
1429       if (oop_map() != NULL) {
1430         FrameValuesOopMapClosure valuesFn(this, reg_map, values, frame_no);
1431         int mask = OopMapValue::callee_saved_value; // | OopMapValue::live_value;
1432         oop_map()->all_do(this, mask, &valuesFn);
1433       }
1434     }
1435   } else if (is_native_frame()) {
1436     // For now just label the frame
1437     nmethod* nm = cb()->as_nmethod_or_null();
1438     values.describe(-1, info_address,
1439                     FormatBuffer<1024>("#%d nmethod " INTPTR_FORMAT " for native method %s", frame_no,
1440                                        p2i(nm), nm->method()->name_and_sig_as_C_string()), 2);
1441   } else {
1442     // provide default info if not handled before
1443     char *info = (char *) "special frame";
1444     if ((_cb != NULL) &&
1445         (_cb->name() != NULL)) {
1446       info = (char *)_cb->name();
1447     }
1448     values.describe(-1, info_address, err_msg("#%d <%s>", frame_no, info), 2);
1449   }
1450 
1451   // platform dependent additional data
1452   describe_pd(values, frame_no);
1453 }
1454 
1455 #endif
1456 
1457 
1458 //-----------------------------------------------------------------------------------
1459 // StackFrameStream implementation
1460 
1461 StackFrameStream::StackFrameStream(JavaThread *thread, bool update, bool allow_missing_reg) : _reg_map(thread, update) {
1462   assert(thread->has_last_Java_frame(), "sanity check");
1463   _fr = thread->last_frame();
1464   _is_done = false;
1465 #ifndef PRODUCT
1466   if (allow_missing_reg) {
1467     _reg_map.set_skip_missing(true);
1468   }
1469 #endif
1470 }
1471 
1472 
1473 #ifndef PRODUCT
1474 
1475 void FrameValues::describe(int owner, intptr_t* location, const char* description, int priority) {
1476   FrameValue fv;
1477   fv.location = location;
1478   fv.owner = owner;
1479   fv.priority = priority;
1480   fv.description = NEW_RESOURCE_ARRAY(char, strlen(description) + 1);
1481   strcpy(fv.description, description);
1482   _values.append(fv);
1483 }
1484 
1485 
1486 #ifdef ASSERT
1487 void FrameValues::validate() {
1488   _values.sort(compare);
1489   bool error = false;


< prev index next >