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