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