blob: 8c9782aefec8129cc7566a0ab4ce1f3ce517ce7c [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)
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_) {
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 Chartierbfd9a432014-05-21 17:43:44 -070086 SHARED_LOCKS_REQUIRED(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 Hertz9f102032014-05-23 08:59:42 +0200155 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200156}
157
Ian Rogers5cf98192014-05-29 21:31:50 -0700158// Prepares deoptimization.
159class DeoptimizeStackVisitor FINAL : public StackVisitor {
160 public:
161 DeoptimizeStackVisitor(Thread* self, Context* context, QuickExceptionHandler* exception_handler)
162 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100163 : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
164 self_(self),
165 exception_handler_(exception_handler),
Ian Rogers5cf98192014-05-29 21:31:50 -0700166 prev_shadow_frame_(nullptr) {
167 CHECK(!self_->HasDeoptimizationShadowFrame());
168 }
169
170 bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700171 exception_handler_->SetHandlerFrameDepth(GetFrameDepth());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700172 ArtMethod* method = GetMethod();
Ian Rogers5cf98192014-05-29 21:31:50 -0700173 if (method == nullptr) {
174 // This is the upcall, we remember the frame and last pc so that we may long jump to them.
175 exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc());
176 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
177 return false; // End stack walk.
178 } else if (method->IsRuntimeMethod()) {
179 // Ignore callee save method.
180 DCHECK(method->IsCalleeSaveMethod());
181 return true;
182 } else {
183 return HandleDeoptimization(method);
184 }
185 }
186
187 private:
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200188 static VRegKind GetVRegKind(uint16_t reg, const std::vector<int32_t>& kinds) {
189 return static_cast<VRegKind>(kinds.at(reg * 2));
190 }
191
Mathieu Chartiere401d142015-04-22 13:56:20 -0700192 bool HandleDeoptimization(ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700193 const DexFile::CodeItem* code_item = m->GetCodeItem();
Ian Rogers5cf98192014-05-29 21:31:50 -0700194 CHECK(code_item != nullptr);
195 uint16_t num_regs = code_item->registers_size_;
196 uint32_t dex_pc = GetDexPc();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700197 StackHandleScope<2> hs(self_); // Dex cache, class loader and method.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700198 mirror::Class* declaring_class = m->GetDeclaringClass();
199 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
200 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700201 verifier::MethodVerifier verifier(self_, h_dex_cache->GetDexFile(), h_dex_cache, h_class_loader,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700202 &m->GetClassDef(), code_item, m->GetDexMethodIndex(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700203 m, m->GetAccessFlags(), true, true, true, true);
Andreas Gampe2e04bb22015-02-10 15:37:27 -0800204 bool verifier_success = verifier.Verify();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700205 CHECK(verifier_success) << PrettyMethod(m);
206 ShadowFrame* new_frame = ShadowFrame::CreateDeoptimizedFrame(num_regs, nullptr, m, dex_pc);
Andreas Gampe2e04bb22015-02-10 15:37:27 -0800207 self_->SetShadowFrameUnderConstruction(new_frame);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200208 const std::vector<int32_t> kinds(verifier.DescribeVRegs(dex_pc));
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000209
210 // Markers for dead values, used when the verifier knows a Dex register is undefined,
211 // or when the compiler knows the register has not been initialized, or is not used
212 // anymore in the method.
213 static constexpr uint32_t kDeadValue = 0xEBADDE09;
214 static constexpr uint64_t kLongDeadValue = 0xEBADDE09EBADDE09;
Ian Rogers5cf98192014-05-29 21:31:50 -0700215 for (uint16_t reg = 0; reg < num_regs; ++reg) {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200216 VRegKind kind = GetVRegKind(reg, kinds);
Ian Rogers5cf98192014-05-29 21:31:50 -0700217 switch (kind) {
218 case kUndefined:
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000219 new_frame->SetVReg(reg, kDeadValue);
Ian Rogers5cf98192014-05-29 21:31:50 -0700220 break;
221 case kConstant:
222 new_frame->SetVReg(reg, kinds.at((reg * 2) + 1));
223 break;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000224 case kReferenceVReg: {
225 uint32_t value = 0;
Mathieu Chartier50030ef2015-05-08 14:19:26 -0700226 // Check IsReferenceVReg in case the compiled GC map doesn't agree with the verifier.
227 // We don't want to copy a stale reference into the shadow frame as a reference.
228 // b/20736048
Mathieu Chartiere401d142015-04-22 13:56:20 -0700229 if (GetVReg(m, reg, kind, &value) && IsReferenceVReg(m, reg)) {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000230 new_frame->SetVRegReference(reg, reinterpret_cast<mirror::Object*>(value));
231 } else {
232 new_frame->SetVReg(reg, kDeadValue);
233 }
Ian Rogers5cf98192014-05-29 21:31:50 -0700234 break;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000235 }
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200236 case kLongLoVReg:
Ian Rogers07140832014-09-30 15:43:59 -0700237 if (GetVRegKind(reg + 1, kinds) == kLongHiVReg) {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200238 // Treat it as a "long" register pair.
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000239 uint64_t value = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700240 if (GetVRegPair(m, reg, kLongLoVReg, kLongHiVReg, &value)) {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000241 new_frame->SetVRegLong(reg, value);
242 } else {
243 new_frame->SetVRegLong(reg, kLongDeadValue);
244 }
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200245 } else {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000246 uint32_t value = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700247 if (GetVReg(m, reg, kind, &value)) {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000248 new_frame->SetVReg(reg, value);
249 } else {
250 new_frame->SetVReg(reg, kDeadValue);
251 }
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200252 }
253 break;
254 case kLongHiVReg:
Ian Rogers07140832014-09-30 15:43:59 -0700255 if (GetVRegKind(reg - 1, kinds) == kLongLoVReg) {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200256 // Nothing to do: we treated it as a "long" register pair.
257 } else {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000258 uint32_t value = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700259 if (GetVReg(m, reg, kind, &value)) {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000260 new_frame->SetVReg(reg, value);
261 } else {
262 new_frame->SetVReg(reg, kDeadValue);
263 }
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200264 }
265 break;
266 case kDoubleLoVReg:
Ian Rogers07140832014-09-30 15:43:59 -0700267 if (GetVRegKind(reg + 1, kinds) == kDoubleHiVReg) {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000268 uint64_t value = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700269 if (GetVRegPair(m, reg, kDoubleLoVReg, kDoubleHiVReg, &value)) {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000270 // Treat it as a "double" register pair.
271 new_frame->SetVRegLong(reg, value);
272 } else {
273 new_frame->SetVRegLong(reg, kLongDeadValue);
274 }
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200275 } else {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000276 uint32_t value = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700277 if (GetVReg(m, reg, kind, &value)) {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000278 new_frame->SetVReg(reg, value);
279 } else {
280 new_frame->SetVReg(reg, kDeadValue);
281 }
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200282 }
283 break;
284 case kDoubleHiVReg:
Ian Rogers07140832014-09-30 15:43:59 -0700285 if (GetVRegKind(reg - 1, kinds) == kDoubleLoVReg) {
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200286 // Nothing to do: we treated it as a "double" register pair.
287 } else {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000288 uint32_t value = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700289 if (GetVReg(m, reg, kind, &value)) {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000290 new_frame->SetVReg(reg, value);
291 } else {
292 new_frame->SetVReg(reg, kDeadValue);
293 }
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200294 }
295 break;
Ian Rogers5cf98192014-05-29 21:31:50 -0700296 default:
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000297 uint32_t value = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700298 if (GetVReg(m, reg, kind, &value)) {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000299 new_frame->SetVReg(reg, value);
300 } else {
301 new_frame->SetVReg(reg, kDeadValue);
302 }
Ian Rogers5cf98192014-05-29 21:31:50 -0700303 break;
304 }
305 }
306 if (prev_shadow_frame_ != nullptr) {
307 prev_shadow_frame_->SetLink(new_frame);
308 } else {
309 self_->SetDeoptimizationShadowFrame(new_frame);
310 }
Andreas Gampe2e04bb22015-02-10 15:37:27 -0800311 self_->ClearShadowFrameUnderConstruction();
Ian Rogers5cf98192014-05-29 21:31:50 -0700312 prev_shadow_frame_ = new_frame;
313 return true;
314 }
315
316 Thread* const self_;
317 QuickExceptionHandler* const exception_handler_;
318 ShadowFrame* prev_shadow_frame_;
319
320 DISALLOW_COPY_AND_ASSIGN(DeoptimizeStackVisitor);
321};
322
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200323void QuickExceptionHandler::DeoptimizeStack() {
324 DCHECK(is_deoptimization_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700325 if (kDebugExceptionDelivery) {
326 self_->DumpStack(LOG(INFO) << "Deoptimizing: ");
327 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200328
329 DeoptimizeStackVisitor visitor(self_, context_, this);
330 visitor.WalkStack(true);
331
332 // Restore deoptimization exception
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000333 self_->SetException(Thread::GetDeoptimizationException());
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100334}
335
336// Unwinds all instrumentation stack frame prior to catch handler or upcall.
337class InstrumentationStackVisitor : public StackVisitor {
338 public:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700339 InstrumentationStackVisitor(Thread* self, size_t frame_depth)
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100340 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100341 : StackVisitor(self, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
Ian Rogerscf7f1912014-10-22 22:06:39 -0700342 frame_depth_(frame_depth),
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100343 instrumentation_frames_to_pop_(0) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700344 CHECK_NE(frame_depth_, kInvalidFrameDepth);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100345 }
346
347 bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700348 size_t current_frame_depth = GetFrameDepth();
349 if (current_frame_depth < frame_depth_) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100350 CHECK(GetMethod() != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700351 if (UNLIKELY(reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == GetReturnPc())) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100352 if (!IsInInlinedFrame()) {
353 // We do not count inlined frames, because we do not instrument them. The reason we
354 // include them in the stack walking is the check against `frame_depth_`, which is
355 // given to us by a visitor that visits inlined frames.
356 ++instrumentation_frames_to_pop_;
357 }
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100358 }
359 return true;
360 } else {
361 // We reached the frame of the catch handler or the upcall.
362 return false;
363 }
364 }
365
366 size_t GetInstrumentationFramesToPop() const {
367 return instrumentation_frames_to_pop_;
368 }
369
370 private:
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700371 const size_t frame_depth_;
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100372 size_t instrumentation_frames_to_pop_;
373
374 DISALLOW_COPY_AND_ASSIGN(InstrumentationStackVisitor);
375};
376
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200377void QuickExceptionHandler::UpdateInstrumentationStack() {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100378 if (method_tracing_active_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700379 InstrumentationStackVisitor visitor(self_, handler_frame_depth_);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100380 visitor.WalkStack(true);
381
382 size_t instrumentation_frames_to_pop = visitor.GetInstrumentationFramesToPop();
383 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
384 for (size_t i = 0; i < instrumentation_frames_to_pop; ++i) {
385 instrumentation->PopMethodForUnwind(self_, is_deoptimization_);
386 }
387 }
388}
389
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200390void QuickExceptionHandler::DoLongJump() {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100391 // Place context back on thread so it will be available when we continue.
392 self_->ReleaseLongJumpContext(context_);
393 context_->SetSP(reinterpret_cast<uintptr_t>(handler_quick_frame_));
394 CHECK_NE(handler_quick_frame_pc_, 0u);
395 context_->SetPC(handler_quick_frame_pc_);
396 context_->SmashCallerSaves();
397 context_->DoLongJump();
Andreas Gampe794ad762015-02-23 08:12:24 -0800398 UNREACHABLE();
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100399}
400
401} // namespace art