blob: 23363654d225fd1248257549cbc3eba01f0389ed [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"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010027#include "jit/jit.h"
28#include "jit/jit_code_cache.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "linear_alloc.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "mirror/object-inl.h"
32#include "mirror/object_array-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010033#include "oat_quick_method_header.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010034#include "quick/quick_method_frame_info.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070035#include "runtime.h"
Dave Allisonf9439142014-03-27 15:10:22 -070036#include "thread.h"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070037#include "thread_list.h"
Mathieu Chartier4e305412014-02-19 10:54:44 -080038#include "verify_object-inl.h"
Ian Rogers1809a722013-08-09 22:05:32 -070039#include "vmap_table.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070040
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080041namespace art {
42
Mathieu Chartier8405bfd2016-02-05 12:00:49 -080043static constexpr bool kDebugStackWalk = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -070044
Ian Rogers62d6c772013-02-27 08:32:07 -080045mirror::Object* ShadowFrame::GetThisObject() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -070046 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -080047 if (m->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070048 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -080049 } else if (m->IsNative()) {
50 return GetVRegReference(0);
51 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070052 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070053 CHECK(code_item != nullptr) << PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -080054 uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
55 return GetVRegReference(reg);
56 }
57}
58
Jeff Haoe701f482013-05-24 11:50:49 -070059mirror::Object* ShadowFrame::GetThisObject(uint16_t num_ins) const {
Mathieu Chartiere401d142015-04-22 13:56:20 -070060 ArtMethod* m = GetMethod();
Jeff Haoe701f482013-05-24 11:50:49 -070061 if (m->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070062 return nullptr;
Jeff Haoe701f482013-05-24 11:50:49 -070063 } else {
Jeff Hao8d448852013-06-03 17:26:19 -070064 return GetVRegReference(NumberOfVRegs() - num_ins);
Jeff Haoe701f482013-05-24 11:50:49 -070065 }
66}
67
TDYa127ce4cc0d2012-11-18 16:59:53 -080068size_t ManagedStack::NumJniShadowFrameReferences() const {
Ian Rogers0399dde2012-06-06 17:09:28 -070069 size_t count = 0;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070070 for (const ManagedStack* current_fragment = this; current_fragment != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070071 current_fragment = current_fragment->GetLink()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070072 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070073 current_frame = current_frame->GetLink()) {
TDYa127ce4cc0d2012-11-18 16:59:53 -080074 if (current_frame->GetMethod()->IsNative()) {
75 // The JNI ShadowFrame only contains references. (For indirect reference.)
76 count += current_frame->NumberOfVRegs();
77 }
Ian Rogers0399dde2012-06-06 17:09:28 -070078 }
79 }
80 return count;
81}
82
Ian Rogersef7d42f2014-01-06 12:55:46 -080083bool ManagedStack::ShadowFramesContain(StackReference<mirror::Object>* shadow_frame_entry) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070084 for (const ManagedStack* current_fragment = this; current_fragment != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070085 current_fragment = current_fragment->GetLink()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070086 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -070087 current_frame = current_frame->GetLink()) {
88 if (current_frame->Contains(shadow_frame_entry)) {
89 return true;
90 }
91 }
92 }
93 return false;
94}
95
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010096StackVisitor::StackVisitor(Thread* thread, Context* context, StackWalkKind walk_kind)
97 : StackVisitor(thread, context, walk_kind, 0) {}
Ian Rogers7a22fa62013-01-23 12:16:16 -080098
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010099StackVisitor::StackVisitor(Thread* thread,
100 Context* context,
101 StackWalkKind walk_kind,
102 size_t num_frames)
103 : thread_(thread),
104 walk_kind_(walk_kind),
105 cur_shadow_frame_(nullptr),
106 cur_quick_frame_(nullptr),
107 cur_quick_frame_pc_(0),
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100108 cur_oat_quick_method_header_(nullptr),
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100109 num_frames_(num_frames),
110 cur_depth_(0),
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100111 current_inlining_depth_(0),
Ian Rogers5cf98192014-05-29 21:31:50 -0700112 context_(context) {
113 DCHECK(thread == Thread::Current() || thread->IsSuspended()) << *thread;
114}
115
Mathieu Chartiere401d142015-04-22 13:56:20 -0700116InlineInfo StackVisitor::GetCurrentInlineInfo() const {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100117 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
118 uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_);
119 CodeInfo code_info = method_header->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000120 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Brazdilf677ebf2015-05-29 16:29:43 +0100121 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100122 DCHECK(stack_map.IsValid());
David Brazdilf677ebf2015-05-29 16:29:43 +0100123 return code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100124}
125
Mathieu Chartiere401d142015-04-22 13:56:20 -0700126ArtMethod* StackVisitor::GetMethod() const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100127 if (cur_shadow_frame_ != nullptr) {
128 return cur_shadow_frame_->GetMethod();
129 } else if (cur_quick_frame_ != nullptr) {
130 if (IsInInlinedFrame()) {
131 size_t depth_in_stack_map = current_inlining_depth_ - 1;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100132 InlineInfo inline_info = GetCurrentInlineInfo();
Mathieu Chartier45bf2502016-03-31 11:07:09 -0700133 DCHECK(walk_kind_ != StackWalkKind::kSkipInlinedFrames);
134 bool allow_resolve = walk_kind_ != StackWalkKind::kIncludeInlinedFramesNoResolve;
135 return allow_resolve
136 ? GetResolvedMethod<true>(*GetCurrentQuickFrame(), inline_info, depth_in_stack_map)
137 : GetResolvedMethod<false>(*GetCurrentQuickFrame(), inline_info, depth_in_stack_map);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100138 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700139 return *cur_quick_frame_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100140 }
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100141 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700142 return nullptr;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100143}
144
Dave Allisonb373e092014-02-20 16:06:36 -0800145uint32_t StackVisitor::GetDexPc(bool abort_on_failure) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700146 if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700147 return cur_shadow_frame_->GetDexPC();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700148 } else if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100149 if (IsInInlinedFrame()) {
150 size_t depth_in_stack_map = current_inlining_depth_ - 1;
151 return GetCurrentInlineInfo().GetDexPcAtDepth(depth_in_stack_map);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100152 } else if (cur_oat_quick_method_header_ == nullptr) {
153 return DexFile::kDexNoIndex;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100154 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100155 return cur_oat_quick_method_header_->ToDexPc(
156 GetMethod(), cur_quick_frame_pc_, abort_on_failure);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100157 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700158 } else {
159 return 0;
160 }
161}
162
Mathieu Chartiere401d142015-04-22 13:56:20 -0700163extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700164 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertza836bc92014-11-25 16:30:53 +0100165
Ian Rogers62d6c772013-02-27 08:32:07 -0800166mirror::Object* StackVisitor::GetThisObject() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700167 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), sizeof(void*));
168 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800169 if (m->IsStatic()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100170 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800171 } else if (m->IsNative()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100172 if (cur_quick_frame_ != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700173 HandleScope* hs = reinterpret_cast<HandleScope*>(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100174 reinterpret_cast<char*>(cur_quick_frame_) + sizeof(ArtMethod*));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700175 return hs->GetReference(0);
Ian Rogers62d6c772013-02-27 08:32:07 -0800176 } else {
177 return cur_shadow_frame_->GetVRegReference(0);
178 }
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000179 } else if (m->IsProxyMethod()) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100180 if (cur_quick_frame_ != nullptr) {
181 return artQuickGetProxyThisObject(cur_quick_frame_);
182 } else {
183 return cur_shadow_frame_->GetVRegReference(0);
184 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800185 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700186 const DexFile::CodeItem* code_item = m->GetCodeItem();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100187 if (code_item == nullptr) {
Ian Rogerse0dcd462014-03-08 15:21:04 -0800188 UNIMPLEMENTED(ERROR) << "Failed to determine this object of abstract or proxy method: "
Ian Rogers62d6c772013-02-27 08:32:07 -0800189 << PrettyMethod(m);
Ian Rogerse0dcd462014-03-08 15:21:04 -0800190 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800191 } else {
192 uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000193 uint32_t value = 0;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700194 bool success = GetVReg(m, reg, kReferenceVReg, &value);
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000195 // We currently always guarantee the `this` object is live throughout the method.
196 CHECK(success) << "Failed to read the this object in " << PrettyMethod(m);
197 return reinterpret_cast<mirror::Object*>(value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800198 }
199 }
200}
201
Ian Rogers0c7abda2012-09-19 13:33:42 -0700202size_t StackVisitor::GetNativePcOffset() const {
203 DCHECK(!IsShadowFrame());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100204 return GetCurrentOatQuickMethodHeader()->NativeQuickPcOffset(cur_quick_frame_pc_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700205}
206
Mathieu Chartiere401d142015-04-22 13:56:20 -0700207bool StackVisitor::IsReferenceVReg(ArtMethod* m, uint16_t vreg) {
Nicolas Geoffrayccc61972015-10-01 14:34:20 +0100208 DCHECK_EQ(m, GetMethod());
Mathieu Chartier50030ef2015-05-08 14:19:26 -0700209 // Process register map (which native and runtime methods don't have)
210 if (m->IsNative() || m->IsRuntimeMethod() || m->IsProxyMethod()) {
211 return false;
212 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100213 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
214 if (method_header->IsOptimized()) {
Mathieu Chartier50030ef2015-05-08 14:19:26 -0700215 return true; // TODO: Implement.
216 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100217 const uint8_t* native_gc_map = method_header->GetNativeGcMap();
Mathieu Chartier50030ef2015-05-08 14:19:26 -0700218 CHECK(native_gc_map != nullptr) << PrettyMethod(m);
219 const DexFile::CodeItem* code_item = m->GetCodeItem();
220 // Can't be null or how would we compile its instructions?
221 DCHECK(code_item != nullptr) << PrettyMethod(m);
222 NativePcOffsetToReferenceMap map(native_gc_map);
223 size_t num_regs = std::min(map.RegWidth() * 8, static_cast<size_t>(code_item->registers_size_));
224 const uint8_t* reg_bitmap = nullptr;
225 if (num_regs > 0) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100226 uintptr_t native_pc_offset = method_header->NativeQuickPcOffset(GetCurrentQuickFramePc());
Mathieu Chartier50030ef2015-05-08 14:19:26 -0700227 reg_bitmap = map.FindBitMap(native_pc_offset);
228 DCHECK(reg_bitmap != nullptr);
229 }
230 // Does this register hold a reference?
231 return vreg < num_regs && TestBitmap(vreg, reg_bitmap);
232}
233
Mingyao Yang99170c62015-07-06 11:10:37 -0700234bool StackVisitor::GetVRegFromDebuggerShadowFrame(uint16_t vreg,
235 VRegKind kind,
236 uint32_t* val) const {
237 size_t frame_id = const_cast<StackVisitor*>(this)->GetFrameId();
238 ShadowFrame* shadow_frame = thread_->FindDebuggerShadowFrame(frame_id);
239 if (shadow_frame != nullptr) {
240 bool* updated_vreg_flags = thread_->GetUpdatedVRegFlags(frame_id);
241 DCHECK(updated_vreg_flags != nullptr);
242 if (updated_vreg_flags[vreg]) {
243 // Value is set by the debugger.
244 if (kind == kReferenceVReg) {
245 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
246 shadow_frame->GetVRegReference(vreg)));
247 } else {
248 *val = shadow_frame->GetVReg(vreg);
249 }
250 return true;
251 }
252 }
253 // No value is set by the debugger.
254 return false;
255}
256
Mathieu Chartiere401d142015-04-22 13:56:20 -0700257bool StackVisitor::GetVReg(ArtMethod* m, uint16_t vreg, VRegKind kind, uint32_t* val) const {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200258 if (cur_quick_frame_ != nullptr) {
259 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800260 DCHECK(m == GetMethod());
Mingyao Yang99170c62015-07-06 11:10:37 -0700261 // Check if there is value set by the debugger.
262 if (GetVRegFromDebuggerShadowFrame(vreg, kind, val)) {
263 return true;
264 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100265 if (cur_oat_quick_method_header_->IsOptimized()) {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100266 return GetVRegFromOptimizedCode(m, vreg, kind, val);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700267 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100268 return GetVRegFromQuickCode(m, vreg, kind, val);
Ian Rogers0399dde2012-06-06 17:09:28 -0700269 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700270 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100271 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertz09687442015-11-17 10:35:39 +0100272 if (kind == kReferenceVReg) {
273 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
274 cur_shadow_frame_->GetVRegReference(vreg)));
275 } else {
276 *val = cur_shadow_frame_->GetVReg(vreg);
277 }
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200278 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700279 }
280}
281
Mathieu Chartiere401d142015-04-22 13:56:20 -0700282bool StackVisitor::GetVRegFromQuickCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100283 uint32_t* val) const {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100284 DCHECK_EQ(m, GetMethod());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100285 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
286 QuickMethodFrameInfo frame_info = method_header->GetFrameInfo();
287 const VmapTable vmap_table(method_header->GetVmapTable());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100288 uint32_t vmap_offset;
289 // TODO: IsInContext stops before spotting floating point registers.
290 if (vmap_table.IsInContext(vreg, kind, &vmap_offset)) {
291 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
292 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
293 uint32_t reg = vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
294 return GetRegisterIfAccessible(reg, kind, val);
295 } else {
296 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700297 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100298 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000299 *val = *GetVRegAddrFromQuickCode(cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
300 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100301 return true;
302 }
303}
304
Mathieu Chartiere401d142015-04-22 13:56:20 -0700305bool StackVisitor::GetVRegFromOptimizedCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100306 uint32_t* val) const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100307 DCHECK_EQ(m, GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100308 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700309 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100310 // its instructions?
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000311 uint16_t number_of_dex_registers = code_item->registers_size_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100312 DCHECK_LT(vreg, code_item->registers_size_);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100313 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
314 CodeInfo code_info = method_header->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000315 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100316
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100317 uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100318 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100319 DCHECK(stack_map.IsValid());
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100320 size_t depth_in_stack_map = current_inlining_depth_ - 1;
321
322 DexRegisterMap dex_register_map = IsInInlinedFrame()
David Brazdilf677ebf2015-05-29 16:29:43 +0100323 ? code_info.GetDexRegisterMapAtDepth(depth_in_stack_map,
324 code_info.GetInlineInfoOf(stack_map, encoding),
325 encoding,
326 number_of_dex_registers)
327 : code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100328
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000329 if (!dex_register_map.IsValid()) {
330 return false;
331 }
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000332 DexRegisterLocation::Kind location_kind =
David Brazdilf677ebf2015-05-29 16:29:43 +0100333 dex_register_map.GetLocationKind(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100334 switch (location_kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000335 case DexRegisterLocation::Kind::kInStack: {
David Brazdilf677ebf2015-05-29 16:29:43 +0100336 const int32_t offset = dex_register_map.GetStackOffsetInBytes(vreg,
337 number_of_dex_registers,
338 code_info,
339 encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100340 const uint8_t* addr = reinterpret_cast<const uint8_t*>(cur_quick_frame_) + offset;
341 *val = *reinterpret_cast<const uint32_t*>(addr);
342 return true;
343 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000344 case DexRegisterLocation::Kind::kInRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100345 case DexRegisterLocation::Kind::kInRegisterHigh:
346 case DexRegisterLocation::Kind::kInFpuRegister:
347 case DexRegisterLocation::Kind::kInFpuRegisterHigh: {
David Brazdilf677ebf2015-05-29 16:29:43 +0100348 uint32_t reg =
349 dex_register_map.GetMachineRegister(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100350 return GetRegisterIfAccessible(reg, kind, val);
351 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000352 case DexRegisterLocation::Kind::kConstant:
David Brazdilf677ebf2015-05-29 16:29:43 +0100353 *val = dex_register_map.GetConstant(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100354 return true;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000355 case DexRegisterLocation::Kind::kNone:
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100356 return false;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000357 default:
358 LOG(FATAL)
David Srbecky7dc11782016-02-25 13:23:56 +0000359 << "Unexpected location kind "
360 << dex_register_map.GetLocationInternalKind(vreg,
361 number_of_dex_registers,
362 code_info,
363 encoding);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000364 UNREACHABLE();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100365 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100366}
367
368bool StackVisitor::GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const {
369 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
David Brazdil77a48ae2015-09-15 12:34:04 +0000370
371 // X86 float registers are 64-bit and the logic below does not apply.
372 DCHECK(!is_float || kRuntimeISA != InstructionSet::kX86);
373
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100374 if (!IsAccessibleRegister(reg, is_float)) {
375 return false;
376 }
377 uintptr_t ptr_val = GetRegister(reg, is_float);
378 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
379 if (target64) {
380 const bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
381 const bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
382 int64_t value_long = static_cast<int64_t>(ptr_val);
383 if (wide_lo) {
384 ptr_val = static_cast<uintptr_t>(Low32Bits(value_long));
385 } else if (wide_hi) {
386 ptr_val = static_cast<uintptr_t>(High32Bits(value_long));
387 }
388 }
389 *val = ptr_val;
390 return true;
391}
392
Mingyao Yang99170c62015-07-06 11:10:37 -0700393bool StackVisitor::GetVRegPairFromDebuggerShadowFrame(uint16_t vreg,
394 VRegKind kind_lo,
395 VRegKind kind_hi,
396 uint64_t* val) const {
397 uint32_t low_32bits;
398 uint32_t high_32bits;
399 bool success = GetVRegFromDebuggerShadowFrame(vreg, kind_lo, &low_32bits);
400 success &= GetVRegFromDebuggerShadowFrame(vreg + 1, kind_hi, &high_32bits);
401 if (success) {
402 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
403 }
404 return success;
405}
406
Mathieu Chartiere401d142015-04-22 13:56:20 -0700407bool StackVisitor::GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200408 VRegKind kind_hi, uint64_t* val) const {
409 if (kind_lo == kLongLoVReg) {
410 DCHECK_EQ(kind_hi, kLongHiVReg);
411 } else if (kind_lo == kDoubleLoVReg) {
412 DCHECK_EQ(kind_hi, kDoubleHiVReg);
413 } else {
414 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100415 UNREACHABLE();
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200416 }
Mingyao Yang99170c62015-07-06 11:10:37 -0700417 // Check if there is value set by the debugger.
418 if (GetVRegPairFromDebuggerShadowFrame(vreg, kind_lo, kind_hi, val)) {
419 return true;
420 }
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200421 if (cur_quick_frame_ != nullptr) {
422 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
423 DCHECK(m == GetMethod());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100424 if (cur_oat_quick_method_header_->IsOptimized()) {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100425 return GetVRegPairFromOptimizedCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200426 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100427 return GetVRegPairFromQuickCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200428 }
429 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100430 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200431 *val = cur_shadow_frame_->GetVRegLong(vreg);
432 return true;
433 }
434}
435
Mathieu Chartiere401d142015-04-22 13:56:20 -0700436bool StackVisitor::GetVRegPairFromQuickCode(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100437 VRegKind kind_hi, uint64_t* val) const {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100438 DCHECK_EQ(m, GetMethod());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100439 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
440 QuickMethodFrameInfo frame_info = method_header->GetFrameInfo();
441 const VmapTable vmap_table(method_header->GetVmapTable());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100442 uint32_t vmap_offset_lo, vmap_offset_hi;
443 // TODO: IsInContext stops before spotting floating point registers.
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700444 if (vmap_table.IsInContext(vreg, kind_lo, &vmap_offset_lo) &&
445 vmap_table.IsInContext(vreg + 1, kind_hi, &vmap_offset_hi)) {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100446 bool is_float = (kind_lo == kDoubleLoVReg);
447 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
448 uint32_t reg_lo = vmap_table.ComputeRegister(spill_mask, vmap_offset_lo, kind_lo);
449 uint32_t reg_hi = vmap_table.ComputeRegister(spill_mask, vmap_offset_hi, kind_hi);
450 return GetRegisterPairIfAccessible(reg_lo, reg_hi, kind_lo, val);
451 } else {
452 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700453 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100454 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000455 uint32_t* addr = GetVRegAddrFromQuickCode(
456 cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
457 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100458 *val = *reinterpret_cast<uint64_t*>(addr);
459 return true;
460 }
461}
462
Mathieu Chartiere401d142015-04-22 13:56:20 -0700463bool StackVisitor::GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100464 VRegKind kind_lo, VRegKind kind_hi,
465 uint64_t* val) const {
466 uint32_t low_32bits;
467 uint32_t high_32bits;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700468 bool success = GetVRegFromOptimizedCode(m, vreg, kind_lo, &low_32bits);
469 success &= GetVRegFromOptimizedCode(m, vreg + 1, kind_hi, &high_32bits);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100470 if (success) {
471 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
472 }
473 return success;
474}
475
476bool StackVisitor::GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
477 VRegKind kind_lo, uint64_t* val) const {
478 const bool is_float = (kind_lo == kDoubleLoVReg);
479 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
480 return false;
481 }
482 uintptr_t ptr_val_lo = GetRegister(reg_lo, is_float);
483 uintptr_t ptr_val_hi = GetRegister(reg_hi, is_float);
484 bool target64 = Is64BitInstructionSet(kRuntimeISA);
485 if (target64) {
486 int64_t value_long_lo = static_cast<int64_t>(ptr_val_lo);
487 int64_t value_long_hi = static_cast<int64_t>(ptr_val_hi);
488 ptr_val_lo = static_cast<uintptr_t>(Low32Bits(value_long_lo));
489 ptr_val_hi = static_cast<uintptr_t>(High32Bits(value_long_hi));
490 }
491 *val = (static_cast<uint64_t>(ptr_val_hi) << 32) | static_cast<uint32_t>(ptr_val_lo);
492 return true;
493}
494
Mingyao Yang636b9252015-07-31 16:40:24 -0700495bool StackVisitor::SetVReg(ArtMethod* m,
496 uint16_t vreg,
497 uint32_t new_value,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800498 VRegKind kind) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700499 const DexFile::CodeItem* code_item = m->GetCodeItem();
500 if (code_item == nullptr) {
501 return false;
502 }
503 ShadowFrame* shadow_frame = GetCurrentShadowFrame();
504 if (shadow_frame == nullptr) {
505 // This is a compiled frame: we must prepare and update a shadow frame that will
506 // be executed by the interpreter after deoptimization of the stack.
507 const size_t frame_id = GetFrameId();
508 const uint16_t num_regs = code_item->registers_size_;
509 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
510 CHECK(shadow_frame != nullptr);
511 // Remember the vreg has been set for debugging and must not be overwritten by the
512 // original value during deoptimization of the stack.
513 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
514 }
515 if (kind == kReferenceVReg) {
516 shadow_frame->SetVRegReference(vreg, reinterpret_cast<mirror::Object*>(new_value));
517 } else {
518 shadow_frame->SetVReg(vreg, new_value);
519 }
520 return true;
521}
522
Mingyao Yang636b9252015-07-31 16:40:24 -0700523bool StackVisitor::SetVRegPair(ArtMethod* m,
524 uint16_t vreg,
525 uint64_t new_value,
526 VRegKind kind_lo,
527 VRegKind kind_hi) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700528 if (kind_lo == kLongLoVReg) {
529 DCHECK_EQ(kind_hi, kLongHiVReg);
530 } else if (kind_lo == kDoubleLoVReg) {
531 DCHECK_EQ(kind_hi, kDoubleHiVReg);
532 } else {
533 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
534 UNREACHABLE();
535 }
536 const DexFile::CodeItem* code_item = m->GetCodeItem();
537 if (code_item == nullptr) {
538 return false;
539 }
540 ShadowFrame* shadow_frame = GetCurrentShadowFrame();
541 if (shadow_frame == nullptr) {
542 // This is a compiled frame: we must prepare for deoptimization (see SetVRegFromDebugger).
543 const size_t frame_id = GetFrameId();
544 const uint16_t num_regs = code_item->registers_size_;
545 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
546 CHECK(shadow_frame != nullptr);
547 // Remember the vreg pair has been set for debugging and must not be overwritten by the
548 // original value during deoptimization of the stack.
549 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
550 thread_->GetUpdatedVRegFlags(frame_id)[vreg + 1] = true;
551 }
552 shadow_frame->SetVRegLong(vreg, new_value);
553 return true;
554}
555
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100556bool StackVisitor::IsAccessibleGPR(uint32_t reg) const {
557 DCHECK(context_ != nullptr);
558 return context_->IsAccessibleGPR(reg);
559}
560
Mathieu Chartier815873e2014-02-13 18:02:13 -0800561uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100562 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
563 DCHECK(context_ != nullptr);
Mathieu Chartier815873e2014-02-13 18:02:13 -0800564 return context_->GetGPRAddress(reg);
565}
566
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100567uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
568 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
569 DCHECK(context_ != nullptr);
570 return context_->GetGPR(reg);
Ian Rogers0399dde2012-06-06 17:09:28 -0700571}
572
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100573bool StackVisitor::IsAccessibleFPR(uint32_t reg) const {
574 DCHECK(context_ != nullptr);
575 return context_->IsAccessibleFPR(reg);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200576}
577
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100578uintptr_t StackVisitor::GetFPR(uint32_t reg) const {
579 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
580 DCHECK(context_ != nullptr);
581 return context_->GetFPR(reg);
582}
583
Ian Rogers0399dde2012-06-06 17:09:28 -0700584uintptr_t StackVisitor::GetReturnPc() const {
Ian Rogers13735952014-10-08 12:43:28 -0700585 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700586 DCHECK(sp != nullptr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100587 uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogers0399dde2012-06-06 17:09:28 -0700588 return *reinterpret_cast<uintptr_t*>(pc_addr);
589}
590
591void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
Ian Rogers13735952014-10-08 12:43:28 -0700592 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700593 CHECK(sp != nullptr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100594 uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogers0399dde2012-06-06 17:09:28 -0700595 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc;
596}
597
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100598size_t StackVisitor::ComputeNumFrames(Thread* thread, StackWalkKind walk_kind) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700599 struct NumFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100600 NumFramesVisitor(Thread* thread_in, StackWalkKind walk_kind_in)
601 : StackVisitor(thread_in, nullptr, walk_kind_in), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700602
Ian Rogers5cf98192014-05-29 21:31:50 -0700603 bool VisitFrame() OVERRIDE {
Ian Rogers0399dde2012-06-06 17:09:28 -0700604 frames++;
605 return true;
606 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700607
Ian Rogers0399dde2012-06-06 17:09:28 -0700608 size_t frames;
609 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100610 NumFramesVisitor visitor(thread, walk_kind);
Ian Rogers0399dde2012-06-06 17:09:28 -0700611 visitor.WalkStack(true);
612 return visitor.frames;
613}
614
Mathieu Chartiere401d142015-04-22 13:56:20 -0700615bool StackVisitor::GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700616 struct HasMoreFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100617 HasMoreFramesVisitor(Thread* thread,
618 StackWalkKind walk_kind,
619 size_t num_frames,
620 size_t frame_height)
621 : StackVisitor(thread, nullptr, walk_kind, num_frames),
622 frame_height_(frame_height),
623 found_frame_(false),
624 has_more_frames_(false),
625 next_method_(nullptr),
626 next_dex_pc_(0) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700627 }
628
Mathieu Chartier90443472015-07-16 20:32:27 -0700629 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700630 if (found_frame_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700631 ArtMethod* method = GetMethod();
Ian Rogers5cf98192014-05-29 21:31:50 -0700632 if (method != nullptr && !method->IsRuntimeMethod()) {
633 has_more_frames_ = true;
634 next_method_ = method;
635 next_dex_pc_ = GetDexPc();
636 return false; // End stack walk once next method is found.
637 }
638 } else if (GetFrameHeight() == frame_height_) {
639 found_frame_ = true;
640 }
641 return true;
642 }
643
644 size_t frame_height_;
645 bool found_frame_;
646 bool has_more_frames_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700647 ArtMethod* next_method_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700648 uint32_t next_dex_pc_;
649 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100650 HasMoreFramesVisitor visitor(thread_, walk_kind_, GetNumFrames(), GetFrameHeight());
Ian Rogers5cf98192014-05-29 21:31:50 -0700651 visitor.WalkStack(true);
652 *next_method = visitor.next_method_;
653 *next_dex_pc = visitor.next_dex_pc_;
654 return visitor.has_more_frames_;
655}
656
Ian Rogers7a22fa62013-01-23 12:16:16 -0800657void StackVisitor::DescribeStack(Thread* thread) {
Ian Rogers306057f2012-11-26 12:45:53 -0800658 struct DescribeStackVisitor : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800659 explicit DescribeStackVisitor(Thread* thread_in)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100660 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800661
Mathieu Chartier90443472015-07-16 20:32:27 -0700662 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers306057f2012-11-26 12:45:53 -0800663 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation();
664 return true;
665 }
666 };
Ian Rogers7a22fa62013-01-23 12:16:16 -0800667 DescribeStackVisitor visitor(thread);
Ian Rogers306057f2012-11-26 12:45:53 -0800668 visitor.WalkStack(true);
669}
670
Ian Rogers40e3bac2012-11-20 00:09:14 -0800671std::string StackVisitor::DescribeLocation() const {
672 std::string result("Visiting method '");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700673 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700674 if (m == nullptr) {
Ian Rogers306057f2012-11-26 12:45:53 -0800675 return "upcall";
676 }
677 result += PrettyMethod(m);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800678 result += StringPrintf("' at dex PC 0x%04x", GetDexPc());
Ian Rogers40e3bac2012-11-20 00:09:14 -0800679 if (!IsShadowFrame()) {
680 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc()));
681 }
682 return result;
683}
684
Ian Rogerse63db272014-07-15 15:36:11 -0700685static instrumentation::InstrumentationStackFrame& GetInstrumentationStackFrame(Thread* thread,
686 uint32_t depth) {
687 CHECK_LT(depth, thread->GetInstrumentationStack()->size());
688 return thread->GetInstrumentationStack()->at(depth);
Ian Rogers7a22fa62013-01-23 12:16:16 -0800689}
690
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100691static void AssertPcIsWithinQuickCode(ArtMethod* method, uintptr_t pc)
692 SHARED_REQUIRES(Locks::mutator_lock_) {
693 if (method->IsNative() || method->IsRuntimeMethod() || method->IsProxyMethod()) {
694 return;
695 }
696
697 if (pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) {
698 return;
699 }
700
701 const void* code = method->GetEntryPointFromQuickCompiledCode();
702 if (code == GetQuickInstrumentationEntryPoint()) {
703 return;
704 }
705
706 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
707 if (class_linker->IsQuickToInterpreterBridge(code) ||
708 class_linker->IsQuickResolutionStub(code)) {
709 return;
710 }
711
712 // If we are the JIT then we may have just compiled the method after the
713 // IsQuickToInterpreterBridge check.
714 jit::Jit* const jit = Runtime::Current()->GetJit();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100715 if (jit != nullptr && jit->GetCodeCache()->ContainsPc(code)) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100716 return;
717 }
718
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100719 uint32_t code_size = OatQuickMethodHeader::FromEntryPoint(code)->code_size_;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100720 uintptr_t code_start = reinterpret_cast<uintptr_t>(code);
721 CHECK(code_start <= pc && pc <= (code_start + code_size))
722 << PrettyMethod(method)
723 << " pc=" << std::hex << pc
Roland Levillain0d5a2812015-11-13 10:07:31 +0000724 << " code_start=" << code_start
725 << " code_size=" << code_size;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100726}
727
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700728void StackVisitor::SanityCheckFrame() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800729 if (kIsDebugBuild) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700730 ArtMethod* method = GetMethod();
731 auto* declaring_class = method->GetDeclaringClass();
732 // Runtime methods have null declaring class.
733 if (!method->IsRuntimeMethod()) {
734 CHECK(declaring_class != nullptr);
735 CHECK_EQ(declaring_class->GetClass(), declaring_class->GetClass()->GetClass())
736 << declaring_class;
737 } else {
738 CHECK(declaring_class == nullptr);
739 }
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700740 Runtime* const runtime = Runtime::Current();
741 LinearAlloc* const linear_alloc = runtime->GetLinearAlloc();
742 if (!linear_alloc->Contains(method)) {
743 // Check class linker linear allocs.
744 mirror::Class* klass = method->GetDeclaringClass();
745 LinearAlloc* const class_linear_alloc = (klass != nullptr)
Mathieu Chartier5b830502016-03-02 10:30:23 -0800746 ? runtime->GetClassLinker()->GetAllocatorForClassLoader(klass->GetClassLoader())
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700747 : linear_alloc;
748 if (!class_linear_alloc->Contains(method)) {
749 // Check image space.
750 bool in_image = false;
751 for (auto& space : runtime->GetHeap()->GetContinuousSpaces()) {
752 if (space->IsImageSpace()) {
753 auto* image_space = space->AsImageSpace();
754 const auto& header = image_space->GetImageHeader();
755 const auto* methods = &header.GetMethodsSection();
756 if (methods->Contains(reinterpret_cast<const uint8_t*>(method) - image_space->Begin())) {
757 in_image = true;
758 break;
759 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700760 }
761 }
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700762 CHECK(in_image) << PrettyMethod(method) << " not in linear alloc or image";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700763 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700764 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800765 if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100766 AssertPcIsWithinQuickCode(method, cur_quick_frame_pc_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800767 // Frame sanity.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100768 size_t frame_size = GetCurrentQuickFrameInfo().FrameSizeInBytes();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800769 CHECK_NE(frame_size, 0u);
Andreas Gampe5b417b92014-03-10 14:18:35 -0700770 // A rough guess at an upper size we expect to see for a frame.
771 // 256 registers
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700772 // 2 words HandleScope overhead
Andreas Gampe5b417b92014-03-10 14:18:35 -0700773 // 3+3 register spills
774 // TODO: this seems architecture specific for the case of JNI frames.
Brian Carlstromed08bd42014-03-19 18:34:17 -0700775 // TODO: 083-compiler-regressions ManyFloatArgs shows this estimate is wrong.
776 // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word);
777 const size_t kMaxExpectedFrameSize = 2 * KB;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100778 CHECK_LE(frame_size, kMaxExpectedFrameSize) << PrettyMethod(method);
779 size_t return_pc_offset = GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800780 CHECK_LT(return_pc_offset, frame_size);
781 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700782 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700783}
784
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100785// Counts the number of references in the parameter list of the corresponding method.
786// Note: Thus does _not_ include "this" for non-static methods.
787static uint32_t GetNumberOfReferenceArgsWithoutReceiver(ArtMethod* method)
788 SHARED_REQUIRES(Locks::mutator_lock_) {
789 uint32_t shorty_len;
790 const char* shorty = method->GetShorty(&shorty_len);
791 uint32_t refs = 0;
792 for (uint32_t i = 1; i < shorty_len ; ++i) {
793 if (shorty[i] == 'L') {
794 refs++;
795 }
796 }
797 return refs;
798}
799
800QuickMethodFrameInfo StackVisitor::GetCurrentQuickFrameInfo() const {
801 if (cur_oat_quick_method_header_ != nullptr) {
802 return cur_oat_quick_method_header_->GetFrameInfo();
803 }
804
805 ArtMethod* method = GetMethod();
806 Runtime* runtime = Runtime::Current();
807
808 if (method->IsAbstract()) {
809 return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs);
810 }
811
812 // This goes before IsProxyMethod since runtime methods have a null declaring class.
813 if (method->IsRuntimeMethod()) {
814 return runtime->GetRuntimeMethodFrameInfo(method);
815 }
816
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100817 if (method->IsProxyMethod()) {
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000818 // There is only one direct method of a proxy class: the constructor. A direct method is
819 // cloned from the original java.lang.reflect.Proxy and is executed as usual quick
820 // compiled method without any stubs. Therefore the method must have a OatQuickMethodHeader.
821 DCHECK(!method->IsDirect() && !method->IsConstructor())
822 << "Constructors of proxy classes must have a OatQuickMethodHeader";
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000823 return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100824 }
825
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000826 // The only remaining case is if the method is native and uses the generic JNI stub.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100827 DCHECK(method->IsNative());
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000828 ClassLinker* class_linker = runtime->GetClassLinker();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100829 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(method, sizeof(void*));
830 DCHECK(class_linker->IsQuickGenericJniStub(entry_point)) << PrettyMethod(method);
831 // Generic JNI frame.
832 uint32_t handle_refs = GetNumberOfReferenceArgsWithoutReceiver(method) + 1;
833 size_t scope_size = HandleScope::SizeOf(handle_refs);
834 QuickMethodFrameInfo callee_info = runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs);
835
836 // Callee saves + handle scope + method ref + alignment
837 // Note: -sizeof(void*) since callee-save frame stores a whole method pointer.
838 size_t frame_size = RoundUp(
839 callee_info.FrameSizeInBytes() - sizeof(void*) + sizeof(ArtMethod*) + scope_size,
840 kStackAlignment);
841 return QuickMethodFrameInfo(frame_size, callee_info.CoreSpillMask(), callee_info.FpSpillMask());
842}
843
Ian Rogers0399dde2012-06-06 17:09:28 -0700844void StackVisitor::WalkStack(bool include_transitions) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800845 DCHECK(thread_ == Thread::Current() || thread_->IsSuspended());
Ian Rogers62d6c772013-02-27 08:32:07 -0800846 CHECK_EQ(cur_depth_, 0U);
847 bool exit_stubs_installed = Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled();
jeffhao725a9572012-11-13 18:20:12 -0800848 uint32_t instrumentation_stack_depth = 0;
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000849 size_t inlined_frames_count = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700850
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700851 for (const ManagedStack* current_fragment = thread_->GetManagedStack();
852 current_fragment != nullptr; current_fragment = current_fragment->GetLink()) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700853 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
854 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700855 cur_quick_frame_pc_ = 0;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100856 cur_oat_quick_method_header_ = nullptr;
Dave Allisonf9439142014-03-27 15:10:22 -0700857
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700858 if (cur_quick_frame_ != nullptr) { // Handle quick stack frames.
Ian Rogers0399dde2012-06-06 17:09:28 -0700859 // Can't be both a shadow and a quick fragment.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700860 DCHECK(current_fragment->GetTopShadowFrame() == nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700861 ArtMethod* method = *cur_quick_frame_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700862 while (method != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100863 cur_oat_quick_method_header_ = method->GetOatQuickMethodHeader(cur_quick_frame_pc_);
Dave Allison5cd33752014-04-15 15:57:58 -0700864 SanityCheckFrame();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100865
Mathieu Chartier45bf2502016-03-31 11:07:09 -0700866 if ((walk_kind_ == StackWalkKind::kIncludeInlinedFrames ||
867 walk_kind_ == StackWalkKind::kIncludeInlinedFramesNoResolve)
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100868 && (cur_oat_quick_method_header_ != nullptr)
869 && cur_oat_quick_method_header_->IsOptimized()) {
870 CodeInfo code_info = cur_oat_quick_method_header_->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000871 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100872 uint32_t native_pc_offset =
873 cur_oat_quick_method_header_->NativeQuickPcOffset(cur_quick_frame_pc_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100874 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
David Srbecky09ed0982016-02-12 21:58:43 +0000875 if (stack_map.IsValid() && stack_map.HasInlineInfo(encoding.stack_map_encoding)) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100876 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100877 DCHECK_EQ(current_inlining_depth_, 0u);
878 for (current_inlining_depth_ = inline_info.GetDepth();
879 current_inlining_depth_ != 0;
880 --current_inlining_depth_) {
881 bool should_continue = VisitFrame();
882 if (UNLIKELY(!should_continue)) {
883 return;
884 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100885 cur_depth_++;
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000886 inlined_frames_count++;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100887 }
888 }
889 }
890
Dave Allison5cd33752014-04-15 15:57:58 -0700891 bool should_continue = VisitFrame();
892 if (UNLIKELY(!should_continue)) {
893 return;
Ian Rogers0399dde2012-06-06 17:09:28 -0700894 }
Dave Allison5cd33752014-04-15 15:57:58 -0700895
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100896 QuickMethodFrameInfo frame_info = GetCurrentQuickFrameInfo();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700897 if (context_ != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100898 context_->FillCalleeSaves(reinterpret_cast<uint8_t*>(cur_quick_frame_), frame_info);
Ian Rogers0399dde2012-06-06 17:09:28 -0700899 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700900 // Compute PC for next stack frame from return PC.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100901 size_t frame_size = frame_info.FrameSizeInBytes();
902 size_t return_pc_offset = frame_size - sizeof(void*);
Ian Rogers13735952014-10-08 12:43:28 -0700903 uint8_t* return_pc_addr = reinterpret_cast<uint8_t*>(cur_quick_frame_) + return_pc_offset;
Ian Rogers0399dde2012-06-06 17:09:28 -0700904 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100905
Ian Rogers62d6c772013-02-27 08:32:07 -0800906 if (UNLIKELY(exit_stubs_installed)) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700907 // While profiling, the return pc is restored from the side stack, except when walking
908 // the stack for an exception where the side stack will be unwound in VisitFrame.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700909 if (reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc) {
Sebastien Hertz74e256b2013-10-04 10:40:37 +0200910 const instrumentation::InstrumentationStackFrame& instrumentation_frame =
Ian Rogerse63db272014-07-15 15:36:11 -0700911 GetInstrumentationStackFrame(thread_, instrumentation_stack_depth);
jeffhao725a9572012-11-13 18:20:12 -0800912 instrumentation_stack_depth++;
Jeff Haofb2802d2013-07-24 13:53:05 -0700913 if (GetMethod() == Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveAll)) {
914 // Skip runtime save all callee frames which are used to deliver exceptions.
915 } else if (instrumentation_frame.interpreter_entry_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700916 ArtMethod* callee = Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs);
Jeff Haofb2802d2013-07-24 13:53:05 -0700917 CHECK_EQ(GetMethod(), callee) << "Expected: " << PrettyMethod(callee) << " Found: "
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100918 << PrettyMethod(GetMethod());
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000919 } else {
920 CHECK_EQ(instrumentation_frame.method_, GetMethod())
921 << "Expected: " << PrettyMethod(instrumentation_frame.method_)
922 << " Found: " << PrettyMethod(GetMethod());
Ian Rogers62d6c772013-02-27 08:32:07 -0800923 }
924 if (num_frames_ != 0) {
925 // Check agreement of frame Ids only if num_frames_ is computed to avoid infinite
926 // recursion.
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000927 size_t frame_id = instrumentation::Instrumentation::ComputeFrameId(
928 thread_,
929 cur_depth_,
930 inlined_frames_count);
931 CHECK_EQ(instrumentation_frame.frame_id_, frame_id);
Ian Rogers62d6c772013-02-27 08:32:07 -0800932 }
jeffhao725a9572012-11-13 18:20:12 -0800933 return_pc = instrumentation_frame.return_pc_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700934 }
935 }
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100936
Ian Rogers0399dde2012-06-06 17:09:28 -0700937 cur_quick_frame_pc_ = return_pc;
Ian Rogers13735952014-10-08 12:43:28 -0700938 uint8_t* next_frame = reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700939 cur_quick_frame_ = reinterpret_cast<ArtMethod**>(next_frame);
940
941 if (kDebugStackWalk) {
942 LOG(INFO) << PrettyMethod(method) << "@" << method << " size=" << frame_size
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100943 << std::boolalpha
944 << " optimized=" << (cur_oat_quick_method_header_ != nullptr &&
945 cur_oat_quick_method_header_->IsOptimized())
Mathieu Chartiere401d142015-04-22 13:56:20 -0700946 << " native=" << method->IsNative()
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100947 << std::noboolalpha
Mathieu Chartiere401d142015-04-22 13:56:20 -0700948 << " entrypoints=" << method->GetEntryPointFromQuickCompiledCode()
949 << "," << method->GetEntryPointFromJni()
Mathieu Chartiere401d142015-04-22 13:56:20 -0700950 << " next=" << *cur_quick_frame_;
951 }
952
Ian Rogers0399dde2012-06-06 17:09:28 -0700953 cur_depth_++;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700954 method = *cur_quick_frame_;
jeffhao6641ea12013-01-02 18:13:42 -0800955 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700956 } else if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700957 do {
958 SanityCheckFrame();
959 bool should_continue = VisitFrame();
960 if (UNLIKELY(!should_continue)) {
961 return;
962 }
963 cur_depth_++;
964 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700965 } while (cur_shadow_frame_ != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700966 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700967 if (include_transitions) {
968 bool should_continue = VisitFrame();
969 if (!should_continue) {
970 return;
971 }
972 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800973 cur_depth_++;
974 }
975 if (num_frames_ != 0) {
976 CHECK_EQ(cur_depth_, num_frames_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700977 }
978}
979
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800980void JavaFrameRootInfo::Describe(std::ostream& os) const {
981 const StackVisitor* visitor = stack_visitor_;
982 CHECK(visitor != nullptr);
983 os << "Type=" << GetType() << " thread_id=" << GetThreadId() << " location=" <<
984 visitor->DescribeLocation() << " vreg=" << vreg_;
985}
986
Mathieu Chartiere401d142015-04-22 13:56:20 -0700987int StackVisitor::GetVRegOffsetFromQuickCode(const DexFile::CodeItem* code_item,
988 uint32_t core_spills, uint32_t fp_spills,
989 size_t frame_size, int reg, InstructionSet isa) {
990 size_t pointer_size = InstructionSetPointerSize(isa);
991 if (kIsDebugBuild) {
992 auto* runtime = Runtime::Current();
993 if (runtime != nullptr) {
994 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
995 }
996 }
Roland Levillain14d90572015-07-16 10:52:26 +0100997 DCHECK_ALIGNED(frame_size, kStackAlignment);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700998 DCHECK_NE(reg, -1);
999 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
1000 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
1001 + sizeof(uint32_t); // Filler.
1002 int num_regs = code_item->registers_size_ - code_item->ins_size_;
1003 int temp_threshold = code_item->registers_size_;
1004 const int max_num_special_temps = 1;
1005 if (reg == temp_threshold) {
1006 // The current method pointer corresponds to special location on stack.
1007 return 0;
1008 } else if (reg >= temp_threshold + max_num_special_temps) {
1009 /*
1010 * Special temporaries may have custom locations and the logic above deals with that.
1011 * However, non-special temporaries are placed relative to the outs.
1012 */
1013 int temps_start = code_item->outs_size_ * sizeof(uint32_t) + pointer_size /* art method */;
1014 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
1015 return temps_start + relative_offset;
1016 } else if (reg < num_regs) {
1017 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
1018 return locals_start + (reg * sizeof(uint32_t));
1019 } else {
1020 // Handle ins.
1021 return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + pointer_size /* art method */;
1022 }
1023}
1024
Andreas Gampe03ec9302015-08-27 17:41:47 -07001025void LockCountData::AddMonitorInternal(Thread* self, mirror::Object* obj) {
1026 if (obj == nullptr) {
1027 return;
1028 }
1029
1030 // If there's an error during enter, we won't have locked the monitor. So check there's no
1031 // exception.
1032 if (self->IsExceptionPending()) {
1033 return;
1034 }
1035
1036 if (monitors_ == nullptr) {
1037 monitors_.reset(new std::vector<mirror::Object*>());
1038 }
1039 monitors_->push_back(obj);
1040}
1041
1042void LockCountData::RemoveMonitorInternal(Thread* self, const mirror::Object* obj) {
1043 if (obj == nullptr) {
1044 return;
1045 }
1046 bool found_object = false;
1047 if (monitors_ != nullptr) {
1048 // We need to remove one pointer to ref, as duplicates are used for counting recursive locks.
1049 // We arbitrarily choose the first one.
1050 auto it = std::find(monitors_->begin(), monitors_->end(), obj);
1051 if (it != monitors_->end()) {
1052 monitors_->erase(it);
1053 found_object = true;
1054 }
1055 }
1056 if (!found_object) {
1057 // The object wasn't found. Time for an IllegalMonitorStateException.
1058 // The order here isn't fully clear. Assume that any other pending exception is swallowed.
1059 // TODO: Maybe make already pending exception a suppressed exception.
1060 self->ClearException();
1061 self->ThrowNewExceptionF("Ljava/lang/IllegalMonitorStateException;",
1062 "did not lock monitor on object of type '%s' before unlocking",
1063 PrettyTypeOf(const_cast<mirror::Object*>(obj)).c_str());
1064 }
1065}
1066
1067// Helper to unlock a monitor. Must be NO_THREAD_SAFETY_ANALYSIS, as we can't statically show
1068// that the object was locked.
1069void MonitorExitHelper(Thread* self, mirror::Object* obj) NO_THREAD_SAFETY_ANALYSIS {
1070 DCHECK(self != nullptr);
1071 DCHECK(obj != nullptr);
1072 obj->MonitorExit(self);
1073}
1074
1075bool LockCountData::CheckAllMonitorsReleasedInternal(Thread* self) {
1076 DCHECK(self != nullptr);
1077 if (monitors_ != nullptr) {
1078 if (!monitors_->empty()) {
1079 // There may be an exception pending, if the method is terminating abruptly. Clear it.
1080 // TODO: Should we add this as a suppressed exception?
1081 self->ClearException();
1082
1083 // OK, there are monitors that are still locked. To enforce structured locking (and avoid
1084 // deadlocks) we unlock all of them before we raise the IllegalMonitorState exception.
1085 for (mirror::Object* obj : *monitors_) {
1086 MonitorExitHelper(self, obj);
1087 // If this raised an exception, ignore. TODO: Should we add this as suppressed
1088 // exceptions?
1089 if (self->IsExceptionPending()) {
1090 self->ClearException();
1091 }
1092 }
1093 // Raise an exception, just give the first object as the sample.
1094 mirror::Object* first = (*monitors_)[0];
1095 self->ThrowNewExceptionF("Ljava/lang/IllegalMonitorStateException;",
1096 "did not unlock monitor on object of type '%s'",
1097 PrettyTypeOf(first).c_str());
1098
1099 // To make sure this path is not triggered again, clean out the monitors.
1100 monitors_->clear();
1101
1102 return false;
1103 }
1104 }
1105 return true;
1106}
1107
Elliott Hughes68e76522011-10-05 13:22:16 -07001108} // namespace art