blob: ff0bcd0356235f79cdccbd45ca8e9044a988e0e1 [file] [log] [blame]
Elliott Hughes68e76522011-10-05 13:22:16 -07001/*
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_SRC_STACK_H_
18#define ART_SRC_STACK_H_
19
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080020#include "dex_file.h"
Ian Rogers0399dde2012-06-06 17:09:28 -070021#include "heap.h"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070022#include "jni.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070023#include "macros.h"
Ian Rogers0399dde2012-06-06 17:09:28 -070024#include "oat/runtime/context.h"
25#include "trace.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070026
27#include <stdint.h>
28
29namespace art {
30
31class Method;
Ian Rogers0399dde2012-06-06 17:09:28 -070032class Object;
33class ShadowFrame;
Ian Rogers365c1022012-06-22 15:05:28 -070034class ScopedJniThreadState;
Elliott Hughes68e76522011-10-05 13:22:16 -070035class Thread;
36
Ian Rogers365c1022012-06-22 15:05:28 -070037jobject GetThreadStack(const ScopedJniThreadState&, Thread*);
Elliott Hughesbfe487b2011-10-26 15:48:55 -070038
Ian Rogers0399dde2012-06-06 17:09:28 -070039class ShadowFrame {
Elliott Hughes68e76522011-10-05 13:22:16 -070040 public:
Ian Rogers0399dde2012-06-06 17:09:28 -070041 // Number of references contained within this shadow frame
42 uint32_t NumberOfReferences() const {
43 return number_of_references_;
44 }
Elliott Hughes68e76522011-10-05 13:22:16 -070045
Ian Rogers0399dde2012-06-06 17:09:28 -070046 void SetNumberOfReferences(uint32_t number_of_references) {
47 number_of_references_ = number_of_references;
48 }
49
50 // Caller dex pc
51 uint32_t GetDexPC() const {
52 return dex_pc_;
53 }
54
55 void SetDexPC(uint32_t dex_pc) {
56 dex_pc_ = dex_pc;
57 }
58
59 // Link to previous shadow frame or NULL
60 ShadowFrame* GetLink() const {
61 return link_;
62 }
63
64 void SetLink(ShadowFrame* frame) {
65 DCHECK_NE(this, frame);
66 link_ = frame;
67 }
68
69 Object* GetReference(size_t i) const {
70 DCHECK_LT(i, number_of_references_);
71 return references_[i];
72 }
73
74 void SetReference(size_t i, Object* object) {
75 DCHECK_LT(i, number_of_references_);
76 references_[i] = object;
77 }
Elliott Hughes91bf6cd2012-02-14 17:27:48 -080078
Elliott Hughes68e76522011-10-05 13:22:16 -070079 Method* GetMethod() const {
Ian Rogers0399dde2012-06-06 17:09:28 -070080 DCHECK_NE(method_, static_cast<void*>(NULL));
81 return method_;
Elliott Hughes68e76522011-10-05 13:22:16 -070082 }
83
Ian Rogers0399dde2012-06-06 17:09:28 -070084 void SetMethod(Method* method) {
85 DCHECK_NE(method, static_cast<void*>(NULL));
86 method_ = method;
Elliott Hughes68e76522011-10-05 13:22:16 -070087 }
88
Ian Rogers0399dde2012-06-06 17:09:28 -070089 bool Contains(Object** shadow_frame_entry) const {
90 return ((&references_[0] <= shadow_frame_entry) &&
91 (shadow_frame_entry <= (&references_[number_of_references_ - 1])));
Elliott Hughes68e76522011-10-05 13:22:16 -070092 }
93
Ian Rogers0399dde2012-06-06 17:09:28 -070094 void VisitRoots(Heap::RootVisitor* visitor, void* arg) {
95 size_t num_refs = NumberOfReferences();
96 for (size_t j = 0; j < num_refs; j++) {
97 Object* object = GetReference(j);
98 if (object != NULL) {
99 visitor(object, arg);
100 }
101 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700102 }
103
Ian Rogers0399dde2012-06-06 17:09:28 -0700104 // Offset of link within shadow frame
105 static size_t LinkOffset() {
106 return OFFSETOF_MEMBER(ShadowFrame, link_);
107 }
108
109 // Offset of method within shadow frame
110 static size_t MethodOffset() {
111 return OFFSETOF_MEMBER(ShadowFrame, method_);
112 }
113
114 // Offset of dex pc within shadow frame
115 static size_t DexPCOffset() {
116 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_);
117 }
118
119 // Offset of length within shadow frame
120 static size_t NumberOfReferencesOffset() {
121 return OFFSETOF_MEMBER(ShadowFrame, number_of_references_);
122 }
123
124 // Offset of references within shadow frame
125 static size_t ReferencesOffset() {
126 return OFFSETOF_MEMBER(ShadowFrame, references_);
127 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700128
129 private:
Ian Rogers0399dde2012-06-06 17:09:28 -0700130 // ShadowFrame should be allocated by the generated code directly.
131 // We should not create new shadow stack in the runtime support function.
132 ~ShadowFrame() {}
Elliott Hughes68e76522011-10-05 13:22:16 -0700133
Ian Rogers0399dde2012-06-06 17:09:28 -0700134 uint32_t number_of_references_;
135 ShadowFrame* link_;
136 Method* method_;
137 uint32_t dex_pc_;
138 Object* references_[];
Elliott Hughes68e76522011-10-05 13:22:16 -0700139
Ian Rogers0399dde2012-06-06 17:09:28 -0700140 DISALLOW_IMPLICIT_CONSTRUCTORS(ShadowFrame);
Elliott Hughes68e76522011-10-05 13:22:16 -0700141};
142
Ian Rogers0399dde2012-06-06 17:09:28 -0700143// The managed stack is used to record fragments of managed code stacks. Managed code stacks
144// may either be shadow frames or lists of frames using fixed frame sizes. Transition records are
145// necessary for transitions between code using different frame layouts and transitions into native
146// code.
147class PACKED ManagedStack {
148 public:
Ian Rogersca190662012-06-26 15:45:57 -0700149 ManagedStack()
150 : link_(NULL), top_shadow_frame_(NULL), top_quick_frame_(NULL), top_quick_frame_pc_(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700151 void PushManagedStackFragment(ManagedStack* fragment);
152 void PopManagedStackFragment(const ManagedStack& record);
153
154 ManagedStack* GetLink() const {
155 return link_;
156 }
157
158 Method** GetTopQuickFrame() const {
159 return top_quick_frame_;
160 }
161
162 void SetTopQuickFrame(Method** top) {
163 top_quick_frame_ = top;
164 }
165
166 uintptr_t GetTopQuickFramePc() const {
167 return top_quick_frame_pc_;
168 }
169
170 void SetTopQuickFramePc(uintptr_t pc) {
171 top_quick_frame_pc_ = pc;
172 }
173
174 static size_t TopQuickFrameOffset() {
175 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_);
176 }
177
178 static size_t TopQuickFramePcOffset() {
179 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_pc_);
180 }
181
182 ShadowFrame* PushShadowFrame(ShadowFrame* new_top_frame) {
183 ShadowFrame* old_frame = top_shadow_frame_;
184 top_shadow_frame_ = new_top_frame;
185 new_top_frame->SetLink(old_frame);
186 return old_frame;
187 }
188
189 ShadowFrame* PopShadowFrame() {
190 CHECK(top_shadow_frame_ != NULL);
191 ShadowFrame* frame = top_shadow_frame_;
192 top_shadow_frame_ = frame->GetLink();
193 return frame;
194 }
195
196 ShadowFrame* GetTopShadowFrame() const {
197 return top_shadow_frame_;
198 }
199
200 static size_t TopShadowFrameOffset() {
201 return OFFSETOF_MEMBER(ManagedStack, top_shadow_frame_);
202 }
203
204 size_t NumShadowFrameReferences() const;
205
206 bool ShadowFramesContain(Object** shadow_frame_entry) const;
207
208 private:
209 ManagedStack* link_;
210 ShadowFrame* top_shadow_frame_;
211 Method** top_quick_frame_;
212 uintptr_t top_quick_frame_pc_;
213};
214
215class StackVisitor {
216 protected:
217 StackVisitor(const ManagedStack* stack, const std::vector<TraceStackFrame>* trace_stack,
Ian Rogersca190662012-06-26 15:45:57 -0700218 Context* context = NULL)
219 : stack_start_(stack), trace_stack_(trace_stack), cur_shadow_frame_(NULL),
220 cur_quick_frame_(NULL), cur_quick_frame_pc_(0), num_frames_(0), cur_depth_(0),
221 context_(context) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700222
223 public:
224 virtual ~StackVisitor() {}
225
226 // Return 'true' if we should continue to visit more frames, 'false' to stop.
227 virtual bool VisitFrame() = 0;
228
229 void WalkStack(bool include_transitions = false);
230
231 Method* GetMethod() const {
232 if (cur_shadow_frame_ != NULL) {
233 return cur_shadow_frame_->GetMethod();
234 } else if (cur_quick_frame_ != NULL) {
235 return *cur_quick_frame_;
236 } else {
237 return NULL;
238 }
239 }
240
241 bool IsShadowFrame() const {
242 return cur_shadow_frame_ != NULL;
243 }
244
245 uintptr_t LoadCalleeSave(int num, size_t frame_size) const {
246 // Callee saves are held at the top of the frame
247 Method* method = GetMethod();
248 DCHECK(method != NULL);
249 byte* save_addr =
250 reinterpret_cast<byte*>(cur_quick_frame_) + frame_size - ((num + 1) * kPointerSize);
251#if defined(__i386__)
252 save_addr -= kPointerSize; // account for return address
253#endif
254 return *reinterpret_cast<uintptr_t*>(save_addr);
255 }
256
257 uint32_t GetDexPc() const;
258
259 // Gets the height of the stack in the managed stack frames, including transitions.
260 size_t GetFrameHeight() {
261 return GetNumFrames() - cur_depth_;
262 }
263
264 // Get a frame ID where 0 is a special value.
265 size_t GetFrameId() {
266 return GetFrameHeight() + 1;
267 }
268
269 size_t GetNumFrames() {
270 if (num_frames_ == 0) {
271 num_frames_ = ComputeNumFrames();
272 }
273 return num_frames_;
274 }
275
276 uint32_t GetVReg(Method* m, int vreg) const;
277
278 void SetVReg(Method* m, int vreg, uint32_t new_value);
279
280 uintptr_t GetGPR(uint32_t reg) const;
281
282 uint32_t GetVReg(const DexFile::CodeItem* code_item, uint32_t core_spills,
283 uint32_t fp_spills, size_t frame_size, int vreg) const {
284 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
285 byte* vreg_addr = reinterpret_cast<byte*>(GetCurrentQuickFrame()) + offset;
286 return *reinterpret_cast<uint32_t*>(vreg_addr);
287 }
288
289 uintptr_t GetReturnPc() const;
290
291 void SetReturnPc(uintptr_t new_ret_pc);
292
293 /*
294 * Return sp-relative offset for a Dalvik virtual register, compiler
295 * spill or Method* in bytes using Method*.
296 * Note that (reg >= 0) refers to a Dalvik register, (reg == -2)
297 * denotes Method* and (reg <= -3) denotes a compiler temp.
298 *
299 * +------------------------+
300 * | IN[ins-1] | {Note: resides in caller's frame}
301 * | . |
302 * | IN[0] |
303 * | caller's Method* |
304 * +========================+ {Note: start of callee's frame}
305 * | core callee-save spill | {variable sized}
306 * +------------------------+
307 * | fp callee-save spill |
308 * +------------------------+
309 * | filler word | {For compatibility, if V[locals-1] used as wide
310 * +------------------------+
311 * | V[locals-1] |
312 * | V[locals-2] |
313 * | . |
314 * | . | ... (reg == 2)
315 * | V[1] | ... (reg == 1)
316 * | V[0] | ... (reg == 0) <---- "locals_start"
317 * +------------------------+
318 * | Compiler temps | ... (reg == -2)
319 * | | ... (reg == -3)
320 * | | ... (reg == -4)
321 * +------------------------+
322 * | stack alignment padding| {0 to (kStackAlignWords-1) of padding}
323 * +------------------------+
324 * | OUT[outs-1] |
325 * | OUT[outs-2] |
326 * | . |
327 * | OUT[0] |
328 * | curMethod* | ... (reg == -1) <<== sp, 16-byte aligned
329 * +========================+
330 */
331 static int GetVRegOffset(const DexFile::CodeItem* code_item,
332 uint32_t core_spills, uint32_t fp_spills,
333 size_t frame_size, int reg) {
334 DCHECK_EQ(frame_size & (kStackAlignment - 1), 0U);
335 int num_spills = __builtin_popcount(core_spills) + __builtin_popcount(fp_spills) + 1; // Filler.
336 int num_ins = code_item->ins_size_;
337 int num_regs = code_item->registers_size_ - num_ins;
338 int locals_start = frame_size - ((num_spills + num_regs) * sizeof(uint32_t));
339 if (reg == -2) {
340 return 0; // Method*
341 } else if (reg <= -3) {
342 return locals_start - ((reg + 1) * sizeof(uint32_t)); // Compiler temp.
343 } else if (reg < num_regs) {
344 return locals_start + (reg * sizeof(uint32_t)); // Dalvik local reg.
345 } else {
346 return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + sizeof(uint32_t); // Dalvik in.
347 }
348 }
349
350 uintptr_t GetCurrentQuickFramePc() const {
351 return cur_quick_frame_pc_;
352 }
353
354 Method** GetCurrentQuickFrame() const {
355 return cur_quick_frame_;
356 }
357
358 ShadowFrame* GetCurrentShadowFrame() const {
359 return cur_shadow_frame_;
360 }
361
362 private:
363 size_t ComputeNumFrames() const;
364
365 TraceStackFrame GetTraceStackFrame(uint32_t depth) const {
366 return trace_stack_->at(trace_stack_->size() - depth - 1);
367 }
368
369 void SanityCheckFrame();
370
371 const ManagedStack* const stack_start_;
372 const std::vector<TraceStackFrame>* const trace_stack_;
373 ShadowFrame* cur_shadow_frame_;
374 Method** cur_quick_frame_;
375 uintptr_t cur_quick_frame_pc_;
376 // Lazily computed, number of frames in the stack.
377 size_t num_frames_;
378 // Depth of the frame we're currently at.
379 size_t cur_depth_;
380 protected:
381 Context* const context_;
382};
383
384static inline uintptr_t AdjustQuickFramePcForDexPcComputation(uintptr_t pc) {
385 // Quick methods record a mapping from quick PCs to Dex PCs at the beginning of the code for
386 // each dex instruction. When walking the stack, the return PC will be set to the instruction
387 // following call which will likely be the start of the next dex instruction. Adjust the PC
388 // for these cases by 2 bytes in case the return PC also has the thumb bit set.
389 if (pc > 0) { pc -= 2; }
390 return pc;
391}
392
Elliott Hughes68e76522011-10-05 13:22:16 -0700393} // namespace art
394
395#endif // ART_SRC_STACK_H_