blob: d7ba1d75d8d3f108f87ff6f9de1f7c45ec2868d3 [file] [log] [blame]
Elliott Hughes68e76522011-10-05 13:22:16 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "stack.h"
18
Andreas Gampe46ee31b2016-12-14 10:11:49 -080019#include "android-base/stringprintf.h"
20
Ian Rogerse63db272014-07-15 15:36:11 -070021#include "arch/context.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070023#include "base/enums.h"
Dave Allisonf9439142014-03-27 15:10:22 -070024#include "base/hex_dump.h"
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010025#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070026#include "entrypoints/runtime_asm_entrypoints.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070027#include "gc/space/image_space.h"
28#include "gc/space/space-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010029#include "jit/jit.h"
30#include "jit/jit_code_cache.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070031#include "linear_alloc.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070032#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "mirror/object-inl.h"
34#include "mirror/object_array-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010035#include "oat_quick_method_header.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010036#include "quick/quick_method_frame_info.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070037#include "runtime.h"
Dave Allisonf9439142014-03-27 15:10:22 -070038#include "thread.h"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070039#include "thread_list.h"
Andreas Gampe90b936d2017-01-31 08:58:55 -080040#include "verify_object.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070041
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080042namespace art {
43
Andreas Gampe46ee31b2016-12-14 10:11:49 -080044using android::base::StringPrintf;
45
Mathieu Chartier8405bfd2016-02-05 12:00:49 -080046static constexpr bool kDebugStackWalk = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -070047
Ian Rogers62d6c772013-02-27 08:32:07 -080048mirror::Object* ShadowFrame::GetThisObject() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -070049 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -080050 if (m->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070051 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -080052 } else if (m->IsNative()) {
53 return GetVRegReference(0);
54 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070055 const DexFile::CodeItem* code_item = m->GetCodeItem();
David Sehr709b0702016-10-13 09:12:37 -070056 CHECK(code_item != nullptr) << ArtMethod::PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -080057 uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
58 return GetVRegReference(reg);
59 }
60}
61
Jeff Haoe701f482013-05-24 11:50:49 -070062mirror::Object* ShadowFrame::GetThisObject(uint16_t num_ins) const {
Mathieu Chartiere401d142015-04-22 13:56:20 -070063 ArtMethod* m = GetMethod();
Jeff Haoe701f482013-05-24 11:50:49 -070064 if (m->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070065 return nullptr;
Jeff Haoe701f482013-05-24 11:50:49 -070066 } else {
Jeff Hao8d448852013-06-03 17:26:19 -070067 return GetVRegReference(NumberOfVRegs() - num_ins);
Jeff Haoe701f482013-05-24 11:50:49 -070068 }
69}
70
TDYa127ce4cc0d2012-11-18 16:59:53 -080071size_t ManagedStack::NumJniShadowFrameReferences() const {
Ian Rogers0399dde2012-06-06 17:09:28 -070072 size_t count = 0;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070073 for (const ManagedStack* current_fragment = this; current_fragment != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070074 current_fragment = current_fragment->GetLink()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070075 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070076 current_frame = current_frame->GetLink()) {
TDYa127ce4cc0d2012-11-18 16:59:53 -080077 if (current_frame->GetMethod()->IsNative()) {
78 // The JNI ShadowFrame only contains references. (For indirect reference.)
79 count += current_frame->NumberOfVRegs();
80 }
Ian Rogers0399dde2012-06-06 17:09:28 -070081 }
82 }
83 return count;
84}
85
Ian Rogersef7d42f2014-01-06 12:55:46 -080086bool ManagedStack::ShadowFramesContain(StackReference<mirror::Object>* shadow_frame_entry) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070087 for (const ManagedStack* current_fragment = this; current_fragment != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070088 current_fragment = current_fragment->GetLink()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070089 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070090 current_frame = current_frame->GetLink()) {
91 if (current_frame->Contains(shadow_frame_entry)) {
92 return true;
93 }
94 }
95 }
96 return false;
97}
98
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -080099StackVisitor::StackVisitor(Thread* thread,
100 Context* context,
101 StackWalkKind walk_kind,
102 bool check_suspended)
103 : StackVisitor(thread, context, walk_kind, 0, check_suspended) {}
Ian Rogers7a22fa62013-01-23 12:16:16 -0800104
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100105StackVisitor::StackVisitor(Thread* thread,
106 Context* context,
107 StackWalkKind walk_kind,
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -0800108 size_t num_frames,
109 bool check_suspended)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100110 : thread_(thread),
111 walk_kind_(walk_kind),
112 cur_shadow_frame_(nullptr),
113 cur_quick_frame_(nullptr),
114 cur_quick_frame_pc_(0),
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100115 cur_oat_quick_method_header_(nullptr),
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100116 num_frames_(num_frames),
117 cur_depth_(0),
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100118 current_inlining_depth_(0),
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -0800119 context_(context),
120 check_suspended_(check_suspended) {
121 if (check_suspended_) {
122 DCHECK(thread == Thread::Current() || thread->IsSuspended()) << *thread;
123 }
Ian Rogers5cf98192014-05-29 21:31:50 -0700124}
125
Mathieu Chartiere401d142015-04-22 13:56:20 -0700126InlineInfo StackVisitor::GetCurrentInlineInfo() const {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100127 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
128 uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_);
129 CodeInfo code_info = method_header->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000130 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Brazdilf677ebf2015-05-29 16:29:43 +0100131 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100132 DCHECK(stack_map.IsValid());
David Brazdilf677ebf2015-05-29 16:29:43 +0100133 return code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100134}
135
Mathieu Chartiere401d142015-04-22 13:56:20 -0700136ArtMethod* StackVisitor::GetMethod() const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100137 if (cur_shadow_frame_ != nullptr) {
138 return cur_shadow_frame_->GetMethod();
139 } else if (cur_quick_frame_ != nullptr) {
140 if (IsInInlinedFrame()) {
141 size_t depth_in_stack_map = current_inlining_depth_ - 1;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100142 InlineInfo inline_info = GetCurrentInlineInfo();
David Srbecky61b28a12016-02-25 21:55:03 +0000143 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
144 CodeInfoEncoding encoding = method_header->GetOptimizedCodeInfo().ExtractEncoding();
Mathieu Chartier45bf2502016-03-31 11:07:09 -0700145 DCHECK(walk_kind_ != StackWalkKind::kSkipInlinedFrames);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100146 return GetResolvedMethod(*GetCurrentQuickFrame(),
147 inline_info,
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800148 encoding.inline_info.encoding,
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100149 depth_in_stack_map);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100150 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700151 return *cur_quick_frame_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100152 }
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100153 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700154 return nullptr;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100155}
156
Dave Allisonb373e092014-02-20 16:06:36 -0800157uint32_t StackVisitor::GetDexPc(bool abort_on_failure) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700158 if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700159 return cur_shadow_frame_->GetDexPC();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700160 } else if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100161 if (IsInInlinedFrame()) {
162 size_t depth_in_stack_map = current_inlining_depth_ - 1;
David Srbecky61b28a12016-02-25 21:55:03 +0000163 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
164 CodeInfoEncoding encoding = method_header->GetOptimizedCodeInfo().ExtractEncoding();
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800165 return GetCurrentInlineInfo().GetDexPcAtDepth(encoding.inline_info.encoding,
David Srbecky61b28a12016-02-25 21:55:03 +0000166 depth_in_stack_map);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100167 } else if (cur_oat_quick_method_header_ == nullptr) {
168 return DexFile::kDexNoIndex;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100169 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100170 return cur_oat_quick_method_header_->ToDexPc(
171 GetMethod(), cur_quick_frame_pc_, abort_on_failure);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100172 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700173 } else {
174 return 0;
175 }
176}
177
Mathieu Chartiere401d142015-04-22 13:56:20 -0700178extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700179 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertza836bc92014-11-25 16:30:53 +0100180
Ian Rogers62d6c772013-02-27 08:32:07 -0800181mirror::Object* StackVisitor::GetThisObject() const {
Andreas Gampe542451c2016-07-26 09:02:02 -0700182 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700183 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800184 if (m->IsStatic()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100185 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800186 } else if (m->IsNative()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100187 if (cur_quick_frame_ != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700188 HandleScope* hs = reinterpret_cast<HandleScope*>(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100189 reinterpret_cast<char*>(cur_quick_frame_) + sizeof(ArtMethod*));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700190 return hs->GetReference(0);
Ian Rogers62d6c772013-02-27 08:32:07 -0800191 } else {
192 return cur_shadow_frame_->GetVRegReference(0);
193 }
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000194 } else if (m->IsProxyMethod()) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100195 if (cur_quick_frame_ != nullptr) {
196 return artQuickGetProxyThisObject(cur_quick_frame_);
197 } else {
198 return cur_shadow_frame_->GetVRegReference(0);
199 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800200 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700201 const DexFile::CodeItem* code_item = m->GetCodeItem();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100202 if (code_item == nullptr) {
Ian Rogerse0dcd462014-03-08 15:21:04 -0800203 UNIMPLEMENTED(ERROR) << "Failed to determine this object of abstract or proxy method: "
David Sehr709b0702016-10-13 09:12:37 -0700204 << ArtMethod::PrettyMethod(m);
Ian Rogerse0dcd462014-03-08 15:21:04 -0800205 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800206 } else {
207 uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000208 uint32_t value = 0;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700209 bool success = GetVReg(m, reg, kReferenceVReg, &value);
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000210 // We currently always guarantee the `this` object is live throughout the method.
David Sehr709b0702016-10-13 09:12:37 -0700211 CHECK(success) << "Failed to read the this object in " << ArtMethod::PrettyMethod(m);
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000212 return reinterpret_cast<mirror::Object*>(value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800213 }
214 }
215}
216
Ian Rogers0c7abda2012-09-19 13:33:42 -0700217size_t StackVisitor::GetNativePcOffset() const {
218 DCHECK(!IsShadowFrame());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100219 return GetCurrentOatQuickMethodHeader()->NativeQuickPcOffset(cur_quick_frame_pc_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700220}
221
Mingyao Yang99170c62015-07-06 11:10:37 -0700222bool StackVisitor::GetVRegFromDebuggerShadowFrame(uint16_t vreg,
223 VRegKind kind,
224 uint32_t* val) const {
225 size_t frame_id = const_cast<StackVisitor*>(this)->GetFrameId();
226 ShadowFrame* shadow_frame = thread_->FindDebuggerShadowFrame(frame_id);
227 if (shadow_frame != nullptr) {
228 bool* updated_vreg_flags = thread_->GetUpdatedVRegFlags(frame_id);
229 DCHECK(updated_vreg_flags != nullptr);
230 if (updated_vreg_flags[vreg]) {
231 // Value is set by the debugger.
232 if (kind == kReferenceVReg) {
233 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
234 shadow_frame->GetVRegReference(vreg)));
235 } else {
236 *val = shadow_frame->GetVReg(vreg);
237 }
238 return true;
239 }
240 }
241 // No value is set by the debugger.
242 return false;
243}
244
Mathieu Chartiere401d142015-04-22 13:56:20 -0700245bool StackVisitor::GetVReg(ArtMethod* m, uint16_t vreg, VRegKind kind, uint32_t* val) const {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200246 if (cur_quick_frame_ != nullptr) {
247 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800248 DCHECK(m == GetMethod());
Mingyao Yang99170c62015-07-06 11:10:37 -0700249 // Check if there is value set by the debugger.
250 if (GetVRegFromDebuggerShadowFrame(vreg, kind, val)) {
251 return true;
252 }
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100253 DCHECK(cur_oat_quick_method_header_->IsOptimized());
254 return GetVRegFromOptimizedCode(m, vreg, kind, val);
Ian Rogers0399dde2012-06-06 17:09:28 -0700255 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100256 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertz09687442015-11-17 10:35:39 +0100257 if (kind == kReferenceVReg) {
258 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
259 cur_shadow_frame_->GetVRegReference(vreg)));
260 } else {
261 *val = cur_shadow_frame_->GetVReg(vreg);
262 }
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200263 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700264 }
265}
266
Mathieu Chartiere401d142015-04-22 13:56:20 -0700267bool StackVisitor::GetVRegFromOptimizedCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100268 uint32_t* val) const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100269 DCHECK_EQ(m, GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100270 const DexFile::CodeItem* code_item = m->GetCodeItem();
David Sehr709b0702016-10-13 09:12:37 -0700271 DCHECK(code_item != nullptr) << m->PrettyMethod(); // Can't be null or how would we compile
272 // its instructions?
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000273 uint16_t number_of_dex_registers = code_item->registers_size_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100274 DCHECK_LT(vreg, code_item->registers_size_);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100275 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
276 CodeInfo code_info = method_header->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000277 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100278
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100279 uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100280 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100281 DCHECK(stack_map.IsValid());
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100282 size_t depth_in_stack_map = current_inlining_depth_ - 1;
283
284 DexRegisterMap dex_register_map = IsInInlinedFrame()
David Brazdilf677ebf2015-05-29 16:29:43 +0100285 ? code_info.GetDexRegisterMapAtDepth(depth_in_stack_map,
286 code_info.GetInlineInfoOf(stack_map, encoding),
287 encoding,
288 number_of_dex_registers)
289 : code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100290
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000291 if (!dex_register_map.IsValid()) {
292 return false;
293 }
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000294 DexRegisterLocation::Kind location_kind =
David Brazdilf677ebf2015-05-29 16:29:43 +0100295 dex_register_map.GetLocationKind(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100296 switch (location_kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000297 case DexRegisterLocation::Kind::kInStack: {
David Brazdilf677ebf2015-05-29 16:29:43 +0100298 const int32_t offset = dex_register_map.GetStackOffsetInBytes(vreg,
299 number_of_dex_registers,
300 code_info,
301 encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100302 const uint8_t* addr = reinterpret_cast<const uint8_t*>(cur_quick_frame_) + offset;
303 *val = *reinterpret_cast<const uint32_t*>(addr);
304 return true;
305 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000306 case DexRegisterLocation::Kind::kInRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100307 case DexRegisterLocation::Kind::kInRegisterHigh:
308 case DexRegisterLocation::Kind::kInFpuRegister:
309 case DexRegisterLocation::Kind::kInFpuRegisterHigh: {
David Brazdilf677ebf2015-05-29 16:29:43 +0100310 uint32_t reg =
311 dex_register_map.GetMachineRegister(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100312 return GetRegisterIfAccessible(reg, kind, val);
313 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000314 case DexRegisterLocation::Kind::kConstant:
David Brazdilf677ebf2015-05-29 16:29:43 +0100315 *val = dex_register_map.GetConstant(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100316 return true;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000317 case DexRegisterLocation::Kind::kNone:
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100318 return false;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000319 default:
320 LOG(FATAL)
David Srbecky7dc11782016-02-25 13:23:56 +0000321 << "Unexpected location kind "
322 << dex_register_map.GetLocationInternalKind(vreg,
323 number_of_dex_registers,
324 code_info,
325 encoding);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000326 UNREACHABLE();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100327 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100328}
329
330bool StackVisitor::GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const {
331 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
David Brazdil77a48ae2015-09-15 12:34:04 +0000332
Vladimir Marko239d6ea2016-09-05 10:44:04 +0100333 if (kRuntimeISA == InstructionSet::kX86 && is_float) {
334 // X86 float registers are 64-bit and each XMM register is provided as two separate
335 // 32-bit registers by the context.
336 reg = (kind == kDoubleHiVReg) ? (2 * reg + 1) : (2 * reg);
337 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000338
Goran Jakovljevic986660c2015-12-10 11:44:50 +0100339 // MIPS32 float registers are used as 64-bit (for MIPS32r2 it is pair
340 // F(2n)-F(2n+1), and for MIPS32r6 it is 64-bit register F(2n)). When
341 // accessing upper 32-bits from double, reg + 1 should be used.
342 if ((kRuntimeISA == InstructionSet::kMips) && (kind == kDoubleHiVReg)) {
343 DCHECK_ALIGNED(reg, 2);
344 reg++;
345 }
346
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100347 if (!IsAccessibleRegister(reg, is_float)) {
348 return false;
349 }
350 uintptr_t ptr_val = GetRegister(reg, is_float);
351 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
352 if (target64) {
353 const bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
354 const bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
355 int64_t value_long = static_cast<int64_t>(ptr_val);
356 if (wide_lo) {
357 ptr_val = static_cast<uintptr_t>(Low32Bits(value_long));
358 } else if (wide_hi) {
359 ptr_val = static_cast<uintptr_t>(High32Bits(value_long));
360 }
361 }
362 *val = ptr_val;
363 return true;
364}
365
Mingyao Yang99170c62015-07-06 11:10:37 -0700366bool StackVisitor::GetVRegPairFromDebuggerShadowFrame(uint16_t vreg,
367 VRegKind kind_lo,
368 VRegKind kind_hi,
369 uint64_t* val) const {
370 uint32_t low_32bits;
371 uint32_t high_32bits;
372 bool success = GetVRegFromDebuggerShadowFrame(vreg, kind_lo, &low_32bits);
373 success &= GetVRegFromDebuggerShadowFrame(vreg + 1, kind_hi, &high_32bits);
374 if (success) {
375 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
376 }
377 return success;
378}
379
Mathieu Chartiere401d142015-04-22 13:56:20 -0700380bool StackVisitor::GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200381 VRegKind kind_hi, uint64_t* val) const {
382 if (kind_lo == kLongLoVReg) {
383 DCHECK_EQ(kind_hi, kLongHiVReg);
384 } else if (kind_lo == kDoubleLoVReg) {
385 DCHECK_EQ(kind_hi, kDoubleHiVReg);
386 } else {
387 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100388 UNREACHABLE();
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200389 }
Mingyao Yang99170c62015-07-06 11:10:37 -0700390 // Check if there is value set by the debugger.
391 if (GetVRegPairFromDebuggerShadowFrame(vreg, kind_lo, kind_hi, val)) {
392 return true;
393 }
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200394 if (cur_quick_frame_ != nullptr) {
395 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
396 DCHECK(m == GetMethod());
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100397 DCHECK(cur_oat_quick_method_header_->IsOptimized());
398 return GetVRegPairFromOptimizedCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200399 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100400 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200401 *val = cur_shadow_frame_->GetVRegLong(vreg);
402 return true;
403 }
404}
405
Mathieu Chartiere401d142015-04-22 13:56:20 -0700406bool StackVisitor::GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100407 VRegKind kind_lo, VRegKind kind_hi,
408 uint64_t* val) const {
409 uint32_t low_32bits;
410 uint32_t high_32bits;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700411 bool success = GetVRegFromOptimizedCode(m, vreg, kind_lo, &low_32bits);
412 success &= GetVRegFromOptimizedCode(m, vreg + 1, kind_hi, &high_32bits);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100413 if (success) {
414 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
415 }
416 return success;
417}
418
419bool StackVisitor::GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
420 VRegKind kind_lo, uint64_t* val) const {
421 const bool is_float = (kind_lo == kDoubleLoVReg);
422 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
423 return false;
424 }
425 uintptr_t ptr_val_lo = GetRegister(reg_lo, is_float);
426 uintptr_t ptr_val_hi = GetRegister(reg_hi, is_float);
427 bool target64 = Is64BitInstructionSet(kRuntimeISA);
428 if (target64) {
429 int64_t value_long_lo = static_cast<int64_t>(ptr_val_lo);
430 int64_t value_long_hi = static_cast<int64_t>(ptr_val_hi);
431 ptr_val_lo = static_cast<uintptr_t>(Low32Bits(value_long_lo));
432 ptr_val_hi = static_cast<uintptr_t>(High32Bits(value_long_hi));
433 }
434 *val = (static_cast<uint64_t>(ptr_val_hi) << 32) | static_cast<uint32_t>(ptr_val_lo);
435 return true;
436}
437
Mingyao Yang636b9252015-07-31 16:40:24 -0700438bool StackVisitor::SetVReg(ArtMethod* m,
439 uint16_t vreg,
440 uint32_t new_value,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800441 VRegKind kind) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700442 const DexFile::CodeItem* code_item = m->GetCodeItem();
443 if (code_item == nullptr) {
444 return false;
445 }
446 ShadowFrame* shadow_frame = GetCurrentShadowFrame();
447 if (shadow_frame == nullptr) {
448 // This is a compiled frame: we must prepare and update a shadow frame that will
449 // be executed by the interpreter after deoptimization of the stack.
450 const size_t frame_id = GetFrameId();
451 const uint16_t num_regs = code_item->registers_size_;
452 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
453 CHECK(shadow_frame != nullptr);
454 // Remember the vreg has been set for debugging and must not be overwritten by the
455 // original value during deoptimization of the stack.
456 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
457 }
458 if (kind == kReferenceVReg) {
459 shadow_frame->SetVRegReference(vreg, reinterpret_cast<mirror::Object*>(new_value));
460 } else {
461 shadow_frame->SetVReg(vreg, new_value);
462 }
463 return true;
464}
465
Mingyao Yang636b9252015-07-31 16:40:24 -0700466bool StackVisitor::SetVRegPair(ArtMethod* m,
467 uint16_t vreg,
468 uint64_t new_value,
469 VRegKind kind_lo,
470 VRegKind kind_hi) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700471 if (kind_lo == kLongLoVReg) {
472 DCHECK_EQ(kind_hi, kLongHiVReg);
473 } else if (kind_lo == kDoubleLoVReg) {
474 DCHECK_EQ(kind_hi, kDoubleHiVReg);
475 } else {
476 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
477 UNREACHABLE();
478 }
479 const DexFile::CodeItem* code_item = m->GetCodeItem();
480 if (code_item == nullptr) {
481 return false;
482 }
483 ShadowFrame* shadow_frame = GetCurrentShadowFrame();
484 if (shadow_frame == nullptr) {
485 // This is a compiled frame: we must prepare for deoptimization (see SetVRegFromDebugger).
486 const size_t frame_id = GetFrameId();
487 const uint16_t num_regs = code_item->registers_size_;
488 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
489 CHECK(shadow_frame != nullptr);
490 // Remember the vreg pair has been set for debugging and must not be overwritten by the
491 // original value during deoptimization of the stack.
492 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
493 thread_->GetUpdatedVRegFlags(frame_id)[vreg + 1] = true;
494 }
495 shadow_frame->SetVRegLong(vreg, new_value);
496 return true;
497}
498
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100499bool StackVisitor::IsAccessibleGPR(uint32_t reg) const {
500 DCHECK(context_ != nullptr);
501 return context_->IsAccessibleGPR(reg);
502}
503
Mathieu Chartier815873e2014-02-13 18:02:13 -0800504uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100505 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
506 DCHECK(context_ != nullptr);
Mathieu Chartier815873e2014-02-13 18:02:13 -0800507 return context_->GetGPRAddress(reg);
508}
509
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100510uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
511 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
512 DCHECK(context_ != nullptr);
513 return context_->GetGPR(reg);
Ian Rogers0399dde2012-06-06 17:09:28 -0700514}
515
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100516bool StackVisitor::IsAccessibleFPR(uint32_t reg) const {
517 DCHECK(context_ != nullptr);
518 return context_->IsAccessibleFPR(reg);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200519}
520
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100521uintptr_t StackVisitor::GetFPR(uint32_t reg) const {
522 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
523 DCHECK(context_ != nullptr);
524 return context_->GetFPR(reg);
525}
526
Ian Rogers0399dde2012-06-06 17:09:28 -0700527uintptr_t StackVisitor::GetReturnPc() const {
Ian Rogers13735952014-10-08 12:43:28 -0700528 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700529 DCHECK(sp != nullptr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100530 uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogers0399dde2012-06-06 17:09:28 -0700531 return *reinterpret_cast<uintptr_t*>(pc_addr);
532}
533
534void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
Ian Rogers13735952014-10-08 12:43:28 -0700535 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700536 CHECK(sp != nullptr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100537 uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogers0399dde2012-06-06 17:09:28 -0700538 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc;
539}
540
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100541size_t StackVisitor::ComputeNumFrames(Thread* thread, StackWalkKind walk_kind) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700542 struct NumFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100543 NumFramesVisitor(Thread* thread_in, StackWalkKind walk_kind_in)
544 : StackVisitor(thread_in, nullptr, walk_kind_in), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700545
Ian Rogers5cf98192014-05-29 21:31:50 -0700546 bool VisitFrame() OVERRIDE {
Ian Rogers0399dde2012-06-06 17:09:28 -0700547 frames++;
548 return true;
549 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700550
Ian Rogers0399dde2012-06-06 17:09:28 -0700551 size_t frames;
552 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100553 NumFramesVisitor visitor(thread, walk_kind);
Ian Rogers0399dde2012-06-06 17:09:28 -0700554 visitor.WalkStack(true);
555 return visitor.frames;
556}
557
Mathieu Chartiere401d142015-04-22 13:56:20 -0700558bool StackVisitor::GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700559 struct HasMoreFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100560 HasMoreFramesVisitor(Thread* thread,
561 StackWalkKind walk_kind,
562 size_t num_frames,
563 size_t frame_height)
564 : StackVisitor(thread, nullptr, walk_kind, num_frames),
565 frame_height_(frame_height),
566 found_frame_(false),
567 has_more_frames_(false),
568 next_method_(nullptr),
569 next_dex_pc_(0) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700570 }
571
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700572 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700573 if (found_frame_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700574 ArtMethod* method = GetMethod();
Ian Rogers5cf98192014-05-29 21:31:50 -0700575 if (method != nullptr && !method->IsRuntimeMethod()) {
576 has_more_frames_ = true;
577 next_method_ = method;
578 next_dex_pc_ = GetDexPc();
579 return false; // End stack walk once next method is found.
580 }
581 } else if (GetFrameHeight() == frame_height_) {
582 found_frame_ = true;
583 }
584 return true;
585 }
586
587 size_t frame_height_;
588 bool found_frame_;
589 bool has_more_frames_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700590 ArtMethod* next_method_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700591 uint32_t next_dex_pc_;
592 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100593 HasMoreFramesVisitor visitor(thread_, walk_kind_, GetNumFrames(), GetFrameHeight());
Ian Rogers5cf98192014-05-29 21:31:50 -0700594 visitor.WalkStack(true);
595 *next_method = visitor.next_method_;
596 *next_dex_pc = visitor.next_dex_pc_;
597 return visitor.has_more_frames_;
598}
599
Ian Rogers7a22fa62013-01-23 12:16:16 -0800600void StackVisitor::DescribeStack(Thread* thread) {
Ian Rogers306057f2012-11-26 12:45:53 -0800601 struct DescribeStackVisitor : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800602 explicit DescribeStackVisitor(Thread* thread_in)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100603 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800604
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700605 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers306057f2012-11-26 12:45:53 -0800606 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation();
607 return true;
608 }
609 };
Ian Rogers7a22fa62013-01-23 12:16:16 -0800610 DescribeStackVisitor visitor(thread);
Ian Rogers306057f2012-11-26 12:45:53 -0800611 visitor.WalkStack(true);
612}
613
Ian Rogers40e3bac2012-11-20 00:09:14 -0800614std::string StackVisitor::DescribeLocation() const {
615 std::string result("Visiting method '");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700616 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700617 if (m == nullptr) {
Ian Rogers306057f2012-11-26 12:45:53 -0800618 return "upcall";
619 }
David Sehr709b0702016-10-13 09:12:37 -0700620 result += m->PrettyMethod();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800621 result += StringPrintf("' at dex PC 0x%04x", GetDexPc());
Ian Rogers40e3bac2012-11-20 00:09:14 -0800622 if (!IsShadowFrame()) {
623 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc()));
624 }
625 return result;
626}
627
Alex Lightdba61482016-12-21 08:20:29 -0800628void StackVisitor::SetMethod(ArtMethod* method) {
629 DCHECK(GetMethod() != nullptr);
630 if (cur_shadow_frame_ != nullptr) {
631 cur_shadow_frame_->SetMethod(method);
632 } else {
633 DCHECK(cur_quick_frame_ != nullptr);
634 CHECK(!IsInInlinedFrame()) << "We do not support setting inlined method's ArtMethod!";
Alex Light1ebe4fe2017-01-30 14:57:11 -0800635 *cur_quick_frame_ = method;
Alex Lightdba61482016-12-21 08:20:29 -0800636 }
637}
638
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100639static void AssertPcIsWithinQuickCode(ArtMethod* method, uintptr_t pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700640 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100641 if (method->IsNative() || method->IsRuntimeMethod() || method->IsProxyMethod()) {
642 return;
643 }
644
645 if (pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) {
646 return;
647 }
648
649 const void* code = method->GetEntryPointFromQuickCompiledCode();
650 if (code == GetQuickInstrumentationEntryPoint()) {
651 return;
652 }
653
654 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
655 if (class_linker->IsQuickToInterpreterBridge(code) ||
656 class_linker->IsQuickResolutionStub(code)) {
657 return;
658 }
659
660 // If we are the JIT then we may have just compiled the method after the
661 // IsQuickToInterpreterBridge check.
Calin Juravleffc87072016-04-20 14:22:09 +0100662 Runtime* runtime = Runtime::Current();
663 if (runtime->UseJitCompilation() && runtime->GetJit()->GetCodeCache()->ContainsPc(code)) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100664 return;
665 }
666
Mingyao Yang063fc772016-08-02 11:02:54 -0700667 uint32_t code_size = OatQuickMethodHeader::FromEntryPoint(code)->GetCodeSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100668 uintptr_t code_start = reinterpret_cast<uintptr_t>(code);
669 CHECK(code_start <= pc && pc <= (code_start + code_size))
David Sehr709b0702016-10-13 09:12:37 -0700670 << method->PrettyMethod()
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100671 << " pc=" << std::hex << pc
Roland Levillain0d5a2812015-11-13 10:07:31 +0000672 << " code_start=" << code_start
673 << " code_size=" << code_size;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100674}
675
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700676void StackVisitor::SanityCheckFrame() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800677 if (kIsDebugBuild) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700678 ArtMethod* method = GetMethod();
679 auto* declaring_class = method->GetDeclaringClass();
680 // Runtime methods have null declaring class.
681 if (!method->IsRuntimeMethod()) {
682 CHECK(declaring_class != nullptr);
683 CHECK_EQ(declaring_class->GetClass(), declaring_class->GetClass()->GetClass())
684 << declaring_class;
685 } else {
686 CHECK(declaring_class == nullptr);
687 }
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700688 Runtime* const runtime = Runtime::Current();
689 LinearAlloc* const linear_alloc = runtime->GetLinearAlloc();
690 if (!linear_alloc->Contains(method)) {
691 // Check class linker linear allocs.
692 mirror::Class* klass = method->GetDeclaringClass();
693 LinearAlloc* const class_linear_alloc = (klass != nullptr)
Mathieu Chartier5b830502016-03-02 10:30:23 -0800694 ? runtime->GetClassLinker()->GetAllocatorForClassLoader(klass->GetClassLoader())
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700695 : linear_alloc;
696 if (!class_linear_alloc->Contains(method)) {
697 // Check image space.
698 bool in_image = false;
699 for (auto& space : runtime->GetHeap()->GetContinuousSpaces()) {
700 if (space->IsImageSpace()) {
701 auto* image_space = space->AsImageSpace();
702 const auto& header = image_space->GetImageHeader();
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700703 const ImageSection& methods = header.GetMethodsSection();
704 const ImageSection& runtime_methods = header.GetRuntimeMethodsSection();
705 const size_t offset = reinterpret_cast<const uint8_t*>(method) - image_space->Begin();
706 if (methods.Contains(offset) || runtime_methods.Contains(offset)) {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700707 in_image = true;
708 break;
709 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700710 }
711 }
David Sehr709b0702016-10-13 09:12:37 -0700712 CHECK(in_image) << method->PrettyMethod() << " not in linear alloc or image";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700713 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700714 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800715 if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100716 AssertPcIsWithinQuickCode(method, cur_quick_frame_pc_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800717 // Frame sanity.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100718 size_t frame_size = GetCurrentQuickFrameInfo().FrameSizeInBytes();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800719 CHECK_NE(frame_size, 0u);
Andreas Gampe5b417b92014-03-10 14:18:35 -0700720 // A rough guess at an upper size we expect to see for a frame.
721 // 256 registers
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700722 // 2 words HandleScope overhead
Andreas Gampe5b417b92014-03-10 14:18:35 -0700723 // 3+3 register spills
724 // TODO: this seems architecture specific for the case of JNI frames.
Brian Carlstromed08bd42014-03-19 18:34:17 -0700725 // TODO: 083-compiler-regressions ManyFloatArgs shows this estimate is wrong.
726 // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word);
727 const size_t kMaxExpectedFrameSize = 2 * KB;
David Sehr709b0702016-10-13 09:12:37 -0700728 CHECK_LE(frame_size, kMaxExpectedFrameSize) << method->PrettyMethod();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100729 size_t return_pc_offset = GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800730 CHECK_LT(return_pc_offset, frame_size);
731 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700732 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700733}
734
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100735// Counts the number of references in the parameter list of the corresponding method.
736// Note: Thus does _not_ include "this" for non-static methods.
737static uint32_t GetNumberOfReferenceArgsWithoutReceiver(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700738 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100739 uint32_t shorty_len;
740 const char* shorty = method->GetShorty(&shorty_len);
741 uint32_t refs = 0;
742 for (uint32_t i = 1; i < shorty_len ; ++i) {
743 if (shorty[i] == 'L') {
744 refs++;
745 }
746 }
747 return refs;
748}
749
750QuickMethodFrameInfo StackVisitor::GetCurrentQuickFrameInfo() const {
751 if (cur_oat_quick_method_header_ != nullptr) {
752 return cur_oat_quick_method_header_->GetFrameInfo();
753 }
754
755 ArtMethod* method = GetMethod();
756 Runtime* runtime = Runtime::Current();
757
758 if (method->IsAbstract()) {
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100759 return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100760 }
761
762 // This goes before IsProxyMethod since runtime methods have a null declaring class.
763 if (method->IsRuntimeMethod()) {
764 return runtime->GetRuntimeMethodFrameInfo(method);
765 }
766
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100767 if (method->IsProxyMethod()) {
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000768 // There is only one direct method of a proxy class: the constructor. A direct method is
769 // cloned from the original java.lang.reflect.Proxy and is executed as usual quick
770 // compiled method without any stubs. Therefore the method must have a OatQuickMethodHeader.
771 DCHECK(!method->IsDirect() && !method->IsConstructor())
772 << "Constructors of proxy classes must have a OatQuickMethodHeader";
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100773 return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100774 }
775
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000776 // The only remaining case is if the method is native and uses the generic JNI stub.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100777 DCHECK(method->IsNative());
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000778 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampe542451c2016-07-26 09:02:02 -0700779 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(method,
780 kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700781 DCHECK(class_linker->IsQuickGenericJniStub(entry_point)) << method->PrettyMethod();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100782 // Generic JNI frame.
783 uint32_t handle_refs = GetNumberOfReferenceArgsWithoutReceiver(method) + 1;
784 size_t scope_size = HandleScope::SizeOf(handle_refs);
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100785 QuickMethodFrameInfo callee_info =
786 runtime->GetCalleeSaveMethodFrameInfo(Runtime::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100787
788 // Callee saves + handle scope + method ref + alignment
789 // Note: -sizeof(void*) since callee-save frame stores a whole method pointer.
790 size_t frame_size = RoundUp(
791 callee_info.FrameSizeInBytes() - sizeof(void*) + sizeof(ArtMethod*) + scope_size,
792 kStackAlignment);
793 return QuickMethodFrameInfo(frame_size, callee_info.CoreSpillMask(), callee_info.FpSpillMask());
794}
795
Andreas Gampe585da952016-12-02 14:52:29 -0800796template <StackVisitor::CountTransitions kCount>
Ian Rogers0399dde2012-06-06 17:09:28 -0700797void StackVisitor::WalkStack(bool include_transitions) {
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -0800798 if (check_suspended_) {
799 DCHECK(thread_ == Thread::Current() || thread_->IsSuspended());
800 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800801 CHECK_EQ(cur_depth_, 0U);
802 bool exit_stubs_installed = Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled();
Alex Lightb81a9842016-12-15 00:59:05 +0000803 uint32_t instrumentation_stack_depth = 0;
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000804 size_t inlined_frames_count = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700805
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700806 for (const ManagedStack* current_fragment = thread_->GetManagedStack();
807 current_fragment != nullptr; current_fragment = current_fragment->GetLink()) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700808 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
809 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700810 cur_quick_frame_pc_ = 0;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100811 cur_oat_quick_method_header_ = nullptr;
Dave Allisonf9439142014-03-27 15:10:22 -0700812
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700813 if (cur_quick_frame_ != nullptr) { // Handle quick stack frames.
Ian Rogers0399dde2012-06-06 17:09:28 -0700814 // Can't be both a shadow and a quick fragment.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700815 DCHECK(current_fragment->GetTopShadowFrame() == nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700816 ArtMethod* method = *cur_quick_frame_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700817 while (method != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100818 cur_oat_quick_method_header_ = method->GetOatQuickMethodHeader(cur_quick_frame_pc_);
Dave Allison5cd33752014-04-15 15:57:58 -0700819 SanityCheckFrame();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100820
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100821 if ((walk_kind_ == StackWalkKind::kIncludeInlinedFrames)
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100822 && (cur_oat_quick_method_header_ != nullptr)
823 && cur_oat_quick_method_header_->IsOptimized()) {
824 CodeInfo code_info = cur_oat_quick_method_header_->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000825 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100826 uint32_t native_pc_offset =
827 cur_oat_quick_method_header_->NativeQuickPcOffset(cur_quick_frame_pc_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100828 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800829 if (stack_map.IsValid() && stack_map.HasInlineInfo(encoding.stack_map.encoding)) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100830 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100831 DCHECK_EQ(current_inlining_depth_, 0u);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800832 for (current_inlining_depth_ = inline_info.GetDepth(encoding.inline_info.encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100833 current_inlining_depth_ != 0;
834 --current_inlining_depth_) {
835 bool should_continue = VisitFrame();
836 if (UNLIKELY(!should_continue)) {
837 return;
838 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100839 cur_depth_++;
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000840 inlined_frames_count++;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100841 }
842 }
843 }
844
Dave Allison5cd33752014-04-15 15:57:58 -0700845 bool should_continue = VisitFrame();
846 if (UNLIKELY(!should_continue)) {
847 return;
Ian Rogers0399dde2012-06-06 17:09:28 -0700848 }
Dave Allison5cd33752014-04-15 15:57:58 -0700849
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100850 QuickMethodFrameInfo frame_info = GetCurrentQuickFrameInfo();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700851 if (context_ != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100852 context_->FillCalleeSaves(reinterpret_cast<uint8_t*>(cur_quick_frame_), frame_info);
Ian Rogers0399dde2012-06-06 17:09:28 -0700853 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700854 // Compute PC for next stack frame from return PC.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100855 size_t frame_size = frame_info.FrameSizeInBytes();
856 size_t return_pc_offset = frame_size - sizeof(void*);
Ian Rogers13735952014-10-08 12:43:28 -0700857 uint8_t* return_pc_addr = reinterpret_cast<uint8_t*>(cur_quick_frame_) + return_pc_offset;
Ian Rogers0399dde2012-06-06 17:09:28 -0700858 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100859
Ian Rogers62d6c772013-02-27 08:32:07 -0800860 if (UNLIKELY(exit_stubs_installed)) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700861 // While profiling, the return pc is restored from the side stack, except when walking
862 // the stack for an exception where the side stack will be unwound in VisitFrame.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700863 if (reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc) {
Andreas Gampe585da952016-12-02 14:52:29 -0800864 CHECK_LT(instrumentation_stack_depth, thread_->GetInstrumentationStack()->size());
Sebastien Hertz74e256b2013-10-04 10:40:37 +0200865 const instrumentation::InstrumentationStackFrame& instrumentation_frame =
Alex Lightb81a9842016-12-15 00:59:05 +0000866 thread_->GetInstrumentationStack()->at(instrumentation_stack_depth);
jeffhao725a9572012-11-13 18:20:12 -0800867 instrumentation_stack_depth++;
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100868 if (GetMethod() ==
869 Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveAllCalleeSaves)) {
Jeff Haofb2802d2013-07-24 13:53:05 -0700870 // Skip runtime save all callee frames which are used to deliver exceptions.
871 } else if (instrumentation_frame.interpreter_entry_) {
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100872 ArtMethod* callee =
873 Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveRefsAndArgs);
David Sehr709b0702016-10-13 09:12:37 -0700874 CHECK_EQ(GetMethod(), callee) << "Expected: " << ArtMethod::PrettyMethod(callee)
875 << " Found: " << ArtMethod::PrettyMethod(GetMethod());
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000876 } else {
877 CHECK_EQ(instrumentation_frame.method_, GetMethod())
David Sehr709b0702016-10-13 09:12:37 -0700878 << "Expected: " << ArtMethod::PrettyMethod(instrumentation_frame.method_)
879 << " Found: " << ArtMethod::PrettyMethod(GetMethod());
Ian Rogers62d6c772013-02-27 08:32:07 -0800880 }
881 if (num_frames_ != 0) {
882 // Check agreement of frame Ids only if num_frames_ is computed to avoid infinite
883 // recursion.
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000884 size_t frame_id = instrumentation::Instrumentation::ComputeFrameId(
885 thread_,
886 cur_depth_,
887 inlined_frames_count);
888 CHECK_EQ(instrumentation_frame.frame_id_, frame_id);
Ian Rogers62d6c772013-02-27 08:32:07 -0800889 }
jeffhao725a9572012-11-13 18:20:12 -0800890 return_pc = instrumentation_frame.return_pc_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700891 }
892 }
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100893
Ian Rogers0399dde2012-06-06 17:09:28 -0700894 cur_quick_frame_pc_ = return_pc;
Ian Rogers13735952014-10-08 12:43:28 -0700895 uint8_t* next_frame = reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700896 cur_quick_frame_ = reinterpret_cast<ArtMethod**>(next_frame);
897
898 if (kDebugStackWalk) {
David Sehr709b0702016-10-13 09:12:37 -0700899 LOG(INFO) << ArtMethod::PrettyMethod(method) << "@" << method << " size=" << frame_size
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100900 << std::boolalpha
901 << " optimized=" << (cur_oat_quick_method_header_ != nullptr &&
902 cur_oat_quick_method_header_->IsOptimized())
Mathieu Chartiere401d142015-04-22 13:56:20 -0700903 << " native=" << method->IsNative()
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100904 << std::noboolalpha
Mathieu Chartiere401d142015-04-22 13:56:20 -0700905 << " entrypoints=" << method->GetEntryPointFromQuickCompiledCode()
906 << "," << method->GetEntryPointFromJni()
Mathieu Chartiere401d142015-04-22 13:56:20 -0700907 << " next=" << *cur_quick_frame_;
908 }
909
Ian Rogers0399dde2012-06-06 17:09:28 -0700910 cur_depth_++;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700911 method = *cur_quick_frame_;
jeffhao6641ea12013-01-02 18:13:42 -0800912 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700913 } else if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700914 do {
915 SanityCheckFrame();
916 bool should_continue = VisitFrame();
917 if (UNLIKELY(!should_continue)) {
918 return;
919 }
920 cur_depth_++;
921 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700922 } while (cur_shadow_frame_ != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700923 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700924 if (include_transitions) {
925 bool should_continue = VisitFrame();
926 if (!should_continue) {
927 return;
928 }
929 }
Andreas Gampe585da952016-12-02 14:52:29 -0800930 if (kCount == CountTransitions::kYes) {
931 cur_depth_++;
932 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800933 }
934 if (num_frames_ != 0) {
935 CHECK_EQ(cur_depth_, num_frames_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700936 }
937}
938
Andreas Gampe585da952016-12-02 14:52:29 -0800939template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kYes>(bool);
940template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kNo>(bool);
941
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800942void JavaFrameRootInfo::Describe(std::ostream& os) const {
943 const StackVisitor* visitor = stack_visitor_;
944 CHECK(visitor != nullptr);
945 os << "Type=" << GetType() << " thread_id=" << GetThreadId() << " location=" <<
946 visitor->DescribeLocation() << " vreg=" << vreg_;
947}
948
Mathieu Chartiere401d142015-04-22 13:56:20 -0700949int StackVisitor::GetVRegOffsetFromQuickCode(const DexFile::CodeItem* code_item,
950 uint32_t core_spills, uint32_t fp_spills,
951 size_t frame_size, int reg, InstructionSet isa) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700952 PointerSize pointer_size = InstructionSetPointerSize(isa);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700953 if (kIsDebugBuild) {
954 auto* runtime = Runtime::Current();
955 if (runtime != nullptr) {
956 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
957 }
958 }
Roland Levillain14d90572015-07-16 10:52:26 +0100959 DCHECK_ALIGNED(frame_size, kStackAlignment);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700960 DCHECK_NE(reg, -1);
961 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
962 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
963 + sizeof(uint32_t); // Filler.
964 int num_regs = code_item->registers_size_ - code_item->ins_size_;
965 int temp_threshold = code_item->registers_size_;
966 const int max_num_special_temps = 1;
967 if (reg == temp_threshold) {
968 // The current method pointer corresponds to special location on stack.
969 return 0;
970 } else if (reg >= temp_threshold + max_num_special_temps) {
971 /*
972 * Special temporaries may have custom locations and the logic above deals with that.
973 * However, non-special temporaries are placed relative to the outs.
974 */
Andreas Gampe542451c2016-07-26 09:02:02 -0700975 int temps_start = code_item->outs_size_ * sizeof(uint32_t)
976 + static_cast<size_t>(pointer_size) /* art method */;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700977 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
978 return temps_start + relative_offset;
979 } else if (reg < num_regs) {
980 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
981 return locals_start + (reg * sizeof(uint32_t));
982 } else {
983 // Handle ins.
Andreas Gampe542451c2016-07-26 09:02:02 -0700984 return frame_size + ((reg - num_regs) * sizeof(uint32_t))
985 + static_cast<size_t>(pointer_size) /* art method */;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700986 }
987}
988
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700989void LockCountData::AddMonitor(Thread* self, mirror::Object* obj) {
Andreas Gampe03ec9302015-08-27 17:41:47 -0700990 if (obj == nullptr) {
991 return;
992 }
993
994 // If there's an error during enter, we won't have locked the monitor. So check there's no
995 // exception.
996 if (self->IsExceptionPending()) {
997 return;
998 }
999
1000 if (monitors_ == nullptr) {
1001 monitors_.reset(new std::vector<mirror::Object*>());
1002 }
1003 monitors_->push_back(obj);
1004}
1005
Andreas Gampe56fdd0e2016-04-28 14:56:54 -07001006void LockCountData::RemoveMonitorOrThrow(Thread* self, const mirror::Object* obj) {
Andreas Gampe03ec9302015-08-27 17:41:47 -07001007 if (obj == nullptr) {
1008 return;
1009 }
1010 bool found_object = false;
1011 if (monitors_ != nullptr) {
1012 // We need to remove one pointer to ref, as duplicates are used for counting recursive locks.
1013 // We arbitrarily choose the first one.
1014 auto it = std::find(monitors_->begin(), monitors_->end(), obj);
1015 if (it != monitors_->end()) {
1016 monitors_->erase(it);
1017 found_object = true;
1018 }
1019 }
1020 if (!found_object) {
1021 // The object wasn't found. Time for an IllegalMonitorStateException.
1022 // The order here isn't fully clear. Assume that any other pending exception is swallowed.
1023 // TODO: Maybe make already pending exception a suppressed exception.
1024 self->ClearException();
1025 self->ThrowNewExceptionF("Ljava/lang/IllegalMonitorStateException;",
1026 "did not lock monitor on object of type '%s' before unlocking",
David Sehr709b0702016-10-13 09:12:37 -07001027 const_cast<mirror::Object*>(obj)->PrettyTypeOf().c_str());
Andreas Gampe03ec9302015-08-27 17:41:47 -07001028 }
1029}
1030
1031// Helper to unlock a monitor. Must be NO_THREAD_SAFETY_ANALYSIS, as we can't statically show
1032// that the object was locked.
1033void MonitorExitHelper(Thread* self, mirror::Object* obj) NO_THREAD_SAFETY_ANALYSIS {
1034 DCHECK(self != nullptr);
1035 DCHECK(obj != nullptr);
1036 obj->MonitorExit(self);
1037}
1038
Andreas Gampe56fdd0e2016-04-28 14:56:54 -07001039bool LockCountData::CheckAllMonitorsReleasedOrThrow(Thread* self) {
Andreas Gampe03ec9302015-08-27 17:41:47 -07001040 DCHECK(self != nullptr);
1041 if (monitors_ != nullptr) {
1042 if (!monitors_->empty()) {
1043 // There may be an exception pending, if the method is terminating abruptly. Clear it.
1044 // TODO: Should we add this as a suppressed exception?
1045 self->ClearException();
1046
1047 // OK, there are monitors that are still locked. To enforce structured locking (and avoid
1048 // deadlocks) we unlock all of them before we raise the IllegalMonitorState exception.
1049 for (mirror::Object* obj : *monitors_) {
1050 MonitorExitHelper(self, obj);
1051 // If this raised an exception, ignore. TODO: Should we add this as suppressed
1052 // exceptions?
1053 if (self->IsExceptionPending()) {
1054 self->ClearException();
1055 }
1056 }
1057 // Raise an exception, just give the first object as the sample.
1058 mirror::Object* first = (*monitors_)[0];
1059 self->ThrowNewExceptionF("Ljava/lang/IllegalMonitorStateException;",
1060 "did not unlock monitor on object of type '%s'",
David Sehr709b0702016-10-13 09:12:37 -07001061 mirror::Object::PrettyTypeOf(first).c_str());
Andreas Gampe03ec9302015-08-27 17:41:47 -07001062
1063 // To make sure this path is not triggered again, clean out the monitors.
1064 monitors_->clear();
1065
1066 return false;
1067 }
1068 }
1069 return true;
1070}
1071
Elliott Hughes68e76522011-10-05 13:22:16 -07001072} // namespace art