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 | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 152 | void StackVisitor::SanityCheckFrame() const { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 153 | #ifndef NDEBUG |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 154 | AbstractMethod* method = GetMethod(); |
| 155 | CHECK(method->GetClass() == AbstractMethod::GetMethodClass() || |
| 156 | method->GetClass() == AbstractMethod::GetConstructorClass()); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 157 | if (cur_quick_frame_ != NULL) { |
buzbee | 8320f38 | 2012-09-11 16:29:42 -0700 | [diff] [blame] | 158 | method->AssertPcIsWithinCode(cur_quick_frame_pc_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 159 | // Frame sanity. |
| 160 | size_t frame_size = method->GetFrameSizeInBytes(); |
| 161 | CHECK_NE(frame_size, 0u); |
| 162 | CHECK_LT(frame_size, 1024u); |
| 163 | size_t return_pc_offset = method->GetReturnPcOffsetInBytes(); |
| 164 | CHECK_LT(return_pc_offset, frame_size); |
| 165 | } |
| 166 | #endif |
| 167 | } |
| 168 | |
| 169 | void StackVisitor::WalkStack(bool include_transitions) { |
| 170 | bool method_tracing_active = Runtime::Current()->IsMethodTracingActive(); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 171 | uint32_t instrumentation_stack_depth = 0; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 172 | for (const ManagedStack* current_fragment = stack_start_; current_fragment != NULL; |
| 173 | current_fragment = current_fragment->GetLink()) { |
| 174 | cur_shadow_frame_ = current_fragment->GetTopShadowFrame(); |
| 175 | cur_quick_frame_ = current_fragment->GetTopQuickFrame(); |
| 176 | cur_quick_frame_pc_ = current_fragment->GetTopQuickFramePc(); |
| 177 | if (cur_quick_frame_ != NULL) { // Handle quick stack frames. |
| 178 | // Can't be both a shadow and a quick fragment. |
| 179 | DCHECK(current_fragment->GetTopShadowFrame() == NULL); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 180 | AbstractMethod* method = *cur_quick_frame_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 181 | do { |
| 182 | SanityCheckFrame(); |
| 183 | bool should_continue = VisitFrame(); |
| 184 | if (UNLIKELY(!should_continue)) { |
| 185 | return; |
| 186 | } |
| 187 | if (context_ != NULL) { |
| 188 | context_->FillCalleeSaves(*this); |
| 189 | } |
| 190 | size_t frame_size = method->GetFrameSizeInBytes(); |
| 191 | // Compute PC for next stack frame from return PC. |
| 192 | size_t return_pc_offset = method->GetReturnPcOffsetInBytes(); |
| 193 | byte* return_pc_addr = reinterpret_cast<byte*>(cur_quick_frame_) + return_pc_offset; |
| 194 | uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr); |
| 195 | if (UNLIKELY(method_tracing_active)) { |
| 196 | // While profiling, the return pc is restored from the side stack, except when walking |
| 197 | // the stack for an exception where the side stack will be unwound in VisitFrame. |
| 198 | // 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] | 199 | if (IsInstrumentationExitPc(return_pc) && !include_transitions) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 200 | // TODO: unify trace and managed stack. |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 201 | InstrumentationStackFrame instrumentation_frame = GetInstrumentationStackFrame(instrumentation_stack_depth); |
| 202 | instrumentation_stack_depth++; |
| 203 | CHECK(instrumentation_frame.method_ == GetMethod()) << "Excepted: " << PrettyMethod(method) |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 204 | << " Found: " << PrettyMethod(GetMethod()); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 205 | return_pc = instrumentation_frame.return_pc_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | cur_quick_frame_pc_ = return_pc; |
| 209 | byte* next_frame = reinterpret_cast<byte*>(cur_quick_frame_) + frame_size; |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 210 | cur_quick_frame_ = reinterpret_cast<AbstractMethod**>(next_frame); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 211 | cur_depth_++; |
| 212 | method = *cur_quick_frame_; |
| 213 | } while (method != NULL); |
| 214 | } else if (cur_shadow_frame_ != NULL) { |
| 215 | do { |
| 216 | SanityCheckFrame(); |
| 217 | bool should_continue = VisitFrame(); |
| 218 | if (UNLIKELY(!should_continue)) { |
| 219 | return; |
| 220 | } |
| 221 | cur_depth_++; |
| 222 | cur_shadow_frame_ = cur_shadow_frame_->GetLink(); |
| 223 | } while(cur_shadow_frame_ != NULL); |
| 224 | } |
| 225 | cur_depth_++; |
| 226 | if (include_transitions) { |
| 227 | bool should_continue = VisitFrame(); |
| 228 | if (!should_continue) { |
| 229 | return; |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 235 | } // namespace art |