Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 1 | /* |
| 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 Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 20 | #include "oat/runtime/context.h" |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 21 | #include "object.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 22 | #include "object_utils.h" |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 23 | #include "thread_list.h" |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 24 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 25 | namespace art { |
| 26 | |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 27 | size_t ManagedStack::NumJniShadowFrameReferences() const { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 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()) { |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 33 | if (current_frame->GetMethod()->IsNative()) { |
| 34 | // The JNI ShadowFrame only contains references. (For indirect reference.) |
| 35 | count += current_frame->NumberOfVRegs(); |
| 36 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | return count; |
| 40 | } |
| 41 | |
| 42 | bool 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 | |
| 55 | uint32_t StackVisitor::GetDexPc() const { |
| 56 | if (cur_shadow_frame_ != NULL) { |
| 57 | return cur_shadow_frame_->GetDexPC(); |
| 58 | } else if (cur_quick_frame_ != NULL) { |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 59 | return GetMethod()->ToDexPc(cur_quick_frame_pc_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 60 | } else { |
| 61 | return 0; |
| 62 | } |
| 63 | } |
| 64 | |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 65 | size_t StackVisitor::GetNativePcOffset() const { |
| 66 | DCHECK(!IsShadowFrame()); |
| 67 | return GetMethod()->NativePcOffset(cur_quick_frame_pc_); |
| 68 | } |
| 69 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 70 | uint32_t StackVisitor::GetVReg(AbstractMethod* m, uint16_t vreg, VRegKind kind) const { |
Ian Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 71 | if (cur_quick_frame_ != NULL) { |
| 72 | DCHECK(context_ != NULL); // You can't reliably read registers without a context. |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 73 | DCHECK(m == GetMethod()); |
Ian Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 74 | const VmapTable vmap_table(m->GetVmapTableRaw()); |
| 75 | uint32_t vmap_offset; |
| 76 | // TODO: IsInContext stops before spotting floating point registers. |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 77 | 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 Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 82 | } 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 Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 85 | size_t frame_size = m->GetFrameSizeInBytes(); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 86 | return GetVReg(cur_quick_frame_, code_item, m->GetCoreSpillMask(), m->GetFpSpillMask(), |
| 87 | frame_size, vreg); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 88 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 89 | } else { |
TDYa127 | 8e950c1 | 2012-11-02 09:58:19 -0700 | [diff] [blame] | 90 | return cur_shadow_frame_->GetVReg(vreg); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 94 | void StackVisitor::SetVReg(AbstractMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind) { |
Ian Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 95 | 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 Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 101 | if (vmap_table.IsInContext(vreg, vmap_offset, kind)) { |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame^] | 102 | 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 Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 115 | } |
Ian Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 116 | } else { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 117 | return cur_shadow_frame_->SetVReg(vreg, new_value); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 118 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | uintptr_t StackVisitor::GetGPR(uint32_t reg) const { |
Ian Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 122 | DCHECK (cur_quick_frame_ != NULL) << "This is a quick frame routine"; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 123 | return context_->GetGPR(reg); |
| 124 | } |
| 125 | |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame^] | 126 | void 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 Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 131 | uintptr_t StackVisitor::GetReturnPc() const { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 132 | AbstractMethod** sp = GetCurrentQuickFrame(); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 133 | DCHECK(sp != NULL); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 134 | byte* pc_addr = reinterpret_cast<byte*>(sp) + GetMethod()->GetReturnPcOffsetInBytes(); |
| 135 | return *reinterpret_cast<uintptr_t*>(pc_addr); |
| 136 | } |
| 137 | |
| 138 | void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 139 | AbstractMethod** sp = GetCurrentQuickFrame(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 140 | 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 | |
| 145 | size_t StackVisitor::ComputeNumFrames() const { |
| 146 | struct NumFramesVisitor : public StackVisitor { |
| 147 | explicit NumFramesVisitor(const ManagedStack* stack, |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 148 | const std::vector<InstrumentationStackFrame>* instrumentation_stack) |
| 149 | : StackVisitor(stack, instrumentation_stack, NULL), frames(0) {} |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 150 | |
| 151 | virtual bool VisitFrame() { |
| 152 | frames++; |
| 153 | return true; |
| 154 | } |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 155 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 156 | size_t frames; |
| 157 | }; |
| 158 | |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 159 | NumFramesVisitor visitor(stack_start_, instrumentation_stack_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 160 | visitor.WalkStack(true); |
| 161 | return visitor.frames; |
| 162 | } |
| 163 | |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame] | 164 | std::string StackVisitor::DescribeLocation() const { |
| 165 | std::string result("Visiting method '"); |
| 166 | result += PrettyMethod(GetMethod()); |
| 167 | result += StringPrintf("' at dex PC 0x%04zx", GetDexPc()); |
| 168 | if (!IsShadowFrame()) { |
| 169 | result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc())); |
| 170 | } |
| 171 | return result; |
| 172 | } |
| 173 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 174 | void StackVisitor::SanityCheckFrame() const { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 175 | #ifndef NDEBUG |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 176 | AbstractMethod* method = GetMethod(); |
| 177 | CHECK(method->GetClass() == AbstractMethod::GetMethodClass() || |
| 178 | method->GetClass() == AbstractMethod::GetConstructorClass()); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 179 | if (cur_quick_frame_ != NULL) { |
buzbee | 8320f38 | 2012-09-11 16:29:42 -0700 | [diff] [blame] | 180 | method->AssertPcIsWithinCode(cur_quick_frame_pc_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 181 | // Frame sanity. |
| 182 | size_t frame_size = method->GetFrameSizeInBytes(); |
| 183 | CHECK_NE(frame_size, 0u); |
| 184 | CHECK_LT(frame_size, 1024u); |
| 185 | size_t return_pc_offset = method->GetReturnPcOffsetInBytes(); |
| 186 | CHECK_LT(return_pc_offset, frame_size); |
| 187 | } |
| 188 | #endif |
| 189 | } |
| 190 | |
| 191 | void StackVisitor::WalkStack(bool include_transitions) { |
| 192 | bool method_tracing_active = Runtime::Current()->IsMethodTracingActive(); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 193 | uint32_t instrumentation_stack_depth = 0; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 194 | for (const ManagedStack* current_fragment = stack_start_; current_fragment != NULL; |
| 195 | current_fragment = current_fragment->GetLink()) { |
| 196 | cur_shadow_frame_ = current_fragment->GetTopShadowFrame(); |
| 197 | cur_quick_frame_ = current_fragment->GetTopQuickFrame(); |
| 198 | cur_quick_frame_pc_ = current_fragment->GetTopQuickFramePc(); |
| 199 | if (cur_quick_frame_ != NULL) { // Handle quick stack frames. |
| 200 | // Can't be both a shadow and a quick fragment. |
| 201 | DCHECK(current_fragment->GetTopShadowFrame() == NULL); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 202 | AbstractMethod* method = *cur_quick_frame_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 203 | do { |
| 204 | SanityCheckFrame(); |
| 205 | bool should_continue = VisitFrame(); |
| 206 | if (UNLIKELY(!should_continue)) { |
| 207 | return; |
| 208 | } |
| 209 | if (context_ != NULL) { |
| 210 | context_->FillCalleeSaves(*this); |
| 211 | } |
| 212 | size_t frame_size = method->GetFrameSizeInBytes(); |
| 213 | // Compute PC for next stack frame from return PC. |
| 214 | size_t return_pc_offset = method->GetReturnPcOffsetInBytes(); |
| 215 | byte* return_pc_addr = reinterpret_cast<byte*>(cur_quick_frame_) + return_pc_offset; |
| 216 | uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr); |
| 217 | if (UNLIKELY(method_tracing_active)) { |
| 218 | // While profiling, the return pc is restored from the side stack, except when walking |
| 219 | // the stack for an exception where the side stack will be unwound in VisitFrame. |
| 220 | // TODO: stop using include_transitions as a proxy for is this the catch block visitor. |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 221 | if (IsInstrumentationExitPc(return_pc) && !include_transitions) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 222 | // TODO: unify trace and managed stack. |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 223 | InstrumentationStackFrame instrumentation_frame = GetInstrumentationStackFrame(instrumentation_stack_depth); |
| 224 | instrumentation_stack_depth++; |
| 225 | CHECK(instrumentation_frame.method_ == GetMethod()) << "Excepted: " << PrettyMethod(method) |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 226 | << " Found: " << PrettyMethod(GetMethod()); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 227 | return_pc = instrumentation_frame.return_pc_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | cur_quick_frame_pc_ = return_pc; |
| 231 | byte* next_frame = reinterpret_cast<byte*>(cur_quick_frame_) + frame_size; |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 232 | cur_quick_frame_ = reinterpret_cast<AbstractMethod**>(next_frame); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 233 | cur_depth_++; |
| 234 | method = *cur_quick_frame_; |
| 235 | } while (method != NULL); |
| 236 | } else if (cur_shadow_frame_ != NULL) { |
| 237 | do { |
| 238 | SanityCheckFrame(); |
| 239 | bool should_continue = VisitFrame(); |
| 240 | if (UNLIKELY(!should_continue)) { |
| 241 | return; |
| 242 | } |
| 243 | cur_depth_++; |
| 244 | cur_shadow_frame_ = cur_shadow_frame_->GetLink(); |
| 245 | } while(cur_shadow_frame_ != NULL); |
| 246 | } |
| 247 | cur_depth_++; |
| 248 | if (include_transitions) { |
| 249 | bool should_continue = VisitFrame(); |
| 250 | if (!should_continue) { |
| 251 | return; |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 257 | } // namespace art |