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