1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_RUNTIME_STACK_H_
18 #define ART_RUNTIME_STACK_H_
19 
20 #include <stdint.h>
21 #include <string>
22 
23 #include "base/macros.h"
24 #include "base/mutex.h"
25 #include "quick/quick_method_frame_info.h"
26 
27 namespace art {
28 
29 namespace mirror {
30 class Object;
31 }  // namespace mirror
32 
33 class ArtMethod;
34 class Context;
35 class HandleScope;
36 class OatQuickMethodHeader;
37 class ShadowFrame;
38 class Thread;
39 union JValue;
40 
41 // The kind of vreg being accessed in calls to Set/GetVReg.
42 enum VRegKind {
43   kReferenceVReg,
44   kIntVReg,
45   kFloatVReg,
46   kLongLoVReg,
47   kLongHiVReg,
48   kDoubleLoVReg,
49   kDoubleHiVReg,
50   kConstant,
51   kImpreciseConstant,
52   kUndefined,
53 };
54 std::ostream& operator<<(std::ostream& os, const VRegKind& rhs);
55 
56 // Size in bytes of the should_deoptimize flag on stack.
57 // We just need 4 bytes for our purpose regardless of the architecture. Frame size
58 // calculation will automatically do alignment for the final frame size.
59 static constexpr size_t kShouldDeoptimizeFlagSize = 4;
60 
61 /*
62  * Our current stack layout.
63  * The Dalvik registers come first, followed by the
64  * Method*, followed by other special temporaries if any, followed by
65  * regular compiler temporary. As of now we only have the Method* as
66  * as a special compiler temporary.
67  * A compiler temporary can be thought of as a virtual register that
68  * does not exist in the dex but holds intermediate values to help
69  * optimizations and code generation. A special compiler temporary is
70  * one whose location in frame is well known while non-special ones
71  * do not have a requirement on location in frame as long as code
72  * generator itself knows how to access them.
73  *
74  * TODO: Update this documentation?
75  *
76  *     +-------------------------------+
77  *     | IN[ins-1]                     |  {Note: resides in caller's frame}
78  *     |       .                       |
79  *     | IN[0]                         |
80  *     | caller's ArtMethod            |  ... ArtMethod*
81  *     +===============================+  {Note: start of callee's frame}
82  *     | core callee-save spill        |  {variable sized}
83  *     +-------------------------------+
84  *     | fp callee-save spill          |
85  *     +-------------------------------+
86  *     | filler word                   |  {For compatibility, if V[locals-1] used as wide
87  *     +-------------------------------+
88  *     | V[locals-1]                   |
89  *     | V[locals-2]                   |
90  *     |      .                        |
91  *     |      .                        |  ... (reg == 2)
92  *     | V[1]                          |  ... (reg == 1)
93  *     | V[0]                          |  ... (reg == 0) <---- "locals_start"
94  *     +-------------------------------+
95  *     | stack alignment padding       |  {0 to (kStackAlignWords-1) of padding}
96  *     +-------------------------------+
97  *     | Compiler temp region          |  ... (reg >= max_num_special_temps)
98  *     |      .                        |
99  *     |      .                        |
100  *     | V[max_num_special_temps + 1]  |
101  *     | V[max_num_special_temps + 0]  |
102  *     +-------------------------------+
103  *     | OUT[outs-1]                   |
104  *     | OUT[outs-2]                   |
105  *     |       .                       |
106  *     | OUT[0]                        |
107  *     | ArtMethod*                    |  ... (reg == num_total_code_regs == special_temp_value) <<== sp, 16-byte aligned
108  *     +===============================+
109  */
110 
111 class StackVisitor {
112  public:
113   // This enum defines a flag to control whether inlined frames are included
114   // when walking the stack.
115   enum class StackWalkKind {
116     kIncludeInlinedFrames,
117     kSkipInlinedFrames,
118   };
119 
120  protected:
121   StackVisitor(Thread* thread,
122                Context* context,
123                StackWalkKind walk_kind,
124                bool check_suspended = true);
125 
126   bool GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const
127       REQUIRES_SHARED(Locks::mutator_lock_);
128 
129  public:
~StackVisitor()130   virtual ~StackVisitor() {}
131   StackVisitor(const StackVisitor&) = default;
132   StackVisitor(StackVisitor&&) = default;
133 
134   // Return 'true' if we should continue to visit more frames, 'false' to stop.
135   virtual bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) = 0;
136 
137   enum class CountTransitions {
138     kYes,
139     kNo,
140   };
141 
142   template <CountTransitions kCount = CountTransitions::kYes>
143   void WalkStack(bool include_transitions = false) REQUIRES_SHARED(Locks::mutator_lock_);
144 
GetThread()145   Thread* GetThread() const {
146     return thread_;
147   }
148 
149   ArtMethod* GetMethod() const REQUIRES_SHARED(Locks::mutator_lock_);
150 
151   // Sets this stack frame's method pointer. This requires a full lock of the MutatorLock. This
152   // doesn't work with inlined methods.
153   void SetMethod(ArtMethod* method) REQUIRES(Locks::mutator_lock_);
154 
GetOuterMethod()155   ArtMethod* GetOuterMethod() const {
156     return *GetCurrentQuickFrame();
157   }
158 
IsShadowFrame()159   bool IsShadowFrame() const {
160     return cur_shadow_frame_ != nullptr;
161   }
162 
163   uint32_t GetDexPc(bool abort_on_failure = true) const REQUIRES_SHARED(Locks::mutator_lock_);
164 
165   mirror::Object* GetThisObject() const REQUIRES_SHARED(Locks::mutator_lock_);
166 
167   size_t GetNativePcOffset() const REQUIRES_SHARED(Locks::mutator_lock_);
168 
169   // Returns the height of the stack in the managed stack frames, including transitions.
GetFrameHeight()170   size_t GetFrameHeight() REQUIRES_SHARED(Locks::mutator_lock_) {
171     return GetNumFrames() - cur_depth_ - 1;
172   }
173 
174   // Returns a frame ID for JDWP use, starting from 1.
GetFrameId()175   size_t GetFrameId() REQUIRES_SHARED(Locks::mutator_lock_) {
176     return GetFrameHeight() + 1;
177   }
178 
GetNumFrames()179   size_t GetNumFrames() REQUIRES_SHARED(Locks::mutator_lock_) {
180     if (num_frames_ == 0) {
181       num_frames_ = ComputeNumFrames(thread_, walk_kind_);
182     }
183     return num_frames_;
184   }
185 
GetFrameDepth()186   size_t GetFrameDepth() const REQUIRES_SHARED(Locks::mutator_lock_) {
187     return cur_depth_;
188   }
189 
190   // Get the method and dex pc immediately after the one that's currently being visited.
191   bool GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc)
192       REQUIRES_SHARED(Locks::mutator_lock_);
193 
194   bool GetVReg(ArtMethod* m, uint16_t vreg, VRegKind kind, uint32_t* val) const
195       REQUIRES_SHARED(Locks::mutator_lock_);
196 
197   bool GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo, VRegKind kind_hi,
198                    uint64_t* val) const
199       REQUIRES_SHARED(Locks::mutator_lock_);
200 
201   // Values will be set in debugger shadow frames. Debugger will make sure deoptimization
202   // is triggered to make the values effective.
203   bool SetVReg(ArtMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind)
204       REQUIRES_SHARED(Locks::mutator_lock_);
205 
206   // Values will be set in debugger shadow frames. Debugger will make sure deoptimization
207   // is triggered to make the values effective.
208   bool SetVRegPair(ArtMethod* m,
209                    uint16_t vreg,
210                    uint64_t new_value,
211                    VRegKind kind_lo,
212                    VRegKind kind_hi)
213       REQUIRES_SHARED(Locks::mutator_lock_);
214 
215   uintptr_t* GetGPRAddress(uint32_t reg) const;
216 
217   uintptr_t GetReturnPc() const REQUIRES_SHARED(Locks::mutator_lock_);
218 
219   void SetReturnPc(uintptr_t new_ret_pc) REQUIRES_SHARED(Locks::mutator_lock_);
220 
IsInInlinedFrame()221   bool IsInInlinedFrame() const {
222     return current_inlining_depth_ != 0;
223   }
224 
GetCurrentInliningDepth()225   size_t GetCurrentInliningDepth() const {
226     return current_inlining_depth_;
227   }
228 
GetCurrentQuickFramePc()229   uintptr_t GetCurrentQuickFramePc() const {
230     return cur_quick_frame_pc_;
231   }
232 
GetCurrentQuickFrame()233   ArtMethod** GetCurrentQuickFrame() const {
234     return cur_quick_frame_;
235   }
236 
GetCurrentShadowFrame()237   ShadowFrame* GetCurrentShadowFrame() const {
238     return cur_shadow_frame_;
239   }
240 
GetCurrentHandleScope(size_t pointer_size)241   HandleScope* GetCurrentHandleScope(size_t pointer_size) const {
242     ArtMethod** sp = GetCurrentQuickFrame();
243     // Skip ArtMethod*; handle scope comes next;
244     return reinterpret_cast<HandleScope*>(reinterpret_cast<uintptr_t>(sp) + pointer_size);
245   }
246 
247   std::string DescribeLocation() const REQUIRES_SHARED(Locks::mutator_lock_);
248 
249   static size_t ComputeNumFrames(Thread* thread, StackWalkKind walk_kind)
250       REQUIRES_SHARED(Locks::mutator_lock_);
251 
252   static void DescribeStack(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_);
253 
GetCurrentOatQuickMethodHeader()254   const OatQuickMethodHeader* GetCurrentOatQuickMethodHeader() const {
255     return cur_oat_quick_method_header_;
256   }
257 
258   QuickMethodFrameInfo GetCurrentQuickFrameInfo() const REQUIRES_SHARED(Locks::mutator_lock_);
259 
260  private:
261   // Private constructor known in the case that num_frames_ has already been computed.
262   StackVisitor(Thread* thread,
263                Context* context,
264                StackWalkKind walk_kind,
265                size_t num_frames,
266                bool check_suspended = true)
267       REQUIRES_SHARED(Locks::mutator_lock_);
268 
IsAccessibleRegister(uint32_t reg,bool is_float)269   bool IsAccessibleRegister(uint32_t reg, bool is_float) const {
270     return is_float ? IsAccessibleFPR(reg) : IsAccessibleGPR(reg);
271   }
GetRegister(uint32_t reg,bool is_float)272   uintptr_t GetRegister(uint32_t reg, bool is_float) const {
273     DCHECK(IsAccessibleRegister(reg, is_float));
274     return is_float ? GetFPR(reg) : GetGPR(reg);
275   }
276 
277   bool IsAccessibleGPR(uint32_t reg) const;
278   uintptr_t GetGPR(uint32_t reg) const;
279 
280   bool IsAccessibleFPR(uint32_t reg) const;
281   uintptr_t GetFPR(uint32_t reg) const;
282 
283   bool GetVRegFromDebuggerShadowFrame(uint16_t vreg, VRegKind kind, uint32_t* val) const
284       REQUIRES_SHARED(Locks::mutator_lock_);
285   bool GetVRegFromOptimizedCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
286                                 uint32_t* val) const
287       REQUIRES_SHARED(Locks::mutator_lock_);
288 
289   bool GetVRegPairFromDebuggerShadowFrame(uint16_t vreg, VRegKind kind_lo, VRegKind kind_hi,
290                                           uint64_t* val) const
291       REQUIRES_SHARED(Locks::mutator_lock_);
292   bool GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg,
293                                     VRegKind kind_lo, VRegKind kind_hi,
294                                     uint64_t* val) const
295       REQUIRES_SHARED(Locks::mutator_lock_);
296   bool GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi, VRegKind kind_lo,
297                                    uint64_t* val) const
298       REQUIRES_SHARED(Locks::mutator_lock_);
299 
300   void SanityCheckFrame() const REQUIRES_SHARED(Locks::mutator_lock_);
301 
302   Thread* const thread_;
303   const StackWalkKind walk_kind_;
304   ShadowFrame* cur_shadow_frame_;
305   ArtMethod** cur_quick_frame_;
306   uintptr_t cur_quick_frame_pc_;
307   const OatQuickMethodHeader* cur_oat_quick_method_header_;
308   // Lazily computed, number of frames in the stack.
309   size_t num_frames_;
310   // Depth of the frame we're currently at.
311   size_t cur_depth_;
312   // Current inlining depth of the method we are currently at.
313   // 0 if there is no inlined frame.
314   size_t current_inlining_depth_;
315 
316  protected:
317   Context* const context_;
318   const bool check_suspended_;
319 };
320 
321 }  // namespace art
322 
323 #endif  // ART_RUNTIME_STACK_H_
324