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