blob: d44224ee96c0b124314dc2ed7aa4d3adecdebc50 [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 Chartiere401d142015-04-22 13:56:20 -070043static constexpr bool kDebugStackWalk = false;
44
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 Brazdilf677ebf2015-05-29 16:29:43 +0100120 StackMapEncoding encoding = code_info.ExtractEncoding();
121 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();
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +0100133 return GetResolvedMethod(*GetCurrentQuickFrame(), inline_info, depth_in_stack_map);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100134 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700135 return *cur_quick_frame_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100136 }
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100137 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700138 return nullptr;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100139}
140
Dave Allisonb373e092014-02-20 16:06:36 -0800141uint32_t StackVisitor::GetDexPc(bool abort_on_failure) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700142 if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700143 return cur_shadow_frame_->GetDexPC();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700144 } else if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100145 if (IsInInlinedFrame()) {
146 size_t depth_in_stack_map = current_inlining_depth_ - 1;
147 return GetCurrentInlineInfo().GetDexPcAtDepth(depth_in_stack_map);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100148 } else if (cur_oat_quick_method_header_ == nullptr) {
149 return DexFile::kDexNoIndex;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100150 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100151 return cur_oat_quick_method_header_->ToDexPc(
152 GetMethod(), cur_quick_frame_pc_, abort_on_failure);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100153 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700154 } else {
155 return 0;
156 }
157}
158
Mathieu Chartiere401d142015-04-22 13:56:20 -0700159extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700160 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertza836bc92014-11-25 16:30:53 +0100161
Ian Rogers62d6c772013-02-27 08:32:07 -0800162mirror::Object* StackVisitor::GetThisObject() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700163 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), sizeof(void*));
164 ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800165 if (m->IsStatic()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100166 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800167 } else if (m->IsNative()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100168 if (cur_quick_frame_ != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700169 HandleScope* hs = reinterpret_cast<HandleScope*>(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100170 reinterpret_cast<char*>(cur_quick_frame_) + sizeof(ArtMethod*));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700171 return hs->GetReference(0);
Ian Rogers62d6c772013-02-27 08:32:07 -0800172 } else {
173 return cur_shadow_frame_->GetVRegReference(0);
174 }
Sebastien Hertza836bc92014-11-25 16:30:53 +0100175 } else if (m->IsProxyMethod()) {
176 if (cur_quick_frame_ != nullptr) {
177 return artQuickGetProxyThisObject(cur_quick_frame_);
178 } else {
179 return cur_shadow_frame_->GetVRegReference(0);
180 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800181 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700182 const DexFile::CodeItem* code_item = m->GetCodeItem();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100183 if (code_item == nullptr) {
Ian Rogerse0dcd462014-03-08 15:21:04 -0800184 UNIMPLEMENTED(ERROR) << "Failed to determine this object of abstract or proxy method: "
Ian Rogers62d6c772013-02-27 08:32:07 -0800185 << PrettyMethod(m);
Ian Rogerse0dcd462014-03-08 15:21:04 -0800186 return nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800187 } else {
188 uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000189 uint32_t value = 0;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700190 bool success = GetVReg(m, reg, kReferenceVReg, &value);
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000191 // We currently always guarantee the `this` object is live throughout the method.
192 CHECK(success) << "Failed to read the this object in " << PrettyMethod(m);
193 return reinterpret_cast<mirror::Object*>(value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800194 }
195 }
196}
197
Ian Rogers0c7abda2012-09-19 13:33:42 -0700198size_t StackVisitor::GetNativePcOffset() const {
199 DCHECK(!IsShadowFrame());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100200 return GetCurrentOatQuickMethodHeader()->NativeQuickPcOffset(cur_quick_frame_pc_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700201}
202
Mathieu Chartiere401d142015-04-22 13:56:20 -0700203bool StackVisitor::IsReferenceVReg(ArtMethod* m, uint16_t vreg) {
Nicolas Geoffrayccc61972015-10-01 14:34:20 +0100204 DCHECK_EQ(m, GetMethod());
Mathieu Chartier50030ef2015-05-08 14:19:26 -0700205 // Process register map (which native and runtime methods don't have)
206 if (m->IsNative() || m->IsRuntimeMethod() || m->IsProxyMethod()) {
207 return false;
208 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100209 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
210 if (method_header->IsOptimized()) {
Mathieu Chartier50030ef2015-05-08 14:19:26 -0700211 return true; // TODO: Implement.
212 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100213 const uint8_t* native_gc_map = method_header->GetNativeGcMap();
Mathieu Chartier50030ef2015-05-08 14:19:26 -0700214 CHECK(native_gc_map != nullptr) << PrettyMethod(m);
215 const DexFile::CodeItem* code_item = m->GetCodeItem();
216 // Can't be null or how would we compile its instructions?
217 DCHECK(code_item != nullptr) << PrettyMethod(m);
218 NativePcOffsetToReferenceMap map(native_gc_map);
219 size_t num_regs = std::min(map.RegWidth() * 8, static_cast<size_t>(code_item->registers_size_));
220 const uint8_t* reg_bitmap = nullptr;
221 if (num_regs > 0) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100222 uintptr_t native_pc_offset = method_header->NativeQuickPcOffset(GetCurrentQuickFramePc());
Mathieu Chartier50030ef2015-05-08 14:19:26 -0700223 reg_bitmap = map.FindBitMap(native_pc_offset);
224 DCHECK(reg_bitmap != nullptr);
225 }
226 // Does this register hold a reference?
227 return vreg < num_regs && TestBitmap(vreg, reg_bitmap);
228}
229
Mingyao Yang99170c62015-07-06 11:10:37 -0700230bool StackVisitor::GetVRegFromDebuggerShadowFrame(uint16_t vreg,
231 VRegKind kind,
232 uint32_t* val) const {
233 size_t frame_id = const_cast<StackVisitor*>(this)->GetFrameId();
234 ShadowFrame* shadow_frame = thread_->FindDebuggerShadowFrame(frame_id);
235 if (shadow_frame != nullptr) {
236 bool* updated_vreg_flags = thread_->GetUpdatedVRegFlags(frame_id);
237 DCHECK(updated_vreg_flags != nullptr);
238 if (updated_vreg_flags[vreg]) {
239 // Value is set by the debugger.
240 if (kind == kReferenceVReg) {
241 *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
242 shadow_frame->GetVRegReference(vreg)));
243 } else {
244 *val = shadow_frame->GetVReg(vreg);
245 }
246 return true;
247 }
248 }
249 // No value is set by the debugger.
250 return false;
251}
252
Mathieu Chartiere401d142015-04-22 13:56:20 -0700253bool StackVisitor::GetVReg(ArtMethod* m, uint16_t vreg, VRegKind kind, uint32_t* val) const {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200254 if (cur_quick_frame_ != nullptr) {
255 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800256 DCHECK(m == GetMethod());
Mingyao Yang99170c62015-07-06 11:10:37 -0700257 // Check if there is value set by the debugger.
258 if (GetVRegFromDebuggerShadowFrame(vreg, kind, val)) {
259 return true;
260 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100261 if (cur_oat_quick_method_header_->IsOptimized()) {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100262 return GetVRegFromOptimizedCode(m, vreg, kind, val);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700263 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100264 return GetVRegFromQuickCode(m, vreg, kind, val);
Ian Rogers0399dde2012-06-06 17:09:28 -0700265 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700266 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100267 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200268 *val = cur_shadow_frame_->GetVReg(vreg);
269 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700270 }
271}
272
Mathieu Chartiere401d142015-04-22 13:56:20 -0700273bool StackVisitor::GetVRegFromQuickCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100274 uint32_t* val) const {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100275 DCHECK_EQ(m, GetMethod());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100276 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
277 QuickMethodFrameInfo frame_info = method_header->GetFrameInfo();
278 const VmapTable vmap_table(method_header->GetVmapTable());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100279 uint32_t vmap_offset;
280 // TODO: IsInContext stops before spotting floating point registers.
281 if (vmap_table.IsInContext(vreg, kind, &vmap_offset)) {
282 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
283 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
284 uint32_t reg = vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
285 return GetRegisterIfAccessible(reg, kind, val);
286 } else {
287 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700288 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100289 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000290 *val = *GetVRegAddrFromQuickCode(cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
291 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100292 return true;
293 }
294}
295
Mathieu Chartiere401d142015-04-22 13:56:20 -0700296bool StackVisitor::GetVRegFromOptimizedCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100297 uint32_t* val) const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100298 DCHECK_EQ(m, GetMethod());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100299 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700300 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100301 // its instructions?
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000302 uint16_t number_of_dex_registers = code_item->registers_size_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100303 DCHECK_LT(vreg, code_item->registers_size_);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100304 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
305 CodeInfo code_info = method_header->GetOptimizedCodeInfo();
David Brazdilf677ebf2015-05-29 16:29:43 +0100306 StackMapEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100307
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100308 uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100309 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100310 DCHECK(stack_map.IsValid());
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100311 size_t depth_in_stack_map = current_inlining_depth_ - 1;
312
313 DexRegisterMap dex_register_map = IsInInlinedFrame()
David Brazdilf677ebf2015-05-29 16:29:43 +0100314 ? code_info.GetDexRegisterMapAtDepth(depth_in_stack_map,
315 code_info.GetInlineInfoOf(stack_map, encoding),
316 encoding,
317 number_of_dex_registers)
318 : code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100319
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000320 DexRegisterLocation::Kind location_kind =
David Brazdilf677ebf2015-05-29 16:29:43 +0100321 dex_register_map.GetLocationKind(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100322 switch (location_kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000323 case DexRegisterLocation::Kind::kInStack: {
David Brazdilf677ebf2015-05-29 16:29:43 +0100324 const int32_t offset = dex_register_map.GetStackOffsetInBytes(vreg,
325 number_of_dex_registers,
326 code_info,
327 encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100328 const uint8_t* addr = reinterpret_cast<const uint8_t*>(cur_quick_frame_) + offset;
329 *val = *reinterpret_cast<const uint32_t*>(addr);
330 return true;
331 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000332 case DexRegisterLocation::Kind::kInRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100333 case DexRegisterLocation::Kind::kInRegisterHigh:
334 case DexRegisterLocation::Kind::kInFpuRegister:
335 case DexRegisterLocation::Kind::kInFpuRegisterHigh: {
David Brazdilf677ebf2015-05-29 16:29:43 +0100336 uint32_t reg =
337 dex_register_map.GetMachineRegister(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100338 return GetRegisterIfAccessible(reg, kind, val);
339 }
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000340 case DexRegisterLocation::Kind::kConstant:
David Brazdilf677ebf2015-05-29 16:29:43 +0100341 *val = dex_register_map.GetConstant(vreg, number_of_dex_registers, code_info, encoding);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100342 return true;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000343 case DexRegisterLocation::Kind::kNone:
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100344 return false;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000345 default:
346 LOG(FATAL)
347 << "Unexpected location kind"
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000348 << DexRegisterLocation::PrettyDescriptor(
David Brazdilf677ebf2015-05-29 16:29:43 +0100349 dex_register_map.GetLocationInternalKind(vreg,
350 number_of_dex_registers,
351 code_info,
352 encoding));
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000353 UNREACHABLE();
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100354 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100355}
356
357bool StackVisitor::GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const {
358 const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
David Brazdil77a48ae2015-09-15 12:34:04 +0000359
360 // X86 float registers are 64-bit and the logic below does not apply.
361 DCHECK(!is_float || kRuntimeISA != InstructionSet::kX86);
362
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100363 if (!IsAccessibleRegister(reg, is_float)) {
364 return false;
365 }
366 uintptr_t ptr_val = GetRegister(reg, is_float);
367 const bool target64 = Is64BitInstructionSet(kRuntimeISA);
368 if (target64) {
369 const bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
370 const bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
371 int64_t value_long = static_cast<int64_t>(ptr_val);
372 if (wide_lo) {
373 ptr_val = static_cast<uintptr_t>(Low32Bits(value_long));
374 } else if (wide_hi) {
375 ptr_val = static_cast<uintptr_t>(High32Bits(value_long));
376 }
377 }
378 *val = ptr_val;
379 return true;
380}
381
Mingyao Yang99170c62015-07-06 11:10:37 -0700382bool StackVisitor::GetVRegPairFromDebuggerShadowFrame(uint16_t vreg,
383 VRegKind kind_lo,
384 VRegKind kind_hi,
385 uint64_t* val) const {
386 uint32_t low_32bits;
387 uint32_t high_32bits;
388 bool success = GetVRegFromDebuggerShadowFrame(vreg, kind_lo, &low_32bits);
389 success &= GetVRegFromDebuggerShadowFrame(vreg + 1, kind_hi, &high_32bits);
390 if (success) {
391 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
392 }
393 return success;
394}
395
Mathieu Chartiere401d142015-04-22 13:56:20 -0700396bool StackVisitor::GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200397 VRegKind kind_hi, uint64_t* val) const {
398 if (kind_lo == kLongLoVReg) {
399 DCHECK_EQ(kind_hi, kLongHiVReg);
400 } else if (kind_lo == kDoubleLoVReg) {
401 DCHECK_EQ(kind_hi, kDoubleHiVReg);
402 } else {
403 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100404 UNREACHABLE();
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200405 }
Mingyao Yang99170c62015-07-06 11:10:37 -0700406 // Check if there is value set by the debugger.
407 if (GetVRegPairFromDebuggerShadowFrame(vreg, kind_lo, kind_hi, val)) {
408 return true;
409 }
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200410 if (cur_quick_frame_ != nullptr) {
411 DCHECK(context_ != nullptr); // You can't reliably read registers without a context.
412 DCHECK(m == GetMethod());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100413 if (cur_oat_quick_method_header_->IsOptimized()) {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100414 return GetVRegPairFromOptimizedCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200415 } else {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100416 return GetVRegPairFromQuickCode(m, vreg, kind_lo, kind_hi, val);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200417 }
418 } else {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100419 DCHECK(cur_shadow_frame_ != nullptr);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200420 *val = cur_shadow_frame_->GetVRegLong(vreg);
421 return true;
422 }
423}
424
Mathieu Chartiere401d142015-04-22 13:56:20 -0700425bool StackVisitor::GetVRegPairFromQuickCode(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100426 VRegKind kind_hi, uint64_t* val) const {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100427 DCHECK_EQ(m, GetMethod());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100428 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
429 QuickMethodFrameInfo frame_info = method_header->GetFrameInfo();
430 const VmapTable vmap_table(method_header->GetVmapTable());
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100431 uint32_t vmap_offset_lo, vmap_offset_hi;
432 // TODO: IsInContext stops before spotting floating point registers.
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700433 if (vmap_table.IsInContext(vreg, kind_lo, &vmap_offset_lo) &&
434 vmap_table.IsInContext(vreg + 1, kind_hi, &vmap_offset_hi)) {
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100435 bool is_float = (kind_lo == kDoubleLoVReg);
436 uint32_t spill_mask = is_float ? frame_info.FpSpillMask() : frame_info.CoreSpillMask();
437 uint32_t reg_lo = vmap_table.ComputeRegister(spill_mask, vmap_offset_lo, kind_lo);
438 uint32_t reg_hi = vmap_table.ComputeRegister(spill_mask, vmap_offset_hi, kind_hi);
439 return GetRegisterPairIfAccessible(reg_lo, reg_hi, kind_lo, val);
440 } else {
441 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700442 DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be null or how would we compile
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100443 // its instructions?
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000444 uint32_t* addr = GetVRegAddrFromQuickCode(
445 cur_quick_frame_, code_item, frame_info.CoreSpillMask(),
446 frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), vreg);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100447 *val = *reinterpret_cast<uint64_t*>(addr);
448 return true;
449 }
450}
451
Mathieu Chartiere401d142015-04-22 13:56:20 -0700452bool StackVisitor::GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100453 VRegKind kind_lo, VRegKind kind_hi,
454 uint64_t* val) const {
455 uint32_t low_32bits;
456 uint32_t high_32bits;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700457 bool success = GetVRegFromOptimizedCode(m, vreg, kind_lo, &low_32bits);
458 success &= GetVRegFromOptimizedCode(m, vreg + 1, kind_hi, &high_32bits);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100459 if (success) {
460 *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
461 }
462 return success;
463}
464
465bool StackVisitor::GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
466 VRegKind kind_lo, uint64_t* val) const {
467 const bool is_float = (kind_lo == kDoubleLoVReg);
468 if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
469 return false;
470 }
471 uintptr_t ptr_val_lo = GetRegister(reg_lo, is_float);
472 uintptr_t ptr_val_hi = GetRegister(reg_hi, is_float);
473 bool target64 = Is64BitInstructionSet(kRuntimeISA);
474 if (target64) {
475 int64_t value_long_lo = static_cast<int64_t>(ptr_val_lo);
476 int64_t value_long_hi = static_cast<int64_t>(ptr_val_hi);
477 ptr_val_lo = static_cast<uintptr_t>(Low32Bits(value_long_lo));
478 ptr_val_hi = static_cast<uintptr_t>(High32Bits(value_long_hi));
479 }
480 *val = (static_cast<uint64_t>(ptr_val_hi) << 32) | static_cast<uint32_t>(ptr_val_lo);
481 return true;
482}
483
Mingyao Yang636b9252015-07-31 16:40:24 -0700484bool StackVisitor::SetVReg(ArtMethod* m,
485 uint16_t vreg,
486 uint32_t new_value,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800487 VRegKind kind) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700488 const DexFile::CodeItem* code_item = m->GetCodeItem();
489 if (code_item == nullptr) {
490 return false;
491 }
492 ShadowFrame* shadow_frame = GetCurrentShadowFrame();
493 if (shadow_frame == nullptr) {
494 // This is a compiled frame: we must prepare and update a shadow frame that will
495 // be executed by the interpreter after deoptimization of the stack.
496 const size_t frame_id = GetFrameId();
497 const uint16_t num_regs = code_item->registers_size_;
498 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
499 CHECK(shadow_frame != nullptr);
500 // Remember the vreg has been set for debugging and must not be overwritten by the
501 // original value during deoptimization of the stack.
502 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
503 }
504 if (kind == kReferenceVReg) {
505 shadow_frame->SetVRegReference(vreg, reinterpret_cast<mirror::Object*>(new_value));
506 } else {
507 shadow_frame->SetVReg(vreg, new_value);
508 }
509 return true;
510}
511
Mingyao Yang636b9252015-07-31 16:40:24 -0700512bool StackVisitor::SetVRegPair(ArtMethod* m,
513 uint16_t vreg,
514 uint64_t new_value,
515 VRegKind kind_lo,
516 VRegKind kind_hi) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700517 if (kind_lo == kLongLoVReg) {
518 DCHECK_EQ(kind_hi, kLongHiVReg);
519 } else if (kind_lo == kDoubleLoVReg) {
520 DCHECK_EQ(kind_hi, kDoubleHiVReg);
521 } else {
522 LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
523 UNREACHABLE();
524 }
525 const DexFile::CodeItem* code_item = m->GetCodeItem();
526 if (code_item == nullptr) {
527 return false;
528 }
529 ShadowFrame* shadow_frame = GetCurrentShadowFrame();
530 if (shadow_frame == nullptr) {
531 // This is a compiled frame: we must prepare for deoptimization (see SetVRegFromDebugger).
532 const size_t frame_id = GetFrameId();
533 const uint16_t num_regs = code_item->registers_size_;
534 shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
535 CHECK(shadow_frame != nullptr);
536 // Remember the vreg pair has been set for debugging and must not be overwritten by the
537 // original value during deoptimization of the stack.
538 thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
539 thread_->GetUpdatedVRegFlags(frame_id)[vreg + 1] = true;
540 }
541 shadow_frame->SetVRegLong(vreg, new_value);
542 return true;
543}
544
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100545bool StackVisitor::IsAccessibleGPR(uint32_t reg) const {
546 DCHECK(context_ != nullptr);
547 return context_->IsAccessibleGPR(reg);
548}
549
Mathieu Chartier815873e2014-02-13 18:02:13 -0800550uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const {
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100551 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
552 DCHECK(context_ != nullptr);
Mathieu Chartier815873e2014-02-13 18:02:13 -0800553 return context_->GetGPRAddress(reg);
554}
555
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100556uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
557 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
558 DCHECK(context_ != nullptr);
559 return context_->GetGPR(reg);
Ian Rogers0399dde2012-06-06 17:09:28 -0700560}
561
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100562bool StackVisitor::IsAccessibleFPR(uint32_t reg) const {
563 DCHECK(context_ != nullptr);
564 return context_->IsAccessibleFPR(reg);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200565}
566
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100567uintptr_t StackVisitor::GetFPR(uint32_t reg) const {
568 DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
569 DCHECK(context_ != nullptr);
570 return context_->GetFPR(reg);
571}
572
Ian Rogers0399dde2012-06-06 17:09:28 -0700573uintptr_t StackVisitor::GetReturnPc() const {
Ian Rogers13735952014-10-08 12:43:28 -0700574 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700575 DCHECK(sp != nullptr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100576 uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogers0399dde2012-06-06 17:09:28 -0700577 return *reinterpret_cast<uintptr_t*>(pc_addr);
578}
579
580void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
Ian Rogers13735952014-10-08 12:43:28 -0700581 uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700582 CHECK(sp != nullptr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100583 uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogers0399dde2012-06-06 17:09:28 -0700584 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc;
585}
586
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100587size_t StackVisitor::ComputeNumFrames(Thread* thread, StackWalkKind walk_kind) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700588 struct NumFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100589 NumFramesVisitor(Thread* thread_in, StackWalkKind walk_kind_in)
590 : StackVisitor(thread_in, nullptr, walk_kind_in), frames(0) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700591
Ian Rogers5cf98192014-05-29 21:31:50 -0700592 bool VisitFrame() OVERRIDE {
Ian Rogers0399dde2012-06-06 17:09:28 -0700593 frames++;
594 return true;
595 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700596
Ian Rogers0399dde2012-06-06 17:09:28 -0700597 size_t frames;
598 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100599 NumFramesVisitor visitor(thread, walk_kind);
Ian Rogers0399dde2012-06-06 17:09:28 -0700600 visitor.WalkStack(true);
601 return visitor.frames;
602}
603
Mathieu Chartiere401d142015-04-22 13:56:20 -0700604bool StackVisitor::GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700605 struct HasMoreFramesVisitor : public StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100606 HasMoreFramesVisitor(Thread* thread,
607 StackWalkKind walk_kind,
608 size_t num_frames,
609 size_t frame_height)
610 : StackVisitor(thread, nullptr, walk_kind, num_frames),
611 frame_height_(frame_height),
612 found_frame_(false),
613 has_more_frames_(false),
614 next_method_(nullptr),
615 next_dex_pc_(0) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700616 }
617
Mathieu Chartier90443472015-07-16 20:32:27 -0700618 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700619 if (found_frame_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700620 ArtMethod* method = GetMethod();
Ian Rogers5cf98192014-05-29 21:31:50 -0700621 if (method != nullptr && !method->IsRuntimeMethod()) {
622 has_more_frames_ = true;
623 next_method_ = method;
624 next_dex_pc_ = GetDexPc();
625 return false; // End stack walk once next method is found.
626 }
627 } else if (GetFrameHeight() == frame_height_) {
628 found_frame_ = true;
629 }
630 return true;
631 }
632
633 size_t frame_height_;
634 bool found_frame_;
635 bool has_more_frames_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700636 ArtMethod* next_method_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700637 uint32_t next_dex_pc_;
638 };
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100639 HasMoreFramesVisitor visitor(thread_, walk_kind_, GetNumFrames(), GetFrameHeight());
Ian Rogers5cf98192014-05-29 21:31:50 -0700640 visitor.WalkStack(true);
641 *next_method = visitor.next_method_;
642 *next_dex_pc = visitor.next_dex_pc_;
643 return visitor.has_more_frames_;
644}
645
Ian Rogers7a22fa62013-01-23 12:16:16 -0800646void StackVisitor::DescribeStack(Thread* thread) {
Ian Rogers306057f2012-11-26 12:45:53 -0800647 struct DescribeStackVisitor : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800648 explicit DescribeStackVisitor(Thread* thread_in)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100649 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800650
Mathieu Chartier90443472015-07-16 20:32:27 -0700651 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers306057f2012-11-26 12:45:53 -0800652 LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation();
653 return true;
654 }
655 };
Ian Rogers7a22fa62013-01-23 12:16:16 -0800656 DescribeStackVisitor visitor(thread);
Ian Rogers306057f2012-11-26 12:45:53 -0800657 visitor.WalkStack(true);
658}
659
Ian Rogers40e3bac2012-11-20 00:09:14 -0800660std::string StackVisitor::DescribeLocation() const {
661 std::string result("Visiting method '");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700662 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700663 if (m == nullptr) {
Ian Rogers306057f2012-11-26 12:45:53 -0800664 return "upcall";
665 }
666 result += PrettyMethod(m);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800667 result += StringPrintf("' at dex PC 0x%04x", GetDexPc());
Ian Rogers40e3bac2012-11-20 00:09:14 -0800668 if (!IsShadowFrame()) {
669 result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc()));
670 }
671 return result;
672}
673
Ian Rogerse63db272014-07-15 15:36:11 -0700674static instrumentation::InstrumentationStackFrame& GetInstrumentationStackFrame(Thread* thread,
675 uint32_t depth) {
676 CHECK_LT(depth, thread->GetInstrumentationStack()->size());
677 return thread->GetInstrumentationStack()->at(depth);
Ian Rogers7a22fa62013-01-23 12:16:16 -0800678}
679
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100680static void AssertPcIsWithinQuickCode(ArtMethod* method, uintptr_t pc)
681 SHARED_REQUIRES(Locks::mutator_lock_) {
682 if (method->IsNative() || method->IsRuntimeMethod() || method->IsProxyMethod()) {
683 return;
684 }
685
686 if (pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) {
687 return;
688 }
689
690 const void* code = method->GetEntryPointFromQuickCompiledCode();
691 if (code == GetQuickInstrumentationEntryPoint()) {
692 return;
693 }
694
695 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
696 if (class_linker->IsQuickToInterpreterBridge(code) ||
697 class_linker->IsQuickResolutionStub(code)) {
698 return;
699 }
700
701 // If we are the JIT then we may have just compiled the method after the
702 // IsQuickToInterpreterBridge check.
703 jit::Jit* const jit = Runtime::Current()->GetJit();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100704 if (jit != nullptr && jit->GetCodeCache()->ContainsPc(code)) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100705 return;
706 }
707
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100708 uint32_t code_size = OatQuickMethodHeader::FromEntryPoint(code)->code_size_;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100709 uintptr_t code_start = reinterpret_cast<uintptr_t>(code);
710 CHECK(code_start <= pc && pc <= (code_start + code_size))
711 << PrettyMethod(method)
712 << " pc=" << std::hex << pc
713 << " code=" << code
714 << " size=" << code_size;
715}
716
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700717void StackVisitor::SanityCheckFrame() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800718 if (kIsDebugBuild) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700719 ArtMethod* method = GetMethod();
720 auto* declaring_class = method->GetDeclaringClass();
721 // Runtime methods have null declaring class.
722 if (!method->IsRuntimeMethod()) {
723 CHECK(declaring_class != nullptr);
724 CHECK_EQ(declaring_class->GetClass(), declaring_class->GetClass()->GetClass())
725 << declaring_class;
726 } else {
727 CHECK(declaring_class == nullptr);
728 }
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700729 Runtime* const runtime = Runtime::Current();
730 LinearAlloc* const linear_alloc = runtime->GetLinearAlloc();
731 if (!linear_alloc->Contains(method)) {
732 // Check class linker linear allocs.
733 mirror::Class* klass = method->GetDeclaringClass();
734 LinearAlloc* const class_linear_alloc = (klass != nullptr)
735 ? ClassLinker::GetAllocatorForClassLoader(klass->GetClassLoader())
736 : linear_alloc;
737 if (!class_linear_alloc->Contains(method)) {
738 // Check image space.
739 bool in_image = false;
740 for (auto& space : runtime->GetHeap()->GetContinuousSpaces()) {
741 if (space->IsImageSpace()) {
742 auto* image_space = space->AsImageSpace();
743 const auto& header = image_space->GetImageHeader();
744 const auto* methods = &header.GetMethodsSection();
745 if (methods->Contains(reinterpret_cast<const uint8_t*>(method) - image_space->Begin())) {
746 in_image = true;
747 break;
748 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700749 }
750 }
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700751 CHECK(in_image) << PrettyMethod(method) << " not in linear alloc or image";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700752 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700753 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800754 if (cur_quick_frame_ != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100755 AssertPcIsWithinQuickCode(method, cur_quick_frame_pc_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800756 // Frame sanity.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100757 size_t frame_size = GetCurrentQuickFrameInfo().FrameSizeInBytes();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800758 CHECK_NE(frame_size, 0u);
Andreas Gampe5b417b92014-03-10 14:18:35 -0700759 // A rough guess at an upper size we expect to see for a frame.
760 // 256 registers
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700761 // 2 words HandleScope overhead
Andreas Gampe5b417b92014-03-10 14:18:35 -0700762 // 3+3 register spills
763 // TODO: this seems architecture specific for the case of JNI frames.
Brian Carlstromed08bd42014-03-19 18:34:17 -0700764 // TODO: 083-compiler-regressions ManyFloatArgs shows this estimate is wrong.
765 // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word);
766 const size_t kMaxExpectedFrameSize = 2 * KB;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100767 CHECK_LE(frame_size, kMaxExpectedFrameSize) << PrettyMethod(method);
768 size_t return_pc_offset = GetCurrentQuickFrameInfo().GetReturnPcOffset();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800769 CHECK_LT(return_pc_offset, frame_size);
770 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700771 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700772}
773
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100774// Counts the number of references in the parameter list of the corresponding method.
775// Note: Thus does _not_ include "this" for non-static methods.
776static uint32_t GetNumberOfReferenceArgsWithoutReceiver(ArtMethod* method)
777 SHARED_REQUIRES(Locks::mutator_lock_) {
778 uint32_t shorty_len;
779 const char* shorty = method->GetShorty(&shorty_len);
780 uint32_t refs = 0;
781 for (uint32_t i = 1; i < shorty_len ; ++i) {
782 if (shorty[i] == 'L') {
783 refs++;
784 }
785 }
786 return refs;
787}
788
789QuickMethodFrameInfo StackVisitor::GetCurrentQuickFrameInfo() const {
790 if (cur_oat_quick_method_header_ != nullptr) {
791 return cur_oat_quick_method_header_->GetFrameInfo();
792 }
793
794 ArtMethod* method = GetMethod();
795 Runtime* runtime = Runtime::Current();
796
797 if (method->IsAbstract()) {
798 return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs);
799 }
800
801 // This goes before IsProxyMethod since runtime methods have a null declaring class.
802 if (method->IsRuntimeMethod()) {
803 return runtime->GetRuntimeMethodFrameInfo(method);
804 }
805
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100806 if (method->IsProxyMethod()) {
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000807 // There is only one direct method of a proxy class: the constructor. A direct method is
808 // cloned from the original java.lang.reflect.Proxy and is executed as usual quick
809 // compiled method without any stubs. Therefore the method must have a OatQuickMethodHeader.
810 DCHECK(!method->IsDirect() && !method->IsConstructor())
811 << "Constructors of proxy classes must have a OatQuickMethodHeader";
812 return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100813 }
814
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000815 // The only remaining case is if the method is native and uses the generic JNI stub.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100816 DCHECK(method->IsNative());
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000817 ClassLinker* class_linker = runtime->GetClassLinker();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100818 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(method, sizeof(void*));
819 DCHECK(class_linker->IsQuickGenericJniStub(entry_point)) << PrettyMethod(method);
820 // Generic JNI frame.
821 uint32_t handle_refs = GetNumberOfReferenceArgsWithoutReceiver(method) + 1;
822 size_t scope_size = HandleScope::SizeOf(handle_refs);
823 QuickMethodFrameInfo callee_info = runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs);
824
825 // Callee saves + handle scope + method ref + alignment
826 // Note: -sizeof(void*) since callee-save frame stores a whole method pointer.
827 size_t frame_size = RoundUp(
828 callee_info.FrameSizeInBytes() - sizeof(void*) + sizeof(ArtMethod*) + scope_size,
829 kStackAlignment);
830 return QuickMethodFrameInfo(frame_size, callee_info.CoreSpillMask(), callee_info.FpSpillMask());
831}
832
Ian Rogers0399dde2012-06-06 17:09:28 -0700833void StackVisitor::WalkStack(bool include_transitions) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800834 DCHECK(thread_ == Thread::Current() || thread_->IsSuspended());
Ian Rogers62d6c772013-02-27 08:32:07 -0800835 CHECK_EQ(cur_depth_, 0U);
836 bool exit_stubs_installed = Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled();
jeffhao725a9572012-11-13 18:20:12 -0800837 uint32_t instrumentation_stack_depth = 0;
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000838 size_t inlined_frames_count = 0;
Dave Allisonf9439142014-03-27 15:10:22 -0700839
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700840 for (const ManagedStack* current_fragment = thread_->GetManagedStack();
841 current_fragment != nullptr; current_fragment = current_fragment->GetLink()) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700842 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
843 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700844 cur_quick_frame_pc_ = 0;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100845 cur_oat_quick_method_header_ = nullptr;
Dave Allisonf9439142014-03-27 15:10:22 -0700846
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700847 if (cur_quick_frame_ != nullptr) { // Handle quick stack frames.
Ian Rogers0399dde2012-06-06 17:09:28 -0700848 // Can't be both a shadow and a quick fragment.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700849 DCHECK(current_fragment->GetTopShadowFrame() == nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700850 ArtMethod* method = *cur_quick_frame_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700851 while (method != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100852 cur_oat_quick_method_header_ = method->GetOatQuickMethodHeader(cur_quick_frame_pc_);
Dave Allison5cd33752014-04-15 15:57:58 -0700853 SanityCheckFrame();
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100854
855 if ((walk_kind_ == StackWalkKind::kIncludeInlinedFrames)
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100856 && (cur_oat_quick_method_header_ != nullptr)
857 && cur_oat_quick_method_header_->IsOptimized()) {
858 CodeInfo code_info = cur_oat_quick_method_header_->GetOptimizedCodeInfo();
David Brazdilf677ebf2015-05-29 16:29:43 +0100859 StackMapEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100860 uint32_t native_pc_offset =
861 cur_oat_quick_method_header_->NativeQuickPcOffset(cur_quick_frame_pc_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100862 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
863 if (stack_map.IsValid() && stack_map.HasInlineInfo(encoding)) {
864 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100865 DCHECK_EQ(current_inlining_depth_, 0u);
866 for (current_inlining_depth_ = inline_info.GetDepth();
867 current_inlining_depth_ != 0;
868 --current_inlining_depth_) {
869 bool should_continue = VisitFrame();
870 if (UNLIKELY(!should_continue)) {
871 return;
872 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100873 cur_depth_++;
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000874 inlined_frames_count++;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100875 }
876 }
877 }
878
Dave Allison5cd33752014-04-15 15:57:58 -0700879 bool should_continue = VisitFrame();
880 if (UNLIKELY(!should_continue)) {
881 return;
Ian Rogers0399dde2012-06-06 17:09:28 -0700882 }
Dave Allison5cd33752014-04-15 15:57:58 -0700883
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100884 QuickMethodFrameInfo frame_info = GetCurrentQuickFrameInfo();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700885 if (context_ != nullptr) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100886 context_->FillCalleeSaves(reinterpret_cast<uint8_t*>(cur_quick_frame_), frame_info);
Ian Rogers0399dde2012-06-06 17:09:28 -0700887 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700888 // Compute PC for next stack frame from return PC.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100889 size_t frame_size = frame_info.FrameSizeInBytes();
890 size_t return_pc_offset = frame_size - sizeof(void*);
Ian Rogers13735952014-10-08 12:43:28 -0700891 uint8_t* return_pc_addr = reinterpret_cast<uint8_t*>(cur_quick_frame_) + return_pc_offset;
Ian Rogers0399dde2012-06-06 17:09:28 -0700892 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100893
Ian Rogers62d6c772013-02-27 08:32:07 -0800894 if (UNLIKELY(exit_stubs_installed)) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700895 // While profiling, the return pc is restored from the side stack, except when walking
896 // the stack for an exception where the side stack will be unwound in VisitFrame.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700897 if (reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc) {
Sebastien Hertz74e256b2013-10-04 10:40:37 +0200898 const instrumentation::InstrumentationStackFrame& instrumentation_frame =
Ian Rogerse63db272014-07-15 15:36:11 -0700899 GetInstrumentationStackFrame(thread_, instrumentation_stack_depth);
jeffhao725a9572012-11-13 18:20:12 -0800900 instrumentation_stack_depth++;
Jeff Haofb2802d2013-07-24 13:53:05 -0700901 if (GetMethod() == Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveAll)) {
902 // Skip runtime save all callee frames which are used to deliver exceptions.
903 } else if (instrumentation_frame.interpreter_entry_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700904 ArtMethod* callee = Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs);
Jeff Haofb2802d2013-07-24 13:53:05 -0700905 CHECK_EQ(GetMethod(), callee) << "Expected: " << PrettyMethod(callee) << " Found: "
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100906 << PrettyMethod(GetMethod());
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000907 } else {
908 CHECK_EQ(instrumentation_frame.method_, GetMethod())
909 << "Expected: " << PrettyMethod(instrumentation_frame.method_)
910 << " Found: " << PrettyMethod(GetMethod());
Ian Rogers62d6c772013-02-27 08:32:07 -0800911 }
912 if (num_frames_ != 0) {
913 // Check agreement of frame Ids only if num_frames_ is computed to avoid infinite
914 // recursion.
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000915 size_t frame_id = instrumentation::Instrumentation::ComputeFrameId(
916 thread_,
917 cur_depth_,
918 inlined_frames_count);
919 CHECK_EQ(instrumentation_frame.frame_id_, frame_id);
Ian Rogers62d6c772013-02-27 08:32:07 -0800920 }
jeffhao725a9572012-11-13 18:20:12 -0800921 return_pc = instrumentation_frame.return_pc_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700922 }
923 }
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100924
Ian Rogers0399dde2012-06-06 17:09:28 -0700925 cur_quick_frame_pc_ = return_pc;
Ian Rogers13735952014-10-08 12:43:28 -0700926 uint8_t* next_frame = reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700927 cur_quick_frame_ = reinterpret_cast<ArtMethod**>(next_frame);
928
929 if (kDebugStackWalk) {
930 LOG(INFO) << PrettyMethod(method) << "@" << method << " size=" << frame_size
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100931 << std::boolalpha
932 << " optimized=" << (cur_oat_quick_method_header_ != nullptr &&
933 cur_oat_quick_method_header_->IsOptimized())
Mathieu Chartiere401d142015-04-22 13:56:20 -0700934 << " native=" << method->IsNative()
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100935 << std::noboolalpha
Mathieu Chartiere401d142015-04-22 13:56:20 -0700936 << " entrypoints=" << method->GetEntryPointFromQuickCompiledCode()
937 << "," << method->GetEntryPointFromJni()
Mathieu Chartiere401d142015-04-22 13:56:20 -0700938 << " next=" << *cur_quick_frame_;
939 }
940
Ian Rogers0399dde2012-06-06 17:09:28 -0700941 cur_depth_++;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700942 method = *cur_quick_frame_;
jeffhao6641ea12013-01-02 18:13:42 -0800943 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700944 } else if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700945 do {
946 SanityCheckFrame();
947 bool should_continue = VisitFrame();
948 if (UNLIKELY(!should_continue)) {
949 return;
950 }
951 cur_depth_++;
952 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700953 } while (cur_shadow_frame_ != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700954 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700955 if (include_transitions) {
956 bool should_continue = VisitFrame();
957 if (!should_continue) {
958 return;
959 }
960 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800961 cur_depth_++;
962 }
963 if (num_frames_ != 0) {
964 CHECK_EQ(cur_depth_, num_frames_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700965 }
966}
967
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800968void JavaFrameRootInfo::Describe(std::ostream& os) const {
969 const StackVisitor* visitor = stack_visitor_;
970 CHECK(visitor != nullptr);
971 os << "Type=" << GetType() << " thread_id=" << GetThreadId() << " location=" <<
972 visitor->DescribeLocation() << " vreg=" << vreg_;
973}
974
Mathieu Chartiere401d142015-04-22 13:56:20 -0700975int StackVisitor::GetVRegOffsetFromQuickCode(const DexFile::CodeItem* code_item,
976 uint32_t core_spills, uint32_t fp_spills,
977 size_t frame_size, int reg, InstructionSet isa) {
978 size_t pointer_size = InstructionSetPointerSize(isa);
979 if (kIsDebugBuild) {
980 auto* runtime = Runtime::Current();
981 if (runtime != nullptr) {
982 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
983 }
984 }
Roland Levillain14d90572015-07-16 10:52:26 +0100985 DCHECK_ALIGNED(frame_size, kStackAlignment);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700986 DCHECK_NE(reg, -1);
987 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
988 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
989 + sizeof(uint32_t); // Filler.
990 int num_regs = code_item->registers_size_ - code_item->ins_size_;
991 int temp_threshold = code_item->registers_size_;
992 const int max_num_special_temps = 1;
993 if (reg == temp_threshold) {
994 // The current method pointer corresponds to special location on stack.
995 return 0;
996 } else if (reg >= temp_threshold + max_num_special_temps) {
997 /*
998 * Special temporaries may have custom locations and the logic above deals with that.
999 * However, non-special temporaries are placed relative to the outs.
1000 */
1001 int temps_start = code_item->outs_size_ * sizeof(uint32_t) + pointer_size /* art method */;
1002 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
1003 return temps_start + relative_offset;
1004 } else if (reg < num_regs) {
1005 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
1006 return locals_start + (reg * sizeof(uint32_t));
1007 } else {
1008 // Handle ins.
1009 return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + pointer_size /* art method */;
1010 }
1011}
1012
Andreas Gampe03ec9302015-08-27 17:41:47 -07001013void LockCountData::AddMonitorInternal(Thread* self, mirror::Object* obj) {
1014 if (obj == nullptr) {
1015 return;
1016 }
1017
1018 // If there's an error during enter, we won't have locked the monitor. So check there's no
1019 // exception.
1020 if (self->IsExceptionPending()) {
1021 return;
1022 }
1023
1024 if (monitors_ == nullptr) {
1025 monitors_.reset(new std::vector<mirror::Object*>());
1026 }
1027 monitors_->push_back(obj);
1028}
1029
1030void LockCountData::RemoveMonitorInternal(Thread* self, const mirror::Object* obj) {
1031 if (obj == nullptr) {
1032 return;
1033 }
1034 bool found_object = false;
1035 if (monitors_ != nullptr) {
1036 // We need to remove one pointer to ref, as duplicates are used for counting recursive locks.
1037 // We arbitrarily choose the first one.
1038 auto it = std::find(monitors_->begin(), monitors_->end(), obj);
1039 if (it != monitors_->end()) {
1040 monitors_->erase(it);
1041 found_object = true;
1042 }
1043 }
1044 if (!found_object) {
1045 // The object wasn't found. Time for an IllegalMonitorStateException.
1046 // The order here isn't fully clear. Assume that any other pending exception is swallowed.
1047 // TODO: Maybe make already pending exception a suppressed exception.
1048 self->ClearException();
1049 self->ThrowNewExceptionF("Ljava/lang/IllegalMonitorStateException;",
1050 "did not lock monitor on object of type '%s' before unlocking",
1051 PrettyTypeOf(const_cast<mirror::Object*>(obj)).c_str());
1052 }
1053}
1054
1055// Helper to unlock a monitor. Must be NO_THREAD_SAFETY_ANALYSIS, as we can't statically show
1056// that the object was locked.
1057void MonitorExitHelper(Thread* self, mirror::Object* obj) NO_THREAD_SAFETY_ANALYSIS {
1058 DCHECK(self != nullptr);
1059 DCHECK(obj != nullptr);
1060 obj->MonitorExit(self);
1061}
1062
1063bool LockCountData::CheckAllMonitorsReleasedInternal(Thread* self) {
1064 DCHECK(self != nullptr);
1065 if (monitors_ != nullptr) {
1066 if (!monitors_->empty()) {
1067 // There may be an exception pending, if the method is terminating abruptly. Clear it.
1068 // TODO: Should we add this as a suppressed exception?
1069 self->ClearException();
1070
1071 // OK, there are monitors that are still locked. To enforce structured locking (and avoid
1072 // deadlocks) we unlock all of them before we raise the IllegalMonitorState exception.
1073 for (mirror::Object* obj : *monitors_) {
1074 MonitorExitHelper(self, obj);
1075 // If this raised an exception, ignore. TODO: Should we add this as suppressed
1076 // exceptions?
1077 if (self->IsExceptionPending()) {
1078 self->ClearException();
1079 }
1080 }
1081 // Raise an exception, just give the first object as the sample.
1082 mirror::Object* first = (*monitors_)[0];
1083 self->ThrowNewExceptionF("Ljava/lang/IllegalMonitorStateException;",
1084 "did not unlock monitor on object of type '%s'",
1085 PrettyTypeOf(first).c_str());
1086
1087 // To make sure this path is not triggered again, clean out the monitors.
1088 monitors_->clear();
1089
1090 return false;
1091 }
1092 }
1093 return true;
1094}
1095
Elliott Hughes68e76522011-10-05 13:22:16 -07001096} // namespace art