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