1 /*
   2  * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2007, 2008, 2009, 2010, 2011 Red Hat, Inc.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "code/scopeDesc.hpp"
  28 #include "interpreter/interpreter.hpp"
  29 #include "interpreter/interpreterRuntime.hpp"
  30 #include "memory/resourceArea.hpp"
  31 #include "oops/markOop.hpp"
  32 #include "oops/method.hpp"
  33 #include "oops/oop.inline.hpp"
  34 #include "runtime/frame.inline.hpp"
  35 #include "runtime/handles.inline.hpp"
  36 #include "runtime/javaCalls.hpp"
  37 #include "runtime/monitorChunk.hpp"
  38 #include "runtime/signature.hpp"
  39 #include "runtime/stubCodeGenerator.hpp"
  40 #include "runtime/stubRoutines.hpp"
  41 #include "vmreg_zero.inline.hpp"
  42 #ifdef COMPILER1
  43 #include "c1/c1_Runtime1.hpp"
  44 #include "runtime/vframeArray.hpp"
  45 #endif
  46 
  47 #ifdef ASSERT
  48 void RegisterMap::check_location_valid() {
  49   ShouldNotCallThis();
  50 }
  51 #endif
  52 
  53 bool frame::is_interpreted_frame() const {
  54   return zeroframe()->is_interpreter_frame();
  55 }
  56 
  57 bool frame::is_fake_stub_frame() const {
  58   return zeroframe()->is_fake_stub_frame();
  59 }
  60 
  61 frame frame::sender_for_entry_frame(RegisterMap *map) const {
  62   assert(zeroframe()->is_entry_frame(), "wrong type of frame");
  63   assert(map != NULL, "map must be set");
  64   assert(!entry_frame_is_first(), "next Java fp must be non zero");
  65   assert(entry_frame_call_wrapper()->anchor()->last_Java_sp() == sender_sp(),
  66          "sender should be next Java frame");
  67   map->clear();
  68   assert(map->include_argument_oops(), "should be set by clear");
  69   return frame(zeroframe()->next(), sender_sp());
  70 }
  71 
  72 frame frame::sender_for_nonentry_frame(RegisterMap *map) const {
  73   assert(zeroframe()->is_interpreter_frame() ||
  74          zeroframe()->is_shark_frame() ||
  75          zeroframe()->is_fake_stub_frame(), "wrong type of frame");
  76   return frame(zeroframe()->next(), sender_sp());
  77 }
  78 
  79 frame frame::sender(RegisterMap* map) const {
  80   // Default is not to follow arguments; the various
  81   // sender_for_xxx methods update this accordingly.
  82   map->set_include_argument_oops(false);
  83 
  84   if (is_entry_frame())
  85     return sender_for_entry_frame(map);
  86   else
  87     return sender_for_nonentry_frame(map);
  88 }
  89 
  90 #ifdef CC_INTERP
  91 BasicObjectLock* frame::interpreter_frame_monitor_begin() const {
  92   return get_interpreterState()->monitor_base();
  93 }
  94 
  95 BasicObjectLock* frame::interpreter_frame_monitor_end() const {
  96   return (BasicObjectLock*) get_interpreterState()->stack_base();
  97 }
  98 #endif // CC_INTERP
  99 
 100 void frame::patch_pc(Thread* thread, address pc) {
 101 
 102   if (pc != NULL) {
 103     _cb = CodeCache::find_blob(pc);
 104     SharkFrame* sharkframe = zeroframe()->as_shark_frame();
 105     sharkframe->set_pc(pc);
 106     _pc = pc;
 107     _deopt_state = is_deoptimized;
 108 
 109   } else {
 110     // We borrow this call to set the thread pointer in the interpreter
 111     // state; the hook to set up deoptimized frames isn't supplied it.
 112     assert(pc == NULL, "should be");
 113     get_interpreterState()->set_thread((JavaThread *) thread);
 114   }
 115 }
 116 
 117 bool frame::safe_for_sender(JavaThread *thread) {
 118   ShouldNotCallThis();
 119   return false;
 120 }
 121 
 122 bool frame::is_interpreted_frame_valid(JavaThread *thread) const {
 123   ShouldNotCallThis();
 124   return false;
 125 }
 126 
 127 BasicType frame::interpreter_frame_result(oop* oop_result,
 128                                           jvalue* value_result) {
 129   assert(is_interpreted_frame(), "interpreted frame expected");
 130   Method* method = interpreter_frame_method();
 131   BasicType type = method->result_type();
 132   intptr_t* tos_addr = (intptr_t *) interpreter_frame_tos_address();
 133   oop obj;
 134 
 135   switch (type) {
 136   case T_VOID:
 137     break;
 138   case T_BOOLEAN:
 139     value_result->z = *(jboolean *) tos_addr;
 140     break;
 141   case T_BYTE:
 142     value_result->b = *(jbyte *) tos_addr;
 143     break;
 144   case T_CHAR:
 145     value_result->c = *(jchar *) tos_addr;
 146     break;
 147   case T_SHORT:
 148     value_result->s = *(jshort *) tos_addr;
 149     break;
 150   case T_INT:
 151     value_result->i = *(jint *) tos_addr;
 152     break;
 153   case T_LONG:
 154     value_result->j = *(jlong *) tos_addr;
 155     break;
 156   case T_FLOAT:
 157     value_result->f = *(jfloat *) tos_addr;
 158     break;
 159   case T_DOUBLE:
 160     value_result->d = *(jdouble *) tos_addr;
 161     break;
 162 
 163   case T_OBJECT:
 164   case T_ARRAY:
 165     if (method->is_native()) {
 166       obj = get_interpreterState()->oop_temp();
 167     }
 168     else {
 169       oop* obj_p = (oop *) tos_addr;
 170       obj = (obj_p == NULL) ? (oop) NULL : *obj_p;
 171     }
 172     assert(obj == NULL || Universe::heap()->is_in(obj), "sanity check");
 173     *oop_result = obj;
 174     break;
 175 
 176   default:
 177     ShouldNotReachHere();
 178   }
 179 
 180   return type;
 181 }
 182 
 183 int frame::frame_size(RegisterMap* map) const {
 184 #ifdef PRODUCT
 185   ShouldNotCallThis();
 186 #endif // PRODUCT
 187   return 0; // make javaVFrame::print_value work
 188 }
 189 
 190 intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
 191   int index = (Interpreter::expr_offset_in_bytes(offset) / wordSize);
 192   return &interpreter_frame_tos_address()[index];
 193 }
 194 
 195 void frame::zero_print_on_error(int           frame_index,
 196                                 outputStream* st,
 197                                 char*         buf,
 198                                 int           buflen) const {
 199   // Divide the buffer between the field and the value
 200   buflen >>= 1;
 201   char *fieldbuf = buf;
 202   char *valuebuf = buf + buflen;
 203 
 204   // Print each word of the frame
 205   for (intptr_t *addr = sp(); addr <= fp(); addr++) {
 206     int offset = fp() - addr;
 207 
 208     // Fill in default values, then try and improve them
 209     snprintf(fieldbuf, buflen, "word[%d]", offset);
 210     snprintf(valuebuf, buflen, PTR_FORMAT, *addr);
 211     zeroframe()->identify_word(frame_index, offset, fieldbuf, valuebuf, buflen);
 212     fieldbuf[buflen - 1] = '\0';
 213     valuebuf[buflen - 1] = '\0';
 214 
 215     // Print the result
 216     st->print_cr(" " PTR_FORMAT ": %-21s = %s", p2i(addr), fieldbuf, valuebuf);
 217   }
 218 }
 219 
 220 void ZeroFrame::identify_word(int   frame_index,
 221                               int   offset,
 222                               char* fieldbuf,
 223                               char* valuebuf,
 224                               int   buflen) const {
 225   switch (offset) {
 226   case next_frame_off:
 227     strncpy(fieldbuf, "next_frame", buflen);
 228     break;
 229 
 230   case frame_type_off:
 231     strncpy(fieldbuf, "frame_type", buflen);
 232     if (is_entry_frame())
 233       strncpy(valuebuf, "ENTRY_FRAME", buflen);
 234     else if (is_interpreter_frame())
 235       strncpy(valuebuf, "INTERPRETER_FRAME", buflen);
 236     else if (is_shark_frame())
 237       strncpy(valuebuf, "SHARK_FRAME", buflen);
 238     else if (is_fake_stub_frame())
 239       strncpy(valuebuf, "FAKE_STUB_FRAME", buflen);
 240     break;
 241 
 242   default:
 243     if (is_entry_frame()) {
 244       as_entry_frame()->identify_word(
 245         frame_index, offset, fieldbuf, valuebuf, buflen);
 246     }
 247     else if (is_interpreter_frame()) {
 248       as_interpreter_frame()->identify_word(
 249         frame_index, offset, fieldbuf, valuebuf, buflen);
 250     }
 251     else if (is_shark_frame()) {
 252       as_shark_frame()->identify_word(
 253         frame_index, offset, fieldbuf, valuebuf, buflen);
 254     }
 255     else if (is_fake_stub_frame()) {
 256       as_fake_stub_frame()->identify_word(
 257         frame_index, offset, fieldbuf, valuebuf, buflen);
 258     }
 259   }
 260 }
 261 
 262 void EntryFrame::identify_word(int   frame_index,
 263                                int   offset,
 264                                char* fieldbuf,
 265                                char* valuebuf,
 266                                int   buflen) const {
 267   switch (offset) {
 268   case call_wrapper_off:
 269     strncpy(fieldbuf, "call_wrapper", buflen);
 270     break;
 271 
 272   default:
 273     snprintf(fieldbuf, buflen, "local[%d]", offset - 3);
 274   }
 275 }
 276 
 277 void InterpreterFrame::identify_word(int   frame_index,
 278                                      int   offset,
 279                                      char* fieldbuf,
 280                                      char* valuebuf,
 281                                      int   buflen) const {
 282   interpreterState istate = interpreter_state();
 283   bool is_valid = istate->self_link() == istate;
 284   intptr_t *addr = addr_of_word(offset);
 285 
 286   // Fixed part
 287   if (addr >= (intptr_t *) istate) {
 288     const char *field = istate->name_of_field_at_address((address) addr);
 289     if (field) {
 290       if (is_valid && !strcmp(field, "_method")) {
 291         istate->method()->name_and_sig_as_C_string(valuebuf, buflen);
 292       }
 293       else if (is_valid && !strcmp(field, "_bcp") && istate->bcp()) {
 294         snprintf(valuebuf, buflen, PTR_FORMAT " (bci %d)",
 295                  (intptr_t) istate->bcp(),
 296                  istate->method()->bci_from(istate->bcp()));
 297       }
 298       snprintf(fieldbuf, buflen, "%sistate->%s",
 299                field[strlen(field) - 1] == ')' ? "(": "", field);
 300     }
 301     else if (addr == (intptr_t *) istate) {
 302       strncpy(fieldbuf, "(vtable for istate)", buflen);
 303     }
 304     return;
 305   }
 306 
 307   // Variable part
 308   if (!is_valid)
 309     return;
 310 
 311   // JNI stuff
 312   if (istate->method()->is_native() && addr < istate->stack_base()) {
 313     address hA = istate->method()->signature_handler();
 314     if (hA != NULL) {
 315       if (hA != (address) InterpreterRuntime::slow_signature_handler) {
 316         InterpreterRuntime::SignatureHandler *handler =
 317           InterpreterRuntime::SignatureHandler::from_handlerAddr(hA);
 318 
 319         intptr_t *params = istate->stack_base() - handler->argument_count();
 320         if (addr >= params) {
 321           int param = addr - params;
 322           const char *desc = "";
 323           if (param == 0)
 324             desc = " (JNIEnv)";
 325           else if (param == 1) {
 326             if (istate->method()->is_static())
 327               desc = " (mirror)";
 328             else
 329               desc = " (this)";
 330           }
 331           snprintf(fieldbuf, buflen, "parameter[%d]%s", param, desc);
 332           return;
 333         }
 334 
 335         for (int i = 0; i < handler->argument_count(); i++) {
 336           if (params[i] == (intptr_t) addr) {
 337             snprintf(fieldbuf, buflen, "unboxed parameter[%d]", i);
 338             return;
 339           }
 340         }
 341       }
 342     }
 343     return;
 344   }
 345 
 346   // Monitors and stack
 347   identify_vp_word(frame_index, addr,
 348                    (intptr_t *) istate->monitor_base(),
 349                    istate->stack_base(),
 350                    fieldbuf, buflen);
 351 }
 352 
 353 void SharkFrame::identify_word(int   frame_index,
 354                                int   offset,
 355                                char* fieldbuf,
 356                                char* valuebuf,
 357                                int   buflen) const {
 358   // Fixed part
 359   switch (offset) {
 360   case pc_off:
 361     strncpy(fieldbuf, "pc", buflen);
 362     if (method()->is_method()) {
 363       CompiledMethod *code = method()->code();
 364       if (code && code->pc_desc_at(pc())) {
 365         SimpleScopeDesc ssd(code, pc());
 366         snprintf(valuebuf, buflen, PTR_FORMAT " (bci %d)",
 367                  (intptr_t) pc(), ssd.bci());
 368       }
 369     }
 370     return;
 371 
 372   case unextended_sp_off:
 373     strncpy(fieldbuf, "unextended_sp", buflen);
 374     return;
 375 
 376   case method_off:
 377     strncpy(fieldbuf, "method", buflen);
 378     if (method()->is_method()) {
 379       method()->name_and_sig_as_C_string(valuebuf, buflen);
 380     }
 381     return;
 382 
 383   case oop_tmp_off:
 384     strncpy(fieldbuf, "oop_tmp", buflen);
 385     return;
 386   }
 387 
 388   // Variable part
 389   if (method()->is_method()) {
 390     identify_vp_word(frame_index, addr_of_word(offset),
 391                      addr_of_word(header_words + 1),
 392                      unextended_sp() + method()->max_stack(),
 393                      fieldbuf, buflen);
 394   }
 395 }
 396 
 397 void ZeroFrame::identify_vp_word(int       frame_index,
 398                                  intptr_t* addr,
 399                                  intptr_t* monitor_base,
 400                                  intptr_t* stack_base,
 401                                  char*     fieldbuf,
 402                                  int       buflen) const {
 403   // Monitors
 404   if (addr >= stack_base && addr < monitor_base) {
 405     int monitor_size = frame::interpreter_frame_monitor_size();
 406     int last_index = (monitor_base - stack_base) / monitor_size - 1;
 407     int index = last_index - (addr - stack_base) / monitor_size;
 408     intptr_t monitor = (intptr_t) (
 409       (BasicObjectLock *) monitor_base - 1 - index);
 410     intptr_t offset = (intptr_t) addr - monitor;
 411 
 412     if (offset == BasicObjectLock::obj_offset_in_bytes())
 413       snprintf(fieldbuf, buflen, "monitor[%d]->_obj", index);
 414     else if (offset ==  BasicObjectLock::lock_offset_in_bytes())
 415       snprintf(fieldbuf, buflen, "monitor[%d]->_lock", index);
 416 
 417     return;
 418   }
 419 
 420   // Expression stack
 421   if (addr < stack_base) {
 422     snprintf(fieldbuf, buflen, "%s[%d]",
 423              frame_index == 0 ? "stack_word" : "local",
 424              (int) (stack_base - addr - 1));
 425     return;
 426   }
 427 }
 428 
 429 #ifndef PRODUCT
 430 
 431 void frame::describe_pd(FrameValues& values, int frame_no) {
 432 
 433 }
 434 
 435 #endif
 436 
 437 intptr_t *frame::initial_deoptimization_info() {
 438   // unused... but returns fp() to minimize changes introduced by 7087445
 439   return fp();
 440 }
 441 
 442 #ifndef PRODUCT
 443 // This is a generic constructor which is only used by pns() in debug.cpp.
 444 frame::frame(void* sp, void* fp, void* pc) {
 445   Unimplemented();
 446 }
 447 
 448 void frame::pd_ps() {}
 449 #endif