1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2016 SAP SE. All rights reserved.
   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 "interpreter/interpreter.hpp"
  28 #include "memory/resourceArea.hpp"
  29 #include "oops/markOop.hpp"
  30 #include "oops/oop.inline.hpp"
  31 #include "runtime/frame.inline.hpp"
  32 #include "runtime/handles.inline.hpp"
  33 #include "runtime/javaCalls.hpp"
  34 #include "runtime/monitorChunk.hpp"
  35 #include "runtime/signature.hpp"
  36 #include "runtime/stubCodeGenerator.hpp"
  37 #include "runtime/stubRoutines.hpp"
  38 #include "vmreg_s390.inline.hpp"
  39 #ifdef COMPILER1
  40 #include "c1/c1_Runtime1.hpp"
  41 #include "runtime/vframeArray.hpp"
  42 #endif
  43 
  44 // Major contributions by Aha, AS.
  45 
  46 #ifdef ASSERT
  47 void RegisterMap::check_location_valid() {
  48 }
  49 #endif // ASSERT
  50 
  51 
  52 // Profiling/safepoint support
  53 
  54 bool frame::safe_for_sender(JavaThread *thread) {
  55   bool safe = false;
  56   address cursp = (address)sp();
  57   address curfp = (address)fp();
  58   if ((cursp != NULL && curfp != NULL &&
  59       (cursp <= thread->stack_base() && cursp >= thread->stack_base() - thread->stack_size())) &&
  60       (curfp <= thread->stack_base() && curfp >= thread->stack_base() - thread->stack_size())) {
  61     safe = true;
  62   }
  63   return safe;
  64 }
  65 
  66 bool frame::is_interpreted_frame() const {
  67   return Interpreter::contains(pc());
  68 }
  69 
  70 // sender_sp
  71 
  72 intptr_t* frame::interpreter_frame_sender_sp() const {
  73   return sender_sp();
  74 }
  75 
  76 frame frame::sender_for_entry_frame(RegisterMap *map) const {
  77   assert(map != NULL, "map must be set");
  78   // Java frame called from C. Skip all C frames and return top C
  79   // frame of that chunk as the sender.
  80   JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
  81 
  82   assert(!entry_frame_is_first(), "next Java sp must be non zero");
  83   assert(jfa->last_Java_sp() > _sp, "must be above this frame on stack");
  84 
  85   map->clear();
  86 
  87   assert(map->include_argument_oops(), "should be set by clear");
  88 
  89   if (jfa->last_Java_pc() != NULL) {
  90     frame fr(jfa->last_Java_sp(), jfa->last_Java_pc());
  91     return fr;
  92   }
  93   // Last_java_pc is not set if we come here from compiled code.
  94   frame fr(jfa->last_Java_sp());
  95   return fr;
  96 }
  97 
  98 frame frame::sender_for_interpreter_frame(RegisterMap *map) const {
  99   // Pass callers sender_sp as unextended_sp.
 100   return frame(sender_sp(), sender_pc(), (intptr_t*)(ijava_state()->sender_sp));
 101 }
 102 
 103 frame frame::sender_for_compiled_frame(RegisterMap *map) const {
 104   assert(map != NULL, "map must be set");
 105   // Frame owned by compiler.
 106 
 107   address pc = *compiled_sender_pc_addr(_cb);
 108   frame caller(compiled_sender_sp(_cb), pc);
 109 
 110   // Now adjust the map.
 111 
 112   // Get the rest.
 113   if (map->update_map()) {
 114     // Tell GC to use argument oopmaps for some runtime stubs that need it.
 115     map->set_include_argument_oops(_cb->caller_must_gc_arguments(map->thread()));
 116     if (_cb->oop_maps() != NULL) {
 117       OopMapSet::update_register_map(this, map);
 118     }
 119   }
 120 
 121   return caller;
 122 }
 123 
 124 intptr_t* frame::compiled_sender_sp(CodeBlob* cb) const {
 125   return sender_sp();
 126 }
 127 
 128 address* frame::compiled_sender_pc_addr(CodeBlob* cb) const {
 129   return sender_pc_addr();
 130 }
 131 
 132 frame frame::sender(RegisterMap* map) const {
 133   // Default is we don't have to follow them. The sender_for_xxx will
 134   // update it accordingly.
 135   map->set_include_argument_oops(false);
 136 
 137   if (is_entry_frame()) {
 138     return sender_for_entry_frame(map);
 139   }
 140   if (is_interpreted_frame()) {
 141     return sender_for_interpreter_frame(map);
 142   }
 143   assert(_cb == CodeCache::find_blob(pc()),"Must be the same");
 144   if (_cb != NULL) {
 145     return sender_for_compiled_frame(map);
 146   }
 147   // Must be native-compiled frame, i.e. the marshaling code for native
 148   // methods that exists in the core system.
 149   return frame(sender_sp(), sender_pc());
 150 }
 151 
 152 void frame::patch_pc(Thread* thread, address pc) {
 153   if (TracePcPatching) {
 154     tty->print_cr("patch_pc at address  " PTR_FORMAT " [" PTR_FORMAT " -> " PTR_FORMAT "] ",
 155                   p2i(&((address*) _sp)[-1]), p2i(((address*) _sp)[-1]), p2i(pc));
 156   }
 157   own_abi()->return_pc = (uint64_t)pc;
 158   _cb = CodeCache::find_blob(pc);
 159   address original_pc = nmethod::get_deopt_original_pc(this);
 160   if (original_pc != NULL) {
 161     assert(original_pc == _pc, "expected original to be stored before patching");
 162     _deopt_state = is_deoptimized;
 163     // Leave _pc as is.
 164   } else {
 165     _deopt_state = not_deoptimized;
 166     _pc = pc;
 167   }
 168 }
 169 
 170 bool frame::is_interpreted_frame_valid(JavaThread* thread) const {
 171   // Is there anything to do?
 172   assert(is_interpreted_frame(), "Not an interpreted frame");
 173   return true;
 174 }
 175 
 176 BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {
 177   assert(is_interpreted_frame(), "interpreted frame expected");
 178   Method* method = interpreter_frame_method();
 179   BasicType type = method->result_type();
 180 
 181   if (method->is_native()) {
 182     address lresult = (address)&(ijava_state()->lresult);
 183     address fresult = (address)&(ijava_state()->fresult);
 184 
 185     switch (type) {
 186       case T_OBJECT:
 187       case T_ARRAY: {
 188         *oop_result = (oop) (void*) ijava_state()->oop_tmp;
 189         break;
 190       }
 191       // We use std/stfd to store the values.
 192       case T_BOOLEAN : value_result->z = (jboolean) *(unsigned long*)lresult; break;
 193       case T_INT     : value_result->i = (jint)     *(long*)lresult;          break;
 194       case T_CHAR    : value_result->c = (jchar)    *(unsigned long*)lresult; break;
 195       case T_SHORT   : value_result->s = (jshort)   *(long*)lresult;          break;
 196       case T_BYTE    : value_result->z = (jbyte)    *(long*)lresult;          break;
 197       case T_LONG    : value_result->j = (jlong)    *(long*)lresult;          break;
 198       case T_FLOAT   : value_result->f = (jfloat)   *(float*)fresult;        break;
 199       case T_DOUBLE  : value_result->d = (jdouble)  *(double*)fresult;        break;
 200       case T_VOID    : break; // Nothing to do.
 201       default        : ShouldNotReachHere();
 202     }
 203   } else {
 204     intptr_t* tos_addr = interpreter_frame_tos_address();
 205     switch (type) {
 206       case T_OBJECT:
 207       case T_ARRAY: {
 208        oop obj = *(oop*)tos_addr;
 209        assert(obj == NULL || Universe::heap()->is_in(obj), "sanity check");
 210        *oop_result = obj;
 211        break;
 212       }
 213       case T_BOOLEAN : value_result->z = (jboolean) *(jint*)tos_addr; break;
 214       case T_BYTE    : value_result->b = (jbyte) *(jint*)tos_addr; break;
 215       case T_CHAR    : value_result->c = (jchar) *(jint*)tos_addr; break;
 216       case T_SHORT   : value_result->s = (jshort) *(jint*)tos_addr; break;
 217       case T_INT     : value_result->i = *(jint*)tos_addr; break;
 218       case T_LONG    : value_result->j = *(jlong*)tos_addr; break;
 219       case T_FLOAT   : value_result->f = *(jfloat*)tos_addr; break;
 220       case T_DOUBLE  : value_result->d = *(jdouble*)tos_addr; break;
 221       case T_VOID    : break; // Nothing to do.
 222       default        : ShouldNotReachHere();
 223     }
 224   }
 225 
 226   return type;
 227 }
 228 
 229 
 230 // Dump all frames starting a given C stack-pointer.
 231 // Use max_frames to limit the number of traced frames.
 232 void frame::back_trace(outputStream* st, intptr_t* start_sp, intptr_t* top_pc, unsigned long flags, int max_frames) {
 233 
 234   static char buf[ 150 ];
 235 
 236   bool print_outgoing_arguments = flags & 0x1;
 237   bool print_istate_pointers    = flags & 0x2;
 238   int num = 0;
 239 
 240   intptr_t* current_sp = (intptr_t*) start_sp;
 241   int last_num_jargs = 0;
 242   int frame_type = 0;
 243   int last_frame_type = 0;
 244 
 245   while (current_sp) {
 246     intptr_t* current_fp = (intptr_t*) *current_sp;
 247     address   current_pc = (num == 0)
 248                            ? (address) top_pc
 249                            : (address) *((intptr_t*)(((address) current_sp) + _z_abi(return_pc)));
 250 
 251     if ((intptr_t*) current_fp != 0 && (intptr_t*) current_fp <= current_sp) {
 252       st->print_cr("ERROR: corrupt stack");
 253       return;
 254     }
 255 
 256     st->print("#%-3d ", num);
 257     const char* type_name = "    ";
 258     const char* function_name = NULL;
 259 
 260     // Detect current frame's frame_type, default to 'C frame'.
 261     frame_type = 0;
 262 
 263     CodeBlob* blob = NULL;
 264 
 265     if (Interpreter::contains(current_pc)) {
 266       frame_type = 1;
 267     } else if (StubRoutines::contains(current_pc)) {
 268       if (StubRoutines::returns_to_call_stub(current_pc)) {
 269         frame_type = 2;
 270       } else {
 271         frame_type = 4;
 272         type_name = "stu";
 273         StubCodeDesc* desc = StubCodeDesc::desc_for (current_pc);
 274         if (desc) {
 275           function_name = desc->name();
 276         } else {
 277           function_name = "unknown stub";
 278         }
 279       }
 280     } else if (CodeCache::contains(current_pc)) {
 281       blob = CodeCache::find_blob_unsafe(current_pc);
 282       if (blob) {
 283         if (blob->is_nmethod()) {
 284           frame_type = 3;
 285         } else if (blob->is_deoptimization_stub()) {
 286           frame_type = 4;
 287           type_name = "deo";
 288           function_name = "deoptimization blob";
 289         } else if (blob->is_uncommon_trap_stub()) {
 290           frame_type = 4;
 291           type_name = "uct";
 292           function_name = "uncommon trap blob";
 293         } else if (blob->is_exception_stub()) {
 294           frame_type = 4;
 295           type_name = "exc";
 296           function_name = "exception blob";
 297         } else if (blob->is_safepoint_stub()) {
 298           frame_type = 4;
 299           type_name = "saf";
 300           function_name = "safepoint blob";
 301         } else if (blob->is_runtime_stub()) {
 302           frame_type = 4;
 303           type_name = "run";
 304           function_name = ((RuntimeStub *)blob)->name();
 305         } else if (blob->is_method_handles_adapter_blob()) {
 306           frame_type = 4;
 307           type_name = "mha";
 308           function_name = "method handles adapter blob";
 309         } else {
 310           frame_type = 4;
 311           type_name = "blo";
 312           function_name = "unknown code blob";
 313         }
 314       } else {
 315         frame_type = 4;
 316         type_name = "blo";
 317         function_name = "unknown code blob";
 318       }
 319     }
 320 
 321     st->print("sp=" PTR_FORMAT " ", p2i(current_sp));
 322 
 323     if (frame_type == 0) {
 324       current_pc = (address) *((intptr_t*)(((address) current_sp) + _z_abi(gpr14)));
 325     }
 326 
 327     st->print("pc=" PTR_FORMAT " ", p2i(current_pc));
 328     st->print(" ");
 329 
 330     switch (frame_type) {
 331       case 0: // C frame:
 332         {
 333           st->print("    ");
 334           if (current_pc == 0) {
 335             st->print("? ");
 336           } else {
 337              // name
 338             int func_offset;
 339             char demangled_name[256];
 340             int demangled_name_len = 256;
 341             if (os::dll_address_to_function_name(current_pc, demangled_name, demangled_name_len, &func_offset)) {
 342               demangled_name[demangled_name_len-1] = '\0';
 343               st->print(func_offset == -1 ? "%s " : "%s+0x%x", demangled_name, func_offset);
 344             } else {
 345               st->print("? ");
 346             }
 347           }
 348         }
 349         break;
 350 
 351       case 1: // interpreter frame:
 352         {
 353           st->print(" i  ");
 354 
 355           if (last_frame_type != 1) last_num_jargs = 8;
 356 
 357           // name
 358           Method* method = *(Method**)((address)current_fp + _z_ijava_state_neg(method));
 359           if (method) {
 360             if (method->is_synchronized()) st->print("synchronized ");
 361             if (method->is_static()) st->print("static ");
 362             if (method->is_native()) st->print("native ");
 363             method->name_and_sig_as_C_string(buf, sizeof(buf));
 364             st->print("%s ", buf);
 365           }
 366           else
 367             st->print("? ");
 368 
 369           intptr_t* tos = (intptr_t*) *(intptr_t*)((address)current_fp + _z_ijava_state_neg(esp));
 370           if (print_istate_pointers) {
 371             st->cr();
 372             st->print("     ");
 373             st->print("ts=" PTR_FORMAT " ", p2i(tos));
 374           }
 375 
 376           // Dump some Java stack slots.
 377           if (print_outgoing_arguments) {
 378             if (method->is_native()) {
 379 #ifdef ASSERT
 380               intptr_t* cargs = (intptr_t*) (((address)current_sp) + _z_abi(carg_1));
 381               for (int i = 0; i < last_num_jargs; i++) {
 382                 // Cargs is not prepushed.
 383                 st->cr();
 384                 st->print("        ");
 385                 st->print(PTR_FORMAT, *(cargs));
 386                 cargs++;
 387               }
 388 #endif /* ASSERT */
 389             }
 390             else {
 391               if (tos) {
 392                 for (int i = 0; i < last_num_jargs; i++) {
 393                   // tos+0 is prepushed, ignore.
 394                   tos++;
 395                   if (tos >= (intptr_t *)((address)current_fp + _z_ijava_state_neg(monitors)))
 396                     break;
 397                   st->cr();
 398                   st->print("        ");
 399                   st->print(PTR_FORMAT " %+.3e %+.3le", *(tos), *(float*)(tos), *(double*)(tos));
 400                 }
 401               }
 402             }
 403             last_num_jargs = method->size_of_parameters();
 404           }
 405         }
 406         break;
 407 
 408       case 2: // entry frame:
 409         {
 410           st->print("v2i ");
 411 
 412           // name
 413           st->print("call stub");
 414         }
 415         break;
 416 
 417       case 3: // compiled frame:
 418         {
 419           st->print(" c  ");
 420 
 421           // name
 422           Method* method = ((nmethod *)blob)->method();
 423           if (method) {
 424             method->name_and_sig_as_C_string(buf, sizeof(buf));
 425             st->print("%s ", buf);
 426           }
 427           else
 428             st->print("? ");
 429         }
 430         break;
 431 
 432       case 4: // named frames
 433         {
 434           st->print("%s ", type_name);
 435 
 436           // name
 437           if (function_name)
 438             st->print("%s", function_name);
 439         }
 440         break;
 441 
 442       default:
 443         break;
 444     }
 445 
 446     st->cr();
 447     st->flush();
 448 
 449     current_sp = current_fp;
 450     last_frame_type = frame_type;
 451     num++;
 452     // Check for maximum # of frames, and stop when reached.
 453     if (max_frames > 0 && --max_frames == 0)
 454       break;
 455   }
 456 
 457 }
 458 
 459 // Convenience function for calls from the debugger.
 460 
 461 extern "C" void bt(intptr_t* start_sp,intptr_t* top_pc) {
 462   frame::back_trace(tty,start_sp, top_pc, 0);
 463 }
 464 
 465 extern "C" void bt_full(intptr_t* start_sp,intptr_t* top_pc) {
 466   frame::back_trace(tty,start_sp, top_pc, (unsigned long)(long)-1);
 467 }
 468 
 469 
 470 // Function for tracing a limited number of frames.
 471 // Use this one if you only need to see the "top of stack" frames.
 472 extern "C" void bt_max(intptr_t *start_sp, intptr_t *top_pc, int max_frames) {
 473   frame::back_trace(tty, start_sp, top_pc, 0, max_frames);
 474 }
 475 
 476 #if !defined(PRODUCT)
 477 
 478 #define DESCRIBE_ADDRESS(name) \
 479   values.describe(frame_no, (intptr_t*)&ijava_state()->name, #name);
 480 
 481 void frame::describe_pd(FrameValues& values, int frame_no) {
 482   if (is_interpreted_frame()) {
 483     // Describe z_ijava_state elements.
 484     DESCRIBE_ADDRESS(method);
 485     DESCRIBE_ADDRESS(locals);
 486     DESCRIBE_ADDRESS(monitors);
 487     DESCRIBE_ADDRESS(cpoolCache);
 488     DESCRIBE_ADDRESS(bcp);
 489     DESCRIBE_ADDRESS(mdx);
 490     DESCRIBE_ADDRESS(esp);
 491     DESCRIBE_ADDRESS(sender_sp);
 492     DESCRIBE_ADDRESS(top_frame_sp);
 493     DESCRIBE_ADDRESS(oop_tmp);
 494     DESCRIBE_ADDRESS(lresult);
 495     DESCRIBE_ADDRESS(fresult);
 496   }
 497 }
 498 
 499 #endif // !PRODUCT
 500 
 501 intptr_t *frame::initial_deoptimization_info() {
 502   // Used to reset the saved FP.
 503   return fp();
 504 }