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