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