OSDN Git Service

Differentiate between native alloc and normal background GC
[android-x86/art.git] / runtime / stack.cc
1 /*
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
19 #include "android-base/stringprintf.h"
20
21 #include "arch/context.h"
22 #include "art_method-inl.h"
23 #include "base/enums.h"
24 #include "base/hex_dump.h"
25 #include "entrypoints/entrypoint_utils-inl.h"
26 #include "entrypoints/runtime_asm_entrypoints.h"
27 #include "gc/space/image_space.h"
28 #include "gc/space/space-inl.h"
29 #include "jit/jit.h"
30 #include "jit/jit_code_cache.h"
31 #include "linear_alloc.h"
32 #include "mirror/class-inl.h"
33 #include "mirror/object-inl.h"
34 #include "mirror/object_array-inl.h"
35 #include "oat_quick_method_header.h"
36 #include "quick/quick_method_frame_info.h"
37 #include "runtime.h"
38 #include "thread.h"
39 #include "thread_list.h"
40 #include "verify_object.h"
41
42 namespace art {
43
44 using android::base::StringPrintf;
45
46 static constexpr bool kDebugStackWalk = false;
47
48 mirror::Object* ShadowFrame::GetThisObject() const {
49   ArtMethod* m = GetMethod();
50   if (m->IsStatic()) {
51     return nullptr;
52   } else if (m->IsNative()) {
53     return GetVRegReference(0);
54   } else {
55     const DexFile::CodeItem* code_item = m->GetCodeItem();
56     CHECK(code_item != nullptr) << ArtMethod::PrettyMethod(m);
57     uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
58     return GetVRegReference(reg);
59   }
60 }
61
62 mirror::Object* ShadowFrame::GetThisObject(uint16_t num_ins) const {
63   ArtMethod* m = GetMethod();
64   if (m->IsStatic()) {
65     return nullptr;
66   } else {
67     return GetVRegReference(NumberOfVRegs() - num_ins);
68   }
69 }
70
71 size_t ManagedStack::NumJniShadowFrameReferences() const {
72   size_t count = 0;
73   for (const ManagedStack* current_fragment = this; current_fragment != nullptr;
74        current_fragment = current_fragment->GetLink()) {
75     for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr;
76          current_frame = current_frame->GetLink()) {
77       if (current_frame->GetMethod()->IsNative()) {
78         // The JNI ShadowFrame only contains references. (For indirect reference.)
79         count += current_frame->NumberOfVRegs();
80       }
81     }
82   }
83   return count;
84 }
85
86 bool ManagedStack::ShadowFramesContain(StackReference<mirror::Object>* shadow_frame_entry) const {
87   for (const ManagedStack* current_fragment = this; current_fragment != nullptr;
88        current_fragment = current_fragment->GetLink()) {
89     for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != nullptr;
90          current_frame = current_frame->GetLink()) {
91       if (current_frame->Contains(shadow_frame_entry)) {
92         return true;
93       }
94     }
95   }
96   return false;
97 }
98
99 StackVisitor::StackVisitor(Thread* thread,
100                            Context* context,
101                            StackWalkKind walk_kind,
102                            bool check_suspended)
103     : StackVisitor(thread, context, walk_kind, 0, check_suspended) {}
104
105 StackVisitor::StackVisitor(Thread* thread,
106                            Context* context,
107                            StackWalkKind walk_kind,
108                            size_t num_frames,
109                            bool check_suspended)
110     : thread_(thread),
111       walk_kind_(walk_kind),
112       cur_shadow_frame_(nullptr),
113       cur_quick_frame_(nullptr),
114       cur_quick_frame_pc_(0),
115       cur_oat_quick_method_header_(nullptr),
116       num_frames_(num_frames),
117       cur_depth_(0),
118       current_inlining_depth_(0),
119       context_(context),
120       check_suspended_(check_suspended) {
121   if (check_suspended_) {
122     DCHECK(thread == Thread::Current() || thread->IsSuspended()) << *thread;
123   }
124 }
125
126 InlineInfo StackVisitor::GetCurrentInlineInfo() const {
127   const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
128   uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_);
129   CodeInfo code_info = method_header->GetOptimizedCodeInfo();
130   CodeInfoEncoding encoding = code_info.ExtractEncoding();
131   StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
132   DCHECK(stack_map.IsValid());
133   return code_info.GetInlineInfoOf(stack_map, encoding);
134 }
135
136 ArtMethod* StackVisitor::GetMethod() const {
137   if (cur_shadow_frame_ != nullptr) {
138     return cur_shadow_frame_->GetMethod();
139   } else if (cur_quick_frame_ != nullptr) {
140     if (IsInInlinedFrame()) {
141       size_t depth_in_stack_map = current_inlining_depth_ - 1;
142       InlineInfo inline_info = GetCurrentInlineInfo();
143       const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
144       CodeInfoEncoding encoding = method_header->GetOptimizedCodeInfo().ExtractEncoding();
145       MethodInfo method_info = method_header->GetOptimizedMethodInfo();
146       DCHECK(walk_kind_ != StackWalkKind::kSkipInlinedFrames);
147       return GetResolvedMethod(*GetCurrentQuickFrame(),
148                                method_info,
149                                inline_info,
150                                encoding.inline_info.encoding,
151                                depth_in_stack_map);
152     } else {
153       return *cur_quick_frame_;
154     }
155   }
156   return nullptr;
157 }
158
159 uint32_t StackVisitor::GetDexPc(bool abort_on_failure) const {
160   if (cur_shadow_frame_ != nullptr) {
161     return cur_shadow_frame_->GetDexPC();
162   } else if (cur_quick_frame_ != nullptr) {
163     if (IsInInlinedFrame()) {
164       size_t depth_in_stack_map = current_inlining_depth_ - 1;
165       const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
166       CodeInfoEncoding encoding = method_header->GetOptimizedCodeInfo().ExtractEncoding();
167       return GetCurrentInlineInfo().GetDexPcAtDepth(encoding.inline_info.encoding,
168                                                     depth_in_stack_map);
169     } else if (cur_oat_quick_method_header_ == nullptr) {
170       return DexFile::kDexNoIndex;
171     } else {
172       return cur_oat_quick_method_header_->ToDexPc(
173           GetMethod(), cur_quick_frame_pc_, abort_on_failure);
174     }
175   } else {
176     return 0;
177   }
178 }
179
180 extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
181     REQUIRES_SHARED(Locks::mutator_lock_);
182
183 mirror::Object* StackVisitor::GetThisObject() const {
184   DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
185   ArtMethod* m = GetMethod();
186   if (m->IsStatic()) {
187     return nullptr;
188   } else if (m->IsNative()) {
189     if (cur_quick_frame_ != nullptr) {
190       HandleScope* hs = reinterpret_cast<HandleScope*>(
191           reinterpret_cast<char*>(cur_quick_frame_) + sizeof(ArtMethod*));
192       return hs->GetReference(0);
193     } else {
194       return cur_shadow_frame_->GetVRegReference(0);
195     }
196   } else if (m->IsProxyMethod()) {
197     if (cur_quick_frame_ != nullptr) {
198       return artQuickGetProxyThisObject(cur_quick_frame_);
199     } else {
200       return cur_shadow_frame_->GetVRegReference(0);
201     }
202   } else {
203     const DexFile::CodeItem* code_item = m->GetCodeItem();
204     if (code_item == nullptr) {
205       UNIMPLEMENTED(ERROR) << "Failed to determine this object of abstract or proxy method: "
206           << ArtMethod::PrettyMethod(m);
207       return nullptr;
208     } else {
209       uint16_t reg = code_item->registers_size_ - code_item->ins_size_;
210       uint32_t value = 0;
211       bool success = GetVReg(m, reg, kReferenceVReg, &value);
212       // We currently always guarantee the `this` object is live throughout the method.
213       CHECK(success) << "Failed to read the this object in " << ArtMethod::PrettyMethod(m);
214       return reinterpret_cast<mirror::Object*>(value);
215     }
216   }
217 }
218
219 size_t StackVisitor::GetNativePcOffset() const {
220   DCHECK(!IsShadowFrame());
221   return GetCurrentOatQuickMethodHeader()->NativeQuickPcOffset(cur_quick_frame_pc_);
222 }
223
224 bool StackVisitor::GetVRegFromDebuggerShadowFrame(uint16_t vreg,
225                                                   VRegKind kind,
226                                                   uint32_t* val) const {
227   size_t frame_id = const_cast<StackVisitor*>(this)->GetFrameId();
228   ShadowFrame* shadow_frame = thread_->FindDebuggerShadowFrame(frame_id);
229   if (shadow_frame != nullptr) {
230     bool* updated_vreg_flags = thread_->GetUpdatedVRegFlags(frame_id);
231     DCHECK(updated_vreg_flags != nullptr);
232     if (updated_vreg_flags[vreg]) {
233       // Value is set by the debugger.
234       if (kind == kReferenceVReg) {
235         *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
236             shadow_frame->GetVRegReference(vreg)));
237       } else {
238         *val = shadow_frame->GetVReg(vreg);
239       }
240       return true;
241     }
242   }
243   // No value is set by the debugger.
244   return false;
245 }
246
247 bool StackVisitor::GetVReg(ArtMethod* m, uint16_t vreg, VRegKind kind, uint32_t* val) const {
248   if (cur_quick_frame_ != nullptr) {
249     DCHECK(context_ != nullptr);  // You can't reliably read registers without a context.
250     DCHECK(m == GetMethod());
251     // Check if there is value set by the debugger.
252     if (GetVRegFromDebuggerShadowFrame(vreg, kind, val)) {
253       return true;
254     }
255     DCHECK(cur_oat_quick_method_header_->IsOptimized());
256     return GetVRegFromOptimizedCode(m, vreg, kind, val);
257   } else {
258     DCHECK(cur_shadow_frame_ != nullptr);
259     if (kind == kReferenceVReg) {
260       *val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
261           cur_shadow_frame_->GetVRegReference(vreg)));
262     } else {
263       *val = cur_shadow_frame_->GetVReg(vreg);
264     }
265     return true;
266   }
267 }
268
269 bool StackVisitor::GetVRegFromOptimizedCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
270                                             uint32_t* val) const {
271   DCHECK_EQ(m, GetMethod());
272   const DexFile::CodeItem* code_item = m->GetCodeItem();
273   DCHECK(code_item != nullptr) << m->PrettyMethod();  // Can't be null or how would we compile
274                                                       // its instructions?
275   uint16_t number_of_dex_registers = code_item->registers_size_;
276   DCHECK_LT(vreg, code_item->registers_size_);
277   const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
278   CodeInfo code_info = method_header->GetOptimizedCodeInfo();
279   CodeInfoEncoding encoding = code_info.ExtractEncoding();
280
281   uint32_t native_pc_offset = method_header->NativeQuickPcOffset(cur_quick_frame_pc_);
282   StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
283   DCHECK(stack_map.IsValid());
284   size_t depth_in_stack_map = current_inlining_depth_ - 1;
285
286   DexRegisterMap dex_register_map = IsInInlinedFrame()
287       ? code_info.GetDexRegisterMapAtDepth(depth_in_stack_map,
288                                            code_info.GetInlineInfoOf(stack_map, encoding),
289                                            encoding,
290                                            number_of_dex_registers)
291       : code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers);
292
293   if (!dex_register_map.IsValid()) {
294     return false;
295   }
296   DexRegisterLocation::Kind location_kind =
297       dex_register_map.GetLocationKind(vreg, number_of_dex_registers, code_info, encoding);
298   switch (location_kind) {
299     case DexRegisterLocation::Kind::kInStack: {
300       const int32_t offset = dex_register_map.GetStackOffsetInBytes(vreg,
301                                                                     number_of_dex_registers,
302                                                                     code_info,
303                                                                     encoding);
304       const uint8_t* addr = reinterpret_cast<const uint8_t*>(cur_quick_frame_) + offset;
305       *val = *reinterpret_cast<const uint32_t*>(addr);
306       return true;
307     }
308     case DexRegisterLocation::Kind::kInRegister:
309     case DexRegisterLocation::Kind::kInRegisterHigh:
310     case DexRegisterLocation::Kind::kInFpuRegister:
311     case DexRegisterLocation::Kind::kInFpuRegisterHigh: {
312       uint32_t reg =
313           dex_register_map.GetMachineRegister(vreg, number_of_dex_registers, code_info, encoding);
314       return GetRegisterIfAccessible(reg, kind, val);
315     }
316     case DexRegisterLocation::Kind::kConstant:
317       *val = dex_register_map.GetConstant(vreg, number_of_dex_registers, code_info, encoding);
318       return true;
319     case DexRegisterLocation::Kind::kNone:
320       return false;
321     default:
322       LOG(FATAL)
323           << "Unexpected location kind "
324           << dex_register_map.GetLocationInternalKind(vreg,
325                                                       number_of_dex_registers,
326                                                       code_info,
327                                                       encoding);
328       UNREACHABLE();
329   }
330 }
331
332 bool StackVisitor::GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const {
333   const bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
334
335   if (kRuntimeISA == InstructionSet::kX86 && is_float) {
336     // X86 float registers are 64-bit and each XMM register is provided as two separate
337     // 32-bit registers by the context.
338     reg = (kind == kDoubleHiVReg) ? (2 * reg + 1) : (2 * reg);
339   }
340
341   // MIPS32 float registers are used as 64-bit (for MIPS32r2 it is pair
342   // F(2n)-F(2n+1), and for MIPS32r6 it is 64-bit register F(2n)). When
343   // accessing upper 32-bits from double, reg + 1 should be used.
344   if ((kRuntimeISA == InstructionSet::kMips) && (kind == kDoubleHiVReg)) {
345     DCHECK_ALIGNED(reg, 2);
346     reg++;
347   }
348
349   if (!IsAccessibleRegister(reg, is_float)) {
350     return false;
351   }
352   uintptr_t ptr_val = GetRegister(reg, is_float);
353   const bool target64 = Is64BitInstructionSet(kRuntimeISA);
354   if (target64) {
355     const bool wide_lo = (kind == kLongLoVReg) || (kind == kDoubleLoVReg);
356     const bool wide_hi = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
357     int64_t value_long = static_cast<int64_t>(ptr_val);
358     if (wide_lo) {
359       ptr_val = static_cast<uintptr_t>(Low32Bits(value_long));
360     } else if (wide_hi) {
361       ptr_val = static_cast<uintptr_t>(High32Bits(value_long));
362     }
363   }
364   *val = ptr_val;
365   return true;
366 }
367
368 bool StackVisitor::GetVRegPairFromDebuggerShadowFrame(uint16_t vreg,
369                                                       VRegKind kind_lo,
370                                                       VRegKind kind_hi,
371                                                       uint64_t* val) const {
372   uint32_t low_32bits;
373   uint32_t high_32bits;
374   bool success = GetVRegFromDebuggerShadowFrame(vreg, kind_lo, &low_32bits);
375   success &= GetVRegFromDebuggerShadowFrame(vreg + 1, kind_hi, &high_32bits);
376   if (success) {
377     *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
378   }
379   return success;
380 }
381
382 bool StackVisitor::GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
383                                VRegKind kind_hi, uint64_t* val) const {
384   if (kind_lo == kLongLoVReg) {
385     DCHECK_EQ(kind_hi, kLongHiVReg);
386   } else if (kind_lo == kDoubleLoVReg) {
387     DCHECK_EQ(kind_hi, kDoubleHiVReg);
388   } else {
389     LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
390     UNREACHABLE();
391   }
392   // Check if there is value set by the debugger.
393   if (GetVRegPairFromDebuggerShadowFrame(vreg, kind_lo, kind_hi, val)) {
394     return true;
395   }
396   if (cur_quick_frame_ != nullptr) {
397     DCHECK(context_ != nullptr);  // You can't reliably read registers without a context.
398     DCHECK(m == GetMethod());
399     DCHECK(cur_oat_quick_method_header_->IsOptimized());
400     return GetVRegPairFromOptimizedCode(m, vreg, kind_lo, kind_hi, val);
401   } else {
402     DCHECK(cur_shadow_frame_ != nullptr);
403     *val = cur_shadow_frame_->GetVRegLong(vreg);
404     return true;
405   }
406 }
407
408 bool StackVisitor::GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg,
409                                                 VRegKind kind_lo, VRegKind kind_hi,
410                                                 uint64_t* val) const {
411   uint32_t low_32bits;
412   uint32_t high_32bits;
413   bool success = GetVRegFromOptimizedCode(m, vreg, kind_lo, &low_32bits);
414   success &= GetVRegFromOptimizedCode(m, vreg + 1, kind_hi, &high_32bits);
415   if (success) {
416     *val = (static_cast<uint64_t>(high_32bits) << 32) | static_cast<uint64_t>(low_32bits);
417   }
418   return success;
419 }
420
421 bool StackVisitor::GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi,
422                                                VRegKind kind_lo, uint64_t* val) const {
423   const bool is_float = (kind_lo == kDoubleLoVReg);
424   if (!IsAccessibleRegister(reg_lo, is_float) || !IsAccessibleRegister(reg_hi, is_float)) {
425     return false;
426   }
427   uintptr_t ptr_val_lo = GetRegister(reg_lo, is_float);
428   uintptr_t ptr_val_hi = GetRegister(reg_hi, is_float);
429   bool target64 = Is64BitInstructionSet(kRuntimeISA);
430   if (target64) {
431     int64_t value_long_lo = static_cast<int64_t>(ptr_val_lo);
432     int64_t value_long_hi = static_cast<int64_t>(ptr_val_hi);
433     ptr_val_lo = static_cast<uintptr_t>(Low32Bits(value_long_lo));
434     ptr_val_hi = static_cast<uintptr_t>(High32Bits(value_long_hi));
435   }
436   *val = (static_cast<uint64_t>(ptr_val_hi) << 32) | static_cast<uint32_t>(ptr_val_lo);
437   return true;
438 }
439
440 bool StackVisitor::SetVReg(ArtMethod* m,
441                            uint16_t vreg,
442                            uint32_t new_value,
443                            VRegKind kind) {
444   const DexFile::CodeItem* code_item = m->GetCodeItem();
445   if (code_item == nullptr) {
446     return false;
447   }
448   ShadowFrame* shadow_frame = GetCurrentShadowFrame();
449   if (shadow_frame == nullptr) {
450     // This is a compiled frame: we must prepare and update a shadow frame that will
451     // be executed by the interpreter after deoptimization of the stack.
452     const size_t frame_id = GetFrameId();
453     const uint16_t num_regs = code_item->registers_size_;
454     shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
455     CHECK(shadow_frame != nullptr);
456     // Remember the vreg has been set for debugging and must not be overwritten by the
457     // original value during deoptimization of the stack.
458     thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
459   }
460   if (kind == kReferenceVReg) {
461     shadow_frame->SetVRegReference(vreg, reinterpret_cast<mirror::Object*>(new_value));
462   } else {
463     shadow_frame->SetVReg(vreg, new_value);
464   }
465   return true;
466 }
467
468 bool StackVisitor::SetVRegPair(ArtMethod* m,
469                                uint16_t vreg,
470                                uint64_t new_value,
471                                VRegKind kind_lo,
472                                VRegKind kind_hi) {
473   if (kind_lo == kLongLoVReg) {
474     DCHECK_EQ(kind_hi, kLongHiVReg);
475   } else if (kind_lo == kDoubleLoVReg) {
476     DCHECK_EQ(kind_hi, kDoubleHiVReg);
477   } else {
478     LOG(FATAL) << "Expected long or double: kind_lo=" << kind_lo << ", kind_hi=" << kind_hi;
479     UNREACHABLE();
480   }
481   const DexFile::CodeItem* code_item = m->GetCodeItem();
482   if (code_item == nullptr) {
483     return false;
484   }
485   ShadowFrame* shadow_frame = GetCurrentShadowFrame();
486   if (shadow_frame == nullptr) {
487     // This is a compiled frame: we must prepare for deoptimization (see SetVRegFromDebugger).
488     const size_t frame_id = GetFrameId();
489     const uint16_t num_regs = code_item->registers_size_;
490     shadow_frame = thread_->FindOrCreateDebuggerShadowFrame(frame_id, num_regs, m, GetDexPc());
491     CHECK(shadow_frame != nullptr);
492     // Remember the vreg pair has been set for debugging and must not be overwritten by the
493     // original value during deoptimization of the stack.
494     thread_->GetUpdatedVRegFlags(frame_id)[vreg] = true;
495     thread_->GetUpdatedVRegFlags(frame_id)[vreg + 1] = true;
496   }
497   shadow_frame->SetVRegLong(vreg, new_value);
498   return true;
499 }
500
501 bool StackVisitor::IsAccessibleGPR(uint32_t reg) const {
502   DCHECK(context_ != nullptr);
503   return context_->IsAccessibleGPR(reg);
504 }
505
506 uintptr_t* StackVisitor::GetGPRAddress(uint32_t reg) const {
507   DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
508   DCHECK(context_ != nullptr);
509   return context_->GetGPRAddress(reg);
510 }
511
512 uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
513   DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
514   DCHECK(context_ != nullptr);
515   return context_->GetGPR(reg);
516 }
517
518 bool StackVisitor::IsAccessibleFPR(uint32_t reg) const {
519   DCHECK(context_ != nullptr);
520   return context_->IsAccessibleFPR(reg);
521 }
522
523 uintptr_t StackVisitor::GetFPR(uint32_t reg) const {
524   DCHECK(cur_quick_frame_ != nullptr) << "This is a quick frame routine";
525   DCHECK(context_ != nullptr);
526   return context_->GetFPR(reg);
527 }
528
529 uintptr_t StackVisitor::GetReturnPc() const {
530   uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
531   DCHECK(sp != nullptr);
532   uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
533   return *reinterpret_cast<uintptr_t*>(pc_addr);
534 }
535
536 void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
537   uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame());
538   CHECK(sp != nullptr);
539   uint8_t* pc_addr = sp + GetCurrentQuickFrameInfo().GetReturnPcOffset();
540   *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc;
541 }
542
543 size_t StackVisitor::ComputeNumFrames(Thread* thread, StackWalkKind walk_kind) {
544   struct NumFramesVisitor : public StackVisitor {
545     NumFramesVisitor(Thread* thread_in, StackWalkKind walk_kind_in)
546         : StackVisitor(thread_in, nullptr, walk_kind_in), frames(0) {}
547
548     bool VisitFrame() OVERRIDE {
549       frames++;
550       return true;
551     }
552
553     size_t frames;
554   };
555   NumFramesVisitor visitor(thread, walk_kind);
556   visitor.WalkStack(true);
557   return visitor.frames;
558 }
559
560 bool StackVisitor::GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc) {
561   struct HasMoreFramesVisitor : public StackVisitor {
562     HasMoreFramesVisitor(Thread* thread,
563                          StackWalkKind walk_kind,
564                          size_t num_frames,
565                          size_t frame_height)
566         : StackVisitor(thread, nullptr, walk_kind, num_frames),
567           frame_height_(frame_height),
568           found_frame_(false),
569           has_more_frames_(false),
570           next_method_(nullptr),
571           next_dex_pc_(0) {
572     }
573
574     bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
575       if (found_frame_) {
576         ArtMethod* method = GetMethod();
577         if (method != nullptr && !method->IsRuntimeMethod()) {
578           has_more_frames_ = true;
579           next_method_ = method;
580           next_dex_pc_ = GetDexPc();
581           return false;  // End stack walk once next method is found.
582         }
583       } else if (GetFrameHeight() == frame_height_) {
584         found_frame_ = true;
585       }
586       return true;
587     }
588
589     size_t frame_height_;
590     bool found_frame_;
591     bool has_more_frames_;
592     ArtMethod* next_method_;
593     uint32_t next_dex_pc_;
594   };
595   HasMoreFramesVisitor visitor(thread_, walk_kind_, GetNumFrames(), GetFrameHeight());
596   visitor.WalkStack(true);
597   *next_method = visitor.next_method_;
598   *next_dex_pc = visitor.next_dex_pc_;
599   return visitor.has_more_frames_;
600 }
601
602 void StackVisitor::DescribeStack(Thread* thread) {
603   struct DescribeStackVisitor : public StackVisitor {
604     explicit DescribeStackVisitor(Thread* thread_in)
605         : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
606
607     bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
608       LOG(INFO) << "Frame Id=" << GetFrameId() << " " << DescribeLocation();
609       return true;
610     }
611   };
612   DescribeStackVisitor visitor(thread);
613   visitor.WalkStack(true);
614 }
615
616 std::string StackVisitor::DescribeLocation() const {
617   std::string result("Visiting method '");
618   ArtMethod* m = GetMethod();
619   if (m == nullptr) {
620     return "upcall";
621   }
622   result += m->PrettyMethod();
623   result += StringPrintf("' at dex PC 0x%04x", GetDexPc());
624   if (!IsShadowFrame()) {
625     result += StringPrintf(" (native PC %p)", reinterpret_cast<void*>(GetCurrentQuickFramePc()));
626   }
627   return result;
628 }
629
630 void StackVisitor::SetMethod(ArtMethod* method) {
631   DCHECK(GetMethod() != nullptr);
632   if (cur_shadow_frame_ != nullptr) {
633     cur_shadow_frame_->SetMethod(method);
634   } else {
635     DCHECK(cur_quick_frame_ != nullptr);
636     CHECK(!IsInInlinedFrame()) << "We do not support setting inlined method's ArtMethod!";
637     *cur_quick_frame_ = method;
638   }
639 }
640
641 static void AssertPcIsWithinQuickCode(ArtMethod* method, uintptr_t pc)
642     REQUIRES_SHARED(Locks::mutator_lock_) {
643   if (method->IsNative() || method->IsRuntimeMethod() || method->IsProxyMethod()) {
644     return;
645   }
646
647   if (pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) {
648     return;
649   }
650
651   const void* code = method->GetEntryPointFromQuickCompiledCode();
652   if (code == GetQuickInstrumentationEntryPoint() || code == GetInvokeObsoleteMethodStub()) {
653     return;
654   }
655
656   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
657   if (class_linker->IsQuickToInterpreterBridge(code) ||
658       class_linker->IsQuickResolutionStub(code)) {
659     return;
660   }
661
662   // If we are the JIT then we may have just compiled the method after the
663   // IsQuickToInterpreterBridge check.
664   Runtime* runtime = Runtime::Current();
665   if (runtime->UseJitCompilation() && runtime->GetJit()->GetCodeCache()->ContainsPc(code)) {
666     return;
667   }
668
669   uint32_t code_size = OatQuickMethodHeader::FromEntryPoint(code)->GetCodeSize();
670   uintptr_t code_start = reinterpret_cast<uintptr_t>(code);
671   CHECK(code_start <= pc && pc <= (code_start + code_size))
672       << method->PrettyMethod()
673       << " pc=" << std::hex << pc
674       << " code_start=" << code_start
675       << " code_size=" << code_size;
676 }
677
678 void StackVisitor::SanityCheckFrame() const {
679   if (kIsDebugBuild) {
680     ArtMethod* method = GetMethod();
681     auto* declaring_class = method->GetDeclaringClass();
682     // Runtime methods have null declaring class.
683     if (!method->IsRuntimeMethod()) {
684       CHECK(declaring_class != nullptr);
685       CHECK_EQ(declaring_class->GetClass(), declaring_class->GetClass()->GetClass())
686           << declaring_class;
687     } else {
688       CHECK(declaring_class == nullptr);
689     }
690     Runtime* const runtime = Runtime::Current();
691     LinearAlloc* const linear_alloc = runtime->GetLinearAlloc();
692     if (!linear_alloc->Contains(method)) {
693       // Check class linker linear allocs.
694       mirror::Class* klass = method->GetDeclaringClass();
695       LinearAlloc* const class_linear_alloc = (klass != nullptr)
696           ? runtime->GetClassLinker()->GetAllocatorForClassLoader(klass->GetClassLoader())
697           : linear_alloc;
698       if (!class_linear_alloc->Contains(method)) {
699         // Check image space.
700         bool in_image = false;
701         for (auto& space : runtime->GetHeap()->GetContinuousSpaces()) {
702           if (space->IsImageSpace()) {
703             auto* image_space = space->AsImageSpace();
704             const auto& header = image_space->GetImageHeader();
705             const ImageSection& methods = header.GetMethodsSection();
706             const ImageSection& runtime_methods = header.GetRuntimeMethodsSection();
707             const size_t offset =  reinterpret_cast<const uint8_t*>(method) - image_space->Begin();
708             if (methods.Contains(offset) || runtime_methods.Contains(offset)) {
709               in_image = true;
710               break;
711             }
712           }
713         }
714         CHECK(in_image) << method->PrettyMethod() << " not in linear alloc or image";
715       }
716     }
717     if (cur_quick_frame_ != nullptr) {
718       AssertPcIsWithinQuickCode(method, cur_quick_frame_pc_);
719       // Frame sanity.
720       size_t frame_size = GetCurrentQuickFrameInfo().FrameSizeInBytes();
721       CHECK_NE(frame_size, 0u);
722       // A rough guess at an upper size we expect to see for a frame.
723       // 256 registers
724       // 2 words HandleScope overhead
725       // 3+3 register spills
726       // TODO: this seems architecture specific for the case of JNI frames.
727       // TODO: 083-compiler-regressions ManyFloatArgs shows this estimate is wrong.
728       // const size_t kMaxExpectedFrameSize = (256 + 2 + 3 + 3) * sizeof(word);
729       const size_t kMaxExpectedFrameSize = 2 * KB;
730       CHECK_LE(frame_size, kMaxExpectedFrameSize) << method->PrettyMethod();
731       size_t return_pc_offset = GetCurrentQuickFrameInfo().GetReturnPcOffset();
732       CHECK_LT(return_pc_offset, frame_size);
733     }
734   }
735 }
736
737 // Counts the number of references in the parameter list of the corresponding method.
738 // Note: Thus does _not_ include "this" for non-static methods.
739 static uint32_t GetNumberOfReferenceArgsWithoutReceiver(ArtMethod* method)
740     REQUIRES_SHARED(Locks::mutator_lock_) {
741   uint32_t shorty_len;
742   const char* shorty = method->GetShorty(&shorty_len);
743   uint32_t refs = 0;
744   for (uint32_t i = 1; i < shorty_len ; ++i) {
745     if (shorty[i] == 'L') {
746       refs++;
747     }
748   }
749   return refs;
750 }
751
752 QuickMethodFrameInfo StackVisitor::GetCurrentQuickFrameInfo() const {
753   if (cur_oat_quick_method_header_ != nullptr) {
754     return cur_oat_quick_method_header_->GetFrameInfo();
755   }
756
757   ArtMethod* method = GetMethod();
758   Runtime* runtime = Runtime::Current();
759
760   if (method->IsAbstract()) {
761     return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kSaveRefsAndArgs);
762   }
763
764   // This goes before IsProxyMethod since runtime methods have a null declaring class.
765   if (method->IsRuntimeMethod()) {
766     return runtime->GetRuntimeMethodFrameInfo(method);
767   }
768
769   if (method->IsProxyMethod()) {
770     // There is only one direct method of a proxy class: the constructor. A direct method is
771     // cloned from the original java.lang.reflect.Proxy and is executed as usual quick
772     // compiled method without any stubs. Therefore the method must have a OatQuickMethodHeader.
773     DCHECK(!method->IsDirect() && !method->IsConstructor())
774         << "Constructors of proxy classes must have a OatQuickMethodHeader";
775     return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kSaveRefsAndArgs);
776   }
777
778   // The only remaining case is if the method is native and uses the generic JNI stub.
779   DCHECK(method->IsNative());
780   ClassLinker* class_linker = runtime->GetClassLinker();
781   const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(method,
782                                                                            kRuntimePointerSize);
783   DCHECK(class_linker->IsQuickGenericJniStub(entry_point)) << method->PrettyMethod();
784   // Generic JNI frame.
785   uint32_t handle_refs = GetNumberOfReferenceArgsWithoutReceiver(method) + 1;
786   size_t scope_size = HandleScope::SizeOf(handle_refs);
787   QuickMethodFrameInfo callee_info =
788       runtime->GetCalleeSaveMethodFrameInfo(Runtime::kSaveRefsAndArgs);
789
790   // Callee saves + handle scope + method ref + alignment
791   // Note: -sizeof(void*) since callee-save frame stores a whole method pointer.
792   size_t frame_size = RoundUp(
793       callee_info.FrameSizeInBytes() - sizeof(void*) + sizeof(ArtMethod*) + scope_size,
794       kStackAlignment);
795   return QuickMethodFrameInfo(frame_size, callee_info.CoreSpillMask(), callee_info.FpSpillMask());
796 }
797
798 template <StackVisitor::CountTransitions kCount>
799 void StackVisitor::WalkStack(bool include_transitions) {
800   if (check_suspended_) {
801     DCHECK(thread_ == Thread::Current() || thread_->IsSuspended());
802   }
803   CHECK_EQ(cur_depth_, 0U);
804   bool exit_stubs_installed = Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled();
805   uint32_t instrumentation_stack_depth = 0;
806   size_t inlined_frames_count = 0;
807
808   for (const ManagedStack* current_fragment = thread_->GetManagedStack();
809        current_fragment != nullptr; current_fragment = current_fragment->GetLink()) {
810     cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
811     cur_quick_frame_ = current_fragment->GetTopQuickFrame();
812     cur_quick_frame_pc_ = 0;
813     cur_oat_quick_method_header_ = nullptr;
814
815     if (cur_quick_frame_ != nullptr) {  // Handle quick stack frames.
816       // Can't be both a shadow and a quick fragment.
817       DCHECK(current_fragment->GetTopShadowFrame() == nullptr);
818       ArtMethod* method = *cur_quick_frame_;
819       while (method != nullptr) {
820         cur_oat_quick_method_header_ = method->GetOatQuickMethodHeader(cur_quick_frame_pc_);
821         SanityCheckFrame();
822
823         if ((walk_kind_ == StackWalkKind::kIncludeInlinedFrames)
824             && (cur_oat_quick_method_header_ != nullptr)
825             && cur_oat_quick_method_header_->IsOptimized()) {
826           CodeInfo code_info = cur_oat_quick_method_header_->GetOptimizedCodeInfo();
827           CodeInfoEncoding encoding = code_info.ExtractEncoding();
828           uint32_t native_pc_offset =
829               cur_oat_quick_method_header_->NativeQuickPcOffset(cur_quick_frame_pc_);
830           StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
831           if (stack_map.IsValid() && stack_map.HasInlineInfo(encoding.stack_map.encoding)) {
832             InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
833             DCHECK_EQ(current_inlining_depth_, 0u);
834             for (current_inlining_depth_ = inline_info.GetDepth(encoding.inline_info.encoding);
835                  current_inlining_depth_ != 0;
836                  --current_inlining_depth_) {
837               bool should_continue = VisitFrame();
838               if (UNLIKELY(!should_continue)) {
839                 return;
840               }
841               cur_depth_++;
842               inlined_frames_count++;
843             }
844           }
845         }
846
847         bool should_continue = VisitFrame();
848         if (UNLIKELY(!should_continue)) {
849           return;
850         }
851
852         QuickMethodFrameInfo frame_info = GetCurrentQuickFrameInfo();
853         if (context_ != nullptr) {
854           context_->FillCalleeSaves(reinterpret_cast<uint8_t*>(cur_quick_frame_), frame_info);
855         }
856         // Compute PC for next stack frame from return PC.
857         size_t frame_size = frame_info.FrameSizeInBytes();
858         size_t return_pc_offset = frame_size - sizeof(void*);
859         uint8_t* return_pc_addr = reinterpret_cast<uint8_t*>(cur_quick_frame_) + return_pc_offset;
860         uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
861
862         if (UNLIKELY(exit_stubs_installed)) {
863           // While profiling, the return pc is restored from the side stack, except when walking
864           // the stack for an exception where the side stack will be unwound in VisitFrame.
865           if (reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == return_pc) {
866             CHECK_LT(instrumentation_stack_depth, thread_->GetInstrumentationStack()->size());
867             const instrumentation::InstrumentationStackFrame& instrumentation_frame =
868                 thread_->GetInstrumentationStack()->at(instrumentation_stack_depth);
869             instrumentation_stack_depth++;
870             if (GetMethod() ==
871                 Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveAllCalleeSaves)) {
872               // Skip runtime save all callee frames which are used to deliver exceptions.
873             } else if (instrumentation_frame.interpreter_entry_) {
874               ArtMethod* callee =
875                   Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveRefsAndArgs);
876               CHECK_EQ(GetMethod(), callee) << "Expected: " << ArtMethod::PrettyMethod(callee)
877                                             << " Found: " << ArtMethod::PrettyMethod(GetMethod());
878             } else {
879               // Instrumentation generally doesn't distinguish between a method's obsolete and
880               // non-obsolete version.
881               CHECK_EQ(instrumentation_frame.method_->GetNonObsoleteMethod(),
882                        GetMethod()->GetNonObsoleteMethod())
883                   << "Expected: "
884                   << ArtMethod::PrettyMethod(instrumentation_frame.method_->GetNonObsoleteMethod())
885                   << " Found: " << ArtMethod::PrettyMethod(GetMethod()->GetNonObsoleteMethod());
886             }
887             if (num_frames_ != 0) {
888               // Check agreement of frame Ids only if num_frames_ is computed to avoid infinite
889               // recursion.
890               size_t frame_id = instrumentation::Instrumentation::ComputeFrameId(
891                   thread_,
892                   cur_depth_,
893                   inlined_frames_count);
894               CHECK_EQ(instrumentation_frame.frame_id_, frame_id);
895             }
896             return_pc = instrumentation_frame.return_pc_;
897           }
898         }
899
900         cur_quick_frame_pc_ = return_pc;
901         uint8_t* next_frame = reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size;
902         cur_quick_frame_ = reinterpret_cast<ArtMethod**>(next_frame);
903
904         if (kDebugStackWalk) {
905           LOG(INFO) << ArtMethod::PrettyMethod(method) << "@" << method << " size=" << frame_size
906               << std::boolalpha
907               << " optimized=" << (cur_oat_quick_method_header_ != nullptr &&
908                                    cur_oat_quick_method_header_->IsOptimized())
909               << " native=" << method->IsNative()
910               << std::noboolalpha
911               << " entrypoints=" << method->GetEntryPointFromQuickCompiledCode()
912               << "," << (method->IsNative() ? method->GetEntryPointFromJni() : nullptr)
913               << " next=" << *cur_quick_frame_;
914         }
915
916         if (kCount == CountTransitions::kYes || !method->IsRuntimeMethod()) {
917           cur_depth_++;
918         }
919         method = *cur_quick_frame_;
920       }
921     } else if (cur_shadow_frame_ != nullptr) {
922       do {
923         SanityCheckFrame();
924         bool should_continue = VisitFrame();
925         if (UNLIKELY(!should_continue)) {
926           return;
927         }
928         cur_depth_++;
929         cur_shadow_frame_ = cur_shadow_frame_->GetLink();
930       } while (cur_shadow_frame_ != nullptr);
931     }
932     if (include_transitions) {
933       bool should_continue = VisitFrame();
934       if (!should_continue) {
935         return;
936       }
937     }
938     if (kCount == CountTransitions::kYes) {
939       cur_depth_++;
940     }
941   }
942   if (num_frames_ != 0) {
943     CHECK_EQ(cur_depth_, num_frames_);
944   }
945 }
946
947 template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kYes>(bool);
948 template void StackVisitor::WalkStack<StackVisitor::CountTransitions::kNo>(bool);
949
950 void JavaFrameRootInfo::Describe(std::ostream& os) const {
951   const StackVisitor* visitor = stack_visitor_;
952   CHECK(visitor != nullptr);
953   os << "Type=" << GetType() << " thread_id=" << GetThreadId() << " location=" <<
954       visitor->DescribeLocation() << " vreg=" << vreg_;
955 }
956
957 int StackVisitor::GetVRegOffsetFromQuickCode(const DexFile::CodeItem* code_item,
958                                              uint32_t core_spills, uint32_t fp_spills,
959                                              size_t frame_size, int reg, InstructionSet isa) {
960   PointerSize pointer_size = InstructionSetPointerSize(isa);
961   if (kIsDebugBuild) {
962     auto* runtime = Runtime::Current();
963     if (runtime != nullptr) {
964       CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
965     }
966   }
967   DCHECK_ALIGNED(frame_size, kStackAlignment);
968   DCHECK_NE(reg, -1);
969   int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
970       + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
971       + sizeof(uint32_t);  // Filler.
972   int num_regs = code_item->registers_size_ - code_item->ins_size_;
973   int temp_threshold = code_item->registers_size_;
974   const int max_num_special_temps = 1;
975   if (reg == temp_threshold) {
976     // The current method pointer corresponds to special location on stack.
977     return 0;
978   } else if (reg >= temp_threshold + max_num_special_temps) {
979     /*
980      * Special temporaries may have custom locations and the logic above deals with that.
981      * However, non-special temporaries are placed relative to the outs.
982      */
983     int temps_start = code_item->outs_size_ * sizeof(uint32_t)
984         + static_cast<size_t>(pointer_size) /* art method */;
985     int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
986     return temps_start + relative_offset;
987   }  else if (reg < num_regs) {
988     int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
989     return locals_start + (reg * sizeof(uint32_t));
990   } else {
991     // Handle ins.
992     return frame_size + ((reg - num_regs) * sizeof(uint32_t))
993         + static_cast<size_t>(pointer_size) /* art method */;
994   }
995 }
996
997 void LockCountData::AddMonitor(Thread* self, mirror::Object* obj) {
998   if (obj == nullptr) {
999     return;
1000   }
1001
1002   // If there's an error during enter, we won't have locked the monitor. So check there's no
1003   // exception.
1004   if (self->IsExceptionPending()) {
1005     return;
1006   }
1007
1008   if (monitors_ == nullptr) {
1009     monitors_.reset(new std::vector<mirror::Object*>());
1010   }
1011   monitors_->push_back(obj);
1012 }
1013
1014 void LockCountData::RemoveMonitorOrThrow(Thread* self, const mirror::Object* obj) {
1015   if (obj == nullptr) {
1016     return;
1017   }
1018   bool found_object = false;
1019   if (monitors_ != nullptr) {
1020     // We need to remove one pointer to ref, as duplicates are used for counting recursive locks.
1021     // We arbitrarily choose the first one.
1022     auto it = std::find(monitors_->begin(), monitors_->end(), obj);
1023     if (it != monitors_->end()) {
1024       monitors_->erase(it);
1025       found_object = true;
1026     }
1027   }
1028   if (!found_object) {
1029     // The object wasn't found. Time for an IllegalMonitorStateException.
1030     // The order here isn't fully clear. Assume that any other pending exception is swallowed.
1031     // TODO: Maybe make already pending exception a suppressed exception.
1032     self->ClearException();
1033     self->ThrowNewExceptionF("Ljava/lang/IllegalMonitorStateException;",
1034                              "did not lock monitor on object of type '%s' before unlocking",
1035                              const_cast<mirror::Object*>(obj)->PrettyTypeOf().c_str());
1036   }
1037 }
1038
1039 // Helper to unlock a monitor. Must be NO_THREAD_SAFETY_ANALYSIS, as we can't statically show
1040 // that the object was locked.
1041 void MonitorExitHelper(Thread* self, mirror::Object* obj) NO_THREAD_SAFETY_ANALYSIS {
1042   DCHECK(self != nullptr);
1043   DCHECK(obj != nullptr);
1044   obj->MonitorExit(self);
1045 }
1046
1047 bool LockCountData::CheckAllMonitorsReleasedOrThrow(Thread* self) {
1048   DCHECK(self != nullptr);
1049   if (monitors_ != nullptr) {
1050     if (!monitors_->empty()) {
1051       // There may be an exception pending, if the method is terminating abruptly. Clear it.
1052       // TODO: Should we add this as a suppressed exception?
1053       self->ClearException();
1054
1055       // OK, there are monitors that are still locked. To enforce structured locking (and avoid
1056       // deadlocks) we unlock all of them before we raise the IllegalMonitorState exception.
1057       for (mirror::Object* obj : *monitors_) {
1058         MonitorExitHelper(self, obj);
1059         // If this raised an exception, ignore. TODO: Should we add this as suppressed
1060         // exceptions?
1061         if (self->IsExceptionPending()) {
1062           self->ClearException();
1063         }
1064       }
1065       // Raise an exception, just give the first object as the sample.
1066       mirror::Object* first = (*monitors_)[0];
1067       self->ThrowNewExceptionF("Ljava/lang/IllegalMonitorStateException;",
1068                                "did not unlock monitor on object of type '%s'",
1069                                mirror::Object::PrettyTypeOf(first).c_str());
1070
1071       // To make sure this path is not triggered again, clean out the monitors.
1072       monitors_->clear();
1073
1074       return false;
1075     }
1076   }
1077   return true;
1078 }
1079
1080 }  // namespace art