blob: 6cca4d29b96adc4b2bf612a5cdfcebbfe3c07f1b [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 Chartier3d21bdf2015-04-22 13:56:20 -070020#include "art_method-inl.h"
Dave Allisonf9439142014-03-27 15:10:22 -070021#include "base/hex_dump.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070022#include "entrypoints/runtime_asm_entrypoints.h"
Mathieu Chartier50030ef2015-05-08 14:19:26 -070023#include "gc_map.h"
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -070024#include "gc/space/image_space.h"
25#include "gc/space/space-inl.h"
26#include "linear_alloc.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070027#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/object-inl.h"
29#include "mirror/object_array-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010030#include "quick/quick_method_frame_info.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070031#include "runtime.h"
Dave Allisonf9439142014-03-27 15:10:22 -070032#include "thread.h"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070033#include "thread_list.h"
Mathieu Chartier4e305412014-02-19 10:54:44 -080034#include "verify_object-inl.h"
Ian Rogers1809a722013-08-09 22:05:32 -070035#include "vmap_table.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070036
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080037namespace art {
38
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -070039static constexpr bool kDebugStackWalk = false;
40
Ian Rogers62d6c772013-02-27 08:32:07 -080041mirror::Object* ShadowFrame::GetThisObject() const {
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -070042 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -080043 if (m->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070044 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -080045 } else if (m->IsNative()) {
46 return GetVRegReference(0);
47 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070048 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070049 CHECK(code_item != nullptr) << PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -080050 uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
51 return GetVRegReference(reg);
52 }
53}
54
Jeff Haoe701f482013-05-24 11:50:49 -070055mirror::Object* ShadowFrame::GetThisObject(uint16_t num_ins) const {
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -070056 ArtMethod* m = GetMethod();
Jeff Haoe701f482013-05-24 11:50:49 -070057 if (m->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070058 return nullptr;
Jeff Haoe701f482013-05-24 11:50:49 -070059 } else {
Jeff Hao8d448852013-06-03 17:26:19 -070060 return GetVRegReference(NumberOfVRegs() - num_ins);
Jeff Haoe701f482013-05-24 11:50:49 -070061 }
62}
63
TDYa127ce4cc0d2012-11-18 16:59:53 -080064size_t ManagedStack::NumJniShadowFrameReferences() const {
Ian Rogers0399dde2012-06-06 17:09:28 -070065 size_t count = 0;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070066 for (const ManagedStack* current_fragment = this; current_fragment != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070067 current_fragment = current_fragment->GetLink()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070068 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070069 current_frame = current_frame->GetLink()) {
TDYa127ce4cc0d2012-11-18 16:59:53 -080070 if (current_frame->GetMethod()->IsNative()) {
71 // The JNI ShadowFrame only contains references. (For indirect reference.)
72 count += current_frame->NumberOfVRegs();
73 }
Ian Rogers0399dde2012-06-06 17:09:28 -070074 }
75 }
76 return count;
77}
78
Ian Rogersef7d42f2014-01-06 12:55:46 -080079bool ManagedStack::ShadowFramesContain(StackReference<mirror::Object>* shadow_frame_entry) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070080 for (const ManagedStack* current_fragment = this; current_fragment != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070081 current_fragment = current_fragment->GetLink()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070082 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070083 current_frame = current_frame->GetLink()) {
84 if (current_frame->Contains(shadow_frame_entry)) {
85 return true;
86 }
87 }
88 }
89 return false;
90}
91
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010092StackVisitor::StackVisitor(Thread* thread, Context* context, StackWalkKind walk_kind)
93 : StackVisitor(thread, context, walk_kind, 0) {}
Ian Rogers7a22fa62013-01-23 12:16:16 -080094
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010095StackVisitor::StackVisitor(Thread* thread,
96 Context* context,
97 StackWalkKind walk_kind,
98 size_t num_frames)
99 : thread_(thread),
100 walk_kind_(walk_kind),
101 cur_shadow_frame_(nullptr),
102 cur_quick_frame_(nullptr),
103 cur_quick_frame_pc_(0),
104 num_frames_(num_frames),
105 cur_depth_(0),
Ian Rogers5cf98192014-05-29 21:31:50 -0700106 context_(context) {
107 DCHECK(thread == Thread::Current() || thread->IsSuspended()) << *thread;
108}
109
Dave Allisonb373e092014-02-20 16:06:36 -0800110uint32_t StackVisitor::GetDexPc(bool abort_on_failure) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700111 if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700112 return cur_shadow_frame_->GetDexPC();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700113 } else if (cur_quick_frame_ != nullptr) {
Dave Allisonb373e092014-02-20 16:06:36 -0800114 return GetMethod()->ToDexPc(cur_quick_frame_pc_, abort_on_failure);
Ian Rogers0399dde2012-06-06 17:09:28 -0700115 } else {
116 return 0;
117 }
118}
119
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700120extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Sebastien Hertza836bc92014-11-25 16:30:53 +0100121 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
122
Ian Rogers62d6c772013-02-27 08:32:07 -0800123mirror::Object* StackVisitor::GetThisObject() const {
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700124 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), sizeof(void*));
125 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800126 if (m->IsStatic()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100127 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800128 } else if (m->IsNative()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100129 if (cur_quick_frame_ != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700130 HandleScope* hs = reinterpret_cast<HandleScope*>(
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700131 reinterpret_cast<char*>(cur_quick_frame_) + m->GetHandleScopeOffset().SizeValue());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700132 return hs->GetReference(0);
Ian Rogers62d6c772013-02-27 08:32:07 -0800133 } else {
134 return cur_shadow_frame_->GetVRegReference(0);
135 }
Sebastien Hertza836bc92014-11-25 16:30:53 +0100136 } else if (m->IsProxyMethod()) {
137 if (cur_quick_frame_ != nullptr) {
138 return artQuickGetProxyThisObject(cur_quick_frame_);
139 } else {
140 return cur_shadow_frame_->GetVRegReference(0);
141 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800142 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700143 const DexFile::CodeItem* code_item = m->GetCodeItem();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100144 if (code_item == nullptr) {
Ian Rogerse0dcd462014-03-08 15:21:04 -0800145 UNIMPLEMENTED(ERROR) << "Failed to determine this object of abstract or proxy method: "
Ian Rogers62d6c772013-02-27 08:32:07 -0800146 << PrettyMethod(m);
Ian Rogerse0dcd462014-03-08 15:21:04 -0800147 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800148 } else {
149 uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000150 uint32_t value = 0;
151 bool success = GetVReg(m, reg, kReferenceVReg, &value);
152 // We currently always guarantee the `this` object is live throughout the method.
153 CHECK(success) << "Failed to read the this object in " << PrettyMethod(m);
154 return reinterpret_cast<mirror::Object*>(value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800155 }
156 }
157}
158
Ian Rogers0c7abda2012-09-19 13:33:42 -0700159size_t StackVisitor::GetNativePcOffset() const {
160 DCHECK(!IsShadowFrame());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700161 return GetMethod()->NativeQuickPcOffset(cur_quick_frame_pc_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700162}
163
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700164bool StackVisitor::IsReferenceVReg(ArtMethod* m, uint16_t vreg) {
Mathieu Chartier50030ef2015-05-08 14:19:26 -0700165 // Process register map (which native and runtime methods don't have)
166 if (m->IsNative() || m->IsRuntimeMethod() || m->IsProxyMethod()) {
167 return false;
168 }
169 if (m->IsOptimized(sizeof(void*))) {
170 return true; // TODO: Implement.
171 }
172 const uint8_t* native_gc_map = m->GetNativeGcMap(sizeof(void*));
173 CHECK(native_gc_map != nullptr) << PrettyMethod(m);
174 const DexFile::CodeItem* code_item = m->GetCodeItem();
175 // Can't be null or how would we compile its instructions?
176 DCHECK(code_item != nullptr) << PrettyMethod(m);
177 NativePcOffsetToReferenceMap map(native_gc_map);
178 size_t num_regs = std::min(map.RegWidth() * 8, static_cast<size_t>(code_item->registers_size_));
179 const uint8_t* reg_bitmap = nullptr;
180 if (num_regs > 0) {
181 Runtime* runtime = Runtime::Current();
182 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(m, sizeof(void*));
183 uintptr_t native_pc_offset = m->NativeQuickPcOffset(GetCurrentQuickFramePc(), entry_point);
184 reg_bitmap = map.FindBitMap(native_pc_offset);
185 DCHECK(reg_bitmap != nullptr);
186 }
187 // Does this register hold a reference?
188 return vreg < num_regs && TestBitmap(vreg, reg_bitmap);
189}
190
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700191bool StackVisitor::GetVReg(ArtMethod* m, uint16_t vreg, VRegKind kind, uint32_t* val) const {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200192 if (cur_quick_frame_ != nullptr) {
193 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800194 DCHECK(m == GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100195 if (m->IsOptimized(sizeof(void*))) {
196 return GetVRegFromOptimizedCode(m, vreg, kind, val);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700197 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100198 return GetVRegFromQuickCode(m, vreg, kind, val);
Ian Rogers0399dde2012-06-06 17:09:28 -0700199 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700200 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100201 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200202 *val = cur_shadow_frame_->GetVReg(vreg);
203 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700204 }
205}
206
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700207bool StackVisitor::GetVRegFromQuickCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100208 uint32_t* val) const {
209 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*));
210 DCHECK(code_pointer != nullptr);
211 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*)));
212 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer);
213 uint32_t vmap_offset;
214 // TODO: IsInContext stops before spotting floating point registers.
215 if (vmap_table.IsInContext(vreg, kind, &vmap_offset)) {
216 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
217 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
218 uint32_t reg = vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
219 return GetRegisterIfAccessible(reg, kind, val);
220 } else {
221 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700222 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100223 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000224 *val = *GetVRegAddrFromQuickCode(cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
225 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100226 return true;
227 }
228}
229
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700230bool StackVisitor::GetVRegFromOptimizedCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100231 uint32_t* val) const {
232 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*));
233 DCHECK(code_pointer != nullptr);
234 uint32_t native_pc_offset = m->NativeQuickPcOffset(cur_quick_frame_pc_);
235 CodeInfo code_info = m->GetOptimizedCodeInfo();
236 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
237 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700238 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100239 // its instructions?
240 DCHECK_LT(vreg, code_item->registers_size_);
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000241 uint16_t number_of_dex_registers = code_item->registers_size_;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000242 DexRegisterMap dex_register_map =
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000243 code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers);
244 DexRegisterLocation::Kind location_kind =
Roland Levillaina552e1c2015-03-26 15:01:03 +0000245 dex_register_map.GetLocationKind(vreg, number_of_dex_registers, code_info);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100246 switch (location_kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000247 case DexRegisterLocation::Kind::kInStack: {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000248 const int32_t offset =
249 dex_register_map.GetStackOffsetInBytes(vreg, number_of_dex_registers, code_info);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100250 const uint8_t* addr = reinterpret_cast<const uint8_t*>(cur_quick_frame_) + offset;
251 *val = *reinterpret_cast<const uint32_t*>(addr);
252 return true;
253 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000254 case DexRegisterLocation::Kind::kInRegister:
255 case DexRegisterLocation::Kind::kInFpuRegister: {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000256 uint32_t reg = dex_register_map.GetMachineRegister(vreg, number_of_dex_registers, code_info);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100257 return GetRegisterIfAccessible(reg, kind, val);
258 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000259 case DexRegisterLocation::Kind::kConstant:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000260 *val = dex_register_map.GetConstant(vreg, number_of_dex_registers, code_info);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100261 return true;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000262 case DexRegisterLocation::Kind::kNone:
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100263 return false;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000264 default:
265 LOG(FATAL)
266 << "Unexpected location kind"
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000267 << DexRegisterLocation::PrettyDescriptor(
Roland Levillaina552e1c2015-03-26 15:01:03 +0000268 dex_register_map.GetLocationInternalKind(vreg, number_of_dex_registers, code_info));
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000269 UNREACHABLE();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100270 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100271}
272
273bool StackVisitor::GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const {
274 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
275 if (!IsAccessibleRegister(reg, is_float)) {
276 return false;
277 }
278 uintptr_t ptr_val = GetRegister(reg, is_float);
279 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
280 if (target64) {
281 const bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
282 const bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
283 int64_t value_long = static_cast<int64_t>(ptr_val);
284 if (wide_lo) {
285 ptr_val = static_cast<uintptr_t>(Low32Bits(value_long));
286 } else if (wide_hi) {
287 ptr_val = static_cast<uintptr_t>(High32Bits(value_long));
288 }
289 }
290 *val = ptr_val;
291 return true;
292}
293
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700294bool StackVisitor::GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200295 VRegKind kind_hi, uint64_t* val) const {
296 if (kind_lo == kLongLoVReg) {
297 DCHECK_EQ(kind_hi, kLongHiVReg);
298 } else if (kind_lo == kDoubleLoVReg) {
299 DCHECK_EQ(kind_hi, kDoubleHiVReg);
300 } else {
301 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100302 UNREACHABLE();
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200303 }
304 if (cur_quick_frame_ != nullptr) {
305 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
306 DCHECK(m == GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100307 if (m->IsOptimized(sizeof(void*))) {
308 return GetVRegPairFromOptimizedCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200309 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100310 return GetVRegPairFromQuickCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200311 }
312 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100313 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200314 *val = cur_shadow_frame_->GetVRegLong(vreg);
315 return true;
316 }
317}
318
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700319bool StackVisitor::GetVRegPairFromQuickCode(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100320 VRegKind kind_hi, uint64_t* val) const {
321 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*));
322 DCHECK(code_pointer != nullptr);
323 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*)));
324 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer);
325 uint32_t vmap_offset_lo, vmap_offset_hi;
326 // TODO: IsInContext stops before spotting floating point registers.
327 if (vmap_table.IsInContext(vreg, kind_lo, &vmap_offset_lo) &&
328 vmap_table.IsInContext(vreg + 1, kind_hi, &vmap_offset_hi)) {
329 bool is_float = (kind_lo == kDoubleLoVReg);
330 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
331 uint32_t reg_lo = vmap_table.ComputeRegister(spill_mask, vmap_offset_lo, kind_lo);
332 uint32_t reg_hi = vmap_table.ComputeRegister(spill_mask, vmap_offset_hi, kind_hi);
333 return GetRegisterPairIfAccessible(reg_lo, reg_hi, kind_lo, val);
334 } else {
335 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700336 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100337 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000338 uint32_t* addr = GetVRegAddrFromQuickCode(
339 cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
340 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100341 *val = *reinterpret_cast<uint64_t*>(addr);
342 return true;
343 }
344}
345
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700346bool StackVisitor::GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100347 VRegKind kind_lo, VRegKind kind_hi,
348 uint64_t* val) const {
349 uint32_t low_32bits;
350 uint32_t high_32bits;
351 bool success = GetVRegFromOptimizedCode(m, vreg, kind_lo, &low_32bits);
352 success &= GetVRegFromOptimizedCode(m, vreg + 1, kind_hi, &high_32bits);
353 if (success) {
354 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
355 }
356 return success;
357}
358
359bool StackVisitor::GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
360 VRegKind kind_lo, uint64_t* val) const {
361 const bool is_float = (kind_lo == kDoubleLoVReg);
362 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
363 return false;
364 }
365 uintptr_t ptr_val_lo = GetRegister(reg_lo, is_float);
366 uintptr_t ptr_val_hi = GetRegister(reg_hi, is_float);
367 bool target64 = Is64BitInstructionSet(kRuntimeISA);
368 if (target64) {
369 int64_t value_long_lo = static_cast<int64_t>(ptr_val_lo);
370 int64_t value_long_hi = static_cast<int64_t>(ptr_val_hi);
371 ptr_val_lo = static_cast<uintptr_t>(Low32Bits(value_long_lo));
372 ptr_val_hi = static_cast<uintptr_t>(High32Bits(value_long_hi));
373 }
374 *val = (static_cast<uint64_t>(ptr_val_hi) << 32) | static_cast<uint32_t>(ptr_val_lo);
375 return true;
376}
377
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700378bool StackVisitor::SetVReg(ArtMethod* m, uint16_t vreg, uint32_t new_value,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800379 VRegKind kind) {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200380 if (cur_quick_frame_ != nullptr) {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100381 DCHECK(context_ != nullptr); // You can't reliably write registers without a context.
382 DCHECK(m == GetMethod());
383 if (m->IsOptimized(sizeof(void*))) {
Nicolas Geoffray7cc56a12015-04-24 14:58:19 +0100384 return false;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100385 } else {
386 return SetVRegFromQuickCode(m, vreg, new_value, kind);
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100387 }
Mathieu Chartier67022432012-11-29 18:04:50 -0800388 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100389 cur_shadow_frame_->SetVReg(vreg, new_value);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200390 return true;
Ian Rogers0ec569a2012-07-01 16:43:46 -0700391 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100392}
393
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700394bool StackVisitor::SetVRegFromQuickCode(ArtMethod* m, uint16_t vreg, uint32_t new_value,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100395 VRegKind kind) {
396 DCHECK(context_ != nullptr); // You can't reliably write registers without a context.
397 DCHECK(m == GetMethod());
398 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*));
399 DCHECK(code_pointer != nullptr);
400 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*)));
401 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer);
402 uint32_t vmap_offset;
403 // TODO: IsInContext stops before spotting floating point registers.
404 if (vmap_table.IsInContext(vreg, kind, &vmap_offset)) {
405 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
406 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
407 uint32_t reg = vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
408 return SetRegisterIfAccessible(reg, new_value, kind);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700409 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100410 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700411 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100412 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000413 uint32_t* addr = GetVRegAddrFromQuickCode(
414 cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
415 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100416 *addr = new_value;
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200417 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700418 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700419}
420
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100421bool StackVisitor::SetRegisterIfAccessible(uint32_t reg, uint32_t new_value, VRegKind kind) {
422 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
423 if (!IsAccessibleRegister(reg, is_float)) {
424 return false;
425 }
426 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
427
428 // Create a new value that can hold both low 32 and high 32 bits, in
429 // case we are running 64 bits.
430 uintptr_t full_new_value = new_value;
431 // Deal with 32 or 64-bit wide registers in a way that builds on all targets.
432 if (target64) {
433 bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
434 bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
435 if (wide_lo || wide_hi) {
436 uintptr_t old_reg_val = GetRegister(reg, is_float);
437 uint64_t new_vreg_portion = static_cast<uint64_t>(new_value);
438 uint64_t old_reg_val_as_wide = static_cast<uint64_t>(old_reg_val);
439 uint64_t mask = 0xffffffff;
440 if (wide_lo) {
441 mask = mask << 32;
442 } else {
443 new_vreg_portion = new_vreg_portion << 32;
444 }
445 full_new_value = static_cast<uintptr_t>((old_reg_val_as_wide & mask) | new_vreg_portion);
446 }
447 }
448 SetRegister(reg, full_new_value, is_float);
449 return true;
450}
451
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700452bool StackVisitor::SetVRegPair(ArtMethod* m, uint16_t vreg, uint64_t new_value,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200453 VRegKind kind_lo, VRegKind kind_hi) {
454 if (kind_lo == kLongLoVReg) {
455 DCHECK_EQ(kind_hi, kLongHiVReg);
456 } else if (kind_lo == kDoubleLoVReg) {
457 DCHECK_EQ(kind_hi, kDoubleHiVReg);
458 } else {
459 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
460 }
461 if (cur_quick_frame_ != nullptr) {
462 DCHECK(context_ != nullptr); // You can't reliably write registers without a context.
463 DCHECK(m == GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100464 if (m->IsOptimized(sizeof(void*))) {
Nicolas Geoffray7cc56a12015-04-24 14:58:19 +0100465 return false;
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200466 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100467 return SetVRegPairFromQuickCode(m, vreg, new_value, kind_lo, kind_hi);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200468 }
469 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100470 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200471 cur_shadow_frame_->SetVRegLong(vreg, new_value);
472 return true;
473 }
474}
475
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700476bool StackVisitor::SetVRegPairFromQuickCode(
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700477 ArtMethod* m, uint16_t vreg, uint64_t new_value, VRegKind kind_lo, VRegKind kind_hi) {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100478 const void* code_pointer = m->GetQuickOatCodePointer(sizeof(void*));
479 DCHECK(code_pointer != nullptr);
480 const VmapTable vmap_table(m->GetVmapTable(code_pointer, sizeof(void*)));
481 QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer);
482 uint32_t vmap_offset_lo, vmap_offset_hi;
483 // TODO: IsInContext stops before spotting floating point registers.
484 if (vmap_table.IsInContext(vreg, kind_lo, &vmap_offset_lo) &&
485 vmap_table.IsInContext(vreg + 1, kind_hi, &vmap_offset_hi)) {
486 bool is_float = (kind_lo == kDoubleLoVReg);
487 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
488 uint32_t reg_lo = vmap_table.ComputeRegister(spill_mask, vmap_offset_lo, kind_lo);
489 uint32_t reg_hi = vmap_table.ComputeRegister(spill_mask, vmap_offset_hi, kind_hi);
490 return SetRegisterPairIfAccessible(reg_lo, reg_hi, new_value, is_float);
491 } else {
492 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700493 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100494 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000495 uint32_t* addr = GetVRegAddrFromQuickCode(
496 cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
497 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100498 *reinterpret_cast<uint64_t*>(addr) = new_value;
499 return true;
500 }
501}
502
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100503bool StackVisitor::SetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
504 uint64_t new_value, bool is_float) {
505 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
506 return false;
507 }
508 uintptr_t new_value_lo = static_cast<uintptr_t>(new_value & 0xFFFFFFFF);
509 uintptr_t new_value_hi = static_cast<uintptr_t>(new_value >> 32);
510 bool target64 = Is64BitInstructionSet(kRuntimeISA);
511 // Deal with 32 or 64-bit wide registers in a way that builds on all targets.
512 if (target64) {
513 DCHECK_EQ(reg_lo, reg_hi);
514 SetRegister(reg_lo, new_value, is_float);
515 } else {
516 SetRegister(reg_lo, new_value_lo, is_float);
517 SetRegister(reg_hi, new_value_hi, is_float);
518 }
519 return true;
520}
521
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100522bool StackVisitor::IsAccessibleGPR(uint32_t reg) const {
523 DCHECK(context_ != nullptr);
524 return context_->IsAccessibleGPR(reg);
525}
526
Mathieu Chartier815873e2014-02-13 18:02:13 -0800527uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100528 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
529 DCHECK(context_ != nullptr);
Mathieu Chartier815873e2014-02-13 18:02:13 -0800530 return context_->GetGPRAddress(reg);
531}
532
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100533uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
534 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
535 DCHECK(context_ != nullptr);
536 return context_->GetGPR(reg);
Ian Rogers0399dde2012-06-06 17:09:28 -0700537}
538
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100539void StackVisitor::SetGPR(uint32_t reg, uintptr_t value) {
540 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
541 DCHECK(context_ != nullptr);
542 context_->SetGPR(reg, value);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200543}
544
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100545bool StackVisitor::IsAccessibleFPR(uint32_t reg) const {
546 DCHECK(context_ != nullptr);
547 return context_->IsAccessibleFPR(reg);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200548}
549
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100550uintptr_t StackVisitor::GetFPR(uint32_t reg) const {
551 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
552 DCHECK(context_ != nullptr);
553 return context_->GetFPR(reg);
554}
555
556void StackVisitor::SetFPR(uint32_t reg, uintptr_t value) {
557 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
558 DCHECK(context_ != nullptr);
559 context_->SetFPR(reg, value);
Mathieu Chartier67022432012-11-29 18:04:50 -0800560}
561
Ian Rogers0399dde2012-06-06 17:09:28 -0700562uintptr_t StackVisitor::GetReturnPc() const {
Ian Rogers13735952014-10-08 12:43:28 -0700563 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700564 DCHECK(sp != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700565 uint8_t* pc_addr = sp + GetMethod()->GetReturnPcOffset().SizeValue();
Ian Rogers0399dde2012-06-06 17:09:28 -0700566 return *reinterpret_cast<uintptr_t*>(pc_addr);
567}
568
569void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
Ian Rogers13735952014-10-08 12:43:28 -0700570 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700571 CHECK(sp != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700572 uint8_t* pc_addr = sp + GetMethod()->GetReturnPcOffset().SizeValue();
Ian Rogers0399dde2012-06-06 17:09:28 -0700573 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc;
574}
575
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100576size_t StackVisitor::ComputeNumFrames(Thread* thread, StackWalkKind walk_kind) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700577 struct NumFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100578 NumFramesVisitor(Thread* thread_in, StackWalkKind walk_kind_in)
579 : StackVisitor(thread_in, nullptr, walk_kind_in), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700580
Ian Rogers5cf98192014-05-29 21:31:50 -0700581 bool VisitFrame() OVERRIDE {
Ian Rogers0399dde2012-06-06 17:09:28 -0700582 frames++;
583 return true;
584 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700585
Ian Rogers0399dde2012-06-06 17:09:28 -0700586 size_t frames;
587 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100588 NumFramesVisitor visitor(thread, walk_kind);
Ian Rogers0399dde2012-06-06 17:09:28 -0700589 visitor.WalkStack(true);
590 return visitor.frames;
591}
592
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700593bool StackVisitor::GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700594 struct HasMoreFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100595 HasMoreFramesVisitor(Thread* thread,
596 StackWalkKind walk_kind,
597 size_t num_frames,
598 size_t frame_height)
599 : StackVisitor(thread, nullptr, walk_kind, num_frames),
600 frame_height_(frame_height),
601 found_frame_(false),
602 has_more_frames_(false),
603 next_method_(nullptr),
604 next_dex_pc_(0) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700605 }
606
607 bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
608 if (found_frame_) {
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700609 ArtMethod* method = GetMethod();
Ian Rogers5cf98192014-05-29 21:31:50 -0700610 if (method != nullptr && !method->IsRuntimeMethod()) {
611 has_more_frames_ = true;
612 next_method_ = method;
613 next_dex_pc_ = GetDexPc();
614 return false; // End stack walk once next method is found.
615 }
616 } else if (GetFrameHeight() == frame_height_) {
617 found_frame_ = true;
618 }
619 return true;
620 }
621
622 size_t frame_height_;
623 bool found_frame_;
624 bool has_more_frames_;
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700625 ArtMethod* next_method_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700626 uint32_t next_dex_pc_;
627 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100628 HasMoreFramesVisitor visitor(thread_, walk_kind_, GetNumFrames(), GetFrameHeight());
Ian Rogers5cf98192014-05-29 21:31:50 -0700629 visitor.WalkStack(true);
630 *next_method = visitor.next_method_;
631 *next_dex_pc = visitor.next_dex_pc_;
632 return visitor.has_more_frames_;
633}
634
Ian Rogers7a22fa62013-01-23 12:16:16 -0800635void StackVisitor::DescribeStack(Thread* thread) {
Ian Rogers306057f2012-11-26 12:45:53 -0800636 struct DescribeStackVisitor : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800637 explicit DescribeStackVisitor(Thread* thread_in)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100638 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800639
Ian Rogers5cf98192014-05-29 21:31:50 -0700640 bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers306057f2012-11-26 12:45:53 -0800641 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation();
642 return true;
643 }
644 };
Ian Rogers7a22fa62013-01-23 12:16:16 -0800645 DescribeStackVisitor visitor(thread);
Ian Rogers306057f2012-11-26 12:45:53 -0800646 visitor.WalkStack(true);
647}
648
Ian Rogers40e3bac2012-11-20 00:09:14 -0800649std::string StackVisitor::DescribeLocation() const {
650 std::string result("Visiting method '");
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700651 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700652 if (m == nullptr) {
Ian Rogers306057f2012-11-26 12:45:53 -0800653 return "upcall";
654 }
655 result += PrettyMethod(m);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800656 result += StringPrintf("' at dex PC 0x%04x", GetDexPc());
Ian Rogers40e3bac2012-11-20 00:09:14 -0800657 if (!IsShadowFrame()) {
658 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc()));
659 }
660 return result;
661}
662
Ian Rogerse63db272014-07-15 15:36:11 -0700663static instrumentation::InstrumentationStackFrame& GetInstrumentationStackFrame(Thread* thread,
664 uint32_t depth) {
665 CHECK_LT(depth, thread->GetInstrumentationStack()->size());
666 return thread->GetInstrumentationStack()->at(depth);
Ian Rogers7a22fa62013-01-23 12:16:16 -0800667}
668
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700669void StackVisitor::SanityCheckFrame() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800670 if (kIsDebugBuild) {
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700671 ArtMethod* method = GetMethod();
672 auto* declaring_class = method->GetDeclaringClass();
673 // Runtime methods have null declaring class.
674 if (!method->IsRuntimeMethod()) {
675 CHECK(declaring_class != nullptr);
676 CHECK_EQ(declaring_class->GetClass(), declaring_class->GetClass()->GetClass())
677 << declaring_class;
678 } else {
679 CHECK(declaring_class == nullptr);
680 }
681 auto* runtime = Runtime::Current();
682 auto* la = runtime->GetLinearAlloc();
683 if (!la->Contains(method)) {
684 // Check image space.
685 bool in_image = false;
686 for (auto& space : runtime->GetHeap()->GetContinuousSpaces()) {
687 if (space->IsImageSpace()) {
688 auto* image_space = space->AsImageSpace();
689 const auto& header = image_space->GetImageHeader();
690 const auto* methods = &header.GetMethodsSection();
691 if (methods->Contains(reinterpret_cast<const uint8_t*>(method) - image_space->Begin())) {
692 in_image = true;
693 break;
694 }
695 }
696 }
697 CHECK(in_image) << PrettyMethod(method) << " not in linear alloc or image";
698 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800699 if (cur_quick_frame_ != nullptr) {
700 method->AssertPcIsWithinQuickCode(cur_quick_frame_pc_);
701 // Frame sanity.
702 size_t frame_size = method->GetFrameSizeInBytes();
703 CHECK_NE(frame_size, 0u);
Andreas Gampe5b417b92014-03-10 14:18:35 -0700704 // A rough guess at an upper size we expect to see for a frame.
705 // 256 registers
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700706 // 2 words HandleScope overhead
Andreas Gampe5b417b92014-03-10 14:18:35 -0700707 // 3+3 register spills
708 // TODO: this seems architecture specific for the case of JNI frames.
Brian Carlstromed08bd42014-03-19 18:34:17 -0700709 // TODO: 083-compiler-regressions ManyFloatArgs shows this estimate is wrong.
710 // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word);
711 const size_t kMaxExpectedFrameSize = 2 * KB;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800712 CHECK_LE(frame_size, kMaxExpectedFrameSize);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700713 size_t return_pc_offset = method->GetReturnPcOffset().SizeValue();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800714 CHECK_LT(return_pc_offset, frame_size);
715 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700716 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700717}
718
719void StackVisitor::WalkStack(bool include_transitions) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800720 DCHECK(thread_ == Thread::Current() || thread_->IsSuspended());
Ian Rogers62d6c772013-02-27 08:32:07 -0800721 CHECK_EQ(cur_depth_, 0U);
722 bool exit_stubs_installed = Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled();
jeffhao725a9572012-11-13 18:20:12 -0800723 uint32_t instrumentation_stack_depth = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700724
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700725 for (const ManagedStack* current_fragment = thread_->GetManagedStack();
726 current_fragment != nullptr; current_fragment = current_fragment->GetLink()) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700727 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
728 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700729 cur_quick_frame_pc_ = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700730
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700731 if (cur_quick_frame_ != nullptr) { // Handle quick stack frames.
Ian Rogers0399dde2012-06-06 17:09:28 -0700732 // Can't be both a shadow and a quick fragment.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700733 DCHECK(current_fragment->GetTopShadowFrame() == nullptr);
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700734 ArtMethod* method = *cur_quick_frame_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700735 while (method != nullptr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700736 SanityCheckFrame();
737 bool should_continue = VisitFrame();
738 if (UNLIKELY(!should_continue)) {
739 return;
Ian Rogers0399dde2012-06-06 17:09:28 -0700740 }
Dave Allison5cd33752014-04-15 15:57:58 -0700741
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700742 if (context_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700743 context_->FillCalleeSaves(*this);
744 }
745 size_t frame_size = method->GetFrameSizeInBytes();
746 // Compute PC for next stack frame from return PC.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700747 size_t return_pc_offset = method->GetReturnPcOffset(frame_size).SizeValue();
Ian Rogers13735952014-10-08 12:43:28 -0700748 uint8_t* return_pc_addr = reinterpret_cast<uint8_t*>(cur_quick_frame_) + return_pc_offset;
Ian Rogers0399dde2012-06-06 17:09:28 -0700749 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800750 if (UNLIKELY(exit_stubs_installed)) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700751 // While profiling, the return pc is restored from the side stack, except when walking
752 // the stack for an exception where the side stack will be unwound in VisitFrame.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700753 if (reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc) {
Sebastien Hertz74e256b2013-10-04 10:40:37 +0200754 const instrumentation::InstrumentationStackFrame& instrumentation_frame =
Ian Rogerse63db272014-07-15 15:36:11 -0700755 GetInstrumentationStackFrame(thread_, instrumentation_stack_depth);
jeffhao725a9572012-11-13 18:20:12 -0800756 instrumentation_stack_depth++;
Jeff Haofb2802d2013-07-24 13:53:05 -0700757 if (GetMethod() == Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveAll)) {
758 // Skip runtime save all callee frames which are used to deliver exceptions.
759 } else if (instrumentation_frame.interpreter_entry_) {
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700760 ArtMethod* callee = Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs);
Jeff Haofb2802d2013-07-24 13:53:05 -0700761 CHECK_EQ(GetMethod(), callee) << "Expected: " << PrettyMethod(callee) << " Found: "
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100762 << PrettyMethod(GetMethod());
Jeff Hao9a916d32013-06-27 18:45:37 -0700763 } else if (instrumentation_frame.method_ != GetMethod()) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800764 LOG(FATAL) << "Expected: " << PrettyMethod(instrumentation_frame.method_)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100765 << " Found: " << PrettyMethod(GetMethod());
Ian Rogers62d6c772013-02-27 08:32:07 -0800766 }
767 if (num_frames_ != 0) {
768 // Check agreement of frame Ids only if num_frames_ is computed to avoid infinite
769 // recursion.
770 CHECK(instrumentation_frame.frame_id_ == GetFrameId())
771 << "Expected: " << instrumentation_frame.frame_id_
772 << " Found: " << GetFrameId();
773 }
jeffhao725a9572012-11-13 18:20:12 -0800774 return_pc = instrumentation_frame.return_pc_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700775 }
776 }
777 cur_quick_frame_pc_ = return_pc;
Ian Rogers13735952014-10-08 12:43:28 -0700778 uint8_t* next_frame = reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size;
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700779 cur_quick_frame_ = reinterpret_cast<ArtMethod**>(next_frame);
780
781 if (kDebugStackWalk) {
782 LOG(INFO) << PrettyMethod(method) << "@" << method << " size=" << frame_size
783 << " optimized=" << method->IsOptimized(sizeof(void*))
784 << " native=" << method->IsNative()
785 << " entrypoints=" << method->GetEntryPointFromQuickCompiledCode()
786 << "," << method->GetEntryPointFromJni()
787 << "," << method->GetEntryPointFromInterpreter()
788 << " next=" << *cur_quick_frame_;
789 }
790
Ian Rogers0399dde2012-06-06 17:09:28 -0700791 cur_depth_++;
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700792 method = *cur_quick_frame_;
jeffhao6641ea12013-01-02 18:13:42 -0800793 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700794 } else if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700795 do {
796 SanityCheckFrame();
797 bool should_continue = VisitFrame();
798 if (UNLIKELY(!should_continue)) {
799 return;
800 }
801 cur_depth_++;
802 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700803 } while (cur_shadow_frame_ != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700804 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700805 if (include_transitions) {
806 bool should_continue = VisitFrame();
807 if (!should_continue) {
808 return;
809 }
810 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800811 cur_depth_++;
812 }
813 if (num_frames_ != 0) {
814 CHECK_EQ(cur_depth_, num_frames_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700815 }
816}
817
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800818void JavaFrameRootInfo::Describe(std::ostream& os) const {
819 const StackVisitor* visitor = stack_visitor_;
820 CHECK(visitor != nullptr);
821 os << "Type=" << GetType() << " thread_id=" << GetThreadId() << " location=" <<
822 visitor->DescribeLocation() << " vreg=" << vreg_;
823}
824
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700825int StackVisitor::GetVRegOffsetFromQuickCode(const DexFile::CodeItem* code_item,
826 uint32_t core_spills, uint32_t fp_spills,
827 size_t frame_size, int reg, InstructionSet isa) {
828 size_t pointer_size = InstructionSetPointerSize(isa);
829 if (kIsDebugBuild) {
830 auto* runtime = Runtime::Current();
831 if (runtime != nullptr) {
832 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
833 }
834 }
835 DCHECK_EQ(frame_size & (kStackAlignment - 1), 0U);
836 DCHECK_NE(reg, -1);
837 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
838 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
839 + sizeof(uint32_t); // Filler.
840 int num_regs = code_item->registers_size_ - code_item->ins_size_;
841 int temp_threshold = code_item->registers_size_;
842 const int max_num_special_temps = 1;
843 if (reg == temp_threshold) {
844 // The current method pointer corresponds to special location on stack.
845 return 0;
846 } else if (reg >= temp_threshold + max_num_special_temps) {
847 /*
848 * Special temporaries may have custom locations and the logic above deals with that.
849 * However, non-special temporaries are placed relative to the outs.
850 */
851 int temps_start = code_item->outs_size_ * sizeof(uint32_t) + pointer_size /* art method */;
852 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
853 return temps_start + relative_offset;
854 } else if (reg < num_regs) {
855 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
856 return locals_start + (reg * sizeof(uint32_t));
857 } else {
858 // Handle ins.
859 return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + pointer_size /* art method */;
860 }
861}
862
Elliott Hughes68e76522011-10-05 13:22:16 -0700863} // namespace art