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