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 | |
| 17 | #include "catch_finder.h" |
| 18 | #include "catch_block_stack_visitor.h" |
| 19 | |
| 20 | namespace art { |
| 21 | |
| 22 | CatchFinder::CatchFinder(Thread* self, const ThrowLocation& throw_location, |
| 23 | mirror::Throwable* exception, bool is_deoptimization) |
| 24 | : self_(self), context_(self->GetLongJumpContext()), |
| 25 | exception_(exception), is_deoptimization_(is_deoptimization), throw_location_(throw_location), |
| 26 | method_tracing_active_(is_deoptimization || |
| 27 | Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()), |
| 28 | handler_quick_frame_(nullptr), handler_quick_frame_pc_(0), |
| 29 | handler_dex_pc_(0), clear_exception_(false), top_shadow_frame_(nullptr), |
| 30 | handler_frame_id_(kInvalidFrameId) { |
| 31 | // Exception not in root sets, can't allow GC. |
| 32 | last_no_assert_suspension_cause_ = self->StartAssertNoThreadSuspension("Finding catch block"); |
| 33 | } |
| 34 | |
| 35 | void CatchFinder::FindCatch() { |
| 36 | // Walk the stack to find catch handler or prepare for deoptimization. |
| 37 | CatchBlockStackVisitor visitor(self_, context_, exception_, is_deoptimization_, this); |
| 38 | visitor.WalkStack(true); |
| 39 | |
| 40 | mirror::ArtMethod* catch_method = *handler_quick_frame_; |
| 41 | if (catch_method == nullptr) { |
| 42 | if (kDebugExceptionDelivery) { |
| 43 | LOG(INFO) << "Handler is upcall"; |
| 44 | } |
| 45 | } else { |
| 46 | CHECK(!is_deoptimization_); |
| 47 | if (kDebugExceptionDelivery) { |
| 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. |
| 58 | self_->SetException(ThrowLocation(), exception_); |
| 59 | } |
| 60 | self_->EndAssertNoThreadSuspension(last_no_assert_suspension_cause_); |
| 61 | // Do instrumentation events after allowing thread suspension again. |
| 62 | if (!is_deoptimization_) { |
| 63 | // The debugger may suspend this thread and walk its stack. Let's do this before popping |
| 64 | // instrumentation frames. |
| 65 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 66 | instrumentation->ExceptionCaughtEvent(self_, throw_location_, catch_method, handler_dex_pc_, |
| 67 | exception_); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Unwinds all instrumentation stack frame prior to catch handler or upcall. |
| 72 | class InstrumentationStackVisitor : public StackVisitor { |
| 73 | public: |
| 74 | InstrumentationStackVisitor(Thread* self, bool is_deoptimization, size_t frame_id) |
| 75 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 76 | : StackVisitor(self, nullptr), |
| 77 | self_(self), frame_id_(frame_id), |
| 78 | instrumentation_frames_to_pop_(0) { |
| 79 | CHECK_NE(frame_id_, kInvalidFrameId); |
| 80 | } |
| 81 | |
| 82 | bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 83 | size_t current_frame_id = GetFrameId(); |
| 84 | if (current_frame_id > frame_id_) { |
| 85 | CHECK(GetMethod() != nullptr); |
| 86 | if (UNLIKELY(GetQuickInstrumentationExitPc() == GetReturnPc())) { |
| 87 | ++instrumentation_frames_to_pop_; |
| 88 | } |
| 89 | return true; |
| 90 | } else { |
| 91 | // We reached the frame of the catch handler or the upcall. |
| 92 | return false; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | size_t GetInstrumentationFramesToPop() const { |
| 97 | return instrumentation_frames_to_pop_; |
| 98 | } |
| 99 | |
| 100 | private: |
| 101 | Thread* const self_; |
| 102 | const size_t frame_id_; |
| 103 | size_t instrumentation_frames_to_pop_; |
| 104 | |
| 105 | DISALLOW_COPY_AND_ASSIGN(InstrumentationStackVisitor); |
| 106 | }; |
| 107 | |
| 108 | void CatchFinder::UpdateInstrumentationStack() { |
| 109 | if (method_tracing_active_) { |
| 110 | InstrumentationStackVisitor visitor(self_, is_deoptimization_, handler_frame_id_); |
| 111 | visitor.WalkStack(true); |
| 112 | |
| 113 | size_t instrumentation_frames_to_pop = visitor.GetInstrumentationFramesToPop(); |
| 114 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 115 | for (size_t i = 0; i < instrumentation_frames_to_pop; ++i) { |
| 116 | instrumentation->PopMethodForUnwind(self_, is_deoptimization_); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | void CatchFinder::DoLongJump() { |
| 122 | if (is_deoptimization_) { |
| 123 | // TODO: proper return value. |
| 124 | self_->SetDeoptimizationShadowFrame(top_shadow_frame_); |
| 125 | } |
| 126 | // Place context back on thread so it will be available when we continue. |
| 127 | self_->ReleaseLongJumpContext(context_); |
| 128 | context_->SetSP(reinterpret_cast<uintptr_t>(handler_quick_frame_)); |
| 129 | CHECK_NE(handler_quick_frame_pc_, 0u); |
| 130 | context_->SetPC(handler_quick_frame_pc_); |
| 131 | context_->SmashCallerSaves(); |
| 132 | context_->DoLongJump(); |
| 133 | } |
| 134 | |
| 135 | } // namespace art |