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 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 27 | size_t ManagedStack::NumShadowFrameReferences() const { |
| 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()) { |
| 33 | count += current_frame->NumberOfReferences(); |
| 34 | } |
| 35 | } |
| 36 | return count; |
| 37 | } |
| 38 | |
| 39 | bool ManagedStack::ShadowFramesContain(Object** shadow_frame_entry) const { |
| 40 | for (const ManagedStack* current_fragment = this; current_fragment != NULL; |
| 41 | current_fragment = current_fragment->GetLink()) { |
| 42 | for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != NULL; |
| 43 | current_frame = current_frame->GetLink()) { |
| 44 | if (current_frame->Contains(shadow_frame_entry)) { |
| 45 | return true; |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | uint32_t StackVisitor::GetDexPc() const { |
| 53 | if (cur_shadow_frame_ != NULL) { |
| 54 | return cur_shadow_frame_->GetDexPC(); |
| 55 | } else if (cur_quick_frame_ != NULL) { |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 56 | return GetMethod()->ToDexPc(cur_quick_frame_pc_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 57 | } else { |
| 58 | return 0; |
| 59 | } |
| 60 | } |
| 61 | |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 62 | size_t StackVisitor::GetNativePcOffset() const { |
| 63 | DCHECK(!IsShadowFrame()); |
| 64 | return GetMethod()->NativePcOffset(cur_quick_frame_pc_); |
| 65 | } |
| 66 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 67 | uint32_t StackVisitor::GetVReg(AbstractMethod* m, uint16_t vreg, VRegKind kind) const { |
Ian Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 68 | if (cur_quick_frame_ != NULL) { |
| 69 | DCHECK(context_ != NULL); // You can't reliably read registers without a context. |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 70 | DCHECK(m == GetMethod()); |
Ian Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 71 | const VmapTable vmap_table(m->GetVmapTableRaw()); |
| 72 | uint32_t vmap_offset; |
| 73 | // TODO: IsInContext stops before spotting floating point registers. |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 74 | if (vmap_table.IsInContext(vreg, vmap_offset, kind)) { |
| 75 | bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg); |
| 76 | uint32_t spill_mask = is_float ? m->GetFpSpillMask() |
| 77 | : m->GetCoreSpillMask(); |
| 78 | return GetGPR(vmap_table.ComputeRegister(spill_mask, vmap_offset, kind)); |
Ian Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 79 | } else { |
| 80 | const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem(); |
| 81 | 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] | 82 | size_t frame_size = m->GetFrameSizeInBytes(); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 83 | return GetVReg(cur_quick_frame_, code_item, m->GetCoreSpillMask(), m->GetFpSpillMask(), |
| 84 | frame_size, vreg); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 85 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 86 | } else { |
TDYa127 | 8e950c1 | 2012-11-02 09:58:19 -0700 | [diff] [blame] | 87 | return cur_shadow_frame_->GetVReg(vreg); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 91 | 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] | 92 | if (cur_quick_frame_ != NULL) { |
| 93 | DCHECK(context_ != NULL); // You can't reliably write registers without a context. |
| 94 | DCHECK(m == GetMethod()); |
| 95 | const VmapTable vmap_table(m->GetVmapTableRaw()); |
| 96 | uint32_t vmap_offset; |
| 97 | // TODO: IsInContext stops before spotting floating point registers. |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 98 | if (vmap_table.IsInContext(vreg, vmap_offset, kind)) { |
Ian Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 99 | UNIMPLEMENTED(FATAL); |
| 100 | } |
| 101 | const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem(); |
| 102 | DCHECK(code_item != NULL) << PrettyMethod(m); // Can't be NULL or how would we compile its instructions? |
| 103 | uint32_t core_spills = m->GetCoreSpillMask(); |
| 104 | uint32_t fp_spills = m->GetFpSpillMask(); |
| 105 | size_t frame_size = m->GetFrameSizeInBytes(); |
| 106 | int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg); |
| 107 | byte* vreg_addr = reinterpret_cast<byte*>(GetCurrentQuickFrame()) + offset; |
| 108 | *reinterpret_cast<uint32_t*>(vreg_addr) = new_value; |
| 109 | } else { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 110 | return cur_shadow_frame_->SetVReg(vreg, new_value); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 111 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | uintptr_t StackVisitor::GetGPR(uint32_t reg) const { |
Ian Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 115 | DCHECK (cur_quick_frame_ != NULL) << "This is a quick frame routine"; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 116 | return context_->GetGPR(reg); |
| 117 | } |
| 118 | |
| 119 | uintptr_t StackVisitor::GetReturnPc() const { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 120 | AbstractMethod** sp = GetCurrentQuickFrame(); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 121 | DCHECK(sp != NULL); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 122 | byte* pc_addr = reinterpret_cast<byte*>(sp) + GetMethod()->GetReturnPcOffsetInBytes(); |
| 123 | return *reinterpret_cast<uintptr_t*>(pc_addr); |
| 124 | } |
| 125 | |
| 126 | void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 127 | AbstractMethod** sp = GetCurrentQuickFrame(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 128 | CHECK(sp != NULL); |
| 129 | byte* pc_addr = reinterpret_cast<byte*>(sp) + GetMethod()->GetReturnPcOffsetInBytes(); |
| 130 | *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc; |
| 131 | } |
| 132 | |
| 133 | size_t StackVisitor::ComputeNumFrames() const { |
| 134 | struct NumFramesVisitor : public StackVisitor { |
| 135 | explicit NumFramesVisitor(const ManagedStack* stack, |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 136 | const std::vector<InstrumentationStackFrame>* instrumentation_stack) |
| 137 | : StackVisitor(stack, instrumentation_stack, NULL), frames(0) {} |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 138 | |
| 139 | virtual bool VisitFrame() { |
| 140 | frames++; |
| 141 | return true; |
| 142 | } |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 143 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 144 | size_t frames; |
| 145 | }; |
| 146 | |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 147 | NumFramesVisitor visitor(stack_start_, instrumentation_stack_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 148 | visitor.WalkStack(true); |
| 149 | return visitor.frames; |
| 150 | } |
| 151 | |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame^] | 152 | std::string StackVisitor::DescribeLocation() const { |
| 153 | std::string result("Visiting method '"); |
| 154 | result += PrettyMethod(GetMethod()); |
| 155 | result += StringPrintf("' at dex PC 0x%04zx", GetDexPc()); |
| 156 | if (!IsShadowFrame()) { |
| 157 | result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc())); |
| 158 | } |
| 159 | return result; |
| 160 | } |
| 161 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 162 | void StackVisitor::SanityCheckFrame() const { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 163 | #ifndef NDEBUG |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 164 | AbstractMethod* method = GetMethod(); |
| 165 | CHECK(method->GetClass() == AbstractMethod::GetMethodClass() || |
| 166 | method->GetClass() == AbstractMethod::GetConstructorClass()); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 167 | if (cur_quick_frame_ != NULL) { |
buzbee | 8320f38 | 2012-09-11 16:29:42 -0700 | [diff] [blame] | 168 | method->AssertPcIsWithinCode(cur_quick_frame_pc_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 169 | // Frame sanity. |
| 170 | size_t frame_size = method->GetFrameSizeInBytes(); |
| 171 | CHECK_NE(frame_size, 0u); |
| 172 | CHECK_LT(frame_size, 1024u); |
| 173 | size_t return_pc_offset = method->GetReturnPcOffsetInBytes(); |
| 174 | CHECK_LT(return_pc_offset, frame_size); |
| 175 | } |
| 176 | #endif |
| 177 | } |
| 178 | |
| 179 | void StackVisitor::WalkStack(bool include_transitions) { |
| 180 | bool method_tracing_active = Runtime::Current()->IsMethodTracingActive(); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 181 | uint32_t instrumentation_stack_depth = 0; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 182 | for (const ManagedStack* current_fragment = stack_start_; current_fragment != NULL; |
| 183 | current_fragment = current_fragment->GetLink()) { |
| 184 | cur_shadow_frame_ = current_fragment->GetTopShadowFrame(); |
| 185 | cur_quick_frame_ = current_fragment->GetTopQuickFrame(); |
| 186 | cur_quick_frame_pc_ = current_fragment->GetTopQuickFramePc(); |
| 187 | if (cur_quick_frame_ != NULL) { // Handle quick stack frames. |
| 188 | // Can't be both a shadow and a quick fragment. |
| 189 | DCHECK(current_fragment->GetTopShadowFrame() == NULL); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 190 | AbstractMethod* method = *cur_quick_frame_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 191 | do { |
| 192 | SanityCheckFrame(); |
| 193 | bool should_continue = VisitFrame(); |
| 194 | if (UNLIKELY(!should_continue)) { |
| 195 | return; |
| 196 | } |
| 197 | if (context_ != NULL) { |
| 198 | context_->FillCalleeSaves(*this); |
| 199 | } |
| 200 | size_t frame_size = method->GetFrameSizeInBytes(); |
| 201 | // Compute PC for next stack frame from return PC. |
| 202 | size_t return_pc_offset = method->GetReturnPcOffsetInBytes(); |
| 203 | byte* return_pc_addr = reinterpret_cast<byte*>(cur_quick_frame_) + return_pc_offset; |
| 204 | uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr); |
| 205 | if (UNLIKELY(method_tracing_active)) { |
| 206 | // While profiling, the return pc is restored from the side stack, except when walking |
| 207 | // the stack for an exception where the side stack will be unwound in VisitFrame. |
| 208 | // 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] | 209 | if (IsInstrumentationExitPc(return_pc) && !include_transitions) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 210 | // TODO: unify trace and managed stack. |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 211 | InstrumentationStackFrame instrumentation_frame = GetInstrumentationStackFrame(instrumentation_stack_depth); |
| 212 | instrumentation_stack_depth++; |
| 213 | CHECK(instrumentation_frame.method_ == GetMethod()) << "Excepted: " << PrettyMethod(method) |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 214 | << " Found: " << PrettyMethod(GetMethod()); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 215 | return_pc = instrumentation_frame.return_pc_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | cur_quick_frame_pc_ = return_pc; |
| 219 | byte* next_frame = reinterpret_cast<byte*>(cur_quick_frame_) + frame_size; |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 220 | cur_quick_frame_ = reinterpret_cast<AbstractMethod**>(next_frame); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 221 | cur_depth_++; |
| 222 | method = *cur_quick_frame_; |
| 223 | } while (method != NULL); |
| 224 | } else if (cur_shadow_frame_ != NULL) { |
| 225 | do { |
| 226 | SanityCheckFrame(); |
| 227 | bool should_continue = VisitFrame(); |
| 228 | if (UNLIKELY(!should_continue)) { |
| 229 | return; |
| 230 | } |
| 231 | cur_depth_++; |
| 232 | cur_shadow_frame_ = cur_shadow_frame_->GetLink(); |
| 233 | } while(cur_shadow_frame_ != NULL); |
| 234 | } |
| 235 | cur_depth_++; |
| 236 | if (include_transitions) { |
| 237 | bool should_continue = VisitFrame(); |
| 238 | if (!should_continue) { |
| 239 | return; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 245 | } // namespace art |