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