blob: 411088794a661eb6efad3d1b98970cead8f5ddfb [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();
116 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100117 DCHECK(stack_map.IsValid());
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100118 return code_info.GetInlineInfoOf(stack_map);
119}
120
Mathieu Chartiere401d142015-04-22 13:56:20 -0700121ArtMethod* StackVisitor::GetMethod() const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100122 if (cur_shadow_frame_ != nullptr) {
123 return cur_shadow_frame_->GetMethod();
124 } else if (cur_quick_frame_ != nullptr) {
125 if (IsInInlinedFrame()) {
126 size_t depth_in_stack_map = current_inlining_depth_ - 1;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100127 InlineInfo inline_info = GetCurrentInlineInfo();
128 uint32_t method_index = inline_info.GetMethodIndexAtDepth(depth_in_stack_map);
129 InvokeType invoke_type =
130 static_cast<InvokeType>(inline_info.GetInvokeTypeAtDepth(depth_in_stack_map));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700131 return GetResolvedMethod(*GetCurrentQuickFrame(), method_index, invoke_type);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100132 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700133 return *cur_quick_frame_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100134 }
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100135 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700136 return nullptr;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100137}
138
Dave Allisonb373e092014-02-20 16:06:36 -0800139uint32_t StackVisitor::GetDexPc(bool abort_on_failure) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700140 if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700141 return cur_shadow_frame_->GetDexPC();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700142 } else if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100143 if (IsInInlinedFrame()) {
144 size_t depth_in_stack_map = current_inlining_depth_ - 1;
145 return GetCurrentInlineInfo().GetDexPcAtDepth(depth_in_stack_map);
146 } else {
147 return GetMethod()->ToDexPc(cur_quick_frame_pc_, abort_on_failure);
148 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700149 } else {
150 return 0;
151 }
152}
153
Mathieu Chartiere401d142015-04-22 13:56:20 -0700154extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Sebastien Hertza836bc92014-11-25 16:30:53 +0100155 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
156
Ian Rogers62d6c772013-02-27 08:32:07 -0800157mirror::Object* StackVisitor::GetThisObject() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700158 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), sizeof(void*));
159 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800160 if (m->IsStatic()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100161 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800162 } else if (m->IsNative()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100163 if (cur_quick_frame_ != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700164 HandleScope* hs = reinterpret_cast<HandleScope*>(
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700165 reinterpret_cast<char*>(cur_quick_frame_) + m->GetHandleScopeOffset().SizeValue());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700166 return hs->GetReference(0);
Ian Rogers62d6c772013-02-27 08:32:07 -0800167 } else {
168 return cur_shadow_frame_->GetVRegReference(0);
169 }
Sebastien Hertza836bc92014-11-25 16:30:53 +0100170 } else if (m->IsProxyMethod()) {
171 if (cur_quick_frame_ != nullptr) {
172 return artQuickGetProxyThisObject(cur_quick_frame_);
173 } else {
174 return cur_shadow_frame_->GetVRegReference(0);
175 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800176 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700177 const DexFile::CodeItem* code_item = m->GetCodeItem();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100178 if (code_item == nullptr) {
Ian Rogerse0dcd462014-03-08 15:21:04 -0800179 UNIMPLEMENTED(ERROR) << "Failed to determine this object of abstract or proxy method: "
Ian Rogers62d6c772013-02-27 08:32:07 -0800180 << PrettyMethod(m);
Ian Rogerse0dcd462014-03-08 15:21:04 -0800181 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800182 } else {
183 uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000184 uint32_t value = 0;
185 bool success = GetVReg(m, reg, kReferenceVReg, &value);
186 // We currently always guarantee the `this` object is live throughout the method.
187 CHECK(success) << "Failed to read the this object in " << PrettyMethod(m);
188 return reinterpret_cast<mirror::Object*>(value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800189 }
190 }
191}
192
Ian Rogers0c7abda2012-09-19 13:33:42 -0700193size_t StackVisitor::GetNativePcOffset() const {
194 DCHECK(!IsShadowFrame());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700195 return GetMethod()->NativeQuickPcOffset(cur_quick_frame_pc_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700196}
197
Mathieu Chartiere401d142015-04-22 13:56:20 -0700198bool StackVisitor::IsReferenceVReg(ArtMethod* m, uint16_t vreg) {
Mathieu Chartier50030ef2015-05-08 14:19:26 -0700199 // Process register map (which native and runtime methods don't have)
200 if (m->IsNative() || m->IsRuntimeMethod() || m->IsProxyMethod()) {
201 return false;
202 }
203 if (m->IsOptimized(sizeof(void*))) {
204 return true; // TODO: Implement.
205 }
206 const uint8_t* native_gc_map = m->GetNativeGcMap(sizeof(void*));
207 CHECK(native_gc_map != nullptr) << PrettyMethod(m);
208 const DexFile::CodeItem* code_item = m->GetCodeItem();
209 // Can't be null or how would we compile its instructions?
210 DCHECK(code_item != nullptr) << PrettyMethod(m);
211 NativePcOffsetToReferenceMap map(native_gc_map);
212 size_t num_regs = std::min(map.RegWidth() * 8, static_cast<size_t>(code_item->registers_size_));
213 const uint8_t* reg_bitmap = nullptr;
214 if (num_regs > 0) {
215 Runtime* runtime = Runtime::Current();
216 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(m, sizeof(void*));
217 uintptr_t native_pc_offset = m->NativeQuickPcOffset(GetCurrentQuickFramePc(), entry_point);
218 reg_bitmap = map.FindBitMap(native_pc_offset);
219 DCHECK(reg_bitmap != nullptr);
220 }
221 // Does this register hold a reference?
222 return vreg < num_regs && TestBitmap(vreg, reg_bitmap);
223}
224
Mathieu Chartiere401d142015-04-22 13:56:20 -0700225bool StackVisitor::GetVReg(ArtMethod* m, uint16_t vreg, VRegKind kind, uint32_t* val) const {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200226 if (cur_quick_frame_ != nullptr) {
227 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800228 DCHECK(m == GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100229 if (m->IsOptimized(sizeof(void*))) {
230 return GetVRegFromOptimizedCode(m, vreg, kind, val);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700231 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100232 return GetVRegFromQuickCode(m, vreg, kind, val);
Ian Rogers0399dde2012-06-06 17:09:28 -0700233 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700234 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100235 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200236 *val = cur_shadow_frame_->GetVReg(vreg);
237 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700238 }
239}
240
Mathieu Chartiere401d142015-04-22 13:56:20 -0700241bool StackVisitor::GetVRegFromQuickCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100242 uint32_t* val) const {
243 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*));
244 DCHECK(code_pointer != nullptr);
245 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*)));
246 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer);
247 uint32_t vmap_offset;
248 // TODO: IsInContext stops before spotting floating point registers.
249 if (vmap_table.IsInContext(vreg, kind, &vmap_offset)) {
250 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
251 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
252 uint32_t reg = vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
253 return GetRegisterIfAccessible(reg, kind, val);
254 } else {
255 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700256 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100257 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000258 *val = *GetVRegAddrFromQuickCode(cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
259 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100260 return true;
261 }
262}
263
Mathieu Chartiere401d142015-04-22 13:56:20 -0700264bool StackVisitor::GetVRegFromOptimizedCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100265 uint32_t* val) const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100266 DCHECK_EQ(m, GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100267 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700268 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100269 // its instructions?
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000270 uint16_t number_of_dex_registers = code_item->registers_size_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100271 DCHECK_LT(vreg, code_item->registers_size_);
272
Mathieu Chartiere401d142015-04-22 13:56:20 -0700273 ArtMethod* outer_method = *GetCurrentQuickFrame();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100274 const void* code_pointer = outer_method->GetQuickOatCodePointer(sizeof(void*));
275 DCHECK(code_pointer != nullptr);
276 CodeInfo code_info = outer_method->GetOptimizedCodeInfo();
277
278 uint32_t native_pc_offset = outer_method->NativeQuickPcOffset(cur_quick_frame_pc_);
279 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
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()
284 ? code_info.GetDexRegisterMapAtDepth(
285 depth_in_stack_map, code_info.GetInlineInfoOf(stack_map), number_of_dex_registers)
286 : code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers);
287
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000288 DexRegisterLocation::Kind location_kind =
Roland Levillaina552e1c2015-03-26 15:01:03 +0000289 dex_register_map.GetLocationKind(vreg, number_of_dex_registers, code_info);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100290 switch (location_kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000291 case DexRegisterLocation::Kind::kInStack: {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000292 const int32_t offset =
293 dex_register_map.GetStackOffsetInBytes(vreg, number_of_dex_registers, code_info);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100294 const uint8_t* addr = reinterpret_cast<const uint8_t*>(cur_quick_frame_) + offset;
295 *val = *reinterpret_cast<const uint32_t*>(addr);
296 return true;
297 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000298 case DexRegisterLocation::Kind::kInRegister:
299 case DexRegisterLocation::Kind::kInFpuRegister: {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000300 uint32_t reg = dex_register_map.GetMachineRegister(vreg, number_of_dex_registers, code_info);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100301 return GetRegisterIfAccessible(reg, kind, val);
302 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000303 case DexRegisterLocation::Kind::kConstant:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000304 *val = dex_register_map.GetConstant(vreg, number_of_dex_registers, code_info);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100305 return true;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000306 case DexRegisterLocation::Kind::kNone:
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100307 return false;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000308 default:
309 LOG(FATAL)
310 << "Unexpected location kind"
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000311 << DexRegisterLocation::PrettyDescriptor(
Roland Levillaina552e1c2015-03-26 15:01:03 +0000312 dex_register_map.GetLocationInternalKind(vreg, number_of_dex_registers, code_info));
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000313 UNREACHABLE();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100314 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100315}
316
317bool StackVisitor::GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const {
318 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
319 if (!IsAccessibleRegister(reg, is_float)) {
320 return false;
321 }
322 uintptr_t ptr_val = GetRegister(reg, is_float);
323 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
324 if (target64) {
325 const bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
326 const bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
327 int64_t value_long = static_cast<int64_t>(ptr_val);
328 if (wide_lo) {
329 ptr_val = static_cast<uintptr_t>(Low32Bits(value_long));
330 } else if (wide_hi) {
331 ptr_val = static_cast<uintptr_t>(High32Bits(value_long));
332 }
333 }
334 *val = ptr_val;
335 return true;
336}
337
Mathieu Chartiere401d142015-04-22 13:56:20 -0700338bool StackVisitor::GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200339 VRegKind kind_hi, uint64_t* val) const {
340 if (kind_lo == kLongLoVReg) {
341 DCHECK_EQ(kind_hi, kLongHiVReg);
342 } else if (kind_lo == kDoubleLoVReg) {
343 DCHECK_EQ(kind_hi, kDoubleHiVReg);
344 } else {
345 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100346 UNREACHABLE();
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200347 }
348 if (cur_quick_frame_ != nullptr) {
349 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
350 DCHECK(m == GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100351 if (m->IsOptimized(sizeof(void*))) {
352 return GetVRegPairFromOptimizedCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200353 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100354 return GetVRegPairFromQuickCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200355 }
356 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100357 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200358 *val = cur_shadow_frame_->GetVRegLong(vreg);
359 return true;
360 }
361}
362
Mathieu Chartiere401d142015-04-22 13:56:20 -0700363bool StackVisitor::GetVRegPairFromQuickCode(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100364 VRegKind kind_hi, uint64_t* val) const {
365 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*));
366 DCHECK(code_pointer != nullptr);
367 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*)));
368 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer);
369 uint32_t vmap_offset_lo, vmap_offset_hi;
370 // TODO: IsInContext stops before spotting floating point registers.
371 if (vmap_table.IsInContext(vreg, kind_lo, &vmap_offset_lo) &&
372 vmap_table.IsInContext(vreg + 1, kind_hi, &vmap_offset_hi)) {
373 bool is_float = (kind_lo == kDoubleLoVReg);
374 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
375 uint32_t reg_lo = vmap_table.ComputeRegister(spill_mask, vmap_offset_lo, kind_lo);
376 uint32_t reg_hi = vmap_table.ComputeRegister(spill_mask, vmap_offset_hi, kind_hi);
377 return GetRegisterPairIfAccessible(reg_lo, reg_hi, kind_lo, val);
378 } else {
379 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700380 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100381 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000382 uint32_t* addr = GetVRegAddrFromQuickCode(
383 cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
384 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100385 *val = *reinterpret_cast<uint64_t*>(addr);
386 return true;
387 }
388}
389
Mathieu Chartiere401d142015-04-22 13:56:20 -0700390bool StackVisitor::GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100391 VRegKind kind_lo, VRegKind kind_hi,
392 uint64_t* val) const {
393 uint32_t low_32bits;
394 uint32_t high_32bits;
395 bool success = GetVRegFromOptimizedCode(m, vreg, kind_lo, &low_32bits);
396 success &= GetVRegFromOptimizedCode(m, vreg + 1, kind_hi, &high_32bits);
397 if (success) {
398 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
399 }
400 return success;
401}
402
403bool StackVisitor::GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
404 VRegKind kind_lo, uint64_t* val) const {
405 const bool is_float = (kind_lo == kDoubleLoVReg);
406 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
407 return false;
408 }
409 uintptr_t ptr_val_lo = GetRegister(reg_lo, is_float);
410 uintptr_t ptr_val_hi = GetRegister(reg_hi, is_float);
411 bool target64 = Is64BitInstructionSet(kRuntimeISA);
412 if (target64) {
413 int64_t value_long_lo = static_cast<int64_t>(ptr_val_lo);
414 int64_t value_long_hi = static_cast<int64_t>(ptr_val_hi);
415 ptr_val_lo = static_cast<uintptr_t>(Low32Bits(value_long_lo));
416 ptr_val_hi = static_cast<uintptr_t>(High32Bits(value_long_hi));
417 }
418 *val = (static_cast<uint64_t>(ptr_val_hi) << 32) | static_cast<uint32_t>(ptr_val_lo);
419 return true;
420}
421
Mathieu Chartiere401d142015-04-22 13:56:20 -0700422bool StackVisitor::SetVReg(ArtMethod* m, uint16_t vreg, uint32_t new_value,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800423 VRegKind kind) {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200424 if (cur_quick_frame_ != nullptr) {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100425 DCHECK(context_ != nullptr); // You can't reliably write registers without a context.
426 DCHECK(m == GetMethod());
427 if (m->IsOptimized(sizeof(void*))) {
Nicolas Geoffray7cc56a12015-04-24 14:58:19 +0100428 return false;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100429 } else {
430 return SetVRegFromQuickCode(m, vreg, new_value, kind);
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100431 }
Mathieu Chartier67022432012-11-29 18:04:50 -0800432 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100433 cur_shadow_frame_->SetVReg(vreg, new_value);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200434 return true;
Ian Rogers0ec569a2012-07-01 16:43:46 -0700435 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100436}
437
Mathieu Chartiere401d142015-04-22 13:56:20 -0700438bool StackVisitor::SetVRegFromQuickCode(ArtMethod* m, uint16_t vreg, uint32_t new_value,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100439 VRegKind kind) {
440 DCHECK(context_ != nullptr); // You can't reliably write registers without a context.
441 DCHECK(m == GetMethod());
442 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*));
443 DCHECK(code_pointer != nullptr);
444 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*)));
445 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer);
446 uint32_t vmap_offset;
447 // TODO: IsInContext stops before spotting floating point registers.
448 if (vmap_table.IsInContext(vreg, kind, &vmap_offset)) {
449 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
450 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
451 uint32_t reg = vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
452 return SetRegisterIfAccessible(reg, new_value, kind);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700453 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100454 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700455 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100456 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000457 uint32_t* addr = GetVRegAddrFromQuickCode(
458 cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
459 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100460 *addr = new_value;
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200461 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700462 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700463}
464
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100465bool StackVisitor::SetRegisterIfAccessible(uint32_t reg, uint32_t new_value, VRegKind kind) {
466 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
467 if (!IsAccessibleRegister(reg, is_float)) {
468 return false;
469 }
470 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
471
472 // Create a new value that can hold both low 32 and high 32 bits, in
473 // case we are running 64 bits.
474 uintptr_t full_new_value = new_value;
475 // Deal with 32 or 64-bit wide registers in a way that builds on all targets.
476 if (target64) {
477 bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
478 bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
479 if (wide_lo || wide_hi) {
480 uintptr_t old_reg_val = GetRegister(reg, is_float);
481 uint64_t new_vreg_portion = static_cast<uint64_t>(new_value);
482 uint64_t old_reg_val_as_wide = static_cast<uint64_t>(old_reg_val);
483 uint64_t mask = 0xffffffff;
484 if (wide_lo) {
485 mask = mask << 32;
486 } else {
487 new_vreg_portion = new_vreg_portion << 32;
488 }
489 full_new_value = static_cast<uintptr_t>((old_reg_val_as_wide & mask) | new_vreg_portion);
490 }
491 }
492 SetRegister(reg, full_new_value, is_float);
493 return true;
494}
495
Mathieu Chartiere401d142015-04-22 13:56:20 -0700496bool StackVisitor::SetVRegPair(ArtMethod* m, uint16_t vreg, uint64_t new_value,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200497 VRegKind kind_lo, VRegKind kind_hi) {
498 if (kind_lo == kLongLoVReg) {
499 DCHECK_EQ(kind_hi, kLongHiVReg);
500 } else if (kind_lo == kDoubleLoVReg) {
501 DCHECK_EQ(kind_hi, kDoubleHiVReg);
502 } else {
503 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
504 }
505 if (cur_quick_frame_ != nullptr) {
506 DCHECK(context_ != nullptr); // You can't reliably write registers without a context.
507 DCHECK(m == GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100508 if (m->IsOptimized(sizeof(void*))) {
Nicolas Geoffray7cc56a12015-04-24 14:58:19 +0100509 return false;
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200510 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100511 return SetVRegPairFromQuickCode(m, vreg, new_value, kind_lo, kind_hi);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200512 }
513 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100514 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200515 cur_shadow_frame_->SetVRegLong(vreg, new_value);
516 return true;
517 }
518}
519
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700520bool StackVisitor::SetVRegPairFromQuickCode(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700521 ArtMethod* m, uint16_t vreg, uint64_t new_value, VRegKind kind_lo, VRegKind kind_hi) {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100522 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*));
523 DCHECK(code_pointer != nullptr);
524 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*)));
525 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer);
526 uint32_t vmap_offset_lo, vmap_offset_hi;
527 // TODO: IsInContext stops before spotting floating point registers.
528 if (vmap_table.IsInContext(vreg, kind_lo, &vmap_offset_lo) &&
529 vmap_table.IsInContext(vreg + 1, kind_hi, &vmap_offset_hi)) {
530 bool is_float = (kind_lo == kDoubleLoVReg);
531 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
532 uint32_t reg_lo = vmap_table.ComputeRegister(spill_mask, vmap_offset_lo, kind_lo);
533 uint32_t reg_hi = vmap_table.ComputeRegister(spill_mask, vmap_offset_hi, kind_hi);
534 return SetRegisterPairIfAccessible(reg_lo, reg_hi, new_value, is_float);
535 } else {
536 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700537 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100538 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000539 uint32_t* addr = GetVRegAddrFromQuickCode(
540 cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
541 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100542 *reinterpret_cast<uint64_t*>(addr) = new_value;
543 return true;
544 }
545}
546
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100547bool StackVisitor::SetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
548 uint64_t new_value, bool is_float) {
549 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
550 return false;
551 }
552 uintptr_t new_value_lo = static_cast<uintptr_t>(new_value & 0xFFFFFFFF);
553 uintptr_t new_value_hi = static_cast<uintptr_t>(new_value >> 32);
554 bool target64 = Is64BitInstructionSet(kRuntimeISA);
555 // Deal with 32 or 64-bit wide registers in a way that builds on all targets.
556 if (target64) {
557 DCHECK_EQ(reg_lo, reg_hi);
558 SetRegister(reg_lo, new_value, is_float);
559 } else {
560 SetRegister(reg_lo, new_value_lo, is_float);
561 SetRegister(reg_hi, new_value_hi, is_float);
562 }
563 return true;
564}
565
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100566bool StackVisitor::IsAccessibleGPR(uint32_t reg) const {
567 DCHECK(context_ != nullptr);
568 return context_->IsAccessibleGPR(reg);
569}
570
Mathieu Chartier815873e2014-02-13 18:02:13 -0800571uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100572 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
573 DCHECK(context_ != nullptr);
Mathieu Chartier815873e2014-02-13 18:02:13 -0800574 return context_->GetGPRAddress(reg);
575}
576
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100577uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
578 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
579 DCHECK(context_ != nullptr);
580 return context_->GetGPR(reg);
Ian Rogers0399dde2012-06-06 17:09:28 -0700581}
582
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100583void StackVisitor::SetGPR(uint32_t reg, uintptr_t value) {
584 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
585 DCHECK(context_ != nullptr);
586 context_->SetGPR(reg, value);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200587}
588
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100589bool StackVisitor::IsAccessibleFPR(uint32_t reg) const {
590 DCHECK(context_ != nullptr);
591 return context_->IsAccessibleFPR(reg);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200592}
593
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100594uintptr_t StackVisitor::GetFPR(uint32_t reg) const {
595 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
596 DCHECK(context_ != nullptr);
597 return context_->GetFPR(reg);
598}
599
600void StackVisitor::SetFPR(uint32_t reg, uintptr_t value) {
601 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
602 DCHECK(context_ != nullptr);
603 context_->SetFPR(reg, value);
Mathieu Chartier67022432012-11-29 18:04:50 -0800604}
605
Ian Rogers0399dde2012-06-06 17:09:28 -0700606uintptr_t StackVisitor::GetReturnPc() const {
Ian Rogers13735952014-10-08 12:43:28 -0700607 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700608 DCHECK(sp != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700609 uint8_t* pc_addr = sp + GetMethod()->GetReturnPcOffset().SizeValue();
Ian Rogers0399dde2012-06-06 17:09:28 -0700610 return *reinterpret_cast<uintptr_t*>(pc_addr);
611}
612
613void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
Ian Rogers13735952014-10-08 12:43:28 -0700614 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700615 CHECK(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 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc;
618}
619
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100620size_t StackVisitor::ComputeNumFrames(Thread* thread, StackWalkKind walk_kind) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700621 struct NumFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100622 NumFramesVisitor(Thread* thread_in, StackWalkKind walk_kind_in)
623 : StackVisitor(thread_in, nullptr, walk_kind_in), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700624
Ian Rogers5cf98192014-05-29 21:31:50 -0700625 bool VisitFrame() OVERRIDE {
Ian Rogers0399dde2012-06-06 17:09:28 -0700626 frames++;
627 return true;
628 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700629
Ian Rogers0399dde2012-06-06 17:09:28 -0700630 size_t frames;
631 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100632 NumFramesVisitor visitor(thread, walk_kind);
Ian Rogers0399dde2012-06-06 17:09:28 -0700633 visitor.WalkStack(true);
634 return visitor.frames;
635}
636
Mathieu Chartiere401d142015-04-22 13:56:20 -0700637bool StackVisitor::GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700638 struct HasMoreFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100639 HasMoreFramesVisitor(Thread* thread,
640 StackWalkKind walk_kind,
641 size_t num_frames,
642 size_t frame_height)
643 : StackVisitor(thread, nullptr, walk_kind, num_frames),
644 frame_height_(frame_height),
645 found_frame_(false),
646 has_more_frames_(false),
647 next_method_(nullptr),
648 next_dex_pc_(0) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700649 }
650
651 bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
652 if (found_frame_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700653 ArtMethod* method = GetMethod();
Ian Rogers5cf98192014-05-29 21:31:50 -0700654 if (method != nullptr && !method->IsRuntimeMethod()) {
655 has_more_frames_ = true;
656 next_method_ = method;
657 next_dex_pc_ = GetDexPc();
658 return false; // End stack walk once next method is found.
659 }
660 } else if (GetFrameHeight() == frame_height_) {
661 found_frame_ = true;
662 }
663 return true;
664 }
665
666 size_t frame_height_;
667 bool found_frame_;
668 bool has_more_frames_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700669 ArtMethod* next_method_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700670 uint32_t next_dex_pc_;
671 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100672 HasMoreFramesVisitor visitor(thread_, walk_kind_, GetNumFrames(), GetFrameHeight());
Ian Rogers5cf98192014-05-29 21:31:50 -0700673 visitor.WalkStack(true);
674 *next_method = visitor.next_method_;
675 *next_dex_pc = visitor.next_dex_pc_;
676 return visitor.has_more_frames_;
677}
678
Ian Rogers7a22fa62013-01-23 12:16:16 -0800679void StackVisitor::DescribeStack(Thread* thread) {
Ian Rogers306057f2012-11-26 12:45:53 -0800680 struct DescribeStackVisitor : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800681 explicit DescribeStackVisitor(Thread* thread_in)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100682 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800683
Ian Rogers5cf98192014-05-29 21:31:50 -0700684 bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers306057f2012-11-26 12:45:53 -0800685 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation();
686 return true;
687 }
688 };
Ian Rogers7a22fa62013-01-23 12:16:16 -0800689 DescribeStackVisitor visitor(thread);
Ian Rogers306057f2012-11-26 12:45:53 -0800690 visitor.WalkStack(true);
691}
692
Ian Rogers40e3bac2012-11-20 00:09:14 -0800693std::string StackVisitor::DescribeLocation() const {
694 std::string result("Visiting method '");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700695 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700696 if (m == nullptr) {
Ian Rogers306057f2012-11-26 12:45:53 -0800697 return "upcall";
698 }
699 result += PrettyMethod(m);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800700 result += StringPrintf("' at dex PC 0x%04x", GetDexPc());
Ian Rogers40e3bac2012-11-20 00:09:14 -0800701 if (!IsShadowFrame()) {
702 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc()));
703 }
704 return result;
705}
706
Ian Rogerse63db272014-07-15 15:36:11 -0700707static instrumentation::InstrumentationStackFrame& GetInstrumentationStackFrame(Thread* thread,
708 uint32_t depth) {
709 CHECK_LT(depth, thread->GetInstrumentationStack()->size());
710 return thread->GetInstrumentationStack()->at(depth);
Ian Rogers7a22fa62013-01-23 12:16:16 -0800711}
712
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700713void StackVisitor::SanityCheckFrame() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800714 if (kIsDebugBuild) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700715 ArtMethod* method = GetMethod();
716 auto* declaring_class = method->GetDeclaringClass();
717 // Runtime methods have null declaring class.
718 if (!method->IsRuntimeMethod()) {
719 CHECK(declaring_class != nullptr);
720 CHECK_EQ(declaring_class->GetClass(), declaring_class->GetClass()->GetClass())
721 << declaring_class;
722 } else {
723 CHECK(declaring_class == nullptr);
724 }
725 auto* runtime = Runtime::Current();
726 auto* la = runtime->GetLinearAlloc();
727 if (!la->Contains(method)) {
728 // Check image space.
729 bool in_image = false;
730 for (auto& space : runtime->GetHeap()->GetContinuousSpaces()) {
731 if (space->IsImageSpace()) {
732 auto* image_space = space->AsImageSpace();
733 const auto& header = image_space->GetImageHeader();
734 const auto* methods = &header.GetMethodsSection();
735 if (methods->Contains(reinterpret_cast<const uint8_t*>(method) - image_space->Begin())) {
736 in_image = true;
737 break;
738 }
739 }
740 }
741 CHECK(in_image) << PrettyMethod(method) << " not in linear alloc or image";
742 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800743 if (cur_quick_frame_ != nullptr) {
744 method->AssertPcIsWithinQuickCode(cur_quick_frame_pc_);
745 // Frame sanity.
746 size_t frame_size = method->GetFrameSizeInBytes();
747 CHECK_NE(frame_size, 0u);
Andreas Gampe5b417b92014-03-10 14:18:35 -0700748 // A rough guess at an upper size we expect to see for a frame.
749 // 256 registers
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700750 // 2 words HandleScope overhead
Andreas Gampe5b417b92014-03-10 14:18:35 -0700751 // 3+3 register spills
752 // TODO: this seems architecture specific for the case of JNI frames.
Brian Carlstromed08bd42014-03-19 18:34:17 -0700753 // TODO: 083-compiler-regressions ManyFloatArgs shows this estimate is wrong.
754 // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word);
755 const size_t kMaxExpectedFrameSize = 2 * KB;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800756 CHECK_LE(frame_size, kMaxExpectedFrameSize);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700757 size_t return_pc_offset = method->GetReturnPcOffset().SizeValue();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800758 CHECK_LT(return_pc_offset, frame_size);
759 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700760 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700761}
762
763void StackVisitor::WalkStack(bool include_transitions) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800764 DCHECK(thread_ == Thread::Current() || thread_->IsSuspended());
Ian Rogers62d6c772013-02-27 08:32:07 -0800765 CHECK_EQ(cur_depth_, 0U);
766 bool exit_stubs_installed = Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled();
jeffhao725a9572012-11-13 18:20:12 -0800767 uint32_t instrumentation_stack_depth = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700768
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700769 for (const ManagedStack* current_fragment = thread_->GetManagedStack();
770 current_fragment != nullptr; current_fragment = current_fragment->GetLink()) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700771 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
772 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700773 cur_quick_frame_pc_ = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700774
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700775 if (cur_quick_frame_ != nullptr) { // Handle quick stack frames.
Ian Rogers0399dde2012-06-06 17:09:28 -0700776 // Can't be both a shadow and a quick fragment.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700777 DCHECK(current_fragment->GetTopShadowFrame() == nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700778 ArtMethod* method = *cur_quick_frame_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700779 while (method != nullptr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700780 SanityCheckFrame();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100781
782 if ((walk_kind_ == StackWalkKind::kIncludeInlinedFrames)
783 && method->IsOptimized(sizeof(void*))) {
784 CodeInfo code_info = method->GetOptimizedCodeInfo();
785 uint32_t native_pc_offset = method->NativeQuickPcOffset(cur_quick_frame_pc_);
786 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100787 if (stack_map.IsValid() && stack_map.HasInlineInfo(code_info)) {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100788 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map);
789 DCHECK_EQ(current_inlining_depth_, 0u);
790 for (current_inlining_depth_ = inline_info.GetDepth();
791 current_inlining_depth_ != 0;
792 --current_inlining_depth_) {
793 bool should_continue = VisitFrame();
794 if (UNLIKELY(!should_continue)) {
795 return;
796 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100797 cur_depth_++;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100798 }
799 }
800 }
801
Dave Allison5cd33752014-04-15 15:57:58 -0700802 bool should_continue = VisitFrame();
803 if (UNLIKELY(!should_continue)) {
804 return;
Ian Rogers0399dde2012-06-06 17:09:28 -0700805 }
Dave Allison5cd33752014-04-15 15:57:58 -0700806
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700807 if (context_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700808 context_->FillCalleeSaves(*this);
809 }
810 size_t frame_size = method->GetFrameSizeInBytes();
811 // Compute PC for next stack frame from return PC.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700812 size_t return_pc_offset = method->GetReturnPcOffset(frame_size).SizeValue();
Ian Rogers13735952014-10-08 12:43:28 -0700813 uint8_t* return_pc_addr = reinterpret_cast<uint8_t*>(cur_quick_frame_) + return_pc_offset;
Ian Rogers0399dde2012-06-06 17:09:28 -0700814 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800815 if (UNLIKELY(exit_stubs_installed)) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700816 // While profiling, the return pc is restored from the side stack, except when walking
817 // the stack for an exception where the side stack will be unwound in VisitFrame.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700818 if (reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc) {
Sebastien Hertz74e256b2013-10-04 10:40:37 +0200819 const instrumentation::InstrumentationStackFrame& instrumentation_frame =
Ian Rogerse63db272014-07-15 15:36:11 -0700820 GetInstrumentationStackFrame(thread_, instrumentation_stack_depth);
jeffhao725a9572012-11-13 18:20:12 -0800821 instrumentation_stack_depth++;
Jeff Haofb2802d2013-07-24 13:53:05 -0700822 if (GetMethod() == Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveAll)) {
823 // Skip runtime save all callee frames which are used to deliver exceptions.
824 } else if (instrumentation_frame.interpreter_entry_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700825 ArtMethod* callee = Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs);
Jeff Haofb2802d2013-07-24 13:53:05 -0700826 CHECK_EQ(GetMethod(), callee) << "Expected: " << PrettyMethod(callee) << " Found: "
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100827 << PrettyMethod(GetMethod());
Jeff Hao9a916d32013-06-27 18:45:37 -0700828 } else if (instrumentation_frame.method_ != GetMethod()) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800829 LOG(FATAL) << "Expected: " << PrettyMethod(instrumentation_frame.method_)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100830 << " Found: " << PrettyMethod(GetMethod());
Ian Rogers62d6c772013-02-27 08:32:07 -0800831 }
832 if (num_frames_ != 0) {
833 // Check agreement of frame Ids only if num_frames_ is computed to avoid infinite
834 // recursion.
835 CHECK(instrumentation_frame.frame_id_ == GetFrameId())
836 << "Expected: " << instrumentation_frame.frame_id_
837 << " Found: " << GetFrameId();
838 }
jeffhao725a9572012-11-13 18:20:12 -0800839 return_pc = instrumentation_frame.return_pc_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700840 }
841 }
842 cur_quick_frame_pc_ = return_pc;
Ian Rogers13735952014-10-08 12:43:28 -0700843 uint8_t* next_frame = reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700844 cur_quick_frame_ = reinterpret_cast<ArtMethod**>(next_frame);
845
846 if (kDebugStackWalk) {
847 LOG(INFO) << PrettyMethod(method) << "@" << method << " size=" << frame_size
848 << " optimized=" << method->IsOptimized(sizeof(void*))
849 << " native=" << method->IsNative()
850 << " entrypoints=" << method->GetEntryPointFromQuickCompiledCode()
851 << "," << method->GetEntryPointFromJni()
852 << "," << method->GetEntryPointFromInterpreter()
853 << " next=" << *cur_quick_frame_;
854 }
855
Ian Rogers0399dde2012-06-06 17:09:28 -0700856 cur_depth_++;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700857 method = *cur_quick_frame_;
jeffhao6641ea12013-01-02 18:13:42 -0800858 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700859 } else if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700860 do {
861 SanityCheckFrame();
862 bool should_continue = VisitFrame();
863 if (UNLIKELY(!should_continue)) {
864 return;
865 }
866 cur_depth_++;
867 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700868 } while (cur_shadow_frame_ != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700869 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700870 if (include_transitions) {
871 bool should_continue = VisitFrame();
872 if (!should_continue) {
873 return;
874 }
875 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800876 cur_depth_++;
877 }
878 if (num_frames_ != 0) {
879 CHECK_EQ(cur_depth_, num_frames_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700880 }
881}
882
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800883void JavaFrameRootInfo::Describe(std::ostream& os) const {
884 const StackVisitor* visitor = stack_visitor_;
885 CHECK(visitor != nullptr);
886 os << "Type=" << GetType() << " thread_id=" << GetThreadId() << " location=" <<
887 visitor->DescribeLocation() << " vreg=" << vreg_;
888}
889
Mathieu Chartiere401d142015-04-22 13:56:20 -0700890int StackVisitor::GetVRegOffsetFromQuickCode(const DexFile::CodeItem* code_item,
891 uint32_t core_spills, uint32_t fp_spills,
892 size_t frame_size, int reg, InstructionSet isa) {
893 size_t pointer_size = InstructionSetPointerSize(isa);
894 if (kIsDebugBuild) {
895 auto* runtime = Runtime::Current();
896 if (runtime != nullptr) {
897 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
898 }
899 }
900 DCHECK_EQ(frame_size & (kStackAlignment - 1), 0U);
901 DCHECK_NE(reg, -1);
902 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
903 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
904 + sizeof(uint32_t); // Filler.
905 int num_regs = code_item->registers_size_ - code_item->ins_size_;
906 int temp_threshold = code_item->registers_size_;
907 const int max_num_special_temps = 1;
908 if (reg == temp_threshold) {
909 // The current method pointer corresponds to special location on stack.
910 return 0;
911 } else if (reg >= temp_threshold + max_num_special_temps) {
912 /*
913 * Special temporaries may have custom locations and the logic above deals with that.
914 * However, non-special temporaries are placed relative to the outs.
915 */
916 int temps_start = code_item->outs_size_ * sizeof(uint32_t) + pointer_size /* art method */;
917 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
918 return temps_start + relative_offset;
919 } else if (reg < num_regs) {
920 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
921 return locals_start + (reg * sizeof(uint32_t));
922 } else {
923 // Handle ins.
924 return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + pointer_size /* art method */;
925 }
926}
927
Elliott Hughes68e76522011-10-05 13:22:16 -0700928} // namespace art