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