blob: 730759a71b9c7fda5bd09cb070a021d37d9fb503 [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
Ian Rogerse63db272014-07-15 15:36:11 -070019#include "arch/context.h"
Ian Rogers5cf98192014-05-29 21:31:50 -070020#include "dex_instruction.h"
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020021#include "entrypoints/entrypoint_utils.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070022#include "entrypoints/runtime_asm_entrypoints.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070023#include "handle_scope-inl.h"
Ian Rogers5cf98192014-05-29 21:31:50 -070024#include "mirror/art_method-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070025#include "mirror/class-inl.h"
26#include "mirror/class_loader.h"
27#include "mirror/throwable.h"
Ian Rogers5cf98192014-05-29 21:31:50 -070028#include "verifier/method_verifier.h"
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010029
30namespace art {
31
Ian Rogers5cf98192014-05-29 21:31:50 -070032static constexpr bool kDebugExceptionDelivery = false;
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -070033static constexpr size_t kInvalidFrameDepth = 0xffffffff;
Ian Rogers5cf98192014-05-29 21:31:50 -070034
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020035QuickExceptionHandler::QuickExceptionHandler(Thread* self, bool is_deoptimization)
36 : self_(self), context_(self->GetLongJumpContext()), is_deoptimization_(is_deoptimization),
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010037 method_tracing_active_(is_deoptimization ||
38 Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()),
Ian Rogers5cf98192014-05-29 21:31:50 -070039 handler_quick_frame_(nullptr), handler_quick_frame_pc_(0), handler_method_(nullptr),
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -070040 handler_dex_pc_(0), clear_exception_(false), handler_frame_depth_(kInvalidFrameDepth) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010041}
42
Ian Rogers5cf98192014-05-29 21:31:50 -070043// Finds catch handler or prepares for deoptimization.
44class 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_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010049 : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
50 self_(self),
51 exception_(exception),
Ian Rogers5cf98192014-05-29 21:31:50 -070052 exception_handler_(exception_handler) {
53 }
54
55 bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
56 mirror::ArtMethod* method = GetMethod();
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -070057 exception_handler_->SetHandlerFrameDepth(GetFrameDepth());
Ian Rogers5cf98192014-05-29 21:31:50 -070058 if (method == nullptr) {
59 // This is the upcall, we remember the frame and last pc so that we may long jump to them.
60 exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc());
61 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
62 uint32_t next_dex_pc;
63 mirror::ArtMethod* next_art_method;
64 bool has_next = GetNextMethodAndDexPc(&next_art_method, &next_dex_pc);
65 // Report the method that did the down call as the handler.
66 exception_handler_->SetHandlerDexPc(next_dex_pc);
67 exception_handler_->SetHandlerMethod(next_art_method);
68 if (!has_next) {
69 // No next method? Check exception handler is set up for the unhandled exception handler
70 // case.
71 DCHECK_EQ(0U, exception_handler_->GetHandlerDexPc());
72 DCHECK(nullptr == exception_handler_->GetHandlerMethod());
73 }
74 return false; // End stack walk.
75 }
76 if (method->IsRuntimeMethod()) {
77 // Ignore callee save method.
78 DCHECK(method->IsCalleeSaveMethod());
79 return true;
80 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070081 StackHandleScope<1> hs(self_);
82 return HandleTryItems(hs.NewHandle(method));
Ian Rogers5cf98192014-05-29 21:31:50 -070083 }
84
85 private:
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070086 bool HandleTryItems(Handle<mirror::ArtMethod> method)
87 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers5cf98192014-05-29 21:31:50 -070088 uint32_t dex_pc = DexFile::kDexNoIndex;
89 if (!method->IsNative()) {
90 dex_pc = GetDexPc();
91 }
92 if (dex_pc != DexFile::kDexNoIndex) {
93 bool clear_exception = false;
94 StackHandleScope<1> hs(Thread::Current());
95 Handle<mirror::Class> to_find(hs.NewHandle((*exception_)->GetClass()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070096 uint32_t found_dex_pc = mirror::ArtMethod::FindCatchBlock(method, to_find, dex_pc,
97 &clear_exception);
Ian Rogers5cf98192014-05-29 21:31:50 -070098 exception_handler_->SetClearException(clear_exception);
99 if (found_dex_pc != DexFile::kDexNoIndex) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700100 exception_handler_->SetHandlerMethod(method.Get());
Ian Rogers5cf98192014-05-29 21:31:50 -0700101 exception_handler_->SetHandlerDexPc(found_dex_pc);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700102 exception_handler_->SetHandlerQuickFramePc(method->ToNativeQuickPc(found_dex_pc));
Ian Rogers5cf98192014-05-29 21:31:50 -0700103 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
104 return false; // End stack walk.
105 }
106 }
107 return true; // Continue stack walk.
108 }
109
110 Thread* const self_;
111 // The exception we're looking for the catch block of.
112 Handle<mirror::Throwable>* exception_;
113 // The quick exception handler we're visiting for.
114 QuickExceptionHandler* const exception_handler_;
115
116 DISALLOW_COPY_AND_ASSIGN(CatchBlockStackVisitor);
117};
118
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000119void QuickExceptionHandler::FindCatch(mirror::Throwable* exception) {
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200120 DCHECK(!is_deoptimization_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700121 if (kDebugExceptionDelivery) {
122 mirror::String* msg = exception->GetDetailMessage();
123 std::string str_msg(msg != nullptr ? msg->ToModifiedUtf8() : "");
124 self_->DumpStack(LOG(INFO) << "Delivering exception: " << PrettyTypeOf(exception)
125 << ": " << str_msg << "\n");
126 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700127 StackHandleScope<1> hs(self_);
128 Handle<mirror::Throwable> exception_ref(hs.NewHandle(exception));
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200129
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100130 // Walk the stack to find catch handler or prepare for deoptimization.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700131 CatchBlockStackVisitor visitor(self_, context_, &exception_ref, this);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100132 visitor.WalkStack(true);
133
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200134 if (kDebugExceptionDelivery) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700135 if (handler_quick_frame_->AsMirrorPtr() == nullptr) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100136 LOG(INFO) << "Handler is upcall";
Ian Rogers5cf98192014-05-29 21:31:50 -0700137 }
138 if (handler_method_ != nullptr) {
139 const DexFile& dex_file = *handler_method_->GetDeclaringClass()->GetDexCache()->GetDexFile();
140 int line_number = dex_file.GetLineNumFromPC(handler_method_, handler_dex_pc_);
141 LOG(INFO) << "Handler: " << PrettyMethod(handler_method_) << " (line: " << line_number << ")";
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100142 }
143 }
144 if (clear_exception_) {
145 // Exception was cleared as part of delivery.
146 DCHECK(!self_->IsExceptionPending());
147 } else {
148 // Put exception back in root set with clear throw location.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000149 self_->SetException(exception_ref.Get());
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100150 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200151 // The debugger may suspend this thread and walk its stack. Let's do this before popping
152 // instrumentation frames.
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000153 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
154 if (instrumentation->HasExceptionCaughtListeners()
155 && self_->IsExceptionThrownByCurrentMethod(exception)) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000156 instrumentation->ExceptionCaughtEvent(self_, exception_ref.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200157 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200158}
159
Ian Rogers5cf98192014-05-29 21:31:50 -0700160// Prepares deoptimization.
161class DeoptimizeStackVisitor FINAL : public StackVisitor {
162 public:
163 DeoptimizeStackVisitor(Thread* self, Context* context, QuickExceptionHandler* exception_handler)
164 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100165 : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
166 self_(self),
167 exception_handler_(exception_handler),
Ian Rogers5cf98192014-05-29 21:31:50 -0700168 prev_shadow_frame_(nullptr) {
169 CHECK(!self_->HasDeoptimizationShadowFrame());
170 }
171
172 bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700173 exception_handler_->SetHandlerFrameDepth(GetFrameDepth());
Ian Rogers5cf98192014-05-29 21:31:50 -0700174 mirror::ArtMethod* method = GetMethod();
175 if (method == nullptr) {
176 // This is the upcall, we remember the frame and last pc so that we may long jump to them.
177 exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc());
178 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
179 return false; // End stack walk.
180 } else if (method->IsRuntimeMethod()) {
181 // Ignore callee save method.
182 DCHECK(method->IsCalleeSaveMethod());
183 return true;
184 } else {
185 return HandleDeoptimization(method);
186 }
187 }
188
189 private:
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200190 static VRegKind GetVRegKind(uint16_t reg, const std::vector<int32_t>& kinds) {
191 return static_cast<VRegKind>(kinds.at(reg * 2));
192 }
193
Ian Rogers5cf98192014-05-29 21:31:50 -0700194 bool HandleDeoptimization(mirror::ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700195 const DexFile::CodeItem* code_item = m->GetCodeItem();
Ian Rogers5cf98192014-05-29 21:31:50 -0700196 CHECK(code_item != nullptr);
197 uint16_t num_regs = code_item->registers_size_;
198 uint32_t dex_pc = GetDexPc();
Andreas Gampe2e04bb22015-02-10 15:37:27 -0800199 StackHandleScope<3> hs(self_); // Dex cache, class loader and method.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700200 mirror::Class* declaring_class = m->GetDeclaringClass();
201 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
202 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700203 Handle<mirror::ArtMethod> h_method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700204 verifier::MethodVerifier verifier(self_, h_dex_cache->GetDexFile(), h_dex_cache, h_class_loader,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700205 &m->GetClassDef(), code_item, m->GetDexMethodIndex(),
Andreas Gampe2e04bb22015-02-10 15:37:27 -0800206 h_method, m->GetAccessFlags(), true, true, true, true);
207 bool verifier_success = verifier.Verify();
208 CHECK(verifier_success) << PrettyMethod(h_method.Get());
Christopher Ferris241a9582015-04-27 15:19:41 -0700209 ShadowFrame* new_frame = ShadowFrame::CreateDeoptimizedFrame(
210 num_regs, nullptr, h_method.Get(), dex_pc);
Andreas Gampe2e04bb22015-02-10 15:37:27 -0800211 self_->SetShadowFrameUnderConstruction(new_frame);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200212 const std::vector<int32_t> kinds(verifier.DescribeVRegs(dex_pc));
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000213
214 // Markers for dead values, used when the verifier knows a Dex register is undefined,
215 // or when the compiler knows the register has not been initialized, or is not used
216 // anymore in the method.
217 static constexpr uint32_t kDeadValue = 0xEBADDE09;
218 static constexpr uint64_t kLongDeadValue = 0xEBADDE09EBADDE09;
Ian Rogers5cf98192014-05-29 21:31:50 -0700219 for (uint16_t reg = 0; reg < num_regs; ++reg) {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200220 VRegKind kind = GetVRegKind(reg, kinds);
Ian Rogers5cf98192014-05-29 21:31:50 -0700221 switch (kind) {
222 case kUndefined:
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000223 new_frame->SetVReg(reg, kDeadValue);
Ian Rogers5cf98192014-05-29 21:31:50 -0700224 break;
225 case kConstant:
226 new_frame->SetVReg(reg, kinds.at((reg * 2) + 1));
227 break;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000228 case kReferenceVReg: {
229 uint32_t value = 0;
Mathieu Chartier50030ef2015-05-08 14:19:26 -0700230 // Check IsReferenceVReg in case the compiled GC map doesn't agree with the verifier.
231 // We don't want to copy a stale reference into the shadow frame as a reference.
232 // b/20736048
233 if (GetVReg(h_method.Get(), reg, kind, &value) && IsReferenceVReg(h_method.Get(), reg)) {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000234 new_frame->SetVRegReference(reg, reinterpret_cast<mirror::Object*>(value));
235 } else {
236 new_frame->SetVReg(reg, kDeadValue);
237 }
Ian Rogers5cf98192014-05-29 21:31:50 -0700238 break;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000239 }
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200240 case kLongLoVReg:
Ian Rogers07140832014-09-30 15:43:59 -0700241 if (GetVRegKind(reg + 1, kinds) == kLongHiVReg) {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200242 // Treat it as a "long" register pair.
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000243 uint64_t value = 0;
244 if (GetVRegPair(h_method.Get(), reg, kLongLoVReg, kLongHiVReg, &value)) {
245 new_frame->SetVRegLong(reg, value);
246 } else {
247 new_frame->SetVRegLong(reg, kLongDeadValue);
248 }
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200249 } else {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000250 uint32_t value = 0;
251 if (GetVReg(h_method.Get(), reg, kind, &value)) {
252 new_frame->SetVReg(reg, value);
253 } else {
254 new_frame->SetVReg(reg, kDeadValue);
255 }
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200256 }
257 break;
258 case kLongHiVReg:
Ian Rogers07140832014-09-30 15:43:59 -0700259 if (GetVRegKind(reg - 1, kinds) == kLongLoVReg) {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200260 // Nothing to do: we treated it as a "long" register pair.
261 } else {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000262 uint32_t value = 0;
263 if (GetVReg(h_method.Get(), reg, kind, &value)) {
264 new_frame->SetVReg(reg, value);
265 } else {
266 new_frame->SetVReg(reg, kDeadValue);
267 }
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200268 }
269 break;
270 case kDoubleLoVReg:
Ian Rogers07140832014-09-30 15:43:59 -0700271 if (GetVRegKind(reg + 1, kinds) == kDoubleHiVReg) {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000272 uint64_t value = 0;
273 if (GetVRegPair(h_method.Get(), reg, kDoubleLoVReg, kDoubleHiVReg, &value)) {
274 // Treat it as a "double" register pair.
275 new_frame->SetVRegLong(reg, value);
276 } else {
277 new_frame->SetVRegLong(reg, kLongDeadValue);
278 }
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200279 } else {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000280 uint32_t value = 0;
281 if (GetVReg(h_method.Get(), reg, kind, &value)) {
282 new_frame->SetVReg(reg, value);
283 } else {
284 new_frame->SetVReg(reg, kDeadValue);
285 }
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200286 }
287 break;
288 case kDoubleHiVReg:
Ian Rogers07140832014-09-30 15:43:59 -0700289 if (GetVRegKind(reg - 1, kinds) == kDoubleLoVReg) {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200290 // Nothing to do: we treated it as a "double" register pair.
291 } else {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000292 uint32_t value = 0;
293 if (GetVReg(h_method.Get(), reg, kind, &value)) {
294 new_frame->SetVReg(reg, value);
295 } else {
296 new_frame->SetVReg(reg, kDeadValue);
297 }
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200298 }
299 break;
Ian Rogers5cf98192014-05-29 21:31:50 -0700300 default:
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000301 uint32_t value = 0;
302 if (GetVReg(h_method.Get(), reg, kind, &value)) {
303 new_frame->SetVReg(reg, value);
304 } else {
305 new_frame->SetVReg(reg, kDeadValue);
306 }
Ian Rogers5cf98192014-05-29 21:31:50 -0700307 break;
308 }
309 }
310 if (prev_shadow_frame_ != nullptr) {
311 prev_shadow_frame_->SetLink(new_frame);
312 } else {
313 self_->SetDeoptimizationShadowFrame(new_frame);
314 }
Andreas Gampe2e04bb22015-02-10 15:37:27 -0800315 self_->ClearShadowFrameUnderConstruction();
Ian Rogers5cf98192014-05-29 21:31:50 -0700316 prev_shadow_frame_ = new_frame;
317 return true;
318 }
319
320 Thread* const self_;
321 QuickExceptionHandler* const exception_handler_;
322 ShadowFrame* prev_shadow_frame_;
323
324 DISALLOW_COPY_AND_ASSIGN(DeoptimizeStackVisitor);
325};
326
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200327void QuickExceptionHandler::DeoptimizeStack() {
328 DCHECK(is_deoptimization_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700329 if (kDebugExceptionDelivery) {
330 self_->DumpStack(LOG(INFO) << "Deoptimizing: ");
331 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200332
333 DeoptimizeStackVisitor visitor(self_, context_, this);
334 visitor.WalkStack(true);
335
336 // Restore deoptimization exception
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000337 self_->SetException(Thread::GetDeoptimizationException());
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100338}
339
340// Unwinds all instrumentation stack frame prior to catch handler or upcall.
341class InstrumentationStackVisitor : public StackVisitor {
342 public:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700343 InstrumentationStackVisitor(Thread* self, size_t frame_depth)
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100344 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100345 : StackVisitor(self, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
Ian Rogerscf7f1912014-10-22 22:06:39 -0700346 frame_depth_(frame_depth),
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100347 instrumentation_frames_to_pop_(0) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700348 CHECK_NE(frame_depth_, kInvalidFrameDepth);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100349 }
350
351 bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700352 size_t current_frame_depth = GetFrameDepth();
353 if (current_frame_depth < frame_depth_) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100354 CHECK(GetMethod() != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700355 if (UNLIKELY(reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == GetReturnPc())) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100356 if (!IsInInlinedFrame()) {
357 // We do not count inlined frames, because we do not instrument them. The reason we
358 // include them in the stack walking is the check against `frame_depth_`, which is
359 // given to us by a visitor that visits inlined frames.
360 ++instrumentation_frames_to_pop_;
361 }
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100362 }
363 return true;
364 } else {
365 // We reached the frame of the catch handler or the upcall.
366 return false;
367 }
368 }
369
370 size_t GetInstrumentationFramesToPop() const {
371 return instrumentation_frames_to_pop_;
372 }
373
374 private:
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700375 const size_t frame_depth_;
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100376 size_t instrumentation_frames_to_pop_;
377
378 DISALLOW_COPY_AND_ASSIGN(InstrumentationStackVisitor);
379};
380
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200381void QuickExceptionHandler::UpdateInstrumentationStack() {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100382 if (method_tracing_active_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700383 InstrumentationStackVisitor visitor(self_, handler_frame_depth_);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100384 visitor.WalkStack(true);
385
386 size_t instrumentation_frames_to_pop = visitor.GetInstrumentationFramesToPop();
387 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
388 for (size_t i = 0; i < instrumentation_frames_to_pop; ++i) {
389 instrumentation->PopMethodForUnwind(self_, is_deoptimization_);
390 }
391 }
392}
393
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200394void QuickExceptionHandler::DoLongJump() {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100395 // Place context back on thread so it will be available when we continue.
396 self_->ReleaseLongJumpContext(context_);
397 context_->SetSP(reinterpret_cast<uintptr_t>(handler_quick_frame_));
398 CHECK_NE(handler_quick_frame_pc_, 0u);
399 context_->SetPC(handler_quick_frame_pc_);
400 context_->SmashCallerSaves();
401 context_->DoLongJump();
Andreas Gampe794ad762015-02-23 08:12:24 -0800402 UNREACHABLE();
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100403}
404
405} // namespace art