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