blob: 8300195d5899d05e861ffc6581d1363efa24a69d [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"
Ian Rogersff093b32014-04-30 19:04:27 -070022#include "mirror/art_method-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070023#include "handle_scope-inl.h"
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010024
25namespace art {
26
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020027QuickExceptionHandler::QuickExceptionHandler(Thread* self, bool is_deoptimization)
28 : self_(self), context_(self->GetLongJumpContext()), is_deoptimization_(is_deoptimization),
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010029 method_tracing_active_(is_deoptimization ||
30 Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()),
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020031 handler_quick_frame_(nullptr), handler_quick_frame_pc_(0), handler_dex_pc_(0),
Sebastien Hertz714f1752014-04-28 15:03:08 +020032 clear_exception_(false), handler_frame_id_(kInvalidFrameId) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010033}
34
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020035void QuickExceptionHandler::FindCatch(const ThrowLocation& throw_location,
36 mirror::Throwable* exception) {
37 DCHECK(!is_deoptimization_);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070038 StackHandleScope<1> hs(self_);
39 Handle<mirror::Throwable> exception_ref(hs.NewHandle(exception));
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020040
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010041 // Walk the stack to find catch handler or prepare for deoptimization.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070042 CatchBlockStackVisitor visitor(self_, context_, &exception_ref, this);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010043 visitor.WalkStack(true);
44
45 mirror::ArtMethod* catch_method = *handler_quick_frame_;
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020046 if (kDebugExceptionDelivery) {
47 if (catch_method == nullptr) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010048 LOG(INFO) << "Handler is upcall";
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020049 } else {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010050 const DexFile& dex_file = *catch_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
51 int line_number = dex_file.GetLineNumFromPC(catch_method, handler_dex_pc_);
52 LOG(INFO) << "Handler: " << PrettyMethod(catch_method) << " (line: " << line_number << ")";
53 }
54 }
55 if (clear_exception_) {
56 // Exception was cleared as part of delivery.
57 DCHECK(!self_->IsExceptionPending());
58 } else {
59 // Put exception back in root set with clear throw location.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070060 self_->SetException(ThrowLocation(), exception_ref.Get());
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010061 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020062 // The debugger may suspend this thread and walk its stack. Let's do this before popping
63 // instrumentation frames.
64 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
65 instrumentation->ExceptionCaughtEvent(self_, throw_location, catch_method, handler_dex_pc_,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070066 exception_ref.Get());
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020067}
68
69void QuickExceptionHandler::DeoptimizeStack() {
70 DCHECK(is_deoptimization_);
71
72 DeoptimizeStackVisitor visitor(self_, context_, this);
73 visitor.WalkStack(true);
74
75 // Restore deoptimization exception
76 self_->SetException(ThrowLocation(), Thread::GetDeoptimizationException());
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010077}
78
79// Unwinds all instrumentation stack frame prior to catch handler or upcall.
80class InstrumentationStackVisitor : public StackVisitor {
81 public:
82 InstrumentationStackVisitor(Thread* self, bool is_deoptimization, size_t frame_id)
83 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
84 : StackVisitor(self, nullptr),
85 self_(self), frame_id_(frame_id),
86 instrumentation_frames_to_pop_(0) {
87 CHECK_NE(frame_id_, kInvalidFrameId);
88 }
89
90 bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
91 size_t current_frame_id = GetFrameId();
92 if (current_frame_id > frame_id_) {
93 CHECK(GetMethod() != nullptr);
94 if (UNLIKELY(GetQuickInstrumentationExitPc() == GetReturnPc())) {
95 ++instrumentation_frames_to_pop_;
96 }
97 return true;
98 } else {
99 // We reached the frame of the catch handler or the upcall.
100 return false;
101 }
102 }
103
104 size_t GetInstrumentationFramesToPop() const {
105 return instrumentation_frames_to_pop_;
106 }
107
108 private:
109 Thread* const self_;
110 const size_t frame_id_;
111 size_t instrumentation_frames_to_pop_;
112
113 DISALLOW_COPY_AND_ASSIGN(InstrumentationStackVisitor);
114};
115
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200116void QuickExceptionHandler::UpdateInstrumentationStack() {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100117 if (method_tracing_active_) {
118 InstrumentationStackVisitor visitor(self_, is_deoptimization_, handler_frame_id_);
119 visitor.WalkStack(true);
120
121 size_t instrumentation_frames_to_pop = visitor.GetInstrumentationFramesToPop();
122 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
123 for (size_t i = 0; i < instrumentation_frames_to_pop; ++i) {
124 instrumentation->PopMethodForUnwind(self_, is_deoptimization_);
125 }
126 }
127}
128
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200129void QuickExceptionHandler::DoLongJump() {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100130 // Place context back on thread so it will be available when we continue.
131 self_->ReleaseLongJumpContext(context_);
132 context_->SetSP(reinterpret_cast<uintptr_t>(handler_quick_frame_));
133 CHECK_NE(handler_quick_frame_pc_, 0u);
134 context_->SetPC(handler_quick_frame_pc_);
135 context_->SmashCallerSaves();
136 context_->DoLongJump();
137}
138
139} // namespace art