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