stack.cc revision a836bc9760419af4a515f96c66100a39e865f3b9
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 "arch/context.h" 20#include "base/hex_dump.h" 21#include "entrypoints/runtime_asm_entrypoints.h" 22#include "mirror/art_method-inl.h" 23#include "mirror/class-inl.h" 24#include "mirror/object.h" 25#include "mirror/object-inl.h" 26#include "mirror/object_array-inl.h" 27#include "quick/quick_method_frame_info.h" 28#include "runtime.h" 29#include "thread.h" 30#include "thread_list.h" 31#include "throw_location.h" 32#include "verify_object-inl.h" 33#include "vmap_table.h" 34 35namespace art { 36 37mirror::Object* ShadowFrame::GetThisObject() const { 38 mirror::ArtMethod* m = GetMethod(); 39 if (m->IsStatic()) { 40 return NULL; 41 } else if (m->IsNative()) { 42 return GetVRegReference(0); 43 } else { 44 const DexFile::CodeItem* code_item = m->GetCodeItem(); 45 CHECK(code_item != NULL) << PrettyMethod(m); 46 uint16_t reg = code_item->registers_size_ - code_item->ins_size_; 47 return GetVRegReference(reg); 48 } 49} 50 51mirror::Object* ShadowFrame::GetThisObject(uint16_t num_ins) const { 52 mirror::ArtMethod* m = GetMethod(); 53 if (m->IsStatic()) { 54 return NULL; 55 } else { 56 return GetVRegReference(NumberOfVRegs() - num_ins); 57 } 58} 59 60ThrowLocation ShadowFrame::GetCurrentLocationForThrow() const { 61 return ThrowLocation(GetThisObject(), GetMethod(), GetDexPC()); 62} 63 64size_t ManagedStack::NumJniShadowFrameReferences() const { 65 size_t count = 0; 66 for (const ManagedStack* current_fragment = this; current_fragment != NULL; 67 current_fragment = current_fragment->GetLink()) { 68 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != NULL; 69 current_frame = current_frame->GetLink()) { 70 if (current_frame->GetMethod()->IsNative()) { 71 // The JNI ShadowFrame only contains references. (For indirect reference.) 72 count += current_frame->NumberOfVRegs(); 73 } 74 } 75 } 76 return count; 77} 78 79bool ManagedStack::ShadowFramesContain(StackReference<mirror::Object>* shadow_frame_entry) const { 80 for (const ManagedStack* current_fragment = this; current_fragment != NULL; 81 current_fragment = current_fragment->GetLink()) { 82 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != NULL; 83 current_frame = current_frame->GetLink()) { 84 if (current_frame->Contains(shadow_frame_entry)) { 85 return true; 86 } 87 } 88 } 89 return false; 90} 91 92StackVisitor::StackVisitor(Thread* thread, Context* context) 93 : thread_(thread), cur_shadow_frame_(NULL), 94 cur_quick_frame_(NULL), cur_quick_frame_pc_(0), num_frames_(0), cur_depth_(0), 95 context_(context) { 96 DCHECK(thread == Thread::Current() || thread->IsSuspended()) << *thread; 97} 98 99StackVisitor::StackVisitor(Thread* thread, Context* context, size_t num_frames) 100 : thread_(thread), cur_shadow_frame_(NULL), 101 cur_quick_frame_(NULL), cur_quick_frame_pc_(0), num_frames_(num_frames), cur_depth_(0), 102 context_(context) { 103 DCHECK(thread == Thread::Current() || thread->IsSuspended()) << *thread; 104} 105 106uint32_t StackVisitor::GetDexPc(bool abort_on_failure) const { 107 if (cur_shadow_frame_ != NULL) { 108 return cur_shadow_frame_->GetDexPC(); 109 } else if (cur_quick_frame_ != NULL) { 110 return GetMethod()->ToDexPc(cur_quick_frame_pc_, abort_on_failure); 111 } else { 112 return 0; 113 } 114} 115 116extern "C" mirror::Object* artQuickGetProxyThisObject(StackReference<mirror::ArtMethod>* sp) 117 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 118 119mirror::Object* StackVisitor::GetThisObject() const { 120 mirror::ArtMethod* m = GetMethod(); 121 if (m->IsStatic()) { 122 return nullptr; 123 } else if (m->IsNative()) { 124 if (cur_quick_frame_ != nullptr) { 125 HandleScope* hs = reinterpret_cast<HandleScope*>( 126 reinterpret_cast<char*>(cur_quick_frame_) + m->GetHandleScopeOffset().SizeValue()); 127 return hs->GetReference(0); 128 } else { 129 return cur_shadow_frame_->GetVRegReference(0); 130 } 131 } else if (m->IsProxyMethod()) { 132 if (cur_quick_frame_ != nullptr) { 133 return artQuickGetProxyThisObject(cur_quick_frame_); 134 } else { 135 return cur_shadow_frame_->GetVRegReference(0); 136 } 137 } else if (m->IsOptimized(sizeof(void*))) { 138 // TODO: Implement, currently only used for exceptions when jdwp is enabled. 139 UNIMPLEMENTED(WARNING) 140 << "StackVisitor::GetThisObject is unimplemented with the optimizing compiler"; 141 return nullptr; 142 } else { 143 const DexFile::CodeItem* code_item = m->GetCodeItem(); 144 if (code_item == nullptr) { 145 UNIMPLEMENTED(ERROR) << "Failed to determine this object of abstract or proxy method: " 146 << PrettyMethod(m); 147 return nullptr; 148 } else { 149 uint16_t reg = code_item->registers_size_ - code_item->ins_size_; 150 return reinterpret_cast<mirror::Object*>(GetVReg(m, reg, kReferenceVReg)); 151 } 152 } 153} 154 155size_t StackVisitor::GetNativePcOffset() const { 156 DCHECK(!IsShadowFrame()); 157 return GetMethod()->NativeQuickPcOffset(cur_quick_frame_pc_); 158} 159 160bool StackVisitor::GetVReg(mirror::ArtMethod* m, uint16_t vreg, VRegKind kind, 161 uint32_t* val) const { 162 if (cur_quick_frame_ != nullptr) { 163 DCHECK(context_ != nullptr); // You can't reliably read registers without a context. 164 DCHECK(m == GetMethod()); 165 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*)); 166 DCHECK(code_pointer != nullptr); 167 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*))); 168 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer); 169 uint32_t vmap_offset; 170 // TODO: IsInContext stops before spotting floating point registers. 171 if (vmap_table.IsInContext(vreg, kind, &vmap_offset)) { 172 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg); 173 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask(); 174 uint32_t reg = vmap_table.ComputeRegister(spill_mask, vmap_offset, kind); 175 uintptr_t ptr_val; 176 bool success = is_float ? GetFPR(reg, &ptr_val) : GetGPR(reg, &ptr_val); 177 if (!success) { 178 return false; 179 } 180 bool target64 = Is64BitInstructionSet(kRuntimeISA); 181 if (target64) { 182 bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg); 183 bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg); 184 int64_t value_long = static_cast<int64_t>(ptr_val); 185 if (wide_lo) { 186 ptr_val = static_cast<uintptr_t>(value_long & 0xFFFFFFFF); 187 } else if (wide_hi) { 188 ptr_val = static_cast<uintptr_t>(value_long >> 32); 189 } 190 } 191 *val = ptr_val; 192 return true; 193 } else { 194 const DexFile::CodeItem* code_item = m->GetCodeItem(); 195 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be NULL or how would we compile 196 // its instructions? 197 *val = *GetVRegAddr(cur_quick_frame_, code_item, frame_info.CoreSpillMask(), 198 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg); 199 return true; 200 } 201 } else { 202 *val = cur_shadow_frame_->GetVReg(vreg); 203 return true; 204 } 205} 206 207bool StackVisitor::GetVRegPair(mirror::ArtMethod* m, uint16_t vreg, VRegKind kind_lo, 208 VRegKind kind_hi, uint64_t* val) const { 209 if (kind_lo == kLongLoVReg) { 210 DCHECK_EQ(kind_hi, kLongHiVReg); 211 } else if (kind_lo == kDoubleLoVReg) { 212 DCHECK_EQ(kind_hi, kDoubleHiVReg); 213 } else { 214 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi; 215 } 216 if (cur_quick_frame_ != nullptr) { 217 DCHECK(context_ != nullptr); // You can't reliably read registers without a context. 218 DCHECK(m == GetMethod()); 219 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*)); 220 DCHECK(code_pointer != nullptr); 221 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*))); 222 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer); 223 uint32_t vmap_offset_lo, vmap_offset_hi; 224 // TODO: IsInContext stops before spotting floating point registers. 225 if (vmap_table.IsInContext(vreg, kind_lo, &vmap_offset_lo) && 226 vmap_table.IsInContext(vreg + 1, kind_hi, &vmap_offset_hi)) { 227 bool is_float = (kind_lo == kDoubleLoVReg); 228 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask(); 229 uint32_t reg_lo = vmap_table.ComputeRegister(spill_mask, vmap_offset_lo, kind_lo); 230 uint32_t reg_hi = vmap_table.ComputeRegister(spill_mask, vmap_offset_hi, kind_hi); 231 uintptr_t ptr_val_lo, ptr_val_hi; 232 bool success = is_float ? GetFPR(reg_lo, &ptr_val_lo) : GetGPR(reg_lo, &ptr_val_lo); 233 success &= is_float ? GetFPR(reg_hi, &ptr_val_hi) : GetGPR(reg_hi, &ptr_val_hi); 234 if (!success) { 235 return false; 236 } 237 bool target64 = Is64BitInstructionSet(kRuntimeISA); 238 if (target64) { 239 int64_t value_long_lo = static_cast<int64_t>(ptr_val_lo); 240 int64_t value_long_hi = static_cast<int64_t>(ptr_val_hi); 241 ptr_val_lo = static_cast<uintptr_t>(value_long_lo & 0xFFFFFFFF); 242 ptr_val_hi = static_cast<uintptr_t>(value_long_hi >> 32); 243 } 244 *val = (static_cast<uint64_t>(ptr_val_hi) << 32) | static_cast<uint32_t>(ptr_val_lo); 245 return true; 246 } else { 247 const DexFile::CodeItem* code_item = m->GetCodeItem(); 248 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be NULL or how would we compile 249 // its instructions? 250 uint32_t* addr = GetVRegAddr(cur_quick_frame_, code_item, frame_info.CoreSpillMask(), 251 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg); 252 *val = *reinterpret_cast<uint64_t*>(addr); 253 return true; 254 } 255 } else { 256 *val = cur_shadow_frame_->GetVRegLong(vreg); 257 return true; 258 } 259} 260 261bool StackVisitor::SetVReg(mirror::ArtMethod* m, uint16_t vreg, uint32_t new_value, 262 VRegKind kind) { 263 if (cur_quick_frame_ != nullptr) { 264 DCHECK(context_ != nullptr); // You can't reliably write registers without a context. 265 DCHECK(m == GetMethod()); 266 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*)); 267 DCHECK(code_pointer != nullptr); 268 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*))); 269 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer); 270 uint32_t vmap_offset; 271 // TODO: IsInContext stops before spotting floating point registers. 272 if (vmap_table.IsInContext(vreg, kind, &vmap_offset)) { 273 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg); 274 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask(); 275 const uint32_t reg = vmap_table.ComputeRegister(spill_mask, vmap_offset, kind); 276 bool target64 = Is64BitInstructionSet(kRuntimeISA); 277 // Deal with 32 or 64-bit wide registers in a way that builds on all targets. 278 if (target64) { 279 bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg); 280 bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg); 281 if (wide_lo || wide_hi) { 282 uintptr_t old_reg_val; 283 bool success = is_float ? GetFPR(reg, &old_reg_val) : GetGPR(reg, &old_reg_val); 284 if (!success) { 285 return false; 286 } 287 uint64_t new_vreg_portion = static_cast<uint64_t>(new_value); 288 uint64_t old_reg_val_as_wide = static_cast<uint64_t>(old_reg_val); 289 uint64_t mask = 0xffffffff; 290 if (wide_lo) { 291 mask = mask << 32; 292 } else { 293 new_vreg_portion = new_vreg_portion << 32; 294 } 295 new_value = static_cast<uintptr_t>((old_reg_val_as_wide & mask) | new_vreg_portion); 296 } 297 } 298 if (is_float) { 299 return SetFPR(reg, new_value); 300 } else { 301 return SetGPR(reg, new_value); 302 } 303 } else { 304 const DexFile::CodeItem* code_item = m->GetCodeItem(); 305 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be NULL or how would we compile 306 // its instructions? 307 uint32_t* addr = GetVRegAddr(cur_quick_frame_, code_item, frame_info.CoreSpillMask(), 308 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg); 309 *addr = new_value; 310 return true; 311 } 312 } else { 313 cur_shadow_frame_->SetVReg(vreg, new_value); 314 return true; 315 } 316} 317 318bool StackVisitor::SetVRegPair(mirror::ArtMethod* m, uint16_t vreg, uint64_t new_value, 319 VRegKind kind_lo, VRegKind kind_hi) { 320 if (kind_lo == kLongLoVReg) { 321 DCHECK_EQ(kind_hi, kLongHiVReg); 322 } else if (kind_lo == kDoubleLoVReg) { 323 DCHECK_EQ(kind_hi, kDoubleHiVReg); 324 } else { 325 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi; 326 } 327 if (cur_quick_frame_ != nullptr) { 328 DCHECK(context_ != nullptr); // You can't reliably write registers without a context. 329 DCHECK(m == GetMethod()); 330 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*)); 331 DCHECK(code_pointer != nullptr); 332 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*))); 333 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer); 334 uint32_t vmap_offset_lo, vmap_offset_hi; 335 // TODO: IsInContext stops before spotting floating point registers. 336 if (vmap_table.IsInContext(vreg, kind_lo, &vmap_offset_lo) && 337 vmap_table.IsInContext(vreg + 1, kind_hi, &vmap_offset_hi)) { 338 bool is_float = (kind_lo == kDoubleLoVReg); 339 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask(); 340 uint32_t reg_lo = vmap_table.ComputeRegister(spill_mask, vmap_offset_lo, kind_lo); 341 uint32_t reg_hi = vmap_table.ComputeRegister(spill_mask, vmap_offset_hi, kind_hi); 342 uintptr_t new_value_lo = static_cast<uintptr_t>(new_value & 0xFFFFFFFF); 343 uintptr_t new_value_hi = static_cast<uintptr_t>(new_value >> 32); 344 bool target64 = Is64BitInstructionSet(kRuntimeISA); 345 // Deal with 32 or 64-bit wide registers in a way that builds on all targets. 346 if (target64) { 347 uintptr_t old_reg_val_lo, old_reg_val_hi; 348 bool success = is_float ? GetFPR(reg_lo, &old_reg_val_lo) : GetGPR(reg_lo, &old_reg_val_lo); 349 success &= is_float ? GetFPR(reg_hi, &old_reg_val_hi) : GetGPR(reg_hi, &old_reg_val_hi); 350 if (!success) { 351 return false; 352 } 353 uint64_t new_vreg_portion_lo = static_cast<uint64_t>(new_value_lo); 354 uint64_t new_vreg_portion_hi = static_cast<uint64_t>(new_value_hi) << 32; 355 uint64_t old_reg_val_lo_as_wide = static_cast<uint64_t>(old_reg_val_lo); 356 uint64_t old_reg_val_hi_as_wide = static_cast<uint64_t>(old_reg_val_hi); 357 uint64_t mask_lo = static_cast<uint64_t>(0xffffffff) << 32; 358 uint64_t mask_hi = 0xffffffff; 359 new_value_lo = static_cast<uintptr_t>((old_reg_val_lo_as_wide & mask_lo) | new_vreg_portion_lo); 360 new_value_hi = static_cast<uintptr_t>((old_reg_val_hi_as_wide & mask_hi) | new_vreg_portion_hi); 361 } 362 bool success = is_float ? SetFPR(reg_lo, new_value_lo) : SetGPR(reg_lo, new_value_lo); 363 success &= is_float ? SetFPR(reg_hi, new_value_hi) : SetGPR(reg_hi, new_value_hi); 364 return success; 365 } else { 366 const DexFile::CodeItem* code_item = m->GetCodeItem(); 367 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be NULL or how would we compile 368 // its instructions? 369 uint32_t* addr = GetVRegAddr(cur_quick_frame_, code_item, frame_info.CoreSpillMask(), 370 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg); 371 *reinterpret_cast<uint64_t*>(addr) = new_value; 372 return true; 373 } 374 } else { 375 cur_shadow_frame_->SetVRegLong(vreg, new_value); 376 return true; 377 } 378} 379 380uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const { 381 DCHECK(cur_quick_frame_ != NULL) << "This is a quick frame routine"; 382 return context_->GetGPRAddress(reg); 383} 384 385bool StackVisitor::GetGPR(uint32_t reg, uintptr_t* val) const { 386 DCHECK(cur_quick_frame_ != NULL) << "This is a quick frame routine"; 387 return context_->GetGPR(reg, val); 388} 389 390bool StackVisitor::SetGPR(uint32_t reg, uintptr_t value) { 391 DCHECK(cur_quick_frame_ != NULL) << "This is a quick frame routine"; 392 return context_->SetGPR(reg, value); 393} 394 395bool StackVisitor::GetFPR(uint32_t reg, uintptr_t* val) const { 396 DCHECK(cur_quick_frame_ != NULL) << "This is a quick frame routine"; 397 return context_->GetFPR(reg, val); 398} 399 400bool StackVisitor::SetFPR(uint32_t reg, uintptr_t value) { 401 DCHECK(cur_quick_frame_ != NULL) << "This is a quick frame routine"; 402 return context_->SetFPR(reg, value); 403} 404 405uintptr_t StackVisitor::GetReturnPc() const { 406 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame()); 407 DCHECK(sp != NULL); 408 uint8_t* pc_addr = sp + GetMethod()->GetReturnPcOffset().SizeValue(); 409 return *reinterpret_cast<uintptr_t*>(pc_addr); 410} 411 412void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) { 413 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame()); 414 CHECK(sp != NULL); 415 uint8_t* pc_addr = sp + GetMethod()->GetReturnPcOffset().SizeValue(); 416 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc; 417} 418 419size_t StackVisitor::ComputeNumFrames(Thread* thread) { 420 struct NumFramesVisitor : public StackVisitor { 421 explicit NumFramesVisitor(Thread* thread_in) 422 : StackVisitor(thread_in, NULL), frames(0) {} 423 424 bool VisitFrame() OVERRIDE { 425 frames++; 426 return true; 427 } 428 429 size_t frames; 430 }; 431 NumFramesVisitor visitor(thread); 432 visitor.WalkStack(true); 433 return visitor.frames; 434} 435 436bool StackVisitor::GetNextMethodAndDexPc(mirror::ArtMethod** next_method, uint32_t* next_dex_pc) { 437 struct HasMoreFramesVisitor : public StackVisitor { 438 explicit HasMoreFramesVisitor(Thread* thread, size_t num_frames, size_t frame_height) 439 : StackVisitor(thread, nullptr, num_frames), frame_height_(frame_height), 440 found_frame_(false), has_more_frames_(false), next_method_(nullptr), next_dex_pc_(0) { 441 } 442 443 bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 444 if (found_frame_) { 445 mirror::ArtMethod* method = GetMethod(); 446 if (method != nullptr && !method->IsRuntimeMethod()) { 447 has_more_frames_ = true; 448 next_method_ = method; 449 next_dex_pc_ = GetDexPc(); 450 return false; // End stack walk once next method is found. 451 } 452 } else if (GetFrameHeight() == frame_height_) { 453 found_frame_ = true; 454 } 455 return true; 456 } 457 458 size_t frame_height_; 459 bool found_frame_; 460 bool has_more_frames_; 461 mirror::ArtMethod* next_method_; 462 uint32_t next_dex_pc_; 463 }; 464 HasMoreFramesVisitor visitor(thread_, GetNumFrames(), GetFrameHeight()); 465 visitor.WalkStack(true); 466 *next_method = visitor.next_method_; 467 *next_dex_pc = visitor.next_dex_pc_; 468 return visitor.has_more_frames_; 469} 470 471void StackVisitor::DescribeStack(Thread* thread) { 472 struct DescribeStackVisitor : public StackVisitor { 473 explicit DescribeStackVisitor(Thread* thread_in) 474 : StackVisitor(thread_in, NULL) {} 475 476 bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 477 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation(); 478 return true; 479 } 480 }; 481 DescribeStackVisitor visitor(thread); 482 visitor.WalkStack(true); 483} 484 485std::string StackVisitor::DescribeLocation() const { 486 std::string result("Visiting method '"); 487 mirror::ArtMethod* m = GetMethod(); 488 if (m == NULL) { 489 return "upcall"; 490 } 491 result += PrettyMethod(m); 492 result += StringPrintf("' at dex PC 0x%04x", GetDexPc()); 493 if (!IsShadowFrame()) { 494 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc())); 495 } 496 return result; 497} 498 499static instrumentation::InstrumentationStackFrame& GetInstrumentationStackFrame(Thread* thread, 500 uint32_t depth) { 501 CHECK_LT(depth, thread->GetInstrumentationStack()->size()); 502 return thread->GetInstrumentationStack()->at(depth); 503} 504 505void StackVisitor::SanityCheckFrame() const { 506 if (kIsDebugBuild) { 507 mirror::ArtMethod* method = GetMethod(); 508 CHECK_EQ(method->GetClass(), mirror::ArtMethod::GetJavaLangReflectArtMethod()); 509 if (cur_quick_frame_ != nullptr) { 510 method->AssertPcIsWithinQuickCode(cur_quick_frame_pc_); 511 // Frame sanity. 512 size_t frame_size = method->GetFrameSizeInBytes(); 513 CHECK_NE(frame_size, 0u); 514 // A rough guess at an upper size we expect to see for a frame. 515 // 256 registers 516 // 2 words HandleScope overhead 517 // 3+3 register spills 518 // TODO: this seems architecture specific for the case of JNI frames. 519 // TODO: 083-compiler-regressions ManyFloatArgs shows this estimate is wrong. 520 // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word); 521 const size_t kMaxExpectedFrameSize = 2 * KB; 522 CHECK_LE(frame_size, kMaxExpectedFrameSize); 523 size_t return_pc_offset = method->GetReturnPcOffset().SizeValue(); 524 CHECK_LT(return_pc_offset, frame_size); 525 } 526 } 527} 528 529void StackVisitor::WalkStack(bool include_transitions) { 530 DCHECK(thread_ == Thread::Current() || thread_->IsSuspended()); 531 CHECK_EQ(cur_depth_, 0U); 532 bool exit_stubs_installed = Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled(); 533 uint32_t instrumentation_stack_depth = 0; 534 535 for (const ManagedStack* current_fragment = thread_->GetManagedStack(); current_fragment != NULL; 536 current_fragment = current_fragment->GetLink()) { 537 cur_shadow_frame_ = current_fragment->GetTopShadowFrame(); 538 cur_quick_frame_ = current_fragment->GetTopQuickFrame(); 539 cur_quick_frame_pc_ = 0; 540 541 if (cur_quick_frame_ != NULL) { // Handle quick stack frames. 542 // Can't be both a shadow and a quick fragment. 543 DCHECK(current_fragment->GetTopShadowFrame() == NULL); 544 mirror::ArtMethod* method = cur_quick_frame_->AsMirrorPtr(); 545 while (method != NULL) { 546 SanityCheckFrame(); 547 bool should_continue = VisitFrame(); 548 if (UNLIKELY(!should_continue)) { 549 return; 550 } 551 552 if (context_ != NULL) { 553 context_->FillCalleeSaves(*this); 554 } 555 size_t frame_size = method->GetFrameSizeInBytes(); 556 // Compute PC for next stack frame from return PC. 557 size_t return_pc_offset = method->GetReturnPcOffset(frame_size).SizeValue(); 558 uint8_t* return_pc_addr = reinterpret_cast<uint8_t*>(cur_quick_frame_) + return_pc_offset; 559 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr); 560 if (UNLIKELY(exit_stubs_installed)) { 561 // While profiling, the return pc is restored from the side stack, except when walking 562 // the stack for an exception where the side stack will be unwound in VisitFrame. 563 if (reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc) { 564 const instrumentation::InstrumentationStackFrame& instrumentation_frame = 565 GetInstrumentationStackFrame(thread_, instrumentation_stack_depth); 566 instrumentation_stack_depth++; 567 if (GetMethod() == Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveAll)) { 568 // Skip runtime save all callee frames which are used to deliver exceptions. 569 } else if (instrumentation_frame.interpreter_entry_) { 570 mirror::ArtMethod* callee = Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs); 571 CHECK_EQ(GetMethod(), callee) << "Expected: " << PrettyMethod(callee) << " Found: " 572 << PrettyMethod(GetMethod()); 573 } else if (instrumentation_frame.method_ != GetMethod()) { 574 LOG(FATAL) << "Expected: " << PrettyMethod(instrumentation_frame.method_) 575 << " Found: " << PrettyMethod(GetMethod()); 576 } 577 if (num_frames_ != 0) { 578 // Check agreement of frame Ids only if num_frames_ is computed to avoid infinite 579 // recursion. 580 CHECK(instrumentation_frame.frame_id_ == GetFrameId()) 581 << "Expected: " << instrumentation_frame.frame_id_ 582 << " Found: " << GetFrameId(); 583 } 584 return_pc = instrumentation_frame.return_pc_; 585 } 586 } 587 cur_quick_frame_pc_ = return_pc; 588 uint8_t* next_frame = reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size; 589 cur_quick_frame_ = reinterpret_cast<StackReference<mirror::ArtMethod>*>(next_frame); 590 cur_depth_++; 591 method = cur_quick_frame_->AsMirrorPtr(); 592 } 593 } else if (cur_shadow_frame_ != NULL) { 594 do { 595 SanityCheckFrame(); 596 bool should_continue = VisitFrame(); 597 if (UNLIKELY(!should_continue)) { 598 return; 599 } 600 cur_depth_++; 601 cur_shadow_frame_ = cur_shadow_frame_->GetLink(); 602 } while (cur_shadow_frame_ != NULL); 603 } 604 if (include_transitions) { 605 bool should_continue = VisitFrame(); 606 if (!should_continue) { 607 return; 608 } 609 } 610 cur_depth_++; 611 } 612 if (num_frames_ != 0) { 613 CHECK_EQ(cur_depth_, num_frames_); 614 } 615} 616 617} // namespace art 618