blob: c998f2a4dd1cd62bd6330ba265b7bd86f0d543a6 [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "mirror/abstract_method-inl.h"
22#include "mirror/object.h"
23#include "mirror/object-inl.h"
24#include "mirror/object_array-inl.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080025#include "object_utils.h"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070026#include "thread_list.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070027
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080028namespace art {
29
TDYa127ce4cc0d2012-11-18 16:59:53 -080030size_t ManagedStack::NumJniShadowFrameReferences() const {
Ian Rogers0399dde2012-06-06 17:09:28 -070031 size_t count = 0;
32 for (const ManagedStack* current_fragment = this; current_fragment != NULL;
33 current_fragment = current_fragment->GetLink()) {
34 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != NULL;
35 current_frame = current_frame->GetLink()) {
TDYa127ce4cc0d2012-11-18 16:59:53 -080036 if (current_frame->GetMethod()->IsNative()) {
37 // The JNI ShadowFrame only contains references. (For indirect reference.)
38 count += current_frame->NumberOfVRegs();
39 }
Ian Rogers0399dde2012-06-06 17:09:28 -070040 }
41 }
42 return count;
43}
44
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045bool ManagedStack::ShadowFramesContain(mirror::Object** shadow_frame_entry) const {
Ian Rogers0399dde2012-06-06 17:09:28 -070046 for (const ManagedStack* current_fragment = this; current_fragment != NULL;
47 current_fragment = current_fragment->GetLink()) {
48 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != NULL;
49 current_frame = current_frame->GetLink()) {
50 if (current_frame->Contains(shadow_frame_entry)) {
51 return true;
52 }
53 }
54 }
55 return false;
56}
57
Ian Rogers7a22fa62013-01-23 12:16:16 -080058StackVisitor::StackVisitor(Thread* thread, Context* context)
59 : thread_(thread), cur_shadow_frame_(NULL),
60 cur_quick_frame_(NULL), cur_quick_frame_pc_(0), num_frames_(0), cur_depth_(0),
61 context_(context) {
62 DCHECK(thread == Thread::Current() || thread->IsSuspended());
63}
64
Ian Rogers0399dde2012-06-06 17:09:28 -070065uint32_t StackVisitor::GetDexPc() const {
66 if (cur_shadow_frame_ != NULL) {
67 return cur_shadow_frame_->GetDexPC();
68 } else if (cur_quick_frame_ != NULL) {
Ian Rogers0c7abda2012-09-19 13:33:42 -070069 return GetMethod()->ToDexPc(cur_quick_frame_pc_);
Ian Rogers0399dde2012-06-06 17:09:28 -070070 } else {
71 return 0;
72 }
73}
74
Ian Rogers0c7abda2012-09-19 13:33:42 -070075size_t StackVisitor::GetNativePcOffset() const {
76 DCHECK(!IsShadowFrame());
77 return GetMethod()->NativePcOffset(cur_quick_frame_pc_);
78}
79
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080uint32_t StackVisitor::GetVReg(mirror::AbstractMethod* m, uint16_t vreg, VRegKind kind) const {
Ian Rogers0ec569a2012-07-01 16:43:46 -070081 if (cur_quick_frame_ != NULL) {
82 DCHECK(context_ != NULL); // You can't reliably read registers without a context.
Ian Rogers2bcb4a42012-11-08 10:39:18 -080083 DCHECK(m == GetMethod());
Ian Rogers0ec569a2012-07-01 16:43:46 -070084 const VmapTable vmap_table(m->GetVmapTableRaw());
85 uint32_t vmap_offset;
86 // TODO: IsInContext stops before spotting floating point registers.
Ian Rogers2bcb4a42012-11-08 10:39:18 -080087 if (vmap_table.IsInContext(vreg, vmap_offset, kind)) {
88 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
89 uint32_t spill_mask = is_float ? m->GetFpSpillMask()
90 : m->GetCoreSpillMask();
91 return GetGPR(vmap_table.ComputeRegister(spill_mask, vmap_offset, kind));
Ian Rogers0ec569a2012-07-01 16:43:46 -070092 } else {
93 const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
94 DCHECK(code_item != NULL) << PrettyMethod(m); // Can't be NULL or how would we compile its instructions?
Ian Rogers0ec569a2012-07-01 16:43:46 -070095 size_t frame_size = m->GetFrameSizeInBytes();
Ian Rogers2bcb4a42012-11-08 10:39:18 -080096 return GetVReg(cur_quick_frame_, code_item, m->GetCoreSpillMask(), m->GetFpSpillMask(),
97 frame_size, vreg);
Ian Rogers0399dde2012-06-06 17:09:28 -070098 }
Ian Rogers0399dde2012-06-06 17:09:28 -070099 } else {
TDYa1278e950c12012-11-02 09:58:19 -0700100 return cur_shadow_frame_->GetVReg(vreg);
Ian Rogers0399dde2012-06-06 17:09:28 -0700101 }
102}
103
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104void StackVisitor::SetVReg(mirror::AbstractMethod* m, uint16_t vreg, uint32_t new_value,
105 VRegKind kind) {
Ian Rogers0ec569a2012-07-01 16:43:46 -0700106 if (cur_quick_frame_ != NULL) {
107 DCHECK(context_ != NULL); // You can't reliably write registers without a context.
108 DCHECK(m == GetMethod());
109 const VmapTable vmap_table(m->GetVmapTableRaw());
110 uint32_t vmap_offset;
111 // TODO: IsInContext stops before spotting floating point registers.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800112 if (vmap_table.IsInContext(vreg, vmap_offset, kind)) {
Mathieu Chartier67022432012-11-29 18:04:50 -0800113 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
114 uint32_t spill_mask = is_float ? m->GetFpSpillMask() : m->GetCoreSpillMask();
115 const uint32_t reg = vmap_table.ComputeRegister(spill_mask, vmap_offset, kReferenceVReg);
116 SetGPR(reg, new_value);
117 } else {
118 const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
119 DCHECK(code_item != NULL) << PrettyMethod(m); // Can't be NULL or how would we compile its instructions?
120 uint32_t core_spills = m->GetCoreSpillMask();
121 uint32_t fp_spills = m->GetFpSpillMask();
122 size_t frame_size = m->GetFrameSizeInBytes();
123 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
124 byte* vreg_addr = reinterpret_cast<byte*>(GetCurrentQuickFrame()) + offset;
125 *reinterpret_cast<uint32_t*>(vreg_addr) = new_value;
Ian Rogers0ec569a2012-07-01 16:43:46 -0700126 }
Ian Rogers0ec569a2012-07-01 16:43:46 -0700127 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800128 return cur_shadow_frame_->SetVReg(vreg, new_value);
Ian Rogers0399dde2012-06-06 17:09:28 -0700129 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700130}
131
132uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
Ian Rogers0ec569a2012-07-01 16:43:46 -0700133 DCHECK (cur_quick_frame_ != NULL) << "This is a quick frame routine";
Ian Rogers0399dde2012-06-06 17:09:28 -0700134 return context_->GetGPR(reg);
135}
136
Mathieu Chartier67022432012-11-29 18:04:50 -0800137void StackVisitor::SetGPR(uint32_t reg, uintptr_t value) {
138 DCHECK (cur_quick_frame_ != NULL) << "This is a quick frame routine";
139 context_->SetGPR(reg, value);
140}
141
Ian Rogers0399dde2012-06-06 17:09:28 -0700142uintptr_t StackVisitor::GetReturnPc() const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800143 mirror::AbstractMethod** sp = GetCurrentQuickFrame();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800144 DCHECK(sp != NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700145 byte* pc_addr = reinterpret_cast<byte*>(sp) + GetMethod()->GetReturnPcOffsetInBytes();
146 return *reinterpret_cast<uintptr_t*>(pc_addr);
147}
148
149void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800150 mirror::AbstractMethod** sp = GetCurrentQuickFrame();
Ian Rogers0399dde2012-06-06 17:09:28 -0700151 CHECK(sp != NULL);
152 byte* pc_addr = reinterpret_cast<byte*>(sp) + GetMethod()->GetReturnPcOffsetInBytes();
153 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc;
154}
155
Ian Rogers7a22fa62013-01-23 12:16:16 -0800156size_t StackVisitor::ComputeNumFrames(Thread* thread) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700157 struct NumFramesVisitor : public StackVisitor {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800158 explicit NumFramesVisitor(Thread* thread)
159 : StackVisitor(thread, NULL), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700160
161 virtual bool VisitFrame() {
162 frames++;
163 return true;
164 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700165
Ian Rogers0399dde2012-06-06 17:09:28 -0700166 size_t frames;
167 };
Ian Rogers7a22fa62013-01-23 12:16:16 -0800168 NumFramesVisitor visitor(thread);
Ian Rogers0399dde2012-06-06 17:09:28 -0700169 visitor.WalkStack(true);
170 return visitor.frames;
171}
172
Ian Rogers7a22fa62013-01-23 12:16:16 -0800173void StackVisitor::DescribeStack(Thread* thread) {
Ian Rogers306057f2012-11-26 12:45:53 -0800174 struct DescribeStackVisitor : public StackVisitor {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800175 explicit DescribeStackVisitor(Thread* thread)
176 : StackVisitor(thread, NULL) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800177
178 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
179 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation();
180 return true;
181 }
182 };
Ian Rogers7a22fa62013-01-23 12:16:16 -0800183 DescribeStackVisitor visitor(thread);
Ian Rogers306057f2012-11-26 12:45:53 -0800184 visitor.WalkStack(true);
185}
186
Ian Rogers40e3bac2012-11-20 00:09:14 -0800187std::string StackVisitor::DescribeLocation() const {
188 std::string result("Visiting method '");
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800189 mirror::AbstractMethod* m = GetMethod();
Ian Rogers306057f2012-11-26 12:45:53 -0800190 if (m == NULL) {
191 return "upcall";
192 }
193 result += PrettyMethod(m);
Ian Rogers40e3bac2012-11-20 00:09:14 -0800194 result += StringPrintf("' at dex PC 0x%04zx", GetDexPc());
195 if (!IsShadowFrame()) {
196 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc()));
197 }
198 return result;
199}
200
Ian Rogers7a22fa62013-01-23 12:16:16 -0800201InstrumentationStackFrame StackVisitor::GetInstrumentationStackFrame(uint32_t depth) const {
202 return thread_->GetInstrumentationStack()->at(depth);
203}
204
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700205void StackVisitor::SanityCheckFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700206#ifndef NDEBUG
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800207 mirror::AbstractMethod* method = GetMethod();
208 CHECK(method->GetClass() == mirror::AbstractMethod::GetMethodClass() ||
209 method->GetClass() == mirror::AbstractMethod::GetConstructorClass());
Ian Rogers0399dde2012-06-06 17:09:28 -0700210 if (cur_quick_frame_ != NULL) {
buzbee8320f382012-09-11 16:29:42 -0700211 method->AssertPcIsWithinCode(cur_quick_frame_pc_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700212 // Frame sanity.
213 size_t frame_size = method->GetFrameSizeInBytes();
214 CHECK_NE(frame_size, 0u);
215 CHECK_LT(frame_size, 1024u);
216 size_t return_pc_offset = method->GetReturnPcOffsetInBytes();
217 CHECK_LT(return_pc_offset, frame_size);
218 }
219#endif
220}
221
222void StackVisitor::WalkStack(bool include_transitions) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800223 DCHECK(thread_ == Thread::Current() || thread_->IsSuspended());
224 const std::deque<InstrumentationStackFrame>* instrumentation_stack =
225 thread_->GetInstrumentationStack();
226 bool method_tracing_active = instrumentation_stack != NULL;
jeffhao725a9572012-11-13 18:20:12 -0800227 uint32_t instrumentation_stack_depth = 0;
Ian Rogers7a22fa62013-01-23 12:16:16 -0800228 for (const ManagedStack* current_fragment = thread_->GetManagedStack(); current_fragment != NULL;
Ian Rogers0399dde2012-06-06 17:09:28 -0700229 current_fragment = current_fragment->GetLink()) {
230 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
231 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
232 cur_quick_frame_pc_ = current_fragment->GetTopQuickFramePc();
233 if (cur_quick_frame_ != NULL) { // Handle quick stack frames.
234 // Can't be both a shadow and a quick fragment.
235 DCHECK(current_fragment->GetTopShadowFrame() == NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800236 mirror::AbstractMethod* method = *cur_quick_frame_;
jeffhao6641ea12013-01-02 18:13:42 -0800237 while (method != NULL) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700238 SanityCheckFrame();
239 bool should_continue = VisitFrame();
240 if (UNLIKELY(!should_continue)) {
241 return;
242 }
243 if (context_ != NULL) {
244 context_->FillCalleeSaves(*this);
245 }
246 size_t frame_size = method->GetFrameSizeInBytes();
247 // Compute PC for next stack frame from return PC.
248 size_t return_pc_offset = method->GetReturnPcOffsetInBytes();
249 byte* return_pc_addr = reinterpret_cast<byte*>(cur_quick_frame_) + return_pc_offset;
250 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
251 if (UNLIKELY(method_tracing_active)) {
252 // While profiling, the return pc is restored from the side stack, except when walking
253 // the stack for an exception where the side stack will be unwound in VisitFrame.
254 // TODO: stop using include_transitions as a proxy for is this the catch block visitor.
Ian Rogers306057f2012-11-26 12:45:53 -0800255 if (GetInstrumentationExitPc() == return_pc && !include_transitions) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700256 // TODO: unify trace and managed stack.
jeffhao725a9572012-11-13 18:20:12 -0800257 InstrumentationStackFrame instrumentation_frame = GetInstrumentationStackFrame(instrumentation_stack_depth);
258 instrumentation_stack_depth++;
259 CHECK(instrumentation_frame.method_ == GetMethod()) << "Excepted: " << PrettyMethod(method)
Ian Rogers0399dde2012-06-06 17:09:28 -0700260 << " Found: " << PrettyMethod(GetMethod());
jeffhao725a9572012-11-13 18:20:12 -0800261 return_pc = instrumentation_frame.return_pc_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700262 }
263 }
264 cur_quick_frame_pc_ = return_pc;
265 byte* next_frame = reinterpret_cast<byte*>(cur_quick_frame_) + frame_size;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800266 cur_quick_frame_ = reinterpret_cast<mirror::AbstractMethod**>(next_frame);
Ian Rogers0399dde2012-06-06 17:09:28 -0700267 cur_depth_++;
268 method = *cur_quick_frame_;
jeffhao6641ea12013-01-02 18:13:42 -0800269 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700270 } else if (cur_shadow_frame_ != NULL) {
271 do {
272 SanityCheckFrame();
273 bool should_continue = VisitFrame();
274 if (UNLIKELY(!should_continue)) {
275 return;
276 }
277 cur_depth_++;
278 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
279 } while(cur_shadow_frame_ != NULL);
280 }
281 cur_depth_++;
282 if (include_transitions) {
283 bool should_continue = VisitFrame();
284 if (!should_continue) {
285 return;
286 }
287 }
288 }
289}
290
Elliott Hughes68e76522011-10-05 13:22:16 -0700291} // namespace art