blob: 0628643a0971ce73b33853084aa605809f2ef900 [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 Chartiercbcedbf2017-03-12 22:24:50 -0700145 MethodInfo method_info = method_header->GetOptimizedMethodInfo();
Mathieu Chartier45bf2502016-03-31 11:07:09 -0700146 DCHECK(walk_kind_ != StackWalkKind::kSkipInlinedFrames);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100147 return GetResolvedMethod(*GetCurrentQuickFrame(),
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700148 method_info,
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100149 inline_info,
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800150 encoding.inline_info.encoding,
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100151 depth_in_stack_map);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100152 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700153 return *cur_quick_frame_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100154 }
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100155 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700156 return nullptr;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100157}
158
Dave Allisonb373e092014-02-20 16:06:36 -0800159uint32_t StackVisitor::GetDexPc(bool abort_on_failure) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700160 if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700161 return cur_shadow_frame_->GetDexPC();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700162 } else if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100163 if (IsInInlinedFrame()) {
164 size_t depth_in_stack_map = current_inlining_depth_ - 1;
David Srbecky61b28a12016-02-25 21:55:03 +0000165 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
166 CodeInfoEncoding encoding = method_header->GetOptimizedCodeInfo().ExtractEncoding();
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800167 return GetCurrentInlineInfo().GetDexPcAtDepth(encoding.inline_info.encoding,
David Srbecky61b28a12016-02-25 21:55:03 +0000168 depth_in_stack_map);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100169 } else if (cur_oat_quick_method_header_ == nullptr) {
170 return DexFile::kDexNoIndex;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100171 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100172 return cur_oat_quick_method_header_->ToDexPc(
173 GetMethod(), cur_quick_frame_pc_, abort_on_failure);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100174 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700175 } else {
176 return 0;
177 }
178}
179
Mathieu Chartiere401d142015-04-22 13:56:20 -0700180extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700181 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertza836bc92014-11-25 16:30:53 +0100182
Ian Rogers62d6c772013-02-27 08:32:07 -0800183mirror::Object* StackVisitor::GetThisObject() const {
Andreas Gampe542451c2016-07-26 09:02:02 -0700184 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700185 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800186 if (m->IsStatic()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100187 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800188 } else if (m->IsNative()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100189 if (cur_quick_frame_ != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700190 HandleScope* hs = reinterpret_cast<HandleScope*>(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100191 reinterpret_cast<char*>(cur_quick_frame_) + sizeof(ArtMethod*));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700192 return hs->GetReference(0);
Ian Rogers62d6c772013-02-27 08:32:07 -0800193 } else {
194 return cur_shadow_frame_->GetVRegReference(0);
195 }
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000196 } else if (m->IsProxyMethod()) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100197 if (cur_quick_frame_ != nullptr) {
198 return artQuickGetProxyThisObject(cur_quick_frame_);
199 } else {
200 return cur_shadow_frame_->GetVRegReference(0);
201 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800202 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700203 const DexFile::CodeItem* code_item = m->GetCodeItem();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100204 if (code_item == nullptr) {
Ian Rogerse0dcd462014-03-08 15:21:04 -0800205 UNIMPLEMENTED(ERROR) << "Failed to determine this object of abstract or proxy method: "
David Sehr709b0702016-10-13 09:12:37 -0700206 << ArtMethod::PrettyMethod(m);
Ian Rogerse0dcd462014-03-08 15:21:04 -0800207 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800208 } else {
209 uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000210 uint32_t value = 0;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700211 bool success = GetVReg(m, reg, kReferenceVReg, &value);
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000212 // We currently always guarantee the `this` object is live throughout the method.
David Sehr709b0702016-10-13 09:12:37 -0700213 CHECK(success) << "Failed to read the this object in " << ArtMethod::PrettyMethod(m);
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000214 return reinterpret_cast<mirror::Object*>(value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800215 }
216 }
217}
218
Ian Rogers0c7abda2012-09-19 13:33:42 -0700219size_t StackVisitor::GetNativePcOffset() const {
220 DCHECK(!IsShadowFrame());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100221 return GetCurrentOatQuickMethodHeader()->NativeQuickPcOffset(cur_quick_frame_pc_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700222}
223
Mingyao Yang99170c62015-07-06 11:10:37 -0700224bool StackVisitor::GetVRegFromDebuggerShadowFrame(uint16_t vreg,
225 VRegKind kind,
226 uint32_t* val) const {
227 size_t frame_id = const_cast<StackVisitor*>(this)->GetFrameId();
228 ShadowFrame* shadow_frame = thread_->FindDebuggerShadowFrame(frame_id);
229 if (shadow_frame != nullptr) {
230 bool* updated_vreg_flags = thread_->GetUpdatedVRegFlags(frame_id);
231 DCHECK(updated_vreg_flags != nullptr);
232 if (updated_vreg_flags[vreg]) {
233 // Value is set by the debugger.
234 if (kind == kReferenceVReg) {
235 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
236 shadow_frame->GetVRegReference(vreg)));
237 } else {
238 *val = shadow_frame->GetVReg(vreg);
239 }
240 return true;
241 }
242 }
243 // No value is set by the debugger.
244 return false;
245}
246
Mathieu Chartiere401d142015-04-22 13:56:20 -0700247bool StackVisitor::GetVReg(ArtMethod* m, uint16_t vreg, VRegKind kind, uint32_t* val) const {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200248 if (cur_quick_frame_ != nullptr) {
249 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800250 DCHECK(m == GetMethod());
Mingyao Yang99170c62015-07-06 11:10:37 -0700251 // Check if there is value set by the debugger.
252 if (GetVRegFromDebuggerShadowFrame(vreg, kind, val)) {
253 return true;
254 }
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100255 DCHECK(cur_oat_quick_method_header_->IsOptimized());
256 return GetVRegFromOptimizedCode(m, vreg, kind, val);
Ian Rogers0399dde2012-06-06 17:09:28 -0700257 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100258 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertz09687442015-11-17 10:35:39 +0100259 if (kind == kReferenceVReg) {
260 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
261 cur_shadow_frame_->GetVRegReference(vreg)));
262 } else {
263 *val = cur_shadow_frame_->GetVReg(vreg);
264 }
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200265 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700266 }
267}
268
Mathieu Chartiere401d142015-04-22 13:56:20 -0700269bool StackVisitor::GetVRegFromOptimizedCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100270 uint32_t* val) const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100271 DCHECK_EQ(m, GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100272 const DexFile::CodeItem* code_item = m->GetCodeItem();
David Sehr709b0702016-10-13 09:12:37 -0700273 DCHECK(code_item != nullptr) << m->PrettyMethod(); // Can't be null or how would we compile
274 // its instructions?
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000275 uint16_t number_of_dex_registers = code_item->registers_size_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100276 DCHECK_LT(vreg, code_item->registers_size_);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100277 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
278 CodeInfo code_info = method_header->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000279 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100280
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100281 uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100282 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100283 DCHECK(stack_map.IsValid());
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100284 size_t depth_in_stack_map = current_inlining_depth_ - 1;
285
286 DexRegisterMap dex_register_map = IsInInlinedFrame()
David Brazdilf677ebf2015-05-29 16:29:43 +0100287 ? code_info.GetDexRegisterMapAtDepth(depth_in_stack_map,
288 code_info.GetInlineInfoOf(stack_map, encoding),
289 encoding,
290 number_of_dex_registers)
291 : code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100292
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000293 if (!dex_register_map.IsValid()) {
294 return false;
295 }
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000296 DexRegisterLocation::Kind location_kind =
David Brazdilf677ebf2015-05-29 16:29:43 +0100297 dex_register_map.GetLocationKind(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100298 switch (location_kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000299 case DexRegisterLocation::Kind::kInStack: {
David Brazdilf677ebf2015-05-29 16:29:43 +0100300 const int32_t offset = dex_register_map.GetStackOffsetInBytes(vreg,
301 number_of_dex_registers,
302 code_info,
303 encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100304 const uint8_t* addr = reinterpret_cast<const uint8_t*>(cur_quick_frame_) + offset;
305 *val = *reinterpret_cast<const uint32_t*>(addr);
306 return true;
307 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000308 case DexRegisterLocation::Kind::kInRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100309 case DexRegisterLocation::Kind::kInRegisterHigh:
310 case DexRegisterLocation::Kind::kInFpuRegister:
311 case DexRegisterLocation::Kind::kInFpuRegisterHigh: {
David Brazdilf677ebf2015-05-29 16:29:43 +0100312 uint32_t reg =
313 dex_register_map.GetMachineRegister(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100314 return GetRegisterIfAccessible(reg, kind, val);
315 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000316 case DexRegisterLocation::Kind::kConstant:
David Brazdilf677ebf2015-05-29 16:29:43 +0100317 *val = dex_register_map.GetConstant(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100318 return true;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000319 case DexRegisterLocation::Kind::kNone:
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100320 return false;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000321 default:
322 LOG(FATAL)
David Srbecky7dc11782016-02-25 13:23:56 +0000323 << "Unexpected location kind "
324 << dex_register_map.GetLocationInternalKind(vreg,
325 number_of_dex_registers,
326 code_info,
327 encoding);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000328 UNREACHABLE();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100329 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100330}
331
332bool StackVisitor::GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const {
333 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
David Brazdil77a48ae2015-09-15 12:34:04 +0000334
Vladimir Marko239d6ea2016-09-05 10:44:04 +0100335 if (kRuntimeISA == InstructionSet::kX86 && is_float) {
336 // X86 float registers are 64-bit and each XMM register is provided as two separate
337 // 32-bit registers by the context.
338 reg = (kind == kDoubleHiVReg) ? (2 * reg + 1) : (2 * reg);
339 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000340
Goran Jakovljevic986660c2015-12-10 11:44:50 +0100341 // MIPS32 float registers are used as 64-bit (for MIPS32r2 it is pair
342 // F(2n)-F(2n+1), and for MIPS32r6 it is 64-bit register F(2n)). When
343 // accessing upper 32-bits from double, reg + 1 should be used.
344 if ((kRuntimeISA == InstructionSet::kMips) && (kind == kDoubleHiVReg)) {
345 DCHECK_ALIGNED(reg, 2);
346 reg++;
347 }
348
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100349 if (!IsAccessibleRegister(reg, is_float)) {
350 return false;
351 }
352 uintptr_t ptr_val = GetRegister(reg, is_float);
353 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
354 if (target64) {
355 const bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
356 const bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
357 int64_t value_long = static_cast<int64_t>(ptr_val);
358 if (wide_lo) {
359 ptr_val = static_cast<uintptr_t>(Low32Bits(value_long));
360 } else if (wide_hi) {
361 ptr_val = static_cast<uintptr_t>(High32Bits(value_long));
362 }
363 }
364 *val = ptr_val;
365 return true;
366}
367
Mingyao Yang99170c62015-07-06 11:10:37 -0700368bool StackVisitor::GetVRegPairFromDebuggerShadowFrame(uint16_t vreg,
369 VRegKind kind_lo,
370 VRegKind kind_hi,
371 uint64_t* val) const {
372 uint32_t low_32bits;
373 uint32_t high_32bits;
374 bool success = GetVRegFromDebuggerShadowFrame(vreg, kind_lo, &low_32bits);
375 success &= GetVRegFromDebuggerShadowFrame(vreg + 1, kind_hi, &high_32bits);
376 if (success) {
377 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
378 }
379 return success;
380}
381
Mathieu Chartiere401d142015-04-22 13:56:20 -0700382bool StackVisitor::GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200383 VRegKind kind_hi, uint64_t* val) const {
384 if (kind_lo == kLongLoVReg) {
385 DCHECK_EQ(kind_hi, kLongHiVReg);
386 } else if (kind_lo == kDoubleLoVReg) {
387 DCHECK_EQ(kind_hi, kDoubleHiVReg);
388 } else {
389 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100390 UNREACHABLE();
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200391 }
Mingyao Yang99170c62015-07-06 11:10:37 -0700392 // Check if there is value set by the debugger.
393 if (GetVRegPairFromDebuggerShadowFrame(vreg, kind_lo, kind_hi, val)) {
394 return true;
395 }
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200396 if (cur_quick_frame_ != nullptr) {
397 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
398 DCHECK(m == GetMethod());
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100399 DCHECK(cur_oat_quick_method_header_->IsOptimized());
400 return GetVRegPairFromOptimizedCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200401 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100402 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200403 *val = cur_shadow_frame_->GetVRegLong(vreg);
404 return true;
405 }
406}
407
Mathieu Chartiere401d142015-04-22 13:56:20 -0700408bool StackVisitor::GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100409 VRegKind kind_lo, VRegKind kind_hi,
410 uint64_t* val) const {
411 uint32_t low_32bits;
412 uint32_t high_32bits;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700413 bool success = GetVRegFromOptimizedCode(m, vreg, kind_lo, &low_32bits);
414 success &= GetVRegFromOptimizedCode(m, vreg + 1, kind_hi, &high_32bits);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100415 if (success) {
416 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
417 }
418 return success;
419}
420
421bool StackVisitor::GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
422 VRegKind kind_lo, uint64_t* val) const {
423 const bool is_float = (kind_lo == kDoubleLoVReg);
424 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
425 return false;
426 }
427 uintptr_t ptr_val_lo = GetRegister(reg_lo, is_float);
428 uintptr_t ptr_val_hi = GetRegister(reg_hi, is_float);
429 bool target64 = Is64BitInstructionSet(kRuntimeISA);
430 if (target64) {
431 int64_t value_long_lo = static_cast<int64_t>(ptr_val_lo);
432 int64_t value_long_hi = static_cast<int64_t>(ptr_val_hi);
433 ptr_val_lo = static_cast<uintptr_t>(Low32Bits(value_long_lo));
434 ptr_val_hi = static_cast<uintptr_t>(High32Bits(value_long_hi));
435 }
436 *val = (static_cast<uint64_t>(ptr_val_hi) << 32) | static_cast<uint32_t>(ptr_val_lo);
437 return true;
438}
439
Mingyao Yang636b9252015-07-31 16:40:24 -0700440bool StackVisitor::SetVReg(ArtMethod* m,
441 uint16_t vreg,
442 uint32_t new_value,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800443 VRegKind kind) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700444 const DexFile::CodeItem* code_item = m->GetCodeItem();
445 if (code_item == nullptr) {
446 return false;
447 }
448 ShadowFrame* shadow_frame = GetCurrentShadowFrame();
449 if (shadow_frame == nullptr) {
450 // This is a compiled frame: we must prepare and update a shadow frame that will
451 // be executed by the interpreter after deoptimization of the stack.
452 const size_t frame_id = GetFrameId();
453 const uint16_t num_regs = code_item->registers_size_;
454 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
455 CHECK(shadow_frame != nullptr);
456 // Remember the vreg has been set for debugging and must not be overwritten by the
457 // original value during deoptimization of the stack.
458 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
459 }
460 if (kind == kReferenceVReg) {
461 shadow_frame->SetVRegReference(vreg, reinterpret_cast<mirror::Object*>(new_value));
462 } else {
463 shadow_frame->SetVReg(vreg, new_value);
464 }
465 return true;
466}
467
Mingyao Yang636b9252015-07-31 16:40:24 -0700468bool StackVisitor::SetVRegPair(ArtMethod* m,
469 uint16_t vreg,
470 uint64_t new_value,
471 VRegKind kind_lo,
472 VRegKind kind_hi) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700473 if (kind_lo == kLongLoVReg) {
474 DCHECK_EQ(kind_hi, kLongHiVReg);
475 } else if (kind_lo == kDoubleLoVReg) {
476 DCHECK_EQ(kind_hi, kDoubleHiVReg);
477 } else {
478 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
479 UNREACHABLE();
480 }
481 const DexFile::CodeItem* code_item = m->GetCodeItem();
482 if (code_item == nullptr) {
483 return false;
484 }
485 ShadowFrame* shadow_frame = GetCurrentShadowFrame();
486 if (shadow_frame == nullptr) {
487 // This is a compiled frame: we must prepare for deoptimization (see SetVRegFromDebugger).
488 const size_t frame_id = GetFrameId();
489 const uint16_t num_regs = code_item->registers_size_;
490 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
491 CHECK(shadow_frame != nullptr);
492 // Remember the vreg pair has been set for debugging and must not be overwritten by the
493 // original value during deoptimization of the stack.
494 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
495 thread_->GetUpdatedVRegFlags(frame_id)[vreg + 1] = true;
496 }
497 shadow_frame->SetVRegLong(vreg, new_value);
498 return true;
499}
500
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100501bool StackVisitor::IsAccessibleGPR(uint32_t reg) const {
502 DCHECK(context_ != nullptr);
503 return context_->IsAccessibleGPR(reg);
504}
505
Mathieu Chartier815873e2014-02-13 18:02:13 -0800506uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100507 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
508 DCHECK(context_ != nullptr);
Mathieu Chartier815873e2014-02-13 18:02:13 -0800509 return context_->GetGPRAddress(reg);
510}
511
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100512uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
513 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
514 DCHECK(context_ != nullptr);
515 return context_->GetGPR(reg);
Ian Rogers0399dde2012-06-06 17:09:28 -0700516}
517
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100518bool StackVisitor::IsAccessibleFPR(uint32_t reg) const {
519 DCHECK(context_ != nullptr);
520 return context_->IsAccessibleFPR(reg);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200521}
522
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100523uintptr_t StackVisitor::GetFPR(uint32_t reg) const {
524 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
525 DCHECK(context_ != nullptr);
526 return context_->GetFPR(reg);
527}
528
Ian Rogers0399dde2012-06-06 17:09:28 -0700529uintptr_t StackVisitor::GetReturnPc() const {
Ian Rogers13735952014-10-08 12:43:28 -0700530 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700531 DCHECK(sp != nullptr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100532 uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogers0399dde2012-06-06 17:09:28 -0700533 return *reinterpret_cast<uintptr_t*>(pc_addr);
534}
535
536void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
Ian Rogers13735952014-10-08 12:43:28 -0700537 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700538 CHECK(sp != nullptr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100539 uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogers0399dde2012-06-06 17:09:28 -0700540 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc;
541}
542
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100543size_t StackVisitor::ComputeNumFrames(Thread* thread, StackWalkKind walk_kind) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700544 struct NumFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100545 NumFramesVisitor(Thread* thread_in, StackWalkKind walk_kind_in)
546 : StackVisitor(thread_in, nullptr, walk_kind_in), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700547
Ian Rogers5cf98192014-05-29 21:31:50 -0700548 bool VisitFrame() OVERRIDE {
Ian Rogers0399dde2012-06-06 17:09:28 -0700549 frames++;
550 return true;
551 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700552
Ian Rogers0399dde2012-06-06 17:09:28 -0700553 size_t frames;
554 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100555 NumFramesVisitor visitor(thread, walk_kind);
Ian Rogers0399dde2012-06-06 17:09:28 -0700556 visitor.WalkStack(true);
557 return visitor.frames;
558}
559
Mathieu Chartiere401d142015-04-22 13:56:20 -0700560bool StackVisitor::GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700561 struct HasMoreFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100562 HasMoreFramesVisitor(Thread* thread,
563 StackWalkKind walk_kind,
564 size_t num_frames,
565 size_t frame_height)
566 : StackVisitor(thread, nullptr, walk_kind, num_frames),
567 frame_height_(frame_height),
568 found_frame_(false),
569 has_more_frames_(false),
570 next_method_(nullptr),
571 next_dex_pc_(0) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700572 }
573
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700574 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700575 if (found_frame_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700576 ArtMethod* method = GetMethod();
Ian Rogers5cf98192014-05-29 21:31:50 -0700577 if (method != nullptr && !method->IsRuntimeMethod()) {
578 has_more_frames_ = true;
579 next_method_ = method;
580 next_dex_pc_ = GetDexPc();
581 return false; // End stack walk once next method is found.
582 }
583 } else if (GetFrameHeight() == frame_height_) {
584 found_frame_ = true;
585 }
586 return true;
587 }
588
589 size_t frame_height_;
590 bool found_frame_;
591 bool has_more_frames_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700592 ArtMethod* next_method_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700593 uint32_t next_dex_pc_;
594 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100595 HasMoreFramesVisitor visitor(thread_, walk_kind_, GetNumFrames(), GetFrameHeight());
Ian Rogers5cf98192014-05-29 21:31:50 -0700596 visitor.WalkStack(true);
597 *next_method = visitor.next_method_;
598 *next_dex_pc = visitor.next_dex_pc_;
599 return visitor.has_more_frames_;
600}
601
Ian Rogers7a22fa62013-01-23 12:16:16 -0800602void StackVisitor::DescribeStack(Thread* thread) {
Ian Rogers306057f2012-11-26 12:45:53 -0800603 struct DescribeStackVisitor : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800604 explicit DescribeStackVisitor(Thread* thread_in)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100605 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800606
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700607 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers306057f2012-11-26 12:45:53 -0800608 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation();
609 return true;
610 }
611 };
Ian Rogers7a22fa62013-01-23 12:16:16 -0800612 DescribeStackVisitor visitor(thread);
Ian Rogers306057f2012-11-26 12:45:53 -0800613 visitor.WalkStack(true);
614}
615
Ian Rogers40e3bac2012-11-20 00:09:14 -0800616std::string StackVisitor::DescribeLocation() const {
617 std::string result("Visiting method '");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700618 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700619 if (m == nullptr) {
Ian Rogers306057f2012-11-26 12:45:53 -0800620 return "upcall";
621 }
David Sehr709b0702016-10-13 09:12:37 -0700622 result += m->PrettyMethod();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800623 result += StringPrintf("' at dex PC 0x%04x", GetDexPc());
Ian Rogers40e3bac2012-11-20 00:09:14 -0800624 if (!IsShadowFrame()) {
625 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc()));
626 }
627 return result;
628}
629
Alex Lightdba61482016-12-21 08:20:29 -0800630void StackVisitor::SetMethod(ArtMethod* method) {
631 DCHECK(GetMethod() != nullptr);
632 if (cur_shadow_frame_ != nullptr) {
633 cur_shadow_frame_->SetMethod(method);
634 } else {
635 DCHECK(cur_quick_frame_ != nullptr);
636 CHECK(!IsInInlinedFrame()) << "We do not support setting inlined method's ArtMethod!";
Alex Light1ebe4fe2017-01-30 14:57:11 -0800637 *cur_quick_frame_ = method;
Alex Lightdba61482016-12-21 08:20:29 -0800638 }
639}
640
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100641static void AssertPcIsWithinQuickCode(ArtMethod* method, uintptr_t pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700642 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100643 if (method->IsNative() || method->IsRuntimeMethod() || method->IsProxyMethod()) {
644 return;
645 }
646
647 if (pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) {
648 return;
649 }
650
651 const void* code = method->GetEntryPointFromQuickCompiledCode();
652 if (code == GetQuickInstrumentationEntryPoint()) {
653 return;
654 }
655
656 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
657 if (class_linker->IsQuickToInterpreterBridge(code) ||
658 class_linker->IsQuickResolutionStub(code)) {
659 return;
660 }
661
662 // If we are the JIT then we may have just compiled the method after the
663 // IsQuickToInterpreterBridge check.
Calin Juravleffc87072016-04-20 14:22:09 +0100664 Runtime* runtime = Runtime::Current();
665 if (runtime->UseJitCompilation() && runtime->GetJit()->GetCodeCache()->ContainsPc(code)) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100666 return;
667 }
668
Mingyao Yang063fc772016-08-02 11:02:54 -0700669 uint32_t code_size = OatQuickMethodHeader::FromEntryPoint(code)->GetCodeSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100670 uintptr_t code_start = reinterpret_cast<uintptr_t>(code);
671 CHECK(code_start <= pc && pc <= (code_start + code_size))
David Sehr709b0702016-10-13 09:12:37 -0700672 << method->PrettyMethod()
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100673 << " pc=" << std::hex << pc
Roland Levillain0d5a2812015-11-13 10:07:31 +0000674 << " code_start=" << code_start
675 << " code_size=" << code_size;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100676}
677
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700678void StackVisitor::SanityCheckFrame() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800679 if (kIsDebugBuild) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700680 ArtMethod* method = GetMethod();
681 auto* declaring_class = method->GetDeclaringClass();
682 // Runtime methods have null declaring class.
683 if (!method->IsRuntimeMethod()) {
684 CHECK(declaring_class != nullptr);
685 CHECK_EQ(declaring_class->GetClass(), declaring_class->GetClass()->GetClass())
686 << declaring_class;
687 } else {
688 CHECK(declaring_class == nullptr);
689 }
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700690 Runtime* const runtime = Runtime::Current();
691 LinearAlloc* const linear_alloc = runtime->GetLinearAlloc();
692 if (!linear_alloc->Contains(method)) {
693 // Check class linker linear allocs.
694 mirror::Class* klass = method->GetDeclaringClass();
695 LinearAlloc* const class_linear_alloc = (klass != nullptr)
Mathieu Chartier5b830502016-03-02 10:30:23 -0800696 ? runtime->GetClassLinker()->GetAllocatorForClassLoader(klass->GetClassLoader())
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700697 : linear_alloc;
698 if (!class_linear_alloc->Contains(method)) {
699 // Check image space.
700 bool in_image = false;
701 for (auto& space : runtime->GetHeap()->GetContinuousSpaces()) {
702 if (space->IsImageSpace()) {
703 auto* image_space = space->AsImageSpace();
704 const auto& header = image_space->GetImageHeader();
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700705 const ImageSection& methods = header.GetMethodsSection();
706 const ImageSection& runtime_methods = header.GetRuntimeMethodsSection();
707 const size_t offset = reinterpret_cast<const uint8_t*>(method) - image_space->Begin();
708 if (methods.Contains(offset) || runtime_methods.Contains(offset)) {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700709 in_image = true;
710 break;
711 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700712 }
713 }
David Sehr709b0702016-10-13 09:12:37 -0700714 CHECK(in_image) << method->PrettyMethod() << " not in linear alloc or image";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700715 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700716 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800717 if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100718 AssertPcIsWithinQuickCode(method, cur_quick_frame_pc_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800719 // Frame sanity.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100720 size_t frame_size = GetCurrentQuickFrameInfo().FrameSizeInBytes();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800721 CHECK_NE(frame_size, 0u);
Andreas Gampe5b417b92014-03-10 14:18:35 -0700722 // A rough guess at an upper size we expect to see for a frame.
723 // 256 registers
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700724 // 2 words HandleScope overhead
Andreas Gampe5b417b92014-03-10 14:18:35 -0700725 // 3+3 register spills
726 // TODO: this seems architecture specific for the case of JNI frames.
Brian Carlstromed08bd42014-03-19 18:34:17 -0700727 // TODO: 083-compiler-regressions ManyFloatArgs shows this estimate is wrong.
728 // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word);
729 const size_t kMaxExpectedFrameSize = 2 * KB;
David Sehr709b0702016-10-13 09:12:37 -0700730 CHECK_LE(frame_size, kMaxExpectedFrameSize) << method->PrettyMethod();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100731 size_t return_pc_offset = GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800732 CHECK_LT(return_pc_offset, frame_size);
733 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700734 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700735}
736
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100737// Counts the number of references in the parameter list of the corresponding method.
738// Note: Thus does _not_ include "this" for non-static methods.
739static uint32_t GetNumberOfReferenceArgsWithoutReceiver(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700740 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100741 uint32_t shorty_len;
742 const char* shorty = method->GetShorty(&shorty_len);
743 uint32_t refs = 0;
744 for (uint32_t i = 1; i < shorty_len ; ++i) {
745 if (shorty[i] == 'L') {
746 refs++;
747 }
748 }
749 return refs;
750}
751
752QuickMethodFrameInfo StackVisitor::GetCurrentQuickFrameInfo() const {
753 if (cur_oat_quick_method_header_ != nullptr) {
754 return cur_oat_quick_method_header_->GetFrameInfo();
755 }
756
757 ArtMethod* method = GetMethod();
758 Runtime* runtime = Runtime::Current();
759
760 if (method->IsAbstract()) {
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100761 return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100762 }
763
764 // This goes before IsProxyMethod since runtime methods have a null declaring class.
765 if (method->IsRuntimeMethod()) {
766 return runtime->GetRuntimeMethodFrameInfo(method);
767 }
768
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100769 if (method->IsProxyMethod()) {
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000770 // There is only one direct method of a proxy class: the constructor. A direct method is
771 // cloned from the original java.lang.reflect.Proxy and is executed as usual quick
772 // compiled method without any stubs. Therefore the method must have a OatQuickMethodHeader.
773 DCHECK(!method->IsDirect() && !method->IsConstructor())
774 << "Constructors of proxy classes must have a OatQuickMethodHeader";
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100775 return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100776 }
777
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000778 // The only remaining case is if the method is native and uses the generic JNI stub.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100779 DCHECK(method->IsNative());
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000780 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampe542451c2016-07-26 09:02:02 -0700781 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(method,
782 kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700783 DCHECK(class_linker->IsQuickGenericJniStub(entry_point)) << method->PrettyMethod();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100784 // Generic JNI frame.
785 uint32_t handle_refs = GetNumberOfReferenceArgsWithoutReceiver(method) + 1;
786 size_t scope_size = HandleScope::SizeOf(handle_refs);
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100787 QuickMethodFrameInfo callee_info =
788 runtime->GetCalleeSaveMethodFrameInfo(Runtime::kSaveRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100789
790 // Callee saves + handle scope + method ref + alignment
791 // Note: -sizeof(void*) since callee-save frame stores a whole method pointer.
792 size_t frame_size = RoundUp(
793 callee_info.FrameSizeInBytes() - sizeof(void*) + sizeof(ArtMethod*) + scope_size,
794 kStackAlignment);
795 return QuickMethodFrameInfo(frame_size, callee_info.CoreSpillMask(), callee_info.FpSpillMask());
796}
797
Andreas Gampe585da952016-12-02 14:52:29 -0800798template <StackVisitor::CountTransitions kCount>
Ian Rogers0399dde2012-06-06 17:09:28 -0700799void StackVisitor::WalkStack(bool include_transitions) {
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -0800800 if (check_suspended_) {
801 DCHECK(thread_ == Thread::Current() || thread_->IsSuspended());
802 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800803 CHECK_EQ(cur_depth_, 0U);
804 bool exit_stubs_installed = Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled();
Alex Lightb81a9842016-12-15 00:59:05 +0000805 uint32_t instrumentation_stack_depth = 0;
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000806 size_t inlined_frames_count = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700807
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700808 for (const ManagedStack* current_fragment = thread_->GetManagedStack();
809 current_fragment != nullptr; current_fragment = current_fragment->GetLink()) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700810 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
811 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700812 cur_quick_frame_pc_ = 0;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100813 cur_oat_quick_method_header_ = nullptr;
Dave Allisonf9439142014-03-27 15:10:22 -0700814
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700815 if (cur_quick_frame_ != nullptr) { // Handle quick stack frames.
Ian Rogers0399dde2012-06-06 17:09:28 -0700816 // Can't be both a shadow and a quick fragment.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700817 DCHECK(current_fragment->GetTopShadowFrame() == nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700818 ArtMethod* method = *cur_quick_frame_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700819 while (method != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100820 cur_oat_quick_method_header_ = method->GetOatQuickMethodHeader(cur_quick_frame_pc_);
Dave Allison5cd33752014-04-15 15:57:58 -0700821 SanityCheckFrame();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100822
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100823 if ((walk_kind_ == StackWalkKind::kIncludeInlinedFrames)
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100824 && (cur_oat_quick_method_header_ != nullptr)
825 && cur_oat_quick_method_header_->IsOptimized()) {
826 CodeInfo code_info = cur_oat_quick_method_header_->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000827 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100828 uint32_t native_pc_offset =
829 cur_oat_quick_method_header_->NativeQuickPcOffset(cur_quick_frame_pc_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100830 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800831 if (stack_map.IsValid() && stack_map.HasInlineInfo(encoding.stack_map.encoding)) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100832 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100833 DCHECK_EQ(current_inlining_depth_, 0u);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800834 for (current_inlining_depth_ = inline_info.GetDepth(encoding.inline_info.encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100835 current_inlining_depth_ != 0;
836 --current_inlining_depth_) {
837 bool should_continue = VisitFrame();
838 if (UNLIKELY(!should_continue)) {
839 return;
840 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100841 cur_depth_++;
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000842 inlined_frames_count++;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100843 }
844 }
845 }
846
Dave Allison5cd33752014-04-15 15:57:58 -0700847 bool should_continue = VisitFrame();
848 if (UNLIKELY(!should_continue)) {
849 return;
Ian Rogers0399dde2012-06-06 17:09:28 -0700850 }
Dave Allison5cd33752014-04-15 15:57:58 -0700851
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100852 QuickMethodFrameInfo frame_info = GetCurrentQuickFrameInfo();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700853 if (context_ != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100854 context_->FillCalleeSaves(reinterpret_cast<uint8_t*>(cur_quick_frame_), frame_info);
Ian Rogers0399dde2012-06-06 17:09:28 -0700855 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700856 // Compute PC for next stack frame from return PC.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100857 size_t frame_size = frame_info.FrameSizeInBytes();
858 size_t return_pc_offset = frame_size - sizeof(void*);
Ian Rogers13735952014-10-08 12:43:28 -0700859 uint8_t* return_pc_addr = reinterpret_cast<uint8_t*>(cur_quick_frame_) + return_pc_offset;
Ian Rogers0399dde2012-06-06 17:09:28 -0700860 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100861
Ian Rogers62d6c772013-02-27 08:32:07 -0800862 if (UNLIKELY(exit_stubs_installed)) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700863 // While profiling, the return pc is restored from the side stack, except when walking
864 // the stack for an exception where the side stack will be unwound in VisitFrame.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700865 if (reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc) {
Andreas Gampe585da952016-12-02 14:52:29 -0800866 CHECK_LT(instrumentation_stack_depth, thread_->GetInstrumentationStack()->size());
Sebastien Hertz74e256b2013-10-04 10:40:37 +0200867 const instrumentation::InstrumentationStackFrame& instrumentation_frame =
Alex Lightb81a9842016-12-15 00:59:05 +0000868 thread_->GetInstrumentationStack()->at(instrumentation_stack_depth);
jeffhao725a9572012-11-13 18:20:12 -0800869 instrumentation_stack_depth++;
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100870 if (GetMethod() ==
871 Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveAllCalleeSaves)) {
Jeff Haofb2802d2013-07-24 13:53:05 -0700872 // Skip runtime save all callee frames which are used to deliver exceptions.
873 } else if (instrumentation_frame.interpreter_entry_) {
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100874 ArtMethod* callee =
875 Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveRefsAndArgs);
David Sehr709b0702016-10-13 09:12:37 -0700876 CHECK_EQ(GetMethod(), callee) << "Expected: " << ArtMethod::PrettyMethod(callee)
877 << " Found: " << ArtMethod::PrettyMethod(GetMethod());
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000878 } else {
Alex Lighteee0bd42017-02-14 15:31:45 +0000879 // Instrumentation generally doesn't distinguish between a method's obsolete and
880 // non-obsolete version.
881 CHECK_EQ(instrumentation_frame.method_->GetNonObsoleteMethod(),
882 GetMethod()->GetNonObsoleteMethod())
883 << "Expected: "
884 << ArtMethod::PrettyMethod(instrumentation_frame.method_->GetNonObsoleteMethod())
885 << " Found: " << ArtMethod::PrettyMethod(GetMethod()->GetNonObsoleteMethod());
Ian Rogers62d6c772013-02-27 08:32:07 -0800886 }
887 if (num_frames_ != 0) {
888 // Check agreement of frame Ids only if num_frames_ is computed to avoid infinite
889 // recursion.
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000890 size_t frame_id = instrumentation::Instrumentation::ComputeFrameId(
891 thread_,
892 cur_depth_,
893 inlined_frames_count);
894 CHECK_EQ(instrumentation_frame.frame_id_, frame_id);
Ian Rogers62d6c772013-02-27 08:32:07 -0800895 }
jeffhao725a9572012-11-13 18:20:12 -0800896 return_pc = instrumentation_frame.return_pc_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700897 }
898 }
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100899
Ian Rogers0399dde2012-06-06 17:09:28 -0700900 cur_quick_frame_pc_ = return_pc;
Ian Rogers13735952014-10-08 12:43:28 -0700901 uint8_t* next_frame = reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700902 cur_quick_frame_ = reinterpret_cast<ArtMethod**>(next_frame);
903
904 if (kDebugStackWalk) {
David Sehr709b0702016-10-13 09:12:37 -0700905 LOG(INFO) << ArtMethod::PrettyMethod(method) << "@" << method << " size=" << frame_size
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100906 << std::boolalpha
907 << " optimized=" << (cur_oat_quick_method_header_ != nullptr &&
908 cur_oat_quick_method_header_->IsOptimized())
Mathieu Chartiere401d142015-04-22 13:56:20 -0700909 << " native=" << method->IsNative()
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100910 << std::noboolalpha
Mathieu Chartiere401d142015-04-22 13:56:20 -0700911 << " entrypoints=" << method->GetEntryPointFromQuickCompiledCode()
Alex Lighteee0bd42017-02-14 15:31:45 +0000912 << "," << (method->IsNative() ? method->GetEntryPointFromJni() : nullptr)
Mathieu Chartiere401d142015-04-22 13:56:20 -0700913 << " next=" << *cur_quick_frame_;
914 }
915
Ian Rogers0399dde2012-06-06 17:09:28 -0700916 cur_depth_++;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700917 method = *cur_quick_frame_;
jeffhao6641ea12013-01-02 18:13:42 -0800918 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700919 } else if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700920 do {
921 SanityCheckFrame();
922 bool should_continue = VisitFrame();
923 if (UNLIKELY(!should_continue)) {
924 return;
925 }
926 cur_depth_++;
927 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700928 } while (cur_shadow_frame_ != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700929 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700930 if (include_transitions) {
931 bool should_continue = VisitFrame();
932 if (!should_continue) {
933 return;
934 }
935 }
Andreas Gampe585da952016-12-02 14:52:29 -0800936 if (kCount == CountTransitions::kYes) {
937 cur_depth_++;
938 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800939 }
940 if (num_frames_ != 0) {
941 CHECK_EQ(cur_depth_, num_frames_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700942 }
943}
944
Andreas Gampe585da952016-12-02 14:52:29 -0800945template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kYes>(bool);
946template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kNo>(bool);
947
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800948void JavaFrameRootInfo::Describe(std::ostream& os) const {
949 const StackVisitor* visitor = stack_visitor_;
950 CHECK(visitor != nullptr);
951 os << "Type=" << GetType() << " thread_id=" << GetThreadId() << " location=" <<
952 visitor->DescribeLocation() << " vreg=" << vreg_;
953}
954
Mathieu Chartiere401d142015-04-22 13:56:20 -0700955int StackVisitor::GetVRegOffsetFromQuickCode(const DexFile::CodeItem* code_item,
956 uint32_t core_spills, uint32_t fp_spills,
957 size_t frame_size, int reg, InstructionSet isa) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700958 PointerSize pointer_size = InstructionSetPointerSize(isa);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700959 if (kIsDebugBuild) {
960 auto* runtime = Runtime::Current();
961 if (runtime != nullptr) {
962 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
963 }
964 }
Roland Levillain14d90572015-07-16 10:52:26 +0100965 DCHECK_ALIGNED(frame_size, kStackAlignment);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700966 DCHECK_NE(reg, -1);
967 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
968 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
969 + sizeof(uint32_t); // Filler.
970 int num_regs = code_item->registers_size_ - code_item->ins_size_;
971 int temp_threshold = code_item->registers_size_;
972 const int max_num_special_temps = 1;
973 if (reg == temp_threshold) {
974 // The current method pointer corresponds to special location on stack.
975 return 0;
976 } else if (reg >= temp_threshold + max_num_special_temps) {
977 /*
978 * Special temporaries may have custom locations and the logic above deals with that.
979 * However, non-special temporaries are placed relative to the outs.
980 */
Andreas Gampe542451c2016-07-26 09:02:02 -0700981 int temps_start = code_item->outs_size_ * sizeof(uint32_t)
982 + static_cast<size_t>(pointer_size) /* art method */;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700983 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
984 return temps_start + relative_offset;
985 } else if (reg < num_regs) {
986 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
987 return locals_start + (reg * sizeof(uint32_t));
988 } else {
989 // Handle ins.
Andreas Gampe542451c2016-07-26 09:02:02 -0700990 return frame_size + ((reg - num_regs) * sizeof(uint32_t))
991 + static_cast<size_t>(pointer_size) /* art method */;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700992 }
993}
994
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700995void LockCountData::AddMonitor(Thread* self, mirror::Object* obj) {
Andreas Gampe03ec9302015-08-27 17:41:47 -0700996 if (obj == nullptr) {
997 return;
998 }
999
1000 // If there's an error during enter, we won't have locked the monitor. So check there's no
1001 // exception.
1002 if (self->IsExceptionPending()) {
1003 return;
1004 }
1005
1006 if (monitors_ == nullptr) {
1007 monitors_.reset(new std::vector<mirror::Object*>());
1008 }
1009 monitors_->push_back(obj);
1010}
1011
Andreas Gampe56fdd0e2016-04-28 14:56:54 -07001012void LockCountData::RemoveMonitorOrThrow(Thread* self, const mirror::Object* obj) {
Andreas Gampe03ec9302015-08-27 17:41:47 -07001013 if (obj == nullptr) {
1014 return;
1015 }
1016 bool found_object = false;
1017 if (monitors_ != nullptr) {
1018 // We need to remove one pointer to ref, as duplicates are used for counting recursive locks.
1019 // We arbitrarily choose the first one.
1020 auto it = std::find(monitors_->begin(), monitors_->end(), obj);
1021 if (it != monitors_->end()) {
1022 monitors_->erase(it);
1023 found_object = true;
1024 }
1025 }
1026 if (!found_object) {
1027 // The object wasn't found. Time for an IllegalMonitorStateException.
1028 // The order here isn't fully clear. Assume that any other pending exception is swallowed.
1029 // TODO: Maybe make already pending exception a suppressed exception.
1030 self->ClearException();
1031 self->ThrowNewExceptionF("Ljava/lang/IllegalMonitorStateException;",
1032 "did not lock monitor on object of type '%s' before unlocking",
David Sehr709b0702016-10-13 09:12:37 -07001033 const_cast<mirror::Object*>(obj)->PrettyTypeOf().c_str());
Andreas Gampe03ec9302015-08-27 17:41:47 -07001034 }
1035}
1036
1037// Helper to unlock a monitor. Must be NO_THREAD_SAFETY_ANALYSIS, as we can't statically show
1038// that the object was locked.
1039void MonitorExitHelper(Thread* self, mirror::Object* obj) NO_THREAD_SAFETY_ANALYSIS {
1040 DCHECK(self != nullptr);
1041 DCHECK(obj != nullptr);
1042 obj->MonitorExit(self);
1043}
1044
Andreas Gampe56fdd0e2016-04-28 14:56:54 -07001045bool LockCountData::CheckAllMonitorsReleasedOrThrow(Thread* self) {
Andreas Gampe03ec9302015-08-27 17:41:47 -07001046 DCHECK(self != nullptr);
1047 if (monitors_ != nullptr) {
1048 if (!monitors_->empty()) {
1049 // There may be an exception pending, if the method is terminating abruptly. Clear it.
1050 // TODO: Should we add this as a suppressed exception?
1051 self->ClearException();
1052
1053 // OK, there are monitors that are still locked. To enforce structured locking (and avoid
1054 // deadlocks) we unlock all of them before we raise the IllegalMonitorState exception.
1055 for (mirror::Object* obj : *monitors_) {
1056 MonitorExitHelper(self, obj);
1057 // If this raised an exception, ignore. TODO: Should we add this as suppressed
1058 // exceptions?
1059 if (self->IsExceptionPending()) {
1060 self->ClearException();
1061 }
1062 }
1063 // Raise an exception, just give the first object as the sample.
1064 mirror::Object* first = (*monitors_)[0];
1065 self->ThrowNewExceptionF("Ljava/lang/IllegalMonitorStateException;",
1066 "did not unlock monitor on object of type '%s'",
David Sehr709b0702016-10-13 09:12:37 -07001067 mirror::Object::PrettyTypeOf(first).c_str());
Andreas Gampe03ec9302015-08-27 17:41:47 -07001068
1069 // To make sure this path is not triggered again, clean out the monitors.
1070 monitors_->clear();
1071
1072 return false;
1073 }
1074 }
1075 return true;
1076}
1077
Elliott Hughes68e76522011-10-05 13:22:16 -07001078} // namespace art