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_FRAME_HPP
 26 #define SHARE_RUNTIME_FRAME_HPP
 27 
 28 #include "code/vmregTypes.hpp"
 29 #include "runtime/basicLock.hpp"
 30 #include "runtime/monitorChunk.hpp"
 31 #include "utilities/growableArray.hpp"
 32 #include "utilities/macros.hpp"
 33 #ifdef ZERO
 34 # include "stack_zero.hpp"
 35 #endif
 36 
 37 typedef class BytecodeInterpreter* interpreterState;
 38 
 39 class CodeBlob;
 40 class CompiledMethod;
 41 class FrameValues;
 42 class vframeArray;
 43 class JavaCallWrapper;
 44 class Method;
 45 class methodHandle;
 46 class RegisterMap;
 47 
 48 enum class DerivedPointerIterationMode {
 49   _with_table,
 50   _directly,
 51   _ignore
 52 };
 53 
 54 // A frame represents a physical stack frame (an activation).  Frames
 55 // can be C or Java frames, and the Java frames can be interpreted or
 56 // compiled.  In contrast, vframes represent source-level activations,
 57 // so that one physical frame can correspond to multiple source level
 58 // frames because of inlining.
 59 
 60 class frame {
 61  private:
 62   // Instance variables:
 63   intptr_t* _sp; // stack pointer (from Thread::last_Java_sp)
 64   address   _pc; // program counter (the next instruction after the call)
 65 
 66   CodeBlob* _cb; // CodeBlob that "owns" pc
 67   enum deopt_state {
 68     not_deoptimized,
 69     is_deoptimized,
 70     unknown
 71   };
 72 
 73   deopt_state _deopt_state;
 74 
 75  public:
 76   // Constructors
 77   frame();
 78 
 79 #ifndef PRODUCT
 80   // This is a generic constructor which is only used by pns() in debug.cpp.
 81   // pns (i.e. print native stack) uses this constructor to create a starting
 82   // frame for stack walking. The implementation of this constructor is platform
 83   // dependent (i.e. SPARC doesn't need an 'fp' argument an will ignore it) but
 84   // we want to keep the signature generic because pns() is shared code.
 85   frame(void* sp, void* fp, void* pc);
 86 #endif
 87 
 88   // Accessors
 89 
 90   // pc: Returns the pc at which this frame will continue normally.
 91   // It must point at the beginning of the next instruction to execute.
 92   address pc() const             { return _pc; }
 93 
 94   // This returns the pc that if you were in the debugger you'd see. Not
 95   // the idealized value in the frame object. This undoes the magic conversion
 96   // that happens for deoptimized frames. In addition it makes the value the
 97   // hardware would want to see in the native frame. The only user (at this point)
 98   // is deoptimization. It likely no one else should ever use it.
 99   address raw_pc() const;
100 
101   void set_pc( address   newpc );
102 
103   intptr_t* sp() const           { return _sp; }
104   void set_sp( intptr_t* newsp ) { _sp = newsp; }
105 
106 
107   CodeBlob* cb() const           { return _cb; }
108 
109   // patching operations
110   void   patch_pc(Thread* thread, address pc);
111 
112   // Every frame needs to return a unique id which distinguishes it from all other frames.
113   // For sparc and ia32 use sp. ia64 can have memory frames that are empty so multiple frames
114   // will have identical sp values. For ia64 the bsp (fp) value will serve. No real frame
115   // should have an id() of NULL so it is a distinguishing value for an unmatchable frame.
116   // We also have relationals which allow comparing a frame to anoth frame's id() allow
117   // us to distinguish younger (more recent activation) from older (less recent activations)
118   // A NULL id is only valid when comparing for equality.
119 
120   intptr_t* id(void) const;
121   bool is_younger(intptr_t* id) const;
122   bool is_older(intptr_t* id) const;
123 
124   // testers
125 
126   // Compares for strict equality. Rarely used or needed.
127   // It can return a different result than f1.id() == f2.id()
128   bool equal(frame other) const;
129 
130   // type testers
131   bool is_interpreted_frame()    const;
132   bool is_java_frame()           const;
133   bool is_entry_frame()          const;             // Java frame called from C?
134   bool is_stub_frame()           const;
135   bool is_ignored_frame()        const;
136   bool is_native_frame()         const;
137   bool is_runtime_frame()        const;
138   bool is_compiled_frame()       const;
139   bool is_safepoint_blob_frame() const;
140   bool is_deoptimized_frame()    const;
141   bool is_optimized_entry_frame()         const;
142 
143   // testers
144   bool is_first_frame() const; // oldest frame? (has no sender)
145   bool is_first_java_frame() const;              // same for Java frame
146 
147   bool is_interpreted_frame_valid(JavaThread* thread) const;       // performs sanity checks on interpreted frames.
148 
149   // is this frame doing a call using the compiled calling convention?
150   bool is_compiled_caller() const {
151     return is_compiled_frame() || is_optimized_entry_frame();
152   }
153 
154   // tells whether this frame is marked for deoptimization
155   bool should_be_deoptimized() const;
156 
157   // tells whether this frame can be deoptimized
158   bool can_be_deoptimized() const;
159 
160   // returns the frame size in stack slots
161   int frame_size(RegisterMap* map) const;
162 
163   // returns the sending frame
164   frame sender(RegisterMap* map) const;
165 
166   bool safe_for_sender(JavaThread *thread);
167 
168   // returns the sender, but skips conversion frames
169   frame real_sender(RegisterMap* map) const;
170 
171   // returns the the sending Java frame, skipping any intermediate C frames
172   // NB: receiver must not be first frame
173   frame java_sender() const;
174 
175  private:
176   // Helper methods for better factored code in frame::sender
177   frame sender_for_compiled_frame(RegisterMap* map) const;
178   frame sender_for_entry_frame(RegisterMap* map) const;
179   frame sender_for_interpreter_frame(RegisterMap* map) const;
180   frame sender_for_native_frame(RegisterMap* map) const;
181   frame sender_for_optimized_entry_frame(RegisterMap* map) const;
182 
183   bool is_entry_frame_valid(JavaThread* thread) const;
184 
185   // All frames:
186 
187   // A low-level interface for vframes:
188 
189  public:
190 
191   intptr_t* addr_at(int index) const             { return &fp()[index];    }
192   intptr_t  at(int index) const                  { return *addr_at(index); }
193 
194   // accessors for locals
195   oop obj_at(int offset) const                   { return *obj_at_addr(offset);  }
196   void obj_at_put(int offset, oop value)         { *obj_at_addr(offset) = value; }
197 
198   jint int_at(int offset) const                  { return *int_at_addr(offset);  }
199   void int_at_put(int offset, jint value)        { *int_at_addr(offset) = value; }
200 
201   oop*      obj_at_addr(int offset) const        { return (oop*)     addr_at(offset); }
202 
203   oop*      adjusted_obj_at_addr(Method* method, int index) { return obj_at_addr(adjust_offset(method, index)); }
204 
205  private:
206   jint*    int_at_addr(int offset) const         { return (jint*)    addr_at(offset); }
207 
208  public:
209   // Link (i.e., the pointer to the previous frame)
210   // might crash if the frame has no parent
211   intptr_t* link() const;
212 
213   // Link (i.e., the pointer to the previous frame) or null if the link cannot be accessed
214   intptr_t* link_or_null() const;
215 
216   // Return address
217   address  sender_pc() const;
218 
219   // Support for deoptimization
220   void deoptimize(JavaThread* thread);
221 
222   // The frame's original SP, before any extension by an interpreted callee;
223   // used for packing debug info into vframeArray objects and vframeArray lookup.
224   intptr_t* unextended_sp() const;
225 
226   // returns the stack pointer of the calling frame
227   intptr_t* sender_sp() const;
228 
229   // Returns the real 'frame pointer' for the current frame.
230   // This is the value expected by the platform ABI when it defines a
231   // frame pointer register. It may differ from the effective value of
232   // the FP register when that register is used in the JVM for other
233   // purposes (like compiled frames on some platforms).
234   // On other platforms, it is defined so that the stack area used by
235   // this frame goes from real_fp() to sp().
236   intptr_t* real_fp() const;
237 
238   // Deoptimization info, if needed (platform dependent).
239   // Stored in the initial_info field of the unroll info, to be used by
240   // the platform dependent deoptimization blobs.
241   intptr_t *initial_deoptimization_info();
242 
243   // Interpreter frames:
244 
245  private:
246   intptr_t** interpreter_frame_locals_addr() const;
247   intptr_t*  interpreter_frame_bcp_addr() const;
248   intptr_t*  interpreter_frame_mdp_addr() const;
249 
250  public:
251   // Locals
252 
253   // The _at version returns a pointer because the address is used for GC.
254   intptr_t* interpreter_frame_local_at(int index) const;
255 
256   void interpreter_frame_set_locals(intptr_t* locs);
257 
258   // byte code index
259   jint interpreter_frame_bci() const;
260 
261   // byte code pointer
262   address interpreter_frame_bcp() const;
263   void    interpreter_frame_set_bcp(address bcp);
264 
265   // method data pointer
266   address interpreter_frame_mdp() const;
267   void    interpreter_frame_set_mdp(address dp);
268 
269   // Find receiver out of caller's (compiled) argument list
270   oop retrieve_receiver(RegisterMap *reg_map);
271 
272   // Return the monitor owner and BasicLock for compiled synchronized
273   // native methods. Used by JVMTI's GetLocalInstance method
274   // (via VM_GetReceiver) to retrieve the receiver from a native wrapper frame.
275   BasicLock* get_native_monitor();
276   oop        get_native_receiver();
277 
278   // Find receiver for an invoke when arguments are just pushed on stack (i.e., callee stack-frame is
279   // not setup)
280   oop interpreter_callee_receiver(Symbol* signature)     { return *interpreter_callee_receiver_addr(signature); }
281 
282 
283   oop* interpreter_callee_receiver_addr(Symbol* signature);
284 
285 
286   // expression stack (may go up or down, direction == 1 or -1)
287  public:
288   intptr_t* interpreter_frame_expression_stack() const;
289 
290   // The _at version returns a pointer because the address is used for GC.
291   intptr_t* interpreter_frame_expression_stack_at(jint offset) const;
292 
293   // top of expression stack
294   intptr_t* interpreter_frame_tos_at(jint offset) const;
295   intptr_t* interpreter_frame_tos_address() const;
296 
297 
298   jint  interpreter_frame_expression_stack_size() const;
299 
300   intptr_t* interpreter_frame_sender_sp() const;
301 
302   // template based interpreter deoptimization support
303   void  set_interpreter_frame_sender_sp(intptr_t* sender_sp);
304   void interpreter_frame_set_monitor_end(BasicObjectLock* value);
305 
306   // Address of the temp oop in the frame. Needed as GC root.
307   oop* interpreter_frame_temp_oop_addr() const;
308 
309   // BasicObjectLocks:
310   //
311   // interpreter_frame_monitor_begin is higher in memory than interpreter_frame_monitor_end
312   // Interpreter_frame_monitor_begin points to one element beyond the oldest one,
313   // interpreter_frame_monitor_end   points to the youngest one, or if there are none,
314   //                                 it points to one beyond where the first element will be.
315   // interpreter_frame_monitor_size  reports the allocation size of a monitor in the interpreter stack.
316   //                                 this value is >= BasicObjectLock::size(), and may be rounded up
317 
318   BasicObjectLock* interpreter_frame_monitor_begin() const;
319   BasicObjectLock* interpreter_frame_monitor_end()   const;
320   BasicObjectLock* next_monitor_in_interpreter_frame(BasicObjectLock* current) const;
321   BasicObjectLock* previous_monitor_in_interpreter_frame(BasicObjectLock* current) const;
322   static int interpreter_frame_monitor_size();
323 
324   void interpreter_frame_verify_monitor(BasicObjectLock* value) const;
325 
326   // Return/result value from this interpreter frame
327   // If the method return type is T_OBJECT or T_ARRAY populates oop_result
328   // For other (non-T_VOID) the appropriate field in the jvalue is populated
329   // with the result value.
330   // Should only be called when at method exit when the method is not
331   // exiting due to an exception.
332   BasicType interpreter_frame_result(oop* oop_result, jvalue* value_result);
333 
334  public:
335   // Method & constant pool cache
336   Method* interpreter_frame_method() const;
337   void interpreter_frame_set_method(Method* method);
338   Method** interpreter_frame_method_addr() const;
339   ConstantPoolCache** interpreter_frame_cache_addr() const;
340   oop* interpreter_frame_mirror_addr() const;
341 
342   void interpreter_frame_set_mirror(oop mirror);
343 
344  public:
345   // Entry frames
346   JavaCallWrapper* entry_frame_call_wrapper() const { return *entry_frame_call_wrapper_addr(); }
347   JavaCallWrapper* entry_frame_call_wrapper_if_safe(JavaThread* thread) const;
348   JavaCallWrapper** entry_frame_call_wrapper_addr() const;
349   intptr_t* entry_frame_argument_at(int offset) const;
350 
351   // tells whether there is another chunk of Delta stack above
352   bool entry_frame_is_first() const;
353   bool optimized_entry_frame_is_first() const;
354 
355   // Safepoints
356 
357  public:
358   oop saved_oop_result(RegisterMap* map) const;
359   void set_saved_oop_result(RegisterMap* map, oop obj);
360 
361   // For debugging
362  private:
363   const char* print_name() const;
364 
365   void describe_pd(FrameValues& values, int frame_no);
366 
367  public:
368   void print_value() const { print_value_on(tty,NULL); }
369   void print_value_on(outputStream* st, JavaThread *thread) const;
370   void print_on(outputStream* st) const;
371   void interpreter_frame_print_on(outputStream* st) const;
372   void print_on_error(outputStream* st, char* buf, int buflen, bool verbose = false) const;
373   static void print_C_frame(outputStream* st, char* buf, int buflen, address pc);
374 
375   // Add annotated descriptions of memory locations belonging to this frame to values
376   void describe(FrameValues& values, int frame_no);
377 
378   // Conversion from a VMReg to physical stack location
379   address oopmapreg_to_location(VMReg reg, const RegisterMap* reg_map) const;
380   oop* oopmapreg_to_oop_location(VMReg reg, const RegisterMap* reg_map) const;
381 
382   // Oops-do's
383   void oops_compiled_arguments_do(Symbol* signature, bool has_receiver, bool has_appendix, const RegisterMap* reg_map, OopClosure* f) const;
384   void oops_interpreted_do(OopClosure* f, const RegisterMap* map, bool query_oop_map_cache = true) const;
385   void buffered_values_interpreted_do(BufferedValueClosure* f);
386 
387  private:
388   void oops_interpreted_arguments_do(Symbol* signature, bool has_receiver, OopClosure* f) const;
389 
390   // Iteration of oops
391   void oops_do_internal(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* map,
392                         bool use_interpreter_oop_map_cache, DerivedPointerIterationMode derived_mode) const;
393   void oops_entry_do(OopClosure* f, const RegisterMap* map) const;
394   void oops_code_blob_do(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* map,
395                          DerivedPointerIterationMode derived_mode) const;
396   int adjust_offset(Method* method, int index); // helper for above fn
397  public:
398   // Memory management
399   void oops_do(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* map,
400                DerivedPointerIterationMode derived_mode) const;
401   void oops_do(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* map) const;
402   void nmethods_do(CodeBlobClosure* cf) const;
403 
404   // RedefineClasses support for finding live interpreted methods on the stack
405   void metadata_do(MetadataClosure* f) const;
406 
407   // Verification
408   void verify(const RegisterMap* map) const;
409   static bool verify_return_pc(address x);
410   // Usage:
411   // assert(frame::verify_return_pc(return_address), "must be a return pc");
412 
413 #include CPU_HEADER(frame)
414 
415 };
416 
417 #ifndef PRODUCT
418 // A simple class to describe a location on the stack
419 class FrameValue {
420  public:
421   intptr_t* location;
422   char* description;
423   int owner;
424   int priority;
425 
426   FrameValue() {
427     location = NULL;
428     description = NULL;
429     owner = -1;
430     priority = 0;
431   }
432 
433 };
434 
435 
436 // A collection of described stack values that can print a symbolic
437 // description of the stack memory.  Interpreter frame values can be
438 // in the caller frames so all the values are collected first and then
439 // sorted before being printed.
440 class FrameValues {
441  private:
442   GrowableArray<FrameValue> _values;
443 
444   static int compare(FrameValue* a, FrameValue* b) {
445     if (a->location == b->location) {
446       return a->priority - b->priority;
447     }
448     return a->location - b->location;
449   }
450 
451  public:
452   // Used by frame functions to describe locations.
453   void describe(int owner, intptr_t* location, const char* description, int priority = 0);
454 
455 #ifdef ASSERT
456   void validate();
457 #endif
458   void print(JavaThread* thread) { print_on(thread, tty); }
459   void print_on(JavaThread* thread, outputStream* out);
460 };
461 
462 #endif
463 
464 
465 #endif // SHARE_RUNTIME_FRAME_HPP