blob: 19df0d26a1b1d29d86de6e8c121a71b3f4247af9 [file] [log] [blame]
Elliott Hughes68e76522011-10-05 13:22:16 -07001/*
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
Andreas Gampe46ee31b2016-12-14 10:11:49 -080019#include "android-base/stringprintf.h"
20
Ian Rogerse63db272014-07-15 15:36:11 -070021#include "arch/context.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.h"
Andreas Gampe8228cdf2017-05-30 15:03:54 -070023#include "base/callee_save_type.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070024#include "base/enums.h"
Dave Allisonf9439142014-03-27 15:10:22 -070025#include "base/hex_dump.h"
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010026#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070027#include "entrypoints/runtime_asm_entrypoints.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "gc/space/image_space.h"
29#include "gc/space/space-inl.h"
Andreas Gampe36a296f2017-06-13 14:11:11 -070030#include "interpreter/shadow_frame.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010031#include "jit/jit.h"
32#include "jit/jit_code_cache.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070033#include "linear_alloc.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070034#include "managed_stack.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070035#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/object-inl.h"
37#include "mirror/object_array-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010038#include "oat_quick_method_header.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010039#include "quick/quick_method_frame_info.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070040#include "runtime.h"
Dave Allisonf9439142014-03-27 15:10:22 -070041#include "thread.h"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070042#include "thread_list.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070043
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080044namespace art {
45
Andreas Gampe46ee31b2016-12-14 10:11:49 -080046using android::base::StringPrintf;
47
Mathieu Chartier8405bfd2016-02-05 12:00:49 -080048static constexpr bool kDebugStackWalk = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -070049
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -080050StackVisitor::StackVisitor(Thread* thread,
51 Context* context,
52 StackWalkKind walk_kind,
53 bool check_suspended)
54 : StackVisitor(thread, context, walk_kind, 0, check_suspended) {}
Ian Rogers7a22fa62013-01-23 12:16:16 -080055
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010056StackVisitor::StackVisitor(Thread* thread,
57 Context* context,
58 StackWalkKind walk_kind,
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -080059 size_t num_frames,
60 bool check_suspended)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010061 : thread_(thread),
62 walk_kind_(walk_kind),
63 cur_shadow_frame_(nullptr),
64 cur_quick_frame_(nullptr),
65 cur_quick_frame_pc_(0),
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010066 cur_oat_quick_method_header_(nullptr),
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010067 num_frames_(num_frames),
68 cur_depth_(0),
Nicolas Geoffray57f61612015-05-15 13:20:41 +010069 current_inlining_depth_(0),
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -080070 context_(context),
71 check_suspended_(check_suspended) {
72 if (check_suspended_) {
73 DCHECK(thread == Thread::Current() || thread->IsSuspended()) << *thread;
74 }
Ian Rogers5cf98192014-05-29 21:31:50 -070075}
76
Andreas Gampe36a296f2017-06-13 14:11:11 -070077static InlineInfo GetCurrentInlineInfo(const OatQuickMethodHeader* method_header,
78 uintptr_t cur_quick_frame_pc)
79 REQUIRES_SHARED(Locks::mutator_lock_) {
80 uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010081 CodeInfo code_info = method_header->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +000082 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Brazdilf677ebf2015-05-29 16:29:43 +010083 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +010084 DCHECK(stack_map.IsValid());
David Brazdilf677ebf2015-05-29 16:29:43 +010085 return code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +010086}
87
Mathieu Chartiere401d142015-04-22 13:56:20 -070088ArtMethod* StackVisitor::GetMethod() const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +010089 if (cur_shadow_frame_ != nullptr) {
90 return cur_shadow_frame_->GetMethod();
91 } else if (cur_quick_frame_ != nullptr) {
92 if (IsInInlinedFrame()) {
93 size_t depth_in_stack_map = current_inlining_depth_ - 1;
Andreas Gampe36a296f2017-06-13 14:11:11 -070094 InlineInfo inline_info = GetCurrentInlineInfo(GetCurrentOatQuickMethodHeader(),
95 cur_quick_frame_pc_);
David Srbecky61b28a12016-02-25 21:55:03 +000096 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
97 CodeInfoEncoding encoding = method_header->GetOptimizedCodeInfo().ExtractEncoding();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070098 MethodInfo method_info = method_header->GetOptimizedMethodInfo();
Mathieu Chartier45bf2502016-03-31 11:07:09 -070099 DCHECK(walk_kind_ != StackWalkKind::kSkipInlinedFrames);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100100 return GetResolvedMethod(*GetCurrentQuickFrame(),
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700101 method_info,
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100102 inline_info,
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800103 encoding.inline_info.encoding,
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100104 depth_in_stack_map);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100105 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700106 return *cur_quick_frame_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100107 }
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100108 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700109 return nullptr;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100110}
111
Dave Allisonb373e092014-02-20 16:06:36 -0800112uint32_t StackVisitor::GetDexPc(bool abort_on_failure) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700113 if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700114 return cur_shadow_frame_->GetDexPC();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700115 } else if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100116 if (IsInInlinedFrame()) {
117 size_t depth_in_stack_map = current_inlining_depth_ - 1;
David Srbecky61b28a12016-02-25 21:55:03 +0000118 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
119 CodeInfoEncoding encoding = method_header->GetOptimizedCodeInfo().ExtractEncoding();
Andreas Gampe36a296f2017-06-13 14:11:11 -0700120 return GetCurrentInlineInfo(GetCurrentOatQuickMethodHeader(), cur_quick_frame_pc_).
121 GetDexPcAtDepth(encoding.inline_info.encoding, depth_in_stack_map);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100122 } else if (cur_oat_quick_method_header_ == nullptr) {
123 return DexFile::kDexNoIndex;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100124 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100125 return cur_oat_quick_method_header_->ToDexPc(
126 GetMethod(), cur_quick_frame_pc_, abort_on_failure);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100127 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700128 } else {
129 return 0;
130 }
131}
132
Mathieu Chartiere401d142015-04-22 13:56:20 -0700133extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700134 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertza836bc92014-11-25 16:30:53 +0100135
Ian Rogers62d6c772013-02-27 08:32:07 -0800136mirror::Object* StackVisitor::GetThisObject() const {
Andreas Gampe542451c2016-07-26 09:02:02 -0700137 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700138 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800139 if (m->IsStatic()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100140 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800141 } else if (m->IsNative()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100142 if (cur_quick_frame_ != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700143 HandleScope* hs = reinterpret_cast<HandleScope*>(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100144 reinterpret_cast<char*>(cur_quick_frame_) + sizeof(ArtMethod*));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700145 return hs->GetReference(0);
Ian Rogers62d6c772013-02-27 08:32:07 -0800146 } else {
147 return cur_shadow_frame_->GetVRegReference(0);
148 }
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000149 } else if (m->IsProxyMethod()) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100150 if (cur_quick_frame_ != nullptr) {
151 return artQuickGetProxyThisObject(cur_quick_frame_);
152 } else {
153 return cur_shadow_frame_->GetVRegReference(0);
154 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800155 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700156 const DexFile::CodeItem* code_item = m->GetCodeItem();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100157 if (code_item == nullptr) {
Ian Rogerse0dcd462014-03-08 15:21:04 -0800158 UNIMPLEMENTED(ERROR) << "Failed to determine this object of abstract or proxy method: "
David Sehr709b0702016-10-13 09:12:37 -0700159 << ArtMethod::PrettyMethod(m);
Ian Rogerse0dcd462014-03-08 15:21:04 -0800160 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800161 } else {
162 uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000163 uint32_t value = 0;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700164 bool success = GetVReg(m, reg, kReferenceVReg, &value);
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000165 // We currently always guarantee the `this` object is live throughout the method.
David Sehr709b0702016-10-13 09:12:37 -0700166 CHECK(success) << "Failed to read the this object in " << ArtMethod::PrettyMethod(m);
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000167 return reinterpret_cast<mirror::Object*>(value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800168 }
169 }
170}
171
Ian Rogers0c7abda2012-09-19 13:33:42 -0700172size_t StackVisitor::GetNativePcOffset() const {
173 DCHECK(!IsShadowFrame());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100174 return GetCurrentOatQuickMethodHeader()->NativeQuickPcOffset(cur_quick_frame_pc_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700175}
176
Mingyao Yang99170c62015-07-06 11:10:37 -0700177bool StackVisitor::GetVRegFromDebuggerShadowFrame(uint16_t vreg,
178 VRegKind kind,
179 uint32_t* val) const {
180 size_t frame_id = const_cast<StackVisitor*>(this)->GetFrameId();
181 ShadowFrame* shadow_frame = thread_->FindDebuggerShadowFrame(frame_id);
182 if (shadow_frame != nullptr) {
183 bool* updated_vreg_flags = thread_->GetUpdatedVRegFlags(frame_id);
184 DCHECK(updated_vreg_flags != nullptr);
185 if (updated_vreg_flags[vreg]) {
186 // Value is set by the debugger.
187 if (kind == kReferenceVReg) {
188 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
189 shadow_frame->GetVRegReference(vreg)));
190 } else {
191 *val = shadow_frame->GetVReg(vreg);
192 }
193 return true;
194 }
195 }
196 // No value is set by the debugger.
197 return false;
198}
199
Mathieu Chartiere401d142015-04-22 13:56:20 -0700200bool StackVisitor::GetVReg(ArtMethod* m, uint16_t vreg, VRegKind kind, uint32_t* val) const {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200201 if (cur_quick_frame_ != nullptr) {
202 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800203 DCHECK(m == GetMethod());
Mingyao Yang99170c62015-07-06 11:10:37 -0700204 // Check if there is value set by the debugger.
205 if (GetVRegFromDebuggerShadowFrame(vreg, kind, val)) {
206 return true;
207 }
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100208 DCHECK(cur_oat_quick_method_header_->IsOptimized());
209 return GetVRegFromOptimizedCode(m, vreg, kind, val);
Ian Rogers0399dde2012-06-06 17:09:28 -0700210 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100211 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertz09687442015-11-17 10:35:39 +0100212 if (kind == kReferenceVReg) {
213 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
214 cur_shadow_frame_->GetVRegReference(vreg)));
215 } else {
216 *val = cur_shadow_frame_->GetVReg(vreg);
217 }
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200218 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700219 }
220}
221
Mathieu Chartiere401d142015-04-22 13:56:20 -0700222bool StackVisitor::GetVRegFromOptimizedCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100223 uint32_t* val) const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100224 DCHECK_EQ(m, GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100225 const DexFile::CodeItem* code_item = m->GetCodeItem();
David Sehr709b0702016-10-13 09:12:37 -0700226 DCHECK(code_item != nullptr) << m->PrettyMethod(); // Can't be null or how would we compile
227 // its instructions?
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000228 uint16_t number_of_dex_registers = code_item->registers_size_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100229 DCHECK_LT(vreg, code_item->registers_size_);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100230 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
231 CodeInfo code_info = method_header->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000232 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100233
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100234 uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100235 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100236 DCHECK(stack_map.IsValid());
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100237 size_t depth_in_stack_map = current_inlining_depth_ - 1;
238
239 DexRegisterMap dex_register_map = IsInInlinedFrame()
David Brazdilf677ebf2015-05-29 16:29:43 +0100240 ? code_info.GetDexRegisterMapAtDepth(depth_in_stack_map,
241 code_info.GetInlineInfoOf(stack_map, encoding),
242 encoding,
243 number_of_dex_registers)
244 : code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100245
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000246 if (!dex_register_map.IsValid()) {
247 return false;
248 }
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000249 DexRegisterLocation::Kind location_kind =
David Brazdilf677ebf2015-05-29 16:29:43 +0100250 dex_register_map.GetLocationKind(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100251 switch (location_kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000252 case DexRegisterLocation::Kind::kInStack: {
David Brazdilf677ebf2015-05-29 16:29:43 +0100253 const int32_t offset = dex_register_map.GetStackOffsetInBytes(vreg,
254 number_of_dex_registers,
255 code_info,
256 encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100257 const uint8_t* addr = reinterpret_cast<const uint8_t*>(cur_quick_frame_) + offset;
258 *val = *reinterpret_cast<const uint32_t*>(addr);
259 return true;
260 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000261 case DexRegisterLocation::Kind::kInRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100262 case DexRegisterLocation::Kind::kInRegisterHigh:
263 case DexRegisterLocation::Kind::kInFpuRegister:
264 case DexRegisterLocation::Kind::kInFpuRegisterHigh: {
David Brazdilf677ebf2015-05-29 16:29:43 +0100265 uint32_t reg =
266 dex_register_map.GetMachineRegister(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100267 return GetRegisterIfAccessible(reg, kind, val);
268 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000269 case DexRegisterLocation::Kind::kConstant:
David Brazdilf677ebf2015-05-29 16:29:43 +0100270 *val = dex_register_map.GetConstant(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100271 return true;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000272 case DexRegisterLocation::Kind::kNone:
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100273 return false;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000274 default:
275 LOG(FATAL)
David Srbecky7dc11782016-02-25 13:23:56 +0000276 << "Unexpected location kind "
277 << dex_register_map.GetLocationInternalKind(vreg,
278 number_of_dex_registers,
279 code_info,
280 encoding);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000281 UNREACHABLE();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100282 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100283}
284
285bool StackVisitor::GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const {
286 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
David Brazdil77a48ae2015-09-15 12:34:04 +0000287
Vladimir Marko239d6ea2016-09-05 10:44:04 +0100288 if (kRuntimeISA == InstructionSet::kX86 && is_float) {
289 // X86 float registers are 64-bit and each XMM register is provided as two separate
290 // 32-bit registers by the context.
291 reg = (kind == kDoubleHiVReg) ? (2 * reg + 1) : (2 * reg);
292 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000293
Goran Jakovljevic986660c2015-12-10 11:44:50 +0100294 // MIPS32 float registers are used as 64-bit (for MIPS32r2 it is pair
295 // F(2n)-F(2n+1), and for MIPS32r6 it is 64-bit register F(2n)). When
296 // accessing upper 32-bits from double, reg + 1 should be used.
297 if ((kRuntimeISA == InstructionSet::kMips) && (kind == kDoubleHiVReg)) {
298 DCHECK_ALIGNED(reg, 2);
299 reg++;
300 }
301
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100302 if (!IsAccessibleRegister(reg, is_float)) {
303 return false;
304 }
305 uintptr_t ptr_val = GetRegister(reg, is_float);
306 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
307 if (target64) {
308 const bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
309 const bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
310 int64_t value_long = static_cast<int64_t>(ptr_val);
311 if (wide_lo) {
312 ptr_val = static_cast<uintptr_t>(Low32Bits(value_long));
313 } else if (wide_hi) {
314 ptr_val = static_cast<uintptr_t>(High32Bits(value_long));
315 }
316 }
317 *val = ptr_val;
318 return true;
319}
320
Mingyao Yang99170c62015-07-06 11:10:37 -0700321bool StackVisitor::GetVRegPairFromDebuggerShadowFrame(uint16_t vreg,
322 VRegKind kind_lo,
323 VRegKind kind_hi,
324 uint64_t* val) const {
325 uint32_t low_32bits;
326 uint32_t high_32bits;
327 bool success = GetVRegFromDebuggerShadowFrame(vreg, kind_lo, &low_32bits);
328 success &= GetVRegFromDebuggerShadowFrame(vreg + 1, kind_hi, &high_32bits);
329 if (success) {
330 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
331 }
332 return success;
333}
334
Mathieu Chartiere401d142015-04-22 13:56:20 -0700335bool StackVisitor::GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200336 VRegKind kind_hi, uint64_t* val) const {
337 if (kind_lo == kLongLoVReg) {
338 DCHECK_EQ(kind_hi, kLongHiVReg);
339 } else if (kind_lo == kDoubleLoVReg) {
340 DCHECK_EQ(kind_hi, kDoubleHiVReg);
341 } else {
342 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100343 UNREACHABLE();
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200344 }
Mingyao Yang99170c62015-07-06 11:10:37 -0700345 // Check if there is value set by the debugger.
346 if (GetVRegPairFromDebuggerShadowFrame(vreg, kind_lo, kind_hi, val)) {
347 return true;
348 }
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200349 if (cur_quick_frame_ != nullptr) {
350 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
351 DCHECK(m == GetMethod());
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100352 DCHECK(cur_oat_quick_method_header_->IsOptimized());
353 return GetVRegPairFromOptimizedCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200354 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100355 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200356 *val = cur_shadow_frame_->GetVRegLong(vreg);
357 return true;
358 }
359}
360
Mathieu Chartiere401d142015-04-22 13:56:20 -0700361bool StackVisitor::GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100362 VRegKind kind_lo, VRegKind kind_hi,
363 uint64_t* val) const {
364 uint32_t low_32bits;
365 uint32_t high_32bits;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700366 bool success = GetVRegFromOptimizedCode(m, vreg, kind_lo, &low_32bits);
367 success &= GetVRegFromOptimizedCode(m, vreg + 1, kind_hi, &high_32bits);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100368 if (success) {
369 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
370 }
371 return success;
372}
373
374bool StackVisitor::GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
375 VRegKind kind_lo, uint64_t* val) const {
376 const bool is_float = (kind_lo == kDoubleLoVReg);
377 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
378 return false;
379 }
380 uintptr_t ptr_val_lo = GetRegister(reg_lo, is_float);
381 uintptr_t ptr_val_hi = GetRegister(reg_hi, is_float);
382 bool target64 = Is64BitInstructionSet(kRuntimeISA);
383 if (target64) {
384 int64_t value_long_lo = static_cast<int64_t>(ptr_val_lo);
385 int64_t value_long_hi = static_cast<int64_t>(ptr_val_hi);
386 ptr_val_lo = static_cast<uintptr_t>(Low32Bits(value_long_lo));
387 ptr_val_hi = static_cast<uintptr_t>(High32Bits(value_long_hi));
388 }
389 *val = (static_cast<uint64_t>(ptr_val_hi) << 32) | static_cast<uint32_t>(ptr_val_lo);
390 return true;
391}
392
Mingyao Yang636b9252015-07-31 16:40:24 -0700393bool StackVisitor::SetVReg(ArtMethod* m,
394 uint16_t vreg,
395 uint32_t new_value,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800396 VRegKind kind) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700397 const DexFile::CodeItem* code_item = m->GetCodeItem();
398 if (code_item == nullptr) {
399 return false;
400 }
401 ShadowFrame* shadow_frame = GetCurrentShadowFrame();
402 if (shadow_frame == nullptr) {
403 // This is a compiled frame: we must prepare and update a shadow frame that will
404 // be executed by the interpreter after deoptimization of the stack.
405 const size_t frame_id = GetFrameId();
406 const uint16_t num_regs = code_item->registers_size_;
407 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
408 CHECK(shadow_frame != nullptr);
409 // Remember the vreg has been set for debugging and must not be overwritten by the
410 // original value during deoptimization of the stack.
411 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
412 }
413 if (kind == kReferenceVReg) {
414 shadow_frame->SetVRegReference(vreg, reinterpret_cast<mirror::Object*>(new_value));
415 } else {
416 shadow_frame->SetVReg(vreg, new_value);
417 }
418 return true;
419}
420
Mingyao Yang636b9252015-07-31 16:40:24 -0700421bool StackVisitor::SetVRegPair(ArtMethod* m,
422 uint16_t vreg,
423 uint64_t new_value,
424 VRegKind kind_lo,
425 VRegKind kind_hi) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700426 if (kind_lo == kLongLoVReg) {
427 DCHECK_EQ(kind_hi, kLongHiVReg);
428 } else if (kind_lo == kDoubleLoVReg) {
429 DCHECK_EQ(kind_hi, kDoubleHiVReg);
430 } else {
431 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
432 UNREACHABLE();
433 }
434 const DexFile::CodeItem* code_item = m->GetCodeItem();
435 if (code_item == nullptr) {
436 return false;
437 }
438 ShadowFrame* shadow_frame = GetCurrentShadowFrame();
439 if (shadow_frame == nullptr) {
440 // This is a compiled frame: we must prepare for deoptimization (see SetVRegFromDebugger).
441 const size_t frame_id = GetFrameId();
442 const uint16_t num_regs = code_item->registers_size_;
443 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
444 CHECK(shadow_frame != nullptr);
445 // Remember the vreg pair has been set for debugging and must not be overwritten by the
446 // original value during deoptimization of the stack.
447 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
448 thread_->GetUpdatedVRegFlags(frame_id)[vreg + 1] = true;
449 }
450 shadow_frame->SetVRegLong(vreg, new_value);
451 return true;
452}
453
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100454bool StackVisitor::IsAccessibleGPR(uint32_t reg) const {
455 DCHECK(context_ != nullptr);
456 return context_->IsAccessibleGPR(reg);
457}
458
Mathieu Chartier815873e2014-02-13 18:02:13 -0800459uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100460 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
461 DCHECK(context_ != nullptr);
Mathieu Chartier815873e2014-02-13 18:02:13 -0800462 return context_->GetGPRAddress(reg);
463}
464
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100465uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
466 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
467 DCHECK(context_ != nullptr);
468 return context_->GetGPR(reg);
Ian Rogers0399dde2012-06-06 17:09:28 -0700469}
470
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100471bool StackVisitor::IsAccessibleFPR(uint32_t reg) const {
472 DCHECK(context_ != nullptr);
473 return context_->IsAccessibleFPR(reg);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200474}
475
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100476uintptr_t StackVisitor::GetFPR(uint32_t reg) const {
477 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
478 DCHECK(context_ != nullptr);
479 return context_->GetFPR(reg);
480}
481
Ian Rogers0399dde2012-06-06 17:09:28 -0700482uintptr_t StackVisitor::GetReturnPc() const {
Ian Rogers13735952014-10-08 12:43:28 -0700483 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700484 DCHECK(sp != nullptr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100485 uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogers0399dde2012-06-06 17:09:28 -0700486 return *reinterpret_cast<uintptr_t*>(pc_addr);
487}
488
489void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
Ian Rogers13735952014-10-08 12:43:28 -0700490 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700491 CHECK(sp != nullptr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100492 uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogers0399dde2012-06-06 17:09:28 -0700493 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc;
494}
495
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100496size_t StackVisitor::ComputeNumFrames(Thread* thread, StackWalkKind walk_kind) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700497 struct NumFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100498 NumFramesVisitor(Thread* thread_in, StackWalkKind walk_kind_in)
499 : StackVisitor(thread_in, nullptr, walk_kind_in), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700500
Ian Rogers5cf98192014-05-29 21:31:50 -0700501 bool VisitFrame() OVERRIDE {
Ian Rogers0399dde2012-06-06 17:09:28 -0700502 frames++;
503 return true;
504 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700505
Ian Rogers0399dde2012-06-06 17:09:28 -0700506 size_t frames;
507 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100508 NumFramesVisitor visitor(thread, walk_kind);
Ian Rogers0399dde2012-06-06 17:09:28 -0700509 visitor.WalkStack(true);
510 return visitor.frames;
511}
512
Mathieu Chartiere401d142015-04-22 13:56:20 -0700513bool StackVisitor::GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700514 struct HasMoreFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100515 HasMoreFramesVisitor(Thread* thread,
516 StackWalkKind walk_kind,
517 size_t num_frames,
518 size_t frame_height)
519 : StackVisitor(thread, nullptr, walk_kind, num_frames),
520 frame_height_(frame_height),
521 found_frame_(false),
522 has_more_frames_(false),
523 next_method_(nullptr),
524 next_dex_pc_(0) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700525 }
526
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700527 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700528 if (found_frame_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700529 ArtMethod* method = GetMethod();
Ian Rogers5cf98192014-05-29 21:31:50 -0700530 if (method != nullptr && !method->IsRuntimeMethod()) {
531 has_more_frames_ = true;
532 next_method_ = method;
533 next_dex_pc_ = GetDexPc();
534 return false; // End stack walk once next method is found.
535 }
536 } else if (GetFrameHeight() == frame_height_) {
537 found_frame_ = true;
538 }
539 return true;
540 }
541
542 size_t frame_height_;
543 bool found_frame_;
544 bool has_more_frames_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700545 ArtMethod* next_method_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700546 uint32_t next_dex_pc_;
547 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100548 HasMoreFramesVisitor visitor(thread_, walk_kind_, GetNumFrames(), GetFrameHeight());
Ian Rogers5cf98192014-05-29 21:31:50 -0700549 visitor.WalkStack(true);
550 *next_method = visitor.next_method_;
551 *next_dex_pc = visitor.next_dex_pc_;
552 return visitor.has_more_frames_;
553}
554
Ian Rogers7a22fa62013-01-23 12:16:16 -0800555void StackVisitor::DescribeStack(Thread* thread) {
Ian Rogers306057f2012-11-26 12:45:53 -0800556 struct DescribeStackVisitor : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800557 explicit DescribeStackVisitor(Thread* thread_in)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100558 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800559
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700560 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers306057f2012-11-26 12:45:53 -0800561 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation();
562 return true;
563 }
564 };
Ian Rogers7a22fa62013-01-23 12:16:16 -0800565 DescribeStackVisitor visitor(thread);
Ian Rogers306057f2012-11-26 12:45:53 -0800566 visitor.WalkStack(true);
567}
568
Ian Rogers40e3bac2012-11-20 00:09:14 -0800569std::string StackVisitor::DescribeLocation() const {
570 std::string result("Visiting method '");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700571 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700572 if (m == nullptr) {
Ian Rogers306057f2012-11-26 12:45:53 -0800573 return "upcall";
574 }
David Sehr709b0702016-10-13 09:12:37 -0700575 result += m->PrettyMethod();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800576 result += StringPrintf("' at dex PC 0x%04x", GetDexPc());
Ian Rogers40e3bac2012-11-20 00:09:14 -0800577 if (!IsShadowFrame()) {
578 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc()));
579 }
580 return result;
581}
582
Alex Lightdba61482016-12-21 08:20:29 -0800583void StackVisitor::SetMethod(ArtMethod* method) {
584 DCHECK(GetMethod() != nullptr);
585 if (cur_shadow_frame_ != nullptr) {
586 cur_shadow_frame_->SetMethod(method);
587 } else {
588 DCHECK(cur_quick_frame_ != nullptr);
589 CHECK(!IsInInlinedFrame()) << "We do not support setting inlined method's ArtMethod!";
Alex Light1ebe4fe2017-01-30 14:57:11 -0800590 *cur_quick_frame_ = method;
Alex Lightdba61482016-12-21 08:20:29 -0800591 }
592}
593
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100594static void AssertPcIsWithinQuickCode(ArtMethod* method, uintptr_t pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700595 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100596 if (method->IsNative() || method->IsRuntimeMethod() || method->IsProxyMethod()) {
597 return;
598 }
599
600 if (pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) {
601 return;
602 }
603
Mingyao Yang88ca8ba2017-05-23 14:21:07 -0700604 Runtime* runtime = Runtime::Current();
605 if (runtime->UseJitCompilation() &&
606 runtime->GetJit()->GetCodeCache()->ContainsPc(reinterpret_cast<const void*>(pc))) {
607 return;
608 }
609
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100610 const void* code = method->GetEntryPointFromQuickCompiledCode();
Alex Lightdb01a092017-04-03 15:39:55 -0700611 if (code == GetQuickInstrumentationEntryPoint() || code == GetInvokeObsoleteMethodStub()) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100612 return;
613 }
614
615 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
616 if (class_linker->IsQuickToInterpreterBridge(code) ||
617 class_linker->IsQuickResolutionStub(code)) {
618 return;
619 }
620
Calin Juravleffc87072016-04-20 14:22:09 +0100621 if (runtime->UseJitCompilation() && runtime->GetJit()->GetCodeCache()->ContainsPc(code)) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100622 return;
623 }
624
Mingyao Yang063fc772016-08-02 11:02:54 -0700625 uint32_t code_size = OatQuickMethodHeader::FromEntryPoint(code)->GetCodeSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100626 uintptr_t code_start = reinterpret_cast<uintptr_t>(code);
627 CHECK(code_start <= pc && pc <= (code_start + code_size))
David Sehr709b0702016-10-13 09:12:37 -0700628 << method->PrettyMethod()
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100629 << " pc=" << std::hex << pc
Roland Levillain0d5a2812015-11-13 10:07:31 +0000630 << " code_start=" << code_start
631 << " code_size=" << code_size;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100632}
633
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700634void StackVisitor::SanityCheckFrame() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800635 if (kIsDebugBuild) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700636 ArtMethod* method = GetMethod();
637 auto* declaring_class = method->GetDeclaringClass();
638 // Runtime methods have null declaring class.
639 if (!method->IsRuntimeMethod()) {
640 CHECK(declaring_class != nullptr);
641 CHECK_EQ(declaring_class->GetClass(), declaring_class->GetClass()->GetClass())
642 << declaring_class;
643 } else {
644 CHECK(declaring_class == nullptr);
645 }
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700646 Runtime* const runtime = Runtime::Current();
647 LinearAlloc* const linear_alloc = runtime->GetLinearAlloc();
648 if (!linear_alloc->Contains(method)) {
649 // Check class linker linear allocs.
650 mirror::Class* klass = method->GetDeclaringClass();
651 LinearAlloc* const class_linear_alloc = (klass != nullptr)
Mathieu Chartier5b830502016-03-02 10:30:23 -0800652 ? runtime->GetClassLinker()->GetAllocatorForClassLoader(klass->GetClassLoader())
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700653 : linear_alloc;
654 if (!class_linear_alloc->Contains(method)) {
655 // Check image space.
656 bool in_image = false;
657 for (auto& space : runtime->GetHeap()->GetContinuousSpaces()) {
658 if (space->IsImageSpace()) {
659 auto* image_space = space->AsImageSpace();
660 const auto& header = image_space->GetImageHeader();
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700661 const ImageSection& methods = header.GetMethodsSection();
662 const ImageSection& runtime_methods = header.GetRuntimeMethodsSection();
663 const size_t offset = reinterpret_cast<const uint8_t*>(method) - image_space->Begin();
664 if (methods.Contains(offset) || runtime_methods.Contains(offset)) {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700665 in_image = true;
666 break;
667 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700668 }
669 }
David Sehr709b0702016-10-13 09:12:37 -0700670 CHECK(in_image) << method->PrettyMethod() << " not in linear alloc or image";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700671 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700672 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800673 if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100674 AssertPcIsWithinQuickCode(method, cur_quick_frame_pc_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800675 // Frame sanity.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100676 size_t frame_size = GetCurrentQuickFrameInfo().FrameSizeInBytes();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800677 CHECK_NE(frame_size, 0u);
Andreas Gampe5b417b92014-03-10 14:18:35 -0700678 // A rough guess at an upper size we expect to see for a frame.
679 // 256 registers
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700680 // 2 words HandleScope overhead
Andreas Gampe5b417b92014-03-10 14:18:35 -0700681 // 3+3 register spills
682 // TODO: this seems architecture specific for the case of JNI frames.
Brian Carlstromed08bd42014-03-19 18:34:17 -0700683 // TODO: 083-compiler-regressions ManyFloatArgs shows this estimate is wrong.
684 // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word);
685 const size_t kMaxExpectedFrameSize = 2 * KB;
David Sehr709b0702016-10-13 09:12:37 -0700686 CHECK_LE(frame_size, kMaxExpectedFrameSize) << method->PrettyMethod();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100687 size_t return_pc_offset = GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800688 CHECK_LT(return_pc_offset, frame_size);
689 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700690 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700691}
692
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100693// Counts the number of references in the parameter list of the corresponding method.
694// Note: Thus does _not_ include "this" for non-static methods.
695static uint32_t GetNumberOfReferenceArgsWithoutReceiver(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700696 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100697 uint32_t shorty_len;
698 const char* shorty = method->GetShorty(&shorty_len);
699 uint32_t refs = 0;
700 for (uint32_t i = 1; i < shorty_len ; ++i) {
701 if (shorty[i] == 'L') {
702 refs++;
703 }
704 }
705 return refs;
706}
707
708QuickMethodFrameInfo StackVisitor::GetCurrentQuickFrameInfo() const {
709 if (cur_oat_quick_method_header_ != nullptr) {
710 return cur_oat_quick_method_header_->GetFrameInfo();
711 }
712
713 ArtMethod* method = GetMethod();
714 Runtime* runtime = Runtime::Current();
715
716 if (method->IsAbstract()) {
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700717 return runtime->GetCalleeSaveMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100718 }
719
720 // This goes before IsProxyMethod since runtime methods have a null declaring class.
721 if (method->IsRuntimeMethod()) {
722 return runtime->GetRuntimeMethodFrameInfo(method);
723 }
724
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100725 if (method->IsProxyMethod()) {
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000726 // There is only one direct method of a proxy class: the constructor. A direct method is
727 // cloned from the original java.lang.reflect.Proxy and is executed as usual quick
728 // compiled method without any stubs. Therefore the method must have a OatQuickMethodHeader.
729 DCHECK(!method->IsDirect() && !method->IsConstructor())
730 << "Constructors of proxy classes must have a OatQuickMethodHeader";
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700731 return runtime->GetCalleeSaveMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100732 }
733
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000734 // The only remaining case is if the method is native and uses the generic JNI stub.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100735 DCHECK(method->IsNative());
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000736 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampe542451c2016-07-26 09:02:02 -0700737 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(method,
738 kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700739 DCHECK(class_linker->IsQuickGenericJniStub(entry_point)) << method->PrettyMethod();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100740 // Generic JNI frame.
741 uint32_t handle_refs = GetNumberOfReferenceArgsWithoutReceiver(method) + 1;
742 size_t scope_size = HandleScope::SizeOf(handle_refs);
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100743 QuickMethodFrameInfo callee_info =
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700744 runtime->GetCalleeSaveMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100745
746 // Callee saves + handle scope + method ref + alignment
747 // Note: -sizeof(void*) since callee-save frame stores a whole method pointer.
748 size_t frame_size = RoundUp(
749 callee_info.FrameSizeInBytes() - sizeof(void*) + sizeof(ArtMethod*) + scope_size,
750 kStackAlignment);
751 return QuickMethodFrameInfo(frame_size, callee_info.CoreSpillMask(), callee_info.FpSpillMask());
752}
753
Andreas Gampe585da952016-12-02 14:52:29 -0800754template <StackVisitor::CountTransitions kCount>
Ian Rogers0399dde2012-06-06 17:09:28 -0700755void StackVisitor::WalkStack(bool include_transitions) {
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -0800756 if (check_suspended_) {
757 DCHECK(thread_ == Thread::Current() || thread_->IsSuspended());
758 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800759 CHECK_EQ(cur_depth_, 0U);
760 bool exit_stubs_installed = Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled();
Alex Lightb81a9842016-12-15 00:59:05 +0000761 uint32_t instrumentation_stack_depth = 0;
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000762 size_t inlined_frames_count = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700763
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700764 for (const ManagedStack* current_fragment = thread_->GetManagedStack();
765 current_fragment != nullptr; current_fragment = current_fragment->GetLink()) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700766 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
767 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700768 cur_quick_frame_pc_ = 0;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100769 cur_oat_quick_method_header_ = nullptr;
Dave Allisonf9439142014-03-27 15:10:22 -0700770
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700771 if (cur_quick_frame_ != nullptr) { // Handle quick stack frames.
Ian Rogers0399dde2012-06-06 17:09:28 -0700772 // Can't be both a shadow and a quick fragment.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700773 DCHECK(current_fragment->GetTopShadowFrame() == nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700774 ArtMethod* method = *cur_quick_frame_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700775 while (method != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100776 cur_oat_quick_method_header_ = method->GetOatQuickMethodHeader(cur_quick_frame_pc_);
Dave Allison5cd33752014-04-15 15:57:58 -0700777 SanityCheckFrame();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100778
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100779 if ((walk_kind_ == StackWalkKind::kIncludeInlinedFrames)
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100780 && (cur_oat_quick_method_header_ != nullptr)
781 && cur_oat_quick_method_header_->IsOptimized()) {
782 CodeInfo code_info = cur_oat_quick_method_header_->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000783 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100784 uint32_t native_pc_offset =
785 cur_oat_quick_method_header_->NativeQuickPcOffset(cur_quick_frame_pc_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100786 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800787 if (stack_map.IsValid() && stack_map.HasInlineInfo(encoding.stack_map.encoding)) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100788 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100789 DCHECK_EQ(current_inlining_depth_, 0u);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800790 for (current_inlining_depth_ = inline_info.GetDepth(encoding.inline_info.encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100791 current_inlining_depth_ != 0;
792 --current_inlining_depth_) {
793 bool should_continue = VisitFrame();
794 if (UNLIKELY(!should_continue)) {
795 return;
796 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100797 cur_depth_++;
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000798 inlined_frames_count++;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100799 }
800 }
801 }
802
Dave Allison5cd33752014-04-15 15:57:58 -0700803 bool should_continue = VisitFrame();
804 if (UNLIKELY(!should_continue)) {
805 return;
Ian Rogers0399dde2012-06-06 17:09:28 -0700806 }
Dave Allison5cd33752014-04-15 15:57:58 -0700807
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100808 QuickMethodFrameInfo frame_info = GetCurrentQuickFrameInfo();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700809 if (context_ != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100810 context_->FillCalleeSaves(reinterpret_cast<uint8_t*>(cur_quick_frame_), frame_info);
Ian Rogers0399dde2012-06-06 17:09:28 -0700811 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700812 // Compute PC for next stack frame from return PC.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100813 size_t frame_size = frame_info.FrameSizeInBytes();
814 size_t return_pc_offset = frame_size - sizeof(void*);
Ian Rogers13735952014-10-08 12:43:28 -0700815 uint8_t* return_pc_addr = reinterpret_cast<uint8_t*>(cur_quick_frame_) + return_pc_offset;
Ian Rogers0399dde2012-06-06 17:09:28 -0700816 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100817
Ian Rogers62d6c772013-02-27 08:32:07 -0800818 if (UNLIKELY(exit_stubs_installed)) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700819 // While profiling, the return pc is restored from the side stack, except when walking
820 // the stack for an exception where the side stack will be unwound in VisitFrame.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700821 if (reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc) {
Andreas Gampe585da952016-12-02 14:52:29 -0800822 CHECK_LT(instrumentation_stack_depth, thread_->GetInstrumentationStack()->size());
Sebastien Hertz74e256b2013-10-04 10:40:37 +0200823 const instrumentation::InstrumentationStackFrame& instrumentation_frame =
Alex Lightb81a9842016-12-15 00:59:05 +0000824 thread_->GetInstrumentationStack()->at(instrumentation_stack_depth);
jeffhao725a9572012-11-13 18:20:12 -0800825 instrumentation_stack_depth++;
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100826 if (GetMethod() ==
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700827 Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveAllCalleeSaves)) {
Jeff Haofb2802d2013-07-24 13:53:05 -0700828 // Skip runtime save all callee frames which are used to deliver exceptions.
829 } else if (instrumentation_frame.interpreter_entry_) {
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100830 ArtMethod* callee =
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700831 Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs);
David Sehr709b0702016-10-13 09:12:37 -0700832 CHECK_EQ(GetMethod(), callee) << "Expected: " << ArtMethod::PrettyMethod(callee)
833 << " Found: " << ArtMethod::PrettyMethod(GetMethod());
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000834 } else {
Alex Lighteee0bd42017-02-14 15:31:45 +0000835 // Instrumentation generally doesn't distinguish between a method's obsolete and
836 // non-obsolete version.
837 CHECK_EQ(instrumentation_frame.method_->GetNonObsoleteMethod(),
838 GetMethod()->GetNonObsoleteMethod())
839 << "Expected: "
840 << ArtMethod::PrettyMethod(instrumentation_frame.method_->GetNonObsoleteMethod())
841 << " Found: " << ArtMethod::PrettyMethod(GetMethod()->GetNonObsoleteMethod());
Ian Rogers62d6c772013-02-27 08:32:07 -0800842 }
843 if (num_frames_ != 0) {
844 // Check agreement of frame Ids only if num_frames_ is computed to avoid infinite
845 // recursion.
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000846 size_t frame_id = instrumentation::Instrumentation::ComputeFrameId(
847 thread_,
848 cur_depth_,
849 inlined_frames_count);
850 CHECK_EQ(instrumentation_frame.frame_id_, frame_id);
Ian Rogers62d6c772013-02-27 08:32:07 -0800851 }
jeffhao725a9572012-11-13 18:20:12 -0800852 return_pc = instrumentation_frame.return_pc_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700853 }
854 }
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100855
Ian Rogers0399dde2012-06-06 17:09:28 -0700856 cur_quick_frame_pc_ = return_pc;
Ian Rogers13735952014-10-08 12:43:28 -0700857 uint8_t* next_frame = reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700858 cur_quick_frame_ = reinterpret_cast<ArtMethod**>(next_frame);
859
860 if (kDebugStackWalk) {
David Sehr709b0702016-10-13 09:12:37 -0700861 LOG(INFO) << ArtMethod::PrettyMethod(method) << "@" << method << " size=" << frame_size
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100862 << std::boolalpha
863 << " optimized=" << (cur_oat_quick_method_header_ != nullptr &&
864 cur_oat_quick_method_header_->IsOptimized())
Mathieu Chartiere401d142015-04-22 13:56:20 -0700865 << " native=" << method->IsNative()
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100866 << std::noboolalpha
Mathieu Chartiere401d142015-04-22 13:56:20 -0700867 << " entrypoints=" << method->GetEntryPointFromQuickCompiledCode()
Alex Lighteee0bd42017-02-14 15:31:45 +0000868 << "," << (method->IsNative() ? method->GetEntryPointFromJni() : nullptr)
Mathieu Chartiere401d142015-04-22 13:56:20 -0700869 << " next=" << *cur_quick_frame_;
870 }
871
Andreas Gampef040be62017-04-14 21:49:33 -0700872 if (kCount == CountTransitions::kYes || !method->IsRuntimeMethod()) {
873 cur_depth_++;
874 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700875 method = *cur_quick_frame_;
jeffhao6641ea12013-01-02 18:13:42 -0800876 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700877 } else if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700878 do {
879 SanityCheckFrame();
880 bool should_continue = VisitFrame();
881 if (UNLIKELY(!should_continue)) {
882 return;
883 }
884 cur_depth_++;
885 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700886 } while (cur_shadow_frame_ != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700887 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700888 if (include_transitions) {
889 bool should_continue = VisitFrame();
890 if (!should_continue) {
891 return;
892 }
893 }
Andreas Gampe585da952016-12-02 14:52:29 -0800894 if (kCount == CountTransitions::kYes) {
895 cur_depth_++;
896 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800897 }
898 if (num_frames_ != 0) {
899 CHECK_EQ(cur_depth_, num_frames_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700900 }
901}
902
Andreas Gampe585da952016-12-02 14:52:29 -0800903template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kYes>(bool);
904template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kNo>(bool);
905
Elliott Hughes68e76522011-10-05 13:22:16 -0700906} // namespace art