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