blob: bc8f8cc6489fc70009db910b486322d1c7b17e01 [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"
Alex Lightb096c912019-09-25 13:33:06 -070018#include <limits>
Elliott Hughes68e76522011-10-05 13:22:16 -070019
Andreas Gampe46ee31b2016-12-14 10:11:49 -080020#include "android-base/stringprintf.h"
21
Ian Rogerse63db272014-07-15 15:36:11 -070022#include "arch/context.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070023#include "art_method-inl.h"
Alex Light721e4022020-01-14 14:45:40 -080024#include "base/aborting.h"
Andreas Gampe8228cdf2017-05-30 15:03:54 -070025#include "base/callee_save_type.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070026#include "base/enums.h"
Dave Allisonf9439142014-03-27 15:10:22 -070027#include "base/hex_dump.h"
Alex Light721e4022020-01-14 14:45:40 -080028#include "base/mutex.h"
David Sehr9e734c72018-01-04 17:56:19 -080029#include "dex/dex_file_types.h"
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010030#include "entrypoints/entrypoint_utils-inl.h"
Vladimir Markod3083dd2018-05-17 08:43:47 +010031#include "entrypoints/quick/callee_save_frame.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070032#include "entrypoints/runtime_asm_entrypoints.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070033#include "gc/space/image_space.h"
34#include "gc/space/space-inl.h"
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +010035#include "interpreter/shadow_frame-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010036#include "jit/jit.h"
37#include "jit/jit_code_cache.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070038#include "linear_alloc.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070039#include "managed_stack.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070040#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041#include "mirror/object-inl.h"
42#include "mirror/object_array-inl.h"
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +000043#include "nterp_helpers.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010044#include "oat_quick_method_header.h"
Vladimir Marko439d1262019-04-12 14:45:07 +010045#include "obj_ptr-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010046#include "quick/quick_method_frame_info.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070047#include "runtime.h"
Dave Allisonf9439142014-03-27 15:10:22 -070048#include "thread.h"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070049#include "thread_list.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070050
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080051namespace art {
52
Andreas Gampe46ee31b2016-12-14 10:11:49 -080053using android::base::StringPrintf;
54
Mathieu Chartier8405bfd2016-02-05 12:00:49 -080055static constexpr bool kDebugStackWalk = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -070056
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -080057StackVisitor::StackVisitor(Thread* thread,
58 Context* context,
59 StackWalkKind walk_kind,
60 bool check_suspended)
61 : StackVisitor(thread, context, walk_kind, 0, check_suspended) {}
Ian Rogers7a22fa62013-01-23 12:16:16 -080062
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010063StackVisitor::StackVisitor(Thread* thread,
64 Context* context,
65 StackWalkKind walk_kind,
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -080066 size_t num_frames,
67 bool check_suspended)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010068 : thread_(thread),
69 walk_kind_(walk_kind),
70 cur_shadow_frame_(nullptr),
71 cur_quick_frame_(nullptr),
72 cur_quick_frame_pc_(0),
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010073 cur_oat_quick_method_header_(nullptr),
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010074 num_frames_(num_frames),
75 cur_depth_(0),
Alex Light721e4022020-01-14 14:45:40 -080076 walk_started_(false),
David Srbecky145a18a2019-06-03 14:35:22 +010077 cur_inline_info_(nullptr, CodeInfo()),
78 cur_stack_map_(0, StackMap()),
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -080079 context_(context),
80 check_suspended_(check_suspended) {
81 if (check_suspended_) {
82 DCHECK(thread == Thread::Current() || thread->IsSuspended()) << *thread;
83 }
Ian Rogers5cf98192014-05-29 21:31:50 -070084}
85
David Srbecky145a18a2019-06-03 14:35:22 +010086CodeInfo* StackVisitor::GetCurrentInlineInfo() const {
87 DCHECK(!(*cur_quick_frame_)->IsNative());
88 const OatQuickMethodHeader* header = GetCurrentOatQuickMethodHeader();
89 if (cur_inline_info_.first != header) {
David Srbecky0d4567f2019-05-30 22:45:40 +010090 cur_inline_info_ = std::make_pair(header, CodeInfo::DecodeInlineInfoOnly(header));
David Srbecky145a18a2019-06-03 14:35:22 +010091 }
92 return &cur_inline_info_.second;
93}
94
95StackMap* StackVisitor::GetCurrentStackMap() const {
96 DCHECK(!(*cur_quick_frame_)->IsNative());
97 const OatQuickMethodHeader* header = GetCurrentOatQuickMethodHeader();
98 if (cur_stack_map_.first != cur_quick_frame_pc_) {
99 uint32_t pc = header->NativeQuickPcOffset(cur_quick_frame_pc_);
100 cur_stack_map_ = std::make_pair(cur_quick_frame_pc_,
101 GetCurrentInlineInfo()->GetStackMapForNativePcOffset(pc));
102 }
103 return &cur_stack_map_.second;
104}
105
Mathieu Chartiere401d142015-04-22 13:56:20 -0700106ArtMethod* StackVisitor::GetMethod() const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100107 if (cur_shadow_frame_ != nullptr) {
108 return cur_shadow_frame_->GetMethod();
109 } else if (cur_quick_frame_ != nullptr) {
110 if (IsInInlinedFrame()) {
David Srbecky145a18a2019-06-03 14:35:22 +0100111 CodeInfo* code_info = GetCurrentInlineInfo();
Mathieu Chartier45bf2502016-03-31 11:07:09 -0700112 DCHECK(walk_kind_ != StackWalkKind::kSkipInlinedFrames);
David Srbecky145a18a2019-06-03 14:35:22 +0100113 return GetResolvedMethod(*GetCurrentQuickFrame(), *code_info, current_inline_frames_);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100114 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700115 return *cur_quick_frame_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100116 }
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100117 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700118 return nullptr;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100119}
120
Dave Allisonb373e092014-02-20 16:06:36 -0800121uint32_t StackVisitor::GetDexPc(bool abort_on_failure) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700122 if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700123 return cur_shadow_frame_->GetDexPC();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700124 } else if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100125 if (IsInInlinedFrame()) {
David Srbecky93bd3612018-07-02 19:30:18 +0100126 return current_inline_frames_.back().GetDexPc();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100127 } else if (cur_oat_quick_method_header_ == nullptr) {
Andreas Gampee2abbc62017-09-15 11:59:26 -0700128 return dex::kDexNoIndex;
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000129 } else if ((*GetCurrentQuickFrame())->IsNative()) {
130 return cur_oat_quick_method_header_->ToDexPc(
131 GetCurrentQuickFrame(), cur_quick_frame_pc_, abort_on_failure);
132 } else if (cur_oat_quick_method_header_->IsOptimized()) {
David Srbecky145a18a2019-06-03 14:35:22 +0100133 StackMap* stack_map = GetCurrentStackMap();
134 DCHECK(stack_map->IsValid());
135 return stack_map->GetDexPc();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100136 } else {
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000137 DCHECK(cur_oat_quick_method_header_->IsNterpMethodHeader());
138 return NterpGetDexPC(cur_quick_frame_);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100139 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700140 } else {
141 return 0;
142 }
143}
144
Mathieu Chartiere401d142015-04-22 13:56:20 -0700145extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700146 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertza836bc92014-11-25 16:30:53 +0100147
Vladimir Markoabedfca2019-05-23 14:07:47 +0100148ObjPtr<mirror::Object> StackVisitor::GetThisObject() const {
Andreas Gampe542451c2016-07-26 09:02:02 -0700149 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700150 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800151 if (m->IsStatic()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100152 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800153 } else if (m->IsNative()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100154 if (cur_quick_frame_ != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700155 HandleScope* hs = reinterpret_cast<HandleScope*>(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100156 reinterpret_cast<char*>(cur_quick_frame_) + sizeof(ArtMethod*));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700157 return hs->GetReference(0);
Ian Rogers62d6c772013-02-27 08:32:07 -0800158 } else {
159 return cur_shadow_frame_->GetVRegReference(0);
160 }
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000161 } else if (m->IsProxyMethod()) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100162 if (cur_quick_frame_ != nullptr) {
163 return artQuickGetProxyThisObject(cur_quick_frame_);
164 } else {
165 return cur_shadow_frame_->GetVRegReference(0);
166 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800167 } else {
David Sehr0225f8e2018-01-31 08:52:24 +0000168 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800169 if (!accessor.HasCodeItem()) {
Ian Rogerse0dcd462014-03-08 15:21:04 -0800170 UNIMPLEMENTED(ERROR) << "Failed to determine this object of abstract or proxy method: "
David Sehr709b0702016-10-13 09:12:37 -0700171 << ArtMethod::PrettyMethod(m);
Ian Rogerse0dcd462014-03-08 15:21:04 -0800172 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800173 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800174 uint16_t reg = accessor.RegistersSize() - accessor.InsSize();
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000175 uint32_t value = 0;
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100176 if (!GetVReg(m, reg, kReferenceVReg, &value)) {
177 return nullptr;
178 }
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000179 return reinterpret_cast<mirror::Object*>(value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800180 }
181 }
182}
183
Ian Rogers0c7abda2012-09-19 13:33:42 -0700184size_t StackVisitor::GetNativePcOffset() const {
185 DCHECK(!IsShadowFrame());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100186 return GetCurrentOatQuickMethodHeader()->NativeQuickPcOffset(cur_quick_frame_pc_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700187}
188
Mingyao Yang99170c62015-07-06 11:10:37 -0700189bool StackVisitor::GetVRegFromDebuggerShadowFrame(uint16_t vreg,
190 VRegKind kind,
191 uint32_t* val) const {
192 size_t frame_id = const_cast<StackVisitor*>(this)->GetFrameId();
193 ShadowFrame* shadow_frame = thread_->FindDebuggerShadowFrame(frame_id);
194 if (shadow_frame != nullptr) {
195 bool* updated_vreg_flags = thread_->GetUpdatedVRegFlags(frame_id);
196 DCHECK(updated_vreg_flags != nullptr);
197 if (updated_vreg_flags[vreg]) {
198 // Value is set by the debugger.
199 if (kind == kReferenceVReg) {
200 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
201 shadow_frame->GetVRegReference(vreg)));
202 } else {
203 *val = shadow_frame->GetVReg(vreg);
204 }
205 return true;
206 }
207 }
208 // No value is set by the debugger.
209 return false;
210}
211
David Srbeckycffa2542019-07-01 15:31:41 +0100212bool StackVisitor::GetVReg(ArtMethod* m,
213 uint16_t vreg,
214 VRegKind kind,
215 uint32_t* val,
216 std::optional<DexRegisterLocation> location) const {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200217 if (cur_quick_frame_ != nullptr) {
218 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800219 DCHECK(m == GetMethod());
Mingyao Yang99170c62015-07-06 11:10:37 -0700220 // Check if there is value set by the debugger.
221 if (GetVRegFromDebuggerShadowFrame(vreg, kind, val)) {
222 return true;
223 }
Nicolas Geoffrayd7651b12019-12-18 14:57:30 +0000224 bool result = false;
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000225 if (cur_oat_quick_method_header_->IsNterpMethodHeader()) {
Nicolas Geoffrayd7651b12019-12-18 14:57:30 +0000226 result = true;
227 *val = (kind == kReferenceVReg)
228 ? NterpGetVRegReference(cur_quick_frame_, vreg)
229 : NterpGetVReg(cur_quick_frame_, vreg);
230 } else {
231 DCHECK(cur_oat_quick_method_header_->IsOptimized());
232 if (location.has_value() && kind != kReferenceVReg) {
233 uint32_t val2 = *val;
234 // The caller already known the register location, so we can use the faster overload
235 // which does not decode the stack maps.
236 result = GetVRegFromOptimizedCode(location.value(), kind, val);
237 // Compare to the slower overload.
238 DCHECK_EQ(result, GetVRegFromOptimizedCode(m, vreg, kind, &val2));
239 DCHECK_EQ(*val, val2);
240 } else {
241 result = GetVRegFromOptimizedCode(m, vreg, kind, val);
242 }
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000243 }
Alex Lightb096c912019-09-25 13:33:06 -0700244 if (kind == kReferenceVReg) {
245 // Perform a read barrier in case we are in a different thread and GC is ongoing.
246 mirror::Object* out = reinterpret_cast<mirror::Object*>(static_cast<uintptr_t>(*val));
247 uintptr_t ptr_out = reinterpret_cast<uintptr_t>(GcRoot<mirror::Object>(out).Read());
248 DCHECK_LT(ptr_out, std::numeric_limits<uint32_t>::max());
249 *val = static_cast<uint32_t>(ptr_out);
250 }
Nicolas Geoffrayd7651b12019-12-18 14:57:30 +0000251 return result;
Ian Rogers0399dde2012-06-06 17:09:28 -0700252 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100253 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertz09687442015-11-17 10:35:39 +0100254 if (kind == kReferenceVReg) {
255 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
256 cur_shadow_frame_->GetVRegReference(vreg)));
257 } else {
258 *val = cur_shadow_frame_->GetVReg(vreg);
259 }
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200260 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700261 }
262}
263
Mathieu Chartiere401d142015-04-22 13:56:20 -0700264bool StackVisitor::GetVRegFromOptimizedCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100265 uint32_t* val) const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100266 DCHECK_EQ(m, GetMethod());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800267 // Can't be null or how would we compile its instructions?
268 DCHECK(m->GetCodeItem() != nullptr) << m->PrettyMethod();
David Sehr0225f8e2018-01-31 08:52:24 +0000269 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800270 uint16_t number_of_dex_registers = accessor.RegistersSize();
271 DCHECK_LT(vreg, number_of_dex_registers);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100272 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
David Srbecky052f8ca2018-04-26 15:42:54 +0100273 CodeInfo code_info(method_header);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100274
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100275 uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_);
David Srbecky052f8ca2018-04-26 15:42:54 +0100276 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100277 DCHECK(stack_map.IsValid());
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100278
279 DexRegisterMap dex_register_map = IsInInlinedFrame()
David Srbecky93bd3612018-07-02 19:30:18 +0100280 ? code_info.GetInlineDexRegisterMapOf(stack_map, current_inline_frames_.back())
David Srbeckyfd89b072018-06-03 12:00:22 +0100281 : code_info.GetDexRegisterMapOf(stack_map);
282 if (dex_register_map.empty()) {
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000283 return false;
284 }
David Srbeckyfd89b072018-06-03 12:00:22 +0100285 DCHECK_EQ(dex_register_map.size(), number_of_dex_registers);
David Srbeckye1402122018-06-13 18:20:45 +0100286 DexRegisterLocation::Kind location_kind = dex_register_map[vreg].GetKind();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100287 switch (location_kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000288 case DexRegisterLocation::Kind::kInStack: {
David Srbeckye1402122018-06-13 18:20:45 +0100289 const int32_t offset = dex_register_map[vreg].GetStackOffsetInBytes();
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100290 BitMemoryRegion stack_mask = code_info.GetStackMaskOf(stack_map);
291 if (kind == kReferenceVReg && !stack_mask.LoadBit(offset / kFrameSlotSize)) {
292 return false;
293 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100294 const uint8_t* addr = reinterpret_cast<const uint8_t*>(cur_quick_frame_) + offset;
295 *val = *reinterpret_cast<const uint32_t*>(addr);
296 return true;
297 }
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100298 case DexRegisterLocation::Kind::kInRegister: {
299 uint32_t register_mask = code_info.GetRegisterMaskOf(stack_map);
300 uint32_t reg = dex_register_map[vreg].GetMachineRegister();
301 if (kind == kReferenceVReg && !(register_mask & (1 << reg))) {
302 return false;
303 }
304 return GetRegisterIfAccessible(reg, kind, val);
305 }
David Brazdild9cb68e2015-08-25 13:52:43 +0100306 case DexRegisterLocation::Kind::kInRegisterHigh:
307 case DexRegisterLocation::Kind::kInFpuRegister:
308 case DexRegisterLocation::Kind::kInFpuRegisterHigh: {
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100309 if (kind == kReferenceVReg) {
310 return false;
311 }
David Srbeckye1402122018-06-13 18:20:45 +0100312 uint32_t reg = dex_register_map[vreg].GetMachineRegister();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100313 return GetRegisterIfAccessible(reg, kind, val);
314 }
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100315 case DexRegisterLocation::Kind::kConstant: {
316 uint32_t result = dex_register_map[vreg].GetConstant();
317 if (kind == kReferenceVReg && result != 0) {
318 return false;
319 }
320 *val = result;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100321 return true;
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100322 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000323 case DexRegisterLocation::Kind::kNone:
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100324 return false;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000325 default:
David Srbeckye1402122018-06-13 18:20:45 +0100326 LOG(FATAL) << "Unexpected location kind " << dex_register_map[vreg].GetKind();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000327 UNREACHABLE();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100328 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100329}
330
David Srbeckycffa2542019-07-01 15:31:41 +0100331bool StackVisitor::GetVRegFromOptimizedCode(DexRegisterLocation location,
332 VRegKind kind,
333 uint32_t* val) const {
334 switch (location.GetKind()) {
335 case DexRegisterLocation::Kind::kInvalid:
336 break;
337 case DexRegisterLocation::Kind::kInStack: {
338 const uint8_t* sp = reinterpret_cast<const uint8_t*>(cur_quick_frame_);
339 *val = *reinterpret_cast<const uint32_t*>(sp + location.GetStackOffsetInBytes());
340 return true;
341 }
342 case DexRegisterLocation::Kind::kInRegister:
343 case DexRegisterLocation::Kind::kInRegisterHigh:
344 case DexRegisterLocation::Kind::kInFpuRegister:
345 case DexRegisterLocation::Kind::kInFpuRegisterHigh:
346 return GetRegisterIfAccessible(location.GetMachineRegister(), kind, val);
347 case DexRegisterLocation::Kind::kConstant:
348 *val = location.GetConstant();
349 return true;
350 case DexRegisterLocation::Kind::kNone:
351 return false;
352 }
353 LOG(FATAL) << "Unexpected location kind " << location.GetKind();
354 UNREACHABLE();
355}
356
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100357bool StackVisitor::GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const {
358 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
David Brazdil77a48ae2015-09-15 12:34:04 +0000359
Vladimir Marko239d6ea2016-09-05 10:44:04 +0100360 if (kRuntimeISA == InstructionSet::kX86 && is_float) {
361 // X86 float registers are 64-bit and each XMM register is provided as two separate
362 // 32-bit registers by the context.
363 reg = (kind == kDoubleHiVReg) ? (2 * reg + 1) : (2 * reg);
364 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000365
Goran Jakovljevic986660c2015-12-10 11:44:50 +0100366 // MIPS32 float registers are used as 64-bit (for MIPS32r2 it is pair
367 // F(2n)-F(2n+1), and for MIPS32r6 it is 64-bit register F(2n)). When
368 // accessing upper 32-bits from double, reg + 1 should be used.
369 if ((kRuntimeISA == InstructionSet::kMips) && (kind == kDoubleHiVReg)) {
370 DCHECK_ALIGNED(reg, 2);
371 reg++;
372 }
373
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100374 if (!IsAccessibleRegister(reg, is_float)) {
375 return false;
376 }
377 uintptr_t ptr_val = GetRegister(reg, is_float);
378 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
379 if (target64) {
380 const bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
381 const bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
382 int64_t value_long = static_cast<int64_t>(ptr_val);
383 if (wide_lo) {
384 ptr_val = static_cast<uintptr_t>(Low32Bits(value_long));
385 } else if (wide_hi) {
386 ptr_val = static_cast<uintptr_t>(High32Bits(value_long));
387 }
388 }
389 *val = ptr_val;
390 return true;
391}
392
Mingyao Yang99170c62015-07-06 11:10:37 -0700393bool StackVisitor::GetVRegPairFromDebuggerShadowFrame(uint16_t vreg,
394 VRegKind kind_lo,
395 VRegKind kind_hi,
396 uint64_t* val) const {
397 uint32_t low_32bits;
398 uint32_t high_32bits;
399 bool success = GetVRegFromDebuggerShadowFrame(vreg, kind_lo, &low_32bits);
400 success &= GetVRegFromDebuggerShadowFrame(vreg + 1, kind_hi, &high_32bits);
401 if (success) {
402 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
403 }
404 return success;
405}
406
Mathieu Chartiere401d142015-04-22 13:56:20 -0700407bool StackVisitor::GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200408 VRegKind kind_hi, uint64_t* val) const {
409 if (kind_lo == kLongLoVReg) {
410 DCHECK_EQ(kind_hi, kLongHiVReg);
411 } else if (kind_lo == kDoubleLoVReg) {
412 DCHECK_EQ(kind_hi, kDoubleHiVReg);
413 } else {
414 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100415 UNREACHABLE();
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200416 }
Mingyao Yang99170c62015-07-06 11:10:37 -0700417 // Check if there is value set by the debugger.
418 if (GetVRegPairFromDebuggerShadowFrame(vreg, kind_lo, kind_hi, val)) {
419 return true;
420 }
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200421 if (cur_quick_frame_ != nullptr) {
422 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
423 DCHECK(m == GetMethod());
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100424 DCHECK(cur_oat_quick_method_header_->IsOptimized());
425 return GetVRegPairFromOptimizedCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200426 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100427 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200428 *val = cur_shadow_frame_->GetVRegLong(vreg);
429 return true;
430 }
431}
432
Mathieu Chartiere401d142015-04-22 13:56:20 -0700433bool StackVisitor::GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100434 VRegKind kind_lo, VRegKind kind_hi,
435 uint64_t* val) const {
436 uint32_t low_32bits;
437 uint32_t high_32bits;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700438 bool success = GetVRegFromOptimizedCode(m, vreg, kind_lo, &low_32bits);
439 success &= GetVRegFromOptimizedCode(m, vreg + 1, kind_hi, &high_32bits);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100440 if (success) {
441 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
442 }
443 return success;
444}
445
446bool StackVisitor::GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
447 VRegKind kind_lo, uint64_t* val) const {
448 const bool is_float = (kind_lo == kDoubleLoVReg);
449 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
450 return false;
451 }
452 uintptr_t ptr_val_lo = GetRegister(reg_lo, is_float);
453 uintptr_t ptr_val_hi = GetRegister(reg_hi, is_float);
454 bool target64 = Is64BitInstructionSet(kRuntimeISA);
455 if (target64) {
456 int64_t value_long_lo = static_cast<int64_t>(ptr_val_lo);
457 int64_t value_long_hi = static_cast<int64_t>(ptr_val_hi);
458 ptr_val_lo = static_cast<uintptr_t>(Low32Bits(value_long_lo));
459 ptr_val_hi = static_cast<uintptr_t>(High32Bits(value_long_hi));
460 }
461 *val = (static_cast<uint64_t>(ptr_val_hi) << 32) | static_cast<uint32_t>(ptr_val_lo);
462 return true;
463}
464
Vladimir Marko439d1262019-04-12 14:45:07 +0100465ShadowFrame* StackVisitor::PrepareSetVReg(ArtMethod* m, uint16_t vreg, bool wide) {
David Sehr0225f8e2018-01-31 08:52:24 +0000466 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800467 if (!accessor.HasCodeItem()) {
Vladimir Marko439d1262019-04-12 14:45:07 +0100468 return nullptr;
Mingyao Yang99170c62015-07-06 11:10:37 -0700469 }
470 ShadowFrame* shadow_frame = GetCurrentShadowFrame();
471 if (shadow_frame == nullptr) {
472 // This is a compiled frame: we must prepare and update a shadow frame that will
473 // be executed by the interpreter after deoptimization of the stack.
474 const size_t frame_id = GetFrameId();
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800475 const uint16_t num_regs = accessor.RegistersSize();
Mingyao Yang99170c62015-07-06 11:10:37 -0700476 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
477 CHECK(shadow_frame != nullptr);
Vladimir Marko439d1262019-04-12 14:45:07 +0100478 // Remember the vreg(s) has been set for debugging and must not be overwritten by the
Mingyao Yang99170c62015-07-06 11:10:37 -0700479 // original value during deoptimization of the stack.
480 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
Vladimir Marko439d1262019-04-12 14:45:07 +0100481 if (wide) {
482 thread_->GetUpdatedVRegFlags(frame_id)[vreg + 1] = true;
483 }
Mingyao Yang99170c62015-07-06 11:10:37 -0700484 }
Vladimir Marko439d1262019-04-12 14:45:07 +0100485 return shadow_frame;
486}
487
488bool StackVisitor::SetVReg(ArtMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind) {
489 DCHECK(kind == kIntVReg || kind == kFloatVReg);
490 ShadowFrame* shadow_frame = PrepareSetVReg(m, vreg, /* wide= */ false);
491 if (shadow_frame == nullptr) {
492 return false;
Mingyao Yang99170c62015-07-06 11:10:37 -0700493 }
Vladimir Marko439d1262019-04-12 14:45:07 +0100494 shadow_frame->SetVReg(vreg, new_value);
495 return true;
496}
497
498bool StackVisitor::SetVRegReference(ArtMethod* m, uint16_t vreg, ObjPtr<mirror::Object> new_value) {
499 ShadowFrame* shadow_frame = PrepareSetVReg(m, vreg, /* wide= */ false);
500 if (shadow_frame == nullptr) {
501 return false;
502 }
503 shadow_frame->SetVRegReference(vreg, new_value);
Mingyao Yang99170c62015-07-06 11:10:37 -0700504 return true;
505}
506
Mingyao Yang636b9252015-07-31 16:40:24 -0700507bool StackVisitor::SetVRegPair(ArtMethod* m,
508 uint16_t vreg,
509 uint64_t new_value,
510 VRegKind kind_lo,
511 VRegKind kind_hi) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700512 if (kind_lo == kLongLoVReg) {
513 DCHECK_EQ(kind_hi, kLongHiVReg);
514 } else if (kind_lo == kDoubleLoVReg) {
515 DCHECK_EQ(kind_hi, kDoubleHiVReg);
516 } else {
517 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
518 UNREACHABLE();
519 }
Vladimir Marko439d1262019-04-12 14:45:07 +0100520 ShadowFrame* shadow_frame = PrepareSetVReg(m, vreg, /* wide= */ true);
Mingyao Yang99170c62015-07-06 11:10:37 -0700521 if (shadow_frame == nullptr) {
Vladimir Marko439d1262019-04-12 14:45:07 +0100522 return false;
Mingyao Yang99170c62015-07-06 11:10:37 -0700523 }
524 shadow_frame->SetVRegLong(vreg, new_value);
525 return true;
526}
527
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100528bool StackVisitor::IsAccessibleGPR(uint32_t reg) const {
529 DCHECK(context_ != nullptr);
530 return context_->IsAccessibleGPR(reg);
531}
532
Mathieu Chartier815873e2014-02-13 18:02:13 -0800533uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100534 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
535 DCHECK(context_ != nullptr);
Mathieu Chartier815873e2014-02-13 18:02:13 -0800536 return context_->GetGPRAddress(reg);
537}
538
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100539uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
540 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
541 DCHECK(context_ != nullptr);
542 return context_->GetGPR(reg);
Ian Rogers0399dde2012-06-06 17:09:28 -0700543}
544
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100545bool StackVisitor::IsAccessibleFPR(uint32_t reg) const {
546 DCHECK(context_ != nullptr);
547 return context_->IsAccessibleFPR(reg);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200548}
549
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100550uintptr_t StackVisitor::GetFPR(uint32_t reg) const {
551 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
552 DCHECK(context_ != nullptr);
553 return context_->GetFPR(reg);
554}
555
Ian Rogers0399dde2012-06-06 17:09:28 -0700556uintptr_t StackVisitor::GetReturnPc() const {
Ian Rogers13735952014-10-08 12:43:28 -0700557 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700558 DCHECK(sp != nullptr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100559 uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogers0399dde2012-06-06 17:09:28 -0700560 return *reinterpret_cast<uintptr_t*>(pc_addr);
561}
562
563void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
Ian Rogers13735952014-10-08 12:43:28 -0700564 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700565 CHECK(sp != nullptr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100566 uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogers0399dde2012-06-06 17:09:28 -0700567 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc;
568}
569
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100570size_t StackVisitor::ComputeNumFrames(Thread* thread, StackWalkKind walk_kind) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700571 struct NumFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100572 NumFramesVisitor(Thread* thread_in, StackWalkKind walk_kind_in)
573 : StackVisitor(thread_in, nullptr, walk_kind_in), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700574
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100575 bool VisitFrame() override {
Ian Rogers0399dde2012-06-06 17:09:28 -0700576 frames++;
577 return true;
578 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700579
Ian Rogers0399dde2012-06-06 17:09:28 -0700580 size_t frames;
581 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100582 NumFramesVisitor visitor(thread, walk_kind);
Ian Rogers0399dde2012-06-06 17:09:28 -0700583 visitor.WalkStack(true);
584 return visitor.frames;
585}
586
Mathieu Chartiere401d142015-04-22 13:56:20 -0700587bool StackVisitor::GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700588 struct HasMoreFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100589 HasMoreFramesVisitor(Thread* thread,
590 StackWalkKind walk_kind,
591 size_t num_frames,
592 size_t frame_height)
593 : StackVisitor(thread, nullptr, walk_kind, num_frames),
594 frame_height_(frame_height),
595 found_frame_(false),
596 has_more_frames_(false),
597 next_method_(nullptr),
598 next_dex_pc_(0) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700599 }
600
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100601 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700602 if (found_frame_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700603 ArtMethod* method = GetMethod();
Ian Rogers5cf98192014-05-29 21:31:50 -0700604 if (method != nullptr && !method->IsRuntimeMethod()) {
605 has_more_frames_ = true;
606 next_method_ = method;
607 next_dex_pc_ = GetDexPc();
608 return false; // End stack walk once next method is found.
609 }
610 } else if (GetFrameHeight() == frame_height_) {
611 found_frame_ = true;
612 }
613 return true;
614 }
615
616 size_t frame_height_;
617 bool found_frame_;
618 bool has_more_frames_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700619 ArtMethod* next_method_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700620 uint32_t next_dex_pc_;
621 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100622 HasMoreFramesVisitor visitor(thread_, walk_kind_, GetNumFrames(), GetFrameHeight());
Ian Rogers5cf98192014-05-29 21:31:50 -0700623 visitor.WalkStack(true);
624 *next_method = visitor.next_method_;
625 *next_dex_pc = visitor.next_dex_pc_;
626 return visitor.has_more_frames_;
627}
628
Ian Rogers7a22fa62013-01-23 12:16:16 -0800629void StackVisitor::DescribeStack(Thread* thread) {
Ian Rogers306057f2012-11-26 12:45:53 -0800630 struct DescribeStackVisitor : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800631 explicit DescribeStackVisitor(Thread* thread_in)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100632 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800633
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100634 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers306057f2012-11-26 12:45:53 -0800635 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation();
636 return true;
637 }
638 };
Ian Rogers7a22fa62013-01-23 12:16:16 -0800639 DescribeStackVisitor visitor(thread);
Ian Rogers306057f2012-11-26 12:45:53 -0800640 visitor.WalkStack(true);
641}
642
Ian Rogers40e3bac2012-11-20 00:09:14 -0800643std::string StackVisitor::DescribeLocation() const {
644 std::string result("Visiting method '");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700645 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700646 if (m == nullptr) {
Ian Rogers306057f2012-11-26 12:45:53 -0800647 return "upcall";
648 }
David Sehr709b0702016-10-13 09:12:37 -0700649 result += m->PrettyMethod();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800650 result += StringPrintf("' at dex PC 0x%04x", GetDexPc());
Ian Rogers40e3bac2012-11-20 00:09:14 -0800651 if (!IsShadowFrame()) {
652 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc()));
653 }
654 return result;
655}
656
Alex Lightdba61482016-12-21 08:20:29 -0800657void StackVisitor::SetMethod(ArtMethod* method) {
658 DCHECK(GetMethod() != nullptr);
659 if (cur_shadow_frame_ != nullptr) {
660 cur_shadow_frame_->SetMethod(method);
661 } else {
662 DCHECK(cur_quick_frame_ != nullptr);
Nicolas Geoffray226805d2018-12-14 10:59:02 +0000663 CHECK(!IsInInlinedFrame()) << "We do not support setting inlined method's ArtMethod: "
664 << GetMethod()->PrettyMethod() << " is inlined into "
665 << GetOuterMethod()->PrettyMethod();
Alex Light1ebe4fe2017-01-30 14:57:11 -0800666 *cur_quick_frame_ = method;
Alex Lightdba61482016-12-21 08:20:29 -0800667 }
668}
669
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100670static void AssertPcIsWithinQuickCode(ArtMethod* method, uintptr_t pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700671 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100672 if (method->IsNative() || method->IsRuntimeMethod() || method->IsProxyMethod()) {
673 return;
674 }
675
676 if (pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) {
677 return;
678 }
679
Mingyao Yang88ca8ba2017-05-23 14:21:07 -0700680 Runtime* runtime = Runtime::Current();
681 if (runtime->UseJitCompilation() &&
682 runtime->GetJit()->GetCodeCache()->ContainsPc(reinterpret_cast<const void*>(pc))) {
683 return;
684 }
685
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100686 const void* code = method->GetEntryPointFromQuickCompiledCode();
Alex Lightdb01a092017-04-03 15:39:55 -0700687 if (code == GetQuickInstrumentationEntryPoint() || code == GetInvokeObsoleteMethodStub()) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100688 return;
689 }
690
691 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
692 if (class_linker->IsQuickToInterpreterBridge(code) ||
693 class_linker->IsQuickResolutionStub(code)) {
694 return;
695 }
696
Calin Juravleffc87072016-04-20 14:22:09 +0100697 if (runtime->UseJitCompilation() && runtime->GetJit()->GetCodeCache()->ContainsPc(code)) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100698 return;
699 }
700
Mingyao Yang063fc772016-08-02 11:02:54 -0700701 uint32_t code_size = OatQuickMethodHeader::FromEntryPoint(code)->GetCodeSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100702 uintptr_t code_start = reinterpret_cast<uintptr_t>(code);
703 CHECK(code_start <= pc && pc <= (code_start + code_size))
David Sehr709b0702016-10-13 09:12:37 -0700704 << method->PrettyMethod()
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100705 << " pc=" << std::hex << pc
Roland Levillain0d5a2812015-11-13 10:07:31 +0000706 << " code_start=" << code_start
707 << " code_size=" << code_size;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100708}
709
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700710void StackVisitor::SanityCheckFrame() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800711 if (kIsDebugBuild) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700712 ArtMethod* method = GetMethod();
Vladimir Markod93e3742018-07-18 10:58:13 +0100713 ObjPtr<mirror::Class> declaring_class = method->GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700714 // Runtime methods have null declaring class.
715 if (!method->IsRuntimeMethod()) {
716 CHECK(declaring_class != nullptr);
717 CHECK_EQ(declaring_class->GetClass(), declaring_class->GetClass()->GetClass())
718 << declaring_class;
719 } else {
720 CHECK(declaring_class == nullptr);
721 }
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700722 Runtime* const runtime = Runtime::Current();
723 LinearAlloc* const linear_alloc = runtime->GetLinearAlloc();
724 if (!linear_alloc->Contains(method)) {
725 // Check class linker linear allocs.
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100726 // We get the canonical method as copied methods may have their declaring
727 // class from another class loader.
Ulya Trafimovich819b3622019-12-12 17:59:10 +0000728 const PointerSize ptrSize = runtime->GetClassLinker()->GetImagePointerSize();
729 ArtMethod* canonical = method->GetCanonicalMethod(ptrSize);
Vladimir Markod93e3742018-07-18 10:58:13 +0100730 ObjPtr<mirror::Class> klass = canonical->GetDeclaringClass();
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700731 LinearAlloc* const class_linear_alloc = (klass != nullptr)
Mathieu Chartier5b830502016-03-02 10:30:23 -0800732 ? runtime->GetClassLinker()->GetAllocatorForClassLoader(klass->GetClassLoader())
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700733 : linear_alloc;
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100734 if (!class_linear_alloc->Contains(canonical)) {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700735 // Check image space.
736 bool in_image = false;
737 for (auto& space : runtime->GetHeap()->GetContinuousSpaces()) {
738 if (space->IsImageSpace()) {
739 auto* image_space = space->AsImageSpace();
740 const auto& header = image_space->GetImageHeader();
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700741 const ImageSection& methods = header.GetMethodsSection();
742 const ImageSection& runtime_methods = header.GetRuntimeMethodsSection();
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100743 const size_t offset = reinterpret_cast<const uint8_t*>(canonical) - image_space->Begin();
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700744 if (methods.Contains(offset) || runtime_methods.Contains(offset)) {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700745 in_image = true;
746 break;
747 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700748 }
749 }
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100750 CHECK(in_image) << canonical->PrettyMethod() << " not in linear alloc or image";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700751 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700752 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800753 if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100754 AssertPcIsWithinQuickCode(method, cur_quick_frame_pc_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800755 // Frame sanity.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100756 size_t frame_size = GetCurrentQuickFrameInfo().FrameSizeInBytes();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800757 CHECK_NE(frame_size, 0u);
Nicolas Geoffray00391822019-12-10 10:17:23 +0000758 // For compiled code, we could try to have a rough guess at an upper size we expect
759 // to see for a frame:
Andreas Gampe5b417b92014-03-10 14:18:35 -0700760 // 256 registers
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700761 // 2 words HandleScope overhead
Andreas Gampe5b417b92014-03-10 14:18:35 -0700762 // 3+3 register spills
Brian Carlstromed08bd42014-03-19 18:34:17 -0700763 // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word);
Nicolas Geoffray00391822019-12-10 10:17:23 +0000764 const size_t kMaxExpectedFrameSize = interpreter::kMaxNterpFrame;
David Sehr709b0702016-10-13 09:12:37 -0700765 CHECK_LE(frame_size, kMaxExpectedFrameSize) << method->PrettyMethod();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100766 size_t return_pc_offset = GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800767 CHECK_LT(return_pc_offset, frame_size);
768 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700769 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700770}
771
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100772// Counts the number of references in the parameter list of the corresponding method.
773// Note: Thus does _not_ include "this" for non-static methods.
774static uint32_t GetNumberOfReferenceArgsWithoutReceiver(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700775 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100776 uint32_t shorty_len;
777 const char* shorty = method->GetShorty(&shorty_len);
778 uint32_t refs = 0;
779 for (uint32_t i = 1; i < shorty_len ; ++i) {
780 if (shorty[i] == 'L') {
781 refs++;
782 }
783 }
784 return refs;
785}
786
787QuickMethodFrameInfo StackVisitor::GetCurrentQuickFrameInfo() const {
788 if (cur_oat_quick_method_header_ != nullptr) {
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000789 if (cur_oat_quick_method_header_->IsOptimized()) {
790 return cur_oat_quick_method_header_->GetFrameInfo();
791 } else {
792 DCHECK(cur_oat_quick_method_header_->IsNterpMethodHeader());
793 return NterpFrameInfo(cur_quick_frame_);
794 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100795 }
796
797 ArtMethod* method = GetMethod();
798 Runtime* runtime = Runtime::Current();
799
800 if (method->IsAbstract()) {
Vladimir Markod3083dd2018-05-17 08:43:47 +0100801 return RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100802 }
803
804 // This goes before IsProxyMethod since runtime methods have a null declaring class.
805 if (method->IsRuntimeMethod()) {
806 return runtime->GetRuntimeMethodFrameInfo(method);
807 }
808
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100809 if (method->IsProxyMethod()) {
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000810 // There is only one direct method of a proxy class: the constructor. A direct method is
811 // cloned from the original java.lang.reflect.Proxy and is executed as usual quick
812 // compiled method without any stubs. Therefore the method must have a OatQuickMethodHeader.
813 DCHECK(!method->IsDirect() && !method->IsConstructor())
814 << "Constructors of proxy classes must have a OatQuickMethodHeader";
Vladimir Markod3083dd2018-05-17 08:43:47 +0100815 return RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100816 }
817
Vladimir Marko2196c652017-11-30 16:16:07 +0000818 // The only remaining case is if the method is native and uses the generic JNI stub,
819 // called either directly or through some (resolution, instrumentation) trampoline.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100820 DCHECK(method->IsNative());
Vladimir Marko2196c652017-11-30 16:16:07 +0000821 if (kIsDebugBuild) {
822 ClassLinker* class_linker = runtime->GetClassLinker();
823 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(method,
824 kRuntimePointerSize);
825 CHECK(class_linker->IsQuickGenericJniStub(entry_point) ||
826 // The current entrypoint (after filtering out trampolines) may have changed
827 // from GenericJNI to JIT-compiled stub since we have entered this frame.
828 (runtime->GetJit() != nullptr &&
829 runtime->GetJit()->GetCodeCache()->ContainsPc(entry_point))) << method->PrettyMethod();
830 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100831 // Generic JNI frame.
832 uint32_t handle_refs = GetNumberOfReferenceArgsWithoutReceiver(method) + 1;
833 size_t scope_size = HandleScope::SizeOf(handle_refs);
Vladimir Markod3083dd2018-05-17 08:43:47 +0100834 constexpr QuickMethodFrameInfo callee_info =
835 RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100836
837 // Callee saves + handle scope + method ref + alignment
838 // Note: -sizeof(void*) since callee-save frame stores a whole method pointer.
839 size_t frame_size = RoundUp(
840 callee_info.FrameSizeInBytes() - sizeof(void*) + sizeof(ArtMethod*) + scope_size,
841 kStackAlignment);
842 return QuickMethodFrameInfo(frame_size, callee_info.CoreSpillMask(), callee_info.FpSpillMask());
843}
844
Andreas Gampe585da952016-12-02 14:52:29 -0800845template <StackVisitor::CountTransitions kCount>
Ian Rogers0399dde2012-06-06 17:09:28 -0700846void StackVisitor::WalkStack(bool include_transitions) {
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -0800847 if (check_suspended_) {
848 DCHECK(thread_ == Thread::Current() || thread_->IsSuspended());
849 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800850 CHECK_EQ(cur_depth_, 0U);
Alex Light721e4022020-01-14 14:45:40 -0800851 CHECK(!walk_started_);
852 walk_started_ = true;
853 bool retry = false;
854 size_t depth;
855 bool skip_to_depth = false;
856 uint64_t current_sequence_number;
857 Thread* self = Thread::Current();
858 // If the instrumentation stack changes due to DeoptimizeThreadStack or similar while we are
859 // running this stack-walk we need to restart and try again.
860 //
861 // This is because when we perform DeoptimizeThreadStack we replace the return-pc of the threads
862 // stack-frames with a trampoline, the actual return-address is placed in the threads
863 // InstrumentationStack. The instrumentation code will fill up this stack as it goes. Since there
864 // is no good way to map instrumentation stack frames to real ones (or vice versa) and we need to
865 // know which instrumentation stack frame we are on in order to find the real return-pc using that
866 // the next stack-frame's size we instead simply keep track of how many instrumentation frames we
867 // have used (as instrumentation_stack_depth). Each time we need a new instrumentation frame we
868 // simply increment this value. This works well enough but if InstrumentationInstallStack is
869 // called concurrently the InstrumentationStack might have frames inserted prior to the point this
870 // StackVisitor is currently at. This would cause the visitor to get the incorrect
871 // instrumentation_frame and start incorrectly walking the stack.
872 //
873 // To prevent this we keep track of when the last update that changes the indexs of
874 // instrumentation frames occurs and restart the stack walk if that changes. To simplify the
875 // process of building stack walkers we also keep track of how many times we have called
876 // VisitFrame and skip that many calls. This prevents any frames from being visited more than once
877 // per 'WalkStack' call.
878 do {
879 retry = false;
880 depth = 0;
881 // Keep track of how far in the stack-walk we are, making sure to also keep track of whether
882 // we've skipped enough to resume calling 'VisitFrame' after a retry.
883 auto increment_depth = [&]() {
884 DCHECK_LE(depth, cur_depth_);
885 ++depth;
886 if (skip_to_depth && depth == cur_depth_) {
887 skip_to_depth = false;
888 } else if (!skip_to_depth) {
889 ++cur_depth_;
890 }
891 };
892 // Wait for any current instrumentation installs to finish, Skip this if we are aborting.
893 if (LIKELY(gAborting == 0)) {
894 MutexLock mu(self, *GetThread()->GetInstrumentationInstallMutex());
895 }
896 // Get Current install state id.
897 {
898 MutexLock mu(self, *GetThread()->GetInstrumentationInstallSequenceNumberMutex());
899 current_sequence_number = GetThread()->GetInstrumentationInstallSequenceNumber();
900 }
901 bool exit_stubs_installed = Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled();
902 uint32_t instrumentation_stack_depth = 0;
903 size_t inlined_frames_count = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700904
Alex Light721e4022020-01-14 14:45:40 -0800905 for (const ManagedStack* current_fragment = thread_->GetManagedStack();
906 current_fragment != nullptr;
907 current_fragment = current_fragment->GetLink()) {
908 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
909 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
910 cur_quick_frame_pc_ = 0;
911 cur_oat_quick_method_header_ = nullptr;
912 if (cur_quick_frame_ != nullptr) { // Handle quick stack frames.
913 // Can't be both a shadow and a quick fragment.
914 DCHECK(current_fragment->GetTopShadowFrame() == nullptr);
915 ArtMethod* method = *cur_quick_frame_;
916 DCHECK(method != nullptr);
917 bool header_retrieved = false;
918 if (method->IsNative()) {
919 // We do not have a PC for the first frame, so we cannot simply use
920 // ArtMethod::GetOatQuickMethodHeader() as we're unable to distinguish there
921 // between GenericJNI frame and JIT-compiled JNI stub; the entrypoint may have
922 // changed since the frame was entered. The top quick frame tag indicates
923 // GenericJNI here, otherwise it's either AOT-compiled or JNI-compiled JNI stub.
924 if (UNLIKELY(current_fragment->GetTopQuickFrameTag())) {
925 // The generic JNI does not have any method header.
926 cur_oat_quick_method_header_ = nullptr;
Vladimir Marko2196c652017-11-30 16:16:07 +0000927 } else {
Alex Light721e4022020-01-14 14:45:40 -0800928 const void* existing_entry_point = method->GetEntryPointFromQuickCompiledCode();
929 CHECK(existing_entry_point != nullptr);
930 Runtime* runtime = Runtime::Current();
931 ClassLinker* class_linker = runtime->GetClassLinker();
932 // Check whether we can quickly get the header from the current entrypoint.
933 if (!class_linker->IsQuickGenericJniStub(existing_entry_point) &&
934 !class_linker->IsQuickResolutionStub(existing_entry_point) &&
935 existing_entry_point != GetQuickInstrumentationEntryPoint()) {
936 cur_oat_quick_method_header_ =
937 OatQuickMethodHeader::FromEntryPoint(existing_entry_point);
Vladimir Marko2196c652017-11-30 16:16:07 +0000938 } else {
Alex Light721e4022020-01-14 14:45:40 -0800939 const void* code = method->GetOatMethodQuickCode(class_linker->GetImagePointerSize());
940 if (code != nullptr) {
941 cur_oat_quick_method_header_ = OatQuickMethodHeader::FromEntryPoint(code);
942 } else {
943 // This must be a JITted JNI stub frame.
944 CHECK(runtime->GetJit() != nullptr);
945 code = runtime->GetJit()->GetCodeCache()->GetJniStubCode(method);
946 CHECK(code != nullptr) << method->PrettyMethod();
947 cur_oat_quick_method_header_ = OatQuickMethodHeader::FromCodePointer(code);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100948 }
949 }
950 }
Alex Light721e4022020-01-14 14:45:40 -0800951 header_retrieved = true;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100952 }
Alex Light721e4022020-01-14 14:45:40 -0800953 while (method != nullptr) {
954 if (!header_retrieved) {
955 cur_oat_quick_method_header_ = method->GetOatQuickMethodHeader(cur_quick_frame_pc_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700956 }
Alex Light721e4022020-01-14 14:45:40 -0800957 header_retrieved = false; // Force header retrieval in next iteration.
958 SanityCheckFrame();
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100959
Alex Light721e4022020-01-14 14:45:40 -0800960 if ((walk_kind_ == StackWalkKind::kIncludeInlinedFrames) &&
961 (cur_oat_quick_method_header_ != nullptr) &&
962 cur_oat_quick_method_header_->IsOptimized() &&
963 !method->IsNative() // JNI methods cannot have any inlined frames.
964 && CodeInfo::HasInlineInfo(cur_oat_quick_method_header_->GetOptimizedCodeInfoPtr())) {
965 DCHECK_NE(cur_quick_frame_pc_, 0u);
966 CodeInfo* code_info = GetCurrentInlineInfo();
967 StackMap* stack_map = GetCurrentStackMap();
968 if (stack_map->IsValid() && stack_map->HasInlineInfo()) {
969 DCHECK_EQ(current_inline_frames_.size(), 0u);
970 for (current_inline_frames_ = code_info->GetInlineInfosOf(*stack_map);
971 !current_inline_frames_.empty();
972 current_inline_frames_.pop_back()) {
973 if (!skip_to_depth) {
974 bool should_continue = VisitFrame();
975 if (UNLIKELY(!should_continue)) {
976 return;
977 }
978 }
979 increment_depth();
980 inlined_frames_count++;
981 }
982 }
983 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700984
Alex Light721e4022020-01-14 14:45:40 -0800985 if (!skip_to_depth) {
986 bool should_continue = VisitFrame();
987 if (UNLIKELY(!should_continue)) {
988 return;
989 }
990 }
991 if (kCount == CountTransitions::kYes || !method->IsRuntimeMethod()) {
992 increment_depth();
993 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700994
Alex Light721e4022020-01-14 14:45:40 -0800995 // NB This uses cur_oat_quick_frame_info_ (and hence cur_quick_frame_pc_ & the previous
996 // return_pc_) to determine the frame information.
997 QuickMethodFrameInfo frame_info = GetCurrentQuickFrameInfo();
998 if (context_ != nullptr) {
999 context_->FillCalleeSaves(reinterpret_cast<uint8_t*>(cur_quick_frame_), frame_info);
1000 }
1001 // Compute PC for next stack frame from return PC.
1002 size_t frame_size = frame_info.FrameSizeInBytes();
1003 size_t return_pc_offset = frame_size - sizeof(void*);
1004 uint8_t* return_pc_addr = reinterpret_cast<uint8_t*>(cur_quick_frame_) + return_pc_offset;
1005 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
1006
1007 if (UNLIKELY(exit_stubs_installed ||
1008 reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc)) {
1009 // While profiling, the return pc is restored from the side stack, except when walking
1010 // the stack for an exception where the side stack will be unwound in VisitFrame.
1011 if (reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc) {
1012 MutexLock mu(self, *GetThread()->GetInstrumentationInstallSequenceNumberMutex());
1013 // Instrument-install walks will change the instrumentation-stack but only put things
1014 // at the end, so we can continue regardless. We hold the InstrumentationInstallMutex
1015 // so we know nothing can mess up the stack.
1016 if (!IsStackInstrumentWalk() &&
1017 GetThread()->GetInstrumentationInstallSequenceNumber() !=
1018 current_sequence_number) {
1019 if (kDebugStackWalk) {
1020 LOG(INFO) << "Retrying walk from begining due to instrumentation change. "
1021 << "Skipping to depth " << cur_depth_;
1022 }
1023 skip_to_depth = true;
1024 retry = true;
1025 // Annoyingly c++ doesn't have a labled continue. If they respected RAII this would
1026 // be a good time to use goto but as is we'll just break twice.
1027 break; // From while (method != nullptr)
1028 } else if (kIsDebugBuild && IsStackInstrumentWalk()) {
1029 GetThread()->GetInstrumentationInstallMutex()->AssertExclusiveHeld(self);
1030 }
1031 CHECK_LT(instrumentation_stack_depth, thread_->GetInstrumentationStack()->size());
1032 const instrumentation::InstrumentationStackFrame& instrumentation_frame =
1033 (*thread_->GetInstrumentationStack())[instrumentation_stack_depth];
1034 instrumentation_stack_depth++;
1035 if (GetMethod() ==
1036 Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveAllCalleeSaves)) {
1037 // Skip runtime save all callee frames which are used to deliver exceptions.
1038 } else if (instrumentation_frame.interpreter_entry_) {
1039 ArtMethod* callee =
1040 Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs);
1041 CHECK_EQ(GetMethod(), callee)
1042 << "Expected: " << ArtMethod::PrettyMethod(callee)
1043 << " Found: " << ArtMethod::PrettyMethod(GetMethod());
1044 } else {
1045 // Instrumentation generally doesn't distinguish between a method's obsolete and
1046 // non-obsolete version.
1047 CHECK_EQ(instrumentation_frame.method_->GetNonObsoleteMethod(),
1048 GetMethod()->GetNonObsoleteMethod())
1049 << "Expected: "
1050 << ArtMethod::PrettyMethod(
1051 instrumentation_frame.method_->GetNonObsoleteMethod())
1052 << " Found: " << ArtMethod::PrettyMethod(GetMethod()->GetNonObsoleteMethod());
1053 }
1054 return_pc = instrumentation_frame.return_pc_;
1055 // TODO It would be nice to do this check but unfortunately it leads to a race since
1056 // we need to perform a recursive stack-walk to calculate the frame-id.
1057 // size_t expected_id = instrumentation_frame.frame_id_;
1058 // if (num_frames_ != 0) {
1059 // // Check agreement of frame Ids only if num_frames_ is computed to avoid infinite
1060 // // recursion.
1061 // size_t frame_id = instrumentation::Instrumentation::ComputeFrameId(
1062 // thread_,
1063 // // We already incremented depth but we want to get the last visited frame.
1064 // depth - 1,
1065 // inlined_frames_count);
1066 // CHECK_EQ(expected_id, frame_id);
1067 // }
1068 }
1069 }
1070
1071 cur_quick_frame_pc_ = return_pc;
1072 uint8_t* next_frame = reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size;
1073 cur_quick_frame_ = reinterpret_cast<ArtMethod**>(next_frame);
1074
1075 if (kDebugStackWalk) {
1076 LOG(INFO) << ArtMethod::PrettyMethod(method) << "@" << method << " size=" << frame_size
1077 << std::boolalpha << " optimized="
1078 << (cur_oat_quick_method_header_ != nullptr &&
1079 cur_oat_quick_method_header_->IsOptimized())
1080 << " native=" << method->IsNative() << std::noboolalpha
1081 << " entrypoints=" << method->GetEntryPointFromQuickCompiledCode() << ","
1082 << (method->IsNative() ? method->GetEntryPointFromJni() : nullptr)
1083 << " next=" << *cur_quick_frame_;
1084 }
1085
1086 method = *cur_quick_frame_;
Andreas Gampef040be62017-04-14 21:49:33 -07001087 }
Alex Light721e4022020-01-14 14:45:40 -08001088 // We want to start again. Break right now.
1089 if (retry) {
1090 break; // From for (const ManagedStack* current_fragment = thread_->GetManagedStack();
1091 }
1092 } else if (cur_shadow_frame_ != nullptr) {
1093 do {
1094 SanityCheckFrame();
1095 if (!skip_to_depth) {
1096 bool should_continue = VisitFrame();
1097 if (UNLIKELY(!should_continue)) {
1098 return;
1099 }
1100 }
1101 increment_depth();
1102 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
1103 } while (cur_shadow_frame_ != nullptr);
jeffhao6641ea12013-01-02 18:13:42 -08001104 }
Alex Light721e4022020-01-14 14:45:40 -08001105 if (!skip_to_depth && include_transitions) {
Ian Rogers0399dde2012-06-06 17:09:28 -07001106 bool should_continue = VisitFrame();
Alex Light721e4022020-01-14 14:45:40 -08001107 if (!should_continue) {
Ian Rogers0399dde2012-06-06 17:09:28 -07001108 return;
1109 }
Alex Light721e4022020-01-14 14:45:40 -08001110 }
1111 if (kCount == CountTransitions::kYes) {
1112 increment_depth();
Ian Rogers0399dde2012-06-06 17:09:28 -07001113 }
1114 }
Alex Light721e4022020-01-14 14:45:40 -08001115 if (retry) {
1116 continue; // Real retry.
Andreas Gampe585da952016-12-02 14:52:29 -08001117 }
Alex Light721e4022020-01-14 14:45:40 -08001118 if (num_frames_ != 0) {
1119 CHECK_EQ(cur_depth_, num_frames_);
1120 }
1121 } while (retry);
Ian Rogers0399dde2012-06-06 17:09:28 -07001122}
1123
Andreas Gampe585da952016-12-02 14:52:29 -08001124template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kYes>(bool);
1125template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kNo>(bool);
1126
Elliott Hughes68e76522011-10-05 13:22:16 -07001127} // namespace art