blob: eb0fe5692d3674ba9e5787526ac0add561c55e79 [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"
Andreas Gampe8228cdf2017-05-30 15:03:54 -070024#include "base/callee_save_type.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070025#include "base/enums.h"
Dave Allisonf9439142014-03-27 15:10:22 -070026#include "base/hex_dump.h"
David Sehr9e734c72018-01-04 17:56:19 -080027#include "dex/dex_file_types.h"
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010028#include "entrypoints/entrypoint_utils-inl.h"
Vladimir Markod3083dd2018-05-17 08:43:47 +010029#include "entrypoints/quick/callee_save_frame.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070030#include "entrypoints/runtime_asm_entrypoints.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070031#include "gc/space/image_space.h"
32#include "gc/space/space-inl.h"
Nicolas Geoffray0315efa2020-06-26 11:42:39 +010033#include "interpreter/mterp/nterp.h"
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +010034#include "interpreter/shadow_frame-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010035#include "jit/jit.h"
36#include "jit/jit_code_cache.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070037#include "linear_alloc.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070038#include "managed_stack.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070039#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040#include "mirror/object-inl.h"
41#include "mirror/object_array-inl.h"
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +000042#include "nterp_helpers.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010043#include "oat_quick_method_header.h"
Vladimir Marko439d1262019-04-12 14:45:07 +010044#include "obj_ptr-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010045#include "quick/quick_method_frame_info.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070046#include "runtime.h"
Dave Allisonf9439142014-03-27 15:10:22 -070047#include "thread.h"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070048#include "thread_list.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070049
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080050namespace art {
51
Andreas Gampe46ee31b2016-12-14 10:11:49 -080052using android::base::StringPrintf;
53
Mathieu Chartier8405bfd2016-02-05 12:00:49 -080054static constexpr bool kDebugStackWalk = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -070055
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -080056StackVisitor::StackVisitor(Thread* thread,
57 Context* context,
58 StackWalkKind walk_kind,
59 bool check_suspended)
60 : StackVisitor(thread, context, walk_kind, 0, check_suspended) {}
Ian Rogers7a22fa62013-01-23 12:16:16 -080061
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010062StackVisitor::StackVisitor(Thread* thread,
63 Context* context,
64 StackWalkKind walk_kind,
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -080065 size_t num_frames,
66 bool check_suspended)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010067 : thread_(thread),
68 walk_kind_(walk_kind),
69 cur_shadow_frame_(nullptr),
70 cur_quick_frame_(nullptr),
71 cur_quick_frame_pc_(0),
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010072 cur_oat_quick_method_header_(nullptr),
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010073 num_frames_(num_frames),
74 cur_depth_(0),
David Srbecky145a18a2019-06-03 14:35:22 +010075 cur_inline_info_(nullptr, CodeInfo()),
76 cur_stack_map_(0, StackMap()),
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -080077 context_(context),
78 check_suspended_(check_suspended) {
79 if (check_suspended_) {
80 DCHECK(thread == Thread::Current() || thread->IsSuspended()) << *thread;
81 }
Ian Rogers5cf98192014-05-29 21:31:50 -070082}
83
David Srbecky145a18a2019-06-03 14:35:22 +010084CodeInfo* StackVisitor::GetCurrentInlineInfo() const {
85 DCHECK(!(*cur_quick_frame_)->IsNative());
86 const OatQuickMethodHeader* header = GetCurrentOatQuickMethodHeader();
87 if (cur_inline_info_.first != header) {
David Srbecky0d4567f2019-05-30 22:45:40 +010088 cur_inline_info_ = std::make_pair(header, CodeInfo::DecodeInlineInfoOnly(header));
David Srbecky145a18a2019-06-03 14:35:22 +010089 }
90 return &cur_inline_info_.second;
91}
92
93StackMap* StackVisitor::GetCurrentStackMap() const {
94 DCHECK(!(*cur_quick_frame_)->IsNative());
95 const OatQuickMethodHeader* header = GetCurrentOatQuickMethodHeader();
96 if (cur_stack_map_.first != cur_quick_frame_pc_) {
97 uint32_t pc = header->NativeQuickPcOffset(cur_quick_frame_pc_);
98 cur_stack_map_ = std::make_pair(cur_quick_frame_pc_,
99 GetCurrentInlineInfo()->GetStackMapForNativePcOffset(pc));
100 }
101 return &cur_stack_map_.second;
102}
103
Mathieu Chartiere401d142015-04-22 13:56:20 -0700104ArtMethod* StackVisitor::GetMethod() const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100105 if (cur_shadow_frame_ != nullptr) {
106 return cur_shadow_frame_->GetMethod();
107 } else if (cur_quick_frame_ != nullptr) {
108 if (IsInInlinedFrame()) {
David Srbecky145a18a2019-06-03 14:35:22 +0100109 CodeInfo* code_info = GetCurrentInlineInfo();
Mathieu Chartier45bf2502016-03-31 11:07:09 -0700110 DCHECK(walk_kind_ != StackWalkKind::kSkipInlinedFrames);
David Srbecky145a18a2019-06-03 14:35:22 +0100111 return GetResolvedMethod(*GetCurrentQuickFrame(), *code_info, current_inline_frames_);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100112 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700113 return *cur_quick_frame_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100114 }
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100115 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700116 return nullptr;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100117}
118
Dave Allisonb373e092014-02-20 16:06:36 -0800119uint32_t StackVisitor::GetDexPc(bool abort_on_failure) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700120 if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700121 return cur_shadow_frame_->GetDexPC();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700122 } else if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100123 if (IsInInlinedFrame()) {
David Srbecky93bd3612018-07-02 19:30:18 +0100124 return current_inline_frames_.back().GetDexPc();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100125 } else if (cur_oat_quick_method_header_ == nullptr) {
Andreas Gampee2abbc62017-09-15 11:59:26 -0700126 return dex::kDexNoIndex;
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000127 } else if ((*GetCurrentQuickFrame())->IsNative()) {
128 return cur_oat_quick_method_header_->ToDexPc(
129 GetCurrentQuickFrame(), cur_quick_frame_pc_, abort_on_failure);
130 } else if (cur_oat_quick_method_header_->IsOptimized()) {
David Srbecky145a18a2019-06-03 14:35:22 +0100131 StackMap* stack_map = GetCurrentStackMap();
132 DCHECK(stack_map->IsValid());
133 return stack_map->GetDexPc();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100134 } else {
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000135 DCHECK(cur_oat_quick_method_header_->IsNterpMethodHeader());
136 return NterpGetDexPC(cur_quick_frame_);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100137 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700138 } else {
139 return 0;
140 }
141}
142
Mathieu Chartiere401d142015-04-22 13:56:20 -0700143extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700144 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertza836bc92014-11-25 16:30:53 +0100145
Vladimir Markoabedfca2019-05-23 14:07:47 +0100146ObjPtr<mirror::Object> StackVisitor::GetThisObject() const {
Andreas Gampe542451c2016-07-26 09:02:02 -0700147 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700148 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800149 if (m->IsStatic()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100150 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800151 } else if (m->IsNative()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100152 if (cur_quick_frame_ != nullptr) {
Vladimir Markocedec9d2021-02-08 16:16:13 +0000153 // The `this` reference is stored in the first out vreg in the caller's frame.
154 const size_t frame_size = GetCurrentQuickFrameInfo().FrameSizeInBytes();
155 auto* stack_ref = reinterpret_cast<StackReference<mirror::Object>*>(
156 reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size + sizeof(ArtMethod*));
157 return stack_ref->AsMirrorPtr();
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.
Nicolas Geoffray6624d582020-09-01 15:02:00 +0100236 result = GetVRegFromOptimizedCode(location.value(), val);
Nicolas Geoffrayd7651b12019-12-18 14:57:30 +0000237 // 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
Nicolas Geoffray6624d582020-09-01 15:02:00 +0100264bool StackVisitor::GetVRegFromOptimizedCode(ArtMethod* m,
265 uint16_t vreg,
266 VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100267 uint32_t* val) const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100268 DCHECK_EQ(m, GetMethod());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800269 // Can't be null or how would we compile its instructions?
270 DCHECK(m->GetCodeItem() != nullptr) << m->PrettyMethod();
David Sehr0225f8e2018-01-31 08:52:24 +0000271 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800272 uint16_t number_of_dex_registers = accessor.RegistersSize();
273 DCHECK_LT(vreg, number_of_dex_registers);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100274 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
David Srbecky052f8ca2018-04-26 15:42:54 +0100275 CodeInfo code_info(method_header);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100276
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100277 uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_);
David Srbecky052f8ca2018-04-26 15:42:54 +0100278 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100279 DCHECK(stack_map.IsValid());
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100280
281 DexRegisterMap dex_register_map = IsInInlinedFrame()
David Srbecky93bd3612018-07-02 19:30:18 +0100282 ? code_info.GetInlineDexRegisterMapOf(stack_map, current_inline_frames_.back())
David Srbeckyfd89b072018-06-03 12:00:22 +0100283 : code_info.GetDexRegisterMapOf(stack_map);
284 if (dex_register_map.empty()) {
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000285 return false;
286 }
David Srbeckyfd89b072018-06-03 12:00:22 +0100287 DCHECK_EQ(dex_register_map.size(), number_of_dex_registers);
David Srbeckye1402122018-06-13 18:20:45 +0100288 DexRegisterLocation::Kind location_kind = dex_register_map[vreg].GetKind();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100289 switch (location_kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000290 case DexRegisterLocation::Kind::kInStack: {
David Srbeckye1402122018-06-13 18:20:45 +0100291 const int32_t offset = dex_register_map[vreg].GetStackOffsetInBytes();
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100292 BitMemoryRegion stack_mask = code_info.GetStackMaskOf(stack_map);
293 if (kind == kReferenceVReg && !stack_mask.LoadBit(offset / kFrameSlotSize)) {
294 return false;
295 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100296 const uint8_t* addr = reinterpret_cast<const uint8_t*>(cur_quick_frame_) + offset;
297 *val = *reinterpret_cast<const uint32_t*>(addr);
298 return true;
299 }
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100300 case DexRegisterLocation::Kind::kInRegister: {
301 uint32_t register_mask = code_info.GetRegisterMaskOf(stack_map);
302 uint32_t reg = dex_register_map[vreg].GetMachineRegister();
303 if (kind == kReferenceVReg && !(register_mask & (1 << reg))) {
304 return false;
305 }
Nicolas Geoffray6624d582020-09-01 15:02:00 +0100306 return GetRegisterIfAccessible(reg, location_kind, val);
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100307 }
David Brazdild9cb68e2015-08-25 13:52:43 +0100308 case DexRegisterLocation::Kind::kInRegisterHigh:
309 case DexRegisterLocation::Kind::kInFpuRegister:
310 case DexRegisterLocation::Kind::kInFpuRegisterHigh: {
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100311 if (kind == kReferenceVReg) {
312 return false;
313 }
David Srbeckye1402122018-06-13 18:20:45 +0100314 uint32_t reg = dex_register_map[vreg].GetMachineRegister();
Nicolas Geoffray6624d582020-09-01 15:02:00 +0100315 return GetRegisterIfAccessible(reg, location_kind, val);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100316 }
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100317 case DexRegisterLocation::Kind::kConstant: {
318 uint32_t result = dex_register_map[vreg].GetConstant();
319 if (kind == kReferenceVReg && result != 0) {
320 return false;
321 }
322 *val = result;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100323 return true;
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100324 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000325 case DexRegisterLocation::Kind::kNone:
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100326 return false;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000327 default:
David Srbeckye1402122018-06-13 18:20:45 +0100328 LOG(FATAL) << "Unexpected location kind " << dex_register_map[vreg].GetKind();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000329 UNREACHABLE();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100330 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100331}
332
Nicolas Geoffray6624d582020-09-01 15:02:00 +0100333bool StackVisitor::GetVRegFromOptimizedCode(DexRegisterLocation location, uint32_t* val) const {
David Srbeckycffa2542019-07-01 15:31:41 +0100334 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:
Nicolas Geoffray6624d582020-09-01 15:02:00 +0100346 return GetRegisterIfAccessible(location.GetMachineRegister(), location.GetKind(), val);
David Srbeckycffa2542019-07-01 15:31:41 +0100347 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
Nicolas Geoffray6624d582020-09-01 15:02:00 +0100357bool StackVisitor::GetRegisterIfAccessible(uint32_t reg,
358 DexRegisterLocation::Kind location_kind,
359 uint32_t* val) const {
360 const bool is_float = (location_kind == DexRegisterLocation::Kind::kInFpuRegister) ||
361 (location_kind == DexRegisterLocation::Kind::kInFpuRegisterHigh);
David Brazdil77a48ae2015-09-15 12:34:04 +0000362
Vladimir Marko239d6ea2016-09-05 10:44:04 +0100363 if (kRuntimeISA == InstructionSet::kX86 && is_float) {
364 // X86 float registers are 64-bit and each XMM register is provided as two separate
365 // 32-bit registers by the context.
Nicolas Geoffray6624d582020-09-01 15:02:00 +0100366 reg = (location_kind == DexRegisterLocation::Kind::kInFpuRegisterHigh)
367 ? (2 * reg + 1)
368 : (2 * reg);
Vladimir Marko239d6ea2016-09-05 10:44:04 +0100369 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000370
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100371 if (!IsAccessibleRegister(reg, is_float)) {
372 return false;
373 }
374 uintptr_t ptr_val = GetRegister(reg, is_float);
375 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
376 if (target64) {
Nicolas Geoffray6624d582020-09-01 15:02:00 +0100377 const bool is_high = (location_kind == DexRegisterLocation::Kind::kInRegisterHigh) ||
378 (location_kind == DexRegisterLocation::Kind::kInFpuRegisterHigh);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100379 int64_t value_long = static_cast<int64_t>(ptr_val);
Nicolas Geoffray6624d582020-09-01 15:02:00 +0100380 ptr_val = static_cast<uintptr_t>(is_high ? High32Bits(value_long) : Low32Bits(value_long));
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100381 }
382 *val = ptr_val;
383 return true;
384}
385
Mingyao Yang99170c62015-07-06 11:10:37 -0700386bool StackVisitor::GetVRegPairFromDebuggerShadowFrame(uint16_t vreg,
387 VRegKind kind_lo,
388 VRegKind kind_hi,
389 uint64_t* val) const {
390 uint32_t low_32bits;
391 uint32_t high_32bits;
392 bool success = GetVRegFromDebuggerShadowFrame(vreg, kind_lo, &low_32bits);
393 success &= GetVRegFromDebuggerShadowFrame(vreg + 1, kind_hi, &high_32bits);
394 if (success) {
395 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
396 }
397 return success;
398}
399
Mathieu Chartiere401d142015-04-22 13:56:20 -0700400bool StackVisitor::GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200401 VRegKind kind_hi, uint64_t* val) const {
402 if (kind_lo == kLongLoVReg) {
403 DCHECK_EQ(kind_hi, kLongHiVReg);
404 } else if (kind_lo == kDoubleLoVReg) {
405 DCHECK_EQ(kind_hi, kDoubleHiVReg);
406 } else {
407 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100408 UNREACHABLE();
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200409 }
Mingyao Yang99170c62015-07-06 11:10:37 -0700410 // Check if there is value set by the debugger.
411 if (GetVRegPairFromDebuggerShadowFrame(vreg, kind_lo, kind_hi, val)) {
412 return true;
413 }
Nicolas Geoffraycaafd622020-01-27 13:08:45 +0000414 if (cur_quick_frame_ == nullptr) {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100415 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200416 *val = cur_shadow_frame_->GetVRegLong(vreg);
417 return true;
418 }
Nicolas Geoffraycaafd622020-01-27 13:08:45 +0000419 if (cur_oat_quick_method_header_->IsNterpMethodHeader()) {
420 uint64_t val_lo = NterpGetVReg(cur_quick_frame_, vreg);
421 uint64_t val_hi = NterpGetVReg(cur_quick_frame_, vreg + 1);
422 *val = (val_hi << 32) + val_lo;
423 return true;
424 }
425
426 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
427 DCHECK(m == GetMethod());
428 DCHECK(cur_oat_quick_method_header_->IsOptimized());
429 return GetVRegPairFromOptimizedCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200430}
431
Mathieu Chartiere401d142015-04-22 13:56:20 -0700432bool StackVisitor::GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100433 VRegKind kind_lo, VRegKind kind_hi,
434 uint64_t* val) const {
435 uint32_t low_32bits;
436 uint32_t high_32bits;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700437 bool success = GetVRegFromOptimizedCode(m, vreg, kind_lo, &low_32bits);
438 success &= GetVRegFromOptimizedCode(m, vreg + 1, kind_hi, &high_32bits);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100439 if (success) {
440 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
441 }
442 return success;
443}
444
Vladimir Marko439d1262019-04-12 14:45:07 +0100445ShadowFrame* StackVisitor::PrepareSetVReg(ArtMethod* m, uint16_t vreg, bool wide) {
David Sehr0225f8e2018-01-31 08:52:24 +0000446 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800447 if (!accessor.HasCodeItem()) {
Vladimir Marko439d1262019-04-12 14:45:07 +0100448 return nullptr;
Mingyao Yang99170c62015-07-06 11:10:37 -0700449 }
450 ShadowFrame* shadow_frame = GetCurrentShadowFrame();
451 if (shadow_frame == nullptr) {
452 // This is a compiled frame: we must prepare and update a shadow frame that will
453 // be executed by the interpreter after deoptimization of the stack.
454 const size_t frame_id = GetFrameId();
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800455 const uint16_t num_regs = accessor.RegistersSize();
Mingyao Yang99170c62015-07-06 11:10:37 -0700456 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
457 CHECK(shadow_frame != nullptr);
Vladimir Marko439d1262019-04-12 14:45:07 +0100458 // Remember the vreg(s) has been set for debugging and must not be overwritten by the
Mingyao Yang99170c62015-07-06 11:10:37 -0700459 // original value during deoptimization of the stack.
460 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
Vladimir Marko439d1262019-04-12 14:45:07 +0100461 if (wide) {
462 thread_->GetUpdatedVRegFlags(frame_id)[vreg + 1] = true;
463 }
Mingyao Yang99170c62015-07-06 11:10:37 -0700464 }
Vladimir Marko439d1262019-04-12 14:45:07 +0100465 return shadow_frame;
466}
467
468bool StackVisitor::SetVReg(ArtMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind) {
469 DCHECK(kind == kIntVReg || kind == kFloatVReg);
470 ShadowFrame* shadow_frame = PrepareSetVReg(m, vreg, /* wide= */ false);
471 if (shadow_frame == nullptr) {
472 return false;
Mingyao Yang99170c62015-07-06 11:10:37 -0700473 }
Vladimir Marko439d1262019-04-12 14:45:07 +0100474 shadow_frame->SetVReg(vreg, new_value);
475 return true;
476}
477
478bool StackVisitor::SetVRegReference(ArtMethod* m, uint16_t vreg, ObjPtr<mirror::Object> new_value) {
479 ShadowFrame* shadow_frame = PrepareSetVReg(m, vreg, /* wide= */ false);
480 if (shadow_frame == nullptr) {
481 return false;
482 }
483 shadow_frame->SetVRegReference(vreg, new_value);
Mingyao Yang99170c62015-07-06 11:10:37 -0700484 return true;
485}
486
Mingyao Yang636b9252015-07-31 16:40:24 -0700487bool StackVisitor::SetVRegPair(ArtMethod* m,
488 uint16_t vreg,
489 uint64_t new_value,
490 VRegKind kind_lo,
491 VRegKind kind_hi) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700492 if (kind_lo == kLongLoVReg) {
493 DCHECK_EQ(kind_hi, kLongHiVReg);
494 } else if (kind_lo == kDoubleLoVReg) {
495 DCHECK_EQ(kind_hi, kDoubleHiVReg);
496 } else {
497 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
498 UNREACHABLE();
499 }
Vladimir Marko439d1262019-04-12 14:45:07 +0100500 ShadowFrame* shadow_frame = PrepareSetVReg(m, vreg, /* wide= */ true);
Mingyao Yang99170c62015-07-06 11:10:37 -0700501 if (shadow_frame == nullptr) {
Vladimir Marko439d1262019-04-12 14:45:07 +0100502 return false;
Mingyao Yang99170c62015-07-06 11:10:37 -0700503 }
504 shadow_frame->SetVRegLong(vreg, new_value);
505 return true;
506}
507
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100508bool StackVisitor::IsAccessibleGPR(uint32_t reg) const {
509 DCHECK(context_ != nullptr);
510 return context_->IsAccessibleGPR(reg);
511}
512
Mathieu Chartier815873e2014-02-13 18:02:13 -0800513uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100514 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
515 DCHECK(context_ != nullptr);
Mathieu Chartier815873e2014-02-13 18:02:13 -0800516 return context_->GetGPRAddress(reg);
517}
518
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100519uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
520 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
521 DCHECK(context_ != nullptr);
522 return context_->GetGPR(reg);
Ian Rogers0399dde2012-06-06 17:09:28 -0700523}
524
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100525bool StackVisitor::IsAccessibleFPR(uint32_t reg) const {
526 DCHECK(context_ != nullptr);
527 return context_->IsAccessibleFPR(reg);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200528}
529
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100530uintptr_t StackVisitor::GetFPR(uint32_t reg) const {
531 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
532 DCHECK(context_ != nullptr);
533 return context_->GetFPR(reg);
534}
535
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000536uintptr_t StackVisitor::GetReturnPcAddr() const {
537 uintptr_t sp = reinterpret_cast<uintptr_t>(GetCurrentQuickFrame());
538 DCHECK_NE(sp, 0u);
539 return sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
540}
541
Ian Rogers0399dde2012-06-06 17:09:28 -0700542uintptr_t StackVisitor::GetReturnPc() const {
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000543 return *reinterpret_cast<uintptr_t*>(GetReturnPcAddr());
Ian Rogers0399dde2012-06-06 17:09:28 -0700544}
545
546void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000547 *reinterpret_cast<uintptr_t*>(GetReturnPcAddr()) = new_ret_pc;
Ian Rogers0399dde2012-06-06 17:09:28 -0700548}
549
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100550size_t StackVisitor::ComputeNumFrames(Thread* thread, StackWalkKind walk_kind) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700551 struct NumFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100552 NumFramesVisitor(Thread* thread_in, StackWalkKind walk_kind_in)
553 : StackVisitor(thread_in, nullptr, walk_kind_in), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700554
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100555 bool VisitFrame() override {
Ian Rogers0399dde2012-06-06 17:09:28 -0700556 frames++;
557 return true;
558 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700559
Ian Rogers0399dde2012-06-06 17:09:28 -0700560 size_t frames;
561 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100562 NumFramesVisitor visitor(thread, walk_kind);
Ian Rogers0399dde2012-06-06 17:09:28 -0700563 visitor.WalkStack(true);
564 return visitor.frames;
565}
566
Mathieu Chartiere401d142015-04-22 13:56:20 -0700567bool StackVisitor::GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700568 struct HasMoreFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100569 HasMoreFramesVisitor(Thread* thread,
570 StackWalkKind walk_kind,
571 size_t num_frames,
572 size_t frame_height)
573 : StackVisitor(thread, nullptr, walk_kind, num_frames),
574 frame_height_(frame_height),
575 found_frame_(false),
576 has_more_frames_(false),
577 next_method_(nullptr),
578 next_dex_pc_(0) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700579 }
580
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100581 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700582 if (found_frame_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700583 ArtMethod* method = GetMethod();
Ian Rogers5cf98192014-05-29 21:31:50 -0700584 if (method != nullptr && !method->IsRuntimeMethod()) {
585 has_more_frames_ = true;
586 next_method_ = method;
587 next_dex_pc_ = GetDexPc();
588 return false; // End stack walk once next method is found.
589 }
590 } else if (GetFrameHeight() == frame_height_) {
591 found_frame_ = true;
592 }
593 return true;
594 }
595
596 size_t frame_height_;
597 bool found_frame_;
598 bool has_more_frames_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700599 ArtMethod* next_method_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700600 uint32_t next_dex_pc_;
601 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100602 HasMoreFramesVisitor visitor(thread_, walk_kind_, GetNumFrames(), GetFrameHeight());
Ian Rogers5cf98192014-05-29 21:31:50 -0700603 visitor.WalkStack(true);
604 *next_method = visitor.next_method_;
605 *next_dex_pc = visitor.next_dex_pc_;
606 return visitor.has_more_frames_;
607}
608
Ian Rogers7a22fa62013-01-23 12:16:16 -0800609void StackVisitor::DescribeStack(Thread* thread) {
Ian Rogers306057f2012-11-26 12:45:53 -0800610 struct DescribeStackVisitor : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800611 explicit DescribeStackVisitor(Thread* thread_in)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100612 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800613
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100614 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers306057f2012-11-26 12:45:53 -0800615 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation();
616 return true;
617 }
618 };
Ian Rogers7a22fa62013-01-23 12:16:16 -0800619 DescribeStackVisitor visitor(thread);
Ian Rogers306057f2012-11-26 12:45:53 -0800620 visitor.WalkStack(true);
621}
622
Ian Rogers40e3bac2012-11-20 00:09:14 -0800623std::string StackVisitor::DescribeLocation() const {
624 std::string result("Visiting method '");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700625 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700626 if (m == nullptr) {
Ian Rogers306057f2012-11-26 12:45:53 -0800627 return "upcall";
628 }
David Sehr709b0702016-10-13 09:12:37 -0700629 result += m->PrettyMethod();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800630 result += StringPrintf("' at dex PC 0x%04x", GetDexPc());
Ian Rogers40e3bac2012-11-20 00:09:14 -0800631 if (!IsShadowFrame()) {
632 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc()));
633 }
634 return result;
635}
636
Alex Lightdba61482016-12-21 08:20:29 -0800637void StackVisitor::SetMethod(ArtMethod* method) {
638 DCHECK(GetMethod() != nullptr);
639 if (cur_shadow_frame_ != nullptr) {
640 cur_shadow_frame_->SetMethod(method);
641 } else {
642 DCHECK(cur_quick_frame_ != nullptr);
Nicolas Geoffray226805d2018-12-14 10:59:02 +0000643 CHECK(!IsInInlinedFrame()) << "We do not support setting inlined method's ArtMethod: "
644 << GetMethod()->PrettyMethod() << " is inlined into "
645 << GetOuterMethod()->PrettyMethod();
Alex Light1ebe4fe2017-01-30 14:57:11 -0800646 *cur_quick_frame_ = method;
Alex Lightdba61482016-12-21 08:20:29 -0800647 }
648}
649
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100650static void AssertPcIsWithinQuickCode(ArtMethod* method, uintptr_t pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700651 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100652 if (method->IsNative() || method->IsRuntimeMethod() || method->IsProxyMethod()) {
653 return;
654 }
655
656 if (pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) {
657 return;
658 }
659
Mingyao Yang88ca8ba2017-05-23 14:21:07 -0700660 Runtime* runtime = Runtime::Current();
661 if (runtime->UseJitCompilation() &&
662 runtime->GetJit()->GetCodeCache()->ContainsPc(reinterpret_cast<const void*>(pc))) {
663 return;
664 }
665
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100666 const void* code = method->GetEntryPointFromQuickCompiledCode();
Alex Lightdb01a092017-04-03 15:39:55 -0700667 if (code == GetQuickInstrumentationEntryPoint() || code == GetInvokeObsoleteMethodStub()) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100668 return;
669 }
670
671 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
672 if (class_linker->IsQuickToInterpreterBridge(code) ||
673 class_linker->IsQuickResolutionStub(code)) {
674 return;
675 }
676
Calin Juravleffc87072016-04-20 14:22:09 +0100677 if (runtime->UseJitCompilation() && runtime->GetJit()->GetCodeCache()->ContainsPc(code)) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100678 return;
679 }
680
Mingyao Yang063fc772016-08-02 11:02:54 -0700681 uint32_t code_size = OatQuickMethodHeader::FromEntryPoint(code)->GetCodeSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100682 uintptr_t code_start = reinterpret_cast<uintptr_t>(code);
683 CHECK(code_start <= pc && pc <= (code_start + code_size))
David Sehr709b0702016-10-13 09:12:37 -0700684 << method->PrettyMethod()
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100685 << " pc=" << std::hex << pc
Roland Levillain0d5a2812015-11-13 10:07:31 +0000686 << " code_start=" << code_start
687 << " code_size=" << code_size;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100688}
689
Orion Hodson6aaa49d2020-07-28 15:53:04 +0100690void StackVisitor::ValidateFrame() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800691 if (kIsDebugBuild) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700692 ArtMethod* method = GetMethod();
Vladimir Markod93e3742018-07-18 10:58:13 +0100693 ObjPtr<mirror::Class> declaring_class = method->GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700694 // Runtime methods have null declaring class.
695 if (!method->IsRuntimeMethod()) {
696 CHECK(declaring_class != nullptr);
697 CHECK_EQ(declaring_class->GetClass(), declaring_class->GetClass()->GetClass())
698 << declaring_class;
699 } else {
700 CHECK(declaring_class == nullptr);
701 }
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700702 Runtime* const runtime = Runtime::Current();
703 LinearAlloc* const linear_alloc = runtime->GetLinearAlloc();
704 if (!linear_alloc->Contains(method)) {
705 // Check class linker linear allocs.
Nicolas Geoffray25b9c7d2020-09-17 17:34:34 +0100706 // We get the canonical method as copied methods may have been allocated
707 // by a different class loader.
Ulya Trafimovich819b3622019-12-12 17:59:10 +0000708 const PointerSize ptrSize = runtime->GetClassLinker()->GetImagePointerSize();
709 ArtMethod* canonical = method->GetCanonicalMethod(ptrSize);
Vladimir Markod93e3742018-07-18 10:58:13 +0100710 ObjPtr<mirror::Class> klass = canonical->GetDeclaringClass();
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700711 LinearAlloc* const class_linear_alloc = (klass != nullptr)
Mathieu Chartier5b830502016-03-02 10:30:23 -0800712 ? runtime->GetClassLinker()->GetAllocatorForClassLoader(klass->GetClassLoader())
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700713 : linear_alloc;
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100714 if (!class_linear_alloc->Contains(canonical)) {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700715 // Check image space.
716 bool in_image = false;
717 for (auto& space : runtime->GetHeap()->GetContinuousSpaces()) {
718 if (space->IsImageSpace()) {
719 auto* image_space = space->AsImageSpace();
720 const auto& header = image_space->GetImageHeader();
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700721 const ImageSection& methods = header.GetMethodsSection();
722 const ImageSection& runtime_methods = header.GetRuntimeMethodsSection();
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100723 const size_t offset = reinterpret_cast<const uint8_t*>(canonical) - image_space->Begin();
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700724 if (methods.Contains(offset) || runtime_methods.Contains(offset)) {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700725 in_image = true;
726 break;
727 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700728 }
729 }
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100730 CHECK(in_image) << canonical->PrettyMethod() << " not in linear alloc or image";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700731 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700732 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800733 if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100734 AssertPcIsWithinQuickCode(method, cur_quick_frame_pc_);
David Srbecky346fd962020-07-27 16:51:00 +0100735 // Frame consistency checks.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100736 size_t frame_size = GetCurrentQuickFrameInfo().FrameSizeInBytes();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800737 CHECK_NE(frame_size, 0u);
Nicolas Geoffray00391822019-12-10 10:17:23 +0000738 // For compiled code, we could try to have a rough guess at an upper size we expect
739 // to see for a frame:
Andreas Gampe5b417b92014-03-10 14:18:35 -0700740 // 256 registers
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700741 // 2 words HandleScope overhead
Andreas Gampe5b417b92014-03-10 14:18:35 -0700742 // 3+3 register spills
Brian Carlstromed08bd42014-03-19 18:34:17 -0700743 // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word);
Nicolas Geoffray0315efa2020-06-26 11:42:39 +0100744 const size_t kMaxExpectedFrameSize = interpreter::kNterpMaxFrame;
David Sehr709b0702016-10-13 09:12:37 -0700745 CHECK_LE(frame_size, kMaxExpectedFrameSize) << method->PrettyMethod();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100746 size_t return_pc_offset = GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800747 CHECK_LT(return_pc_offset, frame_size);
748 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700749 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700750}
751
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100752QuickMethodFrameInfo StackVisitor::GetCurrentQuickFrameInfo() const {
753 if (cur_oat_quick_method_header_ != nullptr) {
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000754 if (cur_oat_quick_method_header_->IsOptimized()) {
755 return cur_oat_quick_method_header_->GetFrameInfo();
756 } else {
757 DCHECK(cur_oat_quick_method_header_->IsNterpMethodHeader());
758 return NterpFrameInfo(cur_quick_frame_);
759 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100760 }
761
762 ArtMethod* method = GetMethod();
763 Runtime* runtime = Runtime::Current();
764
765 if (method->IsAbstract()) {
Vladimir Markod3083dd2018-05-17 08:43:47 +0100766 return RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100767 }
768
769 // This goes before IsProxyMethod since runtime methods have a null declaring class.
770 if (method->IsRuntimeMethod()) {
771 return runtime->GetRuntimeMethodFrameInfo(method);
772 }
773
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100774 if (method->IsProxyMethod()) {
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000775 // There is only one direct method of a proxy class: the constructor. A direct method is
776 // cloned from the original java.lang.reflect.Proxy and is executed as usual quick
777 // compiled method without any stubs. Therefore the method must have a OatQuickMethodHeader.
778 DCHECK(!method->IsDirect() && !method->IsConstructor())
779 << "Constructors of proxy classes must have a OatQuickMethodHeader";
Vladimir Markod3083dd2018-05-17 08:43:47 +0100780 return RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100781 }
782
Vladimir Markofa458ac2020-02-12 14:08:07 +0000783 // The only remaining cases are for native methods that either
784 // - use the Generic JNI stub, called either directly or through some
785 // (resolution, instrumentation) trampoline; or
786 // - fake a Generic JNI frame in art_jni_dlsym_lookup_critical_stub.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100787 DCHECK(method->IsNative());
Vladimir Markofa458ac2020-02-12 14:08:07 +0000788 if (kIsDebugBuild && !method->IsCriticalNative()) {
Vladimir Marko2196c652017-11-30 16:16:07 +0000789 ClassLinker* class_linker = runtime->GetClassLinker();
790 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(method,
791 kRuntimePointerSize);
792 CHECK(class_linker->IsQuickGenericJniStub(entry_point) ||
793 // The current entrypoint (after filtering out trampolines) may have changed
794 // from GenericJNI to JIT-compiled stub since we have entered this frame.
795 (runtime->GetJit() != nullptr &&
796 runtime->GetJit()->GetCodeCache()->ContainsPc(entry_point))) << method->PrettyMethod();
797 }
Vladimir Marko6e043bb2020-02-10 16:56:54 +0000798 // Generic JNI frame is just like the SaveRefsAndArgs frame.
799 // Note that HandleScope, if any, is below the frame.
800 return RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100801}
802
Mythri Alle5097f832021-11-02 14:52:30 +0000803uint8_t* StackVisitor::GetShouldDeoptimizeFlagAddr() const REQUIRES_SHARED(Locks::mutator_lock_) {
804 DCHECK(GetCurrentOatQuickMethodHeader()->HasShouldDeoptimizeFlag());
805 QuickMethodFrameInfo frame_info = GetCurrentQuickFrameInfo();
806 size_t frame_size = frame_info.FrameSizeInBytes();
807 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
808 size_t core_spill_size =
809 POPCOUNT(frame_info.CoreSpillMask()) * GetBytesPerGprSpillLocation(kRuntimeISA);
810 size_t fpu_spill_size =
811 POPCOUNT(frame_info.FpSpillMask()) * GetBytesPerFprSpillLocation(kRuntimeISA);
812 size_t offset = frame_size - core_spill_size - fpu_spill_size - kShouldDeoptimizeFlagSize;
813 uint8_t* should_deoptimize_addr = sp + offset;
814 DCHECK_EQ(*should_deoptimize_addr & ~static_cast<uint8_t>(DeoptimizeFlagValue::kAll), 0);
815 return should_deoptimize_addr;
816}
817
Andreas Gampe585da952016-12-02 14:52:29 -0800818template <StackVisitor::CountTransitions kCount>
Ian Rogers0399dde2012-06-06 17:09:28 -0700819void StackVisitor::WalkStack(bool include_transitions) {
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -0800820 if (check_suspended_) {
821 DCHECK(thread_ == Thread::Current() || thread_->IsSuspended());
822 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800823 CHECK_EQ(cur_depth_, 0U);
Alex Lighte0c6d432020-01-22 22:04:20 +0000824 size_t inlined_frames_count = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700825
Alex Lighte0c6d432020-01-22 22:04:20 +0000826 for (const ManagedStack* current_fragment = thread_->GetManagedStack();
827 current_fragment != nullptr; current_fragment = current_fragment->GetLink()) {
828 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
829 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
830 cur_quick_frame_pc_ = 0;
Nicolas Geoffray51ad7fe2020-02-04 12:46:47 +0000831 DCHECK(cur_oat_quick_method_header_ == nullptr);
Alex Lighte0c6d432020-01-22 22:04:20 +0000832 if (cur_quick_frame_ != nullptr) { // Handle quick stack frames.
833 // Can't be both a shadow and a quick fragment.
834 DCHECK(current_fragment->GetTopShadowFrame() == nullptr);
835 ArtMethod* method = *cur_quick_frame_;
836 DCHECK(method != nullptr);
837 bool header_retrieved = false;
838 if (method->IsNative()) {
839 // We do not have a PC for the first frame, so we cannot simply use
840 // ArtMethod::GetOatQuickMethodHeader() as we're unable to distinguish there
841 // between GenericJNI frame and JIT-compiled JNI stub; the entrypoint may have
842 // changed since the frame was entered. The top quick frame tag indicates
843 // GenericJNI here, otherwise it's either AOT-compiled or JNI-compiled JNI stub.
844 if (UNLIKELY(current_fragment->GetTopQuickFrameTag())) {
845 // The generic JNI does not have any method header.
846 cur_oat_quick_method_header_ = nullptr;
847 } else {
848 const void* existing_entry_point = method->GetEntryPointFromQuickCompiledCode();
849 CHECK(existing_entry_point != nullptr);
850 Runtime* runtime = Runtime::Current();
851 ClassLinker* class_linker = runtime->GetClassLinker();
852 // Check whether we can quickly get the header from the current entrypoint.
853 if (!class_linker->IsQuickGenericJniStub(existing_entry_point) &&
854 !class_linker->IsQuickResolutionStub(existing_entry_point) &&
855 existing_entry_point != GetQuickInstrumentationEntryPoint()) {
856 cur_oat_quick_method_header_ =
857 OatQuickMethodHeader::FromEntryPoint(existing_entry_point);
Vladimir Marko2196c652017-11-30 16:16:07 +0000858 } else {
Alex Lighte0c6d432020-01-22 22:04:20 +0000859 const void* code = method->GetOatMethodQuickCode(class_linker->GetImagePointerSize());
860 if (code != nullptr) {
861 cur_oat_quick_method_header_ = OatQuickMethodHeader::FromEntryPoint(code);
Vladimir Marko2196c652017-11-30 16:16:07 +0000862 } else {
Alex Lighte0c6d432020-01-22 22:04:20 +0000863 // This must be a JITted JNI stub frame.
864 CHECK(runtime->GetJit() != nullptr);
865 code = runtime->GetJit()->GetCodeCache()->GetJniStubCode(method);
866 CHECK(code != nullptr) << method->PrettyMethod();
867 cur_oat_quick_method_header_ = OatQuickMethodHeader::FromCodePointer(code);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100868 }
869 }
870 }
Alex Lighte0c6d432020-01-22 22:04:20 +0000871 header_retrieved = true;
jeffhao6641ea12013-01-02 18:13:42 -0800872 }
Alex Lighte0c6d432020-01-22 22:04:20 +0000873 while (method != nullptr) {
874 if (!header_retrieved) {
875 cur_oat_quick_method_header_ = method->GetOatQuickMethodHeader(cur_quick_frame_pc_);
876 }
877 header_retrieved = false; // Force header retrieval in next iteration.
Orion Hodson6aaa49d2020-07-28 15:53:04 +0100878 ValidateFrame();
Alex Lighte0c6d432020-01-22 22:04:20 +0000879
880 if ((walk_kind_ == StackWalkKind::kIncludeInlinedFrames)
881 && (cur_oat_quick_method_header_ != nullptr)
882 && cur_oat_quick_method_header_->IsOptimized()
883 && !method->IsNative() // JNI methods cannot have any inlined frames.
884 && CodeInfo::HasInlineInfo(cur_oat_quick_method_header_->GetOptimizedCodeInfoPtr())) {
885 DCHECK_NE(cur_quick_frame_pc_, 0u);
886 CodeInfo* code_info = GetCurrentInlineInfo();
887 StackMap* stack_map = GetCurrentStackMap();
888 if (stack_map->IsValid() && stack_map->HasInlineInfo()) {
889 DCHECK_EQ(current_inline_frames_.size(), 0u);
890 for (current_inline_frames_ = code_info->GetInlineInfosOf(*stack_map);
891 !current_inline_frames_.empty();
892 current_inline_frames_.pop_back()) {
893 bool should_continue = VisitFrame();
894 if (UNLIKELY(!should_continue)) {
895 return;
896 }
897 cur_depth_++;
898 inlined_frames_count++;
899 }
900 }
901 }
902
Ian Rogers0399dde2012-06-06 17:09:28 -0700903 bool should_continue = VisitFrame();
Alex Lighte0c6d432020-01-22 22:04:20 +0000904 if (UNLIKELY(!should_continue)) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700905 return;
906 }
Alex Lighte0c6d432020-01-22 22:04:20 +0000907
908 QuickMethodFrameInfo frame_info = GetCurrentQuickFrameInfo();
909 if (context_ != nullptr) {
910 context_->FillCalleeSaves(reinterpret_cast<uint8_t*>(cur_quick_frame_), frame_info);
911 }
912 // Compute PC for next stack frame from return PC.
913 size_t frame_size = frame_info.FrameSizeInBytes();
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000914 uintptr_t return_pc_addr = GetReturnPcAddr();
Alex Lighte0c6d432020-01-22 22:04:20 +0000915 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
916
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000917 if (UNLIKELY(reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc)) {
Alex Lighte0c6d432020-01-22 22:04:20 +0000918 // While profiling, the return pc is restored from the side stack, except when walking
919 // the stack for an exception where the side stack will be unwound in VisitFrame.
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000920 const std::map<uintptr_t, instrumentation::InstrumentationStackFrame>&
921 instrumentation_stack = *thread_->GetInstrumentationStack();
922 auto it = instrumentation_stack.find(return_pc_addr);
923 CHECK(it != instrumentation_stack.end());
924 const instrumentation::InstrumentationStackFrame& instrumentation_frame = it->second;
925 if (GetMethod() ==
926 Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveAllCalleeSaves)) {
927 // Skip runtime save all callee frames which are used to deliver exceptions.
928 } else if (instrumentation_frame.interpreter_entry_) {
929 ArtMethod* callee =
930 Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs);
931 CHECK_EQ(GetMethod(), callee) << "Expected: " << ArtMethod::PrettyMethod(callee)
932 << " Found: " << ArtMethod::PrettyMethod(GetMethod());
Nicolas Geoffray8feb7eb2020-02-04 09:21:33 +0000933 } else if (!instrumentation_frame.method_->IsRuntimeMethod()) {
934 // Trampolines get replaced with their actual method in the stack,
935 // so don't do the check below for runtime methods.
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000936 // Instrumentation generally doesn't distinguish between a method's obsolete and
937 // non-obsolete version.
938 CHECK_EQ(instrumentation_frame.method_->GetNonObsoleteMethod(),
939 GetMethod()->GetNonObsoleteMethod())
940 << "Expected: "
941 << ArtMethod::PrettyMethod(instrumentation_frame.method_->GetNonObsoleteMethod())
942 << " Found: " << ArtMethod::PrettyMethod(GetMethod()->GetNonObsoleteMethod());
Alex Lighte0c6d432020-01-22 22:04:20 +0000943 }
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000944 return_pc = instrumentation_frame.return_pc_;
Alex Lighte0c6d432020-01-22 22:04:20 +0000945 }
946
947 cur_quick_frame_pc_ = return_pc;
948 uint8_t* next_frame = reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size;
949 cur_quick_frame_ = reinterpret_cast<ArtMethod**>(next_frame);
950
951 if (kDebugStackWalk) {
952 LOG(INFO) << ArtMethod::PrettyMethod(method) << "@" << method << " size=" << frame_size
953 << std::boolalpha
954 << " optimized=" << (cur_oat_quick_method_header_ != nullptr &&
955 cur_oat_quick_method_header_->IsOptimized())
956 << " native=" << method->IsNative()
957 << std::noboolalpha
958 << " entrypoints=" << method->GetEntryPointFromQuickCompiledCode()
959 << "," << (method->IsNative() ? method->GetEntryPointFromJni() : nullptr)
960 << " next=" << *cur_quick_frame_;
961 }
962
963 if (kCount == CountTransitions::kYes || !method->IsRuntimeMethod()) {
964 cur_depth_++;
965 }
966 method = *cur_quick_frame_;
Alex Light721e4022020-01-14 14:45:40 -0800967 }
Nicolas Geoffray51ad7fe2020-02-04 12:46:47 +0000968 // We reached a transition frame, it doesn't have a method header.
969 cur_oat_quick_method_header_ = nullptr;
Alex Lighte0c6d432020-01-22 22:04:20 +0000970 } else if (cur_shadow_frame_ != nullptr) {
971 do {
Orion Hodson6aaa49d2020-07-28 15:53:04 +0100972 ValidateFrame();
Alex Lighte0c6d432020-01-22 22:04:20 +0000973 bool should_continue = VisitFrame();
974 if (UNLIKELY(!should_continue)) {
975 return;
976 }
977 cur_depth_++;
978 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
979 } while (cur_shadow_frame_ != nullptr);
980 }
981 if (include_transitions) {
982 bool should_continue = VisitFrame();
983 if (!should_continue) {
984 return;
Ian Rogers0399dde2012-06-06 17:09:28 -0700985 }
986 }
Alex Lighte0c6d432020-01-22 22:04:20 +0000987 if (kCount == CountTransitions::kYes) {
988 cur_depth_++;
Andreas Gampe585da952016-12-02 14:52:29 -0800989 }
Alex Lighte0c6d432020-01-22 22:04:20 +0000990 }
991 if (num_frames_ != 0) {
992 CHECK_EQ(cur_depth_, num_frames_);
993 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700994}
995
Andreas Gampe585da952016-12-02 14:52:29 -0800996template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kYes>(bool);
997template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kNo>(bool);
998
Elliott Hughes68e76522011-10-05 13:22:16 -0700999} // namespace art