blob: d5844b6e835588380e6ee90ebee691386a84eb13 [file] [log] [blame]
Sebastien Hertzd45a1f52014-01-09 14:56:54 +01001/*
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 Hertzfd3077e2014-04-23 10:32:43 +020017#include "quick_exception_handler.h"
18
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010019#include "catch_block_stack_visitor.h"
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020020#include "deoptimize_stack_visitor.h"
21#include "entrypoints/entrypoint_utils.h"
22#include "sirt_ref-inl.h"
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010023
24namespace art {
25
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020026QuickExceptionHandler::QuickExceptionHandler(Thread* self, bool is_deoptimization)
27 : self_(self), context_(self->GetLongJumpContext()), is_deoptimization_(is_deoptimization),
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010028 method_tracing_active_(is_deoptimization ||
29 Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()),
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020030 handler_quick_frame_(nullptr), handler_quick_frame_pc_(0), handler_dex_pc_(0),
31 clear_exception_(false), top_shadow_frame_(nullptr), handler_frame_id_(kInvalidFrameId) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010032}
33
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020034void QuickExceptionHandler::FindCatch(const ThrowLocation& throw_location,
35 mirror::Throwable* exception) {
36 DCHECK(!is_deoptimization_);
37 SirtRef<mirror::Throwable> exception_ref(self_, exception);
38
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010039 // Walk the stack to find catch handler or prepare for deoptimization.
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020040 CatchBlockStackVisitor visitor(self_, context_, exception_ref, this);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010041 visitor.WalkStack(true);
42
43 mirror::ArtMethod* catch_method = *handler_quick_frame_;
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020044 if (kDebugExceptionDelivery) {
45 if (catch_method == nullptr) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010046 LOG(INFO) << "Handler is upcall";
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020047 } else {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010048 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 Hertzfd3077e2014-04-23 10:32:43 +020058 self_->SetException(ThrowLocation(), exception_ref.get());
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010059 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020060 // 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
67void 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 Hertzd45a1f52014-01-09 14:56:54 +010075}
76
77// Unwinds all instrumentation stack frame prior to catch handler or upcall.
78class 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 Hertzfd3077e2014-04-23 10:32:43 +0200114void QuickExceptionHandler::UpdateInstrumentationStack() {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100115 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 Hertzfd3077e2014-04-23 10:32:43 +0200127void QuickExceptionHandler::DoLongJump() {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100128 if (is_deoptimization_) {
129 // TODO: proper return value.
130 self_->SetDeoptimizationShadowFrame(top_shadow_frame_);
131 }
132 // Place context back on thread so it will be available when we continue.
133 self_->ReleaseLongJumpContext(context_);
134 context_->SetSP(reinterpret_cast<uintptr_t>(handler_quick_frame_));
135 CHECK_NE(handler_quick_frame_pc_, 0u);
136 context_->SetPC(handler_quick_frame_pc_);
137 context_->SmashCallerSaves();
138 context_->DoLongJump();
139}
140
141} // namespace art