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