blob: f9efc0b88fe27841fd984c704e058f8165c2152b [file] [log] [blame]
Elliott Hughes68e76522011-10-05 13:22:16 -07001/*
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 Gampe46ee31b2016-12-14 10:11:49 -080019#include "android-base/stringprintf.h"
20
Ian Rogerse63db272014-07-15 15:36:11 -070021#include "arch/context.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070023#include "base/enums.h"
Dave Allisonf9439142014-03-27 15:10:22 -070024#include "base/hex_dump.h"
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010025#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070026#include "entrypoints/runtime_asm_entrypoints.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070027#include "gc/space/image_space.h"
28#include "gc/space/space-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010029#include "jit/jit.h"
30#include "jit/jit_code_cache.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070031#include "linear_alloc.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070032#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "mirror/object-inl.h"
34#include "mirror/object_array-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010035#include "oat_quick_method_header.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010036#include "quick/quick_method_frame_info.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070037#include "runtime.h"
Dave Allisonf9439142014-03-27 15:10:22 -070038#include "thread.h"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070039#include "thread_list.h"
Mathieu Chartier4e305412014-02-19 10:54:44 -080040#include "verify_object-inl.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070041
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080042namespace art {
43
Andreas Gampe46ee31b2016-12-14 10:11:49 -080044using android::base::StringPrintf;
45
Mathieu Chartier8405bfd2016-02-05 12:00:49 -080046static constexpr bool kDebugStackWalk = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -070047
Ian Rogers62d6c772013-02-27 08:32:07 -080048mirror::Object* ShadowFrame::GetThisObject() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -070049 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -080050 if (m->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070051 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -080052 } else if (m->IsNative()) {
53 return GetVRegReference(0);
54 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070055 const DexFile::CodeItem* code_item = m->GetCodeItem();
David Sehr709b0702016-10-13 09:12:37 -070056 CHECK(code_item != nullptr) << ArtMethod::PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -080057 uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
58 return GetVRegReference(reg);
59 }
60}
61
Jeff Haoe701f482013-05-24 11:50:49 -070062mirror::Object* ShadowFrame::GetThisObject(uint16_t num_ins) const {
Mathieu Chartiere401d142015-04-22 13:56:20 -070063 ArtMethod* m = GetMethod();
Jeff Haoe701f482013-05-24 11:50:49 -070064 if (m->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070065 return nullptr;
Jeff Haoe701f482013-05-24 11:50:49 -070066 } else {
Jeff Hao8d448852013-06-03 17:26:19 -070067 return GetVRegReference(NumberOfVRegs() - num_ins);
Jeff Haoe701f482013-05-24 11:50:49 -070068 }
69}
70
TDYa127ce4cc0d2012-11-18 16:59:53 -080071size_t ManagedStack::NumJniShadowFrameReferences() const {
Ian Rogers0399dde2012-06-06 17:09:28 -070072 size_t count = 0;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070073 for (const ManagedStack* current_fragment = this; current_fragment != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070074 current_fragment = current_fragment->GetLink()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070075 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070076 current_frame = current_frame->GetLink()) {
TDYa127ce4cc0d2012-11-18 16:59:53 -080077 if (current_frame->GetMethod()->IsNative()) {
78 // The JNI ShadowFrame only contains references. (For indirect reference.)
79 count += current_frame->NumberOfVRegs();
80 }
Ian Rogers0399dde2012-06-06 17:09:28 -070081 }
82 }
83 return count;
84}
85
Ian Rogersef7d42f2014-01-06 12:55:46 -080086bool ManagedStack::ShadowFramesContain(StackReference<mirror::Object>* shadow_frame_entry) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070087 for (const ManagedStack* current_fragment = this; current_fragment != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070088 current_fragment = current_fragment->GetLink()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070089 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070090 current_frame = current_frame->GetLink()) {
91 if (current_frame->Contains(shadow_frame_entry)) {
92 return true;
93 }
94 }
95 }
96 return false;
97}
98
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010099StackVisitor::StackVisitor(Thread* thread, Context* context, StackWalkKind walk_kind)
100 : StackVisitor(thread, context, walk_kind, 0) {}
Ian Rogers7a22fa62013-01-23 12:16:16 -0800101
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100102StackVisitor::StackVisitor(Thread* thread,
103 Context* context,
104 StackWalkKind walk_kind,
105 size_t num_frames)
106 : thread_(thread),
107 walk_kind_(walk_kind),
108 cur_shadow_frame_(nullptr),
109 cur_quick_frame_(nullptr),
110 cur_quick_frame_pc_(0),
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100111 cur_oat_quick_method_header_(nullptr),
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100112 num_frames_(num_frames),
113 cur_depth_(0),
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100114 current_inlining_depth_(0),
Ian Rogers5cf98192014-05-29 21:31:50 -0700115 context_(context) {
116 DCHECK(thread == Thread::Current() || thread->IsSuspended()) << *thread;
117}
118
Mathieu Chartiere401d142015-04-22 13:56:20 -0700119InlineInfo StackVisitor::GetCurrentInlineInfo() const {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100120 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
121 uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_);
122 CodeInfo code_info = method_header->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000123 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Brazdilf677ebf2015-05-29 16:29:43 +0100124 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100125 DCHECK(stack_map.IsValid());
David Brazdilf677ebf2015-05-29 16:29:43 +0100126 return code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100127}
128
Mathieu Chartiere401d142015-04-22 13:56:20 -0700129ArtMethod* StackVisitor::GetMethod() const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100130 if (cur_shadow_frame_ != nullptr) {
131 return cur_shadow_frame_->GetMethod();
132 } else if (cur_quick_frame_ != nullptr) {
133 if (IsInInlinedFrame()) {
134 size_t depth_in_stack_map = current_inlining_depth_ - 1;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100135 InlineInfo inline_info = GetCurrentInlineInfo();
David Srbecky61b28a12016-02-25 21:55:03 +0000136 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
137 CodeInfoEncoding encoding = method_header->GetOptimizedCodeInfo().ExtractEncoding();
Mathieu Chartier45bf2502016-03-31 11:07:09 -0700138 DCHECK(walk_kind_ != StackWalkKind::kSkipInlinedFrames);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100139 return GetResolvedMethod(*GetCurrentQuickFrame(),
140 inline_info,
141 encoding.inline_info_encoding,
142 depth_in_stack_map);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100143 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700144 return *cur_quick_frame_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100145 }
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100146 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700147 return nullptr;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100148}
149
Dave Allisonb373e092014-02-20 16:06:36 -0800150uint32_t StackVisitor::GetDexPc(bool abort_on_failure) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700151 if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700152 return cur_shadow_frame_->GetDexPC();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700153 } else if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100154 if (IsInInlinedFrame()) {
155 size_t depth_in_stack_map = current_inlining_depth_ - 1;
David Srbecky61b28a12016-02-25 21:55:03 +0000156 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
157 CodeInfoEncoding encoding = method_header->GetOptimizedCodeInfo().ExtractEncoding();
158 return GetCurrentInlineInfo().GetDexPcAtDepth(encoding.inline_info_encoding,
159 depth_in_stack_map);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100160 } else if (cur_oat_quick_method_header_ == nullptr) {
161 return DexFile::kDexNoIndex;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100162 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100163 return cur_oat_quick_method_header_->ToDexPc(
164 GetMethod(), cur_quick_frame_pc_, abort_on_failure);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100165 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700166 } else {
167 return 0;
168 }
169}
170
Mathieu Chartiere401d142015-04-22 13:56:20 -0700171extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700172 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertza836bc92014-11-25 16:30:53 +0100173
Ian Rogers62d6c772013-02-27 08:32:07 -0800174mirror::Object* StackVisitor::GetThisObject() const {
Andreas Gampe542451c2016-07-26 09:02:02 -0700175 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700176 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800177 if (m->IsStatic()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100178 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800179 } else if (m->IsNative()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100180 if (cur_quick_frame_ != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700181 HandleScope* hs = reinterpret_cast<HandleScope*>(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100182 reinterpret_cast<char*>(cur_quick_frame_) + sizeof(ArtMethod*));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700183 return hs->GetReference(0);
Ian Rogers62d6c772013-02-27 08:32:07 -0800184 } else {
185 return cur_shadow_frame_->GetVRegReference(0);
186 }
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000187 } else if (m->IsProxyMethod()) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100188 if (cur_quick_frame_ != nullptr) {
189 return artQuickGetProxyThisObject(cur_quick_frame_);
190 } else {
191 return cur_shadow_frame_->GetVRegReference(0);
192 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800193 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700194 const DexFile::CodeItem* code_item = m->GetCodeItem();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100195 if (code_item == nullptr) {
Ian Rogerse0dcd462014-03-08 15:21:04 -0800196 UNIMPLEMENTED(ERROR) << "Failed to determine this object of abstract or proxy method: "
David Sehr709b0702016-10-13 09:12:37 -0700197 << ArtMethod::PrettyMethod(m);
Ian Rogerse0dcd462014-03-08 15:21:04 -0800198 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800199 } else {
200 uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000201 uint32_t value = 0;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700202 bool success = GetVReg(m, reg, kReferenceVReg, &value);
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000203 // We currently always guarantee the `this` object is live throughout the method.
David Sehr709b0702016-10-13 09:12:37 -0700204 CHECK(success) << "Failed to read the this object in " << ArtMethod::PrettyMethod(m);
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000205 return reinterpret_cast<mirror::Object*>(value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800206 }
207 }
208}
209
Ian Rogers0c7abda2012-09-19 13:33:42 -0700210size_t StackVisitor::GetNativePcOffset() const {
211 DCHECK(!IsShadowFrame());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100212 return GetCurrentOatQuickMethodHeader()->NativeQuickPcOffset(cur_quick_frame_pc_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700213}
214
Mingyao Yang99170c62015-07-06 11:10:37 -0700215bool StackVisitor::GetVRegFromDebuggerShadowFrame(uint16_t vreg,
216 VRegKind kind,
217 uint32_t* val) const {
218 size_t frame_id = const_cast<StackVisitor*>(this)->GetFrameId();
219 ShadowFrame* shadow_frame = thread_->FindDebuggerShadowFrame(frame_id);
220 if (shadow_frame != nullptr) {
221 bool* updated_vreg_flags = thread_->GetUpdatedVRegFlags(frame_id);
222 DCHECK(updated_vreg_flags != nullptr);
223 if (updated_vreg_flags[vreg]) {
224 // Value is set by the debugger.
225 if (kind == kReferenceVReg) {
226 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
227 shadow_frame->GetVRegReference(vreg)));
228 } else {
229 *val = shadow_frame->GetVReg(vreg);
230 }
231 return true;
232 }
233 }
234 // No value is set by the debugger.
235 return false;
236}
237
Mathieu Chartiere401d142015-04-22 13:56:20 -0700238bool StackVisitor::GetVReg(ArtMethod* m, uint16_t vreg, VRegKind kind, uint32_t* val) const {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200239 if (cur_quick_frame_ != nullptr) {
240 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800241 DCHECK(m == GetMethod());
Mingyao Yang99170c62015-07-06 11:10:37 -0700242 // Check if there is value set by the debugger.
243 if (GetVRegFromDebuggerShadowFrame(vreg, kind, val)) {
244 return true;
245 }
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100246 DCHECK(cur_oat_quick_method_header_->IsOptimized());
247 return GetVRegFromOptimizedCode(m, vreg, kind, val);
Ian Rogers0399dde2012-06-06 17:09:28 -0700248 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100249 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertz09687442015-11-17 10:35:39 +0100250 if (kind == kReferenceVReg) {
251 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
252 cur_shadow_frame_->GetVRegReference(vreg)));
253 } else {
254 *val = cur_shadow_frame_->GetVReg(vreg);
255 }
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200256 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700257 }
258}
259
Mathieu Chartiere401d142015-04-22 13:56:20 -0700260bool StackVisitor::GetVRegFromOptimizedCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100261 uint32_t* val) const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100262 DCHECK_EQ(m, GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100263 const DexFile::CodeItem* code_item = m->GetCodeItem();
David Sehr709b0702016-10-13 09:12:37 -0700264 DCHECK(code_item != nullptr) << m->PrettyMethod(); // Can't be null or how would we compile
265 // its instructions?
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000266 uint16_t number_of_dex_registers = code_item->registers_size_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100267 DCHECK_LT(vreg, code_item->registers_size_);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100268 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
269 CodeInfo code_info = method_header->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000270 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100271
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100272 uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100273 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100274 DCHECK(stack_map.IsValid());
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100275 size_t depth_in_stack_map = current_inlining_depth_ - 1;
276
277 DexRegisterMap dex_register_map = IsInInlinedFrame()
David Brazdilf677ebf2015-05-29 16:29:43 +0100278 ? code_info.GetDexRegisterMapAtDepth(depth_in_stack_map,
279 code_info.GetInlineInfoOf(stack_map, encoding),
280 encoding,
281 number_of_dex_registers)
282 : code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100283
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000284 if (!dex_register_map.IsValid()) {
285 return false;
286 }
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000287 DexRegisterLocation::Kind location_kind =
David Brazdilf677ebf2015-05-29 16:29:43 +0100288 dex_register_map.GetLocationKind(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100289 switch (location_kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000290 case DexRegisterLocation::Kind::kInStack: {
David Brazdilf677ebf2015-05-29 16:29:43 +0100291 const int32_t offset = dex_register_map.GetStackOffsetInBytes(vreg,
292 number_of_dex_registers,
293 code_info,
294 encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100295 const uint8_t* addr = reinterpret_cast<const uint8_t*>(cur_quick_frame_) + offset;
296 *val = *reinterpret_cast<const uint32_t*>(addr);
297 return true;
298 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000299 case DexRegisterLocation::Kind::kInRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100300 case DexRegisterLocation::Kind::kInRegisterHigh:
301 case DexRegisterLocation::Kind::kInFpuRegister:
302 case DexRegisterLocation::Kind::kInFpuRegisterHigh: {
David Brazdilf677ebf2015-05-29 16:29:43 +0100303 uint32_t reg =
304 dex_register_map.GetMachineRegister(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100305 return GetRegisterIfAccessible(reg, kind, val);
306 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000307 case DexRegisterLocation::Kind::kConstant:
David Brazdilf677ebf2015-05-29 16:29:43 +0100308 *val = dex_register_map.GetConstant(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100309 return true;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000310 case DexRegisterLocation::Kind::kNone:
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100311 return false;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000312 default:
313 LOG(FATAL)
David Srbecky7dc11782016-02-25 13:23:56 +0000314 << "Unexpected location kind "
315 << dex_register_map.GetLocationInternalKind(vreg,
316 number_of_dex_registers,
317 code_info,
318 encoding);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000319 UNREACHABLE();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100320 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100321}
322
323bool StackVisitor::GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const {
324 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
David Brazdil77a48ae2015-09-15 12:34:04 +0000325
Vladimir Marko239d6ea2016-09-05 10:44:04 +0100326 if (kRuntimeISA == InstructionSet::kX86 && is_float) {
327 // X86 float registers are 64-bit and each XMM register is provided as two separate
328 // 32-bit registers by the context.
329 reg = (kind == kDoubleHiVReg) ? (2 * reg + 1) : (2 * reg);
330 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000331
Goran Jakovljevic986660c2015-12-10 11:44:50 +0100332 // MIPS32 float registers are used as 64-bit (for MIPS32r2 it is pair
333 // F(2n)-F(2n+1), and for MIPS32r6 it is 64-bit register F(2n)). When
334 // accessing upper 32-bits from double, reg + 1 should be used.
335 if ((kRuntimeISA == InstructionSet::kMips) && (kind == kDoubleHiVReg)) {
336 DCHECK_ALIGNED(reg, 2);
337 reg++;
338 }
339
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100340 if (!IsAccessibleRegister(reg, is_float)) {
341 return false;
342 }
343 uintptr_t ptr_val = GetRegister(reg, is_float);
344 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
345 if (target64) {
346 const bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
347 const bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
348 int64_t value_long = static_cast<int64_t>(ptr_val);
349 if (wide_lo) {
350 ptr_val = static_cast<uintptr_t>(Low32Bits(value_long));
351 } else if (wide_hi) {
352 ptr_val = static_cast<uintptr_t>(High32Bits(value_long));
353 }
354 }
355 *val = ptr_val;
356 return true;
357}
358
Mingyao Yang99170c62015-07-06 11:10:37 -0700359bool StackVisitor::GetVRegPairFromDebuggerShadowFrame(uint16_t vreg,
360 VRegKind kind_lo,
361 VRegKind kind_hi,
362 uint64_t* val) const {
363 uint32_t low_32bits;
364 uint32_t high_32bits;
365 bool success = GetVRegFromDebuggerShadowFrame(vreg, kind_lo, &low_32bits);
366 success &= GetVRegFromDebuggerShadowFrame(vreg + 1, kind_hi, &high_32bits);
367 if (success) {
368 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
369 }
370 return success;
371}
372
Mathieu Chartiere401d142015-04-22 13:56:20 -0700373bool StackVisitor::GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200374 VRegKind kind_hi, uint64_t* val) const {
375 if (kind_lo == kLongLoVReg) {
376 DCHECK_EQ(kind_hi, kLongHiVReg);
377 } else if (kind_lo == kDoubleLoVReg) {
378 DCHECK_EQ(kind_hi, kDoubleHiVReg);
379 } else {
380 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100381 UNREACHABLE();
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200382 }
Mingyao Yang99170c62015-07-06 11:10:37 -0700383 // Check if there is value set by the debugger.
384 if (GetVRegPairFromDebuggerShadowFrame(vreg, kind_lo, kind_hi, val)) {
385 return true;
386 }
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200387 if (cur_quick_frame_ != nullptr) {
388 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
389 DCHECK(m == GetMethod());
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100390 DCHECK(cur_oat_quick_method_header_->IsOptimized());
391 return GetVRegPairFromOptimizedCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200392 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100393 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200394 *val = cur_shadow_frame_->GetVRegLong(vreg);
395 return true;
396 }
397}
398
Mathieu Chartiere401d142015-04-22 13:56:20 -0700399bool StackVisitor::GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100400 VRegKind kind_lo, VRegKind kind_hi,
401 uint64_t* val) const {
402 uint32_t low_32bits;
403 uint32_t high_32bits;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700404 bool success = GetVRegFromOptimizedCode(m, vreg, kind_lo, &low_32bits);
405 success &= GetVRegFromOptimizedCode(m, vreg + 1, kind_hi, &high_32bits);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100406 if (success) {
407 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
408 }
409 return success;
410}
411
412bool StackVisitor::GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
413 VRegKind kind_lo, uint64_t* val) const {
414 const bool is_float = (kind_lo == kDoubleLoVReg);
415 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
416 return false;
417 }
418 uintptr_t ptr_val_lo = GetRegister(reg_lo, is_float);
419 uintptr_t ptr_val_hi = GetRegister(reg_hi, is_float);
420 bool target64 = Is64BitInstructionSet(kRuntimeISA);
421 if (target64) {
422 int64_t value_long_lo = static_cast<int64_t>(ptr_val_lo);
423 int64_t value_long_hi = static_cast<int64_t>(ptr_val_hi);
424 ptr_val_lo = static_cast<uintptr_t>(Low32Bits(value_long_lo));
425 ptr_val_hi = static_cast<uintptr_t>(High32Bits(value_long_hi));
426 }
427 *val = (static_cast<uint64_t>(ptr_val_hi) << 32) | static_cast<uint32_t>(ptr_val_lo);
428 return true;
429}
430
Mingyao Yang636b9252015-07-31 16:40:24 -0700431bool StackVisitor::SetVReg(ArtMethod* m,
432 uint16_t vreg,
433 uint32_t new_value,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800434 VRegKind kind) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700435 const DexFile::CodeItem* code_item = m->GetCodeItem();
436 if (code_item == nullptr) {
437 return false;
438 }
439 ShadowFrame* shadow_frame = GetCurrentShadowFrame();
440 if (shadow_frame == nullptr) {
441 // This is a compiled frame: we must prepare and update a shadow frame that will
442 // be executed by the interpreter after deoptimization of the stack.
443 const size_t frame_id = GetFrameId();
444 const uint16_t num_regs = code_item->registers_size_;
445 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
446 CHECK(shadow_frame != nullptr);
447 // Remember the vreg has been set for debugging and must not be overwritten by the
448 // original value during deoptimization of the stack.
449 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
450 }
451 if (kind == kReferenceVReg) {
452 shadow_frame->SetVRegReference(vreg, reinterpret_cast<mirror::Object*>(new_value));
453 } else {
454 shadow_frame->SetVReg(vreg, new_value);
455 }
456 return true;
457}
458
Mingyao Yang636b9252015-07-31 16:40:24 -0700459bool StackVisitor::SetVRegPair(ArtMethod* m,
460 uint16_t vreg,
461 uint64_t new_value,
462 VRegKind kind_lo,
463 VRegKind kind_hi) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700464 if (kind_lo == kLongLoVReg) {
465 DCHECK_EQ(kind_hi, kLongHiVReg);
466 } else if (kind_lo == kDoubleLoVReg) {
467 DCHECK_EQ(kind_hi, kDoubleHiVReg);
468 } else {
469 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
470 UNREACHABLE();
471 }
472 const DexFile::CodeItem* code_item = m->GetCodeItem();
473 if (code_item == nullptr) {
474 return false;
475 }
476 ShadowFrame* shadow_frame = GetCurrentShadowFrame();
477 if (shadow_frame == nullptr) {
478 // This is a compiled frame: we must prepare for deoptimization (see SetVRegFromDebugger).
479 const size_t frame_id = GetFrameId();
480 const uint16_t num_regs = code_item->registers_size_;
481 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
482 CHECK(shadow_frame != nullptr);
483 // Remember the vreg pair has been set for debugging and must not be overwritten by the
484 // original value during deoptimization of the stack.
485 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
486 thread_->GetUpdatedVRegFlags(frame_id)[vreg + 1] = true;
487 }
488 shadow_frame->SetVRegLong(vreg, new_value);
489 return true;
490}
491
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100492bool StackVisitor::IsAccessibleGPR(uint32_t reg) const {
493 DCHECK(context_ != nullptr);
494 return context_->IsAccessibleGPR(reg);
495}
496
Mathieu Chartier815873e2014-02-13 18:02:13 -0800497uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100498 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
499 DCHECK(context_ != nullptr);
Mathieu Chartier815873e2014-02-13 18:02:13 -0800500 return context_->GetGPRAddress(reg);
501}
502
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100503uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
504 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
505 DCHECK(context_ != nullptr);
506 return context_->GetGPR(reg);
Ian Rogers0399dde2012-06-06 17:09:28 -0700507}
508
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100509bool StackVisitor::IsAccessibleFPR(uint32_t reg) const {
510 DCHECK(context_ != nullptr);
511 return context_->IsAccessibleFPR(reg);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200512}
513
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100514uintptr_t StackVisitor::GetFPR(uint32_t reg) const {
515 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
516 DCHECK(context_ != nullptr);
517 return context_->GetFPR(reg);
518}
519
Ian Rogers0399dde2012-06-06 17:09:28 -0700520uintptr_t StackVisitor::GetReturnPc() const {
Ian Rogers13735952014-10-08 12:43:28 -0700521 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700522 DCHECK(sp != nullptr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100523 uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogers0399dde2012-06-06 17:09:28 -0700524 return *reinterpret_cast<uintptr_t*>(pc_addr);
525}
526
527void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
Ian Rogers13735952014-10-08 12:43:28 -0700528 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700529 CHECK(sp != nullptr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100530 uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogers0399dde2012-06-06 17:09:28 -0700531 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc;
532}
533
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100534size_t StackVisitor::ComputeNumFrames(Thread* thread, StackWalkKind walk_kind) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700535 struct NumFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100536 NumFramesVisitor(Thread* thread_in, StackWalkKind walk_kind_in)
537 : StackVisitor(thread_in, nullptr, walk_kind_in), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700538
Ian Rogers5cf98192014-05-29 21:31:50 -0700539 bool VisitFrame() OVERRIDE {
Ian Rogers0399dde2012-06-06 17:09:28 -0700540 frames++;
541 return true;
542 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700543
Ian Rogers0399dde2012-06-06 17:09:28 -0700544 size_t frames;
545 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100546 NumFramesVisitor visitor(thread, walk_kind);
Ian Rogers0399dde2012-06-06 17:09:28 -0700547 visitor.WalkStack(true);
548 return visitor.frames;
549}
550
Mathieu Chartiere401d142015-04-22 13:56:20 -0700551bool StackVisitor::GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700552 struct HasMoreFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100553 HasMoreFramesVisitor(Thread* thread,
554 StackWalkKind walk_kind,
555 size_t num_frames,
556 size_t frame_height)
557 : StackVisitor(thread, nullptr, walk_kind, num_frames),
558 frame_height_(frame_height),
559 found_frame_(false),
560 has_more_frames_(false),
561 next_method_(nullptr),
562 next_dex_pc_(0) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700563 }
564
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700565 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700566 if (found_frame_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700567 ArtMethod* method = GetMethod();
Ian Rogers5cf98192014-05-29 21:31:50 -0700568 if (method != nullptr && !method->IsRuntimeMethod()) {
569 has_more_frames_ = true;
570 next_method_ = method;
571 next_dex_pc_ = GetDexPc();
572 return false; // End stack walk once next method is found.
573 }
574 } else if (GetFrameHeight() == frame_height_) {
575 found_frame_ = true;
576 }
577 return true;
578 }
579
580 size_t frame_height_;
581 bool found_frame_;
582 bool has_more_frames_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700583 ArtMethod* next_method_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700584 uint32_t next_dex_pc_;
585 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100586 HasMoreFramesVisitor visitor(thread_, walk_kind_, GetNumFrames(), GetFrameHeight());
Ian Rogers5cf98192014-05-29 21:31:50 -0700587 visitor.WalkStack(true);
588 *next_method = visitor.next_method_;
589 *next_dex_pc = visitor.next_dex_pc_;
590 return visitor.has_more_frames_;
591}
592
Ian Rogers7a22fa62013-01-23 12:16:16 -0800593void StackVisitor::DescribeStack(Thread* thread) {
Ian Rogers306057f2012-11-26 12:45:53 -0800594 struct DescribeStackVisitor : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800595 explicit DescribeStackVisitor(Thread* thread_in)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100596 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800597
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700598 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers306057f2012-11-26 12:45:53 -0800599 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation();
600 return true;
601 }
602 };
Ian Rogers7a22fa62013-01-23 12:16:16 -0800603 DescribeStackVisitor visitor(thread);
Ian Rogers306057f2012-11-26 12:45:53 -0800604 visitor.WalkStack(true);
605}
606
Ian Rogers40e3bac2012-11-20 00:09:14 -0800607std::string StackVisitor::DescribeLocation() const {
608 std::string result("Visiting method '");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700609 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700610 if (m == nullptr) {
Ian Rogers306057f2012-11-26 12:45:53 -0800611 return "upcall";
612 }
David Sehr709b0702016-10-13 09:12:37 -0700613 result += m->PrettyMethod();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800614 result += StringPrintf("' at dex PC 0x%04x", GetDexPc());
Ian Rogers40e3bac2012-11-20 00:09:14 -0800615 if (!IsShadowFrame()) {
616 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc()));
617 }
618 return result;
619}
620
Alex Lightdba61482016-12-21 08:20:29 -0800621void StackVisitor::SetMethod(ArtMethod* method) {
622 DCHECK(GetMethod() != nullptr);
623 if (cur_shadow_frame_ != nullptr) {
624 cur_shadow_frame_->SetMethod(method);
625 } else {
626 DCHECK(cur_quick_frame_ != nullptr);
627 CHECK(!IsInInlinedFrame()) << "We do not support setting inlined method's ArtMethod!";
628 *cur_quick_frame_ = method;
629 }
630}
631
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100632static void AssertPcIsWithinQuickCode(ArtMethod* method, uintptr_t pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700633 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100634 if (method->IsNative() || method->IsRuntimeMethod() || method->IsProxyMethod()) {
635 return;
636 }
637
638 if (pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) {
639 return;
640 }
641
642 const void* code = method->GetEntryPointFromQuickCompiledCode();
643 if (code == GetQuickInstrumentationEntryPoint()) {
644 return;
645 }
646
647 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
648 if (class_linker->IsQuickToInterpreterBridge(code) ||
649 class_linker->IsQuickResolutionStub(code)) {
650 return;
651 }
652
653 // If we are the JIT then we may have just compiled the method after the
654 // IsQuickToInterpreterBridge check.
Calin Juravleffc87072016-04-20 14:22:09 +0100655 Runtime* runtime = Runtime::Current();
656 if (runtime->UseJitCompilation() && runtime->GetJit()->GetCodeCache()->ContainsPc(code)) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100657 return;
658 }
659
Mingyao Yang063fc772016-08-02 11:02:54 -0700660 uint32_t code_size = OatQuickMethodHeader::FromEntryPoint(code)->GetCodeSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100661 uintptr_t code_start = reinterpret_cast<uintptr_t>(code);
662 CHECK(code_start <= pc && pc <= (code_start + code_size))
David Sehr709b0702016-10-13 09:12:37 -0700663 << method->PrettyMethod()
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100664 << " pc=" << std::hex << pc
Roland Levillain0d5a2812015-11-13 10:07:31 +0000665 << " code_start=" << code_start
666 << " code_size=" << code_size;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100667}
668
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700669void StackVisitor::SanityCheckFrame() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800670 if (kIsDebugBuild) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700671 ArtMethod* method = GetMethod();
672 auto* declaring_class = method->GetDeclaringClass();
673 // Runtime methods have null declaring class.
674 if (!method->IsRuntimeMethod()) {
675 CHECK(declaring_class != nullptr);
676 CHECK_EQ(declaring_class->GetClass(), declaring_class->GetClass()->GetClass())
677 << declaring_class;
678 } else {
679 CHECK(declaring_class == nullptr);
680 }
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700681 Runtime* const runtime = Runtime::Current();
682 LinearAlloc* const linear_alloc = runtime->GetLinearAlloc();
683 if (!linear_alloc->Contains(method)) {
684 // Check class linker linear allocs.
685 mirror::Class* klass = method->GetDeclaringClass();
686 LinearAlloc* const class_linear_alloc = (klass != nullptr)
Mathieu Chartier5b830502016-03-02 10:30:23 -0800687 ? runtime->GetClassLinker()->GetAllocatorForClassLoader(klass->GetClassLoader())
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700688 : linear_alloc;
689 if (!class_linear_alloc->Contains(method)) {
690 // Check image space.
691 bool in_image = false;
692 for (auto& space : runtime->GetHeap()->GetContinuousSpaces()) {
693 if (space->IsImageSpace()) {
694 auto* image_space = space->AsImageSpace();
695 const auto& header = image_space->GetImageHeader();
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700696 const ImageSection& methods = header.GetMethodsSection();
697 const ImageSection& runtime_methods = header.GetRuntimeMethodsSection();
698 const size_t offset = reinterpret_cast<const uint8_t*>(method) - image_space->Begin();
699 if (methods.Contains(offset) || runtime_methods.Contains(offset)) {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700700 in_image = true;
701 break;
702 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700703 }
704 }
David Sehr709b0702016-10-13 09:12:37 -0700705 CHECK(in_image) << method->PrettyMethod() << " not in linear alloc or image";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700706 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700707 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800708 if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100709 AssertPcIsWithinQuickCode(method, cur_quick_frame_pc_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800710 // Frame sanity.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100711 size_t frame_size = GetCurrentQuickFrameInfo().FrameSizeInBytes();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800712 CHECK_NE(frame_size, 0u);
Andreas Gampe5b417b92014-03-10 14:18:35 -0700713 // A rough guess at an upper size we expect to see for a frame.
714 // 256 registers
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700715 // 2 words HandleScope overhead
Andreas Gampe5b417b92014-03-10 14:18:35 -0700716 // 3+3 register spills
717 // TODO: this seems architecture specific for the case of JNI frames.
Brian Carlstromed08bd42014-03-19 18:34:17 -0700718 // TODO: 083-compiler-regressions ManyFloatArgs shows this estimate is wrong.
719 // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word);
720 const size_t kMaxExpectedFrameSize = 2 * KB;
David Sehr709b0702016-10-13 09:12:37 -0700721 CHECK_LE(frame_size, kMaxExpectedFrameSize) << method->PrettyMethod();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100722 size_t return_pc_offset = GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800723 CHECK_LT(return_pc_offset, frame_size);
724 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700725 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700726}
727
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100728// Counts the number of references in the parameter list of the corresponding method.
729// Note: Thus does _not_ include "this" for non-static methods.
730static uint32_t GetNumberOfReferenceArgsWithoutReceiver(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700731 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100732 uint32_t shorty_len;
733 const char* shorty = method->GetShorty(&shorty_len);
734 uint32_t refs = 0;
735 for (uint32_t i = 1; i < shorty_len ; ++i) {
736 if (shorty[i] == 'L') {
737 refs++;
738 }
739 }
740 return refs;
741}
742
743QuickMethodFrameInfo StackVisitor::GetCurrentQuickFrameInfo() const {
744 if (cur_oat_quick_method_header_ != nullptr) {
745 return cur_oat_quick_method_header_->GetFrameInfo();
746 }
747
748 ArtMethod* method = GetMethod();
749 Runtime* runtime = Runtime::Current();
750
751 if (method->IsAbstract()) {
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100752 return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100753 }
754
755 // This goes before IsProxyMethod since runtime methods have a null declaring class.
756 if (method->IsRuntimeMethod()) {
757 return runtime->GetRuntimeMethodFrameInfo(method);
758 }
759
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100760 if (method->IsProxyMethod()) {
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000761 // There is only one direct method of a proxy class: the constructor. A direct method is
762 // cloned from the original java.lang.reflect.Proxy and is executed as usual quick
763 // compiled method without any stubs. Therefore the method must have a OatQuickMethodHeader.
764 DCHECK(!method->IsDirect() && !method->IsConstructor())
765 << "Constructors of proxy classes must have a OatQuickMethodHeader";
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100766 return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100767 }
768
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000769 // The only remaining case is if the method is native and uses the generic JNI stub.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100770 DCHECK(method->IsNative());
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000771 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampe542451c2016-07-26 09:02:02 -0700772 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(method,
773 kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700774 DCHECK(class_linker->IsQuickGenericJniStub(entry_point)) << method->PrettyMethod();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100775 // Generic JNI frame.
776 uint32_t handle_refs = GetNumberOfReferenceArgsWithoutReceiver(method) + 1;
777 size_t scope_size = HandleScope::SizeOf(handle_refs);
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100778 QuickMethodFrameInfo callee_info =
779 runtime->GetCalleeSaveMethodFrameInfo(Runtime::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100780
781 // Callee saves + handle scope + method ref + alignment
782 // Note: -sizeof(void*) since callee-save frame stores a whole method pointer.
783 size_t frame_size = RoundUp(
784 callee_info.FrameSizeInBytes() - sizeof(void*) + sizeof(ArtMethod*) + scope_size,
785 kStackAlignment);
786 return QuickMethodFrameInfo(frame_size, callee_info.CoreSpillMask(), callee_info.FpSpillMask());
787}
788
Andreas Gampe585da952016-12-02 14:52:29 -0800789template <StackVisitor::CountTransitions kCount>
Ian Rogers0399dde2012-06-06 17:09:28 -0700790void StackVisitor::WalkStack(bool include_transitions) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800791 DCHECK(thread_ == Thread::Current() || thread_->IsSuspended());
Ian Rogers62d6c772013-02-27 08:32:07 -0800792 CHECK_EQ(cur_depth_, 0U);
793 bool exit_stubs_installed = Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled();
Alex Lightb81a9842016-12-15 00:59:05 +0000794 uint32_t instrumentation_stack_depth = 0;
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000795 size_t inlined_frames_count = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700796
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700797 for (const ManagedStack* current_fragment = thread_->GetManagedStack();
798 current_fragment != nullptr; current_fragment = current_fragment->GetLink()) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700799 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
800 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700801 cur_quick_frame_pc_ = 0;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100802 cur_oat_quick_method_header_ = nullptr;
Dave Allisonf9439142014-03-27 15:10:22 -0700803
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700804 if (cur_quick_frame_ != nullptr) { // Handle quick stack frames.
Ian Rogers0399dde2012-06-06 17:09:28 -0700805 // Can't be both a shadow and a quick fragment.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700806 DCHECK(current_fragment->GetTopShadowFrame() == nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700807 ArtMethod* method = *cur_quick_frame_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700808 while (method != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100809 cur_oat_quick_method_header_ = method->GetOatQuickMethodHeader(cur_quick_frame_pc_);
Dave Allison5cd33752014-04-15 15:57:58 -0700810 SanityCheckFrame();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100811
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100812 if ((walk_kind_ == StackWalkKind::kIncludeInlinedFrames)
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100813 && (cur_oat_quick_method_header_ != nullptr)
814 && cur_oat_quick_method_header_->IsOptimized()) {
815 CodeInfo code_info = cur_oat_quick_method_header_->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000816 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100817 uint32_t native_pc_offset =
818 cur_oat_quick_method_header_->NativeQuickPcOffset(cur_quick_frame_pc_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100819 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
David Srbecky09ed0982016-02-12 21:58:43 +0000820 if (stack_map.IsValid() && stack_map.HasInlineInfo(encoding.stack_map_encoding)) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100821 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100822 DCHECK_EQ(current_inlining_depth_, 0u);
David Srbecky61b28a12016-02-25 21:55:03 +0000823 for (current_inlining_depth_ = inline_info.GetDepth(encoding.inline_info_encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100824 current_inlining_depth_ != 0;
825 --current_inlining_depth_) {
826 bool should_continue = VisitFrame();
827 if (UNLIKELY(!should_continue)) {
828 return;
829 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100830 cur_depth_++;
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000831 inlined_frames_count++;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100832 }
833 }
834 }
835
Dave Allison5cd33752014-04-15 15:57:58 -0700836 bool should_continue = VisitFrame();
837 if (UNLIKELY(!should_continue)) {
838 return;
Ian Rogers0399dde2012-06-06 17:09:28 -0700839 }
Dave Allison5cd33752014-04-15 15:57:58 -0700840
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100841 QuickMethodFrameInfo frame_info = GetCurrentQuickFrameInfo();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700842 if (context_ != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100843 context_->FillCalleeSaves(reinterpret_cast<uint8_t*>(cur_quick_frame_), frame_info);
Ian Rogers0399dde2012-06-06 17:09:28 -0700844 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700845 // Compute PC for next stack frame from return PC.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100846 size_t frame_size = frame_info.FrameSizeInBytes();
847 size_t return_pc_offset = frame_size - sizeof(void*);
Ian Rogers13735952014-10-08 12:43:28 -0700848 uint8_t* return_pc_addr = reinterpret_cast<uint8_t*>(cur_quick_frame_) + return_pc_offset;
Ian Rogers0399dde2012-06-06 17:09:28 -0700849 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100850
Ian Rogers62d6c772013-02-27 08:32:07 -0800851 if (UNLIKELY(exit_stubs_installed)) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700852 // While profiling, the return pc is restored from the side stack, except when walking
853 // the stack for an exception where the side stack will be unwound in VisitFrame.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700854 if (reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc) {
Andreas Gampe585da952016-12-02 14:52:29 -0800855 CHECK_LT(instrumentation_stack_depth, thread_->GetInstrumentationStack()->size());
Sebastien Hertz74e256b2013-10-04 10:40:37 +0200856 const instrumentation::InstrumentationStackFrame& instrumentation_frame =
Alex Lightb81a9842016-12-15 00:59:05 +0000857 thread_->GetInstrumentationStack()->at(instrumentation_stack_depth);
jeffhao725a9572012-11-13 18:20:12 -0800858 instrumentation_stack_depth++;
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100859 if (GetMethod() ==
860 Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveAllCalleeSaves)) {
Jeff Haofb2802d2013-07-24 13:53:05 -0700861 // Skip runtime save all callee frames which are used to deliver exceptions.
862 } else if (instrumentation_frame.interpreter_entry_) {
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100863 ArtMethod* callee =
864 Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveRefsAndArgs);
David Sehr709b0702016-10-13 09:12:37 -0700865 CHECK_EQ(GetMethod(), callee) << "Expected: " << ArtMethod::PrettyMethod(callee)
866 << " Found: " << ArtMethod::PrettyMethod(GetMethod());
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000867 } else {
868 CHECK_EQ(instrumentation_frame.method_, GetMethod())
David Sehr709b0702016-10-13 09:12:37 -0700869 << "Expected: " << ArtMethod::PrettyMethod(instrumentation_frame.method_)
870 << " Found: " << ArtMethod::PrettyMethod(GetMethod());
Ian Rogers62d6c772013-02-27 08:32:07 -0800871 }
872 if (num_frames_ != 0) {
873 // Check agreement of frame Ids only if num_frames_ is computed to avoid infinite
874 // recursion.
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000875 size_t frame_id = instrumentation::Instrumentation::ComputeFrameId(
876 thread_,
877 cur_depth_,
878 inlined_frames_count);
879 CHECK_EQ(instrumentation_frame.frame_id_, frame_id);
Ian Rogers62d6c772013-02-27 08:32:07 -0800880 }
jeffhao725a9572012-11-13 18:20:12 -0800881 return_pc = instrumentation_frame.return_pc_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700882 }
883 }
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100884
Ian Rogers0399dde2012-06-06 17:09:28 -0700885 cur_quick_frame_pc_ = return_pc;
Ian Rogers13735952014-10-08 12:43:28 -0700886 uint8_t* next_frame = reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700887 cur_quick_frame_ = reinterpret_cast<ArtMethod**>(next_frame);
888
889 if (kDebugStackWalk) {
David Sehr709b0702016-10-13 09:12:37 -0700890 LOG(INFO) << ArtMethod::PrettyMethod(method) << "@" << method << " size=" << frame_size
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100891 << std::boolalpha
892 << " optimized=" << (cur_oat_quick_method_header_ != nullptr &&
893 cur_oat_quick_method_header_->IsOptimized())
Mathieu Chartiere401d142015-04-22 13:56:20 -0700894 << " native=" << method->IsNative()
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100895 << std::noboolalpha
Mathieu Chartiere401d142015-04-22 13:56:20 -0700896 << " entrypoints=" << method->GetEntryPointFromQuickCompiledCode()
897 << "," << method->GetEntryPointFromJni()
Mathieu Chartiere401d142015-04-22 13:56:20 -0700898 << " next=" << *cur_quick_frame_;
899 }
900
Ian Rogers0399dde2012-06-06 17:09:28 -0700901 cur_depth_++;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700902 method = *cur_quick_frame_;
jeffhao6641ea12013-01-02 18:13:42 -0800903 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700904 } else if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700905 do {
906 SanityCheckFrame();
907 bool should_continue = VisitFrame();
908 if (UNLIKELY(!should_continue)) {
909 return;
910 }
911 cur_depth_++;
912 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700913 } while (cur_shadow_frame_ != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700914 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700915 if (include_transitions) {
916 bool should_continue = VisitFrame();
917 if (!should_continue) {
918 return;
919 }
920 }
Andreas Gampe585da952016-12-02 14:52:29 -0800921 if (kCount == CountTransitions::kYes) {
922 cur_depth_++;
923 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800924 }
925 if (num_frames_ != 0) {
926 CHECK_EQ(cur_depth_, num_frames_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700927 }
928}
929
Andreas Gampe585da952016-12-02 14:52:29 -0800930template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kYes>(bool);
931template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kNo>(bool);
932
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800933void JavaFrameRootInfo::Describe(std::ostream& os) const {
934 const StackVisitor* visitor = stack_visitor_;
935 CHECK(visitor != nullptr);
936 os << "Type=" << GetType() << " thread_id=" << GetThreadId() << " location=" <<
937 visitor->DescribeLocation() << " vreg=" << vreg_;
938}
939
Mathieu Chartiere401d142015-04-22 13:56:20 -0700940int StackVisitor::GetVRegOffsetFromQuickCode(const DexFile::CodeItem* code_item,
941 uint32_t core_spills, uint32_t fp_spills,
942 size_t frame_size, int reg, InstructionSet isa) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700943 PointerSize pointer_size = InstructionSetPointerSize(isa);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700944 if (kIsDebugBuild) {
945 auto* runtime = Runtime::Current();
946 if (runtime != nullptr) {
947 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
948 }
949 }
Roland Levillain14d90572015-07-16 10:52:26 +0100950 DCHECK_ALIGNED(frame_size, kStackAlignment);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700951 DCHECK_NE(reg, -1);
952 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
953 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
954 + sizeof(uint32_t); // Filler.
955 int num_regs = code_item->registers_size_ - code_item->ins_size_;
956 int temp_threshold = code_item->registers_size_;
957 const int max_num_special_temps = 1;
958 if (reg == temp_threshold) {
959 // The current method pointer corresponds to special location on stack.
960 return 0;
961 } else if (reg >= temp_threshold + max_num_special_temps) {
962 /*
963 * Special temporaries may have custom locations and the logic above deals with that.
964 * However, non-special temporaries are placed relative to the outs.
965 */
Andreas Gampe542451c2016-07-26 09:02:02 -0700966 int temps_start = code_item->outs_size_ * sizeof(uint32_t)
967 + static_cast<size_t>(pointer_size) /* art method */;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700968 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
969 return temps_start + relative_offset;
970 } else if (reg < num_regs) {
971 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
972 return locals_start + (reg * sizeof(uint32_t));
973 } else {
974 // Handle ins.
Andreas Gampe542451c2016-07-26 09:02:02 -0700975 return frame_size + ((reg - num_regs) * sizeof(uint32_t))
976 + static_cast<size_t>(pointer_size) /* art method */;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700977 }
978}
979
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700980void LockCountData::AddMonitor(Thread* self, mirror::Object* obj) {
Andreas Gampe03ec9302015-08-27 17:41:47 -0700981 if (obj == nullptr) {
982 return;
983 }
984
985 // If there's an error during enter, we won't have locked the monitor. So check there's no
986 // exception.
987 if (self->IsExceptionPending()) {
988 return;
989 }
990
991 if (monitors_ == nullptr) {
992 monitors_.reset(new std::vector<mirror::Object*>());
993 }
994 monitors_->push_back(obj);
995}
996
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700997void LockCountData::RemoveMonitorOrThrow(Thread* self, const mirror::Object* obj) {
Andreas Gampe03ec9302015-08-27 17:41:47 -0700998 if (obj == nullptr) {
999 return;
1000 }
1001 bool found_object = false;
1002 if (monitors_ != nullptr) {
1003 // We need to remove one pointer to ref, as duplicates are used for counting recursive locks.
1004 // We arbitrarily choose the first one.
1005 auto it = std::find(monitors_->begin(), monitors_->end(), obj);
1006 if (it != monitors_->end()) {
1007 monitors_->erase(it);
1008 found_object = true;
1009 }
1010 }
1011 if (!found_object) {
1012 // The object wasn't found. Time for an IllegalMonitorStateException.
1013 // The order here isn't fully clear. Assume that any other pending exception is swallowed.
1014 // TODO: Maybe make already pending exception a suppressed exception.
1015 self->ClearException();
1016 self->ThrowNewExceptionF("Ljava/lang/IllegalMonitorStateException;",
1017 "did not lock monitor on object of type '%s' before unlocking",
David Sehr709b0702016-10-13 09:12:37 -07001018 const_cast<mirror::Object*>(obj)->PrettyTypeOf().c_str());
Andreas Gampe03ec9302015-08-27 17:41:47 -07001019 }
1020}
1021
1022// Helper to unlock a monitor. Must be NO_THREAD_SAFETY_ANALYSIS, as we can't statically show
1023// that the object was locked.
1024void MonitorExitHelper(Thread* self, mirror::Object* obj) NO_THREAD_SAFETY_ANALYSIS {
1025 DCHECK(self != nullptr);
1026 DCHECK(obj != nullptr);
1027 obj->MonitorExit(self);
1028}
1029
Andreas Gampe56fdd0e2016-04-28 14:56:54 -07001030bool LockCountData::CheckAllMonitorsReleasedOrThrow(Thread* self) {
Andreas Gampe03ec9302015-08-27 17:41:47 -07001031 DCHECK(self != nullptr);
1032 if (monitors_ != nullptr) {
1033 if (!monitors_->empty()) {
1034 // There may be an exception pending, if the method is terminating abruptly. Clear it.
1035 // TODO: Should we add this as a suppressed exception?
1036 self->ClearException();
1037
1038 // OK, there are monitors that are still locked. To enforce structured locking (and avoid
1039 // deadlocks) we unlock all of them before we raise the IllegalMonitorState exception.
1040 for (mirror::Object* obj : *monitors_) {
1041 MonitorExitHelper(self, obj);
1042 // If this raised an exception, ignore. TODO: Should we add this as suppressed
1043 // exceptions?
1044 if (self->IsExceptionPending()) {
1045 self->ClearException();
1046 }
1047 }
1048 // Raise an exception, just give the first object as the sample.
1049 mirror::Object* first = (*monitors_)[0];
1050 self->ThrowNewExceptionF("Ljava/lang/IllegalMonitorStateException;",
1051 "did not unlock monitor on object of type '%s'",
David Sehr709b0702016-10-13 09:12:37 -07001052 mirror::Object::PrettyTypeOf(first).c_str());
Andreas Gampe03ec9302015-08-27 17:41:47 -07001053
1054 // To make sure this path is not triggered again, clean out the monitors.
1055 monitors_->clear();
1056
1057 return false;
1058 }
1059 }
1060 return true;
1061}
1062
Elliott Hughes68e76522011-10-05 13:22:16 -07001063} // namespace art