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