1 /*
   2  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_RUNTIME_FRAME_HPP
  26 #define SHARE_VM_RUNTIME_FRAME_HPP
  27 
  28 #include "oops/method.hpp"
  29 #include "runtime/basicLock.hpp"
  30 #include "runtime/monitorChunk.hpp"
  31 #include "runtime/registerMap.hpp"
  32 #ifdef TARGET_ARCH_zero
  33 # include "stack_zero.hpp"
  34 #endif
  35 
  36 typedef class BytecodeInterpreter* interpreterState;
  37 
  38 class CodeBlob;
  39 class FrameValues;
  40 class vframeArray;
  41 
  42 
  43 // A frame represents a physical stack frame (an activation).  Frames
  44 // can be C or Java frames, and the Java frames can be interpreted or
  45 // compiled.  In contrast, vframes represent source-level activations,
  46 // so that one physical frame can correspond to multiple source level
  47 // frames because of inlining.
  48 
  49 class frame VALUE_OBJ_CLASS_SPEC {
  50  private:
  51   // Instance variables:
  52   intptr_t* _sp; // stack pointer (from Thread::last_Java_sp)
  53   address   _pc; // program counter (the next instruction after the call)
  54 
  55   CodeBlob* _cb; // CodeBlob that "owns" pc
  56   enum deopt_state {
  57     not_deoptimized,
  58     is_deoptimized,
  59     unknown
  60   };
  61 
  62   deopt_state _deopt_state;
  63 
  64  public:
  65   // Constructors
  66   frame();
  67 
  68 #ifndef PRODUCT
  69   // This is a generic constructor which is only used by pns() in debug.cpp.
  70   // pns (i.e. print native stack) uses this constructor to create a starting
  71   // frame for stack walking. The implementation of this constructor is platform
  72   // dependent (i.e. SPARC doesn't need an 'fp' argument an will ignore it) but
  73   // we want to keep the signature generic because pns() is shared code.
  74   frame(void* sp, void* fp, void* pc);
  75 #endif
  76 
  77   // Accessors
  78 
  79   // pc: Returns the pc at which this frame will continue normally.
  80   // It must point at the beginning of the next instruction to execute.
  81   address pc() const             { return _pc; }
  82 
  83   // This returns the pc that if you were in the debugger you'd see. Not
  84   // the idealized value in the frame object. This undoes the magic conversion
  85   // that happens for deoptimized frames. In addition it makes the value the
  86   // hardware would want to see in the native frame. The only user (at this point)
  87   // is deoptimization. It likely no one else should ever use it.
  88   address raw_pc() const;
  89 
  90   void set_pc( address   newpc );
  91 
  92   intptr_t* sp() const           { return _sp; }
  93   void set_sp( intptr_t* newsp ) { _sp = newsp; }
  94 
  95 
  96   CodeBlob* cb() const           { return _cb; }
  97 
  98   // patching operations
  99   void   patch_pc(Thread* thread, address pc);
 100 
 101   // Every frame needs to return a unique id which distinguishes it from all other frames.
 102   // For sparc and ia32 use sp. ia64 can have memory frames that are empty so multiple frames
 103   // will have identical sp values. For ia64 the bsp (fp) value will serve. No real frame
 104   // should have an id() of NULL so it is a distinguishing value for an unmatchable frame.
 105   // We also have relationals which allow comparing a frame to anoth frame's id() allow
 106   // us to distinguish younger (more recent activation) from older (less recent activations)
 107   // A NULL id is only valid when comparing for equality.
 108 
 109   intptr_t* id(void) const;
 110   bool is_younger(intptr_t* id) const;
 111   bool is_older(intptr_t* id) const;
 112 
 113   // testers
 114 
 115   // Compares for strict equality. Rarely used or needed.
 116   // It can return a different result than f1.id() == f2.id()
 117   bool equal(frame other) const;
 118 
 119   // type testers
 120   bool is_interpreted_frame()    const;
 121   bool is_java_frame()           const;
 122   bool is_entry_frame()          const;             // Java frame called from C?
 123   bool is_stub_frame()           const;
 124   bool is_ignored_frame()        const;
 125   bool is_native_frame()         const;
 126   bool is_runtime_frame()        const;
 127   bool is_compiled_frame()       const;
 128   bool is_safepoint_blob_frame() const;
 129   bool is_deoptimized_frame()    const;
 130 
 131   // testers
 132   bool is_first_frame() const; // oldest frame? (has no sender)
 133   bool is_first_java_frame() const;              // same for Java frame
 134 
 135   bool is_interpreted_frame_valid(JavaThread* thread) const;       // performs sanity checks on interpreted frames.
 136 
 137   // tells whether this frame is marked for deoptimization
 138   bool should_be_deoptimized() const;
 139 
 140   // tells whether this frame can be deoptimized
 141   bool can_be_deoptimized() const;
 142 
 143   // returns the frame size in stack slots
 144   int frame_size(RegisterMap* map) const;
 145 
 146   // returns the sending frame
 147   frame sender(RegisterMap* map) const;
 148 
 149   // for Profiling - acting on another frame. walks sender frames
 150   // if valid.
 151   frame profile_find_Java_sender_frame(JavaThread *thread);
 152   bool safe_for_sender(JavaThread *thread);
 153 
 154   // returns the sender, but skips conversion frames
 155   frame real_sender(RegisterMap* map) const;
 156 
 157   // returns the the sending Java frame, skipping any intermediate C frames
 158   // NB: receiver must not be first frame
 159   frame java_sender() const;
 160 
 161  private:
 162   // Helper methods for better factored code in frame::sender
 163   frame sender_for_compiled_frame(RegisterMap* map) const;
 164   frame sender_for_entry_frame(RegisterMap* map) const;
 165   frame sender_for_interpreter_frame(RegisterMap* map) const;
 166   frame sender_for_native_frame(RegisterMap* map) const;
 167 
 168   // All frames:
 169 
 170   // A low-level interface for vframes:
 171 
 172  public:
 173 
 174   intptr_t* addr_at(int index) const             { return &fp()[index];    }
 175   intptr_t  at(int index) const                  { return *addr_at(index); }
 176 
 177   // accessors for locals
 178   oop obj_at(int offset) const                   { return *obj_at_addr(offset);  }
 179   void obj_at_put(int offset, oop value)         { *obj_at_addr(offset) = value; }
 180 
 181   jint int_at(int offset) const                  { return *int_at_addr(offset);  }
 182   void int_at_put(int offset, jint value)        { *int_at_addr(offset) = value; }
 183 
 184   oop*      obj_at_addr(int offset) const        { return (oop*)     addr_at(offset); }
 185 
 186   oop*      adjusted_obj_at_addr(Method* method, int index) { return obj_at_addr(adjust_offset(method, index)); }
 187 
 188  private:
 189   jint*    int_at_addr(int offset) const         { return (jint*)    addr_at(offset); }
 190 
 191  public:
 192   // Link (i.e., the pointer to the previous frame)
 193   intptr_t* link() const;
 194 
 195   // Return address
 196   address  sender_pc() const;
 197 
 198   // Support for deoptimization
 199   void deoptimize(JavaThread* thread);
 200 
 201   // The frame's original SP, before any extension by an interpreted callee;
 202   // used for packing debug info into vframeArray objects and vframeArray lookup.
 203   intptr_t* unextended_sp() const;
 204 
 205   // returns the stack pointer of the calling frame
 206   intptr_t* sender_sp() const;
 207 
 208   // Returns the real 'frame pointer' for the current frame.
 209   // This is the value expected by the platform ABI when it defines a
 210   // frame pointer register. It may differ from the effective value of
 211   // the FP register when that register is used in the JVM for other
 212   // purposes (like compiled frames on some platforms).
 213   // On other platforms, it is defined so that the stack area used by
 214   // this frame goes from real_fp() to sp().
 215   intptr_t* real_fp() const;
 216 
 217   // Deoptimization info, if needed (platform dependent).
 218   // Stored in the initial_info field of the unroll info, to be used by
 219   // the platform dependent deoptimization blobs.
 220   intptr_t *initial_deoptimization_info();
 221 
 222   // Interpreter frames:
 223 
 224  private:
 225   intptr_t** interpreter_frame_locals_addr() const;
 226   intptr_t*  interpreter_frame_bcp_addr() const;
 227   intptr_t*  interpreter_frame_mdp_addr() const;
 228 
 229  public:
 230   // Locals
 231 
 232   // The _at version returns a pointer because the address is used for GC.
 233   intptr_t* interpreter_frame_local_at(int index) const;
 234 
 235   void interpreter_frame_set_locals(intptr_t* locs);
 236 
 237   // byte code index
 238   jint interpreter_frame_bci() const;
 239 
 240   // byte code pointer
 241   address interpreter_frame_bcp() const;
 242   void    interpreter_frame_set_bcp(address bcp);
 243 
 244   // method data pointer
 245   address interpreter_frame_mdp() const;
 246   void    interpreter_frame_set_mdp(address dp);
 247 
 248   // Find receiver out of caller's (compiled) argument list
 249   oop retrieve_receiver(RegisterMap *reg_map);
 250 
 251   // Return the monitor owner and BasicLock for compiled synchronized
 252   // native methods so that biased locking can revoke the receiver's
 253   // bias if necessary.  This is also used by JVMTI's GetLocalInstance method
 254   // (via VM_GetReceiver) to retrieve the receiver from a native wrapper frame.
 255   BasicLock* get_native_monitor();
 256   oop        get_native_receiver();
 257 
 258   // Find receiver for an invoke when arguments are just pushed on stack (i.e., callee stack-frame is
 259   // not setup)
 260   oop interpreter_callee_receiver(Symbol* signature)     { return *interpreter_callee_receiver_addr(signature); }
 261 
 262 
 263   oop* interpreter_callee_receiver_addr(Symbol* signature);
 264 
 265 
 266   // expression stack (may go up or down, direction == 1 or -1)
 267  public:
 268   intptr_t* interpreter_frame_expression_stack() const;
 269   static  jint  interpreter_frame_expression_stack_direction();
 270 
 271   // The _at version returns a pointer because the address is used for GC.
 272   intptr_t* interpreter_frame_expression_stack_at(jint offset) const;
 273 
 274   // top of expression stack
 275   intptr_t* interpreter_frame_tos_at(jint offset) const;
 276   intptr_t* interpreter_frame_tos_address() const;
 277 
 278 
 279   jint  interpreter_frame_expression_stack_size() const;
 280 
 281   intptr_t* interpreter_frame_sender_sp() const;
 282 
 283 #ifndef CC_INTERP
 284   // template based interpreter deoptimization support
 285   void  set_interpreter_frame_sender_sp(intptr_t* sender_sp);
 286   void interpreter_frame_set_monitor_end(BasicObjectLock* value);
 287 #endif // CC_INTERP
 288 
 289   // Address of the temp oop in the frame. Needed as GC root.
 290   oop* interpreter_frame_temp_oop_addr() const;
 291 
 292   // BasicObjectLocks:
 293   //
 294   // interpreter_frame_monitor_begin is higher in memory than interpreter_frame_monitor_end
 295   // Interpreter_frame_monitor_begin points to one element beyond the oldest one,
 296   // interpreter_frame_monitor_end   points to the youngest one, or if there are none,
 297   //                                 it points to one beyond where the first element will be.
 298   // interpreter_frame_monitor_size  reports the allocation size of a monitor in the interpreter stack.
 299   //                                 this value is >= BasicObjectLock::size(), and may be rounded up
 300 
 301   BasicObjectLock* interpreter_frame_monitor_begin() const;
 302   BasicObjectLock* interpreter_frame_monitor_end()   const;
 303   BasicObjectLock* next_monitor_in_interpreter_frame(BasicObjectLock* current) const;
 304   BasicObjectLock* previous_monitor_in_interpreter_frame(BasicObjectLock* current) const;
 305   static int interpreter_frame_monitor_size();
 306 
 307   void interpreter_frame_verify_monitor(BasicObjectLock* value) const;
 308 
 309   // Return/result value from this interpreter frame
 310   // If the method return type is T_OBJECT or T_ARRAY populates oop_result
 311   // For other (non-T_VOID) the appropriate field in the jvalue is populated
 312   // with the result value.
 313   // Should only be called when at method exit when the method is not
 314   // exiting due to an exception.
 315   BasicType interpreter_frame_result(oop* oop_result, jvalue* value_result);
 316 
 317  public:
 318   // Method & constant pool cache
 319   Method* interpreter_frame_method() const;
 320   void interpreter_frame_set_method(Method* method);
 321   Method** interpreter_frame_method_addr() const;
 322   ConstantPoolCache** interpreter_frame_cache_addr() const;
 323   oop* interpreter_frame_mirror_addr() const;
 324 
 325   void interpreter_frame_set_mirror(oop mirror);
 326 
 327  public:
 328   // Entry frames
 329   JavaCallWrapper* entry_frame_call_wrapper() const { return *entry_frame_call_wrapper_addr(); }
 330   JavaCallWrapper* entry_frame_call_wrapper_if_safe(JavaThread* thread) const;
 331   JavaCallWrapper** entry_frame_call_wrapper_addr() const;
 332   intptr_t* entry_frame_argument_at(int offset) const;
 333 
 334   // tells whether there is another chunk of Delta stack above
 335   bool entry_frame_is_first() const;
 336 
 337   // Compiled frames:
 338 
 339  public:
 340   // Given the index of a local, and the number of argument words
 341   // in this stack frame, tell which word of the stack frame to find
 342   // the local in.  Arguments are stored above the ofp/rpc pair,
 343   // while other locals are stored below it.
 344   // Since monitors (BasicLock blocks) are also assigned indexes,
 345   // but may have different storage requirements, their presence
 346   // can also affect the calculation of offsets.
 347   static int local_offset_for_compiler(int local_index, int nof_args, int max_nof_locals, int max_nof_monitors);
 348 
 349   // Given the index of a monitor, etc., tell which word of the
 350   // stack frame contains the start of the BasicLock block.
 351   // Note that the local index by convention is the __higher__
 352   // of the two indexes allocated to the block.
 353   static int monitor_offset_for_compiler(int local_index, int nof_args, int max_nof_locals, int max_nof_monitors);
 354 
 355   // Tell the smallest value that local_offset_for_compiler will attain.
 356   // This is used to help determine how much stack frame to allocate.
 357   static int min_local_offset_for_compiler(int nof_args, int max_nof_locals, int max_nof_monitors);
 358 
 359   // Tells if this register must be spilled during a call.
 360   // On Intel, all registers are smashed by calls.
 361   static bool volatile_across_calls(Register reg);
 362 
 363 
 364   // Safepoints
 365 
 366  public:
 367   oop saved_oop_result(RegisterMap* map) const;
 368   void set_saved_oop_result(RegisterMap* map, oop obj);
 369 
 370   // For debugging
 371  private:
 372   const char* print_name() const;
 373 
 374   void describe_pd(FrameValues& values, int frame_no);
 375 
 376  public:
 377   void print_value() const { print_value_on(tty,NULL); }
 378   void print_value_on(outputStream* st, JavaThread *thread) const;
 379   void print_on(outputStream* st) const;
 380   void interpreter_frame_print_on(outputStream* st) const;
 381   void print_on_error(outputStream* st, char* buf, int buflen, bool verbose = false) const;
 382   static void print_C_frame(outputStream* st, char* buf, int buflen, address pc);
 383 
 384   // Add annotated descriptions of memory locations belonging to this frame to values
 385   void describe(FrameValues& values, int frame_no);
 386 
 387   // Conversion from an VMReg to physical stack location
 388   oop* oopmapreg_to_location(VMReg reg, const RegisterMap* regmap) const;
 389 
 390   // Oops-do's
 391   void oops_compiled_arguments_do(Symbol* signature, bool has_receiver, bool has_appendix, const RegisterMap* reg_map, OopClosure* f);
 392   void oops_interpreted_do(OopClosure* f, const RegisterMap* map, bool query_oop_map_cache = true);
 393 
 394  private:
 395   void oops_interpreted_arguments_do(Symbol* signature, bool has_receiver, OopClosure* f);
 396 
 397   // Iteration of oops
 398   void oops_do_internal(OopClosure* f, CodeBlobClosure* cf, RegisterMap* map, bool use_interpreter_oop_map_cache);
 399   void oops_entry_do(OopClosure* f, const RegisterMap* map);
 400   void oops_code_blob_do(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* map);
 401   int adjust_offset(Method* method, int index); // helper for above fn
 402  public:
 403   // Memory management
 404   void oops_do(OopClosure* f, CodeBlobClosure* cf, RegisterMap* map) { oops_do_internal(f, cf, map, true); }
 405   void nmethods_do(CodeBlobClosure* cf);
 406 
 407   // RedefineClasses support for finding live interpreted methods on the stack
 408   void metadata_do(void f(Metadata*));
 409 
 410   // Verification
 411   void verify(const RegisterMap* map);
 412   static bool verify_return_pc(address x);
 413   // Usage:
 414   // assert(frame::verify_return_pc(return_address), "must be a return pc");
 415 
 416   int pd_oop_map_offset_adjustment() const;
 417 
 418 #ifdef TARGET_ARCH_x86
 419 # include "frame_x86.hpp"
 420 #endif
 421 #ifdef TARGET_ARCH_sparc
 422 # include "frame_sparc.hpp"
 423 #endif
 424 #ifdef TARGET_ARCH_zero
 425 # include "frame_zero.hpp"
 426 #endif
 427 #ifdef TARGET_ARCH_arm
 428 # include "frame_arm.hpp"
 429 #endif
 430 #ifdef TARGET_ARCH_ppc
 431 # include "frame_ppc.hpp"
 432 #endif
 433 #ifdef TARGET_ARCH_aarch64
 434 # include "frame_aarch64.hpp"
 435 #endif
 436 
 437 };
 438 
 439 #ifndef PRODUCT
 440 // A simple class to describe a location on the stack
 441 class FrameValue VALUE_OBJ_CLASS_SPEC {
 442  public:
 443   intptr_t* location;
 444   char* description;
 445   int owner;
 446   int priority;
 447 
 448   FrameValue() {
 449     location = NULL;
 450     description = NULL;
 451     owner = -1;
 452     priority = 0;
 453   }
 454 
 455 };
 456 
 457 
 458 // A collection of described stack values that can print a symbolic
 459 // description of the stack memory.  Interpreter frame values can be
 460 // in the caller frames so all the values are collected first and then
 461 // sorted before being printed.
 462 class FrameValues {
 463  private:
 464   GrowableArray<FrameValue> _values;
 465 
 466   static int compare(FrameValue* a, FrameValue* b) {
 467     if (a->location == b->location) {
 468       return a->priority - b->priority;
 469     }
 470     return a->location - b->location;
 471   }
 472 
 473  public:
 474   // Used by frame functions to describe locations.
 475   void describe(int owner, intptr_t* location, const char* description, int priority = 0);
 476 
 477 #ifdef ASSERT
 478   void validate();
 479 #endif
 480   void print(JavaThread* thread);
 481 };
 482 
 483 #endif
 484 
 485 //
 486 // StackFrameStream iterates through the frames of a thread starting from
 487 // top most frame. It automatically takes care of updating the location of
 488 // all (callee-saved) registers. Notice: If a thread is stopped at
 489 // a safepoint, all registers are saved, not only the callee-saved ones.
 490 //
 491 // Use:
 492 //
 493 //   for(StackFrameStream fst(thread); !fst.is_done(); fst.next()) {
 494 //     ...
 495 //   }
 496 //
 497 class StackFrameStream : public StackObj {
 498  private:
 499   frame       _fr;
 500   RegisterMap _reg_map;
 501   bool        _is_done;
 502  public:
 503    StackFrameStream(JavaThread *thread, bool update = true);
 504 
 505   // Iteration
 506   bool is_done()                  { return (_is_done) ? true : (_is_done = _fr.is_first_frame(), false); }
 507   void next()                     { if (!_is_done) _fr = _fr.sender(&_reg_map); }
 508 
 509   // Query
 510   frame *current()                { return &_fr; }
 511   RegisterMap* register_map()     { return &_reg_map; }
 512 };
 513 
 514 #endif // SHARE_VM_RUNTIME_FRAME_HPP