blob: 36a6b7fc47c3d2b490bfff3deeaf18e59e1b03d0 [file] [log] [blame]
Sebastien Hertzd45a1f52014-01-09 14:56:54 +01001/*
2 * Copyright (C) 2014 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
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020017#include "quick_exception_handler.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"
Andreas Gampe542451c2016-07-26 09:02:02 -070021#include "base/enums.h"
Andreas Gampe170331f2017-12-07 18:41:03 -080022#include "base/logging.h" // For VLOG_IS_ON.
David Sehr9e734c72018-01-04 17:56:19 -080023#include "dex/dex_file_types.h"
24#include "dex/dex_instruction.h"
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020025#include "entrypoints/entrypoint_utils.h"
Andreas Gampe639bdd12015-06-03 11:22:45 -070026#include "entrypoints/quick/quick_entrypoints_enum.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070027#include "entrypoints/runtime_asm_entrypoints.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070028#include "handle_scope-inl.h"
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +010029#include "interpreter/shadow_frame-inl.h"
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +000030#include "jit/jit.h"
31#include "jit/jit_code_cache.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070032#include "mirror/class-inl.h"
33#include "mirror/class_loader.h"
34#include "mirror/throwable.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010035#include "oat_quick_method_header.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010036#include "stack.h"
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010037#include "stack_map.h"
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010038
39namespace art {
40
Ian Rogers5cf98192014-05-29 21:31:50 -070041static constexpr bool kDebugExceptionDelivery = false;
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -070042static constexpr size_t kInvalidFrameDepth = 0xffffffff;
Ian Rogers5cf98192014-05-29 21:31:50 -070043
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020044QuickExceptionHandler::QuickExceptionHandler(Thread* self, bool is_deoptimization)
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010045 : self_(self),
46 context_(self->GetLongJumpContext()),
47 is_deoptimization_(is_deoptimization),
48 method_tracing_active_(is_deoptimization ||
49 Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()),
50 handler_quick_frame_(nullptr),
51 handler_quick_frame_pc_(0),
52 handler_method_header_(nullptr),
53 handler_quick_arg0_(0),
54 handler_method_(nullptr),
55 handler_dex_pc_(0),
56 clear_exception_(false),
Mingyao Yangf711f2c2016-05-23 12:29:39 -070057 handler_frame_depth_(kInvalidFrameDepth),
58 full_fragment_done_(false) {}
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010059
Sebastien Hertz520633b2015-09-08 17:03:36 +020060// Finds catch handler.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010061class CatchBlockStackVisitor final : public StackVisitor {
Ian Rogers5cf98192014-05-29 21:31:50 -070062 public:
Alex Light2c8206f2018-06-08 14:51:09 -070063 CatchBlockStackVisitor(Thread* self,
64 Context* context,
65 Handle<mirror::Throwable>* exception,
66 QuickExceptionHandler* exception_handler,
67 uint32_t skip_frames)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070068 REQUIRES_SHARED(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010069 : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010070 exception_(exception),
Alex Light2c8206f2018-06-08 14:51:09 -070071 exception_handler_(exception_handler),
72 skip_frames_(skip_frames) {
Ian Rogers5cf98192014-05-29 21:31:50 -070073 }
74
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010075 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070076 ArtMethod* method = GetMethod();
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -070077 exception_handler_->SetHandlerFrameDepth(GetFrameDepth());
Ian Rogers5cf98192014-05-29 21:31:50 -070078 if (method == nullptr) {
Alex Light2c8206f2018-06-08 14:51:09 -070079 DCHECK_EQ(skip_frames_, 0u)
80 << "We tried to skip an upcall! We should have returned to the upcall to finish delivery";
Ian Rogers5cf98192014-05-29 21:31:50 -070081 // This is the upcall, we remember the frame and last pc so that we may long jump to them.
82 exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc());
83 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010084 exception_handler_->SetHandlerMethodHeader(GetCurrentOatQuickMethodHeader());
Ian Rogers5cf98192014-05-29 21:31:50 -070085 uint32_t next_dex_pc;
Mathieu Chartiere401d142015-04-22 13:56:20 -070086 ArtMethod* next_art_method;
Ian Rogers5cf98192014-05-29 21:31:50 -070087 bool has_next = GetNextMethodAndDexPc(&next_art_method, &next_dex_pc);
88 // Report the method that did the down call as the handler.
89 exception_handler_->SetHandlerDexPc(next_dex_pc);
90 exception_handler_->SetHandlerMethod(next_art_method);
91 if (!has_next) {
92 // No next method? Check exception handler is set up for the unhandled exception handler
93 // case.
94 DCHECK_EQ(0U, exception_handler_->GetHandlerDexPc());
95 DCHECK(nullptr == exception_handler_->GetHandlerMethod());
96 }
97 return false; // End stack walk.
98 }
Alex Light2c8206f2018-06-08 14:51:09 -070099 if (skip_frames_ != 0) {
100 skip_frames_--;
101 return true;
102 }
Ian Rogers5cf98192014-05-29 21:31:50 -0700103 if (method->IsRuntimeMethod()) {
104 // Ignore callee save method.
105 DCHECK(method->IsCalleeSaveMethod());
106 return true;
107 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700108 return HandleTryItems(method);
Ian Rogers5cf98192014-05-29 21:31:50 -0700109 }
110
111 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700112 bool HandleTryItems(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700113 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampee2abbc62017-09-15 11:59:26 -0700114 uint32_t dex_pc = dex::kDexNoIndex;
Ian Rogers5cf98192014-05-29 21:31:50 -0700115 if (!method->IsNative()) {
116 dex_pc = GetDexPc();
117 }
Andreas Gampee2abbc62017-09-15 11:59:26 -0700118 if (dex_pc != dex::kDexNoIndex) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700119 bool clear_exception = false;
Sebastien Hertz26f72862015-09-15 09:52:07 +0200120 StackHandleScope<1> hs(GetThread());
Ian Rogers5cf98192014-05-29 21:31:50 -0700121 Handle<mirror::Class> to_find(hs.NewHandle((*exception_)->GetClass()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700122 uint32_t found_dex_pc = method->FindCatchBlock(to_find, dex_pc, &clear_exception);
Ian Rogers5cf98192014-05-29 21:31:50 -0700123 exception_handler_->SetClearException(clear_exception);
Andreas Gampee2abbc62017-09-15 11:59:26 -0700124 if (found_dex_pc != dex::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700125 exception_handler_->SetHandlerMethod(method);
Ian Rogers5cf98192014-05-29 21:31:50 -0700126 exception_handler_->SetHandlerDexPc(found_dex_pc);
David Brazdil72f7b882015-09-15 17:00:52 +0100127 exception_handler_->SetHandlerQuickFramePc(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100128 GetCurrentOatQuickMethodHeader()->ToNativeQuickPc(
129 method, found_dex_pc, /* is_catch_handler */ true));
Ian Rogers5cf98192014-05-29 21:31:50 -0700130 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100131 exception_handler_->SetHandlerMethodHeader(GetCurrentOatQuickMethodHeader());
Ian Rogers5cf98192014-05-29 21:31:50 -0700132 return false; // End stack walk.
Mingyao Yang99170c62015-07-06 11:10:37 -0700133 } else if (UNLIKELY(GetThread()->HasDebuggerShadowFrames())) {
134 // We are going to unwind this frame. Did we prepare a shadow frame for debugging?
135 size_t frame_id = GetFrameId();
136 ShadowFrame* frame = GetThread()->FindDebuggerShadowFrame(frame_id);
137 if (frame != nullptr) {
138 // We will not execute this shadow frame so we can safely deallocate it.
139 GetThread()->RemoveDebuggerShadowFrameMapping(frame_id);
140 ShadowFrame::DeleteDeoptimizedFrame(frame);
141 }
Ian Rogers5cf98192014-05-29 21:31:50 -0700142 }
143 }
144 return true; // Continue stack walk.
145 }
146
Ian Rogers5cf98192014-05-29 21:31:50 -0700147 // The exception we're looking for the catch block of.
148 Handle<mirror::Throwable>* exception_;
149 // The quick exception handler we're visiting for.
150 QuickExceptionHandler* const exception_handler_;
Alex Light2c8206f2018-06-08 14:51:09 -0700151 // The number of frames to skip searching for catches in.
152 uint32_t skip_frames_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700153
154 DISALLOW_COPY_AND_ASSIGN(CatchBlockStackVisitor);
155};
156
Alex Light2c8206f2018-06-08 14:51:09 -0700157// Counts instrumentation stack frame prior to catch handler or upcall.
158class InstrumentationStackVisitor : public StackVisitor {
159 public:
160 InstrumentationStackVisitor(Thread* self, size_t frame_depth)
161 REQUIRES_SHARED(Locks::mutator_lock_)
162 : StackVisitor(self, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
163 frame_depth_(frame_depth),
164 instrumentation_frames_to_pop_(0) {
165 CHECK_NE(frame_depth_, kInvalidFrameDepth);
166 }
167
Andreas Gampefa6a1b02018-09-07 08:11:55 -0700168 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Light2c8206f2018-06-08 14:51:09 -0700169 size_t current_frame_depth = GetFrameDepth();
170 if (current_frame_depth < frame_depth_) {
171 CHECK(GetMethod() != nullptr);
172 if (UNLIKELY(reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == GetReturnPc())) {
173 if (!IsInInlinedFrame()) {
174 // We do not count inlined frames, because we do not instrument them. The reason we
175 // include them in the stack walking is the check against `frame_depth_`, which is
176 // given to us by a visitor that visits inlined frames.
177 ++instrumentation_frames_to_pop_;
178 }
179 }
180 return true;
181 } else {
182 // We reached the frame of the catch handler or the upcall.
183 return false;
184 }
185 }
186
187 size_t GetInstrumentationFramesToPop() const {
188 return instrumentation_frames_to_pop_;
189 }
190
191 private:
192 const size_t frame_depth_;
193 size_t instrumentation_frames_to_pop_;
194
195 DISALLOW_COPY_AND_ASSIGN(InstrumentationStackVisitor);
196};
197
198// Finds the appropriate exception catch after calling all method exit instrumentation functions.
199// Note that this might change the exception being thrown.
Mathieu Chartierf5769e12017-01-10 15:54:41 -0800200void QuickExceptionHandler::FindCatch(ObjPtr<mirror::Throwable> exception) {
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200201 DCHECK(!is_deoptimization_);
Alex Light2c8206f2018-06-08 14:51:09 -0700202 instrumentation::InstrumentationStackPopper popper(self_);
203 // The number of total frames we have so far popped.
204 uint32_t already_popped = 0;
205 bool popped_to_top = true;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700206 StackHandleScope<1> hs(self_);
Alex Light2c8206f2018-06-08 14:51:09 -0700207 MutableHandle<mirror::Throwable> exception_ref(hs.NewHandle(exception));
208 // Sending the instrumentation events (done by the InstrumentationStackPopper) can cause new
209 // exceptions to be thrown which will override the current exception. Therefore we need to perform
210 // the search for a catch in a loop until we have successfully popped all the way to a catch or
211 // the top of the stack.
212 do {
213 if (kDebugExceptionDelivery) {
214 ObjPtr<mirror::String> msg = exception_ref->GetDetailMessage();
215 std::string str_msg(msg != nullptr ? msg->ToModifiedUtf8() : "");
216 self_->DumpStack(LOG_STREAM(INFO) << "Delivering exception: " << exception_ref->PrettyTypeOf()
217 << ": " << str_msg << "\n");
Ian Rogers5cf98192014-05-29 21:31:50 -0700218 }
Alex Light2c8206f2018-06-08 14:51:09 -0700219
220 // Walk the stack to find catch handler.
221 CatchBlockStackVisitor visitor(self_, context_, &exception_ref, this, /*skip*/already_popped);
222 visitor.WalkStack(true);
223 uint32_t new_pop_count = handler_frame_depth_;
224 DCHECK_GE(new_pop_count, already_popped);
225 already_popped = new_pop_count;
226
227 // Figure out how many of those frames have instrumentation we need to remove (Should be the
228 // exact same as number of new_pop_count if there aren't inlined frames).
229 InstrumentationStackVisitor instrumentation_visitor(self_, handler_frame_depth_);
230 instrumentation_visitor.WalkStack(true);
231 size_t instrumentation_frames_to_pop = instrumentation_visitor.GetInstrumentationFramesToPop();
232
233 if (kDebugExceptionDelivery) {
234 if (*handler_quick_frame_ == nullptr) {
235 LOG(INFO) << "Handler is upcall";
236 }
237 if (handler_method_ != nullptr) {
238 const DexFile* dex_file = handler_method_->GetDeclaringClass()->GetDexCache()->GetDexFile();
239 int line_number = annotations::GetLineNumFromPC(dex_file, handler_method_, handler_dex_pc_);
240 LOG(INFO) << "Handler: " << handler_method_->PrettyMethod() << " (line: "
241 << line_number << ")";
242 }
243 LOG(INFO) << "Will attempt to pop " << instrumentation_frames_to_pop
244 << " off of the instrumentation stack";
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100245 }
Alex Light2c8206f2018-06-08 14:51:09 -0700246 // Exception was cleared as part of delivery.
247 DCHECK(!self_->IsExceptionPending());
248 // If the handler is in optimized code, we need to set the catch environment.
249 if (*handler_quick_frame_ != nullptr &&
250 handler_method_header_ != nullptr &&
251 handler_method_header_->IsOptimized()) {
252 SetCatchEnvironmentForOptimizedHandler(&visitor);
253 }
254 popped_to_top = popper.PopFramesTo(instrumentation_frames_to_pop, exception_ref);
255 } while (!popped_to_top);
Roland Levillainb77b6982017-06-08 18:03:48 +0100256 if (!clear_exception_) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100257 // Put exception back in root set with clear throw location.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000258 self_->SetException(exception_ref.Get());
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100259 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000260}
261
262static VRegKind ToVRegKind(DexRegisterLocation::Kind kind) {
263 // Slightly hacky since we cannot map DexRegisterLocationKind and VRegKind
264 // one to one. However, StackVisitor::GetVRegFromOptimizedCode only needs to
265 // distinguish between core/FPU registers and low/high bits on 64-bit.
266 switch (kind) {
267 case DexRegisterLocation::Kind::kConstant:
268 case DexRegisterLocation::Kind::kInStack:
269 // VRegKind is ignored.
270 return VRegKind::kUndefined;
271
272 case DexRegisterLocation::Kind::kInRegister:
273 // Selects core register. For 64-bit registers, selects low 32 bits.
274 return VRegKind::kLongLoVReg;
275
276 case DexRegisterLocation::Kind::kInRegisterHigh:
277 // Selects core register. For 64-bit registers, selects high 32 bits.
278 return VRegKind::kLongHiVReg;
279
280 case DexRegisterLocation::Kind::kInFpuRegister:
281 // Selects FPU register. For 64-bit registers, selects low 32 bits.
282 return VRegKind::kDoubleLoVReg;
283
284 case DexRegisterLocation::Kind::kInFpuRegisterHigh:
285 // Selects FPU register. For 64-bit registers, selects high 32 bits.
286 return VRegKind::kDoubleHiVReg;
287
288 default:
David Srbecky7dc11782016-02-25 13:23:56 +0000289 LOG(FATAL) << "Unexpected vreg location " << kind;
David Brazdil77a48ae2015-09-15 12:34:04 +0000290 UNREACHABLE();
291 }
292}
293
294void QuickExceptionHandler::SetCatchEnvironmentForOptimizedHandler(StackVisitor* stack_visitor) {
295 DCHECK(!is_deoptimization_);
296 DCHECK(*handler_quick_frame_ != nullptr) << "Method should not be called on upcall exceptions";
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100297 DCHECK(handler_method_ != nullptr && handler_method_header_->IsOptimized());
David Brazdil77a48ae2015-09-15 12:34:04 +0000298
299 if (kDebugExceptionDelivery) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700300 self_->DumpStack(LOG_STREAM(INFO) << "Setting catch phis: ");
David Brazdil77a48ae2015-09-15 12:34:04 +0000301 }
302
David Sehr0225f8e2018-01-31 08:52:24 +0000303 CodeItemDataAccessor accessor(handler_method_->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800304 const size_t number_of_vregs = accessor.RegistersSize();
David Srbecky052f8ca2018-04-26 15:42:54 +0100305 CodeInfo code_info(handler_method_header_);
David Brazdil77a48ae2015-09-15 12:34:04 +0000306
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000307 // Find stack map of the catch block.
David Srbecky052f8ca2018-04-26 15:42:54 +0100308 StackMap catch_stack_map = code_info.GetCatchStackMapForDexPc(GetHandlerDexPc());
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000309 DCHECK(catch_stack_map.IsValid());
David Srbeckyfd89b072018-06-03 12:00:22 +0100310 DexRegisterMap catch_vreg_map = code_info.GetDexRegisterMapOf(catch_stack_map);
311 if (!catch_vreg_map.HasAnyLiveDexRegisters()) {
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000312 return;
313 }
David Srbeckyfd89b072018-06-03 12:00:22 +0100314 DCHECK_EQ(catch_vreg_map.size(), number_of_vregs);
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000315
David Brazdil77a48ae2015-09-15 12:34:04 +0000316 // Find stack map of the throwing instruction.
317 StackMap throw_stack_map =
David Srbecky052f8ca2018-04-26 15:42:54 +0100318 code_info.GetStackMapForNativePcOffset(stack_visitor->GetNativePcOffset());
David Brazdil77a48ae2015-09-15 12:34:04 +0000319 DCHECK(throw_stack_map.IsValid());
David Srbeckyfd89b072018-06-03 12:00:22 +0100320 DexRegisterMap throw_vreg_map = code_info.GetDexRegisterMapOf(throw_stack_map);
321 DCHECK_EQ(throw_vreg_map.size(), number_of_vregs);
David Brazdil77a48ae2015-09-15 12:34:04 +0000322
323 // Copy values between them.
324 for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) {
David Srbeckye1402122018-06-13 18:20:45 +0100325 DexRegisterLocation::Kind catch_location = catch_vreg_map[vreg].GetKind();
David Brazdil77a48ae2015-09-15 12:34:04 +0000326 if (catch_location == DexRegisterLocation::Kind::kNone) {
327 continue;
328 }
329 DCHECK(catch_location == DexRegisterLocation::Kind::kInStack);
330
331 // Get vreg value from its current location.
332 uint32_t vreg_value;
David Srbeckye1402122018-06-13 18:20:45 +0100333 VRegKind vreg_kind = ToVRegKind(throw_vreg_map[vreg].GetKind());
David Brazdil77a48ae2015-09-15 12:34:04 +0000334 bool get_vreg_success = stack_visitor->GetVReg(stack_visitor->GetMethod(),
335 vreg,
336 vreg_kind,
337 &vreg_value);
338 CHECK(get_vreg_success) << "VReg " << vreg << " was optimized out ("
David Sehr709b0702016-10-13 09:12:37 -0700339 << "method=" << ArtMethod::PrettyMethod(stack_visitor->GetMethod())
340 << ", dex_pc=" << stack_visitor->GetDexPc() << ", "
David Brazdil77a48ae2015-09-15 12:34:04 +0000341 << "native_pc_offset=" << stack_visitor->GetNativePcOffset() << ")";
342
343 // Copy value to the catch phi's stack slot.
David Srbeckye1402122018-06-13 18:20:45 +0100344 int32_t slot_offset = catch_vreg_map[vreg].GetStackOffsetInBytes();
David Brazdil77a48ae2015-09-15 12:34:04 +0000345 ArtMethod** frame_top = stack_visitor->GetCurrentQuickFrame();
346 uint8_t* slot_address = reinterpret_cast<uint8_t*>(frame_top) + slot_offset;
347 uint32_t* slot_ptr = reinterpret_cast<uint32_t*>(slot_address);
348 *slot_ptr = vreg_value;
349 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200350}
351
Ian Rogers5cf98192014-05-29 21:31:50 -0700352// Prepares deoptimization.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100353class DeoptimizeStackVisitor final : public StackVisitor {
Ian Rogers5cf98192014-05-29 21:31:50 -0700354 public:
Andreas Gampe639bdd12015-06-03 11:22:45 -0700355 DeoptimizeStackVisitor(Thread* self,
356 Context* context,
357 QuickExceptionHandler* exception_handler,
358 bool single_frame)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700359 REQUIRES_SHARED(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100360 : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100361 exception_handler_(exception_handler),
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700362 prev_shadow_frame_(nullptr),
Andreas Gampe639bdd12015-06-03 11:22:45 -0700363 stacked_shadow_frame_pushed_(false),
364 single_frame_deopt_(single_frame),
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100365 single_frame_done_(false),
Nicolas Geoffrayb52de242016-02-19 12:43:12 +0000366 single_frame_deopt_method_(nullptr),
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700367 single_frame_deopt_quick_method_header_(nullptr),
368 callee_method_(nullptr) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100369 }
370
371 ArtMethod* GetSingleFrameDeoptMethod() const {
372 return single_frame_deopt_method_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700373 }
374
Nicolas Geoffrayb52de242016-02-19 12:43:12 +0000375 const OatQuickMethodHeader* GetSingleFrameDeoptQuickMethodHeader() const {
376 return single_frame_deopt_quick_method_header_;
377 }
378
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700379 void FinishStackWalk() REQUIRES_SHARED(Locks::mutator_lock_) {
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700380 // This is the upcall, or the next full frame in single-frame deopt, or the
381 // code isn't deoptimizeable. We remember the frame and last pc so that we
382 // may long jump to them.
383 exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc());
384 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
385 exception_handler_->SetHandlerMethodHeader(GetCurrentOatQuickMethodHeader());
386 if (!stacked_shadow_frame_pushed_) {
387 // In case there is no deoptimized shadow frame for this upcall, we still
388 // need to push a nullptr to the stack since there is always a matching pop after
389 // the long jump.
390 GetThread()->PushStackedShadowFrame(nullptr,
391 StackedShadowFrameType::kDeoptimizationShadowFrame);
392 stacked_shadow_frame_pushed_ = true;
393 }
394 if (GetMethod() == nullptr) {
395 exception_handler_->SetFullFragmentDone(true);
396 } else {
David Sehr709b0702016-10-13 09:12:37 -0700397 CHECK(callee_method_ != nullptr) << GetMethod()->PrettyMethod(false);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700398 exception_handler_->SetHandlerQuickArg0(reinterpret_cast<uintptr_t>(callee_method_));
399 }
400 }
401
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100402 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700403 exception_handler_->SetHandlerFrameDepth(GetFrameDepth());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700404 ArtMethod* method = GetMethod();
Alex Light0aa7a5a2018-10-10 15:58:14 +0000405 VLOG(deopt) << "Deoptimizing stack: depth: " << GetFrameDepth()
406 << " at method " << ArtMethod::PrettyMethod(method);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700407 if (method == nullptr || single_frame_done_) {
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700408 FinishStackWalk();
Ian Rogers5cf98192014-05-29 21:31:50 -0700409 return false; // End stack walk.
410 } else if (method->IsRuntimeMethod()) {
411 // Ignore callee save method.
412 DCHECK(method->IsCalleeSaveMethod());
413 return true;
Sebastien Hertz520633b2015-09-08 17:03:36 +0200414 } else if (method->IsNative()) {
415 // If we return from JNI with a pending exception and want to deoptimize, we need to skip
416 // the native method.
417 // The top method is a runtime method, the native method comes next.
418 CHECK_EQ(GetFrameDepth(), 1U);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700419 callee_method_ = method;
Sebastien Hertz520633b2015-09-08 17:03:36 +0200420 return true;
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700421 } else if (!single_frame_deopt_ &&
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000422 !Runtime::Current()->IsAsyncDeoptimizeable(GetCurrentQuickFramePc())) {
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700423 // We hit some code that's not deoptimizeable. However, Single-frame deoptimization triggered
424 // from compiled code is always allowed since HDeoptimize always saves the full environment.
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000425 LOG(WARNING) << "Got request to deoptimize un-deoptimizable method "
426 << method->PrettyMethod();
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700427 FinishStackWalk();
428 return false; // End stack walk.
Ian Rogers5cf98192014-05-29 21:31:50 -0700429 } else {
Nicolas Geoffray33856502015-10-20 15:52:58 +0100430 // Check if a shadow frame already exists for debugger's set-local-value purpose.
431 const size_t frame_id = GetFrameId();
432 ShadowFrame* new_frame = GetThread()->FindDebuggerShadowFrame(frame_id);
433 const bool* updated_vregs;
David Sehr0225f8e2018-01-31 08:52:24 +0000434 CodeItemDataAccessor accessor(method->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800435 const size_t num_regs = accessor.RegistersSize();
Nicolas Geoffray33856502015-10-20 15:52:58 +0100436 if (new_frame == nullptr) {
437 new_frame = ShadowFrame::CreateDeoptimizedFrame(num_regs, nullptr, method, GetDexPc());
438 updated_vregs = nullptr;
439 } else {
440 updated_vregs = GetThread()->GetUpdatedVRegFlags(frame_id);
441 DCHECK(updated_vregs != nullptr);
442 }
Andreas Gampebf9611f2016-03-25 16:58:00 -0700443 HandleOptimizingDeoptimization(method, new_frame, updated_vregs);
Nicolas Geoffray33856502015-10-20 15:52:58 +0100444 if (updated_vregs != nullptr) {
445 // Calling Thread::RemoveDebuggerShadowFrameMapping will also delete the updated_vregs
446 // array so this must come after we processed the frame.
447 GetThread()->RemoveDebuggerShadowFrameMapping(frame_id);
448 DCHECK(GetThread()->FindDebuggerShadowFrame(frame_id) == nullptr);
449 }
450 if (prev_shadow_frame_ != nullptr) {
451 prev_shadow_frame_->SetLink(new_frame);
452 } else {
453 // Will be popped after the long jump after DeoptimizeStack(),
454 // right before interpreter::EnterInterpreterFromDeoptimize().
455 stacked_shadow_frame_pushed_ = true;
456 GetThread()->PushStackedShadowFrame(
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700457 new_frame, StackedShadowFrameType::kDeoptimizationShadowFrame);
Nicolas Geoffray33856502015-10-20 15:52:58 +0100458 }
459 prev_shadow_frame_ = new_frame;
460
Andreas Gampe639bdd12015-06-03 11:22:45 -0700461 if (single_frame_deopt_ && !IsInInlinedFrame()) {
462 // Single-frame deopt ends at the first non-inlined frame and needs to store that method.
Andreas Gampe639bdd12015-06-03 11:22:45 -0700463 single_frame_done_ = true;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100464 single_frame_deopt_method_ = method;
Nicolas Geoffrayb52de242016-02-19 12:43:12 +0000465 single_frame_deopt_quick_method_header_ = GetCurrentOatQuickMethodHeader();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700466 }
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700467 callee_method_ = method;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700468 return true;
Ian Rogers5cf98192014-05-29 21:31:50 -0700469 }
470 }
471
472 private:
Nicolas Geoffray33856502015-10-20 15:52:58 +0100473 void HandleOptimizingDeoptimization(ArtMethod* m,
474 ShadowFrame* new_frame,
475 const bool* updated_vregs)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700476 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray33856502015-10-20 15:52:58 +0100477 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
David Srbecky052f8ca2018-04-26 15:42:54 +0100478 CodeInfo code_info(method_header);
Nicolas Geoffray33856502015-10-20 15:52:58 +0100479 uintptr_t native_pc_offset = method_header->NativeQuickPcOffset(GetCurrentQuickFramePc());
David Srbecky052f8ca2018-04-26 15:42:54 +0100480 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
David Sehr0225f8e2018-01-31 08:52:24 +0000481 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800482 const size_t number_of_vregs = accessor.RegistersSize();
David Srbecky052f8ca2018-04-26 15:42:54 +0100483 uint32_t register_mask = code_info.GetRegisterMaskOf(stack_map);
484 BitMemoryRegion stack_mask = code_info.GetStackMaskOf(stack_map);
David Brazdilefc3f022015-10-28 12:19:06 -0500485 DexRegisterMap vreg_map = IsInInlinedFrame()
David Srbecky93bd3612018-07-02 19:30:18 +0100486 ? code_info.GetInlineDexRegisterMapOf(stack_map, GetCurrentInlinedFrame())
David Srbeckyfd89b072018-06-03 12:00:22 +0100487 : code_info.GetDexRegisterMapOf(stack_map);
488 if (vreg_map.empty()) {
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000489 return;
490 }
David Srbeckyfd89b072018-06-03 12:00:22 +0100491 DCHECK_EQ(vreg_map.size(), number_of_vregs);
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000492
Nicolas Geoffray33856502015-10-20 15:52:58 +0100493 for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) {
494 if (updated_vregs != nullptr && updated_vregs[vreg]) {
495 // Keep the value set by debugger.
496 continue;
497 }
498
David Srbeckye1402122018-06-13 18:20:45 +0100499 DexRegisterLocation::Kind location = vreg_map[vreg].GetKind();
Nicolas Geoffray33856502015-10-20 15:52:58 +0100500 static constexpr uint32_t kDeadValue = 0xEBADDE09;
501 uint32_t value = kDeadValue;
502 bool is_reference = false;
503
504 switch (location) {
505 case DexRegisterLocation::Kind::kInStack: {
David Srbeckye1402122018-06-13 18:20:45 +0100506 const int32_t offset = vreg_map[vreg].GetStackOffsetInBytes();
Nicolas Geoffray33856502015-10-20 15:52:58 +0100507 const uint8_t* addr = reinterpret_cast<const uint8_t*>(GetCurrentQuickFrame()) + offset;
508 value = *reinterpret_cast<const uint32_t*>(addr);
509 uint32_t bit = (offset >> 2);
David Srbecky4b59d102018-05-29 21:46:10 +0000510 if (bit < stack_mask.size_in_bits() && stack_mask.LoadBit(bit)) {
Nicolas Geoffray33856502015-10-20 15:52:58 +0100511 is_reference = true;
512 }
513 break;
514 }
515 case DexRegisterLocation::Kind::kInRegister:
516 case DexRegisterLocation::Kind::kInRegisterHigh:
517 case DexRegisterLocation::Kind::kInFpuRegister:
518 case DexRegisterLocation::Kind::kInFpuRegisterHigh: {
David Srbeckye1402122018-06-13 18:20:45 +0100519 uint32_t reg = vreg_map[vreg].GetMachineRegister();
Nicolas Geoffray33856502015-10-20 15:52:58 +0100520 bool result = GetRegisterIfAccessible(reg, ToVRegKind(location), &value);
521 CHECK(result);
522 if (location == DexRegisterLocation::Kind::kInRegister) {
523 if (((1u << reg) & register_mask) != 0) {
524 is_reference = true;
525 }
526 }
527 break;
528 }
529 case DexRegisterLocation::Kind::kConstant: {
David Srbeckye1402122018-06-13 18:20:45 +0100530 value = vreg_map[vreg].GetConstant();
Nicolas Geoffray33856502015-10-20 15:52:58 +0100531 if (value == 0) {
532 // Make it a reference for extra safety.
533 is_reference = true;
534 }
535 break;
536 }
537 case DexRegisterLocation::Kind::kNone: {
538 break;
539 }
540 default: {
David Srbeckye1402122018-06-13 18:20:45 +0100541 LOG(FATAL) << "Unexpected location kind " << vreg_map[vreg].GetKind();
Nicolas Geoffray33856502015-10-20 15:52:58 +0100542 UNREACHABLE();
543 }
544 }
545 if (is_reference) {
546 new_frame->SetVRegReference(vreg, reinterpret_cast<mirror::Object*>(value));
547 } else {
548 new_frame->SetVReg(vreg, value);
549 }
550 }
551 }
552
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200553 static VRegKind GetVRegKind(uint16_t reg, const std::vector<int32_t>& kinds) {
Vladimir Marko35d5b8a2018-07-03 09:18:32 +0100554 return static_cast<VRegKind>(kinds[reg * 2]);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200555 }
556
Ian Rogers5cf98192014-05-29 21:31:50 -0700557 QuickExceptionHandler* const exception_handler_;
558 ShadowFrame* prev_shadow_frame_;
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700559 bool stacked_shadow_frame_pushed_;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700560 const bool single_frame_deopt_;
561 bool single_frame_done_;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100562 ArtMethod* single_frame_deopt_method_;
Nicolas Geoffrayb52de242016-02-19 12:43:12 +0000563 const OatQuickMethodHeader* single_frame_deopt_quick_method_header_;
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700564 ArtMethod* callee_method_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700565
566 DISALLOW_COPY_AND_ASSIGN(DeoptimizeStackVisitor);
567};
568
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700569void QuickExceptionHandler::PrepareForLongJumpToInvokeStubOrInterpreterBridge() {
570 if (full_fragment_done_) {
571 // Restore deoptimization exception. When returning from the invoke stub,
572 // ArtMethod::Invoke() will see the special exception to know deoptimization
573 // is needed.
574 self_->SetException(Thread::GetDeoptimizationException());
575 } else {
576 // PC needs to be of the quick-to-interpreter bridge.
577 int32_t offset;
Andreas Gampe542451c2016-07-26 09:02:02 -0700578 offset = GetThreadOffset<kRuntimePointerSize>(kQuickQuickToInterpreterBridge).Int32Value();
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700579 handler_quick_frame_pc_ = *reinterpret_cast<uintptr_t*>(
580 reinterpret_cast<uint8_t*>(self_) + offset);
581 }
582}
583
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200584void QuickExceptionHandler::DeoptimizeStack() {
585 DCHECK(is_deoptimization_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700586 if (kDebugExceptionDelivery) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700587 self_->DumpStack(LOG_STREAM(INFO) << "Deoptimizing: ");
Ian Rogers5cf98192014-05-29 21:31:50 -0700588 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200589
Andreas Gampe639bdd12015-06-03 11:22:45 -0700590 DeoptimizeStackVisitor visitor(self_, context_, this, false);
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200591 visitor.WalkStack(true);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700592 PrepareForLongJumpToInvokeStubOrInterpreterBridge();
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100593}
594
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100595void QuickExceptionHandler::DeoptimizeSingleFrame(DeoptimizationKind kind) {
Andreas Gampe639bdd12015-06-03 11:22:45 -0700596 DCHECK(is_deoptimization_);
597
Andreas Gampe639bdd12015-06-03 11:22:45 -0700598 DeoptimizeStackVisitor visitor(self_, context_, this, true);
599 visitor.WalkStack(true);
600
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000601 // Compiled code made an explicit deoptimization.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100602 ArtMethod* deopt_method = visitor.GetSingleFrameDeoptMethod();
603 DCHECK(deopt_method != nullptr);
Nicolas Geoffray646d6382017-08-09 10:50:00 +0100604 if (VLOG_IS_ON(deopt) || kDebugExceptionDelivery) {
605 LOG(INFO) << "Single-frame deopting: "
606 << deopt_method->PrettyMethod()
607 << " due to "
608 << GetDeoptimizationKindName(kind);
609 DumpFramesWithType(self_, /* details */ true);
610 }
Calin Juravleffc87072016-04-20 14:22:09 +0100611 if (Runtime::Current()->UseJitCompilation()) {
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000612 Runtime::Current()->GetJit()->GetCodeCache()->InvalidateCompiledCodeFor(
Nicolas Geoffrayb52de242016-02-19 12:43:12 +0000613 deopt_method, visitor.GetSingleFrameDeoptQuickMethodHeader());
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000614 } else {
615 // Transfer the code to interpreter.
616 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
617 deopt_method, GetQuickToInterpreterBridge());
618 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100619
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700620 PrepareForLongJumpToInvokeStubOrInterpreterBridge();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700621}
622
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700623void QuickExceptionHandler::DeoptimizePartialFragmentFixup(uintptr_t return_pc) {
624 // At this point, the instrumentation stack has been updated. We need to install
625 // the real return pc on stack, in case instrumentation stub is stored there,
626 // so that the interpreter bridge code can return to the right place.
627 if (return_pc != 0) {
628 uintptr_t* pc_addr = reinterpret_cast<uintptr_t*>(handler_quick_frame_);
629 CHECK(pc_addr != nullptr);
630 pc_addr--;
631 *reinterpret_cast<uintptr_t*>(pc_addr) = return_pc;
632 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700633
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700634 // Architecture-dependent work. This is to get the LR right for x86 and x86-64.
Andreas Gampe639bdd12015-06-03 11:22:45 -0700635 if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) {
636 // On x86, the return address is on the stack, so just reuse it. Otherwise we would have to
637 // change how longjump works.
638 handler_quick_frame_ = reinterpret_cast<ArtMethod**>(
639 reinterpret_cast<uintptr_t>(handler_quick_frame_) - sizeof(void*));
640 }
641}
642
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700643uintptr_t QuickExceptionHandler::UpdateInstrumentationStack() {
Alex Light2c8206f2018-06-08 14:51:09 -0700644 DCHECK(is_deoptimization_) << "Non-deoptimization handlers should use FindCatch";
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700645 uintptr_t return_pc = 0;
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100646 if (method_tracing_active_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700647 InstrumentationStackVisitor visitor(self_, handler_frame_depth_);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100648 visitor.WalkStack(true);
649
650 size_t instrumentation_frames_to_pop = visitor.GetInstrumentationFramesToPop();
651 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
Alex Light2c8206f2018-06-08 14:51:09 -0700652 return_pc = instrumentation->PopFramesForDeoptimization(self_, instrumentation_frames_to_pop);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100653 }
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700654 return return_pc;
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100655}
656
Andreas Gampe639bdd12015-06-03 11:22:45 -0700657void QuickExceptionHandler::DoLongJump(bool smash_caller_saves) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100658 // Place context back on thread so it will be available when we continue.
659 self_->ReleaseLongJumpContext(context_);
660 context_->SetSP(reinterpret_cast<uintptr_t>(handler_quick_frame_));
661 CHECK_NE(handler_quick_frame_pc_, 0u);
662 context_->SetPC(handler_quick_frame_pc_);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700663 context_->SetArg0(handler_quick_arg0_);
664 if (smash_caller_saves) {
665 context_->SmashCallerSaves();
666 }
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100667 context_->DoLongJump();
Andreas Gampe794ad762015-02-23 08:12:24 -0800668 UNREACHABLE();
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100669}
670
Andreas Gampe639bdd12015-06-03 11:22:45 -0700671// Prints out methods with their type of frame.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100672class DumpFramesWithTypeStackVisitor final : public StackVisitor {
Andreas Gampe639bdd12015-06-03 11:22:45 -0700673 public:
Chih-Hung Hsieh471118e2016-04-29 14:27:41 -0700674 explicit DumpFramesWithTypeStackVisitor(Thread* self, bool show_details = false)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700675 REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe639bdd12015-06-03 11:22:45 -0700676 : StackVisitor(self, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
677 show_details_(show_details) {}
678
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100679 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe639bdd12015-06-03 11:22:45 -0700680 ArtMethod* method = GetMethod();
681 if (show_details_) {
682 LOG(INFO) << "|> pc = " << std::hex << GetCurrentQuickFramePc();
683 LOG(INFO) << "|> addr = " << std::hex << reinterpret_cast<uintptr_t>(GetCurrentQuickFrame());
684 if (GetCurrentQuickFrame() != nullptr && method != nullptr) {
685 LOG(INFO) << "|> ret = " << std::hex << GetReturnPc();
686 }
687 }
688 if (method == nullptr) {
689 // Transition, do go on, we want to unwind over bridges, all the way.
690 if (show_details_) {
691 LOG(INFO) << "N <transition>";
692 }
693 return true;
694 } else if (method->IsRuntimeMethod()) {
695 if (show_details_) {
David Sehr709b0702016-10-13 09:12:37 -0700696 LOG(INFO) << "R " << method->PrettyMethod(true);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700697 }
698 return true;
699 } else {
700 bool is_shadow = GetCurrentShadowFrame() != nullptr;
701 LOG(INFO) << (is_shadow ? "S" : "Q")
702 << ((!is_shadow && IsInInlinedFrame()) ? "i" : " ")
703 << " "
David Sehr709b0702016-10-13 09:12:37 -0700704 << method->PrettyMethod(true);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700705 return true; // Go on.
706 }
707 }
708
709 private:
710 bool show_details_;
711
712 DISALLOW_COPY_AND_ASSIGN(DumpFramesWithTypeStackVisitor);
713};
714
715void QuickExceptionHandler::DumpFramesWithType(Thread* self, bool details) {
716 DumpFramesWithTypeStackVisitor visitor(self, details);
717 visitor.WalkStack(true);
718}
719
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100720} // namespace art