blob: 5aeca98a88cc29b0db60be79e70f2f9220c2ab32 [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 Chartier50030ef2015-05-08 14:19:26 -070024#include "gc_map.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070025#include "gc/space/image_space.h"
26#include "gc/space/space-inl.h"
27#include "linear_alloc.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070028#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "mirror/object-inl.h"
30#include "mirror/object_array-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010031#include "quick/quick_method_frame_info.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070032#include "runtime.h"
Dave Allisonf9439142014-03-27 15:10:22 -070033#include "thread.h"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070034#include "thread_list.h"
Mathieu Chartier4e305412014-02-19 10:54:44 -080035#include "verify_object-inl.h"
Ian Rogers1809a722013-08-09 22:05:32 -070036#include "vmap_table.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070037
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080038namespace art {
39
Mathieu Chartiere401d142015-04-22 13:56:20 -070040static constexpr bool kDebugStackWalk = false;
41
Ian Rogers62d6c772013-02-27 08:32:07 -080042mirror::Object* ShadowFrame::GetThisObject() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -070043 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -080044 if (m->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070045 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -080046 } else if (m->IsNative()) {
47 return GetVRegReference(0);
48 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070049 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070050 CHECK(code_item != nullptr) << PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -080051 uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
52 return GetVRegReference(reg);
53 }
54}
55
Jeff Haoe701f482013-05-24 11:50:49 -070056mirror::Object* ShadowFrame::GetThisObject(uint16_t num_ins) const {
Mathieu Chartiere401d142015-04-22 13:56:20 -070057 ArtMethod* m = GetMethod();
Jeff Haoe701f482013-05-24 11:50:49 -070058 if (m->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070059 return nullptr;
Jeff Haoe701f482013-05-24 11:50:49 -070060 } else {
Jeff Hao8d448852013-06-03 17:26:19 -070061 return GetVRegReference(NumberOfVRegs() - num_ins);
Jeff Haoe701f482013-05-24 11:50:49 -070062 }
63}
64
TDYa127ce4cc0d2012-11-18 16:59:53 -080065size_t ManagedStack::NumJniShadowFrameReferences() const {
Ian Rogers0399dde2012-06-06 17:09:28 -070066 size_t count = 0;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070067 for (const ManagedStack* current_fragment = this; current_fragment != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070068 current_fragment = current_fragment->GetLink()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070069 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070070 current_frame = current_frame->GetLink()) {
TDYa127ce4cc0d2012-11-18 16:59:53 -080071 if (current_frame->GetMethod()->IsNative()) {
72 // The JNI ShadowFrame only contains references. (For indirect reference.)
73 count += current_frame->NumberOfVRegs();
74 }
Ian Rogers0399dde2012-06-06 17:09:28 -070075 }
76 }
77 return count;
78}
79
Ian Rogersef7d42f2014-01-06 12:55:46 -080080bool ManagedStack::ShadowFramesContain(StackReference<mirror::Object>* shadow_frame_entry) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070081 for (const ManagedStack* current_fragment = this; current_fragment != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070082 current_fragment = current_fragment->GetLink()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070083 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070084 current_frame = current_frame->GetLink()) {
85 if (current_frame->Contains(shadow_frame_entry)) {
86 return true;
87 }
88 }
89 }
90 return false;
91}
92
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010093StackVisitor::StackVisitor(Thread* thread, Context* context, StackWalkKind walk_kind)
94 : StackVisitor(thread, context, walk_kind, 0) {}
Ian Rogers7a22fa62013-01-23 12:16:16 -080095
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010096StackVisitor::StackVisitor(Thread* thread,
97 Context* context,
98 StackWalkKind walk_kind,
99 size_t num_frames)
100 : thread_(thread),
101 walk_kind_(walk_kind),
102 cur_shadow_frame_(nullptr),
103 cur_quick_frame_(nullptr),
104 cur_quick_frame_pc_(0),
105 num_frames_(num_frames),
106 cur_depth_(0),
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100107 current_inlining_depth_(0),
Ian Rogers5cf98192014-05-29 21:31:50 -0700108 context_(context) {
109 DCHECK(thread == Thread::Current() || thread->IsSuspended()) << *thread;
110}
111
Mathieu Chartiere401d142015-04-22 13:56:20 -0700112InlineInfo StackVisitor::GetCurrentInlineInfo() const {
113 ArtMethod* outer_method = *GetCurrentQuickFrame();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100114 uint32_t native_pc_offset = outer_method->NativeQuickPcOffset(cur_quick_frame_pc_);
115 CodeInfo code_info = outer_method->GetOptimizedCodeInfo();
David Brazdilf677ebf2015-05-29 16:29:43 +0100116 StackMapEncoding encoding = code_info.ExtractEncoding();
117 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100118 DCHECK(stack_map.IsValid());
David Brazdilf677ebf2015-05-29 16:29:43 +0100119 return code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100120}
121
Mathieu Chartiere401d142015-04-22 13:56:20 -0700122ArtMethod* StackVisitor::GetMethod() const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100123 if (cur_shadow_frame_ != nullptr) {
124 return cur_shadow_frame_->GetMethod();
125 } else if (cur_quick_frame_ != nullptr) {
126 if (IsInInlinedFrame()) {
127 size_t depth_in_stack_map = current_inlining_depth_ - 1;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100128 InlineInfo inline_info = GetCurrentInlineInfo();
129 uint32_t method_index = inline_info.GetMethodIndexAtDepth(depth_in_stack_map);
130 InvokeType invoke_type =
131 static_cast<InvokeType>(inline_info.GetInvokeTypeAtDepth(depth_in_stack_map));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700132 return GetResolvedMethod(*GetCurrentQuickFrame(), method_index, invoke_type);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100133 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700134 return *cur_quick_frame_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100135 }
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100136 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700137 return nullptr;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100138}
139
Dave Allisonb373e092014-02-20 16:06:36 -0800140uint32_t StackVisitor::GetDexPc(bool abort_on_failure) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700141 if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700142 return cur_shadow_frame_->GetDexPC();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700143 } else if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100144 if (IsInInlinedFrame()) {
145 size_t depth_in_stack_map = current_inlining_depth_ - 1;
146 return GetCurrentInlineInfo().GetDexPcAtDepth(depth_in_stack_map);
147 } else {
148 return GetMethod()->ToDexPc(cur_quick_frame_pc_, abort_on_failure);
149 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700150 } else {
151 return 0;
152 }
153}
154
Mathieu Chartiere401d142015-04-22 13:56:20 -0700155extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Sebastien Hertza836bc92014-11-25 16:30:53 +0100156 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
157
Ian Rogers62d6c772013-02-27 08:32:07 -0800158mirror::Object* StackVisitor::GetThisObject() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700159 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), sizeof(void*));
160 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800161 if (m->IsStatic()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100162 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800163 } else if (m->IsNative()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100164 if (cur_quick_frame_ != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700165 HandleScope* hs = reinterpret_cast<HandleScope*>(
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700166 reinterpret_cast<char*>(cur_quick_frame_) + m->GetHandleScopeOffset().SizeValue());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700167 return hs->GetReference(0);
Ian Rogers62d6c772013-02-27 08:32:07 -0800168 } else {
169 return cur_shadow_frame_->GetVRegReference(0);
170 }
Sebastien Hertza836bc92014-11-25 16:30:53 +0100171 } else if (m->IsProxyMethod()) {
172 if (cur_quick_frame_ != nullptr) {
173 return artQuickGetProxyThisObject(cur_quick_frame_);
174 } else {
175 return cur_shadow_frame_->GetVRegReference(0);
176 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800177 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700178 const DexFile::CodeItem* code_item = m->GetCodeItem();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100179 if (code_item == nullptr) {
Ian Rogerse0dcd462014-03-08 15:21:04 -0800180 UNIMPLEMENTED(ERROR) << "Failed to determine this object of abstract or proxy method: "
Ian Rogers62d6c772013-02-27 08:32:07 -0800181 << PrettyMethod(m);
Ian Rogerse0dcd462014-03-08 15:21:04 -0800182 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800183 } else {
184 uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000185 uint32_t value = 0;
186 bool success = GetVReg(m, reg, kReferenceVReg, &value);
187 // We currently always guarantee the `this` object is live throughout the method.
188 CHECK(success) << "Failed to read the this object in " << PrettyMethod(m);
189 return reinterpret_cast<mirror::Object*>(value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800190 }
191 }
192}
193
Ian Rogers0c7abda2012-09-19 13:33:42 -0700194size_t StackVisitor::GetNativePcOffset() const {
195 DCHECK(!IsShadowFrame());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700196 return GetMethod()->NativeQuickPcOffset(cur_quick_frame_pc_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700197}
198
Mathieu Chartiere401d142015-04-22 13:56:20 -0700199bool StackVisitor::IsReferenceVReg(ArtMethod* m, uint16_t vreg) {
Mathieu Chartier50030ef2015-05-08 14:19:26 -0700200 // Process register map (which native and runtime methods don't have)
201 if (m->IsNative() || m->IsRuntimeMethod() || m->IsProxyMethod()) {
202 return false;
203 }
204 if (m->IsOptimized(sizeof(void*))) {
205 return true; // TODO: Implement.
206 }
207 const uint8_t* native_gc_map = m->GetNativeGcMap(sizeof(void*));
208 CHECK(native_gc_map != nullptr) << PrettyMethod(m);
209 const DexFile::CodeItem* code_item = m->GetCodeItem();
210 // Can't be null or how would we compile its instructions?
211 DCHECK(code_item != nullptr) << PrettyMethod(m);
212 NativePcOffsetToReferenceMap map(native_gc_map);
213 size_t num_regs = std::min(map.RegWidth() * 8, static_cast<size_t>(code_item->registers_size_));
214 const uint8_t* reg_bitmap = nullptr;
215 if (num_regs > 0) {
216 Runtime* runtime = Runtime::Current();
217 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(m, sizeof(void*));
218 uintptr_t native_pc_offset = m->NativeQuickPcOffset(GetCurrentQuickFramePc(), entry_point);
219 reg_bitmap = map.FindBitMap(native_pc_offset);
220 DCHECK(reg_bitmap != nullptr);
221 }
222 // Does this register hold a reference?
223 return vreg < num_regs && TestBitmap(vreg, reg_bitmap);
224}
225
Mathieu Chartiere401d142015-04-22 13:56:20 -0700226bool StackVisitor::GetVReg(ArtMethod* m, uint16_t vreg, VRegKind kind, uint32_t* val) const {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200227 if (cur_quick_frame_ != nullptr) {
228 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800229 DCHECK(m == GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100230 if (m->IsOptimized(sizeof(void*))) {
231 return GetVRegFromOptimizedCode(m, vreg, kind, val);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700232 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100233 return GetVRegFromQuickCode(m, vreg, kind, val);
Ian Rogers0399dde2012-06-06 17:09:28 -0700234 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700235 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100236 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200237 *val = cur_shadow_frame_->GetVReg(vreg);
238 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700239 }
240}
241
Mathieu Chartiere401d142015-04-22 13:56:20 -0700242bool StackVisitor::GetVRegFromQuickCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100243 uint32_t* val) const {
244 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*));
245 DCHECK(code_pointer != nullptr);
246 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*)));
247 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer);
248 uint32_t vmap_offset;
249 // TODO: IsInContext stops before spotting floating point registers.
250 if (vmap_table.IsInContext(vreg, kind, &vmap_offset)) {
251 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
252 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
253 uint32_t reg = vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
254 return GetRegisterIfAccessible(reg, kind, val);
255 } else {
256 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700257 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100258 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000259 *val = *GetVRegAddrFromQuickCode(cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
260 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100261 return true;
262 }
263}
264
Mathieu Chartiere401d142015-04-22 13:56:20 -0700265bool StackVisitor::GetVRegFromOptimizedCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100266 uint32_t* val) const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100267 DCHECK_EQ(m, GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100268 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700269 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100270 // its instructions?
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000271 uint16_t number_of_dex_registers = code_item->registers_size_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100272 DCHECK_LT(vreg, code_item->registers_size_);
273
Mathieu Chartiere401d142015-04-22 13:56:20 -0700274 ArtMethod* outer_method = *GetCurrentQuickFrame();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100275 const void* code_pointer = outer_method->GetQuickOatCodePointer(sizeof(void*));
276 DCHECK(code_pointer != nullptr);
277 CodeInfo code_info = outer_method->GetOptimizedCodeInfo();
David Brazdilf677ebf2015-05-29 16:29:43 +0100278 StackMapEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100279
280 uint32_t native_pc_offset = outer_method->NativeQuickPcOffset(cur_quick_frame_pc_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100281 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100282 DCHECK(stack_map.IsValid());
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100283 size_t depth_in_stack_map = current_inlining_depth_ - 1;
284
285 DexRegisterMap dex_register_map = IsInInlinedFrame()
David Brazdilf677ebf2015-05-29 16:29:43 +0100286 ? code_info.GetDexRegisterMapAtDepth(depth_in_stack_map,
287 code_info.GetInlineInfoOf(stack_map, encoding),
288 encoding,
289 number_of_dex_registers)
290 : code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100291
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000292 DexRegisterLocation::Kind location_kind =
David Brazdilf677ebf2015-05-29 16:29:43 +0100293 dex_register_map.GetLocationKind(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100294 switch (location_kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000295 case DexRegisterLocation::Kind::kInStack: {
David Brazdilf677ebf2015-05-29 16:29:43 +0100296 const int32_t offset = dex_register_map.GetStackOffsetInBytes(vreg,
297 number_of_dex_registers,
298 code_info,
299 encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100300 const uint8_t* addr = reinterpret_cast<const uint8_t*>(cur_quick_frame_) + offset;
301 *val = *reinterpret_cast<const uint32_t*>(addr);
302 return true;
303 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000304 case DexRegisterLocation::Kind::kInRegister:
305 case DexRegisterLocation::Kind::kInFpuRegister: {
David Brazdilf677ebf2015-05-29 16:29:43 +0100306 uint32_t reg =
307 dex_register_map.GetMachineRegister(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100308 return GetRegisterIfAccessible(reg, kind, val);
309 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000310 case DexRegisterLocation::Kind::kConstant:
David Brazdilf677ebf2015-05-29 16:29:43 +0100311 *val = dex_register_map.GetConstant(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100312 return true;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000313 case DexRegisterLocation::Kind::kNone:
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100314 return false;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000315 default:
316 LOG(FATAL)
317 << "Unexpected location kind"
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000318 << DexRegisterLocation::PrettyDescriptor(
David Brazdilf677ebf2015-05-29 16:29:43 +0100319 dex_register_map.GetLocationInternalKind(vreg,
320 number_of_dex_registers,
321 code_info,
322 encoding));
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000323 UNREACHABLE();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100324 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100325}
326
327bool StackVisitor::GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const {
328 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
329 if (!IsAccessibleRegister(reg, is_float)) {
330 return false;
331 }
332 uintptr_t ptr_val = GetRegister(reg, is_float);
333 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
334 if (target64) {
335 const bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
336 const bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
337 int64_t value_long = static_cast<int64_t>(ptr_val);
338 if (wide_lo) {
339 ptr_val = static_cast<uintptr_t>(Low32Bits(value_long));
340 } else if (wide_hi) {
341 ptr_val = static_cast<uintptr_t>(High32Bits(value_long));
342 }
343 }
344 *val = ptr_val;
345 return true;
346}
347
Mathieu Chartiere401d142015-04-22 13:56:20 -0700348bool StackVisitor::GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200349 VRegKind kind_hi, uint64_t* val) const {
350 if (kind_lo == kLongLoVReg) {
351 DCHECK_EQ(kind_hi, kLongHiVReg);
352 } else if (kind_lo == kDoubleLoVReg) {
353 DCHECK_EQ(kind_hi, kDoubleHiVReg);
354 } else {
355 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100356 UNREACHABLE();
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200357 }
358 if (cur_quick_frame_ != nullptr) {
359 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
360 DCHECK(m == GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100361 if (m->IsOptimized(sizeof(void*))) {
362 return GetVRegPairFromOptimizedCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200363 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100364 return GetVRegPairFromQuickCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200365 }
366 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100367 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200368 *val = cur_shadow_frame_->GetVRegLong(vreg);
369 return true;
370 }
371}
372
Mathieu Chartiere401d142015-04-22 13:56:20 -0700373bool StackVisitor::GetVRegPairFromQuickCode(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100374 VRegKind kind_hi, uint64_t* val) const {
375 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*));
376 DCHECK(code_pointer != nullptr);
377 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*)));
378 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer);
379 uint32_t vmap_offset_lo, vmap_offset_hi;
380 // TODO: IsInContext stops before spotting floating point registers.
381 if (vmap_table.IsInContext(vreg, kind_lo, &vmap_offset_lo) &&
382 vmap_table.IsInContext(vreg + 1, kind_hi, &vmap_offset_hi)) {
383 bool is_float = (kind_lo == kDoubleLoVReg);
384 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
385 uint32_t reg_lo = vmap_table.ComputeRegister(spill_mask, vmap_offset_lo, kind_lo);
386 uint32_t reg_hi = vmap_table.ComputeRegister(spill_mask, vmap_offset_hi, kind_hi);
387 return GetRegisterPairIfAccessible(reg_lo, reg_hi, kind_lo, val);
388 } else {
389 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700390 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100391 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000392 uint32_t* addr = GetVRegAddrFromQuickCode(
393 cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
394 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100395 *val = *reinterpret_cast<uint64_t*>(addr);
396 return true;
397 }
398}
399
Mathieu Chartiere401d142015-04-22 13:56:20 -0700400bool StackVisitor::GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100401 VRegKind kind_lo, VRegKind kind_hi,
402 uint64_t* val) const {
403 uint32_t low_32bits;
404 uint32_t high_32bits;
405 bool success = GetVRegFromOptimizedCode(m, vreg, kind_lo, &low_32bits);
406 success &= GetVRegFromOptimizedCode(m, vreg + 1, kind_hi, &high_32bits);
407 if (success) {
408 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
409 }
410 return success;
411}
412
413bool StackVisitor::GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
414 VRegKind kind_lo, uint64_t* val) const {
415 const bool is_float = (kind_lo == kDoubleLoVReg);
416 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
417 return false;
418 }
419 uintptr_t ptr_val_lo = GetRegister(reg_lo, is_float);
420 uintptr_t ptr_val_hi = GetRegister(reg_hi, is_float);
421 bool target64 = Is64BitInstructionSet(kRuntimeISA);
422 if (target64) {
423 int64_t value_long_lo = static_cast<int64_t>(ptr_val_lo);
424 int64_t value_long_hi = static_cast<int64_t>(ptr_val_hi);
425 ptr_val_lo = static_cast<uintptr_t>(Low32Bits(value_long_lo));
426 ptr_val_hi = static_cast<uintptr_t>(High32Bits(value_long_hi));
427 }
428 *val = (static_cast<uint64_t>(ptr_val_hi) << 32) | static_cast<uint32_t>(ptr_val_lo);
429 return true;
430}
431
Mathieu Chartiere401d142015-04-22 13:56:20 -0700432bool StackVisitor::SetVReg(ArtMethod* m, uint16_t vreg, uint32_t new_value,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800433 VRegKind kind) {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200434 if (cur_quick_frame_ != nullptr) {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100435 DCHECK(context_ != nullptr); // You can't reliably write registers without a context.
436 DCHECK(m == GetMethod());
437 if (m->IsOptimized(sizeof(void*))) {
Nicolas Geoffray7cc56a12015-04-24 14:58:19 +0100438 return false;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100439 } else {
440 return SetVRegFromQuickCode(m, vreg, new_value, kind);
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100441 }
Mathieu Chartier67022432012-11-29 18:04:50 -0800442 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100443 cur_shadow_frame_->SetVReg(vreg, new_value);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200444 return true;
Ian Rogers0ec569a2012-07-01 16:43:46 -0700445 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100446}
447
Mathieu Chartiere401d142015-04-22 13:56:20 -0700448bool StackVisitor::SetVRegFromQuickCode(ArtMethod* m, uint16_t vreg, uint32_t new_value,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100449 VRegKind kind) {
450 DCHECK(context_ != nullptr); // You can't reliably write registers without a context.
451 DCHECK(m == GetMethod());
452 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*));
453 DCHECK(code_pointer != nullptr);
454 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*)));
455 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer);
456 uint32_t vmap_offset;
457 // TODO: IsInContext stops before spotting floating point registers.
458 if (vmap_table.IsInContext(vreg, kind, &vmap_offset)) {
459 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
460 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
461 uint32_t reg = vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
462 return SetRegisterIfAccessible(reg, new_value, kind);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700463 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100464 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700465 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100466 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000467 uint32_t* addr = GetVRegAddrFromQuickCode(
468 cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
469 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100470 *addr = new_value;
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200471 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700472 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700473}
474
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100475bool StackVisitor::SetRegisterIfAccessible(uint32_t reg, uint32_t new_value, VRegKind kind) {
476 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
477 if (!IsAccessibleRegister(reg, is_float)) {
478 return false;
479 }
480 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
481
482 // Create a new value that can hold both low 32 and high 32 bits, in
483 // case we are running 64 bits.
484 uintptr_t full_new_value = new_value;
485 // Deal with 32 or 64-bit wide registers in a way that builds on all targets.
486 if (target64) {
487 bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
488 bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
489 if (wide_lo || wide_hi) {
490 uintptr_t old_reg_val = GetRegister(reg, is_float);
491 uint64_t new_vreg_portion = static_cast<uint64_t>(new_value);
492 uint64_t old_reg_val_as_wide = static_cast<uint64_t>(old_reg_val);
493 uint64_t mask = 0xffffffff;
494 if (wide_lo) {
495 mask = mask << 32;
496 } else {
497 new_vreg_portion = new_vreg_portion << 32;
498 }
499 full_new_value = static_cast<uintptr_t>((old_reg_val_as_wide & mask) | new_vreg_portion);
500 }
501 }
502 SetRegister(reg, full_new_value, is_float);
503 return true;
504}
505
Mathieu Chartiere401d142015-04-22 13:56:20 -0700506bool StackVisitor::SetVRegPair(ArtMethod* m, uint16_t vreg, uint64_t new_value,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200507 VRegKind kind_lo, VRegKind kind_hi) {
508 if (kind_lo == kLongLoVReg) {
509 DCHECK_EQ(kind_hi, kLongHiVReg);
510 } else if (kind_lo == kDoubleLoVReg) {
511 DCHECK_EQ(kind_hi, kDoubleHiVReg);
512 } else {
513 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
514 }
515 if (cur_quick_frame_ != nullptr) {
516 DCHECK(context_ != nullptr); // You can't reliably write registers without a context.
517 DCHECK(m == GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100518 if (m->IsOptimized(sizeof(void*))) {
Nicolas Geoffray7cc56a12015-04-24 14:58:19 +0100519 return false;
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200520 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100521 return SetVRegPairFromQuickCode(m, vreg, new_value, kind_lo, kind_hi);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200522 }
523 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100524 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200525 cur_shadow_frame_->SetVRegLong(vreg, new_value);
526 return true;
527 }
528}
529
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700530bool StackVisitor::SetVRegPairFromQuickCode(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700531 ArtMethod* m, uint16_t vreg, uint64_t new_value, VRegKind kind_lo, VRegKind kind_hi) {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100532 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*));
533 DCHECK(code_pointer != nullptr);
534 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*)));
535 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer);
536 uint32_t vmap_offset_lo, vmap_offset_hi;
537 // TODO: IsInContext stops before spotting floating point registers.
538 if (vmap_table.IsInContext(vreg, kind_lo, &vmap_offset_lo) &&
539 vmap_table.IsInContext(vreg + 1, kind_hi, &vmap_offset_hi)) {
540 bool is_float = (kind_lo == kDoubleLoVReg);
541 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
542 uint32_t reg_lo = vmap_table.ComputeRegister(spill_mask, vmap_offset_lo, kind_lo);
543 uint32_t reg_hi = vmap_table.ComputeRegister(spill_mask, vmap_offset_hi, kind_hi);
544 return SetRegisterPairIfAccessible(reg_lo, reg_hi, new_value, is_float);
545 } else {
546 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700547 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100548 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000549 uint32_t* addr = GetVRegAddrFromQuickCode(
550 cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
551 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100552 *reinterpret_cast<uint64_t*>(addr) = new_value;
553 return true;
554 }
555}
556
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100557bool StackVisitor::SetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
558 uint64_t new_value, bool is_float) {
559 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
560 return false;
561 }
562 uintptr_t new_value_lo = static_cast<uintptr_t>(new_value & 0xFFFFFFFF);
563 uintptr_t new_value_hi = static_cast<uintptr_t>(new_value >> 32);
564 bool target64 = Is64BitInstructionSet(kRuntimeISA);
565 // Deal with 32 or 64-bit wide registers in a way that builds on all targets.
566 if (target64) {
567 DCHECK_EQ(reg_lo, reg_hi);
568 SetRegister(reg_lo, new_value, is_float);
569 } else {
570 SetRegister(reg_lo, new_value_lo, is_float);
571 SetRegister(reg_hi, new_value_hi, is_float);
572 }
573 return true;
574}
575
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100576bool StackVisitor::IsAccessibleGPR(uint32_t reg) const {
577 DCHECK(context_ != nullptr);
578 return context_->IsAccessibleGPR(reg);
579}
580
Mathieu Chartier815873e2014-02-13 18:02:13 -0800581uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100582 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
583 DCHECK(context_ != nullptr);
Mathieu Chartier815873e2014-02-13 18:02:13 -0800584 return context_->GetGPRAddress(reg);
585}
586
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100587uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
588 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
589 DCHECK(context_ != nullptr);
590 return context_->GetGPR(reg);
Ian Rogers0399dde2012-06-06 17:09:28 -0700591}
592
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100593void StackVisitor::SetGPR(uint32_t reg, uintptr_t value) {
594 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
595 DCHECK(context_ != nullptr);
596 context_->SetGPR(reg, value);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200597}
598
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100599bool StackVisitor::IsAccessibleFPR(uint32_t reg) const {
600 DCHECK(context_ != nullptr);
601 return context_->IsAccessibleFPR(reg);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200602}
603
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100604uintptr_t StackVisitor::GetFPR(uint32_t reg) const {
605 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
606 DCHECK(context_ != nullptr);
607 return context_->GetFPR(reg);
608}
609
610void StackVisitor::SetFPR(uint32_t reg, uintptr_t value) {
611 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
612 DCHECK(context_ != nullptr);
613 context_->SetFPR(reg, value);
Mathieu Chartier67022432012-11-29 18:04:50 -0800614}
615
Ian Rogers0399dde2012-06-06 17:09:28 -0700616uintptr_t StackVisitor::GetReturnPc() const {
Ian Rogers13735952014-10-08 12:43:28 -0700617 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700618 DCHECK(sp != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700619 uint8_t* pc_addr = sp + GetMethod()->GetReturnPcOffset().SizeValue();
Ian Rogers0399dde2012-06-06 17:09:28 -0700620 return *reinterpret_cast<uintptr_t*>(pc_addr);
621}
622
623void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
Ian Rogers13735952014-10-08 12:43:28 -0700624 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700625 CHECK(sp != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700626 uint8_t* pc_addr = sp + GetMethod()->GetReturnPcOffset().SizeValue();
Ian Rogers0399dde2012-06-06 17:09:28 -0700627 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc;
628}
629
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100630size_t StackVisitor::ComputeNumFrames(Thread* thread, StackWalkKind walk_kind) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700631 struct NumFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100632 NumFramesVisitor(Thread* thread_in, StackWalkKind walk_kind_in)
633 : StackVisitor(thread_in, nullptr, walk_kind_in), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700634
Ian Rogers5cf98192014-05-29 21:31:50 -0700635 bool VisitFrame() OVERRIDE {
Ian Rogers0399dde2012-06-06 17:09:28 -0700636 frames++;
637 return true;
638 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700639
Ian Rogers0399dde2012-06-06 17:09:28 -0700640 size_t frames;
641 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100642 NumFramesVisitor visitor(thread, walk_kind);
Ian Rogers0399dde2012-06-06 17:09:28 -0700643 visitor.WalkStack(true);
644 return visitor.frames;
645}
646
Mathieu Chartiere401d142015-04-22 13:56:20 -0700647bool StackVisitor::GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700648 struct HasMoreFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100649 HasMoreFramesVisitor(Thread* thread,
650 StackWalkKind walk_kind,
651 size_t num_frames,
652 size_t frame_height)
653 : StackVisitor(thread, nullptr, walk_kind, num_frames),
654 frame_height_(frame_height),
655 found_frame_(false),
656 has_more_frames_(false),
657 next_method_(nullptr),
658 next_dex_pc_(0) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700659 }
660
661 bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
662 if (found_frame_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700663 ArtMethod* method = GetMethod();
Ian Rogers5cf98192014-05-29 21:31:50 -0700664 if (method != nullptr && !method->IsRuntimeMethod()) {
665 has_more_frames_ = true;
666 next_method_ = method;
667 next_dex_pc_ = GetDexPc();
668 return false; // End stack walk once next method is found.
669 }
670 } else if (GetFrameHeight() == frame_height_) {
671 found_frame_ = true;
672 }
673 return true;
674 }
675
676 size_t frame_height_;
677 bool found_frame_;
678 bool has_more_frames_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700679 ArtMethod* next_method_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700680 uint32_t next_dex_pc_;
681 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100682 HasMoreFramesVisitor visitor(thread_, walk_kind_, GetNumFrames(), GetFrameHeight());
Ian Rogers5cf98192014-05-29 21:31:50 -0700683 visitor.WalkStack(true);
684 *next_method = visitor.next_method_;
685 *next_dex_pc = visitor.next_dex_pc_;
686 return visitor.has_more_frames_;
687}
688
Ian Rogers7a22fa62013-01-23 12:16:16 -0800689void StackVisitor::DescribeStack(Thread* thread) {
Ian Rogers306057f2012-11-26 12:45:53 -0800690 struct DescribeStackVisitor : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800691 explicit DescribeStackVisitor(Thread* thread_in)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100692 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800693
Ian Rogers5cf98192014-05-29 21:31:50 -0700694 bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers306057f2012-11-26 12:45:53 -0800695 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation();
696 return true;
697 }
698 };
Ian Rogers7a22fa62013-01-23 12:16:16 -0800699 DescribeStackVisitor visitor(thread);
Ian Rogers306057f2012-11-26 12:45:53 -0800700 visitor.WalkStack(true);
701}
702
Ian Rogers40e3bac2012-11-20 00:09:14 -0800703std::string StackVisitor::DescribeLocation() const {
704 std::string result("Visiting method '");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700705 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700706 if (m == nullptr) {
Ian Rogers306057f2012-11-26 12:45:53 -0800707 return "upcall";
708 }
709 result += PrettyMethod(m);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800710 result += StringPrintf("' at dex PC 0x%04x", GetDexPc());
Ian Rogers40e3bac2012-11-20 00:09:14 -0800711 if (!IsShadowFrame()) {
712 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc()));
713 }
714 return result;
715}
716
Ian Rogerse63db272014-07-15 15:36:11 -0700717static instrumentation::InstrumentationStackFrame& GetInstrumentationStackFrame(Thread* thread,
718 uint32_t depth) {
719 CHECK_LT(depth, thread->GetInstrumentationStack()->size());
720 return thread->GetInstrumentationStack()->at(depth);
Ian Rogers7a22fa62013-01-23 12:16:16 -0800721}
722
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700723void StackVisitor::SanityCheckFrame() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800724 if (kIsDebugBuild) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700725 ArtMethod* method = GetMethod();
726 auto* declaring_class = method->GetDeclaringClass();
727 // Runtime methods have null declaring class.
728 if (!method->IsRuntimeMethod()) {
729 CHECK(declaring_class != nullptr);
730 CHECK_EQ(declaring_class->GetClass(), declaring_class->GetClass()->GetClass())
731 << declaring_class;
732 } else {
733 CHECK(declaring_class == nullptr);
734 }
735 auto* runtime = Runtime::Current();
736 auto* la = runtime->GetLinearAlloc();
737 if (!la->Contains(method)) {
738 // Check image space.
739 bool in_image = false;
740 for (auto& space : runtime->GetHeap()->GetContinuousSpaces()) {
741 if (space->IsImageSpace()) {
742 auto* image_space = space->AsImageSpace();
743 const auto& header = image_space->GetImageHeader();
744 const auto* methods = &header.GetMethodsSection();
745 if (methods->Contains(reinterpret_cast<const uint8_t*>(method) - image_space->Begin())) {
746 in_image = true;
747 break;
748 }
749 }
750 }
751 CHECK(in_image) << PrettyMethod(method) << " not in linear alloc or image";
752 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800753 if (cur_quick_frame_ != nullptr) {
754 method->AssertPcIsWithinQuickCode(cur_quick_frame_pc_);
755 // Frame sanity.
756 size_t frame_size = method->GetFrameSizeInBytes();
757 CHECK_NE(frame_size, 0u);
Andreas Gampe5b417b92014-03-10 14:18:35 -0700758 // A rough guess at an upper size we expect to see for a frame.
759 // 256 registers
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700760 // 2 words HandleScope overhead
Andreas Gampe5b417b92014-03-10 14:18:35 -0700761 // 3+3 register spills
762 // TODO: this seems architecture specific for the case of JNI frames.
Brian Carlstromed08bd42014-03-19 18:34:17 -0700763 // TODO: 083-compiler-regressions ManyFloatArgs shows this estimate is wrong.
764 // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word);
765 const size_t kMaxExpectedFrameSize = 2 * KB;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800766 CHECK_LE(frame_size, kMaxExpectedFrameSize);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700767 size_t return_pc_offset = method->GetReturnPcOffset().SizeValue();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800768 CHECK_LT(return_pc_offset, frame_size);
769 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700770 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700771}
772
773void StackVisitor::WalkStack(bool include_transitions) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800774 DCHECK(thread_ == Thread::Current() || thread_->IsSuspended());
Ian Rogers62d6c772013-02-27 08:32:07 -0800775 CHECK_EQ(cur_depth_, 0U);
776 bool exit_stubs_installed = Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled();
jeffhao725a9572012-11-13 18:20:12 -0800777 uint32_t instrumentation_stack_depth = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700778
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700779 for (const ManagedStack* current_fragment = thread_->GetManagedStack();
780 current_fragment != nullptr; current_fragment = current_fragment->GetLink()) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700781 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
782 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700783 cur_quick_frame_pc_ = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700784
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700785 if (cur_quick_frame_ != nullptr) { // Handle quick stack frames.
Ian Rogers0399dde2012-06-06 17:09:28 -0700786 // Can't be both a shadow and a quick fragment.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700787 DCHECK(current_fragment->GetTopShadowFrame() == nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700788 ArtMethod* method = *cur_quick_frame_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700789 while (method != nullptr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700790 SanityCheckFrame();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100791
792 if ((walk_kind_ == StackWalkKind::kIncludeInlinedFrames)
793 && method->IsOptimized(sizeof(void*))) {
794 CodeInfo code_info = method->GetOptimizedCodeInfo();
David Brazdilf677ebf2015-05-29 16:29:43 +0100795 StackMapEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100796 uint32_t native_pc_offset = method->NativeQuickPcOffset(cur_quick_frame_pc_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100797 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
798 if (stack_map.IsValid() && stack_map.HasInlineInfo(encoding)) {
799 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100800 DCHECK_EQ(current_inlining_depth_, 0u);
801 for (current_inlining_depth_ = inline_info.GetDepth();
802 current_inlining_depth_ != 0;
803 --current_inlining_depth_) {
804 bool should_continue = VisitFrame();
805 if (UNLIKELY(!should_continue)) {
806 return;
807 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100808 cur_depth_++;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100809 }
810 }
811 }
812
Dave Allison5cd33752014-04-15 15:57:58 -0700813 bool should_continue = VisitFrame();
814 if (UNLIKELY(!should_continue)) {
815 return;
Ian Rogers0399dde2012-06-06 17:09:28 -0700816 }
Dave Allison5cd33752014-04-15 15:57:58 -0700817
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700818 if (context_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700819 context_->FillCalleeSaves(*this);
820 }
821 size_t frame_size = method->GetFrameSizeInBytes();
822 // Compute PC for next stack frame from return PC.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700823 size_t return_pc_offset = method->GetReturnPcOffset(frame_size).SizeValue();
Ian Rogers13735952014-10-08 12:43:28 -0700824 uint8_t* return_pc_addr = reinterpret_cast<uint8_t*>(cur_quick_frame_) + return_pc_offset;
Ian Rogers0399dde2012-06-06 17:09:28 -0700825 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800826 if (UNLIKELY(exit_stubs_installed)) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700827 // While profiling, the return pc is restored from the side stack, except when walking
828 // the stack for an exception where the side stack will be unwound in VisitFrame.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700829 if (reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc) {
Sebastien Hertz74e256b2013-10-04 10:40:37 +0200830 const instrumentation::InstrumentationStackFrame& instrumentation_frame =
Ian Rogerse63db272014-07-15 15:36:11 -0700831 GetInstrumentationStackFrame(thread_, instrumentation_stack_depth);
jeffhao725a9572012-11-13 18:20:12 -0800832 instrumentation_stack_depth++;
Jeff Haofb2802d2013-07-24 13:53:05 -0700833 if (GetMethod() == Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveAll)) {
834 // Skip runtime save all callee frames which are used to deliver exceptions.
835 } else if (instrumentation_frame.interpreter_entry_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700836 ArtMethod* callee = Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs);
Jeff Haofb2802d2013-07-24 13:53:05 -0700837 CHECK_EQ(GetMethod(), callee) << "Expected: " << PrettyMethod(callee) << " Found: "
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100838 << PrettyMethod(GetMethod());
Jeff Hao9a916d32013-06-27 18:45:37 -0700839 } else if (instrumentation_frame.method_ != GetMethod()) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800840 LOG(FATAL) << "Expected: " << PrettyMethod(instrumentation_frame.method_)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100841 << " Found: " << PrettyMethod(GetMethod());
Ian Rogers62d6c772013-02-27 08:32:07 -0800842 }
843 if (num_frames_ != 0) {
844 // Check agreement of frame Ids only if num_frames_ is computed to avoid infinite
845 // recursion.
846 CHECK(instrumentation_frame.frame_id_ == GetFrameId())
847 << "Expected: " << instrumentation_frame.frame_id_
848 << " Found: " << GetFrameId();
849 }
jeffhao725a9572012-11-13 18:20:12 -0800850 return_pc = instrumentation_frame.return_pc_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700851 }
852 }
853 cur_quick_frame_pc_ = return_pc;
Ian Rogers13735952014-10-08 12:43:28 -0700854 uint8_t* next_frame = reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700855 cur_quick_frame_ = reinterpret_cast<ArtMethod**>(next_frame);
856
857 if (kDebugStackWalk) {
858 LOG(INFO) << PrettyMethod(method) << "@" << method << " size=" << frame_size
859 << " optimized=" << method->IsOptimized(sizeof(void*))
860 << " native=" << method->IsNative()
861 << " entrypoints=" << method->GetEntryPointFromQuickCompiledCode()
862 << "," << method->GetEntryPointFromJni()
863 << "," << method->GetEntryPointFromInterpreter()
864 << " next=" << *cur_quick_frame_;
865 }
866
Ian Rogers0399dde2012-06-06 17:09:28 -0700867 cur_depth_++;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700868 method = *cur_quick_frame_;
jeffhao6641ea12013-01-02 18:13:42 -0800869 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700870 } else if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700871 do {
872 SanityCheckFrame();
873 bool should_continue = VisitFrame();
874 if (UNLIKELY(!should_continue)) {
875 return;
876 }
877 cur_depth_++;
878 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700879 } while (cur_shadow_frame_ != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700880 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700881 if (include_transitions) {
882 bool should_continue = VisitFrame();
883 if (!should_continue) {
884 return;
885 }
886 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800887 cur_depth_++;
888 }
889 if (num_frames_ != 0) {
890 CHECK_EQ(cur_depth_, num_frames_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700891 }
892}
893
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800894void JavaFrameRootInfo::Describe(std::ostream& os) const {
895 const StackVisitor* visitor = stack_visitor_;
896 CHECK(visitor != nullptr);
897 os << "Type=" << GetType() << " thread_id=" << GetThreadId() << " location=" <<
898 visitor->DescribeLocation() << " vreg=" << vreg_;
899}
900
Mathieu Chartiere401d142015-04-22 13:56:20 -0700901int StackVisitor::GetVRegOffsetFromQuickCode(const DexFile::CodeItem* code_item,
902 uint32_t core_spills, uint32_t fp_spills,
903 size_t frame_size, int reg, InstructionSet isa) {
904 size_t pointer_size = InstructionSetPointerSize(isa);
905 if (kIsDebugBuild) {
906 auto* runtime = Runtime::Current();
907 if (runtime != nullptr) {
908 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
909 }
910 }
911 DCHECK_EQ(frame_size & (kStackAlignment - 1), 0U);
912 DCHECK_NE(reg, -1);
913 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
914 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
915 + sizeof(uint32_t); // Filler.
916 int num_regs = code_item->registers_size_ - code_item->ins_size_;
917 int temp_threshold = code_item->registers_size_;
918 const int max_num_special_temps = 1;
919 if (reg == temp_threshold) {
920 // The current method pointer corresponds to special location on stack.
921 return 0;
922 } else if (reg >= temp_threshold + max_num_special_temps) {
923 /*
924 * Special temporaries may have custom locations and the logic above deals with that.
925 * However, non-special temporaries are placed relative to the outs.
926 */
927 int temps_start = code_item->outs_size_ * sizeof(uint32_t) + pointer_size /* art method */;
928 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
929 return temps_start + relative_offset;
930 } else if (reg < num_regs) {
931 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
932 return locals_start + (reg * sizeof(uint32_t));
933 } else {
934 // Handle ins.
935 return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + pointer_size /* art method */;
936 }
937}
938
Elliott Hughes68e76522011-10-05 13:22:16 -0700939} // namespace art