blob: 57c6866aa8a82787a85c2eb501980162c333d5fa [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"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method-inl.h"
Ian Rogers5cf98192014-05-29 21:31:50 -070021#include "dex_instruction.h"
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020022#include "entrypoints/entrypoint_utils.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070023#include "entrypoints/runtime_asm_entrypoints.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070024#include "handle_scope-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
Sebastien Hertz520633b2015-09-08 17:03:36 +020043// Finds catch handler.
Ian Rogers5cf98192014-05-29 21:31:50 -070044class CatchBlockStackVisitor FINAL : public StackVisitor {
45 public:
46 CatchBlockStackVisitor(Thread* self, Context* context, Handle<mirror::Throwable>* exception,
47 QuickExceptionHandler* exception_handler)
Mathieu Chartier90443472015-07-16 20:32:27 -070048 SHARED_REQUIRES(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010049 : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010050 exception_(exception),
Ian Rogers5cf98192014-05-29 21:31:50 -070051 exception_handler_(exception_handler) {
52 }
53
Mathieu Chartier90443472015-07-16 20:32:27 -070054 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070055 ArtMethod* method = GetMethod();
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -070056 exception_handler_->SetHandlerFrameDepth(GetFrameDepth());
Ian Rogers5cf98192014-05-29 21:31:50 -070057 if (method == nullptr) {
58 // This is the upcall, we remember the frame and last pc so that we may long jump to them.
59 exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc());
60 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
61 uint32_t next_dex_pc;
Mathieu Chartiere401d142015-04-22 13:56:20 -070062 ArtMethod* next_art_method;
Ian Rogers5cf98192014-05-29 21:31:50 -070063 bool has_next = GetNextMethodAndDexPc(&next_art_method, &next_dex_pc);
64 // Report the method that did the down call as the handler.
65 exception_handler_->SetHandlerDexPc(next_dex_pc);
66 exception_handler_->SetHandlerMethod(next_art_method);
67 if (!has_next) {
68 // No next method? Check exception handler is set up for the unhandled exception handler
69 // case.
70 DCHECK_EQ(0U, exception_handler_->GetHandlerDexPc());
71 DCHECK(nullptr == exception_handler_->GetHandlerMethod());
72 }
73 return false; // End stack walk.
74 }
75 if (method->IsRuntimeMethod()) {
76 // Ignore callee save method.
77 DCHECK(method->IsCalleeSaveMethod());
78 return true;
79 }
Mathieu Chartiere401d142015-04-22 13:56:20 -070080 return HandleTryItems(method);
Ian Rogers5cf98192014-05-29 21:31:50 -070081 }
82
83 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -070084 bool HandleTryItems(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -070085 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers5cf98192014-05-29 21:31:50 -070086 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;
Sebastien Hertz26f72862015-09-15 09:52:07 +020092 StackHandleScope<1> hs(GetThread());
Ian Rogers5cf98192014-05-29 21:31:50 -070093 Handle<mirror::Class> to_find(hs.NewHandle((*exception_)->GetClass()));
Mathieu Chartiere401d142015-04-22 13:56:20 -070094 uint32_t found_dex_pc = method->FindCatchBlock(to_find, dex_pc, &clear_exception);
Ian Rogers5cf98192014-05-29 21:31:50 -070095 exception_handler_->SetClearException(clear_exception);
96 if (found_dex_pc != DexFile::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070097 exception_handler_->SetHandlerMethod(method);
Ian Rogers5cf98192014-05-29 21:31:50 -070098 exception_handler_->SetHandlerDexPc(found_dex_pc);
Ian Rogers6f3dbba2014-10-14 17:41:57 -070099 exception_handler_->SetHandlerQuickFramePc(method->ToNativeQuickPc(found_dex_pc));
Ian Rogers5cf98192014-05-29 21:31:50 -0700100 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
101 return false; // End stack walk.
102 }
103 }
104 return true; // Continue stack walk.
105 }
106
Ian Rogers5cf98192014-05-29 21:31:50 -0700107 // The exception we're looking for the catch block of.
108 Handle<mirror::Throwable>* exception_;
109 // The quick exception handler we're visiting for.
110 QuickExceptionHandler* const exception_handler_;
111
112 DISALLOW_COPY_AND_ASSIGN(CatchBlockStackVisitor);
113};
114
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000115void QuickExceptionHandler::FindCatch(mirror::Throwable* exception) {
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200116 DCHECK(!is_deoptimization_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700117 if (kDebugExceptionDelivery) {
118 mirror::String* msg = exception->GetDetailMessage();
119 std::string str_msg(msg != nullptr ? msg->ToModifiedUtf8() : "");
120 self_->DumpStack(LOG(INFO) << "Delivering exception: " << PrettyTypeOf(exception)
121 << ": " << str_msg << "\n");
122 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700123 StackHandleScope<1> hs(self_);
124 Handle<mirror::Throwable> exception_ref(hs.NewHandle(exception));
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200125
Sebastien Hertz520633b2015-09-08 17:03:36 +0200126 // Walk the stack to find catch handler.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700127 CatchBlockStackVisitor visitor(self_, context_, &exception_ref, this);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100128 visitor.WalkStack(true);
129
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200130 if (kDebugExceptionDelivery) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700131 if (*handler_quick_frame_ == nullptr) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100132 LOG(INFO) << "Handler is upcall";
Ian Rogers5cf98192014-05-29 21:31:50 -0700133 }
134 if (handler_method_ != nullptr) {
135 const DexFile& dex_file = *handler_method_->GetDeclaringClass()->GetDexCache()->GetDexFile();
136 int line_number = dex_file.GetLineNumFromPC(handler_method_, handler_dex_pc_);
137 LOG(INFO) << "Handler: " << PrettyMethod(handler_method_) << " (line: " << line_number << ")";
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100138 }
139 }
140 if (clear_exception_) {
141 // Exception was cleared as part of delivery.
142 DCHECK(!self_->IsExceptionPending());
143 } else {
144 // Put exception back in root set with clear throw location.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000145 self_->SetException(exception_ref.Get());
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100146 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200147}
148
Ian Rogers5cf98192014-05-29 21:31:50 -0700149// Prepares deoptimization.
150class DeoptimizeStackVisitor FINAL : public StackVisitor {
151 public:
152 DeoptimizeStackVisitor(Thread* self, Context* context, QuickExceptionHandler* exception_handler)
Mathieu Chartier90443472015-07-16 20:32:27 -0700153 SHARED_REQUIRES(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100154 : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100155 exception_handler_(exception_handler),
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700156 prev_shadow_frame_(nullptr),
157 stacked_shadow_frame_pushed_(false) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700158 }
159
Mathieu Chartier90443472015-07-16 20:32:27 -0700160 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700161 exception_handler_->SetHandlerFrameDepth(GetFrameDepth());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700162 ArtMethod* method = GetMethod();
Ian Rogers5cf98192014-05-29 21:31:50 -0700163 if (method == nullptr) {
164 // This is the upcall, we remember the frame and last pc so that we may long jump to them.
165 exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc());
166 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700167 if (!stacked_shadow_frame_pushed_) {
168 // In case there is no deoptimized shadow frame for this upcall, we still
169 // need to push a nullptr to the stack since there is always a matching pop after
170 // the long jump.
Sebastien Hertz26f72862015-09-15 09:52:07 +0200171 GetThread()->PushStackedShadowFrame(nullptr,
172 StackedShadowFrameType::kDeoptimizationShadowFrame);
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700173 stacked_shadow_frame_pushed_ = true;
174 }
Ian Rogers5cf98192014-05-29 21:31:50 -0700175 return false; // End stack walk.
176 } else if (method->IsRuntimeMethod()) {
177 // Ignore callee save method.
178 DCHECK(method->IsCalleeSaveMethod());
179 return true;
Sebastien Hertz520633b2015-09-08 17:03:36 +0200180 } else if (method->IsNative()) {
181 // If we return from JNI with a pending exception and want to deoptimize, we need to skip
182 // the native method.
183 // The top method is a runtime method, the native method comes next.
184 CHECK_EQ(GetFrameDepth(), 1U);
185 return true;
Ian Rogers5cf98192014-05-29 21:31:50 -0700186 } else {
187 return HandleDeoptimization(method);
188 }
189 }
190
191 private:
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200192 static VRegKind GetVRegKind(uint16_t reg, const std::vector<int32_t>& kinds) {
193 return static_cast<VRegKind>(kinds.at(reg * 2));
194 }
195
Mathieu Chartier90443472015-07-16 20:32:27 -0700196 bool HandleDeoptimization(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700197 const DexFile::CodeItem* code_item = m->GetCodeItem();
Sebastien Hertz520633b2015-09-08 17:03:36 +0200198 CHECK(code_item != nullptr) << "No code item for " << PrettyMethod(m);
Ian Rogers5cf98192014-05-29 21:31:50 -0700199 uint16_t num_regs = code_item->registers_size_;
200 uint32_t dex_pc = GetDexPc();
Sebastien Hertz26f72862015-09-15 09:52:07 +0200201 StackHandleScope<2> hs(GetThread()); // Dex cache, class loader and method.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700202 mirror::Class* declaring_class = m->GetDeclaringClass();
203 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
204 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Sebastien Hertz26f72862015-09-15 09:52:07 +0200205 verifier::MethodVerifier verifier(GetThread(), h_dex_cache->GetDexFile(), h_dex_cache,
206 h_class_loader, &m->GetClassDef(), code_item,
207 m->GetDexMethodIndex(), m, m->GetAccessFlags(), true, true,
208 true, true);
Andreas Gampe2e04bb22015-02-10 15:37:27 -0800209 bool verifier_success = verifier.Verify();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700210 CHECK(verifier_success) << PrettyMethod(m);
211 ShadowFrame* new_frame = ShadowFrame::CreateDeoptimizedFrame(num_regs, nullptr, m, dex_pc);
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700212 {
Sebastien Hertz26f72862015-09-15 09:52:07 +0200213 ScopedStackedShadowFramePusher pusher(GetThread(), new_frame,
Sebastien Hertzf7958692015-06-09 14:09:14 +0200214 StackedShadowFrameType::kShadowFrameUnderConstruction);
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700215 const std::vector<int32_t> kinds(verifier.DescribeVRegs(dex_pc));
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000216
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700217 // Markers for dead values, used when the verifier knows a Dex register is undefined,
218 // or when the compiler knows the register has not been initialized, or is not used
219 // anymore in the method.
220 static constexpr uint32_t kDeadValue = 0xEBADDE09;
221 static constexpr uint64_t kLongDeadValue = 0xEBADDE09EBADDE09;
222 for (uint16_t reg = 0; reg < num_regs; ++reg) {
223 VRegKind kind = GetVRegKind(reg, kinds);
224 switch (kind) {
225 case kUndefined:
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000226 new_frame->SetVReg(reg, kDeadValue);
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700227 break;
228 case kConstant:
229 new_frame->SetVReg(reg, kinds.at((reg * 2) + 1));
230 break;
231 case kReferenceVReg: {
232 uint32_t value = 0;
233 // Check IsReferenceVReg in case the compiled GC map doesn't agree with the verifier.
234 // We don't want to copy a stale reference into the shadow frame as a reference.
235 // b/20736048
236 if (GetVReg(m, reg, kind, &value) && IsReferenceVReg(m, reg)) {
237 new_frame->SetVRegReference(reg, reinterpret_cast<mirror::Object*>(value));
238 } else {
239 new_frame->SetVReg(reg, kDeadValue);
240 }
241 break;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000242 }
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700243 case kLongLoVReg:
244 if (GetVRegKind(reg + 1, kinds) == kLongHiVReg) {
245 // Treat it as a "long" register pair.
246 uint64_t value = 0;
247 if (GetVRegPair(m, reg, kLongLoVReg, kLongHiVReg, &value)) {
248 new_frame->SetVRegLong(reg, value);
249 } else {
250 new_frame->SetVRegLong(reg, kLongDeadValue);
251 }
252 } else {
253 uint32_t value = 0;
254 if (GetVReg(m, reg, kind, &value)) {
255 new_frame->SetVReg(reg, value);
256 } else {
257 new_frame->SetVReg(reg, kDeadValue);
258 }
259 }
260 break;
261 case kLongHiVReg:
262 if (GetVRegKind(reg - 1, kinds) == kLongLoVReg) {
263 // Nothing to do: we treated it as a "long" register pair.
264 } else {
265 uint32_t value = 0;
266 if (GetVReg(m, reg, kind, &value)) {
267 new_frame->SetVReg(reg, value);
268 } else {
269 new_frame->SetVReg(reg, kDeadValue);
270 }
271 }
272 break;
273 case kDoubleLoVReg:
274 if (GetVRegKind(reg + 1, kinds) == kDoubleHiVReg) {
275 uint64_t value = 0;
276 if (GetVRegPair(m, reg, kDoubleLoVReg, kDoubleHiVReg, &value)) {
277 // Treat it as a "double" register pair.
278 new_frame->SetVRegLong(reg, value);
279 } else {
280 new_frame->SetVRegLong(reg, kLongDeadValue);
281 }
282 } else {
283 uint32_t value = 0;
284 if (GetVReg(m, reg, kind, &value)) {
285 new_frame->SetVReg(reg, value);
286 } else {
287 new_frame->SetVReg(reg, kDeadValue);
288 }
289 }
290 break;
291 case kDoubleHiVReg:
292 if (GetVRegKind(reg - 1, kinds) == kDoubleLoVReg) {
293 // Nothing to do: we treated it as a "double" register pair.
294 } else {
295 uint32_t value = 0;
296 if (GetVReg(m, reg, kind, &value)) {
297 new_frame->SetVReg(reg, value);
298 } else {
299 new_frame->SetVReg(reg, kDeadValue);
300 }
301 }
302 break;
303 default:
304 uint32_t value = 0;
305 if (GetVReg(m, reg, kind, &value)) {
306 new_frame->SetVReg(reg, value);
307 } else {
308 new_frame->SetVReg(reg, kDeadValue);
309 }
310 break;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000311 }
Ian Rogers5cf98192014-05-29 21:31:50 -0700312 }
313 }
314 if (prev_shadow_frame_ != nullptr) {
315 prev_shadow_frame_->SetLink(new_frame);
316 } else {
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700317 // Will be popped after the long jump after DeoptimizeStack(),
318 // right before interpreter::EnterInterpreterFromDeoptimize().
319 stacked_shadow_frame_pushed_ = true;
Sebastien Hertz26f72862015-09-15 09:52:07 +0200320 GetThread()->PushStackedShadowFrame(new_frame,
321 StackedShadowFrameType::kDeoptimizationShadowFrame);
Ian Rogers5cf98192014-05-29 21:31:50 -0700322 }
323 prev_shadow_frame_ = new_frame;
324 return true;
325 }
326
Ian Rogers5cf98192014-05-29 21:31:50 -0700327 QuickExceptionHandler* const exception_handler_;
328 ShadowFrame* prev_shadow_frame_;
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700329 bool stacked_shadow_frame_pushed_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700330
331 DISALLOW_COPY_AND_ASSIGN(DeoptimizeStackVisitor);
332};
333
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200334void QuickExceptionHandler::DeoptimizeStack() {
335 DCHECK(is_deoptimization_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700336 if (kDebugExceptionDelivery) {
337 self_->DumpStack(LOG(INFO) << "Deoptimizing: ");
338 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200339
340 DeoptimizeStackVisitor visitor(self_, context_, this);
341 visitor.WalkStack(true);
342
343 // Restore deoptimization exception
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000344 self_->SetException(Thread::GetDeoptimizationException());
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100345}
346
347// Unwinds all instrumentation stack frame prior to catch handler or upcall.
348class InstrumentationStackVisitor : public StackVisitor {
349 public:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700350 InstrumentationStackVisitor(Thread* self, size_t frame_depth)
Mathieu Chartier90443472015-07-16 20:32:27 -0700351 SHARED_REQUIRES(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100352 : StackVisitor(self, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
Ian Rogerscf7f1912014-10-22 22:06:39 -0700353 frame_depth_(frame_depth),
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100354 instrumentation_frames_to_pop_(0) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700355 CHECK_NE(frame_depth_, kInvalidFrameDepth);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100356 }
357
Mathieu Chartier90443472015-07-16 20:32:27 -0700358 bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700359 size_t current_frame_depth = GetFrameDepth();
360 if (current_frame_depth < frame_depth_) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100361 CHECK(GetMethod() != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700362 if (UNLIKELY(reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == GetReturnPc())) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100363 if (!IsInInlinedFrame()) {
364 // We do not count inlined frames, because we do not instrument them. The reason we
365 // include them in the stack walking is the check against `frame_depth_`, which is
366 // given to us by a visitor that visits inlined frames.
367 ++instrumentation_frames_to_pop_;
368 }
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100369 }
370 return true;
371 } else {
372 // We reached the frame of the catch handler or the upcall.
373 return false;
374 }
375 }
376
377 size_t GetInstrumentationFramesToPop() const {
378 return instrumentation_frames_to_pop_;
379 }
380
381 private:
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700382 const size_t frame_depth_;
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100383 size_t instrumentation_frames_to_pop_;
384
385 DISALLOW_COPY_AND_ASSIGN(InstrumentationStackVisitor);
386};
387
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200388void QuickExceptionHandler::UpdateInstrumentationStack() {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100389 if (method_tracing_active_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700390 InstrumentationStackVisitor visitor(self_, handler_frame_depth_);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100391 visitor.WalkStack(true);
392
393 size_t instrumentation_frames_to_pop = visitor.GetInstrumentationFramesToPop();
394 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
395 for (size_t i = 0; i < instrumentation_frames_to_pop; ++i) {
396 instrumentation->PopMethodForUnwind(self_, is_deoptimization_);
397 }
398 }
399}
400
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200401void QuickExceptionHandler::DoLongJump() {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100402 // Place context back on thread so it will be available when we continue.
403 self_->ReleaseLongJumpContext(context_);
404 context_->SetSP(reinterpret_cast<uintptr_t>(handler_quick_frame_));
405 CHECK_NE(handler_quick_frame_pc_, 0u);
406 context_->SetPC(handler_quick_frame_pc_);
407 context_->SmashCallerSaves();
408 context_->DoLongJump();
Andreas Gampe794ad762015-02-23 08:12:24 -0800409 UNREACHABLE();
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100410}
411
412} // namespace art