Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
| 17 | #include "instrumentation.h" |
| 18 | |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 19 | #include "android-base/macros.h" |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 20 | #include "art_method-inl.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 21 | #include "base/enums.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 22 | #include "class_linker-inl.h" |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 23 | #include "common_runtime_test.h" |
| 24 | #include "common_throws.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 25 | #include "dex/dex_file.h" |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 26 | #include "gc/scoped_gc_critical_section.h" |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 27 | #include "handle_scope-inl.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 28 | #include "jni/jni_internal.h" |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 29 | #include "jvalue.h" |
| 30 | #include "runtime.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 31 | #include "scoped_thread_state_change-inl.h" |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 32 | #include "interpreter/shadow_frame.h" |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 33 | #include "thread-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 34 | #include "thread_list.h" |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 35 | #include "well_known_classes.h" |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 36 | |
| 37 | namespace art { |
| 38 | namespace instrumentation { |
| 39 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 40 | class TestInstrumentationListener final : public instrumentation::InstrumentationListener { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 41 | public: |
| 42 | TestInstrumentationListener() |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 43 | : received_method_enter_event(false), |
| 44 | received_method_exit_event(false), |
| 45 | received_method_exit_object_event(false), |
| 46 | received_method_unwind_event(false), |
| 47 | received_dex_pc_moved_event(false), |
| 48 | received_field_read_event(false), |
| 49 | received_field_written_event(false), |
| 50 | received_field_written_object_event(false), |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 51 | received_exception_thrown_event(false), |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 52 | received_exception_handled_event(false), |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 53 | received_branch_event(false), |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 54 | received_watched_frame_pop(false) {} |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 55 | |
| 56 | virtual ~TestInstrumentationListener() {} |
| 57 | |
| 58 | void MethodEntered(Thread* thread ATTRIBUTE_UNUSED, |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 59 | Handle<mirror::Object> this_object ATTRIBUTE_UNUSED, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 60 | ArtMethod* method ATTRIBUTE_UNUSED, |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 61 | uint32_t dex_pc ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 62 | override REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 63 | received_method_enter_event = true; |
| 64 | } |
| 65 | |
| 66 | void MethodExited(Thread* thread ATTRIBUTE_UNUSED, |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 67 | Handle<mirror::Object> this_object ATTRIBUTE_UNUSED, |
| 68 | ArtMethod* method ATTRIBUTE_UNUSED, |
| 69 | uint32_t dex_pc ATTRIBUTE_UNUSED, |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 70 | instrumentation::OptionalFrame frame ATTRIBUTE_UNUSED, |
| 71 | MutableHandle<mirror::Object>& return_value ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 72 | override REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 73 | received_method_exit_object_event = true; |
| 74 | } |
| 75 | |
| 76 | void MethodExited(Thread* thread ATTRIBUTE_UNUSED, |
| 77 | Handle<mirror::Object> this_object ATTRIBUTE_UNUSED, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 78 | ArtMethod* method ATTRIBUTE_UNUSED, |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 79 | uint32_t dex_pc ATTRIBUTE_UNUSED, |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 80 | instrumentation::OptionalFrame frame ATTRIBUTE_UNUSED, |
| 81 | JValue& return_value ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 82 | override REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 83 | received_method_exit_event = true; |
| 84 | } |
| 85 | |
| 86 | void MethodUnwind(Thread* thread ATTRIBUTE_UNUSED, |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 87 | Handle<mirror::Object> this_object ATTRIBUTE_UNUSED, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 88 | ArtMethod* method ATTRIBUTE_UNUSED, |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 89 | uint32_t dex_pc ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 90 | override REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 91 | received_method_unwind_event = true; |
| 92 | } |
| 93 | |
| 94 | void DexPcMoved(Thread* thread ATTRIBUTE_UNUSED, |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 95 | Handle<mirror::Object> this_object ATTRIBUTE_UNUSED, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 96 | ArtMethod* method ATTRIBUTE_UNUSED, |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 97 | uint32_t new_dex_pc ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 98 | override REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 99 | received_dex_pc_moved_event = true; |
| 100 | } |
| 101 | |
| 102 | void FieldRead(Thread* thread ATTRIBUTE_UNUSED, |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 103 | Handle<mirror::Object> this_object ATTRIBUTE_UNUSED, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 104 | ArtMethod* method ATTRIBUTE_UNUSED, |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 105 | uint32_t dex_pc ATTRIBUTE_UNUSED, |
| 106 | ArtField* field ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 107 | override REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 108 | received_field_read_event = true; |
| 109 | } |
| 110 | |
| 111 | void FieldWritten(Thread* thread ATTRIBUTE_UNUSED, |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 112 | Handle<mirror::Object> this_object ATTRIBUTE_UNUSED, |
| 113 | ArtMethod* method ATTRIBUTE_UNUSED, |
| 114 | uint32_t dex_pc ATTRIBUTE_UNUSED, |
| 115 | ArtField* field ATTRIBUTE_UNUSED, |
| 116 | Handle<mirror::Object> field_value ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 117 | override REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 118 | received_field_written_object_event = true; |
| 119 | } |
| 120 | |
| 121 | void FieldWritten(Thread* thread ATTRIBUTE_UNUSED, |
| 122 | Handle<mirror::Object> this_object ATTRIBUTE_UNUSED, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 123 | ArtMethod* method ATTRIBUTE_UNUSED, |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 124 | uint32_t dex_pc ATTRIBUTE_UNUSED, |
| 125 | ArtField* field ATTRIBUTE_UNUSED, |
| 126 | const JValue& field_value ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 127 | override REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 128 | received_field_written_event = true; |
| 129 | } |
| 130 | |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 131 | void ExceptionThrown(Thread* thread ATTRIBUTE_UNUSED, |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 132 | Handle<mirror::Throwable> exception_object ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 133 | override REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 134 | received_exception_thrown_event = true; |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 135 | } |
| 136 | |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 137 | void ExceptionHandled(Thread* self ATTRIBUTE_UNUSED, |
| 138 | Handle<mirror::Throwable> throwable ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 139 | override REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 140 | received_exception_handled_event = true; |
| 141 | } |
| 142 | |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 143 | void Branch(Thread* thread ATTRIBUTE_UNUSED, |
| 144 | ArtMethod* method ATTRIBUTE_UNUSED, |
| 145 | uint32_t dex_pc ATTRIBUTE_UNUSED, |
| 146 | int32_t dex_pc_offset ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 147 | override REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 148 | received_branch_event = true; |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 149 | } |
| 150 | |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 151 | void WatchedFramePop(Thread* thread ATTRIBUTE_UNUSED, const ShadowFrame& frame ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 152 | override REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 153 | received_watched_frame_pop = true; |
| 154 | } |
| 155 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 156 | void Reset() { |
| 157 | received_method_enter_event = false; |
| 158 | received_method_exit_event = false; |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 159 | received_method_exit_object_event = false; |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 160 | received_method_unwind_event = false; |
| 161 | received_dex_pc_moved_event = false; |
| 162 | received_field_read_event = false; |
| 163 | received_field_written_event = false; |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 164 | received_field_written_object_event = false; |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 165 | received_exception_thrown_event = false; |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 166 | received_exception_handled_event = false; |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 167 | received_branch_event = false; |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 168 | received_watched_frame_pop = false; |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | bool received_method_enter_event; |
| 172 | bool received_method_exit_event; |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 173 | bool received_method_exit_object_event; |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 174 | bool received_method_unwind_event; |
| 175 | bool received_dex_pc_moved_event; |
| 176 | bool received_field_read_event; |
| 177 | bool received_field_written_event; |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 178 | bool received_field_written_object_event; |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 179 | bool received_exception_thrown_event; |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 180 | bool received_exception_handled_event; |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 181 | bool received_branch_event; |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 182 | bool received_watched_frame_pop; |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 183 | |
| 184 | private: |
| 185 | DISALLOW_COPY_AND_ASSIGN(TestInstrumentationListener); |
| 186 | }; |
| 187 | |
| 188 | class InstrumentationTest : public CommonRuntimeTest { |
| 189 | public: |
| 190 | // Unique keys used to test Instrumentation::ConfigureStubs. |
| 191 | static constexpr const char* kClientOneKey = "TestClient1"; |
| 192 | static constexpr const char* kClientTwoKey = "TestClient2"; |
| 193 | |
| 194 | void CheckConfigureStubs(const char* key, Instrumentation::InstrumentationLevel level) { |
| 195 | ScopedObjectAccess soa(Thread::Current()); |
| 196 | instrumentation::Instrumentation* instr = Runtime::Current()->GetInstrumentation(); |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 197 | ScopedThreadSuspension sts(soa.Self(), kSuspended); |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 198 | gc::ScopedGCCriticalSection gcs(soa.Self(), |
| 199 | gc::kGcCauseInstrumentation, |
| 200 | gc::kCollectorTypeInstrumentation); |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 201 | ScopedSuspendAll ssa("Instrumentation::ConfigureStubs"); |
| 202 | instr->ConfigureStubs(key, level); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | Instrumentation::InstrumentationLevel GetCurrentInstrumentationLevel() { |
| 206 | return Runtime::Current()->GetInstrumentation()->GetCurrentInstrumentationLevel(); |
| 207 | } |
| 208 | |
| 209 | size_t GetInstrumentationUserCount() { |
| 210 | ScopedObjectAccess soa(Thread::Current()); |
| 211 | return Runtime::Current()->GetInstrumentation()->requested_instrumentation_levels_.size(); |
| 212 | } |
| 213 | |
| 214 | void TestEvent(uint32_t instrumentation_event) { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 215 | TestEvent(instrumentation_event, nullptr, nullptr, false); |
| 216 | } |
| 217 | |
| 218 | void TestEvent(uint32_t instrumentation_event, |
| 219 | ArtMethod* event_method, |
| 220 | ArtField* event_field, |
| 221 | bool with_object) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 222 | ScopedObjectAccess soa(Thread::Current()); |
| 223 | instrumentation::Instrumentation* instr = Runtime::Current()->GetInstrumentation(); |
| 224 | TestInstrumentationListener listener; |
| 225 | { |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 226 | ScopedThreadSuspension sts(soa.Self(), kSuspended); |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 227 | ScopedSuspendAll ssa("Add instrumentation listener"); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 228 | instr->AddListener(&listener, instrumentation_event); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 229 | } |
| 230 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 231 | mirror::Object* const event_obj = nullptr; |
| 232 | const uint32_t event_dex_pc = 0; |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 233 | ShadowFrameAllocaUniquePtr test_frame = CREATE_SHADOW_FRAME(0, nullptr, event_method, 0); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 234 | |
| 235 | // Check the listener is registered and is notified of the event. |
| 236 | EXPECT_TRUE(HasEventListener(instr, instrumentation_event)); |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 237 | EXPECT_FALSE(DidListenerReceiveEvent(listener, instrumentation_event, with_object)); |
| 238 | ReportEvent(instr, |
| 239 | instrumentation_event, |
| 240 | soa.Self(), |
| 241 | event_method, |
| 242 | event_obj, |
| 243 | event_field, |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 244 | event_dex_pc, |
| 245 | *test_frame); |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 246 | EXPECT_TRUE(DidListenerReceiveEvent(listener, instrumentation_event, with_object)); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 247 | |
| 248 | listener.Reset(); |
| 249 | { |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 250 | ScopedThreadSuspension sts(soa.Self(), kSuspended); |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 251 | ScopedSuspendAll ssa("Remove instrumentation listener"); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 252 | instr->RemoveListener(&listener, instrumentation_event); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | // Check the listener is not registered and is not notified of the event. |
| 256 | EXPECT_FALSE(HasEventListener(instr, instrumentation_event)); |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 257 | EXPECT_FALSE(DidListenerReceiveEvent(listener, instrumentation_event, with_object)); |
| 258 | ReportEvent(instr, |
| 259 | instrumentation_event, |
| 260 | soa.Self(), |
| 261 | event_method, |
| 262 | event_obj, |
| 263 | event_field, |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 264 | event_dex_pc, |
| 265 | *test_frame); |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 266 | EXPECT_FALSE(DidListenerReceiveEvent(listener, instrumentation_event, with_object)); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 267 | } |
| 268 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 269 | void DeoptimizeMethod(Thread* self, ArtMethod* method, bool enable_deoptimization) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 270 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 271 | Runtime* runtime = Runtime::Current(); |
| 272 | instrumentation::Instrumentation* instrumentation = runtime->GetInstrumentation(); |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 273 | ScopedThreadSuspension sts(self, kSuspended); |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 274 | gc::ScopedGCCriticalSection gcs(self, |
| 275 | gc::kGcCauseInstrumentation, |
| 276 | gc::kCollectorTypeInstrumentation); |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 277 | ScopedSuspendAll ssa("Single method deoptimization"); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 278 | if (enable_deoptimization) { |
| 279 | instrumentation->EnableDeoptimization(); |
| 280 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 281 | instrumentation->Deoptimize(method); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 282 | } |
| 283 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 284 | void UndeoptimizeMethod(Thread* self, ArtMethod* method, |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 285 | const char* key, bool disable_deoptimization) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 286 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 287 | Runtime* runtime = Runtime::Current(); |
| 288 | instrumentation::Instrumentation* instrumentation = runtime->GetInstrumentation(); |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 289 | ScopedThreadSuspension sts(self, kSuspended); |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 290 | gc::ScopedGCCriticalSection gcs(self, |
| 291 | gc::kGcCauseInstrumentation, |
| 292 | gc::kCollectorTypeInstrumentation); |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 293 | ScopedSuspendAll ssa("Single method undeoptimization"); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 294 | instrumentation->Undeoptimize(method); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 295 | if (disable_deoptimization) { |
| 296 | instrumentation->DisableDeoptimization(key); |
| 297 | } |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | void DeoptimizeEverything(Thread* self, const char* key, bool enable_deoptimization) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 301 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 302 | Runtime* runtime = Runtime::Current(); |
| 303 | instrumentation::Instrumentation* instrumentation = runtime->GetInstrumentation(); |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 304 | ScopedThreadSuspension sts(self, kSuspended); |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 305 | gc::ScopedGCCriticalSection gcs(self, |
| 306 | gc::kGcCauseInstrumentation, |
| 307 | gc::kCollectorTypeInstrumentation); |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 308 | ScopedSuspendAll ssa("Full deoptimization"); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 309 | if (enable_deoptimization) { |
| 310 | instrumentation->EnableDeoptimization(); |
| 311 | } |
| 312 | instrumentation->DeoptimizeEverything(key); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | void UndeoptimizeEverything(Thread* self, const char* key, bool disable_deoptimization) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 316 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 317 | Runtime* runtime = Runtime::Current(); |
| 318 | instrumentation::Instrumentation* instrumentation = runtime->GetInstrumentation(); |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 319 | ScopedThreadSuspension sts(self, kSuspended); |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 320 | gc::ScopedGCCriticalSection gcs(self, |
| 321 | gc::kGcCauseInstrumentation, |
| 322 | gc::kCollectorTypeInstrumentation); |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 323 | ScopedSuspendAll ssa("Full undeoptimization"); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 324 | instrumentation->UndeoptimizeEverything(key); |
| 325 | if (disable_deoptimization) { |
| 326 | instrumentation->DisableDeoptimization(key); |
| 327 | } |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | void EnableMethodTracing(Thread* self, const char* key, bool needs_interpreter) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 331 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 332 | Runtime* runtime = Runtime::Current(); |
| 333 | instrumentation::Instrumentation* instrumentation = runtime->GetInstrumentation(); |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 334 | ScopedThreadSuspension sts(self, kSuspended); |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 335 | gc::ScopedGCCriticalSection gcs(self, |
| 336 | gc::kGcCauseInstrumentation, |
| 337 | gc::kCollectorTypeInstrumentation); |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 338 | ScopedSuspendAll ssa("EnableMethodTracing"); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 339 | instrumentation->EnableMethodTracing(key, needs_interpreter); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | void DisableMethodTracing(Thread* self, const char* key) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 343 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 344 | Runtime* runtime = Runtime::Current(); |
| 345 | instrumentation::Instrumentation* instrumentation = runtime->GetInstrumentation(); |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 346 | ScopedThreadSuspension sts(self, kSuspended); |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 347 | gc::ScopedGCCriticalSection gcs(self, |
| 348 | gc::kGcCauseInstrumentation, |
| 349 | gc::kCollectorTypeInstrumentation); |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 350 | ScopedSuspendAll ssa("EnableMethodTracing"); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 351 | instrumentation->DisableMethodTracing(key); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | private: |
| 355 | static bool HasEventListener(const instrumentation::Instrumentation* instr, uint32_t event_type) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 356 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 357 | switch (event_type) { |
| 358 | case instrumentation::Instrumentation::kMethodEntered: |
| 359 | return instr->HasMethodEntryListeners(); |
| 360 | case instrumentation::Instrumentation::kMethodExited: |
| 361 | return instr->HasMethodExitListeners(); |
| 362 | case instrumentation::Instrumentation::kMethodUnwind: |
| 363 | return instr->HasMethodUnwindListeners(); |
| 364 | case instrumentation::Instrumentation::kDexPcMoved: |
| 365 | return instr->HasDexPcListeners(); |
| 366 | case instrumentation::Instrumentation::kFieldRead: |
| 367 | return instr->HasFieldReadListeners(); |
| 368 | case instrumentation::Instrumentation::kFieldWritten: |
| 369 | return instr->HasFieldWriteListeners(); |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 370 | case instrumentation::Instrumentation::kExceptionThrown: |
| 371 | return instr->HasExceptionThrownListeners(); |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 372 | case instrumentation::Instrumentation::kExceptionHandled: |
| 373 | return instr->HasExceptionHandledListeners(); |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 374 | case instrumentation::Instrumentation::kBranch: |
| 375 | return instr->HasBranchListeners(); |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 376 | case instrumentation::Instrumentation::kWatchedFramePop: |
| 377 | return instr->HasWatchedFramePopListeners(); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 378 | default: |
| 379 | LOG(FATAL) << "Unknown instrumentation event " << event_type; |
| 380 | UNREACHABLE(); |
| 381 | } |
| 382 | } |
| 383 | |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 384 | static void ReportEvent(const instrumentation::Instrumentation* instr, |
| 385 | uint32_t event_type, |
| 386 | Thread* self, |
| 387 | ArtMethod* method, |
| 388 | mirror::Object* obj, |
| 389 | ArtField* field, |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 390 | uint32_t dex_pc, |
| 391 | const ShadowFrame& frame) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 392 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 393 | switch (event_type) { |
| 394 | case instrumentation::Instrumentation::kMethodEntered: |
| 395 | instr->MethodEnterEvent(self, obj, method, dex_pc); |
| 396 | break; |
| 397 | case instrumentation::Instrumentation::kMethodExited: { |
| 398 | JValue value; |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 399 | instr->MethodExitEvent(self, obj, method, dex_pc, {}, value); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 400 | break; |
| 401 | } |
| 402 | case instrumentation::Instrumentation::kMethodUnwind: |
| 403 | instr->MethodUnwindEvent(self, obj, method, dex_pc); |
| 404 | break; |
| 405 | case instrumentation::Instrumentation::kDexPcMoved: |
| 406 | instr->DexPcMovedEvent(self, obj, method, dex_pc); |
| 407 | break; |
| 408 | case instrumentation::Instrumentation::kFieldRead: |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 409 | instr->FieldReadEvent(self, obj, method, dex_pc, field); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 410 | break; |
| 411 | case instrumentation::Instrumentation::kFieldWritten: { |
| 412 | JValue value; |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 413 | instr->FieldWriteEvent(self, obj, method, dex_pc, field, value); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 414 | break; |
| 415 | } |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 416 | case instrumentation::Instrumentation::kExceptionThrown: { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 417 | ThrowArithmeticExceptionDivideByZero(); |
| 418 | mirror::Throwable* event_exception = self->GetException(); |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 419 | instr->ExceptionThrownEvent(self, event_exception); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 420 | self->ClearException(); |
| 421 | break; |
| 422 | } |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 423 | case instrumentation::Instrumentation::kBranch: |
| 424 | instr->Branch(self, method, dex_pc, -1); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 425 | break; |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 426 | case instrumentation::Instrumentation::kWatchedFramePop: |
| 427 | instr->WatchedFramePopped(self, frame); |
| 428 | break; |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 429 | case instrumentation::Instrumentation::kExceptionHandled: { |
| 430 | ThrowArithmeticExceptionDivideByZero(); |
| 431 | mirror::Throwable* event_exception = self->GetException(); |
| 432 | self->ClearException(); |
| 433 | instr->ExceptionHandledEvent(self, event_exception); |
| 434 | break; |
| 435 | } |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 436 | default: |
| 437 | LOG(FATAL) << "Unknown instrumentation event " << event_type; |
| 438 | UNREACHABLE(); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | static bool DidListenerReceiveEvent(const TestInstrumentationListener& listener, |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 443 | uint32_t event_type, |
| 444 | bool with_object) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 445 | switch (event_type) { |
| 446 | case instrumentation::Instrumentation::kMethodEntered: |
| 447 | return listener.received_method_enter_event; |
| 448 | case instrumentation::Instrumentation::kMethodExited: |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 449 | return (!with_object && listener.received_method_exit_event) || |
| 450 | (with_object && listener.received_method_exit_object_event); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 451 | case instrumentation::Instrumentation::kMethodUnwind: |
| 452 | return listener.received_method_unwind_event; |
| 453 | case instrumentation::Instrumentation::kDexPcMoved: |
| 454 | return listener.received_dex_pc_moved_event; |
| 455 | case instrumentation::Instrumentation::kFieldRead: |
| 456 | return listener.received_field_read_event; |
| 457 | case instrumentation::Instrumentation::kFieldWritten: |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 458 | return (!with_object && listener.received_field_written_event) || |
| 459 | (with_object && listener.received_field_written_object_event); |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 460 | case instrumentation::Instrumentation::kExceptionThrown: |
| 461 | return listener.received_exception_thrown_event; |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 462 | case instrumentation::Instrumentation::kExceptionHandled: |
| 463 | return listener.received_exception_handled_event; |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 464 | case instrumentation::Instrumentation::kBranch: |
| 465 | return listener.received_branch_event; |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 466 | case instrumentation::Instrumentation::kWatchedFramePop: |
| 467 | return listener.received_watched_frame_pop; |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 468 | default: |
| 469 | LOG(FATAL) << "Unknown instrumentation event " << event_type; |
| 470 | UNREACHABLE(); |
| 471 | } |
| 472 | } |
| 473 | }; |
| 474 | |
| 475 | TEST_F(InstrumentationTest, NoInstrumentation) { |
| 476 | ScopedObjectAccess soa(Thread::Current()); |
| 477 | instrumentation::Instrumentation* instr = Runtime::Current()->GetInstrumentation(); |
| 478 | ASSERT_NE(instr, nullptr); |
| 479 | |
| 480 | EXPECT_FALSE(instr->AreExitStubsInstalled()); |
| 481 | EXPECT_FALSE(instr->AreAllMethodsDeoptimized()); |
| 482 | EXPECT_FALSE(instr->IsActive()); |
| 483 | EXPECT_FALSE(instr->ShouldNotifyMethodEnterExitEvents()); |
| 484 | |
| 485 | // Test interpreter table is the default one. |
| 486 | EXPECT_EQ(instrumentation::kMainHandlerTable, instr->GetInterpreterHandlerTable()); |
| 487 | |
| 488 | // Check there is no registered listener. |
| 489 | EXPECT_FALSE(instr->HasDexPcListeners()); |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 490 | EXPECT_FALSE(instr->HasExceptionThrownListeners()); |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 491 | EXPECT_FALSE(instr->HasExceptionHandledListeners()); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 492 | EXPECT_FALSE(instr->HasFieldReadListeners()); |
| 493 | EXPECT_FALSE(instr->HasFieldWriteListeners()); |
| 494 | EXPECT_FALSE(instr->HasMethodEntryListeners()); |
| 495 | EXPECT_FALSE(instr->HasMethodExitListeners()); |
| 496 | EXPECT_FALSE(instr->IsActive()); |
| 497 | } |
| 498 | |
| 499 | // Test instrumentation listeners for each event. |
| 500 | TEST_F(InstrumentationTest, MethodEntryEvent) { |
Mingyao Yang | 2ee1790 | 2017-08-30 11:37:08 -0700 | [diff] [blame] | 501 | ScopedObjectAccess soa(Thread::Current()); |
| 502 | jobject class_loader = LoadDex("Instrumentation"); |
| 503 | Runtime* const runtime = Runtime::Current(); |
| 504 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 505 | StackHandleScope<1> hs(soa.Self()); |
| 506 | Handle<mirror::ClassLoader> loader(hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader))); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 507 | ObjPtr<mirror::Class> klass = class_linker->FindClass(soa.Self(), "LInstrumentation;", loader); |
Mingyao Yang | 2ee1790 | 2017-08-30 11:37:08 -0700 | [diff] [blame] | 508 | ASSERT_TRUE(klass != nullptr); |
| 509 | ArtMethod* method = |
| 510 | klass->FindClassMethod("returnReference", "()Ljava/lang/Object;", kRuntimePointerSize); |
| 511 | ASSERT_TRUE(method != nullptr); |
| 512 | ASSERT_TRUE(method->IsDirect()); |
| 513 | ASSERT_TRUE(method->GetDeclaringClass() == klass); |
| 514 | TestEvent(instrumentation::Instrumentation::kMethodEntered, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 515 | /*event_method=*/ method, |
| 516 | /*event_field=*/ nullptr, |
| 517 | /*with_object=*/ true); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 518 | } |
| 519 | |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 520 | TEST_F(InstrumentationTest, MethodExitObjectEvent) { |
| 521 | ScopedObjectAccess soa(Thread::Current()); |
| 522 | jobject class_loader = LoadDex("Instrumentation"); |
| 523 | Runtime* const runtime = Runtime::Current(); |
| 524 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 525 | StackHandleScope<1> hs(soa.Self()); |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 526 | MutableHandle<mirror::ClassLoader> loader( |
| 527 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader))); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 528 | ObjPtr<mirror::Class> klass = class_linker->FindClass(soa.Self(), "LInstrumentation;", loader); |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 529 | ASSERT_TRUE(klass != nullptr); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 530 | ArtMethod* method = |
| 531 | klass->FindClassMethod("returnReference", "()Ljava/lang/Object;", kRuntimePointerSize); |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 532 | ASSERT_TRUE(method != nullptr); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 533 | ASSERT_TRUE(method->IsDirect()); |
| 534 | ASSERT_TRUE(method->GetDeclaringClass() == klass); |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 535 | TestEvent(instrumentation::Instrumentation::kMethodExited, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 536 | /*event_method=*/ method, |
| 537 | /*event_field=*/ nullptr, |
| 538 | /*with_object=*/ true); |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | TEST_F(InstrumentationTest, MethodExitPrimEvent) { |
| 542 | ScopedObjectAccess soa(Thread::Current()); |
| 543 | jobject class_loader = LoadDex("Instrumentation"); |
| 544 | Runtime* const runtime = Runtime::Current(); |
| 545 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 546 | StackHandleScope<1> hs(soa.Self()); |
| 547 | Handle<mirror::ClassLoader> loader(hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader))); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 548 | ObjPtr<mirror::Class> klass = class_linker->FindClass(soa.Self(), "LInstrumentation;", loader); |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 549 | ASSERT_TRUE(klass != nullptr); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 550 | ArtMethod* method = klass->FindClassMethod("returnPrimitive", "()I", kRuntimePointerSize); |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 551 | ASSERT_TRUE(method != nullptr); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 552 | ASSERT_TRUE(method->IsDirect()); |
| 553 | ASSERT_TRUE(method->GetDeclaringClass() == klass); |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 554 | TestEvent(instrumentation::Instrumentation::kMethodExited, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 555 | /*event_method=*/ method, |
| 556 | /*event_field=*/ nullptr, |
| 557 | /*with_object=*/ false); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | TEST_F(InstrumentationTest, MethodUnwindEvent) { |
| 561 | TestEvent(instrumentation::Instrumentation::kMethodUnwind); |
| 562 | } |
| 563 | |
| 564 | TEST_F(InstrumentationTest, DexPcMovedEvent) { |
| 565 | TestEvent(instrumentation::Instrumentation::kDexPcMoved); |
| 566 | } |
| 567 | |
| 568 | TEST_F(InstrumentationTest, FieldReadEvent) { |
| 569 | TestEvent(instrumentation::Instrumentation::kFieldRead); |
| 570 | } |
| 571 | |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 572 | TEST_F(InstrumentationTest, WatchedFramePop) { |
| 573 | TestEvent(instrumentation::Instrumentation::kWatchedFramePop); |
| 574 | } |
| 575 | |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 576 | TEST_F(InstrumentationTest, FieldWriteObjectEvent) { |
| 577 | ScopedObjectAccess soa(Thread::Current()); |
| 578 | jobject class_loader = LoadDex("Instrumentation"); |
| 579 | Runtime* const runtime = Runtime::Current(); |
| 580 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 581 | StackHandleScope<1> hs(soa.Self()); |
| 582 | Handle<mirror::ClassLoader> loader(hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader))); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 583 | ObjPtr<mirror::Class> klass = class_linker->FindClass(soa.Self(), "LInstrumentation;", loader); |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 584 | ASSERT_TRUE(klass != nullptr); |
| 585 | ArtField* field = klass->FindDeclaredStaticField("referenceField", "Ljava/lang/Object;"); |
| 586 | ASSERT_TRUE(field != nullptr); |
| 587 | |
| 588 | TestEvent(instrumentation::Instrumentation::kFieldWritten, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 589 | /*event_method=*/ nullptr, |
| 590 | /*event_field=*/ field, |
| 591 | /*with_object=*/ true); |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | TEST_F(InstrumentationTest, FieldWritePrimEvent) { |
| 595 | ScopedObjectAccess soa(Thread::Current()); |
| 596 | jobject class_loader = LoadDex("Instrumentation"); |
| 597 | Runtime* const runtime = Runtime::Current(); |
| 598 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 599 | StackHandleScope<1> hs(soa.Self()); |
| 600 | Handle<mirror::ClassLoader> loader(hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader))); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 601 | ObjPtr<mirror::Class> klass = class_linker->FindClass(soa.Self(), "LInstrumentation;", loader); |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 602 | ASSERT_TRUE(klass != nullptr); |
| 603 | ArtField* field = klass->FindDeclaredStaticField("primitiveField", "I"); |
| 604 | ASSERT_TRUE(field != nullptr); |
| 605 | |
| 606 | TestEvent(instrumentation::Instrumentation::kFieldWritten, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 607 | /*event_method=*/ nullptr, |
| 608 | /*event_field=*/ field, |
| 609 | /*with_object=*/ false); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 610 | } |
| 611 | |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 612 | TEST_F(InstrumentationTest, ExceptionHandledEvent) { |
| 613 | TestEvent(instrumentation::Instrumentation::kExceptionHandled); |
| 614 | } |
| 615 | |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 616 | TEST_F(InstrumentationTest, ExceptionThrownEvent) { |
| 617 | TestEvent(instrumentation::Instrumentation::kExceptionThrown); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 618 | } |
| 619 | |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 620 | TEST_F(InstrumentationTest, BranchEvent) { |
| 621 | TestEvent(instrumentation::Instrumentation::kBranch); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | TEST_F(InstrumentationTest, DeoptimizeDirectMethod) { |
| 625 | ScopedObjectAccess soa(Thread::Current()); |
| 626 | jobject class_loader = LoadDex("Instrumentation"); |
| 627 | Runtime* const runtime = Runtime::Current(); |
| 628 | instrumentation::Instrumentation* instr = runtime->GetInstrumentation(); |
| 629 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 630 | StackHandleScope<1> hs(soa.Self()); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 631 | Handle<mirror::ClassLoader> loader(hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader))); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 632 | ObjPtr<mirror::Class> klass = class_linker->FindClass(soa.Self(), "LInstrumentation;", loader); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 633 | ASSERT_TRUE(klass != nullptr); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 634 | ArtMethod* method_to_deoptimize = |
| 635 | klass->FindClassMethod("instanceMethod", "()V", kRuntimePointerSize); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 636 | ASSERT_TRUE(method_to_deoptimize != nullptr); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 637 | ASSERT_TRUE(method_to_deoptimize->IsDirect()); |
| 638 | ASSERT_TRUE(method_to_deoptimize->GetDeclaringClass() == klass); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 639 | |
| 640 | EXPECT_FALSE(instr->AreAllMethodsDeoptimized()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 641 | EXPECT_FALSE(instr->IsDeoptimized(method_to_deoptimize)); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 642 | |
| 643 | DeoptimizeMethod(soa.Self(), method_to_deoptimize, true); |
| 644 | |
| 645 | EXPECT_FALSE(instr->AreAllMethodsDeoptimized()); |
| 646 | EXPECT_TRUE(instr->AreExitStubsInstalled()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 647 | EXPECT_TRUE(instr->IsDeoptimized(method_to_deoptimize)); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 648 | |
| 649 | constexpr const char* instrumentation_key = "DeoptimizeDirectMethod"; |
| 650 | UndeoptimizeMethod(soa.Self(), method_to_deoptimize, instrumentation_key, true); |
| 651 | |
| 652 | EXPECT_FALSE(instr->AreAllMethodsDeoptimized()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 653 | EXPECT_FALSE(instr->IsDeoptimized(method_to_deoptimize)); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | TEST_F(InstrumentationTest, FullDeoptimization) { |
| 657 | ScopedObjectAccess soa(Thread::Current()); |
| 658 | Runtime* const runtime = Runtime::Current(); |
| 659 | instrumentation::Instrumentation* instr = runtime->GetInstrumentation(); |
| 660 | EXPECT_FALSE(instr->AreAllMethodsDeoptimized()); |
| 661 | |
| 662 | constexpr const char* instrumentation_key = "FullDeoptimization"; |
| 663 | DeoptimizeEverything(soa.Self(), instrumentation_key, true); |
| 664 | |
| 665 | EXPECT_TRUE(instr->AreAllMethodsDeoptimized()); |
| 666 | EXPECT_TRUE(instr->AreExitStubsInstalled()); |
| 667 | |
| 668 | UndeoptimizeEverything(soa.Self(), instrumentation_key, true); |
| 669 | |
| 670 | EXPECT_FALSE(instr->AreAllMethodsDeoptimized()); |
| 671 | } |
| 672 | |
| 673 | TEST_F(InstrumentationTest, MixedDeoptimization) { |
| 674 | ScopedObjectAccess soa(Thread::Current()); |
| 675 | jobject class_loader = LoadDex("Instrumentation"); |
| 676 | Runtime* const runtime = Runtime::Current(); |
| 677 | instrumentation::Instrumentation* instr = runtime->GetInstrumentation(); |
| 678 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 679 | StackHandleScope<1> hs(soa.Self()); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 680 | Handle<mirror::ClassLoader> loader(hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader))); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 681 | ObjPtr<mirror::Class> klass = class_linker->FindClass(soa.Self(), "LInstrumentation;", loader); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 682 | ASSERT_TRUE(klass != nullptr); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 683 | ArtMethod* method_to_deoptimize = |
| 684 | klass->FindClassMethod("instanceMethod", "()V", kRuntimePointerSize); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 685 | ASSERT_TRUE(method_to_deoptimize != nullptr); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 686 | ASSERT_TRUE(method_to_deoptimize->IsDirect()); |
| 687 | ASSERT_TRUE(method_to_deoptimize->GetDeclaringClass() == klass); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 688 | |
| 689 | EXPECT_FALSE(instr->AreAllMethodsDeoptimized()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 690 | EXPECT_FALSE(instr->IsDeoptimized(method_to_deoptimize)); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 691 | |
| 692 | DeoptimizeMethod(soa.Self(), method_to_deoptimize, true); |
| 693 | // Deoptimizing a method does not change instrumentation level. |
| 694 | EXPECT_EQ(Instrumentation::InstrumentationLevel::kInstrumentNothing, |
| 695 | GetCurrentInstrumentationLevel()); |
| 696 | EXPECT_FALSE(instr->AreAllMethodsDeoptimized()); |
| 697 | EXPECT_TRUE(instr->AreExitStubsInstalled()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 698 | EXPECT_TRUE(instr->IsDeoptimized(method_to_deoptimize)); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 699 | |
| 700 | constexpr const char* instrumentation_key = "MixedDeoptimization"; |
| 701 | DeoptimizeEverything(soa.Self(), instrumentation_key, false); |
| 702 | EXPECT_EQ(Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter, |
| 703 | GetCurrentInstrumentationLevel()); |
| 704 | EXPECT_TRUE(instr->AreAllMethodsDeoptimized()); |
| 705 | EXPECT_TRUE(instr->AreExitStubsInstalled()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 706 | EXPECT_TRUE(instr->IsDeoptimized(method_to_deoptimize)); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 707 | |
| 708 | UndeoptimizeEverything(soa.Self(), instrumentation_key, false); |
| 709 | EXPECT_EQ(Instrumentation::InstrumentationLevel::kInstrumentNothing, |
| 710 | GetCurrentInstrumentationLevel()); |
| 711 | EXPECT_FALSE(instr->AreAllMethodsDeoptimized()); |
| 712 | EXPECT_TRUE(instr->AreExitStubsInstalled()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 713 | EXPECT_TRUE(instr->IsDeoptimized(method_to_deoptimize)); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 714 | |
| 715 | UndeoptimizeMethod(soa.Self(), method_to_deoptimize, instrumentation_key, true); |
| 716 | EXPECT_EQ(Instrumentation::InstrumentationLevel::kInstrumentNothing, |
| 717 | GetCurrentInstrumentationLevel()); |
| 718 | EXPECT_FALSE(instr->AreAllMethodsDeoptimized()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 719 | EXPECT_FALSE(instr->IsDeoptimized(method_to_deoptimize)); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | TEST_F(InstrumentationTest, MethodTracing_Interpreter) { |
| 723 | ScopedObjectAccess soa(Thread::Current()); |
| 724 | Runtime* const runtime = Runtime::Current(); |
| 725 | instrumentation::Instrumentation* instr = runtime->GetInstrumentation(); |
| 726 | EXPECT_FALSE(instr->AreAllMethodsDeoptimized()); |
| 727 | |
| 728 | constexpr const char* instrumentation_key = "MethodTracing"; |
| 729 | EnableMethodTracing(soa.Self(), instrumentation_key, true); |
| 730 | EXPECT_EQ(Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter, |
| 731 | GetCurrentInstrumentationLevel()); |
| 732 | EXPECT_TRUE(instr->AreAllMethodsDeoptimized()); |
| 733 | EXPECT_TRUE(instr->AreExitStubsInstalled()); |
| 734 | |
| 735 | DisableMethodTracing(soa.Self(), instrumentation_key); |
| 736 | EXPECT_EQ(Instrumentation::InstrumentationLevel::kInstrumentNothing, |
| 737 | GetCurrentInstrumentationLevel()); |
| 738 | EXPECT_FALSE(instr->AreAllMethodsDeoptimized()); |
| 739 | } |
| 740 | |
| 741 | TEST_F(InstrumentationTest, MethodTracing_InstrumentationEntryExitStubs) { |
| 742 | ScopedObjectAccess soa(Thread::Current()); |
| 743 | Runtime* const runtime = Runtime::Current(); |
| 744 | instrumentation::Instrumentation* instr = runtime->GetInstrumentation(); |
| 745 | EXPECT_FALSE(instr->AreAllMethodsDeoptimized()); |
| 746 | |
| 747 | constexpr const char* instrumentation_key = "MethodTracing"; |
| 748 | EnableMethodTracing(soa.Self(), instrumentation_key, false); |
| 749 | EXPECT_EQ(Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs, |
| 750 | GetCurrentInstrumentationLevel()); |
| 751 | EXPECT_FALSE(instr->AreAllMethodsDeoptimized()); |
| 752 | EXPECT_TRUE(instr->AreExitStubsInstalled()); |
| 753 | |
| 754 | DisableMethodTracing(soa.Self(), instrumentation_key); |
| 755 | EXPECT_EQ(Instrumentation::InstrumentationLevel::kInstrumentNothing, |
| 756 | GetCurrentInstrumentationLevel()); |
| 757 | EXPECT_FALSE(instr->AreAllMethodsDeoptimized()); |
| 758 | } |
| 759 | |
| 760 | // We use a macro to print the line number where the test is failing. |
| 761 | #define CHECK_INSTRUMENTATION(_level, _user_count) \ |
| 762 | do { \ |
| 763 | Instrumentation* const instr = Runtime::Current()->GetInstrumentation(); \ |
| 764 | bool interpreter = \ |
Chih-Hung Hsieh | 1a0de6a | 2016-08-26 15:06:11 -0700 | [diff] [blame] | 765 | ((_level) == Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter); \ |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 766 | EXPECT_EQ(_level, GetCurrentInstrumentationLevel()); \ |
| 767 | EXPECT_EQ(_user_count, GetInstrumentationUserCount()); \ |
| 768 | if (instr->IsForcedInterpretOnly()) { \ |
| 769 | EXPECT_TRUE(instr->InterpretOnly()); \ |
| 770 | } else if (interpreter) { \ |
| 771 | EXPECT_TRUE(instr->InterpretOnly()); \ |
| 772 | } else { \ |
| 773 | EXPECT_FALSE(instr->InterpretOnly()); \ |
| 774 | } \ |
| 775 | if (interpreter) { \ |
| 776 | EXPECT_TRUE(instr->AreAllMethodsDeoptimized()); \ |
| 777 | } else { \ |
| 778 | EXPECT_FALSE(instr->AreAllMethodsDeoptimized()); \ |
| 779 | } \ |
| 780 | } while (false) |
| 781 | |
| 782 | TEST_F(InstrumentationTest, ConfigureStubs_Nothing) { |
| 783 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U); |
| 784 | |
| 785 | // Check no-op. |
| 786 | CheckConfigureStubs(kClientOneKey, Instrumentation::InstrumentationLevel::kInstrumentNothing); |
| 787 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U); |
| 788 | } |
| 789 | |
| 790 | TEST_F(InstrumentationTest, ConfigureStubs_InstrumentationStubs) { |
| 791 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U); |
| 792 | |
| 793 | // Check we can switch to instrumentation stubs |
| 794 | CheckConfigureStubs(kClientOneKey, |
| 795 | Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs); |
| 796 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs, |
| 797 | 1U); |
| 798 | |
| 799 | // Check we can disable instrumentation. |
| 800 | CheckConfigureStubs(kClientOneKey, Instrumentation::InstrumentationLevel::kInstrumentNothing); |
| 801 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U); |
| 802 | } |
| 803 | |
| 804 | TEST_F(InstrumentationTest, ConfigureStubs_Interpreter) { |
| 805 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U); |
| 806 | |
| 807 | // Check we can switch to interpreter |
| 808 | CheckConfigureStubs(kClientOneKey, |
| 809 | Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter); |
| 810 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter, 1U); |
| 811 | |
| 812 | // Check we can disable instrumentation. |
| 813 | CheckConfigureStubs(kClientOneKey, Instrumentation::InstrumentationLevel::kInstrumentNothing); |
| 814 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U); |
| 815 | } |
| 816 | |
| 817 | TEST_F(InstrumentationTest, ConfigureStubs_InstrumentationStubsToInterpreter) { |
| 818 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U); |
| 819 | |
| 820 | // Configure stubs with instrumentation stubs. |
| 821 | CheckConfigureStubs(kClientOneKey, |
| 822 | Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs); |
| 823 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs, |
| 824 | 1U); |
| 825 | |
| 826 | // Configure stubs with interpreter. |
| 827 | CheckConfigureStubs(kClientOneKey, |
| 828 | Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter); |
| 829 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter, 1U); |
| 830 | |
| 831 | // Check we can disable instrumentation. |
| 832 | CheckConfigureStubs(kClientOneKey, Instrumentation::InstrumentationLevel::kInstrumentNothing); |
| 833 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U); |
| 834 | } |
| 835 | |
| 836 | TEST_F(InstrumentationTest, ConfigureStubs_InterpreterToInstrumentationStubs) { |
| 837 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U); |
| 838 | |
| 839 | // Configure stubs with interpreter. |
| 840 | CheckConfigureStubs(kClientOneKey, |
| 841 | Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter); |
| 842 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter, 1U); |
| 843 | |
| 844 | // Configure stubs with instrumentation stubs. |
| 845 | CheckConfigureStubs(kClientOneKey, |
| 846 | Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs); |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 847 | // Make sure we are still interpreter since going from interpreter->instrumentation is dangerous. |
| 848 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter, |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 849 | 1U); |
| 850 | |
| 851 | // Check we can disable instrumentation. |
| 852 | CheckConfigureStubs(kClientOneKey, Instrumentation::InstrumentationLevel::kInstrumentNothing); |
| 853 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U); |
| 854 | } |
| 855 | |
| 856 | TEST_F(InstrumentationTest, |
| 857 | ConfigureStubs_InstrumentationStubsToInterpreterToInstrumentationStubs) { |
| 858 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U); |
| 859 | |
| 860 | // Configure stubs with instrumentation stubs. |
| 861 | CheckConfigureStubs(kClientOneKey, |
| 862 | Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs); |
| 863 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs, |
| 864 | 1U); |
| 865 | |
| 866 | // Configure stubs with interpreter. |
| 867 | CheckConfigureStubs(kClientOneKey, |
| 868 | Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter); |
| 869 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter, 1U); |
| 870 | |
| 871 | // Configure stubs with instrumentation stubs again. |
| 872 | CheckConfigureStubs(kClientOneKey, |
| 873 | Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs); |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 874 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter, |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 875 | 1U); |
| 876 | |
| 877 | // Check we can disable instrumentation. |
| 878 | CheckConfigureStubs(kClientOneKey, Instrumentation::InstrumentationLevel::kInstrumentNothing); |
| 879 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U); |
| 880 | } |
| 881 | |
| 882 | TEST_F(InstrumentationTest, MultiConfigureStubs_Nothing) { |
| 883 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U); |
| 884 | |
| 885 | // Check kInstrumentNothing with two clients. |
| 886 | CheckConfigureStubs(kClientOneKey, Instrumentation::InstrumentationLevel::kInstrumentNothing); |
| 887 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U); |
| 888 | |
| 889 | CheckConfigureStubs(kClientTwoKey, Instrumentation::InstrumentationLevel::kInstrumentNothing); |
| 890 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U); |
| 891 | } |
| 892 | |
| 893 | TEST_F(InstrumentationTest, MultiConfigureStubs_InstrumentationStubs) { |
| 894 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U); |
| 895 | |
| 896 | // Configure stubs with instrumentation stubs for 1st client. |
| 897 | CheckConfigureStubs(kClientOneKey, |
| 898 | Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs); |
| 899 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs, |
| 900 | 1U); |
| 901 | |
| 902 | // Configure stubs with instrumentation stubs for 2nd client. |
| 903 | CheckConfigureStubs(kClientTwoKey, |
| 904 | Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs); |
| 905 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs, |
| 906 | 2U); |
| 907 | |
| 908 | // 1st client requests instrumentation deactivation but 2nd client still needs |
| 909 | // instrumentation stubs. |
| 910 | CheckConfigureStubs(kClientOneKey, Instrumentation::InstrumentationLevel::kInstrumentNothing); |
| 911 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs, |
| 912 | 1U); |
| 913 | |
| 914 | // 2nd client requests instrumentation deactivation |
| 915 | CheckConfigureStubs(kClientTwoKey, Instrumentation::InstrumentationLevel::kInstrumentNothing); |
| 916 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U); |
| 917 | } |
| 918 | |
| 919 | TEST_F(InstrumentationTest, MultiConfigureStubs_Interpreter) { |
| 920 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U); |
| 921 | |
| 922 | // Configure stubs with interpreter for 1st client. |
| 923 | CheckConfigureStubs(kClientOneKey, |
| 924 | Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter); |
| 925 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter, 1U); |
| 926 | |
| 927 | // Configure stubs with interpreter for 2nd client. |
| 928 | CheckConfigureStubs(kClientTwoKey, |
| 929 | Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter); |
| 930 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter, 2U); |
| 931 | |
| 932 | // 1st client requests instrumentation deactivation but 2nd client still needs interpreter. |
| 933 | CheckConfigureStubs(kClientOneKey, Instrumentation::InstrumentationLevel::kInstrumentNothing); |
| 934 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter, 1U); |
| 935 | |
| 936 | // 2nd client requests instrumentation deactivation |
| 937 | CheckConfigureStubs(kClientTwoKey, Instrumentation::InstrumentationLevel::kInstrumentNothing); |
| 938 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U); |
| 939 | } |
| 940 | |
| 941 | TEST_F(InstrumentationTest, MultiConfigureStubs_InstrumentationStubsThenInterpreter) { |
| 942 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U); |
| 943 | |
| 944 | // Configure stubs with instrumentation stubs for 1st client. |
| 945 | CheckConfigureStubs(kClientOneKey, |
| 946 | Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs); |
| 947 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs, |
| 948 | 1U); |
| 949 | |
| 950 | // Configure stubs with interpreter for 2nd client. |
| 951 | CheckConfigureStubs(kClientTwoKey, |
| 952 | Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter); |
| 953 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter, 2U); |
| 954 | |
| 955 | // 1st client requests instrumentation deactivation but 2nd client still needs interpreter. |
| 956 | CheckConfigureStubs(kClientOneKey, Instrumentation::InstrumentationLevel::kInstrumentNothing); |
| 957 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter, 1U); |
| 958 | |
| 959 | // 2nd client requests instrumentation deactivation |
| 960 | CheckConfigureStubs(kClientTwoKey, Instrumentation::InstrumentationLevel::kInstrumentNothing); |
| 961 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U); |
| 962 | } |
| 963 | |
| 964 | TEST_F(InstrumentationTest, MultiConfigureStubs_InterpreterThenInstrumentationStubs) { |
| 965 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U); |
| 966 | |
| 967 | // Configure stubs with interpreter for 1st client. |
| 968 | CheckConfigureStubs(kClientOneKey, |
| 969 | Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter); |
| 970 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter, 1U); |
| 971 | |
| 972 | // Configure stubs with instrumentation stubs for 2nd client. |
| 973 | CheckConfigureStubs(kClientTwoKey, |
| 974 | Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs); |
| 975 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter, 2U); |
| 976 | |
| 977 | // 1st client requests instrumentation deactivation but 2nd client still needs |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 978 | // instrumentation stubs. Since we already got interpreter stubs we need to stay there. |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 979 | CheckConfigureStubs(kClientOneKey, Instrumentation::InstrumentationLevel::kInstrumentNothing); |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 980 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter, |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 981 | 1U); |
| 982 | |
| 983 | // 2nd client requests instrumentation deactivation |
| 984 | CheckConfigureStubs(kClientTwoKey, Instrumentation::InstrumentationLevel::kInstrumentNothing); |
| 985 | CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U); |
| 986 | } |
| 987 | |
| 988 | } // namespace instrumentation |
| 989 | } // namespace art |