blob: f7b96eaf2edae768aedb27fe2bb53ad78e5833de [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"
Dave Allisonf9439142014-03-27 15:10:22 -070020#include "base/hex_dump.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070021#include "entrypoints/runtime_asm_entrypoints.h"
Mathieu Chartier50030ef2015-05-08 14:19:26 -070022#include "gc_map.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070023#include "mirror/art_method-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070024#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "mirror/object.h"
26#include "mirror/object-inl.h"
27#include "mirror/object_array-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010028#include "quick/quick_method_frame_info.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070029#include "runtime.h"
Dave Allisonf9439142014-03-27 15:10:22 -070030#include "thread.h"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070031#include "thread_list.h"
Mathieu Chartier4e305412014-02-19 10:54:44 -080032#include "verify_object-inl.h"
Ian Rogers1809a722013-08-09 22:05:32 -070033#include "vmap_table.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070034
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080035namespace art {
36
Ian Rogers62d6c772013-02-27 08:32:07 -080037mirror::Object* ShadowFrame::GetThisObject() const {
Brian Carlstromea46f952013-07-30 01:26:50 -070038 mirror::ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -080039 if (m->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070040 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -080041 } else if (m->IsNative()) {
42 return GetVRegReference(0);
43 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070044 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070045 CHECK(code_item != nullptr) << PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -080046 uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
47 return GetVRegReference(reg);
48 }
49}
50
Jeff Haoe701f482013-05-24 11:50:49 -070051mirror::Object* ShadowFrame::GetThisObject(uint16_t num_ins) const {
Brian Carlstromea46f952013-07-30 01:26:50 -070052 mirror::ArtMethod* m = GetMethod();
Jeff Haoe701f482013-05-24 11:50:49 -070053 if (m->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070054 return nullptr;
Jeff Haoe701f482013-05-24 11:50:49 -070055 } else {
Jeff Hao8d448852013-06-03 17:26:19 -070056 return GetVRegReference(NumberOfVRegs() - num_ins);
Jeff Haoe701f482013-05-24 11:50:49 -070057 }
58}
59
TDYa127ce4cc0d2012-11-18 16:59:53 -080060size_t ManagedStack::NumJniShadowFrameReferences() const {
Ian Rogers0399dde2012-06-06 17:09:28 -070061 size_t count = 0;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070062 for (const ManagedStack* current_fragment = this; current_fragment != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070063 current_fragment = current_fragment->GetLink()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070064 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070065 current_frame = current_frame->GetLink()) {
TDYa127ce4cc0d2012-11-18 16:59:53 -080066 if (current_frame->GetMethod()->IsNative()) {
67 // The JNI ShadowFrame only contains references. (For indirect reference.)
68 count += current_frame->NumberOfVRegs();
69 }
Ian Rogers0399dde2012-06-06 17:09:28 -070070 }
71 }
72 return count;
73}
74
Ian Rogersef7d42f2014-01-06 12:55:46 -080075bool ManagedStack::ShadowFramesContain(StackReference<mirror::Object>* shadow_frame_entry) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070076 for (const ManagedStack* current_fragment = this; current_fragment != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070077 current_fragment = current_fragment->GetLink()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070078 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070079 current_frame = current_frame->GetLink()) {
80 if (current_frame->Contains(shadow_frame_entry)) {
81 return true;
82 }
83 }
84 }
85 return false;
86}
87
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010088StackVisitor::StackVisitor(Thread* thread, Context* context, StackWalkKind walk_kind)
89 : StackVisitor(thread, context, walk_kind, 0) {}
Ian Rogers7a22fa62013-01-23 12:16:16 -080090
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010091StackVisitor::StackVisitor(Thread* thread,
92 Context* context,
93 StackWalkKind walk_kind,
94 size_t num_frames)
95 : thread_(thread),
96 walk_kind_(walk_kind),
97 cur_shadow_frame_(nullptr),
98 cur_quick_frame_(nullptr),
99 cur_quick_frame_pc_(0),
100 num_frames_(num_frames),
101 cur_depth_(0),
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100102 current_inlining_depth_(0),
Ian Rogers5cf98192014-05-29 21:31:50 -0700103 context_(context) {
104 DCHECK(thread == Thread::Current() || thread->IsSuspended()) << *thread;
105}
106
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100107InlineInfo StackVisitor::GetCurrentInlineInfo() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
108 mirror::ArtMethod* outer_method = GetCurrentQuickFrame()->AsMirrorPtr();
109 uint32_t native_pc_offset = outer_method->NativeQuickPcOffset(cur_quick_frame_pc_);
110 CodeInfo code_info = outer_method->GetOptimizedCodeInfo();
111 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100112 DCHECK(stack_map.IsValid());
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100113 return code_info.GetInlineInfoOf(stack_map);
114}
115
116mirror::ArtMethod* StackVisitor::GetMethod() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
117 if (cur_shadow_frame_ != nullptr) {
118 return cur_shadow_frame_->GetMethod();
119 } else if (cur_quick_frame_ != nullptr) {
120 if (IsInInlinedFrame()) {
121 size_t depth_in_stack_map = current_inlining_depth_ - 1;
122 return GetCurrentQuickFrame()->AsMirrorPtr()->GetDexCacheResolvedMethod(
123 GetCurrentInlineInfo().GetMethodIndexAtDepth(depth_in_stack_map));
124 } else {
125 return cur_quick_frame_->AsMirrorPtr();
126 }
127 } else {
128 return nullptr;
129 }
130}
131
Dave Allisonb373e092014-02-20 16:06:36 -0800132uint32_t StackVisitor::GetDexPc(bool abort_on_failure) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700133 if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700134 return cur_shadow_frame_->GetDexPC();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700135 } else if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100136 if (IsInInlinedFrame()) {
137 size_t depth_in_stack_map = current_inlining_depth_ - 1;
138 return GetCurrentInlineInfo().GetDexPcAtDepth(depth_in_stack_map);
139 } else {
140 return GetMethod()->ToDexPc(cur_quick_frame_pc_, abort_on_failure);
141 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700142 } else {
143 return 0;
144 }
145}
146
Sebastien Hertza836bc92014-11-25 16:30:53 +0100147extern "C" mirror::Object* artQuickGetProxyThisObject(StackReference<mirror::ArtMethod>* sp)
148 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
149
Ian Rogers62d6c772013-02-27 08:32:07 -0800150mirror::Object* StackVisitor::GetThisObject() const {
Brian Carlstromea46f952013-07-30 01:26:50 -0700151 mirror::ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800152 if (m->IsStatic()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100153 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800154 } else if (m->IsNative()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100155 if (cur_quick_frame_ != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700156 HandleScope* hs = reinterpret_cast<HandleScope*>(
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700157 reinterpret_cast<char*>(cur_quick_frame_) + m->GetHandleScopeOffset().SizeValue());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700158 return hs->GetReference(0);
Ian Rogers62d6c772013-02-27 08:32:07 -0800159 } else {
160 return cur_shadow_frame_->GetVRegReference(0);
161 }
Sebastien Hertza836bc92014-11-25 16:30:53 +0100162 } else if (m->IsProxyMethod()) {
163 if (cur_quick_frame_ != nullptr) {
164 return artQuickGetProxyThisObject(cur_quick_frame_);
165 } else {
166 return cur_shadow_frame_->GetVRegReference(0);
167 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800168 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700169 const DexFile::CodeItem* code_item = m->GetCodeItem();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100170 if (code_item == nullptr) {
Ian Rogerse0dcd462014-03-08 15:21:04 -0800171 UNIMPLEMENTED(ERROR) << "Failed to determine this object of abstract or proxy method: "
Ian Rogers62d6c772013-02-27 08:32:07 -0800172 << PrettyMethod(m);
Ian Rogerse0dcd462014-03-08 15:21:04 -0800173 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800174 } else {
175 uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000176 uint32_t value = 0;
177 bool success = GetVReg(m, reg, kReferenceVReg, &value);
178 // We currently always guarantee the `this` object is live throughout the method.
179 CHECK(success) << "Failed to read the this object in " << PrettyMethod(m);
180 return reinterpret_cast<mirror::Object*>(value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800181 }
182 }
183}
184
Ian Rogers0c7abda2012-09-19 13:33:42 -0700185size_t StackVisitor::GetNativePcOffset() const {
186 DCHECK(!IsShadowFrame());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700187 return GetMethod()->NativeQuickPcOffset(cur_quick_frame_pc_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700188}
189
Mathieu Chartier50030ef2015-05-08 14:19:26 -0700190bool StackVisitor::IsReferenceVReg(mirror::ArtMethod* m, uint16_t vreg) {
191 // Process register map (which native and runtime methods don't have)
192 if (m->IsNative() || m->IsRuntimeMethod() || m->IsProxyMethod()) {
193 return false;
194 }
195 if (m->IsOptimized(sizeof(void*))) {
196 return true; // TODO: Implement.
197 }
198 const uint8_t* native_gc_map = m->GetNativeGcMap(sizeof(void*));
199 CHECK(native_gc_map != nullptr) << PrettyMethod(m);
200 const DexFile::CodeItem* code_item = m->GetCodeItem();
201 // Can't be null or how would we compile its instructions?
202 DCHECK(code_item != nullptr) << PrettyMethod(m);
203 NativePcOffsetToReferenceMap map(native_gc_map);
204 size_t num_regs = std::min(map.RegWidth() * 8, static_cast<size_t>(code_item->registers_size_));
205 const uint8_t* reg_bitmap = nullptr;
206 if (num_regs > 0) {
207 Runtime* runtime = Runtime::Current();
208 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(m, sizeof(void*));
209 uintptr_t native_pc_offset = m->NativeQuickPcOffset(GetCurrentQuickFramePc(), entry_point);
210 reg_bitmap = map.FindBitMap(native_pc_offset);
211 DCHECK(reg_bitmap != nullptr);
212 }
213 // Does this register hold a reference?
214 return vreg < num_regs && TestBitmap(vreg, reg_bitmap);
215}
216
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200217bool StackVisitor::GetVReg(mirror::ArtMethod* m, uint16_t vreg, VRegKind kind,
218 uint32_t* val) const {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200219 if (cur_quick_frame_ != nullptr) {
220 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800221 DCHECK(m == GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100222 if (m->IsOptimized(sizeof(void*))) {
223 return GetVRegFromOptimizedCode(m, vreg, kind, val);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700224 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100225 return GetVRegFromQuickCode(m, vreg, kind, val);
Ian Rogers0399dde2012-06-06 17:09:28 -0700226 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700227 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100228 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200229 *val = cur_shadow_frame_->GetVReg(vreg);
230 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700231 }
232}
233
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100234bool StackVisitor::GetVRegFromQuickCode(mirror::ArtMethod* m, uint16_t vreg, VRegKind kind,
235 uint32_t* val) const {
236 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*));
237 DCHECK(code_pointer != nullptr);
238 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*)));
239 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer);
240 uint32_t vmap_offset;
241 // TODO: IsInContext stops before spotting floating point registers.
242 if (vmap_table.IsInContext(vreg, kind, &vmap_offset)) {
243 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
244 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
245 uint32_t reg = vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
246 return GetRegisterIfAccessible(reg, kind, val);
247 } else {
248 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700249 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100250 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000251 *val = *GetVRegAddrFromQuickCode(cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
252 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100253 return true;
254 }
255}
256
257bool StackVisitor::GetVRegFromOptimizedCode(mirror::ArtMethod* m, uint16_t vreg, VRegKind kind,
258 uint32_t* val) const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100259 DCHECK_EQ(m, GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100260 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700261 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100262 // its instructions?
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000263 uint16_t number_of_dex_registers = code_item->registers_size_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100264 DCHECK_LT(vreg, code_item->registers_size_);
265
266 mirror::ArtMethod* outer_method = GetCurrentQuickFrame()->AsMirrorPtr();
267 const void* code_pointer = outer_method->GetQuickOatCodePointer(sizeof(void*));
268 DCHECK(code_pointer != nullptr);
269 CodeInfo code_info = outer_method->GetOptimizedCodeInfo();
270
271 uint32_t native_pc_offset = outer_method->NativeQuickPcOffset(cur_quick_frame_pc_);
272 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100273 DCHECK(stack_map.IsValid());
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100274 size_t depth_in_stack_map = current_inlining_depth_ - 1;
275
276 DexRegisterMap dex_register_map = IsInInlinedFrame()
277 ? code_info.GetDexRegisterMapAtDepth(
278 depth_in_stack_map, code_info.GetInlineInfoOf(stack_map), number_of_dex_registers)
279 : code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers);
280
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000281 DexRegisterLocation::Kind location_kind =
Roland Levillaina552e1c2015-03-26 15:01:03 +0000282 dex_register_map.GetLocationKind(vreg, number_of_dex_registers, code_info);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100283 switch (location_kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000284 case DexRegisterLocation::Kind::kInStack: {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000285 const int32_t offset =
286 dex_register_map.GetStackOffsetInBytes(vreg, number_of_dex_registers, code_info);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100287 const uint8_t* addr = reinterpret_cast<const uint8_t*>(cur_quick_frame_) + offset;
288 *val = *reinterpret_cast<const uint32_t*>(addr);
289 return true;
290 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000291 case DexRegisterLocation::Kind::kInRegister:
292 case DexRegisterLocation::Kind::kInFpuRegister: {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000293 uint32_t reg = dex_register_map.GetMachineRegister(vreg, number_of_dex_registers, code_info);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100294 return GetRegisterIfAccessible(reg, kind, val);
295 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000296 case DexRegisterLocation::Kind::kConstant:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000297 *val = dex_register_map.GetConstant(vreg, number_of_dex_registers, code_info);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100298 return true;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000299 case DexRegisterLocation::Kind::kNone:
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100300 return false;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000301 default:
302 LOG(FATAL)
303 << "Unexpected location kind"
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000304 << DexRegisterLocation::PrettyDescriptor(
Roland Levillaina552e1c2015-03-26 15:01:03 +0000305 dex_register_map.GetLocationInternalKind(vreg, number_of_dex_registers, code_info));
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000306 UNREACHABLE();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100307 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100308}
309
310bool StackVisitor::GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const {
311 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
312 if (!IsAccessibleRegister(reg, is_float)) {
313 return false;
314 }
315 uintptr_t ptr_val = GetRegister(reg, is_float);
316 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
317 if (target64) {
318 const bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
319 const bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
320 int64_t value_long = static_cast<int64_t>(ptr_val);
321 if (wide_lo) {
322 ptr_val = static_cast<uintptr_t>(Low32Bits(value_long));
323 } else if (wide_hi) {
324 ptr_val = static_cast<uintptr_t>(High32Bits(value_long));
325 }
326 }
327 *val = ptr_val;
328 return true;
329}
330
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200331bool StackVisitor::GetVRegPair(mirror::ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
332 VRegKind kind_hi, uint64_t* val) const {
333 if (kind_lo == kLongLoVReg) {
334 DCHECK_EQ(kind_hi, kLongHiVReg);
335 } else if (kind_lo == kDoubleLoVReg) {
336 DCHECK_EQ(kind_hi, kDoubleHiVReg);
337 } else {
338 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100339 UNREACHABLE();
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200340 }
341 if (cur_quick_frame_ != nullptr) {
342 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
343 DCHECK(m == GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100344 if (m->IsOptimized(sizeof(void*))) {
345 return GetVRegPairFromOptimizedCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200346 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100347 return GetVRegPairFromQuickCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200348 }
349 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100350 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200351 *val = cur_shadow_frame_->GetVRegLong(vreg);
352 return true;
353 }
354}
355
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100356bool StackVisitor::GetVRegPairFromQuickCode(mirror::ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
357 VRegKind kind_hi, uint64_t* val) const {
358 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*));
359 DCHECK(code_pointer != nullptr);
360 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*)));
361 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer);
362 uint32_t vmap_offset_lo, vmap_offset_hi;
363 // TODO: IsInContext stops before spotting floating point registers.
364 if (vmap_table.IsInContext(vreg, kind_lo, &vmap_offset_lo) &&
365 vmap_table.IsInContext(vreg + 1, kind_hi, &vmap_offset_hi)) {
366 bool is_float = (kind_lo == kDoubleLoVReg);
367 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
368 uint32_t reg_lo = vmap_table.ComputeRegister(spill_mask, vmap_offset_lo, kind_lo);
369 uint32_t reg_hi = vmap_table.ComputeRegister(spill_mask, vmap_offset_hi, kind_hi);
370 return GetRegisterPairIfAccessible(reg_lo, reg_hi, kind_lo, val);
371 } else {
372 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700373 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100374 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000375 uint32_t* addr = GetVRegAddrFromQuickCode(
376 cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
377 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100378 *val = *reinterpret_cast<uint64_t*>(addr);
379 return true;
380 }
381}
382
383bool StackVisitor::GetVRegPairFromOptimizedCode(mirror::ArtMethod* m, uint16_t vreg,
384 VRegKind kind_lo, VRegKind kind_hi,
385 uint64_t* val) const {
386 uint32_t low_32bits;
387 uint32_t high_32bits;
388 bool success = GetVRegFromOptimizedCode(m, vreg, kind_lo, &low_32bits);
389 success &= GetVRegFromOptimizedCode(m, vreg + 1, kind_hi, &high_32bits);
390 if (success) {
391 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
392 }
393 return success;
394}
395
396bool StackVisitor::GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
397 VRegKind kind_lo, uint64_t* val) const {
398 const bool is_float = (kind_lo == kDoubleLoVReg);
399 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
400 return false;
401 }
402 uintptr_t ptr_val_lo = GetRegister(reg_lo, is_float);
403 uintptr_t ptr_val_hi = GetRegister(reg_hi, is_float);
404 bool target64 = Is64BitInstructionSet(kRuntimeISA);
405 if (target64) {
406 int64_t value_long_lo = static_cast<int64_t>(ptr_val_lo);
407 int64_t value_long_hi = static_cast<int64_t>(ptr_val_hi);
408 ptr_val_lo = static_cast<uintptr_t>(Low32Bits(value_long_lo));
409 ptr_val_hi = static_cast<uintptr_t>(High32Bits(value_long_hi));
410 }
411 *val = (static_cast<uint64_t>(ptr_val_hi) << 32) | static_cast<uint32_t>(ptr_val_lo);
412 return true;
413}
414
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200415bool StackVisitor::SetVReg(mirror::ArtMethod* m, uint16_t vreg, uint32_t new_value,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800416 VRegKind kind) {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200417 if (cur_quick_frame_ != nullptr) {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100418 DCHECK(context_ != nullptr); // You can't reliably write registers without a context.
419 DCHECK(m == GetMethod());
420 if (m->IsOptimized(sizeof(void*))) {
Nicolas Geoffray7cc56a12015-04-24 14:58:19 +0100421 return false;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100422 } else {
423 return SetVRegFromQuickCode(m, vreg, new_value, kind);
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100424 }
Mathieu Chartier67022432012-11-29 18:04:50 -0800425 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100426 cur_shadow_frame_->SetVReg(vreg, new_value);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200427 return true;
Ian Rogers0ec569a2012-07-01 16:43:46 -0700428 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100429}
430
431bool StackVisitor::SetVRegFromQuickCode(mirror::ArtMethod* m, uint16_t vreg, uint32_t new_value,
432 VRegKind kind) {
433 DCHECK(context_ != nullptr); // You can't reliably write registers without a context.
434 DCHECK(m == GetMethod());
435 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*));
436 DCHECK(code_pointer != nullptr);
437 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*)));
438 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer);
439 uint32_t vmap_offset;
440 // TODO: IsInContext stops before spotting floating point registers.
441 if (vmap_table.IsInContext(vreg, kind, &vmap_offset)) {
442 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
443 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
444 uint32_t reg = vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
445 return SetRegisterIfAccessible(reg, new_value, kind);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700446 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100447 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700448 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100449 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000450 uint32_t* addr = GetVRegAddrFromQuickCode(
451 cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
452 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100453 *addr = new_value;
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200454 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700455 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700456}
457
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100458bool StackVisitor::SetRegisterIfAccessible(uint32_t reg, uint32_t new_value, VRegKind kind) {
459 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
460 if (!IsAccessibleRegister(reg, is_float)) {
461 return false;
462 }
463 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
464
465 // Create a new value that can hold both low 32 and high 32 bits, in
466 // case we are running 64 bits.
467 uintptr_t full_new_value = new_value;
468 // Deal with 32 or 64-bit wide registers in a way that builds on all targets.
469 if (target64) {
470 bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
471 bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
472 if (wide_lo || wide_hi) {
473 uintptr_t old_reg_val = GetRegister(reg, is_float);
474 uint64_t new_vreg_portion = static_cast<uint64_t>(new_value);
475 uint64_t old_reg_val_as_wide = static_cast<uint64_t>(old_reg_val);
476 uint64_t mask = 0xffffffff;
477 if (wide_lo) {
478 mask = mask << 32;
479 } else {
480 new_vreg_portion = new_vreg_portion << 32;
481 }
482 full_new_value = static_cast<uintptr_t>((old_reg_val_as_wide & mask) | new_vreg_portion);
483 }
484 }
485 SetRegister(reg, full_new_value, is_float);
486 return true;
487}
488
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200489bool StackVisitor::SetVRegPair(mirror::ArtMethod* m, uint16_t vreg, uint64_t new_value,
490 VRegKind kind_lo, VRegKind kind_hi) {
491 if (kind_lo == kLongLoVReg) {
492 DCHECK_EQ(kind_hi, kLongHiVReg);
493 } else if (kind_lo == kDoubleLoVReg) {
494 DCHECK_EQ(kind_hi, kDoubleHiVReg);
495 } else {
496 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
497 }
498 if (cur_quick_frame_ != nullptr) {
499 DCHECK(context_ != nullptr); // You can't reliably write registers without a context.
500 DCHECK(m == GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100501 if (m->IsOptimized(sizeof(void*))) {
Nicolas Geoffray7cc56a12015-04-24 14:58:19 +0100502 return false;
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200503 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100504 return SetVRegPairFromQuickCode(m, vreg, new_value, kind_lo, kind_hi);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200505 }
506 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100507 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200508 cur_shadow_frame_->SetVRegLong(vreg, new_value);
509 return true;
510 }
511}
512
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700513bool StackVisitor::SetVRegPairFromQuickCode(
514 mirror::ArtMethod* m, uint16_t vreg, uint64_t new_value, VRegKind kind_lo, VRegKind kind_hi) {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100515 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*));
516 DCHECK(code_pointer != nullptr);
517 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*)));
518 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer);
519 uint32_t vmap_offset_lo, vmap_offset_hi;
520 // TODO: IsInContext stops before spotting floating point registers.
521 if (vmap_table.IsInContext(vreg, kind_lo, &vmap_offset_lo) &&
522 vmap_table.IsInContext(vreg + 1, kind_hi, &vmap_offset_hi)) {
523 bool is_float = (kind_lo == kDoubleLoVReg);
524 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
525 uint32_t reg_lo = vmap_table.ComputeRegister(spill_mask, vmap_offset_lo, kind_lo);
526 uint32_t reg_hi = vmap_table.ComputeRegister(spill_mask, vmap_offset_hi, kind_hi);
527 return SetRegisterPairIfAccessible(reg_lo, reg_hi, new_value, is_float);
528 } else {
529 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700530 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100531 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000532 uint32_t* addr = GetVRegAddrFromQuickCode(
533 cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
534 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100535 *reinterpret_cast<uint64_t*>(addr) = new_value;
536 return true;
537 }
538}
539
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100540bool StackVisitor::SetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
541 uint64_t new_value, bool is_float) {
542 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
543 return false;
544 }
545 uintptr_t new_value_lo = static_cast<uintptr_t>(new_value & 0xFFFFFFFF);
546 uintptr_t new_value_hi = static_cast<uintptr_t>(new_value >> 32);
547 bool target64 = Is64BitInstructionSet(kRuntimeISA);
548 // Deal with 32 or 64-bit wide registers in a way that builds on all targets.
549 if (target64) {
550 DCHECK_EQ(reg_lo, reg_hi);
551 SetRegister(reg_lo, new_value, is_float);
552 } else {
553 SetRegister(reg_lo, new_value_lo, is_float);
554 SetRegister(reg_hi, new_value_hi, is_float);
555 }
556 return true;
557}
558
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100559bool StackVisitor::IsAccessibleGPR(uint32_t reg) const {
560 DCHECK(context_ != nullptr);
561 return context_->IsAccessibleGPR(reg);
562}
563
Mathieu Chartier815873e2014-02-13 18:02:13 -0800564uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100565 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
566 DCHECK(context_ != nullptr);
Mathieu Chartier815873e2014-02-13 18:02:13 -0800567 return context_->GetGPRAddress(reg);
568}
569
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100570uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
571 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
572 DCHECK(context_ != nullptr);
573 return context_->GetGPR(reg);
Ian Rogers0399dde2012-06-06 17:09:28 -0700574}
575
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100576void StackVisitor::SetGPR(uint32_t reg, uintptr_t value) {
577 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
578 DCHECK(context_ != nullptr);
579 context_->SetGPR(reg, value);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200580}
581
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100582bool StackVisitor::IsAccessibleFPR(uint32_t reg) const {
583 DCHECK(context_ != nullptr);
584 return context_->IsAccessibleFPR(reg);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200585}
586
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100587uintptr_t StackVisitor::GetFPR(uint32_t reg) const {
588 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
589 DCHECK(context_ != nullptr);
590 return context_->GetFPR(reg);
591}
592
593void StackVisitor::SetFPR(uint32_t reg, uintptr_t value) {
594 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
595 DCHECK(context_ != nullptr);
596 context_->SetFPR(reg, value);
Mathieu Chartier67022432012-11-29 18:04:50 -0800597}
598
Ian Rogers0399dde2012-06-06 17:09:28 -0700599uintptr_t StackVisitor::GetReturnPc() const {
Ian Rogers13735952014-10-08 12:43:28 -0700600 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700601 DCHECK(sp != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700602 uint8_t* pc_addr = sp + GetMethod()->GetReturnPcOffset().SizeValue();
Ian Rogers0399dde2012-06-06 17:09:28 -0700603 return *reinterpret_cast<uintptr_t*>(pc_addr);
604}
605
606void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
Ian Rogers13735952014-10-08 12:43:28 -0700607 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700608 CHECK(sp != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700609 uint8_t* pc_addr = sp + GetMethod()->GetReturnPcOffset().SizeValue();
Ian Rogers0399dde2012-06-06 17:09:28 -0700610 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc;
611}
612
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100613size_t StackVisitor::ComputeNumFrames(Thread* thread, StackWalkKind walk_kind) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700614 struct NumFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100615 NumFramesVisitor(Thread* thread_in, StackWalkKind walk_kind_in)
616 : StackVisitor(thread_in, nullptr, walk_kind_in), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700617
Ian Rogers5cf98192014-05-29 21:31:50 -0700618 bool VisitFrame() OVERRIDE {
Ian Rogers0399dde2012-06-06 17:09:28 -0700619 frames++;
620 return true;
621 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700622
Ian Rogers0399dde2012-06-06 17:09:28 -0700623 size_t frames;
624 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100625 NumFramesVisitor visitor(thread, walk_kind);
Ian Rogers0399dde2012-06-06 17:09:28 -0700626 visitor.WalkStack(true);
627 return visitor.frames;
628}
629
Ian Rogers5cf98192014-05-29 21:31:50 -0700630bool StackVisitor::GetNextMethodAndDexPc(mirror::ArtMethod** next_method, uint32_t* next_dex_pc) {
631 struct HasMoreFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100632 HasMoreFramesVisitor(Thread* thread,
633 StackWalkKind walk_kind,
634 size_t num_frames,
635 size_t frame_height)
636 : StackVisitor(thread, nullptr, walk_kind, num_frames),
637 frame_height_(frame_height),
638 found_frame_(false),
639 has_more_frames_(false),
640 next_method_(nullptr),
641 next_dex_pc_(0) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700642 }
643
644 bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
645 if (found_frame_) {
646 mirror::ArtMethod* method = GetMethod();
647 if (method != nullptr && !method->IsRuntimeMethod()) {
648 has_more_frames_ = true;
649 next_method_ = method;
650 next_dex_pc_ = GetDexPc();
651 return false; // End stack walk once next method is found.
652 }
653 } else if (GetFrameHeight() == frame_height_) {
654 found_frame_ = true;
655 }
656 return true;
657 }
658
659 size_t frame_height_;
660 bool found_frame_;
661 bool has_more_frames_;
662 mirror::ArtMethod* next_method_;
663 uint32_t next_dex_pc_;
664 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100665 HasMoreFramesVisitor visitor(thread_, walk_kind_, GetNumFrames(), GetFrameHeight());
Ian Rogers5cf98192014-05-29 21:31:50 -0700666 visitor.WalkStack(true);
667 *next_method = visitor.next_method_;
668 *next_dex_pc = visitor.next_dex_pc_;
669 return visitor.has_more_frames_;
670}
671
Ian Rogers7a22fa62013-01-23 12:16:16 -0800672void StackVisitor::DescribeStack(Thread* thread) {
Ian Rogers306057f2012-11-26 12:45:53 -0800673 struct DescribeStackVisitor : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800674 explicit DescribeStackVisitor(Thread* thread_in)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100675 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800676
Ian Rogers5cf98192014-05-29 21:31:50 -0700677 bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers306057f2012-11-26 12:45:53 -0800678 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation();
679 return true;
680 }
681 };
Ian Rogers7a22fa62013-01-23 12:16:16 -0800682 DescribeStackVisitor visitor(thread);
Ian Rogers306057f2012-11-26 12:45:53 -0800683 visitor.WalkStack(true);
684}
685
Ian Rogers40e3bac2012-11-20 00:09:14 -0800686std::string StackVisitor::DescribeLocation() const {
687 std::string result("Visiting method '");
Brian Carlstromea46f952013-07-30 01:26:50 -0700688 mirror::ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700689 if (m == nullptr) {
Ian Rogers306057f2012-11-26 12:45:53 -0800690 return "upcall";
691 }
692 result += PrettyMethod(m);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800693 result += StringPrintf("' at dex PC 0x%04x", GetDexPc());
Ian Rogers40e3bac2012-11-20 00:09:14 -0800694 if (!IsShadowFrame()) {
695 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc()));
696 }
697 return result;
698}
699
Ian Rogerse63db272014-07-15 15:36:11 -0700700static instrumentation::InstrumentationStackFrame& GetInstrumentationStackFrame(Thread* thread,
701 uint32_t depth) {
702 CHECK_LT(depth, thread->GetInstrumentationStack()->size());
703 return thread->GetInstrumentationStack()->at(depth);
Ian Rogers7a22fa62013-01-23 12:16:16 -0800704}
705
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700706void StackVisitor::SanityCheckFrame() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800707 if (kIsDebugBuild) {
708 mirror::ArtMethod* method = GetMethod();
Mathieu Chartier119c6bd2014-05-09 14:11:47 -0700709 CHECK_EQ(method->GetClass(), mirror::ArtMethod::GetJavaLangReflectArtMethod());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800710 if (cur_quick_frame_ != nullptr) {
711 method->AssertPcIsWithinQuickCode(cur_quick_frame_pc_);
712 // Frame sanity.
713 size_t frame_size = method->GetFrameSizeInBytes();
714 CHECK_NE(frame_size, 0u);
Andreas Gampe5b417b92014-03-10 14:18:35 -0700715 // A rough guess at an upper size we expect to see for a frame.
716 // 256 registers
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700717 // 2 words HandleScope overhead
Andreas Gampe5b417b92014-03-10 14:18:35 -0700718 // 3+3 register spills
719 // TODO: this seems architecture specific for the case of JNI frames.
Brian Carlstromed08bd42014-03-19 18:34:17 -0700720 // TODO: 083-compiler-regressions ManyFloatArgs shows this estimate is wrong.
721 // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word);
722 const size_t kMaxExpectedFrameSize = 2 * KB;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800723 CHECK_LE(frame_size, kMaxExpectedFrameSize);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700724 size_t return_pc_offset = method->GetReturnPcOffset().SizeValue();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800725 CHECK_LT(return_pc_offset, frame_size);
726 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700727 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700728}
729
730void StackVisitor::WalkStack(bool include_transitions) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800731 DCHECK(thread_ == Thread::Current() || thread_->IsSuspended());
Ian Rogers62d6c772013-02-27 08:32:07 -0800732 CHECK_EQ(cur_depth_, 0U);
733 bool exit_stubs_installed = Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled();
jeffhao725a9572012-11-13 18:20:12 -0800734 uint32_t instrumentation_stack_depth = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700735
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700736 for (const ManagedStack* current_fragment = thread_->GetManagedStack();
737 current_fragment != nullptr; current_fragment = current_fragment->GetLink()) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700738 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
739 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700740 cur_quick_frame_pc_ = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700741
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700742 if (cur_quick_frame_ != nullptr) { // Handle quick stack frames.
Ian Rogers0399dde2012-06-06 17:09:28 -0700743 // Can't be both a shadow and a quick fragment.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700744 DCHECK(current_fragment->GetTopShadowFrame() == nullptr);
Andreas Gampecf4035a2014-05-28 22:43:01 -0700745 mirror::ArtMethod* method = cur_quick_frame_->AsMirrorPtr();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700746 while (method != nullptr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700747 SanityCheckFrame();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100748
749 if ((walk_kind_ == StackWalkKind::kIncludeInlinedFrames)
750 && method->IsOptimized(sizeof(void*))) {
751 CodeInfo code_info = method->GetOptimizedCodeInfo();
752 uint32_t native_pc_offset = method->NativeQuickPcOffset(cur_quick_frame_pc_);
753 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100754 if (stack_map.IsValid() && stack_map.HasInlineInfo(code_info)) {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100755 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map);
756 DCHECK_EQ(current_inlining_depth_, 0u);
757 for (current_inlining_depth_ = inline_info.GetDepth();
758 current_inlining_depth_ != 0;
759 --current_inlining_depth_) {
760 bool should_continue = VisitFrame();
761 if (UNLIKELY(!should_continue)) {
762 return;
763 }
764 }
765 }
766 }
767
Dave Allison5cd33752014-04-15 15:57:58 -0700768 bool should_continue = VisitFrame();
769 if (UNLIKELY(!should_continue)) {
770 return;
Ian Rogers0399dde2012-06-06 17:09:28 -0700771 }
Dave Allison5cd33752014-04-15 15:57:58 -0700772
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700773 if (context_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700774 context_->FillCalleeSaves(*this);
775 }
776 size_t frame_size = method->GetFrameSizeInBytes();
777 // Compute PC for next stack frame from return PC.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700778 size_t return_pc_offset = method->GetReturnPcOffset(frame_size).SizeValue();
Ian Rogers13735952014-10-08 12:43:28 -0700779 uint8_t* return_pc_addr = reinterpret_cast<uint8_t*>(cur_quick_frame_) + return_pc_offset;
Ian Rogers0399dde2012-06-06 17:09:28 -0700780 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800781 if (UNLIKELY(exit_stubs_installed)) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700782 // While profiling, the return pc is restored from the side stack, except when walking
783 // the stack for an exception where the side stack will be unwound in VisitFrame.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700784 if (reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc) {
Sebastien Hertz74e256b2013-10-04 10:40:37 +0200785 const instrumentation::InstrumentationStackFrame& instrumentation_frame =
Ian Rogerse63db272014-07-15 15:36:11 -0700786 GetInstrumentationStackFrame(thread_, instrumentation_stack_depth);
jeffhao725a9572012-11-13 18:20:12 -0800787 instrumentation_stack_depth++;
Jeff Haofb2802d2013-07-24 13:53:05 -0700788 if (GetMethod() == Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveAll)) {
789 // Skip runtime save all callee frames which are used to deliver exceptions.
790 } else if (instrumentation_frame.interpreter_entry_) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700791 mirror::ArtMethod* callee =
792 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs);
Jeff Haofb2802d2013-07-24 13:53:05 -0700793 CHECK_EQ(GetMethod(), callee) << "Expected: " << PrettyMethod(callee) << " Found: "
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100794 << PrettyMethod(GetMethod());
Jeff Hao9a916d32013-06-27 18:45:37 -0700795 } else if (instrumentation_frame.method_ != GetMethod()) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800796 LOG(FATAL) << "Expected: " << PrettyMethod(instrumentation_frame.method_)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100797 << " Found: " << PrettyMethod(GetMethod());
Ian Rogers62d6c772013-02-27 08:32:07 -0800798 }
799 if (num_frames_ != 0) {
800 // Check agreement of frame Ids only if num_frames_ is computed to avoid infinite
801 // recursion.
802 CHECK(instrumentation_frame.frame_id_ == GetFrameId())
803 << "Expected: " << instrumentation_frame.frame_id_
804 << " Found: " << GetFrameId();
805 }
jeffhao725a9572012-11-13 18:20:12 -0800806 return_pc = instrumentation_frame.return_pc_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700807 }
808 }
809 cur_quick_frame_pc_ = return_pc;
Ian Rogers13735952014-10-08 12:43:28 -0700810 uint8_t* next_frame = reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size;
Andreas Gampecf4035a2014-05-28 22:43:01 -0700811 cur_quick_frame_ = reinterpret_cast<StackReference<mirror::ArtMethod>*>(next_frame);
Ian Rogers0399dde2012-06-06 17:09:28 -0700812 cur_depth_++;
Andreas Gampecf4035a2014-05-28 22:43:01 -0700813 method = cur_quick_frame_->AsMirrorPtr();
jeffhao6641ea12013-01-02 18:13:42 -0800814 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700815 } else if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700816 do {
817 SanityCheckFrame();
818 bool should_continue = VisitFrame();
819 if (UNLIKELY(!should_continue)) {
820 return;
821 }
822 cur_depth_++;
823 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700824 } while (cur_shadow_frame_ != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700825 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700826 if (include_transitions) {
827 bool should_continue = VisitFrame();
828 if (!should_continue) {
829 return;
830 }
831 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800832 cur_depth_++;
833 }
834 if (num_frames_ != 0) {
835 CHECK_EQ(cur_depth_, num_frames_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700836 }
837}
838
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800839void JavaFrameRootInfo::Describe(std::ostream& os) const {
840 const StackVisitor* visitor = stack_visitor_;
841 CHECK(visitor != nullptr);
842 os << "Type=" << GetType() << " thread_id=" << GetThreadId() << " location=" <<
843 visitor->DescribeLocation() << " vreg=" << vreg_;
844}
845
Elliott Hughes68e76522011-10-05 13:22:16 -0700846} // namespace art