blob: 2e1f4ae311103140085425f2a78bc9f13cdcebb4 [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
Ian Rogers7a22fa62013-01-23 12:16:16 -080055StackVisitor::StackVisitor(Thread* thread, Context* context)
56 : thread_(thread), cur_shadow_frame_(NULL),
57 cur_quick_frame_(NULL), cur_quick_frame_pc_(0), num_frames_(0), cur_depth_(0),
58 context_(context) {
59 DCHECK(thread == Thread::Current() || thread->IsSuspended());
60}
61
Ian Rogers0399dde2012-06-06 17:09:28 -070062uint32_t StackVisitor::GetDexPc() const {
63 if (cur_shadow_frame_ != NULL) {
64 return cur_shadow_frame_->GetDexPC();
65 } else if (cur_quick_frame_ != NULL) {
Ian Rogers0c7abda2012-09-19 13:33:42 -070066 return GetMethod()->ToDexPc(cur_quick_frame_pc_);
Ian Rogers0399dde2012-06-06 17:09:28 -070067 } else {
68 return 0;
69 }
70}
71
Ian Rogers0c7abda2012-09-19 13:33:42 -070072size_t StackVisitor::GetNativePcOffset() const {
73 DCHECK(!IsShadowFrame());
74 return GetMethod()->NativePcOffset(cur_quick_frame_pc_);
75}
76
Ian Rogers2bcb4a42012-11-08 10:39:18 -080077uint32_t StackVisitor::GetVReg(AbstractMethod* m, uint16_t vreg, VRegKind kind) 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.
Ian Rogers2bcb4a42012-11-08 10:39:18 -080080 DCHECK(m == GetMethod());
Ian Rogers0ec569a2012-07-01 16:43:46 -070081 const VmapTable vmap_table(m->GetVmapTableRaw());
82 uint32_t vmap_offset;
83 // TODO: IsInContext stops before spotting floating point registers.
Ian Rogers2bcb4a42012-11-08 10:39:18 -080084 if (vmap_table.IsInContext(vreg, vmap_offset, kind)) {
85 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
86 uint32_t spill_mask = is_float ? m->GetFpSpillMask()
87 : m->GetCoreSpillMask();
88 return GetGPR(vmap_table.ComputeRegister(spill_mask, vmap_offset, kind));
Ian Rogers0ec569a2012-07-01 16:43:46 -070089 } else {
90 const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
91 DCHECK(code_item != NULL) << PrettyMethod(m); // Can't be NULL or how would we compile its instructions?
Ian Rogers0ec569a2012-07-01 16:43:46 -070092 size_t frame_size = m->GetFrameSizeInBytes();
Ian Rogers2bcb4a42012-11-08 10:39:18 -080093 return GetVReg(cur_quick_frame_, code_item, m->GetCoreSpillMask(), m->GetFpSpillMask(),
94 frame_size, vreg);
Ian Rogers0399dde2012-06-06 17:09:28 -070095 }
Ian Rogers0399dde2012-06-06 17:09:28 -070096 } else {
TDYa1278e950c12012-11-02 09:58:19 -070097 return cur_shadow_frame_->GetVReg(vreg);
Ian Rogers0399dde2012-06-06 17:09:28 -070098 }
99}
100
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800101void StackVisitor::SetVReg(AbstractMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind) {
Ian Rogers0ec569a2012-07-01 16:43:46 -0700102 if (cur_quick_frame_ != NULL) {
103 DCHECK(context_ != NULL); // You can't reliably write registers without a context.
104 DCHECK(m == GetMethod());
105 const VmapTable vmap_table(m->GetVmapTableRaw());
106 uint32_t vmap_offset;
107 // TODO: IsInContext stops before spotting floating point registers.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800108 if (vmap_table.IsInContext(vreg, vmap_offset, kind)) {
Mathieu Chartier67022432012-11-29 18:04:50 -0800109 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
110 uint32_t spill_mask = is_float ? m->GetFpSpillMask() : m->GetCoreSpillMask();
111 const uint32_t reg = vmap_table.ComputeRegister(spill_mask, vmap_offset, kReferenceVReg);
112 SetGPR(reg, new_value);
113 } else {
114 const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
115 DCHECK(code_item != NULL) << PrettyMethod(m); // Can't be NULL or how would we compile its instructions?
116 uint32_t core_spills = m->GetCoreSpillMask();
117 uint32_t fp_spills = m->GetFpSpillMask();
118 size_t frame_size = m->GetFrameSizeInBytes();
119 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
120 byte* vreg_addr = reinterpret_cast<byte*>(GetCurrentQuickFrame()) + offset;
121 *reinterpret_cast<uint32_t*>(vreg_addr) = new_value;
Ian Rogers0ec569a2012-07-01 16:43:46 -0700122 }
Ian Rogers0ec569a2012-07-01 16:43:46 -0700123 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800124 return cur_shadow_frame_->SetVReg(vreg, new_value);
Ian Rogers0399dde2012-06-06 17:09:28 -0700125 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700126}
127
128uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
Ian Rogers0ec569a2012-07-01 16:43:46 -0700129 DCHECK (cur_quick_frame_ != NULL) << "This is a quick frame routine";
Ian Rogers0399dde2012-06-06 17:09:28 -0700130 return context_->GetGPR(reg);
131}
132
Mathieu Chartier67022432012-11-29 18:04:50 -0800133void StackVisitor::SetGPR(uint32_t reg, uintptr_t value) {
134 DCHECK (cur_quick_frame_ != NULL) << "This is a quick frame routine";
135 context_->SetGPR(reg, value);
136}
137
Ian Rogers0399dde2012-06-06 17:09:28 -0700138uintptr_t StackVisitor::GetReturnPc() const {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700139 AbstractMethod** sp = GetCurrentQuickFrame();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800140 DCHECK(sp != NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700141 byte* pc_addr = reinterpret_cast<byte*>(sp) + GetMethod()->GetReturnPcOffsetInBytes();
142 return *reinterpret_cast<uintptr_t*>(pc_addr);
143}
144
145void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700146 AbstractMethod** sp = GetCurrentQuickFrame();
Ian Rogers0399dde2012-06-06 17:09:28 -0700147 CHECK(sp != NULL);
148 byte* pc_addr = reinterpret_cast<byte*>(sp) + GetMethod()->GetReturnPcOffsetInBytes();
149 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc;
150}
151
Ian Rogers7a22fa62013-01-23 12:16:16 -0800152size_t StackVisitor::ComputeNumFrames(Thread* thread) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700153 struct NumFramesVisitor : public StackVisitor {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800154 explicit NumFramesVisitor(Thread* thread)
155 : StackVisitor(thread, NULL), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700156
157 virtual bool VisitFrame() {
158 frames++;
159 return true;
160 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700161
Ian Rogers0399dde2012-06-06 17:09:28 -0700162 size_t frames;
163 };
Ian Rogers7a22fa62013-01-23 12:16:16 -0800164 NumFramesVisitor visitor(thread);
Ian Rogers0399dde2012-06-06 17:09:28 -0700165 visitor.WalkStack(true);
166 return visitor.frames;
167}
168
Ian Rogers7a22fa62013-01-23 12:16:16 -0800169void StackVisitor::DescribeStack(Thread* thread) {
Ian Rogers306057f2012-11-26 12:45:53 -0800170 struct DescribeStackVisitor : public StackVisitor {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800171 explicit DescribeStackVisitor(Thread* thread)
172 : StackVisitor(thread, NULL) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800173
174 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
175 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation();
176 return true;
177 }
178 };
Ian Rogers7a22fa62013-01-23 12:16:16 -0800179 DescribeStackVisitor visitor(thread);
Ian Rogers306057f2012-11-26 12:45:53 -0800180 visitor.WalkStack(true);
181}
182
Ian Rogers40e3bac2012-11-20 00:09:14 -0800183std::string StackVisitor::DescribeLocation() const {
184 std::string result("Visiting method '");
Ian Rogers306057f2012-11-26 12:45:53 -0800185 AbstractMethod* m = GetMethod();
186 if (m == NULL) {
187 return "upcall";
188 }
189 result += PrettyMethod(m);
Ian Rogers40e3bac2012-11-20 00:09:14 -0800190 result += StringPrintf("' at dex PC 0x%04zx", GetDexPc());
191 if (!IsShadowFrame()) {
192 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc()));
193 }
194 return result;
195}
196
Ian Rogers7a22fa62013-01-23 12:16:16 -0800197InstrumentationStackFrame StackVisitor::GetInstrumentationStackFrame(uint32_t depth) const {
198 return thread_->GetInstrumentationStack()->at(depth);
199}
200
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700201void StackVisitor::SanityCheckFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700202#ifndef NDEBUG
Mathieu Chartier66f19252012-09-18 08:57:04 -0700203 AbstractMethod* method = GetMethod();
204 CHECK(method->GetClass() == AbstractMethod::GetMethodClass() ||
205 method->GetClass() == AbstractMethod::GetConstructorClass());
Ian Rogers0399dde2012-06-06 17:09:28 -0700206 if (cur_quick_frame_ != NULL) {
buzbee8320f382012-09-11 16:29:42 -0700207 method->AssertPcIsWithinCode(cur_quick_frame_pc_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700208 // Frame sanity.
209 size_t frame_size = method->GetFrameSizeInBytes();
210 CHECK_NE(frame_size, 0u);
211 CHECK_LT(frame_size, 1024u);
212 size_t return_pc_offset = method->GetReturnPcOffsetInBytes();
213 CHECK_LT(return_pc_offset, frame_size);
214 }
215#endif
216}
217
218void StackVisitor::WalkStack(bool include_transitions) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800219 DCHECK(thread_ == Thread::Current() || thread_->IsSuspended());
220 const std::deque<InstrumentationStackFrame>* instrumentation_stack =
221 thread_->GetInstrumentationStack();
222 bool method_tracing_active = instrumentation_stack != NULL;
jeffhao725a9572012-11-13 18:20:12 -0800223 uint32_t instrumentation_stack_depth = 0;
Ian Rogers7a22fa62013-01-23 12:16:16 -0800224 for (const ManagedStack* current_fragment = thread_->GetManagedStack(); current_fragment != NULL;
Ian Rogers0399dde2012-06-06 17:09:28 -0700225 current_fragment = current_fragment->GetLink()) {
226 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
227 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
228 cur_quick_frame_pc_ = current_fragment->GetTopQuickFramePc();
229 if (cur_quick_frame_ != NULL) { // Handle quick stack frames.
230 // Can't be both a shadow and a quick fragment.
231 DCHECK(current_fragment->GetTopShadowFrame() == NULL);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700232 AbstractMethod* method = *cur_quick_frame_;
jeffhao6641ea12013-01-02 18:13:42 -0800233 while (method != NULL) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700234 SanityCheckFrame();
235 bool should_continue = VisitFrame();
236 if (UNLIKELY(!should_continue)) {
237 return;
238 }
239 if (context_ != NULL) {
240 context_->FillCalleeSaves(*this);
241 }
242 size_t frame_size = method->GetFrameSizeInBytes();
243 // Compute PC for next stack frame from return PC.
244 size_t return_pc_offset = method->GetReturnPcOffsetInBytes();
245 byte* return_pc_addr = reinterpret_cast<byte*>(cur_quick_frame_) + return_pc_offset;
246 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
247 if (UNLIKELY(method_tracing_active)) {
248 // While profiling, the return pc is restored from the side stack, except when walking
249 // the stack for an exception where the side stack will be unwound in VisitFrame.
250 // TODO: stop using include_transitions as a proxy for is this the catch block visitor.
Ian Rogers306057f2012-11-26 12:45:53 -0800251 if (GetInstrumentationExitPc() == return_pc && !include_transitions) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700252 // TODO: unify trace and managed stack.
jeffhao725a9572012-11-13 18:20:12 -0800253 InstrumentationStackFrame instrumentation_frame = GetInstrumentationStackFrame(instrumentation_stack_depth);
254 instrumentation_stack_depth++;
255 CHECK(instrumentation_frame.method_ == GetMethod()) << "Excepted: " << PrettyMethod(method)
Ian Rogers0399dde2012-06-06 17:09:28 -0700256 << " Found: " << PrettyMethod(GetMethod());
jeffhao725a9572012-11-13 18:20:12 -0800257 return_pc = instrumentation_frame.return_pc_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700258 }
259 }
260 cur_quick_frame_pc_ = return_pc;
261 byte* next_frame = reinterpret_cast<byte*>(cur_quick_frame_) + frame_size;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700262 cur_quick_frame_ = reinterpret_cast<AbstractMethod**>(next_frame);
Ian Rogers0399dde2012-06-06 17:09:28 -0700263 cur_depth_++;
264 method = *cur_quick_frame_;
jeffhao6641ea12013-01-02 18:13:42 -0800265 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700266 } else if (cur_shadow_frame_ != NULL) {
267 do {
268 SanityCheckFrame();
269 bool should_continue = VisitFrame();
270 if (UNLIKELY(!should_continue)) {
271 return;
272 }
273 cur_depth_++;
274 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
275 } while(cur_shadow_frame_ != NULL);
276 }
277 cur_depth_++;
278 if (include_transitions) {
279 bool should_continue = VisitFrame();
280 if (!should_continue) {
281 return;
282 }
283 }
284 }
285}
286
Elliott Hughes68e76522011-10-05 13:22:16 -0700287} // namespace art