blob: 094c25b97a18a1b6b8f87d109b14b80eee25972c [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 Marko6e043bb2020-02-10 16:56:54 +0000153 HandleScope* hs;
154 if (cur_oat_quick_method_header_ != nullptr) {
155 hs = reinterpret_cast<HandleScope*>(
156 reinterpret_cast<char*>(cur_quick_frame_) + sizeof(ArtMethod*));
157 } else {
158 // GenericJNI frames have the HandleScope under the managed frame.
159 uint32_t shorty_len;
160 const char* shorty = m->GetShorty(&shorty_len);
161 const size_t num_handle_scope_references =
162 /* this */ 1u + std::count(shorty + 1, shorty + shorty_len, 'L');
163 hs = GetGenericJniHandleScope(cur_quick_frame_, num_handle_scope_references);
164 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700165 return hs->GetReference(0);
Ian Rogers62d6c772013-02-27 08:32:07 -0800166 } else {
167 return cur_shadow_frame_->GetVRegReference(0);
168 }
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000169 } else if (m->IsProxyMethod()) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100170 if (cur_quick_frame_ != nullptr) {
171 return artQuickGetProxyThisObject(cur_quick_frame_);
172 } else {
173 return cur_shadow_frame_->GetVRegReference(0);
174 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800175 } else {
David Sehr0225f8e2018-01-31 08:52:24 +0000176 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800177 if (!accessor.HasCodeItem()) {
Ian Rogerse0dcd462014-03-08 15:21:04 -0800178 UNIMPLEMENTED(ERROR) << "Failed to determine this object of abstract or proxy method: "
David Sehr709b0702016-10-13 09:12:37 -0700179 << ArtMethod::PrettyMethod(m);
Ian Rogerse0dcd462014-03-08 15:21:04 -0800180 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800181 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800182 uint16_t reg = accessor.RegistersSize() - accessor.InsSize();
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000183 uint32_t value = 0;
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100184 if (!GetVReg(m, reg, kReferenceVReg, &value)) {
185 return nullptr;
186 }
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000187 return reinterpret_cast<mirror::Object*>(value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800188 }
189 }
190}
191
Ian Rogers0c7abda2012-09-19 13:33:42 -0700192size_t StackVisitor::GetNativePcOffset() const {
193 DCHECK(!IsShadowFrame());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100194 return GetCurrentOatQuickMethodHeader()->NativeQuickPcOffset(cur_quick_frame_pc_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700195}
196
Mingyao Yang99170c62015-07-06 11:10:37 -0700197bool StackVisitor::GetVRegFromDebuggerShadowFrame(uint16_t vreg,
198 VRegKind kind,
199 uint32_t* val) const {
200 size_t frame_id = const_cast<StackVisitor*>(this)->GetFrameId();
201 ShadowFrame* shadow_frame = thread_->FindDebuggerShadowFrame(frame_id);
202 if (shadow_frame != nullptr) {
203 bool* updated_vreg_flags = thread_->GetUpdatedVRegFlags(frame_id);
204 DCHECK(updated_vreg_flags != nullptr);
205 if (updated_vreg_flags[vreg]) {
206 // Value is set by the debugger.
207 if (kind == kReferenceVReg) {
208 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
209 shadow_frame->GetVRegReference(vreg)));
210 } else {
211 *val = shadow_frame->GetVReg(vreg);
212 }
213 return true;
214 }
215 }
216 // No value is set by the debugger.
217 return false;
218}
219
David Srbeckycffa2542019-07-01 15:31:41 +0100220bool StackVisitor::GetVReg(ArtMethod* m,
221 uint16_t vreg,
222 VRegKind kind,
223 uint32_t* val,
224 std::optional<DexRegisterLocation> location) const {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200225 if (cur_quick_frame_ != nullptr) {
226 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800227 DCHECK(m == GetMethod());
Mingyao Yang99170c62015-07-06 11:10:37 -0700228 // Check if there is value set by the debugger.
229 if (GetVRegFromDebuggerShadowFrame(vreg, kind, val)) {
230 return true;
231 }
Nicolas Geoffrayd7651b12019-12-18 14:57:30 +0000232 bool result = false;
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000233 if (cur_oat_quick_method_header_->IsNterpMethodHeader()) {
Nicolas Geoffrayd7651b12019-12-18 14:57:30 +0000234 result = true;
235 *val = (kind == kReferenceVReg)
236 ? NterpGetVRegReference(cur_quick_frame_, vreg)
237 : NterpGetVReg(cur_quick_frame_, vreg);
238 } else {
239 DCHECK(cur_oat_quick_method_header_->IsOptimized());
240 if (location.has_value() && kind != kReferenceVReg) {
241 uint32_t val2 = *val;
242 // The caller already known the register location, so we can use the faster overload
243 // which does not decode the stack maps.
Nicolas Geoffray6624d582020-09-01 15:02:00 +0100244 result = GetVRegFromOptimizedCode(location.value(), val);
Nicolas Geoffrayd7651b12019-12-18 14:57:30 +0000245 // Compare to the slower overload.
246 DCHECK_EQ(result, GetVRegFromOptimizedCode(m, vreg, kind, &val2));
247 DCHECK_EQ(*val, val2);
248 } else {
249 result = GetVRegFromOptimizedCode(m, vreg, kind, val);
250 }
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000251 }
Alex Lightb096c912019-09-25 13:33:06 -0700252 if (kind == kReferenceVReg) {
253 // Perform a read barrier in case we are in a different thread and GC is ongoing.
254 mirror::Object* out = reinterpret_cast<mirror::Object*>(static_cast<uintptr_t>(*val));
255 uintptr_t ptr_out = reinterpret_cast<uintptr_t>(GcRoot<mirror::Object>(out).Read());
256 DCHECK_LT(ptr_out, std::numeric_limits<uint32_t>::max());
257 *val = static_cast<uint32_t>(ptr_out);
258 }
Nicolas Geoffrayd7651b12019-12-18 14:57:30 +0000259 return result;
Ian Rogers0399dde2012-06-06 17:09:28 -0700260 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100261 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertz09687442015-11-17 10:35:39 +0100262 if (kind == kReferenceVReg) {
263 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
264 cur_shadow_frame_->GetVRegReference(vreg)));
265 } else {
266 *val = cur_shadow_frame_->GetVReg(vreg);
267 }
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200268 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700269 }
270}
271
Nicolas Geoffray6624d582020-09-01 15:02:00 +0100272bool StackVisitor::GetVRegFromOptimizedCode(ArtMethod* m,
273 uint16_t vreg,
274 VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100275 uint32_t* val) const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100276 DCHECK_EQ(m, GetMethod());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800277 // Can't be null or how would we compile its instructions?
278 DCHECK(m->GetCodeItem() != nullptr) << m->PrettyMethod();
David Sehr0225f8e2018-01-31 08:52:24 +0000279 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800280 uint16_t number_of_dex_registers = accessor.RegistersSize();
281 DCHECK_LT(vreg, number_of_dex_registers);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100282 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
David Srbecky052f8ca2018-04-26 15:42:54 +0100283 CodeInfo code_info(method_header);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100284
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100285 uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_);
David Srbecky052f8ca2018-04-26 15:42:54 +0100286 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100287 DCHECK(stack_map.IsValid());
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100288
289 DexRegisterMap dex_register_map = IsInInlinedFrame()
David Srbecky93bd3612018-07-02 19:30:18 +0100290 ? code_info.GetInlineDexRegisterMapOf(stack_map, current_inline_frames_.back())
David Srbeckyfd89b072018-06-03 12:00:22 +0100291 : code_info.GetDexRegisterMapOf(stack_map);
292 if (dex_register_map.empty()) {
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000293 return false;
294 }
David Srbeckyfd89b072018-06-03 12:00:22 +0100295 DCHECK_EQ(dex_register_map.size(), number_of_dex_registers);
David Srbeckye1402122018-06-13 18:20:45 +0100296 DexRegisterLocation::Kind location_kind = dex_register_map[vreg].GetKind();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100297 switch (location_kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000298 case DexRegisterLocation::Kind::kInStack: {
David Srbeckye1402122018-06-13 18:20:45 +0100299 const int32_t offset = dex_register_map[vreg].GetStackOffsetInBytes();
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100300 BitMemoryRegion stack_mask = code_info.GetStackMaskOf(stack_map);
301 if (kind == kReferenceVReg && !stack_mask.LoadBit(offset / kFrameSlotSize)) {
302 return false;
303 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100304 const uint8_t* addr = reinterpret_cast<const uint8_t*>(cur_quick_frame_) + offset;
305 *val = *reinterpret_cast<const uint32_t*>(addr);
306 return true;
307 }
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100308 case DexRegisterLocation::Kind::kInRegister: {
309 uint32_t register_mask = code_info.GetRegisterMaskOf(stack_map);
310 uint32_t reg = dex_register_map[vreg].GetMachineRegister();
311 if (kind == kReferenceVReg && !(register_mask & (1 << reg))) {
312 return false;
313 }
Nicolas Geoffray6624d582020-09-01 15:02:00 +0100314 return GetRegisterIfAccessible(reg, location_kind, val);
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100315 }
David Brazdild9cb68e2015-08-25 13:52:43 +0100316 case DexRegisterLocation::Kind::kInRegisterHigh:
317 case DexRegisterLocation::Kind::kInFpuRegister:
318 case DexRegisterLocation::Kind::kInFpuRegisterHigh: {
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100319 if (kind == kReferenceVReg) {
320 return false;
321 }
David Srbeckye1402122018-06-13 18:20:45 +0100322 uint32_t reg = dex_register_map[vreg].GetMachineRegister();
Nicolas Geoffray6624d582020-09-01 15:02:00 +0100323 return GetRegisterIfAccessible(reg, location_kind, val);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100324 }
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100325 case DexRegisterLocation::Kind::kConstant: {
326 uint32_t result = dex_register_map[vreg].GetConstant();
327 if (kind == kReferenceVReg && result != 0) {
328 return false;
329 }
330 *val = result;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100331 return true;
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100332 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000333 case DexRegisterLocation::Kind::kNone:
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100334 return false;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000335 default:
David Srbeckye1402122018-06-13 18:20:45 +0100336 LOG(FATAL) << "Unexpected location kind " << dex_register_map[vreg].GetKind();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000337 UNREACHABLE();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100338 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100339}
340
Nicolas Geoffray6624d582020-09-01 15:02:00 +0100341bool StackVisitor::GetVRegFromOptimizedCode(DexRegisterLocation location, uint32_t* val) const {
David Srbeckycffa2542019-07-01 15:31:41 +0100342 switch (location.GetKind()) {
343 case DexRegisterLocation::Kind::kInvalid:
344 break;
345 case DexRegisterLocation::Kind::kInStack: {
346 const uint8_t* sp = reinterpret_cast<const uint8_t*>(cur_quick_frame_);
347 *val = *reinterpret_cast<const uint32_t*>(sp + location.GetStackOffsetInBytes());
348 return true;
349 }
350 case DexRegisterLocation::Kind::kInRegister:
351 case DexRegisterLocation::Kind::kInRegisterHigh:
352 case DexRegisterLocation::Kind::kInFpuRegister:
353 case DexRegisterLocation::Kind::kInFpuRegisterHigh:
Nicolas Geoffray6624d582020-09-01 15:02:00 +0100354 return GetRegisterIfAccessible(location.GetMachineRegister(), location.GetKind(), val);
David Srbeckycffa2542019-07-01 15:31:41 +0100355 case DexRegisterLocation::Kind::kConstant:
356 *val = location.GetConstant();
357 return true;
358 case DexRegisterLocation::Kind::kNone:
359 return false;
360 }
361 LOG(FATAL) << "Unexpected location kind " << location.GetKind();
362 UNREACHABLE();
363}
364
Nicolas Geoffray6624d582020-09-01 15:02:00 +0100365bool StackVisitor::GetRegisterIfAccessible(uint32_t reg,
366 DexRegisterLocation::Kind location_kind,
367 uint32_t* val) const {
368 const bool is_float = (location_kind == DexRegisterLocation::Kind::kInFpuRegister) ||
369 (location_kind == DexRegisterLocation::Kind::kInFpuRegisterHigh);
David Brazdil77a48ae2015-09-15 12:34:04 +0000370
Vladimir Marko239d6ea2016-09-05 10:44:04 +0100371 if (kRuntimeISA == InstructionSet::kX86 && is_float) {
372 // X86 float registers are 64-bit and each XMM register is provided as two separate
373 // 32-bit registers by the context.
Nicolas Geoffray6624d582020-09-01 15:02:00 +0100374 reg = (location_kind == DexRegisterLocation::Kind::kInFpuRegisterHigh)
375 ? (2 * reg + 1)
376 : (2 * reg);
Vladimir Marko239d6ea2016-09-05 10:44:04 +0100377 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000378
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100379 if (!IsAccessibleRegister(reg, is_float)) {
380 return false;
381 }
382 uintptr_t ptr_val = GetRegister(reg, is_float);
383 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
384 if (target64) {
Nicolas Geoffray6624d582020-09-01 15:02:00 +0100385 const bool is_high = (location_kind == DexRegisterLocation::Kind::kInRegisterHigh) ||
386 (location_kind == DexRegisterLocation::Kind::kInFpuRegisterHigh);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100387 int64_t value_long = static_cast<int64_t>(ptr_val);
Nicolas Geoffray6624d582020-09-01 15:02:00 +0100388 ptr_val = static_cast<uintptr_t>(is_high ? High32Bits(value_long) : Low32Bits(value_long));
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100389 }
390 *val = ptr_val;
391 return true;
392}
393
Mingyao Yang99170c62015-07-06 11:10:37 -0700394bool StackVisitor::GetVRegPairFromDebuggerShadowFrame(uint16_t vreg,
395 VRegKind kind_lo,
396 VRegKind kind_hi,
397 uint64_t* val) const {
398 uint32_t low_32bits;
399 uint32_t high_32bits;
400 bool success = GetVRegFromDebuggerShadowFrame(vreg, kind_lo, &low_32bits);
401 success &= GetVRegFromDebuggerShadowFrame(vreg + 1, kind_hi, &high_32bits);
402 if (success) {
403 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
404 }
405 return success;
406}
407
Mathieu Chartiere401d142015-04-22 13:56:20 -0700408bool StackVisitor::GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200409 VRegKind kind_hi, uint64_t* val) const {
410 if (kind_lo == kLongLoVReg) {
411 DCHECK_EQ(kind_hi, kLongHiVReg);
412 } else if (kind_lo == kDoubleLoVReg) {
413 DCHECK_EQ(kind_hi, kDoubleHiVReg);
414 } else {
415 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100416 UNREACHABLE();
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200417 }
Mingyao Yang99170c62015-07-06 11:10:37 -0700418 // Check if there is value set by the debugger.
419 if (GetVRegPairFromDebuggerShadowFrame(vreg, kind_lo, kind_hi, val)) {
420 return true;
421 }
Nicolas Geoffraycaafd622020-01-27 13:08:45 +0000422 if (cur_quick_frame_ == nullptr) {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100423 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200424 *val = cur_shadow_frame_->GetVRegLong(vreg);
425 return true;
426 }
Nicolas Geoffraycaafd622020-01-27 13:08:45 +0000427 if (cur_oat_quick_method_header_->IsNterpMethodHeader()) {
428 uint64_t val_lo = NterpGetVReg(cur_quick_frame_, vreg);
429 uint64_t val_hi = NterpGetVReg(cur_quick_frame_, vreg + 1);
430 *val = (val_hi << 32) + val_lo;
431 return true;
432 }
433
434 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
435 DCHECK(m == GetMethod());
436 DCHECK(cur_oat_quick_method_header_->IsOptimized());
437 return GetVRegPairFromOptimizedCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200438}
439
Mathieu Chartiere401d142015-04-22 13:56:20 -0700440bool StackVisitor::GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100441 VRegKind kind_lo, VRegKind kind_hi,
442 uint64_t* val) const {
443 uint32_t low_32bits;
444 uint32_t high_32bits;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700445 bool success = GetVRegFromOptimizedCode(m, vreg, kind_lo, &low_32bits);
446 success &= GetVRegFromOptimizedCode(m, vreg + 1, kind_hi, &high_32bits);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100447 if (success) {
448 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
449 }
450 return success;
451}
452
Vladimir Marko439d1262019-04-12 14:45:07 +0100453ShadowFrame* StackVisitor::PrepareSetVReg(ArtMethod* m, uint16_t vreg, bool wide) {
David Sehr0225f8e2018-01-31 08:52:24 +0000454 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800455 if (!accessor.HasCodeItem()) {
Vladimir Marko439d1262019-04-12 14:45:07 +0100456 return nullptr;
Mingyao Yang99170c62015-07-06 11:10:37 -0700457 }
458 ShadowFrame* shadow_frame = GetCurrentShadowFrame();
459 if (shadow_frame == nullptr) {
460 // This is a compiled frame: we must prepare and update a shadow frame that will
461 // be executed by the interpreter after deoptimization of the stack.
462 const size_t frame_id = GetFrameId();
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800463 const uint16_t num_regs = accessor.RegistersSize();
Mingyao Yang99170c62015-07-06 11:10:37 -0700464 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
465 CHECK(shadow_frame != nullptr);
Vladimir Marko439d1262019-04-12 14:45:07 +0100466 // Remember the vreg(s) has been set for debugging and must not be overwritten by the
Mingyao Yang99170c62015-07-06 11:10:37 -0700467 // original value during deoptimization of the stack.
468 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
Vladimir Marko439d1262019-04-12 14:45:07 +0100469 if (wide) {
470 thread_->GetUpdatedVRegFlags(frame_id)[vreg + 1] = true;
471 }
Mingyao Yang99170c62015-07-06 11:10:37 -0700472 }
Vladimir Marko439d1262019-04-12 14:45:07 +0100473 return shadow_frame;
474}
475
476bool StackVisitor::SetVReg(ArtMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind) {
477 DCHECK(kind == kIntVReg || kind == kFloatVReg);
478 ShadowFrame* shadow_frame = PrepareSetVReg(m, vreg, /* wide= */ false);
479 if (shadow_frame == nullptr) {
480 return false;
Mingyao Yang99170c62015-07-06 11:10:37 -0700481 }
Vladimir Marko439d1262019-04-12 14:45:07 +0100482 shadow_frame->SetVReg(vreg, new_value);
483 return true;
484}
485
486bool StackVisitor::SetVRegReference(ArtMethod* m, uint16_t vreg, ObjPtr<mirror::Object> new_value) {
487 ShadowFrame* shadow_frame = PrepareSetVReg(m, vreg, /* wide= */ false);
488 if (shadow_frame == nullptr) {
489 return false;
490 }
491 shadow_frame->SetVRegReference(vreg, new_value);
Mingyao Yang99170c62015-07-06 11:10:37 -0700492 return true;
493}
494
Mingyao Yang636b9252015-07-31 16:40:24 -0700495bool StackVisitor::SetVRegPair(ArtMethod* m,
496 uint16_t vreg,
497 uint64_t new_value,
498 VRegKind kind_lo,
499 VRegKind kind_hi) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700500 if (kind_lo == kLongLoVReg) {
501 DCHECK_EQ(kind_hi, kLongHiVReg);
502 } else if (kind_lo == kDoubleLoVReg) {
503 DCHECK_EQ(kind_hi, kDoubleHiVReg);
504 } else {
505 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
506 UNREACHABLE();
507 }
Vladimir Marko439d1262019-04-12 14:45:07 +0100508 ShadowFrame* shadow_frame = PrepareSetVReg(m, vreg, /* wide= */ true);
Mingyao Yang99170c62015-07-06 11:10:37 -0700509 if (shadow_frame == nullptr) {
Vladimir Marko439d1262019-04-12 14:45:07 +0100510 return false;
Mingyao Yang99170c62015-07-06 11:10:37 -0700511 }
512 shadow_frame->SetVRegLong(vreg, new_value);
513 return true;
514}
515
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100516bool StackVisitor::IsAccessibleGPR(uint32_t reg) const {
517 DCHECK(context_ != nullptr);
518 return context_->IsAccessibleGPR(reg);
519}
520
Mathieu Chartier815873e2014-02-13 18:02:13 -0800521uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100522 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
523 DCHECK(context_ != nullptr);
Mathieu Chartier815873e2014-02-13 18:02:13 -0800524 return context_->GetGPRAddress(reg);
525}
526
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100527uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
528 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
529 DCHECK(context_ != nullptr);
530 return context_->GetGPR(reg);
Ian Rogers0399dde2012-06-06 17:09:28 -0700531}
532
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100533bool StackVisitor::IsAccessibleFPR(uint32_t reg) const {
534 DCHECK(context_ != nullptr);
535 return context_->IsAccessibleFPR(reg);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200536}
537
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100538uintptr_t StackVisitor::GetFPR(uint32_t reg) const {
539 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
540 DCHECK(context_ != nullptr);
541 return context_->GetFPR(reg);
542}
543
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000544uintptr_t StackVisitor::GetReturnPcAddr() const {
545 uintptr_t sp = reinterpret_cast<uintptr_t>(GetCurrentQuickFrame());
546 DCHECK_NE(sp, 0u);
547 return sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
548}
549
Ian Rogers0399dde2012-06-06 17:09:28 -0700550uintptr_t StackVisitor::GetReturnPc() const {
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000551 return *reinterpret_cast<uintptr_t*>(GetReturnPcAddr());
Ian Rogers0399dde2012-06-06 17:09:28 -0700552}
553
554void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000555 *reinterpret_cast<uintptr_t*>(GetReturnPcAddr()) = new_ret_pc;
Ian Rogers0399dde2012-06-06 17:09:28 -0700556}
557
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100558size_t StackVisitor::ComputeNumFrames(Thread* thread, StackWalkKind walk_kind) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700559 struct NumFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100560 NumFramesVisitor(Thread* thread_in, StackWalkKind walk_kind_in)
561 : StackVisitor(thread_in, nullptr, walk_kind_in), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700562
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100563 bool VisitFrame() override {
Ian Rogers0399dde2012-06-06 17:09:28 -0700564 frames++;
565 return true;
566 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700567
Ian Rogers0399dde2012-06-06 17:09:28 -0700568 size_t frames;
569 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100570 NumFramesVisitor visitor(thread, walk_kind);
Ian Rogers0399dde2012-06-06 17:09:28 -0700571 visitor.WalkStack(true);
572 return visitor.frames;
573}
574
Mathieu Chartiere401d142015-04-22 13:56:20 -0700575bool StackVisitor::GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700576 struct HasMoreFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100577 HasMoreFramesVisitor(Thread* thread,
578 StackWalkKind walk_kind,
579 size_t num_frames,
580 size_t frame_height)
581 : StackVisitor(thread, nullptr, walk_kind, num_frames),
582 frame_height_(frame_height),
583 found_frame_(false),
584 has_more_frames_(false),
585 next_method_(nullptr),
586 next_dex_pc_(0) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700587 }
588
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100589 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700590 if (found_frame_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700591 ArtMethod* method = GetMethod();
Ian Rogers5cf98192014-05-29 21:31:50 -0700592 if (method != nullptr && !method->IsRuntimeMethod()) {
593 has_more_frames_ = true;
594 next_method_ = method;
595 next_dex_pc_ = GetDexPc();
596 return false; // End stack walk once next method is found.
597 }
598 } else if (GetFrameHeight() == frame_height_) {
599 found_frame_ = true;
600 }
601 return true;
602 }
603
604 size_t frame_height_;
605 bool found_frame_;
606 bool has_more_frames_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700607 ArtMethod* next_method_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700608 uint32_t next_dex_pc_;
609 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100610 HasMoreFramesVisitor visitor(thread_, walk_kind_, GetNumFrames(), GetFrameHeight());
Ian Rogers5cf98192014-05-29 21:31:50 -0700611 visitor.WalkStack(true);
612 *next_method = visitor.next_method_;
613 *next_dex_pc = visitor.next_dex_pc_;
614 return visitor.has_more_frames_;
615}
616
Ian Rogers7a22fa62013-01-23 12:16:16 -0800617void StackVisitor::DescribeStack(Thread* thread) {
Ian Rogers306057f2012-11-26 12:45:53 -0800618 struct DescribeStackVisitor : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800619 explicit DescribeStackVisitor(Thread* thread_in)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100620 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800621
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100622 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers306057f2012-11-26 12:45:53 -0800623 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation();
624 return true;
625 }
626 };
Ian Rogers7a22fa62013-01-23 12:16:16 -0800627 DescribeStackVisitor visitor(thread);
Ian Rogers306057f2012-11-26 12:45:53 -0800628 visitor.WalkStack(true);
629}
630
Ian Rogers40e3bac2012-11-20 00:09:14 -0800631std::string StackVisitor::DescribeLocation() const {
632 std::string result("Visiting method '");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700633 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700634 if (m == nullptr) {
Ian Rogers306057f2012-11-26 12:45:53 -0800635 return "upcall";
636 }
David Sehr709b0702016-10-13 09:12:37 -0700637 result += m->PrettyMethod();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800638 result += StringPrintf("' at dex PC 0x%04x", GetDexPc());
Ian Rogers40e3bac2012-11-20 00:09:14 -0800639 if (!IsShadowFrame()) {
640 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc()));
641 }
642 return result;
643}
644
Alex Lightdba61482016-12-21 08:20:29 -0800645void StackVisitor::SetMethod(ArtMethod* method) {
646 DCHECK(GetMethod() != nullptr);
647 if (cur_shadow_frame_ != nullptr) {
648 cur_shadow_frame_->SetMethod(method);
649 } else {
650 DCHECK(cur_quick_frame_ != nullptr);
Nicolas Geoffray226805d2018-12-14 10:59:02 +0000651 CHECK(!IsInInlinedFrame()) << "We do not support setting inlined method's ArtMethod: "
652 << GetMethod()->PrettyMethod() << " is inlined into "
653 << GetOuterMethod()->PrettyMethod();
Alex Light1ebe4fe2017-01-30 14:57:11 -0800654 *cur_quick_frame_ = method;
Alex Lightdba61482016-12-21 08:20:29 -0800655 }
656}
657
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100658static void AssertPcIsWithinQuickCode(ArtMethod* method, uintptr_t pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700659 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100660 if (method->IsNative() || method->IsRuntimeMethod() || method->IsProxyMethod()) {
661 return;
662 }
663
664 if (pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) {
665 return;
666 }
667
Mingyao Yang88ca8ba2017-05-23 14:21:07 -0700668 Runtime* runtime = Runtime::Current();
669 if (runtime->UseJitCompilation() &&
670 runtime->GetJit()->GetCodeCache()->ContainsPc(reinterpret_cast<const void*>(pc))) {
671 return;
672 }
673
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100674 const void* code = method->GetEntryPointFromQuickCompiledCode();
Alex Lightdb01a092017-04-03 15:39:55 -0700675 if (code == GetQuickInstrumentationEntryPoint() || code == GetInvokeObsoleteMethodStub()) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100676 return;
677 }
678
679 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
680 if (class_linker->IsQuickToInterpreterBridge(code) ||
681 class_linker->IsQuickResolutionStub(code)) {
682 return;
683 }
684
Calin Juravleffc87072016-04-20 14:22:09 +0100685 if (runtime->UseJitCompilation() && runtime->GetJit()->GetCodeCache()->ContainsPc(code)) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100686 return;
687 }
688
Mingyao Yang063fc772016-08-02 11:02:54 -0700689 uint32_t code_size = OatQuickMethodHeader::FromEntryPoint(code)->GetCodeSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100690 uintptr_t code_start = reinterpret_cast<uintptr_t>(code);
691 CHECK(code_start <= pc && pc <= (code_start + code_size))
David Sehr709b0702016-10-13 09:12:37 -0700692 << method->PrettyMethod()
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100693 << " pc=" << std::hex << pc
Roland Levillain0d5a2812015-11-13 10:07:31 +0000694 << " code_start=" << code_start
695 << " code_size=" << code_size;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100696}
697
Orion Hodson6aaa49d2020-07-28 15:53:04 +0100698void StackVisitor::ValidateFrame() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800699 if (kIsDebugBuild) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700700 ArtMethod* method = GetMethod();
Vladimir Markod93e3742018-07-18 10:58:13 +0100701 ObjPtr<mirror::Class> declaring_class = method->GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700702 // Runtime methods have null declaring class.
703 if (!method->IsRuntimeMethod()) {
704 CHECK(declaring_class != nullptr);
705 CHECK_EQ(declaring_class->GetClass(), declaring_class->GetClass()->GetClass())
706 << declaring_class;
707 } else {
708 CHECK(declaring_class == nullptr);
709 }
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700710 Runtime* const runtime = Runtime::Current();
711 LinearAlloc* const linear_alloc = runtime->GetLinearAlloc();
712 if (!linear_alloc->Contains(method)) {
713 // Check class linker linear allocs.
Nicolas Geoffray25b9c7d2020-09-17 17:34:34 +0100714 // We get the canonical method as copied methods may have been allocated
715 // by a different class loader.
Ulya Trafimovich819b3622019-12-12 17:59:10 +0000716 const PointerSize ptrSize = runtime->GetClassLinker()->GetImagePointerSize();
717 ArtMethod* canonical = method->GetCanonicalMethod(ptrSize);
Vladimir Markod93e3742018-07-18 10:58:13 +0100718 ObjPtr<mirror::Class> klass = canonical->GetDeclaringClass();
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700719 LinearAlloc* const class_linear_alloc = (klass != nullptr)
Mathieu Chartier5b830502016-03-02 10:30:23 -0800720 ? runtime->GetClassLinker()->GetAllocatorForClassLoader(klass->GetClassLoader())
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700721 : linear_alloc;
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100722 if (!class_linear_alloc->Contains(canonical)) {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700723 // Check image space.
724 bool in_image = false;
725 for (auto& space : runtime->GetHeap()->GetContinuousSpaces()) {
726 if (space->IsImageSpace()) {
727 auto* image_space = space->AsImageSpace();
728 const auto& header = image_space->GetImageHeader();
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700729 const ImageSection& methods = header.GetMethodsSection();
730 const ImageSection& runtime_methods = header.GetRuntimeMethodsSection();
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100731 const size_t offset = reinterpret_cast<const uint8_t*>(canonical) - image_space->Begin();
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700732 if (methods.Contains(offset) || runtime_methods.Contains(offset)) {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700733 in_image = true;
734 break;
735 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700736 }
737 }
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100738 CHECK(in_image) << canonical->PrettyMethod() << " not in linear alloc or image";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700739 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700740 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800741 if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100742 AssertPcIsWithinQuickCode(method, cur_quick_frame_pc_);
David Srbecky346fd962020-07-27 16:51:00 +0100743 // Frame consistency checks.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100744 size_t frame_size = GetCurrentQuickFrameInfo().FrameSizeInBytes();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800745 CHECK_NE(frame_size, 0u);
Nicolas Geoffray00391822019-12-10 10:17:23 +0000746 // For compiled code, we could try to have a rough guess at an upper size we expect
747 // to see for a frame:
Andreas Gampe5b417b92014-03-10 14:18:35 -0700748 // 256 registers
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700749 // 2 words HandleScope overhead
Andreas Gampe5b417b92014-03-10 14:18:35 -0700750 // 3+3 register spills
Brian Carlstromed08bd42014-03-19 18:34:17 -0700751 // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word);
Nicolas Geoffray0315efa2020-06-26 11:42:39 +0100752 const size_t kMaxExpectedFrameSize = interpreter::kNterpMaxFrame;
David Sehr709b0702016-10-13 09:12:37 -0700753 CHECK_LE(frame_size, kMaxExpectedFrameSize) << method->PrettyMethod();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100754 size_t return_pc_offset = GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800755 CHECK_LT(return_pc_offset, frame_size);
756 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700757 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700758}
759
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100760QuickMethodFrameInfo StackVisitor::GetCurrentQuickFrameInfo() const {
761 if (cur_oat_quick_method_header_ != nullptr) {
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000762 if (cur_oat_quick_method_header_->IsOptimized()) {
763 return cur_oat_quick_method_header_->GetFrameInfo();
764 } else {
765 DCHECK(cur_oat_quick_method_header_->IsNterpMethodHeader());
766 return NterpFrameInfo(cur_quick_frame_);
767 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100768 }
769
770 ArtMethod* method = GetMethod();
771 Runtime* runtime = Runtime::Current();
772
773 if (method->IsAbstract()) {
Vladimir Markod3083dd2018-05-17 08:43:47 +0100774 return RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100775 }
776
777 // This goes before IsProxyMethod since runtime methods have a null declaring class.
778 if (method->IsRuntimeMethod()) {
779 return runtime->GetRuntimeMethodFrameInfo(method);
780 }
781
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100782 if (method->IsProxyMethod()) {
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000783 // There is only one direct method of a proxy class: the constructor. A direct method is
784 // cloned from the original java.lang.reflect.Proxy and is executed as usual quick
785 // compiled method without any stubs. Therefore the method must have a OatQuickMethodHeader.
786 DCHECK(!method->IsDirect() && !method->IsConstructor())
787 << "Constructors of proxy classes must have a OatQuickMethodHeader";
Vladimir Markod3083dd2018-05-17 08:43:47 +0100788 return RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100789 }
790
Vladimir Markofa458ac2020-02-12 14:08:07 +0000791 // The only remaining cases are for native methods that either
792 // - use the Generic JNI stub, called either directly or through some
793 // (resolution, instrumentation) trampoline; or
794 // - fake a Generic JNI frame in art_jni_dlsym_lookup_critical_stub.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100795 DCHECK(method->IsNative());
Vladimir Markofa458ac2020-02-12 14:08:07 +0000796 if (kIsDebugBuild && !method->IsCriticalNative()) {
Vladimir Marko2196c652017-11-30 16:16:07 +0000797 ClassLinker* class_linker = runtime->GetClassLinker();
798 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(method,
799 kRuntimePointerSize);
800 CHECK(class_linker->IsQuickGenericJniStub(entry_point) ||
801 // The current entrypoint (after filtering out trampolines) may have changed
802 // from GenericJNI to JIT-compiled stub since we have entered this frame.
803 (runtime->GetJit() != nullptr &&
804 runtime->GetJit()->GetCodeCache()->ContainsPc(entry_point))) << method->PrettyMethod();
805 }
Vladimir Marko6e043bb2020-02-10 16:56:54 +0000806 // Generic JNI frame is just like the SaveRefsAndArgs frame.
807 // Note that HandleScope, if any, is below the frame.
808 return RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100809}
810
Andreas Gampe585da952016-12-02 14:52:29 -0800811template <StackVisitor::CountTransitions kCount>
Ian Rogers0399dde2012-06-06 17:09:28 -0700812void StackVisitor::WalkStack(bool include_transitions) {
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -0800813 if (check_suspended_) {
814 DCHECK(thread_ == Thread::Current() || thread_->IsSuspended());
815 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800816 CHECK_EQ(cur_depth_, 0U);
Alex Lighte0c6d432020-01-22 22:04:20 +0000817 size_t inlined_frames_count = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700818
Alex Lighte0c6d432020-01-22 22:04:20 +0000819 for (const ManagedStack* current_fragment = thread_->GetManagedStack();
820 current_fragment != nullptr; current_fragment = current_fragment->GetLink()) {
821 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
822 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
823 cur_quick_frame_pc_ = 0;
Nicolas Geoffray51ad7fe2020-02-04 12:46:47 +0000824 DCHECK(cur_oat_quick_method_header_ == nullptr);
Alex Lighte0c6d432020-01-22 22:04:20 +0000825 if (cur_quick_frame_ != nullptr) { // Handle quick stack frames.
826 // Can't be both a shadow and a quick fragment.
827 DCHECK(current_fragment->GetTopShadowFrame() == nullptr);
828 ArtMethod* method = *cur_quick_frame_;
829 DCHECK(method != nullptr);
830 bool header_retrieved = false;
831 if (method->IsNative()) {
832 // We do not have a PC for the first frame, so we cannot simply use
833 // ArtMethod::GetOatQuickMethodHeader() as we're unable to distinguish there
834 // between GenericJNI frame and JIT-compiled JNI stub; the entrypoint may have
835 // changed since the frame was entered. The top quick frame tag indicates
836 // GenericJNI here, otherwise it's either AOT-compiled or JNI-compiled JNI stub.
837 if (UNLIKELY(current_fragment->GetTopQuickFrameTag())) {
838 // The generic JNI does not have any method header.
839 cur_oat_quick_method_header_ = nullptr;
840 } else {
841 const void* existing_entry_point = method->GetEntryPointFromQuickCompiledCode();
842 CHECK(existing_entry_point != nullptr);
843 Runtime* runtime = Runtime::Current();
844 ClassLinker* class_linker = runtime->GetClassLinker();
845 // Check whether we can quickly get the header from the current entrypoint.
846 if (!class_linker->IsQuickGenericJniStub(existing_entry_point) &&
847 !class_linker->IsQuickResolutionStub(existing_entry_point) &&
848 existing_entry_point != GetQuickInstrumentationEntryPoint()) {
849 cur_oat_quick_method_header_ =
850 OatQuickMethodHeader::FromEntryPoint(existing_entry_point);
Vladimir Marko2196c652017-11-30 16:16:07 +0000851 } else {
Alex Lighte0c6d432020-01-22 22:04:20 +0000852 const void* code = method->GetOatMethodQuickCode(class_linker->GetImagePointerSize());
853 if (code != nullptr) {
854 cur_oat_quick_method_header_ = OatQuickMethodHeader::FromEntryPoint(code);
Vladimir Marko2196c652017-11-30 16:16:07 +0000855 } else {
Alex Lighte0c6d432020-01-22 22:04:20 +0000856 // This must be a JITted JNI stub frame.
857 CHECK(runtime->GetJit() != nullptr);
858 code = runtime->GetJit()->GetCodeCache()->GetJniStubCode(method);
859 CHECK(code != nullptr) << method->PrettyMethod();
860 cur_oat_quick_method_header_ = OatQuickMethodHeader::FromCodePointer(code);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100861 }
862 }
863 }
Alex Lighte0c6d432020-01-22 22:04:20 +0000864 header_retrieved = true;
jeffhao6641ea12013-01-02 18:13:42 -0800865 }
Alex Lighte0c6d432020-01-22 22:04:20 +0000866 while (method != nullptr) {
867 if (!header_retrieved) {
868 cur_oat_quick_method_header_ = method->GetOatQuickMethodHeader(cur_quick_frame_pc_);
869 }
870 header_retrieved = false; // Force header retrieval in next iteration.
Orion Hodson6aaa49d2020-07-28 15:53:04 +0100871 ValidateFrame();
Alex Lighte0c6d432020-01-22 22:04:20 +0000872
873 if ((walk_kind_ == StackWalkKind::kIncludeInlinedFrames)
874 && (cur_oat_quick_method_header_ != nullptr)
875 && cur_oat_quick_method_header_->IsOptimized()
876 && !method->IsNative() // JNI methods cannot have any inlined frames.
877 && CodeInfo::HasInlineInfo(cur_oat_quick_method_header_->GetOptimizedCodeInfoPtr())) {
878 DCHECK_NE(cur_quick_frame_pc_, 0u);
879 CodeInfo* code_info = GetCurrentInlineInfo();
880 StackMap* stack_map = GetCurrentStackMap();
881 if (stack_map->IsValid() && stack_map->HasInlineInfo()) {
882 DCHECK_EQ(current_inline_frames_.size(), 0u);
883 for (current_inline_frames_ = code_info->GetInlineInfosOf(*stack_map);
884 !current_inline_frames_.empty();
885 current_inline_frames_.pop_back()) {
886 bool should_continue = VisitFrame();
887 if (UNLIKELY(!should_continue)) {
888 return;
889 }
890 cur_depth_++;
891 inlined_frames_count++;
892 }
893 }
894 }
895
Ian Rogers0399dde2012-06-06 17:09:28 -0700896 bool should_continue = VisitFrame();
Alex Lighte0c6d432020-01-22 22:04:20 +0000897 if (UNLIKELY(!should_continue)) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700898 return;
899 }
Alex Lighte0c6d432020-01-22 22:04:20 +0000900
901 QuickMethodFrameInfo frame_info = GetCurrentQuickFrameInfo();
902 if (context_ != nullptr) {
903 context_->FillCalleeSaves(reinterpret_cast<uint8_t*>(cur_quick_frame_), frame_info);
904 }
905 // Compute PC for next stack frame from return PC.
906 size_t frame_size = frame_info.FrameSizeInBytes();
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000907 uintptr_t return_pc_addr = GetReturnPcAddr();
Alex Lighte0c6d432020-01-22 22:04:20 +0000908 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
909
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000910 if (UNLIKELY(reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc)) {
Alex Lighte0c6d432020-01-22 22:04:20 +0000911 // While profiling, the return pc is restored from the side stack, except when walking
912 // the stack for an exception where the side stack will be unwound in VisitFrame.
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000913 const std::map<uintptr_t, instrumentation::InstrumentationStackFrame>&
914 instrumentation_stack = *thread_->GetInstrumentationStack();
915 auto it = instrumentation_stack.find(return_pc_addr);
916 CHECK(it != instrumentation_stack.end());
917 const instrumentation::InstrumentationStackFrame& instrumentation_frame = it->second;
918 if (GetMethod() ==
919 Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveAllCalleeSaves)) {
920 // Skip runtime save all callee frames which are used to deliver exceptions.
921 } else if (instrumentation_frame.interpreter_entry_) {
922 ArtMethod* callee =
923 Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs);
924 CHECK_EQ(GetMethod(), callee) << "Expected: " << ArtMethod::PrettyMethod(callee)
925 << " Found: " << ArtMethod::PrettyMethod(GetMethod());
Nicolas Geoffray8feb7eb2020-02-04 09:21:33 +0000926 } else if (!instrumentation_frame.method_->IsRuntimeMethod()) {
927 // Trampolines get replaced with their actual method in the stack,
928 // so don't do the check below for runtime methods.
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000929 // Instrumentation generally doesn't distinguish between a method's obsolete and
930 // non-obsolete version.
931 CHECK_EQ(instrumentation_frame.method_->GetNonObsoleteMethod(),
932 GetMethod()->GetNonObsoleteMethod())
933 << "Expected: "
934 << ArtMethod::PrettyMethod(instrumentation_frame.method_->GetNonObsoleteMethod())
935 << " Found: " << ArtMethod::PrettyMethod(GetMethod()->GetNonObsoleteMethod());
Alex Lighte0c6d432020-01-22 22:04:20 +0000936 }
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000937 return_pc = instrumentation_frame.return_pc_;
Alex Lighte0c6d432020-01-22 22:04:20 +0000938 }
939
940 cur_quick_frame_pc_ = return_pc;
941 uint8_t* next_frame = reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size;
942 cur_quick_frame_ = reinterpret_cast<ArtMethod**>(next_frame);
943
944 if (kDebugStackWalk) {
945 LOG(INFO) << ArtMethod::PrettyMethod(method) << "@" << method << " size=" << frame_size
946 << std::boolalpha
947 << " optimized=" << (cur_oat_quick_method_header_ != nullptr &&
948 cur_oat_quick_method_header_->IsOptimized())
949 << " native=" << method->IsNative()
950 << std::noboolalpha
951 << " entrypoints=" << method->GetEntryPointFromQuickCompiledCode()
952 << "," << (method->IsNative() ? method->GetEntryPointFromJni() : nullptr)
953 << " next=" << *cur_quick_frame_;
954 }
955
956 if (kCount == CountTransitions::kYes || !method->IsRuntimeMethod()) {
957 cur_depth_++;
958 }
959 method = *cur_quick_frame_;
Alex Light721e4022020-01-14 14:45:40 -0800960 }
Nicolas Geoffray51ad7fe2020-02-04 12:46:47 +0000961 // We reached a transition frame, it doesn't have a method header.
962 cur_oat_quick_method_header_ = nullptr;
Alex Lighte0c6d432020-01-22 22:04:20 +0000963 } else if (cur_shadow_frame_ != nullptr) {
964 do {
Orion Hodson6aaa49d2020-07-28 15:53:04 +0100965 ValidateFrame();
Alex Lighte0c6d432020-01-22 22:04:20 +0000966 bool should_continue = VisitFrame();
967 if (UNLIKELY(!should_continue)) {
968 return;
969 }
970 cur_depth_++;
971 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
972 } while (cur_shadow_frame_ != nullptr);
973 }
974 if (include_transitions) {
975 bool should_continue = VisitFrame();
976 if (!should_continue) {
977 return;
Ian Rogers0399dde2012-06-06 17:09:28 -0700978 }
979 }
Alex Lighte0c6d432020-01-22 22:04:20 +0000980 if (kCount == CountTransitions::kYes) {
981 cur_depth_++;
Andreas Gampe585da952016-12-02 14:52:29 -0800982 }
Alex Lighte0c6d432020-01-22 22:04:20 +0000983 }
984 if (num_frames_ != 0) {
985 CHECK_EQ(cur_depth_, num_frames_);
986 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700987}
988
Andreas Gampe585da952016-12-02 14:52:29 -0800989template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kYes>(bool);
990template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kNo>(bool);
991
Elliott Hughes68e76522011-10-05 13:22:16 -0700992} // namespace art