blob: 25939d2f2c602d6214ad70a171ec5b4f241a9b22 [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 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),
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -080071 context_(context),
72 check_suspended_(check_suspended) {
73 if (check_suspended_) {
74 DCHECK(thread == Thread::Current() || thread->IsSuspended()) << *thread;
75 }
Ian Rogers5cf98192014-05-29 21:31:50 -070076}
77
Mathieu Chartiere401d142015-04-22 13:56:20 -070078ArtMethod* StackVisitor::GetMethod() const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +010079 if (cur_shadow_frame_ != nullptr) {
80 return cur_shadow_frame_->GetMethod();
81 } else if (cur_quick_frame_ != nullptr) {
82 if (IsInInlinedFrame()) {
David Srbecky61b28a12016-02-25 21:55:03 +000083 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
David Srbecky8cd54542018-07-15 23:58:44 +010084 CodeInfo code_info(method_header);
Mathieu Chartier45bf2502016-03-31 11:07:09 -070085 DCHECK(walk_kind_ != StackWalkKind::kSkipInlinedFrames);
David Srbecky8cd54542018-07-15 23:58:44 +010086 return GetResolvedMethod(*GetCurrentQuickFrame(), code_info, current_inline_frames_);
Nicolas Geoffray57f61612015-05-15 13:20:41 +010087 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -070088 return *cur_quick_frame_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +010089 }
Nicolas Geoffray57f61612015-05-15 13:20:41 +010090 }
Mathieu Chartiere401d142015-04-22 13:56:20 -070091 return nullptr;
Nicolas Geoffray57f61612015-05-15 13:20:41 +010092}
93
Dave Allisonb373e092014-02-20 16:06:36 -080094uint32_t StackVisitor::GetDexPc(bool abort_on_failure) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070095 if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -070096 return cur_shadow_frame_->GetDexPC();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070097 } else if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray57f61612015-05-15 13:20:41 +010098 if (IsInInlinedFrame()) {
David Srbecky93bd3612018-07-02 19:30:18 +010099 return current_inline_frames_.back().GetDexPc();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100100 } else if (cur_oat_quick_method_header_ == nullptr) {
Andreas Gampee2abbc62017-09-15 11:59:26 -0700101 return dex::kDexNoIndex;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100102 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100103 return cur_oat_quick_method_header_->ToDexPc(
104 GetMethod(), cur_quick_frame_pc_, abort_on_failure);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100105 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700106 } else {
107 return 0;
108 }
109}
110
Mathieu Chartiere401d142015-04-22 13:56:20 -0700111extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700112 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertza836bc92014-11-25 16:30:53 +0100113
Ian Rogers62d6c772013-02-27 08:32:07 -0800114mirror::Object* StackVisitor::GetThisObject() const {
Andreas Gampe542451c2016-07-26 09:02:02 -0700115 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700116 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800117 if (m->IsStatic()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100118 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800119 } else if (m->IsNative()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100120 if (cur_quick_frame_ != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700121 HandleScope* hs = reinterpret_cast<HandleScope*>(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100122 reinterpret_cast<char*>(cur_quick_frame_) + sizeof(ArtMethod*));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700123 return hs->GetReference(0);
Ian Rogers62d6c772013-02-27 08:32:07 -0800124 } else {
125 return cur_shadow_frame_->GetVRegReference(0);
126 }
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000127 } else if (m->IsProxyMethod()) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100128 if (cur_quick_frame_ != nullptr) {
129 return artQuickGetProxyThisObject(cur_quick_frame_);
130 } else {
131 return cur_shadow_frame_->GetVRegReference(0);
132 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800133 } else {
David Sehr0225f8e2018-01-31 08:52:24 +0000134 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800135 if (!accessor.HasCodeItem()) {
Ian Rogerse0dcd462014-03-08 15:21:04 -0800136 UNIMPLEMENTED(ERROR) << "Failed to determine this object of abstract or proxy method: "
David Sehr709b0702016-10-13 09:12:37 -0700137 << ArtMethod::PrettyMethod(m);
Ian Rogerse0dcd462014-03-08 15:21:04 -0800138 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800139 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800140 uint16_t reg = accessor.RegistersSize() - accessor.InsSize();
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000141 uint32_t value = 0;
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100142 if (!GetVReg(m, reg, kReferenceVReg, &value)) {
143 return nullptr;
144 }
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000145 return reinterpret_cast<mirror::Object*>(value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800146 }
147 }
148}
149
Ian Rogers0c7abda2012-09-19 13:33:42 -0700150size_t StackVisitor::GetNativePcOffset() const {
151 DCHECK(!IsShadowFrame());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100152 return GetCurrentOatQuickMethodHeader()->NativeQuickPcOffset(cur_quick_frame_pc_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700153}
154
Mingyao Yang99170c62015-07-06 11:10:37 -0700155bool StackVisitor::GetVRegFromDebuggerShadowFrame(uint16_t vreg,
156 VRegKind kind,
157 uint32_t* val) const {
158 size_t frame_id = const_cast<StackVisitor*>(this)->GetFrameId();
159 ShadowFrame* shadow_frame = thread_->FindDebuggerShadowFrame(frame_id);
160 if (shadow_frame != nullptr) {
161 bool* updated_vreg_flags = thread_->GetUpdatedVRegFlags(frame_id);
162 DCHECK(updated_vreg_flags != nullptr);
163 if (updated_vreg_flags[vreg]) {
164 // Value is set by the debugger.
165 if (kind == kReferenceVReg) {
166 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
167 shadow_frame->GetVRegReference(vreg)));
168 } else {
169 *val = shadow_frame->GetVReg(vreg);
170 }
171 return true;
172 }
173 }
174 // No value is set by the debugger.
175 return false;
176}
177
Mathieu Chartiere401d142015-04-22 13:56:20 -0700178bool StackVisitor::GetVReg(ArtMethod* m, uint16_t vreg, VRegKind kind, uint32_t* val) const {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200179 if (cur_quick_frame_ != nullptr) {
180 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800181 DCHECK(m == GetMethod());
Mingyao Yang99170c62015-07-06 11:10:37 -0700182 // Check if there is value set by the debugger.
183 if (GetVRegFromDebuggerShadowFrame(vreg, kind, val)) {
184 return true;
185 }
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100186 DCHECK(cur_oat_quick_method_header_->IsOptimized());
187 return GetVRegFromOptimizedCode(m, vreg, kind, val);
Ian Rogers0399dde2012-06-06 17:09:28 -0700188 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100189 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertz09687442015-11-17 10:35:39 +0100190 if (kind == kReferenceVReg) {
191 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
192 cur_shadow_frame_->GetVRegReference(vreg)));
193 } else {
194 *val = cur_shadow_frame_->GetVReg(vreg);
195 }
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200196 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700197 }
198}
199
Mathieu Chartiere401d142015-04-22 13:56:20 -0700200bool StackVisitor::GetVRegFromOptimizedCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100201 uint32_t* val) const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100202 DCHECK_EQ(m, GetMethod());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800203 // Can't be null or how would we compile its instructions?
204 DCHECK(m->GetCodeItem() != nullptr) << m->PrettyMethod();
David Sehr0225f8e2018-01-31 08:52:24 +0000205 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800206 uint16_t number_of_dex_registers = accessor.RegistersSize();
207 DCHECK_LT(vreg, number_of_dex_registers);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100208 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
David Srbecky052f8ca2018-04-26 15:42:54 +0100209 CodeInfo code_info(method_header);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100210
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100211 uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_);
David Srbecky052f8ca2018-04-26 15:42:54 +0100212 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100213 DCHECK(stack_map.IsValid());
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100214
215 DexRegisterMap dex_register_map = IsInInlinedFrame()
David Srbecky93bd3612018-07-02 19:30:18 +0100216 ? code_info.GetInlineDexRegisterMapOf(stack_map, current_inline_frames_.back())
David Srbeckyfd89b072018-06-03 12:00:22 +0100217 : code_info.GetDexRegisterMapOf(stack_map);
218 if (dex_register_map.empty()) {
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000219 return false;
220 }
David Srbeckyfd89b072018-06-03 12:00:22 +0100221 DCHECK_EQ(dex_register_map.size(), number_of_dex_registers);
David Srbeckye1402122018-06-13 18:20:45 +0100222 DexRegisterLocation::Kind location_kind = dex_register_map[vreg].GetKind();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100223 switch (location_kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000224 case DexRegisterLocation::Kind::kInStack: {
David Srbeckye1402122018-06-13 18:20:45 +0100225 const int32_t offset = dex_register_map[vreg].GetStackOffsetInBytes();
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100226 BitMemoryRegion stack_mask = code_info.GetStackMaskOf(stack_map);
227 if (kind == kReferenceVReg && !stack_mask.LoadBit(offset / kFrameSlotSize)) {
228 return false;
229 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100230 const uint8_t* addr = reinterpret_cast<const uint8_t*>(cur_quick_frame_) + offset;
231 *val = *reinterpret_cast<const uint32_t*>(addr);
232 return true;
233 }
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100234 case DexRegisterLocation::Kind::kInRegister: {
235 uint32_t register_mask = code_info.GetRegisterMaskOf(stack_map);
236 uint32_t reg = dex_register_map[vreg].GetMachineRegister();
237 if (kind == kReferenceVReg && !(register_mask & (1 << reg))) {
238 return false;
239 }
240 return GetRegisterIfAccessible(reg, kind, val);
241 }
David Brazdild9cb68e2015-08-25 13:52:43 +0100242 case DexRegisterLocation::Kind::kInRegisterHigh:
243 case DexRegisterLocation::Kind::kInFpuRegister:
244 case DexRegisterLocation::Kind::kInFpuRegisterHigh: {
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100245 if (kind == kReferenceVReg) {
246 return false;
247 }
David Srbeckye1402122018-06-13 18:20:45 +0100248 uint32_t reg = dex_register_map[vreg].GetMachineRegister();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100249 return GetRegisterIfAccessible(reg, kind, val);
250 }
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100251 case DexRegisterLocation::Kind::kConstant: {
252 uint32_t result = dex_register_map[vreg].GetConstant();
253 if (kind == kReferenceVReg && result != 0) {
254 return false;
255 }
256 *val = result;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100257 return true;
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100258 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000259 case DexRegisterLocation::Kind::kNone:
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100260 return false;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000261 default:
David Srbeckye1402122018-06-13 18:20:45 +0100262 LOG(FATAL) << "Unexpected location kind " << dex_register_map[vreg].GetKind();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000263 UNREACHABLE();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100264 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100265}
266
267bool StackVisitor::GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const {
268 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
David Brazdil77a48ae2015-09-15 12:34:04 +0000269
Vladimir Marko239d6ea2016-09-05 10:44:04 +0100270 if (kRuntimeISA == InstructionSet::kX86 && is_float) {
271 // X86 float registers are 64-bit and each XMM register is provided as two separate
272 // 32-bit registers by the context.
273 reg = (kind == kDoubleHiVReg) ? (2 * reg + 1) : (2 * reg);
274 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000275
Goran Jakovljevic986660c2015-12-10 11:44:50 +0100276 // MIPS32 float registers are used as 64-bit (for MIPS32r2 it is pair
277 // F(2n)-F(2n+1), and for MIPS32r6 it is 64-bit register F(2n)). When
278 // accessing upper 32-bits from double, reg + 1 should be used.
279 if ((kRuntimeISA == InstructionSet::kMips) && (kind == kDoubleHiVReg)) {
280 DCHECK_ALIGNED(reg, 2);
281 reg++;
282 }
283
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100284 if (!IsAccessibleRegister(reg, is_float)) {
285 return false;
286 }
287 uintptr_t ptr_val = GetRegister(reg, is_float);
288 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
289 if (target64) {
290 const bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
291 const bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
292 int64_t value_long = static_cast<int64_t>(ptr_val);
293 if (wide_lo) {
294 ptr_val = static_cast<uintptr_t>(Low32Bits(value_long));
295 } else if (wide_hi) {
296 ptr_val = static_cast<uintptr_t>(High32Bits(value_long));
297 }
298 }
299 *val = ptr_val;
300 return true;
301}
302
Mingyao Yang99170c62015-07-06 11:10:37 -0700303bool StackVisitor::GetVRegPairFromDebuggerShadowFrame(uint16_t vreg,
304 VRegKind kind_lo,
305 VRegKind kind_hi,
306 uint64_t* val) const {
307 uint32_t low_32bits;
308 uint32_t high_32bits;
309 bool success = GetVRegFromDebuggerShadowFrame(vreg, kind_lo, &low_32bits);
310 success &= GetVRegFromDebuggerShadowFrame(vreg + 1, kind_hi, &high_32bits);
311 if (success) {
312 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
313 }
314 return success;
315}
316
Mathieu Chartiere401d142015-04-22 13:56:20 -0700317bool StackVisitor::GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200318 VRegKind kind_hi, uint64_t* val) const {
319 if (kind_lo == kLongLoVReg) {
320 DCHECK_EQ(kind_hi, kLongHiVReg);
321 } else if (kind_lo == kDoubleLoVReg) {
322 DCHECK_EQ(kind_hi, kDoubleHiVReg);
323 } else {
324 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100325 UNREACHABLE();
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200326 }
Mingyao Yang99170c62015-07-06 11:10:37 -0700327 // Check if there is value set by the debugger.
328 if (GetVRegPairFromDebuggerShadowFrame(vreg, kind_lo, kind_hi, val)) {
329 return true;
330 }
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200331 if (cur_quick_frame_ != nullptr) {
332 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
333 DCHECK(m == GetMethod());
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100334 DCHECK(cur_oat_quick_method_header_->IsOptimized());
335 return GetVRegPairFromOptimizedCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200336 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100337 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200338 *val = cur_shadow_frame_->GetVRegLong(vreg);
339 return true;
340 }
341}
342
Mathieu Chartiere401d142015-04-22 13:56:20 -0700343bool StackVisitor::GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100344 VRegKind kind_lo, VRegKind kind_hi,
345 uint64_t* val) const {
346 uint32_t low_32bits;
347 uint32_t high_32bits;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700348 bool success = GetVRegFromOptimizedCode(m, vreg, kind_lo, &low_32bits);
349 success &= GetVRegFromOptimizedCode(m, vreg + 1, kind_hi, &high_32bits);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100350 if (success) {
351 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
352 }
353 return success;
354}
355
356bool StackVisitor::GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
357 VRegKind kind_lo, uint64_t* val) const {
358 const bool is_float = (kind_lo == kDoubleLoVReg);
359 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
360 return false;
361 }
362 uintptr_t ptr_val_lo = GetRegister(reg_lo, is_float);
363 uintptr_t ptr_val_hi = GetRegister(reg_hi, is_float);
364 bool target64 = Is64BitInstructionSet(kRuntimeISA);
365 if (target64) {
366 int64_t value_long_lo = static_cast<int64_t>(ptr_val_lo);
367 int64_t value_long_hi = static_cast<int64_t>(ptr_val_hi);
368 ptr_val_lo = static_cast<uintptr_t>(Low32Bits(value_long_lo));
369 ptr_val_hi = static_cast<uintptr_t>(High32Bits(value_long_hi));
370 }
371 *val = (static_cast<uint64_t>(ptr_val_hi) << 32) | static_cast<uint32_t>(ptr_val_lo);
372 return true;
373}
374
Mingyao Yang636b9252015-07-31 16:40:24 -0700375bool StackVisitor::SetVReg(ArtMethod* m,
376 uint16_t vreg,
377 uint32_t new_value,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800378 VRegKind kind) {
David Sehr0225f8e2018-01-31 08:52:24 +0000379 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800380 if (!accessor.HasCodeItem()) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700381 return false;
382 }
383 ShadowFrame* shadow_frame = GetCurrentShadowFrame();
384 if (shadow_frame == nullptr) {
385 // This is a compiled frame: we must prepare and update a shadow frame that will
386 // be executed by the interpreter after deoptimization of the stack.
387 const size_t frame_id = GetFrameId();
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800388 const uint16_t num_regs = accessor.RegistersSize();
Mingyao Yang99170c62015-07-06 11:10:37 -0700389 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
390 CHECK(shadow_frame != nullptr);
391 // Remember the vreg has been set for debugging and must not be overwritten by the
392 // original value during deoptimization of the stack.
393 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
394 }
395 if (kind == kReferenceVReg) {
396 shadow_frame->SetVRegReference(vreg, reinterpret_cast<mirror::Object*>(new_value));
397 } else {
398 shadow_frame->SetVReg(vreg, new_value);
399 }
400 return true;
401}
402
Mingyao Yang636b9252015-07-31 16:40:24 -0700403bool StackVisitor::SetVRegPair(ArtMethod* m,
404 uint16_t vreg,
405 uint64_t new_value,
406 VRegKind kind_lo,
407 VRegKind kind_hi) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700408 if (kind_lo == kLongLoVReg) {
409 DCHECK_EQ(kind_hi, kLongHiVReg);
410 } else if (kind_lo == kDoubleLoVReg) {
411 DCHECK_EQ(kind_hi, kDoubleHiVReg);
412 } else {
413 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
414 UNREACHABLE();
415 }
David Sehr0225f8e2018-01-31 08:52:24 +0000416 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800417 if (!accessor.HasCodeItem()) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700418 return false;
419 }
420 ShadowFrame* shadow_frame = GetCurrentShadowFrame();
421 if (shadow_frame == nullptr) {
422 // This is a compiled frame: we must prepare for deoptimization (see SetVRegFromDebugger).
423 const size_t frame_id = GetFrameId();
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800424 const uint16_t num_regs = accessor.RegistersSize();
Mingyao Yang99170c62015-07-06 11:10:37 -0700425 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
426 CHECK(shadow_frame != nullptr);
427 // Remember the vreg pair has been set for debugging and must not be overwritten by the
428 // original value during deoptimization of the stack.
429 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
430 thread_->GetUpdatedVRegFlags(frame_id)[vreg + 1] = true;
431 }
432 shadow_frame->SetVRegLong(vreg, new_value);
433 return true;
434}
435
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100436bool StackVisitor::IsAccessibleGPR(uint32_t reg) const {
437 DCHECK(context_ != nullptr);
438 return context_->IsAccessibleGPR(reg);
439}
440
Mathieu Chartier815873e2014-02-13 18:02:13 -0800441uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100442 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
443 DCHECK(context_ != nullptr);
Mathieu Chartier815873e2014-02-13 18:02:13 -0800444 return context_->GetGPRAddress(reg);
445}
446
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100447uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
448 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
449 DCHECK(context_ != nullptr);
450 return context_->GetGPR(reg);
Ian Rogers0399dde2012-06-06 17:09:28 -0700451}
452
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100453bool StackVisitor::IsAccessibleFPR(uint32_t reg) const {
454 DCHECK(context_ != nullptr);
455 return context_->IsAccessibleFPR(reg);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200456}
457
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100458uintptr_t StackVisitor::GetFPR(uint32_t reg) const {
459 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
460 DCHECK(context_ != nullptr);
461 return context_->GetFPR(reg);
462}
463
Ian Rogers0399dde2012-06-06 17:09:28 -0700464uintptr_t StackVisitor::GetReturnPc() const {
Ian Rogers13735952014-10-08 12:43:28 -0700465 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700466 DCHECK(sp != nullptr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100467 uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogers0399dde2012-06-06 17:09:28 -0700468 return *reinterpret_cast<uintptr_t*>(pc_addr);
469}
470
471void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
Ian Rogers13735952014-10-08 12:43:28 -0700472 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700473 CHECK(sp != nullptr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100474 uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogers0399dde2012-06-06 17:09:28 -0700475 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc;
476}
477
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100478size_t StackVisitor::ComputeNumFrames(Thread* thread, StackWalkKind walk_kind) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700479 struct NumFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100480 NumFramesVisitor(Thread* thread_in, StackWalkKind walk_kind_in)
481 : StackVisitor(thread_in, nullptr, walk_kind_in), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700482
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100483 bool VisitFrame() override {
Ian Rogers0399dde2012-06-06 17:09:28 -0700484 frames++;
485 return true;
486 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700487
Ian Rogers0399dde2012-06-06 17:09:28 -0700488 size_t frames;
489 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100490 NumFramesVisitor visitor(thread, walk_kind);
Ian Rogers0399dde2012-06-06 17:09:28 -0700491 visitor.WalkStack(true);
492 return visitor.frames;
493}
494
Mathieu Chartiere401d142015-04-22 13:56:20 -0700495bool StackVisitor::GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700496 struct HasMoreFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100497 HasMoreFramesVisitor(Thread* thread,
498 StackWalkKind walk_kind,
499 size_t num_frames,
500 size_t frame_height)
501 : StackVisitor(thread, nullptr, walk_kind, num_frames),
502 frame_height_(frame_height),
503 found_frame_(false),
504 has_more_frames_(false),
505 next_method_(nullptr),
506 next_dex_pc_(0) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700507 }
508
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100509 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700510 if (found_frame_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700511 ArtMethod* method = GetMethod();
Ian Rogers5cf98192014-05-29 21:31:50 -0700512 if (method != nullptr && !method->IsRuntimeMethod()) {
513 has_more_frames_ = true;
514 next_method_ = method;
515 next_dex_pc_ = GetDexPc();
516 return false; // End stack walk once next method is found.
517 }
518 } else if (GetFrameHeight() == frame_height_) {
519 found_frame_ = true;
520 }
521 return true;
522 }
523
524 size_t frame_height_;
525 bool found_frame_;
526 bool has_more_frames_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700527 ArtMethod* next_method_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700528 uint32_t next_dex_pc_;
529 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100530 HasMoreFramesVisitor visitor(thread_, walk_kind_, GetNumFrames(), GetFrameHeight());
Ian Rogers5cf98192014-05-29 21:31:50 -0700531 visitor.WalkStack(true);
532 *next_method = visitor.next_method_;
533 *next_dex_pc = visitor.next_dex_pc_;
534 return visitor.has_more_frames_;
535}
536
Ian Rogers7a22fa62013-01-23 12:16:16 -0800537void StackVisitor::DescribeStack(Thread* thread) {
Ian Rogers306057f2012-11-26 12:45:53 -0800538 struct DescribeStackVisitor : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800539 explicit DescribeStackVisitor(Thread* thread_in)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100540 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800541
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100542 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers306057f2012-11-26 12:45:53 -0800543 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation();
544 return true;
545 }
546 };
Ian Rogers7a22fa62013-01-23 12:16:16 -0800547 DescribeStackVisitor visitor(thread);
Ian Rogers306057f2012-11-26 12:45:53 -0800548 visitor.WalkStack(true);
549}
550
Ian Rogers40e3bac2012-11-20 00:09:14 -0800551std::string StackVisitor::DescribeLocation() const {
552 std::string result("Visiting method '");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700553 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700554 if (m == nullptr) {
Ian Rogers306057f2012-11-26 12:45:53 -0800555 return "upcall";
556 }
David Sehr709b0702016-10-13 09:12:37 -0700557 result += m->PrettyMethod();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800558 result += StringPrintf("' at dex PC 0x%04x", GetDexPc());
Ian Rogers40e3bac2012-11-20 00:09:14 -0800559 if (!IsShadowFrame()) {
560 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc()));
561 }
562 return result;
563}
564
Alex Lightdba61482016-12-21 08:20:29 -0800565void StackVisitor::SetMethod(ArtMethod* method) {
566 DCHECK(GetMethod() != nullptr);
567 if (cur_shadow_frame_ != nullptr) {
568 cur_shadow_frame_->SetMethod(method);
569 } else {
570 DCHECK(cur_quick_frame_ != nullptr);
571 CHECK(!IsInInlinedFrame()) << "We do not support setting inlined method's ArtMethod!";
Alex Light1ebe4fe2017-01-30 14:57:11 -0800572 *cur_quick_frame_ = method;
Alex Lightdba61482016-12-21 08:20:29 -0800573 }
574}
575
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100576static void AssertPcIsWithinQuickCode(ArtMethod* method, uintptr_t pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700577 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100578 if (method->IsNative() || method->IsRuntimeMethod() || method->IsProxyMethod()) {
579 return;
580 }
581
582 if (pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) {
583 return;
584 }
585
Mingyao Yang88ca8ba2017-05-23 14:21:07 -0700586 Runtime* runtime = Runtime::Current();
587 if (runtime->UseJitCompilation() &&
588 runtime->GetJit()->GetCodeCache()->ContainsPc(reinterpret_cast<const void*>(pc))) {
589 return;
590 }
591
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100592 const void* code = method->GetEntryPointFromQuickCompiledCode();
Alex Lightdb01a092017-04-03 15:39:55 -0700593 if (code == GetQuickInstrumentationEntryPoint() || code == GetInvokeObsoleteMethodStub()) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100594 return;
595 }
596
597 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
598 if (class_linker->IsQuickToInterpreterBridge(code) ||
599 class_linker->IsQuickResolutionStub(code)) {
600 return;
601 }
602
Calin Juravleffc87072016-04-20 14:22:09 +0100603 if (runtime->UseJitCompilation() && runtime->GetJit()->GetCodeCache()->ContainsPc(code)) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100604 return;
605 }
606
Mingyao Yang063fc772016-08-02 11:02:54 -0700607 uint32_t code_size = OatQuickMethodHeader::FromEntryPoint(code)->GetCodeSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100608 uintptr_t code_start = reinterpret_cast<uintptr_t>(code);
609 CHECK(code_start <= pc && pc <= (code_start + code_size))
David Sehr709b0702016-10-13 09:12:37 -0700610 << method->PrettyMethod()
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100611 << " pc=" << std::hex << pc
Roland Levillain0d5a2812015-11-13 10:07:31 +0000612 << " code_start=" << code_start
613 << " code_size=" << code_size;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100614}
615
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700616void StackVisitor::SanityCheckFrame() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800617 if (kIsDebugBuild) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700618 ArtMethod* method = GetMethod();
Vladimir Markod93e3742018-07-18 10:58:13 +0100619 ObjPtr<mirror::Class> declaring_class = method->GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700620 // Runtime methods have null declaring class.
621 if (!method->IsRuntimeMethod()) {
622 CHECK(declaring_class != nullptr);
623 CHECK_EQ(declaring_class->GetClass(), declaring_class->GetClass()->GetClass())
624 << declaring_class;
625 } else {
626 CHECK(declaring_class == nullptr);
627 }
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700628 Runtime* const runtime = Runtime::Current();
629 LinearAlloc* const linear_alloc = runtime->GetLinearAlloc();
630 if (!linear_alloc->Contains(method)) {
631 // Check class linker linear allocs.
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100632 // We get the canonical method as copied methods may have their declaring
633 // class from another class loader.
634 ArtMethod* canonical = method->GetCanonicalMethod();
Vladimir Markod93e3742018-07-18 10:58:13 +0100635 ObjPtr<mirror::Class> klass = canonical->GetDeclaringClass();
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700636 LinearAlloc* const class_linear_alloc = (klass != nullptr)
Mathieu Chartier5b830502016-03-02 10:30:23 -0800637 ? runtime->GetClassLinker()->GetAllocatorForClassLoader(klass->GetClassLoader())
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700638 : linear_alloc;
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100639 if (!class_linear_alloc->Contains(canonical)) {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700640 // Check image space.
641 bool in_image = false;
642 for (auto& space : runtime->GetHeap()->GetContinuousSpaces()) {
643 if (space->IsImageSpace()) {
644 auto* image_space = space->AsImageSpace();
645 const auto& header = image_space->GetImageHeader();
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700646 const ImageSection& methods = header.GetMethodsSection();
647 const ImageSection& runtime_methods = header.GetRuntimeMethodsSection();
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100648 const size_t offset = reinterpret_cast<const uint8_t*>(canonical) - image_space->Begin();
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700649 if (methods.Contains(offset) || runtime_methods.Contains(offset)) {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700650 in_image = true;
651 break;
652 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700653 }
654 }
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100655 CHECK(in_image) << canonical->PrettyMethod() << " not in linear alloc or image";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700656 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700657 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800658 if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100659 AssertPcIsWithinQuickCode(method, cur_quick_frame_pc_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800660 // Frame sanity.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100661 size_t frame_size = GetCurrentQuickFrameInfo().FrameSizeInBytes();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800662 CHECK_NE(frame_size, 0u);
Andreas Gampe5b417b92014-03-10 14:18:35 -0700663 // A rough guess at an upper size we expect to see for a frame.
664 // 256 registers
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700665 // 2 words HandleScope overhead
Andreas Gampe5b417b92014-03-10 14:18:35 -0700666 // 3+3 register spills
667 // TODO: this seems architecture specific for the case of JNI frames.
Brian Carlstromed08bd42014-03-19 18:34:17 -0700668 // TODO: 083-compiler-regressions ManyFloatArgs shows this estimate is wrong.
669 // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word);
670 const size_t kMaxExpectedFrameSize = 2 * KB;
David Sehr709b0702016-10-13 09:12:37 -0700671 CHECK_LE(frame_size, kMaxExpectedFrameSize) << method->PrettyMethod();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100672 size_t return_pc_offset = GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800673 CHECK_LT(return_pc_offset, frame_size);
674 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700675 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700676}
677
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100678// Counts the number of references in the parameter list of the corresponding method.
679// Note: Thus does _not_ include "this" for non-static methods.
680static uint32_t GetNumberOfReferenceArgsWithoutReceiver(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700681 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100682 uint32_t shorty_len;
683 const char* shorty = method->GetShorty(&shorty_len);
684 uint32_t refs = 0;
685 for (uint32_t i = 1; i < shorty_len ; ++i) {
686 if (shorty[i] == 'L') {
687 refs++;
688 }
689 }
690 return refs;
691}
692
693QuickMethodFrameInfo StackVisitor::GetCurrentQuickFrameInfo() const {
694 if (cur_oat_quick_method_header_ != nullptr) {
695 return cur_oat_quick_method_header_->GetFrameInfo();
696 }
697
698 ArtMethod* method = GetMethod();
699 Runtime* runtime = Runtime::Current();
700
701 if (method->IsAbstract()) {
Vladimir Markod3083dd2018-05-17 08:43:47 +0100702 return RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100703 }
704
705 // This goes before IsProxyMethod since runtime methods have a null declaring class.
706 if (method->IsRuntimeMethod()) {
707 return runtime->GetRuntimeMethodFrameInfo(method);
708 }
709
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100710 if (method->IsProxyMethod()) {
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000711 // There is only one direct method of a proxy class: the constructor. A direct method is
712 // cloned from the original java.lang.reflect.Proxy and is executed as usual quick
713 // compiled method without any stubs. Therefore the method must have a OatQuickMethodHeader.
714 DCHECK(!method->IsDirect() && !method->IsConstructor())
715 << "Constructors of proxy classes must have a OatQuickMethodHeader";
Vladimir Markod3083dd2018-05-17 08:43:47 +0100716 return RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100717 }
718
Vladimir Marko2196c652017-11-30 16:16:07 +0000719 // The only remaining case is if the method is native and uses the generic JNI stub,
720 // called either directly or through some (resolution, instrumentation) trampoline.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100721 DCHECK(method->IsNative());
Vladimir Marko2196c652017-11-30 16:16:07 +0000722 if (kIsDebugBuild) {
723 ClassLinker* class_linker = runtime->GetClassLinker();
724 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(method,
725 kRuntimePointerSize);
726 CHECK(class_linker->IsQuickGenericJniStub(entry_point) ||
727 // The current entrypoint (after filtering out trampolines) may have changed
728 // from GenericJNI to JIT-compiled stub since we have entered this frame.
729 (runtime->GetJit() != nullptr &&
730 runtime->GetJit()->GetCodeCache()->ContainsPc(entry_point))) << method->PrettyMethod();
731 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100732 // Generic JNI frame.
733 uint32_t handle_refs = GetNumberOfReferenceArgsWithoutReceiver(method) + 1;
734 size_t scope_size = HandleScope::SizeOf(handle_refs);
Vladimir Markod3083dd2018-05-17 08:43:47 +0100735 constexpr QuickMethodFrameInfo callee_info =
736 RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100737
738 // Callee saves + handle scope + method ref + alignment
739 // Note: -sizeof(void*) since callee-save frame stores a whole method pointer.
740 size_t frame_size = RoundUp(
741 callee_info.FrameSizeInBytes() - sizeof(void*) + sizeof(ArtMethod*) + scope_size,
742 kStackAlignment);
743 return QuickMethodFrameInfo(frame_size, callee_info.CoreSpillMask(), callee_info.FpSpillMask());
744}
745
Andreas Gampe585da952016-12-02 14:52:29 -0800746template <StackVisitor::CountTransitions kCount>
Ian Rogers0399dde2012-06-06 17:09:28 -0700747void StackVisitor::WalkStack(bool include_transitions) {
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -0800748 if (check_suspended_) {
749 DCHECK(thread_ == Thread::Current() || thread_->IsSuspended());
750 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800751 CHECK_EQ(cur_depth_, 0U);
752 bool exit_stubs_installed = Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled();
Alex Lightb81a9842016-12-15 00:59:05 +0000753 uint32_t instrumentation_stack_depth = 0;
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000754 size_t inlined_frames_count = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700755
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700756 for (const ManagedStack* current_fragment = thread_->GetManagedStack();
757 current_fragment != nullptr; current_fragment = current_fragment->GetLink()) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700758 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
759 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700760 cur_quick_frame_pc_ = 0;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100761 cur_oat_quick_method_header_ = nullptr;
Dave Allisonf9439142014-03-27 15:10:22 -0700762
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700763 if (cur_quick_frame_ != nullptr) { // Handle quick stack frames.
Ian Rogers0399dde2012-06-06 17:09:28 -0700764 // Can't be both a shadow and a quick fragment.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700765 DCHECK(current_fragment->GetTopShadowFrame() == nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700766 ArtMethod* method = *cur_quick_frame_;
Vladimir Marko2196c652017-11-30 16:16:07 +0000767 DCHECK(method != nullptr);
768 bool header_retrieved = false;
769 if (method->IsNative()) {
770 // We do not have a PC for the first frame, so we cannot simply use
771 // ArtMethod::GetOatQuickMethodHeader() as we're unable to distinguish there
772 // between GenericJNI frame and JIT-compiled JNI stub; the entrypoint may have
773 // changed since the frame was entered. The top quick frame tag indicates
774 // GenericJNI here, otherwise it's either AOT-compiled or JNI-compiled JNI stub.
775 if (UNLIKELY(current_fragment->GetTopQuickFrameTag())) {
776 // The generic JNI does not have any method header.
777 cur_oat_quick_method_header_ = nullptr;
778 } else {
779 const void* existing_entry_point = method->GetEntryPointFromQuickCompiledCode();
780 CHECK(existing_entry_point != nullptr);
781 Runtime* runtime = Runtime::Current();
782 ClassLinker* class_linker = runtime->GetClassLinker();
783 // Check whether we can quickly get the header from the current entrypoint.
784 if (!class_linker->IsQuickGenericJniStub(existing_entry_point) &&
785 !class_linker->IsQuickResolutionStub(existing_entry_point) &&
786 existing_entry_point != GetQuickInstrumentationEntryPoint()) {
787 cur_oat_quick_method_header_ =
788 OatQuickMethodHeader::FromEntryPoint(existing_entry_point);
789 } else {
790 const void* code = method->GetOatMethodQuickCode(class_linker->GetImagePointerSize());
791 if (code != nullptr) {
792 cur_oat_quick_method_header_ = OatQuickMethodHeader::FromEntryPoint(code);
793 } else {
794 // This must be a JITted JNI stub frame.
795 CHECK(runtime->GetJit() != nullptr);
796 code = runtime->GetJit()->GetCodeCache()->GetJniStubCode(method);
797 CHECK(code != nullptr) << method->PrettyMethod();
798 cur_oat_quick_method_header_ = OatQuickMethodHeader::FromCodePointer(code);
799 }
800 }
801 }
802 header_retrieved = true;
803 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700804 while (method != nullptr) {
Vladimir Marko2196c652017-11-30 16:16:07 +0000805 if (!header_retrieved) {
806 cur_oat_quick_method_header_ = method->GetOatQuickMethodHeader(cur_quick_frame_pc_);
807 }
808 header_retrieved = false; // Force header retrieval in next iteration.
Dave Allison5cd33752014-04-15 15:57:58 -0700809 SanityCheckFrame();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100810
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100811 if ((walk_kind_ == StackWalkKind::kIncludeInlinedFrames)
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100812 && (cur_oat_quick_method_header_ != nullptr)
David Srbeckyafc97bc2018-07-05 08:14:35 +0000813 && cur_oat_quick_method_header_->IsOptimized()
814 // JNI methods cannot have any inlined frames.
815 && !method->IsNative()) {
816 DCHECK_NE(cur_quick_frame_pc_, 0u);
David Srbecky6ee06e92018-07-25 21:45:54 +0100817 CodeInfo code_info(cur_oat_quick_method_header_, CodeInfo::DecodeFlags::InlineInfoOnly);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100818 uint32_t native_pc_offset =
819 cur_oat_quick_method_header_->NativeQuickPcOffset(cur_quick_frame_pc_);
David Srbecky052f8ca2018-04-26 15:42:54 +0100820 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
821 if (stack_map.IsValid() && stack_map.HasInlineInfo()) {
David Srbecky93bd3612018-07-02 19:30:18 +0100822 DCHECK_EQ(current_inline_frames_.size(), 0u);
823 for (current_inline_frames_ = code_info.GetInlineInfosOf(stack_map);
824 !current_inline_frames_.empty();
825 current_inline_frames_.pop_back()) {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100826 bool should_continue = VisitFrame();
827 if (UNLIKELY(!should_continue)) {
828 return;
829 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100830 cur_depth_++;
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000831 inlined_frames_count++;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100832 }
833 }
834 }
835
Dave Allison5cd33752014-04-15 15:57:58 -0700836 bool should_continue = VisitFrame();
837 if (UNLIKELY(!should_continue)) {
838 return;
Ian Rogers0399dde2012-06-06 17:09:28 -0700839 }
Dave Allison5cd33752014-04-15 15:57:58 -0700840
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100841 QuickMethodFrameInfo frame_info = GetCurrentQuickFrameInfo();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700842 if (context_ != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100843 context_->FillCalleeSaves(reinterpret_cast<uint8_t*>(cur_quick_frame_), frame_info);
Ian Rogers0399dde2012-06-06 17:09:28 -0700844 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700845 // Compute PC for next stack frame from return PC.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100846 size_t frame_size = frame_info.FrameSizeInBytes();
847 size_t return_pc_offset = frame_size - sizeof(void*);
Ian Rogers13735952014-10-08 12:43:28 -0700848 uint8_t* return_pc_addr = reinterpret_cast<uint8_t*>(cur_quick_frame_) + return_pc_offset;
Ian Rogers0399dde2012-06-06 17:09:28 -0700849 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100850
Alex Lightb32c6a92018-06-07 16:48:04 -0700851 if (UNLIKELY(exit_stubs_installed ||
852 reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc)) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700853 // While profiling, the return pc is restored from the side stack, except when walking
854 // the stack for an exception where the side stack will be unwound in VisitFrame.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700855 if (reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc) {
Andreas Gampe585da952016-12-02 14:52:29 -0800856 CHECK_LT(instrumentation_stack_depth, thread_->GetInstrumentationStack()->size());
Sebastien Hertz74e256b2013-10-04 10:40:37 +0200857 const instrumentation::InstrumentationStackFrame& instrumentation_frame =
Vladimir Marko35d5b8a2018-07-03 09:18:32 +0100858 (*thread_->GetInstrumentationStack())[instrumentation_stack_depth];
jeffhao725a9572012-11-13 18:20:12 -0800859 instrumentation_stack_depth++;
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100860 if (GetMethod() ==
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700861 Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveAllCalleeSaves)) {
Jeff Haofb2802d2013-07-24 13:53:05 -0700862 // Skip runtime save all callee frames which are used to deliver exceptions.
863 } else if (instrumentation_frame.interpreter_entry_) {
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100864 ArtMethod* callee =
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700865 Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs);
David Sehr709b0702016-10-13 09:12:37 -0700866 CHECK_EQ(GetMethod(), callee) << "Expected: " << ArtMethod::PrettyMethod(callee)
867 << " Found: " << ArtMethod::PrettyMethod(GetMethod());
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000868 } else {
Alex Lighteee0bd42017-02-14 15:31:45 +0000869 // Instrumentation generally doesn't distinguish between a method's obsolete and
870 // non-obsolete version.
871 CHECK_EQ(instrumentation_frame.method_->GetNonObsoleteMethod(),
872 GetMethod()->GetNonObsoleteMethod())
873 << "Expected: "
874 << ArtMethod::PrettyMethod(instrumentation_frame.method_->GetNonObsoleteMethod())
875 << " Found: " << ArtMethod::PrettyMethod(GetMethod()->GetNonObsoleteMethod());
Ian Rogers62d6c772013-02-27 08:32:07 -0800876 }
877 if (num_frames_ != 0) {
878 // Check agreement of frame Ids only if num_frames_ is computed to avoid infinite
879 // recursion.
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000880 size_t frame_id = instrumentation::Instrumentation::ComputeFrameId(
881 thread_,
882 cur_depth_,
883 inlined_frames_count);
884 CHECK_EQ(instrumentation_frame.frame_id_, frame_id);
Ian Rogers62d6c772013-02-27 08:32:07 -0800885 }
jeffhao725a9572012-11-13 18:20:12 -0800886 return_pc = instrumentation_frame.return_pc_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700887 }
888 }
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100889
Ian Rogers0399dde2012-06-06 17:09:28 -0700890 cur_quick_frame_pc_ = return_pc;
Ian Rogers13735952014-10-08 12:43:28 -0700891 uint8_t* next_frame = reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700892 cur_quick_frame_ = reinterpret_cast<ArtMethod**>(next_frame);
893
894 if (kDebugStackWalk) {
David Sehr709b0702016-10-13 09:12:37 -0700895 LOG(INFO) << ArtMethod::PrettyMethod(method) << "@" << method << " size=" << frame_size
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100896 << std::boolalpha
897 << " optimized=" << (cur_oat_quick_method_header_ != nullptr &&
898 cur_oat_quick_method_header_->IsOptimized())
Mathieu Chartiere401d142015-04-22 13:56:20 -0700899 << " native=" << method->IsNative()
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100900 << std::noboolalpha
Mathieu Chartiere401d142015-04-22 13:56:20 -0700901 << " entrypoints=" << method->GetEntryPointFromQuickCompiledCode()
Alex Lighteee0bd42017-02-14 15:31:45 +0000902 << "," << (method->IsNative() ? method->GetEntryPointFromJni() : nullptr)
Mathieu Chartiere401d142015-04-22 13:56:20 -0700903 << " next=" << *cur_quick_frame_;
904 }
905
Andreas Gampef040be62017-04-14 21:49:33 -0700906 if (kCount == CountTransitions::kYes || !method->IsRuntimeMethod()) {
907 cur_depth_++;
908 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700909 method = *cur_quick_frame_;
jeffhao6641ea12013-01-02 18:13:42 -0800910 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700911 } else if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700912 do {
913 SanityCheckFrame();
914 bool should_continue = VisitFrame();
915 if (UNLIKELY(!should_continue)) {
916 return;
917 }
918 cur_depth_++;
919 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700920 } while (cur_shadow_frame_ != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700921 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700922 if (include_transitions) {
923 bool should_continue = VisitFrame();
924 if (!should_continue) {
925 return;
926 }
927 }
Andreas Gampe585da952016-12-02 14:52:29 -0800928 if (kCount == CountTransitions::kYes) {
929 cur_depth_++;
930 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800931 }
932 if (num_frames_ != 0) {
933 CHECK_EQ(cur_depth_, num_frames_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700934 }
935}
936
Andreas Gampe585da952016-12-02 14:52:29 -0800937template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kYes>(bool);
938template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kNo>(bool);
939
Elliott Hughes68e76522011-10-05 13:22:16 -0700940} // namespace art