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 | void ManagedStack::PushManagedStackFragment(ManagedStack* fragment) { |
| 28 | // Copy this top fragment into given fragment. |
| 29 | memcpy(fragment, this, sizeof(ManagedStack)); |
| 30 | // Clear this fragment, which has become the top. |
| 31 | memset(this, 0, sizeof(ManagedStack)); |
| 32 | // Link our top fragment onto the given fragment. |
| 33 | link_ = fragment; |
| 34 | } |
| 35 | |
| 36 | void ManagedStack::PopManagedStackFragment(const ManagedStack& fragment) { |
| 37 | DCHECK(&fragment == link_); |
| 38 | // Copy this given fragment back to the top. |
| 39 | memcpy(this, &fragment, sizeof(ManagedStack)); |
| 40 | } |
| 41 | |
| 42 | size_t ManagedStack::NumShadowFrameReferences() const { |
| 43 | size_t count = 0; |
| 44 | for (const ManagedStack* current_fragment = this; current_fragment != NULL; |
| 45 | current_fragment = current_fragment->GetLink()) { |
| 46 | for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != NULL; |
| 47 | current_frame = current_frame->GetLink()) { |
| 48 | count += current_frame->NumberOfReferences(); |
| 49 | } |
| 50 | } |
| 51 | return count; |
| 52 | } |
| 53 | |
| 54 | bool ManagedStack::ShadowFramesContain(Object** shadow_frame_entry) const { |
| 55 | for (const ManagedStack* current_fragment = this; current_fragment != NULL; |
| 56 | current_fragment = current_fragment->GetLink()) { |
| 57 | for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != NULL; |
| 58 | current_frame = current_frame->GetLink()) { |
| 59 | if (current_frame->Contains(shadow_frame_entry)) { |
| 60 | return true; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | uint32_t StackVisitor::GetDexPc() const { |
| 68 | if (cur_shadow_frame_ != NULL) { |
| 69 | return cur_shadow_frame_->GetDexPC(); |
| 70 | } else if (cur_quick_frame_ != NULL) { |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame^] | 71 | return GetMethod()->ToDexPc(cur_quick_frame_pc_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 72 | } else { |
| 73 | return 0; |
| 74 | } |
| 75 | } |
| 76 | |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame^] | 77 | size_t StackVisitor::GetNativePcOffset() const { |
| 78 | DCHECK(!IsShadowFrame()); |
| 79 | return GetMethod()->NativePcOffset(cur_quick_frame_pc_); |
| 80 | } |
| 81 | |
| 82 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 83 | uint32_t StackVisitor::GetVReg(Method* m, int vreg) const { |
Ian Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 84 | if (cur_quick_frame_ != NULL) { |
| 85 | DCHECK(context_ != NULL); // You can't reliably read registers without a context. |
| 86 | DCHECK(m == GetMethod()); |
| 87 | uint32_t core_spills = m->GetCoreSpillMask(); |
| 88 | const VmapTable vmap_table(m->GetVmapTableRaw()); |
| 89 | uint32_t vmap_offset; |
| 90 | // TODO: IsInContext stops before spotting floating point registers. |
| 91 | if (vmap_table.IsInContext(vreg, vmap_offset)) { |
| 92 | // Compute the register we need to load from the context. |
| 93 | uint32_t spill_mask = core_spills; |
| 94 | CHECK_LT(vmap_offset, static_cast<uint32_t>(__builtin_popcount(spill_mask))); |
| 95 | uint32_t matches = 0; |
| 96 | uint32_t spill_shifts = 0; |
| 97 | while (matches != (vmap_offset + 1)) { |
| 98 | DCHECK_NE(spill_mask, 0u); |
| 99 | matches += spill_mask & 1; // Add 1 if the low bit is set. |
| 100 | spill_mask >>= 1; |
| 101 | spill_shifts++; |
| 102 | } |
| 103 | spill_shifts--; // Wind back one as we want the last match. |
| 104 | return GetGPR(spill_shifts); |
| 105 | } else { |
| 106 | const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem(); |
| 107 | DCHECK(code_item != NULL) << PrettyMethod(m); // Can't be NULL or how would we compile its instructions? |
| 108 | uint32_t fp_spills = m->GetFpSpillMask(); |
| 109 | size_t frame_size = m->GetFrameSizeInBytes(); |
| 110 | return GetVReg(cur_quick_frame_, code_item, core_spills, fp_spills, frame_size, vreg); |
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 | } else { |
Ian Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 113 | LOG(FATAL) << "Unimplemented - shadow frame GetVReg"; |
| 114 | return 0; // Keep GCC happy. |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | void StackVisitor::SetVReg(Method* m, int vreg, uint32_t new_value) { |
Ian Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 119 | if (cur_quick_frame_ != NULL) { |
| 120 | DCHECK(context_ != NULL); // You can't reliably write registers without a context. |
| 121 | DCHECK(m == GetMethod()); |
| 122 | const VmapTable vmap_table(m->GetVmapTableRaw()); |
| 123 | uint32_t vmap_offset; |
| 124 | // TODO: IsInContext stops before spotting floating point registers. |
| 125 | if (vmap_table.IsInContext(vreg, vmap_offset)) { |
| 126 | UNIMPLEMENTED(FATAL); |
| 127 | } |
| 128 | const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem(); |
| 129 | DCHECK(code_item != NULL) << PrettyMethod(m); // Can't be NULL or how would we compile its instructions? |
| 130 | uint32_t core_spills = m->GetCoreSpillMask(); |
| 131 | uint32_t fp_spills = m->GetFpSpillMask(); |
| 132 | size_t frame_size = m->GetFrameSizeInBytes(); |
| 133 | int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg); |
| 134 | byte* vreg_addr = reinterpret_cast<byte*>(GetCurrentQuickFrame()) + offset; |
| 135 | *reinterpret_cast<uint32_t*>(vreg_addr) = new_value; |
| 136 | } else { |
| 137 | LOG(FATAL) << "Unimplemented - shadow frame SetVReg"; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 138 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | uintptr_t StackVisitor::GetGPR(uint32_t reg) const { |
Ian Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 142 | DCHECK (cur_quick_frame_ != NULL) << "This is a quick frame routine"; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 143 | return context_->GetGPR(reg); |
| 144 | } |
| 145 | |
| 146 | uintptr_t StackVisitor::GetReturnPc() const { |
| 147 | Method** sp = GetCurrentQuickFrame(); |
| 148 | CHECK(sp != NULL); |
| 149 | byte* pc_addr = reinterpret_cast<byte*>(sp) + GetMethod()->GetReturnPcOffsetInBytes(); |
| 150 | return *reinterpret_cast<uintptr_t*>(pc_addr); |
| 151 | } |
| 152 | |
| 153 | void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) { |
| 154 | Method** sp = GetCurrentQuickFrame(); |
| 155 | CHECK(sp != NULL); |
| 156 | byte* pc_addr = reinterpret_cast<byte*>(sp) + GetMethod()->GetReturnPcOffsetInBytes(); |
| 157 | *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc; |
| 158 | } |
| 159 | |
| 160 | size_t StackVisitor::ComputeNumFrames() const { |
| 161 | struct NumFramesVisitor : public StackVisitor { |
| 162 | explicit NumFramesVisitor(const ManagedStack* stack, |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 163 | const std::vector<TraceStackFrame>* trace_stack) |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 164 | : StackVisitor(stack, trace_stack, NULL), frames(0) {} |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 165 | |
| 166 | virtual bool VisitFrame() { |
| 167 | frames++; |
| 168 | return true; |
| 169 | } |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 170 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 171 | size_t frames; |
| 172 | }; |
| 173 | |
| 174 | NumFramesVisitor visitor(stack_start_, trace_stack_); |
| 175 | visitor.WalkStack(true); |
| 176 | return visitor.frames; |
| 177 | } |
| 178 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 179 | void StackVisitor::SanityCheckFrame() const { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 180 | #ifndef NDEBUG |
| 181 | Method* method = GetMethod(); |
| 182 | CHECK(method->GetClass() == Method::GetMethodClass() || |
| 183 | method->GetClass() == Method::GetConstructorClass()); |
| 184 | if (cur_quick_frame_ != NULL) { |
buzbee | 8320f38 | 2012-09-11 16:29:42 -0700 | [diff] [blame] | 185 | method->AssertPcIsWithinCode(cur_quick_frame_pc_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 186 | // Frame sanity. |
| 187 | size_t frame_size = method->GetFrameSizeInBytes(); |
| 188 | CHECK_NE(frame_size, 0u); |
| 189 | CHECK_LT(frame_size, 1024u); |
| 190 | size_t return_pc_offset = method->GetReturnPcOffsetInBytes(); |
| 191 | CHECK_LT(return_pc_offset, frame_size); |
| 192 | } |
| 193 | #endif |
| 194 | } |
| 195 | |
| 196 | void StackVisitor::WalkStack(bool include_transitions) { |
| 197 | bool method_tracing_active = Runtime::Current()->IsMethodTracingActive(); |
| 198 | uint32_t trace_stack_depth = 0; |
| 199 | for (const ManagedStack* current_fragment = stack_start_; current_fragment != NULL; |
| 200 | current_fragment = current_fragment->GetLink()) { |
| 201 | cur_shadow_frame_ = current_fragment->GetTopShadowFrame(); |
| 202 | cur_quick_frame_ = current_fragment->GetTopQuickFrame(); |
| 203 | cur_quick_frame_pc_ = current_fragment->GetTopQuickFramePc(); |
| 204 | if (cur_quick_frame_ != NULL) { // Handle quick stack frames. |
| 205 | // Can't be both a shadow and a quick fragment. |
| 206 | DCHECK(current_fragment->GetTopShadowFrame() == NULL); |
| 207 | Method* method = *cur_quick_frame_; |
| 208 | do { |
| 209 | SanityCheckFrame(); |
| 210 | bool should_continue = VisitFrame(); |
| 211 | if (UNLIKELY(!should_continue)) { |
| 212 | return; |
| 213 | } |
| 214 | if (context_ != NULL) { |
| 215 | context_->FillCalleeSaves(*this); |
| 216 | } |
| 217 | size_t frame_size = method->GetFrameSizeInBytes(); |
| 218 | // Compute PC for next stack frame from return PC. |
| 219 | size_t return_pc_offset = method->GetReturnPcOffsetInBytes(); |
| 220 | byte* return_pc_addr = reinterpret_cast<byte*>(cur_quick_frame_) + return_pc_offset; |
| 221 | uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr); |
| 222 | if (UNLIKELY(method_tracing_active)) { |
| 223 | // While profiling, the return pc is restored from the side stack, except when walking |
| 224 | // the stack for an exception where the side stack will be unwound in VisitFrame. |
| 225 | // TODO: stop using include_transitions as a proxy for is this the catch block visitor. |
| 226 | if (IsTraceExitPc(return_pc) && !include_transitions) { |
| 227 | // TODO: unify trace and managed stack. |
| 228 | TraceStackFrame trace_frame = GetTraceStackFrame(trace_stack_depth); |
| 229 | trace_stack_depth++; |
| 230 | CHECK(trace_frame.method_ == GetMethod()) << "Excepted: " << PrettyMethod(method) |
| 231 | << " Found: " << PrettyMethod(GetMethod()); |
| 232 | return_pc = trace_frame.return_pc_; |
| 233 | } |
| 234 | } |
| 235 | cur_quick_frame_pc_ = return_pc; |
| 236 | byte* next_frame = reinterpret_cast<byte*>(cur_quick_frame_) + frame_size; |
| 237 | cur_quick_frame_ = reinterpret_cast<Method**>(next_frame); |
| 238 | cur_depth_++; |
| 239 | method = *cur_quick_frame_; |
| 240 | } while (method != NULL); |
| 241 | } else if (cur_shadow_frame_ != NULL) { |
| 242 | do { |
| 243 | SanityCheckFrame(); |
| 244 | bool should_continue = VisitFrame(); |
| 245 | if (UNLIKELY(!should_continue)) { |
| 246 | return; |
| 247 | } |
| 248 | cur_depth_++; |
| 249 | cur_shadow_frame_ = cur_shadow_frame_->GetLink(); |
| 250 | } while(cur_shadow_frame_ != NULL); |
| 251 | } |
| 252 | cur_depth_++; |
| 253 | if (include_transitions) { |
| 254 | bool should_continue = VisitFrame(); |
| 255 | if (!should_continue) { |
| 256 | return; |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 262 | } // namespace art |