blob: eec0460015ffebc14c253086800ac215c9c2ff3c [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 Gampe8228cdf2017-05-30 15:03:54 -070023#include "base/callee_save_type.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070024#include "base/enums.h"
Dave Allisonf9439142014-03-27 15:10:22 -070025#include "base/hex_dump.h"
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010026#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070027#include "entrypoints/runtime_asm_entrypoints.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "gc/space/image_space.h"
29#include "gc/space/space-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010030#include "jit/jit.h"
31#include "jit/jit_code_cache.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070032#include "linear_alloc.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070033#include "managed_stack.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070034#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035#include "mirror/object-inl.h"
36#include "mirror/object_array-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010037#include "oat_quick_method_header.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010038#include "quick/quick_method_frame_info.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070039#include "runtime.h"
Dave Allisonf9439142014-03-27 15:10:22 -070040#include "thread.h"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070041#include "thread_list.h"
Andreas Gampe90b936d2017-01-31 08:58:55 -080042#include "verify_object.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070043
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080044namespace art {
45
Andreas Gampe46ee31b2016-12-14 10:11:49 -080046using android::base::StringPrintf;
47
Mathieu Chartier8405bfd2016-02-05 12:00:49 -080048static constexpr bool kDebugStackWalk = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -070049
Ian Rogers62d6c772013-02-27 08:32:07 -080050mirror::Object* ShadowFrame::GetThisObject() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -070051 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -080052 if (m->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070053 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -080054 } else if (m->IsNative()) {
55 return GetVRegReference(0);
56 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070057 const DexFile::CodeItem* code_item = m->GetCodeItem();
David Sehr709b0702016-10-13 09:12:37 -070058 CHECK(code_item != nullptr) << ArtMethod::PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -080059 uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
60 return GetVRegReference(reg);
61 }
62}
63
Jeff Haoe701f482013-05-24 11:50:49 -070064mirror::Object* ShadowFrame::GetThisObject(uint16_t num_ins) const {
Mathieu Chartiere401d142015-04-22 13:56:20 -070065 ArtMethod* m = GetMethod();
Jeff Haoe701f482013-05-24 11:50:49 -070066 if (m->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070067 return nullptr;
Jeff Haoe701f482013-05-24 11:50:49 -070068 } else {
Jeff Hao8d448852013-06-03 17:26:19 -070069 return GetVRegReference(NumberOfVRegs() - num_ins);
Jeff Haoe701f482013-05-24 11:50:49 -070070 }
71}
72
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -080073StackVisitor::StackVisitor(Thread* thread,
74 Context* context,
75 StackWalkKind walk_kind,
76 bool check_suspended)
77 : StackVisitor(thread, context, walk_kind, 0, check_suspended) {}
Ian Rogers7a22fa62013-01-23 12:16:16 -080078
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010079StackVisitor::StackVisitor(Thread* thread,
80 Context* context,
81 StackWalkKind walk_kind,
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -080082 size_t num_frames,
83 bool check_suspended)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010084 : thread_(thread),
85 walk_kind_(walk_kind),
86 cur_shadow_frame_(nullptr),
87 cur_quick_frame_(nullptr),
88 cur_quick_frame_pc_(0),
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010089 cur_oat_quick_method_header_(nullptr),
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010090 num_frames_(num_frames),
91 cur_depth_(0),
Nicolas Geoffray57f61612015-05-15 13:20:41 +010092 current_inlining_depth_(0),
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -080093 context_(context),
94 check_suspended_(check_suspended) {
95 if (check_suspended_) {
96 DCHECK(thread == Thread::Current() || thread->IsSuspended()) << *thread;
97 }
Ian Rogers5cf98192014-05-29 21:31:50 -070098}
99
Mathieu Chartiere401d142015-04-22 13:56:20 -0700100InlineInfo StackVisitor::GetCurrentInlineInfo() const {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100101 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
102 uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_);
103 CodeInfo code_info = method_header->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000104 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Brazdilf677ebf2015-05-29 16:29:43 +0100105 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100106 DCHECK(stack_map.IsValid());
David Brazdilf677ebf2015-05-29 16:29:43 +0100107 return code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100108}
109
Mathieu Chartiere401d142015-04-22 13:56:20 -0700110ArtMethod* StackVisitor::GetMethod() const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100111 if (cur_shadow_frame_ != nullptr) {
112 return cur_shadow_frame_->GetMethod();
113 } else if (cur_quick_frame_ != nullptr) {
114 if (IsInInlinedFrame()) {
115 size_t depth_in_stack_map = current_inlining_depth_ - 1;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100116 InlineInfo inline_info = GetCurrentInlineInfo();
David Srbecky61b28a12016-02-25 21:55:03 +0000117 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
118 CodeInfoEncoding encoding = method_header->GetOptimizedCodeInfo().ExtractEncoding();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700119 MethodInfo method_info = method_header->GetOptimizedMethodInfo();
Mathieu Chartier45bf2502016-03-31 11:07:09 -0700120 DCHECK(walk_kind_ != StackWalkKind::kSkipInlinedFrames);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100121 return GetResolvedMethod(*GetCurrentQuickFrame(),
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700122 method_info,
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100123 inline_info,
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800124 encoding.inline_info.encoding,
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100125 depth_in_stack_map);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100126 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700127 return *cur_quick_frame_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100128 }
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100129 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700130 return nullptr;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100131}
132
Dave Allisonb373e092014-02-20 16:06:36 -0800133uint32_t StackVisitor::GetDexPc(bool abort_on_failure) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700134 if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700135 return cur_shadow_frame_->GetDexPC();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700136 } else if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100137 if (IsInInlinedFrame()) {
138 size_t depth_in_stack_map = current_inlining_depth_ - 1;
David Srbecky61b28a12016-02-25 21:55:03 +0000139 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
140 CodeInfoEncoding encoding = method_header->GetOptimizedCodeInfo().ExtractEncoding();
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800141 return GetCurrentInlineInfo().GetDexPcAtDepth(encoding.inline_info.encoding,
David Srbecky61b28a12016-02-25 21:55:03 +0000142 depth_in_stack_map);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100143 } else if (cur_oat_quick_method_header_ == nullptr) {
144 return DexFile::kDexNoIndex;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100145 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100146 return cur_oat_quick_method_header_->ToDexPc(
147 GetMethod(), cur_quick_frame_pc_, abort_on_failure);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100148 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700149 } else {
150 return 0;
151 }
152}
153
Mathieu Chartiere401d142015-04-22 13:56:20 -0700154extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700155 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertza836bc92014-11-25 16:30:53 +0100156
Ian Rogers62d6c772013-02-27 08:32:07 -0800157mirror::Object* StackVisitor::GetThisObject() const {
Andreas Gampe542451c2016-07-26 09:02:02 -0700158 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700159 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800160 if (m->IsStatic()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100161 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800162 } else if (m->IsNative()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100163 if (cur_quick_frame_ != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700164 HandleScope* hs = reinterpret_cast<HandleScope*>(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100165 reinterpret_cast<char*>(cur_quick_frame_) + sizeof(ArtMethod*));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700166 return hs->GetReference(0);
Ian Rogers62d6c772013-02-27 08:32:07 -0800167 } else {
168 return cur_shadow_frame_->GetVRegReference(0);
169 }
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000170 } else if (m->IsProxyMethod()) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100171 if (cur_quick_frame_ != nullptr) {
172 return artQuickGetProxyThisObject(cur_quick_frame_);
173 } else {
174 return cur_shadow_frame_->GetVRegReference(0);
175 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800176 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700177 const DexFile::CodeItem* code_item = m->GetCodeItem();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100178 if (code_item == nullptr) {
Ian Rogerse0dcd462014-03-08 15:21:04 -0800179 UNIMPLEMENTED(ERROR) << "Failed to determine this object of abstract or proxy method: "
David Sehr709b0702016-10-13 09:12:37 -0700180 << ArtMethod::PrettyMethod(m);
Ian Rogerse0dcd462014-03-08 15:21:04 -0800181 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800182 } else {
183 uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000184 uint32_t value = 0;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700185 bool success = GetVReg(m, reg, kReferenceVReg, &value);
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000186 // We currently always guarantee the `this` object is live throughout the method.
David Sehr709b0702016-10-13 09:12:37 -0700187 CHECK(success) << "Failed to read the this object in " << ArtMethod::PrettyMethod(m);
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000188 return reinterpret_cast<mirror::Object*>(value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800189 }
190 }
191}
192
Ian Rogers0c7abda2012-09-19 13:33:42 -0700193size_t StackVisitor::GetNativePcOffset() const {
194 DCHECK(!IsShadowFrame());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100195 return GetCurrentOatQuickMethodHeader()->NativeQuickPcOffset(cur_quick_frame_pc_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700196}
197
Mingyao Yang99170c62015-07-06 11:10:37 -0700198bool StackVisitor::GetVRegFromDebuggerShadowFrame(uint16_t vreg,
199 VRegKind kind,
200 uint32_t* val) const {
201 size_t frame_id = const_cast<StackVisitor*>(this)->GetFrameId();
202 ShadowFrame* shadow_frame = thread_->FindDebuggerShadowFrame(frame_id);
203 if (shadow_frame != nullptr) {
204 bool* updated_vreg_flags = thread_->GetUpdatedVRegFlags(frame_id);
205 DCHECK(updated_vreg_flags != nullptr);
206 if (updated_vreg_flags[vreg]) {
207 // Value is set by the debugger.
208 if (kind == kReferenceVReg) {
209 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
210 shadow_frame->GetVRegReference(vreg)));
211 } else {
212 *val = shadow_frame->GetVReg(vreg);
213 }
214 return true;
215 }
216 }
217 // No value is set by the debugger.
218 return false;
219}
220
Mathieu Chartiere401d142015-04-22 13:56:20 -0700221bool StackVisitor::GetVReg(ArtMethod* m, uint16_t vreg, VRegKind kind, uint32_t* val) const {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200222 if (cur_quick_frame_ != nullptr) {
223 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800224 DCHECK(m == GetMethod());
Mingyao Yang99170c62015-07-06 11:10:37 -0700225 // Check if there is value set by the debugger.
226 if (GetVRegFromDebuggerShadowFrame(vreg, kind, val)) {
227 return true;
228 }
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100229 DCHECK(cur_oat_quick_method_header_->IsOptimized());
230 return GetVRegFromOptimizedCode(m, vreg, kind, val);
Ian Rogers0399dde2012-06-06 17:09:28 -0700231 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100232 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertz09687442015-11-17 10:35:39 +0100233 if (kind == kReferenceVReg) {
234 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
235 cur_shadow_frame_->GetVRegReference(vreg)));
236 } else {
237 *val = cur_shadow_frame_->GetVReg(vreg);
238 }
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200239 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700240 }
241}
242
Mathieu Chartiere401d142015-04-22 13:56:20 -0700243bool StackVisitor::GetVRegFromOptimizedCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100244 uint32_t* val) const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100245 DCHECK_EQ(m, GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100246 const DexFile::CodeItem* code_item = m->GetCodeItem();
David Sehr709b0702016-10-13 09:12:37 -0700247 DCHECK(code_item != nullptr) << m->PrettyMethod(); // Can't be null or how would we compile
248 // its instructions?
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000249 uint16_t number_of_dex_registers = code_item->registers_size_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100250 DCHECK_LT(vreg, code_item->registers_size_);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100251 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
252 CodeInfo code_info = method_header->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000253 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100254
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100255 uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100256 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100257 DCHECK(stack_map.IsValid());
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100258 size_t depth_in_stack_map = current_inlining_depth_ - 1;
259
260 DexRegisterMap dex_register_map = IsInInlinedFrame()
David Brazdilf677ebf2015-05-29 16:29:43 +0100261 ? code_info.GetDexRegisterMapAtDepth(depth_in_stack_map,
262 code_info.GetInlineInfoOf(stack_map, encoding),
263 encoding,
264 number_of_dex_registers)
265 : code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100266
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000267 if (!dex_register_map.IsValid()) {
268 return false;
269 }
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000270 DexRegisterLocation::Kind location_kind =
David Brazdilf677ebf2015-05-29 16:29:43 +0100271 dex_register_map.GetLocationKind(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100272 switch (location_kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000273 case DexRegisterLocation::Kind::kInStack: {
David Brazdilf677ebf2015-05-29 16:29:43 +0100274 const int32_t offset = dex_register_map.GetStackOffsetInBytes(vreg,
275 number_of_dex_registers,
276 code_info,
277 encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100278 const uint8_t* addr = reinterpret_cast<const uint8_t*>(cur_quick_frame_) + offset;
279 *val = *reinterpret_cast<const uint32_t*>(addr);
280 return true;
281 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000282 case DexRegisterLocation::Kind::kInRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100283 case DexRegisterLocation::Kind::kInRegisterHigh:
284 case DexRegisterLocation::Kind::kInFpuRegister:
285 case DexRegisterLocation::Kind::kInFpuRegisterHigh: {
David Brazdilf677ebf2015-05-29 16:29:43 +0100286 uint32_t reg =
287 dex_register_map.GetMachineRegister(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100288 return GetRegisterIfAccessible(reg, kind, val);
289 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000290 case DexRegisterLocation::Kind::kConstant:
David Brazdilf677ebf2015-05-29 16:29:43 +0100291 *val = dex_register_map.GetConstant(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100292 return true;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000293 case DexRegisterLocation::Kind::kNone:
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100294 return false;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000295 default:
296 LOG(FATAL)
David Srbecky7dc11782016-02-25 13:23:56 +0000297 << "Unexpected location kind "
298 << dex_register_map.GetLocationInternalKind(vreg,
299 number_of_dex_registers,
300 code_info,
301 encoding);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000302 UNREACHABLE();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100303 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100304}
305
306bool StackVisitor::GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const {
307 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
David Brazdil77a48ae2015-09-15 12:34:04 +0000308
Vladimir Marko239d6ea2016-09-05 10:44:04 +0100309 if (kRuntimeISA == InstructionSet::kX86 && is_float) {
310 // X86 float registers are 64-bit and each XMM register is provided as two separate
311 // 32-bit registers by the context.
312 reg = (kind == kDoubleHiVReg) ? (2 * reg + 1) : (2 * reg);
313 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000314
Goran Jakovljevic986660c2015-12-10 11:44:50 +0100315 // MIPS32 float registers are used as 64-bit (for MIPS32r2 it is pair
316 // F(2n)-F(2n+1), and for MIPS32r6 it is 64-bit register F(2n)). When
317 // accessing upper 32-bits from double, reg + 1 should be used.
318 if ((kRuntimeISA == InstructionSet::kMips) && (kind == kDoubleHiVReg)) {
319 DCHECK_ALIGNED(reg, 2);
320 reg++;
321 }
322
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100323 if (!IsAccessibleRegister(reg, is_float)) {
324 return false;
325 }
326 uintptr_t ptr_val = GetRegister(reg, is_float);
327 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
328 if (target64) {
329 const bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
330 const bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
331 int64_t value_long = static_cast<int64_t>(ptr_val);
332 if (wide_lo) {
333 ptr_val = static_cast<uintptr_t>(Low32Bits(value_long));
334 } else if (wide_hi) {
335 ptr_val = static_cast<uintptr_t>(High32Bits(value_long));
336 }
337 }
338 *val = ptr_val;
339 return true;
340}
341
Mingyao Yang99170c62015-07-06 11:10:37 -0700342bool StackVisitor::GetVRegPairFromDebuggerShadowFrame(uint16_t vreg,
343 VRegKind kind_lo,
344 VRegKind kind_hi,
345 uint64_t* val) const {
346 uint32_t low_32bits;
347 uint32_t high_32bits;
348 bool success = GetVRegFromDebuggerShadowFrame(vreg, kind_lo, &low_32bits);
349 success &= GetVRegFromDebuggerShadowFrame(vreg + 1, kind_hi, &high_32bits);
350 if (success) {
351 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
352 }
353 return success;
354}
355
Mathieu Chartiere401d142015-04-22 13:56:20 -0700356bool StackVisitor::GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200357 VRegKind kind_hi, uint64_t* val) const {
358 if (kind_lo == kLongLoVReg) {
359 DCHECK_EQ(kind_hi, kLongHiVReg);
360 } else if (kind_lo == kDoubleLoVReg) {
361 DCHECK_EQ(kind_hi, kDoubleHiVReg);
362 } else {
363 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100364 UNREACHABLE();
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200365 }
Mingyao Yang99170c62015-07-06 11:10:37 -0700366 // Check if there is value set by the debugger.
367 if (GetVRegPairFromDebuggerShadowFrame(vreg, kind_lo, kind_hi, val)) {
368 return true;
369 }
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200370 if (cur_quick_frame_ != nullptr) {
371 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
372 DCHECK(m == GetMethod());
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100373 DCHECK(cur_oat_quick_method_header_->IsOptimized());
374 return GetVRegPairFromOptimizedCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200375 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100376 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200377 *val = cur_shadow_frame_->GetVRegLong(vreg);
378 return true;
379 }
380}
381
Mathieu Chartiere401d142015-04-22 13:56:20 -0700382bool StackVisitor::GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100383 VRegKind kind_lo, VRegKind kind_hi,
384 uint64_t* val) const {
385 uint32_t low_32bits;
386 uint32_t high_32bits;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700387 bool success = GetVRegFromOptimizedCode(m, vreg, kind_lo, &low_32bits);
388 success &= GetVRegFromOptimizedCode(m, vreg + 1, kind_hi, &high_32bits);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100389 if (success) {
390 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
391 }
392 return success;
393}
394
395bool StackVisitor::GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
396 VRegKind kind_lo, uint64_t* val) const {
397 const bool is_float = (kind_lo == kDoubleLoVReg);
398 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
399 return false;
400 }
401 uintptr_t ptr_val_lo = GetRegister(reg_lo, is_float);
402 uintptr_t ptr_val_hi = GetRegister(reg_hi, is_float);
403 bool target64 = Is64BitInstructionSet(kRuntimeISA);
404 if (target64) {
405 int64_t value_long_lo = static_cast<int64_t>(ptr_val_lo);
406 int64_t value_long_hi = static_cast<int64_t>(ptr_val_hi);
407 ptr_val_lo = static_cast<uintptr_t>(Low32Bits(value_long_lo));
408 ptr_val_hi = static_cast<uintptr_t>(High32Bits(value_long_hi));
409 }
410 *val = (static_cast<uint64_t>(ptr_val_hi) << 32) | static_cast<uint32_t>(ptr_val_lo);
411 return true;
412}
413
Mingyao Yang636b9252015-07-31 16:40:24 -0700414bool StackVisitor::SetVReg(ArtMethod* m,
415 uint16_t vreg,
416 uint32_t new_value,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800417 VRegKind kind) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700418 const DexFile::CodeItem* code_item = m->GetCodeItem();
419 if (code_item == nullptr) {
420 return false;
421 }
422 ShadowFrame* shadow_frame = GetCurrentShadowFrame();
423 if (shadow_frame == nullptr) {
424 // This is a compiled frame: we must prepare and update a shadow frame that will
425 // be executed by the interpreter after deoptimization of the stack.
426 const size_t frame_id = GetFrameId();
427 const uint16_t num_regs = code_item->registers_size_;
428 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
429 CHECK(shadow_frame != nullptr);
430 // Remember the vreg has been set for debugging and must not be overwritten by the
431 // original value during deoptimization of the stack.
432 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
433 }
434 if (kind == kReferenceVReg) {
435 shadow_frame->SetVRegReference(vreg, reinterpret_cast<mirror::Object*>(new_value));
436 } else {
437 shadow_frame->SetVReg(vreg, new_value);
438 }
439 return true;
440}
441
Mingyao Yang636b9252015-07-31 16:40:24 -0700442bool StackVisitor::SetVRegPair(ArtMethod* m,
443 uint16_t vreg,
444 uint64_t new_value,
445 VRegKind kind_lo,
446 VRegKind kind_hi) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700447 if (kind_lo == kLongLoVReg) {
448 DCHECK_EQ(kind_hi, kLongHiVReg);
449 } else if (kind_lo == kDoubleLoVReg) {
450 DCHECK_EQ(kind_hi, kDoubleHiVReg);
451 } else {
452 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
453 UNREACHABLE();
454 }
455 const DexFile::CodeItem* code_item = m->GetCodeItem();
456 if (code_item == nullptr) {
457 return false;
458 }
459 ShadowFrame* shadow_frame = GetCurrentShadowFrame();
460 if (shadow_frame == nullptr) {
461 // This is a compiled frame: we must prepare for deoptimization (see SetVRegFromDebugger).
462 const size_t frame_id = GetFrameId();
463 const uint16_t num_regs = code_item->registers_size_;
464 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
465 CHECK(shadow_frame != nullptr);
466 // Remember the vreg pair has been set for debugging and must not be overwritten by the
467 // original value during deoptimization of the stack.
468 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
469 thread_->GetUpdatedVRegFlags(frame_id)[vreg + 1] = true;
470 }
471 shadow_frame->SetVRegLong(vreg, new_value);
472 return true;
473}
474
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100475bool StackVisitor::IsAccessibleGPR(uint32_t reg) const {
476 DCHECK(context_ != nullptr);
477 return context_->IsAccessibleGPR(reg);
478}
479
Mathieu Chartier815873e2014-02-13 18:02:13 -0800480uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100481 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
482 DCHECK(context_ != nullptr);
Mathieu Chartier815873e2014-02-13 18:02:13 -0800483 return context_->GetGPRAddress(reg);
484}
485
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100486uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
487 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
488 DCHECK(context_ != nullptr);
489 return context_->GetGPR(reg);
Ian Rogers0399dde2012-06-06 17:09:28 -0700490}
491
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100492bool StackVisitor::IsAccessibleFPR(uint32_t reg) const {
493 DCHECK(context_ != nullptr);
494 return context_->IsAccessibleFPR(reg);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200495}
496
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100497uintptr_t StackVisitor::GetFPR(uint32_t reg) const {
498 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
499 DCHECK(context_ != nullptr);
500 return context_->GetFPR(reg);
501}
502
Ian Rogers0399dde2012-06-06 17:09:28 -0700503uintptr_t StackVisitor::GetReturnPc() const {
Ian Rogers13735952014-10-08 12:43:28 -0700504 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700505 DCHECK(sp != nullptr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100506 uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogers0399dde2012-06-06 17:09:28 -0700507 return *reinterpret_cast<uintptr_t*>(pc_addr);
508}
509
510void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
Ian Rogers13735952014-10-08 12:43:28 -0700511 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700512 CHECK(sp != nullptr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100513 uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogers0399dde2012-06-06 17:09:28 -0700514 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc;
515}
516
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100517size_t StackVisitor::ComputeNumFrames(Thread* thread, StackWalkKind walk_kind) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700518 struct NumFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100519 NumFramesVisitor(Thread* thread_in, StackWalkKind walk_kind_in)
520 : StackVisitor(thread_in, nullptr, walk_kind_in), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700521
Ian Rogers5cf98192014-05-29 21:31:50 -0700522 bool VisitFrame() OVERRIDE {
Ian Rogers0399dde2012-06-06 17:09:28 -0700523 frames++;
524 return true;
525 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700526
Ian Rogers0399dde2012-06-06 17:09:28 -0700527 size_t frames;
528 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100529 NumFramesVisitor visitor(thread, walk_kind);
Ian Rogers0399dde2012-06-06 17:09:28 -0700530 visitor.WalkStack(true);
531 return visitor.frames;
532}
533
Mathieu Chartiere401d142015-04-22 13:56:20 -0700534bool StackVisitor::GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700535 struct HasMoreFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100536 HasMoreFramesVisitor(Thread* thread,
537 StackWalkKind walk_kind,
538 size_t num_frames,
539 size_t frame_height)
540 : StackVisitor(thread, nullptr, walk_kind, num_frames),
541 frame_height_(frame_height),
542 found_frame_(false),
543 has_more_frames_(false),
544 next_method_(nullptr),
545 next_dex_pc_(0) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700546 }
547
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700548 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700549 if (found_frame_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700550 ArtMethod* method = GetMethod();
Ian Rogers5cf98192014-05-29 21:31:50 -0700551 if (method != nullptr && !method->IsRuntimeMethod()) {
552 has_more_frames_ = true;
553 next_method_ = method;
554 next_dex_pc_ = GetDexPc();
555 return false; // End stack walk once next method is found.
556 }
557 } else if (GetFrameHeight() == frame_height_) {
558 found_frame_ = true;
559 }
560 return true;
561 }
562
563 size_t frame_height_;
564 bool found_frame_;
565 bool has_more_frames_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700566 ArtMethod* next_method_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700567 uint32_t next_dex_pc_;
568 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100569 HasMoreFramesVisitor visitor(thread_, walk_kind_, GetNumFrames(), GetFrameHeight());
Ian Rogers5cf98192014-05-29 21:31:50 -0700570 visitor.WalkStack(true);
571 *next_method = visitor.next_method_;
572 *next_dex_pc = visitor.next_dex_pc_;
573 return visitor.has_more_frames_;
574}
575
Ian Rogers7a22fa62013-01-23 12:16:16 -0800576void StackVisitor::DescribeStack(Thread* thread) {
Ian Rogers306057f2012-11-26 12:45:53 -0800577 struct DescribeStackVisitor : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800578 explicit DescribeStackVisitor(Thread* thread_in)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100579 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800580
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700581 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers306057f2012-11-26 12:45:53 -0800582 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation();
583 return true;
584 }
585 };
Ian Rogers7a22fa62013-01-23 12:16:16 -0800586 DescribeStackVisitor visitor(thread);
Ian Rogers306057f2012-11-26 12:45:53 -0800587 visitor.WalkStack(true);
588}
589
Ian Rogers40e3bac2012-11-20 00:09:14 -0800590std::string StackVisitor::DescribeLocation() const {
591 std::string result("Visiting method '");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700592 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700593 if (m == nullptr) {
Ian Rogers306057f2012-11-26 12:45:53 -0800594 return "upcall";
595 }
David Sehr709b0702016-10-13 09:12:37 -0700596 result += m->PrettyMethod();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800597 result += StringPrintf("' at dex PC 0x%04x", GetDexPc());
Ian Rogers40e3bac2012-11-20 00:09:14 -0800598 if (!IsShadowFrame()) {
599 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc()));
600 }
601 return result;
602}
603
Alex Lightdba61482016-12-21 08:20:29 -0800604void StackVisitor::SetMethod(ArtMethod* method) {
605 DCHECK(GetMethod() != nullptr);
606 if (cur_shadow_frame_ != nullptr) {
607 cur_shadow_frame_->SetMethod(method);
608 } else {
609 DCHECK(cur_quick_frame_ != nullptr);
610 CHECK(!IsInInlinedFrame()) << "We do not support setting inlined method's ArtMethod!";
Alex Light1ebe4fe2017-01-30 14:57:11 -0800611 *cur_quick_frame_ = method;
Alex Lightdba61482016-12-21 08:20:29 -0800612 }
613}
614
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100615static void AssertPcIsWithinQuickCode(ArtMethod* method, uintptr_t pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700616 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100617 if (method->IsNative() || method->IsRuntimeMethod() || method->IsProxyMethod()) {
618 return;
619 }
620
621 if (pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) {
622 return;
623 }
624
Mingyao Yang88ca8ba2017-05-23 14:21:07 -0700625 Runtime* runtime = Runtime::Current();
626 if (runtime->UseJitCompilation() &&
627 runtime->GetJit()->GetCodeCache()->ContainsPc(reinterpret_cast<const void*>(pc))) {
628 return;
629 }
630
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100631 const void* code = method->GetEntryPointFromQuickCompiledCode();
Alex Lightdb01a092017-04-03 15:39:55 -0700632 if (code == GetQuickInstrumentationEntryPoint() || code == GetInvokeObsoleteMethodStub()) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100633 return;
634 }
635
636 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
637 if (class_linker->IsQuickToInterpreterBridge(code) ||
638 class_linker->IsQuickResolutionStub(code)) {
639 return;
640 }
641
Calin Juravleffc87072016-04-20 14:22:09 +0100642 if (runtime->UseJitCompilation() && runtime->GetJit()->GetCodeCache()->ContainsPc(code)) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100643 return;
644 }
645
Mingyao Yang063fc772016-08-02 11:02:54 -0700646 uint32_t code_size = OatQuickMethodHeader::FromEntryPoint(code)->GetCodeSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100647 uintptr_t code_start = reinterpret_cast<uintptr_t>(code);
648 CHECK(code_start <= pc && pc <= (code_start + code_size))
David Sehr709b0702016-10-13 09:12:37 -0700649 << method->PrettyMethod()
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100650 << " pc=" << std::hex << pc
Roland Levillain0d5a2812015-11-13 10:07:31 +0000651 << " code_start=" << code_start
652 << " code_size=" << code_size;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100653}
654
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700655void StackVisitor::SanityCheckFrame() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800656 if (kIsDebugBuild) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700657 ArtMethod* method = GetMethod();
658 auto* declaring_class = method->GetDeclaringClass();
659 // Runtime methods have null declaring class.
660 if (!method->IsRuntimeMethod()) {
661 CHECK(declaring_class != nullptr);
662 CHECK_EQ(declaring_class->GetClass(), declaring_class->GetClass()->GetClass())
663 << declaring_class;
664 } else {
665 CHECK(declaring_class == nullptr);
666 }
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700667 Runtime* const runtime = Runtime::Current();
668 LinearAlloc* const linear_alloc = runtime->GetLinearAlloc();
669 if (!linear_alloc->Contains(method)) {
670 // Check class linker linear allocs.
671 mirror::Class* klass = method->GetDeclaringClass();
672 LinearAlloc* const class_linear_alloc = (klass != nullptr)
Mathieu Chartier5b830502016-03-02 10:30:23 -0800673 ? runtime->GetClassLinker()->GetAllocatorForClassLoader(klass->GetClassLoader())
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700674 : linear_alloc;
675 if (!class_linear_alloc->Contains(method)) {
676 // Check image space.
677 bool in_image = false;
678 for (auto& space : runtime->GetHeap()->GetContinuousSpaces()) {
679 if (space->IsImageSpace()) {
680 auto* image_space = space->AsImageSpace();
681 const auto& header = image_space->GetImageHeader();
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700682 const ImageSection& methods = header.GetMethodsSection();
683 const ImageSection& runtime_methods = header.GetRuntimeMethodsSection();
684 const size_t offset = reinterpret_cast<const uint8_t*>(method) - image_space->Begin();
685 if (methods.Contains(offset) || runtime_methods.Contains(offset)) {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700686 in_image = true;
687 break;
688 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700689 }
690 }
David Sehr709b0702016-10-13 09:12:37 -0700691 CHECK(in_image) << method->PrettyMethod() << " not in linear alloc or image";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700692 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700693 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800694 if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100695 AssertPcIsWithinQuickCode(method, cur_quick_frame_pc_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800696 // Frame sanity.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100697 size_t frame_size = GetCurrentQuickFrameInfo().FrameSizeInBytes();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800698 CHECK_NE(frame_size, 0u);
Andreas Gampe5b417b92014-03-10 14:18:35 -0700699 // A rough guess at an upper size we expect to see for a frame.
700 // 256 registers
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700701 // 2 words HandleScope overhead
Andreas Gampe5b417b92014-03-10 14:18:35 -0700702 // 3+3 register spills
703 // TODO: this seems architecture specific for the case of JNI frames.
Brian Carlstromed08bd42014-03-19 18:34:17 -0700704 // TODO: 083-compiler-regressions ManyFloatArgs shows this estimate is wrong.
705 // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word);
706 const size_t kMaxExpectedFrameSize = 2 * KB;
David Sehr709b0702016-10-13 09:12:37 -0700707 CHECK_LE(frame_size, kMaxExpectedFrameSize) << method->PrettyMethod();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100708 size_t return_pc_offset = GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800709 CHECK_LT(return_pc_offset, frame_size);
710 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700711 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700712}
713
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100714// Counts the number of references in the parameter list of the corresponding method.
715// Note: Thus does _not_ include "this" for non-static methods.
716static uint32_t GetNumberOfReferenceArgsWithoutReceiver(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700717 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100718 uint32_t shorty_len;
719 const char* shorty = method->GetShorty(&shorty_len);
720 uint32_t refs = 0;
721 for (uint32_t i = 1; i < shorty_len ; ++i) {
722 if (shorty[i] == 'L') {
723 refs++;
724 }
725 }
726 return refs;
727}
728
729QuickMethodFrameInfo StackVisitor::GetCurrentQuickFrameInfo() const {
730 if (cur_oat_quick_method_header_ != nullptr) {
731 return cur_oat_quick_method_header_->GetFrameInfo();
732 }
733
734 ArtMethod* method = GetMethod();
735 Runtime* runtime = Runtime::Current();
736
737 if (method->IsAbstract()) {
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700738 return runtime->GetCalleeSaveMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100739 }
740
741 // This goes before IsProxyMethod since runtime methods have a null declaring class.
742 if (method->IsRuntimeMethod()) {
743 return runtime->GetRuntimeMethodFrameInfo(method);
744 }
745
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100746 if (method->IsProxyMethod()) {
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000747 // There is only one direct method of a proxy class: the constructor. A direct method is
748 // cloned from the original java.lang.reflect.Proxy and is executed as usual quick
749 // compiled method without any stubs. Therefore the method must have a OatQuickMethodHeader.
750 DCHECK(!method->IsDirect() && !method->IsConstructor())
751 << "Constructors of proxy classes must have a OatQuickMethodHeader";
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700752 return runtime->GetCalleeSaveMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100753 }
754
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000755 // The only remaining case is if the method is native and uses the generic JNI stub.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100756 DCHECK(method->IsNative());
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000757 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampe542451c2016-07-26 09:02:02 -0700758 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(method,
759 kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700760 DCHECK(class_linker->IsQuickGenericJniStub(entry_point)) << method->PrettyMethod();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100761 // Generic JNI frame.
762 uint32_t handle_refs = GetNumberOfReferenceArgsWithoutReceiver(method) + 1;
763 size_t scope_size = HandleScope::SizeOf(handle_refs);
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100764 QuickMethodFrameInfo callee_info =
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700765 runtime->GetCalleeSaveMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100766
767 // Callee saves + handle scope + method ref + alignment
768 // Note: -sizeof(void*) since callee-save frame stores a whole method pointer.
769 size_t frame_size = RoundUp(
770 callee_info.FrameSizeInBytes() - sizeof(void*) + sizeof(ArtMethod*) + scope_size,
771 kStackAlignment);
772 return QuickMethodFrameInfo(frame_size, callee_info.CoreSpillMask(), callee_info.FpSpillMask());
773}
774
Andreas Gampe585da952016-12-02 14:52:29 -0800775template <StackVisitor::CountTransitions kCount>
Ian Rogers0399dde2012-06-06 17:09:28 -0700776void StackVisitor::WalkStack(bool include_transitions) {
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -0800777 if (check_suspended_) {
778 DCHECK(thread_ == Thread::Current() || thread_->IsSuspended());
779 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800780 CHECK_EQ(cur_depth_, 0U);
781 bool exit_stubs_installed = Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled();
Alex Lightb81a9842016-12-15 00:59:05 +0000782 uint32_t instrumentation_stack_depth = 0;
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000783 size_t inlined_frames_count = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700784
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700785 for (const ManagedStack* current_fragment = thread_->GetManagedStack();
786 current_fragment != nullptr; current_fragment = current_fragment->GetLink()) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700787 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
788 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700789 cur_quick_frame_pc_ = 0;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100790 cur_oat_quick_method_header_ = nullptr;
Dave Allisonf9439142014-03-27 15:10:22 -0700791
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700792 if (cur_quick_frame_ != nullptr) { // Handle quick stack frames.
Ian Rogers0399dde2012-06-06 17:09:28 -0700793 // Can't be both a shadow and a quick fragment.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700794 DCHECK(current_fragment->GetTopShadowFrame() == nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700795 ArtMethod* method = *cur_quick_frame_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700796 while (method != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100797 cur_oat_quick_method_header_ = method->GetOatQuickMethodHeader(cur_quick_frame_pc_);
Dave Allison5cd33752014-04-15 15:57:58 -0700798 SanityCheckFrame();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100799
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100800 if ((walk_kind_ == StackWalkKind::kIncludeInlinedFrames)
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100801 && (cur_oat_quick_method_header_ != nullptr)
802 && cur_oat_quick_method_header_->IsOptimized()) {
803 CodeInfo code_info = cur_oat_quick_method_header_->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000804 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100805 uint32_t native_pc_offset =
806 cur_oat_quick_method_header_->NativeQuickPcOffset(cur_quick_frame_pc_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100807 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800808 if (stack_map.IsValid() && stack_map.HasInlineInfo(encoding.stack_map.encoding)) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100809 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100810 DCHECK_EQ(current_inlining_depth_, 0u);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800811 for (current_inlining_depth_ = inline_info.GetDepth(encoding.inline_info.encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100812 current_inlining_depth_ != 0;
813 --current_inlining_depth_) {
814 bool should_continue = VisitFrame();
815 if (UNLIKELY(!should_continue)) {
816 return;
817 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100818 cur_depth_++;
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000819 inlined_frames_count++;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100820 }
821 }
822 }
823
Dave Allison5cd33752014-04-15 15:57:58 -0700824 bool should_continue = VisitFrame();
825 if (UNLIKELY(!should_continue)) {
826 return;
Ian Rogers0399dde2012-06-06 17:09:28 -0700827 }
Dave Allison5cd33752014-04-15 15:57:58 -0700828
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100829 QuickMethodFrameInfo frame_info = GetCurrentQuickFrameInfo();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700830 if (context_ != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100831 context_->FillCalleeSaves(reinterpret_cast<uint8_t*>(cur_quick_frame_), frame_info);
Ian Rogers0399dde2012-06-06 17:09:28 -0700832 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700833 // Compute PC for next stack frame from return PC.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100834 size_t frame_size = frame_info.FrameSizeInBytes();
835 size_t return_pc_offset = frame_size - sizeof(void*);
Ian Rogers13735952014-10-08 12:43:28 -0700836 uint8_t* return_pc_addr = reinterpret_cast<uint8_t*>(cur_quick_frame_) + return_pc_offset;
Ian Rogers0399dde2012-06-06 17:09:28 -0700837 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100838
Ian Rogers62d6c772013-02-27 08:32:07 -0800839 if (UNLIKELY(exit_stubs_installed)) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700840 // While profiling, the return pc is restored from the side stack, except when walking
841 // the stack for an exception where the side stack will be unwound in VisitFrame.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700842 if (reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc) {
Andreas Gampe585da952016-12-02 14:52:29 -0800843 CHECK_LT(instrumentation_stack_depth, thread_->GetInstrumentationStack()->size());
Sebastien Hertz74e256b2013-10-04 10:40:37 +0200844 const instrumentation::InstrumentationStackFrame& instrumentation_frame =
Alex Lightb81a9842016-12-15 00:59:05 +0000845 thread_->GetInstrumentationStack()->at(instrumentation_stack_depth);
jeffhao725a9572012-11-13 18:20:12 -0800846 instrumentation_stack_depth++;
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100847 if (GetMethod() ==
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700848 Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveAllCalleeSaves)) {
Jeff Haofb2802d2013-07-24 13:53:05 -0700849 // Skip runtime save all callee frames which are used to deliver exceptions.
850 } else if (instrumentation_frame.interpreter_entry_) {
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100851 ArtMethod* callee =
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700852 Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs);
David Sehr709b0702016-10-13 09:12:37 -0700853 CHECK_EQ(GetMethod(), callee) << "Expected: " << ArtMethod::PrettyMethod(callee)
854 << " Found: " << ArtMethod::PrettyMethod(GetMethod());
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000855 } else {
Alex Lighteee0bd42017-02-14 15:31:45 +0000856 // Instrumentation generally doesn't distinguish between a method's obsolete and
857 // non-obsolete version.
858 CHECK_EQ(instrumentation_frame.method_->GetNonObsoleteMethod(),
859 GetMethod()->GetNonObsoleteMethod())
860 << "Expected: "
861 << ArtMethod::PrettyMethod(instrumentation_frame.method_->GetNonObsoleteMethod())
862 << " Found: " << ArtMethod::PrettyMethod(GetMethod()->GetNonObsoleteMethod());
Ian Rogers62d6c772013-02-27 08:32:07 -0800863 }
864 if (num_frames_ != 0) {
865 // Check agreement of frame Ids only if num_frames_ is computed to avoid infinite
866 // recursion.
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000867 size_t frame_id = instrumentation::Instrumentation::ComputeFrameId(
868 thread_,
869 cur_depth_,
870 inlined_frames_count);
871 CHECK_EQ(instrumentation_frame.frame_id_, frame_id);
Ian Rogers62d6c772013-02-27 08:32:07 -0800872 }
jeffhao725a9572012-11-13 18:20:12 -0800873 return_pc = instrumentation_frame.return_pc_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700874 }
875 }
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100876
Ian Rogers0399dde2012-06-06 17:09:28 -0700877 cur_quick_frame_pc_ = return_pc;
Ian Rogers13735952014-10-08 12:43:28 -0700878 uint8_t* next_frame = reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700879 cur_quick_frame_ = reinterpret_cast<ArtMethod**>(next_frame);
880
881 if (kDebugStackWalk) {
David Sehr709b0702016-10-13 09:12:37 -0700882 LOG(INFO) << ArtMethod::PrettyMethod(method) << "@" << method << " size=" << frame_size
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100883 << std::boolalpha
884 << " optimized=" << (cur_oat_quick_method_header_ != nullptr &&
885 cur_oat_quick_method_header_->IsOptimized())
Mathieu Chartiere401d142015-04-22 13:56:20 -0700886 << " native=" << method->IsNative()
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100887 << std::noboolalpha
Mathieu Chartiere401d142015-04-22 13:56:20 -0700888 << " entrypoints=" << method->GetEntryPointFromQuickCompiledCode()
Alex Lighteee0bd42017-02-14 15:31:45 +0000889 << "," << (method->IsNative() ? method->GetEntryPointFromJni() : nullptr)
Mathieu Chartiere401d142015-04-22 13:56:20 -0700890 << " next=" << *cur_quick_frame_;
891 }
892
Andreas Gampef040be62017-04-14 21:49:33 -0700893 if (kCount == CountTransitions::kYes || !method->IsRuntimeMethod()) {
894 cur_depth_++;
895 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700896 method = *cur_quick_frame_;
jeffhao6641ea12013-01-02 18:13:42 -0800897 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700898 } else if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700899 do {
900 SanityCheckFrame();
901 bool should_continue = VisitFrame();
902 if (UNLIKELY(!should_continue)) {
903 return;
904 }
905 cur_depth_++;
906 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700907 } while (cur_shadow_frame_ != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700908 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700909 if (include_transitions) {
910 bool should_continue = VisitFrame();
911 if (!should_continue) {
912 return;
913 }
914 }
Andreas Gampe585da952016-12-02 14:52:29 -0800915 if (kCount == CountTransitions::kYes) {
916 cur_depth_++;
917 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800918 }
919 if (num_frames_ != 0) {
920 CHECK_EQ(cur_depth_, num_frames_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700921 }
922}
923
Andreas Gampe585da952016-12-02 14:52:29 -0800924template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kYes>(bool);
925template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kNo>(bool);
926
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800927void JavaFrameRootInfo::Describe(std::ostream& os) const {
928 const StackVisitor* visitor = stack_visitor_;
929 CHECK(visitor != nullptr);
930 os << "Type=" << GetType() << " thread_id=" << GetThreadId() << " location=" <<
931 visitor->DescribeLocation() << " vreg=" << vreg_;
932}
933
Mathieu Chartiere401d142015-04-22 13:56:20 -0700934int StackVisitor::GetVRegOffsetFromQuickCode(const DexFile::CodeItem* code_item,
935 uint32_t core_spills, uint32_t fp_spills,
936 size_t frame_size, int reg, InstructionSet isa) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700937 PointerSize pointer_size = InstructionSetPointerSize(isa);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700938 if (kIsDebugBuild) {
939 auto* runtime = Runtime::Current();
940 if (runtime != nullptr) {
941 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
942 }
943 }
Roland Levillain14d90572015-07-16 10:52:26 +0100944 DCHECK_ALIGNED(frame_size, kStackAlignment);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700945 DCHECK_NE(reg, -1);
946 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
947 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
948 + sizeof(uint32_t); // Filler.
949 int num_regs = code_item->registers_size_ - code_item->ins_size_;
950 int temp_threshold = code_item->registers_size_;
951 const int max_num_special_temps = 1;
952 if (reg == temp_threshold) {
953 // The current method pointer corresponds to special location on stack.
954 return 0;
955 } else if (reg >= temp_threshold + max_num_special_temps) {
956 /*
957 * Special temporaries may have custom locations and the logic above deals with that.
958 * However, non-special temporaries are placed relative to the outs.
959 */
Andreas Gampe542451c2016-07-26 09:02:02 -0700960 int temps_start = code_item->outs_size_ * sizeof(uint32_t)
961 + static_cast<size_t>(pointer_size) /* art method */;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700962 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
963 return temps_start + relative_offset;
964 } else if (reg < num_regs) {
965 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
966 return locals_start + (reg * sizeof(uint32_t));
967 } else {
968 // Handle ins.
Andreas Gampe542451c2016-07-26 09:02:02 -0700969 return frame_size + ((reg - num_regs) * sizeof(uint32_t))
970 + static_cast<size_t>(pointer_size) /* art method */;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700971 }
972}
973
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700974void LockCountData::AddMonitor(Thread* self, mirror::Object* obj) {
Andreas Gampe03ec9302015-08-27 17:41:47 -0700975 if (obj == nullptr) {
976 return;
977 }
978
979 // If there's an error during enter, we won't have locked the monitor. So check there's no
980 // exception.
981 if (self->IsExceptionPending()) {
982 return;
983 }
984
985 if (monitors_ == nullptr) {
986 monitors_.reset(new std::vector<mirror::Object*>());
987 }
988 monitors_->push_back(obj);
989}
990
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700991void LockCountData::RemoveMonitorOrThrow(Thread* self, const mirror::Object* obj) {
Andreas Gampe03ec9302015-08-27 17:41:47 -0700992 if (obj == nullptr) {
993 return;
994 }
995 bool found_object = false;
996 if (monitors_ != nullptr) {
997 // We need to remove one pointer to ref, as duplicates are used for counting recursive locks.
998 // We arbitrarily choose the first one.
999 auto it = std::find(monitors_->begin(), monitors_->end(), obj);
1000 if (it != monitors_->end()) {
1001 monitors_->erase(it);
1002 found_object = true;
1003 }
1004 }
1005 if (!found_object) {
1006 // The object wasn't found. Time for an IllegalMonitorStateException.
1007 // The order here isn't fully clear. Assume that any other pending exception is swallowed.
1008 // TODO: Maybe make already pending exception a suppressed exception.
1009 self->ClearException();
1010 self->ThrowNewExceptionF("Ljava/lang/IllegalMonitorStateException;",
1011 "did not lock monitor on object of type '%s' before unlocking",
David Sehr709b0702016-10-13 09:12:37 -07001012 const_cast<mirror::Object*>(obj)->PrettyTypeOf().c_str());
Andreas Gampe03ec9302015-08-27 17:41:47 -07001013 }
1014}
1015
1016// Helper to unlock a monitor. Must be NO_THREAD_SAFETY_ANALYSIS, as we can't statically show
1017// that the object was locked.
1018void MonitorExitHelper(Thread* self, mirror::Object* obj) NO_THREAD_SAFETY_ANALYSIS {
1019 DCHECK(self != nullptr);
1020 DCHECK(obj != nullptr);
1021 obj->MonitorExit(self);
1022}
1023
Andreas Gampe56fdd0e2016-04-28 14:56:54 -07001024bool LockCountData::CheckAllMonitorsReleasedOrThrow(Thread* self) {
Andreas Gampe03ec9302015-08-27 17:41:47 -07001025 DCHECK(self != nullptr);
1026 if (monitors_ != nullptr) {
1027 if (!monitors_->empty()) {
1028 // There may be an exception pending, if the method is terminating abruptly. Clear it.
1029 // TODO: Should we add this as a suppressed exception?
1030 self->ClearException();
1031
1032 // OK, there are monitors that are still locked. To enforce structured locking (and avoid
1033 // deadlocks) we unlock all of them before we raise the IllegalMonitorState exception.
1034 for (mirror::Object* obj : *monitors_) {
1035 MonitorExitHelper(self, obj);
1036 // If this raised an exception, ignore. TODO: Should we add this as suppressed
1037 // exceptions?
1038 if (self->IsExceptionPending()) {
1039 self->ClearException();
1040 }
1041 }
1042 // Raise an exception, just give the first object as the sample.
1043 mirror::Object* first = (*monitors_)[0];
1044 self->ThrowNewExceptionF("Ljava/lang/IllegalMonitorStateException;",
1045 "did not unlock monitor on object of type '%s'",
David Sehr709b0702016-10-13 09:12:37 -07001046 mirror::Object::PrettyTypeOf(first).c_str());
Andreas Gampe03ec9302015-08-27 17:41:47 -07001047
1048 // To make sure this path is not triggered again, clean out the monitors.
1049 monitors_->clear();
1050
1051 return false;
1052 }
1053 }
1054 return true;
1055}
1056
Elliott Hughes68e76522011-10-05 13:22:16 -07001057} // namespace art