blob: 09b56a118119c93d2bfb6855c51a1cb9cf7db09f [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"
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010021#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070022#include "entrypoints/runtime_asm_entrypoints.h"
Mathieu Chartier50030ef2015-05-08 14:19:26 -070023#include "gc_map.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070024#include "mirror/art_method-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "mirror/object.h"
27#include "mirror/object-inl.h"
28#include "mirror/object_array-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010029#include "quick/quick_method_frame_info.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070030#include "runtime.h"
Dave Allisonf9439142014-03-27 15:10:22 -070031#include "thread.h"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070032#include "thread_list.h"
Mathieu Chartier4e305412014-02-19 10:54:44 -080033#include "verify_object-inl.h"
Ian Rogers1809a722013-08-09 22:05:32 -070034#include "vmap_table.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070035
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080036namespace art {
37
Ian Rogers62d6c772013-02-27 08:32:07 -080038mirror::Object* ShadowFrame::GetThisObject() const {
Brian Carlstromea46f952013-07-30 01:26:50 -070039 mirror::ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -080040 if (m->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070041 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -080042 } else if (m->IsNative()) {
43 return GetVRegReference(0);
44 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070045 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070046 CHECK(code_item != nullptr) << PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -080047 uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
48 return GetVRegReference(reg);
49 }
50}
51
Jeff Haoe701f482013-05-24 11:50:49 -070052mirror::Object* ShadowFrame::GetThisObject(uint16_t num_ins) const {
Brian Carlstromea46f952013-07-30 01:26:50 -070053 mirror::ArtMethod* m = GetMethod();
Jeff Haoe701f482013-05-24 11:50:49 -070054 if (m->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070055 return nullptr;
Jeff Haoe701f482013-05-24 11:50:49 -070056 } else {
Jeff Hao8d448852013-06-03 17:26:19 -070057 return GetVRegReference(NumberOfVRegs() - num_ins);
Jeff Haoe701f482013-05-24 11:50:49 -070058 }
59}
60
TDYa127ce4cc0d2012-11-18 16:59:53 -080061size_t ManagedStack::NumJniShadowFrameReferences() const {
Ian Rogers0399dde2012-06-06 17:09:28 -070062 size_t count = 0;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070063 for (const ManagedStack* current_fragment = this; current_fragment != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070064 current_fragment = current_fragment->GetLink()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070065 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070066 current_frame = current_frame->GetLink()) {
TDYa127ce4cc0d2012-11-18 16:59:53 -080067 if (current_frame->GetMethod()->IsNative()) {
68 // The JNI ShadowFrame only contains references. (For indirect reference.)
69 count += current_frame->NumberOfVRegs();
70 }
Ian Rogers0399dde2012-06-06 17:09:28 -070071 }
72 }
73 return count;
74}
75
Ian Rogersef7d42f2014-01-06 12:55:46 -080076bool ManagedStack::ShadowFramesContain(StackReference<mirror::Object>* shadow_frame_entry) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070077 for (const ManagedStack* current_fragment = this; current_fragment != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070078 current_fragment = current_fragment->GetLink()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070079 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070080 current_frame = current_frame->GetLink()) {
81 if (current_frame->Contains(shadow_frame_entry)) {
82 return true;
83 }
84 }
85 }
86 return false;
87}
88
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010089StackVisitor::StackVisitor(Thread* thread, Context* context, StackWalkKind walk_kind)
90 : StackVisitor(thread, context, walk_kind, 0) {}
Ian Rogers7a22fa62013-01-23 12:16:16 -080091
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010092StackVisitor::StackVisitor(Thread* thread,
93 Context* context,
94 StackWalkKind walk_kind,
95 size_t num_frames)
96 : thread_(thread),
97 walk_kind_(walk_kind),
98 cur_shadow_frame_(nullptr),
99 cur_quick_frame_(nullptr),
100 cur_quick_frame_pc_(0),
101 num_frames_(num_frames),
102 cur_depth_(0),
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100103 current_inlining_depth_(0),
Ian Rogers5cf98192014-05-29 21:31:50 -0700104 context_(context) {
105 DCHECK(thread == Thread::Current() || thread->IsSuspended()) << *thread;
106}
107
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100108InlineInfo StackVisitor::GetCurrentInlineInfo() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
109 mirror::ArtMethod* outer_method = GetCurrentQuickFrame()->AsMirrorPtr();
110 uint32_t native_pc_offset = outer_method->NativeQuickPcOffset(cur_quick_frame_pc_);
111 CodeInfo code_info = outer_method->GetOptimizedCodeInfo();
112 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100113 DCHECK(stack_map.IsValid());
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100114 return code_info.GetInlineInfoOf(stack_map);
115}
116
117mirror::ArtMethod* StackVisitor::GetMethod() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
118 if (cur_shadow_frame_ != nullptr) {
119 return cur_shadow_frame_->GetMethod();
120 } else if (cur_quick_frame_ != nullptr) {
121 if (IsInInlinedFrame()) {
122 size_t depth_in_stack_map = current_inlining_depth_ - 1;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100123 InlineInfo inline_info = GetCurrentInlineInfo();
124 uint32_t method_index = inline_info.GetMethodIndexAtDepth(depth_in_stack_map);
125 InvokeType invoke_type =
126 static_cast<InvokeType>(inline_info.GetInvokeTypeAtDepth(depth_in_stack_map));
127 return GetResolvedMethod(GetCurrentQuickFrame()->AsMirrorPtr(), method_index, invoke_type);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100128 } else {
129 return cur_quick_frame_->AsMirrorPtr();
130 }
131 } else {
132 return nullptr;
133 }
134}
135
Dave Allisonb373e092014-02-20 16:06:36 -0800136uint32_t StackVisitor::GetDexPc(bool abort_on_failure) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700137 if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700138 return cur_shadow_frame_->GetDexPC();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700139 } else if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100140 if (IsInInlinedFrame()) {
141 size_t depth_in_stack_map = current_inlining_depth_ - 1;
142 return GetCurrentInlineInfo().GetDexPcAtDepth(depth_in_stack_map);
143 } else {
144 return GetMethod()->ToDexPc(cur_quick_frame_pc_, abort_on_failure);
145 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700146 } else {
147 return 0;
148 }
149}
150
Sebastien Hertza836bc92014-11-25 16:30:53 +0100151extern "C" mirror::Object* artQuickGetProxyThisObject(StackReference<mirror::ArtMethod>* sp)
152 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
153
Ian Rogers62d6c772013-02-27 08:32:07 -0800154mirror::Object* StackVisitor::GetThisObject() const {
Brian Carlstromea46f952013-07-30 01:26:50 -0700155 mirror::ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800156 if (m->IsStatic()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100157 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800158 } else if (m->IsNative()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100159 if (cur_quick_frame_ != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700160 HandleScope* hs = reinterpret_cast<HandleScope*>(
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700161 reinterpret_cast<char*>(cur_quick_frame_) + m->GetHandleScopeOffset().SizeValue());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700162 return hs->GetReference(0);
Ian Rogers62d6c772013-02-27 08:32:07 -0800163 } else {
164 return cur_shadow_frame_->GetVRegReference(0);
165 }
Sebastien Hertza836bc92014-11-25 16:30:53 +0100166 } else if (m->IsProxyMethod()) {
167 if (cur_quick_frame_ != nullptr) {
168 return artQuickGetProxyThisObject(cur_quick_frame_);
169 } else {
170 return cur_shadow_frame_->GetVRegReference(0);
171 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800172 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700173 const DexFile::CodeItem* code_item = m->GetCodeItem();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100174 if (code_item == nullptr) {
Ian Rogerse0dcd462014-03-08 15:21:04 -0800175 UNIMPLEMENTED(ERROR) << "Failed to determine this object of abstract or proxy method: "
Ian Rogers62d6c772013-02-27 08:32:07 -0800176 << PrettyMethod(m);
Ian Rogerse0dcd462014-03-08 15:21:04 -0800177 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800178 } else {
179 uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000180 uint32_t value = 0;
181 bool success = GetVReg(m, reg, kReferenceVReg, &value);
182 // We currently always guarantee the `this` object is live throughout the method.
183 CHECK(success) << "Failed to read the this object in " << PrettyMethod(m);
184 return reinterpret_cast<mirror::Object*>(value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800185 }
186 }
187}
188
Ian Rogers0c7abda2012-09-19 13:33:42 -0700189size_t StackVisitor::GetNativePcOffset() const {
190 DCHECK(!IsShadowFrame());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700191 return GetMethod()->NativeQuickPcOffset(cur_quick_frame_pc_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700192}
193
Mathieu Chartier50030ef2015-05-08 14:19:26 -0700194bool StackVisitor::IsReferenceVReg(mirror::ArtMethod* m, uint16_t vreg) {
195 // Process register map (which native and runtime methods don't have)
196 if (m->IsNative() || m->IsRuntimeMethod() || m->IsProxyMethod()) {
197 return false;
198 }
199 if (m->IsOptimized(sizeof(void*))) {
200 return true; // TODO: Implement.
201 }
202 const uint8_t* native_gc_map = m->GetNativeGcMap(sizeof(void*));
203 CHECK(native_gc_map != nullptr) << PrettyMethod(m);
204 const DexFile::CodeItem* code_item = m->GetCodeItem();
205 // Can't be null or how would we compile its instructions?
206 DCHECK(code_item != nullptr) << PrettyMethod(m);
207 NativePcOffsetToReferenceMap map(native_gc_map);
208 size_t num_regs = std::min(map.RegWidth() * 8, static_cast<size_t>(code_item->registers_size_));
209 const uint8_t* reg_bitmap = nullptr;
210 if (num_regs > 0) {
211 Runtime* runtime = Runtime::Current();
212 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(m, sizeof(void*));
213 uintptr_t native_pc_offset = m->NativeQuickPcOffset(GetCurrentQuickFramePc(), entry_point);
214 reg_bitmap = map.FindBitMap(native_pc_offset);
215 DCHECK(reg_bitmap != nullptr);
216 }
217 // Does this register hold a reference?
218 return vreg < num_regs && TestBitmap(vreg, reg_bitmap);
219}
220
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200221bool StackVisitor::GetVReg(mirror::ArtMethod* m, uint16_t vreg, VRegKind kind,
222 uint32_t* val) const {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200223 if (cur_quick_frame_ != nullptr) {
224 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800225 DCHECK(m == GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100226 if (m->IsOptimized(sizeof(void*))) {
227 return GetVRegFromOptimizedCode(m, vreg, kind, val);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700228 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100229 return GetVRegFromQuickCode(m, vreg, kind, val);
Ian Rogers0399dde2012-06-06 17:09:28 -0700230 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700231 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100232 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200233 *val = cur_shadow_frame_->GetVReg(vreg);
234 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700235 }
236}
237
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100238bool StackVisitor::GetVRegFromQuickCode(mirror::ArtMethod* m, uint16_t vreg, VRegKind kind,
239 uint32_t* val) const {
240 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*));
241 DCHECK(code_pointer != nullptr);
242 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*)));
243 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer);
244 uint32_t vmap_offset;
245 // TODO: IsInContext stops before spotting floating point registers.
246 if (vmap_table.IsInContext(vreg, kind, &vmap_offset)) {
247 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
248 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
249 uint32_t reg = vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
250 return GetRegisterIfAccessible(reg, kind, val);
251 } else {
252 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700253 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100254 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000255 *val = *GetVRegAddrFromQuickCode(cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
256 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100257 return true;
258 }
259}
260
261bool StackVisitor::GetVRegFromOptimizedCode(mirror::ArtMethod* m, uint16_t vreg, VRegKind kind,
262 uint32_t* val) const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100263 DCHECK_EQ(m, GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100264 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700265 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100266 // its instructions?
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000267 uint16_t number_of_dex_registers = code_item->registers_size_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100268 DCHECK_LT(vreg, code_item->registers_size_);
269
270 mirror::ArtMethod* outer_method = GetCurrentQuickFrame()->AsMirrorPtr();
271 const void* code_pointer = outer_method->GetQuickOatCodePointer(sizeof(void*));
272 DCHECK(code_pointer != nullptr);
273 CodeInfo code_info = outer_method->GetOptimizedCodeInfo();
274
275 uint32_t native_pc_offset = outer_method->NativeQuickPcOffset(cur_quick_frame_pc_);
276 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100277 DCHECK(stack_map.IsValid());
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100278 size_t depth_in_stack_map = current_inlining_depth_ - 1;
279
280 DexRegisterMap dex_register_map = IsInInlinedFrame()
281 ? code_info.GetDexRegisterMapAtDepth(
282 depth_in_stack_map, code_info.GetInlineInfoOf(stack_map), number_of_dex_registers)
283 : code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers);
284
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000285 DexRegisterLocation::Kind location_kind =
Roland Levillaina552e1c2015-03-26 15:01:03 +0000286 dex_register_map.GetLocationKind(vreg, number_of_dex_registers, code_info);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100287 switch (location_kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000288 case DexRegisterLocation::Kind::kInStack: {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000289 const int32_t offset =
290 dex_register_map.GetStackOffsetInBytes(vreg, number_of_dex_registers, code_info);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100291 const uint8_t* addr = reinterpret_cast<const uint8_t*>(cur_quick_frame_) + offset;
292 *val = *reinterpret_cast<const uint32_t*>(addr);
293 return true;
294 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000295 case DexRegisterLocation::Kind::kInRegister:
296 case DexRegisterLocation::Kind::kInFpuRegister: {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000297 uint32_t reg = dex_register_map.GetMachineRegister(vreg, number_of_dex_registers, code_info);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100298 return GetRegisterIfAccessible(reg, kind, val);
299 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000300 case DexRegisterLocation::Kind::kConstant:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000301 *val = dex_register_map.GetConstant(vreg, number_of_dex_registers, code_info);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100302 return true;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000303 case DexRegisterLocation::Kind::kNone:
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100304 return false;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000305 default:
306 LOG(FATAL)
307 << "Unexpected location kind"
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000308 << DexRegisterLocation::PrettyDescriptor(
Roland Levillaina552e1c2015-03-26 15:01:03 +0000309 dex_register_map.GetLocationInternalKind(vreg, number_of_dex_registers, code_info));
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000310 UNREACHABLE();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100311 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100312}
313
314bool StackVisitor::GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const {
315 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
316 if (!IsAccessibleRegister(reg, is_float)) {
317 return false;
318 }
319 uintptr_t ptr_val = GetRegister(reg, is_float);
320 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
321 if (target64) {
322 const bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
323 const bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
324 int64_t value_long = static_cast<int64_t>(ptr_val);
325 if (wide_lo) {
326 ptr_val = static_cast<uintptr_t>(Low32Bits(value_long));
327 } else if (wide_hi) {
328 ptr_val = static_cast<uintptr_t>(High32Bits(value_long));
329 }
330 }
331 *val = ptr_val;
332 return true;
333}
334
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200335bool StackVisitor::GetVRegPair(mirror::ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
336 VRegKind kind_hi, uint64_t* val) const {
337 if (kind_lo == kLongLoVReg) {
338 DCHECK_EQ(kind_hi, kLongHiVReg);
339 } else if (kind_lo == kDoubleLoVReg) {
340 DCHECK_EQ(kind_hi, kDoubleHiVReg);
341 } else {
342 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100343 UNREACHABLE();
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200344 }
345 if (cur_quick_frame_ != nullptr) {
346 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
347 DCHECK(m == GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100348 if (m->IsOptimized(sizeof(void*))) {
349 return GetVRegPairFromOptimizedCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200350 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100351 return GetVRegPairFromQuickCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200352 }
353 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100354 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200355 *val = cur_shadow_frame_->GetVRegLong(vreg);
356 return true;
357 }
358}
359
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100360bool StackVisitor::GetVRegPairFromQuickCode(mirror::ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
361 VRegKind kind_hi, uint64_t* val) const {
362 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*));
363 DCHECK(code_pointer != nullptr);
364 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*)));
365 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer);
366 uint32_t vmap_offset_lo, vmap_offset_hi;
367 // TODO: IsInContext stops before spotting floating point registers.
368 if (vmap_table.IsInContext(vreg, kind_lo, &vmap_offset_lo) &&
369 vmap_table.IsInContext(vreg + 1, kind_hi, &vmap_offset_hi)) {
370 bool is_float = (kind_lo == kDoubleLoVReg);
371 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
372 uint32_t reg_lo = vmap_table.ComputeRegister(spill_mask, vmap_offset_lo, kind_lo);
373 uint32_t reg_hi = vmap_table.ComputeRegister(spill_mask, vmap_offset_hi, kind_hi);
374 return GetRegisterPairIfAccessible(reg_lo, reg_hi, kind_lo, val);
375 } else {
376 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700377 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100378 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000379 uint32_t* addr = GetVRegAddrFromQuickCode(
380 cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
381 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100382 *val = *reinterpret_cast<uint64_t*>(addr);
383 return true;
384 }
385}
386
387bool StackVisitor::GetVRegPairFromOptimizedCode(mirror::ArtMethod* m, uint16_t vreg,
388 VRegKind kind_lo, VRegKind kind_hi,
389 uint64_t* val) const {
390 uint32_t low_32bits;
391 uint32_t high_32bits;
392 bool success = GetVRegFromOptimizedCode(m, vreg, kind_lo, &low_32bits);
393 success &= GetVRegFromOptimizedCode(m, vreg + 1, kind_hi, &high_32bits);
394 if (success) {
395 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
396 }
397 return success;
398}
399
400bool StackVisitor::GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
401 VRegKind kind_lo, uint64_t* val) const {
402 const bool is_float = (kind_lo == kDoubleLoVReg);
403 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
404 return false;
405 }
406 uintptr_t ptr_val_lo = GetRegister(reg_lo, is_float);
407 uintptr_t ptr_val_hi = GetRegister(reg_hi, is_float);
408 bool target64 = Is64BitInstructionSet(kRuntimeISA);
409 if (target64) {
410 int64_t value_long_lo = static_cast<int64_t>(ptr_val_lo);
411 int64_t value_long_hi = static_cast<int64_t>(ptr_val_hi);
412 ptr_val_lo = static_cast<uintptr_t>(Low32Bits(value_long_lo));
413 ptr_val_hi = static_cast<uintptr_t>(High32Bits(value_long_hi));
414 }
415 *val = (static_cast<uint64_t>(ptr_val_hi) << 32) | static_cast<uint32_t>(ptr_val_lo);
416 return true;
417}
418
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200419bool StackVisitor::SetVReg(mirror::ArtMethod* m, uint16_t vreg, uint32_t new_value,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800420 VRegKind kind) {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200421 if (cur_quick_frame_ != nullptr) {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100422 DCHECK(context_ != nullptr); // You can't reliably write registers without a context.
423 DCHECK(m == GetMethod());
424 if (m->IsOptimized(sizeof(void*))) {
Nicolas Geoffray7cc56a12015-04-24 14:58:19 +0100425 return false;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100426 } else {
427 return SetVRegFromQuickCode(m, vreg, new_value, kind);
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100428 }
Mathieu Chartier67022432012-11-29 18:04:50 -0800429 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100430 cur_shadow_frame_->SetVReg(vreg, new_value);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200431 return true;
Ian Rogers0ec569a2012-07-01 16:43:46 -0700432 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100433}
434
435bool StackVisitor::SetVRegFromQuickCode(mirror::ArtMethod* m, uint16_t vreg, uint32_t new_value,
436 VRegKind kind) {
437 DCHECK(context_ != nullptr); // You can't reliably write registers without a context.
438 DCHECK(m == GetMethod());
439 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*));
440 DCHECK(code_pointer != nullptr);
441 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*)));
442 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer);
443 uint32_t vmap_offset;
444 // TODO: IsInContext stops before spotting floating point registers.
445 if (vmap_table.IsInContext(vreg, kind, &vmap_offset)) {
446 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
447 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
448 uint32_t reg = vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
449 return SetRegisterIfAccessible(reg, new_value, kind);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700450 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100451 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700452 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100453 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000454 uint32_t* addr = GetVRegAddrFromQuickCode(
455 cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
456 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100457 *addr = new_value;
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200458 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700459 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700460}
461
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100462bool StackVisitor::SetRegisterIfAccessible(uint32_t reg, uint32_t new_value, VRegKind kind) {
463 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
464 if (!IsAccessibleRegister(reg, is_float)) {
465 return false;
466 }
467 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
468
469 // Create a new value that can hold both low 32 and high 32 bits, in
470 // case we are running 64 bits.
471 uintptr_t full_new_value = new_value;
472 // Deal with 32 or 64-bit wide registers in a way that builds on all targets.
473 if (target64) {
474 bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
475 bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
476 if (wide_lo || wide_hi) {
477 uintptr_t old_reg_val = GetRegister(reg, is_float);
478 uint64_t new_vreg_portion = static_cast<uint64_t>(new_value);
479 uint64_t old_reg_val_as_wide = static_cast<uint64_t>(old_reg_val);
480 uint64_t mask = 0xffffffff;
481 if (wide_lo) {
482 mask = mask << 32;
483 } else {
484 new_vreg_portion = new_vreg_portion << 32;
485 }
486 full_new_value = static_cast<uintptr_t>((old_reg_val_as_wide & mask) | new_vreg_portion);
487 }
488 }
489 SetRegister(reg, full_new_value, is_float);
490 return true;
491}
492
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200493bool StackVisitor::SetVRegPair(mirror::ArtMethod* m, uint16_t vreg, uint64_t new_value,
494 VRegKind kind_lo, VRegKind kind_hi) {
495 if (kind_lo == kLongLoVReg) {
496 DCHECK_EQ(kind_hi, kLongHiVReg);
497 } else if (kind_lo == kDoubleLoVReg) {
498 DCHECK_EQ(kind_hi, kDoubleHiVReg);
499 } else {
500 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
501 }
502 if (cur_quick_frame_ != nullptr) {
503 DCHECK(context_ != nullptr); // You can't reliably write registers without a context.
504 DCHECK(m == GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100505 if (m->IsOptimized(sizeof(void*))) {
Nicolas Geoffray7cc56a12015-04-24 14:58:19 +0100506 return false;
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200507 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100508 return SetVRegPairFromQuickCode(m, vreg, new_value, kind_lo, kind_hi);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200509 }
510 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100511 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200512 cur_shadow_frame_->SetVRegLong(vreg, new_value);
513 return true;
514 }
515}
516
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700517bool StackVisitor::SetVRegPairFromQuickCode(
518 mirror::ArtMethod* m, uint16_t vreg, uint64_t new_value, VRegKind kind_lo, VRegKind kind_hi) {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100519 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*));
520 DCHECK(code_pointer != nullptr);
521 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*)));
522 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer);
523 uint32_t vmap_offset_lo, vmap_offset_hi;
524 // TODO: IsInContext stops before spotting floating point registers.
525 if (vmap_table.IsInContext(vreg, kind_lo, &vmap_offset_lo) &&
526 vmap_table.IsInContext(vreg + 1, kind_hi, &vmap_offset_hi)) {
527 bool is_float = (kind_lo == kDoubleLoVReg);
528 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
529 uint32_t reg_lo = vmap_table.ComputeRegister(spill_mask, vmap_offset_lo, kind_lo);
530 uint32_t reg_hi = vmap_table.ComputeRegister(spill_mask, vmap_offset_hi, kind_hi);
531 return SetRegisterPairIfAccessible(reg_lo, reg_hi, new_value, is_float);
532 } else {
533 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700534 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100535 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000536 uint32_t* addr = GetVRegAddrFromQuickCode(
537 cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
538 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100539 *reinterpret_cast<uint64_t*>(addr) = new_value;
540 return true;
541 }
542}
543
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100544bool StackVisitor::SetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
545 uint64_t new_value, bool is_float) {
546 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
547 return false;
548 }
549 uintptr_t new_value_lo = static_cast<uintptr_t>(new_value & 0xFFFFFFFF);
550 uintptr_t new_value_hi = static_cast<uintptr_t>(new_value >> 32);
551 bool target64 = Is64BitInstructionSet(kRuntimeISA);
552 // Deal with 32 or 64-bit wide registers in a way that builds on all targets.
553 if (target64) {
554 DCHECK_EQ(reg_lo, reg_hi);
555 SetRegister(reg_lo, new_value, is_float);
556 } else {
557 SetRegister(reg_lo, new_value_lo, is_float);
558 SetRegister(reg_hi, new_value_hi, is_float);
559 }
560 return true;
561}
562
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100563bool StackVisitor::IsAccessibleGPR(uint32_t reg) const {
564 DCHECK(context_ != nullptr);
565 return context_->IsAccessibleGPR(reg);
566}
567
Mathieu Chartier815873e2014-02-13 18:02:13 -0800568uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100569 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
570 DCHECK(context_ != nullptr);
Mathieu Chartier815873e2014-02-13 18:02:13 -0800571 return context_->GetGPRAddress(reg);
572}
573
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100574uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
575 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
576 DCHECK(context_ != nullptr);
577 return context_->GetGPR(reg);
Ian Rogers0399dde2012-06-06 17:09:28 -0700578}
579
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100580void StackVisitor::SetGPR(uint32_t reg, uintptr_t value) {
581 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
582 DCHECK(context_ != nullptr);
583 context_->SetGPR(reg, value);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200584}
585
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100586bool StackVisitor::IsAccessibleFPR(uint32_t reg) const {
587 DCHECK(context_ != nullptr);
588 return context_->IsAccessibleFPR(reg);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200589}
590
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100591uintptr_t StackVisitor::GetFPR(uint32_t reg) const {
592 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
593 DCHECK(context_ != nullptr);
594 return context_->GetFPR(reg);
595}
596
597void StackVisitor::SetFPR(uint32_t reg, uintptr_t value) {
598 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
599 DCHECK(context_ != nullptr);
600 context_->SetFPR(reg, value);
Mathieu Chartier67022432012-11-29 18:04:50 -0800601}
602
Ian Rogers0399dde2012-06-06 17:09:28 -0700603uintptr_t StackVisitor::GetReturnPc() const {
Ian Rogers13735952014-10-08 12:43:28 -0700604 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700605 DCHECK(sp != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700606 uint8_t* pc_addr = sp + GetMethod()->GetReturnPcOffset().SizeValue();
Ian Rogers0399dde2012-06-06 17:09:28 -0700607 return *reinterpret_cast<uintptr_t*>(pc_addr);
608}
609
610void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
Ian Rogers13735952014-10-08 12:43:28 -0700611 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700612 CHECK(sp != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700613 uint8_t* pc_addr = sp + GetMethod()->GetReturnPcOffset().SizeValue();
Ian Rogers0399dde2012-06-06 17:09:28 -0700614 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc;
615}
616
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100617size_t StackVisitor::ComputeNumFrames(Thread* thread, StackWalkKind walk_kind) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700618 struct NumFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100619 NumFramesVisitor(Thread* thread_in, StackWalkKind walk_kind_in)
620 : StackVisitor(thread_in, nullptr, walk_kind_in), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700621
Ian Rogers5cf98192014-05-29 21:31:50 -0700622 bool VisitFrame() OVERRIDE {
Ian Rogers0399dde2012-06-06 17:09:28 -0700623 frames++;
624 return true;
625 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700626
Ian Rogers0399dde2012-06-06 17:09:28 -0700627 size_t frames;
628 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100629 NumFramesVisitor visitor(thread, walk_kind);
Ian Rogers0399dde2012-06-06 17:09:28 -0700630 visitor.WalkStack(true);
631 return visitor.frames;
632}
633
Ian Rogers5cf98192014-05-29 21:31:50 -0700634bool StackVisitor::GetNextMethodAndDexPc(mirror::ArtMethod** next_method, uint32_t* next_dex_pc) {
635 struct HasMoreFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100636 HasMoreFramesVisitor(Thread* thread,
637 StackWalkKind walk_kind,
638 size_t num_frames,
639 size_t frame_height)
640 : StackVisitor(thread, nullptr, walk_kind, num_frames),
641 frame_height_(frame_height),
642 found_frame_(false),
643 has_more_frames_(false),
644 next_method_(nullptr),
645 next_dex_pc_(0) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700646 }
647
648 bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
649 if (found_frame_) {
650 mirror::ArtMethod* method = GetMethod();
651 if (method != nullptr && !method->IsRuntimeMethod()) {
652 has_more_frames_ = true;
653 next_method_ = method;
654 next_dex_pc_ = GetDexPc();
655 return false; // End stack walk once next method is found.
656 }
657 } else if (GetFrameHeight() == frame_height_) {
658 found_frame_ = true;
659 }
660 return true;
661 }
662
663 size_t frame_height_;
664 bool found_frame_;
665 bool has_more_frames_;
666 mirror::ArtMethod* next_method_;
667 uint32_t next_dex_pc_;
668 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100669 HasMoreFramesVisitor visitor(thread_, walk_kind_, GetNumFrames(), GetFrameHeight());
Ian Rogers5cf98192014-05-29 21:31:50 -0700670 visitor.WalkStack(true);
671 *next_method = visitor.next_method_;
672 *next_dex_pc = visitor.next_dex_pc_;
673 return visitor.has_more_frames_;
674}
675
Ian Rogers7a22fa62013-01-23 12:16:16 -0800676void StackVisitor::DescribeStack(Thread* thread) {
Ian Rogers306057f2012-11-26 12:45:53 -0800677 struct DescribeStackVisitor : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800678 explicit DescribeStackVisitor(Thread* thread_in)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100679 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800680
Ian Rogers5cf98192014-05-29 21:31:50 -0700681 bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers306057f2012-11-26 12:45:53 -0800682 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation();
683 return true;
684 }
685 };
Ian Rogers7a22fa62013-01-23 12:16:16 -0800686 DescribeStackVisitor visitor(thread);
Ian Rogers306057f2012-11-26 12:45:53 -0800687 visitor.WalkStack(true);
688}
689
Ian Rogers40e3bac2012-11-20 00:09:14 -0800690std::string StackVisitor::DescribeLocation() const {
691 std::string result("Visiting method '");
Brian Carlstromea46f952013-07-30 01:26:50 -0700692 mirror::ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700693 if (m == nullptr) {
Ian Rogers306057f2012-11-26 12:45:53 -0800694 return "upcall";
695 }
696 result += PrettyMethod(m);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800697 result += StringPrintf("' at dex PC 0x%04x", GetDexPc());
Ian Rogers40e3bac2012-11-20 00:09:14 -0800698 if (!IsShadowFrame()) {
699 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc()));
700 }
701 return result;
702}
703
Ian Rogerse63db272014-07-15 15:36:11 -0700704static instrumentation::InstrumentationStackFrame& GetInstrumentationStackFrame(Thread* thread,
705 uint32_t depth) {
706 CHECK_LT(depth, thread->GetInstrumentationStack()->size());
707 return thread->GetInstrumentationStack()->at(depth);
Ian Rogers7a22fa62013-01-23 12:16:16 -0800708}
709
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700710void StackVisitor::SanityCheckFrame() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800711 if (kIsDebugBuild) {
712 mirror::ArtMethod* method = GetMethod();
Mathieu Chartier119c6bd2014-05-09 14:11:47 -0700713 CHECK_EQ(method->GetClass(), mirror::ArtMethod::GetJavaLangReflectArtMethod());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800714 if (cur_quick_frame_ != nullptr) {
715 method->AssertPcIsWithinQuickCode(cur_quick_frame_pc_);
716 // Frame sanity.
717 size_t frame_size = method->GetFrameSizeInBytes();
718 CHECK_NE(frame_size, 0u);
Andreas Gampe5b417b92014-03-10 14:18:35 -0700719 // A rough guess at an upper size we expect to see for a frame.
720 // 256 registers
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700721 // 2 words HandleScope overhead
Andreas Gampe5b417b92014-03-10 14:18:35 -0700722 // 3+3 register spills
723 // TODO: this seems architecture specific for the case of JNI frames.
Brian Carlstromed08bd42014-03-19 18:34:17 -0700724 // TODO: 083-compiler-regressions ManyFloatArgs shows this estimate is wrong.
725 // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word);
726 const size_t kMaxExpectedFrameSize = 2 * KB;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800727 CHECK_LE(frame_size, kMaxExpectedFrameSize);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700728 size_t return_pc_offset = method->GetReturnPcOffset().SizeValue();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800729 CHECK_LT(return_pc_offset, frame_size);
730 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700731 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700732}
733
734void StackVisitor::WalkStack(bool include_transitions) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800735 DCHECK(thread_ == Thread::Current() || thread_->IsSuspended());
Ian Rogers62d6c772013-02-27 08:32:07 -0800736 CHECK_EQ(cur_depth_, 0U);
737 bool exit_stubs_installed = Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled();
jeffhao725a9572012-11-13 18:20:12 -0800738 uint32_t instrumentation_stack_depth = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700739
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700740 for (const ManagedStack* current_fragment = thread_->GetManagedStack();
741 current_fragment != nullptr; current_fragment = current_fragment->GetLink()) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700742 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
743 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700744 cur_quick_frame_pc_ = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700745
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700746 if (cur_quick_frame_ != nullptr) { // Handle quick stack frames.
Ian Rogers0399dde2012-06-06 17:09:28 -0700747 // Can't be both a shadow and a quick fragment.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700748 DCHECK(current_fragment->GetTopShadowFrame() == nullptr);
Andreas Gampecf4035a2014-05-28 22:43:01 -0700749 mirror::ArtMethod* method = cur_quick_frame_->AsMirrorPtr();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700750 while (method != nullptr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700751 SanityCheckFrame();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100752
753 if ((walk_kind_ == StackWalkKind::kIncludeInlinedFrames)
754 && method->IsOptimized(sizeof(void*))) {
755 CodeInfo code_info = method->GetOptimizedCodeInfo();
756 uint32_t native_pc_offset = method->NativeQuickPcOffset(cur_quick_frame_pc_);
757 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100758 if (stack_map.IsValid() && stack_map.HasInlineInfo(code_info)) {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100759 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map);
760 DCHECK_EQ(current_inlining_depth_, 0u);
761 for (current_inlining_depth_ = inline_info.GetDepth();
762 current_inlining_depth_ != 0;
763 --current_inlining_depth_) {
764 bool should_continue = VisitFrame();
765 if (UNLIKELY(!should_continue)) {
766 return;
767 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100768 cur_depth_++;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100769 }
770 }
771 }
772
Dave Allison5cd33752014-04-15 15:57:58 -0700773 bool should_continue = VisitFrame();
774 if (UNLIKELY(!should_continue)) {
775 return;
Ian Rogers0399dde2012-06-06 17:09:28 -0700776 }
Dave Allison5cd33752014-04-15 15:57:58 -0700777
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700778 if (context_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700779 context_->FillCalleeSaves(*this);
780 }
781 size_t frame_size = method->GetFrameSizeInBytes();
782 // Compute PC for next stack frame from return PC.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700783 size_t return_pc_offset = method->GetReturnPcOffset(frame_size).SizeValue();
Ian Rogers13735952014-10-08 12:43:28 -0700784 uint8_t* return_pc_addr = reinterpret_cast<uint8_t*>(cur_quick_frame_) + return_pc_offset;
Ian Rogers0399dde2012-06-06 17:09:28 -0700785 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800786 if (UNLIKELY(exit_stubs_installed)) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700787 // While profiling, the return pc is restored from the side stack, except when walking
788 // the stack for an exception where the side stack will be unwound in VisitFrame.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700789 if (reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc) {
Sebastien Hertz74e256b2013-10-04 10:40:37 +0200790 const instrumentation::InstrumentationStackFrame& instrumentation_frame =
Ian Rogerse63db272014-07-15 15:36:11 -0700791 GetInstrumentationStackFrame(thread_, instrumentation_stack_depth);
jeffhao725a9572012-11-13 18:20:12 -0800792 instrumentation_stack_depth++;
Jeff Haofb2802d2013-07-24 13:53:05 -0700793 if (GetMethod() == Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveAll)) {
794 // Skip runtime save all callee frames which are used to deliver exceptions.
795 } else if (instrumentation_frame.interpreter_entry_) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700796 mirror::ArtMethod* callee =
797 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs);
Jeff Haofb2802d2013-07-24 13:53:05 -0700798 CHECK_EQ(GetMethod(), callee) << "Expected: " << PrettyMethod(callee) << " Found: "
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100799 << PrettyMethod(GetMethod());
Jeff Hao9a916d32013-06-27 18:45:37 -0700800 } else if (instrumentation_frame.method_ != GetMethod()) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800801 LOG(FATAL) << "Expected: " << PrettyMethod(instrumentation_frame.method_)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100802 << " Found: " << PrettyMethod(GetMethod());
Ian Rogers62d6c772013-02-27 08:32:07 -0800803 }
804 if (num_frames_ != 0) {
805 // Check agreement of frame Ids only if num_frames_ is computed to avoid infinite
806 // recursion.
807 CHECK(instrumentation_frame.frame_id_ == GetFrameId())
808 << "Expected: " << instrumentation_frame.frame_id_
809 << " Found: " << GetFrameId();
810 }
jeffhao725a9572012-11-13 18:20:12 -0800811 return_pc = instrumentation_frame.return_pc_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700812 }
813 }
814 cur_quick_frame_pc_ = return_pc;
Ian Rogers13735952014-10-08 12:43:28 -0700815 uint8_t* next_frame = reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size;
Andreas Gampecf4035a2014-05-28 22:43:01 -0700816 cur_quick_frame_ = reinterpret_cast<StackReference<mirror::ArtMethod>*>(next_frame);
Ian Rogers0399dde2012-06-06 17:09:28 -0700817 cur_depth_++;
Andreas Gampecf4035a2014-05-28 22:43:01 -0700818 method = cur_quick_frame_->AsMirrorPtr();
jeffhao6641ea12013-01-02 18:13:42 -0800819 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700820 } else if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700821 do {
822 SanityCheckFrame();
823 bool should_continue = VisitFrame();
824 if (UNLIKELY(!should_continue)) {
825 return;
826 }
827 cur_depth_++;
828 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700829 } while (cur_shadow_frame_ != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700830 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700831 if (include_transitions) {
832 bool should_continue = VisitFrame();
833 if (!should_continue) {
834 return;
835 }
836 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800837 cur_depth_++;
838 }
839 if (num_frames_ != 0) {
840 CHECK_EQ(cur_depth_, num_frames_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700841 }
842}
843
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800844void JavaFrameRootInfo::Describe(std::ostream& os) const {
845 const StackVisitor* visitor = stack_visitor_;
846 CHECK(visitor != nullptr);
847 os << "Type=" << GetType() << " thread_id=" << GetThreadId() << " location=" <<
848 visitor->DescribeLocation() << " vreg=" << vreg_;
849}
850
Elliott Hughes68e76522011-10-05 13:22:16 -0700851} // namespace art