Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 1 | /* |
| 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 Light | b096c91 | 2019-09-25 13:33:06 -0700 | [diff] [blame] | 18 | #include <limits> |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 19 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 20 | #include "android-base/stringprintf.h" |
| 21 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 22 | #include "arch/context.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 23 | #include "art_method-inl.h" |
Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 24 | #include "base/callee_save_type.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 25 | #include "base/enums.h" |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 26 | #include "base/hex_dump.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 27 | #include "dex/dex_file_types.h" |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 28 | #include "entrypoints/entrypoint_utils-inl.h" |
Vladimir Marko | d3083dd | 2018-05-17 08:43:47 +0100 | [diff] [blame] | 29 | #include "entrypoints/quick/callee_save_frame.h" |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 30 | #include "entrypoints/runtime_asm_entrypoints.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 31 | #include "gc/space/image_space.h" |
| 32 | #include "gc/space/space-inl.h" |
Nicolas Geoffray | 0315efa | 2020-06-26 11:42:39 +0100 | [diff] [blame] | 33 | #include "interpreter/mterp/nterp.h" |
Vladimir Marko | 6ec2a1b | 2018-05-22 15:33:48 +0100 | [diff] [blame] | 34 | #include "interpreter/shadow_frame-inl.h" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 35 | #include "jit/jit.h" |
| 36 | #include "jit/jit_code_cache.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 37 | #include "linear_alloc.h" |
Andreas Gampe | 513061a | 2017-06-01 09:17:34 -0700 | [diff] [blame] | 38 | #include "managed_stack.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 39 | #include "mirror/class-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 40 | #include "mirror/object-inl.h" |
| 41 | #include "mirror/object_array-inl.h" |
Nicolas Geoffray | 013d1ee | 2019-12-04 16:18:15 +0000 | [diff] [blame] | 42 | #include "nterp_helpers.h" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 43 | #include "oat_quick_method_header.h" |
Vladimir Marko | 439d126 | 2019-04-12 14:45:07 +0100 | [diff] [blame] | 44 | #include "obj_ptr-inl.h" |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 45 | #include "quick/quick_method_frame_info.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 46 | #include "runtime.h" |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 47 | #include "thread.h" |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 48 | #include "thread_list.h" |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 49 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 50 | namespace art { |
| 51 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 52 | using android::base::StringPrintf; |
| 53 | |
Mathieu Chartier | 8405bfd | 2016-02-05 12:00:49 -0800 | [diff] [blame] | 54 | static constexpr bool kDebugStackWalk = false; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 55 | |
Hiroshi Yamauchi | 02f365f | 2017-02-03 15:06:00 -0800 | [diff] [blame] | 56 | StackVisitor::StackVisitor(Thread* thread, |
| 57 | Context* context, |
| 58 | StackWalkKind walk_kind, |
| 59 | bool check_suspended) |
| 60 | : StackVisitor(thread, context, walk_kind, 0, check_suspended) {} |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 61 | |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 62 | StackVisitor::StackVisitor(Thread* thread, |
| 63 | Context* context, |
| 64 | StackWalkKind walk_kind, |
Hiroshi Yamauchi | 02f365f | 2017-02-03 15:06:00 -0800 | [diff] [blame] | 65 | size_t num_frames, |
| 66 | bool check_suspended) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 67 | : thread_(thread), |
| 68 | walk_kind_(walk_kind), |
| 69 | cur_shadow_frame_(nullptr), |
| 70 | cur_quick_frame_(nullptr), |
| 71 | cur_quick_frame_pc_(0), |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 72 | cur_oat_quick_method_header_(nullptr), |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 73 | num_frames_(num_frames), |
| 74 | cur_depth_(0), |
David Srbecky | 145a18a | 2019-06-03 14:35:22 +0100 | [diff] [blame] | 75 | cur_inline_info_(nullptr, CodeInfo()), |
| 76 | cur_stack_map_(0, StackMap()), |
Hiroshi Yamauchi | 02f365f | 2017-02-03 15:06:00 -0800 | [diff] [blame] | 77 | context_(context), |
| 78 | check_suspended_(check_suspended) { |
| 79 | if (check_suspended_) { |
| 80 | DCHECK(thread == Thread::Current() || thread->IsSuspended()) << *thread; |
| 81 | } |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 82 | } |
| 83 | |
David Srbecky | 145a18a | 2019-06-03 14:35:22 +0100 | [diff] [blame] | 84 | CodeInfo* StackVisitor::GetCurrentInlineInfo() const { |
| 85 | DCHECK(!(*cur_quick_frame_)->IsNative()); |
| 86 | const OatQuickMethodHeader* header = GetCurrentOatQuickMethodHeader(); |
| 87 | if (cur_inline_info_.first != header) { |
David Srbecky | 0d4567f | 2019-05-30 22:45:40 +0100 | [diff] [blame] | 88 | cur_inline_info_ = std::make_pair(header, CodeInfo::DecodeInlineInfoOnly(header)); |
David Srbecky | 145a18a | 2019-06-03 14:35:22 +0100 | [diff] [blame] | 89 | } |
| 90 | return &cur_inline_info_.second; |
| 91 | } |
| 92 | |
| 93 | StackMap* 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 Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 104 | ArtMethod* StackVisitor::GetMethod() const { |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 105 | if (cur_shadow_frame_ != nullptr) { |
| 106 | return cur_shadow_frame_->GetMethod(); |
| 107 | } else if (cur_quick_frame_ != nullptr) { |
| 108 | if (IsInInlinedFrame()) { |
David Srbecky | 145a18a | 2019-06-03 14:35:22 +0100 | [diff] [blame] | 109 | CodeInfo* code_info = GetCurrentInlineInfo(); |
Mathieu Chartier | 45bf250 | 2016-03-31 11:07:09 -0700 | [diff] [blame] | 110 | DCHECK(walk_kind_ != StackWalkKind::kSkipInlinedFrames); |
David Srbecky | 145a18a | 2019-06-03 14:35:22 +0100 | [diff] [blame] | 111 | return GetResolvedMethod(*GetCurrentQuickFrame(), *code_info, current_inline_frames_); |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 112 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 113 | return *cur_quick_frame_; |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 114 | } |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 115 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 116 | return nullptr; |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 119 | uint32_t StackVisitor::GetDexPc(bool abort_on_failure) const { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 120 | if (cur_shadow_frame_ != nullptr) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 121 | return cur_shadow_frame_->GetDexPC(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 122 | } else if (cur_quick_frame_ != nullptr) { |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 123 | if (IsInInlinedFrame()) { |
David Srbecky | 93bd361 | 2018-07-02 19:30:18 +0100 | [diff] [blame] | 124 | return current_inline_frames_.back().GetDexPc(); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 125 | } else if (cur_oat_quick_method_header_ == nullptr) { |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 126 | return dex::kDexNoIndex; |
Nicolas Geoffray | 013d1ee | 2019-12-04 16:18:15 +0000 | [diff] [blame] | 127 | } 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 Srbecky | 145a18a | 2019-06-03 14:35:22 +0100 | [diff] [blame] | 131 | StackMap* stack_map = GetCurrentStackMap(); |
| 132 | DCHECK(stack_map->IsValid()); |
| 133 | return stack_map->GetDexPc(); |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 134 | } else { |
Nicolas Geoffray | 013d1ee | 2019-12-04 16:18:15 +0000 | [diff] [blame] | 135 | DCHECK(cur_oat_quick_method_header_->IsNterpMethodHeader()); |
| 136 | return NterpGetDexPC(cur_quick_frame_); |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 137 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 138 | } else { |
| 139 | return 0; |
| 140 | } |
| 141 | } |
| 142 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 143 | extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 144 | REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | a836bc9 | 2014-11-25 16:30:53 +0100 | [diff] [blame] | 145 | |
Vladimir Marko | abedfca | 2019-05-23 14:07:47 +0100 | [diff] [blame] | 146 | ObjPtr<mirror::Object> StackVisitor::GetThisObject() const { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 147 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 148 | ArtMethod* m = GetMethod(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 149 | if (m->IsStatic()) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 150 | return nullptr; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 151 | } else if (m->IsNative()) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 152 | if (cur_quick_frame_ != nullptr) { |
Vladimir Marko | cedec9d | 2021-02-08 16:16:13 +0000 | [diff] [blame] | 153 | // The `this` reference is stored in the first out vreg in the caller's frame. |
| 154 | const size_t frame_size = GetCurrentQuickFrameInfo().FrameSizeInBytes(); |
| 155 | auto* stack_ref = reinterpret_cast<StackReference<mirror::Object>*>( |
| 156 | reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size + sizeof(ArtMethod*)); |
| 157 | return stack_ref->AsMirrorPtr(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 158 | } else { |
| 159 | return cur_shadow_frame_->GetVRegReference(0); |
| 160 | } |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 161 | } else if (m->IsProxyMethod()) { |
Sebastien Hertz | a836bc9 | 2014-11-25 16:30:53 +0100 | [diff] [blame] | 162 | if (cur_quick_frame_ != nullptr) { |
| 163 | return artQuickGetProxyThisObject(cur_quick_frame_); |
| 164 | } else { |
| 165 | return cur_shadow_frame_->GetVRegReference(0); |
| 166 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 167 | } else { |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 168 | CodeItemDataAccessor accessor(m->DexInstructionData()); |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 169 | if (!accessor.HasCodeItem()) { |
Ian Rogers | e0dcd46 | 2014-03-08 15:21:04 -0800 | [diff] [blame] | 170 | UNIMPLEMENTED(ERROR) << "Failed to determine this object of abstract or proxy method: " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 171 | << ArtMethod::PrettyMethod(m); |
Ian Rogers | e0dcd46 | 2014-03-08 15:21:04 -0800 | [diff] [blame] | 172 | return nullptr; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 173 | } else { |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 174 | uint16_t reg = accessor.RegistersSize() - accessor.InsSize(); |
Nicolas Geoffray | 15b9d52 | 2015-03-12 15:05:13 +0000 | [diff] [blame] | 175 | uint32_t value = 0; |
Nicolas Geoffray | 4cbfadc | 2018-10-10 16:09:43 +0100 | [diff] [blame] | 176 | if (!GetVReg(m, reg, kReferenceVReg, &value)) { |
| 177 | return nullptr; |
| 178 | } |
Nicolas Geoffray | 15b9d52 | 2015-03-12 15:05:13 +0000 | [diff] [blame] | 179 | return reinterpret_cast<mirror::Object*>(value); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 184 | size_t StackVisitor::GetNativePcOffset() const { |
| 185 | DCHECK(!IsShadowFrame()); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 186 | return GetCurrentOatQuickMethodHeader()->NativeQuickPcOffset(cur_quick_frame_pc_); |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 189 | bool StackVisitor::GetVRegFromDebuggerShadowFrame(uint16_t vreg, |
| 190 | VRegKind kind, |
| 191 | uint32_t* val) const { |
| 192 | size_t frame_id = const_cast<StackVisitor*>(this)->GetFrameId(); |
| 193 | ShadowFrame* shadow_frame = thread_->FindDebuggerShadowFrame(frame_id); |
| 194 | if (shadow_frame != nullptr) { |
| 195 | bool* updated_vreg_flags = thread_->GetUpdatedVRegFlags(frame_id); |
| 196 | DCHECK(updated_vreg_flags != nullptr); |
| 197 | if (updated_vreg_flags[vreg]) { |
| 198 | // Value is set by the debugger. |
| 199 | if (kind == kReferenceVReg) { |
| 200 | *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>( |
| 201 | shadow_frame->GetVRegReference(vreg))); |
| 202 | } else { |
| 203 | *val = shadow_frame->GetVReg(vreg); |
| 204 | } |
| 205 | return true; |
| 206 | } |
| 207 | } |
| 208 | // No value is set by the debugger. |
| 209 | return false; |
| 210 | } |
| 211 | |
David Srbecky | cffa254 | 2019-07-01 15:31:41 +0100 | [diff] [blame] | 212 | bool StackVisitor::GetVReg(ArtMethod* m, |
| 213 | uint16_t vreg, |
| 214 | VRegKind kind, |
| 215 | uint32_t* val, |
| 216 | std::optional<DexRegisterLocation> location) const { |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 217 | if (cur_quick_frame_ != nullptr) { |
| 218 | DCHECK(context_ != nullptr); // You can't reliably read registers without a context. |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 219 | DCHECK(m == GetMethod()); |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 220 | // Check if there is value set by the debugger. |
| 221 | if (GetVRegFromDebuggerShadowFrame(vreg, kind, val)) { |
| 222 | return true; |
| 223 | } |
Nicolas Geoffray | d7651b1 | 2019-12-18 14:57:30 +0000 | [diff] [blame] | 224 | bool result = false; |
Nicolas Geoffray | 013d1ee | 2019-12-04 16:18:15 +0000 | [diff] [blame] | 225 | if (cur_oat_quick_method_header_->IsNterpMethodHeader()) { |
Nicolas Geoffray | d7651b1 | 2019-12-18 14:57:30 +0000 | [diff] [blame] | 226 | result = true; |
| 227 | *val = (kind == kReferenceVReg) |
| 228 | ? NterpGetVRegReference(cur_quick_frame_, vreg) |
| 229 | : NterpGetVReg(cur_quick_frame_, vreg); |
| 230 | } else { |
| 231 | DCHECK(cur_oat_quick_method_header_->IsOptimized()); |
| 232 | if (location.has_value() && kind != kReferenceVReg) { |
| 233 | uint32_t val2 = *val; |
| 234 | // The caller already known the register location, so we can use the faster overload |
| 235 | // which does not decode the stack maps. |
Nicolas Geoffray | 6624d58 | 2020-09-01 15:02:00 +0100 | [diff] [blame] | 236 | result = GetVRegFromOptimizedCode(location.value(), val); |
Nicolas Geoffray | d7651b1 | 2019-12-18 14:57:30 +0000 | [diff] [blame] | 237 | // Compare to the slower overload. |
| 238 | DCHECK_EQ(result, GetVRegFromOptimizedCode(m, vreg, kind, &val2)); |
| 239 | DCHECK_EQ(*val, val2); |
| 240 | } else { |
| 241 | result = GetVRegFromOptimizedCode(m, vreg, kind, val); |
| 242 | } |
Nicolas Geoffray | 013d1ee | 2019-12-04 16:18:15 +0000 | [diff] [blame] | 243 | } |
Alex Light | b096c91 | 2019-09-25 13:33:06 -0700 | [diff] [blame] | 244 | if (kind == kReferenceVReg) { |
| 245 | // Perform a read barrier in case we are in a different thread and GC is ongoing. |
| 246 | mirror::Object* out = reinterpret_cast<mirror::Object*>(static_cast<uintptr_t>(*val)); |
| 247 | uintptr_t ptr_out = reinterpret_cast<uintptr_t>(GcRoot<mirror::Object>(out).Read()); |
| 248 | DCHECK_LT(ptr_out, std::numeric_limits<uint32_t>::max()); |
| 249 | *val = static_cast<uint32_t>(ptr_out); |
| 250 | } |
Nicolas Geoffray | d7651b1 | 2019-12-18 14:57:30 +0000 | [diff] [blame] | 251 | return result; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 252 | } else { |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 253 | DCHECK(cur_shadow_frame_ != nullptr); |
Sebastien Hertz | 0968744 | 2015-11-17 10:35:39 +0100 | [diff] [blame] | 254 | if (kind == kReferenceVReg) { |
| 255 | *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>( |
| 256 | cur_shadow_frame_->GetVRegReference(vreg))); |
| 257 | } else { |
| 258 | *val = cur_shadow_frame_->GetVReg(vreg); |
| 259 | } |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 260 | return true; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
Nicolas Geoffray | 6624d58 | 2020-09-01 15:02:00 +0100 | [diff] [blame] | 264 | bool StackVisitor::GetVRegFromOptimizedCode(ArtMethod* m, |
| 265 | uint16_t vreg, |
| 266 | VRegKind kind, |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 267 | uint32_t* val) const { |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 268 | DCHECK_EQ(m, GetMethod()); |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 269 | // Can't be null or how would we compile its instructions? |
| 270 | DCHECK(m->GetCodeItem() != nullptr) << m->PrettyMethod(); |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 271 | CodeItemDataAccessor accessor(m->DexInstructionData()); |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 272 | uint16_t number_of_dex_registers = accessor.RegistersSize(); |
| 273 | DCHECK_LT(vreg, number_of_dex_registers); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 274 | const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader(); |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 275 | CodeInfo code_info(method_header); |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 276 | |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 277 | uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_); |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 278 | StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset); |
Nicolas Geoffray | e12997f | 2015-05-22 14:01:33 +0100 | [diff] [blame] | 279 | DCHECK(stack_map.IsValid()); |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 280 | |
| 281 | DexRegisterMap dex_register_map = IsInInlinedFrame() |
David Srbecky | 93bd361 | 2018-07-02 19:30:18 +0100 | [diff] [blame] | 282 | ? code_info.GetInlineDexRegisterMapOf(stack_map, current_inline_frames_.back()) |
David Srbecky | fd89b07 | 2018-06-03 12:00:22 +0100 | [diff] [blame] | 283 | : code_info.GetDexRegisterMapOf(stack_map); |
| 284 | if (dex_register_map.empty()) { |
Nicolas Geoffray | 012fc4e | 2016-01-08 15:58:19 +0000 | [diff] [blame] | 285 | return false; |
| 286 | } |
David Srbecky | fd89b07 | 2018-06-03 12:00:22 +0100 | [diff] [blame] | 287 | DCHECK_EQ(dex_register_map.size(), number_of_dex_registers); |
David Srbecky | e140212 | 2018-06-13 18:20:45 +0100 | [diff] [blame] | 288 | DexRegisterLocation::Kind location_kind = dex_register_map[vreg].GetKind(); |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 289 | switch (location_kind) { |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 290 | case DexRegisterLocation::Kind::kInStack: { |
David Srbecky | e140212 | 2018-06-13 18:20:45 +0100 | [diff] [blame] | 291 | const int32_t offset = dex_register_map[vreg].GetStackOffsetInBytes(); |
Nicolas Geoffray | 4cbfadc | 2018-10-10 16:09:43 +0100 | [diff] [blame] | 292 | BitMemoryRegion stack_mask = code_info.GetStackMaskOf(stack_map); |
| 293 | if (kind == kReferenceVReg && !stack_mask.LoadBit(offset / kFrameSlotSize)) { |
| 294 | return false; |
| 295 | } |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 296 | const uint8_t* addr = reinterpret_cast<const uint8_t*>(cur_quick_frame_) + offset; |
| 297 | *val = *reinterpret_cast<const uint32_t*>(addr); |
| 298 | return true; |
| 299 | } |
Nicolas Geoffray | 4cbfadc | 2018-10-10 16:09:43 +0100 | [diff] [blame] | 300 | case DexRegisterLocation::Kind::kInRegister: { |
| 301 | uint32_t register_mask = code_info.GetRegisterMaskOf(stack_map); |
| 302 | uint32_t reg = dex_register_map[vreg].GetMachineRegister(); |
| 303 | if (kind == kReferenceVReg && !(register_mask & (1 << reg))) { |
| 304 | return false; |
| 305 | } |
Nicolas Geoffray | 6624d58 | 2020-09-01 15:02:00 +0100 | [diff] [blame] | 306 | return GetRegisterIfAccessible(reg, location_kind, val); |
Nicolas Geoffray | 4cbfadc | 2018-10-10 16:09:43 +0100 | [diff] [blame] | 307 | } |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 308 | case DexRegisterLocation::Kind::kInRegisterHigh: |
| 309 | case DexRegisterLocation::Kind::kInFpuRegister: |
| 310 | case DexRegisterLocation::Kind::kInFpuRegisterHigh: { |
Nicolas Geoffray | 4cbfadc | 2018-10-10 16:09:43 +0100 | [diff] [blame] | 311 | if (kind == kReferenceVReg) { |
| 312 | return false; |
| 313 | } |
David Srbecky | e140212 | 2018-06-13 18:20:45 +0100 | [diff] [blame] | 314 | uint32_t reg = dex_register_map[vreg].GetMachineRegister(); |
Nicolas Geoffray | 6624d58 | 2020-09-01 15:02:00 +0100 | [diff] [blame] | 315 | return GetRegisterIfAccessible(reg, location_kind, val); |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 316 | } |
Nicolas Geoffray | 4cbfadc | 2018-10-10 16:09:43 +0100 | [diff] [blame] | 317 | case DexRegisterLocation::Kind::kConstant: { |
| 318 | uint32_t result = dex_register_map[vreg].GetConstant(); |
| 319 | if (kind == kReferenceVReg && result != 0) { |
| 320 | return false; |
| 321 | } |
| 322 | *val = result; |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 323 | return true; |
Nicolas Geoffray | 4cbfadc | 2018-10-10 16:09:43 +0100 | [diff] [blame] | 324 | } |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 325 | case DexRegisterLocation::Kind::kNone: |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 326 | return false; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 327 | default: |
David Srbecky | e140212 | 2018-06-13 18:20:45 +0100 | [diff] [blame] | 328 | LOG(FATAL) << "Unexpected location kind " << dex_register_map[vreg].GetKind(); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 329 | UNREACHABLE(); |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 330 | } |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 331 | } |
| 332 | |
Nicolas Geoffray | 6624d58 | 2020-09-01 15:02:00 +0100 | [diff] [blame] | 333 | bool StackVisitor::GetVRegFromOptimizedCode(DexRegisterLocation location, uint32_t* val) const { |
David Srbecky | cffa254 | 2019-07-01 15:31:41 +0100 | [diff] [blame] | 334 | switch (location.GetKind()) { |
| 335 | case DexRegisterLocation::Kind::kInvalid: |
| 336 | break; |
| 337 | case DexRegisterLocation::Kind::kInStack: { |
| 338 | const uint8_t* sp = reinterpret_cast<const uint8_t*>(cur_quick_frame_); |
| 339 | *val = *reinterpret_cast<const uint32_t*>(sp + location.GetStackOffsetInBytes()); |
| 340 | return true; |
| 341 | } |
| 342 | case DexRegisterLocation::Kind::kInRegister: |
| 343 | case DexRegisterLocation::Kind::kInRegisterHigh: |
| 344 | case DexRegisterLocation::Kind::kInFpuRegister: |
| 345 | case DexRegisterLocation::Kind::kInFpuRegisterHigh: |
Nicolas Geoffray | 6624d58 | 2020-09-01 15:02:00 +0100 | [diff] [blame] | 346 | return GetRegisterIfAccessible(location.GetMachineRegister(), location.GetKind(), val); |
David Srbecky | cffa254 | 2019-07-01 15:31:41 +0100 | [diff] [blame] | 347 | case DexRegisterLocation::Kind::kConstant: |
| 348 | *val = location.GetConstant(); |
| 349 | return true; |
| 350 | case DexRegisterLocation::Kind::kNone: |
| 351 | return false; |
| 352 | } |
| 353 | LOG(FATAL) << "Unexpected location kind " << location.GetKind(); |
| 354 | UNREACHABLE(); |
| 355 | } |
| 356 | |
Nicolas Geoffray | 6624d58 | 2020-09-01 15:02:00 +0100 | [diff] [blame] | 357 | bool StackVisitor::GetRegisterIfAccessible(uint32_t reg, |
| 358 | DexRegisterLocation::Kind location_kind, |
| 359 | uint32_t* val) const { |
| 360 | const bool is_float = (location_kind == DexRegisterLocation::Kind::kInFpuRegister) || |
| 361 | (location_kind == DexRegisterLocation::Kind::kInFpuRegisterHigh); |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 362 | |
Vladimir Marko | 239d6ea | 2016-09-05 10:44:04 +0100 | [diff] [blame] | 363 | if (kRuntimeISA == InstructionSet::kX86 && is_float) { |
| 364 | // X86 float registers are 64-bit and each XMM register is provided as two separate |
| 365 | // 32-bit registers by the context. |
Nicolas Geoffray | 6624d58 | 2020-09-01 15:02:00 +0100 | [diff] [blame] | 366 | reg = (location_kind == DexRegisterLocation::Kind::kInFpuRegisterHigh) |
| 367 | ? (2 * reg + 1) |
| 368 | : (2 * reg); |
Vladimir Marko | 239d6ea | 2016-09-05 10:44:04 +0100 | [diff] [blame] | 369 | } |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 370 | |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 371 | if (!IsAccessibleRegister(reg, is_float)) { |
| 372 | return false; |
| 373 | } |
| 374 | uintptr_t ptr_val = GetRegister(reg, is_float); |
| 375 | const bool target64 = Is64BitInstructionSet(kRuntimeISA); |
| 376 | if (target64) { |
Nicolas Geoffray | 6624d58 | 2020-09-01 15:02:00 +0100 | [diff] [blame] | 377 | const bool is_high = (location_kind == DexRegisterLocation::Kind::kInRegisterHigh) || |
| 378 | (location_kind == DexRegisterLocation::Kind::kInFpuRegisterHigh); |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 379 | int64_t value_long = static_cast<int64_t>(ptr_val); |
Nicolas Geoffray | 6624d58 | 2020-09-01 15:02:00 +0100 | [diff] [blame] | 380 | ptr_val = static_cast<uintptr_t>(is_high ? High32Bits(value_long) : Low32Bits(value_long)); |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 381 | } |
| 382 | *val = ptr_val; |
| 383 | return true; |
| 384 | } |
| 385 | |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 386 | bool StackVisitor::GetVRegPairFromDebuggerShadowFrame(uint16_t vreg, |
| 387 | VRegKind kind_lo, |
| 388 | VRegKind kind_hi, |
| 389 | uint64_t* val) const { |
| 390 | uint32_t low_32bits; |
| 391 | uint32_t high_32bits; |
| 392 | bool success = GetVRegFromDebuggerShadowFrame(vreg, kind_lo, &low_32bits); |
| 393 | success &= GetVRegFromDebuggerShadowFrame(vreg + 1, kind_hi, &high_32bits); |
| 394 | if (success) { |
| 395 | *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits); |
| 396 | } |
| 397 | return success; |
| 398 | } |
| 399 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 400 | bool StackVisitor::GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo, |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 401 | VRegKind kind_hi, uint64_t* val) const { |
| 402 | if (kind_lo == kLongLoVReg) { |
| 403 | DCHECK_EQ(kind_hi, kLongHiVReg); |
| 404 | } else if (kind_lo == kDoubleLoVReg) { |
| 405 | DCHECK_EQ(kind_hi, kDoubleHiVReg); |
| 406 | } else { |
| 407 | LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi; |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 408 | UNREACHABLE(); |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 409 | } |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 410 | // Check if there is value set by the debugger. |
| 411 | if (GetVRegPairFromDebuggerShadowFrame(vreg, kind_lo, kind_hi, val)) { |
| 412 | return true; |
| 413 | } |
Nicolas Geoffray | caafd62 | 2020-01-27 13:08:45 +0000 | [diff] [blame] | 414 | if (cur_quick_frame_ == nullptr) { |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 415 | DCHECK(cur_shadow_frame_ != nullptr); |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 416 | *val = cur_shadow_frame_->GetVRegLong(vreg); |
| 417 | return true; |
| 418 | } |
Nicolas Geoffray | caafd62 | 2020-01-27 13:08:45 +0000 | [diff] [blame] | 419 | if (cur_oat_quick_method_header_->IsNterpMethodHeader()) { |
| 420 | uint64_t val_lo = NterpGetVReg(cur_quick_frame_, vreg); |
| 421 | uint64_t val_hi = NterpGetVReg(cur_quick_frame_, vreg + 1); |
| 422 | *val = (val_hi << 32) + val_lo; |
| 423 | return true; |
| 424 | } |
| 425 | |
| 426 | DCHECK(context_ != nullptr); // You can't reliably read registers without a context. |
| 427 | DCHECK(m == GetMethod()); |
| 428 | DCHECK(cur_oat_quick_method_header_->IsOptimized()); |
| 429 | return GetVRegPairFromOptimizedCode(m, vreg, kind_lo, kind_hi, val); |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 430 | } |
| 431 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 432 | bool StackVisitor::GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg, |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 433 | VRegKind kind_lo, VRegKind kind_hi, |
| 434 | uint64_t* val) const { |
| 435 | uint32_t low_32bits; |
| 436 | uint32_t high_32bits; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 437 | bool success = GetVRegFromOptimizedCode(m, vreg, kind_lo, &low_32bits); |
| 438 | success &= GetVRegFromOptimizedCode(m, vreg + 1, kind_hi, &high_32bits); |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 439 | if (success) { |
| 440 | *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits); |
| 441 | } |
| 442 | return success; |
| 443 | } |
| 444 | |
Vladimir Marko | 439d126 | 2019-04-12 14:45:07 +0100 | [diff] [blame] | 445 | ShadowFrame* StackVisitor::PrepareSetVReg(ArtMethod* m, uint16_t vreg, bool wide) { |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 446 | CodeItemDataAccessor accessor(m->DexInstructionData()); |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 447 | if (!accessor.HasCodeItem()) { |
Vladimir Marko | 439d126 | 2019-04-12 14:45:07 +0100 | [diff] [blame] | 448 | return nullptr; |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 449 | } |
| 450 | ShadowFrame* shadow_frame = GetCurrentShadowFrame(); |
| 451 | if (shadow_frame == nullptr) { |
| 452 | // This is a compiled frame: we must prepare and update a shadow frame that will |
| 453 | // be executed by the interpreter after deoptimization of the stack. |
| 454 | const size_t frame_id = GetFrameId(); |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 455 | const uint16_t num_regs = accessor.RegistersSize(); |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 456 | shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc()); |
| 457 | CHECK(shadow_frame != nullptr); |
Vladimir Marko | 439d126 | 2019-04-12 14:45:07 +0100 | [diff] [blame] | 458 | // Remember the vreg(s) has been set for debugging and must not be overwritten by the |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 459 | // original value during deoptimization of the stack. |
| 460 | thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true; |
Vladimir Marko | 439d126 | 2019-04-12 14:45:07 +0100 | [diff] [blame] | 461 | if (wide) { |
| 462 | thread_->GetUpdatedVRegFlags(frame_id)[vreg + 1] = true; |
| 463 | } |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 464 | } |
Vladimir Marko | 439d126 | 2019-04-12 14:45:07 +0100 | [diff] [blame] | 465 | return shadow_frame; |
| 466 | } |
| 467 | |
| 468 | bool StackVisitor::SetVReg(ArtMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind) { |
| 469 | DCHECK(kind == kIntVReg || kind == kFloatVReg); |
| 470 | ShadowFrame* shadow_frame = PrepareSetVReg(m, vreg, /* wide= */ false); |
| 471 | if (shadow_frame == nullptr) { |
| 472 | return false; |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 473 | } |
Vladimir Marko | 439d126 | 2019-04-12 14:45:07 +0100 | [diff] [blame] | 474 | shadow_frame->SetVReg(vreg, new_value); |
| 475 | return true; |
| 476 | } |
| 477 | |
| 478 | bool StackVisitor::SetVRegReference(ArtMethod* m, uint16_t vreg, ObjPtr<mirror::Object> new_value) { |
| 479 | ShadowFrame* shadow_frame = PrepareSetVReg(m, vreg, /* wide= */ false); |
| 480 | if (shadow_frame == nullptr) { |
| 481 | return false; |
| 482 | } |
| 483 | shadow_frame->SetVRegReference(vreg, new_value); |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 484 | return true; |
| 485 | } |
| 486 | |
Mingyao Yang | 636b925 | 2015-07-31 16:40:24 -0700 | [diff] [blame] | 487 | bool StackVisitor::SetVRegPair(ArtMethod* m, |
| 488 | uint16_t vreg, |
| 489 | uint64_t new_value, |
| 490 | VRegKind kind_lo, |
| 491 | VRegKind kind_hi) { |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 492 | if (kind_lo == kLongLoVReg) { |
| 493 | DCHECK_EQ(kind_hi, kLongHiVReg); |
| 494 | } else if (kind_lo == kDoubleLoVReg) { |
| 495 | DCHECK_EQ(kind_hi, kDoubleHiVReg); |
| 496 | } else { |
| 497 | LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi; |
| 498 | UNREACHABLE(); |
| 499 | } |
Vladimir Marko | 439d126 | 2019-04-12 14:45:07 +0100 | [diff] [blame] | 500 | ShadowFrame* shadow_frame = PrepareSetVReg(m, vreg, /* wide= */ true); |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 501 | if (shadow_frame == nullptr) { |
Vladimir Marko | 439d126 | 2019-04-12 14:45:07 +0100 | [diff] [blame] | 502 | return false; |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 503 | } |
| 504 | shadow_frame->SetVRegLong(vreg, new_value); |
| 505 | return true; |
| 506 | } |
| 507 | |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 508 | bool StackVisitor::IsAccessibleGPR(uint32_t reg) const { |
| 509 | DCHECK(context_ != nullptr); |
| 510 | return context_->IsAccessibleGPR(reg); |
| 511 | } |
| 512 | |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 513 | uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const { |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 514 | DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine"; |
| 515 | DCHECK(context_ != nullptr); |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 516 | return context_->GetGPRAddress(reg); |
| 517 | } |
| 518 | |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 519 | uintptr_t StackVisitor::GetGPR(uint32_t reg) const { |
| 520 | DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine"; |
| 521 | DCHECK(context_ != nullptr); |
| 522 | return context_->GetGPR(reg); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 525 | bool StackVisitor::IsAccessibleFPR(uint32_t reg) const { |
| 526 | DCHECK(context_ != nullptr); |
| 527 | return context_->IsAccessibleFPR(reg); |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 528 | } |
| 529 | |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 530 | uintptr_t StackVisitor::GetFPR(uint32_t reg) const { |
| 531 | DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine"; |
| 532 | DCHECK(context_ != nullptr); |
| 533 | return context_->GetFPR(reg); |
| 534 | } |
| 535 | |
Nicolas Geoffray | e91e795 | 2020-01-23 10:15:56 +0000 | [diff] [blame] | 536 | uintptr_t StackVisitor::GetReturnPcAddr() const { |
| 537 | uintptr_t sp = reinterpret_cast<uintptr_t>(GetCurrentQuickFrame()); |
| 538 | DCHECK_NE(sp, 0u); |
| 539 | return sp + GetCurrentQuickFrameInfo().GetReturnPcOffset(); |
| 540 | } |
| 541 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 542 | uintptr_t StackVisitor::GetReturnPc() const { |
Nicolas Geoffray | e91e795 | 2020-01-23 10:15:56 +0000 | [diff] [blame] | 543 | return *reinterpret_cast<uintptr_t*>(GetReturnPcAddr()); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) { |
Nicolas Geoffray | e91e795 | 2020-01-23 10:15:56 +0000 | [diff] [blame] | 547 | *reinterpret_cast<uintptr_t*>(GetReturnPcAddr()) = new_ret_pc; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 550 | size_t StackVisitor::ComputeNumFrames(Thread* thread, StackWalkKind walk_kind) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 551 | struct NumFramesVisitor : public StackVisitor { |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 552 | NumFramesVisitor(Thread* thread_in, StackWalkKind walk_kind_in) |
| 553 | : StackVisitor(thread_in, nullptr, walk_kind_in), frames(0) {} |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 554 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 555 | bool VisitFrame() override { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 556 | frames++; |
| 557 | return true; |
| 558 | } |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 559 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 560 | size_t frames; |
| 561 | }; |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 562 | NumFramesVisitor visitor(thread, walk_kind); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 563 | visitor.WalkStack(true); |
| 564 | return visitor.frames; |
| 565 | } |
| 566 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 567 | bool StackVisitor::GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc) { |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 568 | struct HasMoreFramesVisitor : public StackVisitor { |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 569 | HasMoreFramesVisitor(Thread* thread, |
| 570 | StackWalkKind walk_kind, |
| 571 | size_t num_frames, |
| 572 | size_t frame_height) |
| 573 | : StackVisitor(thread, nullptr, walk_kind, num_frames), |
| 574 | frame_height_(frame_height), |
| 575 | found_frame_(false), |
| 576 | has_more_frames_(false), |
| 577 | next_method_(nullptr), |
| 578 | next_dex_pc_(0) { |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 581 | bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 582 | if (found_frame_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 583 | ArtMethod* method = GetMethod(); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 584 | if (method != nullptr && !method->IsRuntimeMethod()) { |
| 585 | has_more_frames_ = true; |
| 586 | next_method_ = method; |
| 587 | next_dex_pc_ = GetDexPc(); |
| 588 | return false; // End stack walk once next method is found. |
| 589 | } |
| 590 | } else if (GetFrameHeight() == frame_height_) { |
| 591 | found_frame_ = true; |
| 592 | } |
| 593 | return true; |
| 594 | } |
| 595 | |
| 596 | size_t frame_height_; |
| 597 | bool found_frame_; |
| 598 | bool has_more_frames_; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 599 | ArtMethod* next_method_; |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 600 | uint32_t next_dex_pc_; |
| 601 | }; |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 602 | HasMoreFramesVisitor visitor(thread_, walk_kind_, GetNumFrames(), GetFrameHeight()); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 603 | visitor.WalkStack(true); |
| 604 | *next_method = visitor.next_method_; |
| 605 | *next_dex_pc = visitor.next_dex_pc_; |
| 606 | return visitor.has_more_frames_; |
| 607 | } |
| 608 | |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 609 | void StackVisitor::DescribeStack(Thread* thread) { |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 610 | struct DescribeStackVisitor : public StackVisitor { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 611 | explicit DescribeStackVisitor(Thread* thread_in) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 612 | : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {} |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 613 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 614 | bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 615 | LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation(); |
| 616 | return true; |
| 617 | } |
| 618 | }; |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 619 | DescribeStackVisitor visitor(thread); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 620 | visitor.WalkStack(true); |
| 621 | } |
| 622 | |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame] | 623 | std::string StackVisitor::DescribeLocation() const { |
| 624 | std::string result("Visiting method '"); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 625 | ArtMethod* m = GetMethod(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 626 | if (m == nullptr) { |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 627 | return "upcall"; |
| 628 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 629 | result += m->PrettyMethod(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 630 | result += StringPrintf("' at dex PC 0x%04x", GetDexPc()); |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame] | 631 | if (!IsShadowFrame()) { |
| 632 | result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc())); |
| 633 | } |
| 634 | return result; |
| 635 | } |
| 636 | |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 637 | void StackVisitor::SetMethod(ArtMethod* method) { |
| 638 | DCHECK(GetMethod() != nullptr); |
| 639 | if (cur_shadow_frame_ != nullptr) { |
| 640 | cur_shadow_frame_->SetMethod(method); |
| 641 | } else { |
| 642 | DCHECK(cur_quick_frame_ != nullptr); |
Nicolas Geoffray | 226805d | 2018-12-14 10:59:02 +0000 | [diff] [blame] | 643 | CHECK(!IsInInlinedFrame()) << "We do not support setting inlined method's ArtMethod: " |
| 644 | << GetMethod()->PrettyMethod() << " is inlined into " |
| 645 | << GetOuterMethod()->PrettyMethod(); |
Alex Light | 1ebe4fe | 2017-01-30 14:57:11 -0800 | [diff] [blame] | 646 | *cur_quick_frame_ = method; |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 650 | static void AssertPcIsWithinQuickCode(ArtMethod* method, uintptr_t pc) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 651 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 652 | if (method->IsNative() || method->IsRuntimeMethod() || method->IsProxyMethod()) { |
| 653 | return; |
| 654 | } |
| 655 | |
| 656 | if (pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) { |
| 657 | return; |
| 658 | } |
| 659 | |
Mingyao Yang | 88ca8ba | 2017-05-23 14:21:07 -0700 | [diff] [blame] | 660 | Runtime* runtime = Runtime::Current(); |
| 661 | if (runtime->UseJitCompilation() && |
| 662 | runtime->GetJit()->GetCodeCache()->ContainsPc(reinterpret_cast<const void*>(pc))) { |
| 663 | return; |
| 664 | } |
| 665 | |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 666 | const void* code = method->GetEntryPointFromQuickCompiledCode(); |
Alex Light | db01a09 | 2017-04-03 15:39:55 -0700 | [diff] [blame] | 667 | if (code == GetQuickInstrumentationEntryPoint() || code == GetInvokeObsoleteMethodStub()) { |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 668 | return; |
| 669 | } |
| 670 | |
| 671 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 672 | if (class_linker->IsQuickToInterpreterBridge(code) || |
| 673 | class_linker->IsQuickResolutionStub(code)) { |
| 674 | return; |
| 675 | } |
| 676 | |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 677 | if (runtime->UseJitCompilation() && runtime->GetJit()->GetCodeCache()->ContainsPc(code)) { |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 678 | return; |
| 679 | } |
| 680 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 681 | uint32_t code_size = OatQuickMethodHeader::FromEntryPoint(code)->GetCodeSize(); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 682 | uintptr_t code_start = reinterpret_cast<uintptr_t>(code); |
| 683 | CHECK(code_start <= pc && pc <= (code_start + code_size)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 684 | << method->PrettyMethod() |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 685 | << " pc=" << std::hex << pc |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 686 | << " code_start=" << code_start |
| 687 | << " code_size=" << code_size; |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 688 | } |
| 689 | |
Orion Hodson | 6aaa49d | 2020-07-28 15:53:04 +0100 | [diff] [blame] | 690 | void StackVisitor::ValidateFrame() const { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 691 | if (kIsDebugBuild) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 692 | ArtMethod* method = GetMethod(); |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 693 | ObjPtr<mirror::Class> declaring_class = method->GetDeclaringClass(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 694 | // Runtime methods have null declaring class. |
| 695 | if (!method->IsRuntimeMethod()) { |
| 696 | CHECK(declaring_class != nullptr); |
| 697 | CHECK_EQ(declaring_class->GetClass(), declaring_class->GetClass()->GetClass()) |
| 698 | << declaring_class; |
| 699 | } else { |
| 700 | CHECK(declaring_class == nullptr); |
| 701 | } |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 702 | Runtime* const runtime = Runtime::Current(); |
| 703 | LinearAlloc* const linear_alloc = runtime->GetLinearAlloc(); |
| 704 | if (!linear_alloc->Contains(method)) { |
| 705 | // Check class linker linear allocs. |
Nicolas Geoffray | 25b9c7d | 2020-09-17 17:34:34 +0100 | [diff] [blame] | 706 | // We get the canonical method as copied methods may have been allocated |
| 707 | // by a different class loader. |
Ulya Trafimovich | 819b362 | 2019-12-12 17:59:10 +0000 | [diff] [blame] | 708 | const PointerSize ptrSize = runtime->GetClassLinker()->GetImagePointerSize(); |
| 709 | ArtMethod* canonical = method->GetCanonicalMethod(ptrSize); |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 710 | ObjPtr<mirror::Class> klass = canonical->GetDeclaringClass(); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 711 | LinearAlloc* const class_linear_alloc = (klass != nullptr) |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 712 | ? runtime->GetClassLinker()->GetAllocatorForClassLoader(klass->GetClassLoader()) |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 713 | : linear_alloc; |
Nicolas Geoffray | 48b40cc | 2017-08-07 16:52:40 +0100 | [diff] [blame] | 714 | if (!class_linear_alloc->Contains(canonical)) { |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 715 | // Check image space. |
| 716 | bool in_image = false; |
| 717 | for (auto& space : runtime->GetHeap()->GetContinuousSpaces()) { |
| 718 | if (space->IsImageSpace()) { |
| 719 | auto* image_space = space->AsImageSpace(); |
| 720 | const auto& header = image_space->GetImageHeader(); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 721 | const ImageSection& methods = header.GetMethodsSection(); |
| 722 | const ImageSection& runtime_methods = header.GetRuntimeMethodsSection(); |
Nicolas Geoffray | 48b40cc | 2017-08-07 16:52:40 +0100 | [diff] [blame] | 723 | const size_t offset = reinterpret_cast<const uint8_t*>(canonical) - image_space->Begin(); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 724 | if (methods.Contains(offset) || runtime_methods.Contains(offset)) { |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 725 | in_image = true; |
| 726 | break; |
| 727 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 728 | } |
| 729 | } |
Nicolas Geoffray | 48b40cc | 2017-08-07 16:52:40 +0100 | [diff] [blame] | 730 | CHECK(in_image) << canonical->PrettyMethod() << " not in linear alloc or image"; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 731 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 732 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 733 | if (cur_quick_frame_ != nullptr) { |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 734 | AssertPcIsWithinQuickCode(method, cur_quick_frame_pc_); |
David Srbecky | 346fd96 | 2020-07-27 16:51:00 +0100 | [diff] [blame] | 735 | // Frame consistency checks. |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 736 | size_t frame_size = GetCurrentQuickFrameInfo().FrameSizeInBytes(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 737 | CHECK_NE(frame_size, 0u); |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 738 | // For compiled code, we could try to have a rough guess at an upper size we expect |
| 739 | // to see for a frame: |
Andreas Gampe | 5b417b9 | 2014-03-10 14:18:35 -0700 | [diff] [blame] | 740 | // 256 registers |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 741 | // 2 words HandleScope overhead |
Andreas Gampe | 5b417b9 | 2014-03-10 14:18:35 -0700 | [diff] [blame] | 742 | // 3+3 register spills |
Brian Carlstrom | ed08bd4 | 2014-03-19 18:34:17 -0700 | [diff] [blame] | 743 | // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word); |
Nicolas Geoffray | 0315efa | 2020-06-26 11:42:39 +0100 | [diff] [blame] | 744 | const size_t kMaxExpectedFrameSize = interpreter::kNterpMaxFrame; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 745 | CHECK_LE(frame_size, kMaxExpectedFrameSize) << method->PrettyMethod(); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 746 | size_t return_pc_offset = GetCurrentQuickFrameInfo().GetReturnPcOffset(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 747 | CHECK_LT(return_pc_offset, frame_size); |
| 748 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 749 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 750 | } |
| 751 | |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 752 | QuickMethodFrameInfo StackVisitor::GetCurrentQuickFrameInfo() const { |
| 753 | if (cur_oat_quick_method_header_ != nullptr) { |
Nicolas Geoffray | 013d1ee | 2019-12-04 16:18:15 +0000 | [diff] [blame] | 754 | if (cur_oat_quick_method_header_->IsOptimized()) { |
| 755 | return cur_oat_quick_method_header_->GetFrameInfo(); |
| 756 | } else { |
| 757 | DCHECK(cur_oat_quick_method_header_->IsNterpMethodHeader()); |
| 758 | return NterpFrameInfo(cur_quick_frame_); |
| 759 | } |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | ArtMethod* method = GetMethod(); |
| 763 | Runtime* runtime = Runtime::Current(); |
| 764 | |
| 765 | if (method->IsAbstract()) { |
Vladimir Marko | d3083dd | 2018-05-17 08:43:47 +0100 | [diff] [blame] | 766 | return RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | // This goes before IsProxyMethod since runtime methods have a null declaring class. |
| 770 | if (method->IsRuntimeMethod()) { |
| 771 | return runtime->GetRuntimeMethodFrameInfo(method); |
| 772 | } |
| 773 | |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 774 | if (method->IsProxyMethod()) { |
Nicolas Geoffray | 22cf3d3 | 2015-11-02 11:57:11 +0000 | [diff] [blame] | 775 | // There is only one direct method of a proxy class: the constructor. A direct method is |
| 776 | // cloned from the original java.lang.reflect.Proxy and is executed as usual quick |
| 777 | // compiled method without any stubs. Therefore the method must have a OatQuickMethodHeader. |
| 778 | DCHECK(!method->IsDirect() && !method->IsConstructor()) |
| 779 | << "Constructors of proxy classes must have a OatQuickMethodHeader"; |
Vladimir Marko | d3083dd | 2018-05-17 08:43:47 +0100 | [diff] [blame] | 780 | return RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 781 | } |
| 782 | |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 783 | // The only remaining cases are for native methods that either |
| 784 | // - use the Generic JNI stub, called either directly or through some |
| 785 | // (resolution, instrumentation) trampoline; or |
| 786 | // - fake a Generic JNI frame in art_jni_dlsym_lookup_critical_stub. |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 787 | DCHECK(method->IsNative()); |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 788 | if (kIsDebugBuild && !method->IsCriticalNative()) { |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 789 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 790 | const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(method, |
| 791 | kRuntimePointerSize); |
| 792 | CHECK(class_linker->IsQuickGenericJniStub(entry_point) || |
| 793 | // The current entrypoint (after filtering out trampolines) may have changed |
| 794 | // from GenericJNI to JIT-compiled stub since we have entered this frame. |
| 795 | (runtime->GetJit() != nullptr && |
| 796 | runtime->GetJit()->GetCodeCache()->ContainsPc(entry_point))) << method->PrettyMethod(); |
| 797 | } |
Vladimir Marko | 6e043bb | 2020-02-10 16:56:54 +0000 | [diff] [blame] | 798 | // Generic JNI frame is just like the SaveRefsAndArgs frame. |
| 799 | // Note that HandleScope, if any, is below the frame. |
| 800 | return RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 801 | } |
| 802 | |
Mythri Alle | 5097f83 | 2021-11-02 14:52:30 +0000 | [diff] [blame] | 803 | uint8_t* StackVisitor::GetShouldDeoptimizeFlagAddr() const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 804 | DCHECK(GetCurrentOatQuickMethodHeader()->HasShouldDeoptimizeFlag()); |
| 805 | QuickMethodFrameInfo frame_info = GetCurrentQuickFrameInfo(); |
| 806 | size_t frame_size = frame_info.FrameSizeInBytes(); |
| 807 | uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame()); |
| 808 | size_t core_spill_size = |
| 809 | POPCOUNT(frame_info.CoreSpillMask()) * GetBytesPerGprSpillLocation(kRuntimeISA); |
| 810 | size_t fpu_spill_size = |
| 811 | POPCOUNT(frame_info.FpSpillMask()) * GetBytesPerFprSpillLocation(kRuntimeISA); |
| 812 | size_t offset = frame_size - core_spill_size - fpu_spill_size - kShouldDeoptimizeFlagSize; |
| 813 | uint8_t* should_deoptimize_addr = sp + offset; |
| 814 | DCHECK_EQ(*should_deoptimize_addr & ~static_cast<uint8_t>(DeoptimizeFlagValue::kAll), 0); |
| 815 | return should_deoptimize_addr; |
| 816 | } |
| 817 | |
Andreas Gampe | 585da95 | 2016-12-02 14:52:29 -0800 | [diff] [blame] | 818 | template <StackVisitor::CountTransitions kCount> |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 819 | void StackVisitor::WalkStack(bool include_transitions) { |
Hiroshi Yamauchi | 02f365f | 2017-02-03 15:06:00 -0800 | [diff] [blame] | 820 | if (check_suspended_) { |
| 821 | DCHECK(thread_ == Thread::Current() || thread_->IsSuspended()); |
| 822 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 823 | CHECK_EQ(cur_depth_, 0U); |
Alex Light | e0c6d43 | 2020-01-22 22:04:20 +0000 | [diff] [blame] | 824 | size_t inlined_frames_count = 0; |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 825 | |
Alex Light | e0c6d43 | 2020-01-22 22:04:20 +0000 | [diff] [blame] | 826 | for (const ManagedStack* current_fragment = thread_->GetManagedStack(); |
| 827 | current_fragment != nullptr; current_fragment = current_fragment->GetLink()) { |
| 828 | cur_shadow_frame_ = current_fragment->GetTopShadowFrame(); |
| 829 | cur_quick_frame_ = current_fragment->GetTopQuickFrame(); |
| 830 | cur_quick_frame_pc_ = 0; |
Nicolas Geoffray | 51ad7fe | 2020-02-04 12:46:47 +0000 | [diff] [blame] | 831 | DCHECK(cur_oat_quick_method_header_ == nullptr); |
Alex Light | e0c6d43 | 2020-01-22 22:04:20 +0000 | [diff] [blame] | 832 | if (cur_quick_frame_ != nullptr) { // Handle quick stack frames. |
| 833 | // Can't be both a shadow and a quick fragment. |
| 834 | DCHECK(current_fragment->GetTopShadowFrame() == nullptr); |
| 835 | ArtMethod* method = *cur_quick_frame_; |
| 836 | DCHECK(method != nullptr); |
| 837 | bool header_retrieved = false; |
| 838 | if (method->IsNative()) { |
| 839 | // We do not have a PC for the first frame, so we cannot simply use |
| 840 | // ArtMethod::GetOatQuickMethodHeader() as we're unable to distinguish there |
| 841 | // between GenericJNI frame and JIT-compiled JNI stub; the entrypoint may have |
| 842 | // changed since the frame was entered. The top quick frame tag indicates |
| 843 | // GenericJNI here, otherwise it's either AOT-compiled or JNI-compiled JNI stub. |
| 844 | if (UNLIKELY(current_fragment->GetTopQuickFrameTag())) { |
| 845 | // The generic JNI does not have any method header. |
| 846 | cur_oat_quick_method_header_ = nullptr; |
| 847 | } else { |
| 848 | const void* existing_entry_point = method->GetEntryPointFromQuickCompiledCode(); |
| 849 | CHECK(existing_entry_point != nullptr); |
| 850 | Runtime* runtime = Runtime::Current(); |
| 851 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 852 | // Check whether we can quickly get the header from the current entrypoint. |
| 853 | if (!class_linker->IsQuickGenericJniStub(existing_entry_point) && |
| 854 | !class_linker->IsQuickResolutionStub(existing_entry_point) && |
| 855 | existing_entry_point != GetQuickInstrumentationEntryPoint()) { |
| 856 | cur_oat_quick_method_header_ = |
| 857 | OatQuickMethodHeader::FromEntryPoint(existing_entry_point); |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 858 | } else { |
Alex Light | e0c6d43 | 2020-01-22 22:04:20 +0000 | [diff] [blame] | 859 | const void* code = method->GetOatMethodQuickCode(class_linker->GetImagePointerSize()); |
| 860 | if (code != nullptr) { |
| 861 | cur_oat_quick_method_header_ = OatQuickMethodHeader::FromEntryPoint(code); |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 862 | } else { |
Alex Light | e0c6d43 | 2020-01-22 22:04:20 +0000 | [diff] [blame] | 863 | // This must be a JITted JNI stub frame. |
| 864 | CHECK(runtime->GetJit() != nullptr); |
| 865 | code = runtime->GetJit()->GetCodeCache()->GetJniStubCode(method); |
| 866 | CHECK(code != nullptr) << method->PrettyMethod(); |
| 867 | cur_oat_quick_method_header_ = OatQuickMethodHeader::FromCodePointer(code); |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 868 | } |
| 869 | } |
| 870 | } |
Alex Light | e0c6d43 | 2020-01-22 22:04:20 +0000 | [diff] [blame] | 871 | header_retrieved = true; |
jeffhao | 6641ea1 | 2013-01-02 18:13:42 -0800 | [diff] [blame] | 872 | } |
Alex Light | e0c6d43 | 2020-01-22 22:04:20 +0000 | [diff] [blame] | 873 | while (method != nullptr) { |
| 874 | if (!header_retrieved) { |
| 875 | cur_oat_quick_method_header_ = method->GetOatQuickMethodHeader(cur_quick_frame_pc_); |
| 876 | } |
| 877 | header_retrieved = false; // Force header retrieval in next iteration. |
Orion Hodson | 6aaa49d | 2020-07-28 15:53:04 +0100 | [diff] [blame] | 878 | ValidateFrame(); |
Alex Light | e0c6d43 | 2020-01-22 22:04:20 +0000 | [diff] [blame] | 879 | |
| 880 | if ((walk_kind_ == StackWalkKind::kIncludeInlinedFrames) |
| 881 | && (cur_oat_quick_method_header_ != nullptr) |
| 882 | && cur_oat_quick_method_header_->IsOptimized() |
| 883 | && !method->IsNative() // JNI methods cannot have any inlined frames. |
| 884 | && CodeInfo::HasInlineInfo(cur_oat_quick_method_header_->GetOptimizedCodeInfoPtr())) { |
| 885 | DCHECK_NE(cur_quick_frame_pc_, 0u); |
| 886 | CodeInfo* code_info = GetCurrentInlineInfo(); |
| 887 | StackMap* stack_map = GetCurrentStackMap(); |
| 888 | if (stack_map->IsValid() && stack_map->HasInlineInfo()) { |
| 889 | DCHECK_EQ(current_inline_frames_.size(), 0u); |
| 890 | for (current_inline_frames_ = code_info->GetInlineInfosOf(*stack_map); |
| 891 | !current_inline_frames_.empty(); |
| 892 | current_inline_frames_.pop_back()) { |
| 893 | bool should_continue = VisitFrame(); |
| 894 | if (UNLIKELY(!should_continue)) { |
| 895 | return; |
| 896 | } |
| 897 | cur_depth_++; |
| 898 | inlined_frames_count++; |
| 899 | } |
| 900 | } |
| 901 | } |
| 902 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 903 | bool should_continue = VisitFrame(); |
Alex Light | e0c6d43 | 2020-01-22 22:04:20 +0000 | [diff] [blame] | 904 | if (UNLIKELY(!should_continue)) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 905 | return; |
| 906 | } |
Alex Light | e0c6d43 | 2020-01-22 22:04:20 +0000 | [diff] [blame] | 907 | |
| 908 | QuickMethodFrameInfo frame_info = GetCurrentQuickFrameInfo(); |
| 909 | if (context_ != nullptr) { |
| 910 | context_->FillCalleeSaves(reinterpret_cast<uint8_t*>(cur_quick_frame_), frame_info); |
| 911 | } |
| 912 | // Compute PC for next stack frame from return PC. |
| 913 | size_t frame_size = frame_info.FrameSizeInBytes(); |
Nicolas Geoffray | e91e795 | 2020-01-23 10:15:56 +0000 | [diff] [blame] | 914 | uintptr_t return_pc_addr = GetReturnPcAddr(); |
Alex Light | e0c6d43 | 2020-01-22 22:04:20 +0000 | [diff] [blame] | 915 | uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr); |
| 916 | |
Nicolas Geoffray | e91e795 | 2020-01-23 10:15:56 +0000 | [diff] [blame] | 917 | if (UNLIKELY(reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc)) { |
Alex Light | e0c6d43 | 2020-01-22 22:04:20 +0000 | [diff] [blame] | 918 | // While profiling, the return pc is restored from the side stack, except when walking |
| 919 | // the stack for an exception where the side stack will be unwound in VisitFrame. |
Nicolas Geoffray | e91e795 | 2020-01-23 10:15:56 +0000 | [diff] [blame] | 920 | const std::map<uintptr_t, instrumentation::InstrumentationStackFrame>& |
| 921 | instrumentation_stack = *thread_->GetInstrumentationStack(); |
| 922 | auto it = instrumentation_stack.find(return_pc_addr); |
| 923 | CHECK(it != instrumentation_stack.end()); |
| 924 | const instrumentation::InstrumentationStackFrame& instrumentation_frame = it->second; |
| 925 | if (GetMethod() == |
| 926 | Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveAllCalleeSaves)) { |
| 927 | // Skip runtime save all callee frames which are used to deliver exceptions. |
| 928 | } else if (instrumentation_frame.interpreter_entry_) { |
| 929 | ArtMethod* callee = |
| 930 | Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs); |
| 931 | CHECK_EQ(GetMethod(), callee) << "Expected: " << ArtMethod::PrettyMethod(callee) |
| 932 | << " Found: " << ArtMethod::PrettyMethod(GetMethod()); |
Nicolas Geoffray | 8feb7eb | 2020-02-04 09:21:33 +0000 | [diff] [blame] | 933 | } else if (!instrumentation_frame.method_->IsRuntimeMethod()) { |
| 934 | // Trampolines get replaced with their actual method in the stack, |
| 935 | // so don't do the check below for runtime methods. |
Nicolas Geoffray | e91e795 | 2020-01-23 10:15:56 +0000 | [diff] [blame] | 936 | // Instrumentation generally doesn't distinguish between a method's obsolete and |
| 937 | // non-obsolete version. |
| 938 | CHECK_EQ(instrumentation_frame.method_->GetNonObsoleteMethod(), |
| 939 | GetMethod()->GetNonObsoleteMethod()) |
| 940 | << "Expected: " |
| 941 | << ArtMethod::PrettyMethod(instrumentation_frame.method_->GetNonObsoleteMethod()) |
| 942 | << " Found: " << ArtMethod::PrettyMethod(GetMethod()->GetNonObsoleteMethod()); |
Alex Light | e0c6d43 | 2020-01-22 22:04:20 +0000 | [diff] [blame] | 943 | } |
Nicolas Geoffray | e91e795 | 2020-01-23 10:15:56 +0000 | [diff] [blame] | 944 | return_pc = instrumentation_frame.return_pc_; |
Alex Light | e0c6d43 | 2020-01-22 22:04:20 +0000 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | cur_quick_frame_pc_ = return_pc; |
| 948 | uint8_t* next_frame = reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size; |
| 949 | cur_quick_frame_ = reinterpret_cast<ArtMethod**>(next_frame); |
| 950 | |
| 951 | if (kDebugStackWalk) { |
| 952 | LOG(INFO) << ArtMethod::PrettyMethod(method) << "@" << method << " size=" << frame_size |
| 953 | << std::boolalpha |
| 954 | << " optimized=" << (cur_oat_quick_method_header_ != nullptr && |
| 955 | cur_oat_quick_method_header_->IsOptimized()) |
| 956 | << " native=" << method->IsNative() |
| 957 | << std::noboolalpha |
| 958 | << " entrypoints=" << method->GetEntryPointFromQuickCompiledCode() |
| 959 | << "," << (method->IsNative() ? method->GetEntryPointFromJni() : nullptr) |
| 960 | << " next=" << *cur_quick_frame_; |
| 961 | } |
| 962 | |
| 963 | if (kCount == CountTransitions::kYes || !method->IsRuntimeMethod()) { |
| 964 | cur_depth_++; |
| 965 | } |
| 966 | method = *cur_quick_frame_; |
Alex Light | 721e402 | 2020-01-14 14:45:40 -0800 | [diff] [blame] | 967 | } |
Nicolas Geoffray | 51ad7fe | 2020-02-04 12:46:47 +0000 | [diff] [blame] | 968 | // We reached a transition frame, it doesn't have a method header. |
| 969 | cur_oat_quick_method_header_ = nullptr; |
Alex Light | e0c6d43 | 2020-01-22 22:04:20 +0000 | [diff] [blame] | 970 | } else if (cur_shadow_frame_ != nullptr) { |
| 971 | do { |
Orion Hodson | 6aaa49d | 2020-07-28 15:53:04 +0100 | [diff] [blame] | 972 | ValidateFrame(); |
Alex Light | e0c6d43 | 2020-01-22 22:04:20 +0000 | [diff] [blame] | 973 | bool should_continue = VisitFrame(); |
| 974 | if (UNLIKELY(!should_continue)) { |
| 975 | return; |
| 976 | } |
| 977 | cur_depth_++; |
| 978 | cur_shadow_frame_ = cur_shadow_frame_->GetLink(); |
| 979 | } while (cur_shadow_frame_ != nullptr); |
| 980 | } |
| 981 | if (include_transitions) { |
| 982 | bool should_continue = VisitFrame(); |
| 983 | if (!should_continue) { |
| 984 | return; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 985 | } |
| 986 | } |
Alex Light | e0c6d43 | 2020-01-22 22:04:20 +0000 | [diff] [blame] | 987 | if (kCount == CountTransitions::kYes) { |
| 988 | cur_depth_++; |
Andreas Gampe | 585da95 | 2016-12-02 14:52:29 -0800 | [diff] [blame] | 989 | } |
Alex Light | e0c6d43 | 2020-01-22 22:04:20 +0000 | [diff] [blame] | 990 | } |
| 991 | if (num_frames_ != 0) { |
| 992 | CHECK_EQ(cur_depth_, num_frames_); |
| 993 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 994 | } |
| 995 | |
Andreas Gampe | 585da95 | 2016-12-02 14:52:29 -0800 | [diff] [blame] | 996 | template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kYes>(bool); |
| 997 | template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kNo>(bool); |
| 998 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 999 | } // namespace art |