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_VFRAME_HPP
  26 #define SHARE_RUNTIME_VFRAME_HPP
  27 
  28 #include "code/debugInfo.hpp"
  29 #include "code/debugInfoRec.hpp"
  30 #include "code/location.hpp"
  31 #include "oops/oop.hpp"
  32 #include "runtime/frame.hpp"
  33 #include "runtime/stackValue.hpp"
  34 #include "runtime/stackValueCollection.hpp"
  35 #include "utilities/growableArray.hpp"
  36 
  37 // vframes are virtual stack frames representing source level activations.
  38 // A single frame may hold several source level activations in the case of
  39 // optimized code. The debugging stored with the optimized code enables
  40 // us to unfold a frame as a stack of vframes.
  41 // A cVFrame represents an activation of a non-java method.
  42 
  43 // The vframe inheritance hierarchy:
  44 // - vframe
  45 //   - javaVFrame
  46 //     - interpretedVFrame
  47 //     - compiledVFrame     ; (used for both compiled Java methods and native stubs)
  48 //   - externalVFrame
  49 //     - entryVFrame        ; special frame created when calling Java from C
  50 
  51 // - BasicLock
  52 
  53 class vframe: public ResourceObj {
  54  protected:
  55   frame        _fr;      // Raw frame behind the virtual frame.
  56   RegisterMap  _reg_map; // Register map for the raw frame (used to handle callee-saved registers).
  57   JavaThread*  _thread;  // The thread owning the raw frame.
  58 
  59   vframe(const frame* fr, const RegisterMap* reg_map, JavaThread* thread);
  60   vframe(const frame* fr, JavaThread* thread);
  61  public:
  62   // Factory method for creating vframes
  63   static vframe* new_vframe(const frame* f, const RegisterMap *reg_map, JavaThread* thread);
  64 
  65   // Accessors
  66   frame              fr()           const { return _fr;       }
  67   CodeBlob*          cb()         const { return _fr.cb();  }
  68   CompiledMethod*   nm()         const {
  69       assert( cb() != NULL && cb()->is_compiled(), "usage");
  70       return (CompiledMethod*) cb();
  71   }
  72 
  73 // ???? Does this need to be a copy?
  74   frame*             frame_pointer() { return &_fr;       }
  75   const RegisterMap* register_map() const { return &_reg_map; }
  76   JavaThread*        thread()       const { return _thread;   }
  77 
  78   // Returns the sender vframe
  79   virtual vframe* sender() const;
  80 
  81   // Returns the next javaVFrame on the stack (skipping all other kinds of frame)
  82   javaVFrame *java_sender() const;
  83 
  84   // Answers if the this is the top vframe in the frame, i.e., if the sender vframe
  85   // is in the caller frame
  86   virtual bool is_top() const { return true; }
  87 
  88   // Returns top vframe within same frame (see is_top())
  89   virtual vframe* top() const;
  90 
  91   // Type testing operations
  92   virtual bool is_entry_frame()       const { return false; }
  93   virtual bool is_java_frame()        const { return false; }
  94   virtual bool is_interpreted_frame() const { return false; }
  95   virtual bool is_compiled_frame()    const { return false; }
  96 
  97 #ifndef PRODUCT
  98   // printing operations
  99   virtual void print_value() const;
 100   virtual void print();
 101 #endif
 102 };
 103 
 104 
 105 class javaVFrame: public vframe {
 106  public:
 107   // JVM state
 108   virtual Method*                      method()         const = 0;
 109   virtual int                          bci()            const = 0;
 110   virtual StackValueCollection*        locals()         const = 0;
 111   virtual StackValueCollection*        expressions()    const = 0;
 112   // the order returned by monitors() is from oldest -> youngest#4418568
 113   virtual GrowableArray<MonitorInfo*>* monitors()       const = 0;
 114 
 115   // Debugging support via JVMTI.
 116   // NOTE that this is not guaranteed to give correct results for compiled vframes.
 117   // Deoptimize first if necessary.
 118   virtual void set_locals(StackValueCollection* values) const = 0;
 119 
 120   // Test operation
 121   bool is_java_frame() const { return true; }
 122 
 123  protected:
 124   javaVFrame(const frame* fr, const RegisterMap* reg_map, JavaThread* thread) : vframe(fr, reg_map, thread) {}
 125   javaVFrame(const frame* fr, JavaThread* thread) : vframe(fr, thread) {}
 126 
 127  public:
 128   // casting
 129   static javaVFrame* cast(vframe* vf) {
 130     assert(vf == NULL || vf->is_java_frame(), "must be java frame");
 131     return (javaVFrame*) vf;
 132   }
 133 
 134   // Return an array of monitors locked by this frame in the youngest to oldest order
 135   GrowableArray<MonitorInfo*>* locked_monitors();
 136 
 137   // printing used during stack dumps and diagnostics
 138   static void print_locked_object_class_name(outputStream* st, Handle obj, const char* lock_state);
 139   void print_lock_info_on(outputStream* st, int frame_count);
 140   void print_lock_info(int frame_count) { print_lock_info_on(tty, frame_count); }
 141 
 142 #ifndef PRODUCT
 143  public:
 144   // printing operations
 145   void print();
 146   void print_value() const;
 147   void print_activation(int index) const;
 148 
 149   // verify operations
 150   virtual void verify() const;
 151 
 152   // Structural compare
 153   bool structural_compare(javaVFrame* other);
 154 #endif
 155   friend class vframe;
 156 };
 157 
 158 class interpretedVFrame: public javaVFrame {
 159  public:
 160   // JVM state
 161   Method*                      method()         const;
 162   int                          bci()            const;
 163   StackValueCollection*        locals()         const;
 164   StackValueCollection*        expressions()    const;
 165   GrowableArray<MonitorInfo*>* monitors()       const;
 166 
 167   void set_locals(StackValueCollection* values) const;
 168 
 169   // Test operation
 170   bool is_interpreted_frame() const { return true; }
 171 
 172  protected:
 173   interpretedVFrame(const frame* fr, const RegisterMap* reg_map, JavaThread* thread) : javaVFrame(fr, reg_map, thread) {};
 174 
 175  public:
 176   // Accessors for Byte Code Pointer
 177   u_char* bcp() const;
 178   void set_bcp(u_char* bcp);
 179 
 180   // casting
 181   static interpretedVFrame* cast(vframe* vf) {
 182     assert(vf == NULL || vf->is_interpreted_frame(), "must be interpreted frame");
 183     return (interpretedVFrame*) vf;
 184   }
 185 
 186  private:
 187   static const int bcp_offset;
 188   intptr_t* locals_addr_at(int offset) const;
 189   StackValueCollection* stack_data(bool expressions) const;
 190   // returns where the parameters starts relative to the frame pointer
 191   int start_of_parameters() const;
 192 
 193 #ifndef PRODUCT
 194  public:
 195   // verify operations
 196   void verify() const;
 197 #endif
 198   friend class vframe;
 199 };
 200 
 201 
 202 class externalVFrame: public vframe {
 203  protected:
 204   externalVFrame(const frame* fr, const RegisterMap* reg_map, JavaThread* thread) : vframe(fr, reg_map, thread) {}
 205 
 206 #ifndef PRODUCT
 207  public:
 208   // printing operations
 209   void print_value() const;
 210   void print();
 211 #endif
 212   friend class vframe;
 213 };
 214 
 215 class entryVFrame: public externalVFrame {
 216  public:
 217   bool is_entry_frame() const { return true; }
 218 
 219  protected:
 220   entryVFrame(const frame* fr, const RegisterMap* reg_map, JavaThread* thread);
 221 
 222  public:
 223   // casting
 224   static entryVFrame* cast(vframe* vf) {
 225     assert(vf == NULL || vf->is_entry_frame(), "must be entry frame");
 226     return (entryVFrame*) vf;
 227   }
 228 
 229 #ifndef PRODUCT
 230  public:
 231   // printing
 232   void print_value() const;
 233   void print();
 234 #endif
 235   friend class vframe;
 236 };
 237 
 238 
 239 // A MonitorInfo is a ResourceObject that describes a the pair:
 240 // 1) the owner of the monitor
 241 // 2) the monitor lock
 242 class MonitorInfo : public ResourceObj {
 243  private:
 244   oop        _owner; // the object owning the monitor
 245   BasicLock* _lock;
 246   oop        _owner_klass; // klass (mirror) if owner was scalar replaced
 247   bool       _eliminated;
 248   bool       _owner_is_scalar_replaced;
 249  public:
 250   // Constructor
 251   MonitorInfo(oop owner, BasicLock* lock, bool eliminated, bool owner_is_scalar_replaced) {
 252     if (!owner_is_scalar_replaced) {
 253       _owner = owner;
 254       _owner_klass = NULL;
 255     } else {
 256       assert(eliminated, "monitor should be eliminated for scalar replaced object");
 257       _owner = NULL;
 258       _owner_klass = owner;
 259     }
 260     _lock  = lock;
 261     _eliminated = eliminated;
 262     _owner_is_scalar_replaced = owner_is_scalar_replaced;
 263   }
 264   // Accessors
 265   oop        owner() const {
 266     assert(!_owner_is_scalar_replaced, "should not be called for scalar replaced object");
 267     return _owner;
 268   }
 269   oop   owner_klass() const {
 270     assert(_owner_is_scalar_replaced, "should not be called for not scalar replaced object");
 271     return _owner_klass;
 272   }
 273   BasicLock* lock()  const { return _lock;  }
 274   bool eliminated()  const { return _eliminated; }
 275   bool owner_is_scalar_replaced()  const { return _owner_is_scalar_replaced; }
 276 };
 277 
 278 class vframeStreamCommon : StackObj {
 279  protected:
 280   // common
 281   frame        _prev_frame;
 282   frame        _frame;
 283   JavaThread*  _thread;
 284   RegisterMap  _reg_map;
 285   enum { interpreted_mode, compiled_mode, at_end_mode } _mode;
 286 
 287   // For compiled_mode
 288   int _decode_offset;
 289   int _sender_decode_offset;
 290   int _vframe_id;
 291 
 292   // Cached information
 293   Method* _method;
 294   int       _bci;
 295 
 296   // Should VM activations be ignored or not
 297   bool _stop_at_java_call_stub;
 298 
 299   bool fill_in_compiled_inlined_sender();
 300   void fill_from_compiled_frame(int decode_offset);
 301   void fill_from_compiled_native_frame();
 302 
 303   void fill_from_interpreter_frame();
 304   bool fill_from_frame();
 305 
 306   // Helper routine for security_get_caller_frame
 307   void skip_prefixed_method_and_wrappers();
 308 
 309   DEBUG_ONLY(void found_bad_method_frame() const;)
 310 
 311  public:
 312   // Constructor
 313   inline vframeStreamCommon(JavaThread* thread);
 314 
 315   // Accessors
 316   Method* method() const { return _method; }
 317   int bci() const { return _bci; }
 318   inline intptr_t* frame_id() const;
 319   address frame_pc() const { return _frame.pc(); }
 320 
 321   CodeBlob*          cb()         const { return _frame.cb();  }
 322   CompiledMethod*   nm()         const {
 323       assert( cb() != NULL && cb()->is_compiled(), "usage");
 324       return (CompiledMethod*) cb();
 325   }
 326 
 327   javaVFrame* asJavaVFrame();
 328 
 329   // Frame type
 330   inline bool is_interpreted_frame() const;
 331   inline bool is_entry_frame() const;
 332 
 333   // Iteration
 334   inline void next();
 335   void security_next();
 336 
 337   bool at_end() const { return _mode == at_end_mode; }
 338 
 339   // Implements security traversal. Skips depth no. of frame including
 340   // special security frames and prefixed native methods
 341   void security_get_caller_frame(int depth);
 342 
 343   // Helper routine for JVM_LatestUserDefinedLoader -- needed for 1.4
 344   // reflection implementation
 345   void skip_reflection_related_frames();
 346 };
 347 
 348 class vframeStream : public vframeStreamCommon {
 349  public:
 350   // Constructors
 351   vframeStream(JavaThread* thread, bool stop_at_java_call_stub = false);
 352 
 353   // top_frame may not be at safepoint, start with sender
 354   vframeStream(JavaThread* thread, frame top_frame, bool stop_at_java_call_stub = false);
 355 };
 356 
 357 #endif // SHARE_RUNTIME_VFRAME_HPP