2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "android-base/stringprintf.h"
21 #include "arch/context.h"
22 #include "art_method-inl.h"
23 #include "base/enums.h"
24 #include "base/hex_dump.h"
25 #include "entrypoints/entrypoint_utils-inl.h"
26 #include "entrypoints/runtime_asm_entrypoints.h"
27 #include "gc/space/image_space.h"
28 #include "gc/space/space-inl.h"
30 #include "jit/jit_code_cache.h"
31 #include "linear_alloc.h"
32 #include "mirror/class-inl.h"
33 #include "mirror/object-inl.h"
34 #include "mirror/object_array-inl.h"
35 #include "oat_quick_method_header.h"
36 #include "quick/quick_method_frame_info.h"
39 #include "thread_list.h"
40 #include "verify_object.h"
44 using android::base::StringPrintf;
46 static constexpr bool kDebugStackWalk = false;
48 mirror::Object* ShadowFrame::GetThisObject() const {
49 ArtMethod* m = GetMethod();
52 } else if (m->IsNative()) {
53 return GetVRegReference(0);
55 const DexFile::CodeItem* code_item = m->GetCodeItem();
56 CHECK(code_item != nullptr) << ArtMethod::PrettyMethod(m);
57 uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
58 return GetVRegReference(reg);
62 mirror::Object* ShadowFrame::GetThisObject(uint16_t num_ins) const {
63 ArtMethod* m = GetMethod();
67 return GetVRegReference(NumberOfVRegs() - num_ins);
71 size_t ManagedStack::NumJniShadowFrameReferences() const {
73 for (const ManagedStack* current_fragment = this; current_fragment != nullptr;
74 current_fragment = current_fragment->GetLink()) {
75 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr;
76 current_frame = current_frame->GetLink()) {
77 if (current_frame->GetMethod()->IsNative()) {
78 // The JNI ShadowFrame only contains references. (For indirect reference.)
79 count += current_frame->NumberOfVRegs();
86 bool ManagedStack::ShadowFramesContain(StackReference<mirror::Object>* shadow_frame_entry) const {
87 for (const ManagedStack* current_fragment = this; current_fragment != nullptr;
88 current_fragment = current_fragment->GetLink()) {
89 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr;
90 current_frame = current_frame->GetLink()) {
91 if (current_frame->Contains(shadow_frame_entry)) {
99 StackVisitor::StackVisitor(Thread* thread,
101 StackWalkKind walk_kind,
102 bool check_suspended)
103 : StackVisitor(thread, context, walk_kind, 0, check_suspended) {}
105 StackVisitor::StackVisitor(Thread* thread,
107 StackWalkKind walk_kind,
109 bool check_suspended)
111 walk_kind_(walk_kind),
112 cur_shadow_frame_(nullptr),
113 cur_quick_frame_(nullptr),
114 cur_quick_frame_pc_(0),
115 cur_oat_quick_method_header_(nullptr),
116 num_frames_(num_frames),
118 current_inlining_depth_(0),
120 check_suspended_(check_suspended) {
121 if (check_suspended_) {
122 DCHECK(thread == Thread::Current() || thread->IsSuspended()) << *thread;
126 InlineInfo StackVisitor::GetCurrentInlineInfo() const {
127 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
128 uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_);
129 CodeInfo code_info = method_header->GetOptimizedCodeInfo();
130 CodeInfoEncoding encoding = code_info.ExtractEncoding();
131 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
132 DCHECK(stack_map.IsValid());
133 return code_info.GetInlineInfoOf(stack_map, encoding);
136 ArtMethod* StackVisitor::GetMethod() const {
137 if (cur_shadow_frame_ != nullptr) {
138 return cur_shadow_frame_->GetMethod();
139 } else if (cur_quick_frame_ != nullptr) {
140 if (IsInInlinedFrame()) {
141 size_t depth_in_stack_map = current_inlining_depth_ - 1;
142 InlineInfo inline_info = GetCurrentInlineInfo();
143 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
144 CodeInfoEncoding encoding = method_header->GetOptimizedCodeInfo().ExtractEncoding();
145 MethodInfo method_info = method_header->GetOptimizedMethodInfo();
146 DCHECK(walk_kind_ != StackWalkKind::kSkipInlinedFrames);
147 return GetResolvedMethod(*GetCurrentQuickFrame(),
150 encoding.inline_info.encoding,
153 return *cur_quick_frame_;
159 uint32_t StackVisitor::GetDexPc(bool abort_on_failure) const {
160 if (cur_shadow_frame_ != nullptr) {
161 return cur_shadow_frame_->GetDexPC();
162 } else if (cur_quick_frame_ != nullptr) {
163 if (IsInInlinedFrame()) {
164 size_t depth_in_stack_map = current_inlining_depth_ - 1;
165 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
166 CodeInfoEncoding encoding = method_header->GetOptimizedCodeInfo().ExtractEncoding();
167 return GetCurrentInlineInfo().GetDexPcAtDepth(encoding.inline_info.encoding,
169 } else if (cur_oat_quick_method_header_ == nullptr) {
170 return DexFile::kDexNoIndex;
172 return cur_oat_quick_method_header_->ToDexPc(
173 GetMethod(), cur_quick_frame_pc_, abort_on_failure);
180 extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
181 REQUIRES_SHARED(Locks::mutator_lock_);
183 mirror::Object* StackVisitor::GetThisObject() const {
184 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
185 ArtMethod* m = GetMethod();
188 } else if (m->IsNative()) {
189 if (cur_quick_frame_ != nullptr) {
190 HandleScope* hs = reinterpret_cast<HandleScope*>(
191 reinterpret_cast<char*>(cur_quick_frame_) + sizeof(ArtMethod*));
192 return hs->GetReference(0);
194 return cur_shadow_frame_->GetVRegReference(0);
196 } else if (m->IsProxyMethod()) {
197 if (cur_quick_frame_ != nullptr) {
198 return artQuickGetProxyThisObject(cur_quick_frame_);
200 return cur_shadow_frame_->GetVRegReference(0);
203 const DexFile::CodeItem* code_item = m->GetCodeItem();
204 if (code_item == nullptr) {
205 UNIMPLEMENTED(ERROR) << "Failed to determine this object of abstract or proxy method: "
206 << ArtMethod::PrettyMethod(m);
209 uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
211 bool success = GetVReg(m, reg, kReferenceVReg, &value);
212 // We currently always guarantee the `this` object is live throughout the method.
213 CHECK(success) << "Failed to read the this object in " << ArtMethod::PrettyMethod(m);
214 return reinterpret_cast<mirror::Object*>(value);
219 size_t StackVisitor::GetNativePcOffset() const {
220 DCHECK(!IsShadowFrame());
221 return GetCurrentOatQuickMethodHeader()->NativeQuickPcOffset(cur_quick_frame_pc_);
224 bool StackVisitor::GetVRegFromDebuggerShadowFrame(uint16_t vreg,
226 uint32_t* val) const {
227 size_t frame_id = const_cast<StackVisitor*>(this)->GetFrameId();
228 ShadowFrame* shadow_frame = thread_->FindDebuggerShadowFrame(frame_id);
229 if (shadow_frame != nullptr) {
230 bool* updated_vreg_flags = thread_->GetUpdatedVRegFlags(frame_id);
231 DCHECK(updated_vreg_flags != nullptr);
232 if (updated_vreg_flags[vreg]) {
233 // Value is set by the debugger.
234 if (kind == kReferenceVReg) {
235 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
236 shadow_frame->GetVRegReference(vreg)));
238 *val = shadow_frame->GetVReg(vreg);
243 // No value is set by the debugger.
247 bool StackVisitor::GetVReg(ArtMethod* m, uint16_t vreg, VRegKind kind, uint32_t* val) const {
248 if (cur_quick_frame_ != nullptr) {
249 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
250 DCHECK(m == GetMethod());
251 // Check if there is value set by the debugger.
252 if (GetVRegFromDebuggerShadowFrame(vreg, kind, val)) {
255 DCHECK(cur_oat_quick_method_header_->IsOptimized());
256 return GetVRegFromOptimizedCode(m, vreg, kind, val);
258 DCHECK(cur_shadow_frame_ != nullptr);
259 if (kind == kReferenceVReg) {
260 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
261 cur_shadow_frame_->GetVRegReference(vreg)));
263 *val = cur_shadow_frame_->GetVReg(vreg);
269 bool StackVisitor::GetVRegFromOptimizedCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
270 uint32_t* val) const {
271 DCHECK_EQ(m, GetMethod());
272 const DexFile::CodeItem* code_item = m->GetCodeItem();
273 DCHECK(code_item != nullptr) << m->PrettyMethod(); // Can't be null or how would we compile
275 uint16_t number_of_dex_registers = code_item->registers_size_;
276 DCHECK_LT(vreg, code_item->registers_size_);
277 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
278 CodeInfo code_info = method_header->GetOptimizedCodeInfo();
279 CodeInfoEncoding encoding = code_info.ExtractEncoding();
281 uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_);
282 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
283 DCHECK(stack_map.IsValid());
284 size_t depth_in_stack_map = current_inlining_depth_ - 1;
286 DexRegisterMap dex_register_map = IsInInlinedFrame()
287 ? code_info.GetDexRegisterMapAtDepth(depth_in_stack_map,
288 code_info.GetInlineInfoOf(stack_map, encoding),
290 number_of_dex_registers)
291 : code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers);
293 if (!dex_register_map.IsValid()) {
296 DexRegisterLocation::Kind location_kind =
297 dex_register_map.GetLocationKind(vreg, number_of_dex_registers, code_info, encoding);
298 switch (location_kind) {
299 case DexRegisterLocation::Kind::kInStack: {
300 const int32_t offset = dex_register_map.GetStackOffsetInBytes(vreg,
301 number_of_dex_registers,
304 const uint8_t* addr = reinterpret_cast<const uint8_t*>(cur_quick_frame_) + offset;
305 *val = *reinterpret_cast<const uint32_t*>(addr);
308 case DexRegisterLocation::Kind::kInRegister:
309 case DexRegisterLocation::Kind::kInRegisterHigh:
310 case DexRegisterLocation::Kind::kInFpuRegister:
311 case DexRegisterLocation::Kind::kInFpuRegisterHigh: {
313 dex_register_map.GetMachineRegister(vreg, number_of_dex_registers, code_info, encoding);
314 return GetRegisterIfAccessible(reg, kind, val);
316 case DexRegisterLocation::Kind::kConstant:
317 *val = dex_register_map.GetConstant(vreg, number_of_dex_registers, code_info, encoding);
319 case DexRegisterLocation::Kind::kNone:
323 << "Unexpected location kind "
324 << dex_register_map.GetLocationInternalKind(vreg,
325 number_of_dex_registers,
332 bool StackVisitor::GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const {
333 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
335 if (kRuntimeISA == InstructionSet::kX86 && is_float) {
336 // X86 float registers are 64-bit and each XMM register is provided as two separate
337 // 32-bit registers by the context.
338 reg = (kind == kDoubleHiVReg) ? (2 * reg + 1) : (2 * reg);
341 // MIPS32 float registers are used as 64-bit (for MIPS32r2 it is pair
342 // F(2n)-F(2n+1), and for MIPS32r6 it is 64-bit register F(2n)). When
343 // accessing upper 32-bits from double, reg + 1 should be used.
344 if ((kRuntimeISA == InstructionSet::kMips) && (kind == kDoubleHiVReg)) {
345 DCHECK_ALIGNED(reg, 2);
349 if (!IsAccessibleRegister(reg, is_float)) {
352 uintptr_t ptr_val = GetRegister(reg, is_float);
353 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
355 const bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
356 const bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
357 int64_t value_long = static_cast<int64_t>(ptr_val);
359 ptr_val = static_cast<uintptr_t>(Low32Bits(value_long));
360 } else if (wide_hi) {
361 ptr_val = static_cast<uintptr_t>(High32Bits(value_long));
368 bool StackVisitor::GetVRegPairFromDebuggerShadowFrame(uint16_t vreg,
371 uint64_t* val) const {
373 uint32_t high_32bits;
374 bool success = GetVRegFromDebuggerShadowFrame(vreg, kind_lo, &low_32bits);
375 success &= GetVRegFromDebuggerShadowFrame(vreg + 1, kind_hi, &high_32bits);
377 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
382 bool StackVisitor::GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
383 VRegKind kind_hi, uint64_t* val) const {
384 if (kind_lo == kLongLoVReg) {
385 DCHECK_EQ(kind_hi, kLongHiVReg);
386 } else if (kind_lo == kDoubleLoVReg) {
387 DCHECK_EQ(kind_hi, kDoubleHiVReg);
389 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
392 // Check if there is value set by the debugger.
393 if (GetVRegPairFromDebuggerShadowFrame(vreg, kind_lo, kind_hi, val)) {
396 if (cur_quick_frame_ != nullptr) {
397 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
398 DCHECK(m == GetMethod());
399 DCHECK(cur_oat_quick_method_header_->IsOptimized());
400 return GetVRegPairFromOptimizedCode(m, vreg, kind_lo, kind_hi, val);
402 DCHECK(cur_shadow_frame_ != nullptr);
403 *val = cur_shadow_frame_->GetVRegLong(vreg);
408 bool StackVisitor::GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg,
409 VRegKind kind_lo, VRegKind kind_hi,
410 uint64_t* val) const {
412 uint32_t high_32bits;
413 bool success = GetVRegFromOptimizedCode(m, vreg, kind_lo, &low_32bits);
414 success &= GetVRegFromOptimizedCode(m, vreg + 1, kind_hi, &high_32bits);
416 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
421 bool StackVisitor::GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
422 VRegKind kind_lo, uint64_t* val) const {
423 const bool is_float = (kind_lo == kDoubleLoVReg);
424 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
427 uintptr_t ptr_val_lo = GetRegister(reg_lo, is_float);
428 uintptr_t ptr_val_hi = GetRegister(reg_hi, is_float);
429 bool target64 = Is64BitInstructionSet(kRuntimeISA);
431 int64_t value_long_lo = static_cast<int64_t>(ptr_val_lo);
432 int64_t value_long_hi = static_cast<int64_t>(ptr_val_hi);
433 ptr_val_lo = static_cast<uintptr_t>(Low32Bits(value_long_lo));
434 ptr_val_hi = static_cast<uintptr_t>(High32Bits(value_long_hi));
436 *val = (static_cast<uint64_t>(ptr_val_hi) << 32) | static_cast<uint32_t>(ptr_val_lo);
440 bool StackVisitor::SetVReg(ArtMethod* m,
444 const DexFile::CodeItem* code_item = m->GetCodeItem();
445 if (code_item == nullptr) {
448 ShadowFrame* shadow_frame = GetCurrentShadowFrame();
449 if (shadow_frame == nullptr) {
450 // This is a compiled frame: we must prepare and update a shadow frame that will
451 // be executed by the interpreter after deoptimization of the stack.
452 const size_t frame_id = GetFrameId();
453 const uint16_t num_regs = code_item->registers_size_;
454 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
455 CHECK(shadow_frame != nullptr);
456 // Remember the vreg has been set for debugging and must not be overwritten by the
457 // original value during deoptimization of the stack.
458 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
460 if (kind == kReferenceVReg) {
461 shadow_frame->SetVRegReference(vreg, reinterpret_cast<mirror::Object*>(new_value));
463 shadow_frame->SetVReg(vreg, new_value);
468 bool StackVisitor::SetVRegPair(ArtMethod* m,
473 if (kind_lo == kLongLoVReg) {
474 DCHECK_EQ(kind_hi, kLongHiVReg);
475 } else if (kind_lo == kDoubleLoVReg) {
476 DCHECK_EQ(kind_hi, kDoubleHiVReg);
478 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
481 const DexFile::CodeItem* code_item = m->GetCodeItem();
482 if (code_item == nullptr) {
485 ShadowFrame* shadow_frame = GetCurrentShadowFrame();
486 if (shadow_frame == nullptr) {
487 // This is a compiled frame: we must prepare for deoptimization (see SetVRegFromDebugger).
488 const size_t frame_id = GetFrameId();
489 const uint16_t num_regs = code_item->registers_size_;
490 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
491 CHECK(shadow_frame != nullptr);
492 // Remember the vreg pair has been set for debugging and must not be overwritten by the
493 // original value during deoptimization of the stack.
494 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
495 thread_->GetUpdatedVRegFlags(frame_id)[vreg + 1] = true;
497 shadow_frame->SetVRegLong(vreg, new_value);
501 bool StackVisitor::IsAccessibleGPR(uint32_t reg) const {
502 DCHECK(context_ != nullptr);
503 return context_->IsAccessibleGPR(reg);
506 uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const {
507 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
508 DCHECK(context_ != nullptr);
509 return context_->GetGPRAddress(reg);
512 uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
513 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
514 DCHECK(context_ != nullptr);
515 return context_->GetGPR(reg);
518 bool StackVisitor::IsAccessibleFPR(uint32_t reg) const {
519 DCHECK(context_ != nullptr);
520 return context_->IsAccessibleFPR(reg);
523 uintptr_t StackVisitor::GetFPR(uint32_t reg) const {
524 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
525 DCHECK(context_ != nullptr);
526 return context_->GetFPR(reg);
529 uintptr_t StackVisitor::GetReturnPc() const {
530 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
531 DCHECK(sp != nullptr);
532 uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
533 return *reinterpret_cast<uintptr_t*>(pc_addr);
536 void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
537 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
538 CHECK(sp != nullptr);
539 uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
540 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc;
543 size_t StackVisitor::ComputeNumFrames(Thread* thread, StackWalkKind walk_kind) {
544 struct NumFramesVisitor : public StackVisitor {
545 NumFramesVisitor(Thread* thread_in, StackWalkKind walk_kind_in)
546 : StackVisitor(thread_in, nullptr, walk_kind_in), frames(0) {}
548 bool VisitFrame() OVERRIDE {
555 NumFramesVisitor visitor(thread, walk_kind);
556 visitor.WalkStack(true);
557 return visitor.frames;
560 bool StackVisitor::GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc) {
561 struct HasMoreFramesVisitor : public StackVisitor {
562 HasMoreFramesVisitor(Thread* thread,
563 StackWalkKind walk_kind,
566 : StackVisitor(thread, nullptr, walk_kind, num_frames),
567 frame_height_(frame_height),
569 has_more_frames_(false),
570 next_method_(nullptr),
574 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
576 ArtMethod* method = GetMethod();
577 if (method != nullptr && !method->IsRuntimeMethod()) {
578 has_more_frames_ = true;
579 next_method_ = method;
580 next_dex_pc_ = GetDexPc();
581 return false; // End stack walk once next method is found.
583 } else if (GetFrameHeight() == frame_height_) {
589 size_t frame_height_;
591 bool has_more_frames_;
592 ArtMethod* next_method_;
593 uint32_t next_dex_pc_;
595 HasMoreFramesVisitor visitor(thread_, walk_kind_, GetNumFrames(), GetFrameHeight());
596 visitor.WalkStack(true);
597 *next_method = visitor.next_method_;
598 *next_dex_pc = visitor.next_dex_pc_;
599 return visitor.has_more_frames_;
602 void StackVisitor::DescribeStack(Thread* thread) {
603 struct DescribeStackVisitor : public StackVisitor {
604 explicit DescribeStackVisitor(Thread* thread_in)
605 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
607 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
608 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation();
612 DescribeStackVisitor visitor(thread);
613 visitor.WalkStack(true);
616 std::string StackVisitor::DescribeLocation() const {
617 std::string result("Visiting method '");
618 ArtMethod* m = GetMethod();
622 result += m->PrettyMethod();
623 result += StringPrintf("' at dex PC 0x%04x", GetDexPc());
624 if (!IsShadowFrame()) {
625 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc()));
630 void StackVisitor::SetMethod(ArtMethod* method) {
631 DCHECK(GetMethod() != nullptr);
632 if (cur_shadow_frame_ != nullptr) {
633 cur_shadow_frame_->SetMethod(method);
635 DCHECK(cur_quick_frame_ != nullptr);
636 CHECK(!IsInInlinedFrame()) << "We do not support setting inlined method's ArtMethod!";
637 *cur_quick_frame_ = method;
641 static void AssertPcIsWithinQuickCode(ArtMethod* method, uintptr_t pc)
642 REQUIRES_SHARED(Locks::mutator_lock_) {
643 if (method->IsNative() || method->IsRuntimeMethod() || method->IsProxyMethod()) {
647 if (pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) {
651 const void* code = method->GetEntryPointFromQuickCompiledCode();
652 if (code == GetQuickInstrumentationEntryPoint() || code == GetInvokeObsoleteMethodStub()) {
656 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
657 if (class_linker->IsQuickToInterpreterBridge(code) ||
658 class_linker->IsQuickResolutionStub(code)) {
662 // If we are the JIT then we may have just compiled the method after the
663 // IsQuickToInterpreterBridge check.
664 Runtime* runtime = Runtime::Current();
665 if (runtime->UseJitCompilation() && runtime->GetJit()->GetCodeCache()->ContainsPc(code)) {
669 uint32_t code_size = OatQuickMethodHeader::FromEntryPoint(code)->GetCodeSize();
670 uintptr_t code_start = reinterpret_cast<uintptr_t>(code);
671 CHECK(code_start <= pc && pc <= (code_start + code_size))
672 << method->PrettyMethod()
673 << " pc=" << std::hex << pc
674 << " code_start=" << code_start
675 << " code_size=" << code_size;
678 void StackVisitor::SanityCheckFrame() const {
680 ArtMethod* method = GetMethod();
681 auto* declaring_class = method->GetDeclaringClass();
682 // Runtime methods have null declaring class.
683 if (!method->IsRuntimeMethod()) {
684 CHECK(declaring_class != nullptr);
685 CHECK_EQ(declaring_class->GetClass(), declaring_class->GetClass()->GetClass())
688 CHECK(declaring_class == nullptr);
690 Runtime* const runtime = Runtime::Current();
691 LinearAlloc* const linear_alloc = runtime->GetLinearAlloc();
692 if (!linear_alloc->Contains(method)) {
693 // Check class linker linear allocs.
694 mirror::Class* klass = method->GetDeclaringClass();
695 LinearAlloc* const class_linear_alloc = (klass != nullptr)
696 ? runtime->GetClassLinker()->GetAllocatorForClassLoader(klass->GetClassLoader())
698 if (!class_linear_alloc->Contains(method)) {
699 // Check image space.
700 bool in_image = false;
701 for (auto& space : runtime->GetHeap()->GetContinuousSpaces()) {
702 if (space->IsImageSpace()) {
703 auto* image_space = space->AsImageSpace();
704 const auto& header = image_space->GetImageHeader();
705 const ImageSection& methods = header.GetMethodsSection();
706 const ImageSection& runtime_methods = header.GetRuntimeMethodsSection();
707 const size_t offset = reinterpret_cast<const uint8_t*>(method) - image_space->Begin();
708 if (methods.Contains(offset) || runtime_methods.Contains(offset)) {
714 CHECK(in_image) << method->PrettyMethod() << " not in linear alloc or image";
717 if (cur_quick_frame_ != nullptr) {
718 AssertPcIsWithinQuickCode(method, cur_quick_frame_pc_);
720 size_t frame_size = GetCurrentQuickFrameInfo().FrameSizeInBytes();
721 CHECK_NE(frame_size, 0u);
722 // A rough guess at an upper size we expect to see for a frame.
724 // 2 words HandleScope overhead
725 // 3+3 register spills
726 // TODO: this seems architecture specific for the case of JNI frames.
727 // TODO: 083-compiler-regressions ManyFloatArgs shows this estimate is wrong.
728 // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word);
729 const size_t kMaxExpectedFrameSize = 2 * KB;
730 CHECK_LE(frame_size, kMaxExpectedFrameSize) << method->PrettyMethod();
731 size_t return_pc_offset = GetCurrentQuickFrameInfo().GetReturnPcOffset();
732 CHECK_LT(return_pc_offset, frame_size);
737 // Counts the number of references in the parameter list of the corresponding method.
738 // Note: Thus does _not_ include "this" for non-static methods.
739 static uint32_t GetNumberOfReferenceArgsWithoutReceiver(ArtMethod* method)
740 REQUIRES_SHARED(Locks::mutator_lock_) {
742 const char* shorty = method->GetShorty(&shorty_len);
744 for (uint32_t i = 1; i < shorty_len ; ++i) {
745 if (shorty[i] == 'L') {
752 QuickMethodFrameInfo StackVisitor::GetCurrentQuickFrameInfo() const {
753 if (cur_oat_quick_method_header_ != nullptr) {
754 return cur_oat_quick_method_header_->GetFrameInfo();
757 ArtMethod* method = GetMethod();
758 Runtime* runtime = Runtime::Current();
760 if (method->IsAbstract()) {
761 return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kSaveRefsAndArgs);
764 // This goes before IsProxyMethod since runtime methods have a null declaring class.
765 if (method->IsRuntimeMethod()) {
766 return runtime->GetRuntimeMethodFrameInfo(method);
769 if (method->IsProxyMethod()) {
770 // There is only one direct method of a proxy class: the constructor. A direct method is
771 // cloned from the original java.lang.reflect.Proxy and is executed as usual quick
772 // compiled method without any stubs. Therefore the method must have a OatQuickMethodHeader.
773 DCHECK(!method->IsDirect() && !method->IsConstructor())
774 << "Constructors of proxy classes must have a OatQuickMethodHeader";
775 return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kSaveRefsAndArgs);
778 // The only remaining case is if the method is native and uses the generic JNI stub.
779 DCHECK(method->IsNative());
780 ClassLinker* class_linker = runtime->GetClassLinker();
781 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(method,
782 kRuntimePointerSize);
783 DCHECK(class_linker->IsQuickGenericJniStub(entry_point)) << method->PrettyMethod();
784 // Generic JNI frame.
785 uint32_t handle_refs = GetNumberOfReferenceArgsWithoutReceiver(method) + 1;
786 size_t scope_size = HandleScope::SizeOf(handle_refs);
787 QuickMethodFrameInfo callee_info =
788 runtime->GetCalleeSaveMethodFrameInfo(Runtime::kSaveRefsAndArgs);
790 // Callee saves + handle scope + method ref + alignment
791 // Note: -sizeof(void*) since callee-save frame stores a whole method pointer.
792 size_t frame_size = RoundUp(
793 callee_info.FrameSizeInBytes() - sizeof(void*) + sizeof(ArtMethod*) + scope_size,
795 return QuickMethodFrameInfo(frame_size, callee_info.CoreSpillMask(), callee_info.FpSpillMask());
798 template <StackVisitor::CountTransitions kCount>
799 void StackVisitor::WalkStack(bool include_transitions) {
800 if (check_suspended_) {
801 DCHECK(thread_ == Thread::Current() || thread_->IsSuspended());
803 CHECK_EQ(cur_depth_, 0U);
804 bool exit_stubs_installed = Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled();
805 uint32_t instrumentation_stack_depth = 0;
806 size_t inlined_frames_count = 0;
808 for (const ManagedStack* current_fragment = thread_->GetManagedStack();
809 current_fragment != nullptr; current_fragment = current_fragment->GetLink()) {
810 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
811 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
812 cur_quick_frame_pc_ = 0;
813 cur_oat_quick_method_header_ = nullptr;
815 if (cur_quick_frame_ != nullptr) { // Handle quick stack frames.
816 // Can't be both a shadow and a quick fragment.
817 DCHECK(current_fragment->GetTopShadowFrame() == nullptr);
818 ArtMethod* method = *cur_quick_frame_;
819 while (method != nullptr) {
820 cur_oat_quick_method_header_ = method->GetOatQuickMethodHeader(cur_quick_frame_pc_);
823 if ((walk_kind_ == StackWalkKind::kIncludeInlinedFrames)
824 && (cur_oat_quick_method_header_ != nullptr)
825 && cur_oat_quick_method_header_->IsOptimized()) {
826 CodeInfo code_info = cur_oat_quick_method_header_->GetOptimizedCodeInfo();
827 CodeInfoEncoding encoding = code_info.ExtractEncoding();
828 uint32_t native_pc_offset =
829 cur_oat_quick_method_header_->NativeQuickPcOffset(cur_quick_frame_pc_);
830 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
831 if (stack_map.IsValid() && stack_map.HasInlineInfo(encoding.stack_map.encoding)) {
832 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
833 DCHECK_EQ(current_inlining_depth_, 0u);
834 for (current_inlining_depth_ = inline_info.GetDepth(encoding.inline_info.encoding);
835 current_inlining_depth_ != 0;
836 --current_inlining_depth_) {
837 bool should_continue = VisitFrame();
838 if (UNLIKELY(!should_continue)) {
842 inlined_frames_count++;
847 bool should_continue = VisitFrame();
848 if (UNLIKELY(!should_continue)) {
852 QuickMethodFrameInfo frame_info = GetCurrentQuickFrameInfo();
853 if (context_ != nullptr) {
854 context_->FillCalleeSaves(reinterpret_cast<uint8_t*>(cur_quick_frame_), frame_info);
856 // Compute PC for next stack frame from return PC.
857 size_t frame_size = frame_info.FrameSizeInBytes();
858 size_t return_pc_offset = frame_size - sizeof(void*);
859 uint8_t* return_pc_addr = reinterpret_cast<uint8_t*>(cur_quick_frame_) + return_pc_offset;
860 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
862 if (UNLIKELY(exit_stubs_installed)) {
863 // While profiling, the return pc is restored from the side stack, except when walking
864 // the stack for an exception where the side stack will be unwound in VisitFrame.
865 if (reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc) {
866 CHECK_LT(instrumentation_stack_depth, thread_->GetInstrumentationStack()->size());
867 const instrumentation::InstrumentationStackFrame& instrumentation_frame =
868 thread_->GetInstrumentationStack()->at(instrumentation_stack_depth);
869 instrumentation_stack_depth++;
871 Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveAllCalleeSaves)) {
872 // Skip runtime save all callee frames which are used to deliver exceptions.
873 } else if (instrumentation_frame.interpreter_entry_) {
875 Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveRefsAndArgs);
876 CHECK_EQ(GetMethod(), callee) << "Expected: " << ArtMethod::PrettyMethod(callee)
877 << " Found: " << ArtMethod::PrettyMethod(GetMethod());
879 // Instrumentation generally doesn't distinguish between a method's obsolete and
880 // non-obsolete version.
881 CHECK_EQ(instrumentation_frame.method_->GetNonObsoleteMethod(),
882 GetMethod()->GetNonObsoleteMethod())
884 << ArtMethod::PrettyMethod(instrumentation_frame.method_->GetNonObsoleteMethod())
885 << " Found: " << ArtMethod::PrettyMethod(GetMethod()->GetNonObsoleteMethod());
887 if (num_frames_ != 0) {
888 // Check agreement of frame Ids only if num_frames_ is computed to avoid infinite
890 size_t frame_id = instrumentation::Instrumentation::ComputeFrameId(
893 inlined_frames_count);
894 CHECK_EQ(instrumentation_frame.frame_id_, frame_id);
896 return_pc = instrumentation_frame.return_pc_;
900 cur_quick_frame_pc_ = return_pc;
901 uint8_t* next_frame = reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size;
902 cur_quick_frame_ = reinterpret_cast<ArtMethod**>(next_frame);
904 if (kDebugStackWalk) {
905 LOG(INFO) << ArtMethod::PrettyMethod(method) << "@" << method << " size=" << frame_size
907 << " optimized=" << (cur_oat_quick_method_header_ != nullptr &&
908 cur_oat_quick_method_header_->IsOptimized())
909 << " native=" << method->IsNative()
911 << " entrypoints=" << method->GetEntryPointFromQuickCompiledCode()
912 << "," << (method->IsNative() ? method->GetEntryPointFromJni() : nullptr)
913 << " next=" << *cur_quick_frame_;
916 if (kCount == CountTransitions::kYes || !method->IsRuntimeMethod()) {
919 method = *cur_quick_frame_;
921 } else if (cur_shadow_frame_ != nullptr) {
924 bool should_continue = VisitFrame();
925 if (UNLIKELY(!should_continue)) {
929 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
930 } while (cur_shadow_frame_ != nullptr);
932 if (include_transitions) {
933 bool should_continue = VisitFrame();
934 if (!should_continue) {
938 if (kCount == CountTransitions::kYes) {
942 if (num_frames_ != 0) {
943 CHECK_EQ(cur_depth_, num_frames_);
947 template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kYes>(bool);
948 template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kNo>(bool);
950 void JavaFrameRootInfo::Describe(std::ostream& os) const {
951 const StackVisitor* visitor = stack_visitor_;
952 CHECK(visitor != nullptr);
953 os << "Type=" << GetType() << " thread_id=" << GetThreadId() << " location=" <<
954 visitor->DescribeLocation() << " vreg=" << vreg_;
957 int StackVisitor::GetVRegOffsetFromQuickCode(const DexFile::CodeItem* code_item,
958 uint32_t core_spills, uint32_t fp_spills,
959 size_t frame_size, int reg, InstructionSet isa) {
960 PointerSize pointer_size = InstructionSetPointerSize(isa);
962 auto* runtime = Runtime::Current();
963 if (runtime != nullptr) {
964 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
967 DCHECK_ALIGNED(frame_size, kStackAlignment);
969 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
970 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
971 + sizeof(uint32_t); // Filler.
972 int num_regs = code_item->registers_size_ - code_item->ins_size_;
973 int temp_threshold = code_item->registers_size_;
974 const int max_num_special_temps = 1;
975 if (reg == temp_threshold) {
976 // The current method pointer corresponds to special location on stack.
978 } else if (reg >= temp_threshold + max_num_special_temps) {
980 * Special temporaries may have custom locations and the logic above deals with that.
981 * However, non-special temporaries are placed relative to the outs.
983 int temps_start = code_item->outs_size_ * sizeof(uint32_t)
984 + static_cast<size_t>(pointer_size) /* art method */;
985 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
986 return temps_start + relative_offset;
987 } else if (reg < num_regs) {
988 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
989 return locals_start + (reg * sizeof(uint32_t));
992 return frame_size + ((reg - num_regs) * sizeof(uint32_t))
993 + static_cast<size_t>(pointer_size) /* art method */;
997 void LockCountData::AddMonitor(Thread* self, mirror::Object* obj) {
998 if (obj == nullptr) {
1002 // If there's an error during enter, we won't have locked the monitor. So check there's no
1004 if (self->IsExceptionPending()) {
1008 if (monitors_ == nullptr) {
1009 monitors_.reset(new std::vector<mirror::Object*>());
1011 monitors_->push_back(obj);
1014 void LockCountData::RemoveMonitorOrThrow(Thread* self, const mirror::Object* obj) {
1015 if (obj == nullptr) {
1018 bool found_object = false;
1019 if (monitors_ != nullptr) {
1020 // We need to remove one pointer to ref, as duplicates are used for counting recursive locks.
1021 // We arbitrarily choose the first one.
1022 auto it = std::find(monitors_->begin(), monitors_->end(), obj);
1023 if (it != monitors_->end()) {
1024 monitors_->erase(it);
1025 found_object = true;
1028 if (!found_object) {
1029 // The object wasn't found. Time for an IllegalMonitorStateException.
1030 // The order here isn't fully clear. Assume that any other pending exception is swallowed.
1031 // TODO: Maybe make already pending exception a suppressed exception.
1032 self->ClearException();
1033 self->ThrowNewExceptionF("Ljava/lang/IllegalMonitorStateException;",
1034 "did not lock monitor on object of type '%s' before unlocking",
1035 const_cast<mirror::Object*>(obj)->PrettyTypeOf().c_str());
1039 // Helper to unlock a monitor. Must be NO_THREAD_SAFETY_ANALYSIS, as we can't statically show
1040 // that the object was locked.
1041 void MonitorExitHelper(Thread* self, mirror::Object* obj) NO_THREAD_SAFETY_ANALYSIS {
1042 DCHECK(self != nullptr);
1043 DCHECK(obj != nullptr);
1044 obj->MonitorExit(self);
1047 bool LockCountData::CheckAllMonitorsReleasedOrThrow(Thread* self) {
1048 DCHECK(self != nullptr);
1049 if (monitors_ != nullptr) {
1050 if (!monitors_->empty()) {
1051 // There may be an exception pending, if the method is terminating abruptly. Clear it.
1052 // TODO: Should we add this as a suppressed exception?
1053 self->ClearException();
1055 // OK, there are monitors that are still locked. To enforce structured locking (and avoid
1056 // deadlocks) we unlock all of them before we raise the IllegalMonitorState exception.
1057 for (mirror::Object* obj : *monitors_) {
1058 MonitorExitHelper(self, obj);
1059 // If this raised an exception, ignore. TODO: Should we add this as suppressed
1061 if (self->IsExceptionPending()) {
1062 self->ClearException();
1065 // Raise an exception, just give the first object as the sample.
1066 mirror::Object* first = (*monitors_)[0];
1067 self->ThrowNewExceptionF("Ljava/lang/IllegalMonitorStateException;",
1068 "did not unlock monitor on object of type '%s'",
1069 mirror::Object::PrettyTypeOf(first).c_str());
1071 // To make sure this path is not triggered again, clean out the monitors.