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 | 50030ef | 2015-05-08 14:19:26 -0700 | [diff] [blame] | 24 | #include "gc_map.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 25 | #include "gc/space/image_space.h" |
| 26 | #include "gc/space/space-inl.h" |
| 27 | #include "linear_alloc.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 28 | #include "mirror/class-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | #include "mirror/object-inl.h" |
| 30 | #include "mirror/object_array-inl.h" |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 31 | #include "quick/quick_method_frame_info.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 32 | #include "runtime.h" |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 33 | #include "thread.h" |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 34 | #include "thread_list.h" |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 35 | #include "verify_object-inl.h" |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 36 | #include "vmap_table.h" |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 37 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 38 | namespace art { |
| 39 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 40 | static constexpr bool kDebugStackWalk = false; |
| 41 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 42 | mirror::Object* ShadowFrame::GetThisObject() const { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 43 | ArtMethod* m = GetMethod(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 44 | if (m->IsStatic()) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 45 | return nullptr; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 46 | } else if (m->IsNative()) { |
| 47 | return GetVRegReference(0); |
| 48 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 49 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 50 | CHECK(code_item != nullptr) << PrettyMethod(m); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 51 | uint16_t reg = code_item->registers_size_ - code_item->ins_size_; |
| 52 | return GetVRegReference(reg); |
| 53 | } |
| 54 | } |
| 55 | |
Jeff Hao | e701f48 | 2013-05-24 11:50:49 -0700 | [diff] [blame] | 56 | mirror::Object* ShadowFrame::GetThisObject(uint16_t num_ins) const { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 57 | ArtMethod* m = GetMethod(); |
Jeff Hao | e701f48 | 2013-05-24 11:50:49 -0700 | [diff] [blame] | 58 | if (m->IsStatic()) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 59 | return nullptr; |
Jeff Hao | e701f48 | 2013-05-24 11:50:49 -0700 | [diff] [blame] | 60 | } else { |
Jeff Hao | 8d44885 | 2013-06-03 17:26:19 -0700 | [diff] [blame] | 61 | return GetVRegReference(NumberOfVRegs() - num_ins); |
Jeff Hao | e701f48 | 2013-05-24 11:50:49 -0700 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 65 | size_t ManagedStack::NumJniShadowFrameReferences() const { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 66 | size_t count = 0; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 67 | for (const ManagedStack* current_fragment = this; current_fragment != nullptr; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 68 | current_fragment = current_fragment->GetLink()) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 69 | for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 70 | current_frame = current_frame->GetLink()) { |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 71 | if (current_frame->GetMethod()->IsNative()) { |
| 72 | // The JNI ShadowFrame only contains references. (For indirect reference.) |
| 73 | count += current_frame->NumberOfVRegs(); |
| 74 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | return count; |
| 78 | } |
| 79 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 80 | bool ManagedStack::ShadowFramesContain(StackReference<mirror::Object>* shadow_frame_entry) const { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 81 | for (const ManagedStack* current_fragment = this; current_fragment != nullptr; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 82 | current_fragment = current_fragment->GetLink()) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 83 | for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 84 | current_frame = current_frame->GetLink()) { |
| 85 | if (current_frame->Contains(shadow_frame_entry)) { |
| 86 | return true; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | return false; |
| 91 | } |
| 92 | |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 93 | StackVisitor::StackVisitor(Thread* thread, Context* context, StackWalkKind walk_kind) |
| 94 | : StackVisitor(thread, context, walk_kind, 0) {} |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 95 | |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 96 | StackVisitor::StackVisitor(Thread* thread, |
| 97 | Context* context, |
| 98 | StackWalkKind walk_kind, |
| 99 | size_t num_frames) |
| 100 | : thread_(thread), |
| 101 | walk_kind_(walk_kind), |
| 102 | cur_shadow_frame_(nullptr), |
| 103 | cur_quick_frame_(nullptr), |
| 104 | cur_quick_frame_pc_(0), |
| 105 | num_frames_(num_frames), |
| 106 | cur_depth_(0), |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 107 | current_inlining_depth_(0), |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 108 | context_(context) { |
| 109 | DCHECK(thread == Thread::Current() || thread->IsSuspended()) << *thread; |
| 110 | } |
| 111 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 112 | InlineInfo StackVisitor::GetCurrentInlineInfo() const { |
| 113 | ArtMethod* outer_method = *GetCurrentQuickFrame(); |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 114 | uint32_t native_pc_offset = outer_method->NativeQuickPcOffset(cur_quick_frame_pc_); |
| 115 | CodeInfo code_info = outer_method->GetOptimizedCodeInfo(); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 116 | StackMapEncoding encoding = code_info.ExtractEncoding(); |
| 117 | StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding); |
Nicolas Geoffray | e12997f | 2015-05-22 14:01:33 +0100 | [diff] [blame] | 118 | DCHECK(stack_map.IsValid()); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 119 | return code_info.GetInlineInfoOf(stack_map, encoding); |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 122 | ArtMethod* StackVisitor::GetMethod() const { |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 123 | if (cur_shadow_frame_ != nullptr) { |
| 124 | return cur_shadow_frame_->GetMethod(); |
| 125 | } else if (cur_quick_frame_ != nullptr) { |
| 126 | if (IsInInlinedFrame()) { |
| 127 | size_t depth_in_stack_map = current_inlining_depth_ - 1; |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 128 | InlineInfo inline_info = GetCurrentInlineInfo(); |
Nicolas Geoffray | 32c9ea5 | 2015-06-12 14:52:33 +0100 | [diff] [blame] | 129 | return GetResolvedMethod(*GetCurrentQuickFrame(), inline_info, depth_in_stack_map); |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 130 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 131 | return *cur_quick_frame_; |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 132 | } |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 133 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 134 | return nullptr; |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 135 | } |
| 136 | |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 137 | uint32_t StackVisitor::GetDexPc(bool abort_on_failure) const { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 138 | if (cur_shadow_frame_ != nullptr) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 139 | return cur_shadow_frame_->GetDexPC(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 140 | } else if (cur_quick_frame_ != nullptr) { |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 141 | if (IsInInlinedFrame()) { |
| 142 | size_t depth_in_stack_map = current_inlining_depth_ - 1; |
| 143 | return GetCurrentInlineInfo().GetDexPcAtDepth(depth_in_stack_map); |
| 144 | } else { |
| 145 | return GetMethod()->ToDexPc(cur_quick_frame_pc_, abort_on_failure); |
| 146 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 147 | } else { |
| 148 | return 0; |
| 149 | } |
| 150 | } |
| 151 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 152 | extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 153 | SHARED_REQUIRES(Locks::mutator_lock_); |
Sebastien Hertz | a836bc9 | 2014-11-25 16:30:53 +0100 | [diff] [blame] | 154 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 155 | mirror::Object* StackVisitor::GetThisObject() const { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 156 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), sizeof(void*)); |
| 157 | ArtMethod* m = GetMethod(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 158 | if (m->IsStatic()) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 159 | return nullptr; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 160 | } else if (m->IsNative()) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 161 | if (cur_quick_frame_ != nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 162 | HandleScope* hs = reinterpret_cast<HandleScope*>( |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 163 | reinterpret_cast<char*>(cur_quick_frame_) + m->GetHandleScopeOffset().SizeValue()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 164 | return hs->GetReference(0); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 165 | } else { |
| 166 | return cur_shadow_frame_->GetVRegReference(0); |
| 167 | } |
Sebastien Hertz | a836bc9 | 2014-11-25 16:30:53 +0100 | [diff] [blame] | 168 | } else if (m->IsProxyMethod()) { |
| 169 | if (cur_quick_frame_ != nullptr) { |
| 170 | return artQuickGetProxyThisObject(cur_quick_frame_); |
| 171 | } else { |
| 172 | return cur_shadow_frame_->GetVRegReference(0); |
| 173 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 174 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 175 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 176 | if (code_item == nullptr) { |
Ian Rogers | e0dcd46 | 2014-03-08 15:21:04 -0800 | [diff] [blame] | 177 | UNIMPLEMENTED(ERROR) << "Failed to determine this object of abstract or proxy method: " |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 178 | << PrettyMethod(m); |
Ian Rogers | e0dcd46 | 2014-03-08 15:21:04 -0800 | [diff] [blame] | 179 | return nullptr; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 180 | } else { |
| 181 | uint16_t reg = code_item->registers_size_ - code_item->ins_size_; |
Nicolas Geoffray | 15b9d52 | 2015-03-12 15:05:13 +0000 | [diff] [blame] | 182 | uint32_t value = 0; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 183 | bool success = GetVReg(m, reg, kReferenceVReg, &value); |
Nicolas Geoffray | 15b9d52 | 2015-03-12 15:05:13 +0000 | [diff] [blame] | 184 | // We currently always guarantee the `this` object is live throughout the method. |
| 185 | CHECK(success) << "Failed to read the this object in " << PrettyMethod(m); |
| 186 | return reinterpret_cast<mirror::Object*>(value); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 191 | size_t StackVisitor::GetNativePcOffset() const { |
| 192 | DCHECK(!IsShadowFrame()); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 193 | return GetMethod()->NativeQuickPcOffset(cur_quick_frame_pc_); |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 196 | bool StackVisitor::IsReferenceVReg(ArtMethod* m, uint16_t vreg) { |
Mathieu Chartier | 50030ef | 2015-05-08 14:19:26 -0700 | [diff] [blame] | 197 | // Process register map (which native and runtime methods don't have) |
| 198 | if (m->IsNative() || m->IsRuntimeMethod() || m->IsProxyMethod()) { |
| 199 | return false; |
| 200 | } |
| 201 | if (m->IsOptimized(sizeof(void*))) { |
| 202 | return true; // TODO: Implement. |
| 203 | } |
| 204 | const uint8_t* native_gc_map = m->GetNativeGcMap(sizeof(void*)); |
| 205 | CHECK(native_gc_map != nullptr) << PrettyMethod(m); |
| 206 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
| 207 | // Can't be null or how would we compile its instructions? |
| 208 | DCHECK(code_item != nullptr) << PrettyMethod(m); |
| 209 | NativePcOffsetToReferenceMap map(native_gc_map); |
| 210 | size_t num_regs = std::min(map.RegWidth() * 8, static_cast<size_t>(code_item->registers_size_)); |
| 211 | const uint8_t* reg_bitmap = nullptr; |
| 212 | if (num_regs > 0) { |
| 213 | Runtime* runtime = Runtime::Current(); |
| 214 | const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(m, sizeof(void*)); |
| 215 | uintptr_t native_pc_offset = m->NativeQuickPcOffset(GetCurrentQuickFramePc(), entry_point); |
| 216 | reg_bitmap = map.FindBitMap(native_pc_offset); |
| 217 | DCHECK(reg_bitmap != nullptr); |
| 218 | } |
| 219 | // Does this register hold a reference? |
| 220 | return vreg < num_regs && TestBitmap(vreg, reg_bitmap); |
| 221 | } |
| 222 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 223 | 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] | 224 | if (cur_quick_frame_ != nullptr) { |
| 225 | DCHECK(context_ != nullptr); // You can't reliably read registers without a context. |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 226 | DCHECK(m == GetMethod()); |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 227 | if (m->IsOptimized(sizeof(void*))) { |
| 228 | return GetVRegFromOptimizedCode(m, vreg, kind, val); |
Ian Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 229 | } else { |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 230 | return GetVRegFromQuickCode(m, vreg, kind, val); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 231 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 232 | } else { |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 233 | DCHECK(cur_shadow_frame_ != nullptr); |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 234 | *val = cur_shadow_frame_->GetVReg(vreg); |
| 235 | return true; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 239 | bool StackVisitor::GetVRegFromQuickCode(ArtMethod* m, uint16_t vreg, VRegKind kind, |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 240 | uint32_t* val) const { |
| 241 | const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*)); |
| 242 | DCHECK(code_pointer != nullptr); |
| 243 | const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*))); |
| 244 | QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer); |
| 245 | uint32_t vmap_offset; |
| 246 | // TODO: IsInContext stops before spotting floating point registers. |
| 247 | if (vmap_table.IsInContext(vreg, kind, &vmap_offset)) { |
| 248 | bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg); |
| 249 | uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask(); |
| 250 | uint32_t reg = vmap_table.ComputeRegister(spill_mask, vmap_offset, kind); |
| 251 | return GetRegisterIfAccessible(reg, kind, val); |
| 252 | } else { |
| 253 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 254 | 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] | 255 | // its instructions? |
Nicolas Geoffray | 15b9d52 | 2015-03-12 15:05:13 +0000 | [diff] [blame] | 256 | *val = *GetVRegAddrFromQuickCode(cur_quick_frame_, code_item, frame_info.CoreSpillMask(), |
| 257 | frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg); |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 258 | return true; |
| 259 | } |
| 260 | } |
| 261 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 262 | bool StackVisitor::GetVRegFromOptimizedCode(ArtMethod* m, uint16_t vreg, VRegKind kind, |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 263 | uint32_t* val) const { |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 264 | DCHECK_EQ(m, GetMethod()); |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 265 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 266 | 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] | 267 | // its instructions? |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 268 | uint16_t number_of_dex_registers = code_item->registers_size_; |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 269 | DCHECK_LT(vreg, code_item->registers_size_); |
| 270 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 271 | ArtMethod* outer_method = *GetCurrentQuickFrame(); |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 272 | const void* code_pointer = outer_method->GetQuickOatCodePointer(sizeof(void*)); |
| 273 | DCHECK(code_pointer != nullptr); |
| 274 | CodeInfo code_info = outer_method->GetOptimizedCodeInfo(); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 275 | StackMapEncoding encoding = code_info.ExtractEncoding(); |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 276 | |
| 277 | uint32_t native_pc_offset = outer_method->NativeQuickPcOffset(cur_quick_frame_pc_); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 278 | StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding); |
Nicolas Geoffray | e12997f | 2015-05-22 14:01:33 +0100 | [diff] [blame] | 279 | DCHECK(stack_map.IsValid()); |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 280 | size_t depth_in_stack_map = current_inlining_depth_ - 1; |
| 281 | |
| 282 | DexRegisterMap dex_register_map = IsInInlinedFrame() |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 283 | ? code_info.GetDexRegisterMapAtDepth(depth_in_stack_map, |
| 284 | code_info.GetInlineInfoOf(stack_map, encoding), |
| 285 | encoding, |
| 286 | number_of_dex_registers) |
| 287 | : code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers); |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 288 | |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 289 | DexRegisterLocation::Kind location_kind = |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 290 | dex_register_map.GetLocationKind(vreg, number_of_dex_registers, code_info, encoding); |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 291 | switch (location_kind) { |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 292 | case DexRegisterLocation::Kind::kInStack: { |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 293 | const int32_t offset = dex_register_map.GetStackOffsetInBytes(vreg, |
| 294 | number_of_dex_registers, |
| 295 | code_info, |
| 296 | encoding); |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 297 | const uint8_t* addr = reinterpret_cast<const uint8_t*>(cur_quick_frame_) + offset; |
| 298 | *val = *reinterpret_cast<const uint32_t*>(addr); |
| 299 | return true; |
| 300 | } |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 301 | case DexRegisterLocation::Kind::kInRegister: |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 302 | case DexRegisterLocation::Kind::kInRegisterHigh: |
| 303 | case DexRegisterLocation::Kind::kInFpuRegister: |
| 304 | case DexRegisterLocation::Kind::kInFpuRegisterHigh: { |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 305 | uint32_t reg = |
| 306 | dex_register_map.GetMachineRegister(vreg, number_of_dex_registers, code_info, encoding); |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 307 | return GetRegisterIfAccessible(reg, kind, val); |
| 308 | } |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 309 | case DexRegisterLocation::Kind::kConstant: |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 310 | *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] | 311 | return true; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 312 | case DexRegisterLocation::Kind::kNone: |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 313 | return false; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 314 | default: |
| 315 | LOG(FATAL) |
| 316 | << "Unexpected location kind" |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 317 | << DexRegisterLocation::PrettyDescriptor( |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 318 | dex_register_map.GetLocationInternalKind(vreg, |
| 319 | number_of_dex_registers, |
| 320 | code_info, |
| 321 | encoding)); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 322 | UNREACHABLE(); |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 323 | } |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | bool StackVisitor::GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const { |
| 327 | const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg); |
| 328 | if (!IsAccessibleRegister(reg, is_float)) { |
| 329 | return false; |
| 330 | } |
| 331 | uintptr_t ptr_val = GetRegister(reg, is_float); |
| 332 | const bool target64 = Is64BitInstructionSet(kRuntimeISA); |
| 333 | if (target64) { |
| 334 | const bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg); |
| 335 | const bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg); |
| 336 | int64_t value_long = static_cast<int64_t>(ptr_val); |
| 337 | if (wide_lo) { |
| 338 | ptr_val = static_cast<uintptr_t>(Low32Bits(value_long)); |
| 339 | } else if (wide_hi) { |
| 340 | ptr_val = static_cast<uintptr_t>(High32Bits(value_long)); |
| 341 | } |
| 342 | } |
| 343 | *val = ptr_val; |
| 344 | return true; |
| 345 | } |
| 346 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 347 | bool StackVisitor::GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo, |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 348 | VRegKind kind_hi, uint64_t* val) const { |
| 349 | if (kind_lo == kLongLoVReg) { |
| 350 | DCHECK_EQ(kind_hi, kLongHiVReg); |
| 351 | } else if (kind_lo == kDoubleLoVReg) { |
| 352 | DCHECK_EQ(kind_hi, kDoubleHiVReg); |
| 353 | } else { |
| 354 | 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] | 355 | UNREACHABLE(); |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 356 | } |
| 357 | if (cur_quick_frame_ != nullptr) { |
| 358 | DCHECK(context_ != nullptr); // You can't reliably read registers without a context. |
| 359 | DCHECK(m == GetMethod()); |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 360 | if (m->IsOptimized(sizeof(void*))) { |
| 361 | return GetVRegPairFromOptimizedCode(m, vreg, kind_lo, kind_hi, val); |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 362 | } else { |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 363 | return GetVRegPairFromQuickCode(m, vreg, kind_lo, kind_hi, val); |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 364 | } |
| 365 | } else { |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 366 | DCHECK(cur_shadow_frame_ != nullptr); |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 367 | *val = cur_shadow_frame_->GetVRegLong(vreg); |
| 368 | return true; |
| 369 | } |
| 370 | } |
| 371 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 372 | bool StackVisitor::GetVRegPairFromQuickCode(ArtMethod* m, uint16_t vreg, VRegKind kind_lo, |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 373 | VRegKind kind_hi, uint64_t* val) const { |
| 374 | const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*)); |
| 375 | DCHECK(code_pointer != nullptr); |
| 376 | const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*))); |
| 377 | QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer); |
| 378 | uint32_t vmap_offset_lo, vmap_offset_hi; |
| 379 | // TODO: IsInContext stops before spotting floating point registers. |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 380 | if (vmap_table.IsInContext(vreg, kind_lo, &vmap_offset_lo) && |
| 381 | vmap_table.IsInContext(vreg + 1, kind_hi, &vmap_offset_hi)) { |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 382 | bool is_float = (kind_lo == kDoubleLoVReg); |
| 383 | uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask(); |
| 384 | uint32_t reg_lo = vmap_table.ComputeRegister(spill_mask, vmap_offset_lo, kind_lo); |
| 385 | uint32_t reg_hi = vmap_table.ComputeRegister(spill_mask, vmap_offset_hi, kind_hi); |
| 386 | return GetRegisterPairIfAccessible(reg_lo, reg_hi, kind_lo, val); |
| 387 | } else { |
| 388 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 389 | 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] | 390 | // its instructions? |
Nicolas Geoffray | 15b9d52 | 2015-03-12 15:05:13 +0000 | [diff] [blame] | 391 | uint32_t* addr = GetVRegAddrFromQuickCode( |
| 392 | cur_quick_frame_, code_item, frame_info.CoreSpillMask(), |
| 393 | frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg); |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 394 | *val = *reinterpret_cast<uint64_t*>(addr); |
| 395 | return true; |
| 396 | } |
| 397 | } |
| 398 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 399 | bool StackVisitor::GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg, |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 400 | VRegKind kind_lo, VRegKind kind_hi, |
| 401 | uint64_t* val) const { |
| 402 | uint32_t low_32bits; |
| 403 | uint32_t high_32bits; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 404 | bool success = GetVRegFromOptimizedCode(m, vreg, kind_lo, &low_32bits); |
| 405 | success &= GetVRegFromOptimizedCode(m, vreg + 1, kind_hi, &high_32bits); |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 406 | if (success) { |
| 407 | *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits); |
| 408 | } |
| 409 | return success; |
| 410 | } |
| 411 | |
| 412 | bool StackVisitor::GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi, |
| 413 | VRegKind kind_lo, uint64_t* val) const { |
| 414 | const bool is_float = (kind_lo == kDoubleLoVReg); |
| 415 | if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) { |
| 416 | return false; |
| 417 | } |
| 418 | uintptr_t ptr_val_lo = GetRegister(reg_lo, is_float); |
| 419 | uintptr_t ptr_val_hi = GetRegister(reg_hi, is_float); |
| 420 | bool target64 = Is64BitInstructionSet(kRuntimeISA); |
| 421 | if (target64) { |
| 422 | int64_t value_long_lo = static_cast<int64_t>(ptr_val_lo); |
| 423 | int64_t value_long_hi = static_cast<int64_t>(ptr_val_hi); |
| 424 | ptr_val_lo = static_cast<uintptr_t>(Low32Bits(value_long_lo)); |
| 425 | ptr_val_hi = static_cast<uintptr_t>(High32Bits(value_long_hi)); |
| 426 | } |
| 427 | *val = (static_cast<uint64_t>(ptr_val_hi) << 32) | static_cast<uint32_t>(ptr_val_lo); |
| 428 | return true; |
| 429 | } |
| 430 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 431 | bool StackVisitor::SetVReg(ArtMethod* m, uint16_t vreg, uint32_t new_value, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 432 | VRegKind kind) { |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 433 | if (cur_quick_frame_ != nullptr) { |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 434 | DCHECK(context_ != nullptr); // You can't reliably write registers without a context. |
| 435 | DCHECK(m == GetMethod()); |
| 436 | if (m->IsOptimized(sizeof(void*))) { |
Nicolas Geoffray | 7cc56a1 | 2015-04-24 14:58:19 +0100 | [diff] [blame] | 437 | return false; |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 438 | } else { |
| 439 | return SetVRegFromQuickCode(m, vreg, new_value, kind); |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 440 | } |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 441 | } else { |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 442 | cur_shadow_frame_->SetVReg(vreg, new_value); |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 443 | return true; |
Ian Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 444 | } |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 445 | } |
| 446 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 447 | bool StackVisitor::SetVRegFromQuickCode(ArtMethod* m, uint16_t vreg, uint32_t new_value, |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 448 | VRegKind kind) { |
| 449 | DCHECK(context_ != nullptr); // You can't reliably write registers without a context. |
| 450 | DCHECK(m == GetMethod()); |
| 451 | const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*)); |
| 452 | DCHECK(code_pointer != nullptr); |
| 453 | const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*))); |
| 454 | QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer); |
| 455 | uint32_t vmap_offset; |
| 456 | // TODO: IsInContext stops before spotting floating point registers. |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 457 | if (vmap_table.IsInContext(vreg, kind, &vmap_offset)) { |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 458 | bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg); |
| 459 | uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask(); |
| 460 | uint32_t reg = vmap_table.ComputeRegister(spill_mask, vmap_offset, kind); |
| 461 | return SetRegisterIfAccessible(reg, new_value, kind); |
Ian Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 462 | } else { |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 463 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 464 | 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] | 465 | // its instructions? |
Nicolas Geoffray | 15b9d52 | 2015-03-12 15:05:13 +0000 | [diff] [blame] | 466 | uint32_t* addr = GetVRegAddrFromQuickCode( |
| 467 | cur_quick_frame_, code_item, frame_info.CoreSpillMask(), |
| 468 | frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg); |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 469 | *addr = new_value; |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 470 | return true; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 471 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 474 | bool StackVisitor::SetRegisterIfAccessible(uint32_t reg, uint32_t new_value, VRegKind kind) { |
| 475 | const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg); |
| 476 | if (!IsAccessibleRegister(reg, is_float)) { |
| 477 | return false; |
| 478 | } |
| 479 | const bool target64 = Is64BitInstructionSet(kRuntimeISA); |
| 480 | |
| 481 | // Create a new value that can hold both low 32 and high 32 bits, in |
| 482 | // case we are running 64 bits. |
| 483 | uintptr_t full_new_value = new_value; |
| 484 | // Deal with 32 or 64-bit wide registers in a way that builds on all targets. |
| 485 | if (target64) { |
| 486 | bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg); |
| 487 | bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg); |
| 488 | if (wide_lo || wide_hi) { |
| 489 | uintptr_t old_reg_val = GetRegister(reg, is_float); |
| 490 | uint64_t new_vreg_portion = static_cast<uint64_t>(new_value); |
| 491 | uint64_t old_reg_val_as_wide = static_cast<uint64_t>(old_reg_val); |
| 492 | uint64_t mask = 0xffffffff; |
| 493 | if (wide_lo) { |
| 494 | mask = mask << 32; |
| 495 | } else { |
| 496 | new_vreg_portion = new_vreg_portion << 32; |
| 497 | } |
| 498 | full_new_value = static_cast<uintptr_t>((old_reg_val_as_wide & mask) | new_vreg_portion); |
| 499 | } |
| 500 | } |
| 501 | SetRegister(reg, full_new_value, is_float); |
| 502 | return true; |
| 503 | } |
| 504 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 505 | bool StackVisitor::SetVRegPair(ArtMethod* m, uint16_t vreg, uint64_t new_value, |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 506 | VRegKind kind_lo, VRegKind kind_hi) { |
| 507 | if (kind_lo == kLongLoVReg) { |
| 508 | DCHECK_EQ(kind_hi, kLongHiVReg); |
| 509 | } else if (kind_lo == kDoubleLoVReg) { |
| 510 | DCHECK_EQ(kind_hi, kDoubleHiVReg); |
| 511 | } else { |
| 512 | LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi; |
| 513 | } |
| 514 | if (cur_quick_frame_ != nullptr) { |
| 515 | DCHECK(context_ != nullptr); // You can't reliably write registers without a context. |
| 516 | DCHECK(m == GetMethod()); |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 517 | if (m->IsOptimized(sizeof(void*))) { |
Nicolas Geoffray | 7cc56a1 | 2015-04-24 14:58:19 +0100 | [diff] [blame] | 518 | return false; |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 519 | } else { |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 520 | return SetVRegPairFromQuickCode(m, vreg, new_value, kind_lo, kind_hi); |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 521 | } |
| 522 | } else { |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 523 | DCHECK(cur_shadow_frame_ != nullptr); |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 524 | cur_shadow_frame_->SetVRegLong(vreg, new_value); |
| 525 | return true; |
| 526 | } |
| 527 | } |
| 528 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 529 | bool StackVisitor::SetVRegPairFromQuickCode( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 530 | ArtMethod* m, uint16_t vreg, uint64_t new_value, VRegKind kind_lo, VRegKind kind_hi) { |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 531 | const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*)); |
| 532 | DCHECK(code_pointer != nullptr); |
| 533 | const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*))); |
| 534 | QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer); |
| 535 | uint32_t vmap_offset_lo, vmap_offset_hi; |
| 536 | // TODO: IsInContext stops before spotting floating point registers. |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 537 | if (vmap_table.IsInContext(vreg, kind_lo, &vmap_offset_lo) && |
| 538 | vmap_table.IsInContext(vreg + 1, kind_hi, &vmap_offset_hi)) { |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 539 | bool is_float = (kind_lo == kDoubleLoVReg); |
| 540 | uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask(); |
| 541 | uint32_t reg_lo = vmap_table.ComputeRegister(spill_mask, vmap_offset_lo, kind_lo); |
| 542 | uint32_t reg_hi = vmap_table.ComputeRegister(spill_mask, vmap_offset_hi, kind_hi); |
| 543 | return SetRegisterPairIfAccessible(reg_lo, reg_hi, new_value, is_float); |
| 544 | } else { |
| 545 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 546 | 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] | 547 | // its instructions? |
Nicolas Geoffray | 15b9d52 | 2015-03-12 15:05:13 +0000 | [diff] [blame] | 548 | uint32_t* addr = GetVRegAddrFromQuickCode( |
| 549 | cur_quick_frame_, code_item, frame_info.CoreSpillMask(), |
| 550 | frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg); |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 551 | *reinterpret_cast<uint64_t*>(addr) = new_value; |
| 552 | return true; |
| 553 | } |
| 554 | } |
| 555 | |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 556 | bool StackVisitor::SetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi, |
| 557 | uint64_t new_value, bool is_float) { |
| 558 | if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) { |
| 559 | return false; |
| 560 | } |
| 561 | uintptr_t new_value_lo = static_cast<uintptr_t>(new_value & 0xFFFFFFFF); |
| 562 | uintptr_t new_value_hi = static_cast<uintptr_t>(new_value >> 32); |
| 563 | bool target64 = Is64BitInstructionSet(kRuntimeISA); |
| 564 | // Deal with 32 or 64-bit wide registers in a way that builds on all targets. |
| 565 | if (target64) { |
| 566 | DCHECK_EQ(reg_lo, reg_hi); |
| 567 | SetRegister(reg_lo, new_value, is_float); |
| 568 | } else { |
| 569 | SetRegister(reg_lo, new_value_lo, is_float); |
| 570 | SetRegister(reg_hi, new_value_hi, is_float); |
| 571 | } |
| 572 | return true; |
| 573 | } |
| 574 | |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 575 | bool StackVisitor::IsAccessibleGPR(uint32_t reg) const { |
| 576 | DCHECK(context_ != nullptr); |
| 577 | return context_->IsAccessibleGPR(reg); |
| 578 | } |
| 579 | |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 580 | uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const { |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 581 | DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine"; |
| 582 | DCHECK(context_ != nullptr); |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 583 | return context_->GetGPRAddress(reg); |
| 584 | } |
| 585 | |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 586 | uintptr_t StackVisitor::GetGPR(uint32_t reg) const { |
| 587 | DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine"; |
| 588 | DCHECK(context_ != nullptr); |
| 589 | return context_->GetGPR(reg); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 590 | } |
| 591 | |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 592 | void StackVisitor::SetGPR(uint32_t reg, uintptr_t value) { |
| 593 | DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine"; |
| 594 | DCHECK(context_ != nullptr); |
| 595 | context_->SetGPR(reg, value); |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 596 | } |
| 597 | |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 598 | bool StackVisitor::IsAccessibleFPR(uint32_t reg) const { |
| 599 | DCHECK(context_ != nullptr); |
| 600 | return context_->IsAccessibleFPR(reg); |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 601 | } |
| 602 | |
Sebastien Hertz | 96ba8dc | 2015-01-22 18:57:14 +0100 | [diff] [blame] | 603 | uintptr_t StackVisitor::GetFPR(uint32_t reg) const { |
| 604 | DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine"; |
| 605 | DCHECK(context_ != nullptr); |
| 606 | return context_->GetFPR(reg); |
| 607 | } |
| 608 | |
| 609 | void StackVisitor::SetFPR(uint32_t reg, uintptr_t value) { |
| 610 | DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine"; |
| 611 | DCHECK(context_ != nullptr); |
| 612 | context_->SetFPR(reg, value); |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 613 | } |
| 614 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 615 | uintptr_t StackVisitor::GetReturnPc() const { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 616 | uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 617 | DCHECK(sp != nullptr); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 618 | uint8_t* pc_addr = sp + GetMethod()->GetReturnPcOffset().SizeValue(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 619 | return *reinterpret_cast<uintptr_t*>(pc_addr); |
| 620 | } |
| 621 | |
| 622 | void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 623 | uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 624 | CHECK(sp != nullptr); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 625 | uint8_t* pc_addr = sp + GetMethod()->GetReturnPcOffset().SizeValue(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 626 | *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc; |
| 627 | } |
| 628 | |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 629 | size_t StackVisitor::ComputeNumFrames(Thread* thread, StackWalkKind walk_kind) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 630 | struct NumFramesVisitor : public StackVisitor { |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 631 | NumFramesVisitor(Thread* thread_in, StackWalkKind walk_kind_in) |
| 632 | : StackVisitor(thread_in, nullptr, walk_kind_in), frames(0) {} |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 633 | |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 634 | bool VisitFrame() OVERRIDE { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 635 | frames++; |
| 636 | return true; |
| 637 | } |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 638 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 639 | size_t frames; |
| 640 | }; |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 641 | NumFramesVisitor visitor(thread, walk_kind); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 642 | visitor.WalkStack(true); |
| 643 | return visitor.frames; |
| 644 | } |
| 645 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 646 | bool StackVisitor::GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc) { |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 647 | struct HasMoreFramesVisitor : public StackVisitor { |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 648 | HasMoreFramesVisitor(Thread* thread, |
| 649 | StackWalkKind walk_kind, |
| 650 | size_t num_frames, |
| 651 | size_t frame_height) |
| 652 | : StackVisitor(thread, nullptr, walk_kind, num_frames), |
| 653 | frame_height_(frame_height), |
| 654 | found_frame_(false), |
| 655 | has_more_frames_(false), |
| 656 | next_method_(nullptr), |
| 657 | next_dex_pc_(0) { |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 660 | bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 661 | if (found_frame_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 662 | ArtMethod* method = GetMethod(); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 663 | if (method != nullptr && !method->IsRuntimeMethod()) { |
| 664 | has_more_frames_ = true; |
| 665 | next_method_ = method; |
| 666 | next_dex_pc_ = GetDexPc(); |
| 667 | return false; // End stack walk once next method is found. |
| 668 | } |
| 669 | } else if (GetFrameHeight() == frame_height_) { |
| 670 | found_frame_ = true; |
| 671 | } |
| 672 | return true; |
| 673 | } |
| 674 | |
| 675 | size_t frame_height_; |
| 676 | bool found_frame_; |
| 677 | bool has_more_frames_; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 678 | ArtMethod* next_method_; |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 679 | uint32_t next_dex_pc_; |
| 680 | }; |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 681 | HasMoreFramesVisitor visitor(thread_, walk_kind_, GetNumFrames(), GetFrameHeight()); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 682 | visitor.WalkStack(true); |
| 683 | *next_method = visitor.next_method_; |
| 684 | *next_dex_pc = visitor.next_dex_pc_; |
| 685 | return visitor.has_more_frames_; |
| 686 | } |
| 687 | |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 688 | void StackVisitor::DescribeStack(Thread* thread) { |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 689 | struct DescribeStackVisitor : public StackVisitor { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 690 | explicit DescribeStackVisitor(Thread* thread_in) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 691 | : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {} |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 692 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 693 | bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 694 | LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation(); |
| 695 | return true; |
| 696 | } |
| 697 | }; |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 698 | DescribeStackVisitor visitor(thread); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 699 | visitor.WalkStack(true); |
| 700 | } |
| 701 | |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame] | 702 | std::string StackVisitor::DescribeLocation() const { |
| 703 | std::string result("Visiting method '"); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 704 | ArtMethod* m = GetMethod(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 705 | if (m == nullptr) { |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 706 | return "upcall"; |
| 707 | } |
| 708 | result += PrettyMethod(m); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 709 | result += StringPrintf("' at dex PC 0x%04x", GetDexPc()); |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame] | 710 | if (!IsShadowFrame()) { |
| 711 | result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc())); |
| 712 | } |
| 713 | return result; |
| 714 | } |
| 715 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 716 | static instrumentation::InstrumentationStackFrame& GetInstrumentationStackFrame(Thread* thread, |
| 717 | uint32_t depth) { |
| 718 | CHECK_LT(depth, thread->GetInstrumentationStack()->size()); |
| 719 | return thread->GetInstrumentationStack()->at(depth); |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 720 | } |
| 721 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 722 | void StackVisitor::SanityCheckFrame() const { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 723 | if (kIsDebugBuild) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 724 | ArtMethod* method = GetMethod(); |
| 725 | auto* declaring_class = method->GetDeclaringClass(); |
| 726 | // Runtime methods have null declaring class. |
| 727 | if (!method->IsRuntimeMethod()) { |
| 728 | CHECK(declaring_class != nullptr); |
| 729 | CHECK_EQ(declaring_class->GetClass(), declaring_class->GetClass()->GetClass()) |
| 730 | << declaring_class; |
| 731 | } else { |
| 732 | CHECK(declaring_class == nullptr); |
| 733 | } |
| 734 | auto* runtime = Runtime::Current(); |
| 735 | auto* la = runtime->GetLinearAlloc(); |
| 736 | if (!la->Contains(method)) { |
| 737 | // Check image space. |
| 738 | bool in_image = false; |
| 739 | for (auto& space : runtime->GetHeap()->GetContinuousSpaces()) { |
| 740 | if (space->IsImageSpace()) { |
| 741 | auto* image_space = space->AsImageSpace(); |
| 742 | const auto& header = image_space->GetImageHeader(); |
| 743 | const auto* methods = &header.GetMethodsSection(); |
| 744 | if (methods->Contains(reinterpret_cast<const uint8_t*>(method) - image_space->Begin())) { |
| 745 | in_image = true; |
| 746 | break; |
| 747 | } |
| 748 | } |
| 749 | } |
| 750 | CHECK(in_image) << PrettyMethod(method) << " not in linear alloc or image"; |
| 751 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 752 | if (cur_quick_frame_ != nullptr) { |
| 753 | method->AssertPcIsWithinQuickCode(cur_quick_frame_pc_); |
| 754 | // Frame sanity. |
| 755 | size_t frame_size = method->GetFrameSizeInBytes(); |
| 756 | CHECK_NE(frame_size, 0u); |
Andreas Gampe | 5b417b9 | 2014-03-10 14:18:35 -0700 | [diff] [blame] | 757 | // A rough guess at an upper size we expect to see for a frame. |
| 758 | // 256 registers |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 759 | // 2 words HandleScope overhead |
Andreas Gampe | 5b417b9 | 2014-03-10 14:18:35 -0700 | [diff] [blame] | 760 | // 3+3 register spills |
| 761 | // TODO: this seems architecture specific for the case of JNI frames. |
Brian Carlstrom | ed08bd4 | 2014-03-19 18:34:17 -0700 | [diff] [blame] | 762 | // TODO: 083-compiler-regressions ManyFloatArgs shows this estimate is wrong. |
| 763 | // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word); |
| 764 | const size_t kMaxExpectedFrameSize = 2 * KB; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 765 | CHECK_LE(frame_size, kMaxExpectedFrameSize); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 766 | size_t return_pc_offset = method->GetReturnPcOffset().SizeValue(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 767 | CHECK_LT(return_pc_offset, frame_size); |
| 768 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 769 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | void StackVisitor::WalkStack(bool include_transitions) { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 773 | DCHECK(thread_ == Thread::Current() || thread_->IsSuspended()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 774 | CHECK_EQ(cur_depth_, 0U); |
| 775 | bool exit_stubs_installed = Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled(); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 776 | uint32_t instrumentation_stack_depth = 0; |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 777 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 778 | for (const ManagedStack* current_fragment = thread_->GetManagedStack(); |
| 779 | current_fragment != nullptr; current_fragment = current_fragment->GetLink()) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 780 | cur_shadow_frame_ = current_fragment->GetTopShadowFrame(); |
| 781 | cur_quick_frame_ = current_fragment->GetTopQuickFrame(); |
Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 782 | cur_quick_frame_pc_ = 0; |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 783 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 784 | if (cur_quick_frame_ != nullptr) { // Handle quick stack frames. |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 785 | // Can't be both a shadow and a quick fragment. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 786 | DCHECK(current_fragment->GetTopShadowFrame() == nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 787 | ArtMethod* method = *cur_quick_frame_; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 788 | while (method != nullptr) { |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 789 | SanityCheckFrame(); |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 790 | |
| 791 | if ((walk_kind_ == StackWalkKind::kIncludeInlinedFrames) |
| 792 | && method->IsOptimized(sizeof(void*))) { |
| 793 | CodeInfo code_info = method->GetOptimizedCodeInfo(); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 794 | StackMapEncoding encoding = code_info.ExtractEncoding(); |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 795 | uint32_t native_pc_offset = method->NativeQuickPcOffset(cur_quick_frame_pc_); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 796 | StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding); |
| 797 | if (stack_map.IsValid() && stack_map.HasInlineInfo(encoding)) { |
| 798 | InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding); |
Nicolas Geoffray | 57f6161 | 2015-05-15 13:20:41 +0100 | [diff] [blame] | 799 | DCHECK_EQ(current_inlining_depth_, 0u); |
| 800 | for (current_inlining_depth_ = inline_info.GetDepth(); |
| 801 | current_inlining_depth_ != 0; |
| 802 | --current_inlining_depth_) { |
| 803 | bool should_continue = VisitFrame(); |
| 804 | if (UNLIKELY(!should_continue)) { |
| 805 | return; |
| 806 | } |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 807 | cur_depth_++; |
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 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 817 | if (context_ != nullptr) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 818 | context_->FillCalleeSaves(*this); |
| 819 | } |
| 820 | size_t frame_size = method->GetFrameSizeInBytes(); |
| 821 | // Compute PC for next stack frame from return PC. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 822 | size_t return_pc_offset = method->GetReturnPcOffset(frame_size).SizeValue(); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 823 | 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] | 824 | uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 825 | if (UNLIKELY(exit_stubs_installed)) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 826 | // While profiling, the return pc is restored from the side stack, except when walking |
| 827 | // 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] | 828 | if (reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc) { |
Sebastien Hertz | 74e256b | 2013-10-04 10:40:37 +0200 | [diff] [blame] | 829 | const instrumentation::InstrumentationStackFrame& instrumentation_frame = |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 830 | GetInstrumentationStackFrame(thread_, instrumentation_stack_depth); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 831 | instrumentation_stack_depth++; |
Jeff Hao | fb2802d | 2013-07-24 13:53:05 -0700 | [diff] [blame] | 832 | if (GetMethod() == Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveAll)) { |
| 833 | // Skip runtime save all callee frames which are used to deliver exceptions. |
| 834 | } else if (instrumentation_frame.interpreter_entry_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 835 | ArtMethod* callee = Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs); |
Jeff Hao | fb2802d | 2013-07-24 13:53:05 -0700 | [diff] [blame] | 836 | CHECK_EQ(GetMethod(), callee) << "Expected: " << PrettyMethod(callee) << " Found: " |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 837 | << PrettyMethod(GetMethod()); |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 838 | } else if (instrumentation_frame.method_ != GetMethod()) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 839 | LOG(FATAL) << "Expected: " << PrettyMethod(instrumentation_frame.method_) |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 840 | << " Found: " << PrettyMethod(GetMethod()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 841 | } |
| 842 | if (num_frames_ != 0) { |
| 843 | // Check agreement of frame Ids only if num_frames_ is computed to avoid infinite |
| 844 | // recursion. |
| 845 | CHECK(instrumentation_frame.frame_id_ == GetFrameId()) |
| 846 | << "Expected: " << instrumentation_frame.frame_id_ |
| 847 | << " Found: " << GetFrameId(); |
| 848 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 849 | return_pc = instrumentation_frame.return_pc_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 850 | } |
| 851 | } |
| 852 | cur_quick_frame_pc_ = return_pc; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 853 | 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] | 854 | cur_quick_frame_ = reinterpret_cast<ArtMethod**>(next_frame); |
| 855 | |
| 856 | if (kDebugStackWalk) { |
| 857 | LOG(INFO) << PrettyMethod(method) << "@" << method << " size=" << frame_size |
| 858 | << " optimized=" << method->IsOptimized(sizeof(void*)) |
| 859 | << " native=" << method->IsNative() |
| 860 | << " entrypoints=" << method->GetEntryPointFromQuickCompiledCode() |
| 861 | << "," << method->GetEntryPointFromJni() |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 862 | << " next=" << *cur_quick_frame_; |
| 863 | } |
| 864 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 865 | cur_depth_++; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 866 | method = *cur_quick_frame_; |
jeffhao | 6641ea1 | 2013-01-02 18:13:42 -0800 | [diff] [blame] | 867 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 868 | } else if (cur_shadow_frame_ != nullptr) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 869 | do { |
| 870 | SanityCheckFrame(); |
| 871 | bool should_continue = VisitFrame(); |
| 872 | if (UNLIKELY(!should_continue)) { |
| 873 | return; |
| 874 | } |
| 875 | cur_depth_++; |
| 876 | cur_shadow_frame_ = cur_shadow_frame_->GetLink(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 877 | } while (cur_shadow_frame_ != nullptr); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 878 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 879 | if (include_transitions) { |
| 880 | bool should_continue = VisitFrame(); |
| 881 | if (!should_continue) { |
| 882 | return; |
| 883 | } |
| 884 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 885 | cur_depth_++; |
| 886 | } |
| 887 | if (num_frames_ != 0) { |
| 888 | CHECK_EQ(cur_depth_, num_frames_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 889 | } |
| 890 | } |
| 891 | |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 892 | void JavaFrameRootInfo::Describe(std::ostream& os) const { |
| 893 | const StackVisitor* visitor = stack_visitor_; |
| 894 | CHECK(visitor != nullptr); |
| 895 | os << "Type=" << GetType() << " thread_id=" << GetThreadId() << " location=" << |
| 896 | visitor->DescribeLocation() << " vreg=" << vreg_; |
| 897 | } |
| 898 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 899 | int StackVisitor::GetVRegOffsetFromQuickCode(const DexFile::CodeItem* code_item, |
| 900 | uint32_t core_spills, uint32_t fp_spills, |
| 901 | size_t frame_size, int reg, InstructionSet isa) { |
| 902 | size_t pointer_size = InstructionSetPointerSize(isa); |
| 903 | if (kIsDebugBuild) { |
| 904 | auto* runtime = Runtime::Current(); |
| 905 | if (runtime != nullptr) { |
| 906 | CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size); |
| 907 | } |
| 908 | } |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 909 | DCHECK_ALIGNED(frame_size, kStackAlignment); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 910 | DCHECK_NE(reg, -1); |
| 911 | int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa) |
| 912 | + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa) |
| 913 | + sizeof(uint32_t); // Filler. |
| 914 | int num_regs = code_item->registers_size_ - code_item->ins_size_; |
| 915 | int temp_threshold = code_item->registers_size_; |
| 916 | const int max_num_special_temps = 1; |
| 917 | if (reg == temp_threshold) { |
| 918 | // The current method pointer corresponds to special location on stack. |
| 919 | return 0; |
| 920 | } else if (reg >= temp_threshold + max_num_special_temps) { |
| 921 | /* |
| 922 | * Special temporaries may have custom locations and the logic above deals with that. |
| 923 | * However, non-special temporaries are placed relative to the outs. |
| 924 | */ |
| 925 | int temps_start = code_item->outs_size_ * sizeof(uint32_t) + pointer_size /* art method */; |
| 926 | int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t); |
| 927 | return temps_start + relative_offset; |
| 928 | } else if (reg < num_regs) { |
| 929 | int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t); |
| 930 | return locals_start + (reg * sizeof(uint32_t)); |
| 931 | } else { |
| 932 | // Handle ins. |
| 933 | return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + pointer_size /* art method */; |
| 934 | } |
| 935 | } |
| 936 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 937 | } // namespace art |