blob: e962dec8b6f863a8772c8298b6b2c8c02888b100 [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)) {
Mathieu Chartier67022432012-11-29 18:04:50 -0800102 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
103 uint32_t spill_mask = is_float ? m->GetFpSpillMask() : m->GetCoreSpillMask();
104 const uint32_t reg = vmap_table.ComputeRegister(spill_mask, vmap_offset, kReferenceVReg);
105 SetGPR(reg, new_value);
106 } else {
107 const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
108 DCHECK(code_item != NULL) << PrettyMethod(m); // Can't be NULL or how would we compile its instructions?
109 uint32_t core_spills = m->GetCoreSpillMask();
110 uint32_t fp_spills = m->GetFpSpillMask();
111 size_t frame_size = m->GetFrameSizeInBytes();
112 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
113 byte* vreg_addr = reinterpret_cast<byte*>(GetCurrentQuickFrame()) + offset;
114 *reinterpret_cast<uint32_t*>(vreg_addr) = new_value;
Ian Rogers0ec569a2012-07-01 16:43:46 -0700115 }
Ian Rogers0ec569a2012-07-01 16:43:46 -0700116 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800117 return cur_shadow_frame_->SetVReg(vreg, new_value);
Ian Rogers0399dde2012-06-06 17:09:28 -0700118 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700119}
120
121uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
Ian Rogers0ec569a2012-07-01 16:43:46 -0700122 DCHECK (cur_quick_frame_ != NULL) << "This is a quick frame routine";
Ian Rogers0399dde2012-06-06 17:09:28 -0700123 return context_->GetGPR(reg);
124}
125
Mathieu Chartier67022432012-11-29 18:04:50 -0800126void StackVisitor::SetGPR(uint32_t reg, uintptr_t value) {
127 DCHECK (cur_quick_frame_ != NULL) << "This is a quick frame routine";
128 context_->SetGPR(reg, value);
129}
130
Ian Rogers0399dde2012-06-06 17:09:28 -0700131uintptr_t StackVisitor::GetReturnPc() const {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700132 AbstractMethod** sp = GetCurrentQuickFrame();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800133 DCHECK(sp != NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700134 byte* pc_addr = reinterpret_cast<byte*>(sp) + GetMethod()->GetReturnPcOffsetInBytes();
135 return *reinterpret_cast<uintptr_t*>(pc_addr);
136}
137
138void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700139 AbstractMethod** sp = GetCurrentQuickFrame();
Ian Rogers0399dde2012-06-06 17:09:28 -0700140 CHECK(sp != NULL);
141 byte* pc_addr = reinterpret_cast<byte*>(sp) + GetMethod()->GetReturnPcOffsetInBytes();
142 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc;
143}
144
Ian Rogers306057f2012-11-26 12:45:53 -0800145size_t StackVisitor::ComputeNumFrames(const ManagedStack* stack,
146 const std::deque<InstrumentationStackFrame>* instr_stack) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700147 struct NumFramesVisitor : public StackVisitor {
148 explicit NumFramesVisitor(const ManagedStack* stack,
Ian Rogers306057f2012-11-26 12:45:53 -0800149 const std::deque<InstrumentationStackFrame>* instrumentation_stack)
jeffhao725a9572012-11-13 18:20:12 -0800150 : StackVisitor(stack, instrumentation_stack, NULL), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700151
152 virtual bool VisitFrame() {
153 frames++;
154 return true;
155 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700156
Ian Rogers0399dde2012-06-06 17:09:28 -0700157 size_t frames;
158 };
Ian Rogers306057f2012-11-26 12:45:53 -0800159 UNUSED(instr_stack); // We don't use the instrumentation stack as we don't require dex pcs...
160 NumFramesVisitor visitor(stack, NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700161 visitor.WalkStack(true);
162 return visitor.frames;
163}
164
Ian Rogers306057f2012-11-26 12:45:53 -0800165void StackVisitor::DescribeStack(const ManagedStack* stack,
166 const std::deque<InstrumentationStackFrame>* instr_stack) {
167 struct DescribeStackVisitor : public StackVisitor {
168 explicit DescribeStackVisitor(const ManagedStack* stack,
169 const std::deque<InstrumentationStackFrame>* instrumentation_stack)
170 : StackVisitor(stack, instrumentation_stack, NULL) {}
171
172 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
173 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation();
174 return true;
175 }
176 };
177 DescribeStackVisitor visitor(stack, instr_stack);
178 visitor.WalkStack(true);
179}
180
Ian Rogers40e3bac2012-11-20 00:09:14 -0800181std::string StackVisitor::DescribeLocation() const {
182 std::string result("Visiting method '");
Ian Rogers306057f2012-11-26 12:45:53 -0800183 AbstractMethod* m = GetMethod();
184 if (m == NULL) {
185 return "upcall";
186 }
187 result += PrettyMethod(m);
Ian Rogers40e3bac2012-11-20 00:09:14 -0800188 result += StringPrintf("' at dex PC 0x%04zx", GetDexPc());
189 if (!IsShadowFrame()) {
190 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc()));
191 }
192 return result;
193}
194
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700195void StackVisitor::SanityCheckFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700196#ifndef NDEBUG
Mathieu Chartier66f19252012-09-18 08:57:04 -0700197 AbstractMethod* method = GetMethod();
198 CHECK(method->GetClass() == AbstractMethod::GetMethodClass() ||
199 method->GetClass() == AbstractMethod::GetConstructorClass());
Ian Rogers0399dde2012-06-06 17:09:28 -0700200 if (cur_quick_frame_ != NULL) {
buzbee8320f382012-09-11 16:29:42 -0700201 method->AssertPcIsWithinCode(cur_quick_frame_pc_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700202 // Frame sanity.
203 size_t frame_size = method->GetFrameSizeInBytes();
204 CHECK_NE(frame_size, 0u);
205 CHECK_LT(frame_size, 1024u);
206 size_t return_pc_offset = method->GetReturnPcOffsetInBytes();
207 CHECK_LT(return_pc_offset, frame_size);
208 }
209#endif
210}
211
212void StackVisitor::WalkStack(bool include_transitions) {
Ian Rogers306057f2012-11-26 12:45:53 -0800213 bool method_tracing_active = instrumentation_stack_ != NULL;
jeffhao725a9572012-11-13 18:20:12 -0800214 uint32_t instrumentation_stack_depth = 0;
Ian Rogers0399dde2012-06-06 17:09:28 -0700215 for (const ManagedStack* current_fragment = stack_start_; current_fragment != NULL;
216 current_fragment = current_fragment->GetLink()) {
217 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
218 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
219 cur_quick_frame_pc_ = current_fragment->GetTopQuickFramePc();
220 if (cur_quick_frame_ != NULL) { // Handle quick stack frames.
221 // Can't be both a shadow and a quick fragment.
222 DCHECK(current_fragment->GetTopShadowFrame() == NULL);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700223 AbstractMethod* method = *cur_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700224 do {
225 SanityCheckFrame();
226 bool should_continue = VisitFrame();
227 if (UNLIKELY(!should_continue)) {
228 return;
229 }
230 if (context_ != NULL) {
231 context_->FillCalleeSaves(*this);
232 }
233 size_t frame_size = method->GetFrameSizeInBytes();
234 // Compute PC for next stack frame from return PC.
235 size_t return_pc_offset = method->GetReturnPcOffsetInBytes();
236 byte* return_pc_addr = reinterpret_cast<byte*>(cur_quick_frame_) + return_pc_offset;
237 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
238 if (UNLIKELY(method_tracing_active)) {
239 // While profiling, the return pc is restored from the side stack, except when walking
240 // the stack for an exception where the side stack will be unwound in VisitFrame.
241 // TODO: stop using include_transitions as a proxy for is this the catch block visitor.
Ian Rogers306057f2012-11-26 12:45:53 -0800242 if (GetInstrumentationExitPc() == return_pc && !include_transitions) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700243 // TODO: unify trace and managed stack.
jeffhao725a9572012-11-13 18:20:12 -0800244 InstrumentationStackFrame instrumentation_frame = GetInstrumentationStackFrame(instrumentation_stack_depth);
245 instrumentation_stack_depth++;
246 CHECK(instrumentation_frame.method_ == GetMethod()) << "Excepted: " << PrettyMethod(method)
Ian Rogers0399dde2012-06-06 17:09:28 -0700247 << " Found: " << PrettyMethod(GetMethod());
jeffhao725a9572012-11-13 18:20:12 -0800248 return_pc = instrumentation_frame.return_pc_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700249 }
250 }
251 cur_quick_frame_pc_ = return_pc;
252 byte* next_frame = reinterpret_cast<byte*>(cur_quick_frame_) + frame_size;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700253 cur_quick_frame_ = reinterpret_cast<AbstractMethod**>(next_frame);
Ian Rogers0399dde2012-06-06 17:09:28 -0700254 cur_depth_++;
255 method = *cur_quick_frame_;
256 } while (method != NULL);
257 } else if (cur_shadow_frame_ != NULL) {
258 do {
259 SanityCheckFrame();
260 bool should_continue = VisitFrame();
261 if (UNLIKELY(!should_continue)) {
262 return;
263 }
264 cur_depth_++;
265 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
266 } while(cur_shadow_frame_ != NULL);
267 }
268 cur_depth_++;
269 if (include_transitions) {
270 bool should_continue = VisitFrame();
271 if (!should_continue) {
272 return;
273 }
274 }
275 }
276}
277
Elliott Hughes68e76522011-10-05 13:22:16 -0700278} // namespace art