blob: 2ea39491c21a87695f1f4afd6b833bc627663040 [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 Markod3083dd2018-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 Marko6ec2a1b2018-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 Marko439d1262019-04-12 14:45:07 +010041#include "obj_ptr-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010042#include "quick/quick_method_frame_info.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070043#include "runtime.h"
Dave Allisonf9439142014-03-27 15:10:22 -070044#include "thread.h"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070045#include "thread_list.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070046
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080047namespace art {
48
Andreas Gampe46ee31b2016-12-14 10:11:49 -080049using android::base::StringPrintf;
50
Mathieu Chartier8405bfd2016-02-05 12:00:49 -080051static constexpr bool kDebugStackWalk = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -070052
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -080053StackVisitor::StackVisitor(Thread* thread,
54 Context* context,
55 StackWalkKind walk_kind,
56 bool check_suspended)
57 : StackVisitor(thread, context, walk_kind, 0, check_suspended) {}
Ian Rogers7a22fa62013-01-23 12:16:16 -080058
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010059StackVisitor::StackVisitor(Thread* thread,
60 Context* context,
61 StackWalkKind walk_kind,
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -080062 size_t num_frames,
63 bool check_suspended)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010064 : thread_(thread),
65 walk_kind_(walk_kind),
66 cur_shadow_frame_(nullptr),
67 cur_quick_frame_(nullptr),
68 cur_quick_frame_pc_(0),
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010069 cur_oat_quick_method_header_(nullptr),
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010070 num_frames_(num_frames),
71 cur_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
Mathieu Chartiere401d142015-04-22 13:56:20 -070079ArtMethod* StackVisitor::GetMethod() const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +010080 if (cur_shadow_frame_ != nullptr) {
81 return cur_shadow_frame_->GetMethod();
82 } else if (cur_quick_frame_ != nullptr) {
83 if (IsInInlinedFrame()) {
David Srbecky61b28a12016-02-25 21:55:03 +000084 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
David Srbecky8cd54542018-07-15 23:58:44 +010085 CodeInfo code_info(method_header);
Mathieu Chartier45bf2502016-03-31 11:07:09 -070086 DCHECK(walk_kind_ != StackWalkKind::kSkipInlinedFrames);
David Srbecky8cd54542018-07-15 23:58:44 +010087 return GetResolvedMethod(*GetCurrentQuickFrame(), code_info, current_inline_frames_);
Nicolas Geoffray57f61612015-05-15 13:20:41 +010088 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -070089 return *cur_quick_frame_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +010090 }
Nicolas Geoffray57f61612015-05-15 13:20:41 +010091 }
Mathieu Chartiere401d142015-04-22 13:56:20 -070092 return nullptr;
Nicolas Geoffray57f61612015-05-15 13:20:41 +010093}
94
Dave Allisonb373e092014-02-20 16:06:36 -080095uint32_t StackVisitor::GetDexPc(bool abort_on_failure) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070096 if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -070097 return cur_shadow_frame_->GetDexPC();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070098 } else if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray57f61612015-05-15 13:20:41 +010099 if (IsInInlinedFrame()) {
David Srbecky93bd3612018-07-02 19:30:18 +0100100 return current_inline_frames_.back().GetDexPc();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100101 } else if (cur_oat_quick_method_header_ == nullptr) {
Andreas Gampee2abbc62017-09-15 11:59:26 -0700102 return dex::kDexNoIndex;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100103 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100104 return cur_oat_quick_method_header_->ToDexPc(
105 GetMethod(), cur_quick_frame_pc_, abort_on_failure);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100106 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700107 } else {
108 return 0;
109 }
110}
111
Mathieu Chartiere401d142015-04-22 13:56:20 -0700112extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700113 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertza836bc92014-11-25 16:30:53 +0100114
Ian Rogers62d6c772013-02-27 08:32:07 -0800115mirror::Object* StackVisitor::GetThisObject() const {
Andreas Gampe542451c2016-07-26 09:02:02 -0700116 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700117 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800118 if (m->IsStatic()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100119 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800120 } else if (m->IsNative()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100121 if (cur_quick_frame_ != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700122 HandleScope* hs = reinterpret_cast<HandleScope*>(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100123 reinterpret_cast<char*>(cur_quick_frame_) + sizeof(ArtMethod*));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700124 return hs->GetReference(0);
Ian Rogers62d6c772013-02-27 08:32:07 -0800125 } else {
126 return cur_shadow_frame_->GetVRegReference(0);
127 }
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000128 } else if (m->IsProxyMethod()) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100129 if (cur_quick_frame_ != nullptr) {
130 return artQuickGetProxyThisObject(cur_quick_frame_);
131 } else {
132 return cur_shadow_frame_->GetVRegReference(0);
133 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800134 } else {
David Sehr0225f8e2018-01-31 08:52:24 +0000135 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800136 if (!accessor.HasCodeItem()) {
Ian Rogerse0dcd462014-03-08 15:21:04 -0800137 UNIMPLEMENTED(ERROR) << "Failed to determine this object of abstract or proxy method: "
David Sehr709b0702016-10-13 09:12:37 -0700138 << ArtMethod::PrettyMethod(m);
Ian Rogerse0dcd462014-03-08 15:21:04 -0800139 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800140 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800141 uint16_t reg = accessor.RegistersSize() - accessor.InsSize();
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000142 uint32_t value = 0;
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100143 if (!GetVReg(m, reg, kReferenceVReg, &value)) {
144 return nullptr;
145 }
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000146 return reinterpret_cast<mirror::Object*>(value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800147 }
148 }
149}
150
Ian Rogers0c7abda2012-09-19 13:33:42 -0700151size_t StackVisitor::GetNativePcOffset() const {
152 DCHECK(!IsShadowFrame());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100153 return GetCurrentOatQuickMethodHeader()->NativeQuickPcOffset(cur_quick_frame_pc_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700154}
155
Mingyao Yang99170c62015-07-06 11:10:37 -0700156bool StackVisitor::GetVRegFromDebuggerShadowFrame(uint16_t vreg,
157 VRegKind kind,
158 uint32_t* val) const {
159 size_t frame_id = const_cast<StackVisitor*>(this)->GetFrameId();
160 ShadowFrame* shadow_frame = thread_->FindDebuggerShadowFrame(frame_id);
161 if (shadow_frame != nullptr) {
162 bool* updated_vreg_flags = thread_->GetUpdatedVRegFlags(frame_id);
163 DCHECK(updated_vreg_flags != nullptr);
164 if (updated_vreg_flags[vreg]) {
165 // Value is set by the debugger.
166 if (kind == kReferenceVReg) {
167 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
168 shadow_frame->GetVRegReference(vreg)));
169 } else {
170 *val = shadow_frame->GetVReg(vreg);
171 }
172 return true;
173 }
174 }
175 // No value is set by the debugger.
176 return false;
177}
178
Mathieu Chartiere401d142015-04-22 13:56:20 -0700179bool StackVisitor::GetVReg(ArtMethod* m, uint16_t vreg, VRegKind kind, uint32_t* val) const {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200180 if (cur_quick_frame_ != nullptr) {
181 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800182 DCHECK(m == GetMethod());
Mingyao Yang99170c62015-07-06 11:10:37 -0700183 // Check if there is value set by the debugger.
184 if (GetVRegFromDebuggerShadowFrame(vreg, kind, val)) {
185 return true;
186 }
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100187 DCHECK(cur_oat_quick_method_header_->IsOptimized());
188 return GetVRegFromOptimizedCode(m, vreg, kind, val);
Ian Rogers0399dde2012-06-06 17:09:28 -0700189 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100190 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertz09687442015-11-17 10:35:39 +0100191 if (kind == kReferenceVReg) {
192 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
193 cur_shadow_frame_->GetVRegReference(vreg)));
194 } else {
195 *val = cur_shadow_frame_->GetVReg(vreg);
196 }
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200197 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700198 }
199}
200
Mathieu Chartiere401d142015-04-22 13:56:20 -0700201bool StackVisitor::GetVRegFromOptimizedCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100202 uint32_t* val) const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100203 DCHECK_EQ(m, GetMethod());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800204 // Can't be null or how would we compile its instructions?
205 DCHECK(m->GetCodeItem() != nullptr) << m->PrettyMethod();
David Sehr0225f8e2018-01-31 08:52:24 +0000206 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800207 uint16_t number_of_dex_registers = accessor.RegistersSize();
208 DCHECK_LT(vreg, number_of_dex_registers);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100209 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
David Srbecky052f8ca2018-04-26 15:42:54 +0100210 CodeInfo code_info(method_header);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100211
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100212 uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_);
David Srbecky052f8ca2018-04-26 15:42:54 +0100213 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100214 DCHECK(stack_map.IsValid());
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100215
216 DexRegisterMap dex_register_map = IsInInlinedFrame()
David Srbecky93bd3612018-07-02 19:30:18 +0100217 ? code_info.GetInlineDexRegisterMapOf(stack_map, current_inline_frames_.back())
David Srbeckyfd89b072018-06-03 12:00:22 +0100218 : code_info.GetDexRegisterMapOf(stack_map);
219 if (dex_register_map.empty()) {
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000220 return false;
221 }
David Srbeckyfd89b072018-06-03 12:00:22 +0100222 DCHECK_EQ(dex_register_map.size(), number_of_dex_registers);
David Srbeckye1402122018-06-13 18:20:45 +0100223 DexRegisterLocation::Kind location_kind = dex_register_map[vreg].GetKind();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100224 switch (location_kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000225 case DexRegisterLocation::Kind::kInStack: {
David Srbeckye1402122018-06-13 18:20:45 +0100226 const int32_t offset = dex_register_map[vreg].GetStackOffsetInBytes();
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100227 BitMemoryRegion stack_mask = code_info.GetStackMaskOf(stack_map);
228 if (kind == kReferenceVReg && !stack_mask.LoadBit(offset / kFrameSlotSize)) {
229 return false;
230 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100231 const uint8_t* addr = reinterpret_cast<const uint8_t*>(cur_quick_frame_) + offset;
232 *val = *reinterpret_cast<const uint32_t*>(addr);
233 return true;
234 }
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100235 case DexRegisterLocation::Kind::kInRegister: {
236 uint32_t register_mask = code_info.GetRegisterMaskOf(stack_map);
237 uint32_t reg = dex_register_map[vreg].GetMachineRegister();
238 if (kind == kReferenceVReg && !(register_mask & (1 << reg))) {
239 return false;
240 }
241 return GetRegisterIfAccessible(reg, kind, val);
242 }
David Brazdild9cb68e2015-08-25 13:52:43 +0100243 case DexRegisterLocation::Kind::kInRegisterHigh:
244 case DexRegisterLocation::Kind::kInFpuRegister:
245 case DexRegisterLocation::Kind::kInFpuRegisterHigh: {
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100246 if (kind == kReferenceVReg) {
247 return false;
248 }
David Srbeckye1402122018-06-13 18:20:45 +0100249 uint32_t reg = dex_register_map[vreg].GetMachineRegister();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100250 return GetRegisterIfAccessible(reg, kind, val);
251 }
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100252 case DexRegisterLocation::Kind::kConstant: {
253 uint32_t result = dex_register_map[vreg].GetConstant();
254 if (kind == kReferenceVReg && result != 0) {
255 return false;
256 }
257 *val = result;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100258 return true;
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100259 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000260 case DexRegisterLocation::Kind::kNone:
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100261 return false;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000262 default:
David Srbeckye1402122018-06-13 18:20:45 +0100263 LOG(FATAL) << "Unexpected location kind " << dex_register_map[vreg].GetKind();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000264 UNREACHABLE();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100265 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100266}
267
268bool StackVisitor::GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const {
269 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
David Brazdil77a48ae2015-09-15 12:34:04 +0000270
Vladimir Marko239d6ea2016-09-05 10:44:04 +0100271 if (kRuntimeISA == InstructionSet::kX86 && is_float) {
272 // X86 float registers are 64-bit and each XMM register is provided as two separate
273 // 32-bit registers by the context.
274 reg = (kind == kDoubleHiVReg) ? (2 * reg + 1) : (2 * reg);
275 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000276
Goran Jakovljevic986660c2015-12-10 11:44:50 +0100277 // MIPS32 float registers are used as 64-bit (for MIPS32r2 it is pair
278 // F(2n)-F(2n+1), and for MIPS32r6 it is 64-bit register F(2n)). When
279 // accessing upper 32-bits from double, reg + 1 should be used.
280 if ((kRuntimeISA == InstructionSet::kMips) && (kind == kDoubleHiVReg)) {
281 DCHECK_ALIGNED(reg, 2);
282 reg++;
283 }
284
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100285 if (!IsAccessibleRegister(reg, is_float)) {
286 return false;
287 }
288 uintptr_t ptr_val = GetRegister(reg, is_float);
289 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
290 if (target64) {
291 const bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
292 const bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
293 int64_t value_long = static_cast<int64_t>(ptr_val);
294 if (wide_lo) {
295 ptr_val = static_cast<uintptr_t>(Low32Bits(value_long));
296 } else if (wide_hi) {
297 ptr_val = static_cast<uintptr_t>(High32Bits(value_long));
298 }
299 }
300 *val = ptr_val;
301 return true;
302}
303
Mingyao Yang99170c62015-07-06 11:10:37 -0700304bool StackVisitor::GetVRegPairFromDebuggerShadowFrame(uint16_t vreg,
305 VRegKind kind_lo,
306 VRegKind kind_hi,
307 uint64_t* val) const {
308 uint32_t low_32bits;
309 uint32_t high_32bits;
310 bool success = GetVRegFromDebuggerShadowFrame(vreg, kind_lo, &low_32bits);
311 success &= GetVRegFromDebuggerShadowFrame(vreg + 1, kind_hi, &high_32bits);
312 if (success) {
313 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
314 }
315 return success;
316}
317
Mathieu Chartiere401d142015-04-22 13:56:20 -0700318bool StackVisitor::GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200319 VRegKind kind_hi, uint64_t* val) const {
320 if (kind_lo == kLongLoVReg) {
321 DCHECK_EQ(kind_hi, kLongHiVReg);
322 } else if (kind_lo == kDoubleLoVReg) {
323 DCHECK_EQ(kind_hi, kDoubleHiVReg);
324 } else {
325 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100326 UNREACHABLE();
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200327 }
Mingyao Yang99170c62015-07-06 11:10:37 -0700328 // Check if there is value set by the debugger.
329 if (GetVRegPairFromDebuggerShadowFrame(vreg, kind_lo, kind_hi, val)) {
330 return true;
331 }
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200332 if (cur_quick_frame_ != nullptr) {
333 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
334 DCHECK(m == GetMethod());
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100335 DCHECK(cur_oat_quick_method_header_->IsOptimized());
336 return GetVRegPairFromOptimizedCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200337 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100338 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200339 *val = cur_shadow_frame_->GetVRegLong(vreg);
340 return true;
341 }
342}
343
Mathieu Chartiere401d142015-04-22 13:56:20 -0700344bool StackVisitor::GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100345 VRegKind kind_lo, VRegKind kind_hi,
346 uint64_t* val) const {
347 uint32_t low_32bits;
348 uint32_t high_32bits;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700349 bool success = GetVRegFromOptimizedCode(m, vreg, kind_lo, &low_32bits);
350 success &= GetVRegFromOptimizedCode(m, vreg + 1, kind_hi, &high_32bits);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100351 if (success) {
352 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
353 }
354 return success;
355}
356
357bool StackVisitor::GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
358 VRegKind kind_lo, uint64_t* val) const {
359 const bool is_float = (kind_lo == kDoubleLoVReg);
360 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
361 return false;
362 }
363 uintptr_t ptr_val_lo = GetRegister(reg_lo, is_float);
364 uintptr_t ptr_val_hi = GetRegister(reg_hi, is_float);
365 bool target64 = Is64BitInstructionSet(kRuntimeISA);
366 if (target64) {
367 int64_t value_long_lo = static_cast<int64_t>(ptr_val_lo);
368 int64_t value_long_hi = static_cast<int64_t>(ptr_val_hi);
369 ptr_val_lo = static_cast<uintptr_t>(Low32Bits(value_long_lo));
370 ptr_val_hi = static_cast<uintptr_t>(High32Bits(value_long_hi));
371 }
372 *val = (static_cast<uint64_t>(ptr_val_hi) << 32) | static_cast<uint32_t>(ptr_val_lo);
373 return true;
374}
375
Vladimir Marko439d1262019-04-12 14:45:07 +0100376ShadowFrame* StackVisitor::PrepareSetVReg(ArtMethod* m, uint16_t vreg, bool wide) {
David Sehr0225f8e2018-01-31 08:52:24 +0000377 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800378 if (!accessor.HasCodeItem()) {
Vladimir Marko439d1262019-04-12 14:45:07 +0100379 return nullptr;
Mingyao Yang99170c62015-07-06 11:10:37 -0700380 }
381 ShadowFrame* shadow_frame = GetCurrentShadowFrame();
382 if (shadow_frame == nullptr) {
383 // This is a compiled frame: we must prepare and update a shadow frame that will
384 // be executed by the interpreter after deoptimization of the stack.
385 const size_t frame_id = GetFrameId();
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800386 const uint16_t num_regs = accessor.RegistersSize();
Mingyao Yang99170c62015-07-06 11:10:37 -0700387 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
388 CHECK(shadow_frame != nullptr);
Vladimir Marko439d1262019-04-12 14:45:07 +0100389 // Remember the vreg(s) has been set for debugging and must not be overwritten by the
Mingyao Yang99170c62015-07-06 11:10:37 -0700390 // original value during deoptimization of the stack.
391 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
Vladimir Marko439d1262019-04-12 14:45:07 +0100392 if (wide) {
393 thread_->GetUpdatedVRegFlags(frame_id)[vreg + 1] = true;
394 }
Mingyao Yang99170c62015-07-06 11:10:37 -0700395 }
Vladimir Marko439d1262019-04-12 14:45:07 +0100396 return shadow_frame;
397}
398
399bool StackVisitor::SetVReg(ArtMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind) {
400 DCHECK(kind == kIntVReg || kind == kFloatVReg);
401 ShadowFrame* shadow_frame = PrepareSetVReg(m, vreg, /* wide= */ false);
402 if (shadow_frame == nullptr) {
403 return false;
Mingyao Yang99170c62015-07-06 11:10:37 -0700404 }
Vladimir Marko439d1262019-04-12 14:45:07 +0100405 shadow_frame->SetVReg(vreg, new_value);
406 return true;
407}
408
409bool StackVisitor::SetVRegReference(ArtMethod* m, uint16_t vreg, ObjPtr<mirror::Object> new_value) {
410 ShadowFrame* shadow_frame = PrepareSetVReg(m, vreg, /* wide= */ false);
411 if (shadow_frame == nullptr) {
412 return false;
413 }
414 shadow_frame->SetVRegReference(vreg, new_value);
Mingyao Yang99170c62015-07-06 11:10:37 -0700415 return true;
416}
417
Mingyao Yang636b9252015-07-31 16:40:24 -0700418bool StackVisitor::SetVRegPair(ArtMethod* m,
419 uint16_t vreg,
420 uint64_t new_value,
421 VRegKind kind_lo,
422 VRegKind kind_hi) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700423 if (kind_lo == kLongLoVReg) {
424 DCHECK_EQ(kind_hi, kLongHiVReg);
425 } else if (kind_lo == kDoubleLoVReg) {
426 DCHECK_EQ(kind_hi, kDoubleHiVReg);
427 } else {
428 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
429 UNREACHABLE();
430 }
Vladimir Marko439d1262019-04-12 14:45:07 +0100431 ShadowFrame* shadow_frame = PrepareSetVReg(m, vreg, /* wide= */ true);
Mingyao Yang99170c62015-07-06 11:10:37 -0700432 if (shadow_frame == nullptr) {
Vladimir Marko439d1262019-04-12 14:45:07 +0100433 return false;
Mingyao Yang99170c62015-07-06 11:10:37 -0700434 }
435 shadow_frame->SetVRegLong(vreg, new_value);
436 return true;
437}
438
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100439bool StackVisitor::IsAccessibleGPR(uint32_t reg) const {
440 DCHECK(context_ != nullptr);
441 return context_->IsAccessibleGPR(reg);
442}
443
Mathieu Chartier815873e2014-02-13 18:02:13 -0800444uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100445 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
446 DCHECK(context_ != nullptr);
Mathieu Chartier815873e2014-02-13 18:02:13 -0800447 return context_->GetGPRAddress(reg);
448}
449
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100450uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
451 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
452 DCHECK(context_ != nullptr);
453 return context_->GetGPR(reg);
Ian Rogers0399dde2012-06-06 17:09:28 -0700454}
455
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100456bool StackVisitor::IsAccessibleFPR(uint32_t reg) const {
457 DCHECK(context_ != nullptr);
458 return context_->IsAccessibleFPR(reg);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200459}
460
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100461uintptr_t StackVisitor::GetFPR(uint32_t reg) const {
462 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
463 DCHECK(context_ != nullptr);
464 return context_->GetFPR(reg);
465}
466
Ian Rogers0399dde2012-06-06 17:09:28 -0700467uintptr_t StackVisitor::GetReturnPc() const {
Ian Rogers13735952014-10-08 12:43:28 -0700468 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700469 DCHECK(sp != nullptr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100470 uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogers0399dde2012-06-06 17:09:28 -0700471 return *reinterpret_cast<uintptr_t*>(pc_addr);
472}
473
474void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
Ian Rogers13735952014-10-08 12:43:28 -0700475 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700476 CHECK(sp != nullptr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100477 uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogers0399dde2012-06-06 17:09:28 -0700478 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc;
479}
480
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100481size_t StackVisitor::ComputeNumFrames(Thread* thread, StackWalkKind walk_kind) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700482 struct NumFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100483 NumFramesVisitor(Thread* thread_in, StackWalkKind walk_kind_in)
484 : StackVisitor(thread_in, nullptr, walk_kind_in), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700485
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100486 bool VisitFrame() override {
Ian Rogers0399dde2012-06-06 17:09:28 -0700487 frames++;
488 return true;
489 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700490
Ian Rogers0399dde2012-06-06 17:09:28 -0700491 size_t frames;
492 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100493 NumFramesVisitor visitor(thread, walk_kind);
Ian Rogers0399dde2012-06-06 17:09:28 -0700494 visitor.WalkStack(true);
495 return visitor.frames;
496}
497
Mathieu Chartiere401d142015-04-22 13:56:20 -0700498bool StackVisitor::GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700499 struct HasMoreFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100500 HasMoreFramesVisitor(Thread* thread,
501 StackWalkKind walk_kind,
502 size_t num_frames,
503 size_t frame_height)
504 : StackVisitor(thread, nullptr, walk_kind, num_frames),
505 frame_height_(frame_height),
506 found_frame_(false),
507 has_more_frames_(false),
508 next_method_(nullptr),
509 next_dex_pc_(0) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700510 }
511
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100512 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700513 if (found_frame_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700514 ArtMethod* method = GetMethod();
Ian Rogers5cf98192014-05-29 21:31:50 -0700515 if (method != nullptr && !method->IsRuntimeMethod()) {
516 has_more_frames_ = true;
517 next_method_ = method;
518 next_dex_pc_ = GetDexPc();
519 return false; // End stack walk once next method is found.
520 }
521 } else if (GetFrameHeight() == frame_height_) {
522 found_frame_ = true;
523 }
524 return true;
525 }
526
527 size_t frame_height_;
528 bool found_frame_;
529 bool has_more_frames_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700530 ArtMethod* next_method_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700531 uint32_t next_dex_pc_;
532 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100533 HasMoreFramesVisitor visitor(thread_, walk_kind_, GetNumFrames(), GetFrameHeight());
Ian Rogers5cf98192014-05-29 21:31:50 -0700534 visitor.WalkStack(true);
535 *next_method = visitor.next_method_;
536 *next_dex_pc = visitor.next_dex_pc_;
537 return visitor.has_more_frames_;
538}
539
Ian Rogers7a22fa62013-01-23 12:16:16 -0800540void StackVisitor::DescribeStack(Thread* thread) {
Ian Rogers306057f2012-11-26 12:45:53 -0800541 struct DescribeStackVisitor : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800542 explicit DescribeStackVisitor(Thread* thread_in)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100543 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800544
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100545 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers306057f2012-11-26 12:45:53 -0800546 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation();
547 return true;
548 }
549 };
Ian Rogers7a22fa62013-01-23 12:16:16 -0800550 DescribeStackVisitor visitor(thread);
Ian Rogers306057f2012-11-26 12:45:53 -0800551 visitor.WalkStack(true);
552}
553
Ian Rogers40e3bac2012-11-20 00:09:14 -0800554std::string StackVisitor::DescribeLocation() const {
555 std::string result("Visiting method '");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700556 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700557 if (m == nullptr) {
Ian Rogers306057f2012-11-26 12:45:53 -0800558 return "upcall";
559 }
David Sehr709b0702016-10-13 09:12:37 -0700560 result += m->PrettyMethod();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800561 result += StringPrintf("' at dex PC 0x%04x", GetDexPc());
Ian Rogers40e3bac2012-11-20 00:09:14 -0800562 if (!IsShadowFrame()) {
563 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc()));
564 }
565 return result;
566}
567
Alex Lightdba61482016-12-21 08:20:29 -0800568void StackVisitor::SetMethod(ArtMethod* method) {
569 DCHECK(GetMethod() != nullptr);
570 if (cur_shadow_frame_ != nullptr) {
571 cur_shadow_frame_->SetMethod(method);
572 } else {
573 DCHECK(cur_quick_frame_ != nullptr);
Nicolas Geoffray226805d2018-12-14 10:59:02 +0000574 CHECK(!IsInInlinedFrame()) << "We do not support setting inlined method's ArtMethod: "
575 << GetMethod()->PrettyMethod() << " is inlined into "
576 << GetOuterMethod()->PrettyMethod();
Alex Light1ebe4fe2017-01-30 14:57:11 -0800577 *cur_quick_frame_ = method;
Alex Lightdba61482016-12-21 08:20:29 -0800578 }
579}
580
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100581static void AssertPcIsWithinQuickCode(ArtMethod* method, uintptr_t pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700582 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100583 if (method->IsNative() || method->IsRuntimeMethod() || method->IsProxyMethod()) {
584 return;
585 }
586
587 if (pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) {
588 return;
589 }
590
Mingyao Yang88ca8ba2017-05-23 14:21:07 -0700591 Runtime* runtime = Runtime::Current();
592 if (runtime->UseJitCompilation() &&
593 runtime->GetJit()->GetCodeCache()->ContainsPc(reinterpret_cast<const void*>(pc))) {
594 return;
595 }
596
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100597 const void* code = method->GetEntryPointFromQuickCompiledCode();
Alex Lightdb01a092017-04-03 15:39:55 -0700598 if (code == GetQuickInstrumentationEntryPoint() || code == GetInvokeObsoleteMethodStub()) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100599 return;
600 }
601
602 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
603 if (class_linker->IsQuickToInterpreterBridge(code) ||
604 class_linker->IsQuickResolutionStub(code)) {
605 return;
606 }
607
Calin Juravleffc87072016-04-20 14:22:09 +0100608 if (runtime->UseJitCompilation() && runtime->GetJit()->GetCodeCache()->ContainsPc(code)) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100609 return;
610 }
611
Mingyao Yang063fc772016-08-02 11:02:54 -0700612 uint32_t code_size = OatQuickMethodHeader::FromEntryPoint(code)->GetCodeSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100613 uintptr_t code_start = reinterpret_cast<uintptr_t>(code);
614 CHECK(code_start <= pc && pc <= (code_start + code_size))
David Sehr709b0702016-10-13 09:12:37 -0700615 << method->PrettyMethod()
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100616 << " pc=" << std::hex << pc
Roland Levillain0d5a2812015-11-13 10:07:31 +0000617 << " code_start=" << code_start
618 << " code_size=" << code_size;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100619}
620
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700621void StackVisitor::SanityCheckFrame() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800622 if (kIsDebugBuild) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700623 ArtMethod* method = GetMethod();
Vladimir Markod93e3742018-07-18 10:58:13 +0100624 ObjPtr<mirror::Class> declaring_class = method->GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700625 // Runtime methods have null declaring class.
626 if (!method->IsRuntimeMethod()) {
627 CHECK(declaring_class != nullptr);
628 CHECK_EQ(declaring_class->GetClass(), declaring_class->GetClass()->GetClass())
629 << declaring_class;
630 } else {
631 CHECK(declaring_class == nullptr);
632 }
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700633 Runtime* const runtime = Runtime::Current();
634 LinearAlloc* const linear_alloc = runtime->GetLinearAlloc();
635 if (!linear_alloc->Contains(method)) {
636 // Check class linker linear allocs.
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100637 // We get the canonical method as copied methods may have their declaring
638 // class from another class loader.
639 ArtMethod* canonical = method->GetCanonicalMethod();
Vladimir Markod93e3742018-07-18 10:58:13 +0100640 ObjPtr<mirror::Class> klass = canonical->GetDeclaringClass();
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700641 LinearAlloc* const class_linear_alloc = (klass != nullptr)
Mathieu Chartier5b830502016-03-02 10:30:23 -0800642 ? runtime->GetClassLinker()->GetAllocatorForClassLoader(klass->GetClassLoader())
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700643 : linear_alloc;
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100644 if (!class_linear_alloc->Contains(canonical)) {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700645 // Check image space.
646 bool in_image = false;
647 for (auto& space : runtime->GetHeap()->GetContinuousSpaces()) {
648 if (space->IsImageSpace()) {
649 auto* image_space = space->AsImageSpace();
650 const auto& header = image_space->GetImageHeader();
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700651 const ImageSection& methods = header.GetMethodsSection();
652 const ImageSection& runtime_methods = header.GetRuntimeMethodsSection();
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100653 const size_t offset = reinterpret_cast<const uint8_t*>(canonical) - image_space->Begin();
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700654 if (methods.Contains(offset) || runtime_methods.Contains(offset)) {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700655 in_image = true;
656 break;
657 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700658 }
659 }
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100660 CHECK(in_image) << canonical->PrettyMethod() << " not in linear alloc or image";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700661 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700662 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800663 if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100664 AssertPcIsWithinQuickCode(method, cur_quick_frame_pc_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800665 // Frame sanity.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100666 size_t frame_size = GetCurrentQuickFrameInfo().FrameSizeInBytes();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800667 CHECK_NE(frame_size, 0u);
Andreas Gampe5b417b92014-03-10 14:18:35 -0700668 // A rough guess at an upper size we expect to see for a frame.
669 // 256 registers
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700670 // 2 words HandleScope overhead
Andreas Gampe5b417b92014-03-10 14:18:35 -0700671 // 3+3 register spills
672 // TODO: this seems architecture specific for the case of JNI frames.
Brian Carlstromed08bd42014-03-19 18:34:17 -0700673 // TODO: 083-compiler-regressions ManyFloatArgs shows this estimate is wrong.
674 // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word);
675 const size_t kMaxExpectedFrameSize = 2 * KB;
David Sehr709b0702016-10-13 09:12:37 -0700676 CHECK_LE(frame_size, kMaxExpectedFrameSize) << method->PrettyMethod();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100677 size_t return_pc_offset = GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800678 CHECK_LT(return_pc_offset, frame_size);
679 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700680 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700681}
682
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100683// Counts the number of references in the parameter list of the corresponding method.
684// Note: Thus does _not_ include "this" for non-static methods.
685static uint32_t GetNumberOfReferenceArgsWithoutReceiver(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700686 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100687 uint32_t shorty_len;
688 const char* shorty = method->GetShorty(&shorty_len);
689 uint32_t refs = 0;
690 for (uint32_t i = 1; i < shorty_len ; ++i) {
691 if (shorty[i] == 'L') {
692 refs++;
693 }
694 }
695 return refs;
696}
697
698QuickMethodFrameInfo StackVisitor::GetCurrentQuickFrameInfo() const {
699 if (cur_oat_quick_method_header_ != nullptr) {
700 return cur_oat_quick_method_header_->GetFrameInfo();
701 }
702
703 ArtMethod* method = GetMethod();
704 Runtime* runtime = Runtime::Current();
705
706 if (method->IsAbstract()) {
Vladimir Markod3083dd2018-05-17 08:43:47 +0100707 return RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100708 }
709
710 // This goes before IsProxyMethod since runtime methods have a null declaring class.
711 if (method->IsRuntimeMethod()) {
712 return runtime->GetRuntimeMethodFrameInfo(method);
713 }
714
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100715 if (method->IsProxyMethod()) {
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000716 // There is only one direct method of a proxy class: the constructor. A direct method is
717 // cloned from the original java.lang.reflect.Proxy and is executed as usual quick
718 // compiled method without any stubs. Therefore the method must have a OatQuickMethodHeader.
719 DCHECK(!method->IsDirect() && !method->IsConstructor())
720 << "Constructors of proxy classes must have a OatQuickMethodHeader";
Vladimir Markod3083dd2018-05-17 08:43:47 +0100721 return RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100722 }
723
Vladimir Marko2196c652017-11-30 16:16:07 +0000724 // The only remaining case is if the method is native and uses the generic JNI stub,
725 // called either directly or through some (resolution, instrumentation) trampoline.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100726 DCHECK(method->IsNative());
Vladimir Marko2196c652017-11-30 16:16:07 +0000727 if (kIsDebugBuild) {
728 ClassLinker* class_linker = runtime->GetClassLinker();
729 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(method,
730 kRuntimePointerSize);
731 CHECK(class_linker->IsQuickGenericJniStub(entry_point) ||
732 // The current entrypoint (after filtering out trampolines) may have changed
733 // from GenericJNI to JIT-compiled stub since we have entered this frame.
734 (runtime->GetJit() != nullptr &&
735 runtime->GetJit()->GetCodeCache()->ContainsPc(entry_point))) << method->PrettyMethod();
736 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100737 // Generic JNI frame.
738 uint32_t handle_refs = GetNumberOfReferenceArgsWithoutReceiver(method) + 1;
739 size_t scope_size = HandleScope::SizeOf(handle_refs);
Vladimir Markod3083dd2018-05-17 08:43:47 +0100740 constexpr QuickMethodFrameInfo callee_info =
741 RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100742
743 // Callee saves + handle scope + method ref + alignment
744 // Note: -sizeof(void*) since callee-save frame stores a whole method pointer.
745 size_t frame_size = RoundUp(
746 callee_info.FrameSizeInBytes() - sizeof(void*) + sizeof(ArtMethod*) + scope_size,
747 kStackAlignment);
748 return QuickMethodFrameInfo(frame_size, callee_info.CoreSpillMask(), callee_info.FpSpillMask());
749}
750
Andreas Gampe585da952016-12-02 14:52:29 -0800751template <StackVisitor::CountTransitions kCount>
Ian Rogers0399dde2012-06-06 17:09:28 -0700752void StackVisitor::WalkStack(bool include_transitions) {
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -0800753 if (check_suspended_) {
754 DCHECK(thread_ == Thread::Current() || thread_->IsSuspended());
755 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800756 CHECK_EQ(cur_depth_, 0U);
757 bool exit_stubs_installed = Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled();
Alex Lightb81a9842016-12-15 00:59:05 +0000758 uint32_t instrumentation_stack_depth = 0;
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000759 size_t inlined_frames_count = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700760
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700761 for (const ManagedStack* current_fragment = thread_->GetManagedStack();
762 current_fragment != nullptr; current_fragment = current_fragment->GetLink()) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700763 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
764 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700765 cur_quick_frame_pc_ = 0;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100766 cur_oat_quick_method_header_ = nullptr;
Dave Allisonf9439142014-03-27 15:10:22 -0700767
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700768 if (cur_quick_frame_ != nullptr) { // Handle quick stack frames.
Ian Rogers0399dde2012-06-06 17:09:28 -0700769 // Can't be both a shadow and a quick fragment.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700770 DCHECK(current_fragment->GetTopShadowFrame() == nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700771 ArtMethod* method = *cur_quick_frame_;
Vladimir Marko2196c652017-11-30 16:16:07 +0000772 DCHECK(method != nullptr);
773 bool header_retrieved = false;
774 if (method->IsNative()) {
775 // We do not have a PC for the first frame, so we cannot simply use
776 // ArtMethod::GetOatQuickMethodHeader() as we're unable to distinguish there
777 // between GenericJNI frame and JIT-compiled JNI stub; the entrypoint may have
778 // changed since the frame was entered. The top quick frame tag indicates
779 // GenericJNI here, otherwise it's either AOT-compiled or JNI-compiled JNI stub.
780 if (UNLIKELY(current_fragment->GetTopQuickFrameTag())) {
781 // The generic JNI does not have any method header.
782 cur_oat_quick_method_header_ = nullptr;
783 } else {
784 const void* existing_entry_point = method->GetEntryPointFromQuickCompiledCode();
785 CHECK(existing_entry_point != nullptr);
786 Runtime* runtime = Runtime::Current();
787 ClassLinker* class_linker = runtime->GetClassLinker();
788 // Check whether we can quickly get the header from the current entrypoint.
789 if (!class_linker->IsQuickGenericJniStub(existing_entry_point) &&
790 !class_linker->IsQuickResolutionStub(existing_entry_point) &&
791 existing_entry_point != GetQuickInstrumentationEntryPoint()) {
792 cur_oat_quick_method_header_ =
793 OatQuickMethodHeader::FromEntryPoint(existing_entry_point);
794 } else {
795 const void* code = method->GetOatMethodQuickCode(class_linker->GetImagePointerSize());
796 if (code != nullptr) {
797 cur_oat_quick_method_header_ = OatQuickMethodHeader::FromEntryPoint(code);
798 } else {
799 // This must be a JITted JNI stub frame.
800 CHECK(runtime->GetJit() != nullptr);
801 code = runtime->GetJit()->GetCodeCache()->GetJniStubCode(method);
802 CHECK(code != nullptr) << method->PrettyMethod();
803 cur_oat_quick_method_header_ = OatQuickMethodHeader::FromCodePointer(code);
804 }
805 }
806 }
807 header_retrieved = true;
808 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700809 while (method != nullptr) {
Vladimir Marko2196c652017-11-30 16:16:07 +0000810 if (!header_retrieved) {
811 cur_oat_quick_method_header_ = method->GetOatQuickMethodHeader(cur_quick_frame_pc_);
812 }
813 header_retrieved = false; // Force header retrieval in next iteration.
Dave Allison5cd33752014-04-15 15:57:58 -0700814 SanityCheckFrame();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100815
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100816 if ((walk_kind_ == StackWalkKind::kIncludeInlinedFrames)
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100817 && (cur_oat_quick_method_header_ != nullptr)
David Srbeckyafc97bc2018-07-05 08:14:35 +0000818 && cur_oat_quick_method_header_->IsOptimized()
819 // JNI methods cannot have any inlined frames.
820 && !method->IsNative()) {
821 DCHECK_NE(cur_quick_frame_pc_, 0u);
David Srbecky2259f1c2019-01-16 23:18:30 +0000822 current_code_info_ = CodeInfo(cur_oat_quick_method_header_,
823 CodeInfo::DecodeFlags::InlineInfoOnly);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100824 uint32_t native_pc_offset =
825 cur_oat_quick_method_header_->NativeQuickPcOffset(cur_quick_frame_pc_);
David Srbecky2259f1c2019-01-16 23:18:30 +0000826 StackMap stack_map = current_code_info_.GetStackMapForNativePcOffset(native_pc_offset);
David Srbecky052f8ca2018-04-26 15:42:54 +0100827 if (stack_map.IsValid() && stack_map.HasInlineInfo()) {
David Srbecky93bd3612018-07-02 19:30:18 +0100828 DCHECK_EQ(current_inline_frames_.size(), 0u);
David Srbecky2259f1c2019-01-16 23:18:30 +0000829 for (current_inline_frames_ = current_code_info_.GetInlineInfosOf(stack_map);
David Srbecky93bd3612018-07-02 19:30:18 +0100830 !current_inline_frames_.empty();
831 current_inline_frames_.pop_back()) {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100832 bool should_continue = VisitFrame();
833 if (UNLIKELY(!should_continue)) {
834 return;
835 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100836 cur_depth_++;
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000837 inlined_frames_count++;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100838 }
839 }
840 }
841
Dave Allison5cd33752014-04-15 15:57:58 -0700842 bool should_continue = VisitFrame();
843 if (UNLIKELY(!should_continue)) {
844 return;
Ian Rogers0399dde2012-06-06 17:09:28 -0700845 }
Dave Allison5cd33752014-04-15 15:57:58 -0700846
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100847 QuickMethodFrameInfo frame_info = GetCurrentQuickFrameInfo();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700848 if (context_ != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100849 context_->FillCalleeSaves(reinterpret_cast<uint8_t*>(cur_quick_frame_), frame_info);
Ian Rogers0399dde2012-06-06 17:09:28 -0700850 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700851 // Compute PC for next stack frame from return PC.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100852 size_t frame_size = frame_info.FrameSizeInBytes();
853 size_t return_pc_offset = frame_size - sizeof(void*);
Ian Rogers13735952014-10-08 12:43:28 -0700854 uint8_t* return_pc_addr = reinterpret_cast<uint8_t*>(cur_quick_frame_) + return_pc_offset;
Ian Rogers0399dde2012-06-06 17:09:28 -0700855 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100856
Alex Lightb32c6a92018-06-07 16:48:04 -0700857 if (UNLIKELY(exit_stubs_installed ||
858 reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc)) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700859 // While profiling, the return pc is restored from the side stack, except when walking
860 // the stack for an exception where the side stack will be unwound in VisitFrame.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700861 if (reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc) {
Andreas Gampe585da952016-12-02 14:52:29 -0800862 CHECK_LT(instrumentation_stack_depth, thread_->GetInstrumentationStack()->size());
Sebastien Hertz74e256b2013-10-04 10:40:37 +0200863 const instrumentation::InstrumentationStackFrame& instrumentation_frame =
Vladimir Marko35d5b8a2018-07-03 09:18:32 +0100864 (*thread_->GetInstrumentationStack())[instrumentation_stack_depth];
jeffhao725a9572012-11-13 18:20:12 -0800865 instrumentation_stack_depth++;
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100866 if (GetMethod() ==
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700867 Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveAllCalleeSaves)) {
Jeff Haofb2802d2013-07-24 13:53:05 -0700868 // Skip runtime save all callee frames which are used to deliver exceptions.
869 } else if (instrumentation_frame.interpreter_entry_) {
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100870 ArtMethod* callee =
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700871 Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs);
David Sehr709b0702016-10-13 09:12:37 -0700872 CHECK_EQ(GetMethod(), callee) << "Expected: " << ArtMethod::PrettyMethod(callee)
873 << " Found: " << ArtMethod::PrettyMethod(GetMethod());
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000874 } else {
Alex Lighteee0bd42017-02-14 15:31:45 +0000875 // Instrumentation generally doesn't distinguish between a method's obsolete and
876 // non-obsolete version.
877 CHECK_EQ(instrumentation_frame.method_->GetNonObsoleteMethod(),
878 GetMethod()->GetNonObsoleteMethod())
879 << "Expected: "
880 << ArtMethod::PrettyMethod(instrumentation_frame.method_->GetNonObsoleteMethod())
881 << " Found: " << ArtMethod::PrettyMethod(GetMethod()->GetNonObsoleteMethod());
Ian Rogers62d6c772013-02-27 08:32:07 -0800882 }
883 if (num_frames_ != 0) {
884 // Check agreement of frame Ids only if num_frames_ is computed to avoid infinite
885 // recursion.
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000886 size_t frame_id = instrumentation::Instrumentation::ComputeFrameId(
887 thread_,
888 cur_depth_,
889 inlined_frames_count);
890 CHECK_EQ(instrumentation_frame.frame_id_, frame_id);
Ian Rogers62d6c772013-02-27 08:32:07 -0800891 }
jeffhao725a9572012-11-13 18:20:12 -0800892 return_pc = instrumentation_frame.return_pc_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700893 }
894 }
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100895
Ian Rogers0399dde2012-06-06 17:09:28 -0700896 cur_quick_frame_pc_ = return_pc;
Ian Rogers13735952014-10-08 12:43:28 -0700897 uint8_t* next_frame = reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700898 cur_quick_frame_ = reinterpret_cast<ArtMethod**>(next_frame);
899
900 if (kDebugStackWalk) {
David Sehr709b0702016-10-13 09:12:37 -0700901 LOG(INFO) << ArtMethod::PrettyMethod(method) << "@" << method << " size=" << frame_size
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100902 << std::boolalpha
903 << " optimized=" << (cur_oat_quick_method_header_ != nullptr &&
904 cur_oat_quick_method_header_->IsOptimized())
Mathieu Chartiere401d142015-04-22 13:56:20 -0700905 << " native=" << method->IsNative()
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100906 << std::noboolalpha
Mathieu Chartiere401d142015-04-22 13:56:20 -0700907 << " entrypoints=" << method->GetEntryPointFromQuickCompiledCode()
Alex Lighteee0bd42017-02-14 15:31:45 +0000908 << "," << (method->IsNative() ? method->GetEntryPointFromJni() : nullptr)
Mathieu Chartiere401d142015-04-22 13:56:20 -0700909 << " next=" << *cur_quick_frame_;
910 }
911
Andreas Gampef040be62017-04-14 21:49:33 -0700912 if (kCount == CountTransitions::kYes || !method->IsRuntimeMethod()) {
913 cur_depth_++;
914 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700915 method = *cur_quick_frame_;
jeffhao6641ea12013-01-02 18:13:42 -0800916 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700917 } else if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700918 do {
919 SanityCheckFrame();
920 bool should_continue = VisitFrame();
921 if (UNLIKELY(!should_continue)) {
922 return;
923 }
924 cur_depth_++;
925 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700926 } while (cur_shadow_frame_ != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700927 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700928 if (include_transitions) {
929 bool should_continue = VisitFrame();
930 if (!should_continue) {
931 return;
932 }
933 }
Andreas Gampe585da952016-12-02 14:52:29 -0800934 if (kCount == CountTransitions::kYes) {
935 cur_depth_++;
936 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800937 }
938 if (num_frames_ != 0) {
939 CHECK_EQ(cur_depth_, num_frames_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700940 }
941}
942
Andreas Gampe585da952016-12-02 14:52:29 -0800943template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kYes>(bool);
944template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kNo>(bool);
945
Elliott Hughes68e76522011-10-05 13:22:16 -0700946} // namespace art