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 | |
| 67 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 68 | uint32_t StackVisitor::GetVReg(AbstractMethod* m, int vreg) const { |
Ian Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 69 | if (cur_quick_frame_ != NULL) { |
| 70 | DCHECK(context_ != NULL); // You can't reliably read registers without a context. |
| 71 | DCHECK(m == GetMethod()); |
| 72 | uint32_t core_spills = m->GetCoreSpillMask(); |
| 73 | const VmapTable vmap_table(m->GetVmapTableRaw()); |
| 74 | uint32_t vmap_offset; |
| 75 | // TODO: IsInContext stops before spotting floating point registers. |
| 76 | if (vmap_table.IsInContext(vreg, vmap_offset)) { |
| 77 | // Compute the register we need to load from the context. |
| 78 | uint32_t spill_mask = core_spills; |
| 79 | CHECK_LT(vmap_offset, static_cast<uint32_t>(__builtin_popcount(spill_mask))); |
| 80 | uint32_t matches = 0; |
| 81 | uint32_t spill_shifts = 0; |
| 82 | while (matches != (vmap_offset + 1)) { |
| 83 | DCHECK_NE(spill_mask, 0u); |
| 84 | matches += spill_mask & 1; // Add 1 if the low bit is set. |
| 85 | spill_mask >>= 1; |
| 86 | spill_shifts++; |
| 87 | } |
| 88 | spill_shifts--; // Wind back one as we want the last match. |
| 89 | return GetGPR(spill_shifts); |
| 90 | } else { |
| 91 | const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem(); |
| 92 | DCHECK(code_item != NULL) << PrettyMethod(m); // Can't be NULL or how would we compile its instructions? |
| 93 | uint32_t fp_spills = m->GetFpSpillMask(); |
| 94 | size_t frame_size = m->GetFrameSizeInBytes(); |
| 95 | 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] | 96 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 97 | } else { |
Ian Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 98 | LOG(FATAL) << "Unimplemented - shadow frame GetVReg"; |
| 99 | return 0; // Keep GCC happy. |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 103 | void StackVisitor::SetVReg(AbstractMethod* m, int vreg, uint32_t new_value) { |
Ian Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 104 | if (cur_quick_frame_ != NULL) { |
| 105 | DCHECK(context_ != NULL); // You can't reliably write registers without a context. |
| 106 | DCHECK(m == GetMethod()); |
| 107 | const VmapTable vmap_table(m->GetVmapTableRaw()); |
| 108 | uint32_t vmap_offset; |
| 109 | // TODO: IsInContext stops before spotting floating point registers. |
| 110 | if (vmap_table.IsInContext(vreg, vmap_offset)) { |
| 111 | UNIMPLEMENTED(FATAL); |
| 112 | } |
| 113 | const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem(); |
| 114 | DCHECK(code_item != NULL) << PrettyMethod(m); // Can't be NULL or how would we compile its instructions? |
| 115 | uint32_t core_spills = m->GetCoreSpillMask(); |
| 116 | uint32_t fp_spills = m->GetFpSpillMask(); |
| 117 | size_t frame_size = m->GetFrameSizeInBytes(); |
| 118 | int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg); |
| 119 | byte* vreg_addr = reinterpret_cast<byte*>(GetCurrentQuickFrame()) + offset; |
| 120 | *reinterpret_cast<uint32_t*>(vreg_addr) = new_value; |
| 121 | } else { |
| 122 | LOG(FATAL) << "Unimplemented - shadow frame SetVReg"; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 123 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | uintptr_t StackVisitor::GetGPR(uint32_t reg) const { |
Ian Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 127 | DCHECK (cur_quick_frame_ != NULL) << "This is a quick frame routine"; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 128 | return context_->GetGPR(reg); |
| 129 | } |
| 130 | |
| 131 | uintptr_t StackVisitor::GetReturnPc() const { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 132 | AbstractMethod** sp = GetCurrentQuickFrame(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 133 | CHECK(sp != NULL); |
| 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, |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 148 | const std::vector<TraceStackFrame>* trace_stack) |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 149 | : StackVisitor(stack, trace_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 | |
| 159 | NumFramesVisitor visitor(stack_start_, trace_stack_); |
| 160 | visitor.WalkStack(true); |
| 161 | return visitor.frames; |
| 162 | } |
| 163 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 164 | void StackVisitor::SanityCheckFrame() const { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 165 | #ifndef NDEBUG |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 166 | AbstractMethod* method = GetMethod(); |
| 167 | CHECK(method->GetClass() == AbstractMethod::GetMethodClass() || |
| 168 | method->GetClass() == AbstractMethod::GetConstructorClass()); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 169 | if (cur_quick_frame_ != NULL) { |
buzbee | 8320f38 | 2012-09-11 16:29:42 -0700 | [diff] [blame] | 170 | method->AssertPcIsWithinCode(cur_quick_frame_pc_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 171 | // Frame sanity. |
| 172 | size_t frame_size = method->GetFrameSizeInBytes(); |
| 173 | CHECK_NE(frame_size, 0u); |
| 174 | CHECK_LT(frame_size, 1024u); |
| 175 | size_t return_pc_offset = method->GetReturnPcOffsetInBytes(); |
| 176 | CHECK_LT(return_pc_offset, frame_size); |
| 177 | } |
| 178 | #endif |
| 179 | } |
| 180 | |
| 181 | void StackVisitor::WalkStack(bool include_transitions) { |
| 182 | bool method_tracing_active = Runtime::Current()->IsMethodTracingActive(); |
| 183 | uint32_t trace_stack_depth = 0; |
| 184 | for (const ManagedStack* current_fragment = stack_start_; current_fragment != NULL; |
| 185 | current_fragment = current_fragment->GetLink()) { |
| 186 | cur_shadow_frame_ = current_fragment->GetTopShadowFrame(); |
| 187 | cur_quick_frame_ = current_fragment->GetTopQuickFrame(); |
| 188 | cur_quick_frame_pc_ = current_fragment->GetTopQuickFramePc(); |
| 189 | if (cur_quick_frame_ != NULL) { // Handle quick stack frames. |
| 190 | // Can't be both a shadow and a quick fragment. |
| 191 | DCHECK(current_fragment->GetTopShadowFrame() == NULL); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 192 | AbstractMethod* method = *cur_quick_frame_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 193 | do { |
| 194 | SanityCheckFrame(); |
| 195 | bool should_continue = VisitFrame(); |
| 196 | if (UNLIKELY(!should_continue)) { |
| 197 | return; |
| 198 | } |
| 199 | if (context_ != NULL) { |
| 200 | context_->FillCalleeSaves(*this); |
| 201 | } |
| 202 | size_t frame_size = method->GetFrameSizeInBytes(); |
| 203 | // Compute PC for next stack frame from return PC. |
| 204 | size_t return_pc_offset = method->GetReturnPcOffsetInBytes(); |
| 205 | byte* return_pc_addr = reinterpret_cast<byte*>(cur_quick_frame_) + return_pc_offset; |
| 206 | uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr); |
| 207 | if (UNLIKELY(method_tracing_active)) { |
| 208 | // While profiling, the return pc is restored from the side stack, except when walking |
| 209 | // the stack for an exception where the side stack will be unwound in VisitFrame. |
| 210 | // TODO: stop using include_transitions as a proxy for is this the catch block visitor. |
| 211 | if (IsTraceExitPc(return_pc) && !include_transitions) { |
| 212 | // TODO: unify trace and managed stack. |
| 213 | TraceStackFrame trace_frame = GetTraceStackFrame(trace_stack_depth); |
| 214 | trace_stack_depth++; |
| 215 | CHECK(trace_frame.method_ == GetMethod()) << "Excepted: " << PrettyMethod(method) |
| 216 | << " Found: " << PrettyMethod(GetMethod()); |
| 217 | return_pc = trace_frame.return_pc_; |
| 218 | } |
| 219 | } |
| 220 | cur_quick_frame_pc_ = return_pc; |
| 221 | byte* next_frame = reinterpret_cast<byte*>(cur_quick_frame_) + frame_size; |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 222 | cur_quick_frame_ = reinterpret_cast<AbstractMethod**>(next_frame); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 223 | cur_depth_++; |
| 224 | method = *cur_quick_frame_; |
| 225 | } while (method != NULL); |
| 226 | } else if (cur_shadow_frame_ != NULL) { |
| 227 | do { |
| 228 | SanityCheckFrame(); |
| 229 | bool should_continue = VisitFrame(); |
| 230 | if (UNLIKELY(!should_continue)) { |
| 231 | return; |
| 232 | } |
| 233 | cur_depth_++; |
| 234 | cur_shadow_frame_ = cur_shadow_frame_->GetLink(); |
| 235 | } while(cur_shadow_frame_ != NULL); |
| 236 | } |
| 237 | cur_depth_++; |
| 238 | if (include_transitions) { |
| 239 | bool should_continue = VisitFrame(); |
| 240 | if (!should_continue) { |
| 241 | return; |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 247 | } // namespace art |