blob: a5e2f4402ca7f9b80337f7f1f3c8b81a21b23849 [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.
244 result = GetVRegFromOptimizedCode(location.value(), kind, val);
245 // 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
Mathieu Chartiere401d142015-04-22 13:56:20 -0700272bool StackVisitor::GetVRegFromOptimizedCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100273 uint32_t* val) const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100274 DCHECK_EQ(m, GetMethod());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800275 // Can't be null or how would we compile its instructions?
276 DCHECK(m->GetCodeItem() != nullptr) << m->PrettyMethod();
David Sehr0225f8e2018-01-31 08:52:24 +0000277 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800278 uint16_t number_of_dex_registers = accessor.RegistersSize();
279 DCHECK_LT(vreg, number_of_dex_registers);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100280 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
David Srbecky052f8ca2018-04-26 15:42:54 +0100281 CodeInfo code_info(method_header);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100282
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100283 uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_);
David Srbecky052f8ca2018-04-26 15:42:54 +0100284 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100285 DCHECK(stack_map.IsValid());
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100286
287 DexRegisterMap dex_register_map = IsInInlinedFrame()
David Srbecky93bd3612018-07-02 19:30:18 +0100288 ? code_info.GetInlineDexRegisterMapOf(stack_map, current_inline_frames_.back())
David Srbeckyfd89b072018-06-03 12:00:22 +0100289 : code_info.GetDexRegisterMapOf(stack_map);
290 if (dex_register_map.empty()) {
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000291 return false;
292 }
David Srbeckyfd89b072018-06-03 12:00:22 +0100293 DCHECK_EQ(dex_register_map.size(), number_of_dex_registers);
David Srbeckye1402122018-06-13 18:20:45 +0100294 DexRegisterLocation::Kind location_kind = dex_register_map[vreg].GetKind();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100295 switch (location_kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000296 case DexRegisterLocation::Kind::kInStack: {
David Srbeckye1402122018-06-13 18:20:45 +0100297 const int32_t offset = dex_register_map[vreg].GetStackOffsetInBytes();
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100298 BitMemoryRegion stack_mask = code_info.GetStackMaskOf(stack_map);
299 if (kind == kReferenceVReg && !stack_mask.LoadBit(offset / kFrameSlotSize)) {
300 return false;
301 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100302 const uint8_t* addr = reinterpret_cast<const uint8_t*>(cur_quick_frame_) + offset;
303 *val = *reinterpret_cast<const uint32_t*>(addr);
304 return true;
305 }
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100306 case DexRegisterLocation::Kind::kInRegister: {
307 uint32_t register_mask = code_info.GetRegisterMaskOf(stack_map);
308 uint32_t reg = dex_register_map[vreg].GetMachineRegister();
309 if (kind == kReferenceVReg && !(register_mask & (1 << reg))) {
310 return false;
311 }
312 return GetRegisterIfAccessible(reg, kind, val);
313 }
David Brazdild9cb68e2015-08-25 13:52:43 +0100314 case DexRegisterLocation::Kind::kInRegisterHigh:
315 case DexRegisterLocation::Kind::kInFpuRegister:
316 case DexRegisterLocation::Kind::kInFpuRegisterHigh: {
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100317 if (kind == kReferenceVReg) {
318 return false;
319 }
David Srbeckye1402122018-06-13 18:20:45 +0100320 uint32_t reg = dex_register_map[vreg].GetMachineRegister();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100321 return GetRegisterIfAccessible(reg, kind, val);
322 }
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100323 case DexRegisterLocation::Kind::kConstant: {
324 uint32_t result = dex_register_map[vreg].GetConstant();
325 if (kind == kReferenceVReg && result != 0) {
326 return false;
327 }
328 *val = result;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100329 return true;
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100330 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000331 case DexRegisterLocation::Kind::kNone:
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100332 return false;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000333 default:
David Srbeckye1402122018-06-13 18:20:45 +0100334 LOG(FATAL) << "Unexpected location kind " << dex_register_map[vreg].GetKind();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000335 UNREACHABLE();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100336 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100337}
338
David Srbeckycffa2542019-07-01 15:31:41 +0100339bool StackVisitor::GetVRegFromOptimizedCode(DexRegisterLocation location,
340 VRegKind kind,
341 uint32_t* val) const {
342 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:
354 return GetRegisterIfAccessible(location.GetMachineRegister(), kind, val);
355 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
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100365bool StackVisitor::GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const {
366 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
David Brazdil77a48ae2015-09-15 12:34:04 +0000367
Vladimir Marko239d6ea2016-09-05 10:44:04 +0100368 if (kRuntimeISA == InstructionSet::kX86 && is_float) {
369 // X86 float registers are 64-bit and each XMM register is provided as two separate
370 // 32-bit registers by the context.
371 reg = (kind == kDoubleHiVReg) ? (2 * reg + 1) : (2 * reg);
372 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000373
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100374 if (!IsAccessibleRegister(reg, is_float)) {
375 return false;
376 }
377 uintptr_t ptr_val = GetRegister(reg, is_float);
378 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
379 if (target64) {
380 const bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
381 const bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
382 int64_t value_long = static_cast<int64_t>(ptr_val);
383 if (wide_lo) {
384 ptr_val = static_cast<uintptr_t>(Low32Bits(value_long));
385 } else if (wide_hi) {
386 ptr_val = static_cast<uintptr_t>(High32Bits(value_long));
387 }
388 }
389 *val = ptr_val;
390 return true;
391}
392
Mingyao Yang99170c62015-07-06 11:10:37 -0700393bool StackVisitor::GetVRegPairFromDebuggerShadowFrame(uint16_t vreg,
394 VRegKind kind_lo,
395 VRegKind kind_hi,
396 uint64_t* val) const {
397 uint32_t low_32bits;
398 uint32_t high_32bits;
399 bool success = GetVRegFromDebuggerShadowFrame(vreg, kind_lo, &low_32bits);
400 success &= GetVRegFromDebuggerShadowFrame(vreg + 1, kind_hi, &high_32bits);
401 if (success) {
402 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
403 }
404 return success;
405}
406
Mathieu Chartiere401d142015-04-22 13:56:20 -0700407bool StackVisitor::GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200408 VRegKind kind_hi, uint64_t* val) const {
409 if (kind_lo == kLongLoVReg) {
410 DCHECK_EQ(kind_hi, kLongHiVReg);
411 } else if (kind_lo == kDoubleLoVReg) {
412 DCHECK_EQ(kind_hi, kDoubleHiVReg);
413 } else {
414 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100415 UNREACHABLE();
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200416 }
Mingyao Yang99170c62015-07-06 11:10:37 -0700417 // Check if there is value set by the debugger.
418 if (GetVRegPairFromDebuggerShadowFrame(vreg, kind_lo, kind_hi, val)) {
419 return true;
420 }
Nicolas Geoffraycaafd622020-01-27 13:08:45 +0000421 if (cur_quick_frame_ == nullptr) {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100422 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200423 *val = cur_shadow_frame_->GetVRegLong(vreg);
424 return true;
425 }
Nicolas Geoffraycaafd622020-01-27 13:08:45 +0000426 if (cur_oat_quick_method_header_->IsNterpMethodHeader()) {
427 uint64_t val_lo = NterpGetVReg(cur_quick_frame_, vreg);
428 uint64_t val_hi = NterpGetVReg(cur_quick_frame_, vreg + 1);
429 *val = (val_hi << 32) + val_lo;
430 return true;
431 }
432
433 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
434 DCHECK(m == GetMethod());
435 DCHECK(cur_oat_quick_method_header_->IsOptimized());
436 return GetVRegPairFromOptimizedCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200437}
438
Mathieu Chartiere401d142015-04-22 13:56:20 -0700439bool StackVisitor::GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100440 VRegKind kind_lo, VRegKind kind_hi,
441 uint64_t* val) const {
442 uint32_t low_32bits;
443 uint32_t high_32bits;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700444 bool success = GetVRegFromOptimizedCode(m, vreg, kind_lo, &low_32bits);
445 success &= GetVRegFromOptimizedCode(m, vreg + 1, kind_hi, &high_32bits);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100446 if (success) {
447 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
448 }
449 return success;
450}
451
452bool StackVisitor::GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
453 VRegKind kind_lo, uint64_t* val) const {
454 const bool is_float = (kind_lo == kDoubleLoVReg);
455 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
456 return false;
457 }
458 uintptr_t ptr_val_lo = GetRegister(reg_lo, is_float);
459 uintptr_t ptr_val_hi = GetRegister(reg_hi, is_float);
460 bool target64 = Is64BitInstructionSet(kRuntimeISA);
461 if (target64) {
462 int64_t value_long_lo = static_cast<int64_t>(ptr_val_lo);
463 int64_t value_long_hi = static_cast<int64_t>(ptr_val_hi);
464 ptr_val_lo = static_cast<uintptr_t>(Low32Bits(value_long_lo));
465 ptr_val_hi = static_cast<uintptr_t>(High32Bits(value_long_hi));
466 }
467 *val = (static_cast<uint64_t>(ptr_val_hi) << 32) | static_cast<uint32_t>(ptr_val_lo);
468 return true;
469}
470
Vladimir Marko439d1262019-04-12 14:45:07 +0100471ShadowFrame* StackVisitor::PrepareSetVReg(ArtMethod* m, uint16_t vreg, bool wide) {
David Sehr0225f8e2018-01-31 08:52:24 +0000472 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800473 if (!accessor.HasCodeItem()) {
Vladimir Marko439d1262019-04-12 14:45:07 +0100474 return nullptr;
Mingyao Yang99170c62015-07-06 11:10:37 -0700475 }
476 ShadowFrame* shadow_frame = GetCurrentShadowFrame();
477 if (shadow_frame == nullptr) {
478 // This is a compiled frame: we must prepare and update a shadow frame that will
479 // be executed by the interpreter after deoptimization of the stack.
480 const size_t frame_id = GetFrameId();
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800481 const uint16_t num_regs = accessor.RegistersSize();
Mingyao Yang99170c62015-07-06 11:10:37 -0700482 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
483 CHECK(shadow_frame != nullptr);
Vladimir Marko439d1262019-04-12 14:45:07 +0100484 // Remember the vreg(s) has been set for debugging and must not be overwritten by the
Mingyao Yang99170c62015-07-06 11:10:37 -0700485 // original value during deoptimization of the stack.
486 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
Vladimir Marko439d1262019-04-12 14:45:07 +0100487 if (wide) {
488 thread_->GetUpdatedVRegFlags(frame_id)[vreg + 1] = true;
489 }
Mingyao Yang99170c62015-07-06 11:10:37 -0700490 }
Vladimir Marko439d1262019-04-12 14:45:07 +0100491 return shadow_frame;
492}
493
494bool StackVisitor::SetVReg(ArtMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind) {
495 DCHECK(kind == kIntVReg || kind == kFloatVReg);
496 ShadowFrame* shadow_frame = PrepareSetVReg(m, vreg, /* wide= */ false);
497 if (shadow_frame == nullptr) {
498 return false;
Mingyao Yang99170c62015-07-06 11:10:37 -0700499 }
Vladimir Marko439d1262019-04-12 14:45:07 +0100500 shadow_frame->SetVReg(vreg, new_value);
501 return true;
502}
503
504bool StackVisitor::SetVRegReference(ArtMethod* m, uint16_t vreg, ObjPtr<mirror::Object> new_value) {
505 ShadowFrame* shadow_frame = PrepareSetVReg(m, vreg, /* wide= */ false);
506 if (shadow_frame == nullptr) {
507 return false;
508 }
509 shadow_frame->SetVRegReference(vreg, new_value);
Mingyao Yang99170c62015-07-06 11:10:37 -0700510 return true;
511}
512
Mingyao Yang636b9252015-07-31 16:40:24 -0700513bool StackVisitor::SetVRegPair(ArtMethod* m,
514 uint16_t vreg,
515 uint64_t new_value,
516 VRegKind kind_lo,
517 VRegKind kind_hi) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700518 if (kind_lo == kLongLoVReg) {
519 DCHECK_EQ(kind_hi, kLongHiVReg);
520 } else if (kind_lo == kDoubleLoVReg) {
521 DCHECK_EQ(kind_hi, kDoubleHiVReg);
522 } else {
523 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
524 UNREACHABLE();
525 }
Vladimir Marko439d1262019-04-12 14:45:07 +0100526 ShadowFrame* shadow_frame = PrepareSetVReg(m, vreg, /* wide= */ true);
Mingyao Yang99170c62015-07-06 11:10:37 -0700527 if (shadow_frame == nullptr) {
Vladimir Marko439d1262019-04-12 14:45:07 +0100528 return false;
Mingyao Yang99170c62015-07-06 11:10:37 -0700529 }
530 shadow_frame->SetVRegLong(vreg, new_value);
531 return true;
532}
533
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100534bool StackVisitor::IsAccessibleGPR(uint32_t reg) const {
535 DCHECK(context_ != nullptr);
536 return context_->IsAccessibleGPR(reg);
537}
538
Mathieu Chartier815873e2014-02-13 18:02:13 -0800539uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100540 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
541 DCHECK(context_ != nullptr);
Mathieu Chartier815873e2014-02-13 18:02:13 -0800542 return context_->GetGPRAddress(reg);
543}
544
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100545uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
546 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
547 DCHECK(context_ != nullptr);
548 return context_->GetGPR(reg);
Ian Rogers0399dde2012-06-06 17:09:28 -0700549}
550
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100551bool StackVisitor::IsAccessibleFPR(uint32_t reg) const {
552 DCHECK(context_ != nullptr);
553 return context_->IsAccessibleFPR(reg);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200554}
555
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100556uintptr_t StackVisitor::GetFPR(uint32_t reg) const {
557 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
558 DCHECK(context_ != nullptr);
559 return context_->GetFPR(reg);
560}
561
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000562uintptr_t StackVisitor::GetReturnPcAddr() const {
563 uintptr_t sp = reinterpret_cast<uintptr_t>(GetCurrentQuickFrame());
564 DCHECK_NE(sp, 0u);
565 return sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
566}
567
Ian Rogers0399dde2012-06-06 17:09:28 -0700568uintptr_t StackVisitor::GetReturnPc() const {
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000569 return *reinterpret_cast<uintptr_t*>(GetReturnPcAddr());
Ian Rogers0399dde2012-06-06 17:09:28 -0700570}
571
572void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000573 *reinterpret_cast<uintptr_t*>(GetReturnPcAddr()) = new_ret_pc;
Ian Rogers0399dde2012-06-06 17:09:28 -0700574}
575
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100576size_t StackVisitor::ComputeNumFrames(Thread* thread, StackWalkKind walk_kind) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700577 struct NumFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100578 NumFramesVisitor(Thread* thread_in, StackWalkKind walk_kind_in)
579 : StackVisitor(thread_in, nullptr, walk_kind_in), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700580
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100581 bool VisitFrame() override {
Ian Rogers0399dde2012-06-06 17:09:28 -0700582 frames++;
583 return true;
584 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700585
Ian Rogers0399dde2012-06-06 17:09:28 -0700586 size_t frames;
587 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100588 NumFramesVisitor visitor(thread, walk_kind);
Ian Rogers0399dde2012-06-06 17:09:28 -0700589 visitor.WalkStack(true);
590 return visitor.frames;
591}
592
Mathieu Chartiere401d142015-04-22 13:56:20 -0700593bool StackVisitor::GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700594 struct HasMoreFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100595 HasMoreFramesVisitor(Thread* thread,
596 StackWalkKind walk_kind,
597 size_t num_frames,
598 size_t frame_height)
599 : StackVisitor(thread, nullptr, walk_kind, num_frames),
600 frame_height_(frame_height),
601 found_frame_(false),
602 has_more_frames_(false),
603 next_method_(nullptr),
604 next_dex_pc_(0) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700605 }
606
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100607 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700608 if (found_frame_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700609 ArtMethod* method = GetMethod();
Ian Rogers5cf98192014-05-29 21:31:50 -0700610 if (method != nullptr && !method->IsRuntimeMethod()) {
611 has_more_frames_ = true;
612 next_method_ = method;
613 next_dex_pc_ = GetDexPc();
614 return false; // End stack walk once next method is found.
615 }
616 } else if (GetFrameHeight() == frame_height_) {
617 found_frame_ = true;
618 }
619 return true;
620 }
621
622 size_t frame_height_;
623 bool found_frame_;
624 bool has_more_frames_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700625 ArtMethod* next_method_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700626 uint32_t next_dex_pc_;
627 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100628 HasMoreFramesVisitor visitor(thread_, walk_kind_, GetNumFrames(), GetFrameHeight());
Ian Rogers5cf98192014-05-29 21:31:50 -0700629 visitor.WalkStack(true);
630 *next_method = visitor.next_method_;
631 *next_dex_pc = visitor.next_dex_pc_;
632 return visitor.has_more_frames_;
633}
634
Ian Rogers7a22fa62013-01-23 12:16:16 -0800635void StackVisitor::DescribeStack(Thread* thread) {
Ian Rogers306057f2012-11-26 12:45:53 -0800636 struct DescribeStackVisitor : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800637 explicit DescribeStackVisitor(Thread* thread_in)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100638 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800639
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100640 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers306057f2012-11-26 12:45:53 -0800641 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation();
642 return true;
643 }
644 };
Ian Rogers7a22fa62013-01-23 12:16:16 -0800645 DescribeStackVisitor visitor(thread);
Ian Rogers306057f2012-11-26 12:45:53 -0800646 visitor.WalkStack(true);
647}
648
Ian Rogers40e3bac2012-11-20 00:09:14 -0800649std::string StackVisitor::DescribeLocation() const {
650 std::string result("Visiting method '");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700651 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700652 if (m == nullptr) {
Ian Rogers306057f2012-11-26 12:45:53 -0800653 return "upcall";
654 }
David Sehr709b0702016-10-13 09:12:37 -0700655 result += m->PrettyMethod();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800656 result += StringPrintf("' at dex PC 0x%04x", GetDexPc());
Ian Rogers40e3bac2012-11-20 00:09:14 -0800657 if (!IsShadowFrame()) {
658 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc()));
659 }
660 return result;
661}
662
Alex Lightdba61482016-12-21 08:20:29 -0800663void StackVisitor::SetMethod(ArtMethod* method) {
664 DCHECK(GetMethod() != nullptr);
665 if (cur_shadow_frame_ != nullptr) {
666 cur_shadow_frame_->SetMethod(method);
667 } else {
668 DCHECK(cur_quick_frame_ != nullptr);
Nicolas Geoffray226805d2018-12-14 10:59:02 +0000669 CHECK(!IsInInlinedFrame()) << "We do not support setting inlined method's ArtMethod: "
670 << GetMethod()->PrettyMethod() << " is inlined into "
671 << GetOuterMethod()->PrettyMethod();
Alex Light1ebe4fe2017-01-30 14:57:11 -0800672 *cur_quick_frame_ = method;
Alex Lightdba61482016-12-21 08:20:29 -0800673 }
674}
675
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100676static void AssertPcIsWithinQuickCode(ArtMethod* method, uintptr_t pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700677 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100678 if (method->IsNative() || method->IsRuntimeMethod() || method->IsProxyMethod()) {
679 return;
680 }
681
682 if (pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) {
683 return;
684 }
685
Mingyao Yang88ca8ba2017-05-23 14:21:07 -0700686 Runtime* runtime = Runtime::Current();
687 if (runtime->UseJitCompilation() &&
688 runtime->GetJit()->GetCodeCache()->ContainsPc(reinterpret_cast<const void*>(pc))) {
689 return;
690 }
691
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100692 const void* code = method->GetEntryPointFromQuickCompiledCode();
Alex Lightdb01a092017-04-03 15:39:55 -0700693 if (code == GetQuickInstrumentationEntryPoint() || code == GetInvokeObsoleteMethodStub()) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100694 return;
695 }
696
697 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
698 if (class_linker->IsQuickToInterpreterBridge(code) ||
699 class_linker->IsQuickResolutionStub(code)) {
700 return;
701 }
702
Calin Juravleffc87072016-04-20 14:22:09 +0100703 if (runtime->UseJitCompilation() && runtime->GetJit()->GetCodeCache()->ContainsPc(code)) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100704 return;
705 }
706
Mingyao Yang063fc772016-08-02 11:02:54 -0700707 uint32_t code_size = OatQuickMethodHeader::FromEntryPoint(code)->GetCodeSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100708 uintptr_t code_start = reinterpret_cast<uintptr_t>(code);
709 CHECK(code_start <= pc && pc <= (code_start + code_size))
David Sehr709b0702016-10-13 09:12:37 -0700710 << method->PrettyMethod()
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100711 << " pc=" << std::hex << pc
Roland Levillain0d5a2812015-11-13 10:07:31 +0000712 << " code_start=" << code_start
713 << " code_size=" << code_size;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100714}
715
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700716void StackVisitor::SanityCheckFrame() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800717 if (kIsDebugBuild) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700718 ArtMethod* method = GetMethod();
Vladimir Markod93e3742018-07-18 10:58:13 +0100719 ObjPtr<mirror::Class> declaring_class = method->GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700720 // Runtime methods have null declaring class.
721 if (!method->IsRuntimeMethod()) {
722 CHECK(declaring_class != nullptr);
723 CHECK_EQ(declaring_class->GetClass(), declaring_class->GetClass()->GetClass())
724 << declaring_class;
725 } else {
726 CHECK(declaring_class == nullptr);
727 }
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700728 Runtime* const runtime = Runtime::Current();
729 LinearAlloc* const linear_alloc = runtime->GetLinearAlloc();
730 if (!linear_alloc->Contains(method)) {
731 // Check class linker linear allocs.
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100732 // We get the canonical method as copied methods may have their declaring
733 // class from another class loader.
Ulya Trafimovich819b3622019-12-12 17:59:10 +0000734 const PointerSize ptrSize = runtime->GetClassLinker()->GetImagePointerSize();
735 ArtMethod* canonical = method->GetCanonicalMethod(ptrSize);
Vladimir Markod93e3742018-07-18 10:58:13 +0100736 ObjPtr<mirror::Class> klass = canonical->GetDeclaringClass();
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700737 LinearAlloc* const class_linear_alloc = (klass != nullptr)
Mathieu Chartier5b830502016-03-02 10:30:23 -0800738 ? runtime->GetClassLinker()->GetAllocatorForClassLoader(klass->GetClassLoader())
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700739 : linear_alloc;
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100740 if (!class_linear_alloc->Contains(canonical)) {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700741 // Check image space.
742 bool in_image = false;
743 for (auto& space : runtime->GetHeap()->GetContinuousSpaces()) {
744 if (space->IsImageSpace()) {
745 auto* image_space = space->AsImageSpace();
746 const auto& header = image_space->GetImageHeader();
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700747 const ImageSection& methods = header.GetMethodsSection();
748 const ImageSection& runtime_methods = header.GetRuntimeMethodsSection();
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100749 const size_t offset = reinterpret_cast<const uint8_t*>(canonical) - image_space->Begin();
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700750 if (methods.Contains(offset) || runtime_methods.Contains(offset)) {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700751 in_image = true;
752 break;
753 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700754 }
755 }
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100756 CHECK(in_image) << canonical->PrettyMethod() << " not in linear alloc or image";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700757 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700758 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800759 if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100760 AssertPcIsWithinQuickCode(method, cur_quick_frame_pc_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800761 // Frame sanity.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100762 size_t frame_size = GetCurrentQuickFrameInfo().FrameSizeInBytes();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800763 CHECK_NE(frame_size, 0u);
Nicolas Geoffray00391822019-12-10 10:17:23 +0000764 // For compiled code, we could try to have a rough guess at an upper size we expect
765 // to see for a frame:
Andreas Gampe5b417b92014-03-10 14:18:35 -0700766 // 256 registers
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700767 // 2 words HandleScope overhead
Andreas Gampe5b417b92014-03-10 14:18:35 -0700768 // 3+3 register spills
Brian Carlstromed08bd42014-03-19 18:34:17 -0700769 // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word);
Nicolas Geoffray0315efa2020-06-26 11:42:39 +0100770 const size_t kMaxExpectedFrameSize = interpreter::kNterpMaxFrame;
David Sehr709b0702016-10-13 09:12:37 -0700771 CHECK_LE(frame_size, kMaxExpectedFrameSize) << method->PrettyMethod();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100772 size_t return_pc_offset = GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800773 CHECK_LT(return_pc_offset, frame_size);
774 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700775 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700776}
777
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100778QuickMethodFrameInfo StackVisitor::GetCurrentQuickFrameInfo() const {
779 if (cur_oat_quick_method_header_ != nullptr) {
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000780 if (cur_oat_quick_method_header_->IsOptimized()) {
781 return cur_oat_quick_method_header_->GetFrameInfo();
782 } else {
783 DCHECK(cur_oat_quick_method_header_->IsNterpMethodHeader());
784 return NterpFrameInfo(cur_quick_frame_);
785 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100786 }
787
788 ArtMethod* method = GetMethod();
789 Runtime* runtime = Runtime::Current();
790
791 if (method->IsAbstract()) {
Vladimir Markod3083dd2018-05-17 08:43:47 +0100792 return RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100793 }
794
795 // This goes before IsProxyMethod since runtime methods have a null declaring class.
796 if (method->IsRuntimeMethod()) {
797 return runtime->GetRuntimeMethodFrameInfo(method);
798 }
799
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100800 if (method->IsProxyMethod()) {
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000801 // There is only one direct method of a proxy class: the constructor. A direct method is
802 // cloned from the original java.lang.reflect.Proxy and is executed as usual quick
803 // compiled method without any stubs. Therefore the method must have a OatQuickMethodHeader.
804 DCHECK(!method->IsDirect() && !method->IsConstructor())
805 << "Constructors of proxy classes must have a OatQuickMethodHeader";
Vladimir Markod3083dd2018-05-17 08:43:47 +0100806 return RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100807 }
808
Vladimir Markofa458ac2020-02-12 14:08:07 +0000809 // The only remaining cases are for native methods that either
810 // - use the Generic JNI stub, called either directly or through some
811 // (resolution, instrumentation) trampoline; or
812 // - fake a Generic JNI frame in art_jni_dlsym_lookup_critical_stub.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100813 DCHECK(method->IsNative());
Vladimir Markofa458ac2020-02-12 14:08:07 +0000814 if (kIsDebugBuild && !method->IsCriticalNative()) {
Vladimir Marko2196c652017-11-30 16:16:07 +0000815 ClassLinker* class_linker = runtime->GetClassLinker();
816 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(method,
817 kRuntimePointerSize);
818 CHECK(class_linker->IsQuickGenericJniStub(entry_point) ||
819 // The current entrypoint (after filtering out trampolines) may have changed
820 // from GenericJNI to JIT-compiled stub since we have entered this frame.
821 (runtime->GetJit() != nullptr &&
822 runtime->GetJit()->GetCodeCache()->ContainsPc(entry_point))) << method->PrettyMethod();
823 }
Vladimir Marko6e043bb2020-02-10 16:56:54 +0000824 // Generic JNI frame is just like the SaveRefsAndArgs frame.
825 // Note that HandleScope, if any, is below the frame.
826 return RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100827}
828
Andreas Gampe585da952016-12-02 14:52:29 -0800829template <StackVisitor::CountTransitions kCount>
Ian Rogers0399dde2012-06-06 17:09:28 -0700830void StackVisitor::WalkStack(bool include_transitions) {
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -0800831 if (check_suspended_) {
832 DCHECK(thread_ == Thread::Current() || thread_->IsSuspended());
833 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800834 CHECK_EQ(cur_depth_, 0U);
Alex Lighte0c6d432020-01-22 22:04:20 +0000835 size_t inlined_frames_count = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700836
Alex Lighte0c6d432020-01-22 22:04:20 +0000837 for (const ManagedStack* current_fragment = thread_->GetManagedStack();
838 current_fragment != nullptr; current_fragment = current_fragment->GetLink()) {
839 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
840 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
841 cur_quick_frame_pc_ = 0;
Nicolas Geoffray51ad7fe2020-02-04 12:46:47 +0000842 DCHECK(cur_oat_quick_method_header_ == nullptr);
Alex Lighte0c6d432020-01-22 22:04:20 +0000843 if (cur_quick_frame_ != nullptr) { // Handle quick stack frames.
844 // Can't be both a shadow and a quick fragment.
845 DCHECK(current_fragment->GetTopShadowFrame() == nullptr);
846 ArtMethod* method = *cur_quick_frame_;
847 DCHECK(method != nullptr);
848 bool header_retrieved = false;
849 if (method->IsNative()) {
850 // We do not have a PC for the first frame, so we cannot simply use
851 // ArtMethod::GetOatQuickMethodHeader() as we're unable to distinguish there
852 // between GenericJNI frame and JIT-compiled JNI stub; the entrypoint may have
853 // changed since the frame was entered. The top quick frame tag indicates
854 // GenericJNI here, otherwise it's either AOT-compiled or JNI-compiled JNI stub.
855 if (UNLIKELY(current_fragment->GetTopQuickFrameTag())) {
856 // The generic JNI does not have any method header.
857 cur_oat_quick_method_header_ = nullptr;
858 } else {
859 const void* existing_entry_point = method->GetEntryPointFromQuickCompiledCode();
860 CHECK(existing_entry_point != nullptr);
861 Runtime* runtime = Runtime::Current();
862 ClassLinker* class_linker = runtime->GetClassLinker();
863 // Check whether we can quickly get the header from the current entrypoint.
864 if (!class_linker->IsQuickGenericJniStub(existing_entry_point) &&
865 !class_linker->IsQuickResolutionStub(existing_entry_point) &&
866 existing_entry_point != GetQuickInstrumentationEntryPoint()) {
867 cur_oat_quick_method_header_ =
868 OatQuickMethodHeader::FromEntryPoint(existing_entry_point);
Vladimir Marko2196c652017-11-30 16:16:07 +0000869 } else {
Alex Lighte0c6d432020-01-22 22:04:20 +0000870 const void* code = method->GetOatMethodQuickCode(class_linker->GetImagePointerSize());
871 if (code != nullptr) {
872 cur_oat_quick_method_header_ = OatQuickMethodHeader::FromEntryPoint(code);
Vladimir Marko2196c652017-11-30 16:16:07 +0000873 } else {
Alex Lighte0c6d432020-01-22 22:04:20 +0000874 // This must be a JITted JNI stub frame.
875 CHECK(runtime->GetJit() != nullptr);
876 code = runtime->GetJit()->GetCodeCache()->GetJniStubCode(method);
877 CHECK(code != nullptr) << method->PrettyMethod();
878 cur_oat_quick_method_header_ = OatQuickMethodHeader::FromCodePointer(code);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100879 }
880 }
881 }
Alex Lighte0c6d432020-01-22 22:04:20 +0000882 header_retrieved = true;
jeffhao6641ea12013-01-02 18:13:42 -0800883 }
Alex Lighte0c6d432020-01-22 22:04:20 +0000884 while (method != nullptr) {
885 if (!header_retrieved) {
886 cur_oat_quick_method_header_ = method->GetOatQuickMethodHeader(cur_quick_frame_pc_);
887 }
888 header_retrieved = false; // Force header retrieval in next iteration.
889 SanityCheckFrame();
890
891 if ((walk_kind_ == StackWalkKind::kIncludeInlinedFrames)
892 && (cur_oat_quick_method_header_ != nullptr)
893 && cur_oat_quick_method_header_->IsOptimized()
894 && !method->IsNative() // JNI methods cannot have any inlined frames.
895 && CodeInfo::HasInlineInfo(cur_oat_quick_method_header_->GetOptimizedCodeInfoPtr())) {
896 DCHECK_NE(cur_quick_frame_pc_, 0u);
897 CodeInfo* code_info = GetCurrentInlineInfo();
898 StackMap* stack_map = GetCurrentStackMap();
899 if (stack_map->IsValid() && stack_map->HasInlineInfo()) {
900 DCHECK_EQ(current_inline_frames_.size(), 0u);
901 for (current_inline_frames_ = code_info->GetInlineInfosOf(*stack_map);
902 !current_inline_frames_.empty();
903 current_inline_frames_.pop_back()) {
904 bool should_continue = VisitFrame();
905 if (UNLIKELY(!should_continue)) {
906 return;
907 }
908 cur_depth_++;
909 inlined_frames_count++;
910 }
911 }
912 }
913
Ian Rogers0399dde2012-06-06 17:09:28 -0700914 bool should_continue = VisitFrame();
Alex Lighte0c6d432020-01-22 22:04:20 +0000915 if (UNLIKELY(!should_continue)) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700916 return;
917 }
Alex Lighte0c6d432020-01-22 22:04:20 +0000918
919 QuickMethodFrameInfo frame_info = GetCurrentQuickFrameInfo();
920 if (context_ != nullptr) {
921 context_->FillCalleeSaves(reinterpret_cast<uint8_t*>(cur_quick_frame_), frame_info);
922 }
923 // Compute PC for next stack frame from return PC.
924 size_t frame_size = frame_info.FrameSizeInBytes();
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000925 uintptr_t return_pc_addr = GetReturnPcAddr();
Alex Lighte0c6d432020-01-22 22:04:20 +0000926 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
927
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000928 if (UNLIKELY(reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc)) {
Alex Lighte0c6d432020-01-22 22:04:20 +0000929 // While profiling, the return pc is restored from the side stack, except when walking
930 // the stack for an exception where the side stack will be unwound in VisitFrame.
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000931 const std::map<uintptr_t, instrumentation::InstrumentationStackFrame>&
932 instrumentation_stack = *thread_->GetInstrumentationStack();
933 auto it = instrumentation_stack.find(return_pc_addr);
934 CHECK(it != instrumentation_stack.end());
935 const instrumentation::InstrumentationStackFrame& instrumentation_frame = it->second;
936 if (GetMethod() ==
937 Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveAllCalleeSaves)) {
938 // Skip runtime save all callee frames which are used to deliver exceptions.
939 } else if (instrumentation_frame.interpreter_entry_) {
940 ArtMethod* callee =
941 Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs);
942 CHECK_EQ(GetMethod(), callee) << "Expected: " << ArtMethod::PrettyMethod(callee)
943 << " Found: " << ArtMethod::PrettyMethod(GetMethod());
Nicolas Geoffray8feb7eb2020-02-04 09:21:33 +0000944 } else if (!instrumentation_frame.method_->IsRuntimeMethod()) {
945 // Trampolines get replaced with their actual method in the stack,
946 // so don't do the check below for runtime methods.
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000947 // Instrumentation generally doesn't distinguish between a method's obsolete and
948 // non-obsolete version.
949 CHECK_EQ(instrumentation_frame.method_->GetNonObsoleteMethod(),
950 GetMethod()->GetNonObsoleteMethod())
951 << "Expected: "
952 << ArtMethod::PrettyMethod(instrumentation_frame.method_->GetNonObsoleteMethod())
953 << " Found: " << ArtMethod::PrettyMethod(GetMethod()->GetNonObsoleteMethod());
Alex Lighte0c6d432020-01-22 22:04:20 +0000954 }
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000955 return_pc = instrumentation_frame.return_pc_;
Alex Lighte0c6d432020-01-22 22:04:20 +0000956 }
957
958 cur_quick_frame_pc_ = return_pc;
959 uint8_t* next_frame = reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size;
960 cur_quick_frame_ = reinterpret_cast<ArtMethod**>(next_frame);
961
962 if (kDebugStackWalk) {
963 LOG(INFO) << ArtMethod::PrettyMethod(method) << "@" << method << " size=" << frame_size
964 << std::boolalpha
965 << " optimized=" << (cur_oat_quick_method_header_ != nullptr &&
966 cur_oat_quick_method_header_->IsOptimized())
967 << " native=" << method->IsNative()
968 << std::noboolalpha
969 << " entrypoints=" << method->GetEntryPointFromQuickCompiledCode()
970 << "," << (method->IsNative() ? method->GetEntryPointFromJni() : nullptr)
971 << " next=" << *cur_quick_frame_;
972 }
973
974 if (kCount == CountTransitions::kYes || !method->IsRuntimeMethod()) {
975 cur_depth_++;
976 }
977 method = *cur_quick_frame_;
Alex Light721e4022020-01-14 14:45:40 -0800978 }
Nicolas Geoffray51ad7fe2020-02-04 12:46:47 +0000979 // We reached a transition frame, it doesn't have a method header.
980 cur_oat_quick_method_header_ = nullptr;
Alex Lighte0c6d432020-01-22 22:04:20 +0000981 } else if (cur_shadow_frame_ != nullptr) {
982 do {
983 SanityCheckFrame();
984 bool should_continue = VisitFrame();
985 if (UNLIKELY(!should_continue)) {
986 return;
987 }
988 cur_depth_++;
989 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
990 } while (cur_shadow_frame_ != nullptr);
991 }
992 if (include_transitions) {
993 bool should_continue = VisitFrame();
994 if (!should_continue) {
995 return;
Ian Rogers0399dde2012-06-06 17:09:28 -0700996 }
997 }
Alex Lighte0c6d432020-01-22 22:04:20 +0000998 if (kCount == CountTransitions::kYes) {
999 cur_depth_++;
Andreas Gampe585da952016-12-02 14:52:29 -08001000 }
Alex Lighte0c6d432020-01-22 22:04:20 +00001001 }
1002 if (num_frames_ != 0) {
1003 CHECK_EQ(cur_depth_, num_frames_);
1004 }
Ian Rogers0399dde2012-06-06 17:09:28 -07001005}
1006
Andreas Gampe585da952016-12-02 14:52:29 -08001007template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kYes>(bool);
1008template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kNo>(bool);
1009
Elliott Hughes68e76522011-10-05 13:22:16 -07001010} // namespace art