blob: 56ef5aaa905e2358a46df2563666b212d877cc24 [file] [log] [blame]
Elliott Hughes68e76522011-10-05 13:22:16 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "stack.h"
18
Ian Rogerse63db272014-07-15 15:36:11 -070019#include "arch/context.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method-inl.h"
Dave Allisonf9439142014-03-27 15:10:22 -070021#include "base/hex_dump.h"
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010022#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070023#include "entrypoints/runtime_asm_entrypoints.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070024#include "gc/space/image_space.h"
25#include "gc/space/space-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010026#include "jit/jit.h"
27#include "jit/jit_code_cache.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "linear_alloc.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070029#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "mirror/object-inl.h"
31#include "mirror/object_array-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010032#include "oat_quick_method_header.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010033#include "quick/quick_method_frame_info.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070034#include "runtime.h"
Dave Allisonf9439142014-03-27 15:10:22 -070035#include "thread.h"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070036#include "thread_list.h"
Mathieu Chartier4e305412014-02-19 10:54:44 -080037#include "verify_object-inl.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070038
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080039namespace art {
40
Mathieu Chartier8405bfd2016-02-05 12:00:49 -080041static constexpr bool kDebugStackWalk = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -070042
Ian Rogers62d6c772013-02-27 08:32:07 -080043mirror::Object* ShadowFrame::GetThisObject() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -070044 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -080045 if (m->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070046 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -080047 } else if (m->IsNative()) {
48 return GetVRegReference(0);
49 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070050 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070051 CHECK(code_item != nullptr) << PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -080052 uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
53 return GetVRegReference(reg);
54 }
55}
56
Jeff Haoe701f482013-05-24 11:50:49 -070057mirror::Object* ShadowFrame::GetThisObject(uint16_t num_ins) const {
Mathieu Chartiere401d142015-04-22 13:56:20 -070058 ArtMethod* m = GetMethod();
Jeff Haoe701f482013-05-24 11:50:49 -070059 if (m->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070060 return nullptr;
Jeff Haoe701f482013-05-24 11:50:49 -070061 } else {
Jeff Hao8d448852013-06-03 17:26:19 -070062 return GetVRegReference(NumberOfVRegs() - num_ins);
Jeff Haoe701f482013-05-24 11:50:49 -070063 }
64}
65
TDYa127ce4cc0d2012-11-18 16:59:53 -080066size_t ManagedStack::NumJniShadowFrameReferences() const {
Ian Rogers0399dde2012-06-06 17:09:28 -070067 size_t count = 0;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070068 for (const ManagedStack* current_fragment = this; current_fragment != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070069 current_fragment = current_fragment->GetLink()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070070 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070071 current_frame = current_frame->GetLink()) {
TDYa127ce4cc0d2012-11-18 16:59:53 -080072 if (current_frame->GetMethod()->IsNative()) {
73 // The JNI ShadowFrame only contains references. (For indirect reference.)
74 count += current_frame->NumberOfVRegs();
75 }
Ian Rogers0399dde2012-06-06 17:09:28 -070076 }
77 }
78 return count;
79}
80
Ian Rogersef7d42f2014-01-06 12:55:46 -080081bool ManagedStack::ShadowFramesContain(StackReference<mirror::Object>* shadow_frame_entry) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070082 for (const ManagedStack* current_fragment = this; current_fragment != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070083 current_fragment = current_fragment->GetLink()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070084 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070085 current_frame = current_frame->GetLink()) {
86 if (current_frame->Contains(shadow_frame_entry)) {
87 return true;
88 }
89 }
90 }
91 return false;
92}
93
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010094StackVisitor::StackVisitor(Thread* thread, Context* context, StackWalkKind walk_kind)
95 : StackVisitor(thread, context, walk_kind, 0) {}
Ian Rogers7a22fa62013-01-23 12:16:16 -080096
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010097StackVisitor::StackVisitor(Thread* thread,
98 Context* context,
99 StackWalkKind walk_kind,
100 size_t num_frames)
101 : thread_(thread),
102 walk_kind_(walk_kind),
103 cur_shadow_frame_(nullptr),
104 cur_quick_frame_(nullptr),
105 cur_quick_frame_pc_(0),
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100106 cur_oat_quick_method_header_(nullptr),
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100107 num_frames_(num_frames),
108 cur_depth_(0),
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100109 current_inlining_depth_(0),
Ian Rogers5cf98192014-05-29 21:31:50 -0700110 context_(context) {
111 DCHECK(thread == Thread::Current() || thread->IsSuspended()) << *thread;
112}
113
Mathieu Chartiere401d142015-04-22 13:56:20 -0700114InlineInfo StackVisitor::GetCurrentInlineInfo() const {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100115 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
116 uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_);
117 CodeInfo code_info = method_header->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000118 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Brazdilf677ebf2015-05-29 16:29:43 +0100119 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100120 DCHECK(stack_map.IsValid());
David Brazdilf677ebf2015-05-29 16:29:43 +0100121 return code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100122}
123
Mathieu Chartiere401d142015-04-22 13:56:20 -0700124ArtMethod* StackVisitor::GetMethod() const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100125 if (cur_shadow_frame_ != nullptr) {
126 return cur_shadow_frame_->GetMethod();
127 } else if (cur_quick_frame_ != nullptr) {
128 if (IsInInlinedFrame()) {
129 size_t depth_in_stack_map = current_inlining_depth_ - 1;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100130 InlineInfo inline_info = GetCurrentInlineInfo();
David Srbecky61b28a12016-02-25 21:55:03 +0000131 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
132 CodeInfoEncoding encoding = method_header->GetOptimizedCodeInfo().ExtractEncoding();
Mathieu Chartier45bf2502016-03-31 11:07:09 -0700133 DCHECK(walk_kind_ != StackWalkKind::kSkipInlinedFrames);
134 bool allow_resolve = walk_kind_ != StackWalkKind::kIncludeInlinedFramesNoResolve;
135 return allow_resolve
David Srbecky61b28a12016-02-25 21:55:03 +0000136 ? GetResolvedMethod<true>(*GetCurrentQuickFrame(),
137 inline_info,
138 encoding.inline_info_encoding,
139 depth_in_stack_map)
140 : GetResolvedMethod<false>(*GetCurrentQuickFrame(),
141 inline_info,
142 encoding.inline_info_encoding,
143 depth_in_stack_map);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100144 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700145 return *cur_quick_frame_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100146 }
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100147 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700148 return nullptr;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100149}
150
Dave Allisonb373e092014-02-20 16:06:36 -0800151uint32_t StackVisitor::GetDexPc(bool abort_on_failure) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700152 if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700153 return cur_shadow_frame_->GetDexPC();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700154 } else if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100155 if (IsInInlinedFrame()) {
156 size_t depth_in_stack_map = current_inlining_depth_ - 1;
David Srbecky61b28a12016-02-25 21:55:03 +0000157 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
158 CodeInfoEncoding encoding = method_header->GetOptimizedCodeInfo().ExtractEncoding();
159 return GetCurrentInlineInfo().GetDexPcAtDepth(encoding.inline_info_encoding,
160 depth_in_stack_map);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100161 } else if (cur_oat_quick_method_header_ == nullptr) {
162 return DexFile::kDexNoIndex;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100163 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100164 return cur_oat_quick_method_header_->ToDexPc(
165 GetMethod(), cur_quick_frame_pc_, abort_on_failure);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100166 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700167 } else {
168 return 0;
169 }
170}
171
Mathieu Chartiere401d142015-04-22 13:56:20 -0700172extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700173 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertza836bc92014-11-25 16:30:53 +0100174
Ian Rogers62d6c772013-02-27 08:32:07 -0800175mirror::Object* StackVisitor::GetThisObject() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700176 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), sizeof(void*));
177 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800178 if (m->IsStatic()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100179 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800180 } else if (m->IsNative()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100181 if (cur_quick_frame_ != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700182 HandleScope* hs = reinterpret_cast<HandleScope*>(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100183 reinterpret_cast<char*>(cur_quick_frame_) + sizeof(ArtMethod*));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700184 return hs->GetReference(0);
Ian Rogers62d6c772013-02-27 08:32:07 -0800185 } else {
186 return cur_shadow_frame_->GetVRegReference(0);
187 }
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000188 } else if (m->IsProxyMethod()) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100189 if (cur_quick_frame_ != nullptr) {
190 return artQuickGetProxyThisObject(cur_quick_frame_);
191 } else {
192 return cur_shadow_frame_->GetVRegReference(0);
193 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800194 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700195 const DexFile::CodeItem* code_item = m->GetCodeItem();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100196 if (code_item == nullptr) {
Ian Rogerse0dcd462014-03-08 15:21:04 -0800197 UNIMPLEMENTED(ERROR) << "Failed to determine this object of abstract or proxy method: "
Ian Rogers62d6c772013-02-27 08:32:07 -0800198 << PrettyMethod(m);
Ian Rogerse0dcd462014-03-08 15:21:04 -0800199 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800200 } else {
201 uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000202 uint32_t value = 0;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700203 bool success = GetVReg(m, reg, kReferenceVReg, &value);
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000204 // We currently always guarantee the `this` object is live throughout the method.
205 CHECK(success) << "Failed to read the this object in " << PrettyMethod(m);
206 return reinterpret_cast<mirror::Object*>(value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800207 }
208 }
209}
210
Ian Rogers0c7abda2012-09-19 13:33:42 -0700211size_t StackVisitor::GetNativePcOffset() const {
212 DCHECK(!IsShadowFrame());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100213 return GetCurrentOatQuickMethodHeader()->NativeQuickPcOffset(cur_quick_frame_pc_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700214}
215
Mingyao Yang99170c62015-07-06 11:10:37 -0700216bool StackVisitor::GetVRegFromDebuggerShadowFrame(uint16_t vreg,
217 VRegKind kind,
218 uint32_t* val) const {
219 size_t frame_id = const_cast<StackVisitor*>(this)->GetFrameId();
220 ShadowFrame* shadow_frame = thread_->FindDebuggerShadowFrame(frame_id);
221 if (shadow_frame != nullptr) {
222 bool* updated_vreg_flags = thread_->GetUpdatedVRegFlags(frame_id);
223 DCHECK(updated_vreg_flags != nullptr);
224 if (updated_vreg_flags[vreg]) {
225 // Value is set by the debugger.
226 if (kind == kReferenceVReg) {
227 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
228 shadow_frame->GetVRegReference(vreg)));
229 } else {
230 *val = shadow_frame->GetVReg(vreg);
231 }
232 return true;
233 }
234 }
235 // No value is set by the debugger.
236 return false;
237}
238
Mathieu Chartiere401d142015-04-22 13:56:20 -0700239bool StackVisitor::GetVReg(ArtMethod* m, uint16_t vreg, VRegKind kind, uint32_t* val) const {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200240 if (cur_quick_frame_ != nullptr) {
241 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800242 DCHECK(m == GetMethod());
Mingyao Yang99170c62015-07-06 11:10:37 -0700243 // Check if there is value set by the debugger.
244 if (GetVRegFromDebuggerShadowFrame(vreg, kind, val)) {
245 return true;
246 }
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100247 DCHECK(cur_oat_quick_method_header_->IsOptimized());
248 return GetVRegFromOptimizedCode(m, vreg, kind, val);
Ian Rogers0399dde2012-06-06 17:09:28 -0700249 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100250 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertz09687442015-11-17 10:35:39 +0100251 if (kind == kReferenceVReg) {
252 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
253 cur_shadow_frame_->GetVRegReference(vreg)));
254 } else {
255 *val = cur_shadow_frame_->GetVReg(vreg);
256 }
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200257 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700258 }
259}
260
Mathieu Chartiere401d142015-04-22 13:56:20 -0700261bool StackVisitor::GetVRegFromOptimizedCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100262 uint32_t* val) const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100263 DCHECK_EQ(m, GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100264 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700265 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100266 // its instructions?
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000267 uint16_t number_of_dex_registers = code_item->registers_size_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100268 DCHECK_LT(vreg, code_item->registers_size_);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100269 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
270 CodeInfo code_info = method_header->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000271 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100272
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100273 uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100274 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100275 DCHECK(stack_map.IsValid());
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100276 size_t depth_in_stack_map = current_inlining_depth_ - 1;
277
278 DexRegisterMap dex_register_map = IsInInlinedFrame()
David Brazdilf677ebf2015-05-29 16:29:43 +0100279 ? code_info.GetDexRegisterMapAtDepth(depth_in_stack_map,
280 code_info.GetInlineInfoOf(stack_map, encoding),
281 encoding,
282 number_of_dex_registers)
283 : code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100284
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000285 if (!dex_register_map.IsValid()) {
286 return false;
287 }
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000288 DexRegisterLocation::Kind location_kind =
David Brazdilf677ebf2015-05-29 16:29:43 +0100289 dex_register_map.GetLocationKind(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100290 switch (location_kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000291 case DexRegisterLocation::Kind::kInStack: {
David Brazdilf677ebf2015-05-29 16:29:43 +0100292 const int32_t offset = dex_register_map.GetStackOffsetInBytes(vreg,
293 number_of_dex_registers,
294 code_info,
295 encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100296 const uint8_t* addr = reinterpret_cast<const uint8_t*>(cur_quick_frame_) + offset;
297 *val = *reinterpret_cast<const uint32_t*>(addr);
298 return true;
299 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000300 case DexRegisterLocation::Kind::kInRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100301 case DexRegisterLocation::Kind::kInRegisterHigh:
302 case DexRegisterLocation::Kind::kInFpuRegister:
303 case DexRegisterLocation::Kind::kInFpuRegisterHigh: {
David Brazdilf677ebf2015-05-29 16:29:43 +0100304 uint32_t reg =
305 dex_register_map.GetMachineRegister(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100306 return GetRegisterIfAccessible(reg, kind, val);
307 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000308 case DexRegisterLocation::Kind::kConstant:
David Brazdilf677ebf2015-05-29 16:29:43 +0100309 *val = dex_register_map.GetConstant(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100310 return true;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000311 case DexRegisterLocation::Kind::kNone:
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100312 return false;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000313 default:
314 LOG(FATAL)
David Srbecky7dc11782016-02-25 13:23:56 +0000315 << "Unexpected location kind "
316 << dex_register_map.GetLocationInternalKind(vreg,
317 number_of_dex_registers,
318 code_info,
319 encoding);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000320 UNREACHABLE();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100321 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100322}
323
324bool StackVisitor::GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const {
325 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
David Brazdil77a48ae2015-09-15 12:34:04 +0000326
327 // X86 float registers are 64-bit and the logic below does not apply.
328 DCHECK(!is_float || kRuntimeISA != InstructionSet::kX86);
329
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100330 if (!IsAccessibleRegister(reg, is_float)) {
331 return false;
332 }
333 uintptr_t ptr_val = GetRegister(reg, is_float);
334 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
335 if (target64) {
336 const bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
337 const bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
338 int64_t value_long = static_cast<int64_t>(ptr_val);
339 if (wide_lo) {
340 ptr_val = static_cast<uintptr_t>(Low32Bits(value_long));
341 } else if (wide_hi) {
342 ptr_val = static_cast<uintptr_t>(High32Bits(value_long));
343 }
344 }
345 *val = ptr_val;
346 return true;
347}
348
Mingyao Yang99170c62015-07-06 11:10:37 -0700349bool StackVisitor::GetVRegPairFromDebuggerShadowFrame(uint16_t vreg,
350 VRegKind kind_lo,
351 VRegKind kind_hi,
352 uint64_t* val) const {
353 uint32_t low_32bits;
354 uint32_t high_32bits;
355 bool success = GetVRegFromDebuggerShadowFrame(vreg, kind_lo, &low_32bits);
356 success &= GetVRegFromDebuggerShadowFrame(vreg + 1, kind_hi, &high_32bits);
357 if (success) {
358 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
359 }
360 return success;
361}
362
Mathieu Chartiere401d142015-04-22 13:56:20 -0700363bool StackVisitor::GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200364 VRegKind kind_hi, uint64_t* val) const {
365 if (kind_lo == kLongLoVReg) {
366 DCHECK_EQ(kind_hi, kLongHiVReg);
367 } else if (kind_lo == kDoubleLoVReg) {
368 DCHECK_EQ(kind_hi, kDoubleHiVReg);
369 } else {
370 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100371 UNREACHABLE();
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200372 }
Mingyao Yang99170c62015-07-06 11:10:37 -0700373 // Check if there is value set by the debugger.
374 if (GetVRegPairFromDebuggerShadowFrame(vreg, kind_lo, kind_hi, val)) {
375 return true;
376 }
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200377 if (cur_quick_frame_ != nullptr) {
378 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
379 DCHECK(m == GetMethod());
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100380 DCHECK(cur_oat_quick_method_header_->IsOptimized());
381 return GetVRegPairFromOptimizedCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200382 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100383 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200384 *val = cur_shadow_frame_->GetVRegLong(vreg);
385 return true;
386 }
387}
388
Mathieu Chartiere401d142015-04-22 13:56:20 -0700389bool StackVisitor::GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100390 VRegKind kind_lo, VRegKind kind_hi,
391 uint64_t* val) const {
392 uint32_t low_32bits;
393 uint32_t high_32bits;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700394 bool success = GetVRegFromOptimizedCode(m, vreg, kind_lo, &low_32bits);
395 success &= GetVRegFromOptimizedCode(m, vreg + 1, kind_hi, &high_32bits);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100396 if (success) {
397 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
398 }
399 return success;
400}
401
402bool StackVisitor::GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
403 VRegKind kind_lo, uint64_t* val) const {
404 const bool is_float = (kind_lo == kDoubleLoVReg);
405 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
406 return false;
407 }
408 uintptr_t ptr_val_lo = GetRegister(reg_lo, is_float);
409 uintptr_t ptr_val_hi = GetRegister(reg_hi, is_float);
410 bool target64 = Is64BitInstructionSet(kRuntimeISA);
411 if (target64) {
412 int64_t value_long_lo = static_cast<int64_t>(ptr_val_lo);
413 int64_t value_long_hi = static_cast<int64_t>(ptr_val_hi);
414 ptr_val_lo = static_cast<uintptr_t>(Low32Bits(value_long_lo));
415 ptr_val_hi = static_cast<uintptr_t>(High32Bits(value_long_hi));
416 }
417 *val = (static_cast<uint64_t>(ptr_val_hi) << 32) | static_cast<uint32_t>(ptr_val_lo);
418 return true;
419}
420
Mingyao Yang636b9252015-07-31 16:40:24 -0700421bool StackVisitor::SetVReg(ArtMethod* m,
422 uint16_t vreg,
423 uint32_t new_value,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800424 VRegKind kind) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700425 const DexFile::CodeItem* code_item = m->GetCodeItem();
426 if (code_item == nullptr) {
427 return false;
428 }
429 ShadowFrame* shadow_frame = GetCurrentShadowFrame();
430 if (shadow_frame == nullptr) {
431 // This is a compiled frame: we must prepare and update a shadow frame that will
432 // be executed by the interpreter after deoptimization of the stack.
433 const size_t frame_id = GetFrameId();
434 const uint16_t num_regs = code_item->registers_size_;
435 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
436 CHECK(shadow_frame != nullptr);
437 // Remember the vreg has been set for debugging and must not be overwritten by the
438 // original value during deoptimization of the stack.
439 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
440 }
441 if (kind == kReferenceVReg) {
442 shadow_frame->SetVRegReference(vreg, reinterpret_cast<mirror::Object*>(new_value));
443 } else {
444 shadow_frame->SetVReg(vreg, new_value);
445 }
446 return true;
447}
448
Mingyao Yang636b9252015-07-31 16:40:24 -0700449bool StackVisitor::SetVRegPair(ArtMethod* m,
450 uint16_t vreg,
451 uint64_t new_value,
452 VRegKind kind_lo,
453 VRegKind kind_hi) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700454 if (kind_lo == kLongLoVReg) {
455 DCHECK_EQ(kind_hi, kLongHiVReg);
456 } else if (kind_lo == kDoubleLoVReg) {
457 DCHECK_EQ(kind_hi, kDoubleHiVReg);
458 } else {
459 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
460 UNREACHABLE();
461 }
462 const DexFile::CodeItem* code_item = m->GetCodeItem();
463 if (code_item == nullptr) {
464 return false;
465 }
466 ShadowFrame* shadow_frame = GetCurrentShadowFrame();
467 if (shadow_frame == nullptr) {
468 // This is a compiled frame: we must prepare for deoptimization (see SetVRegFromDebugger).
469 const size_t frame_id = GetFrameId();
470 const uint16_t num_regs = code_item->registers_size_;
471 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
472 CHECK(shadow_frame != nullptr);
473 // Remember the vreg pair has been set for debugging and must not be overwritten by the
474 // original value during deoptimization of the stack.
475 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
476 thread_->GetUpdatedVRegFlags(frame_id)[vreg + 1] = true;
477 }
478 shadow_frame->SetVRegLong(vreg, new_value);
479 return true;
480}
481
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100482bool StackVisitor::IsAccessibleGPR(uint32_t reg) const {
483 DCHECK(context_ != nullptr);
484 return context_->IsAccessibleGPR(reg);
485}
486
Mathieu Chartier815873e2014-02-13 18:02:13 -0800487uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100488 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
489 DCHECK(context_ != nullptr);
Mathieu Chartier815873e2014-02-13 18:02:13 -0800490 return context_->GetGPRAddress(reg);
491}
492
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100493uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
494 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
495 DCHECK(context_ != nullptr);
496 return context_->GetGPR(reg);
Ian Rogers0399dde2012-06-06 17:09:28 -0700497}
498
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100499bool StackVisitor::IsAccessibleFPR(uint32_t reg) const {
500 DCHECK(context_ != nullptr);
501 return context_->IsAccessibleFPR(reg);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200502}
503
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100504uintptr_t StackVisitor::GetFPR(uint32_t reg) const {
505 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
506 DCHECK(context_ != nullptr);
507 return context_->GetFPR(reg);
508}
509
Ian Rogers0399dde2012-06-06 17:09:28 -0700510uintptr_t StackVisitor::GetReturnPc() const {
Ian Rogers13735952014-10-08 12:43:28 -0700511 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700512 DCHECK(sp != nullptr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100513 uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogers0399dde2012-06-06 17:09:28 -0700514 return *reinterpret_cast<uintptr_t*>(pc_addr);
515}
516
517void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
Ian Rogers13735952014-10-08 12:43:28 -0700518 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700519 CHECK(sp != nullptr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100520 uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogers0399dde2012-06-06 17:09:28 -0700521 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc;
522}
523
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100524size_t StackVisitor::ComputeNumFrames(Thread* thread, StackWalkKind walk_kind) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700525 struct NumFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100526 NumFramesVisitor(Thread* thread_in, StackWalkKind walk_kind_in)
527 : StackVisitor(thread_in, nullptr, walk_kind_in), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700528
Ian Rogers5cf98192014-05-29 21:31:50 -0700529 bool VisitFrame() OVERRIDE {
Ian Rogers0399dde2012-06-06 17:09:28 -0700530 frames++;
531 return true;
532 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700533
Ian Rogers0399dde2012-06-06 17:09:28 -0700534 size_t frames;
535 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100536 NumFramesVisitor visitor(thread, walk_kind);
Ian Rogers0399dde2012-06-06 17:09:28 -0700537 visitor.WalkStack(true);
538 return visitor.frames;
539}
540
Mathieu Chartiere401d142015-04-22 13:56:20 -0700541bool StackVisitor::GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700542 struct HasMoreFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100543 HasMoreFramesVisitor(Thread* thread,
544 StackWalkKind walk_kind,
545 size_t num_frames,
546 size_t frame_height)
547 : StackVisitor(thread, nullptr, walk_kind, num_frames),
548 frame_height_(frame_height),
549 found_frame_(false),
550 has_more_frames_(false),
551 next_method_(nullptr),
552 next_dex_pc_(0) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700553 }
554
Mathieu Chartier90443472015-07-16 20:32:27 -0700555 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700556 if (found_frame_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700557 ArtMethod* method = GetMethod();
Ian Rogers5cf98192014-05-29 21:31:50 -0700558 if (method != nullptr && !method->IsRuntimeMethod()) {
559 has_more_frames_ = true;
560 next_method_ = method;
561 next_dex_pc_ = GetDexPc();
562 return false; // End stack walk once next method is found.
563 }
564 } else if (GetFrameHeight() == frame_height_) {
565 found_frame_ = true;
566 }
567 return true;
568 }
569
570 size_t frame_height_;
571 bool found_frame_;
572 bool has_more_frames_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700573 ArtMethod* next_method_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700574 uint32_t next_dex_pc_;
575 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100576 HasMoreFramesVisitor visitor(thread_, walk_kind_, GetNumFrames(), GetFrameHeight());
Ian Rogers5cf98192014-05-29 21:31:50 -0700577 visitor.WalkStack(true);
578 *next_method = visitor.next_method_;
579 *next_dex_pc = visitor.next_dex_pc_;
580 return visitor.has_more_frames_;
581}
582
Ian Rogers7a22fa62013-01-23 12:16:16 -0800583void StackVisitor::DescribeStack(Thread* thread) {
Ian Rogers306057f2012-11-26 12:45:53 -0800584 struct DescribeStackVisitor : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800585 explicit DescribeStackVisitor(Thread* thread_in)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100586 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800587
Mathieu Chartier90443472015-07-16 20:32:27 -0700588 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers306057f2012-11-26 12:45:53 -0800589 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation();
590 return true;
591 }
592 };
Ian Rogers7a22fa62013-01-23 12:16:16 -0800593 DescribeStackVisitor visitor(thread);
Ian Rogers306057f2012-11-26 12:45:53 -0800594 visitor.WalkStack(true);
595}
596
Ian Rogers40e3bac2012-11-20 00:09:14 -0800597std::string StackVisitor::DescribeLocation() const {
598 std::string result("Visiting method '");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700599 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700600 if (m == nullptr) {
Ian Rogers306057f2012-11-26 12:45:53 -0800601 return "upcall";
602 }
603 result += PrettyMethod(m);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800604 result += StringPrintf("' at dex PC 0x%04x", GetDexPc());
Ian Rogers40e3bac2012-11-20 00:09:14 -0800605 if (!IsShadowFrame()) {
606 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc()));
607 }
608 return result;
609}
610
Ian Rogerse63db272014-07-15 15:36:11 -0700611static instrumentation::InstrumentationStackFrame& GetInstrumentationStackFrame(Thread* thread,
612 uint32_t depth) {
613 CHECK_LT(depth, thread->GetInstrumentationStack()->size());
614 return thread->GetInstrumentationStack()->at(depth);
Ian Rogers7a22fa62013-01-23 12:16:16 -0800615}
616
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100617static void AssertPcIsWithinQuickCode(ArtMethod* method, uintptr_t pc)
618 SHARED_REQUIRES(Locks::mutator_lock_) {
619 if (method->IsNative() || method->IsRuntimeMethod() || method->IsProxyMethod()) {
620 return;
621 }
622
623 if (pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) {
624 return;
625 }
626
627 const void* code = method->GetEntryPointFromQuickCompiledCode();
628 if (code == GetQuickInstrumentationEntryPoint()) {
629 return;
630 }
631
632 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
633 if (class_linker->IsQuickToInterpreterBridge(code) ||
634 class_linker->IsQuickResolutionStub(code)) {
635 return;
636 }
637
638 // If we are the JIT then we may have just compiled the method after the
639 // IsQuickToInterpreterBridge check.
640 jit::Jit* const jit = Runtime::Current()->GetJit();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100641 if (jit != nullptr && jit->GetCodeCache()->ContainsPc(code)) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100642 return;
643 }
644
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100645 uint32_t code_size = OatQuickMethodHeader::FromEntryPoint(code)->code_size_;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100646 uintptr_t code_start = reinterpret_cast<uintptr_t>(code);
647 CHECK(code_start <= pc && pc <= (code_start + code_size))
648 << PrettyMethod(method)
649 << " pc=" << std::hex << pc
Roland Levillain0d5a2812015-11-13 10:07:31 +0000650 << " code_start=" << code_start
651 << " code_size=" << code_size;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100652}
653
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700654void StackVisitor::SanityCheckFrame() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800655 if (kIsDebugBuild) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700656 ArtMethod* method = GetMethod();
657 auto* declaring_class = method->GetDeclaringClass();
658 // Runtime methods have null declaring class.
659 if (!method->IsRuntimeMethod()) {
660 CHECK(declaring_class != nullptr);
661 CHECK_EQ(declaring_class->GetClass(), declaring_class->GetClass()->GetClass())
662 << declaring_class;
663 } else {
664 CHECK(declaring_class == nullptr);
665 }
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700666 Runtime* const runtime = Runtime::Current();
667 LinearAlloc* const linear_alloc = runtime->GetLinearAlloc();
668 if (!linear_alloc->Contains(method)) {
669 // Check class linker linear allocs.
670 mirror::Class* klass = method->GetDeclaringClass();
671 LinearAlloc* const class_linear_alloc = (klass != nullptr)
Mathieu Chartier5b830502016-03-02 10:30:23 -0800672 ? runtime->GetClassLinker()->GetAllocatorForClassLoader(klass->GetClassLoader())
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700673 : linear_alloc;
674 if (!class_linear_alloc->Contains(method)) {
675 // Check image space.
676 bool in_image = false;
677 for (auto& space : runtime->GetHeap()->GetContinuousSpaces()) {
678 if (space->IsImageSpace()) {
679 auto* image_space = space->AsImageSpace();
680 const auto& header = image_space->GetImageHeader();
681 const auto* methods = &header.GetMethodsSection();
682 if (methods->Contains(reinterpret_cast<const uint8_t*>(method) - image_space->Begin())) {
683 in_image = true;
684 break;
685 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700686 }
687 }
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700688 CHECK(in_image) << PrettyMethod(method) << " not in linear alloc or image";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700689 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700690 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800691 if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100692 AssertPcIsWithinQuickCode(method, cur_quick_frame_pc_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800693 // Frame sanity.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100694 size_t frame_size = GetCurrentQuickFrameInfo().FrameSizeInBytes();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800695 CHECK_NE(frame_size, 0u);
Andreas Gampe5b417b92014-03-10 14:18:35 -0700696 // A rough guess at an upper size we expect to see for a frame.
697 // 256 registers
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700698 // 2 words HandleScope overhead
Andreas Gampe5b417b92014-03-10 14:18:35 -0700699 // 3+3 register spills
700 // TODO: this seems architecture specific for the case of JNI frames.
Brian Carlstromed08bd42014-03-19 18:34:17 -0700701 // TODO: 083-compiler-regressions ManyFloatArgs shows this estimate is wrong.
702 // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word);
703 const size_t kMaxExpectedFrameSize = 2 * KB;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100704 CHECK_LE(frame_size, kMaxExpectedFrameSize) << PrettyMethod(method);
705 size_t return_pc_offset = GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800706 CHECK_LT(return_pc_offset, frame_size);
707 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700708 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700709}
710
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100711// Counts the number of references in the parameter list of the corresponding method.
712// Note: Thus does _not_ include "this" for non-static methods.
713static uint32_t GetNumberOfReferenceArgsWithoutReceiver(ArtMethod* method)
714 SHARED_REQUIRES(Locks::mutator_lock_) {
715 uint32_t shorty_len;
716 const char* shorty = method->GetShorty(&shorty_len);
717 uint32_t refs = 0;
718 for (uint32_t i = 1; i < shorty_len ; ++i) {
719 if (shorty[i] == 'L') {
720 refs++;
721 }
722 }
723 return refs;
724}
725
726QuickMethodFrameInfo StackVisitor::GetCurrentQuickFrameInfo() const {
727 if (cur_oat_quick_method_header_ != nullptr) {
728 return cur_oat_quick_method_header_->GetFrameInfo();
729 }
730
731 ArtMethod* method = GetMethod();
732 Runtime* runtime = Runtime::Current();
733
734 if (method->IsAbstract()) {
735 return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs);
736 }
737
738 // This goes before IsProxyMethod since runtime methods have a null declaring class.
739 if (method->IsRuntimeMethod()) {
740 return runtime->GetRuntimeMethodFrameInfo(method);
741 }
742
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100743 if (method->IsProxyMethod()) {
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000744 // There is only one direct method of a proxy class: the constructor. A direct method is
745 // cloned from the original java.lang.reflect.Proxy and is executed as usual quick
746 // compiled method without any stubs. Therefore the method must have a OatQuickMethodHeader.
747 DCHECK(!method->IsDirect() && !method->IsConstructor())
748 << "Constructors of proxy classes must have a OatQuickMethodHeader";
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000749 return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100750 }
751
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000752 // The only remaining case is if the method is native and uses the generic JNI stub.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100753 DCHECK(method->IsNative());
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000754 ClassLinker* class_linker = runtime->GetClassLinker();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100755 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(method, sizeof(void*));
756 DCHECK(class_linker->IsQuickGenericJniStub(entry_point)) << PrettyMethod(method);
757 // Generic JNI frame.
758 uint32_t handle_refs = GetNumberOfReferenceArgsWithoutReceiver(method) + 1;
759 size_t scope_size = HandleScope::SizeOf(handle_refs);
760 QuickMethodFrameInfo callee_info = runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs);
761
762 // Callee saves + handle scope + method ref + alignment
763 // Note: -sizeof(void*) since callee-save frame stores a whole method pointer.
764 size_t frame_size = RoundUp(
765 callee_info.FrameSizeInBytes() - sizeof(void*) + sizeof(ArtMethod*) + scope_size,
766 kStackAlignment);
767 return QuickMethodFrameInfo(frame_size, callee_info.CoreSpillMask(), callee_info.FpSpillMask());
768}
769
Ian Rogers0399dde2012-06-06 17:09:28 -0700770void StackVisitor::WalkStack(bool include_transitions) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800771 DCHECK(thread_ == Thread::Current() || thread_->IsSuspended());
Ian Rogers62d6c772013-02-27 08:32:07 -0800772 CHECK_EQ(cur_depth_, 0U);
773 bool exit_stubs_installed = Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled();
jeffhao725a9572012-11-13 18:20:12 -0800774 uint32_t instrumentation_stack_depth = 0;
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000775 size_t inlined_frames_count = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700776
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700777 for (const ManagedStack* current_fragment = thread_->GetManagedStack();
778 current_fragment != nullptr; current_fragment = current_fragment->GetLink()) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700779 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
780 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700781 cur_quick_frame_pc_ = 0;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100782 cur_oat_quick_method_header_ = nullptr;
Dave Allisonf9439142014-03-27 15:10:22 -0700783
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700784 if (cur_quick_frame_ != nullptr) { // Handle quick stack frames.
Ian Rogers0399dde2012-06-06 17:09:28 -0700785 // Can't be both a shadow and a quick fragment.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700786 DCHECK(current_fragment->GetTopShadowFrame() == nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700787 ArtMethod* method = *cur_quick_frame_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700788 while (method != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100789 cur_oat_quick_method_header_ = method->GetOatQuickMethodHeader(cur_quick_frame_pc_);
Dave Allison5cd33752014-04-15 15:57:58 -0700790 SanityCheckFrame();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100791
Mathieu Chartier45bf2502016-03-31 11:07:09 -0700792 if ((walk_kind_ == StackWalkKind::kIncludeInlinedFrames ||
793 walk_kind_ == StackWalkKind::kIncludeInlinedFramesNoResolve)
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100794 && (cur_oat_quick_method_header_ != nullptr)
795 && cur_oat_quick_method_header_->IsOptimized()) {
796 CodeInfo code_info = cur_oat_quick_method_header_->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000797 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100798 uint32_t native_pc_offset =
799 cur_oat_quick_method_header_->NativeQuickPcOffset(cur_quick_frame_pc_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100800 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
David Srbecky09ed0982016-02-12 21:58:43 +0000801 if (stack_map.IsValid() && stack_map.HasInlineInfo(encoding.stack_map_encoding)) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100802 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100803 DCHECK_EQ(current_inlining_depth_, 0u);
David Srbecky61b28a12016-02-25 21:55:03 +0000804 for (current_inlining_depth_ = inline_info.GetDepth(encoding.inline_info_encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100805 current_inlining_depth_ != 0;
806 --current_inlining_depth_) {
807 bool should_continue = VisitFrame();
808 if (UNLIKELY(!should_continue)) {
809 return;
810 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100811 cur_depth_++;
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000812 inlined_frames_count++;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100813 }
814 }
815 }
816
Dave Allison5cd33752014-04-15 15:57:58 -0700817 bool should_continue = VisitFrame();
818 if (UNLIKELY(!should_continue)) {
819 return;
Ian Rogers0399dde2012-06-06 17:09:28 -0700820 }
Dave Allison5cd33752014-04-15 15:57:58 -0700821
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100822 QuickMethodFrameInfo frame_info = GetCurrentQuickFrameInfo();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700823 if (context_ != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100824 context_->FillCalleeSaves(reinterpret_cast<uint8_t*>(cur_quick_frame_), frame_info);
Ian Rogers0399dde2012-06-06 17:09:28 -0700825 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700826 // Compute PC for next stack frame from return PC.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100827 size_t frame_size = frame_info.FrameSizeInBytes();
828 size_t return_pc_offset = frame_size - sizeof(void*);
Ian Rogers13735952014-10-08 12:43:28 -0700829 uint8_t* return_pc_addr = reinterpret_cast<uint8_t*>(cur_quick_frame_) + return_pc_offset;
Ian Rogers0399dde2012-06-06 17:09:28 -0700830 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100831
Ian Rogers62d6c772013-02-27 08:32:07 -0800832 if (UNLIKELY(exit_stubs_installed)) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700833 // While profiling, the return pc is restored from the side stack, except when walking
834 // the stack for an exception where the side stack will be unwound in VisitFrame.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700835 if (reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc) {
Sebastien Hertz74e256b2013-10-04 10:40:37 +0200836 const instrumentation::InstrumentationStackFrame& instrumentation_frame =
Ian Rogerse63db272014-07-15 15:36:11 -0700837 GetInstrumentationStackFrame(thread_, instrumentation_stack_depth);
jeffhao725a9572012-11-13 18:20:12 -0800838 instrumentation_stack_depth++;
Jeff Haofb2802d2013-07-24 13:53:05 -0700839 if (GetMethod() == Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveAll)) {
840 // Skip runtime save all callee frames which are used to deliver exceptions.
841 } else if (instrumentation_frame.interpreter_entry_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700842 ArtMethod* callee = Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs);
Jeff Haofb2802d2013-07-24 13:53:05 -0700843 CHECK_EQ(GetMethod(), callee) << "Expected: " << PrettyMethod(callee) << " Found: "
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100844 << PrettyMethod(GetMethod());
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000845 } else {
846 CHECK_EQ(instrumentation_frame.method_, GetMethod())
847 << "Expected: " << PrettyMethod(instrumentation_frame.method_)
848 << " Found: " << PrettyMethod(GetMethod());
Ian Rogers62d6c772013-02-27 08:32:07 -0800849 }
850 if (num_frames_ != 0) {
851 // Check agreement of frame Ids only if num_frames_ is computed to avoid infinite
852 // recursion.
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000853 size_t frame_id = instrumentation::Instrumentation::ComputeFrameId(
854 thread_,
855 cur_depth_,
856 inlined_frames_count);
857 CHECK_EQ(instrumentation_frame.frame_id_, frame_id);
Ian Rogers62d6c772013-02-27 08:32:07 -0800858 }
jeffhao725a9572012-11-13 18:20:12 -0800859 return_pc = instrumentation_frame.return_pc_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700860 }
861 }
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100862
Ian Rogers0399dde2012-06-06 17:09:28 -0700863 cur_quick_frame_pc_ = return_pc;
Ian Rogers13735952014-10-08 12:43:28 -0700864 uint8_t* next_frame = reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700865 cur_quick_frame_ = reinterpret_cast<ArtMethod**>(next_frame);
866
867 if (kDebugStackWalk) {
868 LOG(INFO) << PrettyMethod(method) << "@" << method << " size=" << frame_size
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100869 << std::boolalpha
870 << " optimized=" << (cur_oat_quick_method_header_ != nullptr &&
871 cur_oat_quick_method_header_->IsOptimized())
Mathieu Chartiere401d142015-04-22 13:56:20 -0700872 << " native=" << method->IsNative()
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100873 << std::noboolalpha
Mathieu Chartiere401d142015-04-22 13:56:20 -0700874 << " entrypoints=" << method->GetEntryPointFromQuickCompiledCode()
875 << "," << method->GetEntryPointFromJni()
Mathieu Chartiere401d142015-04-22 13:56:20 -0700876 << " next=" << *cur_quick_frame_;
877 }
878
Ian Rogers0399dde2012-06-06 17:09:28 -0700879 cur_depth_++;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700880 method = *cur_quick_frame_;
jeffhao6641ea12013-01-02 18:13:42 -0800881 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700882 } else if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700883 do {
884 SanityCheckFrame();
885 bool should_continue = VisitFrame();
886 if (UNLIKELY(!should_continue)) {
887 return;
888 }
889 cur_depth_++;
890 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700891 } while (cur_shadow_frame_ != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700892 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700893 if (include_transitions) {
894 bool should_continue = VisitFrame();
895 if (!should_continue) {
896 return;
897 }
898 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800899 cur_depth_++;
900 }
901 if (num_frames_ != 0) {
902 CHECK_EQ(cur_depth_, num_frames_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700903 }
904}
905
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800906void JavaFrameRootInfo::Describe(std::ostream& os) const {
907 const StackVisitor* visitor = stack_visitor_;
908 CHECK(visitor != nullptr);
909 os << "Type=" << GetType() << " thread_id=" << GetThreadId() << " location=" <<
910 visitor->DescribeLocation() << " vreg=" << vreg_;
911}
912
Mathieu Chartiere401d142015-04-22 13:56:20 -0700913int StackVisitor::GetVRegOffsetFromQuickCode(const DexFile::CodeItem* code_item,
914 uint32_t core_spills, uint32_t fp_spills,
915 size_t frame_size, int reg, InstructionSet isa) {
916 size_t pointer_size = InstructionSetPointerSize(isa);
917 if (kIsDebugBuild) {
918 auto* runtime = Runtime::Current();
919 if (runtime != nullptr) {
920 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
921 }
922 }
Roland Levillain14d90572015-07-16 10:52:26 +0100923 DCHECK_ALIGNED(frame_size, kStackAlignment);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700924 DCHECK_NE(reg, -1);
925 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
926 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
927 + sizeof(uint32_t); // Filler.
928 int num_regs = code_item->registers_size_ - code_item->ins_size_;
929 int temp_threshold = code_item->registers_size_;
930 const int max_num_special_temps = 1;
931 if (reg == temp_threshold) {
932 // The current method pointer corresponds to special location on stack.
933 return 0;
934 } else if (reg >= temp_threshold + max_num_special_temps) {
935 /*
936 * Special temporaries may have custom locations and the logic above deals with that.
937 * However, non-special temporaries are placed relative to the outs.
938 */
939 int temps_start = code_item->outs_size_ * sizeof(uint32_t) + pointer_size /* art method */;
940 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
941 return temps_start + relative_offset;
942 } else if (reg < num_regs) {
943 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
944 return locals_start + (reg * sizeof(uint32_t));
945 } else {
946 // Handle ins.
947 return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + pointer_size /* art method */;
948 }
949}
950
Andreas Gampe03ec9302015-08-27 17:41:47 -0700951void LockCountData::AddMonitorInternal(Thread* self, mirror::Object* obj) {
952 if (obj == nullptr) {
953 return;
954 }
955
956 // If there's an error during enter, we won't have locked the monitor. So check there's no
957 // exception.
958 if (self->IsExceptionPending()) {
959 return;
960 }
961
962 if (monitors_ == nullptr) {
963 monitors_.reset(new std::vector<mirror::Object*>());
964 }
965 monitors_->push_back(obj);
966}
967
968void LockCountData::RemoveMonitorInternal(Thread* self, const mirror::Object* obj) {
969 if (obj == nullptr) {
970 return;
971 }
972 bool found_object = false;
973 if (monitors_ != nullptr) {
974 // We need to remove one pointer to ref, as duplicates are used for counting recursive locks.
975 // We arbitrarily choose the first one.
976 auto it = std::find(monitors_->begin(), monitors_->end(), obj);
977 if (it != monitors_->end()) {
978 monitors_->erase(it);
979 found_object = true;
980 }
981 }
982 if (!found_object) {
983 // The object wasn't found. Time for an IllegalMonitorStateException.
984 // The order here isn't fully clear. Assume that any other pending exception is swallowed.
985 // TODO: Maybe make already pending exception a suppressed exception.
986 self->ClearException();
987 self->ThrowNewExceptionF("Ljava/lang/IllegalMonitorStateException;",
988 "did not lock monitor on object of type '%s' before unlocking",
989 PrettyTypeOf(const_cast<mirror::Object*>(obj)).c_str());
990 }
991}
992
993// Helper to unlock a monitor. Must be NO_THREAD_SAFETY_ANALYSIS, as we can't statically show
994// that the object was locked.
995void MonitorExitHelper(Thread* self, mirror::Object* obj) NO_THREAD_SAFETY_ANALYSIS {
996 DCHECK(self != nullptr);
997 DCHECK(obj != nullptr);
998 obj->MonitorExit(self);
999}
1000
1001bool LockCountData::CheckAllMonitorsReleasedInternal(Thread* self) {
1002 DCHECK(self != nullptr);
1003 if (monitors_ != nullptr) {
1004 if (!monitors_->empty()) {
1005 // There may be an exception pending, if the method is terminating abruptly. Clear it.
1006 // TODO: Should we add this as a suppressed exception?
1007 self->ClearException();
1008
1009 // OK, there are monitors that are still locked. To enforce structured locking (and avoid
1010 // deadlocks) we unlock all of them before we raise the IllegalMonitorState exception.
1011 for (mirror::Object* obj : *monitors_) {
1012 MonitorExitHelper(self, obj);
1013 // If this raised an exception, ignore. TODO: Should we add this as suppressed
1014 // exceptions?
1015 if (self->IsExceptionPending()) {
1016 self->ClearException();
1017 }
1018 }
1019 // Raise an exception, just give the first object as the sample.
1020 mirror::Object* first = (*monitors_)[0];
1021 self->ThrowNewExceptionF("Ljava/lang/IllegalMonitorStateException;",
1022 "did not unlock monitor on object of type '%s'",
1023 PrettyTypeOf(first).c_str());
1024
1025 // To make sure this path is not triggered again, clean out the monitors.
1026 monitors_->clear();
1027
1028 return false;
1029 }
1030 }
1031 return true;
1032}
1033
Elliott Hughes68e76522011-10-05 13:22:16 -07001034} // namespace art