blob: 04590910fa2d7443ae526909ef19895b36fcde1a [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"
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +010033#include "interpreter/shadow_frame-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010034#include "jit/jit.h"
35#include "jit/jit_code_cache.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070036#include "linear_alloc.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070037#include "managed_stack.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070038#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/object-inl.h"
40#include "mirror/object_array-inl.h"
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +000041#include "nterp_helpers.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010042#include "oat_quick_method_header.h"
Vladimir Marko439d1262019-04-12 14:45:07 +010043#include "obj_ptr-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010044#include "quick/quick_method_frame_info.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070045#include "runtime.h"
Dave Allisonf9439142014-03-27 15:10:22 -070046#include "thread.h"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070047#include "thread_list.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070048
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080049namespace art {
50
Andreas Gampe46ee31b2016-12-14 10:11:49 -080051using android::base::StringPrintf;
52
Mathieu Chartier8405bfd2016-02-05 12:00:49 -080053static constexpr bool kDebugStackWalk = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -070054
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -080055StackVisitor::StackVisitor(Thread* thread,
56 Context* context,
57 StackWalkKind walk_kind,
58 bool check_suspended)
59 : StackVisitor(thread, context, walk_kind, 0, check_suspended) {}
Ian Rogers7a22fa62013-01-23 12:16:16 -080060
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010061StackVisitor::StackVisitor(Thread* thread,
62 Context* context,
63 StackWalkKind walk_kind,
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -080064 size_t num_frames,
65 bool check_suspended)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010066 : thread_(thread),
67 walk_kind_(walk_kind),
68 cur_shadow_frame_(nullptr),
69 cur_quick_frame_(nullptr),
70 cur_quick_frame_pc_(0),
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010071 cur_oat_quick_method_header_(nullptr),
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010072 num_frames_(num_frames),
73 cur_depth_(0),
David Srbecky145a18a2019-06-03 14:35:22 +010074 cur_inline_info_(nullptr, CodeInfo()),
75 cur_stack_map_(0, StackMap()),
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -080076 context_(context),
77 check_suspended_(check_suspended) {
78 if (check_suspended_) {
79 DCHECK(thread == Thread::Current() || thread->IsSuspended()) << *thread;
80 }
Ian Rogers5cf98192014-05-29 21:31:50 -070081}
82
David Srbecky145a18a2019-06-03 14:35:22 +010083CodeInfo* StackVisitor::GetCurrentInlineInfo() const {
84 DCHECK(!(*cur_quick_frame_)->IsNative());
85 const OatQuickMethodHeader* header = GetCurrentOatQuickMethodHeader();
86 if (cur_inline_info_.first != header) {
David Srbecky0d4567f2019-05-30 22:45:40 +010087 cur_inline_info_ = std::make_pair(header, CodeInfo::DecodeInlineInfoOnly(header));
David Srbecky145a18a2019-06-03 14:35:22 +010088 }
89 return &cur_inline_info_.second;
90}
91
92StackMap* StackVisitor::GetCurrentStackMap() const {
93 DCHECK(!(*cur_quick_frame_)->IsNative());
94 const OatQuickMethodHeader* header = GetCurrentOatQuickMethodHeader();
95 if (cur_stack_map_.first != cur_quick_frame_pc_) {
96 uint32_t pc = header->NativeQuickPcOffset(cur_quick_frame_pc_);
97 cur_stack_map_ = std::make_pair(cur_quick_frame_pc_,
98 GetCurrentInlineInfo()->GetStackMapForNativePcOffset(pc));
99 }
100 return &cur_stack_map_.second;
101}
102
Mathieu Chartiere401d142015-04-22 13:56:20 -0700103ArtMethod* StackVisitor::GetMethod() const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100104 if (cur_shadow_frame_ != nullptr) {
105 return cur_shadow_frame_->GetMethod();
106 } else if (cur_quick_frame_ != nullptr) {
107 if (IsInInlinedFrame()) {
David Srbecky145a18a2019-06-03 14:35:22 +0100108 CodeInfo* code_info = GetCurrentInlineInfo();
Mathieu Chartier45bf2502016-03-31 11:07:09 -0700109 DCHECK(walk_kind_ != StackWalkKind::kSkipInlinedFrames);
David Srbecky145a18a2019-06-03 14:35:22 +0100110 return GetResolvedMethod(*GetCurrentQuickFrame(), *code_info, current_inline_frames_);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100111 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700112 return *cur_quick_frame_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100113 }
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100114 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700115 return nullptr;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100116}
117
Dave Allisonb373e092014-02-20 16:06:36 -0800118uint32_t StackVisitor::GetDexPc(bool abort_on_failure) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700119 if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700120 return cur_shadow_frame_->GetDexPC();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700121 } else if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100122 if (IsInInlinedFrame()) {
David Srbecky93bd3612018-07-02 19:30:18 +0100123 return current_inline_frames_.back().GetDexPc();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100124 } else if (cur_oat_quick_method_header_ == nullptr) {
Andreas Gampee2abbc62017-09-15 11:59:26 -0700125 return dex::kDexNoIndex;
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000126 } else if ((*GetCurrentQuickFrame())->IsNative()) {
127 return cur_oat_quick_method_header_->ToDexPc(
128 GetCurrentQuickFrame(), cur_quick_frame_pc_, abort_on_failure);
129 } else if (cur_oat_quick_method_header_->IsOptimized()) {
David Srbecky145a18a2019-06-03 14:35:22 +0100130 StackMap* stack_map = GetCurrentStackMap();
131 DCHECK(stack_map->IsValid());
132 return stack_map->GetDexPc();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100133 } else {
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000134 DCHECK(cur_oat_quick_method_header_->IsNterpMethodHeader());
135 return NterpGetDexPC(cur_quick_frame_);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100136 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700137 } else {
138 return 0;
139 }
140}
141
Mathieu Chartiere401d142015-04-22 13:56:20 -0700142extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700143 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertza836bc92014-11-25 16:30:53 +0100144
Vladimir Markoabedfca2019-05-23 14:07:47 +0100145ObjPtr<mirror::Object> StackVisitor::GetThisObject() const {
Andreas Gampe542451c2016-07-26 09:02:02 -0700146 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700147 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800148 if (m->IsStatic()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100149 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800150 } else if (m->IsNative()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100151 if (cur_quick_frame_ != nullptr) {
Vladimir Marko6e043bb2020-02-10 16:56:54 +0000152 HandleScope* hs;
153 if (cur_oat_quick_method_header_ != nullptr) {
154 hs = reinterpret_cast<HandleScope*>(
155 reinterpret_cast<char*>(cur_quick_frame_) + sizeof(ArtMethod*));
156 } else {
157 // GenericJNI frames have the HandleScope under the managed frame.
158 uint32_t shorty_len;
159 const char* shorty = m->GetShorty(&shorty_len);
160 const size_t num_handle_scope_references =
161 /* this */ 1u + std::count(shorty + 1, shorty + shorty_len, 'L');
162 hs = GetGenericJniHandleScope(cur_quick_frame_, num_handle_scope_references);
163 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700164 return hs->GetReference(0);
Ian Rogers62d6c772013-02-27 08:32:07 -0800165 } else {
166 return cur_shadow_frame_->GetVRegReference(0);
167 }
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000168 } else if (m->IsProxyMethod()) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100169 if (cur_quick_frame_ != nullptr) {
170 return artQuickGetProxyThisObject(cur_quick_frame_);
171 } else {
172 return cur_shadow_frame_->GetVRegReference(0);
173 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800174 } else {
David Sehr0225f8e2018-01-31 08:52:24 +0000175 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800176 if (!accessor.HasCodeItem()) {
Ian Rogerse0dcd462014-03-08 15:21:04 -0800177 UNIMPLEMENTED(ERROR) << "Failed to determine this object of abstract or proxy method: "
David Sehr709b0702016-10-13 09:12:37 -0700178 << ArtMethod::PrettyMethod(m);
Ian Rogerse0dcd462014-03-08 15:21:04 -0800179 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800180 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800181 uint16_t reg = accessor.RegistersSize() - accessor.InsSize();
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000182 uint32_t value = 0;
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100183 if (!GetVReg(m, reg, kReferenceVReg, &value)) {
184 return nullptr;
185 }
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000186 return reinterpret_cast<mirror::Object*>(value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800187 }
188 }
189}
190
Ian Rogers0c7abda2012-09-19 13:33:42 -0700191size_t StackVisitor::GetNativePcOffset() const {
192 DCHECK(!IsShadowFrame());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100193 return GetCurrentOatQuickMethodHeader()->NativeQuickPcOffset(cur_quick_frame_pc_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700194}
195
Mingyao Yang99170c62015-07-06 11:10:37 -0700196bool StackVisitor::GetVRegFromDebuggerShadowFrame(uint16_t vreg,
197 VRegKind kind,
198 uint32_t* val) const {
199 size_t frame_id = const_cast<StackVisitor*>(this)->GetFrameId();
200 ShadowFrame* shadow_frame = thread_->FindDebuggerShadowFrame(frame_id);
201 if (shadow_frame != nullptr) {
202 bool* updated_vreg_flags = thread_->GetUpdatedVRegFlags(frame_id);
203 DCHECK(updated_vreg_flags != nullptr);
204 if (updated_vreg_flags[vreg]) {
205 // Value is set by the debugger.
206 if (kind == kReferenceVReg) {
207 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
208 shadow_frame->GetVRegReference(vreg)));
209 } else {
210 *val = shadow_frame->GetVReg(vreg);
211 }
212 return true;
213 }
214 }
215 // No value is set by the debugger.
216 return false;
217}
218
David Srbeckycffa2542019-07-01 15:31:41 +0100219bool StackVisitor::GetVReg(ArtMethod* m,
220 uint16_t vreg,
221 VRegKind kind,
222 uint32_t* val,
223 std::optional<DexRegisterLocation> location) const {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200224 if (cur_quick_frame_ != nullptr) {
225 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800226 DCHECK(m == GetMethod());
Mingyao Yang99170c62015-07-06 11:10:37 -0700227 // Check if there is value set by the debugger.
228 if (GetVRegFromDebuggerShadowFrame(vreg, kind, val)) {
229 return true;
230 }
Nicolas Geoffrayd7651b12019-12-18 14:57:30 +0000231 bool result = false;
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000232 if (cur_oat_quick_method_header_->IsNterpMethodHeader()) {
Nicolas Geoffrayd7651b12019-12-18 14:57:30 +0000233 result = true;
234 *val = (kind == kReferenceVReg)
235 ? NterpGetVRegReference(cur_quick_frame_, vreg)
236 : NterpGetVReg(cur_quick_frame_, vreg);
237 } else {
238 DCHECK(cur_oat_quick_method_header_->IsOptimized());
239 if (location.has_value() && kind != kReferenceVReg) {
240 uint32_t val2 = *val;
241 // The caller already known the register location, so we can use the faster overload
242 // which does not decode the stack maps.
243 result = GetVRegFromOptimizedCode(location.value(), kind, val);
244 // Compare to the slower overload.
245 DCHECK_EQ(result, GetVRegFromOptimizedCode(m, vreg, kind, &val2));
246 DCHECK_EQ(*val, val2);
247 } else {
248 result = GetVRegFromOptimizedCode(m, vreg, kind, val);
249 }
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000250 }
Alex Lightb096c912019-09-25 13:33:06 -0700251 if (kind == kReferenceVReg) {
252 // Perform a read barrier in case we are in a different thread and GC is ongoing.
253 mirror::Object* out = reinterpret_cast<mirror::Object*>(static_cast<uintptr_t>(*val));
254 uintptr_t ptr_out = reinterpret_cast<uintptr_t>(GcRoot<mirror::Object>(out).Read());
255 DCHECK_LT(ptr_out, std::numeric_limits<uint32_t>::max());
256 *val = static_cast<uint32_t>(ptr_out);
257 }
Nicolas Geoffrayd7651b12019-12-18 14:57:30 +0000258 return result;
Ian Rogers0399dde2012-06-06 17:09:28 -0700259 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100260 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertz09687442015-11-17 10:35:39 +0100261 if (kind == kReferenceVReg) {
262 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
263 cur_shadow_frame_->GetVRegReference(vreg)));
264 } else {
265 *val = cur_shadow_frame_->GetVReg(vreg);
266 }
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200267 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700268 }
269}
270
Mathieu Chartiere401d142015-04-22 13:56:20 -0700271bool StackVisitor::GetVRegFromOptimizedCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100272 uint32_t* val) const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100273 DCHECK_EQ(m, GetMethod());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800274 // Can't be null or how would we compile its instructions?
275 DCHECK(m->GetCodeItem() != nullptr) << m->PrettyMethod();
David Sehr0225f8e2018-01-31 08:52:24 +0000276 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800277 uint16_t number_of_dex_registers = accessor.RegistersSize();
278 DCHECK_LT(vreg, number_of_dex_registers);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100279 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
David Srbecky052f8ca2018-04-26 15:42:54 +0100280 CodeInfo code_info(method_header);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100281
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100282 uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_);
David Srbecky052f8ca2018-04-26 15:42:54 +0100283 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100284 DCHECK(stack_map.IsValid());
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100285
286 DexRegisterMap dex_register_map = IsInInlinedFrame()
David Srbecky93bd3612018-07-02 19:30:18 +0100287 ? code_info.GetInlineDexRegisterMapOf(stack_map, current_inline_frames_.back())
David Srbeckyfd89b072018-06-03 12:00:22 +0100288 : code_info.GetDexRegisterMapOf(stack_map);
289 if (dex_register_map.empty()) {
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000290 return false;
291 }
David Srbeckyfd89b072018-06-03 12:00:22 +0100292 DCHECK_EQ(dex_register_map.size(), number_of_dex_registers);
David Srbeckye1402122018-06-13 18:20:45 +0100293 DexRegisterLocation::Kind location_kind = dex_register_map[vreg].GetKind();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100294 switch (location_kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000295 case DexRegisterLocation::Kind::kInStack: {
David Srbeckye1402122018-06-13 18:20:45 +0100296 const int32_t offset = dex_register_map[vreg].GetStackOffsetInBytes();
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100297 BitMemoryRegion stack_mask = code_info.GetStackMaskOf(stack_map);
298 if (kind == kReferenceVReg && !stack_mask.LoadBit(offset / kFrameSlotSize)) {
299 return false;
300 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100301 const uint8_t* addr = reinterpret_cast<const uint8_t*>(cur_quick_frame_) + offset;
302 *val = *reinterpret_cast<const uint32_t*>(addr);
303 return true;
304 }
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100305 case DexRegisterLocation::Kind::kInRegister: {
306 uint32_t register_mask = code_info.GetRegisterMaskOf(stack_map);
307 uint32_t reg = dex_register_map[vreg].GetMachineRegister();
308 if (kind == kReferenceVReg && !(register_mask & (1 << reg))) {
309 return false;
310 }
311 return GetRegisterIfAccessible(reg, kind, val);
312 }
David Brazdild9cb68e2015-08-25 13:52:43 +0100313 case DexRegisterLocation::Kind::kInRegisterHigh:
314 case DexRegisterLocation::Kind::kInFpuRegister:
315 case DexRegisterLocation::Kind::kInFpuRegisterHigh: {
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100316 if (kind == kReferenceVReg) {
317 return false;
318 }
David Srbeckye1402122018-06-13 18:20:45 +0100319 uint32_t reg = dex_register_map[vreg].GetMachineRegister();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100320 return GetRegisterIfAccessible(reg, kind, val);
321 }
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100322 case DexRegisterLocation::Kind::kConstant: {
323 uint32_t result = dex_register_map[vreg].GetConstant();
324 if (kind == kReferenceVReg && result != 0) {
325 return false;
326 }
327 *val = result;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100328 return true;
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100329 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000330 case DexRegisterLocation::Kind::kNone:
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100331 return false;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000332 default:
David Srbeckye1402122018-06-13 18:20:45 +0100333 LOG(FATAL) << "Unexpected location kind " << dex_register_map[vreg].GetKind();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000334 UNREACHABLE();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100335 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100336}
337
David Srbeckycffa2542019-07-01 15:31:41 +0100338bool StackVisitor::GetVRegFromOptimizedCode(DexRegisterLocation location,
339 VRegKind kind,
340 uint32_t* val) const {
341 switch (location.GetKind()) {
342 case DexRegisterLocation::Kind::kInvalid:
343 break;
344 case DexRegisterLocation::Kind::kInStack: {
345 const uint8_t* sp = reinterpret_cast<const uint8_t*>(cur_quick_frame_);
346 *val = *reinterpret_cast<const uint32_t*>(sp + location.GetStackOffsetInBytes());
347 return true;
348 }
349 case DexRegisterLocation::Kind::kInRegister:
350 case DexRegisterLocation::Kind::kInRegisterHigh:
351 case DexRegisterLocation::Kind::kInFpuRegister:
352 case DexRegisterLocation::Kind::kInFpuRegisterHigh:
353 return GetRegisterIfAccessible(location.GetMachineRegister(), kind, val);
354 case DexRegisterLocation::Kind::kConstant:
355 *val = location.GetConstant();
356 return true;
357 case DexRegisterLocation::Kind::kNone:
358 return false;
359 }
360 LOG(FATAL) << "Unexpected location kind " << location.GetKind();
361 UNREACHABLE();
362}
363
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100364bool StackVisitor::GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const {
365 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
David Brazdil77a48ae2015-09-15 12:34:04 +0000366
Vladimir Marko239d6ea2016-09-05 10:44:04 +0100367 if (kRuntimeISA == InstructionSet::kX86 && is_float) {
368 // X86 float registers are 64-bit and each XMM register is provided as two separate
369 // 32-bit registers by the context.
370 reg = (kind == kDoubleHiVReg) ? (2 * reg + 1) : (2 * reg);
371 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000372
Goran Jakovljevic986660c2015-12-10 11:44:50 +0100373 // MIPS32 float registers are used as 64-bit (for MIPS32r2 it is pair
374 // F(2n)-F(2n+1), and for MIPS32r6 it is 64-bit register F(2n)). When
375 // accessing upper 32-bits from double, reg + 1 should be used.
376 if ((kRuntimeISA == InstructionSet::kMips) && (kind == kDoubleHiVReg)) {
377 DCHECK_ALIGNED(reg, 2);
378 reg++;
379 }
380
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100381 if (!IsAccessibleRegister(reg, is_float)) {
382 return false;
383 }
384 uintptr_t ptr_val = GetRegister(reg, is_float);
385 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
386 if (target64) {
387 const bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
388 const bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
389 int64_t value_long = static_cast<int64_t>(ptr_val);
390 if (wide_lo) {
391 ptr_val = static_cast<uintptr_t>(Low32Bits(value_long));
392 } else if (wide_hi) {
393 ptr_val = static_cast<uintptr_t>(High32Bits(value_long));
394 }
395 }
396 *val = ptr_val;
397 return true;
398}
399
Mingyao Yang99170c62015-07-06 11:10:37 -0700400bool StackVisitor::GetVRegPairFromDebuggerShadowFrame(uint16_t vreg,
401 VRegKind kind_lo,
402 VRegKind kind_hi,
403 uint64_t* val) const {
404 uint32_t low_32bits;
405 uint32_t high_32bits;
406 bool success = GetVRegFromDebuggerShadowFrame(vreg, kind_lo, &low_32bits);
407 success &= GetVRegFromDebuggerShadowFrame(vreg + 1, kind_hi, &high_32bits);
408 if (success) {
409 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
410 }
411 return success;
412}
413
Mathieu Chartiere401d142015-04-22 13:56:20 -0700414bool StackVisitor::GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200415 VRegKind kind_hi, uint64_t* val) const {
416 if (kind_lo == kLongLoVReg) {
417 DCHECK_EQ(kind_hi, kLongHiVReg);
418 } else if (kind_lo == kDoubleLoVReg) {
419 DCHECK_EQ(kind_hi, kDoubleHiVReg);
420 } else {
421 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100422 UNREACHABLE();
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200423 }
Mingyao Yang99170c62015-07-06 11:10:37 -0700424 // Check if there is value set by the debugger.
425 if (GetVRegPairFromDebuggerShadowFrame(vreg, kind_lo, kind_hi, val)) {
426 return true;
427 }
Nicolas Geoffraycaafd622020-01-27 13:08:45 +0000428 if (cur_quick_frame_ == nullptr) {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100429 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200430 *val = cur_shadow_frame_->GetVRegLong(vreg);
431 return true;
432 }
Nicolas Geoffraycaafd622020-01-27 13:08:45 +0000433 if (cur_oat_quick_method_header_->IsNterpMethodHeader()) {
434 uint64_t val_lo = NterpGetVReg(cur_quick_frame_, vreg);
435 uint64_t val_hi = NterpGetVReg(cur_quick_frame_, vreg + 1);
436 *val = (val_hi << 32) + val_lo;
437 return true;
438 }
439
440 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
441 DCHECK(m == GetMethod());
442 DCHECK(cur_oat_quick_method_header_->IsOptimized());
443 return GetVRegPairFromOptimizedCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200444}
445
Mathieu Chartiere401d142015-04-22 13:56:20 -0700446bool StackVisitor::GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100447 VRegKind kind_lo, VRegKind kind_hi,
448 uint64_t* val) const {
449 uint32_t low_32bits;
450 uint32_t high_32bits;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700451 bool success = GetVRegFromOptimizedCode(m, vreg, kind_lo, &low_32bits);
452 success &= GetVRegFromOptimizedCode(m, vreg + 1, kind_hi, &high_32bits);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100453 if (success) {
454 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
455 }
456 return success;
457}
458
459bool StackVisitor::GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
460 VRegKind kind_lo, uint64_t* val) const {
461 const bool is_float = (kind_lo == kDoubleLoVReg);
462 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
463 return false;
464 }
465 uintptr_t ptr_val_lo = GetRegister(reg_lo, is_float);
466 uintptr_t ptr_val_hi = GetRegister(reg_hi, is_float);
467 bool target64 = Is64BitInstructionSet(kRuntimeISA);
468 if (target64) {
469 int64_t value_long_lo = static_cast<int64_t>(ptr_val_lo);
470 int64_t value_long_hi = static_cast<int64_t>(ptr_val_hi);
471 ptr_val_lo = static_cast<uintptr_t>(Low32Bits(value_long_lo));
472 ptr_val_hi = static_cast<uintptr_t>(High32Bits(value_long_hi));
473 }
474 *val = (static_cast<uint64_t>(ptr_val_hi) << 32) | static_cast<uint32_t>(ptr_val_lo);
475 return true;
476}
477
Vladimir Marko439d1262019-04-12 14:45:07 +0100478ShadowFrame* StackVisitor::PrepareSetVReg(ArtMethod* m, uint16_t vreg, bool wide) {
David Sehr0225f8e2018-01-31 08:52:24 +0000479 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800480 if (!accessor.HasCodeItem()) {
Vladimir Marko439d1262019-04-12 14:45:07 +0100481 return nullptr;
Mingyao Yang99170c62015-07-06 11:10:37 -0700482 }
483 ShadowFrame* shadow_frame = GetCurrentShadowFrame();
484 if (shadow_frame == nullptr) {
485 // This is a compiled frame: we must prepare and update a shadow frame that will
486 // be executed by the interpreter after deoptimization of the stack.
487 const size_t frame_id = GetFrameId();
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800488 const uint16_t num_regs = accessor.RegistersSize();
Mingyao Yang99170c62015-07-06 11:10:37 -0700489 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
490 CHECK(shadow_frame != nullptr);
Vladimir Marko439d1262019-04-12 14:45:07 +0100491 // Remember the vreg(s) has been set for debugging and must not be overwritten by the
Mingyao Yang99170c62015-07-06 11:10:37 -0700492 // original value during deoptimization of the stack.
493 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
Vladimir Marko439d1262019-04-12 14:45:07 +0100494 if (wide) {
495 thread_->GetUpdatedVRegFlags(frame_id)[vreg + 1] = true;
496 }
Mingyao Yang99170c62015-07-06 11:10:37 -0700497 }
Vladimir Marko439d1262019-04-12 14:45:07 +0100498 return shadow_frame;
499}
500
501bool StackVisitor::SetVReg(ArtMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind) {
502 DCHECK(kind == kIntVReg || kind == kFloatVReg);
503 ShadowFrame* shadow_frame = PrepareSetVReg(m, vreg, /* wide= */ false);
504 if (shadow_frame == nullptr) {
505 return false;
Mingyao Yang99170c62015-07-06 11:10:37 -0700506 }
Vladimir Marko439d1262019-04-12 14:45:07 +0100507 shadow_frame->SetVReg(vreg, new_value);
508 return true;
509}
510
511bool StackVisitor::SetVRegReference(ArtMethod* m, uint16_t vreg, ObjPtr<mirror::Object> new_value) {
512 ShadowFrame* shadow_frame = PrepareSetVReg(m, vreg, /* wide= */ false);
513 if (shadow_frame == nullptr) {
514 return false;
515 }
516 shadow_frame->SetVRegReference(vreg, new_value);
Mingyao Yang99170c62015-07-06 11:10:37 -0700517 return true;
518}
519
Mingyao Yang636b9252015-07-31 16:40:24 -0700520bool StackVisitor::SetVRegPair(ArtMethod* m,
521 uint16_t vreg,
522 uint64_t new_value,
523 VRegKind kind_lo,
524 VRegKind kind_hi) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700525 if (kind_lo == kLongLoVReg) {
526 DCHECK_EQ(kind_hi, kLongHiVReg);
527 } else if (kind_lo == kDoubleLoVReg) {
528 DCHECK_EQ(kind_hi, kDoubleHiVReg);
529 } else {
530 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
531 UNREACHABLE();
532 }
Vladimir Marko439d1262019-04-12 14:45:07 +0100533 ShadowFrame* shadow_frame = PrepareSetVReg(m, vreg, /* wide= */ true);
Mingyao Yang99170c62015-07-06 11:10:37 -0700534 if (shadow_frame == nullptr) {
Vladimir Marko439d1262019-04-12 14:45:07 +0100535 return false;
Mingyao Yang99170c62015-07-06 11:10:37 -0700536 }
537 shadow_frame->SetVRegLong(vreg, new_value);
538 return true;
539}
540
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100541bool StackVisitor::IsAccessibleGPR(uint32_t reg) const {
542 DCHECK(context_ != nullptr);
543 return context_->IsAccessibleGPR(reg);
544}
545
Mathieu Chartier815873e2014-02-13 18:02:13 -0800546uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100547 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
548 DCHECK(context_ != nullptr);
Mathieu Chartier815873e2014-02-13 18:02:13 -0800549 return context_->GetGPRAddress(reg);
550}
551
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100552uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
553 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
554 DCHECK(context_ != nullptr);
555 return context_->GetGPR(reg);
Ian Rogers0399dde2012-06-06 17:09:28 -0700556}
557
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100558bool StackVisitor::IsAccessibleFPR(uint32_t reg) const {
559 DCHECK(context_ != nullptr);
560 return context_->IsAccessibleFPR(reg);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200561}
562
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100563uintptr_t StackVisitor::GetFPR(uint32_t reg) const {
564 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
565 DCHECK(context_ != nullptr);
566 return context_->GetFPR(reg);
567}
568
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000569uintptr_t StackVisitor::GetReturnPcAddr() const {
570 uintptr_t sp = reinterpret_cast<uintptr_t>(GetCurrentQuickFrame());
571 DCHECK_NE(sp, 0u);
572 return sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
573}
574
Ian Rogers0399dde2012-06-06 17:09:28 -0700575uintptr_t StackVisitor::GetReturnPc() const {
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000576 return *reinterpret_cast<uintptr_t*>(GetReturnPcAddr());
Ian Rogers0399dde2012-06-06 17:09:28 -0700577}
578
579void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000580 *reinterpret_cast<uintptr_t*>(GetReturnPcAddr()) = new_ret_pc;
Ian Rogers0399dde2012-06-06 17:09:28 -0700581}
582
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100583size_t StackVisitor::ComputeNumFrames(Thread* thread, StackWalkKind walk_kind) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700584 struct NumFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100585 NumFramesVisitor(Thread* thread_in, StackWalkKind walk_kind_in)
586 : StackVisitor(thread_in, nullptr, walk_kind_in), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700587
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100588 bool VisitFrame() override {
Ian Rogers0399dde2012-06-06 17:09:28 -0700589 frames++;
590 return true;
591 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700592
Ian Rogers0399dde2012-06-06 17:09:28 -0700593 size_t frames;
594 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100595 NumFramesVisitor visitor(thread, walk_kind);
Ian Rogers0399dde2012-06-06 17:09:28 -0700596 visitor.WalkStack(true);
597 return visitor.frames;
598}
599
Mathieu Chartiere401d142015-04-22 13:56:20 -0700600bool StackVisitor::GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700601 struct HasMoreFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100602 HasMoreFramesVisitor(Thread* thread,
603 StackWalkKind walk_kind,
604 size_t num_frames,
605 size_t frame_height)
606 : StackVisitor(thread, nullptr, walk_kind, num_frames),
607 frame_height_(frame_height),
608 found_frame_(false),
609 has_more_frames_(false),
610 next_method_(nullptr),
611 next_dex_pc_(0) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700612 }
613
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100614 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700615 if (found_frame_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700616 ArtMethod* method = GetMethod();
Ian Rogers5cf98192014-05-29 21:31:50 -0700617 if (method != nullptr && !method->IsRuntimeMethod()) {
618 has_more_frames_ = true;
619 next_method_ = method;
620 next_dex_pc_ = GetDexPc();
621 return false; // End stack walk once next method is found.
622 }
623 } else if (GetFrameHeight() == frame_height_) {
624 found_frame_ = true;
625 }
626 return true;
627 }
628
629 size_t frame_height_;
630 bool found_frame_;
631 bool has_more_frames_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700632 ArtMethod* next_method_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700633 uint32_t next_dex_pc_;
634 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100635 HasMoreFramesVisitor visitor(thread_, walk_kind_, GetNumFrames(), GetFrameHeight());
Ian Rogers5cf98192014-05-29 21:31:50 -0700636 visitor.WalkStack(true);
637 *next_method = visitor.next_method_;
638 *next_dex_pc = visitor.next_dex_pc_;
639 return visitor.has_more_frames_;
640}
641
Ian Rogers7a22fa62013-01-23 12:16:16 -0800642void StackVisitor::DescribeStack(Thread* thread) {
Ian Rogers306057f2012-11-26 12:45:53 -0800643 struct DescribeStackVisitor : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800644 explicit DescribeStackVisitor(Thread* thread_in)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100645 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800646
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100647 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers306057f2012-11-26 12:45:53 -0800648 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation();
649 return true;
650 }
651 };
Ian Rogers7a22fa62013-01-23 12:16:16 -0800652 DescribeStackVisitor visitor(thread);
Ian Rogers306057f2012-11-26 12:45:53 -0800653 visitor.WalkStack(true);
654}
655
Ian Rogers40e3bac2012-11-20 00:09:14 -0800656std::string StackVisitor::DescribeLocation() const {
657 std::string result("Visiting method '");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700658 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700659 if (m == nullptr) {
Ian Rogers306057f2012-11-26 12:45:53 -0800660 return "upcall";
661 }
David Sehr709b0702016-10-13 09:12:37 -0700662 result += m->PrettyMethod();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800663 result += StringPrintf("' at dex PC 0x%04x", GetDexPc());
Ian Rogers40e3bac2012-11-20 00:09:14 -0800664 if (!IsShadowFrame()) {
665 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc()));
666 }
667 return result;
668}
669
Alex Lightdba61482016-12-21 08:20:29 -0800670void StackVisitor::SetMethod(ArtMethod* method) {
671 DCHECK(GetMethod() != nullptr);
672 if (cur_shadow_frame_ != nullptr) {
673 cur_shadow_frame_->SetMethod(method);
674 } else {
675 DCHECK(cur_quick_frame_ != nullptr);
Nicolas Geoffray226805d2018-12-14 10:59:02 +0000676 CHECK(!IsInInlinedFrame()) << "We do not support setting inlined method's ArtMethod: "
677 << GetMethod()->PrettyMethod() << " is inlined into "
678 << GetOuterMethod()->PrettyMethod();
Alex Light1ebe4fe2017-01-30 14:57:11 -0800679 *cur_quick_frame_ = method;
Alex Lightdba61482016-12-21 08:20:29 -0800680 }
681}
682
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100683static void AssertPcIsWithinQuickCode(ArtMethod* method, uintptr_t pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700684 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100685 if (method->IsNative() || method->IsRuntimeMethod() || method->IsProxyMethod()) {
686 return;
687 }
688
689 if (pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) {
690 return;
691 }
692
Mingyao Yang88ca8ba2017-05-23 14:21:07 -0700693 Runtime* runtime = Runtime::Current();
694 if (runtime->UseJitCompilation() &&
695 runtime->GetJit()->GetCodeCache()->ContainsPc(reinterpret_cast<const void*>(pc))) {
696 return;
697 }
698
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100699 const void* code = method->GetEntryPointFromQuickCompiledCode();
Alex Lightdb01a092017-04-03 15:39:55 -0700700 if (code == GetQuickInstrumentationEntryPoint() || code == GetInvokeObsoleteMethodStub()) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100701 return;
702 }
703
704 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
705 if (class_linker->IsQuickToInterpreterBridge(code) ||
706 class_linker->IsQuickResolutionStub(code)) {
707 return;
708 }
709
Calin Juravleffc87072016-04-20 14:22:09 +0100710 if (runtime->UseJitCompilation() && runtime->GetJit()->GetCodeCache()->ContainsPc(code)) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100711 return;
712 }
713
Mingyao Yang063fc772016-08-02 11:02:54 -0700714 uint32_t code_size = OatQuickMethodHeader::FromEntryPoint(code)->GetCodeSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100715 uintptr_t code_start = reinterpret_cast<uintptr_t>(code);
716 CHECK(code_start <= pc && pc <= (code_start + code_size))
David Sehr709b0702016-10-13 09:12:37 -0700717 << method->PrettyMethod()
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100718 << " pc=" << std::hex << pc
Roland Levillain0d5a2812015-11-13 10:07:31 +0000719 << " code_start=" << code_start
720 << " code_size=" << code_size;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100721}
722
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700723void StackVisitor::SanityCheckFrame() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800724 if (kIsDebugBuild) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700725 ArtMethod* method = GetMethod();
Vladimir Markod93e3742018-07-18 10:58:13 +0100726 ObjPtr<mirror::Class> declaring_class = method->GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700727 // Runtime methods have null declaring class.
728 if (!method->IsRuntimeMethod()) {
729 CHECK(declaring_class != nullptr);
730 CHECK_EQ(declaring_class->GetClass(), declaring_class->GetClass()->GetClass())
731 << declaring_class;
732 } else {
733 CHECK(declaring_class == nullptr);
734 }
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700735 Runtime* const runtime = Runtime::Current();
736 LinearAlloc* const linear_alloc = runtime->GetLinearAlloc();
737 if (!linear_alloc->Contains(method)) {
738 // Check class linker linear allocs.
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100739 // We get the canonical method as copied methods may have their declaring
740 // class from another class loader.
Ulya Trafimovich819b3622019-12-12 17:59:10 +0000741 const PointerSize ptrSize = runtime->GetClassLinker()->GetImagePointerSize();
742 ArtMethod* canonical = method->GetCanonicalMethod(ptrSize);
Vladimir Markod93e3742018-07-18 10:58:13 +0100743 ObjPtr<mirror::Class> klass = canonical->GetDeclaringClass();
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700744 LinearAlloc* const class_linear_alloc = (klass != nullptr)
Mathieu Chartier5b830502016-03-02 10:30:23 -0800745 ? runtime->GetClassLinker()->GetAllocatorForClassLoader(klass->GetClassLoader())
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700746 : linear_alloc;
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100747 if (!class_linear_alloc->Contains(canonical)) {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700748 // Check image space.
749 bool in_image = false;
750 for (auto& space : runtime->GetHeap()->GetContinuousSpaces()) {
751 if (space->IsImageSpace()) {
752 auto* image_space = space->AsImageSpace();
753 const auto& header = image_space->GetImageHeader();
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700754 const ImageSection& methods = header.GetMethodsSection();
755 const ImageSection& runtime_methods = header.GetRuntimeMethodsSection();
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100756 const size_t offset = reinterpret_cast<const uint8_t*>(canonical) - image_space->Begin();
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700757 if (methods.Contains(offset) || runtime_methods.Contains(offset)) {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700758 in_image = true;
759 break;
760 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700761 }
762 }
Nicolas Geoffray48b40cc2017-08-07 16:52:40 +0100763 CHECK(in_image) << canonical->PrettyMethod() << " not in linear alloc or image";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700764 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700765 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800766 if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100767 AssertPcIsWithinQuickCode(method, cur_quick_frame_pc_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800768 // Frame sanity.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100769 size_t frame_size = GetCurrentQuickFrameInfo().FrameSizeInBytes();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800770 CHECK_NE(frame_size, 0u);
Nicolas Geoffray00391822019-12-10 10:17:23 +0000771 // For compiled code, we could try to have a rough guess at an upper size we expect
772 // to see for a frame:
Andreas Gampe5b417b92014-03-10 14:18:35 -0700773 // 256 registers
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700774 // 2 words HandleScope overhead
Andreas Gampe5b417b92014-03-10 14:18:35 -0700775 // 3+3 register spills
Brian Carlstromed08bd42014-03-19 18:34:17 -0700776 // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word);
Nicolas Geoffray00391822019-12-10 10:17:23 +0000777 const size_t kMaxExpectedFrameSize = interpreter::kMaxNterpFrame;
David Sehr709b0702016-10-13 09:12:37 -0700778 CHECK_LE(frame_size, kMaxExpectedFrameSize) << method->PrettyMethod();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100779 size_t return_pc_offset = GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800780 CHECK_LT(return_pc_offset, frame_size);
781 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700782 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700783}
784
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100785QuickMethodFrameInfo StackVisitor::GetCurrentQuickFrameInfo() const {
786 if (cur_oat_quick_method_header_ != nullptr) {
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000787 if (cur_oat_quick_method_header_->IsOptimized()) {
788 return cur_oat_quick_method_header_->GetFrameInfo();
789 } else {
790 DCHECK(cur_oat_quick_method_header_->IsNterpMethodHeader());
791 return NterpFrameInfo(cur_quick_frame_);
792 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100793 }
794
795 ArtMethod* method = GetMethod();
796 Runtime* runtime = Runtime::Current();
797
798 if (method->IsAbstract()) {
Vladimir Markod3083dd2018-05-17 08:43:47 +0100799 return RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100800 }
801
802 // This goes before IsProxyMethod since runtime methods have a null declaring class.
803 if (method->IsRuntimeMethod()) {
804 return runtime->GetRuntimeMethodFrameInfo(method);
805 }
806
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100807 if (method->IsProxyMethod()) {
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000808 // There is only one direct method of a proxy class: the constructor. A direct method is
809 // cloned from the original java.lang.reflect.Proxy and is executed as usual quick
810 // compiled method without any stubs. Therefore the method must have a OatQuickMethodHeader.
811 DCHECK(!method->IsDirect() && !method->IsConstructor())
812 << "Constructors of proxy classes must have a OatQuickMethodHeader";
Vladimir Markod3083dd2018-05-17 08:43:47 +0100813 return RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100814 }
815
Vladimir Marko2196c652017-11-30 16:16:07 +0000816 // The only remaining case is if the method is native and uses the generic JNI stub,
817 // called either directly or through some (resolution, instrumentation) trampoline.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100818 DCHECK(method->IsNative());
Vladimir Marko2196c652017-11-30 16:16:07 +0000819 if (kIsDebugBuild) {
820 ClassLinker* class_linker = runtime->GetClassLinker();
821 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(method,
822 kRuntimePointerSize);
823 CHECK(class_linker->IsQuickGenericJniStub(entry_point) ||
824 // The current entrypoint (after filtering out trampolines) may have changed
825 // from GenericJNI to JIT-compiled stub since we have entered this frame.
826 (runtime->GetJit() != nullptr &&
827 runtime->GetJit()->GetCodeCache()->ContainsPc(entry_point))) << method->PrettyMethod();
828 }
Vladimir Marko6e043bb2020-02-10 16:56:54 +0000829 // Generic JNI frame is just like the SaveRefsAndArgs frame.
830 // Note that HandleScope, if any, is below the frame.
831 return RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100832}
833
Andreas Gampe585da952016-12-02 14:52:29 -0800834template <StackVisitor::CountTransitions kCount>
Ian Rogers0399dde2012-06-06 17:09:28 -0700835void StackVisitor::WalkStack(bool include_transitions) {
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -0800836 if (check_suspended_) {
837 DCHECK(thread_ == Thread::Current() || thread_->IsSuspended());
838 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800839 CHECK_EQ(cur_depth_, 0U);
Alex Lighte0c6d432020-01-22 22:04:20 +0000840 size_t inlined_frames_count = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700841
Alex Lighte0c6d432020-01-22 22:04:20 +0000842 for (const ManagedStack* current_fragment = thread_->GetManagedStack();
843 current_fragment != nullptr; current_fragment = current_fragment->GetLink()) {
844 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
845 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
846 cur_quick_frame_pc_ = 0;
Nicolas Geoffray51ad7fe2020-02-04 12:46:47 +0000847 DCHECK(cur_oat_quick_method_header_ == nullptr);
Alex Lighte0c6d432020-01-22 22:04:20 +0000848 if (cur_quick_frame_ != nullptr) { // Handle quick stack frames.
849 // Can't be both a shadow and a quick fragment.
850 DCHECK(current_fragment->GetTopShadowFrame() == nullptr);
851 ArtMethod* method = *cur_quick_frame_;
852 DCHECK(method != nullptr);
853 bool header_retrieved = false;
854 if (method->IsNative()) {
855 // We do not have a PC for the first frame, so we cannot simply use
856 // ArtMethod::GetOatQuickMethodHeader() as we're unable to distinguish there
857 // between GenericJNI frame and JIT-compiled JNI stub; the entrypoint may have
858 // changed since the frame was entered. The top quick frame tag indicates
859 // GenericJNI here, otherwise it's either AOT-compiled or JNI-compiled JNI stub.
860 if (UNLIKELY(current_fragment->GetTopQuickFrameTag())) {
861 // The generic JNI does not have any method header.
862 cur_oat_quick_method_header_ = nullptr;
863 } else {
864 const void* existing_entry_point = method->GetEntryPointFromQuickCompiledCode();
865 CHECK(existing_entry_point != nullptr);
866 Runtime* runtime = Runtime::Current();
867 ClassLinker* class_linker = runtime->GetClassLinker();
868 // Check whether we can quickly get the header from the current entrypoint.
869 if (!class_linker->IsQuickGenericJniStub(existing_entry_point) &&
870 !class_linker->IsQuickResolutionStub(existing_entry_point) &&
871 existing_entry_point != GetQuickInstrumentationEntryPoint()) {
872 cur_oat_quick_method_header_ =
873 OatQuickMethodHeader::FromEntryPoint(existing_entry_point);
Vladimir Marko2196c652017-11-30 16:16:07 +0000874 } else {
Alex Lighte0c6d432020-01-22 22:04:20 +0000875 const void* code = method->GetOatMethodQuickCode(class_linker->GetImagePointerSize());
876 if (code != nullptr) {
877 cur_oat_quick_method_header_ = OatQuickMethodHeader::FromEntryPoint(code);
Vladimir Marko2196c652017-11-30 16:16:07 +0000878 } else {
Alex Lighte0c6d432020-01-22 22:04:20 +0000879 // This must be a JITted JNI stub frame.
880 CHECK(runtime->GetJit() != nullptr);
881 code = runtime->GetJit()->GetCodeCache()->GetJniStubCode(method);
882 CHECK(code != nullptr) << method->PrettyMethod();
883 cur_oat_quick_method_header_ = OatQuickMethodHeader::FromCodePointer(code);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100884 }
885 }
886 }
Alex Lighte0c6d432020-01-22 22:04:20 +0000887 header_retrieved = true;
jeffhao6641ea12013-01-02 18:13:42 -0800888 }
Alex Lighte0c6d432020-01-22 22:04:20 +0000889 while (method != nullptr) {
890 if (!header_retrieved) {
891 cur_oat_quick_method_header_ = method->GetOatQuickMethodHeader(cur_quick_frame_pc_);
892 }
893 header_retrieved = false; // Force header retrieval in next iteration.
894 SanityCheckFrame();
895
896 if ((walk_kind_ == StackWalkKind::kIncludeInlinedFrames)
897 && (cur_oat_quick_method_header_ != nullptr)
898 && cur_oat_quick_method_header_->IsOptimized()
899 && !method->IsNative() // JNI methods cannot have any inlined frames.
900 && CodeInfo::HasInlineInfo(cur_oat_quick_method_header_->GetOptimizedCodeInfoPtr())) {
901 DCHECK_NE(cur_quick_frame_pc_, 0u);
902 CodeInfo* code_info = GetCurrentInlineInfo();
903 StackMap* stack_map = GetCurrentStackMap();
904 if (stack_map->IsValid() && stack_map->HasInlineInfo()) {
905 DCHECK_EQ(current_inline_frames_.size(), 0u);
906 for (current_inline_frames_ = code_info->GetInlineInfosOf(*stack_map);
907 !current_inline_frames_.empty();
908 current_inline_frames_.pop_back()) {
909 bool should_continue = VisitFrame();
910 if (UNLIKELY(!should_continue)) {
911 return;
912 }
913 cur_depth_++;
914 inlined_frames_count++;
915 }
916 }
917 }
918
Ian Rogers0399dde2012-06-06 17:09:28 -0700919 bool should_continue = VisitFrame();
Alex Lighte0c6d432020-01-22 22:04:20 +0000920 if (UNLIKELY(!should_continue)) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700921 return;
922 }
Alex Lighte0c6d432020-01-22 22:04:20 +0000923
924 QuickMethodFrameInfo frame_info = GetCurrentQuickFrameInfo();
925 if (context_ != nullptr) {
926 context_->FillCalleeSaves(reinterpret_cast<uint8_t*>(cur_quick_frame_), frame_info);
927 }
928 // Compute PC for next stack frame from return PC.
929 size_t frame_size = frame_info.FrameSizeInBytes();
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000930 uintptr_t return_pc_addr = GetReturnPcAddr();
Alex Lighte0c6d432020-01-22 22:04:20 +0000931 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
932
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000933 if (UNLIKELY(reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc)) {
Alex Lighte0c6d432020-01-22 22:04:20 +0000934 // While profiling, the return pc is restored from the side stack, except when walking
935 // the stack for an exception where the side stack will be unwound in VisitFrame.
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000936 const std::map<uintptr_t, instrumentation::InstrumentationStackFrame>&
937 instrumentation_stack = *thread_->GetInstrumentationStack();
938 auto it = instrumentation_stack.find(return_pc_addr);
939 CHECK(it != instrumentation_stack.end());
940 const instrumentation::InstrumentationStackFrame& instrumentation_frame = it->second;
941 if (GetMethod() ==
942 Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveAllCalleeSaves)) {
943 // Skip runtime save all callee frames which are used to deliver exceptions.
944 } else if (instrumentation_frame.interpreter_entry_) {
945 ArtMethod* callee =
946 Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs);
947 CHECK_EQ(GetMethod(), callee) << "Expected: " << ArtMethod::PrettyMethod(callee)
948 << " Found: " << ArtMethod::PrettyMethod(GetMethod());
Nicolas Geoffray8feb7eb2020-02-04 09:21:33 +0000949 } else if (!instrumentation_frame.method_->IsRuntimeMethod()) {
950 // Trampolines get replaced with their actual method in the stack,
951 // so don't do the check below for runtime methods.
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000952 // Instrumentation generally doesn't distinguish between a method's obsolete and
953 // non-obsolete version.
954 CHECK_EQ(instrumentation_frame.method_->GetNonObsoleteMethod(),
955 GetMethod()->GetNonObsoleteMethod())
956 << "Expected: "
957 << ArtMethod::PrettyMethod(instrumentation_frame.method_->GetNonObsoleteMethod())
958 << " Found: " << ArtMethod::PrettyMethod(GetMethod()->GetNonObsoleteMethod());
Alex Lighte0c6d432020-01-22 22:04:20 +0000959 }
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000960 return_pc = instrumentation_frame.return_pc_;
Alex Lighte0c6d432020-01-22 22:04:20 +0000961 }
962
963 cur_quick_frame_pc_ = return_pc;
964 uint8_t* next_frame = reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size;
965 cur_quick_frame_ = reinterpret_cast<ArtMethod**>(next_frame);
966
967 if (kDebugStackWalk) {
968 LOG(INFO) << ArtMethod::PrettyMethod(method) << "@" << method << " size=" << frame_size
969 << std::boolalpha
970 << " optimized=" << (cur_oat_quick_method_header_ != nullptr &&
971 cur_oat_quick_method_header_->IsOptimized())
972 << " native=" << method->IsNative()
973 << std::noboolalpha
974 << " entrypoints=" << method->GetEntryPointFromQuickCompiledCode()
975 << "," << (method->IsNative() ? method->GetEntryPointFromJni() : nullptr)
976 << " next=" << *cur_quick_frame_;
977 }
978
979 if (kCount == CountTransitions::kYes || !method->IsRuntimeMethod()) {
980 cur_depth_++;
981 }
982 method = *cur_quick_frame_;
Alex Light721e4022020-01-14 14:45:40 -0800983 }
Nicolas Geoffray51ad7fe2020-02-04 12:46:47 +0000984 // We reached a transition frame, it doesn't have a method header.
985 cur_oat_quick_method_header_ = nullptr;
Alex Lighte0c6d432020-01-22 22:04:20 +0000986 } else if (cur_shadow_frame_ != nullptr) {
987 do {
988 SanityCheckFrame();
989 bool should_continue = VisitFrame();
990 if (UNLIKELY(!should_continue)) {
991 return;
992 }
993 cur_depth_++;
994 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
995 } while (cur_shadow_frame_ != nullptr);
996 }
997 if (include_transitions) {
998 bool should_continue = VisitFrame();
999 if (!should_continue) {
1000 return;
Ian Rogers0399dde2012-06-06 17:09:28 -07001001 }
1002 }
Alex Lighte0c6d432020-01-22 22:04:20 +00001003 if (kCount == CountTransitions::kYes) {
1004 cur_depth_++;
Andreas Gampe585da952016-12-02 14:52:29 -08001005 }
Alex Lighte0c6d432020-01-22 22:04:20 +00001006 }
1007 if (num_frames_ != 0) {
1008 CHECK_EQ(cur_depth_, num_frames_);
1009 }
Ian Rogers0399dde2012-06-06 17:09:28 -07001010}
1011
Andreas Gampe585da952016-12-02 14:52:29 -08001012template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kYes>(bool);
1013template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kNo>(bool);
1014
Elliott Hughes68e76522011-10-05 13:22:16 -07001015} // namespace art