blob: aee0d64290fdcf8fd0dec8a888478b762d42b97b [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"
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020023#include "sirt_ref-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_);
38 SirtRef<mirror::Throwable> exception_ref(self_, exception);
39
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010040 // Walk the stack to find catch handler or prepare for deoptimization.
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020041 CatchBlockStackVisitor visitor(self_, context_, exception_ref, this);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010042 visitor.WalkStack(true);
43
44 mirror::ArtMethod* catch_method = *handler_quick_frame_;
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020045 if (kDebugExceptionDelivery) {
46 if (catch_method == nullptr) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010047 LOG(INFO) << "Handler is upcall";
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020048 } else {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010049 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 Hertzfd3077e2014-04-23 10:32:43 +020059 self_->SetException(ThrowLocation(), exception_ref.get());
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010060 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020061 // 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
68void 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 Hertzd45a1f52014-01-09 14:56:54 +010076}
77
78// Unwinds all instrumentation stack frame prior to catch handler or upcall.
79class 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 Hertzfd3077e2014-04-23 10:32:43 +0200115void QuickExceptionHandler::UpdateInstrumentationStack() {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100116 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 Hertzfd3077e2014-04-23 10:32:43 +0200128void QuickExceptionHandler::DoLongJump() {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100129 // 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