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 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 19 | #include "arch/context.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 20 | #include "art_method-inl.h" |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 21 | #include "base/hex_dump.h" |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 22 | #include "entrypoints/entrypoint_utils-inl.h" |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 23 | #include "entrypoints/runtime_asm_entrypoints.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 24 | #include "gc/space/image_space.h" |
| 25 | #include "gc/space/space-inl.h" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 26 | #include "jit/jit.h" |
| 27 | #include "jit/jit_code_cache.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 28 | #include "linear_alloc.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 29 | #include "mirror/class-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 30 | #include "mirror/object-inl.h" |
| 31 | #include "mirror/object_array-inl.h" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 32 | #include "oat_quick_method_header.h" |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 33 | #include "quick/quick_method_frame_info.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 34 | #include "runtime.h" |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 35 | #include "thread.h" |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 36 | #include "thread_list.h" |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 37 | #include "verify_object-inl.h" |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 38 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 39 | namespace art { |
| 40 | |
Mathieu Chartier | 8405bfd | 2016-02-05 12:00:49 -0800 | [diff] [blame] | 41 | static constexpr bool kDebugStackWalk = false; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 42 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 43 | mirror::Object* ShadowFrame::GetThisObject() const { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 44 | ArtMethod* m = GetMethod(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 45 | if (m->IsStatic()) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 46 | return nullptr; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 47 | } else if (m->IsNative()) { |
| 48 | return GetVRegReference(0); |
| 49 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 50 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 51 | CHECK(code_item != nullptr) << PrettyMethod(m); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 52 | uint16_t reg = code_item->registers_size_ - code_item->ins_size_; |
| 53 | return GetVRegReference(reg); |
| 54 | } |
| 55 | } |
| 56 | |
Jeff Hao | e701f48 | 2013-05-24 11:50:49 -0700 | [diff] [blame] | 57 | mirror::Object* ShadowFrame::GetThisObject(uint16_t num_ins) const { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 58 | ArtMethod* m = GetMethod(); |
Jeff Hao | e701f48 | 2013-05-24 11:50:49 -0700 | [diff] [blame] | 59 | if (m->IsStatic()) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 60 | return nullptr; |
Jeff Hao | e701f48 | 2013-05-24 11:50:49 -0700 | [diff] [blame] | 61 | } else { |
Jeff Hao | 8d44885 | 2013-06-03 17:26:19 -0700 | [diff] [blame] | 62 | return GetVRegReference(NumberOfVRegs() - num_ins); |
Jeff Hao | e701f48 | 2013-05-24 11:50:49 -0700 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 66 | size_t ManagedStack::NumJniShadowFrameReferences() const { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 67 | size_t count = 0; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 68 | for (const ManagedStack* current_fragment = this; current_fragment != nullptr; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 69 | current_fragment = current_fragment->GetLink()) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 70 | for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 71 | current_frame = current_frame->GetLink()) { |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 72 | if (current_frame->GetMethod()->IsNative()) { |
| 73 | // The JNI ShadowFrame only contains references. (For indirect reference.) |
| 74 | count += current_frame->NumberOfVRegs(); |
| 75 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | return count; |
| 79 | } |
| 80 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 81 | bool ManagedStack::ShadowFramesContain(StackReference<mirror::Object>* shadow_frame_entry) const { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 82 | for (const ManagedStack* current_fragment = this; current_fragment != nullptr; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 83 | current_fragment = current_fragment->GetLink()) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 84 | for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 85 | current_frame = current_frame->GetLink()) { |
| 86 | if (current_frame->Contains(shadow_frame_entry)) { |
| 87 | return true; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | return false; |
| 92 | } |
| 93 | |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 94 | StackVisitor::StackVisitor(Thread* thread, Context* context, StackWalkKind walk_kind) |
| 95 | : StackVisitor(thread, context, walk_kind, 0) {} |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 96 | |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 97 | StackVisitor::StackVisitor(Thread* thread, |
| 98 | Context* context, |
| 99 | StackWalkKind walk_kind, |
| 100 | size_t num_frames) |
| 101 | : thread_(thread), |
| 102 | walk_kind_(walk_kind), |
| 103 | cur_shadow_frame_(nullptr), |
| 104 | cur_quick_frame_(nullptr), |
| 105 | cur_quick_frame_pc_(0), |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 106 | cur_oat_quick_method_header_(nullptr), |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 107 | num_frames_(num_frames), |
| 108 | cur_depth_(0), |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 109 | current_inlining_depth_(0), |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 110 | context_(context) { |
| 111 | DCHECK(thread == Thread::Current() || thread->IsSuspended()) << *thread; |
| 112 | } |
| 113 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 114 | InlineInfo StackVisitor::GetCurrentInlineInfo() const { |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 115 | const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader(); |
| 116 | uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_); |
| 117 | CodeInfo code_info = method_header->GetOptimizedCodeInfo(); |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 118 | CodeInfoEncoding encoding = code_info.ExtractEncoding(); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 119 | StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding); |
Nicolas Geoffray | e12997f | 2015-05-22 14:01:33 +0100 | [diff] [blame] | 120 | DCHECK(stack_map.IsValid()); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 121 | return code_info.GetInlineInfoOf(stack_map, encoding); |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 124 | ArtMethod* StackVisitor::GetMethod() const { |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 125 | if (cur_shadow_frame_ != nullptr) { |
| 126 | return cur_shadow_frame_->GetMethod(); |
| 127 | } else if (cur_quick_frame_ != nullptr) { |
| 128 | if (IsInInlinedFrame()) { |
| 129 | size_t depth_in_stack_map = current_inlining_depth_ - 1; |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 130 | InlineInfo inline_info = GetCurrentInlineInfo(); |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 131 | const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader(); |
| 132 | CodeInfoEncoding encoding = method_header->GetOptimizedCodeInfo().ExtractEncoding(); |
Mathieu Chartier | 45bf250 | 2016-03-31 11:07:09 -0700 | [diff] [blame] | 133 | DCHECK(walk_kind_ != StackWalkKind::kSkipInlinedFrames); |
Nicolas Geoffray | c6df1e3 | 2016-07-04 10:15:47 +0100 | [diff] [blame] | 134 | return GetResolvedMethod(*GetCurrentQuickFrame(), |
| 135 | inline_info, |
| 136 | encoding.inline_info_encoding, |
| 137 | depth_in_stack_map); |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 138 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 139 | return *cur_quick_frame_; |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 140 | } |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 141 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 142 | return nullptr; |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 143 | } |
| 144 | |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 145 | uint32_t StackVisitor::GetDexPc(bool abort_on_failure) const { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 146 | if (cur_shadow_frame_ != nullptr) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 147 | return cur_shadow_frame_->GetDexPC(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 148 | } else if (cur_quick_frame_ != nullptr) { |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 149 | if (IsInInlinedFrame()) { |
| 150 | size_t depth_in_stack_map = current_inlining_depth_ - 1; |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 151 | const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader(); |
| 152 | CodeInfoEncoding encoding = method_header->GetOptimizedCodeInfo().ExtractEncoding(); |
| 153 | return GetCurrentInlineInfo().GetDexPcAtDepth(encoding.inline_info_encoding, |
| 154 | depth_in_stack_map); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 155 | } else if (cur_oat_quick_method_header_ == nullptr) { |
| 156 | return DexFile::kDexNoIndex; |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 157 | } else { |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 158 | return cur_oat_quick_method_header_->ToDexPc( |
| 159 | GetMethod(), cur_quick_frame_pc_, abort_on_failure); |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 160 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 161 | } else { |
| 162 | return 0; |
| 163 | } |
| 164 | } |
| 165 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 166 | extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 167 | SHARED_REQUIRES(Locks::mutator_lock_); |
Sebastien Hertz | a836bc9 | 2014-11-25 16:30:53 +0100 | [diff] [blame] | 168 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 169 | mirror::Object* StackVisitor::GetThisObject() const { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 170 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), sizeof(void*)); |
| 171 | ArtMethod* m = GetMethod(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 172 | if (m->IsStatic()) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 173 | return nullptr; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 174 | } else if (m->IsNative()) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 175 | if (cur_quick_frame_ != nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 176 | HandleScope* hs = reinterpret_cast<HandleScope*>( |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 177 | reinterpret_cast<char*>(cur_quick_frame_) + sizeof(ArtMethod*)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 178 | return hs->GetReference(0); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 179 | } else { |
| 180 | return cur_shadow_frame_->GetVRegReference(0); |
| 181 | } |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 182 | } else if (m->IsProxyMethod()) { |
Sebastien Hertz | a836bc9 | 2014-11-25 16:30:53 +0100 | [diff] [blame] | 183 | if (cur_quick_frame_ != nullptr) { |
| 184 | return artQuickGetProxyThisObject(cur_quick_frame_); |
| 185 | } else { |
| 186 | return cur_shadow_frame_->GetVRegReference(0); |
| 187 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 188 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 189 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 190 | if (code_item == nullptr) { |
Ian Rogers | e0dcd46 | 2014-03-08 15:21:04 -0800 | [diff] [blame] | 191 | UNIMPLEMENTED(ERROR) << "Failed to determine this object of abstract or proxy method: " |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 192 | << PrettyMethod(m); |
Ian Rogers | e0dcd46 | 2014-03-08 15:21:04 -0800 | [diff] [blame] | 193 | return nullptr; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 194 | } else { |
| 195 | uint16_t reg = code_item->registers_size_ - code_item->ins_size_; |
Nicolas Geoffray | 15b9d52 | 2015-03-12 15:05:13 +0000 | [diff] [blame] | 196 | uint32_t value = 0; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 197 | bool success = GetVReg(m, reg, kReferenceVReg, &value); |
Nicolas Geoffray | 15b9d52 | 2015-03-12 15:05:13 +0000 | [diff] [blame] | 198 | // We currently always guarantee the `this` object is live throughout the method. |
| 199 | CHECK(success) << "Failed to read the this object in " << PrettyMethod(m); |
| 200 | return reinterpret_cast<mirror::Object*>(value); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 205 | size_t StackVisitor::GetNativePcOffset() const { |
| 206 | DCHECK(!IsShadowFrame()); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 207 | return GetCurrentOatQuickMethodHeader()->NativeQuickPcOffset(cur_quick_frame_pc_); |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 210 | bool StackVisitor::GetVRegFromDebuggerShadowFrame(uint16_t vreg, |
| 211 | VRegKind kind, |
| 212 | uint32_t* val) const { |
| 213 | size_t frame_id = const_cast<StackVisitor*>(this)->GetFrameId(); |
| 214 | ShadowFrame* shadow_frame = thread_->FindDebuggerShadowFrame(frame_id); |
| 215 | if (shadow_frame != nullptr) { |
| 216 | bool* updated_vreg_flags = thread_->GetUpdatedVRegFlags(frame_id); |
| 217 | DCHECK(updated_vreg_flags != nullptr); |
| 218 | if (updated_vreg_flags[vreg]) { |
| 219 | // Value is set by the debugger. |
| 220 | if (kind == kReferenceVReg) { |
| 221 | *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>( |
| 222 | shadow_frame->GetVRegReference(vreg))); |
| 223 | } else { |
| 224 | *val = shadow_frame->GetVReg(vreg); |
| 225 | } |
| 226 | return true; |
| 227 | } |
| 228 | } |
| 229 | // No value is set by the debugger. |
| 230 | return false; |
| 231 | } |
| 232 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 233 | 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] | 234 | if (cur_quick_frame_ != nullptr) { |
| 235 | DCHECK(context_ != nullptr); // You can't reliably read registers without a context. |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 236 | DCHECK(m == GetMethod()); |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 237 | // Check if there is value set by the debugger. |
| 238 | if (GetVRegFromDebuggerShadowFrame(vreg, kind, val)) { |
| 239 | return true; |
| 240 | } |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 241 | DCHECK(cur_oat_quick_method_header_->IsOptimized()); |
| 242 | return GetVRegFromOptimizedCode(m, vreg, kind, val); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 243 | } else { |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 244 | DCHECK(cur_shadow_frame_ != nullptr); |
Sebastien Hertz | 0968744 | 2015-11-17 10:35:39 +0100 | [diff] [blame] | 245 | if (kind == kReferenceVReg) { |
| 246 | *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>( |
| 247 | cur_shadow_frame_->GetVRegReference(vreg))); |
| 248 | } else { |
| 249 | *val = cur_shadow_frame_->GetVReg(vreg); |
| 250 | } |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 251 | return true; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 255 | bool StackVisitor::GetVRegFromOptimizedCode(ArtMethod* m, uint16_t vreg, VRegKind kind, |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 256 | uint32_t* val) const { |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 257 | DCHECK_EQ(m, GetMethod()); |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 258 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 259 | DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 260 | // its instructions? |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 261 | uint16_t number_of_dex_registers = code_item->registers_size_; |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 262 | DCHECK_LT(vreg, code_item->registers_size_); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 263 | const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader(); |
| 264 | CodeInfo code_info = method_header->GetOptimizedCodeInfo(); |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 265 | CodeInfoEncoding encoding = code_info.ExtractEncoding(); |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 266 | |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 267 | uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 268 | StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding); |
Nicolas Geoffray | e12997f | 2015-05-22 14:01:33 +0100 | [diff] [blame] | 269 | DCHECK(stack_map.IsValid()); |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 270 | size_t depth_in_stack_map = current_inlining_depth_ - 1; |
| 271 | |
| 272 | DexRegisterMap dex_register_map = IsInInlinedFrame() |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 273 | ? code_info.GetDexRegisterMapAtDepth(depth_in_stack_map, |
| 274 | code_info.GetInlineInfoOf(stack_map, encoding), |
| 275 | encoding, |
| 276 | number_of_dex_registers) |
| 277 | : code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers); |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 278 | |
Nicolas Geoffray | 012fc4e | 2016-01-08 15:58:19 +0000 | [diff] [blame] | 279 | if (!dex_register_map.IsValid()) { |
| 280 | return false; |
| 281 | } |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 282 | DexRegisterLocation::Kind location_kind = |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 283 | dex_register_map.GetLocationKind(vreg, number_of_dex_registers, code_info, encoding); |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 284 | switch (location_kind) { |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 285 | case DexRegisterLocation::Kind::kInStack: { |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 286 | const int32_t offset = dex_register_map.GetStackOffsetInBytes(vreg, |
| 287 | number_of_dex_registers, |
| 288 | code_info, |
| 289 | encoding); |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 290 | const uint8_t* addr = reinterpret_cast<const uint8_t*>(cur_quick_frame_) + offset; |
| 291 | *val = *reinterpret_cast<const uint32_t*>(addr); |
| 292 | return true; |
| 293 | } |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 294 | case DexRegisterLocation::Kind::kInRegister: |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 295 | case DexRegisterLocation::Kind::kInRegisterHigh: |
| 296 | case DexRegisterLocation::Kind::kInFpuRegister: |
| 297 | case DexRegisterLocation::Kind::kInFpuRegisterHigh: { |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 298 | uint32_t reg = |
| 299 | dex_register_map.GetMachineRegister(vreg, number_of_dex_registers, code_info, encoding); |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 300 | return GetRegisterIfAccessible(reg, kind, val); |
| 301 | } |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 302 | case DexRegisterLocation::Kind::kConstant: |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 303 | *val = dex_register_map.GetConstant(vreg, number_of_dex_registers, code_info, encoding); |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 304 | return true; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 305 | case DexRegisterLocation::Kind::kNone: |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 306 | return false; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 307 | default: |
| 308 | LOG(FATAL) |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 309 | << "Unexpected location kind " |
| 310 | << dex_register_map.GetLocationInternalKind(vreg, |
| 311 | number_of_dex_registers, |
| 312 | code_info, |
| 313 | encoding); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 314 | UNREACHABLE(); |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 315 | } |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | bool StackVisitor::GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const { |
| 319 | const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg); |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 320 | |
| 321 | // X86 float registers are 64-bit and the logic below does not apply. |
| 322 | DCHECK(!is_float || kRuntimeISA != InstructionSet::kX86); |
| 323 | |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 324 | if (!IsAccessibleRegister(reg, is_float)) { |
| 325 | return false; |
| 326 | } |
| 327 | uintptr_t ptr_val = GetRegister(reg, is_float); |
| 328 | const bool target64 = Is64BitInstructionSet(kRuntimeISA); |
| 329 | if (target64) { |
| 330 | const bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg); |
| 331 | const bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg); |
| 332 | int64_t value_long = static_cast<int64_t>(ptr_val); |
| 333 | if (wide_lo) { |
| 334 | ptr_val = static_cast<uintptr_t>(Low32Bits(value_long)); |
| 335 | } else if (wide_hi) { |
| 336 | ptr_val = static_cast<uintptr_t>(High32Bits(value_long)); |
| 337 | } |
| 338 | } |
| 339 | *val = ptr_val; |
| 340 | return true; |
| 341 | } |
| 342 | |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 343 | bool StackVisitor::GetVRegPairFromDebuggerShadowFrame(uint16_t vreg, |
| 344 | VRegKind kind_lo, |
| 345 | VRegKind kind_hi, |
| 346 | uint64_t* val) const { |
| 347 | uint32_t low_32bits; |
| 348 | uint32_t high_32bits; |
| 349 | bool success = GetVRegFromDebuggerShadowFrame(vreg, kind_lo, &low_32bits); |
| 350 | success &= GetVRegFromDebuggerShadowFrame(vreg + 1, kind_hi, &high_32bits); |
| 351 | if (success) { |
| 352 | *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits); |
| 353 | } |
| 354 | return success; |
| 355 | } |
| 356 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 357 | bool StackVisitor::GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo, |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 358 | VRegKind kind_hi, uint64_t* val) const { |
| 359 | if (kind_lo == kLongLoVReg) { |
| 360 | DCHECK_EQ(kind_hi, kLongHiVReg); |
| 361 | } else if (kind_lo == kDoubleLoVReg) { |
| 362 | DCHECK_EQ(kind_hi, kDoubleHiVReg); |
| 363 | } else { |
| 364 | 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] | 365 | UNREACHABLE(); |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 366 | } |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 367 | // Check if there is value set by the debugger. |
| 368 | if (GetVRegPairFromDebuggerShadowFrame(vreg, kind_lo, kind_hi, val)) { |
| 369 | return true; |
| 370 | } |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 371 | if (cur_quick_frame_ != nullptr) { |
| 372 | DCHECK(context_ != nullptr); // You can't reliably read registers without a context. |
| 373 | DCHECK(m == GetMethod()); |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 374 | DCHECK(cur_oat_quick_method_header_->IsOptimized()); |
| 375 | return GetVRegPairFromOptimizedCode(m, vreg, kind_lo, kind_hi, val); |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 376 | } else { |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 377 | DCHECK(cur_shadow_frame_ != nullptr); |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 378 | *val = cur_shadow_frame_->GetVRegLong(vreg); |
| 379 | return true; |
| 380 | } |
| 381 | } |
| 382 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 383 | bool StackVisitor::GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg, |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 384 | VRegKind kind_lo, VRegKind kind_hi, |
| 385 | uint64_t* val) const { |
| 386 | uint32_t low_32bits; |
| 387 | uint32_t high_32bits; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 388 | bool success = GetVRegFromOptimizedCode(m, vreg, kind_lo, &low_32bits); |
| 389 | success &= GetVRegFromOptimizedCode(m, vreg + 1, kind_hi, &high_32bits); |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 390 | if (success) { |
| 391 | *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits); |
| 392 | } |
| 393 | return success; |
| 394 | } |
| 395 | |
| 396 | bool StackVisitor::GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi, |
| 397 | VRegKind kind_lo, uint64_t* val) const { |
| 398 | const bool is_float = (kind_lo == kDoubleLoVReg); |
| 399 | if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) { |
| 400 | return false; |
| 401 | } |
| 402 | uintptr_t ptr_val_lo = GetRegister(reg_lo, is_float); |
| 403 | uintptr_t ptr_val_hi = GetRegister(reg_hi, is_float); |
| 404 | bool target64 = Is64BitInstructionSet(kRuntimeISA); |
| 405 | if (target64) { |
| 406 | int64_t value_long_lo = static_cast<int64_t>(ptr_val_lo); |
| 407 | int64_t value_long_hi = static_cast<int64_t>(ptr_val_hi); |
| 408 | ptr_val_lo = static_cast<uintptr_t>(Low32Bits(value_long_lo)); |
| 409 | ptr_val_hi = static_cast<uintptr_t>(High32Bits(value_long_hi)); |
| 410 | } |
| 411 | *val = (static_cast<uint64_t>(ptr_val_hi) << 32) | static_cast<uint32_t>(ptr_val_lo); |
| 412 | return true; |
| 413 | } |
| 414 | |
Mingyao Yang | 636b925 | 2015-07-31 16:40:24 -0700 | [diff] [blame] | 415 | bool StackVisitor::SetVReg(ArtMethod* m, |
| 416 | uint16_t vreg, |
| 417 | uint32_t new_value, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 418 | VRegKind kind) { |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 419 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
| 420 | if (code_item == nullptr) { |
| 421 | return false; |
| 422 | } |
| 423 | ShadowFrame* shadow_frame = GetCurrentShadowFrame(); |
| 424 | if (shadow_frame == nullptr) { |
| 425 | // This is a compiled frame: we must prepare and update a shadow frame that will |
| 426 | // be executed by the interpreter after deoptimization of the stack. |
| 427 | const size_t frame_id = GetFrameId(); |
| 428 | const uint16_t num_regs = code_item->registers_size_; |
| 429 | shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc()); |
| 430 | CHECK(shadow_frame != nullptr); |
| 431 | // Remember the vreg has been set for debugging and must not be overwritten by the |
| 432 | // original value during deoptimization of the stack. |
| 433 | thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true; |
| 434 | } |
| 435 | if (kind == kReferenceVReg) { |
| 436 | shadow_frame->SetVRegReference(vreg, reinterpret_cast<mirror::Object*>(new_value)); |
| 437 | } else { |
| 438 | shadow_frame->SetVReg(vreg, new_value); |
| 439 | } |
| 440 | return true; |
| 441 | } |
| 442 | |
Mingyao Yang | 636b925 | 2015-07-31 16:40:24 -0700 | [diff] [blame] | 443 | bool StackVisitor::SetVRegPair(ArtMethod* m, |
| 444 | uint16_t vreg, |
| 445 | uint64_t new_value, |
| 446 | VRegKind kind_lo, |
| 447 | VRegKind kind_hi) { |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 448 | if (kind_lo == kLongLoVReg) { |
| 449 | DCHECK_EQ(kind_hi, kLongHiVReg); |
| 450 | } else if (kind_lo == kDoubleLoVReg) { |
| 451 | DCHECK_EQ(kind_hi, kDoubleHiVReg); |
| 452 | } else { |
| 453 | LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi; |
| 454 | UNREACHABLE(); |
| 455 | } |
| 456 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
| 457 | if (code_item == nullptr) { |
| 458 | return false; |
| 459 | } |
| 460 | ShadowFrame* shadow_frame = GetCurrentShadowFrame(); |
| 461 | if (shadow_frame == nullptr) { |
| 462 | // This is a compiled frame: we must prepare for deoptimization (see SetVRegFromDebugger). |
| 463 | const size_t frame_id = GetFrameId(); |
| 464 | const uint16_t num_regs = code_item->registers_size_; |
| 465 | shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc()); |
| 466 | CHECK(shadow_frame != nullptr); |
| 467 | // Remember the vreg pair has been set for debugging and must not be overwritten by the |
| 468 | // original value during deoptimization of the stack. |
| 469 | thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true; |
| 470 | thread_->GetUpdatedVRegFlags(frame_id)[vreg + 1] = true; |
| 471 | } |
| 472 | shadow_frame->SetVRegLong(vreg, new_value); |
| 473 | return true; |
| 474 | } |
| 475 | |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 476 | bool StackVisitor::IsAccessibleGPR(uint32_t reg) const { |
| 477 | DCHECK(context_ != nullptr); |
| 478 | return context_->IsAccessibleGPR(reg); |
| 479 | } |
| 480 | |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 481 | uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const { |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 482 | DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine"; |
| 483 | DCHECK(context_ != nullptr); |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 484 | return context_->GetGPRAddress(reg); |
| 485 | } |
| 486 | |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 487 | uintptr_t StackVisitor::GetGPR(uint32_t reg) const { |
| 488 | DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine"; |
| 489 | DCHECK(context_ != nullptr); |
| 490 | return context_->GetGPR(reg); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 493 | bool StackVisitor::IsAccessibleFPR(uint32_t reg) const { |
| 494 | DCHECK(context_ != nullptr); |
| 495 | return context_->IsAccessibleFPR(reg); |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 496 | } |
| 497 | |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 498 | uintptr_t StackVisitor::GetFPR(uint32_t reg) const { |
| 499 | DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine"; |
| 500 | DCHECK(context_ != nullptr); |
| 501 | return context_->GetFPR(reg); |
| 502 | } |
| 503 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 504 | uintptr_t StackVisitor::GetReturnPc() const { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 505 | uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 506 | DCHECK(sp != nullptr); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 507 | uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 508 | return *reinterpret_cast<uintptr_t*>(pc_addr); |
| 509 | } |
| 510 | |
| 511 | void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 512 | uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 513 | CHECK(sp != nullptr); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 514 | uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 515 | *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc; |
| 516 | } |
| 517 | |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 518 | size_t StackVisitor::ComputeNumFrames(Thread* thread, StackWalkKind walk_kind) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 519 | struct NumFramesVisitor : public StackVisitor { |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 520 | NumFramesVisitor(Thread* thread_in, StackWalkKind walk_kind_in) |
| 521 | : StackVisitor(thread_in, nullptr, walk_kind_in), frames(0) {} |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 522 | |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 523 | bool VisitFrame() OVERRIDE { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 524 | frames++; |
| 525 | return true; |
| 526 | } |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 527 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 528 | size_t frames; |
| 529 | }; |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 530 | NumFramesVisitor visitor(thread, walk_kind); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 531 | visitor.WalkStack(true); |
| 532 | return visitor.frames; |
| 533 | } |
| 534 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 535 | bool StackVisitor::GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc) { |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 536 | struct HasMoreFramesVisitor : public StackVisitor { |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 537 | HasMoreFramesVisitor(Thread* thread, |
| 538 | StackWalkKind walk_kind, |
| 539 | size_t num_frames, |
| 540 | size_t frame_height) |
| 541 | : StackVisitor(thread, nullptr, walk_kind, num_frames), |
| 542 | frame_height_(frame_height), |
| 543 | found_frame_(false), |
| 544 | has_more_frames_(false), |
| 545 | next_method_(nullptr), |
| 546 | next_dex_pc_(0) { |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 549 | bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 550 | if (found_frame_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 551 | ArtMethod* method = GetMethod(); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 552 | if (method != nullptr && !method->IsRuntimeMethod()) { |
| 553 | has_more_frames_ = true; |
| 554 | next_method_ = method; |
| 555 | next_dex_pc_ = GetDexPc(); |
| 556 | return false; // End stack walk once next method is found. |
| 557 | } |
| 558 | } else if (GetFrameHeight() == frame_height_) { |
| 559 | found_frame_ = true; |
| 560 | } |
| 561 | return true; |
| 562 | } |
| 563 | |
| 564 | size_t frame_height_; |
| 565 | bool found_frame_; |
| 566 | bool has_more_frames_; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 567 | ArtMethod* next_method_; |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 568 | uint32_t next_dex_pc_; |
| 569 | }; |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 570 | HasMoreFramesVisitor visitor(thread_, walk_kind_, GetNumFrames(), GetFrameHeight()); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 571 | visitor.WalkStack(true); |
| 572 | *next_method = visitor.next_method_; |
| 573 | *next_dex_pc = visitor.next_dex_pc_; |
| 574 | return visitor.has_more_frames_; |
| 575 | } |
| 576 | |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 577 | void StackVisitor::DescribeStack(Thread* thread) { |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 578 | struct DescribeStackVisitor : public StackVisitor { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 579 | explicit DescribeStackVisitor(Thread* thread_in) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 580 | : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {} |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 581 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 582 | bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 583 | LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation(); |
| 584 | return true; |
| 585 | } |
| 586 | }; |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 587 | DescribeStackVisitor visitor(thread); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 588 | visitor.WalkStack(true); |
| 589 | } |
| 590 | |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame] | 591 | std::string StackVisitor::DescribeLocation() const { |
| 592 | std::string result("Visiting method '"); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 593 | ArtMethod* m = GetMethod(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 594 | if (m == nullptr) { |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 595 | return "upcall"; |
| 596 | } |
| 597 | result += PrettyMethod(m); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 598 | result += StringPrintf("' at dex PC 0x%04x", GetDexPc()); |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame] | 599 | if (!IsShadowFrame()) { |
| 600 | result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc())); |
| 601 | } |
| 602 | return result; |
| 603 | } |
| 604 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 605 | static instrumentation::InstrumentationStackFrame& GetInstrumentationStackFrame(Thread* thread, |
| 606 | uint32_t depth) { |
| 607 | CHECK_LT(depth, thread->GetInstrumentationStack()->size()); |
| 608 | return thread->GetInstrumentationStack()->at(depth); |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 609 | } |
| 610 | |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 611 | static void AssertPcIsWithinQuickCode(ArtMethod* method, uintptr_t pc) |
| 612 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 613 | if (method->IsNative() || method->IsRuntimeMethod() || method->IsProxyMethod()) { |
| 614 | return; |
| 615 | } |
| 616 | |
| 617 | if (pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) { |
| 618 | return; |
| 619 | } |
| 620 | |
| 621 | const void* code = method->GetEntryPointFromQuickCompiledCode(); |
| 622 | if (code == GetQuickInstrumentationEntryPoint()) { |
| 623 | return; |
| 624 | } |
| 625 | |
| 626 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 627 | if (class_linker->IsQuickToInterpreterBridge(code) || |
| 628 | class_linker->IsQuickResolutionStub(code)) { |
| 629 | return; |
| 630 | } |
| 631 | |
| 632 | // If we are the JIT then we may have just compiled the method after the |
| 633 | // IsQuickToInterpreterBridge check. |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 634 | Runtime* runtime = Runtime::Current(); |
| 635 | if (runtime->UseJitCompilation() && runtime->GetJit()->GetCodeCache()->ContainsPc(code)) { |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 636 | return; |
| 637 | } |
| 638 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 639 | uint32_t code_size = OatQuickMethodHeader::FromEntryPoint(code)->code_size_; |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 640 | uintptr_t code_start = reinterpret_cast<uintptr_t>(code); |
| 641 | CHECK(code_start <= pc && pc <= (code_start + code_size)) |
| 642 | << PrettyMethod(method) |
| 643 | << " pc=" << std::hex << pc |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 644 | << " code_start=" << code_start |
| 645 | << " code_size=" << code_size; |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 646 | } |
| 647 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 648 | void StackVisitor::SanityCheckFrame() const { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 649 | if (kIsDebugBuild) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 650 | ArtMethod* method = GetMethod(); |
| 651 | auto* declaring_class = method->GetDeclaringClass(); |
| 652 | // Runtime methods have null declaring class. |
| 653 | if (!method->IsRuntimeMethod()) { |
| 654 | CHECK(declaring_class != nullptr); |
| 655 | CHECK_EQ(declaring_class->GetClass(), declaring_class->GetClass()->GetClass()) |
| 656 | << declaring_class; |
| 657 | } else { |
| 658 | CHECK(declaring_class == nullptr); |
| 659 | } |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 660 | Runtime* const runtime = Runtime::Current(); |
| 661 | LinearAlloc* const linear_alloc = runtime->GetLinearAlloc(); |
| 662 | if (!linear_alloc->Contains(method)) { |
| 663 | // Check class linker linear allocs. |
| 664 | mirror::Class* klass = method->GetDeclaringClass(); |
| 665 | LinearAlloc* const class_linear_alloc = (klass != nullptr) |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 666 | ? runtime->GetClassLinker()->GetAllocatorForClassLoader(klass->GetClassLoader()) |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 667 | : linear_alloc; |
| 668 | if (!class_linear_alloc->Contains(method)) { |
| 669 | // Check image space. |
| 670 | bool in_image = false; |
| 671 | for (auto& space : runtime->GetHeap()->GetContinuousSpaces()) { |
| 672 | if (space->IsImageSpace()) { |
| 673 | auto* image_space = space->AsImageSpace(); |
| 674 | const auto& header = image_space->GetImageHeader(); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 675 | const ImageSection& methods = header.GetMethodsSection(); |
| 676 | const ImageSection& runtime_methods = header.GetRuntimeMethodsSection(); |
| 677 | const size_t offset = reinterpret_cast<const uint8_t*>(method) - image_space->Begin(); |
| 678 | if (methods.Contains(offset) || runtime_methods.Contains(offset)) { |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 679 | in_image = true; |
| 680 | break; |
| 681 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 682 | } |
| 683 | } |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 684 | CHECK(in_image) << PrettyMethod(method) << " not in linear alloc or image"; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 685 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 686 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 687 | if (cur_quick_frame_ != nullptr) { |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 688 | AssertPcIsWithinQuickCode(method, cur_quick_frame_pc_); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 689 | // Frame sanity. |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 690 | size_t frame_size = GetCurrentQuickFrameInfo().FrameSizeInBytes(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 691 | CHECK_NE(frame_size, 0u); |
Andreas Gampe | 5b417b9 | 2014-03-10 14:18:35 -0700 | [diff] [blame] | 692 | // A rough guess at an upper size we expect to see for a frame. |
| 693 | // 256 registers |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 694 | // 2 words HandleScope overhead |
Andreas Gampe | 5b417b9 | 2014-03-10 14:18:35 -0700 | [diff] [blame] | 695 | // 3+3 register spills |
| 696 | // TODO: this seems architecture specific for the case of JNI frames. |
Brian Carlstrom | ed08bd4 | 2014-03-19 18:34:17 -0700 | [diff] [blame] | 697 | // TODO: 083-compiler-regressions ManyFloatArgs shows this estimate is wrong. |
| 698 | // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word); |
| 699 | const size_t kMaxExpectedFrameSize = 2 * KB; |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 700 | CHECK_LE(frame_size, kMaxExpectedFrameSize) << PrettyMethod(method); |
| 701 | size_t return_pc_offset = GetCurrentQuickFrameInfo().GetReturnPcOffset(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 702 | CHECK_LT(return_pc_offset, frame_size); |
| 703 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 704 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 705 | } |
| 706 | |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 707 | // Counts the number of references in the parameter list of the corresponding method. |
| 708 | // Note: Thus does _not_ include "this" for non-static methods. |
| 709 | static uint32_t GetNumberOfReferenceArgsWithoutReceiver(ArtMethod* method) |
| 710 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 711 | uint32_t shorty_len; |
| 712 | const char* shorty = method->GetShorty(&shorty_len); |
| 713 | uint32_t refs = 0; |
| 714 | for (uint32_t i = 1; i < shorty_len ; ++i) { |
| 715 | if (shorty[i] == 'L') { |
| 716 | refs++; |
| 717 | } |
| 718 | } |
| 719 | return refs; |
| 720 | } |
| 721 | |
| 722 | QuickMethodFrameInfo StackVisitor::GetCurrentQuickFrameInfo() const { |
| 723 | if (cur_oat_quick_method_header_ != nullptr) { |
| 724 | return cur_oat_quick_method_header_->GetFrameInfo(); |
| 725 | } |
| 726 | |
| 727 | ArtMethod* method = GetMethod(); |
| 728 | Runtime* runtime = Runtime::Current(); |
| 729 | |
| 730 | if (method->IsAbstract()) { |
| 731 | return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs); |
| 732 | } |
| 733 | |
| 734 | // This goes before IsProxyMethod since runtime methods have a null declaring class. |
| 735 | if (method->IsRuntimeMethod()) { |
| 736 | return runtime->GetRuntimeMethodFrameInfo(method); |
| 737 | } |
| 738 | |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 739 | if (method->IsProxyMethod()) { |
Nicolas Geoffray | 22cf3d3 | 2015-11-02 11:57:11 +0000 | [diff] [blame] | 740 | // There is only one direct method of a proxy class: the constructor. A direct method is |
| 741 | // cloned from the original java.lang.reflect.Proxy and is executed as usual quick |
| 742 | // compiled method without any stubs. Therefore the method must have a OatQuickMethodHeader. |
| 743 | DCHECK(!method->IsDirect() && !method->IsConstructor()) |
| 744 | << "Constructors of proxy classes must have a OatQuickMethodHeader"; |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 745 | return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 746 | } |
| 747 | |
Nicolas Geoffray | 22cf3d3 | 2015-11-02 11:57:11 +0000 | [diff] [blame] | 748 | // The only remaining case is if the method is native and uses the generic JNI stub. |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 749 | DCHECK(method->IsNative()); |
Nicolas Geoffray | 22cf3d3 | 2015-11-02 11:57:11 +0000 | [diff] [blame] | 750 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 751 | const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(method, sizeof(void*)); |
| 752 | DCHECK(class_linker->IsQuickGenericJniStub(entry_point)) << PrettyMethod(method); |
| 753 | // Generic JNI frame. |
| 754 | uint32_t handle_refs = GetNumberOfReferenceArgsWithoutReceiver(method) + 1; |
| 755 | size_t scope_size = HandleScope::SizeOf(handle_refs); |
| 756 | QuickMethodFrameInfo callee_info = runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs); |
| 757 | |
| 758 | // Callee saves + handle scope + method ref + alignment |
| 759 | // Note: -sizeof(void*) since callee-save frame stores a whole method pointer. |
| 760 | size_t frame_size = RoundUp( |
| 761 | callee_info.FrameSizeInBytes() - sizeof(void*) + sizeof(ArtMethod*) + scope_size, |
| 762 | kStackAlignment); |
| 763 | return QuickMethodFrameInfo(frame_size, callee_info.CoreSpillMask(), callee_info.FpSpillMask()); |
| 764 | } |
| 765 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 766 | void StackVisitor::WalkStack(bool include_transitions) { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 767 | DCHECK(thread_ == Thread::Current() || thread_->IsSuspended()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 768 | CHECK_EQ(cur_depth_, 0U); |
| 769 | bool exit_stubs_installed = Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled(); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 770 | uint32_t instrumentation_stack_depth = 0; |
Sebastien Hertz | b2feaaf | 2015-10-12 13:40:10 +0000 | [diff] [blame] | 771 | size_t inlined_frames_count = 0; |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 772 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 773 | for (const ManagedStack* current_fragment = thread_->GetManagedStack(); |
| 774 | current_fragment != nullptr; current_fragment = current_fragment->GetLink()) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 775 | cur_shadow_frame_ = current_fragment->GetTopShadowFrame(); |
| 776 | cur_quick_frame_ = current_fragment->GetTopQuickFrame(); |
Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 777 | cur_quick_frame_pc_ = 0; |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 778 | cur_oat_quick_method_header_ = nullptr; |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 779 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 780 | if (cur_quick_frame_ != nullptr) { // Handle quick stack frames. |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 781 | // Can't be both a shadow and a quick fragment. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 782 | DCHECK(current_fragment->GetTopShadowFrame() == nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 783 | ArtMethod* method = *cur_quick_frame_; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 784 | while (method != nullptr) { |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 785 | cur_oat_quick_method_header_ = method->GetOatQuickMethodHeader(cur_quick_frame_pc_); |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 786 | SanityCheckFrame(); |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 787 | |
Nicolas Geoffray | c6df1e3 | 2016-07-04 10:15:47 +0100 | [diff] [blame] | 788 | if ((walk_kind_ == StackWalkKind::kIncludeInlinedFrames) |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 789 | && (cur_oat_quick_method_header_ != nullptr) |
| 790 | && cur_oat_quick_method_header_->IsOptimized()) { |
| 791 | CodeInfo code_info = cur_oat_quick_method_header_->GetOptimizedCodeInfo(); |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 792 | CodeInfoEncoding encoding = code_info.ExtractEncoding(); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 793 | uint32_t native_pc_offset = |
| 794 | cur_oat_quick_method_header_->NativeQuickPcOffset(cur_quick_frame_pc_); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 795 | StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding); |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 796 | if (stack_map.IsValid() && stack_map.HasInlineInfo(encoding.stack_map_encoding)) { |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 797 | InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding); |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 798 | DCHECK_EQ(current_inlining_depth_, 0u); |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 799 | for (current_inlining_depth_ = inline_info.GetDepth(encoding.inline_info_encoding); |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 800 | current_inlining_depth_ != 0; |
| 801 | --current_inlining_depth_) { |
| 802 | bool should_continue = VisitFrame(); |
| 803 | if (UNLIKELY(!should_continue)) { |
| 804 | return; |
| 805 | } |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 806 | cur_depth_++; |
Sebastien Hertz | b2feaaf | 2015-10-12 13:40:10 +0000 | [diff] [blame] | 807 | inlined_frames_count++; |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 808 | } |
| 809 | } |
| 810 | } |
| 811 | |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 812 | bool should_continue = VisitFrame(); |
| 813 | if (UNLIKELY(!should_continue)) { |
| 814 | return; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 815 | } |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 816 | |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 817 | QuickMethodFrameInfo frame_info = GetCurrentQuickFrameInfo(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 818 | if (context_ != nullptr) { |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 819 | context_->FillCalleeSaves(reinterpret_cast<uint8_t*>(cur_quick_frame_), frame_info); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 820 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 821 | // Compute PC for next stack frame from return PC. |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 822 | size_t frame_size = frame_info.FrameSizeInBytes(); |
| 823 | size_t return_pc_offset = frame_size - sizeof(void*); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 824 | 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] | 825 | uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 826 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 827 | if (UNLIKELY(exit_stubs_installed)) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 828 | // While profiling, the return pc is restored from the side stack, except when walking |
| 829 | // 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] | 830 | if (reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc) { |
Sebastien Hertz | 74e256b | 2013-10-04 10:40:37 +0200 | [diff] [blame] | 831 | const instrumentation::InstrumentationStackFrame& instrumentation_frame = |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 832 | GetInstrumentationStackFrame(thread_, instrumentation_stack_depth); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 833 | instrumentation_stack_depth++; |
Jeff Hao | fb2802d | 2013-07-24 13:53:05 -0700 | [diff] [blame] | 834 | if (GetMethod() == Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveAll)) { |
| 835 | // Skip runtime save all callee frames which are used to deliver exceptions. |
| 836 | } else if (instrumentation_frame.interpreter_entry_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 837 | ArtMethod* callee = Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs); |
Jeff Hao | fb2802d | 2013-07-24 13:53:05 -0700 | [diff] [blame] | 838 | CHECK_EQ(GetMethod(), callee) << "Expected: " << PrettyMethod(callee) << " Found: " |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 839 | << PrettyMethod(GetMethod()); |
Sebastien Hertz | b2feaaf | 2015-10-12 13:40:10 +0000 | [diff] [blame] | 840 | } else { |
| 841 | CHECK_EQ(instrumentation_frame.method_, GetMethod()) |
| 842 | << "Expected: " << PrettyMethod(instrumentation_frame.method_) |
| 843 | << " Found: " << PrettyMethod(GetMethod()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 844 | } |
| 845 | if (num_frames_ != 0) { |
| 846 | // Check agreement of frame Ids only if num_frames_ is computed to avoid infinite |
| 847 | // recursion. |
Sebastien Hertz | b2feaaf | 2015-10-12 13:40:10 +0000 | [diff] [blame] | 848 | size_t frame_id = instrumentation::Instrumentation::ComputeFrameId( |
| 849 | thread_, |
| 850 | cur_depth_, |
| 851 | inlined_frames_count); |
| 852 | CHECK_EQ(instrumentation_frame.frame_id_, frame_id); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 853 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 854 | return_pc = instrumentation_frame.return_pc_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 855 | } |
| 856 | } |
Nicolas Geoffray | 6bc4374 | 2015-10-12 18:11:10 +0100 | [diff] [blame] | 857 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 858 | cur_quick_frame_pc_ = return_pc; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 859 | 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] | 860 | cur_quick_frame_ = reinterpret_cast<ArtMethod**>(next_frame); |
| 861 | |
| 862 | if (kDebugStackWalk) { |
| 863 | LOG(INFO) << PrettyMethod(method) << "@" << method << " size=" << frame_size |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 864 | << std::boolalpha |
| 865 | << " optimized=" << (cur_oat_quick_method_header_ != nullptr && |
| 866 | cur_oat_quick_method_header_->IsOptimized()) |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 867 | << " native=" << method->IsNative() |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 868 | << std::noboolalpha |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 869 | << " entrypoints=" << method->GetEntryPointFromQuickCompiledCode() |
| 870 | << "," << method->GetEntryPointFromJni() |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 871 | << " next=" << *cur_quick_frame_; |
| 872 | } |
| 873 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 874 | cur_depth_++; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 875 | method = *cur_quick_frame_; |
jeffhao | 6641ea1 | 2013-01-02 18:13:42 -0800 | [diff] [blame] | 876 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 877 | } else if (cur_shadow_frame_ != nullptr) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 878 | do { |
| 879 | SanityCheckFrame(); |
| 880 | bool should_continue = VisitFrame(); |
| 881 | if (UNLIKELY(!should_continue)) { |
| 882 | return; |
| 883 | } |
| 884 | cur_depth_++; |
| 885 | cur_shadow_frame_ = cur_shadow_frame_->GetLink(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 886 | } while (cur_shadow_frame_ != nullptr); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 887 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 888 | if (include_transitions) { |
| 889 | bool should_continue = VisitFrame(); |
| 890 | if (!should_continue) { |
| 891 | return; |
| 892 | } |
| 893 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 894 | cur_depth_++; |
| 895 | } |
| 896 | if (num_frames_ != 0) { |
| 897 | CHECK_EQ(cur_depth_, num_frames_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 901 | void JavaFrameRootInfo::Describe(std::ostream& os) const { |
| 902 | const StackVisitor* visitor = stack_visitor_; |
| 903 | CHECK(visitor != nullptr); |
| 904 | os << "Type=" << GetType() << " thread_id=" << GetThreadId() << " location=" << |
| 905 | visitor->DescribeLocation() << " vreg=" << vreg_; |
| 906 | } |
| 907 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 908 | int StackVisitor::GetVRegOffsetFromQuickCode(const DexFile::CodeItem* code_item, |
| 909 | uint32_t core_spills, uint32_t fp_spills, |
| 910 | size_t frame_size, int reg, InstructionSet isa) { |
| 911 | size_t pointer_size = InstructionSetPointerSize(isa); |
| 912 | if (kIsDebugBuild) { |
| 913 | auto* runtime = Runtime::Current(); |
| 914 | if (runtime != nullptr) { |
| 915 | CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size); |
| 916 | } |
| 917 | } |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 918 | DCHECK_ALIGNED(frame_size, kStackAlignment); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 919 | DCHECK_NE(reg, -1); |
| 920 | int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa) |
| 921 | + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa) |
| 922 | + sizeof(uint32_t); // Filler. |
| 923 | int num_regs = code_item->registers_size_ - code_item->ins_size_; |
| 924 | int temp_threshold = code_item->registers_size_; |
| 925 | const int max_num_special_temps = 1; |
| 926 | if (reg == temp_threshold) { |
| 927 | // The current method pointer corresponds to special location on stack. |
| 928 | return 0; |
| 929 | } else if (reg >= temp_threshold + max_num_special_temps) { |
| 930 | /* |
| 931 | * Special temporaries may have custom locations and the logic above deals with that. |
| 932 | * However, non-special temporaries are placed relative to the outs. |
| 933 | */ |
| 934 | int temps_start = code_item->outs_size_ * sizeof(uint32_t) + pointer_size /* art method */; |
| 935 | int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t); |
| 936 | return temps_start + relative_offset; |
| 937 | } else if (reg < num_regs) { |
| 938 | int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t); |
| 939 | return locals_start + (reg * sizeof(uint32_t)); |
| 940 | } else { |
| 941 | // Handle ins. |
| 942 | return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + pointer_size /* art method */; |
| 943 | } |
| 944 | } |
| 945 | |
Andreas Gampe | 56fdd0e | 2016-04-28 14:56:54 -0700 | [diff] [blame] | 946 | void LockCountData::AddMonitor(Thread* self, mirror::Object* obj) { |
Andreas Gampe | 03ec930 | 2015-08-27 17:41:47 -0700 | [diff] [blame] | 947 | if (obj == nullptr) { |
| 948 | return; |
| 949 | } |
| 950 | |
| 951 | // If there's an error during enter, we won't have locked the monitor. So check there's no |
| 952 | // exception. |
| 953 | if (self->IsExceptionPending()) { |
| 954 | return; |
| 955 | } |
| 956 | |
| 957 | if (monitors_ == nullptr) { |
| 958 | monitors_.reset(new std::vector<mirror::Object*>()); |
| 959 | } |
| 960 | monitors_->push_back(obj); |
| 961 | } |
| 962 | |
Andreas Gampe | 56fdd0e | 2016-04-28 14:56:54 -0700 | [diff] [blame] | 963 | void LockCountData::RemoveMonitorOrThrow(Thread* self, const mirror::Object* obj) { |
Andreas Gampe | 03ec930 | 2015-08-27 17:41:47 -0700 | [diff] [blame] | 964 | if (obj == nullptr) { |
| 965 | return; |
| 966 | } |
| 967 | bool found_object = false; |
| 968 | if (monitors_ != nullptr) { |
| 969 | // We need to remove one pointer to ref, as duplicates are used for counting recursive locks. |
| 970 | // We arbitrarily choose the first one. |
| 971 | auto it = std::find(monitors_->begin(), monitors_->end(), obj); |
| 972 | if (it != monitors_->end()) { |
| 973 | monitors_->erase(it); |
| 974 | found_object = true; |
| 975 | } |
| 976 | } |
| 977 | if (!found_object) { |
| 978 | // The object wasn't found. Time for an IllegalMonitorStateException. |
| 979 | // The order here isn't fully clear. Assume that any other pending exception is swallowed. |
| 980 | // TODO: Maybe make already pending exception a suppressed exception. |
| 981 | self->ClearException(); |
| 982 | self->ThrowNewExceptionF("Ljava/lang/IllegalMonitorStateException;", |
| 983 | "did not lock monitor on object of type '%s' before unlocking", |
| 984 | PrettyTypeOf(const_cast<mirror::Object*>(obj)).c_str()); |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | // Helper to unlock a monitor. Must be NO_THREAD_SAFETY_ANALYSIS, as we can't statically show |
| 989 | // that the object was locked. |
| 990 | void MonitorExitHelper(Thread* self, mirror::Object* obj) NO_THREAD_SAFETY_ANALYSIS { |
| 991 | DCHECK(self != nullptr); |
| 992 | DCHECK(obj != nullptr); |
| 993 | obj->MonitorExit(self); |
| 994 | } |
| 995 | |
Andreas Gampe | 56fdd0e | 2016-04-28 14:56:54 -0700 | [diff] [blame] | 996 | bool LockCountData::CheckAllMonitorsReleasedOrThrow(Thread* self) { |
Andreas Gampe | 03ec930 | 2015-08-27 17:41:47 -0700 | [diff] [blame] | 997 | DCHECK(self != nullptr); |
| 998 | if (monitors_ != nullptr) { |
| 999 | if (!monitors_->empty()) { |
| 1000 | // There may be an exception pending, if the method is terminating abruptly. Clear it. |
| 1001 | // TODO: Should we add this as a suppressed exception? |
| 1002 | self->ClearException(); |
| 1003 | |
| 1004 | // OK, there are monitors that are still locked. To enforce structured locking (and avoid |
| 1005 | // deadlocks) we unlock all of them before we raise the IllegalMonitorState exception. |
| 1006 | for (mirror::Object* obj : *monitors_) { |
| 1007 | MonitorExitHelper(self, obj); |
| 1008 | // If this raised an exception, ignore. TODO: Should we add this as suppressed |
| 1009 | // exceptions? |
| 1010 | if (self->IsExceptionPending()) { |
| 1011 | self->ClearException(); |
| 1012 | } |
| 1013 | } |
| 1014 | // Raise an exception, just give the first object as the sample. |
| 1015 | mirror::Object* first = (*monitors_)[0]; |
| 1016 | self->ThrowNewExceptionF("Ljava/lang/IllegalMonitorStateException;", |
| 1017 | "did not unlock monitor on object of type '%s'", |
| 1018 | PrettyTypeOf(first).c_str()); |
| 1019 | |
| 1020 | // To make sure this path is not triggered again, clean out the monitors. |
| 1021 | monitors_->clear(); |
| 1022 | |
| 1023 | return false; |
| 1024 | } |
| 1025 | } |
| 1026 | return true; |
| 1027 | } |
| 1028 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 1029 | } // namespace art |