blob: 2916eaaf5e82b473cff36440e9a154f8af2d1f8f [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"
Igor Murashkinfb326cf2015-07-23 16:53:53 -070022#include "base/out.h"
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010023#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070024#include "entrypoints/runtime_asm_entrypoints.h"
Mathieu Chartier50030ef2015-05-08 14:19:26 -070025#include "gc_map.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070026#include "gc/space/image_space.h"
27#include "gc/space/space-inl.h"
28#include "linear_alloc.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070029#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "mirror/object-inl.h"
31#include "mirror/object_array-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010032#include "quick/quick_method_frame_info.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070033#include "runtime.h"
Dave Allisonf9439142014-03-27 15:10:22 -070034#include "thread.h"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070035#include "thread_list.h"
Mathieu Chartier4e305412014-02-19 10:54:44 -080036#include "verify_object-inl.h"
Ian Rogers1809a722013-08-09 22:05:32 -070037#include "vmap_table.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070038
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080039namespace art {
40
Mathieu Chartiere401d142015-04-22 13:56:20 -070041static constexpr bool kDebugStackWalk = false;
42
Ian Rogers62d6c772013-02-27 08:32:07 -080043mirror::Object* ShadowFrame::GetThisObject() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -070044 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -080045 if (m->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070046 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -080047 } else if (m->IsNative()) {
48 return GetVRegReference(0);
49 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070050 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070051 CHECK(code_item != nullptr) << PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -080052 uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
53 return GetVRegReference(reg);
54 }
55}
56
Jeff Haoe701f482013-05-24 11:50:49 -070057mirror::Object* ShadowFrame::GetThisObject(uint16_t num_ins) const {
Mathieu Chartiere401d142015-04-22 13:56:20 -070058 ArtMethod* m = GetMethod();
Jeff Haoe701f482013-05-24 11:50:49 -070059 if (m->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070060 return nullptr;
Jeff Haoe701f482013-05-24 11:50:49 -070061 } else {
Jeff Hao8d448852013-06-03 17:26:19 -070062 return GetVRegReference(NumberOfVRegs() - num_ins);
Jeff Haoe701f482013-05-24 11:50:49 -070063 }
64}
65
TDYa127ce4cc0d2012-11-18 16:59:53 -080066size_t ManagedStack::NumJniShadowFrameReferences() const {
Ian Rogers0399dde2012-06-06 17:09:28 -070067 size_t count = 0;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070068 for (const ManagedStack* current_fragment = this; current_fragment != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070069 current_fragment = current_fragment->GetLink()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070070 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070071 current_frame = current_frame->GetLink()) {
TDYa127ce4cc0d2012-11-18 16:59:53 -080072 if (current_frame->GetMethod()->IsNative()) {
73 // The JNI ShadowFrame only contains references. (For indirect reference.)
74 count += current_frame->NumberOfVRegs();
75 }
Ian Rogers0399dde2012-06-06 17:09:28 -070076 }
77 }
78 return count;
79}
80
Ian Rogersef7d42f2014-01-06 12:55:46 -080081bool ManagedStack::ShadowFramesContain(StackReference<mirror::Object>* shadow_frame_entry) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070082 for (const ManagedStack* current_fragment = this; current_fragment != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070083 current_fragment = current_fragment->GetLink()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070084 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070085 current_frame = current_frame->GetLink()) {
86 if (current_frame->Contains(shadow_frame_entry)) {
87 return true;
88 }
89 }
90 }
91 return false;
92}
93
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010094StackVisitor::StackVisitor(Thread* thread, Context* context, StackWalkKind walk_kind)
95 : StackVisitor(thread, context, walk_kind, 0) {}
Ian Rogers7a22fa62013-01-23 12:16:16 -080096
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010097StackVisitor::StackVisitor(Thread* thread,
98 Context* context,
99 StackWalkKind walk_kind,
100 size_t num_frames)
101 : thread_(thread),
102 walk_kind_(walk_kind),
103 cur_shadow_frame_(nullptr),
104 cur_quick_frame_(nullptr),
105 cur_quick_frame_pc_(0),
106 num_frames_(num_frames),
107 cur_depth_(0),
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100108 current_inlining_depth_(0),
Ian Rogers5cf98192014-05-29 21:31:50 -0700109 context_(context) {
110 DCHECK(thread == Thread::Current() || thread->IsSuspended()) << *thread;
111}
112
Mathieu Chartiere401d142015-04-22 13:56:20 -0700113InlineInfo StackVisitor::GetCurrentInlineInfo() const {
114 ArtMethod* outer_method = *GetCurrentQuickFrame();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100115 uint32_t native_pc_offset = outer_method->NativeQuickPcOffset(cur_quick_frame_pc_);
116 CodeInfo code_info = outer_method->GetOptimizedCodeInfo();
David Brazdilf677ebf2015-05-29 16:29:43 +0100117 StackMapEncoding encoding = code_info.ExtractEncoding();
118 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100119 DCHECK(stack_map.IsValid());
David Brazdilf677ebf2015-05-29 16:29:43 +0100120 return code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100121}
122
Mathieu Chartiere401d142015-04-22 13:56:20 -0700123ArtMethod* StackVisitor::GetMethod() const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100124 if (cur_shadow_frame_ != nullptr) {
125 return cur_shadow_frame_->GetMethod();
126 } else if (cur_quick_frame_ != nullptr) {
127 if (IsInInlinedFrame()) {
128 size_t depth_in_stack_map = current_inlining_depth_ - 1;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100129 InlineInfo inline_info = GetCurrentInlineInfo();
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +0100130 return GetResolvedMethod(*GetCurrentQuickFrame(), inline_info, depth_in_stack_map);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100131 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700132 return *cur_quick_frame_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100133 }
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100134 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700135 return nullptr;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100136}
137
Dave Allisonb373e092014-02-20 16:06:36 -0800138uint32_t StackVisitor::GetDexPc(bool abort_on_failure) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700139 if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700140 return cur_shadow_frame_->GetDexPC();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700141 } else if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100142 if (IsInInlinedFrame()) {
143 size_t depth_in_stack_map = current_inlining_depth_ - 1;
144 return GetCurrentInlineInfo().GetDexPcAtDepth(depth_in_stack_map);
145 } else {
146 return GetMethod()->ToDexPc(cur_quick_frame_pc_, abort_on_failure);
147 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700148 } else {
149 return 0;
150 }
151}
152
Mathieu Chartiere401d142015-04-22 13:56:20 -0700153extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700154 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertza836bc92014-11-25 16:30:53 +0100155
Ian Rogers62d6c772013-02-27 08:32:07 -0800156mirror::Object* StackVisitor::GetThisObject() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700157 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), sizeof(void*));
158 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800159 if (m->IsStatic()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100160 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800161 } else if (m->IsNative()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100162 if (cur_quick_frame_ != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700163 HandleScope* hs = reinterpret_cast<HandleScope*>(
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700164 reinterpret_cast<char*>(cur_quick_frame_) + m->GetHandleScopeOffset().SizeValue());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700165 return hs->GetReference(0);
Ian Rogers62d6c772013-02-27 08:32:07 -0800166 } else {
167 return cur_shadow_frame_->GetVRegReference(0);
168 }
Sebastien Hertza836bc92014-11-25 16:30:53 +0100169 } else if (m->IsProxyMethod()) {
170 if (cur_quick_frame_ != nullptr) {
171 return artQuickGetProxyThisObject(cur_quick_frame_);
172 } else {
173 return cur_shadow_frame_->GetVRegReference(0);
174 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800175 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700176 const DexFile::CodeItem* code_item = m->GetCodeItem();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100177 if (code_item == nullptr) {
Ian Rogerse0dcd462014-03-08 15:21:04 -0800178 UNIMPLEMENTED(ERROR) << "Failed to determine this object of abstract or proxy method: "
Ian Rogers62d6c772013-02-27 08:32:07 -0800179 << PrettyMethod(m);
Ian Rogerse0dcd462014-03-08 15:21:04 -0800180 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800181 } else {
182 uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000183 uint32_t value = 0;
Igor Murashkinfb326cf2015-07-23 16:53:53 -0700184 bool success = GetVReg(m, reg, kReferenceVReg, outof(value));
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000185 // We currently always guarantee the `this` object is live throughout the method.
186 CHECK(success) << "Failed to read the this object in " << PrettyMethod(m);
187 return reinterpret_cast<mirror::Object*>(value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800188 }
189 }
190}
191
Ian Rogers0c7abda2012-09-19 13:33:42 -0700192size_t StackVisitor::GetNativePcOffset() const {
193 DCHECK(!IsShadowFrame());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700194 return GetMethod()->NativeQuickPcOffset(cur_quick_frame_pc_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700195}
196
Mathieu Chartiere401d142015-04-22 13:56:20 -0700197bool StackVisitor::IsReferenceVReg(ArtMethod* m, uint16_t vreg) {
Mathieu Chartier50030ef2015-05-08 14:19:26 -0700198 // Process register map (which native and runtime methods don't have)
199 if (m->IsNative() || m->IsRuntimeMethod() || m->IsProxyMethod()) {
200 return false;
201 }
202 if (m->IsOptimized(sizeof(void*))) {
203 return true; // TODO: Implement.
204 }
205 const uint8_t* native_gc_map = m->GetNativeGcMap(sizeof(void*));
206 CHECK(native_gc_map != nullptr) << PrettyMethod(m);
207 const DexFile::CodeItem* code_item = m->GetCodeItem();
208 // Can't be null or how would we compile its instructions?
209 DCHECK(code_item != nullptr) << PrettyMethod(m);
210 NativePcOffsetToReferenceMap map(native_gc_map);
211 size_t num_regs = std::min(map.RegWidth() * 8, static_cast<size_t>(code_item->registers_size_));
212 const uint8_t* reg_bitmap = nullptr;
213 if (num_regs > 0) {
214 Runtime* runtime = Runtime::Current();
215 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(m, sizeof(void*));
216 uintptr_t native_pc_offset = m->NativeQuickPcOffset(GetCurrentQuickFramePc(), entry_point);
217 reg_bitmap = map.FindBitMap(native_pc_offset);
218 DCHECK(reg_bitmap != nullptr);
219 }
220 // Does this register hold a reference?
221 return vreg < num_regs && TestBitmap(vreg, reg_bitmap);
222}
223
Mathieu Chartiere401d142015-04-22 13:56:20 -0700224bool StackVisitor::GetVReg(ArtMethod* m, uint16_t vreg, VRegKind kind, uint32_t* val) const {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200225 if (cur_quick_frame_ != nullptr) {
226 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800227 DCHECK(m == GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100228 if (m->IsOptimized(sizeof(void*))) {
229 return GetVRegFromOptimizedCode(m, vreg, kind, val);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700230 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100231 return GetVRegFromQuickCode(m, vreg, kind, val);
Ian Rogers0399dde2012-06-06 17:09:28 -0700232 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700233 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100234 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200235 *val = cur_shadow_frame_->GetVReg(vreg);
236 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700237 }
238}
239
Mathieu Chartiere401d142015-04-22 13:56:20 -0700240bool StackVisitor::GetVRegFromQuickCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100241 uint32_t* val) const {
242 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*));
243 DCHECK(code_pointer != nullptr);
244 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*)));
245 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer);
246 uint32_t vmap_offset;
247 // TODO: IsInContext stops before spotting floating point registers.
248 if (vmap_table.IsInContext(vreg, kind, &vmap_offset)) {
249 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
250 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
251 uint32_t reg = vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
252 return GetRegisterIfAccessible(reg, kind, val);
253 } else {
254 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700255 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100256 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000257 *val = *GetVRegAddrFromQuickCode(cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
258 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100259 return true;
260 }
261}
262
Mathieu Chartiere401d142015-04-22 13:56:20 -0700263bool StackVisitor::GetVRegFromOptimizedCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100264 uint32_t* val) const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100265 DCHECK_EQ(m, GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100266 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700267 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100268 // its instructions?
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000269 uint16_t number_of_dex_registers = code_item->registers_size_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100270 DCHECK_LT(vreg, code_item->registers_size_);
271
Mathieu Chartiere401d142015-04-22 13:56:20 -0700272 ArtMethod* outer_method = *GetCurrentQuickFrame();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100273 const void* code_pointer = outer_method->GetQuickOatCodePointer(sizeof(void*));
274 DCHECK(code_pointer != nullptr);
275 CodeInfo code_info = outer_method->GetOptimizedCodeInfo();
David Brazdilf677ebf2015-05-29 16:29:43 +0100276 StackMapEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100277
278 uint32_t native_pc_offset = outer_method->NativeQuickPcOffset(cur_quick_frame_pc_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100279 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100280 DCHECK(stack_map.IsValid());
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100281 size_t depth_in_stack_map = current_inlining_depth_ - 1;
282
283 DexRegisterMap dex_register_map = IsInInlinedFrame()
David Brazdilf677ebf2015-05-29 16:29:43 +0100284 ? code_info.GetDexRegisterMapAtDepth(depth_in_stack_map,
285 code_info.GetInlineInfoOf(stack_map, encoding),
286 encoding,
287 number_of_dex_registers)
288 : code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100289
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000290 DexRegisterLocation::Kind location_kind =
David Brazdilf677ebf2015-05-29 16:29:43 +0100291 dex_register_map.GetLocationKind(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100292 switch (location_kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000293 case DexRegisterLocation::Kind::kInStack: {
David Brazdilf677ebf2015-05-29 16:29:43 +0100294 const int32_t offset = dex_register_map.GetStackOffsetInBytes(vreg,
295 number_of_dex_registers,
296 code_info,
297 encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100298 const uint8_t* addr = reinterpret_cast<const uint8_t*>(cur_quick_frame_) + offset;
299 *val = *reinterpret_cast<const uint32_t*>(addr);
300 return true;
301 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000302 case DexRegisterLocation::Kind::kInRegister:
303 case DexRegisterLocation::Kind::kInFpuRegister: {
David Brazdilf677ebf2015-05-29 16:29:43 +0100304 uint32_t reg =
305 dex_register_map.GetMachineRegister(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100306 return GetRegisterIfAccessible(reg, kind, val);
307 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000308 case DexRegisterLocation::Kind::kConstant:
David Brazdilf677ebf2015-05-29 16:29:43 +0100309 *val = dex_register_map.GetConstant(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100310 return true;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000311 case DexRegisterLocation::Kind::kNone:
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100312 return false;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000313 default:
314 LOG(FATAL)
315 << "Unexpected location kind"
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000316 << DexRegisterLocation::PrettyDescriptor(
David Brazdilf677ebf2015-05-29 16:29:43 +0100317 dex_register_map.GetLocationInternalKind(vreg,
318 number_of_dex_registers,
319 code_info,
320 encoding));
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000321 UNREACHABLE();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100322 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100323}
324
325bool StackVisitor::GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const {
326 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
327 if (!IsAccessibleRegister(reg, is_float)) {
328 return false;
329 }
330 uintptr_t ptr_val = GetRegister(reg, is_float);
331 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
332 if (target64) {
333 const bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
334 const bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
335 int64_t value_long = static_cast<int64_t>(ptr_val);
336 if (wide_lo) {
337 ptr_val = static_cast<uintptr_t>(Low32Bits(value_long));
338 } else if (wide_hi) {
339 ptr_val = static_cast<uintptr_t>(High32Bits(value_long));
340 }
341 }
342 *val = ptr_val;
343 return true;
344}
345
Mathieu Chartiere401d142015-04-22 13:56:20 -0700346bool StackVisitor::GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200347 VRegKind kind_hi, uint64_t* val) const {
348 if (kind_lo == kLongLoVReg) {
349 DCHECK_EQ(kind_hi, kLongHiVReg);
350 } else if (kind_lo == kDoubleLoVReg) {
351 DCHECK_EQ(kind_hi, kDoubleHiVReg);
352 } else {
353 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100354 UNREACHABLE();
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200355 }
356 if (cur_quick_frame_ != nullptr) {
357 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
358 DCHECK(m == GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100359 if (m->IsOptimized(sizeof(void*))) {
360 return GetVRegPairFromOptimizedCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200361 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100362 return GetVRegPairFromQuickCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200363 }
364 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100365 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200366 *val = cur_shadow_frame_->GetVRegLong(vreg);
367 return true;
368 }
369}
370
Mathieu Chartiere401d142015-04-22 13:56:20 -0700371bool StackVisitor::GetVRegPairFromQuickCode(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100372 VRegKind kind_hi, uint64_t* val) const {
373 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*));
374 DCHECK(code_pointer != nullptr);
375 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*)));
376 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer);
377 uint32_t vmap_offset_lo, vmap_offset_hi;
378 // TODO: IsInContext stops before spotting floating point registers.
Igor Murashkinfb326cf2015-07-23 16:53:53 -0700379 if (vmap_table.IsInContext(vreg, kind_lo, outof(vmap_offset_lo)) &&
380 vmap_table.IsInContext(vreg + 1, kind_hi, outof(vmap_offset_hi))) {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100381 bool is_float = (kind_lo == kDoubleLoVReg);
382 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
383 uint32_t reg_lo = vmap_table.ComputeRegister(spill_mask, vmap_offset_lo, kind_lo);
384 uint32_t reg_hi = vmap_table.ComputeRegister(spill_mask, vmap_offset_hi, kind_hi);
385 return GetRegisterPairIfAccessible(reg_lo, reg_hi, kind_lo, val);
386 } else {
387 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700388 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100389 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000390 uint32_t* addr = GetVRegAddrFromQuickCode(
391 cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
392 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100393 *val = *reinterpret_cast<uint64_t*>(addr);
394 return true;
395 }
396}
397
Mathieu Chartiere401d142015-04-22 13:56:20 -0700398bool StackVisitor::GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100399 VRegKind kind_lo, VRegKind kind_hi,
400 uint64_t* val) const {
401 uint32_t low_32bits;
402 uint32_t high_32bits;
Igor Murashkinfb326cf2015-07-23 16:53:53 -0700403 bool success = GetVRegFromOptimizedCode(m, vreg, kind_lo, outof(low_32bits));
404 success &= GetVRegFromOptimizedCode(m, vreg + 1, kind_hi, outof(high_32bits));
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100405 if (success) {
406 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
407 }
408 return success;
409}
410
411bool StackVisitor::GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
412 VRegKind kind_lo, uint64_t* val) const {
413 const bool is_float = (kind_lo == kDoubleLoVReg);
414 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
415 return false;
416 }
417 uintptr_t ptr_val_lo = GetRegister(reg_lo, is_float);
418 uintptr_t ptr_val_hi = GetRegister(reg_hi, is_float);
419 bool target64 = Is64BitInstructionSet(kRuntimeISA);
420 if (target64) {
421 int64_t value_long_lo = static_cast<int64_t>(ptr_val_lo);
422 int64_t value_long_hi = static_cast<int64_t>(ptr_val_hi);
423 ptr_val_lo = static_cast<uintptr_t>(Low32Bits(value_long_lo));
424 ptr_val_hi = static_cast<uintptr_t>(High32Bits(value_long_hi));
425 }
426 *val = (static_cast<uint64_t>(ptr_val_hi) << 32) | static_cast<uint32_t>(ptr_val_lo);
427 return true;
428}
429
Mathieu Chartiere401d142015-04-22 13:56:20 -0700430bool StackVisitor::SetVReg(ArtMethod* m, uint16_t vreg, uint32_t new_value,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800431 VRegKind kind) {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200432 if (cur_quick_frame_ != nullptr) {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100433 DCHECK(context_ != nullptr); // You can't reliably write registers without a context.
434 DCHECK(m == GetMethod());
435 if (m->IsOptimized(sizeof(void*))) {
Nicolas Geoffray7cc56a12015-04-24 14:58:19 +0100436 return false;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100437 } else {
438 return SetVRegFromQuickCode(m, vreg, new_value, kind);
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100439 }
Mathieu Chartier67022432012-11-29 18:04:50 -0800440 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100441 cur_shadow_frame_->SetVReg(vreg, new_value);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200442 return true;
Ian Rogers0ec569a2012-07-01 16:43:46 -0700443 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100444}
445
Mathieu Chartiere401d142015-04-22 13:56:20 -0700446bool StackVisitor::SetVRegFromQuickCode(ArtMethod* m, uint16_t vreg, uint32_t new_value,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100447 VRegKind kind) {
448 DCHECK(context_ != nullptr); // You can't reliably write registers without a context.
449 DCHECK(m == GetMethod());
450 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*));
451 DCHECK(code_pointer != nullptr);
452 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*)));
453 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer);
454 uint32_t vmap_offset;
455 // TODO: IsInContext stops before spotting floating point registers.
Igor Murashkinfb326cf2015-07-23 16:53:53 -0700456 if (vmap_table.IsInContext(vreg, kind, outof(vmap_offset))) {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100457 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
458 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
459 uint32_t reg = vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
460 return SetRegisterIfAccessible(reg, new_value, kind);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700461 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100462 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700463 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100464 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000465 uint32_t* addr = GetVRegAddrFromQuickCode(
466 cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
467 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100468 *addr = new_value;
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200469 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700470 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700471}
472
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100473bool StackVisitor::SetRegisterIfAccessible(uint32_t reg, uint32_t new_value, VRegKind kind) {
474 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
475 if (!IsAccessibleRegister(reg, is_float)) {
476 return false;
477 }
478 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
479
480 // Create a new value that can hold both low 32 and high 32 bits, in
481 // case we are running 64 bits.
482 uintptr_t full_new_value = new_value;
483 // Deal with 32 or 64-bit wide registers in a way that builds on all targets.
484 if (target64) {
485 bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
486 bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
487 if (wide_lo || wide_hi) {
488 uintptr_t old_reg_val = GetRegister(reg, is_float);
489 uint64_t new_vreg_portion = static_cast<uint64_t>(new_value);
490 uint64_t old_reg_val_as_wide = static_cast<uint64_t>(old_reg_val);
491 uint64_t mask = 0xffffffff;
492 if (wide_lo) {
493 mask = mask << 32;
494 } else {
495 new_vreg_portion = new_vreg_portion << 32;
496 }
497 full_new_value = static_cast<uintptr_t>((old_reg_val_as_wide & mask) | new_vreg_portion);
498 }
499 }
500 SetRegister(reg, full_new_value, is_float);
501 return true;
502}
503
Mathieu Chartiere401d142015-04-22 13:56:20 -0700504bool StackVisitor::SetVRegPair(ArtMethod* m, uint16_t vreg, uint64_t new_value,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200505 VRegKind kind_lo, VRegKind kind_hi) {
506 if (kind_lo == kLongLoVReg) {
507 DCHECK_EQ(kind_hi, kLongHiVReg);
508 } else if (kind_lo == kDoubleLoVReg) {
509 DCHECK_EQ(kind_hi, kDoubleHiVReg);
510 } else {
511 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
512 }
513 if (cur_quick_frame_ != nullptr) {
514 DCHECK(context_ != nullptr); // You can't reliably write registers without a context.
515 DCHECK(m == GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100516 if (m->IsOptimized(sizeof(void*))) {
Nicolas Geoffray7cc56a12015-04-24 14:58:19 +0100517 return false;
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200518 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100519 return SetVRegPairFromQuickCode(m, vreg, new_value, kind_lo, kind_hi);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200520 }
521 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100522 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200523 cur_shadow_frame_->SetVRegLong(vreg, new_value);
524 return true;
525 }
526}
527
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700528bool StackVisitor::SetVRegPairFromQuickCode(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700529 ArtMethod* m, uint16_t vreg, uint64_t new_value, VRegKind kind_lo, VRegKind kind_hi) {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100530 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*));
531 DCHECK(code_pointer != nullptr);
532 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*)));
533 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer);
534 uint32_t vmap_offset_lo, vmap_offset_hi;
535 // TODO: IsInContext stops before spotting floating point registers.
Igor Murashkinfb326cf2015-07-23 16:53:53 -0700536 if (vmap_table.IsInContext(vreg, kind_lo, outof(vmap_offset_lo)) &&
537 vmap_table.IsInContext(vreg + 1, kind_hi, outof(vmap_offset_hi))) {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100538 bool is_float = (kind_lo == kDoubleLoVReg);
539 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
540 uint32_t reg_lo = vmap_table.ComputeRegister(spill_mask, vmap_offset_lo, kind_lo);
541 uint32_t reg_hi = vmap_table.ComputeRegister(spill_mask, vmap_offset_hi, kind_hi);
542 return SetRegisterPairIfAccessible(reg_lo, reg_hi, new_value, is_float);
543 } else {
544 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700545 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100546 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000547 uint32_t* addr = GetVRegAddrFromQuickCode(
548 cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
549 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100550 *reinterpret_cast<uint64_t*>(addr) = new_value;
551 return true;
552 }
553}
554
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100555bool StackVisitor::SetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
556 uint64_t new_value, bool is_float) {
557 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
558 return false;
559 }
560 uintptr_t new_value_lo = static_cast<uintptr_t>(new_value & 0xFFFFFFFF);
561 uintptr_t new_value_hi = static_cast<uintptr_t>(new_value >> 32);
562 bool target64 = Is64BitInstructionSet(kRuntimeISA);
563 // Deal with 32 or 64-bit wide registers in a way that builds on all targets.
564 if (target64) {
565 DCHECK_EQ(reg_lo, reg_hi);
566 SetRegister(reg_lo, new_value, is_float);
567 } else {
568 SetRegister(reg_lo, new_value_lo, is_float);
569 SetRegister(reg_hi, new_value_hi, is_float);
570 }
571 return true;
572}
573
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100574bool StackVisitor::IsAccessibleGPR(uint32_t reg) const {
575 DCHECK(context_ != nullptr);
576 return context_->IsAccessibleGPR(reg);
577}
578
Mathieu Chartier815873e2014-02-13 18:02:13 -0800579uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100580 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
581 DCHECK(context_ != nullptr);
Mathieu Chartier815873e2014-02-13 18:02:13 -0800582 return context_->GetGPRAddress(reg);
583}
584
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100585uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
586 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
587 DCHECK(context_ != nullptr);
588 return context_->GetGPR(reg);
Ian Rogers0399dde2012-06-06 17:09:28 -0700589}
590
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100591void StackVisitor::SetGPR(uint32_t reg, uintptr_t value) {
592 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
593 DCHECK(context_ != nullptr);
594 context_->SetGPR(reg, value);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200595}
596
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100597bool StackVisitor::IsAccessibleFPR(uint32_t reg) const {
598 DCHECK(context_ != nullptr);
599 return context_->IsAccessibleFPR(reg);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200600}
601
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100602uintptr_t StackVisitor::GetFPR(uint32_t reg) const {
603 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
604 DCHECK(context_ != nullptr);
605 return context_->GetFPR(reg);
606}
607
608void StackVisitor::SetFPR(uint32_t reg, uintptr_t value) {
609 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
610 DCHECK(context_ != nullptr);
611 context_->SetFPR(reg, value);
Mathieu Chartier67022432012-11-29 18:04:50 -0800612}
613
Ian Rogers0399dde2012-06-06 17:09:28 -0700614uintptr_t StackVisitor::GetReturnPc() const {
Ian Rogers13735952014-10-08 12:43:28 -0700615 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700616 DCHECK(sp != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700617 uint8_t* pc_addr = sp + GetMethod()->GetReturnPcOffset().SizeValue();
Ian Rogers0399dde2012-06-06 17:09:28 -0700618 return *reinterpret_cast<uintptr_t*>(pc_addr);
619}
620
621void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
Ian Rogers13735952014-10-08 12:43:28 -0700622 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700623 CHECK(sp != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700624 uint8_t* pc_addr = sp + GetMethod()->GetReturnPcOffset().SizeValue();
Ian Rogers0399dde2012-06-06 17:09:28 -0700625 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc;
626}
627
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100628size_t StackVisitor::ComputeNumFrames(Thread* thread, StackWalkKind walk_kind) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700629 struct NumFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100630 NumFramesVisitor(Thread* thread_in, StackWalkKind walk_kind_in)
631 : StackVisitor(thread_in, nullptr, walk_kind_in), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700632
Ian Rogers5cf98192014-05-29 21:31:50 -0700633 bool VisitFrame() OVERRIDE {
Ian Rogers0399dde2012-06-06 17:09:28 -0700634 frames++;
635 return true;
636 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700637
Ian Rogers0399dde2012-06-06 17:09:28 -0700638 size_t frames;
639 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100640 NumFramesVisitor visitor(thread, walk_kind);
Ian Rogers0399dde2012-06-06 17:09:28 -0700641 visitor.WalkStack(true);
642 return visitor.frames;
643}
644
Mathieu Chartiere401d142015-04-22 13:56:20 -0700645bool StackVisitor::GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700646 struct HasMoreFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100647 HasMoreFramesVisitor(Thread* thread,
648 StackWalkKind walk_kind,
649 size_t num_frames,
650 size_t frame_height)
651 : StackVisitor(thread, nullptr, walk_kind, num_frames),
652 frame_height_(frame_height),
653 found_frame_(false),
654 has_more_frames_(false),
655 next_method_(nullptr),
656 next_dex_pc_(0) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700657 }
658
Mathieu Chartier90443472015-07-16 20:32:27 -0700659 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700660 if (found_frame_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700661 ArtMethod* method = GetMethod();
Ian Rogers5cf98192014-05-29 21:31:50 -0700662 if (method != nullptr && !method->IsRuntimeMethod()) {
663 has_more_frames_ = true;
664 next_method_ = method;
665 next_dex_pc_ = GetDexPc();
666 return false; // End stack walk once next method is found.
667 }
668 } else if (GetFrameHeight() == frame_height_) {
669 found_frame_ = true;
670 }
671 return true;
672 }
673
674 size_t frame_height_;
675 bool found_frame_;
676 bool has_more_frames_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700677 ArtMethod* next_method_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700678 uint32_t next_dex_pc_;
679 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100680 HasMoreFramesVisitor visitor(thread_, walk_kind_, GetNumFrames(), GetFrameHeight());
Ian Rogers5cf98192014-05-29 21:31:50 -0700681 visitor.WalkStack(true);
682 *next_method = visitor.next_method_;
683 *next_dex_pc = visitor.next_dex_pc_;
684 return visitor.has_more_frames_;
685}
686
Ian Rogers7a22fa62013-01-23 12:16:16 -0800687void StackVisitor::DescribeStack(Thread* thread) {
Ian Rogers306057f2012-11-26 12:45:53 -0800688 struct DescribeStackVisitor : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800689 explicit DescribeStackVisitor(Thread* thread_in)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100690 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800691
Mathieu Chartier90443472015-07-16 20:32:27 -0700692 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers306057f2012-11-26 12:45:53 -0800693 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation();
694 return true;
695 }
696 };
Ian Rogers7a22fa62013-01-23 12:16:16 -0800697 DescribeStackVisitor visitor(thread);
Ian Rogers306057f2012-11-26 12:45:53 -0800698 visitor.WalkStack(true);
699}
700
Ian Rogers40e3bac2012-11-20 00:09:14 -0800701std::string StackVisitor::DescribeLocation() const {
702 std::string result("Visiting method '");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700703 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700704 if (m == nullptr) {
Ian Rogers306057f2012-11-26 12:45:53 -0800705 return "upcall";
706 }
707 result += PrettyMethod(m);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800708 result += StringPrintf("' at dex PC 0x%04x", GetDexPc());
Ian Rogers40e3bac2012-11-20 00:09:14 -0800709 if (!IsShadowFrame()) {
710 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc()));
711 }
712 return result;
713}
714
Ian Rogerse63db272014-07-15 15:36:11 -0700715static instrumentation::InstrumentationStackFrame& GetInstrumentationStackFrame(Thread* thread,
716 uint32_t depth) {
717 CHECK_LT(depth, thread->GetInstrumentationStack()->size());
718 return thread->GetInstrumentationStack()->at(depth);
Ian Rogers7a22fa62013-01-23 12:16:16 -0800719}
720
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700721void StackVisitor::SanityCheckFrame() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800722 if (kIsDebugBuild) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700723 ArtMethod* method = GetMethod();
724 auto* declaring_class = method->GetDeclaringClass();
725 // Runtime methods have null declaring class.
726 if (!method->IsRuntimeMethod()) {
727 CHECK(declaring_class != nullptr);
728 CHECK_EQ(declaring_class->GetClass(), declaring_class->GetClass()->GetClass())
729 << declaring_class;
730 } else {
731 CHECK(declaring_class == nullptr);
732 }
733 auto* runtime = Runtime::Current();
734 auto* la = runtime->GetLinearAlloc();
735 if (!la->Contains(method)) {
736 // Check image space.
737 bool in_image = false;
738 for (auto& space : runtime->GetHeap()->GetContinuousSpaces()) {
739 if (space->IsImageSpace()) {
740 auto* image_space = space->AsImageSpace();
741 const auto& header = image_space->GetImageHeader();
742 const auto* methods = &header.GetMethodsSection();
743 if (methods->Contains(reinterpret_cast<const uint8_t*>(method) - image_space->Begin())) {
744 in_image = true;
745 break;
746 }
747 }
748 }
749 CHECK(in_image) << PrettyMethod(method) << " not in linear alloc or image";
750 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800751 if (cur_quick_frame_ != nullptr) {
752 method->AssertPcIsWithinQuickCode(cur_quick_frame_pc_);
753 // Frame sanity.
754 size_t frame_size = method->GetFrameSizeInBytes();
755 CHECK_NE(frame_size, 0u);
Andreas Gampe5b417b92014-03-10 14:18:35 -0700756 // A rough guess at an upper size we expect to see for a frame.
757 // 256 registers
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700758 // 2 words HandleScope overhead
Andreas Gampe5b417b92014-03-10 14:18:35 -0700759 // 3+3 register spills
760 // TODO: this seems architecture specific for the case of JNI frames.
Brian Carlstromed08bd42014-03-19 18:34:17 -0700761 // TODO: 083-compiler-regressions ManyFloatArgs shows this estimate is wrong.
762 // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word);
763 const size_t kMaxExpectedFrameSize = 2 * KB;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800764 CHECK_LE(frame_size, kMaxExpectedFrameSize);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700765 size_t return_pc_offset = method->GetReturnPcOffset().SizeValue();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800766 CHECK_LT(return_pc_offset, frame_size);
767 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700768 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700769}
770
771void StackVisitor::WalkStack(bool include_transitions) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800772 DCHECK(thread_ == Thread::Current() || thread_->IsSuspended());
Ian Rogers62d6c772013-02-27 08:32:07 -0800773 CHECK_EQ(cur_depth_, 0U);
774 bool exit_stubs_installed = Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled();
jeffhao725a9572012-11-13 18:20:12 -0800775 uint32_t instrumentation_stack_depth = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700776
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700777 for (const ManagedStack* current_fragment = thread_->GetManagedStack();
778 current_fragment != nullptr; current_fragment = current_fragment->GetLink()) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700779 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
780 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700781 cur_quick_frame_pc_ = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700782
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700783 if (cur_quick_frame_ != nullptr) { // Handle quick stack frames.
Ian Rogers0399dde2012-06-06 17:09:28 -0700784 // Can't be both a shadow and a quick fragment.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700785 DCHECK(current_fragment->GetTopShadowFrame() == nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700786 ArtMethod* method = *cur_quick_frame_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700787 while (method != nullptr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700788 SanityCheckFrame();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100789
790 if ((walk_kind_ == StackWalkKind::kIncludeInlinedFrames)
791 && method->IsOptimized(sizeof(void*))) {
792 CodeInfo code_info = method->GetOptimizedCodeInfo();
David Brazdilf677ebf2015-05-29 16:29:43 +0100793 StackMapEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100794 uint32_t native_pc_offset = method->NativeQuickPcOffset(cur_quick_frame_pc_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100795 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
796 if (stack_map.IsValid() && stack_map.HasInlineInfo(encoding)) {
797 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100798 DCHECK_EQ(current_inlining_depth_, 0u);
799 for (current_inlining_depth_ = inline_info.GetDepth();
800 current_inlining_depth_ != 0;
801 --current_inlining_depth_) {
802 bool should_continue = VisitFrame();
803 if (UNLIKELY(!should_continue)) {
804 return;
805 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100806 cur_depth_++;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100807 }
808 }
809 }
810
Dave Allison5cd33752014-04-15 15:57:58 -0700811 bool should_continue = VisitFrame();
812 if (UNLIKELY(!should_continue)) {
813 return;
Ian Rogers0399dde2012-06-06 17:09:28 -0700814 }
Dave Allison5cd33752014-04-15 15:57:58 -0700815
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700816 if (context_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700817 context_->FillCalleeSaves(*this);
818 }
819 size_t frame_size = method->GetFrameSizeInBytes();
820 // Compute PC for next stack frame from return PC.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700821 size_t return_pc_offset = method->GetReturnPcOffset(frame_size).SizeValue();
Ian Rogers13735952014-10-08 12:43:28 -0700822 uint8_t* return_pc_addr = reinterpret_cast<uint8_t*>(cur_quick_frame_) + return_pc_offset;
Ian Rogers0399dde2012-06-06 17:09:28 -0700823 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800824 if (UNLIKELY(exit_stubs_installed)) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700825 // While profiling, the return pc is restored from the side stack, except when walking
826 // the stack for an exception where the side stack will be unwound in VisitFrame.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700827 if (reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc) {
Sebastien Hertz74e256b2013-10-04 10:40:37 +0200828 const instrumentation::InstrumentationStackFrame& instrumentation_frame =
Ian Rogerse63db272014-07-15 15:36:11 -0700829 GetInstrumentationStackFrame(thread_, instrumentation_stack_depth);
jeffhao725a9572012-11-13 18:20:12 -0800830 instrumentation_stack_depth++;
Jeff Haofb2802d2013-07-24 13:53:05 -0700831 if (GetMethod() == Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveAll)) {
832 // Skip runtime save all callee frames which are used to deliver exceptions.
833 } else if (instrumentation_frame.interpreter_entry_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700834 ArtMethod* callee = Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs);
Jeff Haofb2802d2013-07-24 13:53:05 -0700835 CHECK_EQ(GetMethod(), callee) << "Expected: " << PrettyMethod(callee) << " Found: "
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100836 << PrettyMethod(GetMethod());
Jeff Hao9a916d32013-06-27 18:45:37 -0700837 } else if (instrumentation_frame.method_ != GetMethod()) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800838 LOG(FATAL) << "Expected: " << PrettyMethod(instrumentation_frame.method_)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100839 << " Found: " << PrettyMethod(GetMethod());
Ian Rogers62d6c772013-02-27 08:32:07 -0800840 }
841 if (num_frames_ != 0) {
842 // Check agreement of frame Ids only if num_frames_ is computed to avoid infinite
843 // recursion.
844 CHECK(instrumentation_frame.frame_id_ == GetFrameId())
845 << "Expected: " << instrumentation_frame.frame_id_
846 << " Found: " << GetFrameId();
847 }
jeffhao725a9572012-11-13 18:20:12 -0800848 return_pc = instrumentation_frame.return_pc_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700849 }
850 }
851 cur_quick_frame_pc_ = return_pc;
Ian Rogers13735952014-10-08 12:43:28 -0700852 uint8_t* next_frame = reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700853 cur_quick_frame_ = reinterpret_cast<ArtMethod**>(next_frame);
854
855 if (kDebugStackWalk) {
856 LOG(INFO) << PrettyMethod(method) << "@" << method << " size=" << frame_size
857 << " optimized=" << method->IsOptimized(sizeof(void*))
858 << " native=" << method->IsNative()
859 << " entrypoints=" << method->GetEntryPointFromQuickCompiledCode()
860 << "," << method->GetEntryPointFromJni()
Mathieu Chartiere401d142015-04-22 13:56:20 -0700861 << " next=" << *cur_quick_frame_;
862 }
863
Ian Rogers0399dde2012-06-06 17:09:28 -0700864 cur_depth_++;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700865 method = *cur_quick_frame_;
jeffhao6641ea12013-01-02 18:13:42 -0800866 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700867 } else if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700868 do {
869 SanityCheckFrame();
870 bool should_continue = VisitFrame();
871 if (UNLIKELY(!should_continue)) {
872 return;
873 }
874 cur_depth_++;
875 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700876 } while (cur_shadow_frame_ != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700877 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700878 if (include_transitions) {
879 bool should_continue = VisitFrame();
880 if (!should_continue) {
881 return;
882 }
883 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800884 cur_depth_++;
885 }
886 if (num_frames_ != 0) {
887 CHECK_EQ(cur_depth_, num_frames_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700888 }
889}
890
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800891void JavaFrameRootInfo::Describe(std::ostream& os) const {
892 const StackVisitor* visitor = stack_visitor_;
893 CHECK(visitor != nullptr);
894 os << "Type=" << GetType() << " thread_id=" << GetThreadId() << " location=" <<
895 visitor->DescribeLocation() << " vreg=" << vreg_;
896}
897
Mathieu Chartiere401d142015-04-22 13:56:20 -0700898int StackVisitor::GetVRegOffsetFromQuickCode(const DexFile::CodeItem* code_item,
899 uint32_t core_spills, uint32_t fp_spills,
900 size_t frame_size, int reg, InstructionSet isa) {
901 size_t pointer_size = InstructionSetPointerSize(isa);
902 if (kIsDebugBuild) {
903 auto* runtime = Runtime::Current();
904 if (runtime != nullptr) {
905 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
906 }
907 }
Roland Levillain14d90572015-07-16 10:52:26 +0100908 DCHECK_ALIGNED(frame_size, kStackAlignment);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700909 DCHECK_NE(reg, -1);
910 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
911 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
912 + sizeof(uint32_t); // Filler.
913 int num_regs = code_item->registers_size_ - code_item->ins_size_;
914 int temp_threshold = code_item->registers_size_;
915 const int max_num_special_temps = 1;
916 if (reg == temp_threshold) {
917 // The current method pointer corresponds to special location on stack.
918 return 0;
919 } else if (reg >= temp_threshold + max_num_special_temps) {
920 /*
921 * Special temporaries may have custom locations and the logic above deals with that.
922 * However, non-special temporaries are placed relative to the outs.
923 */
924 int temps_start = code_item->outs_size_ * sizeof(uint32_t) + pointer_size /* art method */;
925 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
926 return temps_start + relative_offset;
927 } else if (reg < num_regs) {
928 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
929 return locals_start + (reg * sizeof(uint32_t));
930 } else {
931 // Handle ins.
932 return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + pointer_size /* art method */;
933 }
934}
935
Elliott Hughes68e76522011-10-05 13:22:16 -0700936} // namespace art