blob: eedf83f6df40c0d136bf2f90f9ef0bde6181ffc8 [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#ifndef ART_RUNTIME_QUICK_EXCEPTION_HANDLER_H_
18#define ART_RUNTIME_QUICK_EXCEPTION_HANDLER_H_
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010019
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020020#include "base/logging.h"
Andreas Gampe794ad762015-02-23 08:12:24 -080021#include "base/macros.h"
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020022#include "base/mutex.h"
Andreas Gampecf4035a2014-05-28 22:43:01 -070023#include "stack.h" // StackReference
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010024
25namespace art {
26
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020027namespace mirror {
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020028class Throwable;
29} // namespace mirror
Mathieu Chartiere401d142015-04-22 13:56:20 -070030class ArtMethod;
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020031class Context;
32class Thread;
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020033class ShadowFrame;
34
Elliott Hughes956af0f2014-12-11 14:34:28 -080035// Manages exception delivery for Quick backend.
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020036class QuickExceptionHandler {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010037 public:
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020038 QuickExceptionHandler(Thread* self, bool is_deoptimization)
Mathieu Chartier90443472015-07-16 20:32:27 -070039 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010040
Andreas Gampe65b798e2015-04-06 09:35:22 -070041 NO_RETURN ~QuickExceptionHandler() {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010042 LOG(FATAL) << "UNREACHABLE"; // Expected to take long jump.
Ian Rogers2c4257b2014-10-24 14:20:06 -070043 UNREACHABLE();
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010044 }
45
Sebastien Hertz520633b2015-09-08 17:03:36 +020046 // Find the catch handler for the given exception.
Mathieu Chartier90443472015-07-16 20:32:27 -070047 void FindCatch(mirror::Throwable* exception) SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz520633b2015-09-08 17:03:36 +020048
49 // Deoptimize the stack to the upcall. For every compiled frame, we create a "copy"
50 // shadow frame that will be executed with the interpreter.
Mathieu Chartier90443472015-07-16 20:32:27 -070051 void DeoptimizeStack() SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampe639bdd12015-06-03 11:22:45 -070052 void DeoptimizeSingleFrame() SHARED_REQUIRES(Locks::mutator_lock_);
53 void DeoptimizeSingleFrameArchDependentFixup() SHARED_REQUIRES(Locks::mutator_lock_);
54
Sebastien Hertz520633b2015-09-08 17:03:36 +020055 // Update the instrumentation stack by removing all methods that will be unwound
56 // by the exception being thrown.
Mathieu Chartier90443472015-07-16 20:32:27 -070057 void UpdateInstrumentationStack() SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz520633b2015-09-08 17:03:36 +020058
David Brazdil77a48ae2015-09-15 12:34:04 +000059 // Set up environment before delivering an exception to optimized code.
60 void SetCatchEnvironmentForOptimizedHandler(StackVisitor* stack_visitor)
61 SHARED_REQUIRES(Locks::mutator_lock_);
62
Sebastien Hertz520633b2015-09-08 17:03:36 +020063 // Long jump either to a catch handler or to the upcall.
Andreas Gampe639bdd12015-06-03 11:22:45 -070064 NO_RETURN void DoLongJump(bool smash_caller_saves = true) SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010065
Mathieu Chartiere401d142015-04-22 13:56:20 -070066 void SetHandlerQuickFrame(ArtMethod** handler_quick_frame) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010067 handler_quick_frame_ = handler_quick_frame;
68 }
69
70 void SetHandlerQuickFramePc(uintptr_t handler_quick_frame_pc) {
71 handler_quick_frame_pc_ = handler_quick_frame_pc;
72 }
73
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010074 void SetHandlerMethodHeader(const OatQuickMethodHeader* handler_method_header) {
75 handler_method_header_ = handler_method_header;
76 }
77
Andreas Gampe639bdd12015-06-03 11:22:45 -070078 void SetHandlerQuickArg0(uintptr_t handler_quick_arg0) {
79 handler_quick_arg0_ = handler_quick_arg0;
80 }
81
Mathieu Chartiere401d142015-04-22 13:56:20 -070082 ArtMethod* GetHandlerMethod() const {
Ian Rogers5cf98192014-05-29 21:31:50 -070083 return handler_method_;
84 }
85
Mathieu Chartiere401d142015-04-22 13:56:20 -070086 void SetHandlerMethod(ArtMethod* handler_quick_method) {
Ian Rogers5cf98192014-05-29 21:31:50 -070087 handler_method_ = handler_quick_method;
88 }
89
90 uint32_t GetHandlerDexPc() const {
91 return handler_dex_pc_;
92 }
93
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010094 void SetHandlerDexPc(uint32_t dex_pc) {
95 handler_dex_pc_ = dex_pc;
96 }
97
98 void SetClearException(bool clear_exception) {
99 clear_exception_ = clear_exception;
100 }
101
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700102 void SetHandlerFrameDepth(size_t frame_depth) {
103 handler_frame_depth_ = frame_depth;
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100104 }
105
Andreas Gampe639bdd12015-06-03 11:22:45 -0700106 // Walk the stack frames of the given thread, printing out non-runtime methods with their types
107 // of frames. Helps to verify that single-frame deopt really only deopted one frame.
108 static void DumpFramesWithType(Thread* self, bool details = false)
109 SHARED_REQUIRES(Locks::mutator_lock_);
110
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100111 private:
112 Thread* const self_;
113 Context* const context_;
Sebastien Hertz520633b2015-09-08 17:03:36 +0200114 // Should we deoptimize the stack?
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100115 const bool is_deoptimization_;
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100116 // Is method tracing active?
Sebastien Hertz520633b2015-09-08 17:03:36 +0200117 const bool method_tracing_active_;
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100118 // Quick frame with found handler or last frame if no handler found.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700119 ArtMethod** handler_quick_frame_;
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100120 // PC to branch to for the handler.
121 uintptr_t handler_quick_frame_pc_;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100122 // Quick code of the handler.
123 const OatQuickMethodHeader* handler_method_header_;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700124 // The value for argument 0.
125 uintptr_t handler_quick_arg0_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700126 // The handler method to report to the debugger.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700127 ArtMethod* handler_method_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700128 // The handler's dex PC, zero implies an uncaught exception.
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100129 uint32_t handler_dex_pc_;
130 // Should the exception be cleared as the catch block has no move-exception?
131 bool clear_exception_;
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700132 // Frame depth of the catch handler or the upcall.
133 size_t handler_frame_depth_;
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100134
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200135 DISALLOW_COPY_AND_ASSIGN(QuickExceptionHandler);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100136};
137
138} // namespace art
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200139#endif // ART_RUNTIME_QUICK_EXCEPTION_HANDLER_H_