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 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 19 | #include "arch/context.h" |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 20 | #include "dex_instruction.h" |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 21 | #include "entrypoints/entrypoint_utils.h" |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 22 | #include "entrypoints/runtime_asm_entrypoints.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 23 | #include "handle_scope-inl.h" |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 24 | #include "mirror/art_method-inl.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 25 | #include "mirror/class-inl.h" |
| 26 | #include "mirror/class_loader.h" |
| 27 | #include "mirror/throwable.h" |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 28 | #include "verifier/method_verifier.h" |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 32 | static constexpr bool kDebugExceptionDelivery = false; |
Hiroshi Yamauchi | 649278c | 2014-08-13 11:12:22 -0700 | [diff] [blame] | 33 | static constexpr size_t kInvalidFrameDepth = 0xffffffff; |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 34 | |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 35 | QuickExceptionHandler::QuickExceptionHandler(Thread* self, bool is_deoptimization) |
| 36 | : self_(self), context_(self->GetLongJumpContext()), is_deoptimization_(is_deoptimization), |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 37 | method_tracing_active_(is_deoptimization || |
| 38 | Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()), |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 39 | handler_quick_frame_(nullptr), handler_quick_frame_pc_(0), handler_method_(nullptr), |
Hiroshi Yamauchi | 649278c | 2014-08-13 11:12:22 -0700 | [diff] [blame] | 40 | handler_dex_pc_(0), clear_exception_(false), handler_frame_depth_(kInvalidFrameDepth) { |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 41 | } |
| 42 | |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 43 | // Finds catch handler or prepares for deoptimization. |
| 44 | class CatchBlockStackVisitor FINAL : public StackVisitor { |
| 45 | public: |
| 46 | CatchBlockStackVisitor(Thread* self, Context* context, Handle<mirror::Throwable>* exception, |
| 47 | QuickExceptionHandler* exception_handler) |
| 48 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 49 | : StackVisitor(self, context), self_(self), exception_(exception), |
| 50 | exception_handler_(exception_handler) { |
| 51 | } |
| 52 | |
| 53 | bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 54 | mirror::ArtMethod* method = GetMethod(); |
Hiroshi Yamauchi | 649278c | 2014-08-13 11:12:22 -0700 | [diff] [blame] | 55 | exception_handler_->SetHandlerFrameDepth(GetFrameDepth()); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 56 | if (method == nullptr) { |
| 57 | // This is the upcall, we remember the frame and last pc so that we may long jump to them. |
| 58 | exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc()); |
| 59 | exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame()); |
| 60 | uint32_t next_dex_pc; |
| 61 | mirror::ArtMethod* next_art_method; |
| 62 | bool has_next = GetNextMethodAndDexPc(&next_art_method, &next_dex_pc); |
| 63 | // Report the method that did the down call as the handler. |
| 64 | exception_handler_->SetHandlerDexPc(next_dex_pc); |
| 65 | exception_handler_->SetHandlerMethod(next_art_method); |
| 66 | if (!has_next) { |
| 67 | // No next method? Check exception handler is set up for the unhandled exception handler |
| 68 | // case. |
| 69 | DCHECK_EQ(0U, exception_handler_->GetHandlerDexPc()); |
| 70 | DCHECK(nullptr == exception_handler_->GetHandlerMethod()); |
| 71 | } |
| 72 | return false; // End stack walk. |
| 73 | } |
| 74 | if (method->IsRuntimeMethod()) { |
| 75 | // Ignore callee save method. |
| 76 | DCHECK(method->IsCalleeSaveMethod()); |
| 77 | return true; |
| 78 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 79 | StackHandleScope<1> hs(self_); |
| 80 | return HandleTryItems(hs.NewHandle(method)); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | private: |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 84 | bool HandleTryItems(Handle<mirror::ArtMethod> method) |
| 85 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 86 | uint32_t dex_pc = DexFile::kDexNoIndex; |
| 87 | if (!method->IsNative()) { |
| 88 | dex_pc = GetDexPc(); |
| 89 | } |
| 90 | if (dex_pc != DexFile::kDexNoIndex) { |
| 91 | bool clear_exception = false; |
| 92 | StackHandleScope<1> hs(Thread::Current()); |
| 93 | Handle<mirror::Class> to_find(hs.NewHandle((*exception_)->GetClass())); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 94 | uint32_t found_dex_pc = mirror::ArtMethod::FindCatchBlock(method, to_find, dex_pc, |
| 95 | &clear_exception); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 96 | exception_handler_->SetClearException(clear_exception); |
| 97 | if (found_dex_pc != DexFile::kDexNoIndex) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 98 | exception_handler_->SetHandlerMethod(method.Get()); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 99 | exception_handler_->SetHandlerDexPc(found_dex_pc); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 100 | exception_handler_->SetHandlerQuickFramePc(method->ToNativeQuickPc(found_dex_pc)); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 101 | exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame()); |
| 102 | return false; // End stack walk. |
| 103 | } |
| 104 | } |
| 105 | return true; // Continue stack walk. |
| 106 | } |
| 107 | |
| 108 | Thread* const self_; |
| 109 | // The exception we're looking for the catch block of. |
| 110 | Handle<mirror::Throwable>* exception_; |
| 111 | // The quick exception handler we're visiting for. |
| 112 | QuickExceptionHandler* const exception_handler_; |
| 113 | |
| 114 | DISALLOW_COPY_AND_ASSIGN(CatchBlockStackVisitor); |
| 115 | }; |
| 116 | |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 117 | void QuickExceptionHandler::FindCatch(mirror::Throwable* exception) { |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 118 | DCHECK(!is_deoptimization_); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 119 | if (kDebugExceptionDelivery) { |
| 120 | mirror::String* msg = exception->GetDetailMessage(); |
| 121 | std::string str_msg(msg != nullptr ? msg->ToModifiedUtf8() : ""); |
| 122 | self_->DumpStack(LOG(INFO) << "Delivering exception: " << PrettyTypeOf(exception) |
| 123 | << ": " << str_msg << "\n"); |
| 124 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 125 | StackHandleScope<1> hs(self_); |
| 126 | Handle<mirror::Throwable> exception_ref(hs.NewHandle(exception)); |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 127 | |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 128 | // Walk the stack to find catch handler or prepare for deoptimization. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 129 | CatchBlockStackVisitor visitor(self_, context_, &exception_ref, this); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 130 | visitor.WalkStack(true); |
| 131 | |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 132 | if (kDebugExceptionDelivery) { |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 133 | if (handler_quick_frame_->AsMirrorPtr() == nullptr) { |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 134 | LOG(INFO) << "Handler is upcall"; |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 135 | } |
| 136 | if (handler_method_ != nullptr) { |
| 137 | const DexFile& dex_file = *handler_method_->GetDeclaringClass()->GetDexCache()->GetDexFile(); |
| 138 | int line_number = dex_file.GetLineNumFromPC(handler_method_, handler_dex_pc_); |
| 139 | LOG(INFO) << "Handler: " << PrettyMethod(handler_method_) << " (line: " << line_number << ")"; |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | if (clear_exception_) { |
| 143 | // Exception was cleared as part of delivery. |
| 144 | DCHECK(!self_->IsExceptionPending()); |
| 145 | } else { |
| 146 | // Put exception back in root set with clear throw location. |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 147 | self_->SetException(exception_ref.Get()); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 148 | } |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 149 | // The debugger may suspend this thread and walk its stack. Let's do this before popping |
| 150 | // instrumentation frames. |
Nicolas Geoffray | 7642cfc | 2015-02-26 10:56:09 +0000 | [diff] [blame] | 151 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 152 | if (instrumentation->HasExceptionCaughtListeners() |
| 153 | && self_->IsExceptionThrownByCurrentMethod(exception)) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 154 | instrumentation->ExceptionCaughtEvent(self_, exception_ref.Get()); |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 155 | } |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 156 | } |
| 157 | |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 158 | // Prepares deoptimization. |
| 159 | class DeoptimizeStackVisitor FINAL : public StackVisitor { |
| 160 | public: |
| 161 | DeoptimizeStackVisitor(Thread* self, Context* context, QuickExceptionHandler* exception_handler) |
| 162 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 163 | : StackVisitor(self, context), self_(self), exception_handler_(exception_handler), |
| 164 | prev_shadow_frame_(nullptr) { |
| 165 | CHECK(!self_->HasDeoptimizationShadowFrame()); |
| 166 | } |
| 167 | |
| 168 | bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 649278c | 2014-08-13 11:12:22 -0700 | [diff] [blame] | 169 | exception_handler_->SetHandlerFrameDepth(GetFrameDepth()); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 170 | mirror::ArtMethod* method = GetMethod(); |
| 171 | if (method == nullptr) { |
| 172 | // This is the upcall, we remember the frame and last pc so that we may long jump to them. |
| 173 | exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc()); |
| 174 | exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame()); |
| 175 | return false; // End stack walk. |
| 176 | } else if (method->IsRuntimeMethod()) { |
| 177 | // Ignore callee save method. |
| 178 | DCHECK(method->IsCalleeSaveMethod()); |
| 179 | return true; |
| 180 | } else { |
| 181 | return HandleDeoptimization(method); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | private: |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 186 | static VRegKind GetVRegKind(uint16_t reg, const std::vector<int32_t>& kinds) { |
| 187 | return static_cast<VRegKind>(kinds.at(reg * 2)); |
| 188 | } |
| 189 | |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 190 | bool HandleDeoptimization(mirror::ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 191 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 192 | CHECK(code_item != nullptr); |
| 193 | uint16_t num_regs = code_item->registers_size_; |
| 194 | uint32_t dex_pc = GetDexPc(); |
Andreas Gampe | 2e04bb2 | 2015-02-10 15:37:27 -0800 | [diff] [blame] | 195 | StackHandleScope<3> hs(self_); // Dex cache, class loader and method. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 196 | mirror::Class* declaring_class = m->GetDeclaringClass(); |
| 197 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(declaring_class->GetDexCache())); |
| 198 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(declaring_class->GetClassLoader())); |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 199 | Handle<mirror::ArtMethod> h_method(hs.NewHandle(m)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 200 | verifier::MethodVerifier verifier(self_, h_dex_cache->GetDexFile(), h_dex_cache, h_class_loader, |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 201 | &m->GetClassDef(), code_item, m->GetDexMethodIndex(), |
Andreas Gampe | 2e04bb2 | 2015-02-10 15:37:27 -0800 | [diff] [blame] | 202 | h_method, m->GetAccessFlags(), true, true, true, true); |
| 203 | bool verifier_success = verifier.Verify(); |
| 204 | CHECK(verifier_success) << PrettyMethod(h_method.Get()); |
| 205 | ShadowFrame* new_frame = ShadowFrame::Create(num_regs, nullptr, h_method.Get(), dex_pc); |
| 206 | self_->SetShadowFrameUnderConstruction(new_frame); |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 207 | const std::vector<int32_t> kinds(verifier.DescribeVRegs(dex_pc)); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 208 | for (uint16_t reg = 0; reg < num_regs; ++reg) { |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 209 | VRegKind kind = GetVRegKind(reg, kinds); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 210 | switch (kind) { |
| 211 | case kUndefined: |
| 212 | new_frame->SetVReg(reg, 0xEBADDE09); |
| 213 | break; |
| 214 | case kConstant: |
| 215 | new_frame->SetVReg(reg, kinds.at((reg * 2) + 1)); |
| 216 | break; |
| 217 | case kReferenceVReg: |
| 218 | new_frame->SetVRegReference(reg, |
Andreas Gampe | 2e04bb2 | 2015-02-10 15:37:27 -0800 | [diff] [blame] | 219 | reinterpret_cast<mirror::Object*>(GetVReg(h_method.Get(), |
| 220 | reg, kind))); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 221 | break; |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 222 | case kLongLoVReg: |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 223 | if (GetVRegKind(reg + 1, kinds) == kLongHiVReg) { |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 224 | // Treat it as a "long" register pair. |
Andreas Gampe | 2e04bb2 | 2015-02-10 15:37:27 -0800 | [diff] [blame] | 225 | new_frame->SetVRegLong(reg, GetVRegPair(h_method.Get(), reg, kLongLoVReg, kLongHiVReg)); |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 226 | } else { |
Andreas Gampe | 2e04bb2 | 2015-02-10 15:37:27 -0800 | [diff] [blame] | 227 | new_frame->SetVReg(reg, GetVReg(h_method.Get(), reg, kind)); |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 228 | } |
| 229 | break; |
| 230 | case kLongHiVReg: |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 231 | if (GetVRegKind(reg - 1, kinds) == kLongLoVReg) { |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 232 | // Nothing to do: we treated it as a "long" register pair. |
| 233 | } else { |
Andreas Gampe | 2e04bb2 | 2015-02-10 15:37:27 -0800 | [diff] [blame] | 234 | new_frame->SetVReg(reg, GetVReg(h_method.Get(), reg, kind)); |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 235 | } |
| 236 | break; |
| 237 | case kDoubleLoVReg: |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 238 | if (GetVRegKind(reg + 1, kinds) == kDoubleHiVReg) { |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 239 | // Treat it as a "double" register pair. |
Andreas Gampe | 2e04bb2 | 2015-02-10 15:37:27 -0800 | [diff] [blame] | 240 | new_frame->SetVRegLong(reg, GetVRegPair(h_method.Get(), reg, kDoubleLoVReg, kDoubleHiVReg)); |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 241 | } else { |
Andreas Gampe | 2e04bb2 | 2015-02-10 15:37:27 -0800 | [diff] [blame] | 242 | new_frame->SetVReg(reg, GetVReg(h_method.Get(), reg, kind)); |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 243 | } |
| 244 | break; |
| 245 | case kDoubleHiVReg: |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 246 | if (GetVRegKind(reg - 1, kinds) == kDoubleLoVReg) { |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 247 | // Nothing to do: we treated it as a "double" register pair. |
| 248 | } else { |
Andreas Gampe | 2e04bb2 | 2015-02-10 15:37:27 -0800 | [diff] [blame] | 249 | new_frame->SetVReg(reg, GetVReg(h_method.Get(), reg, kind)); |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 250 | } |
| 251 | break; |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 252 | default: |
Andreas Gampe | 2e04bb2 | 2015-02-10 15:37:27 -0800 | [diff] [blame] | 253 | new_frame->SetVReg(reg, GetVReg(h_method.Get(), reg, kind)); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 254 | break; |
| 255 | } |
| 256 | } |
| 257 | if (prev_shadow_frame_ != nullptr) { |
| 258 | prev_shadow_frame_->SetLink(new_frame); |
| 259 | } else { |
| 260 | self_->SetDeoptimizationShadowFrame(new_frame); |
| 261 | } |
Andreas Gampe | 2e04bb2 | 2015-02-10 15:37:27 -0800 | [diff] [blame] | 262 | self_->ClearShadowFrameUnderConstruction(); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 263 | prev_shadow_frame_ = new_frame; |
| 264 | return true; |
| 265 | } |
| 266 | |
| 267 | Thread* const self_; |
| 268 | QuickExceptionHandler* const exception_handler_; |
| 269 | ShadowFrame* prev_shadow_frame_; |
| 270 | |
| 271 | DISALLOW_COPY_AND_ASSIGN(DeoptimizeStackVisitor); |
| 272 | }; |
| 273 | |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 274 | void QuickExceptionHandler::DeoptimizeStack() { |
| 275 | DCHECK(is_deoptimization_); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 276 | if (kDebugExceptionDelivery) { |
| 277 | self_->DumpStack(LOG(INFO) << "Deoptimizing: "); |
| 278 | } |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 279 | |
| 280 | DeoptimizeStackVisitor visitor(self_, context_, this); |
| 281 | visitor.WalkStack(true); |
| 282 | |
| 283 | // Restore deoptimization exception |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 284 | self_->SetException(Thread::GetDeoptimizationException()); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | // Unwinds all instrumentation stack frame prior to catch handler or upcall. |
| 288 | class InstrumentationStackVisitor : public StackVisitor { |
| 289 | public: |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 290 | InstrumentationStackVisitor(Thread* self, size_t frame_depth) |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 291 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 292 | : StackVisitor(self, nullptr), |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 293 | frame_depth_(frame_depth), |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 294 | instrumentation_frames_to_pop_(0) { |
Hiroshi Yamauchi | 649278c | 2014-08-13 11:12:22 -0700 | [diff] [blame] | 295 | CHECK_NE(frame_depth_, kInvalidFrameDepth); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 649278c | 2014-08-13 11:12:22 -0700 | [diff] [blame] | 299 | size_t current_frame_depth = GetFrameDepth(); |
| 300 | if (current_frame_depth < frame_depth_) { |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 301 | CHECK(GetMethod() != nullptr); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 302 | if (UNLIKELY(reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == GetReturnPc())) { |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 303 | ++instrumentation_frames_to_pop_; |
| 304 | } |
| 305 | return true; |
| 306 | } else { |
| 307 | // We reached the frame of the catch handler or the upcall. |
| 308 | return false; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | size_t GetInstrumentationFramesToPop() const { |
| 313 | return instrumentation_frames_to_pop_; |
| 314 | } |
| 315 | |
| 316 | private: |
Hiroshi Yamauchi | 649278c | 2014-08-13 11:12:22 -0700 | [diff] [blame] | 317 | const size_t frame_depth_; |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 318 | size_t instrumentation_frames_to_pop_; |
| 319 | |
| 320 | DISALLOW_COPY_AND_ASSIGN(InstrumentationStackVisitor); |
| 321 | }; |
| 322 | |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 323 | void QuickExceptionHandler::UpdateInstrumentationStack() { |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 324 | if (method_tracing_active_) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 325 | InstrumentationStackVisitor visitor(self_, handler_frame_depth_); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 326 | visitor.WalkStack(true); |
| 327 | |
| 328 | size_t instrumentation_frames_to_pop = visitor.GetInstrumentationFramesToPop(); |
| 329 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 330 | for (size_t i = 0; i < instrumentation_frames_to_pop; ++i) { |
| 331 | instrumentation->PopMethodForUnwind(self_, is_deoptimization_); |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 336 | void QuickExceptionHandler::DoLongJump() { |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 337 | // Place context back on thread so it will be available when we continue. |
| 338 | self_->ReleaseLongJumpContext(context_); |
| 339 | context_->SetSP(reinterpret_cast<uintptr_t>(handler_quick_frame_)); |
| 340 | CHECK_NE(handler_quick_frame_pc_, 0u); |
| 341 | context_->SetPC(handler_quick_frame_pc_); |
| 342 | context_->SmashCallerSaves(); |
| 343 | context_->DoLongJump(); |
Andreas Gampe | 794ad76 | 2015-02-23 08:12:24 -0800 | [diff] [blame] | 344 | UNREACHABLE(); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | } // namespace art |