blob: 9d5ce9f38579fa9a5642ce9202a64432d2040f18 [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
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)
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),
50 self_(self),
51 exception_(exception),
Ian Rogers5cf98192014-05-29 21:31:50 -070052 exception_handler_(exception_handler) {
53 }
54
Mathieu Chartier90443472015-07-16 20:32:27 -070055 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070056 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;
Mathieu Chartiere401d142015-04-22 13:56:20 -070063 ArtMethod* next_art_method;
Ian Rogers5cf98192014-05-29 21:31:50 -070064 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 Chartiere401d142015-04-22 13:56:20 -070081 return HandleTryItems(method);
Ian Rogers5cf98192014-05-29 21:31:50 -070082 }
83
84 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -070085 bool HandleTryItems(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -070086 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers5cf98192014-05-29 21:31:50 -070087 uint32_t dex_pc = DexFile::kDexNoIndex;
88 if (!method->IsNative()) {
89 dex_pc = GetDexPc();
90 }
91 if (dex_pc != DexFile::kDexNoIndex) {
92 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -070093 StackHandleScope<1> hs(self_);
Ian Rogers5cf98192014-05-29 21:31:50 -070094 Handle<mirror::Class> to_find(hs.NewHandle((*exception_)->GetClass()));
Mathieu Chartiere401d142015-04-22 13:56:20 -070095 uint32_t found_dex_pc = method->FindCatchBlock(to_find, dex_pc, &clear_exception);
Ian Rogers5cf98192014-05-29 21:31:50 -070096 exception_handler_->SetClearException(clear_exception);
97 if (found_dex_pc != DexFile::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070098 exception_handler_->SetHandlerMethod(method);
Ian Rogers5cf98192014-05-29 21:31:50 -070099 exception_handler_->SetHandlerDexPc(found_dex_pc);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700100 exception_handler_->SetHandlerQuickFramePc(method->ToNativeQuickPc(found_dex_pc));
Ian Rogers5cf98192014-05-29 21:31:50 -0700101 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 Geoffray14691c52015-03-05 10:40:17 +0000117void QuickExceptionHandler::FindCatch(mirror::Throwable* exception) {
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200118 DCHECK(!is_deoptimization_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700119 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 Chartiereb8167a2014-05-07 15:43:14 -0700125 StackHandleScope<1> hs(self_);
126 Handle<mirror::Throwable> exception_ref(hs.NewHandle(exception));
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200127
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100128 // Walk the stack to find catch handler or prepare for deoptimization.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700129 CatchBlockStackVisitor visitor(self_, context_, &exception_ref, this);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100130 visitor.WalkStack(true);
131
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200132 if (kDebugExceptionDelivery) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700133 if (*handler_quick_frame_ == nullptr) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100134 LOG(INFO) << "Handler is upcall";
Ian Rogers5cf98192014-05-29 21:31:50 -0700135 }
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 Hertzd45a1f52014-01-09 14:56:54 +0100140 }
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 Geoffray14691c52015-03-05 10:40:17 +0000147 self_->SetException(exception_ref.Get());
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100148 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200149 // The debugger may suspend this thread and walk its stack. Let's do this before popping
150 // instrumentation frames.
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000151 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
152 if (instrumentation->HasExceptionCaughtListeners()
153 && self_->IsExceptionThrownByCurrentMethod(exception)) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000154 instrumentation->ExceptionCaughtEvent(self_, exception_ref.Get());
Sebastien Hertzb995c0a2015-08-24 14:27:01 +0200155 // Instrumentation may have been updated.
156 method_tracing_active_ = is_deoptimization_ ||
157 Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled();
Sebastien Hertz9f102032014-05-23 08:59:42 +0200158 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200159}
160
Ian Rogers5cf98192014-05-29 21:31:50 -0700161// Prepares deoptimization.
162class DeoptimizeStackVisitor FINAL : public StackVisitor {
163 public:
164 DeoptimizeStackVisitor(Thread* self, Context* context, QuickExceptionHandler* exception_handler)
Mathieu Chartier90443472015-07-16 20:32:27 -0700165 SHARED_REQUIRES(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100166 : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
167 self_(self),
168 exception_handler_(exception_handler),
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700169 prev_shadow_frame_(nullptr),
170 stacked_shadow_frame_pushed_(false) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700171 }
172
Mathieu Chartier90443472015-07-16 20:32:27 -0700173 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700174 exception_handler_->SetHandlerFrameDepth(GetFrameDepth());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700175 ArtMethod* method = GetMethod();
Ian Rogers5cf98192014-05-29 21:31:50 -0700176 if (method == nullptr) {
177 // This is the upcall, we remember the frame and last pc so that we may long jump to them.
178 exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc());
179 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700180 if (!stacked_shadow_frame_pushed_) {
181 // In case there is no deoptimized shadow frame for this upcall, we still
182 // need to push a nullptr to the stack since there is always a matching pop after
183 // the long jump.
Sebastien Hertzf7958692015-06-09 14:09:14 +0200184 self_->PushStackedShadowFrame(nullptr, StackedShadowFrameType::kDeoptimizationShadowFrame);
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700185 stacked_shadow_frame_pushed_ = true;
186 }
Ian Rogers5cf98192014-05-29 21:31:50 -0700187 return false; // End stack walk.
188 } else if (method->IsRuntimeMethod()) {
189 // Ignore callee save method.
190 DCHECK(method->IsCalleeSaveMethod());
191 return true;
192 } else {
193 return HandleDeoptimization(method);
194 }
195 }
196
197 private:
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200198 static VRegKind GetVRegKind(uint16_t reg, const std::vector<int32_t>& kinds) {
199 return static_cast<VRegKind>(kinds.at(reg * 2));
200 }
201
Mathieu Chartier90443472015-07-16 20:32:27 -0700202 bool HandleDeoptimization(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700203 const DexFile::CodeItem* code_item = m->GetCodeItem();
Ian Rogers5cf98192014-05-29 21:31:50 -0700204 CHECK(code_item != nullptr);
205 uint16_t num_regs = code_item->registers_size_;
206 uint32_t dex_pc = GetDexPc();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700207 StackHandleScope<2> hs(self_); // Dex cache, class loader and method.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700208 mirror::Class* declaring_class = m->GetDeclaringClass();
209 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
210 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700211 verifier::MethodVerifier verifier(self_, h_dex_cache->GetDexFile(), h_dex_cache, h_class_loader,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700212 &m->GetClassDef(), code_item, m->GetDexMethodIndex(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700213 m, m->GetAccessFlags(), true, true, true, true);
Andreas Gampe2e04bb22015-02-10 15:37:27 -0800214 bool verifier_success = verifier.Verify();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700215 CHECK(verifier_success) << PrettyMethod(m);
216 ShadowFrame* new_frame = ShadowFrame::CreateDeoptimizedFrame(num_regs, nullptr, m, dex_pc);
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700217 {
Sebastien Hertzf7958692015-06-09 14:09:14 +0200218 ScopedStackedShadowFramePusher pusher(self_, new_frame,
219 StackedShadowFrameType::kShadowFrameUnderConstruction);
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700220 const std::vector<int32_t> kinds(verifier.DescribeVRegs(dex_pc));
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000221
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700222 // Markers for dead values, used when the verifier knows a Dex register is undefined,
223 // or when the compiler knows the register has not been initialized, or is not used
224 // anymore in the method.
225 static constexpr uint32_t kDeadValue = 0xEBADDE09;
226 static constexpr uint64_t kLongDeadValue = 0xEBADDE09EBADDE09;
227 for (uint16_t reg = 0; reg < num_regs; ++reg) {
228 VRegKind kind = GetVRegKind(reg, kinds);
229 switch (kind) {
230 case kUndefined:
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000231 new_frame->SetVReg(reg, kDeadValue);
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700232 break;
233 case kConstant:
234 new_frame->SetVReg(reg, kinds.at((reg * 2) + 1));
235 break;
236 case kReferenceVReg: {
237 uint32_t value = 0;
238 // Check IsReferenceVReg in case the compiled GC map doesn't agree with the verifier.
239 // We don't want to copy a stale reference into the shadow frame as a reference.
240 // b/20736048
241 if (GetVReg(m, reg, kind, &value) && IsReferenceVReg(m, reg)) {
242 new_frame->SetVRegReference(reg, reinterpret_cast<mirror::Object*>(value));
243 } else {
244 new_frame->SetVReg(reg, kDeadValue);
245 }
246 break;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000247 }
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700248 case kLongLoVReg:
249 if (GetVRegKind(reg + 1, kinds) == kLongHiVReg) {
250 // Treat it as a "long" register pair.
251 uint64_t value = 0;
252 if (GetVRegPair(m, reg, kLongLoVReg, kLongHiVReg, &value)) {
253 new_frame->SetVRegLong(reg, value);
254 } else {
255 new_frame->SetVRegLong(reg, kLongDeadValue);
256 }
257 } else {
258 uint32_t value = 0;
259 if (GetVReg(m, reg, kind, &value)) {
260 new_frame->SetVReg(reg, value);
261 } else {
262 new_frame->SetVReg(reg, kDeadValue);
263 }
264 }
265 break;
266 case kLongHiVReg:
267 if (GetVRegKind(reg - 1, kinds) == kLongLoVReg) {
268 // Nothing to do: we treated it as a "long" register pair.
269 } else {
270 uint32_t value = 0;
271 if (GetVReg(m, reg, kind, &value)) {
272 new_frame->SetVReg(reg, value);
273 } else {
274 new_frame->SetVReg(reg, kDeadValue);
275 }
276 }
277 break;
278 case kDoubleLoVReg:
279 if (GetVRegKind(reg + 1, kinds) == kDoubleHiVReg) {
280 uint64_t value = 0;
281 if (GetVRegPair(m, reg, kDoubleLoVReg, kDoubleHiVReg, &value)) {
282 // Treat it as a "double" register pair.
283 new_frame->SetVRegLong(reg, value);
284 } else {
285 new_frame->SetVRegLong(reg, kLongDeadValue);
286 }
287 } else {
288 uint32_t value = 0;
289 if (GetVReg(m, reg, kind, &value)) {
290 new_frame->SetVReg(reg, value);
291 } else {
292 new_frame->SetVReg(reg, kDeadValue);
293 }
294 }
295 break;
296 case kDoubleHiVReg:
297 if (GetVRegKind(reg - 1, kinds) == kDoubleLoVReg) {
298 // Nothing to do: we treated it as a "double" register pair.
299 } else {
300 uint32_t value = 0;
301 if (GetVReg(m, reg, kind, &value)) {
302 new_frame->SetVReg(reg, value);
303 } else {
304 new_frame->SetVReg(reg, kDeadValue);
305 }
306 }
307 break;
308 default:
309 uint32_t value = 0;
310 if (GetVReg(m, reg, kind, &value)) {
311 new_frame->SetVReg(reg, value);
312 } else {
313 new_frame->SetVReg(reg, kDeadValue);
314 }
315 break;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000316 }
Ian Rogers5cf98192014-05-29 21:31:50 -0700317 }
318 }
319 if (prev_shadow_frame_ != nullptr) {
320 prev_shadow_frame_->SetLink(new_frame);
321 } else {
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700322 // Will be popped after the long jump after DeoptimizeStack(),
323 // right before interpreter::EnterInterpreterFromDeoptimize().
324 stacked_shadow_frame_pushed_ = true;
Sebastien Hertzf7958692015-06-09 14:09:14 +0200325 self_->PushStackedShadowFrame(new_frame, StackedShadowFrameType::kDeoptimizationShadowFrame);
Ian Rogers5cf98192014-05-29 21:31:50 -0700326 }
327 prev_shadow_frame_ = new_frame;
328 return true;
329 }
330
331 Thread* const self_;
332 QuickExceptionHandler* const exception_handler_;
333 ShadowFrame* prev_shadow_frame_;
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700334 bool stacked_shadow_frame_pushed_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700335
336 DISALLOW_COPY_AND_ASSIGN(DeoptimizeStackVisitor);
337};
338
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200339void QuickExceptionHandler::DeoptimizeStack() {
340 DCHECK(is_deoptimization_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700341 if (kDebugExceptionDelivery) {
342 self_->DumpStack(LOG(INFO) << "Deoptimizing: ");
343 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200344
345 DeoptimizeStackVisitor visitor(self_, context_, this);
346 visitor.WalkStack(true);
347
348 // Restore deoptimization exception
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000349 self_->SetException(Thread::GetDeoptimizationException());
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100350}
351
352// Unwinds all instrumentation stack frame prior to catch handler or upcall.
353class InstrumentationStackVisitor : public StackVisitor {
354 public:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700355 InstrumentationStackVisitor(Thread* self, size_t frame_depth)
Mathieu Chartier90443472015-07-16 20:32:27 -0700356 SHARED_REQUIRES(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100357 : StackVisitor(self, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
Ian Rogerscf7f1912014-10-22 22:06:39 -0700358 frame_depth_(frame_depth),
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100359 instrumentation_frames_to_pop_(0) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700360 CHECK_NE(frame_depth_, kInvalidFrameDepth);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100361 }
362
Mathieu Chartier90443472015-07-16 20:32:27 -0700363 bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700364 size_t current_frame_depth = GetFrameDepth();
365 if (current_frame_depth < frame_depth_) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100366 CHECK(GetMethod() != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700367 if (UNLIKELY(reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == GetReturnPc())) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100368 if (!IsInInlinedFrame()) {
369 // We do not count inlined frames, because we do not instrument them. The reason we
370 // include them in the stack walking is the check against `frame_depth_`, which is
371 // given to us by a visitor that visits inlined frames.
372 ++instrumentation_frames_to_pop_;
373 }
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100374 }
375 return true;
376 } else {
377 // We reached the frame of the catch handler or the upcall.
378 return false;
379 }
380 }
381
382 size_t GetInstrumentationFramesToPop() const {
383 return instrumentation_frames_to_pop_;
384 }
385
386 private:
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700387 const size_t frame_depth_;
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100388 size_t instrumentation_frames_to_pop_;
389
390 DISALLOW_COPY_AND_ASSIGN(InstrumentationStackVisitor);
391};
392
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200393void QuickExceptionHandler::UpdateInstrumentationStack() {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100394 if (method_tracing_active_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700395 InstrumentationStackVisitor visitor(self_, handler_frame_depth_);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100396 visitor.WalkStack(true);
397
398 size_t instrumentation_frames_to_pop = visitor.GetInstrumentationFramesToPop();
399 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
400 for (size_t i = 0; i < instrumentation_frames_to_pop; ++i) {
401 instrumentation->PopMethodForUnwind(self_, is_deoptimization_);
402 }
403 }
404}
405
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200406void QuickExceptionHandler::DoLongJump() {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100407 // Place context back on thread so it will be available when we continue.
408 self_->ReleaseLongJumpContext(context_);
409 context_->SetSP(reinterpret_cast<uintptr_t>(handler_quick_frame_));
410 CHECK_NE(handler_quick_frame_pc_, 0u);
411 context_->SetPC(handler_quick_frame_pc_);
412 context_->SmashCallerSaves();
413 context_->DoLongJump();
Andreas Gampe794ad762015-02-23 08:12:24 -0800414 UNREACHABLE();
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100415}
416
417} // namespace art