blob: b07b24428296a359ebf39adb92a095967a40c4b4 [file] [log] [blame]
Elliott Hughes68e76522011-10-05 13:22:16 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "stack.h"
18
Ian Rogerse63db272014-07-15 15:36:11 -070019#include "arch/context.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method-inl.h"
Dave Allisonf9439142014-03-27 15:10:22 -070021#include "base/hex_dump.h"
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010022#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070023#include "entrypoints/runtime_asm_entrypoints.h"
Mathieu Chartier50030ef2015-05-08 14:19:26 -070024#include "gc_map.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070025#include "gc/space/image_space.h"
26#include "gc/space/space-inl.h"
27#include "linear_alloc.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070028#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "mirror/object-inl.h"
30#include "mirror/object_array-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010031#include "quick/quick_method_frame_info.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070032#include "runtime.h"
Dave Allisonf9439142014-03-27 15:10:22 -070033#include "thread.h"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070034#include "thread_list.h"
Mathieu Chartier4e305412014-02-19 10:54:44 -080035#include "verify_object-inl.h"
Ian Rogers1809a722013-08-09 22:05:32 -070036#include "vmap_table.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070037
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080038namespace art {
39
Mathieu Chartiere401d142015-04-22 13:56:20 -070040static constexpr bool kDebugStackWalk = false;
41
Ian Rogers62d6c772013-02-27 08:32:07 -080042mirror::Object* ShadowFrame::GetThisObject() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -070043 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -080044 if (m->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070045 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -080046 } else if (m->IsNative()) {
47 return GetVRegReference(0);
48 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070049 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070050 CHECK(code_item != nullptr) << PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -080051 uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
52 return GetVRegReference(reg);
53 }
54}
55
Jeff Haoe701f482013-05-24 11:50:49 -070056mirror::Object* ShadowFrame::GetThisObject(uint16_t num_ins) const {
Mathieu Chartiere401d142015-04-22 13:56:20 -070057 ArtMethod* m = GetMethod();
Jeff Haoe701f482013-05-24 11:50:49 -070058 if (m->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070059 return nullptr;
Jeff Haoe701f482013-05-24 11:50:49 -070060 } else {
Jeff Hao8d448852013-06-03 17:26:19 -070061 return GetVRegReference(NumberOfVRegs() - num_ins);
Jeff Haoe701f482013-05-24 11:50:49 -070062 }
63}
64
TDYa127ce4cc0d2012-11-18 16:59:53 -080065size_t ManagedStack::NumJniShadowFrameReferences() const {
Ian Rogers0399dde2012-06-06 17:09:28 -070066 size_t count = 0;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070067 for (const ManagedStack* current_fragment = this; current_fragment != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070068 current_fragment = current_fragment->GetLink()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070069 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070070 current_frame = current_frame->GetLink()) {
TDYa127ce4cc0d2012-11-18 16:59:53 -080071 if (current_frame->GetMethod()->IsNative()) {
72 // The JNI ShadowFrame only contains references. (For indirect reference.)
73 count += current_frame->NumberOfVRegs();
74 }
Ian Rogers0399dde2012-06-06 17:09:28 -070075 }
76 }
77 return count;
78}
79
Ian Rogersef7d42f2014-01-06 12:55:46 -080080bool ManagedStack::ShadowFramesContain(StackReference<mirror::Object>* shadow_frame_entry) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070081 for (const ManagedStack* current_fragment = this; current_fragment != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070082 current_fragment = current_fragment->GetLink()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070083 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070084 current_frame = current_frame->GetLink()) {
85 if (current_frame->Contains(shadow_frame_entry)) {
86 return true;
87 }
88 }
89 }
90 return false;
91}
92
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010093StackVisitor::StackVisitor(Thread* thread, Context* context, StackWalkKind walk_kind)
94 : StackVisitor(thread, context, walk_kind, 0) {}
Ian Rogers7a22fa62013-01-23 12:16:16 -080095
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010096StackVisitor::StackVisitor(Thread* thread,
97 Context* context,
98 StackWalkKind walk_kind,
99 size_t num_frames)
100 : thread_(thread),
101 walk_kind_(walk_kind),
102 cur_shadow_frame_(nullptr),
103 cur_quick_frame_(nullptr),
104 cur_quick_frame_pc_(0),
105 num_frames_(num_frames),
106 cur_depth_(0),
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100107 current_inlining_depth_(0),
Ian Rogers5cf98192014-05-29 21:31:50 -0700108 context_(context) {
109 DCHECK(thread == Thread::Current() || thread->IsSuspended()) << *thread;
110}
111
Mathieu Chartiere401d142015-04-22 13:56:20 -0700112InlineInfo StackVisitor::GetCurrentInlineInfo() const {
113 ArtMethod* outer_method = *GetCurrentQuickFrame();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100114 uint32_t native_pc_offset = outer_method->NativeQuickPcOffset(cur_quick_frame_pc_);
115 CodeInfo code_info = outer_method->GetOptimizedCodeInfo();
David Brazdilf677ebf2015-05-29 16:29:43 +0100116 StackMapEncoding encoding = code_info.ExtractEncoding();
117 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100118 DCHECK(stack_map.IsValid());
David Brazdilf677ebf2015-05-29 16:29:43 +0100119 return code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100120}
121
Mathieu Chartiere401d142015-04-22 13:56:20 -0700122ArtMethod* StackVisitor::GetMethod() const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100123 if (cur_shadow_frame_ != nullptr) {
124 return cur_shadow_frame_->GetMethod();
125 } else if (cur_quick_frame_ != nullptr) {
126 if (IsInInlinedFrame()) {
127 size_t depth_in_stack_map = current_inlining_depth_ - 1;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100128 InlineInfo inline_info = GetCurrentInlineInfo();
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +0100129 return GetResolvedMethod(*GetCurrentQuickFrame(), inline_info, depth_in_stack_map);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100130 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700131 return *cur_quick_frame_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100132 }
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100133 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700134 return nullptr;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100135}
136
Dave Allisonb373e092014-02-20 16:06:36 -0800137uint32_t StackVisitor::GetDexPc(bool abort_on_failure) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700138 if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700139 return cur_shadow_frame_->GetDexPC();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700140 } else if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100141 if (IsInInlinedFrame()) {
142 size_t depth_in_stack_map = current_inlining_depth_ - 1;
143 return GetCurrentInlineInfo().GetDexPcAtDepth(depth_in_stack_map);
144 } else {
145 return GetMethod()->ToDexPc(cur_quick_frame_pc_, abort_on_failure);
146 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700147 } else {
148 return 0;
149 }
150}
151
Mathieu Chartiere401d142015-04-22 13:56:20 -0700152extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700153 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertza836bc92014-11-25 16:30:53 +0100154
Ian Rogers62d6c772013-02-27 08:32:07 -0800155mirror::Object* StackVisitor::GetThisObject() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700156 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), sizeof(void*));
157 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800158 if (m->IsStatic()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100159 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800160 } else if (m->IsNative()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100161 if (cur_quick_frame_ != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700162 HandleScope* hs = reinterpret_cast<HandleScope*>(
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700163 reinterpret_cast<char*>(cur_quick_frame_) + m->GetHandleScopeOffset().SizeValue());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700164 return hs->GetReference(0);
Ian Rogers62d6c772013-02-27 08:32:07 -0800165 } else {
166 return cur_shadow_frame_->GetVRegReference(0);
167 }
Sebastien Hertza836bc92014-11-25 16:30:53 +0100168 } else if (m->IsProxyMethod()) {
169 if (cur_quick_frame_ != nullptr) {
170 return artQuickGetProxyThisObject(cur_quick_frame_);
171 } else {
172 return cur_shadow_frame_->GetVRegReference(0);
173 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800174 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700175 const DexFile::CodeItem* code_item = m->GetCodeItem();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100176 if (code_item == nullptr) {
Ian Rogerse0dcd462014-03-08 15:21:04 -0800177 UNIMPLEMENTED(ERROR) << "Failed to determine this object of abstract or proxy method: "
Ian Rogers62d6c772013-02-27 08:32:07 -0800178 << PrettyMethod(m);
Ian Rogerse0dcd462014-03-08 15:21:04 -0800179 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800180 } else {
181 uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000182 uint32_t value = 0;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700183 bool success = GetVReg(m, reg, kReferenceVReg, &value);
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000184 // We currently always guarantee the `this` object is live throughout the method.
185 CHECK(success) << "Failed to read the this object in " << PrettyMethod(m);
186 return reinterpret_cast<mirror::Object*>(value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800187 }
188 }
189}
190
Ian Rogers0c7abda2012-09-19 13:33:42 -0700191size_t StackVisitor::GetNativePcOffset() const {
192 DCHECK(!IsShadowFrame());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700193 return GetMethod()->NativeQuickPcOffset(cur_quick_frame_pc_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700194}
195
Mathieu Chartiere401d142015-04-22 13:56:20 -0700196bool StackVisitor::IsReferenceVReg(ArtMethod* m, uint16_t vreg) {
Mathieu Chartier50030ef2015-05-08 14:19:26 -0700197 // Process register map (which native and runtime methods don't have)
198 if (m->IsNative() || m->IsRuntimeMethod() || m->IsProxyMethod()) {
199 return false;
200 }
201 if (m->IsOptimized(sizeof(void*))) {
202 return true; // TODO: Implement.
203 }
204 const uint8_t* native_gc_map = m->GetNativeGcMap(sizeof(void*));
205 CHECK(native_gc_map != nullptr) << PrettyMethod(m);
206 const DexFile::CodeItem* code_item = m->GetCodeItem();
207 // Can't be null or how would we compile its instructions?
208 DCHECK(code_item != nullptr) << PrettyMethod(m);
209 NativePcOffsetToReferenceMap map(native_gc_map);
210 size_t num_regs = std::min(map.RegWidth() * 8, static_cast<size_t>(code_item->registers_size_));
211 const uint8_t* reg_bitmap = nullptr;
212 if (num_regs > 0) {
213 Runtime* runtime = Runtime::Current();
214 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(m, sizeof(void*));
215 uintptr_t native_pc_offset = m->NativeQuickPcOffset(GetCurrentQuickFramePc(), entry_point);
216 reg_bitmap = map.FindBitMap(native_pc_offset);
217 DCHECK(reg_bitmap != nullptr);
218 }
219 // Does this register hold a reference?
220 return vreg < num_regs && TestBitmap(vreg, reg_bitmap);
221}
222
Mathieu Chartiere401d142015-04-22 13:56:20 -0700223bool StackVisitor::GetVReg(ArtMethod* m, uint16_t vreg, VRegKind kind, uint32_t* val) const {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200224 if (cur_quick_frame_ != nullptr) {
225 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800226 DCHECK(m == GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100227 if (m->IsOptimized(sizeof(void*))) {
228 return GetVRegFromOptimizedCode(m, vreg, kind, val);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700229 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100230 return GetVRegFromQuickCode(m, vreg, kind, val);
Ian Rogers0399dde2012-06-06 17:09:28 -0700231 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700232 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100233 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200234 *val = cur_shadow_frame_->GetVReg(vreg);
235 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700236 }
237}
238
Mathieu Chartiere401d142015-04-22 13:56:20 -0700239bool StackVisitor::GetVRegFromQuickCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100240 uint32_t* val) const {
241 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*));
242 DCHECK(code_pointer != nullptr);
243 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*)));
244 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer);
245 uint32_t vmap_offset;
246 // TODO: IsInContext stops before spotting floating point registers.
247 if (vmap_table.IsInContext(vreg, kind, &vmap_offset)) {
248 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
249 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
250 uint32_t reg = vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
251 return GetRegisterIfAccessible(reg, kind, val);
252 } else {
253 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700254 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100255 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000256 *val = *GetVRegAddrFromQuickCode(cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
257 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100258 return true;
259 }
260}
261
Mathieu Chartiere401d142015-04-22 13:56:20 -0700262bool StackVisitor::GetVRegFromOptimizedCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100263 uint32_t* val) const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100264 DCHECK_EQ(m, GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100265 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700266 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100267 // its instructions?
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000268 uint16_t number_of_dex_registers = code_item->registers_size_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100269 DCHECK_LT(vreg, code_item->registers_size_);
270
Mathieu Chartiere401d142015-04-22 13:56:20 -0700271 ArtMethod* outer_method = *GetCurrentQuickFrame();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100272 const void* code_pointer = outer_method->GetQuickOatCodePointer(sizeof(void*));
273 DCHECK(code_pointer != nullptr);
274 CodeInfo code_info = outer_method->GetOptimizedCodeInfo();
David Brazdilf677ebf2015-05-29 16:29:43 +0100275 StackMapEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100276
277 uint32_t native_pc_offset = outer_method->NativeQuickPcOffset(cur_quick_frame_pc_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100278 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100279 DCHECK(stack_map.IsValid());
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100280 size_t depth_in_stack_map = current_inlining_depth_ - 1;
281
282 DexRegisterMap dex_register_map = IsInInlinedFrame()
David Brazdilf677ebf2015-05-29 16:29:43 +0100283 ? code_info.GetDexRegisterMapAtDepth(depth_in_stack_map,
284 code_info.GetInlineInfoOf(stack_map, encoding),
285 encoding,
286 number_of_dex_registers)
287 : code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100288
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000289 DexRegisterLocation::Kind location_kind =
David Brazdilf677ebf2015-05-29 16:29:43 +0100290 dex_register_map.GetLocationKind(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100291 switch (location_kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000292 case DexRegisterLocation::Kind::kInStack: {
David Brazdilf677ebf2015-05-29 16:29:43 +0100293 const int32_t offset = dex_register_map.GetStackOffsetInBytes(vreg,
294 number_of_dex_registers,
295 code_info,
296 encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100297 const uint8_t* addr = reinterpret_cast<const uint8_t*>(cur_quick_frame_) + offset;
298 *val = *reinterpret_cast<const uint32_t*>(addr);
299 return true;
300 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000301 case DexRegisterLocation::Kind::kInRegister:
302 case DexRegisterLocation::Kind::kInFpuRegister: {
David Brazdilf677ebf2015-05-29 16:29:43 +0100303 uint32_t reg =
304 dex_register_map.GetMachineRegister(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100305 return GetRegisterIfAccessible(reg, kind, val);
306 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000307 case DexRegisterLocation::Kind::kConstant:
David Brazdilf677ebf2015-05-29 16:29:43 +0100308 *val = dex_register_map.GetConstant(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100309 return true;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000310 case DexRegisterLocation::Kind::kNone:
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100311 return false;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000312 default:
313 LOG(FATAL)
314 << "Unexpected location kind"
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000315 << DexRegisterLocation::PrettyDescriptor(
David Brazdilf677ebf2015-05-29 16:29:43 +0100316 dex_register_map.GetLocationInternalKind(vreg,
317 number_of_dex_registers,
318 code_info,
319 encoding));
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000320 UNREACHABLE();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100321 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100322}
323
324bool StackVisitor::GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const {
325 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
326 if (!IsAccessibleRegister(reg, is_float)) {
327 return false;
328 }
329 uintptr_t ptr_val = GetRegister(reg, is_float);
330 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
331 if (target64) {
332 const bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
333 const bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
334 int64_t value_long = static_cast<int64_t>(ptr_val);
335 if (wide_lo) {
336 ptr_val = static_cast<uintptr_t>(Low32Bits(value_long));
337 } else if (wide_hi) {
338 ptr_val = static_cast<uintptr_t>(High32Bits(value_long));
339 }
340 }
341 *val = ptr_val;
342 return true;
343}
344
Mathieu Chartiere401d142015-04-22 13:56:20 -0700345bool StackVisitor::GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200346 VRegKind kind_hi, uint64_t* val) const {
347 if (kind_lo == kLongLoVReg) {
348 DCHECK_EQ(kind_hi, kLongHiVReg);
349 } else if (kind_lo == kDoubleLoVReg) {
350 DCHECK_EQ(kind_hi, kDoubleHiVReg);
351 } else {
352 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100353 UNREACHABLE();
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200354 }
355 if (cur_quick_frame_ != nullptr) {
356 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
357 DCHECK(m == GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100358 if (m->IsOptimized(sizeof(void*))) {
359 return GetVRegPairFromOptimizedCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200360 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100361 return GetVRegPairFromQuickCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200362 }
363 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100364 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200365 *val = cur_shadow_frame_->GetVRegLong(vreg);
366 return true;
367 }
368}
369
Mathieu Chartiere401d142015-04-22 13:56:20 -0700370bool StackVisitor::GetVRegPairFromQuickCode(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100371 VRegKind kind_hi, uint64_t* val) const {
372 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*));
373 DCHECK(code_pointer != nullptr);
374 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*)));
375 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer);
376 uint32_t vmap_offset_lo, vmap_offset_hi;
377 // TODO: IsInContext stops before spotting floating point registers.
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700378 if (vmap_table.IsInContext(vreg, kind_lo, &vmap_offset_lo) &&
379 vmap_table.IsInContext(vreg + 1, kind_hi, &vmap_offset_hi)) {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100380 bool is_float = (kind_lo == kDoubleLoVReg);
381 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
382 uint32_t reg_lo = vmap_table.ComputeRegister(spill_mask, vmap_offset_lo, kind_lo);
383 uint32_t reg_hi = vmap_table.ComputeRegister(spill_mask, vmap_offset_hi, kind_hi);
384 return GetRegisterPairIfAccessible(reg_lo, reg_hi, kind_lo, val);
385 } else {
386 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700387 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100388 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000389 uint32_t* addr = GetVRegAddrFromQuickCode(
390 cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
391 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100392 *val = *reinterpret_cast<uint64_t*>(addr);
393 return true;
394 }
395}
396
Mathieu Chartiere401d142015-04-22 13:56:20 -0700397bool StackVisitor::GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100398 VRegKind kind_lo, VRegKind kind_hi,
399 uint64_t* val) const {
400 uint32_t low_32bits;
401 uint32_t high_32bits;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700402 bool success = GetVRegFromOptimizedCode(m, vreg, kind_lo, &low_32bits);
403 success &= GetVRegFromOptimizedCode(m, vreg + 1, kind_hi, &high_32bits);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100404 if (success) {
405 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
406 }
407 return success;
408}
409
410bool StackVisitor::GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
411 VRegKind kind_lo, uint64_t* val) const {
412 const bool is_float = (kind_lo == kDoubleLoVReg);
413 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
414 return false;
415 }
416 uintptr_t ptr_val_lo = GetRegister(reg_lo, is_float);
417 uintptr_t ptr_val_hi = GetRegister(reg_hi, is_float);
418 bool target64 = Is64BitInstructionSet(kRuntimeISA);
419 if (target64) {
420 int64_t value_long_lo = static_cast<int64_t>(ptr_val_lo);
421 int64_t value_long_hi = static_cast<int64_t>(ptr_val_hi);
422 ptr_val_lo = static_cast<uintptr_t>(Low32Bits(value_long_lo));
423 ptr_val_hi = static_cast<uintptr_t>(High32Bits(value_long_hi));
424 }
425 *val = (static_cast<uint64_t>(ptr_val_hi) << 32) | static_cast<uint32_t>(ptr_val_lo);
426 return true;
427}
428
Mathieu Chartiere401d142015-04-22 13:56:20 -0700429bool StackVisitor::SetVReg(ArtMethod* m, uint16_t vreg, uint32_t new_value,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800430 VRegKind kind) {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200431 if (cur_quick_frame_ != nullptr) {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100432 DCHECK(context_ != nullptr); // You can't reliably write registers without a context.
433 DCHECK(m == GetMethod());
434 if (m->IsOptimized(sizeof(void*))) {
Nicolas Geoffray7cc56a12015-04-24 14:58:19 +0100435 return false;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100436 } else {
437 return SetVRegFromQuickCode(m, vreg, new_value, kind);
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100438 }
Mathieu Chartier67022432012-11-29 18:04:50 -0800439 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100440 cur_shadow_frame_->SetVReg(vreg, new_value);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200441 return true;
Ian Rogers0ec569a2012-07-01 16:43:46 -0700442 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100443}
444
Mathieu Chartiere401d142015-04-22 13:56:20 -0700445bool StackVisitor::SetVRegFromQuickCode(ArtMethod* m, uint16_t vreg, uint32_t new_value,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100446 VRegKind kind) {
447 DCHECK(context_ != nullptr); // You can't reliably write registers without a context.
448 DCHECK(m == GetMethod());
449 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*));
450 DCHECK(code_pointer != nullptr);
451 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*)));
452 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer);
453 uint32_t vmap_offset;
454 // TODO: IsInContext stops before spotting floating point registers.
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700455 if (vmap_table.IsInContext(vreg, kind, &vmap_offset)) {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100456 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
457 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
458 uint32_t reg = vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
459 return SetRegisterIfAccessible(reg, new_value, kind);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700460 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100461 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700462 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100463 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000464 uint32_t* addr = GetVRegAddrFromQuickCode(
465 cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
466 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100467 *addr = new_value;
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200468 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700469 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700470}
471
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100472bool StackVisitor::SetRegisterIfAccessible(uint32_t reg, uint32_t new_value, VRegKind kind) {
473 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
474 if (!IsAccessibleRegister(reg, is_float)) {
475 return false;
476 }
477 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
478
479 // Create a new value that can hold both low 32 and high 32 bits, in
480 // case we are running 64 bits.
481 uintptr_t full_new_value = new_value;
482 // Deal with 32 or 64-bit wide registers in a way that builds on all targets.
483 if (target64) {
484 bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
485 bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
486 if (wide_lo || wide_hi) {
487 uintptr_t old_reg_val = GetRegister(reg, is_float);
488 uint64_t new_vreg_portion = static_cast<uint64_t>(new_value);
489 uint64_t old_reg_val_as_wide = static_cast<uint64_t>(old_reg_val);
490 uint64_t mask = 0xffffffff;
491 if (wide_lo) {
492 mask = mask << 32;
493 } else {
494 new_vreg_portion = new_vreg_portion << 32;
495 }
496 full_new_value = static_cast<uintptr_t>((old_reg_val_as_wide & mask) | new_vreg_portion);
497 }
498 }
499 SetRegister(reg, full_new_value, is_float);
500 return true;
501}
502
Mathieu Chartiere401d142015-04-22 13:56:20 -0700503bool StackVisitor::SetVRegPair(ArtMethod* m, uint16_t vreg, uint64_t new_value,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200504 VRegKind kind_lo, VRegKind kind_hi) {
505 if (kind_lo == kLongLoVReg) {
506 DCHECK_EQ(kind_hi, kLongHiVReg);
507 } else if (kind_lo == kDoubleLoVReg) {
508 DCHECK_EQ(kind_hi, kDoubleHiVReg);
509 } else {
510 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
511 }
512 if (cur_quick_frame_ != nullptr) {
513 DCHECK(context_ != nullptr); // You can't reliably write registers without a context.
514 DCHECK(m == GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100515 if (m->IsOptimized(sizeof(void*))) {
Nicolas Geoffray7cc56a12015-04-24 14:58:19 +0100516 return false;
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200517 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100518 return SetVRegPairFromQuickCode(m, vreg, new_value, kind_lo, kind_hi);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200519 }
520 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100521 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200522 cur_shadow_frame_->SetVRegLong(vreg, new_value);
523 return true;
524 }
525}
526
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700527bool StackVisitor::SetVRegPairFromQuickCode(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700528 ArtMethod* m, uint16_t vreg, uint64_t new_value, VRegKind kind_lo, VRegKind kind_hi) {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100529 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*));
530 DCHECK(code_pointer != nullptr);
531 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*)));
532 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer);
533 uint32_t vmap_offset_lo, vmap_offset_hi;
534 // TODO: IsInContext stops before spotting floating point registers.
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700535 if (vmap_table.IsInContext(vreg, kind_lo, &vmap_offset_lo) &&
536 vmap_table.IsInContext(vreg + 1, kind_hi, &vmap_offset_hi)) {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100537 bool is_float = (kind_lo == kDoubleLoVReg);
538 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
539 uint32_t reg_lo = vmap_table.ComputeRegister(spill_mask, vmap_offset_lo, kind_lo);
540 uint32_t reg_hi = vmap_table.ComputeRegister(spill_mask, vmap_offset_hi, kind_hi);
541 return SetRegisterPairIfAccessible(reg_lo, reg_hi, new_value, is_float);
542 } else {
543 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700544 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100545 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000546 uint32_t* addr = GetVRegAddrFromQuickCode(
547 cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
548 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100549 *reinterpret_cast<uint64_t*>(addr) = new_value;
550 return true;
551 }
552}
553
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100554bool StackVisitor::SetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
555 uint64_t new_value, bool is_float) {
556 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
557 return false;
558 }
559 uintptr_t new_value_lo = static_cast<uintptr_t>(new_value & 0xFFFFFFFF);
560 uintptr_t new_value_hi = static_cast<uintptr_t>(new_value >> 32);
561 bool target64 = Is64BitInstructionSet(kRuntimeISA);
562 // Deal with 32 or 64-bit wide registers in a way that builds on all targets.
563 if (target64) {
564 DCHECK_EQ(reg_lo, reg_hi);
565 SetRegister(reg_lo, new_value, is_float);
566 } else {
567 SetRegister(reg_lo, new_value_lo, is_float);
568 SetRegister(reg_hi, new_value_hi, is_float);
569 }
570 return true;
571}
572
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100573bool StackVisitor::IsAccessibleGPR(uint32_t reg) const {
574 DCHECK(context_ != nullptr);
575 return context_->IsAccessibleGPR(reg);
576}
577
Mathieu Chartier815873e2014-02-13 18:02:13 -0800578uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100579 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
580 DCHECK(context_ != nullptr);
Mathieu Chartier815873e2014-02-13 18:02:13 -0800581 return context_->GetGPRAddress(reg);
582}
583
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100584uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
585 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
586 DCHECK(context_ != nullptr);
587 return context_->GetGPR(reg);
Ian Rogers0399dde2012-06-06 17:09:28 -0700588}
589
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100590void StackVisitor::SetGPR(uint32_t reg, uintptr_t value) {
591 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
592 DCHECK(context_ != nullptr);
593 context_->SetGPR(reg, value);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200594}
595
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100596bool StackVisitor::IsAccessibleFPR(uint32_t reg) const {
597 DCHECK(context_ != nullptr);
598 return context_->IsAccessibleFPR(reg);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200599}
600
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100601uintptr_t StackVisitor::GetFPR(uint32_t reg) const {
602 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
603 DCHECK(context_ != nullptr);
604 return context_->GetFPR(reg);
605}
606
607void StackVisitor::SetFPR(uint32_t reg, uintptr_t value) {
608 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
609 DCHECK(context_ != nullptr);
610 context_->SetFPR(reg, value);
Mathieu Chartier67022432012-11-29 18:04:50 -0800611}
612
Ian Rogers0399dde2012-06-06 17:09:28 -0700613uintptr_t StackVisitor::GetReturnPc() const {
Ian Rogers13735952014-10-08 12:43:28 -0700614 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700615 DCHECK(sp != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700616 uint8_t* pc_addr = sp + GetMethod()->GetReturnPcOffset().SizeValue();
Ian Rogers0399dde2012-06-06 17:09:28 -0700617 return *reinterpret_cast<uintptr_t*>(pc_addr);
618}
619
620void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
Ian Rogers13735952014-10-08 12:43:28 -0700621 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700622 CHECK(sp != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700623 uint8_t* pc_addr = sp + GetMethod()->GetReturnPcOffset().SizeValue();
Ian Rogers0399dde2012-06-06 17:09:28 -0700624 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc;
625}
626
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100627size_t StackVisitor::ComputeNumFrames(Thread* thread, StackWalkKind walk_kind) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700628 struct NumFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100629 NumFramesVisitor(Thread* thread_in, StackWalkKind walk_kind_in)
630 : StackVisitor(thread_in, nullptr, walk_kind_in), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700631
Ian Rogers5cf98192014-05-29 21:31:50 -0700632 bool VisitFrame() OVERRIDE {
Ian Rogers0399dde2012-06-06 17:09:28 -0700633 frames++;
634 return true;
635 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700636
Ian Rogers0399dde2012-06-06 17:09:28 -0700637 size_t frames;
638 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100639 NumFramesVisitor visitor(thread, walk_kind);
Ian Rogers0399dde2012-06-06 17:09:28 -0700640 visitor.WalkStack(true);
641 return visitor.frames;
642}
643
Mathieu Chartiere401d142015-04-22 13:56:20 -0700644bool StackVisitor::GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700645 struct HasMoreFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100646 HasMoreFramesVisitor(Thread* thread,
647 StackWalkKind walk_kind,
648 size_t num_frames,
649 size_t frame_height)
650 : StackVisitor(thread, nullptr, walk_kind, num_frames),
651 frame_height_(frame_height),
652 found_frame_(false),
653 has_more_frames_(false),
654 next_method_(nullptr),
655 next_dex_pc_(0) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700656 }
657
Mathieu Chartier90443472015-07-16 20:32:27 -0700658 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700659 if (found_frame_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700660 ArtMethod* method = GetMethod();
Ian Rogers5cf98192014-05-29 21:31:50 -0700661 if (method != nullptr && !method->IsRuntimeMethod()) {
662 has_more_frames_ = true;
663 next_method_ = method;
664 next_dex_pc_ = GetDexPc();
665 return false; // End stack walk once next method is found.
666 }
667 } else if (GetFrameHeight() == frame_height_) {
668 found_frame_ = true;
669 }
670 return true;
671 }
672
673 size_t frame_height_;
674 bool found_frame_;
675 bool has_more_frames_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700676 ArtMethod* next_method_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700677 uint32_t next_dex_pc_;
678 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100679 HasMoreFramesVisitor visitor(thread_, walk_kind_, GetNumFrames(), GetFrameHeight());
Ian Rogers5cf98192014-05-29 21:31:50 -0700680 visitor.WalkStack(true);
681 *next_method = visitor.next_method_;
682 *next_dex_pc = visitor.next_dex_pc_;
683 return visitor.has_more_frames_;
684}
685
Ian Rogers7a22fa62013-01-23 12:16:16 -0800686void StackVisitor::DescribeStack(Thread* thread) {
Ian Rogers306057f2012-11-26 12:45:53 -0800687 struct DescribeStackVisitor : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800688 explicit DescribeStackVisitor(Thread* thread_in)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100689 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800690
Mathieu Chartier90443472015-07-16 20:32:27 -0700691 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers306057f2012-11-26 12:45:53 -0800692 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation();
693 return true;
694 }
695 };
Ian Rogers7a22fa62013-01-23 12:16:16 -0800696 DescribeStackVisitor visitor(thread);
Ian Rogers306057f2012-11-26 12:45:53 -0800697 visitor.WalkStack(true);
698}
699
Ian Rogers40e3bac2012-11-20 00:09:14 -0800700std::string StackVisitor::DescribeLocation() const {
701 std::string result("Visiting method '");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700702 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700703 if (m == nullptr) {
Ian Rogers306057f2012-11-26 12:45:53 -0800704 return "upcall";
705 }
706 result += PrettyMethod(m);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800707 result += StringPrintf("' at dex PC 0x%04x", GetDexPc());
Ian Rogers40e3bac2012-11-20 00:09:14 -0800708 if (!IsShadowFrame()) {
709 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc()));
710 }
711 return result;
712}
713
Ian Rogerse63db272014-07-15 15:36:11 -0700714static instrumentation::InstrumentationStackFrame& GetInstrumentationStackFrame(Thread* thread,
715 uint32_t depth) {
716 CHECK_LT(depth, thread->GetInstrumentationStack()->size());
717 return thread->GetInstrumentationStack()->at(depth);
Ian Rogers7a22fa62013-01-23 12:16:16 -0800718}
719
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700720void StackVisitor::SanityCheckFrame() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800721 if (kIsDebugBuild) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700722 ArtMethod* method = GetMethod();
723 auto* declaring_class = method->GetDeclaringClass();
724 // Runtime methods have null declaring class.
725 if (!method->IsRuntimeMethod()) {
726 CHECK(declaring_class != nullptr);
727 CHECK_EQ(declaring_class->GetClass(), declaring_class->GetClass()->GetClass())
728 << declaring_class;
729 } else {
730 CHECK(declaring_class == nullptr);
731 }
732 auto* runtime = Runtime::Current();
733 auto* la = runtime->GetLinearAlloc();
734 if (!la->Contains(method)) {
735 // Check image space.
736 bool in_image = false;
737 for (auto& space : runtime->GetHeap()->GetContinuousSpaces()) {
738 if (space->IsImageSpace()) {
739 auto* image_space = space->AsImageSpace();
740 const auto& header = image_space->GetImageHeader();
741 const auto* methods = &header.GetMethodsSection();
742 if (methods->Contains(reinterpret_cast<const uint8_t*>(method) - image_space->Begin())) {
743 in_image = true;
744 break;
745 }
746 }
747 }
748 CHECK(in_image) << PrettyMethod(method) << " not in linear alloc or image";
749 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800750 if (cur_quick_frame_ != nullptr) {
751 method->AssertPcIsWithinQuickCode(cur_quick_frame_pc_);
752 // Frame sanity.
753 size_t frame_size = method->GetFrameSizeInBytes();
754 CHECK_NE(frame_size, 0u);
Andreas Gampe5b417b92014-03-10 14:18:35 -0700755 // A rough guess at an upper size we expect to see for a frame.
756 // 256 registers
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700757 // 2 words HandleScope overhead
Andreas Gampe5b417b92014-03-10 14:18:35 -0700758 // 3+3 register spills
759 // TODO: this seems architecture specific for the case of JNI frames.
Brian Carlstromed08bd42014-03-19 18:34:17 -0700760 // TODO: 083-compiler-regressions ManyFloatArgs shows this estimate is wrong.
761 // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word);
762 const size_t kMaxExpectedFrameSize = 2 * KB;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800763 CHECK_LE(frame_size, kMaxExpectedFrameSize);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700764 size_t return_pc_offset = method->GetReturnPcOffset().SizeValue();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800765 CHECK_LT(return_pc_offset, frame_size);
766 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700767 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700768}
769
770void StackVisitor::WalkStack(bool include_transitions) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800771 DCHECK(thread_ == Thread::Current() || thread_->IsSuspended());
Ian Rogers62d6c772013-02-27 08:32:07 -0800772 CHECK_EQ(cur_depth_, 0U);
773 bool exit_stubs_installed = Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled();
jeffhao725a9572012-11-13 18:20:12 -0800774 uint32_t instrumentation_stack_depth = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700775
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700776 for (const ManagedStack* current_fragment = thread_->GetManagedStack();
777 current_fragment != nullptr; current_fragment = current_fragment->GetLink()) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700778 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
779 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700780 cur_quick_frame_pc_ = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700781
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700782 if (cur_quick_frame_ != nullptr) { // Handle quick stack frames.
Ian Rogers0399dde2012-06-06 17:09:28 -0700783 // Can't be both a shadow and a quick fragment.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700784 DCHECK(current_fragment->GetTopShadowFrame() == nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700785 ArtMethod* method = *cur_quick_frame_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700786 while (method != nullptr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700787 SanityCheckFrame();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100788
789 if ((walk_kind_ == StackWalkKind::kIncludeInlinedFrames)
790 && method->IsOptimized(sizeof(void*))) {
791 CodeInfo code_info = method->GetOptimizedCodeInfo();
David Brazdilf677ebf2015-05-29 16:29:43 +0100792 StackMapEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100793 uint32_t native_pc_offset = method->NativeQuickPcOffset(cur_quick_frame_pc_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100794 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
795 if (stack_map.IsValid() && stack_map.HasInlineInfo(encoding)) {
796 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100797 DCHECK_EQ(current_inlining_depth_, 0u);
798 for (current_inlining_depth_ = inline_info.GetDepth();
799 current_inlining_depth_ != 0;
800 --current_inlining_depth_) {
801 bool should_continue = VisitFrame();
802 if (UNLIKELY(!should_continue)) {
803 return;
804 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100805 cur_depth_++;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100806 }
807 }
808 }
809
Dave Allison5cd33752014-04-15 15:57:58 -0700810 bool should_continue = VisitFrame();
811 if (UNLIKELY(!should_continue)) {
812 return;
Ian Rogers0399dde2012-06-06 17:09:28 -0700813 }
Dave Allison5cd33752014-04-15 15:57:58 -0700814
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700815 if (context_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700816 context_->FillCalleeSaves(*this);
817 }
818 size_t frame_size = method->GetFrameSizeInBytes();
819 // Compute PC for next stack frame from return PC.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700820 size_t return_pc_offset = method->GetReturnPcOffset(frame_size).SizeValue();
Ian Rogers13735952014-10-08 12:43:28 -0700821 uint8_t* return_pc_addr = reinterpret_cast<uint8_t*>(cur_quick_frame_) + return_pc_offset;
Ian Rogers0399dde2012-06-06 17:09:28 -0700822 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800823 if (UNLIKELY(exit_stubs_installed)) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700824 // While profiling, the return pc is restored from the side stack, except when walking
825 // the stack for an exception where the side stack will be unwound in VisitFrame.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700826 if (reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc) {
Sebastien Hertz74e256b2013-10-04 10:40:37 +0200827 const instrumentation::InstrumentationStackFrame& instrumentation_frame =
Ian Rogerse63db272014-07-15 15:36:11 -0700828 GetInstrumentationStackFrame(thread_, instrumentation_stack_depth);
jeffhao725a9572012-11-13 18:20:12 -0800829 instrumentation_stack_depth++;
Jeff Haofb2802d2013-07-24 13:53:05 -0700830 if (GetMethod() == Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveAll)) {
831 // Skip runtime save all callee frames which are used to deliver exceptions.
832 } else if (instrumentation_frame.interpreter_entry_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700833 ArtMethod* callee = Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs);
Jeff Haofb2802d2013-07-24 13:53:05 -0700834 CHECK_EQ(GetMethod(), callee) << "Expected: " << PrettyMethod(callee) << " Found: "
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100835 << PrettyMethod(GetMethod());
Jeff Hao9a916d32013-06-27 18:45:37 -0700836 } else if (instrumentation_frame.method_ != GetMethod()) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800837 LOG(FATAL) << "Expected: " << PrettyMethod(instrumentation_frame.method_)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100838 << " Found: " << PrettyMethod(GetMethod());
Ian Rogers62d6c772013-02-27 08:32:07 -0800839 }
840 if (num_frames_ != 0) {
841 // Check agreement of frame Ids only if num_frames_ is computed to avoid infinite
842 // recursion.
843 CHECK(instrumentation_frame.frame_id_ == GetFrameId())
844 << "Expected: " << instrumentation_frame.frame_id_
845 << " Found: " << GetFrameId();
846 }
jeffhao725a9572012-11-13 18:20:12 -0800847 return_pc = instrumentation_frame.return_pc_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700848 }
849 }
850 cur_quick_frame_pc_ = return_pc;
Ian Rogers13735952014-10-08 12:43:28 -0700851 uint8_t* next_frame = reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700852 cur_quick_frame_ = reinterpret_cast<ArtMethod**>(next_frame);
853
854 if (kDebugStackWalk) {
855 LOG(INFO) << PrettyMethod(method) << "@" << method << " size=" << frame_size
856 << " optimized=" << method->IsOptimized(sizeof(void*))
857 << " native=" << method->IsNative()
858 << " entrypoints=" << method->GetEntryPointFromQuickCompiledCode()
859 << "," << method->GetEntryPointFromJni()
Mathieu Chartiere401d142015-04-22 13:56:20 -0700860 << " next=" << *cur_quick_frame_;
861 }
862
Ian Rogers0399dde2012-06-06 17:09:28 -0700863 cur_depth_++;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700864 method = *cur_quick_frame_;
jeffhao6641ea12013-01-02 18:13:42 -0800865 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700866 } else if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700867 do {
868 SanityCheckFrame();
869 bool should_continue = VisitFrame();
870 if (UNLIKELY(!should_continue)) {
871 return;
872 }
873 cur_depth_++;
874 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700875 } while (cur_shadow_frame_ != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700876 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700877 if (include_transitions) {
878 bool should_continue = VisitFrame();
879 if (!should_continue) {
880 return;
881 }
882 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800883 cur_depth_++;
884 }
885 if (num_frames_ != 0) {
886 CHECK_EQ(cur_depth_, num_frames_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700887 }
888}
889
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800890void JavaFrameRootInfo::Describe(std::ostream& os) const {
891 const StackVisitor* visitor = stack_visitor_;
892 CHECK(visitor != nullptr);
893 os << "Type=" << GetType() << " thread_id=" << GetThreadId() << " location=" <<
894 visitor->DescribeLocation() << " vreg=" << vreg_;
895}
896
Mathieu Chartiere401d142015-04-22 13:56:20 -0700897int StackVisitor::GetVRegOffsetFromQuickCode(const DexFile::CodeItem* code_item,
898 uint32_t core_spills, uint32_t fp_spills,
899 size_t frame_size, int reg, InstructionSet isa) {
900 size_t pointer_size = InstructionSetPointerSize(isa);
901 if (kIsDebugBuild) {
902 auto* runtime = Runtime::Current();
903 if (runtime != nullptr) {
904 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
905 }
906 }
Roland Levillain14d90572015-07-16 10:52:26 +0100907 DCHECK_ALIGNED(frame_size, kStackAlignment);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700908 DCHECK_NE(reg, -1);
909 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
910 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
911 + sizeof(uint32_t); // Filler.
912 int num_regs = code_item->registers_size_ - code_item->ins_size_;
913 int temp_threshold = code_item->registers_size_;
914 const int max_num_special_temps = 1;
915 if (reg == temp_threshold) {
916 // The current method pointer corresponds to special location on stack.
917 return 0;
918 } else if (reg >= temp_threshold + max_num_special_temps) {
919 /*
920 * Special temporaries may have custom locations and the logic above deals with that.
921 * However, non-special temporaries are placed relative to the outs.
922 */
923 int temps_start = code_item->outs_size_ * sizeof(uint32_t) + pointer_size /* art method */;
924 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
925 return temps_start + relative_offset;
926 } else if (reg < num_regs) {
927 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
928 return locals_start + (reg * sizeof(uint32_t));
929 } else {
930 // Handle ins.
931 return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + pointer_size /* art method */;
932 }
933}
934
Elliott Hughes68e76522011-10-05 13:22:16 -0700935} // namespace art