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 "memory/universe.hpp"
  32 #include "oops/markWord.hpp"
  33 #include "oops/method.hpp"
  34 #include "oops/oop.inline.hpp"
  35 #include "runtime/frame.inline.hpp"
  36 #include "runtime/handles.inline.hpp"
  37 #include "runtime/javaCalls.hpp"
  38 #include "runtime/monitorChunk.hpp"
  39 #include "runtime/signature.hpp"
  40 #include "runtime/stubCodeGenerator.hpp"
  41 #include "runtime/stubRoutines.hpp"
  42 #include "vmreg_zero.inline.hpp"
  43 #ifdef COMPILER1
  44 #include "c1/c1_Runtime1.hpp"
  45 #include "runtime/vframeArray.hpp"
  46 #endif
  47 
  48 #ifdef ASSERT
  49 void RegisterMap::check_location_valid() {
  50   ShouldNotCallThis();
  51 }
  52 #endif
  53 
  54 bool frame::is_interpreted_frame() const {
  55   return zeroframe()->is_interpreter_frame();
  56 }
  57 
  58 bool frame::is_fake_stub_frame() const {
  59   return zeroframe()->is_fake_stub_frame();
  60 }
  61 
  62 frame frame::sender_for_entry_frame(RegisterMap *map) const {
  63   assert(zeroframe()->is_entry_frame(), "wrong type of frame");
  64   assert(map != NULL, "map must be set");
  65   assert(!entry_frame_is_first(), "next Java fp must be non zero");
  66   assert(entry_frame_call_wrapper()->anchor()->last_Java_sp() == sender_sp(),
  67          "sender should be next Java frame");
  68   map->clear();
  69   assert(map->include_argument_oops(), "should be set by clear");
  70   return frame(zeroframe()->next(), sender_sp());
  71 }
  72 
  73 frame frame::sender_for_nonentry_frame(RegisterMap *map) const {
  74   assert(zeroframe()->is_interpreter_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     _pc = pc;
 105     _deopt_state = is_deoptimized;
 106 
 107   } else {
 108     // We borrow this call to set the thread pointer in the interpreter
 109     // state; the hook to set up deoptimized frames isn't supplied it.
 110     assert(pc == NULL, "should be");
 111     get_interpreterState()->set_thread((JavaThread *) thread);
 112   }
 113 }
 114 
 115 bool frame::safe_for_sender(JavaThread *thread) {
 116   ShouldNotCallThis();
 117   return false;
 118 }
 119 
 120 bool frame::is_interpreted_frame_valid(JavaThread *thread) const {
 121   ShouldNotCallThis();
 122   return false;
 123 }
 124 
 125 BasicType frame::interpreter_frame_result(oop* oop_result,
 126                                           jvalue* value_result) {
 127   assert(is_interpreted_frame(), "interpreted frame expected");
 128   Method* method = interpreter_frame_method();
 129   BasicType type = method->result_type();
 130   intptr_t* tos_addr = (intptr_t *) interpreter_frame_tos_address();
 131   oop obj;
 132 
 133   switch (type) {
 134   case T_VOID:
 135     break;
 136   case T_BOOLEAN:
 137     value_result->z = *(jboolean *) tos_addr;
 138     break;
 139   case T_BYTE:
 140     value_result->b = *(jbyte *) tos_addr;
 141     break;
 142   case T_CHAR:
 143     value_result->c = *(jchar *) tos_addr;
 144     break;
 145   case T_SHORT:
 146     value_result->s = *(jshort *) tos_addr;
 147     break;
 148   case T_INT:
 149     value_result->i = *(jint *) tos_addr;
 150     break;
 151   case T_LONG:
 152     value_result->j = *(jlong *) tos_addr;
 153     break;
 154   case T_FLOAT:
 155     value_result->f = *(jfloat *) tos_addr;
 156     break;
 157   case T_DOUBLE:
 158     value_result->d = *(jdouble *) tos_addr;
 159     break;
 160 
 161   case T_OBJECT:
 162   case T_ARRAY:
 163     if (method->is_native()) {
 164       obj = get_interpreterState()->oop_temp();
 165     }
 166     else {
 167       oop* obj_p = (oop *) tos_addr;
 168       obj = (obj_p == NULL) ? (oop) NULL : *obj_p;
 169     }
 170     assert(obj == NULL || Universe::heap()->is_in(obj), "sanity check");
 171     *oop_result = obj;
 172     break;
 173 
 174   default:
 175     ShouldNotReachHere();
 176   }
 177 
 178   return type;
 179 }
 180 
 181 int frame::frame_size(RegisterMap* map) const {
 182 #ifdef PRODUCT
 183   ShouldNotCallThis();
 184 #endif // PRODUCT
 185   return 0; // make javaVFrame::print_value work
 186 }
 187 
 188 intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
 189   int index = (Interpreter::expr_offset_in_bytes(offset) / wordSize);
 190   return &interpreter_frame_tos_address()[index];
 191 }
 192 
 193 void frame::zero_print_on_error(int           frame_index,
 194                                 outputStream* st,
 195                                 char*         buf,
 196                                 int           buflen) const {
 197   // Divide the buffer between the field and the value
 198   buflen >>= 1;
 199   char *fieldbuf = buf;
 200   char *valuebuf = buf + buflen;
 201 
 202   // Print each word of the frame
 203   for (intptr_t *addr = sp(); addr <= fp(); addr++) {
 204     int offset = fp() - addr;
 205 
 206     // Fill in default values, then try and improve them
 207     snprintf(fieldbuf, buflen, "word[%d]", offset);
 208     snprintf(valuebuf, buflen, PTR_FORMAT, *addr);
 209     zeroframe()->identify_word(frame_index, offset, fieldbuf, valuebuf, buflen);
 210     fieldbuf[buflen - 1] = '\0';
 211     valuebuf[buflen - 1] = '\0';
 212 
 213     // Print the result
 214     st->print_cr(" " PTR_FORMAT ": %-21s = %s", p2i(addr), fieldbuf, valuebuf);
 215   }
 216 }
 217 
 218 void ZeroFrame::identify_word(int   frame_index,
 219                               int   offset,
 220                               char* fieldbuf,
 221                               char* valuebuf,
 222                               int   buflen) const {
 223   switch (offset) {
 224   case next_frame_off:
 225     strncpy(fieldbuf, "next_frame", buflen);
 226     break;
 227 
 228   case frame_type_off:
 229     strncpy(fieldbuf, "frame_type", buflen);
 230     if (is_entry_frame())
 231       strncpy(valuebuf, "ENTRY_FRAME", buflen);
 232     else if (is_interpreter_frame())
 233       strncpy(valuebuf, "INTERPRETER_FRAME", buflen);
 234     else if (is_fake_stub_frame())
 235       strncpy(valuebuf, "FAKE_STUB_FRAME", buflen);
 236     break;
 237 
 238   default:
 239     if (is_entry_frame()) {
 240       as_entry_frame()->identify_word(
 241         frame_index, offset, fieldbuf, valuebuf, buflen);
 242     }
 243     else if (is_interpreter_frame()) {
 244       as_interpreter_frame()->identify_word(
 245         frame_index, offset, fieldbuf, valuebuf, buflen);
 246     }
 247     else if (is_fake_stub_frame()) {
 248       as_fake_stub_frame()->identify_word(
 249         frame_index, offset, fieldbuf, valuebuf, buflen);
 250     }
 251   }
 252 }
 253 
 254 void EntryFrame::identify_word(int   frame_index,
 255                                int   offset,
 256                                char* fieldbuf,
 257                                char* valuebuf,
 258                                int   buflen) const {
 259   switch (offset) {
 260   case call_wrapper_off:
 261     strncpy(fieldbuf, "call_wrapper", buflen);
 262     break;
 263 
 264   default:
 265     snprintf(fieldbuf, buflen, "local[%d]", offset - 3);
 266   }
 267 }
 268 
 269 void InterpreterFrame::identify_word(int   frame_index,
 270                                      int   offset,
 271                                      char* fieldbuf,
 272                                      char* valuebuf,
 273                                      int   buflen) const {
 274   interpreterState istate = interpreter_state();
 275   bool is_valid = istate->self_link() == istate;
 276   intptr_t *addr = addr_of_word(offset);
 277 
 278   // Fixed part
 279   if (addr >= (intptr_t *) istate) {
 280     const char *field = istate->name_of_field_at_address((address) addr);
 281     if (field) {
 282       if (is_valid && !strcmp(field, "_method")) {
 283         istate->method()->name_and_sig_as_C_string(valuebuf, buflen);
 284       }
 285       else if (is_valid && !strcmp(field, "_bcp") && istate->bcp()) {
 286         snprintf(valuebuf, buflen, PTR_FORMAT " (bci %d)",
 287                  (intptr_t) istate->bcp(),
 288                  istate->method()->bci_from(istate->bcp()));
 289       }
 290       snprintf(fieldbuf, buflen, "%sistate->%s",
 291                field[strlen(field) - 1] == ')' ? "(": "", field);
 292     }
 293     else if (addr == (intptr_t *) istate) {
 294       strncpy(fieldbuf, "(vtable for istate)", buflen);
 295     }
 296     return;
 297   }
 298 
 299   // Variable part
 300   if (!is_valid)
 301     return;
 302 
 303   // JNI stuff
 304   if (istate->method()->is_native() && addr < istate->stack_base()) {
 305     address hA = istate->method()->signature_handler();
 306     if (hA != NULL) {
 307       if (hA != (address) InterpreterRuntime::slow_signature_handler) {
 308         InterpreterRuntime::SignatureHandler *handler =
 309           InterpreterRuntime::SignatureHandler::from_handlerAddr(hA);
 310 
 311         intptr_t *params = istate->stack_base() - handler->argument_count();
 312         if (addr >= params) {
 313           int param = addr - params;
 314           const char *desc = "";
 315           if (param == 0)
 316             desc = " (JNIEnv)";
 317           else if (param == 1) {
 318             if (istate->method()->is_static())
 319               desc = " (mirror)";
 320             else
 321               desc = " (this)";
 322           }
 323           snprintf(fieldbuf, buflen, "parameter[%d]%s", param, desc);
 324           return;
 325         }
 326 
 327         for (int i = 0; i < handler->argument_count(); i++) {
 328           if (params[i] == (intptr_t) addr) {
 329             snprintf(fieldbuf, buflen, "unboxed parameter[%d]", i);
 330             return;
 331           }
 332         }
 333       }
 334     }
 335     return;
 336   }
 337 
 338   // Monitors and stack
 339   identify_vp_word(frame_index, addr,
 340                    (intptr_t *) istate->monitor_base(),
 341                    istate->stack_base(),
 342                    fieldbuf, buflen);
 343 }
 344 
 345 void ZeroFrame::identify_vp_word(int       frame_index,
 346                                  intptr_t* addr,
 347                                  intptr_t* monitor_base,
 348                                  intptr_t* stack_base,
 349                                  char*     fieldbuf,
 350                                  int       buflen) const {
 351   // Monitors
 352   if (addr >= stack_base && addr < monitor_base) {
 353     int monitor_size = frame::interpreter_frame_monitor_size();
 354     int last_index = (monitor_base - stack_base) / monitor_size - 1;
 355     int index = last_index - (addr - stack_base) / monitor_size;
 356     intptr_t monitor = (intptr_t) (
 357       (BasicObjectLock *) monitor_base - 1 - index);
 358     intptr_t offset = (intptr_t) addr - monitor;
 359 
 360     if (offset == BasicObjectLock::obj_offset_in_bytes())
 361       snprintf(fieldbuf, buflen, "monitor[%d]->_obj", index);
 362     else if (offset ==  BasicObjectLock::lock_offset_in_bytes())
 363       snprintf(fieldbuf, buflen, "monitor[%d]->_lock", index);
 364 
 365     return;
 366   }
 367 
 368   // Expression stack
 369   if (addr < stack_base) {
 370     snprintf(fieldbuf, buflen, "%s[%d]",
 371              frame_index == 0 ? "stack_word" : "local",
 372              (int) (stack_base - addr - 1));
 373     return;
 374   }
 375 }
 376 
 377 #ifndef PRODUCT
 378 
 379 void frame::describe_pd(FrameValues& values, int frame_no) {
 380 
 381 }
 382 
 383 #endif
 384 
 385 intptr_t *frame::initial_deoptimization_info() {
 386   // unused... but returns fp() to minimize changes introduced by 7087445
 387   return fp();
 388 }
 389 
 390 #ifndef PRODUCT
 391 // This is a generic constructor which is only used by pns() in debug.cpp.
 392 frame::frame(void* sp, void* fp, void* pc) {
 393   Unimplemented();
 394 }
 395 
 396 void frame::pd_ps() {}
 397 #endif