blob: 3d041d0ee479227cb2d9a92d6e8d792c06ec1802 [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"
David Sehr9e734c72018-01-04 17:56:19 -080026#include "dex/dex_file_types.h"
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010027#include "entrypoints/entrypoint_utils-inl.h"
Vladimir Markofe72cd32018-05-17 08:43:47 +010028#include "entrypoints/quick/callee_save_frame.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070029#include "entrypoints/runtime_asm_entrypoints.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070030#include "gc/space/image_space.h"
31#include "gc/space/space-inl.h"
Vladimir Marko55c12cd2018-05-22 15:33:48 +010032#include "interpreter/shadow_frame-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010033#include "jit/jit.h"
34#include "jit/jit_code_cache.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070035#include "linear_alloc.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070036#include "managed_stack.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070037#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "mirror/object-inl.h"
39#include "mirror/object_array-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010040#include "oat_quick_method_header.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010041#include "quick/quick_method_frame_info.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070042#include "runtime.h"
Dave Allisonf9439142014-03-27 15:10:22 -070043#include "thread.h"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070044#include "thread_list.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070045
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080046namespace art {
47
Andreas Gampe46ee31b2016-12-14 10:11:49 -080048using android::base::StringPrintf;
49
Mathieu Chartier8405bfd2016-02-05 12:00:49 -080050static constexpr bool kDebugStackWalk = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -070051
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -080052StackVisitor::StackVisitor(Thread* thread,
53 Context* context,
54 StackWalkKind walk_kind,
55 bool check_suspended)
56 : StackVisitor(thread, context, walk_kind, 0, check_suspended) {}
Ian Rogers7a22fa62013-01-23 12:16:16 -080057
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010058StackVisitor::StackVisitor(Thread* thread,
59 Context* context,
60 StackWalkKind walk_kind,
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -080061 size_t num_frames,
62 bool check_suspended)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010063 : thread_(thread),
64 walk_kind_(walk_kind),
65 cur_shadow_frame_(nullptr),
66 cur_quick_frame_(nullptr),
67 cur_quick_frame_pc_(0),
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010068 cur_oat_quick_method_header_(nullptr),
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010069 num_frames_(num_frames),
70 cur_depth_(0),
Nicolas Geoffray57f61612015-05-15 13:20:41 +010071 current_inlining_depth_(0),
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -080072 context_(context),
73 check_suspended_(check_suspended) {
74 if (check_suspended_) {
75 DCHECK(thread == Thread::Current() || thread->IsSuspended()) << *thread;
76 }
Ian Rogers5cf98192014-05-29 21:31:50 -070077}
78
Andreas Gampe36a296f2017-06-13 14:11:11 -070079static InlineInfo GetCurrentInlineInfo(const OatQuickMethodHeader* method_header,
80 uintptr_t cur_quick_frame_pc)
81 REQUIRES_SHARED(Locks::mutator_lock_) {
82 uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010083 CodeInfo code_info = method_header->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +000084 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Brazdilf677ebf2015-05-29 16:29:43 +010085 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +010086 DCHECK(stack_map.IsValid());
David Brazdilf677ebf2015-05-29 16:29:43 +010087 return code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +010088}
89
Mathieu Chartiere401d142015-04-22 13:56:20 -070090ArtMethod* StackVisitor::GetMethod() const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +010091 if (cur_shadow_frame_ != nullptr) {
92 return cur_shadow_frame_->GetMethod();
93 } else if (cur_quick_frame_ != nullptr) {
94 if (IsInInlinedFrame()) {
95 size_t depth_in_stack_map = current_inlining_depth_ - 1;
Andreas Gampe36a296f2017-06-13 14:11:11 -070096 InlineInfo inline_info = GetCurrentInlineInfo(GetCurrentOatQuickMethodHeader(),
97 cur_quick_frame_pc_);
David Srbecky61b28a12016-02-25 21:55:03 +000098 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
99 CodeInfoEncoding encoding = method_header->GetOptimizedCodeInfo().ExtractEncoding();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700100 MethodInfo method_info = method_header->GetOptimizedMethodInfo();
Mathieu Chartier45bf2502016-03-31 11:07:09 -0700101 DCHECK(walk_kind_ != StackWalkKind::kSkipInlinedFrames);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100102 return GetResolvedMethod(*GetCurrentQuickFrame(),
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700103 method_info,
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100104 inline_info,
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800105 encoding.inline_info.encoding,
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100106 depth_in_stack_map);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100107 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700108 return *cur_quick_frame_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100109 }
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100110 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700111 return nullptr;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100112}
113
Dave Allisonb373e092014-02-20 16:06:36 -0800114uint32_t StackVisitor::GetDexPc(bool abort_on_failure) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700115 if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700116 return cur_shadow_frame_->GetDexPC();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700117 } else if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100118 if (IsInInlinedFrame()) {
119 size_t depth_in_stack_map = current_inlining_depth_ - 1;
David Srbecky61b28a12016-02-25 21:55:03 +0000120 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
121 CodeInfoEncoding encoding = method_header->GetOptimizedCodeInfo().ExtractEncoding();
Andreas Gampe36a296f2017-06-13 14:11:11 -0700122 return GetCurrentInlineInfo(GetCurrentOatQuickMethodHeader(), cur_quick_frame_pc_).
123 GetDexPcAtDepth(encoding.inline_info.encoding, depth_in_stack_map);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100124 } else if (cur_oat_quick_method_header_ == nullptr) {
Andreas Gampee2abbc62017-09-15 11:59:26 -0700125 return dex::kDexNoIndex;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100126 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100127 return cur_oat_quick_method_header_->ToDexPc(
128 GetMethod(), cur_quick_frame_pc_, abort_on_failure);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100129 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700130 } else {
131 return 0;
132 }
133}
134
Mathieu Chartiere401d142015-04-22 13:56:20 -0700135extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700136 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertza836bc92014-11-25 16:30:53 +0100137
Ian Rogers62d6c772013-02-27 08:32:07 -0800138mirror::Object* StackVisitor::GetThisObject() const {
Andreas Gampe542451c2016-07-26 09:02:02 -0700139 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700140 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800141 if (m->IsStatic()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100142 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800143 } else if (m->IsNative()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100144 if (cur_quick_frame_ != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700145 HandleScope* hs = reinterpret_cast<HandleScope*>(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100146 reinterpret_cast<char*>(cur_quick_frame_) + sizeof(ArtMethod*));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700147 return hs->GetReference(0);
Ian Rogers62d6c772013-02-27 08:32:07 -0800148 } else {
149 return cur_shadow_frame_->GetVRegReference(0);
150 }
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000151 } else if (m->IsProxyMethod()) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100152 if (cur_quick_frame_ != nullptr) {
153 return artQuickGetProxyThisObject(cur_quick_frame_);
154 } else {
155 return cur_shadow_frame_->GetVRegReference(0);
156 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800157 } else {
David Sehr0225f8e2018-01-31 08:52:24 +0000158 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800159 if (!accessor.HasCodeItem()) {
Ian Rogerse0dcd462014-03-08 15:21:04 -0800160 UNIMPLEMENTED(ERROR) << "Failed to determine this object of abstract or proxy method: "
David Sehr709b0702016-10-13 09:12:37 -0700161 << ArtMethod::PrettyMethod(m);
Ian Rogerse0dcd462014-03-08 15:21:04 -0800162 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800163 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800164 uint16_t reg = accessor.RegistersSize() - accessor.InsSize();
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000165 uint32_t value = 0;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700166 bool success = GetVReg(m, reg, kReferenceVReg, &value);
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000167 // We currently always guarantee the `this` object is live throughout the method.
David Sehr709b0702016-10-13 09:12:37 -0700168 CHECK(success) << "Failed to read the this object in " << ArtMethod::PrettyMethod(m);
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000169 return reinterpret_cast<mirror::Object*>(value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800170 }
171 }
172}
173
Ian Rogers0c7abda2012-09-19 13:33:42 -0700174size_t StackVisitor::GetNativePcOffset() const {
175 DCHECK(!IsShadowFrame());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100176 return GetCurrentOatQuickMethodHeader()->NativeQuickPcOffset(cur_quick_frame_pc_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700177}
178
Mingyao Yang99170c62015-07-06 11:10:37 -0700179bool StackVisitor::GetVRegFromDebuggerShadowFrame(uint16_t vreg,
180 VRegKind kind,
181 uint32_t* val) const {
182 size_t frame_id = const_cast<StackVisitor*>(this)->GetFrameId();
183 ShadowFrame* shadow_frame = thread_->FindDebuggerShadowFrame(frame_id);
184 if (shadow_frame != nullptr) {
185 bool* updated_vreg_flags = thread_->GetUpdatedVRegFlags(frame_id);
186 DCHECK(updated_vreg_flags != nullptr);
187 if (updated_vreg_flags[vreg]) {
188 // Value is set by the debugger.
189 if (kind == kReferenceVReg) {
190 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
191 shadow_frame->GetVRegReference(vreg)));
192 } else {
193 *val = shadow_frame->GetVReg(vreg);
194 }
195 return true;
196 }
197 }
198 // No value is set by the debugger.
199 return false;
200}
201
Mathieu Chartiere401d142015-04-22 13:56:20 -0700202bool StackVisitor::GetVReg(ArtMethod* m, uint16_t vreg, VRegKind kind, uint32_t* val) const {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200203 if (cur_quick_frame_ != nullptr) {
204 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800205 DCHECK(m == GetMethod());
Mingyao Yang99170c62015-07-06 11:10:37 -0700206 // Check if there is value set by the debugger.
207 if (GetVRegFromDebuggerShadowFrame(vreg, kind, val)) {
208 return true;
209 }
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100210 DCHECK(cur_oat_quick_method_header_->IsOptimized());
211 return GetVRegFromOptimizedCode(m, vreg, kind, val);
Ian Rogers0399dde2012-06-06 17:09:28 -0700212 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100213 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertz09687442015-11-17 10:35:39 +0100214 if (kind == kReferenceVReg) {
215 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
216 cur_shadow_frame_->GetVRegReference(vreg)));
217 } else {
218 *val = cur_shadow_frame_->GetVReg(vreg);
219 }
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200220 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700221 }
222}
223
Mathieu Chartiere401d142015-04-22 13:56:20 -0700224bool StackVisitor::GetVRegFromOptimizedCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100225 uint32_t* val) const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100226 DCHECK_EQ(m, GetMethod());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800227 // Can't be null or how would we compile its instructions?
228 DCHECK(m->GetCodeItem() != nullptr) << m->PrettyMethod();
David Sehr0225f8e2018-01-31 08:52:24 +0000229 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800230 uint16_t number_of_dex_registers = accessor.RegistersSize();
231 DCHECK_LT(vreg, number_of_dex_registers);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100232 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
233 CodeInfo code_info = method_header->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000234 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100235
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100236 uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100237 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100238 DCHECK(stack_map.IsValid());
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100239 size_t depth_in_stack_map = current_inlining_depth_ - 1;
240
241 DexRegisterMap dex_register_map = IsInInlinedFrame()
David Brazdilf677ebf2015-05-29 16:29:43 +0100242 ? code_info.GetDexRegisterMapAtDepth(depth_in_stack_map,
243 code_info.GetInlineInfoOf(stack_map, encoding),
244 encoding,
245 number_of_dex_registers)
246 : code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100247
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000248 if (!dex_register_map.IsValid()) {
249 return false;
250 }
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000251 DexRegisterLocation::Kind location_kind =
David Brazdilf677ebf2015-05-29 16:29:43 +0100252 dex_register_map.GetLocationKind(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100253 switch (location_kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000254 case DexRegisterLocation::Kind::kInStack: {
David Brazdilf677ebf2015-05-29 16:29:43 +0100255 const int32_t offset = dex_register_map.GetStackOffsetInBytes(vreg,
256 number_of_dex_registers,
257 code_info,
258 encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100259 const uint8_t* addr = reinterpret_cast<const uint8_t*>(cur_quick_frame_) + offset;
260 *val = *reinterpret_cast<const uint32_t*>(addr);
261 return true;
262 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000263 case DexRegisterLocation::Kind::kInRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100264 case DexRegisterLocation::Kind::kInRegisterHigh:
265 case DexRegisterLocation::Kind::kInFpuRegister:
266 case DexRegisterLocation::Kind::kInFpuRegisterHigh: {
David Brazdilf677ebf2015-05-29 16:29:43 +0100267 uint32_t reg =
268 dex_register_map.GetMachineRegister(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100269 return GetRegisterIfAccessible(reg, kind, val);
270 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000271 case DexRegisterLocation::Kind::kConstant:
David Brazdilf677ebf2015-05-29 16:29:43 +0100272 *val = dex_register_map.GetConstant(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100273 return true;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000274 case DexRegisterLocation::Kind::kNone:
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100275 return false;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000276 default:
277 LOG(FATAL)
David Srbecky7dc11782016-02-25 13:23:56 +0000278 << "Unexpected location kind "
279 << dex_register_map.GetLocationInternalKind(vreg,
280 number_of_dex_registers,
281 code_info,
282 encoding);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000283 UNREACHABLE();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100284 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100285}
286
287bool StackVisitor::GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const {
288 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
David Brazdil77a48ae2015-09-15 12:34:04 +0000289
Vladimir Marko239d6ea2016-09-05 10:44:04 +0100290 if (kRuntimeISA == InstructionSet::kX86 && is_float) {
291 // X86 float registers are 64-bit and each XMM register is provided as two separate
292 // 32-bit registers by the context.
293 reg = (kind == kDoubleHiVReg) ? (2 * reg + 1) : (2 * reg);
294 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000295
Goran Jakovljevic986660c2015-12-10 11:44:50 +0100296 // MIPS32 float registers are used as 64-bit (for MIPS32r2 it is pair
297 // F(2n)-F(2n+1), and for MIPS32r6 it is 64-bit register F(2n)). When
298 // accessing upper 32-bits from double, reg + 1 should be used.
299 if ((kRuntimeISA == InstructionSet::kMips) && (kind == kDoubleHiVReg)) {
300 DCHECK_ALIGNED(reg, 2);
301 reg++;
302 }
303
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100304 if (!IsAccessibleRegister(reg, is_float)) {
305 return false;
306 }
307 uintptr_t ptr_val = GetRegister(reg, is_float);
308 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
309 if (target64) {
310 const bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
311 const bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
312 int64_t value_long = static_cast<int64_t>(ptr_val);
313 if (wide_lo) {
314 ptr_val = static_cast<uintptr_t>(Low32Bits(value_long));
315 } else if (wide_hi) {
316 ptr_val = static_cast<uintptr_t>(High32Bits(value_long));
317 }
318 }
319 *val = ptr_val;
320 return true;
321}
322
Mingyao Yang99170c62015-07-06 11:10:37 -0700323bool StackVisitor::GetVRegPairFromDebuggerShadowFrame(uint16_t vreg,
324 VRegKind kind_lo,
325 VRegKind kind_hi,
326 uint64_t* val) const {
327 uint32_t low_32bits;
328 uint32_t high_32bits;
329 bool success = GetVRegFromDebuggerShadowFrame(vreg, kind_lo, &low_32bits);
330 success &= GetVRegFromDebuggerShadowFrame(vreg + 1, kind_hi, &high_32bits);
331 if (success) {
332 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
333 }
334 return success;
335}
336
Mathieu Chartiere401d142015-04-22 13:56:20 -0700337bool StackVisitor::GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200338 VRegKind kind_hi, uint64_t* val) const {
339 if (kind_lo == kLongLoVReg) {
340 DCHECK_EQ(kind_hi, kLongHiVReg);
341 } else if (kind_lo == kDoubleLoVReg) {
342 DCHECK_EQ(kind_hi, kDoubleHiVReg);
343 } else {
344 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100345 UNREACHABLE();
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200346 }
Mingyao Yang99170c62015-07-06 11:10:37 -0700347 // Check if there is value set by the debugger.
348 if (GetVRegPairFromDebuggerShadowFrame(vreg, kind_lo, kind_hi, val)) {
349 return true;
350 }
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200351 if (cur_quick_frame_ != nullptr) {
352 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
353 DCHECK(m == GetMethod());
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100354 DCHECK(cur_oat_quick_method_header_->IsOptimized());
355 return GetVRegPairFromOptimizedCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200356 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100357 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200358 *val = cur_shadow_frame_->GetVRegLong(vreg);
359 return true;
360 }
361}
362
Mathieu Chartiere401d142015-04-22 13:56:20 -0700363bool StackVisitor::GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100364 VRegKind kind_lo, VRegKind kind_hi,
365 uint64_t* val) const {
366 uint32_t low_32bits;
367 uint32_t high_32bits;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700368 bool success = GetVRegFromOptimizedCode(m, vreg, kind_lo, &low_32bits);
369 success &= GetVRegFromOptimizedCode(m, vreg + 1, kind_hi, &high_32bits);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100370 if (success) {
371 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
372 }
373 return success;
374}
375
376bool StackVisitor::GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
377 VRegKind kind_lo, uint64_t* val) const {
378 const bool is_float = (kind_lo == kDoubleLoVReg);
379 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
380 return false;
381 }
382 uintptr_t ptr_val_lo = GetRegister(reg_lo, is_float);
383 uintptr_t ptr_val_hi = GetRegister(reg_hi, is_float);
384 bool target64 = Is64BitInstructionSet(kRuntimeISA);
385 if (target64) {
386 int64_t value_long_lo = static_cast<int64_t>(ptr_val_lo);
387 int64_t value_long_hi = static_cast<int64_t>(ptr_val_hi);
388 ptr_val_lo = static_cast<uintptr_t>(Low32Bits(value_long_lo));
389 ptr_val_hi = static_cast<uintptr_t>(High32Bits(value_long_hi));
390 }
391 *val = (static_cast<uint64_t>(ptr_val_hi) << 32) | static_cast<uint32_t>(ptr_val_lo);
392 return true;
393}
394
Mingyao Yang636b9252015-07-31 16:40:24 -0700395bool StackVisitor::SetVReg(ArtMethod* m,
396 uint16_t vreg,
397 uint32_t new_value,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800398 VRegKind kind) {
David Sehr0225f8e2018-01-31 08:52:24 +0000399 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800400 if (!accessor.HasCodeItem()) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700401 return false;
402 }
403 ShadowFrame* shadow_frame = GetCurrentShadowFrame();
404 if (shadow_frame == nullptr) {
405 // This is a compiled frame: we must prepare and update a shadow frame that will
406 // be executed by the interpreter after deoptimization of the stack.
407 const size_t frame_id = GetFrameId();
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800408 const uint16_t num_regs = accessor.RegistersSize();
Mingyao Yang99170c62015-07-06 11:10:37 -0700409 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
410 CHECK(shadow_frame != nullptr);
411 // Remember the vreg has been set for debugging and must not be overwritten by the
412 // original value during deoptimization of the stack.
413 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
414 }
415 if (kind == kReferenceVReg) {
416 shadow_frame->SetVRegReference(vreg, reinterpret_cast<mirror::Object*>(new_value));
417 } else {
418 shadow_frame->SetVReg(vreg, new_value);
419 }
420 return true;
421}
422
Mingyao Yang636b9252015-07-31 16:40:24 -0700423bool StackVisitor::SetVRegPair(ArtMethod* m,
424 uint16_t vreg,
425 uint64_t new_value,
426 VRegKind kind_lo,
427 VRegKind kind_hi) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700428 if (kind_lo == kLongLoVReg) {
429 DCHECK_EQ(kind_hi, kLongHiVReg);
430 } else if (kind_lo == kDoubleLoVReg) {
431 DCHECK_EQ(kind_hi, kDoubleHiVReg);
432 } else {
433 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
434 UNREACHABLE();
435 }
David Sehr0225f8e2018-01-31 08:52:24 +0000436 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800437 if (!accessor.HasCodeItem()) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700438 return false;
439 }
440 ShadowFrame* shadow_frame = GetCurrentShadowFrame();
441 if (shadow_frame == nullptr) {
442 // This is a compiled frame: we must prepare for deoptimization (see SetVRegFromDebugger).
443 const size_t frame_id = GetFrameId();
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800444 const uint16_t num_regs = accessor.RegistersSize();
Mingyao Yang99170c62015-07-06 11:10:37 -0700445 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
446 CHECK(shadow_frame != nullptr);
447 // Remember the vreg pair has been set for debugging and must not be overwritten by the
448 // original value during deoptimization of the stack.
449 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
450 thread_->GetUpdatedVRegFlags(frame_id)[vreg + 1] = true;
451 }
452 shadow_frame->SetVRegLong(vreg, new_value);
453 return true;
454}
455
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100456bool StackVisitor::IsAccessibleGPR(uint32_t reg) const {
457 DCHECK(context_ != nullptr);
458 return context_->IsAccessibleGPR(reg);
459}
460
Mathieu Chartier815873e2014-02-13 18:02:13 -0800461uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100462 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
463 DCHECK(context_ != nullptr);
Mathieu Chartier815873e2014-02-13 18:02:13 -0800464 return context_->GetGPRAddress(reg);
465}
466
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100467uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
468 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
469 DCHECK(context_ != nullptr);
470 return context_->GetGPR(reg);
Ian Rogers0399dde2012-06-06 17:09:28 -0700471}
472
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100473bool StackVisitor::IsAccessibleFPR(uint32_t reg) const {
474 DCHECK(context_ != nullptr);
475 return context_->IsAccessibleFPR(reg);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200476}
477
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100478uintptr_t StackVisitor::GetFPR(uint32_t reg) const {
479 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
480 DCHECK(context_ != nullptr);
481 return context_->GetFPR(reg);
482}
483
Ian Rogers0399dde2012-06-06 17:09:28 -0700484uintptr_t StackVisitor::GetReturnPc() const {
Ian Rogers13735952014-10-08 12:43:28 -0700485 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700486 DCHECK(sp != nullptr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100487 uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogers0399dde2012-06-06 17:09:28 -0700488 return *reinterpret_cast<uintptr_t*>(pc_addr);
489}
490
491void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
Ian Rogers13735952014-10-08 12:43:28 -0700492 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700493 CHECK(sp != nullptr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100494 uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogers0399dde2012-06-06 17:09:28 -0700495 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc;
496}
497
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100498size_t StackVisitor::ComputeNumFrames(Thread* thread, StackWalkKind walk_kind) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700499 struct NumFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100500 NumFramesVisitor(Thread* thread_in, StackWalkKind walk_kind_in)
501 : StackVisitor(thread_in, nullptr, walk_kind_in), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700502
Ian Rogers5cf98192014-05-29 21:31:50 -0700503 bool VisitFrame() OVERRIDE {
Ian Rogers0399dde2012-06-06 17:09:28 -0700504 frames++;
505 return true;
506 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700507
Ian Rogers0399dde2012-06-06 17:09:28 -0700508 size_t frames;
509 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100510 NumFramesVisitor visitor(thread, walk_kind);
Ian Rogers0399dde2012-06-06 17:09:28 -0700511 visitor.WalkStack(true);
512 return visitor.frames;
513}
514
Mathieu Chartiere401d142015-04-22 13:56:20 -0700515bool StackVisitor::GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700516 struct HasMoreFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100517 HasMoreFramesVisitor(Thread* thread,
518 StackWalkKind walk_kind,
519 size_t num_frames,
520 size_t frame_height)
521 : StackVisitor(thread, nullptr, walk_kind, num_frames),
522 frame_height_(frame_height),
523 found_frame_(false),
524 has_more_frames_(false),
525 next_method_(nullptr),
526 next_dex_pc_(0) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700527 }
528
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700529 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700530 if (found_frame_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700531 ArtMethod* method = GetMethod();
Ian Rogers5cf98192014-05-29 21:31:50 -0700532 if (method != nullptr && !method->IsRuntimeMethod()) {
533 has_more_frames_ = true;
534 next_method_ = method;
535 next_dex_pc_ = GetDexPc();
536 return false; // End stack walk once next method is found.
537 }
538 } else if (GetFrameHeight() == frame_height_) {
539 found_frame_ = true;
540 }
541 return true;
542 }
543
544 size_t frame_height_;
545 bool found_frame_;
546 bool has_more_frames_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700547 ArtMethod* next_method_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700548 uint32_t next_dex_pc_;
549 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100550 HasMoreFramesVisitor visitor(thread_, walk_kind_, GetNumFrames(), GetFrameHeight());
Ian Rogers5cf98192014-05-29 21:31:50 -0700551 visitor.WalkStack(true);
552 *next_method = visitor.next_method_;
553 *next_dex_pc = visitor.next_dex_pc_;
554 return visitor.has_more_frames_;
555}
556
Ian Rogers7a22fa62013-01-23 12:16:16 -0800557void StackVisitor::DescribeStack(Thread* thread) {
Ian Rogers306057f2012-11-26 12:45:53 -0800558 struct DescribeStackVisitor : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800559 explicit DescribeStackVisitor(Thread* thread_in)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100560 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800561
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700562 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers306057f2012-11-26 12:45:53 -0800563 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation();
564 return true;
565 }
566 };
Ian Rogers7a22fa62013-01-23 12:16:16 -0800567 DescribeStackVisitor visitor(thread);
Ian Rogers306057f2012-11-26 12:45:53 -0800568 visitor.WalkStack(true);
569}
570
Ian Rogers40e3bac2012-11-20 00:09:14 -0800571std::string StackVisitor::DescribeLocation() const {
572 std::string result("Visiting method '");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700573 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700574 if (m == nullptr) {
Ian Rogers306057f2012-11-26 12:45:53 -0800575 return "upcall";
576 }
David Sehr709b0702016-10-13 09:12:37 -0700577 result += m->PrettyMethod();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800578 result += StringPrintf("' at dex PC 0x%04x", GetDexPc());
Ian Rogers40e3bac2012-11-20 00:09:14 -0800579 if (!IsShadowFrame()) {
580 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc()));
581 }
582 return result;
583}
584
Alex Lightdba61482016-12-21 08:20:29 -0800585void StackVisitor::SetMethod(ArtMethod* method) {
586 DCHECK(GetMethod() != nullptr);
587 if (cur_shadow_frame_ != nullptr) {
588 cur_shadow_frame_->SetMethod(method);
589 } else {
590 DCHECK(cur_quick_frame_ != nullptr);
591 CHECK(!IsInInlinedFrame()) << "We do not support setting inlined method's ArtMethod!";
Alex Light1ebe4fe2017-01-30 14:57:11 -0800592 *cur_quick_frame_ = method;
Alex Lightdba61482016-12-21 08:20:29 -0800593 }
594}
595
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100596static void AssertPcIsWithinQuickCode(ArtMethod* method, uintptr_t pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700597 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100598 if (method->IsNative() || method->IsRuntimeMethod() || method->IsProxyMethod()) {
599 return;
600 }
601
602 if (pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) {
603 return;
604 }
605
Mingyao Yang88ca8ba2017-05-23 14:21:07 -0700606 Runtime* runtime = Runtime::Current();
607 if (runtime->UseJitCompilation() &&
608 runtime->GetJit()->GetCodeCache()->ContainsPc(reinterpret_cast<const void*>(pc))) {
609 return;
610 }
611
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100612 const void* code = method->GetEntryPointFromQuickCompiledCode();
Alex Lightdb01a092017-04-03 15:39:55 -0700613 if (code == GetQuickInstrumentationEntryPoint() || code == GetInvokeObsoleteMethodStub()) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100614 return;
615 }
616
617 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
618 if (class_linker->IsQuickToInterpreterBridge(code) ||
619 class_linker->IsQuickResolutionStub(code)) {
620 return;
621 }
622
Calin Juravleffc87072016-04-20 14:22:09 +0100623 if (runtime->UseJitCompilation() && runtime->GetJit()->GetCodeCache()->ContainsPc(code)) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100624 return;
625 }
626
Mingyao Yang063fc772016-08-02 11:02:54 -0700627 uint32_t code_size = OatQuickMethodHeader::FromEntryPoint(code)->GetCodeSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100628 uintptr_t code_start = reinterpret_cast<uintptr_t>(code);
629 CHECK(code_start <= pc && pc <= (code_start + code_size))
David Sehr709b0702016-10-13 09:12:37 -0700630 << method->PrettyMethod()
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100631 << " pc=" << std::hex << pc
Roland Levillain0d5a2812015-11-13 10:07:31 +0000632 << " code_start=" << code_start
633 << " code_size=" << code_size;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100634}
635
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700636void StackVisitor::SanityCheckFrame() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800637 if (kIsDebugBuild) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700638 ArtMethod* method = GetMethod();
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100639 mirror::Class* declaring_class = method->GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700640 // Runtime methods have null declaring class.
641 if (!method->IsRuntimeMethod()) {
642 CHECK(declaring_class != nullptr);
643 CHECK_EQ(declaring_class->GetClass(), declaring_class->GetClass()->GetClass())
644 << declaring_class;
645 } else {
646 CHECK(declaring_class == nullptr);
647 }
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700648 Runtime* const runtime = Runtime::Current();
649 LinearAlloc* const linear_alloc = runtime->GetLinearAlloc();
650 if (!linear_alloc->Contains(method)) {
651 // Check class linker linear allocs.
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100652 // We get the canonical method as copied methods may have their declaring
653 // class from another class loader.
654 ArtMethod* canonical = method->GetCanonicalMethod();
655 mirror::Class* klass = canonical->GetDeclaringClass();
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700656 LinearAlloc* const class_linear_alloc = (klass != nullptr)
Mathieu Chartier5b830502016-03-02 10:30:23 -0800657 ? runtime->GetClassLinker()->GetAllocatorForClassLoader(klass->GetClassLoader())
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700658 : linear_alloc;
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100659 if (!class_linear_alloc->Contains(canonical)) {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700660 // Check image space.
661 bool in_image = false;
662 for (auto& space : runtime->GetHeap()->GetContinuousSpaces()) {
663 if (space->IsImageSpace()) {
664 auto* image_space = space->AsImageSpace();
665 const auto& header = image_space->GetImageHeader();
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700666 const ImageSection& methods = header.GetMethodsSection();
667 const ImageSection& runtime_methods = header.GetRuntimeMethodsSection();
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100668 const size_t offset = reinterpret_cast<const uint8_t*>(canonical) - image_space->Begin();
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700669 if (methods.Contains(offset) || runtime_methods.Contains(offset)) {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700670 in_image = true;
671 break;
672 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700673 }
674 }
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100675 CHECK(in_image) << canonical->PrettyMethod() << " not in linear alloc or image";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700676 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700677 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800678 if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100679 AssertPcIsWithinQuickCode(method, cur_quick_frame_pc_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800680 // Frame sanity.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100681 size_t frame_size = GetCurrentQuickFrameInfo().FrameSizeInBytes();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800682 CHECK_NE(frame_size, 0u);
Andreas Gampe5b417b92014-03-10 14:18:35 -0700683 // A rough guess at an upper size we expect to see for a frame.
684 // 256 registers
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700685 // 2 words HandleScope overhead
Andreas Gampe5b417b92014-03-10 14:18:35 -0700686 // 3+3 register spills
687 // TODO: this seems architecture specific for the case of JNI frames.
Brian Carlstromed08bd42014-03-19 18:34:17 -0700688 // TODO: 083-compiler-regressions ManyFloatArgs shows this estimate is wrong.
689 // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word);
690 const size_t kMaxExpectedFrameSize = 2 * KB;
David Sehr709b0702016-10-13 09:12:37 -0700691 CHECK_LE(frame_size, kMaxExpectedFrameSize) << method->PrettyMethod();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100692 size_t return_pc_offset = GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800693 CHECK_LT(return_pc_offset, frame_size);
694 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700695 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700696}
697
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100698// Counts the number of references in the parameter list of the corresponding method.
699// Note: Thus does _not_ include "this" for non-static methods.
700static uint32_t GetNumberOfReferenceArgsWithoutReceiver(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700701 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100702 uint32_t shorty_len;
703 const char* shorty = method->GetShorty(&shorty_len);
704 uint32_t refs = 0;
705 for (uint32_t i = 1; i < shorty_len ; ++i) {
706 if (shorty[i] == 'L') {
707 refs++;
708 }
709 }
710 return refs;
711}
712
713QuickMethodFrameInfo StackVisitor::GetCurrentQuickFrameInfo() const {
714 if (cur_oat_quick_method_header_ != nullptr) {
715 return cur_oat_quick_method_header_->GetFrameInfo();
716 }
717
718 ArtMethod* method = GetMethod();
719 Runtime* runtime = Runtime::Current();
720
721 if (method->IsAbstract()) {
Vladimir Markofe72cd32018-05-17 08:43:47 +0100722 return RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100723 }
724
725 // This goes before IsProxyMethod since runtime methods have a null declaring class.
726 if (method->IsRuntimeMethod()) {
727 return runtime->GetRuntimeMethodFrameInfo(method);
728 }
729
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100730 if (method->IsProxyMethod()) {
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000731 // There is only one direct method of a proxy class: the constructor. A direct method is
732 // cloned from the original java.lang.reflect.Proxy and is executed as usual quick
733 // compiled method without any stubs. Therefore the method must have a OatQuickMethodHeader.
734 DCHECK(!method->IsDirect() && !method->IsConstructor())
735 << "Constructors of proxy classes must have a OatQuickMethodHeader";
Vladimir Markofe72cd32018-05-17 08:43:47 +0100736 return RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100737 }
738
Vladimir Marko2196c652017-11-30 16:16:07 +0000739 // The only remaining case is if the method is native and uses the generic JNI stub,
740 // called either directly or through some (resolution, instrumentation) trampoline.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100741 DCHECK(method->IsNative());
Vladimir Marko2196c652017-11-30 16:16:07 +0000742 if (kIsDebugBuild) {
743 ClassLinker* class_linker = runtime->GetClassLinker();
744 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(method,
745 kRuntimePointerSize);
746 CHECK(class_linker->IsQuickGenericJniStub(entry_point) ||
747 // The current entrypoint (after filtering out trampolines) may have changed
748 // from GenericJNI to JIT-compiled stub since we have entered this frame.
749 (runtime->GetJit() != nullptr &&
750 runtime->GetJit()->GetCodeCache()->ContainsPc(entry_point))) << method->PrettyMethod();
751 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100752 // Generic JNI frame.
753 uint32_t handle_refs = GetNumberOfReferenceArgsWithoutReceiver(method) + 1;
754 size_t scope_size = HandleScope::SizeOf(handle_refs);
Vladimir Markofe72cd32018-05-17 08:43:47 +0100755 constexpr QuickMethodFrameInfo callee_info =
756 RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100757
758 // Callee saves + handle scope + method ref + alignment
759 // Note: -sizeof(void*) since callee-save frame stores a whole method pointer.
760 size_t frame_size = RoundUp(
761 callee_info.FrameSizeInBytes() - sizeof(void*) + sizeof(ArtMethod*) + scope_size,
762 kStackAlignment);
763 return QuickMethodFrameInfo(frame_size, callee_info.CoreSpillMask(), callee_info.FpSpillMask());
764}
765
Andreas Gampe585da952016-12-02 14:52:29 -0800766template <StackVisitor::CountTransitions kCount>
Ian Rogers0399dde2012-06-06 17:09:28 -0700767void StackVisitor::WalkStack(bool include_transitions) {
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -0800768 if (check_suspended_) {
769 DCHECK(thread_ == Thread::Current() || thread_->IsSuspended());
770 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800771 CHECK_EQ(cur_depth_, 0U);
772 bool exit_stubs_installed = Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled();
Alex Lightb81a9842016-12-15 00:59:05 +0000773 uint32_t instrumentation_stack_depth = 0;
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000774 size_t inlined_frames_count = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700775
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700776 for (const ManagedStack* current_fragment = thread_->GetManagedStack();
777 current_fragment != nullptr; current_fragment = current_fragment->GetLink()) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700778 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
779 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700780 cur_quick_frame_pc_ = 0;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100781 cur_oat_quick_method_header_ = nullptr;
Dave Allisonf9439142014-03-27 15:10:22 -0700782
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700783 if (cur_quick_frame_ != nullptr) { // Handle quick stack frames.
Ian Rogers0399dde2012-06-06 17:09:28 -0700784 // Can't be both a shadow and a quick fragment.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700785 DCHECK(current_fragment->GetTopShadowFrame() == nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700786 ArtMethod* method = *cur_quick_frame_;
Vladimir Marko2196c652017-11-30 16:16:07 +0000787 DCHECK(method != nullptr);
788 bool header_retrieved = false;
789 if (method->IsNative()) {
790 // We do not have a PC for the first frame, so we cannot simply use
791 // ArtMethod::GetOatQuickMethodHeader() as we're unable to distinguish there
792 // between GenericJNI frame and JIT-compiled JNI stub; the entrypoint may have
793 // changed since the frame was entered. The top quick frame tag indicates
794 // GenericJNI here, otherwise it's either AOT-compiled or JNI-compiled JNI stub.
795 if (UNLIKELY(current_fragment->GetTopQuickFrameTag())) {
796 // The generic JNI does not have any method header.
797 cur_oat_quick_method_header_ = nullptr;
798 } else {
799 const void* existing_entry_point = method->GetEntryPointFromQuickCompiledCode();
800 CHECK(existing_entry_point != nullptr);
801 Runtime* runtime = Runtime::Current();
802 ClassLinker* class_linker = runtime->GetClassLinker();
803 // Check whether we can quickly get the header from the current entrypoint.
804 if (!class_linker->IsQuickGenericJniStub(existing_entry_point) &&
805 !class_linker->IsQuickResolutionStub(existing_entry_point) &&
806 existing_entry_point != GetQuickInstrumentationEntryPoint()) {
807 cur_oat_quick_method_header_ =
808 OatQuickMethodHeader::FromEntryPoint(existing_entry_point);
809 } else {
810 const void* code = method->GetOatMethodQuickCode(class_linker->GetImagePointerSize());
811 if (code != nullptr) {
812 cur_oat_quick_method_header_ = OatQuickMethodHeader::FromEntryPoint(code);
813 } else {
814 // This must be a JITted JNI stub frame.
815 CHECK(runtime->GetJit() != nullptr);
816 code = runtime->GetJit()->GetCodeCache()->GetJniStubCode(method);
817 CHECK(code != nullptr) << method->PrettyMethod();
818 cur_oat_quick_method_header_ = OatQuickMethodHeader::FromCodePointer(code);
819 }
820 }
821 }
822 header_retrieved = true;
823 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700824 while (method != nullptr) {
Vladimir Marko2196c652017-11-30 16:16:07 +0000825 if (!header_retrieved) {
826 cur_oat_quick_method_header_ = method->GetOatQuickMethodHeader(cur_quick_frame_pc_);
827 }
828 header_retrieved = false; // Force header retrieval in next iteration.
Dave Allison5cd33752014-04-15 15:57:58 -0700829 SanityCheckFrame();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100830
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100831 if ((walk_kind_ == StackWalkKind::kIncludeInlinedFrames)
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100832 && (cur_oat_quick_method_header_ != nullptr)
833 && cur_oat_quick_method_header_->IsOptimized()) {
834 CodeInfo code_info = cur_oat_quick_method_header_->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000835 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100836 uint32_t native_pc_offset =
837 cur_oat_quick_method_header_->NativeQuickPcOffset(cur_quick_frame_pc_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100838 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800839 if (stack_map.IsValid() && stack_map.HasInlineInfo(encoding.stack_map.encoding)) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100840 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100841 DCHECK_EQ(current_inlining_depth_, 0u);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800842 for (current_inlining_depth_ = inline_info.GetDepth(encoding.inline_info.encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100843 current_inlining_depth_ != 0;
844 --current_inlining_depth_) {
845 bool should_continue = VisitFrame();
846 if (UNLIKELY(!should_continue)) {
847 return;
848 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100849 cur_depth_++;
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000850 inlined_frames_count++;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100851 }
852 }
853 }
854
Dave Allison5cd33752014-04-15 15:57:58 -0700855 bool should_continue = VisitFrame();
856 if (UNLIKELY(!should_continue)) {
857 return;
Ian Rogers0399dde2012-06-06 17:09:28 -0700858 }
Dave Allison5cd33752014-04-15 15:57:58 -0700859
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100860 QuickMethodFrameInfo frame_info = GetCurrentQuickFrameInfo();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700861 if (context_ != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100862 context_->FillCalleeSaves(reinterpret_cast<uint8_t*>(cur_quick_frame_), frame_info);
Ian Rogers0399dde2012-06-06 17:09:28 -0700863 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700864 // Compute PC for next stack frame from return PC.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100865 size_t frame_size = frame_info.FrameSizeInBytes();
866 size_t return_pc_offset = frame_size - sizeof(void*);
Ian Rogers13735952014-10-08 12:43:28 -0700867 uint8_t* return_pc_addr = reinterpret_cast<uint8_t*>(cur_quick_frame_) + return_pc_offset;
Ian Rogers0399dde2012-06-06 17:09:28 -0700868 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100869
Ian Rogers62d6c772013-02-27 08:32:07 -0800870 if (UNLIKELY(exit_stubs_installed)) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700871 // While profiling, the return pc is restored from the side stack, except when walking
872 // the stack for an exception where the side stack will be unwound in VisitFrame.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700873 if (reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc) {
Andreas Gampe585da952016-12-02 14:52:29 -0800874 CHECK_LT(instrumentation_stack_depth, thread_->GetInstrumentationStack()->size());
Sebastien Hertz74e256b2013-10-04 10:40:37 +0200875 const instrumentation::InstrumentationStackFrame& instrumentation_frame =
Alex Lightb81a9842016-12-15 00:59:05 +0000876 thread_->GetInstrumentationStack()->at(instrumentation_stack_depth);
jeffhao725a9572012-11-13 18:20:12 -0800877 instrumentation_stack_depth++;
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100878 if (GetMethod() ==
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700879 Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveAllCalleeSaves)) {
Jeff Haofb2802d2013-07-24 13:53:05 -0700880 // Skip runtime save all callee frames which are used to deliver exceptions.
881 } else if (instrumentation_frame.interpreter_entry_) {
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100882 ArtMethod* callee =
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700883 Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs);
David Sehr709b0702016-10-13 09:12:37 -0700884 CHECK_EQ(GetMethod(), callee) << "Expected: " << ArtMethod::PrettyMethod(callee)
885 << " Found: " << ArtMethod::PrettyMethod(GetMethod());
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000886 } else {
Alex Lighteee0bd42017-02-14 15:31:45 +0000887 // Instrumentation generally doesn't distinguish between a method's obsolete and
888 // non-obsolete version.
889 CHECK_EQ(instrumentation_frame.method_->GetNonObsoleteMethod(),
890 GetMethod()->GetNonObsoleteMethod())
891 << "Expected: "
892 << ArtMethod::PrettyMethod(instrumentation_frame.method_->GetNonObsoleteMethod())
893 << " Found: " << ArtMethod::PrettyMethod(GetMethod()->GetNonObsoleteMethod());
Ian Rogers62d6c772013-02-27 08:32:07 -0800894 }
895 if (num_frames_ != 0) {
896 // Check agreement of frame Ids only if num_frames_ is computed to avoid infinite
897 // recursion.
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000898 size_t frame_id = instrumentation::Instrumentation::ComputeFrameId(
899 thread_,
900 cur_depth_,
901 inlined_frames_count);
902 CHECK_EQ(instrumentation_frame.frame_id_, frame_id);
Ian Rogers62d6c772013-02-27 08:32:07 -0800903 }
jeffhao725a9572012-11-13 18:20:12 -0800904 return_pc = instrumentation_frame.return_pc_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700905 }
906 }
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100907
Ian Rogers0399dde2012-06-06 17:09:28 -0700908 cur_quick_frame_pc_ = return_pc;
Ian Rogers13735952014-10-08 12:43:28 -0700909 uint8_t* next_frame = reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700910 cur_quick_frame_ = reinterpret_cast<ArtMethod**>(next_frame);
911
912 if (kDebugStackWalk) {
David Sehr709b0702016-10-13 09:12:37 -0700913 LOG(INFO) << ArtMethod::PrettyMethod(method) << "@" << method << " size=" << frame_size
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100914 << std::boolalpha
915 << " optimized=" << (cur_oat_quick_method_header_ != nullptr &&
916 cur_oat_quick_method_header_->IsOptimized())
Mathieu Chartiere401d142015-04-22 13:56:20 -0700917 << " native=" << method->IsNative()
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100918 << std::noboolalpha
Mathieu Chartiere401d142015-04-22 13:56:20 -0700919 << " entrypoints=" << method->GetEntryPointFromQuickCompiledCode()
Alex Lighteee0bd42017-02-14 15:31:45 +0000920 << "," << (method->IsNative() ? method->GetEntryPointFromJni() : nullptr)
Mathieu Chartiere401d142015-04-22 13:56:20 -0700921 << " next=" << *cur_quick_frame_;
922 }
923
Andreas Gampef040be62017-04-14 21:49:33 -0700924 if (kCount == CountTransitions::kYes || !method->IsRuntimeMethod()) {
925 cur_depth_++;
926 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700927 method = *cur_quick_frame_;
jeffhao6641ea12013-01-02 18:13:42 -0800928 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700929 } else if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700930 do {
931 SanityCheckFrame();
932 bool should_continue = VisitFrame();
933 if (UNLIKELY(!should_continue)) {
934 return;
935 }
936 cur_depth_++;
937 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700938 } while (cur_shadow_frame_ != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700939 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700940 if (include_transitions) {
941 bool should_continue = VisitFrame();
942 if (!should_continue) {
943 return;
944 }
945 }
Andreas Gampe585da952016-12-02 14:52:29 -0800946 if (kCount == CountTransitions::kYes) {
947 cur_depth_++;
948 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800949 }
950 if (num_frames_ != 0) {
951 CHECK_EQ(cur_depth_, num_frames_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700952 }
953}
954
Andreas Gampe585da952016-12-02 14:52:29 -0800955template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kYes>(bool);
956template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kNo>(bool);
957
Elliott Hughes68e76522011-10-05 13:22:16 -0700958} // namespace art