blob: be6fe458dec6d5b2c77f41d51bd49e812094bec0 [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
TDYa127ce4cc0d2012-11-18 16:59:53 -080027size_t ManagedStack::NumJniShadowFrameReferences() const {
Ian Rogers0399dde2012-06-06 17:09:28 -070028 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()) {
TDYa127ce4cc0d2012-11-18 16:59:53 -080033 if (current_frame->GetMethod()->IsNative()) {
34 // The JNI ShadowFrame only contains references. (For indirect reference.)
35 count += current_frame->NumberOfVRegs();
36 }
Ian Rogers0399dde2012-06-06 17:09:28 -070037 }
38 }
39 return count;
40}
41
42bool ManagedStack::ShadowFramesContain(Object** shadow_frame_entry) const {
43 for (const ManagedStack* current_fragment = this; current_fragment != NULL;
44 current_fragment = current_fragment->GetLink()) {
45 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != NULL;
46 current_frame = current_frame->GetLink()) {
47 if (current_frame->Contains(shadow_frame_entry)) {
48 return true;
49 }
50 }
51 }
52 return false;
53}
54
55uint32_t StackVisitor::GetDexPc() const {
56 if (cur_shadow_frame_ != NULL) {
57 return cur_shadow_frame_->GetDexPC();
58 } else if (cur_quick_frame_ != NULL) {
Ian Rogers0c7abda2012-09-19 13:33:42 -070059 return GetMethod()->ToDexPc(cur_quick_frame_pc_);
Ian Rogers0399dde2012-06-06 17:09:28 -070060 } else {
61 return 0;
62 }
63}
64
Ian Rogers0c7abda2012-09-19 13:33:42 -070065size_t StackVisitor::GetNativePcOffset() const {
66 DCHECK(!IsShadowFrame());
67 return GetMethod()->NativePcOffset(cur_quick_frame_pc_);
68}
69
Ian Rogers2bcb4a42012-11-08 10:39:18 -080070uint32_t StackVisitor::GetVReg(AbstractMethod* m, uint16_t vreg, VRegKind kind) const {
Ian Rogers0ec569a2012-07-01 16:43:46 -070071 if (cur_quick_frame_ != NULL) {
72 DCHECK(context_ != NULL); // You can't reliably read registers without a context.
Ian Rogers2bcb4a42012-11-08 10:39:18 -080073 DCHECK(m == GetMethod());
Ian Rogers0ec569a2012-07-01 16:43:46 -070074 const VmapTable vmap_table(m->GetVmapTableRaw());
75 uint32_t vmap_offset;
76 // TODO: IsInContext stops before spotting floating point registers.
Ian Rogers2bcb4a42012-11-08 10:39:18 -080077 if (vmap_table.IsInContext(vreg, vmap_offset, kind)) {
78 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
79 uint32_t spill_mask = is_float ? m->GetFpSpillMask()
80 : m->GetCoreSpillMask();
81 return GetGPR(vmap_table.ComputeRegister(spill_mask, vmap_offset, kind));
Ian Rogers0ec569a2012-07-01 16:43:46 -070082 } else {
83 const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
84 DCHECK(code_item != NULL) << PrettyMethod(m); // Can't be NULL or how would we compile its instructions?
Ian Rogers0ec569a2012-07-01 16:43:46 -070085 size_t frame_size = m->GetFrameSizeInBytes();
Ian Rogers2bcb4a42012-11-08 10:39:18 -080086 return GetVReg(cur_quick_frame_, code_item, m->GetCoreSpillMask(), m->GetFpSpillMask(),
87 frame_size, vreg);
Ian Rogers0399dde2012-06-06 17:09:28 -070088 }
Ian Rogers0399dde2012-06-06 17:09:28 -070089 } else {
TDYa1278e950c12012-11-02 09:58:19 -070090 return cur_shadow_frame_->GetVReg(vreg);
Ian Rogers0399dde2012-06-06 17:09:28 -070091 }
92}
93
Ian Rogers2bcb4a42012-11-08 10:39:18 -080094void StackVisitor::SetVReg(AbstractMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind) {
Ian Rogers0ec569a2012-07-01 16:43:46 -070095 if (cur_quick_frame_ != NULL) {
96 DCHECK(context_ != NULL); // You can't reliably write registers without a context.
97 DCHECK(m == GetMethod());
98 const VmapTable vmap_table(m->GetVmapTableRaw());
99 uint32_t vmap_offset;
100 // TODO: IsInContext stops before spotting floating point registers.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800101 if (vmap_table.IsInContext(vreg, vmap_offset, kind)) {
Ian Rogers0ec569a2012-07-01 16:43:46 -0700102 UNIMPLEMENTED(FATAL);
103 }
104 const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
105 DCHECK(code_item != NULL) << PrettyMethod(m); // Can't be NULL or how would we compile its instructions?
106 uint32_t core_spills = m->GetCoreSpillMask();
107 uint32_t fp_spills = m->GetFpSpillMask();
108 size_t frame_size = m->GetFrameSizeInBytes();
109 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
110 byte* vreg_addr = reinterpret_cast<byte*>(GetCurrentQuickFrame()) + offset;
111 *reinterpret_cast<uint32_t*>(vreg_addr) = new_value;
112 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800113 return cur_shadow_frame_->SetVReg(vreg, new_value);
Ian Rogers0399dde2012-06-06 17:09:28 -0700114 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700115}
116
117uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
Ian Rogers0ec569a2012-07-01 16:43:46 -0700118 DCHECK (cur_quick_frame_ != NULL) << "This is a quick frame routine";
Ian Rogers0399dde2012-06-06 17:09:28 -0700119 return context_->GetGPR(reg);
120}
121
122uintptr_t StackVisitor::GetReturnPc() const {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700123 AbstractMethod** sp = GetCurrentQuickFrame();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800124 DCHECK(sp != NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700125 byte* pc_addr = reinterpret_cast<byte*>(sp) + GetMethod()->GetReturnPcOffsetInBytes();
126 return *reinterpret_cast<uintptr_t*>(pc_addr);
127}
128
129void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700130 AbstractMethod** sp = GetCurrentQuickFrame();
Ian Rogers0399dde2012-06-06 17:09:28 -0700131 CHECK(sp != NULL);
132 byte* pc_addr = reinterpret_cast<byte*>(sp) + GetMethod()->GetReturnPcOffsetInBytes();
133 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc;
134}
135
136size_t StackVisitor::ComputeNumFrames() const {
137 struct NumFramesVisitor : public StackVisitor {
138 explicit NumFramesVisitor(const ManagedStack* stack,
jeffhao725a9572012-11-13 18:20:12 -0800139 const std::vector<InstrumentationStackFrame>* instrumentation_stack)
140 : StackVisitor(stack, instrumentation_stack, NULL), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700141
142 virtual bool VisitFrame() {
143 frames++;
144 return true;
145 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700146
Ian Rogers0399dde2012-06-06 17:09:28 -0700147 size_t frames;
148 };
149
jeffhao725a9572012-11-13 18:20:12 -0800150 NumFramesVisitor visitor(stack_start_, instrumentation_stack_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700151 visitor.WalkStack(true);
152 return visitor.frames;
153}
154
Ian Rogers40e3bac2012-11-20 00:09:14 -0800155std::string StackVisitor::DescribeLocation() const {
156 std::string result("Visiting method '");
157 result += PrettyMethod(GetMethod());
158 result += StringPrintf("' at dex PC 0x%04zx", GetDexPc());
159 if (!IsShadowFrame()) {
160 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc()));
161 }
162 return result;
163}
164
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700165void StackVisitor::SanityCheckFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700166#ifndef NDEBUG
Mathieu Chartier66f19252012-09-18 08:57:04 -0700167 AbstractMethod* method = GetMethod();
168 CHECK(method->GetClass() == AbstractMethod::GetMethodClass() ||
169 method->GetClass() == AbstractMethod::GetConstructorClass());
Ian Rogers0399dde2012-06-06 17:09:28 -0700170 if (cur_quick_frame_ != NULL) {
buzbee8320f382012-09-11 16:29:42 -0700171 method->AssertPcIsWithinCode(cur_quick_frame_pc_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700172 // Frame sanity.
173 size_t frame_size = method->GetFrameSizeInBytes();
174 CHECK_NE(frame_size, 0u);
175 CHECK_LT(frame_size, 1024u);
176 size_t return_pc_offset = method->GetReturnPcOffsetInBytes();
177 CHECK_LT(return_pc_offset, frame_size);
178 }
179#endif
180}
181
182void StackVisitor::WalkStack(bool include_transitions) {
183 bool method_tracing_active = Runtime::Current()->IsMethodTracingActive();
jeffhao725a9572012-11-13 18:20:12 -0800184 uint32_t instrumentation_stack_depth = 0;
Ian Rogers0399dde2012-06-06 17:09:28 -0700185 for (const ManagedStack* current_fragment = stack_start_; current_fragment != NULL;
186 current_fragment = current_fragment->GetLink()) {
187 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
188 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
189 cur_quick_frame_pc_ = current_fragment->GetTopQuickFramePc();
190 if (cur_quick_frame_ != NULL) { // Handle quick stack frames.
191 // Can't be both a shadow and a quick fragment.
192 DCHECK(current_fragment->GetTopShadowFrame() == NULL);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700193 AbstractMethod* method = *cur_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700194 do {
195 SanityCheckFrame();
196 bool should_continue = VisitFrame();
197 if (UNLIKELY(!should_continue)) {
198 return;
199 }
200 if (context_ != NULL) {
201 context_->FillCalleeSaves(*this);
202 }
203 size_t frame_size = method->GetFrameSizeInBytes();
204 // Compute PC for next stack frame from return PC.
205 size_t return_pc_offset = method->GetReturnPcOffsetInBytes();
206 byte* return_pc_addr = reinterpret_cast<byte*>(cur_quick_frame_) + return_pc_offset;
207 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
208 if (UNLIKELY(method_tracing_active)) {
209 // While profiling, the return pc is restored from the side stack, except when walking
210 // the stack for an exception where the side stack will be unwound in VisitFrame.
211 // TODO: stop using include_transitions as a proxy for is this the catch block visitor.
jeffhao725a9572012-11-13 18:20:12 -0800212 if (IsInstrumentationExitPc(return_pc) && !include_transitions) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700213 // TODO: unify trace and managed stack.
jeffhao725a9572012-11-13 18:20:12 -0800214 InstrumentationStackFrame instrumentation_frame = GetInstrumentationStackFrame(instrumentation_stack_depth);
215 instrumentation_stack_depth++;
216 CHECK(instrumentation_frame.method_ == GetMethod()) << "Excepted: " << PrettyMethod(method)
Ian Rogers0399dde2012-06-06 17:09:28 -0700217 << " Found: " << PrettyMethod(GetMethod());
jeffhao725a9572012-11-13 18:20:12 -0800218 return_pc = instrumentation_frame.return_pc_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700219 }
220 }
221 cur_quick_frame_pc_ = return_pc;
222 byte* next_frame = reinterpret_cast<byte*>(cur_quick_frame_) + frame_size;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700223 cur_quick_frame_ = reinterpret_cast<AbstractMethod**>(next_frame);
Ian Rogers0399dde2012-06-06 17:09:28 -0700224 cur_depth_++;
225 method = *cur_quick_frame_;
226 } while (method != NULL);
227 } else if (cur_shadow_frame_ != NULL) {
228 do {
229 SanityCheckFrame();
230 bool should_continue = VisitFrame();
231 if (UNLIKELY(!should_continue)) {
232 return;
233 }
234 cur_depth_++;
235 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
236 } while(cur_shadow_frame_ != NULL);
237 }
238 cur_depth_++;
239 if (include_transitions) {
240 bool should_continue = VisitFrame();
241 if (!should_continue) {
242 return;
243 }
244 }
245 }
246}
247
Elliott Hughes68e76522011-10-05 13:22:16 -0700248} // namespace art