blob: 792da88a6321609e70bab1b002fb8ff07190a098 [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
Ian Rogerse63db272014-07-15 15:36:11 -070019#include "arch/context.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070021#include "base/enums.h"
Dave Allisonf9439142014-03-27 15:10:22 -070022#include "base/hex_dump.h"
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010023#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070024#include "entrypoints/runtime_asm_entrypoints.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070025#include "gc/space/image_space.h"
26#include "gc/space/space-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010027#include "jit/jit.h"
28#include "jit/jit_code_cache.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "linear_alloc.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "mirror/object-inl.h"
32#include "mirror/object_array-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010033#include "oat_quick_method_header.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010034#include "quick/quick_method_frame_info.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070035#include "runtime.h"
Dave Allisonf9439142014-03-27 15:10:22 -070036#include "thread.h"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070037#include "thread_list.h"
Mathieu Chartier4e305412014-02-19 10:54:44 -080038#include "verify_object-inl.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070039
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080040namespace art {
41
Mathieu Chartier8405bfd2016-02-05 12:00:49 -080042static constexpr bool kDebugStackWalk = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -070043
Ian Rogers62d6c772013-02-27 08:32:07 -080044mirror::Object* ShadowFrame::GetThisObject() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -070045 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -080046 if (m->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070047 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -080048 } else if (m->IsNative()) {
49 return GetVRegReference(0);
50 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070051 const DexFile::CodeItem* code_item = m->GetCodeItem();
David Sehr709b0702016-10-13 09:12:37 -070052 CHECK(code_item != nullptr) << ArtMethod::PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -080053 uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
54 return GetVRegReference(reg);
55 }
56}
57
Jeff Haoe701f482013-05-24 11:50:49 -070058mirror::Object* ShadowFrame::GetThisObject(uint16_t num_ins) const {
Mathieu Chartiere401d142015-04-22 13:56:20 -070059 ArtMethod* m = GetMethod();
Jeff Haoe701f482013-05-24 11:50:49 -070060 if (m->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070061 return nullptr;
Jeff Haoe701f482013-05-24 11:50:49 -070062 } else {
Jeff Hao8d448852013-06-03 17:26:19 -070063 return GetVRegReference(NumberOfVRegs() - num_ins);
Jeff Haoe701f482013-05-24 11:50:49 -070064 }
65}
66
TDYa127ce4cc0d2012-11-18 16:59:53 -080067size_t ManagedStack::NumJniShadowFrameReferences() const {
Ian Rogers0399dde2012-06-06 17:09:28 -070068 size_t count = 0;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070069 for (const ManagedStack* current_fragment = this; current_fragment != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070070 current_fragment = current_fragment->GetLink()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070071 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070072 current_frame = current_frame->GetLink()) {
TDYa127ce4cc0d2012-11-18 16:59:53 -080073 if (current_frame->GetMethod()->IsNative()) {
74 // The JNI ShadowFrame only contains references. (For indirect reference.)
75 count += current_frame->NumberOfVRegs();
76 }
Ian Rogers0399dde2012-06-06 17:09:28 -070077 }
78 }
79 return count;
80}
81
Ian Rogersef7d42f2014-01-06 12:55:46 -080082bool ManagedStack::ShadowFramesContain(StackReference<mirror::Object>* shadow_frame_entry) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070083 for (const ManagedStack* current_fragment = this; current_fragment != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070084 current_fragment = current_fragment->GetLink()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070085 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070086 current_frame = current_frame->GetLink()) {
87 if (current_frame->Contains(shadow_frame_entry)) {
88 return true;
89 }
90 }
91 }
92 return false;
93}
94
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010095StackVisitor::StackVisitor(Thread* thread, Context* context, StackWalkKind walk_kind)
96 : StackVisitor(thread, context, walk_kind, 0) {}
Ian Rogers7a22fa62013-01-23 12:16:16 -080097
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010098StackVisitor::StackVisitor(Thread* thread,
99 Context* context,
100 StackWalkKind walk_kind,
101 size_t num_frames)
102 : thread_(thread),
103 walk_kind_(walk_kind),
104 cur_shadow_frame_(nullptr),
105 cur_quick_frame_(nullptr),
106 cur_quick_frame_pc_(0),
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100107 cur_oat_quick_method_header_(nullptr),
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100108 num_frames_(num_frames),
109 cur_depth_(0),
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100110 current_inlining_depth_(0),
Ian Rogers5cf98192014-05-29 21:31:50 -0700111 context_(context) {
112 DCHECK(thread == Thread::Current() || thread->IsSuspended()) << *thread;
113}
114
Mathieu Chartiere401d142015-04-22 13:56:20 -0700115InlineInfo StackVisitor::GetCurrentInlineInfo() const {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100116 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
117 uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_);
118 CodeInfo code_info = method_header->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000119 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Brazdilf677ebf2015-05-29 16:29:43 +0100120 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100121 DCHECK(stack_map.IsValid());
David Brazdilf677ebf2015-05-29 16:29:43 +0100122 return code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100123}
124
Mathieu Chartiere401d142015-04-22 13:56:20 -0700125ArtMethod* StackVisitor::GetMethod() const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100126 if (cur_shadow_frame_ != nullptr) {
127 return cur_shadow_frame_->GetMethod();
128 } else if (cur_quick_frame_ != nullptr) {
129 if (IsInInlinedFrame()) {
130 size_t depth_in_stack_map = current_inlining_depth_ - 1;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100131 InlineInfo inline_info = GetCurrentInlineInfo();
David Srbecky61b28a12016-02-25 21:55:03 +0000132 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
133 CodeInfoEncoding encoding = method_header->GetOptimizedCodeInfo().ExtractEncoding();
Mathieu Chartier45bf2502016-03-31 11:07:09 -0700134 DCHECK(walk_kind_ != StackWalkKind::kSkipInlinedFrames);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100135 return GetResolvedMethod(*GetCurrentQuickFrame(),
136 inline_info,
137 encoding.inline_info_encoding,
138 depth_in_stack_map);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100139 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700140 return *cur_quick_frame_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100141 }
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100142 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700143 return nullptr;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100144}
145
Dave Allisonb373e092014-02-20 16:06:36 -0800146uint32_t StackVisitor::GetDexPc(bool abort_on_failure) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700147 if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700148 return cur_shadow_frame_->GetDexPC();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700149 } else if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100150 if (IsInInlinedFrame()) {
151 size_t depth_in_stack_map = current_inlining_depth_ - 1;
David Srbecky61b28a12016-02-25 21:55:03 +0000152 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
153 CodeInfoEncoding encoding = method_header->GetOptimizedCodeInfo().ExtractEncoding();
154 return GetCurrentInlineInfo().GetDexPcAtDepth(encoding.inline_info_encoding,
155 depth_in_stack_map);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100156 } else if (cur_oat_quick_method_header_ == nullptr) {
157 return DexFile::kDexNoIndex;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100158 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100159 return cur_oat_quick_method_header_->ToDexPc(
160 GetMethod(), cur_quick_frame_pc_, abort_on_failure);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100161 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700162 } else {
163 return 0;
164 }
165}
166
Mathieu Chartiere401d142015-04-22 13:56:20 -0700167extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700168 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertza836bc92014-11-25 16:30:53 +0100169
Ian Rogers62d6c772013-02-27 08:32:07 -0800170mirror::Object* StackVisitor::GetThisObject() const {
Andreas Gampe542451c2016-07-26 09:02:02 -0700171 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700172 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800173 if (m->IsStatic()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100174 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800175 } else if (m->IsNative()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100176 if (cur_quick_frame_ != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700177 HandleScope* hs = reinterpret_cast<HandleScope*>(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100178 reinterpret_cast<char*>(cur_quick_frame_) + sizeof(ArtMethod*));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700179 return hs->GetReference(0);
Ian Rogers62d6c772013-02-27 08:32:07 -0800180 } else {
181 return cur_shadow_frame_->GetVRegReference(0);
182 }
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000183 } else if (m->IsProxyMethod()) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100184 if (cur_quick_frame_ != nullptr) {
185 return artQuickGetProxyThisObject(cur_quick_frame_);
186 } else {
187 return cur_shadow_frame_->GetVRegReference(0);
188 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800189 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700190 const DexFile::CodeItem* code_item = m->GetCodeItem();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100191 if (code_item == nullptr) {
Ian Rogerse0dcd462014-03-08 15:21:04 -0800192 UNIMPLEMENTED(ERROR) << "Failed to determine this object of abstract or proxy method: "
David Sehr709b0702016-10-13 09:12:37 -0700193 << ArtMethod::PrettyMethod(m);
Ian Rogerse0dcd462014-03-08 15:21:04 -0800194 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800195 } else {
196 uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000197 uint32_t value = 0;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700198 bool success = GetVReg(m, reg, kReferenceVReg, &value);
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000199 // We currently always guarantee the `this` object is live throughout the method.
David Sehr709b0702016-10-13 09:12:37 -0700200 CHECK(success) << "Failed to read the this object in " << ArtMethod::PrettyMethod(m);
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000201 return reinterpret_cast<mirror::Object*>(value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800202 }
203 }
204}
205
Ian Rogers0c7abda2012-09-19 13:33:42 -0700206size_t StackVisitor::GetNativePcOffset() const {
207 DCHECK(!IsShadowFrame());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100208 return GetCurrentOatQuickMethodHeader()->NativeQuickPcOffset(cur_quick_frame_pc_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700209}
210
Mingyao Yang99170c62015-07-06 11:10:37 -0700211bool StackVisitor::GetVRegFromDebuggerShadowFrame(uint16_t vreg,
212 VRegKind kind,
213 uint32_t* val) const {
214 size_t frame_id = const_cast<StackVisitor*>(this)->GetFrameId();
215 ShadowFrame* shadow_frame = thread_->FindDebuggerShadowFrame(frame_id);
216 if (shadow_frame != nullptr) {
217 bool* updated_vreg_flags = thread_->GetUpdatedVRegFlags(frame_id);
218 DCHECK(updated_vreg_flags != nullptr);
219 if (updated_vreg_flags[vreg]) {
220 // Value is set by the debugger.
221 if (kind == kReferenceVReg) {
222 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
223 shadow_frame->GetVRegReference(vreg)));
224 } else {
225 *val = shadow_frame->GetVReg(vreg);
226 }
227 return true;
228 }
229 }
230 // No value is set by the debugger.
231 return false;
232}
233
Mathieu Chartiere401d142015-04-22 13:56:20 -0700234bool StackVisitor::GetVReg(ArtMethod* m, uint16_t vreg, VRegKind kind, uint32_t* val) const {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200235 if (cur_quick_frame_ != nullptr) {
236 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800237 DCHECK(m == GetMethod());
Mingyao Yang99170c62015-07-06 11:10:37 -0700238 // Check if there is value set by the debugger.
239 if (GetVRegFromDebuggerShadowFrame(vreg, kind, val)) {
240 return true;
241 }
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100242 DCHECK(cur_oat_quick_method_header_->IsOptimized());
243 return GetVRegFromOptimizedCode(m, vreg, kind, val);
Ian Rogers0399dde2012-06-06 17:09:28 -0700244 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100245 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertz09687442015-11-17 10:35:39 +0100246 if (kind == kReferenceVReg) {
247 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
248 cur_shadow_frame_->GetVRegReference(vreg)));
249 } else {
250 *val = cur_shadow_frame_->GetVReg(vreg);
251 }
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200252 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700253 }
254}
255
Mathieu Chartiere401d142015-04-22 13:56:20 -0700256bool StackVisitor::GetVRegFromOptimizedCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100257 uint32_t* val) const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100258 DCHECK_EQ(m, GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100259 const DexFile::CodeItem* code_item = m->GetCodeItem();
David Sehr709b0702016-10-13 09:12:37 -0700260 DCHECK(code_item != nullptr) << m->PrettyMethod(); // Can't be null or how would we compile
261 // its instructions?
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000262 uint16_t number_of_dex_registers = code_item->registers_size_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100263 DCHECK_LT(vreg, code_item->registers_size_);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100264 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
265 CodeInfo code_info = method_header->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000266 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100267
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100268 uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100269 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100270 DCHECK(stack_map.IsValid());
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100271 size_t depth_in_stack_map = current_inlining_depth_ - 1;
272
273 DexRegisterMap dex_register_map = IsInInlinedFrame()
David Brazdilf677ebf2015-05-29 16:29:43 +0100274 ? code_info.GetDexRegisterMapAtDepth(depth_in_stack_map,
275 code_info.GetInlineInfoOf(stack_map, encoding),
276 encoding,
277 number_of_dex_registers)
278 : code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100279
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000280 if (!dex_register_map.IsValid()) {
281 return false;
282 }
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000283 DexRegisterLocation::Kind location_kind =
David Brazdilf677ebf2015-05-29 16:29:43 +0100284 dex_register_map.GetLocationKind(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100285 switch (location_kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000286 case DexRegisterLocation::Kind::kInStack: {
David Brazdilf677ebf2015-05-29 16:29:43 +0100287 const int32_t offset = dex_register_map.GetStackOffsetInBytes(vreg,
288 number_of_dex_registers,
289 code_info,
290 encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100291 const uint8_t* addr = reinterpret_cast<const uint8_t*>(cur_quick_frame_) + offset;
292 *val = *reinterpret_cast<const uint32_t*>(addr);
293 return true;
294 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000295 case DexRegisterLocation::Kind::kInRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100296 case DexRegisterLocation::Kind::kInRegisterHigh:
297 case DexRegisterLocation::Kind::kInFpuRegister:
298 case DexRegisterLocation::Kind::kInFpuRegisterHigh: {
David Brazdilf677ebf2015-05-29 16:29:43 +0100299 uint32_t reg =
300 dex_register_map.GetMachineRegister(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100301 return GetRegisterIfAccessible(reg, kind, val);
302 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000303 case DexRegisterLocation::Kind::kConstant:
David Brazdilf677ebf2015-05-29 16:29:43 +0100304 *val = dex_register_map.GetConstant(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100305 return true;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000306 case DexRegisterLocation::Kind::kNone:
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100307 return false;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000308 default:
309 LOG(FATAL)
David Srbecky7dc11782016-02-25 13:23:56 +0000310 << "Unexpected location kind "
311 << dex_register_map.GetLocationInternalKind(vreg,
312 number_of_dex_registers,
313 code_info,
314 encoding);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000315 UNREACHABLE();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100316 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100317}
318
319bool StackVisitor::GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const {
320 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
David Brazdil77a48ae2015-09-15 12:34:04 +0000321
Vladimir Marko239d6ea2016-09-05 10:44:04 +0100322 if (kRuntimeISA == InstructionSet::kX86 && is_float) {
323 // X86 float registers are 64-bit and each XMM register is provided as two separate
324 // 32-bit registers by the context.
325 reg = (kind == kDoubleHiVReg) ? (2 * reg + 1) : (2 * reg);
326 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000327
Goran Jakovljevic986660c2015-12-10 11:44:50 +0100328 // MIPS32 float registers are used as 64-bit (for MIPS32r2 it is pair
329 // F(2n)-F(2n+1), and for MIPS32r6 it is 64-bit register F(2n)). When
330 // accessing upper 32-bits from double, reg + 1 should be used.
331 if ((kRuntimeISA == InstructionSet::kMips) && (kind == kDoubleHiVReg)) {
332 DCHECK_ALIGNED(reg, 2);
333 reg++;
334 }
335
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100336 if (!IsAccessibleRegister(reg, is_float)) {
337 return false;
338 }
339 uintptr_t ptr_val = GetRegister(reg, is_float);
340 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
341 if (target64) {
342 const bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
343 const bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
344 int64_t value_long = static_cast<int64_t>(ptr_val);
345 if (wide_lo) {
346 ptr_val = static_cast<uintptr_t>(Low32Bits(value_long));
347 } else if (wide_hi) {
348 ptr_val = static_cast<uintptr_t>(High32Bits(value_long));
349 }
350 }
351 *val = ptr_val;
352 return true;
353}
354
Mingyao Yang99170c62015-07-06 11:10:37 -0700355bool StackVisitor::GetVRegPairFromDebuggerShadowFrame(uint16_t vreg,
356 VRegKind kind_lo,
357 VRegKind kind_hi,
358 uint64_t* val) const {
359 uint32_t low_32bits;
360 uint32_t high_32bits;
361 bool success = GetVRegFromDebuggerShadowFrame(vreg, kind_lo, &low_32bits);
362 success &= GetVRegFromDebuggerShadowFrame(vreg + 1, kind_hi, &high_32bits);
363 if (success) {
364 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
365 }
366 return success;
367}
368
Mathieu Chartiere401d142015-04-22 13:56:20 -0700369bool StackVisitor::GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200370 VRegKind kind_hi, uint64_t* val) const {
371 if (kind_lo == kLongLoVReg) {
372 DCHECK_EQ(kind_hi, kLongHiVReg);
373 } else if (kind_lo == kDoubleLoVReg) {
374 DCHECK_EQ(kind_hi, kDoubleHiVReg);
375 } else {
376 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100377 UNREACHABLE();
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200378 }
Mingyao Yang99170c62015-07-06 11:10:37 -0700379 // Check if there is value set by the debugger.
380 if (GetVRegPairFromDebuggerShadowFrame(vreg, kind_lo, kind_hi, val)) {
381 return true;
382 }
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200383 if (cur_quick_frame_ != nullptr) {
384 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
385 DCHECK(m == GetMethod());
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100386 DCHECK(cur_oat_quick_method_header_->IsOptimized());
387 return GetVRegPairFromOptimizedCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200388 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100389 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200390 *val = cur_shadow_frame_->GetVRegLong(vreg);
391 return true;
392 }
393}
394
Mathieu Chartiere401d142015-04-22 13:56:20 -0700395bool StackVisitor::GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100396 VRegKind kind_lo, VRegKind kind_hi,
397 uint64_t* val) const {
398 uint32_t low_32bits;
399 uint32_t high_32bits;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700400 bool success = GetVRegFromOptimizedCode(m, vreg, kind_lo, &low_32bits);
401 success &= GetVRegFromOptimizedCode(m, vreg + 1, kind_hi, &high_32bits);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100402 if (success) {
403 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
404 }
405 return success;
406}
407
408bool StackVisitor::GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
409 VRegKind kind_lo, uint64_t* val) const {
410 const bool is_float = (kind_lo == kDoubleLoVReg);
411 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
412 return false;
413 }
414 uintptr_t ptr_val_lo = GetRegister(reg_lo, is_float);
415 uintptr_t ptr_val_hi = GetRegister(reg_hi, is_float);
416 bool target64 = Is64BitInstructionSet(kRuntimeISA);
417 if (target64) {
418 int64_t value_long_lo = static_cast<int64_t>(ptr_val_lo);
419 int64_t value_long_hi = static_cast<int64_t>(ptr_val_hi);
420 ptr_val_lo = static_cast<uintptr_t>(Low32Bits(value_long_lo));
421 ptr_val_hi = static_cast<uintptr_t>(High32Bits(value_long_hi));
422 }
423 *val = (static_cast<uint64_t>(ptr_val_hi) << 32) | static_cast<uint32_t>(ptr_val_lo);
424 return true;
425}
426
Mingyao Yang636b9252015-07-31 16:40:24 -0700427bool StackVisitor::SetVReg(ArtMethod* m,
428 uint16_t vreg,
429 uint32_t new_value,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800430 VRegKind kind) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700431 const DexFile::CodeItem* code_item = m->GetCodeItem();
432 if (code_item == nullptr) {
433 return false;
434 }
435 ShadowFrame* shadow_frame = GetCurrentShadowFrame();
436 if (shadow_frame == nullptr) {
437 // This is a compiled frame: we must prepare and update a shadow frame that will
438 // be executed by the interpreter after deoptimization of the stack.
439 const size_t frame_id = GetFrameId();
440 const uint16_t num_regs = code_item->registers_size_;
441 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
442 CHECK(shadow_frame != nullptr);
443 // Remember the vreg has been set for debugging and must not be overwritten by the
444 // original value during deoptimization of the stack.
445 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
446 }
447 if (kind == kReferenceVReg) {
448 shadow_frame->SetVRegReference(vreg, reinterpret_cast<mirror::Object*>(new_value));
449 } else {
450 shadow_frame->SetVReg(vreg, new_value);
451 }
452 return true;
453}
454
Mingyao Yang636b9252015-07-31 16:40:24 -0700455bool StackVisitor::SetVRegPair(ArtMethod* m,
456 uint16_t vreg,
457 uint64_t new_value,
458 VRegKind kind_lo,
459 VRegKind kind_hi) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700460 if (kind_lo == kLongLoVReg) {
461 DCHECK_EQ(kind_hi, kLongHiVReg);
462 } else if (kind_lo == kDoubleLoVReg) {
463 DCHECK_EQ(kind_hi, kDoubleHiVReg);
464 } else {
465 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
466 UNREACHABLE();
467 }
468 const DexFile::CodeItem* code_item = m->GetCodeItem();
469 if (code_item == nullptr) {
470 return false;
471 }
472 ShadowFrame* shadow_frame = GetCurrentShadowFrame();
473 if (shadow_frame == nullptr) {
474 // This is a compiled frame: we must prepare for deoptimization (see SetVRegFromDebugger).
475 const size_t frame_id = GetFrameId();
476 const uint16_t num_regs = code_item->registers_size_;
477 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
478 CHECK(shadow_frame != nullptr);
479 // Remember the vreg pair has been set for debugging and must not be overwritten by the
480 // original value during deoptimization of the stack.
481 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
482 thread_->GetUpdatedVRegFlags(frame_id)[vreg + 1] = true;
483 }
484 shadow_frame->SetVRegLong(vreg, new_value);
485 return true;
486}
487
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100488bool StackVisitor::IsAccessibleGPR(uint32_t reg) const {
489 DCHECK(context_ != nullptr);
490 return context_->IsAccessibleGPR(reg);
491}
492
Mathieu Chartier815873e2014-02-13 18:02:13 -0800493uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100494 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
495 DCHECK(context_ != nullptr);
Mathieu Chartier815873e2014-02-13 18:02:13 -0800496 return context_->GetGPRAddress(reg);
497}
498
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100499uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
500 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
501 DCHECK(context_ != nullptr);
502 return context_->GetGPR(reg);
Ian Rogers0399dde2012-06-06 17:09:28 -0700503}
504
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100505bool StackVisitor::IsAccessibleFPR(uint32_t reg) const {
506 DCHECK(context_ != nullptr);
507 return context_->IsAccessibleFPR(reg);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200508}
509
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100510uintptr_t StackVisitor::GetFPR(uint32_t reg) const {
511 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
512 DCHECK(context_ != nullptr);
513 return context_->GetFPR(reg);
514}
515
Ian Rogers0399dde2012-06-06 17:09:28 -0700516uintptr_t StackVisitor::GetReturnPc() const {
Ian Rogers13735952014-10-08 12:43:28 -0700517 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700518 DCHECK(sp != nullptr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100519 uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogers0399dde2012-06-06 17:09:28 -0700520 return *reinterpret_cast<uintptr_t*>(pc_addr);
521}
522
523void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
Ian Rogers13735952014-10-08 12:43:28 -0700524 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700525 CHECK(sp != nullptr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100526 uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogers0399dde2012-06-06 17:09:28 -0700527 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc;
528}
529
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100530size_t StackVisitor::ComputeNumFrames(Thread* thread, StackWalkKind walk_kind) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700531 struct NumFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100532 NumFramesVisitor(Thread* thread_in, StackWalkKind walk_kind_in)
533 : StackVisitor(thread_in, nullptr, walk_kind_in), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700534
Ian Rogers5cf98192014-05-29 21:31:50 -0700535 bool VisitFrame() OVERRIDE {
Ian Rogers0399dde2012-06-06 17:09:28 -0700536 frames++;
537 return true;
538 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700539
Ian Rogers0399dde2012-06-06 17:09:28 -0700540 size_t frames;
541 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100542 NumFramesVisitor visitor(thread, walk_kind);
Ian Rogers0399dde2012-06-06 17:09:28 -0700543 visitor.WalkStack(true);
544 return visitor.frames;
545}
546
Mathieu Chartiere401d142015-04-22 13:56:20 -0700547bool StackVisitor::GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700548 struct HasMoreFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100549 HasMoreFramesVisitor(Thread* thread,
550 StackWalkKind walk_kind,
551 size_t num_frames,
552 size_t frame_height)
553 : StackVisitor(thread, nullptr, walk_kind, num_frames),
554 frame_height_(frame_height),
555 found_frame_(false),
556 has_more_frames_(false),
557 next_method_(nullptr),
558 next_dex_pc_(0) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700559 }
560
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700561 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700562 if (found_frame_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700563 ArtMethod* method = GetMethod();
Ian Rogers5cf98192014-05-29 21:31:50 -0700564 if (method != nullptr && !method->IsRuntimeMethod()) {
565 has_more_frames_ = true;
566 next_method_ = method;
567 next_dex_pc_ = GetDexPc();
568 return false; // End stack walk once next method is found.
569 }
570 } else if (GetFrameHeight() == frame_height_) {
571 found_frame_ = true;
572 }
573 return true;
574 }
575
576 size_t frame_height_;
577 bool found_frame_;
578 bool has_more_frames_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700579 ArtMethod* next_method_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700580 uint32_t next_dex_pc_;
581 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100582 HasMoreFramesVisitor visitor(thread_, walk_kind_, GetNumFrames(), GetFrameHeight());
Ian Rogers5cf98192014-05-29 21:31:50 -0700583 visitor.WalkStack(true);
584 *next_method = visitor.next_method_;
585 *next_dex_pc = visitor.next_dex_pc_;
586 return visitor.has_more_frames_;
587}
588
Ian Rogers7a22fa62013-01-23 12:16:16 -0800589void StackVisitor::DescribeStack(Thread* thread) {
Ian Rogers306057f2012-11-26 12:45:53 -0800590 struct DescribeStackVisitor : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800591 explicit DescribeStackVisitor(Thread* thread_in)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100592 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800593
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700594 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers306057f2012-11-26 12:45:53 -0800595 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation();
596 return true;
597 }
598 };
Ian Rogers7a22fa62013-01-23 12:16:16 -0800599 DescribeStackVisitor visitor(thread);
Ian Rogers306057f2012-11-26 12:45:53 -0800600 visitor.WalkStack(true);
601}
602
Ian Rogers40e3bac2012-11-20 00:09:14 -0800603std::string StackVisitor::DescribeLocation() const {
604 std::string result("Visiting method '");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700605 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700606 if (m == nullptr) {
Ian Rogers306057f2012-11-26 12:45:53 -0800607 return "upcall";
608 }
David Sehr709b0702016-10-13 09:12:37 -0700609 result += m->PrettyMethod();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800610 result += StringPrintf("' at dex PC 0x%04x", GetDexPc());
Ian Rogers40e3bac2012-11-20 00:09:14 -0800611 if (!IsShadowFrame()) {
612 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc()));
613 }
614 return result;
615}
616
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100617static void AssertPcIsWithinQuickCode(ArtMethod* method, uintptr_t pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700618 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100619 if (method->IsNative() || method->IsRuntimeMethod() || method->IsProxyMethod()) {
620 return;
621 }
622
623 if (pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) {
624 return;
625 }
626
627 const void* code = method->GetEntryPointFromQuickCompiledCode();
628 if (code == GetQuickInstrumentationEntryPoint()) {
629 return;
630 }
631
632 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
633 if (class_linker->IsQuickToInterpreterBridge(code) ||
634 class_linker->IsQuickResolutionStub(code)) {
635 return;
636 }
637
638 // If we are the JIT then we may have just compiled the method after the
639 // IsQuickToInterpreterBridge check.
Calin Juravleffc87072016-04-20 14:22:09 +0100640 Runtime* runtime = Runtime::Current();
641 if (runtime->UseJitCompilation() && runtime->GetJit()->GetCodeCache()->ContainsPc(code)) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100642 return;
643 }
644
Mingyao Yang063fc772016-08-02 11:02:54 -0700645 uint32_t code_size = OatQuickMethodHeader::FromEntryPoint(code)->GetCodeSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100646 uintptr_t code_start = reinterpret_cast<uintptr_t>(code);
647 CHECK(code_start <= pc && pc <= (code_start + code_size))
David Sehr709b0702016-10-13 09:12:37 -0700648 << method->PrettyMethod()
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100649 << " pc=" << std::hex << pc
Roland Levillain0d5a2812015-11-13 10:07:31 +0000650 << " code_start=" << code_start
651 << " code_size=" << code_size;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100652}
653
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700654void StackVisitor::SanityCheckFrame() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800655 if (kIsDebugBuild) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700656 ArtMethod* method = GetMethod();
657 auto* declaring_class = method->GetDeclaringClass();
658 // Runtime methods have null declaring class.
659 if (!method->IsRuntimeMethod()) {
660 CHECK(declaring_class != nullptr);
661 CHECK_EQ(declaring_class->GetClass(), declaring_class->GetClass()->GetClass())
662 << declaring_class;
663 } else {
664 CHECK(declaring_class == nullptr);
665 }
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700666 Runtime* const runtime = Runtime::Current();
667 LinearAlloc* const linear_alloc = runtime->GetLinearAlloc();
668 if (!linear_alloc->Contains(method)) {
669 // Check class linker linear allocs.
670 mirror::Class* klass = method->GetDeclaringClass();
671 LinearAlloc* const class_linear_alloc = (klass != nullptr)
Mathieu Chartier5b830502016-03-02 10:30:23 -0800672 ? runtime->GetClassLinker()->GetAllocatorForClassLoader(klass->GetClassLoader())
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700673 : linear_alloc;
674 if (!class_linear_alloc->Contains(method)) {
675 // Check image space.
676 bool in_image = false;
677 for (auto& space : runtime->GetHeap()->GetContinuousSpaces()) {
678 if (space->IsImageSpace()) {
679 auto* image_space = space->AsImageSpace();
680 const auto& header = image_space->GetImageHeader();
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700681 const ImageSection& methods = header.GetMethodsSection();
682 const ImageSection& runtime_methods = header.GetRuntimeMethodsSection();
683 const size_t offset = reinterpret_cast<const uint8_t*>(method) - image_space->Begin();
684 if (methods.Contains(offset) || runtime_methods.Contains(offset)) {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700685 in_image = true;
686 break;
687 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700688 }
689 }
David Sehr709b0702016-10-13 09:12:37 -0700690 CHECK(in_image) << method->PrettyMethod() << " not in linear alloc or image";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700691 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700692 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800693 if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100694 AssertPcIsWithinQuickCode(method, cur_quick_frame_pc_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800695 // Frame sanity.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100696 size_t frame_size = GetCurrentQuickFrameInfo().FrameSizeInBytes();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800697 CHECK_NE(frame_size, 0u);
Andreas Gampe5b417b92014-03-10 14:18:35 -0700698 // A rough guess at an upper size we expect to see for a frame.
699 // 256 registers
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700700 // 2 words HandleScope overhead
Andreas Gampe5b417b92014-03-10 14:18:35 -0700701 // 3+3 register spills
702 // TODO: this seems architecture specific for the case of JNI frames.
Brian Carlstromed08bd42014-03-19 18:34:17 -0700703 // TODO: 083-compiler-regressions ManyFloatArgs shows this estimate is wrong.
704 // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word);
705 const size_t kMaxExpectedFrameSize = 2 * KB;
David Sehr709b0702016-10-13 09:12:37 -0700706 CHECK_LE(frame_size, kMaxExpectedFrameSize) << method->PrettyMethod();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100707 size_t return_pc_offset = GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800708 CHECK_LT(return_pc_offset, frame_size);
709 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700710 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700711}
712
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100713// Counts the number of references in the parameter list of the corresponding method.
714// Note: Thus does _not_ include "this" for non-static methods.
715static uint32_t GetNumberOfReferenceArgsWithoutReceiver(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700716 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100717 uint32_t shorty_len;
718 const char* shorty = method->GetShorty(&shorty_len);
719 uint32_t refs = 0;
720 for (uint32_t i = 1; i < shorty_len ; ++i) {
721 if (shorty[i] == 'L') {
722 refs++;
723 }
724 }
725 return refs;
726}
727
728QuickMethodFrameInfo StackVisitor::GetCurrentQuickFrameInfo() const {
729 if (cur_oat_quick_method_header_ != nullptr) {
730 return cur_oat_quick_method_header_->GetFrameInfo();
731 }
732
733 ArtMethod* method = GetMethod();
734 Runtime* runtime = Runtime::Current();
735
736 if (method->IsAbstract()) {
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100737 return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100738 }
739
740 // This goes before IsProxyMethod since runtime methods have a null declaring class.
741 if (method->IsRuntimeMethod()) {
742 return runtime->GetRuntimeMethodFrameInfo(method);
743 }
744
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100745 if (method->IsProxyMethod()) {
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000746 // There is only one direct method of a proxy class: the constructor. A direct method is
747 // cloned from the original java.lang.reflect.Proxy and is executed as usual quick
748 // compiled method without any stubs. Therefore the method must have a OatQuickMethodHeader.
749 DCHECK(!method->IsDirect() && !method->IsConstructor())
750 << "Constructors of proxy classes must have a OatQuickMethodHeader";
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100751 return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100752 }
753
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000754 // The only remaining case is if the method is native and uses the generic JNI stub.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100755 DCHECK(method->IsNative());
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000756 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampe542451c2016-07-26 09:02:02 -0700757 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(method,
758 kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700759 DCHECK(class_linker->IsQuickGenericJniStub(entry_point)) << method->PrettyMethod();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100760 // Generic JNI frame.
761 uint32_t handle_refs = GetNumberOfReferenceArgsWithoutReceiver(method) + 1;
762 size_t scope_size = HandleScope::SizeOf(handle_refs);
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100763 QuickMethodFrameInfo callee_info =
764 runtime->GetCalleeSaveMethodFrameInfo(Runtime::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100765
766 // Callee saves + handle scope + method ref + alignment
767 // Note: -sizeof(void*) since callee-save frame stores a whole method pointer.
768 size_t frame_size = RoundUp(
769 callee_info.FrameSizeInBytes() - sizeof(void*) + sizeof(ArtMethod*) + scope_size,
770 kStackAlignment);
771 return QuickMethodFrameInfo(frame_size, callee_info.CoreSpillMask(), callee_info.FpSpillMask());
772}
773
Andreas Gampe585da952016-12-02 14:52:29 -0800774template <StackVisitor::CountTransitions kCount>
Ian Rogers0399dde2012-06-06 17:09:28 -0700775void StackVisitor::WalkStack(bool include_transitions) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800776 DCHECK(thread_ == Thread::Current() || thread_->IsSuspended());
Ian Rogers62d6c772013-02-27 08:32:07 -0800777 CHECK_EQ(cur_depth_, 0U);
778 bool exit_stubs_installed = Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled();
Alex Lightb81a9842016-12-15 00:59:05 +0000779 uint32_t instrumentation_stack_depth = 0;
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000780 size_t inlined_frames_count = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700781
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700782 for (const ManagedStack* current_fragment = thread_->GetManagedStack();
783 current_fragment != nullptr; current_fragment = current_fragment->GetLink()) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700784 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
785 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700786 cur_quick_frame_pc_ = 0;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100787 cur_oat_quick_method_header_ = nullptr;
Dave Allisonf9439142014-03-27 15:10:22 -0700788
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700789 if (cur_quick_frame_ != nullptr) { // Handle quick stack frames.
Ian Rogers0399dde2012-06-06 17:09:28 -0700790 // Can't be both a shadow and a quick fragment.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700791 DCHECK(current_fragment->GetTopShadowFrame() == nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700792 ArtMethod* method = *cur_quick_frame_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700793 while (method != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100794 cur_oat_quick_method_header_ = method->GetOatQuickMethodHeader(cur_quick_frame_pc_);
Dave Allison5cd33752014-04-15 15:57:58 -0700795 SanityCheckFrame();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100796
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100797 if ((walk_kind_ == StackWalkKind::kIncludeInlinedFrames)
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100798 && (cur_oat_quick_method_header_ != nullptr)
799 && cur_oat_quick_method_header_->IsOptimized()) {
800 CodeInfo code_info = cur_oat_quick_method_header_->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000801 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100802 uint32_t native_pc_offset =
803 cur_oat_quick_method_header_->NativeQuickPcOffset(cur_quick_frame_pc_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100804 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
David Srbecky09ed0982016-02-12 21:58:43 +0000805 if (stack_map.IsValid() && stack_map.HasInlineInfo(encoding.stack_map_encoding)) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100806 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100807 DCHECK_EQ(current_inlining_depth_, 0u);
David Srbecky61b28a12016-02-25 21:55:03 +0000808 for (current_inlining_depth_ = inline_info.GetDepth(encoding.inline_info_encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100809 current_inlining_depth_ != 0;
810 --current_inlining_depth_) {
811 bool should_continue = VisitFrame();
812 if (UNLIKELY(!should_continue)) {
813 return;
814 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100815 cur_depth_++;
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000816 inlined_frames_count++;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100817 }
818 }
819 }
820
Dave Allison5cd33752014-04-15 15:57:58 -0700821 bool should_continue = VisitFrame();
822 if (UNLIKELY(!should_continue)) {
823 return;
Ian Rogers0399dde2012-06-06 17:09:28 -0700824 }
Dave Allison5cd33752014-04-15 15:57:58 -0700825
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100826 QuickMethodFrameInfo frame_info = GetCurrentQuickFrameInfo();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700827 if (context_ != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100828 context_->FillCalleeSaves(reinterpret_cast<uint8_t*>(cur_quick_frame_), frame_info);
Ian Rogers0399dde2012-06-06 17:09:28 -0700829 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700830 // Compute PC for next stack frame from return PC.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100831 size_t frame_size = frame_info.FrameSizeInBytes();
832 size_t return_pc_offset = frame_size - sizeof(void*);
Ian Rogers13735952014-10-08 12:43:28 -0700833 uint8_t* return_pc_addr = reinterpret_cast<uint8_t*>(cur_quick_frame_) + return_pc_offset;
Ian Rogers0399dde2012-06-06 17:09:28 -0700834 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100835
Ian Rogers62d6c772013-02-27 08:32:07 -0800836 if (UNLIKELY(exit_stubs_installed)) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700837 // While profiling, the return pc is restored from the side stack, except when walking
838 // the stack for an exception where the side stack will be unwound in VisitFrame.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700839 if (reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc) {
Andreas Gampe585da952016-12-02 14:52:29 -0800840 CHECK_LT(instrumentation_stack_depth, thread_->GetInstrumentationStack()->size());
Sebastien Hertz74e256b2013-10-04 10:40:37 +0200841 const instrumentation::InstrumentationStackFrame& instrumentation_frame =
Alex Lightb81a9842016-12-15 00:59:05 +0000842 thread_->GetInstrumentationStack()->at(instrumentation_stack_depth);
jeffhao725a9572012-11-13 18:20:12 -0800843 instrumentation_stack_depth++;
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100844 if (GetMethod() ==
845 Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveAllCalleeSaves)) {
Jeff Haofb2802d2013-07-24 13:53:05 -0700846 // Skip runtime save all callee frames which are used to deliver exceptions.
847 } else if (instrumentation_frame.interpreter_entry_) {
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100848 ArtMethod* callee =
849 Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveRefsAndArgs);
David Sehr709b0702016-10-13 09:12:37 -0700850 CHECK_EQ(GetMethod(), callee) << "Expected: " << ArtMethod::PrettyMethod(callee)
851 << " Found: " << ArtMethod::PrettyMethod(GetMethod());
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000852 } else {
853 CHECK_EQ(instrumentation_frame.method_, GetMethod())
David Sehr709b0702016-10-13 09:12:37 -0700854 << "Expected: " << ArtMethod::PrettyMethod(instrumentation_frame.method_)
855 << " Found: " << ArtMethod::PrettyMethod(GetMethod());
Ian Rogers62d6c772013-02-27 08:32:07 -0800856 }
857 if (num_frames_ != 0) {
858 // Check agreement of frame Ids only if num_frames_ is computed to avoid infinite
859 // recursion.
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000860 size_t frame_id = instrumentation::Instrumentation::ComputeFrameId(
861 thread_,
862 cur_depth_,
863 inlined_frames_count);
864 CHECK_EQ(instrumentation_frame.frame_id_, frame_id);
Ian Rogers62d6c772013-02-27 08:32:07 -0800865 }
jeffhao725a9572012-11-13 18:20:12 -0800866 return_pc = instrumentation_frame.return_pc_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700867 }
868 }
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100869
Ian Rogers0399dde2012-06-06 17:09:28 -0700870 cur_quick_frame_pc_ = return_pc;
Ian Rogers13735952014-10-08 12:43:28 -0700871 uint8_t* next_frame = reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700872 cur_quick_frame_ = reinterpret_cast<ArtMethod**>(next_frame);
873
874 if (kDebugStackWalk) {
David Sehr709b0702016-10-13 09:12:37 -0700875 LOG(INFO) << ArtMethod::PrettyMethod(method) << "@" << method << " size=" << frame_size
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100876 << std::boolalpha
877 << " optimized=" << (cur_oat_quick_method_header_ != nullptr &&
878 cur_oat_quick_method_header_->IsOptimized())
Mathieu Chartiere401d142015-04-22 13:56:20 -0700879 << " native=" << method->IsNative()
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100880 << std::noboolalpha
Mathieu Chartiere401d142015-04-22 13:56:20 -0700881 << " entrypoints=" << method->GetEntryPointFromQuickCompiledCode()
882 << "," << method->GetEntryPointFromJni()
Mathieu Chartiere401d142015-04-22 13:56:20 -0700883 << " next=" << *cur_quick_frame_;
884 }
885
Ian Rogers0399dde2012-06-06 17:09:28 -0700886 cur_depth_++;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700887 method = *cur_quick_frame_;
jeffhao6641ea12013-01-02 18:13:42 -0800888 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700889 } else if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700890 do {
891 SanityCheckFrame();
892 bool should_continue = VisitFrame();
893 if (UNLIKELY(!should_continue)) {
894 return;
895 }
896 cur_depth_++;
897 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700898 } while (cur_shadow_frame_ != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700899 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700900 if (include_transitions) {
901 bool should_continue = VisitFrame();
902 if (!should_continue) {
903 return;
904 }
905 }
Andreas Gampe585da952016-12-02 14:52:29 -0800906 if (kCount == CountTransitions::kYes) {
907 cur_depth_++;
908 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800909 }
910 if (num_frames_ != 0) {
911 CHECK_EQ(cur_depth_, num_frames_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700912 }
913}
914
Andreas Gampe585da952016-12-02 14:52:29 -0800915template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kYes>(bool);
916template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kNo>(bool);
917
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800918void JavaFrameRootInfo::Describe(std::ostream& os) const {
919 const StackVisitor* visitor = stack_visitor_;
920 CHECK(visitor != nullptr);
921 os << "Type=" << GetType() << " thread_id=" << GetThreadId() << " location=" <<
922 visitor->DescribeLocation() << " vreg=" << vreg_;
923}
924
Mathieu Chartiere401d142015-04-22 13:56:20 -0700925int StackVisitor::GetVRegOffsetFromQuickCode(const DexFile::CodeItem* code_item,
926 uint32_t core_spills, uint32_t fp_spills,
927 size_t frame_size, int reg, InstructionSet isa) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700928 PointerSize pointer_size = InstructionSetPointerSize(isa);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700929 if (kIsDebugBuild) {
930 auto* runtime = Runtime::Current();
931 if (runtime != nullptr) {
932 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
933 }
934 }
Roland Levillain14d90572015-07-16 10:52:26 +0100935 DCHECK_ALIGNED(frame_size, kStackAlignment);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700936 DCHECK_NE(reg, -1);
937 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
938 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
939 + sizeof(uint32_t); // Filler.
940 int num_regs = code_item->registers_size_ - code_item->ins_size_;
941 int temp_threshold = code_item->registers_size_;
942 const int max_num_special_temps = 1;
943 if (reg == temp_threshold) {
944 // The current method pointer corresponds to special location on stack.
945 return 0;
946 } else if (reg >= temp_threshold + max_num_special_temps) {
947 /*
948 * Special temporaries may have custom locations and the logic above deals with that.
949 * However, non-special temporaries are placed relative to the outs.
950 */
Andreas Gampe542451c2016-07-26 09:02:02 -0700951 int temps_start = code_item->outs_size_ * sizeof(uint32_t)
952 + static_cast<size_t>(pointer_size) /* art method */;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700953 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
954 return temps_start + relative_offset;
955 } else if (reg < num_regs) {
956 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
957 return locals_start + (reg * sizeof(uint32_t));
958 } else {
959 // Handle ins.
Andreas Gampe542451c2016-07-26 09:02:02 -0700960 return frame_size + ((reg - num_regs) * sizeof(uint32_t))
961 + static_cast<size_t>(pointer_size) /* art method */;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700962 }
963}
964
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700965void LockCountData::AddMonitor(Thread* self, mirror::Object* obj) {
Andreas Gampe03ec9302015-08-27 17:41:47 -0700966 if (obj == nullptr) {
967 return;
968 }
969
970 // If there's an error during enter, we won't have locked the monitor. So check there's no
971 // exception.
972 if (self->IsExceptionPending()) {
973 return;
974 }
975
976 if (monitors_ == nullptr) {
977 monitors_.reset(new std::vector<mirror::Object*>());
978 }
979 monitors_->push_back(obj);
980}
981
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700982void LockCountData::RemoveMonitorOrThrow(Thread* self, const mirror::Object* obj) {
Andreas Gampe03ec9302015-08-27 17:41:47 -0700983 if (obj == nullptr) {
984 return;
985 }
986 bool found_object = false;
987 if (monitors_ != nullptr) {
988 // We need to remove one pointer to ref, as duplicates are used for counting recursive locks.
989 // We arbitrarily choose the first one.
990 auto it = std::find(monitors_->begin(), monitors_->end(), obj);
991 if (it != monitors_->end()) {
992 monitors_->erase(it);
993 found_object = true;
994 }
995 }
996 if (!found_object) {
997 // The object wasn't found. Time for an IllegalMonitorStateException.
998 // The order here isn't fully clear. Assume that any other pending exception is swallowed.
999 // TODO: Maybe make already pending exception a suppressed exception.
1000 self->ClearException();
1001 self->ThrowNewExceptionF("Ljava/lang/IllegalMonitorStateException;",
1002 "did not lock monitor on object of type '%s' before unlocking",
David Sehr709b0702016-10-13 09:12:37 -07001003 const_cast<mirror::Object*>(obj)->PrettyTypeOf().c_str());
Andreas Gampe03ec9302015-08-27 17:41:47 -07001004 }
1005}
1006
1007// Helper to unlock a monitor. Must be NO_THREAD_SAFETY_ANALYSIS, as we can't statically show
1008// that the object was locked.
1009void MonitorExitHelper(Thread* self, mirror::Object* obj) NO_THREAD_SAFETY_ANALYSIS {
1010 DCHECK(self != nullptr);
1011 DCHECK(obj != nullptr);
1012 obj->MonitorExit(self);
1013}
1014
Andreas Gampe56fdd0e2016-04-28 14:56:54 -07001015bool LockCountData::CheckAllMonitorsReleasedOrThrow(Thread* self) {
Andreas Gampe03ec9302015-08-27 17:41:47 -07001016 DCHECK(self != nullptr);
1017 if (monitors_ != nullptr) {
1018 if (!monitors_->empty()) {
1019 // There may be an exception pending, if the method is terminating abruptly. Clear it.
1020 // TODO: Should we add this as a suppressed exception?
1021 self->ClearException();
1022
1023 // OK, there are monitors that are still locked. To enforce structured locking (and avoid
1024 // deadlocks) we unlock all of them before we raise the IllegalMonitorState exception.
1025 for (mirror::Object* obj : *monitors_) {
1026 MonitorExitHelper(self, obj);
1027 // If this raised an exception, ignore. TODO: Should we add this as suppressed
1028 // exceptions?
1029 if (self->IsExceptionPending()) {
1030 self->ClearException();
1031 }
1032 }
1033 // Raise an exception, just give the first object as the sample.
1034 mirror::Object* first = (*monitors_)[0];
1035 self->ThrowNewExceptionF("Ljava/lang/IllegalMonitorStateException;",
1036 "did not unlock monitor on object of type '%s'",
David Sehr709b0702016-10-13 09:12:37 -07001037 mirror::Object::PrettyTypeOf(first).c_str());
Andreas Gampe03ec9302015-08-27 17:41:47 -07001038
1039 // To make sure this path is not triggered again, clean out the monitors.
1040 monitors_->clear();
1041
1042 return false;
1043 }
1044 }
1045 return true;
1046}
1047
Elliott Hughes68e76522011-10-05 13:22:16 -07001048} // namespace art