Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 1 | /* |
| 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 Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 17 | #include "quick_exception_handler.h" |
| 18 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 19 | #include "arch/context.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 20 | #include "art_method-inl.h" |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 21 | #include "dex_instruction.h" |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 22 | #include "entrypoints/entrypoint_utils.h" |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 23 | #include "entrypoints/quick/quick_entrypoints_enum.h" |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 24 | #include "entrypoints/runtime_asm_entrypoints.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 25 | #include "handle_scope-inl.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 26 | #include "mirror/class-inl.h" |
| 27 | #include "mirror/class_loader.h" |
| 28 | #include "mirror/throwable.h" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 29 | #include "oat_quick_method_header.h" |
Nicolas Geoffray | 6bc4374 | 2015-10-12 18:11:10 +0100 | [diff] [blame] | 30 | #include "stack_map.h" |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 31 | #include "verifier/method_verifier.h" |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 32 | |
| 33 | namespace art { |
| 34 | |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 35 | static constexpr bool kDebugExceptionDelivery = false; |
Hiroshi Yamauchi | 649278c | 2014-08-13 11:12:22 -0700 | [diff] [blame] | 36 | static constexpr size_t kInvalidFrameDepth = 0xffffffff; |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 37 | |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 38 | QuickExceptionHandler::QuickExceptionHandler(Thread* self, bool is_deoptimization) |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 39 | : self_(self), |
| 40 | context_(self->GetLongJumpContext()), |
| 41 | is_deoptimization_(is_deoptimization), |
| 42 | method_tracing_active_(is_deoptimization || |
| 43 | Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()), |
| 44 | handler_quick_frame_(nullptr), |
| 45 | handler_quick_frame_pc_(0), |
| 46 | handler_method_header_(nullptr), |
| 47 | handler_quick_arg0_(0), |
| 48 | handler_method_(nullptr), |
| 49 | handler_dex_pc_(0), |
| 50 | clear_exception_(false), |
| 51 | handler_frame_depth_(kInvalidFrameDepth) {} |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 52 | |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 53 | // Finds catch handler. |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 54 | class CatchBlockStackVisitor FINAL : public StackVisitor { |
| 55 | public: |
| 56 | CatchBlockStackVisitor(Thread* self, Context* context, Handle<mirror::Throwable>* exception, |
| 57 | QuickExceptionHandler* exception_handler) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 58 | SHARED_REQUIRES(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 59 | : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 60 | exception_(exception), |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 61 | exception_handler_(exception_handler) { |
| 62 | } |
| 63 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 64 | bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 65 | ArtMethod* method = GetMethod(); |
Hiroshi Yamauchi | 649278c | 2014-08-13 11:12:22 -0700 | [diff] [blame] | 66 | exception_handler_->SetHandlerFrameDepth(GetFrameDepth()); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 67 | if (method == nullptr) { |
| 68 | // This is the upcall, we remember the frame and last pc so that we may long jump to them. |
| 69 | exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc()); |
| 70 | exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame()); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 71 | exception_handler_->SetHandlerMethodHeader(GetCurrentOatQuickMethodHeader()); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 72 | uint32_t next_dex_pc; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 73 | ArtMethod* next_art_method; |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 74 | bool has_next = GetNextMethodAndDexPc(&next_art_method, &next_dex_pc); |
| 75 | // Report the method that did the down call as the handler. |
| 76 | exception_handler_->SetHandlerDexPc(next_dex_pc); |
| 77 | exception_handler_->SetHandlerMethod(next_art_method); |
| 78 | if (!has_next) { |
| 79 | // No next method? Check exception handler is set up for the unhandled exception handler |
| 80 | // case. |
| 81 | DCHECK_EQ(0U, exception_handler_->GetHandlerDexPc()); |
| 82 | DCHECK(nullptr == exception_handler_->GetHandlerMethod()); |
| 83 | } |
| 84 | return false; // End stack walk. |
| 85 | } |
| 86 | if (method->IsRuntimeMethod()) { |
| 87 | // Ignore callee save method. |
| 88 | DCHECK(method->IsCalleeSaveMethod()); |
| 89 | return true; |
| 90 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 91 | return HandleTryItems(method); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | private: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 95 | bool HandleTryItems(ArtMethod* method) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 96 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 97 | uint32_t dex_pc = DexFile::kDexNoIndex; |
| 98 | if (!method->IsNative()) { |
| 99 | dex_pc = GetDexPc(); |
| 100 | } |
| 101 | if (dex_pc != DexFile::kDexNoIndex) { |
| 102 | bool clear_exception = false; |
Sebastien Hertz | 26f7286 | 2015-09-15 09:52:07 +0200 | [diff] [blame] | 103 | StackHandleScope<1> hs(GetThread()); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 104 | Handle<mirror::Class> to_find(hs.NewHandle((*exception_)->GetClass())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 105 | uint32_t found_dex_pc = method->FindCatchBlock(to_find, dex_pc, &clear_exception); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 106 | exception_handler_->SetClearException(clear_exception); |
| 107 | if (found_dex_pc != DexFile::kDexNoIndex) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 108 | exception_handler_->SetHandlerMethod(method); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 109 | exception_handler_->SetHandlerDexPc(found_dex_pc); |
David Brazdil | 72f7b88 | 2015-09-15 17:00:52 +0100 | [diff] [blame] | 110 | exception_handler_->SetHandlerQuickFramePc( |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 111 | GetCurrentOatQuickMethodHeader()->ToNativeQuickPc( |
| 112 | method, found_dex_pc, /* is_catch_handler */ true)); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 113 | exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame()); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 114 | exception_handler_->SetHandlerMethodHeader(GetCurrentOatQuickMethodHeader()); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 115 | return false; // End stack walk. |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 116 | } else if (UNLIKELY(GetThread()->HasDebuggerShadowFrames())) { |
| 117 | // We are going to unwind this frame. Did we prepare a shadow frame for debugging? |
| 118 | size_t frame_id = GetFrameId(); |
| 119 | ShadowFrame* frame = GetThread()->FindDebuggerShadowFrame(frame_id); |
| 120 | if (frame != nullptr) { |
| 121 | // We will not execute this shadow frame so we can safely deallocate it. |
| 122 | GetThread()->RemoveDebuggerShadowFrameMapping(frame_id); |
| 123 | ShadowFrame::DeleteDeoptimizedFrame(frame); |
| 124 | } |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | return true; // Continue stack walk. |
| 128 | } |
| 129 | |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 130 | // The exception we're looking for the catch block of. |
| 131 | Handle<mirror::Throwable>* exception_; |
| 132 | // The quick exception handler we're visiting for. |
| 133 | QuickExceptionHandler* const exception_handler_; |
| 134 | |
| 135 | DISALLOW_COPY_AND_ASSIGN(CatchBlockStackVisitor); |
| 136 | }; |
| 137 | |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 138 | void QuickExceptionHandler::FindCatch(mirror::Throwable* exception) { |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 139 | DCHECK(!is_deoptimization_); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 140 | if (kDebugExceptionDelivery) { |
| 141 | mirror::String* msg = exception->GetDetailMessage(); |
| 142 | std::string str_msg(msg != nullptr ? msg->ToModifiedUtf8() : ""); |
| 143 | self_->DumpStack(LOG(INFO) << "Delivering exception: " << PrettyTypeOf(exception) |
| 144 | << ": " << str_msg << "\n"); |
| 145 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 146 | StackHandleScope<1> hs(self_); |
| 147 | Handle<mirror::Throwable> exception_ref(hs.NewHandle(exception)); |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 148 | |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 149 | // Walk the stack to find catch handler. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 150 | CatchBlockStackVisitor visitor(self_, context_, &exception_ref, this); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 151 | visitor.WalkStack(true); |
| 152 | |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 153 | if (kDebugExceptionDelivery) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 154 | if (*handler_quick_frame_ == nullptr) { |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 155 | LOG(INFO) << "Handler is upcall"; |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 156 | } |
| 157 | if (handler_method_ != nullptr) { |
| 158 | const DexFile& dex_file = *handler_method_->GetDeclaringClass()->GetDexCache()->GetDexFile(); |
| 159 | int line_number = dex_file.GetLineNumFromPC(handler_method_, handler_dex_pc_); |
| 160 | LOG(INFO) << "Handler: " << PrettyMethod(handler_method_) << " (line: " << line_number << ")"; |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | if (clear_exception_) { |
| 164 | // Exception was cleared as part of delivery. |
| 165 | DCHECK(!self_->IsExceptionPending()); |
| 166 | } else { |
| 167 | // Put exception back in root set with clear throw location. |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 168 | self_->SetException(exception_ref.Get()); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 169 | } |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 170 | // If the handler is in optimized code, we need to set the catch environment. |
| 171 | if (*handler_quick_frame_ != nullptr && |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 172 | handler_method_header_ != nullptr && |
| 173 | handler_method_header_->IsOptimized()) { |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 174 | SetCatchEnvironmentForOptimizedHandler(&visitor); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | static VRegKind ToVRegKind(DexRegisterLocation::Kind kind) { |
| 179 | // Slightly hacky since we cannot map DexRegisterLocationKind and VRegKind |
| 180 | // one to one. However, StackVisitor::GetVRegFromOptimizedCode only needs to |
| 181 | // distinguish between core/FPU registers and low/high bits on 64-bit. |
| 182 | switch (kind) { |
| 183 | case DexRegisterLocation::Kind::kConstant: |
| 184 | case DexRegisterLocation::Kind::kInStack: |
| 185 | // VRegKind is ignored. |
| 186 | return VRegKind::kUndefined; |
| 187 | |
| 188 | case DexRegisterLocation::Kind::kInRegister: |
| 189 | // Selects core register. For 64-bit registers, selects low 32 bits. |
| 190 | return VRegKind::kLongLoVReg; |
| 191 | |
| 192 | case DexRegisterLocation::Kind::kInRegisterHigh: |
| 193 | // Selects core register. For 64-bit registers, selects high 32 bits. |
| 194 | return VRegKind::kLongHiVReg; |
| 195 | |
| 196 | case DexRegisterLocation::Kind::kInFpuRegister: |
| 197 | // Selects FPU register. For 64-bit registers, selects low 32 bits. |
| 198 | return VRegKind::kDoubleLoVReg; |
| 199 | |
| 200 | case DexRegisterLocation::Kind::kInFpuRegisterHigh: |
| 201 | // Selects FPU register. For 64-bit registers, selects high 32 bits. |
| 202 | return VRegKind::kDoubleHiVReg; |
| 203 | |
| 204 | default: |
| 205 | LOG(FATAL) << "Unexpected vreg location " |
| 206 | << DexRegisterLocation::PrettyDescriptor(kind); |
| 207 | UNREACHABLE(); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | void QuickExceptionHandler::SetCatchEnvironmentForOptimizedHandler(StackVisitor* stack_visitor) { |
| 212 | DCHECK(!is_deoptimization_); |
| 213 | DCHECK(*handler_quick_frame_ != nullptr) << "Method should not be called on upcall exceptions"; |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 214 | DCHECK(handler_method_ != nullptr && handler_method_header_->IsOptimized()); |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 215 | |
| 216 | if (kDebugExceptionDelivery) { |
| 217 | self_->DumpStack(LOG(INFO) << "Setting catch phis: "); |
| 218 | } |
| 219 | |
| 220 | const size_t number_of_vregs = handler_method_->GetCodeItem()->registers_size_; |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 221 | CodeInfo code_info = handler_method_header_->GetOptimizedCodeInfo(); |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 222 | StackMapEncoding encoding = code_info.ExtractEncoding(); |
| 223 | |
| 224 | // Find stack map of the throwing instruction. |
| 225 | StackMap throw_stack_map = |
| 226 | code_info.GetStackMapForNativePcOffset(stack_visitor->GetNativePcOffset(), encoding); |
| 227 | DCHECK(throw_stack_map.IsValid()); |
| 228 | DexRegisterMap throw_vreg_map = |
| 229 | code_info.GetDexRegisterMapOf(throw_stack_map, encoding, number_of_vregs); |
| 230 | |
| 231 | // Find stack map of the catch block. |
| 232 | StackMap catch_stack_map = code_info.GetCatchStackMapForDexPc(GetHandlerDexPc(), encoding); |
| 233 | DCHECK(catch_stack_map.IsValid()); |
| 234 | DexRegisterMap catch_vreg_map = |
| 235 | code_info.GetDexRegisterMapOf(catch_stack_map, encoding, number_of_vregs); |
| 236 | |
| 237 | // Copy values between them. |
| 238 | for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) { |
| 239 | DexRegisterLocation::Kind catch_location = |
| 240 | catch_vreg_map.GetLocationKind(vreg, number_of_vregs, code_info, encoding); |
| 241 | if (catch_location == DexRegisterLocation::Kind::kNone) { |
| 242 | continue; |
| 243 | } |
| 244 | DCHECK(catch_location == DexRegisterLocation::Kind::kInStack); |
| 245 | |
| 246 | // Get vreg value from its current location. |
| 247 | uint32_t vreg_value; |
| 248 | VRegKind vreg_kind = ToVRegKind(throw_vreg_map.GetLocationKind(vreg, |
| 249 | number_of_vregs, |
| 250 | code_info, |
| 251 | encoding)); |
| 252 | bool get_vreg_success = stack_visitor->GetVReg(stack_visitor->GetMethod(), |
| 253 | vreg, |
| 254 | vreg_kind, |
| 255 | &vreg_value); |
| 256 | CHECK(get_vreg_success) << "VReg " << vreg << " was optimized out (" |
| 257 | << "method=" << PrettyMethod(stack_visitor->GetMethod()) << ", " |
| 258 | << "dex_pc=" << stack_visitor->GetDexPc() << ", " |
| 259 | << "native_pc_offset=" << stack_visitor->GetNativePcOffset() << ")"; |
| 260 | |
| 261 | // Copy value to the catch phi's stack slot. |
| 262 | int32_t slot_offset = catch_vreg_map.GetStackOffsetInBytes(vreg, |
| 263 | number_of_vregs, |
| 264 | code_info, |
| 265 | encoding); |
| 266 | ArtMethod** frame_top = stack_visitor->GetCurrentQuickFrame(); |
| 267 | uint8_t* slot_address = reinterpret_cast<uint8_t*>(frame_top) + slot_offset; |
| 268 | uint32_t* slot_ptr = reinterpret_cast<uint32_t*>(slot_address); |
| 269 | *slot_ptr = vreg_value; |
| 270 | } |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 271 | } |
| 272 | |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 273 | // Prepares deoptimization. |
| 274 | class DeoptimizeStackVisitor FINAL : public StackVisitor { |
| 275 | public: |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 276 | DeoptimizeStackVisitor(Thread* self, |
| 277 | Context* context, |
| 278 | QuickExceptionHandler* exception_handler, |
| 279 | bool single_frame) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 280 | SHARED_REQUIRES(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 281 | : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 282 | exception_handler_(exception_handler), |
Mingyao Yang | 1f2d3ba | 2015-05-18 12:12:50 -0700 | [diff] [blame] | 283 | prev_shadow_frame_(nullptr), |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 284 | stacked_shadow_frame_pushed_(false), |
| 285 | single_frame_deopt_(single_frame), |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame^] | 286 | single_frame_done_(false), |
| 287 | single_frame_deopt_method_(nullptr) { |
| 288 | } |
| 289 | |
| 290 | ArtMethod* GetSingleFrameDeoptMethod() const { |
| 291 | return single_frame_deopt_method_; |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 294 | bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 649278c | 2014-08-13 11:12:22 -0700 | [diff] [blame] | 295 | exception_handler_->SetHandlerFrameDepth(GetFrameDepth()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 296 | ArtMethod* method = GetMethod(); |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 297 | if (method == nullptr || single_frame_done_) { |
| 298 | // This is the upcall (or the next full frame in single-frame deopt), we remember the frame |
| 299 | // and last pc so that we may long jump to them. |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 300 | exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc()); |
| 301 | exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame()); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 302 | exception_handler_->SetHandlerMethodHeader(GetCurrentOatQuickMethodHeader()); |
Mingyao Yang | 1f2d3ba | 2015-05-18 12:12:50 -0700 | [diff] [blame] | 303 | if (!stacked_shadow_frame_pushed_) { |
| 304 | // In case there is no deoptimized shadow frame for this upcall, we still |
| 305 | // need to push a nullptr to the stack since there is always a matching pop after |
| 306 | // the long jump. |
Sebastien Hertz | 26f7286 | 2015-09-15 09:52:07 +0200 | [diff] [blame] | 307 | GetThread()->PushStackedShadowFrame(nullptr, |
| 308 | StackedShadowFrameType::kDeoptimizationShadowFrame); |
Mingyao Yang | 1f2d3ba | 2015-05-18 12:12:50 -0700 | [diff] [blame] | 309 | stacked_shadow_frame_pushed_ = true; |
| 310 | } |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 311 | return false; // End stack walk. |
| 312 | } else if (method->IsRuntimeMethod()) { |
| 313 | // Ignore callee save method. |
| 314 | DCHECK(method->IsCalleeSaveMethod()); |
| 315 | return true; |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 316 | } else if (method->IsNative()) { |
| 317 | // If we return from JNI with a pending exception and want to deoptimize, we need to skip |
| 318 | // the native method. |
| 319 | // The top method is a runtime method, the native method comes next. |
| 320 | CHECK_EQ(GetFrameDepth(), 1U); |
| 321 | return true; |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 322 | } else { |
Nicolas Geoffray | 3385650 | 2015-10-20 15:52:58 +0100 | [diff] [blame] | 323 | // Check if a shadow frame already exists for debugger's set-local-value purpose. |
| 324 | const size_t frame_id = GetFrameId(); |
| 325 | ShadowFrame* new_frame = GetThread()->FindDebuggerShadowFrame(frame_id); |
| 326 | const bool* updated_vregs; |
| 327 | const size_t num_regs = method->GetCodeItem()->registers_size_; |
| 328 | if (new_frame == nullptr) { |
| 329 | new_frame = ShadowFrame::CreateDeoptimizedFrame(num_regs, nullptr, method, GetDexPc()); |
| 330 | updated_vregs = nullptr; |
| 331 | } else { |
| 332 | updated_vregs = GetThread()->GetUpdatedVRegFlags(frame_id); |
| 333 | DCHECK(updated_vregs != nullptr); |
| 334 | } |
| 335 | if (GetCurrentOatQuickMethodHeader()->IsOptimized()) { |
| 336 | HandleOptimizingDeoptimization(method, new_frame, updated_vregs); |
| 337 | } else { |
| 338 | HandleQuickDeoptimization(method, new_frame, updated_vregs); |
| 339 | } |
| 340 | if (updated_vregs != nullptr) { |
| 341 | // Calling Thread::RemoveDebuggerShadowFrameMapping will also delete the updated_vregs |
| 342 | // array so this must come after we processed the frame. |
| 343 | GetThread()->RemoveDebuggerShadowFrameMapping(frame_id); |
| 344 | DCHECK(GetThread()->FindDebuggerShadowFrame(frame_id) == nullptr); |
| 345 | } |
| 346 | if (prev_shadow_frame_ != nullptr) { |
| 347 | prev_shadow_frame_->SetLink(new_frame); |
| 348 | } else { |
| 349 | // Will be popped after the long jump after DeoptimizeStack(), |
| 350 | // right before interpreter::EnterInterpreterFromDeoptimize(). |
| 351 | stacked_shadow_frame_pushed_ = true; |
| 352 | GetThread()->PushStackedShadowFrame( |
| 353 | new_frame, |
| 354 | single_frame_deopt_ |
| 355 | ? StackedShadowFrameType::kSingleFrameDeoptimizationShadowFrame |
| 356 | : StackedShadowFrameType::kDeoptimizationShadowFrame); |
| 357 | } |
| 358 | prev_shadow_frame_ = new_frame; |
| 359 | |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 360 | if (single_frame_deopt_ && !IsInInlinedFrame()) { |
| 361 | // Single-frame deopt ends at the first non-inlined frame and needs to store that method. |
| 362 | exception_handler_->SetHandlerQuickArg0(reinterpret_cast<uintptr_t>(method)); |
| 363 | single_frame_done_ = true; |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame^] | 364 | single_frame_deopt_method_ = method; |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 365 | } |
| 366 | return true; |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
| 370 | private: |
Nicolas Geoffray | 3385650 | 2015-10-20 15:52:58 +0100 | [diff] [blame] | 371 | void HandleOptimizingDeoptimization(ArtMethod* m, |
| 372 | ShadowFrame* new_frame, |
| 373 | const bool* updated_vregs) |
| 374 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 375 | const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader(); |
| 376 | CodeInfo code_info = method_header->GetOptimizedCodeInfo(); |
| 377 | uintptr_t native_pc_offset = method_header->NativeQuickPcOffset(GetCurrentQuickFramePc()); |
| 378 | StackMapEncoding encoding = code_info.ExtractEncoding(); |
| 379 | StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding); |
| 380 | const size_t number_of_vregs = m->GetCodeItem()->registers_size_; |
Nicolas Geoffray | 3385650 | 2015-10-20 15:52:58 +0100 | [diff] [blame] | 381 | MemoryRegion stack_mask = stack_map.GetStackMask(encoding); |
| 382 | uint32_t register_mask = stack_map.GetRegisterMask(encoding); |
David Brazdil | efc3f02 | 2015-10-28 12:19:06 -0500 | [diff] [blame] | 383 | DexRegisterMap vreg_map = IsInInlinedFrame() |
| 384 | ? code_info.GetDexRegisterMapAtDepth(GetCurrentInliningDepth() - 1, |
| 385 | code_info.GetInlineInfoOf(stack_map, encoding), |
| 386 | encoding, |
| 387 | number_of_vregs) |
| 388 | : code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_vregs); |
Nicolas Geoffray | 3385650 | 2015-10-20 15:52:58 +0100 | [diff] [blame] | 389 | |
| 390 | for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) { |
| 391 | if (updated_vregs != nullptr && updated_vregs[vreg]) { |
| 392 | // Keep the value set by debugger. |
| 393 | continue; |
| 394 | } |
| 395 | |
| 396 | DexRegisterLocation::Kind location = |
| 397 | vreg_map.GetLocationKind(vreg, number_of_vregs, code_info, encoding); |
| 398 | static constexpr uint32_t kDeadValue = 0xEBADDE09; |
| 399 | uint32_t value = kDeadValue; |
| 400 | bool is_reference = false; |
| 401 | |
| 402 | switch (location) { |
| 403 | case DexRegisterLocation::Kind::kInStack: { |
| 404 | const int32_t offset = vreg_map.GetStackOffsetInBytes(vreg, |
| 405 | number_of_vregs, |
| 406 | code_info, |
| 407 | encoding); |
| 408 | const uint8_t* addr = reinterpret_cast<const uint8_t*>(GetCurrentQuickFrame()) + offset; |
| 409 | value = *reinterpret_cast<const uint32_t*>(addr); |
| 410 | uint32_t bit = (offset >> 2); |
| 411 | if (stack_mask.size_in_bits() > bit && stack_mask.LoadBit(bit)) { |
| 412 | is_reference = true; |
| 413 | } |
| 414 | break; |
| 415 | } |
| 416 | case DexRegisterLocation::Kind::kInRegister: |
| 417 | case DexRegisterLocation::Kind::kInRegisterHigh: |
| 418 | case DexRegisterLocation::Kind::kInFpuRegister: |
| 419 | case DexRegisterLocation::Kind::kInFpuRegisterHigh: { |
| 420 | uint32_t reg = vreg_map.GetMachineRegister(vreg, number_of_vregs, code_info, encoding); |
| 421 | bool result = GetRegisterIfAccessible(reg, ToVRegKind(location), &value); |
| 422 | CHECK(result); |
| 423 | if (location == DexRegisterLocation::Kind::kInRegister) { |
| 424 | if (((1u << reg) & register_mask) != 0) { |
| 425 | is_reference = true; |
| 426 | } |
| 427 | } |
| 428 | break; |
| 429 | } |
| 430 | case DexRegisterLocation::Kind::kConstant: { |
| 431 | value = vreg_map.GetConstant(vreg, number_of_vregs, code_info, encoding); |
| 432 | if (value == 0) { |
| 433 | // Make it a reference for extra safety. |
| 434 | is_reference = true; |
| 435 | } |
| 436 | break; |
| 437 | } |
| 438 | case DexRegisterLocation::Kind::kNone: { |
| 439 | break; |
| 440 | } |
| 441 | default: { |
| 442 | LOG(FATAL) |
| 443 | << "Unexpected location kind" |
| 444 | << DexRegisterLocation::PrettyDescriptor( |
| 445 | vreg_map.GetLocationInternalKind(vreg, |
| 446 | number_of_vregs, |
| 447 | code_info, |
| 448 | encoding)); |
| 449 | UNREACHABLE(); |
| 450 | } |
| 451 | } |
| 452 | if (is_reference) { |
| 453 | new_frame->SetVRegReference(vreg, reinterpret_cast<mirror::Object*>(value)); |
| 454 | } else { |
| 455 | new_frame->SetVReg(vreg, value); |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 460 | static VRegKind GetVRegKind(uint16_t reg, const std::vector<int32_t>& kinds) { |
| 461 | return static_cast<VRegKind>(kinds.at(reg * 2)); |
| 462 | } |
| 463 | |
Nicolas Geoffray | 3385650 | 2015-10-20 15:52:58 +0100 | [diff] [blame] | 464 | void HandleQuickDeoptimization(ArtMethod* m, |
| 465 | ShadowFrame* new_frame, |
| 466 | const bool* updated_vregs) |
| 467 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 468 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 469 | CHECK(code_item != nullptr) << "No code item for " << PrettyMethod(m); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 470 | uint16_t num_regs = code_item->registers_size_; |
| 471 | uint32_t dex_pc = GetDexPc(); |
Nicolas Geoffray | 3385650 | 2015-10-20 15:52:58 +0100 | [diff] [blame] | 472 | StackHandleScope<2> hs(GetThread()); // Dex cache and class loader. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 473 | mirror::Class* declaring_class = m->GetDeclaringClass(); |
| 474 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(declaring_class->GetDexCache())); |
| 475 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(declaring_class->GetClassLoader())); |
Sebastien Hertz | 26f7286 | 2015-09-15 09:52:07 +0200 | [diff] [blame] | 476 | verifier::MethodVerifier verifier(GetThread(), h_dex_cache->GetDexFile(), h_dex_cache, |
| 477 | h_class_loader, &m->GetClassDef(), code_item, |
| 478 | m->GetDexMethodIndex(), m, m->GetAccessFlags(), true, true, |
| 479 | true, true); |
Andreas Gampe | 2e04bb2 | 2015-02-10 15:37:27 -0800 | [diff] [blame] | 480 | bool verifier_success = verifier.Verify(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 481 | CHECK(verifier_success) << PrettyMethod(m); |
Mingyao Yang | 1f2d3ba | 2015-05-18 12:12:50 -0700 | [diff] [blame] | 482 | { |
Sebastien Hertz | 26f7286 | 2015-09-15 09:52:07 +0200 | [diff] [blame] | 483 | ScopedStackedShadowFramePusher pusher(GetThread(), new_frame, |
Sebastien Hertz | f795869 | 2015-06-09 14:09:14 +0200 | [diff] [blame] | 484 | StackedShadowFrameType::kShadowFrameUnderConstruction); |
Mingyao Yang | 1f2d3ba | 2015-05-18 12:12:50 -0700 | [diff] [blame] | 485 | const std::vector<int32_t> kinds(verifier.DescribeVRegs(dex_pc)); |
Nicolas Geoffray | 15b9d52 | 2015-03-12 15:05:13 +0000 | [diff] [blame] | 486 | |
Mingyao Yang | 1f2d3ba | 2015-05-18 12:12:50 -0700 | [diff] [blame] | 487 | // Markers for dead values, used when the verifier knows a Dex register is undefined, |
| 488 | // or when the compiler knows the register has not been initialized, or is not used |
| 489 | // anymore in the method. |
| 490 | static constexpr uint32_t kDeadValue = 0xEBADDE09; |
| 491 | static constexpr uint64_t kLongDeadValue = 0xEBADDE09EBADDE09; |
| 492 | for (uint16_t reg = 0; reg < num_regs; ++reg) { |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 493 | if (updated_vregs != nullptr && updated_vregs[reg]) { |
| 494 | // Keep the value set by debugger. |
| 495 | continue; |
| 496 | } |
Mingyao Yang | 1f2d3ba | 2015-05-18 12:12:50 -0700 | [diff] [blame] | 497 | VRegKind kind = GetVRegKind(reg, kinds); |
| 498 | switch (kind) { |
| 499 | case kUndefined: |
Nicolas Geoffray | 15b9d52 | 2015-03-12 15:05:13 +0000 | [diff] [blame] | 500 | new_frame->SetVReg(reg, kDeadValue); |
Mingyao Yang | 1f2d3ba | 2015-05-18 12:12:50 -0700 | [diff] [blame] | 501 | break; |
| 502 | case kConstant: |
| 503 | new_frame->SetVReg(reg, kinds.at((reg * 2) + 1)); |
| 504 | break; |
| 505 | case kReferenceVReg: { |
| 506 | uint32_t value = 0; |
| 507 | // Check IsReferenceVReg in case the compiled GC map doesn't agree with the verifier. |
| 508 | // We don't want to copy a stale reference into the shadow frame as a reference. |
| 509 | // b/20736048 |
| 510 | if (GetVReg(m, reg, kind, &value) && IsReferenceVReg(m, reg)) { |
| 511 | new_frame->SetVRegReference(reg, reinterpret_cast<mirror::Object*>(value)); |
| 512 | } else { |
| 513 | new_frame->SetVReg(reg, kDeadValue); |
| 514 | } |
| 515 | break; |
Nicolas Geoffray | 15b9d52 | 2015-03-12 15:05:13 +0000 | [diff] [blame] | 516 | } |
Mingyao Yang | 1f2d3ba | 2015-05-18 12:12:50 -0700 | [diff] [blame] | 517 | case kLongLoVReg: |
| 518 | if (GetVRegKind(reg + 1, kinds) == kLongHiVReg) { |
| 519 | // Treat it as a "long" register pair. |
| 520 | uint64_t value = 0; |
| 521 | if (GetVRegPair(m, reg, kLongLoVReg, kLongHiVReg, &value)) { |
| 522 | new_frame->SetVRegLong(reg, value); |
| 523 | } else { |
| 524 | new_frame->SetVRegLong(reg, kLongDeadValue); |
| 525 | } |
| 526 | } else { |
| 527 | uint32_t value = 0; |
| 528 | if (GetVReg(m, reg, kind, &value)) { |
| 529 | new_frame->SetVReg(reg, value); |
| 530 | } else { |
| 531 | new_frame->SetVReg(reg, kDeadValue); |
| 532 | } |
| 533 | } |
| 534 | break; |
| 535 | case kLongHiVReg: |
| 536 | if (GetVRegKind(reg - 1, kinds) == kLongLoVReg) { |
| 537 | // Nothing to do: we treated it as a "long" register pair. |
| 538 | } else { |
| 539 | uint32_t value = 0; |
| 540 | if (GetVReg(m, reg, kind, &value)) { |
| 541 | new_frame->SetVReg(reg, value); |
| 542 | } else { |
| 543 | new_frame->SetVReg(reg, kDeadValue); |
| 544 | } |
| 545 | } |
| 546 | break; |
| 547 | case kDoubleLoVReg: |
| 548 | if (GetVRegKind(reg + 1, kinds) == kDoubleHiVReg) { |
| 549 | uint64_t value = 0; |
| 550 | if (GetVRegPair(m, reg, kDoubleLoVReg, kDoubleHiVReg, &value)) { |
| 551 | // Treat it as a "double" register pair. |
| 552 | new_frame->SetVRegLong(reg, value); |
| 553 | } else { |
| 554 | new_frame->SetVRegLong(reg, kLongDeadValue); |
| 555 | } |
| 556 | } else { |
| 557 | uint32_t value = 0; |
| 558 | if (GetVReg(m, reg, kind, &value)) { |
| 559 | new_frame->SetVReg(reg, value); |
| 560 | } else { |
| 561 | new_frame->SetVReg(reg, kDeadValue); |
| 562 | } |
| 563 | } |
| 564 | break; |
| 565 | case kDoubleHiVReg: |
| 566 | if (GetVRegKind(reg - 1, kinds) == kDoubleLoVReg) { |
| 567 | // Nothing to do: we treated it as a "double" register pair. |
| 568 | } else { |
| 569 | uint32_t value = 0; |
| 570 | if (GetVReg(m, reg, kind, &value)) { |
| 571 | new_frame->SetVReg(reg, value); |
| 572 | } else { |
| 573 | new_frame->SetVReg(reg, kDeadValue); |
| 574 | } |
| 575 | } |
| 576 | break; |
| 577 | default: |
| 578 | uint32_t value = 0; |
| 579 | if (GetVReg(m, reg, kind, &value)) { |
| 580 | new_frame->SetVReg(reg, value); |
| 581 | } else { |
| 582 | new_frame->SetVReg(reg, kDeadValue); |
| 583 | } |
| 584 | break; |
Nicolas Geoffray | 15b9d52 | 2015-03-12 15:05:13 +0000 | [diff] [blame] | 585 | } |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 586 | } |
| 587 | } |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 590 | QuickExceptionHandler* const exception_handler_; |
| 591 | ShadowFrame* prev_shadow_frame_; |
Mingyao Yang | 1f2d3ba | 2015-05-18 12:12:50 -0700 | [diff] [blame] | 592 | bool stacked_shadow_frame_pushed_; |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 593 | const bool single_frame_deopt_; |
| 594 | bool single_frame_done_; |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame^] | 595 | ArtMethod* single_frame_deopt_method_; |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 596 | |
| 597 | DISALLOW_COPY_AND_ASSIGN(DeoptimizeStackVisitor); |
| 598 | }; |
| 599 | |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 600 | void QuickExceptionHandler::DeoptimizeStack() { |
| 601 | DCHECK(is_deoptimization_); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 602 | if (kDebugExceptionDelivery) { |
| 603 | self_->DumpStack(LOG(INFO) << "Deoptimizing: "); |
| 604 | } |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 605 | |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 606 | DeoptimizeStackVisitor visitor(self_, context_, this, false); |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 607 | visitor.WalkStack(true); |
| 608 | |
| 609 | // Restore deoptimization exception |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 610 | self_->SetException(Thread::GetDeoptimizationException()); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 611 | } |
| 612 | |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 613 | void QuickExceptionHandler::DeoptimizeSingleFrame() { |
| 614 | DCHECK(is_deoptimization_); |
| 615 | |
| 616 | if (VLOG_IS_ON(deopt) || kDebugExceptionDelivery) { |
| 617 | LOG(INFO) << "Single-frame deopting:"; |
| 618 | DumpFramesWithType(self_, true); |
| 619 | } |
| 620 | |
| 621 | DeoptimizeStackVisitor visitor(self_, context_, this, true); |
| 622 | visitor.WalkStack(true); |
| 623 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame^] | 624 | // Compiled code made an explicit deoptimization. Transfer the code |
| 625 | // to interpreter and clear the counter to JIT the method again. |
| 626 | ArtMethod* deopt_method = visitor.GetSingleFrameDeoptMethod(); |
| 627 | DCHECK(deopt_method != nullptr); |
| 628 | deopt_method->ClearCounter(); |
| 629 | Runtime::Current()->GetInstrumentation()->UpdateMethodsCode( |
| 630 | deopt_method, GetQuickToInterpreterBridge()); |
| 631 | |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 632 | // PC needs to be of the quick-to-interpreter bridge. |
| 633 | int32_t offset; |
| 634 | #ifdef __LP64__ |
| 635 | offset = GetThreadOffset<8>(kQuickQuickToInterpreterBridge).Int32Value(); |
| 636 | #else |
| 637 | offset = GetThreadOffset<4>(kQuickQuickToInterpreterBridge).Int32Value(); |
| 638 | #endif |
| 639 | handler_quick_frame_pc_ = *reinterpret_cast<uintptr_t*>( |
| 640 | reinterpret_cast<uint8_t*>(self_) + offset); |
| 641 | } |
| 642 | |
| 643 | void QuickExceptionHandler::DeoptimizeSingleFrameArchDependentFixup() { |
| 644 | // Architecture-dependent work. This is to get the LR right for x86 and x86-64. |
| 645 | |
| 646 | if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) { |
| 647 | // On x86, the return address is on the stack, so just reuse it. Otherwise we would have to |
| 648 | // change how longjump works. |
| 649 | handler_quick_frame_ = reinterpret_cast<ArtMethod**>( |
| 650 | reinterpret_cast<uintptr_t>(handler_quick_frame_) - sizeof(void*)); |
| 651 | } |
| 652 | } |
| 653 | |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 654 | // Unwinds all instrumentation stack frame prior to catch handler or upcall. |
| 655 | class InstrumentationStackVisitor : public StackVisitor { |
| 656 | public: |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 657 | InstrumentationStackVisitor(Thread* self, size_t frame_depth) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 658 | SHARED_REQUIRES(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 659 | : StackVisitor(self, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 660 | frame_depth_(frame_depth), |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 661 | instrumentation_frames_to_pop_(0) { |
Hiroshi Yamauchi | 649278c | 2014-08-13 11:12:22 -0700 | [diff] [blame] | 662 | CHECK_NE(frame_depth_, kInvalidFrameDepth); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 663 | } |
| 664 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 665 | bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 649278c | 2014-08-13 11:12:22 -0700 | [diff] [blame] | 666 | size_t current_frame_depth = GetFrameDepth(); |
| 667 | if (current_frame_depth < frame_depth_) { |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 668 | CHECK(GetMethod() != nullptr); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 669 | if (UNLIKELY(reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == GetReturnPc())) { |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 670 | if (!IsInInlinedFrame()) { |
| 671 | // We do not count inlined frames, because we do not instrument them. The reason we |
| 672 | // include them in the stack walking is the check against `frame_depth_`, which is |
| 673 | // given to us by a visitor that visits inlined frames. |
| 674 | ++instrumentation_frames_to_pop_; |
| 675 | } |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 676 | } |
| 677 | return true; |
| 678 | } else { |
| 679 | // We reached the frame of the catch handler or the upcall. |
| 680 | return false; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | size_t GetInstrumentationFramesToPop() const { |
| 685 | return instrumentation_frames_to_pop_; |
| 686 | } |
| 687 | |
| 688 | private: |
Hiroshi Yamauchi | 649278c | 2014-08-13 11:12:22 -0700 | [diff] [blame] | 689 | const size_t frame_depth_; |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 690 | size_t instrumentation_frames_to_pop_; |
| 691 | |
| 692 | DISALLOW_COPY_AND_ASSIGN(InstrumentationStackVisitor); |
| 693 | }; |
| 694 | |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 695 | void QuickExceptionHandler::UpdateInstrumentationStack() { |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 696 | if (method_tracing_active_) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 697 | InstrumentationStackVisitor visitor(self_, handler_frame_depth_); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 698 | visitor.WalkStack(true); |
| 699 | |
| 700 | size_t instrumentation_frames_to_pop = visitor.GetInstrumentationFramesToPop(); |
| 701 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 702 | for (size_t i = 0; i < instrumentation_frames_to_pop; ++i) { |
| 703 | instrumentation->PopMethodForUnwind(self_, is_deoptimization_); |
| 704 | } |
| 705 | } |
| 706 | } |
| 707 | |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 708 | void QuickExceptionHandler::DoLongJump(bool smash_caller_saves) { |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 709 | // Place context back on thread so it will be available when we continue. |
| 710 | self_->ReleaseLongJumpContext(context_); |
| 711 | context_->SetSP(reinterpret_cast<uintptr_t>(handler_quick_frame_)); |
| 712 | CHECK_NE(handler_quick_frame_pc_, 0u); |
| 713 | context_->SetPC(handler_quick_frame_pc_); |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 714 | context_->SetArg0(handler_quick_arg0_); |
| 715 | if (smash_caller_saves) { |
| 716 | context_->SmashCallerSaves(); |
| 717 | } |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 718 | context_->DoLongJump(); |
Andreas Gampe | 794ad76 | 2015-02-23 08:12:24 -0800 | [diff] [blame] | 719 | UNREACHABLE(); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 720 | } |
| 721 | |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 722 | // Prints out methods with their type of frame. |
| 723 | class DumpFramesWithTypeStackVisitor FINAL : public StackVisitor { |
| 724 | public: |
| 725 | DumpFramesWithTypeStackVisitor(Thread* self, bool show_details = false) |
| 726 | SHARED_REQUIRES(Locks::mutator_lock_) |
| 727 | : StackVisitor(self, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 728 | show_details_(show_details) {} |
| 729 | |
| 730 | bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
| 731 | ArtMethod* method = GetMethod(); |
| 732 | if (show_details_) { |
| 733 | LOG(INFO) << "|> pc = " << std::hex << GetCurrentQuickFramePc(); |
| 734 | LOG(INFO) << "|> addr = " << std::hex << reinterpret_cast<uintptr_t>(GetCurrentQuickFrame()); |
| 735 | if (GetCurrentQuickFrame() != nullptr && method != nullptr) { |
| 736 | LOG(INFO) << "|> ret = " << std::hex << GetReturnPc(); |
| 737 | } |
| 738 | } |
| 739 | if (method == nullptr) { |
| 740 | // Transition, do go on, we want to unwind over bridges, all the way. |
| 741 | if (show_details_) { |
| 742 | LOG(INFO) << "N <transition>"; |
| 743 | } |
| 744 | return true; |
| 745 | } else if (method->IsRuntimeMethod()) { |
| 746 | if (show_details_) { |
| 747 | LOG(INFO) << "R " << PrettyMethod(method, true); |
| 748 | } |
| 749 | return true; |
| 750 | } else { |
| 751 | bool is_shadow = GetCurrentShadowFrame() != nullptr; |
| 752 | LOG(INFO) << (is_shadow ? "S" : "Q") |
| 753 | << ((!is_shadow && IsInInlinedFrame()) ? "i" : " ") |
| 754 | << " " |
| 755 | << PrettyMethod(method, true); |
| 756 | return true; // Go on. |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | private: |
| 761 | bool show_details_; |
| 762 | |
| 763 | DISALLOW_COPY_AND_ASSIGN(DumpFramesWithTypeStackVisitor); |
| 764 | }; |
| 765 | |
| 766 | void QuickExceptionHandler::DumpFramesWithType(Thread* self, bool details) { |
| 767 | DumpFramesWithTypeStackVisitor visitor(self, details); |
| 768 | visitor.WalkStack(true); |
| 769 | } |
| 770 | |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 771 | } // namespace art |