blob: 9795a77cbecb2656bec893c3d54e0542ca6361a6 [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 -070027void ManagedStack::PushManagedStackFragment(ManagedStack* fragment) {
28 // Copy this top fragment into given fragment.
29 memcpy(fragment, this, sizeof(ManagedStack));
30 // Clear this fragment, which has become the top.
31 memset(this, 0, sizeof(ManagedStack));
32 // Link our top fragment onto the given fragment.
33 link_ = fragment;
34}
35
36void ManagedStack::PopManagedStackFragment(const ManagedStack& fragment) {
37 DCHECK(&fragment == link_);
38 // Copy this given fragment back to the top.
39 memcpy(this, &fragment, sizeof(ManagedStack));
40}
41
42size_t ManagedStack::NumShadowFrameReferences() const {
43 size_t count = 0;
44 for (const ManagedStack* current_fragment = this; current_fragment != NULL;
45 current_fragment = current_fragment->GetLink()) {
46 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != NULL;
47 current_frame = current_frame->GetLink()) {
48 count += current_frame->NumberOfReferences();
49 }
50 }
51 return count;
52}
53
54bool ManagedStack::ShadowFramesContain(Object** shadow_frame_entry) const {
55 for (const ManagedStack* current_fragment = this; current_fragment != NULL;
56 current_fragment = current_fragment->GetLink()) {
57 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != NULL;
58 current_frame = current_frame->GetLink()) {
59 if (current_frame->Contains(shadow_frame_entry)) {
60 return true;
61 }
62 }
63 }
64 return false;
65}
66
67uint32_t StackVisitor::GetDexPc() const {
68 if (cur_shadow_frame_ != NULL) {
69 return cur_shadow_frame_->GetDexPC();
70 } else if (cur_quick_frame_ != NULL) {
71 return GetMethod()->ToDexPC(AdjustQuickFramePcForDexPcComputation(cur_quick_frame_pc_));
72 } else {
73 return 0;
74 }
75}
76
77uint32_t StackVisitor::GetVReg(Method* m, int vreg) const {
Ian Rogers0ec569a2012-07-01 16:43:46 -070078 if (cur_quick_frame_ != NULL) {
79 DCHECK(context_ != NULL); // You can't reliably read registers without a context.
80 DCHECK(m == GetMethod());
81 uint32_t core_spills = m->GetCoreSpillMask();
82 const VmapTable vmap_table(m->GetVmapTableRaw());
83 uint32_t vmap_offset;
84 // TODO: IsInContext stops before spotting floating point registers.
85 if (vmap_table.IsInContext(vreg, vmap_offset)) {
86 // Compute the register we need to load from the context.
87 uint32_t spill_mask = core_spills;
88 CHECK_LT(vmap_offset, static_cast<uint32_t>(__builtin_popcount(spill_mask)));
89 uint32_t matches = 0;
90 uint32_t spill_shifts = 0;
91 while (matches != (vmap_offset + 1)) {
92 DCHECK_NE(spill_mask, 0u);
93 matches += spill_mask & 1; // Add 1 if the low bit is set.
94 spill_mask >>= 1;
95 spill_shifts++;
96 }
97 spill_shifts--; // Wind back one as we want the last match.
98 return GetGPR(spill_shifts);
99 } else {
100 const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
101 DCHECK(code_item != NULL) << PrettyMethod(m); // Can't be NULL or how would we compile its instructions?
102 uint32_t fp_spills = m->GetFpSpillMask();
103 size_t frame_size = m->GetFrameSizeInBytes();
104 return GetVReg(cur_quick_frame_, code_item, core_spills, fp_spills, frame_size, vreg);
Ian Rogers0399dde2012-06-06 17:09:28 -0700105 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700106 } else {
Ian Rogers0ec569a2012-07-01 16:43:46 -0700107 LOG(FATAL) << "Unimplemented - shadow frame GetVReg";
108 return 0; // Keep GCC happy.
Ian Rogers0399dde2012-06-06 17:09:28 -0700109 }
110}
111
112void StackVisitor::SetVReg(Method* m, int vreg, uint32_t new_value) {
Ian Rogers0ec569a2012-07-01 16:43:46 -0700113 if (cur_quick_frame_ != NULL) {
114 DCHECK(context_ != NULL); // You can't reliably write registers without a context.
115 DCHECK(m == GetMethod());
116 const VmapTable vmap_table(m->GetVmapTableRaw());
117 uint32_t vmap_offset;
118 // TODO: IsInContext stops before spotting floating point registers.
119 if (vmap_table.IsInContext(vreg, vmap_offset)) {
120 UNIMPLEMENTED(FATAL);
121 }
122 const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
123 DCHECK(code_item != NULL) << PrettyMethod(m); // Can't be NULL or how would we compile its instructions?
124 uint32_t core_spills = m->GetCoreSpillMask();
125 uint32_t fp_spills = m->GetFpSpillMask();
126 size_t frame_size = m->GetFrameSizeInBytes();
127 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
128 byte* vreg_addr = reinterpret_cast<byte*>(GetCurrentQuickFrame()) + offset;
129 *reinterpret_cast<uint32_t*>(vreg_addr) = new_value;
130 } else {
131 LOG(FATAL) << "Unimplemented - shadow frame SetVReg";
Ian Rogers0399dde2012-06-06 17:09:28 -0700132 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700133}
134
135uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
Ian Rogers0ec569a2012-07-01 16:43:46 -0700136 DCHECK (cur_quick_frame_ != NULL) << "This is a quick frame routine";
Ian Rogers0399dde2012-06-06 17:09:28 -0700137 return context_->GetGPR(reg);
138}
139
140uintptr_t StackVisitor::GetReturnPc() const {
141 Method** sp = GetCurrentQuickFrame();
142 CHECK(sp != NULL);
143 byte* pc_addr = reinterpret_cast<byte*>(sp) + GetMethod()->GetReturnPcOffsetInBytes();
144 return *reinterpret_cast<uintptr_t*>(pc_addr);
145}
146
147void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
148 Method** sp = GetCurrentQuickFrame();
149 CHECK(sp != NULL);
150 byte* pc_addr = reinterpret_cast<byte*>(sp) + GetMethod()->GetReturnPcOffsetInBytes();
151 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc;
152}
153
154size_t StackVisitor::ComputeNumFrames() const {
155 struct NumFramesVisitor : public StackVisitor {
156 explicit NumFramesVisitor(const ManagedStack* stack,
Ian Rogersca190662012-06-26 15:45:57 -0700157 const std::vector<TraceStackFrame>* trace_stack)
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700158 : StackVisitor(stack, trace_stack, NULL), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700159
160 virtual bool VisitFrame() {
161 frames++;
162 return true;
163 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700164
Ian Rogers0399dde2012-06-06 17:09:28 -0700165 size_t frames;
166 };
167
168 NumFramesVisitor visitor(stack_start_, trace_stack_);
169 visitor.WalkStack(true);
170 return visitor.frames;
171}
172
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700173void StackVisitor::SanityCheckFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700174#ifndef NDEBUG
175 Method* method = GetMethod();
176 CHECK(method->GetClass() == Method::GetMethodClass() ||
177 method->GetClass() == Method::GetConstructorClass());
178 if (cur_quick_frame_ != NULL) {
179 method->AssertPcIsWithinCode(AdjustQuickFramePcForDexPcComputation(cur_quick_frame_pc_));
180 // Frame sanity.
181 size_t frame_size = method->GetFrameSizeInBytes();
182 CHECK_NE(frame_size, 0u);
183 CHECK_LT(frame_size, 1024u);
184 size_t return_pc_offset = method->GetReturnPcOffsetInBytes();
185 CHECK_LT(return_pc_offset, frame_size);
186 }
187#endif
188}
189
190void StackVisitor::WalkStack(bool include_transitions) {
191 bool method_tracing_active = Runtime::Current()->IsMethodTracingActive();
192 uint32_t trace_stack_depth = 0;
193 for (const ManagedStack* current_fragment = stack_start_; current_fragment != NULL;
194 current_fragment = current_fragment->GetLink()) {
195 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
196 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
197 cur_quick_frame_pc_ = current_fragment->GetTopQuickFramePc();
198 if (cur_quick_frame_ != NULL) { // Handle quick stack frames.
199 // Can't be both a shadow and a quick fragment.
200 DCHECK(current_fragment->GetTopShadowFrame() == NULL);
201 Method* method = *cur_quick_frame_;
202 do {
203 SanityCheckFrame();
204 bool should_continue = VisitFrame();
205 if (UNLIKELY(!should_continue)) {
206 return;
207 }
208 if (context_ != NULL) {
209 context_->FillCalleeSaves(*this);
210 }
211 size_t frame_size = method->GetFrameSizeInBytes();
212 // Compute PC for next stack frame from return PC.
213 size_t return_pc_offset = method->GetReturnPcOffsetInBytes();
214 byte* return_pc_addr = reinterpret_cast<byte*>(cur_quick_frame_) + return_pc_offset;
215 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
216 if (UNLIKELY(method_tracing_active)) {
217 // While profiling, the return pc is restored from the side stack, except when walking
218 // the stack for an exception where the side stack will be unwound in VisitFrame.
219 // TODO: stop using include_transitions as a proxy for is this the catch block visitor.
220 if (IsTraceExitPc(return_pc) && !include_transitions) {
221 // TODO: unify trace and managed stack.
222 TraceStackFrame trace_frame = GetTraceStackFrame(trace_stack_depth);
223 trace_stack_depth++;
224 CHECK(trace_frame.method_ == GetMethod()) << "Excepted: " << PrettyMethod(method)
225 << " Found: " << PrettyMethod(GetMethod());
226 return_pc = trace_frame.return_pc_;
227 }
228 }
229 cur_quick_frame_pc_ = return_pc;
230 byte* next_frame = reinterpret_cast<byte*>(cur_quick_frame_) + frame_size;
231 cur_quick_frame_ = reinterpret_cast<Method**>(next_frame);
232 cur_depth_++;
233 method = *cur_quick_frame_;
234 } while (method != NULL);
235 } else if (cur_shadow_frame_ != NULL) {
236 do {
237 SanityCheckFrame();
238 bool should_continue = VisitFrame();
239 if (UNLIKELY(!should_continue)) {
240 return;
241 }
242 cur_depth_++;
243 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
244 } while(cur_shadow_frame_ != NULL);
245 }
246 cur_depth_++;
247 if (include_transitions) {
248 bool should_continue = VisitFrame();
249 if (!should_continue) {
250 return;
251 }
252 }
253 }
254}
255
Elliott Hughes68e76522011-10-05 13:22:16 -0700256} // namespace art