1 /*
   2  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2007, 2008, 2009, 2010 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/methodOop.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   // We borrow this call to set the thread pointer in the interpreter
 102   // state; the hook to set up deoptimized frames isn't supplied it.
 103   assert(pc == NULL, "should be");
 104   get_interpreterState()->set_thread((JavaThread *) thread);
 105 }
 106 
 107 bool frame::safe_for_sender(JavaThread *thread) {
 108   ShouldNotCallThis();
 109 }
 110 
 111 void frame::pd_gc_epilog() {
 112 }
 113 
 114 bool frame::is_interpreted_frame_valid(JavaThread *thread) const {
 115   ShouldNotCallThis();
 116 }
 117 
 118 BasicType frame::interpreter_frame_result(oop* oop_result,
 119                                           jvalue* value_result) {
 120   assert(is_interpreted_frame(), "interpreted frame expected");
 121   methodOop method = interpreter_frame_method();
 122   BasicType type = method->result_type();
 123   intptr_t* tos_addr = (intptr_t *) interpreter_frame_tos_address();
 124   oop obj;
 125 
 126   switch (type) {
 127   case T_VOID:
 128     break;
 129   case T_BOOLEAN:
 130     value_result->z = *(jboolean *) tos_addr;
 131     break;
 132   case T_BYTE:
 133     value_result->b = *(jbyte *) tos_addr;
 134     break;
 135   case T_CHAR:
 136     value_result->c = *(jchar *) tos_addr;
 137     break;
 138   case T_SHORT:
 139     value_result->s = *(jshort *) tos_addr;
 140     break;
 141   case T_INT:
 142     value_result->i = *(jint *) tos_addr;
 143     break;
 144   case T_LONG:
 145     value_result->j = *(jlong *) tos_addr;
 146     break;
 147   case T_FLOAT:
 148     value_result->f = *(jfloat *) tos_addr;
 149     break;
 150   case T_DOUBLE:
 151     value_result->d = *(jdouble *) tos_addr;
 152     break;
 153 
 154   case T_OBJECT:
 155   case T_ARRAY:
 156     if (method->is_native()) {
 157       obj = get_interpreterState()->oop_temp();
 158     }
 159     else {
 160       oop* obj_p = (oop *) tos_addr;
 161       obj = (obj_p == NULL) ? (oop) NULL : *obj_p;
 162     }
 163     assert(obj == NULL || Universe::heap()->is_in(obj), "sanity check");
 164     *oop_result = obj;
 165     break;
 166 
 167   default:
 168     ShouldNotReachHere();
 169   }
 170 
 171   return type;
 172 }
 173 
 174 int frame::frame_size(RegisterMap* map) const {
 175 #ifdef PRODUCT
 176   ShouldNotCallThis();
 177 #else
 178   return 0; // make javaVFrame::print_value work
 179 #endif // PRODUCT
 180 }
 181 
 182 intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
 183   int index = (Interpreter::expr_offset_in_bytes(offset) / wordSize);
 184   return &interpreter_frame_tos_address()[index];
 185 }
 186 
 187 void frame::zero_print_on_error(int           frame_index,
 188                                 outputStream* st,
 189                                 char*         buf,
 190                                 int           buflen) const {
 191   // Divide the buffer between the field and the value
 192   buflen >>= 1;
 193   char *fieldbuf = buf;
 194   char *valuebuf = buf + buflen;
 195 
 196   // Print each word of the frame
 197   for (intptr_t *addr = sp(); addr <= fp(); addr++) {
 198     int offset = fp() - addr;
 199 
 200     // Fill in default values, then try and improve them
 201     snprintf(fieldbuf, buflen, "word[%d]", offset);
 202     snprintf(valuebuf, buflen, PTR_FORMAT, *addr);
 203     zeroframe()->identify_word(frame_index, offset, fieldbuf, valuebuf, buflen);
 204     fieldbuf[buflen - 1] = '\0';
 205     valuebuf[buflen - 1] = '\0';
 206 
 207     // Print the result
 208     st->print_cr(" " PTR_FORMAT ": %-21s = %s", addr, fieldbuf, valuebuf);
 209   }
 210 }
 211 
 212 void ZeroFrame::identify_word(int   frame_index,
 213                               int   offset,
 214                               char* fieldbuf,
 215                               char* valuebuf,
 216                               int   buflen) const {
 217   switch (offset) {
 218   case next_frame_off:
 219     strncpy(fieldbuf, "next_frame", buflen);
 220     break;
 221 
 222   case frame_type_off:
 223     strncpy(fieldbuf, "frame_type", buflen);
 224     if (is_entry_frame())
 225       strncpy(valuebuf, "ENTRY_FRAME", buflen);
 226     else if (is_interpreter_frame())
 227       strncpy(valuebuf, "INTERPRETER_FRAME", buflen);
 228     else if (is_shark_frame())
 229       strncpy(valuebuf, "SHARK_FRAME", buflen);
 230     else if (is_fake_stub_frame())
 231       strncpy(valuebuf, "FAKE_STUB_FRAME", buflen);
 232     break;
 233 
 234   default:
 235     if (is_entry_frame()) {
 236       as_entry_frame()->identify_word(
 237         frame_index, offset, fieldbuf, valuebuf, buflen);
 238     }
 239     else if (is_interpreter_frame()) {
 240       as_interpreter_frame()->identify_word(
 241         frame_index, offset, fieldbuf, valuebuf, buflen);
 242     }
 243     else if (is_shark_frame()) {
 244       as_shark_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 SharkFrame::identify_word(int   frame_index,
 346                                int   offset,
 347                                char* fieldbuf,
 348                                char* valuebuf,
 349                                int   buflen) const {
 350   // Fixed part
 351   switch (offset) {
 352   case pc_off:
 353     strncpy(fieldbuf, "pc", buflen);
 354     if (method()->is_oop()) {
 355       nmethod *code = method()->code();
 356       if (code && code->pc_desc_at(pc())) {
 357         SimpleScopeDesc ssd(code, pc());
 358         snprintf(valuebuf, buflen, PTR_FORMAT " (bci %d)",
 359                  (intptr_t) pc(), ssd.bci());
 360       }
 361     }
 362     return;
 363 
 364   case unextended_sp_off:
 365     strncpy(fieldbuf, "unextended_sp", buflen);
 366     return;
 367 
 368   case method_off:
 369     strncpy(fieldbuf, "method", buflen);
 370     if (method()->is_oop()) {
 371       method()->name_and_sig_as_C_string(valuebuf, buflen);
 372     }
 373     return;
 374 
 375   case oop_tmp_off:
 376     strncpy(fieldbuf, "oop_tmp", buflen);
 377     return;
 378   }
 379 
 380   // Variable part
 381   if (method()->is_oop()) {
 382     identify_vp_word(frame_index, addr_of_word(offset),
 383                      addr_of_word(header_words + 1),
 384                      unextended_sp() + method()->max_stack(),
 385                      fieldbuf, buflen);
 386   }
 387 }
 388 
 389 void ZeroFrame::identify_vp_word(int       frame_index,
 390                                  intptr_t* addr,
 391                                  intptr_t* monitor_base,
 392                                  intptr_t* stack_base,
 393                                  char*     fieldbuf,
 394                                  int       buflen) const {
 395   // Monitors
 396   if (addr >= stack_base && addr < monitor_base) {
 397     int monitor_size = frame::interpreter_frame_monitor_size();
 398     int last_index = (monitor_base - stack_base) / monitor_size - 1;
 399     int index = last_index - (addr - stack_base) / monitor_size;
 400     intptr_t monitor = (intptr_t) (
 401       (BasicObjectLock *) monitor_base - 1 - index);
 402     intptr_t offset = (intptr_t) addr - monitor;
 403 
 404     if (offset == BasicObjectLock::obj_offset_in_bytes())
 405       snprintf(fieldbuf, buflen, "monitor[%d]->_obj", index);
 406     else if (offset ==  BasicObjectLock::lock_offset_in_bytes())
 407       snprintf(fieldbuf, buflen, "monitor[%d]->_lock", index);
 408 
 409     return;
 410   }
 411 
 412   // Expression stack
 413   if (addr < stack_base) {
 414     snprintf(fieldbuf, buflen, "%s[%d]",
 415              frame_index == 0 ? "stack_word" : "local",
 416              (int) (stack_base - addr - 1));
 417     return;
 418   }
 419 }