blob: f7126526be53b868358860723a0a720d1bbfefe0 [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
Ian Rogers2bcb4a42012-11-08 10:39:18 -080067uint32_t StackVisitor::GetVReg(AbstractMethod* m, uint16_t vreg, VRegKind kind) const {
Ian Rogers0ec569a2012-07-01 16:43:46 -070068 if (cur_quick_frame_ != NULL) {
69 DCHECK(context_ != NULL); // You can't reliably read registers without a context.
Ian Rogers2bcb4a42012-11-08 10:39:18 -080070 DCHECK(m == GetMethod());
Ian Rogers0ec569a2012-07-01 16:43:46 -070071 const VmapTable vmap_table(m->GetVmapTableRaw());
72 uint32_t vmap_offset;
73 // TODO: IsInContext stops before spotting floating point registers.
Ian Rogers2bcb4a42012-11-08 10:39:18 -080074 if (vmap_table.IsInContext(vreg, vmap_offset, kind)) {
75 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
76 uint32_t spill_mask = is_float ? m->GetFpSpillMask()
77 : m->GetCoreSpillMask();
78 return GetGPR(vmap_table.ComputeRegister(spill_mask, vmap_offset, kind));
Ian Rogers0ec569a2012-07-01 16:43:46 -070079 } else {
80 const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
81 DCHECK(code_item != NULL) << PrettyMethod(m); // Can't be NULL or how would we compile its instructions?
Ian Rogers0ec569a2012-07-01 16:43:46 -070082 size_t frame_size = m->GetFrameSizeInBytes();
Ian Rogers2bcb4a42012-11-08 10:39:18 -080083 return GetVReg(cur_quick_frame_, code_item, m->GetCoreSpillMask(), m->GetFpSpillMask(),
84 frame_size, vreg);
Ian Rogers0399dde2012-06-06 17:09:28 -070085 }
Ian Rogers0399dde2012-06-06 17:09:28 -070086 } else {
TDYa1278e950c12012-11-02 09:58:19 -070087 return cur_shadow_frame_->GetVReg(vreg);
Ian Rogers0399dde2012-06-06 17:09:28 -070088 }
89}
90
Ian Rogers2bcb4a42012-11-08 10:39:18 -080091void StackVisitor::SetVReg(AbstractMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind) {
Ian Rogers0ec569a2012-07-01 16:43:46 -070092 if (cur_quick_frame_ != NULL) {
93 DCHECK(context_ != NULL); // You can't reliably write registers without a context.
94 DCHECK(m == GetMethod());
95 const VmapTable vmap_table(m->GetVmapTableRaw());
96 uint32_t vmap_offset;
97 // TODO: IsInContext stops before spotting floating point registers.
Ian Rogers2bcb4a42012-11-08 10:39:18 -080098 if (vmap_table.IsInContext(vreg, vmap_offset, kind)) {
Ian Rogers0ec569a2012-07-01 16:43:46 -070099 UNIMPLEMENTED(FATAL);
100 }
101 const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
102 DCHECK(code_item != NULL) << PrettyMethod(m); // Can't be NULL or how would we compile its instructions?
103 uint32_t core_spills = m->GetCoreSpillMask();
104 uint32_t fp_spills = m->GetFpSpillMask();
105 size_t frame_size = m->GetFrameSizeInBytes();
106 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
107 byte* vreg_addr = reinterpret_cast<byte*>(GetCurrentQuickFrame()) + offset;
108 *reinterpret_cast<uint32_t*>(vreg_addr) = new_value;
109 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800110 return cur_shadow_frame_->SetVReg(vreg, new_value);
Ian Rogers0399dde2012-06-06 17:09:28 -0700111 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700112}
113
114uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
Ian Rogers0ec569a2012-07-01 16:43:46 -0700115 DCHECK (cur_quick_frame_ != NULL) << "This is a quick frame routine";
Ian Rogers0399dde2012-06-06 17:09:28 -0700116 return context_->GetGPR(reg);
117}
118
119uintptr_t StackVisitor::GetReturnPc() const {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700120 AbstractMethod** sp = GetCurrentQuickFrame();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800121 DCHECK(sp != NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700122 byte* pc_addr = reinterpret_cast<byte*>(sp) + GetMethod()->GetReturnPcOffsetInBytes();
123 return *reinterpret_cast<uintptr_t*>(pc_addr);
124}
125
126void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700127 AbstractMethod** sp = GetCurrentQuickFrame();
Ian Rogers0399dde2012-06-06 17:09:28 -0700128 CHECK(sp != NULL);
129 byte* pc_addr = reinterpret_cast<byte*>(sp) + GetMethod()->GetReturnPcOffsetInBytes();
130 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc;
131}
132
133size_t StackVisitor::ComputeNumFrames() const {
134 struct NumFramesVisitor : public StackVisitor {
135 explicit NumFramesVisitor(const ManagedStack* stack,
jeffhao725a9572012-11-13 18:20:12 -0800136 const std::vector<InstrumentationStackFrame>* instrumentation_stack)
137 : StackVisitor(stack, instrumentation_stack, NULL), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700138
139 virtual bool VisitFrame() {
140 frames++;
141 return true;
142 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700143
Ian Rogers0399dde2012-06-06 17:09:28 -0700144 size_t frames;
145 };
146
jeffhao725a9572012-11-13 18:20:12 -0800147 NumFramesVisitor visitor(stack_start_, instrumentation_stack_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700148 visitor.WalkStack(true);
149 return visitor.frames;
150}
151
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700152void StackVisitor::SanityCheckFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700153#ifndef NDEBUG
Mathieu Chartier66f19252012-09-18 08:57:04 -0700154 AbstractMethod* method = GetMethod();
155 CHECK(method->GetClass() == AbstractMethod::GetMethodClass() ||
156 method->GetClass() == AbstractMethod::GetConstructorClass());
Ian Rogers0399dde2012-06-06 17:09:28 -0700157 if (cur_quick_frame_ != NULL) {
buzbee8320f382012-09-11 16:29:42 -0700158 method->AssertPcIsWithinCode(cur_quick_frame_pc_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700159 // Frame sanity.
160 size_t frame_size = method->GetFrameSizeInBytes();
161 CHECK_NE(frame_size, 0u);
162 CHECK_LT(frame_size, 1024u);
163 size_t return_pc_offset = method->GetReturnPcOffsetInBytes();
164 CHECK_LT(return_pc_offset, frame_size);
165 }
166#endif
167}
168
169void StackVisitor::WalkStack(bool include_transitions) {
170 bool method_tracing_active = Runtime::Current()->IsMethodTracingActive();
jeffhao725a9572012-11-13 18:20:12 -0800171 uint32_t instrumentation_stack_depth = 0;
Ian Rogers0399dde2012-06-06 17:09:28 -0700172 for (const ManagedStack* current_fragment = stack_start_; current_fragment != NULL;
173 current_fragment = current_fragment->GetLink()) {
174 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
175 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
176 cur_quick_frame_pc_ = current_fragment->GetTopQuickFramePc();
177 if (cur_quick_frame_ != NULL) { // Handle quick stack frames.
178 // Can't be both a shadow and a quick fragment.
179 DCHECK(current_fragment->GetTopShadowFrame() == NULL);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700180 AbstractMethod* method = *cur_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700181 do {
182 SanityCheckFrame();
183 bool should_continue = VisitFrame();
184 if (UNLIKELY(!should_continue)) {
185 return;
186 }
187 if (context_ != NULL) {
188 context_->FillCalleeSaves(*this);
189 }
190 size_t frame_size = method->GetFrameSizeInBytes();
191 // Compute PC for next stack frame from return PC.
192 size_t return_pc_offset = method->GetReturnPcOffsetInBytes();
193 byte* return_pc_addr = reinterpret_cast<byte*>(cur_quick_frame_) + return_pc_offset;
194 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
195 if (UNLIKELY(method_tracing_active)) {
196 // While profiling, the return pc is restored from the side stack, except when walking
197 // the stack for an exception where the side stack will be unwound in VisitFrame.
198 // TODO: stop using include_transitions as a proxy for is this the catch block visitor.
jeffhao725a9572012-11-13 18:20:12 -0800199 if (IsInstrumentationExitPc(return_pc) && !include_transitions) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700200 // TODO: unify trace and managed stack.
jeffhao725a9572012-11-13 18:20:12 -0800201 InstrumentationStackFrame instrumentation_frame = GetInstrumentationStackFrame(instrumentation_stack_depth);
202 instrumentation_stack_depth++;
203 CHECK(instrumentation_frame.method_ == GetMethod()) << "Excepted: " << PrettyMethod(method)
Ian Rogers0399dde2012-06-06 17:09:28 -0700204 << " Found: " << PrettyMethod(GetMethod());
jeffhao725a9572012-11-13 18:20:12 -0800205 return_pc = instrumentation_frame.return_pc_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700206 }
207 }
208 cur_quick_frame_pc_ = return_pc;
209 byte* next_frame = reinterpret_cast<byte*>(cur_quick_frame_) + frame_size;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700210 cur_quick_frame_ = reinterpret_cast<AbstractMethod**>(next_frame);
Ian Rogers0399dde2012-06-06 17:09:28 -0700211 cur_depth_++;
212 method = *cur_quick_frame_;
213 } while (method != NULL);
214 } else if (cur_shadow_frame_ != NULL) {
215 do {
216 SanityCheckFrame();
217 bool should_continue = VisitFrame();
218 if (UNLIKELY(!should_continue)) {
219 return;
220 }
221 cur_depth_++;
222 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
223 } while(cur_shadow_frame_ != NULL);
224 }
225 cur_depth_++;
226 if (include_transitions) {
227 bool should_continue = VisitFrame();
228 if (!should_continue) {
229 return;
230 }
231 }
232 }
233}
234
Elliott Hughes68e76522011-10-05 13:22:16 -0700235} // namespace art