blob: 7ec57b41b25c0effe8beaf55a9ba1b6e88ebf3cb [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#include "stack.h"
18
19#include "compiler.h"
Ian Rogers0399dde2012-06-06 17:09:28 -070020#include "oat/runtime/context.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070021#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080022#include "object_utils.h"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070023#include "thread_list.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070024
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080025namespace art {
26
Ian Rogers0399dde2012-06-06 17:09:28 -070027size_t ManagedStack::NumShadowFrameReferences() const {
28 size_t count = 0;
29 for (const ManagedStack* current_fragment = this; current_fragment != NULL;
30 current_fragment = current_fragment->GetLink()) {
31 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != NULL;
32 current_frame = current_frame->GetLink()) {
33 count += current_frame->NumberOfReferences();
34 }
35 }
36 return count;
37}
38
39bool ManagedStack::ShadowFramesContain(Object** shadow_frame_entry) const {
40 for (const ManagedStack* current_fragment = this; current_fragment != NULL;
41 current_fragment = current_fragment->GetLink()) {
42 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != NULL;
43 current_frame = current_frame->GetLink()) {
44 if (current_frame->Contains(shadow_frame_entry)) {
45 return true;
46 }
47 }
48 }
49 return false;
50}
51
52uint32_t StackVisitor::GetDexPc() const {
53 if (cur_shadow_frame_ != NULL) {
54 return cur_shadow_frame_->GetDexPC();
55 } else if (cur_quick_frame_ != NULL) {
Ian Rogers0c7abda2012-09-19 13:33:42 -070056 return GetMethod()->ToDexPc(cur_quick_frame_pc_);
Ian Rogers0399dde2012-06-06 17:09:28 -070057 } else {
58 return 0;
59 }
60}
61
Ian Rogers0c7abda2012-09-19 13:33:42 -070062size_t StackVisitor::GetNativePcOffset() const {
63 DCHECK(!IsShadowFrame());
64 return GetMethod()->NativePcOffset(cur_quick_frame_pc_);
65}
66
67
Mathieu Chartier66f19252012-09-18 08:57:04 -070068uint32_t StackVisitor::GetVReg(AbstractMethod* m, int vreg) const {
Ian Rogers0ec569a2012-07-01 16:43:46 -070069 if (cur_quick_frame_ != NULL) {
70 DCHECK(context_ != NULL); // You can't reliably read registers without a context.
71 DCHECK(m == GetMethod());
72 uint32_t core_spills = m->GetCoreSpillMask();
73 const VmapTable vmap_table(m->GetVmapTableRaw());
74 uint32_t vmap_offset;
75 // TODO: IsInContext stops before spotting floating point registers.
76 if (vmap_table.IsInContext(vreg, vmap_offset)) {
77 // Compute the register we need to load from the context.
78 uint32_t spill_mask = core_spills;
79 CHECK_LT(vmap_offset, static_cast<uint32_t>(__builtin_popcount(spill_mask)));
80 uint32_t matches = 0;
81 uint32_t spill_shifts = 0;
82 while (matches != (vmap_offset + 1)) {
83 DCHECK_NE(spill_mask, 0u);
84 matches += spill_mask & 1; // Add 1 if the low bit is set.
85 spill_mask >>= 1;
86 spill_shifts++;
87 }
88 spill_shifts--; // Wind back one as we want the last match.
89 return GetGPR(spill_shifts);
90 } else {
91 const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
92 DCHECK(code_item != NULL) << PrettyMethod(m); // Can't be NULL or how would we compile its instructions?
93 uint32_t fp_spills = m->GetFpSpillMask();
94 size_t frame_size = m->GetFrameSizeInBytes();
95 return GetVReg(cur_quick_frame_, code_item, core_spills, fp_spills, frame_size, vreg);
Ian Rogers0399dde2012-06-06 17:09:28 -070096 }
Ian Rogers0399dde2012-06-06 17:09:28 -070097 } else {
Ian Rogers0ec569a2012-07-01 16:43:46 -070098 LOG(FATAL) << "Unimplemented - shadow frame GetVReg";
99 return 0; // Keep GCC happy.
Ian Rogers0399dde2012-06-06 17:09:28 -0700100 }
101}
102
Mathieu Chartier66f19252012-09-18 08:57:04 -0700103void StackVisitor::SetVReg(AbstractMethod* m, int vreg, uint32_t new_value) {
Ian Rogers0ec569a2012-07-01 16:43:46 -0700104 if (cur_quick_frame_ != NULL) {
105 DCHECK(context_ != NULL); // You can't reliably write registers without a context.
106 DCHECK(m == GetMethod());
107 const VmapTable vmap_table(m->GetVmapTableRaw());
108 uint32_t vmap_offset;
109 // TODO: IsInContext stops before spotting floating point registers.
110 if (vmap_table.IsInContext(vreg, vmap_offset)) {
111 UNIMPLEMENTED(FATAL);
112 }
113 const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
114 DCHECK(code_item != NULL) << PrettyMethod(m); // Can't be NULL or how would we compile its instructions?
115 uint32_t core_spills = m->GetCoreSpillMask();
116 uint32_t fp_spills = m->GetFpSpillMask();
117 size_t frame_size = m->GetFrameSizeInBytes();
118 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
119 byte* vreg_addr = reinterpret_cast<byte*>(GetCurrentQuickFrame()) + offset;
120 *reinterpret_cast<uint32_t*>(vreg_addr) = new_value;
121 } else {
122 LOG(FATAL) << "Unimplemented - shadow frame SetVReg";
Ian Rogers0399dde2012-06-06 17:09:28 -0700123 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700124}
125
126uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
Ian Rogers0ec569a2012-07-01 16:43:46 -0700127 DCHECK (cur_quick_frame_ != NULL) << "This is a quick frame routine";
Ian Rogers0399dde2012-06-06 17:09:28 -0700128 return context_->GetGPR(reg);
129}
130
131uintptr_t StackVisitor::GetReturnPc() const {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700132 AbstractMethod** sp = GetCurrentQuickFrame();
Ian Rogers0399dde2012-06-06 17:09:28 -0700133 CHECK(sp != NULL);
134 byte* pc_addr = reinterpret_cast<byte*>(sp) + GetMethod()->GetReturnPcOffsetInBytes();
135 return *reinterpret_cast<uintptr_t*>(pc_addr);
136}
137
138void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700139 AbstractMethod** sp = GetCurrentQuickFrame();
Ian Rogers0399dde2012-06-06 17:09:28 -0700140 CHECK(sp != NULL);
141 byte* pc_addr = reinterpret_cast<byte*>(sp) + GetMethod()->GetReturnPcOffsetInBytes();
142 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc;
143}
144
145size_t StackVisitor::ComputeNumFrames() const {
146 struct NumFramesVisitor : public StackVisitor {
147 explicit NumFramesVisitor(const ManagedStack* stack,
Ian Rogersca190662012-06-26 15:45:57 -0700148 const std::vector<TraceStackFrame>* trace_stack)
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700149 : StackVisitor(stack, trace_stack, NULL), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700150
151 virtual bool VisitFrame() {
152 frames++;
153 return true;
154 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700155
Ian Rogers0399dde2012-06-06 17:09:28 -0700156 size_t frames;
157 };
158
159 NumFramesVisitor visitor(stack_start_, trace_stack_);
160 visitor.WalkStack(true);
161 return visitor.frames;
162}
163
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700164void StackVisitor::SanityCheckFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700165#ifndef NDEBUG
Mathieu Chartier66f19252012-09-18 08:57:04 -0700166 AbstractMethod* method = GetMethod();
167 CHECK(method->GetClass() == AbstractMethod::GetMethodClass() ||
168 method->GetClass() == AbstractMethod::GetConstructorClass());
Ian Rogers0399dde2012-06-06 17:09:28 -0700169 if (cur_quick_frame_ != NULL) {
buzbee8320f382012-09-11 16:29:42 -0700170 method->AssertPcIsWithinCode(cur_quick_frame_pc_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700171 // Frame sanity.
172 size_t frame_size = method->GetFrameSizeInBytes();
173 CHECK_NE(frame_size, 0u);
174 CHECK_LT(frame_size, 1024u);
175 size_t return_pc_offset = method->GetReturnPcOffsetInBytes();
176 CHECK_LT(return_pc_offset, frame_size);
177 }
178#endif
179}
180
181void StackVisitor::WalkStack(bool include_transitions) {
182 bool method_tracing_active = Runtime::Current()->IsMethodTracingActive();
183 uint32_t trace_stack_depth = 0;
184 for (const ManagedStack* current_fragment = stack_start_; current_fragment != NULL;
185 current_fragment = current_fragment->GetLink()) {
186 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
187 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
188 cur_quick_frame_pc_ = current_fragment->GetTopQuickFramePc();
189 if (cur_quick_frame_ != NULL) { // Handle quick stack frames.
190 // Can't be both a shadow and a quick fragment.
191 DCHECK(current_fragment->GetTopShadowFrame() == NULL);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700192 AbstractMethod* method = *cur_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700193 do {
194 SanityCheckFrame();
195 bool should_continue = VisitFrame();
196 if (UNLIKELY(!should_continue)) {
197 return;
198 }
199 if (context_ != NULL) {
200 context_->FillCalleeSaves(*this);
201 }
202 size_t frame_size = method->GetFrameSizeInBytes();
203 // Compute PC for next stack frame from return PC.
204 size_t return_pc_offset = method->GetReturnPcOffsetInBytes();
205 byte* return_pc_addr = reinterpret_cast<byte*>(cur_quick_frame_) + return_pc_offset;
206 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
207 if (UNLIKELY(method_tracing_active)) {
208 // While profiling, the return pc is restored from the side stack, except when walking
209 // the stack for an exception where the side stack will be unwound in VisitFrame.
210 // TODO: stop using include_transitions as a proxy for is this the catch block visitor.
211 if (IsTraceExitPc(return_pc) && !include_transitions) {
212 // TODO: unify trace and managed stack.
213 TraceStackFrame trace_frame = GetTraceStackFrame(trace_stack_depth);
214 trace_stack_depth++;
215 CHECK(trace_frame.method_ == GetMethod()) << "Excepted: " << PrettyMethod(method)
216 << " Found: " << PrettyMethod(GetMethod());
217 return_pc = trace_frame.return_pc_;
218 }
219 }
220 cur_quick_frame_pc_ = return_pc;
221 byte* next_frame = reinterpret_cast<byte*>(cur_quick_frame_) + frame_size;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700222 cur_quick_frame_ = reinterpret_cast<AbstractMethod**>(next_frame);
Ian Rogers0399dde2012-06-06 17:09:28 -0700223 cur_depth_++;
224 method = *cur_quick_frame_;
225 } while (method != NULL);
226 } else if (cur_shadow_frame_ != NULL) {
227 do {
228 SanityCheckFrame();
229 bool should_continue = VisitFrame();
230 if (UNLIKELY(!should_continue)) {
231 return;
232 }
233 cur_depth_++;
234 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
235 } while(cur_shadow_frame_ != NULL);
236 }
237 cur_depth_++;
238 if (include_transitions) {
239 bool should_continue = VisitFrame();
240 if (!should_continue) {
241 return;
242 }
243 }
244 }
245}
246
Elliott Hughes68e76522011-10-05 13:22:16 -0700247} // namespace art