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