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