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