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