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)) { |
Ian Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 102 | UNIMPLEMENTED(FATAL); |
| 103 | } |
| 104 | const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem(); |
| 105 | DCHECK(code_item != NULL) << PrettyMethod(m); // Can't be NULL or how would we compile its instructions? |
| 106 | uint32_t core_spills = m->GetCoreSpillMask(); |
| 107 | uint32_t fp_spills = m->GetFpSpillMask(); |
| 108 | size_t frame_size = m->GetFrameSizeInBytes(); |
| 109 | int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg); |
| 110 | byte* vreg_addr = reinterpret_cast<byte*>(GetCurrentQuickFrame()) + offset; |
| 111 | *reinterpret_cast<uint32_t*>(vreg_addr) = new_value; |
| 112 | } else { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 113 | return cur_shadow_frame_->SetVReg(vreg, new_value); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 114 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | uintptr_t StackVisitor::GetGPR(uint32_t reg) const { |
Ian Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 118 | DCHECK (cur_quick_frame_ != NULL) << "This is a quick frame routine"; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 119 | return context_->GetGPR(reg); |
| 120 | } |
| 121 | |
| 122 | uintptr_t StackVisitor::GetReturnPc() const { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 123 | AbstractMethod** sp = GetCurrentQuickFrame(); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 124 | DCHECK(sp != NULL); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 125 | byte* pc_addr = reinterpret_cast<byte*>(sp) + GetMethod()->GetReturnPcOffsetInBytes(); |
| 126 | return *reinterpret_cast<uintptr_t*>(pc_addr); |
| 127 | } |
| 128 | |
| 129 | void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 130 | AbstractMethod** sp = GetCurrentQuickFrame(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 131 | CHECK(sp != NULL); |
| 132 | byte* pc_addr = reinterpret_cast<byte*>(sp) + GetMethod()->GetReturnPcOffsetInBytes(); |
| 133 | *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc; |
| 134 | } |
| 135 | |
| 136 | size_t StackVisitor::ComputeNumFrames() const { |
| 137 | struct NumFramesVisitor : public StackVisitor { |
| 138 | explicit NumFramesVisitor(const ManagedStack* stack, |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 139 | const std::vector<InstrumentationStackFrame>* instrumentation_stack) |
| 140 | : StackVisitor(stack, instrumentation_stack, NULL), frames(0) {} |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 141 | |
| 142 | virtual bool VisitFrame() { |
| 143 | frames++; |
| 144 | return true; |
| 145 | } |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 146 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 147 | size_t frames; |
| 148 | }; |
| 149 | |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 150 | NumFramesVisitor visitor(stack_start_, instrumentation_stack_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 151 | visitor.WalkStack(true); |
| 152 | return visitor.frames; |
| 153 | } |
| 154 | |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame] | 155 | std::string StackVisitor::DescribeLocation() const { |
| 156 | std::string result("Visiting method '"); |
| 157 | result += PrettyMethod(GetMethod()); |
| 158 | result += StringPrintf("' at dex PC 0x%04zx", GetDexPc()); |
| 159 | if (!IsShadowFrame()) { |
| 160 | result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc())); |
| 161 | } |
| 162 | return result; |
| 163 | } |
| 164 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 165 | void StackVisitor::SanityCheckFrame() const { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 166 | #ifndef NDEBUG |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 167 | AbstractMethod* method = GetMethod(); |
| 168 | CHECK(method->GetClass() == AbstractMethod::GetMethodClass() || |
| 169 | method->GetClass() == AbstractMethod::GetConstructorClass()); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 170 | if (cur_quick_frame_ != NULL) { |
buzbee | 8320f38 | 2012-09-11 16:29:42 -0700 | [diff] [blame] | 171 | method->AssertPcIsWithinCode(cur_quick_frame_pc_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 172 | // Frame sanity. |
| 173 | size_t frame_size = method->GetFrameSizeInBytes(); |
| 174 | CHECK_NE(frame_size, 0u); |
| 175 | CHECK_LT(frame_size, 1024u); |
| 176 | size_t return_pc_offset = method->GetReturnPcOffsetInBytes(); |
| 177 | CHECK_LT(return_pc_offset, frame_size); |
| 178 | } |
| 179 | #endif |
| 180 | } |
| 181 | |
| 182 | void StackVisitor::WalkStack(bool include_transitions) { |
| 183 | bool method_tracing_active = Runtime::Current()->IsMethodTracingActive(); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 184 | uint32_t instrumentation_stack_depth = 0; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 185 | for (const ManagedStack* current_fragment = stack_start_; current_fragment != NULL; |
| 186 | current_fragment = current_fragment->GetLink()) { |
| 187 | cur_shadow_frame_ = current_fragment->GetTopShadowFrame(); |
| 188 | cur_quick_frame_ = current_fragment->GetTopQuickFrame(); |
| 189 | cur_quick_frame_pc_ = current_fragment->GetTopQuickFramePc(); |
| 190 | if (cur_quick_frame_ != NULL) { // Handle quick stack frames. |
| 191 | // Can't be both a shadow and a quick fragment. |
| 192 | DCHECK(current_fragment->GetTopShadowFrame() == NULL); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 193 | AbstractMethod* method = *cur_quick_frame_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 194 | do { |
| 195 | SanityCheckFrame(); |
| 196 | bool should_continue = VisitFrame(); |
| 197 | if (UNLIKELY(!should_continue)) { |
| 198 | return; |
| 199 | } |
| 200 | if (context_ != NULL) { |
| 201 | context_->FillCalleeSaves(*this); |
| 202 | } |
| 203 | size_t frame_size = method->GetFrameSizeInBytes(); |
| 204 | // Compute PC for next stack frame from return PC. |
| 205 | size_t return_pc_offset = method->GetReturnPcOffsetInBytes(); |
| 206 | byte* return_pc_addr = reinterpret_cast<byte*>(cur_quick_frame_) + return_pc_offset; |
| 207 | uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr); |
| 208 | if (UNLIKELY(method_tracing_active)) { |
| 209 | // While profiling, the return pc is restored from the side stack, except when walking |
| 210 | // the stack for an exception where the side stack will be unwound in VisitFrame. |
| 211 | // 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] | 212 | if (IsInstrumentationExitPc(return_pc) && !include_transitions) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 213 | // TODO: unify trace and managed stack. |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 214 | InstrumentationStackFrame instrumentation_frame = GetInstrumentationStackFrame(instrumentation_stack_depth); |
| 215 | instrumentation_stack_depth++; |
| 216 | CHECK(instrumentation_frame.method_ == GetMethod()) << "Excepted: " << PrettyMethod(method) |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 217 | << " Found: " << PrettyMethod(GetMethod()); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 218 | return_pc = instrumentation_frame.return_pc_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | cur_quick_frame_pc_ = return_pc; |
| 222 | byte* next_frame = reinterpret_cast<byte*>(cur_quick_frame_) + frame_size; |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 223 | cur_quick_frame_ = reinterpret_cast<AbstractMethod**>(next_frame); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 224 | cur_depth_++; |
| 225 | method = *cur_quick_frame_; |
| 226 | } while (method != NULL); |
| 227 | } else if (cur_shadow_frame_ != NULL) { |
| 228 | do { |
| 229 | SanityCheckFrame(); |
| 230 | bool should_continue = VisitFrame(); |
| 231 | if (UNLIKELY(!should_continue)) { |
| 232 | return; |
| 233 | } |
| 234 | cur_depth_++; |
| 235 | cur_shadow_frame_ = cur_shadow_frame_->GetLink(); |
| 236 | } while(cur_shadow_frame_ != NULL); |
| 237 | } |
| 238 | cur_depth_++; |
| 239 | if (include_transitions) { |
| 240 | bool should_continue = VisitFrame(); |
| 241 | if (!should_continue) { |
| 242 | return; |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 248 | } // namespace art |