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 | |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 19 | #include "catch_block_stack_visitor.h" |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 20 | #include "deoptimize_stack_visitor.h" |
| 21 | #include "entrypoints/entrypoint_utils.h" |
| 22 | #include "sirt_ref-inl.h" |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 23 | |
| 24 | namespace art { |
| 25 | |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 26 | QuickExceptionHandler::QuickExceptionHandler(Thread* self, bool is_deoptimization) |
| 27 | : self_(self), context_(self->GetLongJumpContext()), is_deoptimization_(is_deoptimization), |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 28 | method_tracing_active_(is_deoptimization || |
| 29 | Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()), |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 30 | handler_quick_frame_(nullptr), handler_quick_frame_pc_(0), handler_dex_pc_(0), |
Sebastien Hertz | 714f175 | 2014-04-28 15:03:08 +0200 | [diff] [blame^] | 31 | clear_exception_(false), handler_frame_id_(kInvalidFrameId) { |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 32 | } |
| 33 | |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 34 | void QuickExceptionHandler::FindCatch(const ThrowLocation& throw_location, |
| 35 | mirror::Throwable* exception) { |
| 36 | DCHECK(!is_deoptimization_); |
| 37 | SirtRef<mirror::Throwable> exception_ref(self_, exception); |
| 38 | |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 39 | // Walk the stack to find catch handler or prepare for deoptimization. |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 40 | CatchBlockStackVisitor visitor(self_, context_, exception_ref, this); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 41 | visitor.WalkStack(true); |
| 42 | |
| 43 | mirror::ArtMethod* catch_method = *handler_quick_frame_; |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 44 | if (kDebugExceptionDelivery) { |
| 45 | if (catch_method == nullptr) { |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 46 | LOG(INFO) << "Handler is upcall"; |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 47 | } else { |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 48 | const DexFile& dex_file = *catch_method->GetDeclaringClass()->GetDexCache()->GetDexFile(); |
| 49 | int line_number = dex_file.GetLineNumFromPC(catch_method, handler_dex_pc_); |
| 50 | LOG(INFO) << "Handler: " << PrettyMethod(catch_method) << " (line: " << line_number << ")"; |
| 51 | } |
| 52 | } |
| 53 | if (clear_exception_) { |
| 54 | // Exception was cleared as part of delivery. |
| 55 | DCHECK(!self_->IsExceptionPending()); |
| 56 | } else { |
| 57 | // Put exception back in root set with clear throw location. |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 58 | self_->SetException(ThrowLocation(), exception_ref.get()); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 59 | } |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 60 | // The debugger may suspend this thread and walk its stack. Let's do this before popping |
| 61 | // instrumentation frames. |
| 62 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 63 | instrumentation->ExceptionCaughtEvent(self_, throw_location, catch_method, handler_dex_pc_, |
| 64 | exception_ref.get()); |
| 65 | } |
| 66 | |
| 67 | void QuickExceptionHandler::DeoptimizeStack() { |
| 68 | DCHECK(is_deoptimization_); |
| 69 | |
| 70 | DeoptimizeStackVisitor visitor(self_, context_, this); |
| 71 | visitor.WalkStack(true); |
| 72 | |
| 73 | // Restore deoptimization exception |
| 74 | self_->SetException(ThrowLocation(), Thread::GetDeoptimizationException()); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | // Unwinds all instrumentation stack frame prior to catch handler or upcall. |
| 78 | class InstrumentationStackVisitor : public StackVisitor { |
| 79 | public: |
| 80 | InstrumentationStackVisitor(Thread* self, bool is_deoptimization, size_t frame_id) |
| 81 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 82 | : StackVisitor(self, nullptr), |
| 83 | self_(self), frame_id_(frame_id), |
| 84 | instrumentation_frames_to_pop_(0) { |
| 85 | CHECK_NE(frame_id_, kInvalidFrameId); |
| 86 | } |
| 87 | |
| 88 | bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 89 | size_t current_frame_id = GetFrameId(); |
| 90 | if (current_frame_id > frame_id_) { |
| 91 | CHECK(GetMethod() != nullptr); |
| 92 | if (UNLIKELY(GetQuickInstrumentationExitPc() == GetReturnPc())) { |
| 93 | ++instrumentation_frames_to_pop_; |
| 94 | } |
| 95 | return true; |
| 96 | } else { |
| 97 | // We reached the frame of the catch handler or the upcall. |
| 98 | return false; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | size_t GetInstrumentationFramesToPop() const { |
| 103 | return instrumentation_frames_to_pop_; |
| 104 | } |
| 105 | |
| 106 | private: |
| 107 | Thread* const self_; |
| 108 | const size_t frame_id_; |
| 109 | size_t instrumentation_frames_to_pop_; |
| 110 | |
| 111 | DISALLOW_COPY_AND_ASSIGN(InstrumentationStackVisitor); |
| 112 | }; |
| 113 | |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 114 | void QuickExceptionHandler::UpdateInstrumentationStack() { |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 115 | if (method_tracing_active_) { |
| 116 | InstrumentationStackVisitor visitor(self_, is_deoptimization_, handler_frame_id_); |
| 117 | visitor.WalkStack(true); |
| 118 | |
| 119 | size_t instrumentation_frames_to_pop = visitor.GetInstrumentationFramesToPop(); |
| 120 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 121 | for (size_t i = 0; i < instrumentation_frames_to_pop; ++i) { |
| 122 | instrumentation->PopMethodForUnwind(self_, is_deoptimization_); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 127 | void QuickExceptionHandler::DoLongJump() { |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 128 | // Place context back on thread so it will be available when we continue. |
| 129 | self_->ReleaseLongJumpContext(context_); |
| 130 | context_->SetSP(reinterpret_cast<uintptr_t>(handler_quick_frame_)); |
| 131 | CHECK_NE(handler_quick_frame_pc_, 0u); |
| 132 | context_->SetPC(handler_quick_frame_pc_); |
| 133 | context_->SmashCallerSaves(); |
| 134 | context_->DoLongJump(); |
| 135 | } |
| 136 | |
| 137 | } // namespace art |