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