stack.cc revision cf4035a4c41ccfcc3e89a0cee25f5218a11b0705
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 "base/hex_dump.h" 20#include "mirror/art_method-inl.h" 21#include "mirror/class-inl.h" 22#include "mirror/object.h" 23#include "mirror/object-inl.h" 24#include "mirror/object_array-inl.h" 25#include "object_utils.h" 26#include "quick/quick_method_frame_info.h" 27#include "runtime.h" 28#include "thread.h" 29#include "thread_list.h" 30#include "throw_location.h" 31#include "verify_object-inl.h" 32#include "vmap_table.h" 33 34namespace art { 35 36mirror::Object* ShadowFrame::GetThisObject() const { 37 mirror::ArtMethod* m = GetMethod(); 38 if (m->IsStatic()) { 39 return NULL; 40 } else if (m->IsNative()) { 41 return GetVRegReference(0); 42 } else { 43 const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem(); 44 CHECK(code_item != NULL) << PrettyMethod(m); 45 uint16_t reg = code_item->registers_size_ - code_item->ins_size_; 46 return GetVRegReference(reg); 47 } 48} 49 50mirror::Object* ShadowFrame::GetThisObject(uint16_t num_ins) const { 51 mirror::ArtMethod* m = GetMethod(); 52 if (m->IsStatic()) { 53 return NULL; 54 } else { 55 return GetVRegReference(NumberOfVRegs() - num_ins); 56 } 57} 58 59ThrowLocation ShadowFrame::GetCurrentLocationForThrow() const { 60 return ThrowLocation(GetThisObject(), GetMethod(), GetDexPC()); 61} 62 63size_t ManagedStack::NumJniShadowFrameReferences() const { 64 size_t count = 0; 65 for (const ManagedStack* current_fragment = this; current_fragment != NULL; 66 current_fragment = current_fragment->GetLink()) { 67 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != NULL; 68 current_frame = current_frame->GetLink()) { 69 if (current_frame->GetMethod()->IsNative()) { 70 // The JNI ShadowFrame only contains references. (For indirect reference.) 71 count += current_frame->NumberOfVRegs(); 72 } 73 } 74 } 75 return count; 76} 77 78bool ManagedStack::ShadowFramesContain(StackReference<mirror::Object>* shadow_frame_entry) const { 79 for (const ManagedStack* current_fragment = this; current_fragment != NULL; 80 current_fragment = current_fragment->GetLink()) { 81 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != NULL; 82 current_frame = current_frame->GetLink()) { 83 if (current_frame->Contains(shadow_frame_entry)) { 84 return true; 85 } 86 } 87 } 88 return false; 89} 90 91StackVisitor::StackVisitor(Thread* thread, Context* context) 92 : thread_(thread), cur_shadow_frame_(NULL), 93 cur_quick_frame_(NULL), cur_quick_frame_pc_(0), num_frames_(0), cur_depth_(0), 94 context_(context) { 95 DCHECK(thread == Thread::Current() || thread->IsSuspended()) << *thread; 96} 97 98uint32_t StackVisitor::GetDexPc(bool abort_on_failure) const { 99 if (cur_shadow_frame_ != NULL) { 100 return cur_shadow_frame_->GetDexPC(); 101 } else if (cur_quick_frame_ != NULL) { 102 return GetMethod()->ToDexPc(cur_quick_frame_pc_, abort_on_failure); 103 } else { 104 return 0; 105 } 106} 107 108mirror::Object* StackVisitor::GetThisObject() const { 109 mirror::ArtMethod* m = GetMethod(); 110 if (m->IsStatic()) { 111 return NULL; 112 } else if (m->IsNative()) { 113 if (cur_quick_frame_ != NULL) { 114 HandleScope* hs = reinterpret_cast<HandleScope*>( 115 reinterpret_cast<char*>(cur_quick_frame_) + m->GetHandleScopeOffsetInBytes()); 116 return hs->GetReference(0); 117 } else { 118 return cur_shadow_frame_->GetVRegReference(0); 119 } 120 } else { 121 const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem(); 122 if (code_item == NULL) { 123 UNIMPLEMENTED(ERROR) << "Failed to determine this object of abstract or proxy method: " 124 << PrettyMethod(m); 125 return nullptr; 126 } else { 127 uint16_t reg = code_item->registers_size_ - code_item->ins_size_; 128 return reinterpret_cast<mirror::Object*>(GetVReg(m, reg, kReferenceVReg)); 129 } 130 } 131} 132 133size_t StackVisitor::GetNativePcOffset() const { 134 DCHECK(!IsShadowFrame()); 135 return GetMethod()->NativePcOffset(cur_quick_frame_pc_); 136} 137 138uint32_t StackVisitor::GetVReg(mirror::ArtMethod* m, uint16_t vreg, VRegKind kind) const { 139 if (cur_quick_frame_ != NULL) { 140 DCHECK(context_ != NULL); // You can't reliably read registers without a context. 141 DCHECK(m == GetMethod()); 142 const void* code_pointer = m->GetQuickOatCodePointer(); 143 DCHECK(code_pointer != nullptr); 144 const VmapTable vmap_table(m->GetVmapTable(code_pointer)); 145 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer); 146 uint32_t vmap_offset; 147 // TODO: IsInContext stops before spotting floating point registers. 148 if (vmap_table.IsInContext(vreg, kind, &vmap_offset)) { 149 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg); 150 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask(); 151 return GetGPR(vmap_table.ComputeRegister(spill_mask, vmap_offset, kind)); 152 } else { 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 return *GetVRegAddr(cur_quick_frame_, code_item, frame_info.CoreSpillMask(), 156 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg); 157 } 158 } else { 159 return cur_shadow_frame_->GetVReg(vreg); 160 } 161} 162 163void StackVisitor::SetVReg(mirror::ArtMethod* m, uint16_t vreg, uint32_t new_value, 164 VRegKind kind) { 165 if (cur_quick_frame_ != NULL) { 166 DCHECK(context_ != NULL); // You can't reliably write registers without a context. 167 DCHECK(m == GetMethod()); 168 const void* code_pointer = m->GetQuickOatCodePointer(); 169 DCHECK(code_pointer != nullptr); 170 const VmapTable vmap_table(m->GetVmapTable(code_pointer)); 171 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer); 172 uint32_t vmap_offset; 173 // TODO: IsInContext stops before spotting floating point registers. 174 if (vmap_table.IsInContext(vreg, kind, &vmap_offset)) { 175 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg); 176 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask(); 177 const uint32_t reg = vmap_table.ComputeRegister(spill_mask, vmap_offset, kReferenceVReg); 178 SetGPR(reg, new_value); 179 } else { 180 const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem(); 181 DCHECK(code_item != NULL) << PrettyMethod(m); // Can't be NULL or how would we compile its instructions? 182 int offset = GetVRegOffset(code_item, frame_info.CoreSpillMask(), frame_info.FpSpillMask(), 183 frame_info.FrameSizeInBytes(), vreg, kRuntimeISA); 184 byte* vreg_addr = reinterpret_cast<byte*>(GetCurrentQuickFrame()) + offset; 185 *reinterpret_cast<uint32_t*>(vreg_addr) = new_value; 186 } 187 } else { 188 return cur_shadow_frame_->SetVReg(vreg, new_value); 189 } 190} 191 192uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const { 193 DCHECK(cur_quick_frame_ != NULL) << "This is a quick frame routine"; 194 return context_->GetGPRAddress(reg); 195} 196 197uintptr_t StackVisitor::GetGPR(uint32_t reg) const { 198 DCHECK(cur_quick_frame_ != NULL) << "This is a quick frame routine"; 199 return context_->GetGPR(reg); 200} 201 202void StackVisitor::SetGPR(uint32_t reg, uintptr_t value) { 203 DCHECK(cur_quick_frame_ != NULL) << "This is a quick frame routine"; 204 context_->SetGPR(reg, value); 205} 206 207uintptr_t StackVisitor::GetReturnPc() const { 208 byte* sp = reinterpret_cast<byte*>(GetCurrentQuickFrame()); 209 DCHECK(sp != NULL); 210 byte* pc_addr = sp + GetMethod()->GetReturnPcOffsetInBytes(); 211 return *reinterpret_cast<uintptr_t*>(pc_addr); 212} 213 214void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) { 215 byte* sp = reinterpret_cast<byte*>(GetCurrentQuickFrame()); 216 CHECK(sp != NULL); 217 byte* pc_addr = sp + GetMethod()->GetReturnPcOffsetInBytes(); 218 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc; 219} 220 221size_t StackVisitor::ComputeNumFrames(Thread* thread) { 222 struct NumFramesVisitor : public StackVisitor { 223 explicit NumFramesVisitor(Thread* thread) 224 : StackVisitor(thread, NULL), frames(0) {} 225 226 virtual bool VisitFrame() { 227 frames++; 228 return true; 229 } 230 231 size_t frames; 232 }; 233 NumFramesVisitor visitor(thread); 234 visitor.WalkStack(true); 235 return visitor.frames; 236} 237 238void StackVisitor::DescribeStack(Thread* thread) { 239 struct DescribeStackVisitor : public StackVisitor { 240 explicit DescribeStackVisitor(Thread* thread) 241 : StackVisitor(thread, NULL) {} 242 243 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 244 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation(); 245 return true; 246 } 247 }; 248 DescribeStackVisitor visitor(thread); 249 visitor.WalkStack(true); 250} 251 252std::string StackVisitor::DescribeLocation() const { 253 std::string result("Visiting method '"); 254 mirror::ArtMethod* m = GetMethod(); 255 if (m == NULL) { 256 return "upcall"; 257 } 258 result += PrettyMethod(m); 259 result += StringPrintf("' at dex PC 0x%04x", GetDexPc()); 260 if (!IsShadowFrame()) { 261 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc())); 262 } 263 return result; 264} 265 266instrumentation::InstrumentationStackFrame& StackVisitor::GetInstrumentationStackFrame(uint32_t depth) const { 267 CHECK_LT(depth, thread_->GetInstrumentationStack()->size()); 268 return thread_->GetInstrumentationStack()->at(depth); 269} 270 271void StackVisitor::SanityCheckFrame() const { 272 if (kIsDebugBuild) { 273 mirror::ArtMethod* method = GetMethod(); 274 CHECK_EQ(method->GetClass(), mirror::ArtMethod::GetJavaLangReflectArtMethod()); 275 if (cur_quick_frame_ != nullptr) { 276 method->AssertPcIsWithinQuickCode(cur_quick_frame_pc_); 277 // Frame sanity. 278 size_t frame_size = method->GetFrameSizeInBytes(); 279 CHECK_NE(frame_size, 0u); 280 // A rough guess at an upper size we expect to see for a frame. 281 // 256 registers 282 // 2 words HandleScope overhead 283 // 3+3 register spills 284 // TODO: this seems architecture specific for the case of JNI frames. 285 // TODO: 083-compiler-regressions ManyFloatArgs shows this estimate is wrong. 286 // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word); 287 const size_t kMaxExpectedFrameSize = 2 * KB; 288 CHECK_LE(frame_size, kMaxExpectedFrameSize); 289 size_t return_pc_offset = method->GetReturnPcOffsetInBytes(); 290 CHECK_LT(return_pc_offset, frame_size); 291 } 292 } 293} 294 295void StackVisitor::WalkStack(bool include_transitions) { 296 DCHECK(thread_ == Thread::Current() || thread_->IsSuspended()); 297 CHECK_EQ(cur_depth_, 0U); 298 bool exit_stubs_installed = Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled(); 299 uint32_t instrumentation_stack_depth = 0; 300 301 for (const ManagedStack* current_fragment = thread_->GetManagedStack(); current_fragment != NULL; 302 current_fragment = current_fragment->GetLink()) { 303 cur_shadow_frame_ = current_fragment->GetTopShadowFrame(); 304 cur_quick_frame_ = current_fragment->GetTopQuickFrame(); 305 cur_quick_frame_pc_ = current_fragment->GetTopQuickFramePc(); 306 307 if (cur_quick_frame_ != NULL) { // Handle quick stack frames. 308 // Can't be both a shadow and a quick fragment. 309 DCHECK(current_fragment->GetTopShadowFrame() == NULL); 310 mirror::ArtMethod* method = cur_quick_frame_->AsMirrorPtr(); 311 while (method != NULL) { 312 SanityCheckFrame(); 313 bool should_continue = VisitFrame(); 314 if (UNLIKELY(!should_continue)) { 315 return; 316 } 317 318 if (context_ != NULL) { 319 context_->FillCalleeSaves(*this); 320 } 321 size_t frame_size = method->GetFrameSizeInBytes(); 322 // Compute PC for next stack frame from return PC. 323 size_t return_pc_offset = method->GetReturnPcOffsetInBytes(frame_size); 324 byte* return_pc_addr = reinterpret_cast<byte*>(cur_quick_frame_) + return_pc_offset; 325 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr); 326 if (UNLIKELY(exit_stubs_installed)) { 327 // While profiling, the return pc is restored from the side stack, except when walking 328 // the stack for an exception where the side stack will be unwound in VisitFrame. 329 if (GetQuickInstrumentationExitPc() == return_pc) { 330 const instrumentation::InstrumentationStackFrame& instrumentation_frame = 331 GetInstrumentationStackFrame(instrumentation_stack_depth); 332 instrumentation_stack_depth++; 333 if (GetMethod() == Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveAll)) { 334 // Skip runtime save all callee frames which are used to deliver exceptions. 335 } else if (instrumentation_frame.interpreter_entry_) { 336 mirror::ArtMethod* callee = Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs); 337 CHECK_EQ(GetMethod(), callee) << "Expected: " << PrettyMethod(callee) << " Found: " 338 << PrettyMethod(GetMethod()); 339 } else if (instrumentation_frame.method_ != GetMethod()) { 340 LOG(FATAL) << "Expected: " << PrettyMethod(instrumentation_frame.method_) 341 << " Found: " << PrettyMethod(GetMethod()); 342 } 343 if (num_frames_ != 0) { 344 // Check agreement of frame Ids only if num_frames_ is computed to avoid infinite 345 // recursion. 346 CHECK(instrumentation_frame.frame_id_ == GetFrameId()) 347 << "Expected: " << instrumentation_frame.frame_id_ 348 << " Found: " << GetFrameId(); 349 } 350 return_pc = instrumentation_frame.return_pc_; 351 } 352 } 353 cur_quick_frame_pc_ = return_pc; 354 byte* next_frame = reinterpret_cast<byte*>(cur_quick_frame_) + frame_size; 355 cur_quick_frame_ = reinterpret_cast<StackReference<mirror::ArtMethod>*>(next_frame); 356 cur_depth_++; 357 method = cur_quick_frame_->AsMirrorPtr(); 358 } 359 } else if (cur_shadow_frame_ != NULL) { 360 do { 361 SanityCheckFrame(); 362 bool should_continue = VisitFrame(); 363 if (UNLIKELY(!should_continue)) { 364 return; 365 } 366 cur_depth_++; 367 cur_shadow_frame_ = cur_shadow_frame_->GetLink(); 368 } while (cur_shadow_frame_ != NULL); 369 } 370 if (include_transitions) { 371 bool should_continue = VisitFrame(); 372 if (!should_continue) { 373 return; 374 } 375 } 376 cur_depth_++; 377 } 378 if (num_frames_ != 0) { 379 CHECK_EQ(cur_depth_, num_frames_); 380 } 381} 382 383} // namespace art 384