blob: 9b77d1219aea22ae736c40b99bc82a35c4f66391 [file] [log] [blame]
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001/*
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 Markoba118822017-06-12 15:41:56 +010019#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070020#include "base/enums.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070021#include "class_linker-inl.h"
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020022#include "common_runtime_test.h"
23#include "common_throws.h"
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020024#include "dex_file.h"
Mathieu Chartieraa516822015-10-02 15:53:37 -070025#include "gc/scoped_gc_critical_section.h"
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020026#include "handle_scope-inl.h"
Alex Lightd7661582017-05-01 13:48:16 -070027#include "jni_internal.h"
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020028#include "jvalue.h"
29#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070030#include "scoped_thread_state_change-inl.h"
Alex Lighte814f9d2017-07-31 16:14:39 -070031#include "interpreter/shadow_frame.h"
Alex Lightd7661582017-05-01 13:48:16 -070032#include "thread-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070033#include "thread_list.h"
Alex Lightd7661582017-05-01 13:48:16 -070034#include "well_known_classes.h"
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020035
36namespace art {
37namespace instrumentation {
38
39class TestInstrumentationListener FINAL : public instrumentation::InstrumentationListener {
40 public:
41 TestInstrumentationListener()
Alex Lightd7661582017-05-01 13:48:16 -070042 : 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 Light6e1607e2017-08-23 10:06:18 -070050 received_exception_thrown_event(false),
Alex Light9fb1ab12017-09-05 09:32:49 -070051 received_exception_handled_event(false),
Alex Lightd7661582017-05-01 13:48:16 -070052 received_branch_event(false),
Alex Lighte814f9d2017-07-31 16:14:39 -070053 received_invoke_virtual_or_interface_event(false),
54 received_watched_frame_pop(false) {}
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020055
56 virtual ~TestInstrumentationListener() {}
57
58 void MethodEntered(Thread* thread ATTRIBUTE_UNUSED,
Alex Lightd7661582017-05-01 13:48:16 -070059 Handle<mirror::Object> this_object ATTRIBUTE_UNUSED,
Mathieu Chartiere401d142015-04-22 13:56:20 -070060 ArtMethod* method ATTRIBUTE_UNUSED,
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020061 uint32_t dex_pc ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070062 OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020063 received_method_enter_event = true;
64 }
65
66 void MethodExited(Thread* thread ATTRIBUTE_UNUSED,
Alex Lightd7661582017-05-01 13:48:16 -070067 Handle<mirror::Object> this_object ATTRIBUTE_UNUSED,
68 ArtMethod* method ATTRIBUTE_UNUSED,
69 uint32_t dex_pc ATTRIBUTE_UNUSED,
70 Handle<mirror::Object> return_value ATTRIBUTE_UNUSED)
71 OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
72 received_method_exit_object_event = true;
73 }
74
75 void MethodExited(Thread* thread ATTRIBUTE_UNUSED,
76 Handle<mirror::Object> this_object ATTRIBUTE_UNUSED,
Mathieu Chartiere401d142015-04-22 13:56:20 -070077 ArtMethod* method ATTRIBUTE_UNUSED,
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020078 uint32_t dex_pc ATTRIBUTE_UNUSED,
79 const JValue& return_value ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070080 OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020081 received_method_exit_event = true;
82 }
83
84 void MethodUnwind(Thread* thread ATTRIBUTE_UNUSED,
Alex Lightd7661582017-05-01 13:48:16 -070085 Handle<mirror::Object> this_object ATTRIBUTE_UNUSED,
Mathieu Chartiere401d142015-04-22 13:56:20 -070086 ArtMethod* method ATTRIBUTE_UNUSED,
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020087 uint32_t dex_pc ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070088 OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020089 received_method_unwind_event = true;
90 }
91
92 void DexPcMoved(Thread* thread ATTRIBUTE_UNUSED,
Alex Lightd7661582017-05-01 13:48:16 -070093 Handle<mirror::Object> this_object ATTRIBUTE_UNUSED,
Mathieu Chartiere401d142015-04-22 13:56:20 -070094 ArtMethod* method ATTRIBUTE_UNUSED,
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020095 uint32_t new_dex_pc ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070096 OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020097 received_dex_pc_moved_event = true;
98 }
99
100 void FieldRead(Thread* thread ATTRIBUTE_UNUSED,
Alex Lightd7661582017-05-01 13:48:16 -0700101 Handle<mirror::Object> this_object ATTRIBUTE_UNUSED,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700102 ArtMethod* method ATTRIBUTE_UNUSED,
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200103 uint32_t dex_pc ATTRIBUTE_UNUSED,
104 ArtField* field ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700105 OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200106 received_field_read_event = true;
107 }
108
109 void FieldWritten(Thread* thread ATTRIBUTE_UNUSED,
Alex Lightd7661582017-05-01 13:48:16 -0700110 Handle<mirror::Object> this_object ATTRIBUTE_UNUSED,
111 ArtMethod* method ATTRIBUTE_UNUSED,
112 uint32_t dex_pc ATTRIBUTE_UNUSED,
113 ArtField* field ATTRIBUTE_UNUSED,
114 Handle<mirror::Object> field_value ATTRIBUTE_UNUSED)
115 OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
116 received_field_written_object_event = true;
117 }
118
119 void FieldWritten(Thread* thread ATTRIBUTE_UNUSED,
120 Handle<mirror::Object> this_object ATTRIBUTE_UNUSED,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700121 ArtMethod* method ATTRIBUTE_UNUSED,
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200122 uint32_t dex_pc ATTRIBUTE_UNUSED,
123 ArtField* field ATTRIBUTE_UNUSED,
124 const JValue& field_value ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700125 OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200126 received_field_written_event = true;
127 }
128
Alex Light6e1607e2017-08-23 10:06:18 -0700129 void ExceptionThrown(Thread* thread ATTRIBUTE_UNUSED,
Alex Lightd7661582017-05-01 13:48:16 -0700130 Handle<mirror::Throwable> exception_object ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700131 OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Light6e1607e2017-08-23 10:06:18 -0700132 received_exception_thrown_event = true;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200133 }
134
Alex Light9fb1ab12017-09-05 09:32:49 -0700135 void ExceptionHandled(Thread* self ATTRIBUTE_UNUSED,
136 Handle<mirror::Throwable> throwable ATTRIBUTE_UNUSED)
137 OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
138 received_exception_handled_event = true;
139 }
140
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000141 void Branch(Thread* thread ATTRIBUTE_UNUSED,
142 ArtMethod* method ATTRIBUTE_UNUSED,
143 uint32_t dex_pc ATTRIBUTE_UNUSED,
144 int32_t dex_pc_offset ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700145 OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000146 received_branch_event = true;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200147 }
148
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100149 void InvokeVirtualOrInterface(Thread* thread ATTRIBUTE_UNUSED,
Alex Lightd7661582017-05-01 13:48:16 -0700150 Handle<mirror::Object> this_object ATTRIBUTE_UNUSED,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100151 ArtMethod* caller ATTRIBUTE_UNUSED,
152 uint32_t dex_pc ATTRIBUTE_UNUSED,
153 ArtMethod* callee ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700154 OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100155 received_invoke_virtual_or_interface_event = true;
156 }
157
Alex Lighte814f9d2017-07-31 16:14:39 -0700158 void WatchedFramePop(Thread* thread ATTRIBUTE_UNUSED, const ShadowFrame& frame ATTRIBUTE_UNUSED)
159 OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
160 received_watched_frame_pop = true;
161 }
162
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200163 void Reset() {
164 received_method_enter_event = false;
165 received_method_exit_event = false;
Alex Lightd7661582017-05-01 13:48:16 -0700166 received_method_exit_object_event = false;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200167 received_method_unwind_event = false;
168 received_dex_pc_moved_event = false;
169 received_field_read_event = false;
170 received_field_written_event = false;
Alex Lightd7661582017-05-01 13:48:16 -0700171 received_field_written_object_event = false;
Alex Light6e1607e2017-08-23 10:06:18 -0700172 received_exception_thrown_event = false;
Alex Light9fb1ab12017-09-05 09:32:49 -0700173 received_exception_handled_event = false;
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000174 received_branch_event = false;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100175 received_invoke_virtual_or_interface_event = false;
Alex Lighte814f9d2017-07-31 16:14:39 -0700176 received_watched_frame_pop = false;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200177 }
178
179 bool received_method_enter_event;
180 bool received_method_exit_event;
Alex Lightd7661582017-05-01 13:48:16 -0700181 bool received_method_exit_object_event;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200182 bool received_method_unwind_event;
183 bool received_dex_pc_moved_event;
184 bool received_field_read_event;
185 bool received_field_written_event;
Alex Lightd7661582017-05-01 13:48:16 -0700186 bool received_field_written_object_event;
Alex Light6e1607e2017-08-23 10:06:18 -0700187 bool received_exception_thrown_event;
Alex Light9fb1ab12017-09-05 09:32:49 -0700188 bool received_exception_handled_event;
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000189 bool received_branch_event;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100190 bool received_invoke_virtual_or_interface_event;
Alex Lighte814f9d2017-07-31 16:14:39 -0700191 bool received_watched_frame_pop;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200192
193 private:
194 DISALLOW_COPY_AND_ASSIGN(TestInstrumentationListener);
195};
196
197class InstrumentationTest : public CommonRuntimeTest {
198 public:
199 // Unique keys used to test Instrumentation::ConfigureStubs.
200 static constexpr const char* kClientOneKey = "TestClient1";
201 static constexpr const char* kClientTwoKey = "TestClient2";
202
203 void CheckConfigureStubs(const char* key, Instrumentation::InstrumentationLevel level) {
204 ScopedObjectAccess soa(Thread::Current());
205 instrumentation::Instrumentation* instr = Runtime::Current()->GetInstrumentation();
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700206 ScopedThreadSuspension sts(soa.Self(), kSuspended);
Mathieu Chartieraa516822015-10-02 15:53:37 -0700207 gc::ScopedGCCriticalSection gcs(soa.Self(),
208 gc::kGcCauseInstrumentation,
209 gc::kCollectorTypeInstrumentation);
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700210 ScopedSuspendAll ssa("Instrumentation::ConfigureStubs");
211 instr->ConfigureStubs(key, level);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200212 }
213
214 Instrumentation::InstrumentationLevel GetCurrentInstrumentationLevel() {
215 return Runtime::Current()->GetInstrumentation()->GetCurrentInstrumentationLevel();
216 }
217
218 size_t GetInstrumentationUserCount() {
219 ScopedObjectAccess soa(Thread::Current());
220 return Runtime::Current()->GetInstrumentation()->requested_instrumentation_levels_.size();
221 }
222
223 void TestEvent(uint32_t instrumentation_event) {
Alex Lightd7661582017-05-01 13:48:16 -0700224 TestEvent(instrumentation_event, nullptr, nullptr, false);
225 }
226
227 void TestEvent(uint32_t instrumentation_event,
228 ArtMethod* event_method,
229 ArtField* event_field,
230 bool with_object) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200231 ScopedObjectAccess soa(Thread::Current());
232 instrumentation::Instrumentation* instr = Runtime::Current()->GetInstrumentation();
233 TestInstrumentationListener listener;
234 {
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700235 ScopedThreadSuspension sts(soa.Self(), kSuspended);
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700236 ScopedSuspendAll ssa("Add instrumentation listener");
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200237 instr->AddListener(&listener, instrumentation_event);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200238 }
239
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200240 mirror::Object* const event_obj = nullptr;
241 const uint32_t event_dex_pc = 0;
Alex Lighte814f9d2017-07-31 16:14:39 -0700242 ShadowFrameAllocaUniquePtr test_frame = CREATE_SHADOW_FRAME(0, nullptr, event_method, 0);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200243
244 // Check the listener is registered and is notified of the event.
245 EXPECT_TRUE(HasEventListener(instr, instrumentation_event));
Alex Lightd7661582017-05-01 13:48:16 -0700246 EXPECT_FALSE(DidListenerReceiveEvent(listener, instrumentation_event, with_object));
247 ReportEvent(instr,
248 instrumentation_event,
249 soa.Self(),
250 event_method,
251 event_obj,
252 event_field,
Alex Lighte814f9d2017-07-31 16:14:39 -0700253 event_dex_pc,
254 *test_frame);
Alex Lightd7661582017-05-01 13:48:16 -0700255 EXPECT_TRUE(DidListenerReceiveEvent(listener, instrumentation_event, with_object));
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200256
257 listener.Reset();
258 {
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700259 ScopedThreadSuspension sts(soa.Self(), kSuspended);
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700260 ScopedSuspendAll ssa("Remove instrumentation listener");
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200261 instr->RemoveListener(&listener, instrumentation_event);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200262 }
263
264 // Check the listener is not registered and is not notified of the event.
265 EXPECT_FALSE(HasEventListener(instr, instrumentation_event));
Alex Lightd7661582017-05-01 13:48:16 -0700266 EXPECT_FALSE(DidListenerReceiveEvent(listener, instrumentation_event, with_object));
267 ReportEvent(instr,
268 instrumentation_event,
269 soa.Self(),
270 event_method,
271 event_obj,
272 event_field,
Alex Lighte814f9d2017-07-31 16:14:39 -0700273 event_dex_pc,
274 *test_frame);
Alex Lightd7661582017-05-01 13:48:16 -0700275 EXPECT_FALSE(DidListenerReceiveEvent(listener, instrumentation_event, with_object));
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200276 }
277
Mathieu Chartiere401d142015-04-22 13:56:20 -0700278 void DeoptimizeMethod(Thread* self, ArtMethod* method, bool enable_deoptimization)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700279 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200280 Runtime* runtime = Runtime::Current();
281 instrumentation::Instrumentation* instrumentation = runtime->GetInstrumentation();
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700282 ScopedThreadSuspension sts(self, kSuspended);
Mathieu Chartieraa516822015-10-02 15:53:37 -0700283 gc::ScopedGCCriticalSection gcs(self,
284 gc::kGcCauseInstrumentation,
285 gc::kCollectorTypeInstrumentation);
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700286 ScopedSuspendAll ssa("Single method deoptimization");
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200287 if (enable_deoptimization) {
288 instrumentation->EnableDeoptimization();
289 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700290 instrumentation->Deoptimize(method);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200291 }
292
Mathieu Chartiere401d142015-04-22 13:56:20 -0700293 void UndeoptimizeMethod(Thread* self, ArtMethod* method,
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200294 const char* key, bool disable_deoptimization)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700295 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200296 Runtime* runtime = Runtime::Current();
297 instrumentation::Instrumentation* instrumentation = runtime->GetInstrumentation();
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700298 ScopedThreadSuspension sts(self, kSuspended);
Mathieu Chartieraa516822015-10-02 15:53:37 -0700299 gc::ScopedGCCriticalSection gcs(self,
300 gc::kGcCauseInstrumentation,
301 gc::kCollectorTypeInstrumentation);
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700302 ScopedSuspendAll ssa("Single method undeoptimization");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700303 instrumentation->Undeoptimize(method);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200304 if (disable_deoptimization) {
305 instrumentation->DisableDeoptimization(key);
306 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200307 }
308
309 void DeoptimizeEverything(Thread* self, const char* key, bool enable_deoptimization)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700310 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200311 Runtime* runtime = Runtime::Current();
312 instrumentation::Instrumentation* instrumentation = runtime->GetInstrumentation();
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700313 ScopedThreadSuspension sts(self, kSuspended);
Mathieu Chartieraa516822015-10-02 15:53:37 -0700314 gc::ScopedGCCriticalSection gcs(self,
315 gc::kGcCauseInstrumentation,
316 gc::kCollectorTypeInstrumentation);
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700317 ScopedSuspendAll ssa("Full deoptimization");
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200318 if (enable_deoptimization) {
319 instrumentation->EnableDeoptimization();
320 }
321 instrumentation->DeoptimizeEverything(key);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200322 }
323
324 void UndeoptimizeEverything(Thread* self, const char* key, bool disable_deoptimization)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700325 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200326 Runtime* runtime = Runtime::Current();
327 instrumentation::Instrumentation* instrumentation = runtime->GetInstrumentation();
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700328 ScopedThreadSuspension sts(self, kSuspended);
Mathieu Chartieraa516822015-10-02 15:53:37 -0700329 gc::ScopedGCCriticalSection gcs(self,
330 gc::kGcCauseInstrumentation,
331 gc::kCollectorTypeInstrumentation);
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700332 ScopedSuspendAll ssa("Full undeoptimization");
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200333 instrumentation->UndeoptimizeEverything(key);
334 if (disable_deoptimization) {
335 instrumentation->DisableDeoptimization(key);
336 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200337 }
338
339 void EnableMethodTracing(Thread* self, const char* key, bool needs_interpreter)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700340 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200341 Runtime* runtime = Runtime::Current();
342 instrumentation::Instrumentation* instrumentation = runtime->GetInstrumentation();
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700343 ScopedThreadSuspension sts(self, kSuspended);
Mathieu Chartieraa516822015-10-02 15:53:37 -0700344 gc::ScopedGCCriticalSection gcs(self,
345 gc::kGcCauseInstrumentation,
346 gc::kCollectorTypeInstrumentation);
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700347 ScopedSuspendAll ssa("EnableMethodTracing");
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200348 instrumentation->EnableMethodTracing(key, needs_interpreter);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200349 }
350
351 void DisableMethodTracing(Thread* self, const char* key)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700352 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200353 Runtime* runtime = Runtime::Current();
354 instrumentation::Instrumentation* instrumentation = runtime->GetInstrumentation();
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700355 ScopedThreadSuspension sts(self, kSuspended);
Mathieu Chartieraa516822015-10-02 15:53:37 -0700356 gc::ScopedGCCriticalSection gcs(self,
357 gc::kGcCauseInstrumentation,
358 gc::kCollectorTypeInstrumentation);
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700359 ScopedSuspendAll ssa("EnableMethodTracing");
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200360 instrumentation->DisableMethodTracing(key);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200361 }
362
363 private:
364 static bool HasEventListener(const instrumentation::Instrumentation* instr, uint32_t event_type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700365 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200366 switch (event_type) {
367 case instrumentation::Instrumentation::kMethodEntered:
368 return instr->HasMethodEntryListeners();
369 case instrumentation::Instrumentation::kMethodExited:
370 return instr->HasMethodExitListeners();
371 case instrumentation::Instrumentation::kMethodUnwind:
372 return instr->HasMethodUnwindListeners();
373 case instrumentation::Instrumentation::kDexPcMoved:
374 return instr->HasDexPcListeners();
375 case instrumentation::Instrumentation::kFieldRead:
376 return instr->HasFieldReadListeners();
377 case instrumentation::Instrumentation::kFieldWritten:
378 return instr->HasFieldWriteListeners();
Alex Light6e1607e2017-08-23 10:06:18 -0700379 case instrumentation::Instrumentation::kExceptionThrown:
380 return instr->HasExceptionThrownListeners();
Alex Light9fb1ab12017-09-05 09:32:49 -0700381 case instrumentation::Instrumentation::kExceptionHandled:
382 return instr->HasExceptionHandledListeners();
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000383 case instrumentation::Instrumentation::kBranch:
384 return instr->HasBranchListeners();
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100385 case instrumentation::Instrumentation::kInvokeVirtualOrInterface:
386 return instr->HasInvokeVirtualOrInterfaceListeners();
Alex Lighte814f9d2017-07-31 16:14:39 -0700387 case instrumentation::Instrumentation::kWatchedFramePop:
388 return instr->HasWatchedFramePopListeners();
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200389 default:
390 LOG(FATAL) << "Unknown instrumentation event " << event_type;
391 UNREACHABLE();
392 }
393 }
394
Alex Lightd7661582017-05-01 13:48:16 -0700395 static void ReportEvent(const instrumentation::Instrumentation* instr,
396 uint32_t event_type,
397 Thread* self,
398 ArtMethod* method,
399 mirror::Object* obj,
400 ArtField* field,
Alex Lighte814f9d2017-07-31 16:14:39 -0700401 uint32_t dex_pc,
402 const ShadowFrame& frame)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700403 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200404 switch (event_type) {
405 case instrumentation::Instrumentation::kMethodEntered:
406 instr->MethodEnterEvent(self, obj, method, dex_pc);
407 break;
408 case instrumentation::Instrumentation::kMethodExited: {
409 JValue value;
410 instr->MethodExitEvent(self, obj, method, dex_pc, value);
411 break;
412 }
413 case instrumentation::Instrumentation::kMethodUnwind:
414 instr->MethodUnwindEvent(self, obj, method, dex_pc);
415 break;
416 case instrumentation::Instrumentation::kDexPcMoved:
417 instr->DexPcMovedEvent(self, obj, method, dex_pc);
418 break;
419 case instrumentation::Instrumentation::kFieldRead:
Alex Lightd7661582017-05-01 13:48:16 -0700420 instr->FieldReadEvent(self, obj, method, dex_pc, field);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200421 break;
422 case instrumentation::Instrumentation::kFieldWritten: {
423 JValue value;
Alex Lightd7661582017-05-01 13:48:16 -0700424 instr->FieldWriteEvent(self, obj, method, dex_pc, field, value);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200425 break;
426 }
Alex Light6e1607e2017-08-23 10:06:18 -0700427 case instrumentation::Instrumentation::kExceptionThrown: {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200428 ThrowArithmeticExceptionDivideByZero();
429 mirror::Throwable* event_exception = self->GetException();
Alex Light6e1607e2017-08-23 10:06:18 -0700430 instr->ExceptionThrownEvent(self, event_exception);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200431 self->ClearException();
432 break;
433 }
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000434 case instrumentation::Instrumentation::kBranch:
435 instr->Branch(self, method, dex_pc, -1);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200436 break;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100437 case instrumentation::Instrumentation::kInvokeVirtualOrInterface:
438 instr->InvokeVirtualOrInterface(self, obj, method, dex_pc, method);
439 break;
Alex Lighte814f9d2017-07-31 16:14:39 -0700440 case instrumentation::Instrumentation::kWatchedFramePop:
441 instr->WatchedFramePopped(self, frame);
442 break;
Alex Light9fb1ab12017-09-05 09:32:49 -0700443 case instrumentation::Instrumentation::kExceptionHandled: {
444 ThrowArithmeticExceptionDivideByZero();
445 mirror::Throwable* event_exception = self->GetException();
446 self->ClearException();
447 instr->ExceptionHandledEvent(self, event_exception);
448 break;
449 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200450 default:
451 LOG(FATAL) << "Unknown instrumentation event " << event_type;
452 UNREACHABLE();
453 }
454 }
455
456 static bool DidListenerReceiveEvent(const TestInstrumentationListener& listener,
Alex Lightd7661582017-05-01 13:48:16 -0700457 uint32_t event_type,
458 bool with_object) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200459 switch (event_type) {
460 case instrumentation::Instrumentation::kMethodEntered:
461 return listener.received_method_enter_event;
462 case instrumentation::Instrumentation::kMethodExited:
Alex Lightd7661582017-05-01 13:48:16 -0700463 return (!with_object && listener.received_method_exit_event) ||
464 (with_object && listener.received_method_exit_object_event);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200465 case instrumentation::Instrumentation::kMethodUnwind:
466 return listener.received_method_unwind_event;
467 case instrumentation::Instrumentation::kDexPcMoved:
468 return listener.received_dex_pc_moved_event;
469 case instrumentation::Instrumentation::kFieldRead:
470 return listener.received_field_read_event;
471 case instrumentation::Instrumentation::kFieldWritten:
Alex Lightd7661582017-05-01 13:48:16 -0700472 return (!with_object && listener.received_field_written_event) ||
473 (with_object && listener.received_field_written_object_event);
Alex Light6e1607e2017-08-23 10:06:18 -0700474 case instrumentation::Instrumentation::kExceptionThrown:
475 return listener.received_exception_thrown_event;
Alex Light9fb1ab12017-09-05 09:32:49 -0700476 case instrumentation::Instrumentation::kExceptionHandled:
477 return listener.received_exception_handled_event;
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000478 case instrumentation::Instrumentation::kBranch:
479 return listener.received_branch_event;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100480 case instrumentation::Instrumentation::kInvokeVirtualOrInterface:
481 return listener.received_invoke_virtual_or_interface_event;
Alex Lighte814f9d2017-07-31 16:14:39 -0700482 case instrumentation::Instrumentation::kWatchedFramePop:
483 return listener.received_watched_frame_pop;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200484 default:
485 LOG(FATAL) << "Unknown instrumentation event " << event_type;
486 UNREACHABLE();
487 }
488 }
489};
490
491TEST_F(InstrumentationTest, NoInstrumentation) {
492 ScopedObjectAccess soa(Thread::Current());
493 instrumentation::Instrumentation* instr = Runtime::Current()->GetInstrumentation();
494 ASSERT_NE(instr, nullptr);
495
496 EXPECT_FALSE(instr->AreExitStubsInstalled());
497 EXPECT_FALSE(instr->AreAllMethodsDeoptimized());
498 EXPECT_FALSE(instr->IsActive());
499 EXPECT_FALSE(instr->ShouldNotifyMethodEnterExitEvents());
500
501 // Test interpreter table is the default one.
502 EXPECT_EQ(instrumentation::kMainHandlerTable, instr->GetInterpreterHandlerTable());
503
504 // Check there is no registered listener.
505 EXPECT_FALSE(instr->HasDexPcListeners());
Alex Light6e1607e2017-08-23 10:06:18 -0700506 EXPECT_FALSE(instr->HasExceptionThrownListeners());
Alex Light9fb1ab12017-09-05 09:32:49 -0700507 EXPECT_FALSE(instr->HasExceptionHandledListeners());
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200508 EXPECT_FALSE(instr->HasFieldReadListeners());
509 EXPECT_FALSE(instr->HasFieldWriteListeners());
510 EXPECT_FALSE(instr->HasMethodEntryListeners());
511 EXPECT_FALSE(instr->HasMethodExitListeners());
512 EXPECT_FALSE(instr->IsActive());
513}
514
515// Test instrumentation listeners for each event.
516TEST_F(InstrumentationTest, MethodEntryEvent) {
Nicolas Geoffray07c70282017-08-30 08:09:42 +0000517 TestEvent(instrumentation::Instrumentation::kMethodEntered);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200518}
519
Alex Lightd7661582017-05-01 13:48:16 -0700520TEST_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());
526 Handle<mirror::ClassLoader> loader(hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader)));
527 mirror::Class* klass = class_linker->FindClass(soa.Self(), "LInstrumentation;", loader);
528 ASSERT_TRUE(klass != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +0100529 ArtMethod* method =
530 klass->FindClassMethod("returnReference", "()Ljava/lang/Object;", kRuntimePointerSize);
Alex Lightd7661582017-05-01 13:48:16 -0700531 ASSERT_TRUE(method != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +0100532 ASSERT_TRUE(method->IsDirect());
533 ASSERT_TRUE(method->GetDeclaringClass() == klass);
Alex Lightd7661582017-05-01 13:48:16 -0700534 TestEvent(instrumentation::Instrumentation::kMethodExited,
535 /*event_method*/ method,
536 /*event_field*/ nullptr,
537 /*with_object*/ true);
538}
539
540TEST_F(InstrumentationTest, MethodExitPrimEvent) {
541 ScopedObjectAccess soa(Thread::Current());
542 jobject class_loader = LoadDex("Instrumentation");
543 Runtime* const runtime = Runtime::Current();
544 ClassLinker* class_linker = runtime->GetClassLinker();
545 StackHandleScope<1> hs(soa.Self());
546 Handle<mirror::ClassLoader> loader(hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader)));
547 mirror::Class* klass = class_linker->FindClass(soa.Self(), "LInstrumentation;", loader);
548 ASSERT_TRUE(klass != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +0100549 ArtMethod* method = klass->FindClassMethod("returnPrimitive", "()I", kRuntimePointerSize);
Alex Lightd7661582017-05-01 13:48:16 -0700550 ASSERT_TRUE(method != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +0100551 ASSERT_TRUE(method->IsDirect());
552 ASSERT_TRUE(method->GetDeclaringClass() == klass);
Alex Lightd7661582017-05-01 13:48:16 -0700553 TestEvent(instrumentation::Instrumentation::kMethodExited,
554 /*event_method*/ method,
555 /*event_field*/ nullptr,
556 /*with_object*/ false);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200557}
558
559TEST_F(InstrumentationTest, MethodUnwindEvent) {
560 TestEvent(instrumentation::Instrumentation::kMethodUnwind);
561}
562
563TEST_F(InstrumentationTest, DexPcMovedEvent) {
564 TestEvent(instrumentation::Instrumentation::kDexPcMoved);
565}
566
567TEST_F(InstrumentationTest, FieldReadEvent) {
568 TestEvent(instrumentation::Instrumentation::kFieldRead);
569}
570
Alex Lighte814f9d2017-07-31 16:14:39 -0700571TEST_F(InstrumentationTest, WatchedFramePop) {
572 TestEvent(instrumentation::Instrumentation::kWatchedFramePop);
573}
574
Alex Lightd7661582017-05-01 13:48:16 -0700575TEST_F(InstrumentationTest, FieldWriteObjectEvent) {
576 ScopedObjectAccess soa(Thread::Current());
577 jobject class_loader = LoadDex("Instrumentation");
578 Runtime* const runtime = Runtime::Current();
579 ClassLinker* class_linker = runtime->GetClassLinker();
580 StackHandleScope<1> hs(soa.Self());
581 Handle<mirror::ClassLoader> loader(hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader)));
582 mirror::Class* klass = class_linker->FindClass(soa.Self(), "LInstrumentation;", loader);
583 ASSERT_TRUE(klass != nullptr);
584 ArtField* field = klass->FindDeclaredStaticField("referenceField", "Ljava/lang/Object;");
585 ASSERT_TRUE(field != nullptr);
586
587 TestEvent(instrumentation::Instrumentation::kFieldWritten,
588 /*event_method*/ nullptr,
589 /*event_field*/ field,
590 /*with_object*/ true);
591}
592
593TEST_F(InstrumentationTest, FieldWritePrimEvent) {
594 ScopedObjectAccess soa(Thread::Current());
595 jobject class_loader = LoadDex("Instrumentation");
596 Runtime* const runtime = Runtime::Current();
597 ClassLinker* class_linker = runtime->GetClassLinker();
598 StackHandleScope<1> hs(soa.Self());
599 Handle<mirror::ClassLoader> loader(hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader)));
600 mirror::Class* klass = class_linker->FindClass(soa.Self(), "LInstrumentation;", loader);
601 ASSERT_TRUE(klass != nullptr);
602 ArtField* field = klass->FindDeclaredStaticField("primitiveField", "I");
603 ASSERT_TRUE(field != nullptr);
604
605 TestEvent(instrumentation::Instrumentation::kFieldWritten,
606 /*event_method*/ nullptr,
607 /*event_field*/ field,
608 /*with_object*/ false);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200609}
610
Alex Light9fb1ab12017-09-05 09:32:49 -0700611TEST_F(InstrumentationTest, ExceptionHandledEvent) {
612 TestEvent(instrumentation::Instrumentation::kExceptionHandled);
613}
614
Alex Light6e1607e2017-08-23 10:06:18 -0700615TEST_F(InstrumentationTest, ExceptionThrownEvent) {
616 TestEvent(instrumentation::Instrumentation::kExceptionThrown);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200617}
618
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000619TEST_F(InstrumentationTest, BranchEvent) {
620 TestEvent(instrumentation::Instrumentation::kBranch);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200621}
622
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100623TEST_F(InstrumentationTest, InvokeVirtualOrInterfaceEvent) {
624 TestEvent(instrumentation::Instrumentation::kInvokeVirtualOrInterface);
625}
626
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200627TEST_F(InstrumentationTest, DeoptimizeDirectMethod) {
628 ScopedObjectAccess soa(Thread::Current());
629 jobject class_loader = LoadDex("Instrumentation");
630 Runtime* const runtime = Runtime::Current();
631 instrumentation::Instrumentation* instr = runtime->GetInstrumentation();
632 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700633 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier0795f232016-09-27 18:43:30 -0700634 Handle<mirror::ClassLoader> loader(hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader)));
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200635 mirror::Class* klass = class_linker->FindClass(soa.Self(), "LInstrumentation;", loader);
636 ASSERT_TRUE(klass != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +0100637 ArtMethod* method_to_deoptimize =
638 klass->FindClassMethod("instanceMethod", "()V", kRuntimePointerSize);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700639 ASSERT_TRUE(method_to_deoptimize != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +0100640 ASSERT_TRUE(method_to_deoptimize->IsDirect());
641 ASSERT_TRUE(method_to_deoptimize->GetDeclaringClass() == klass);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200642
643 EXPECT_FALSE(instr->AreAllMethodsDeoptimized());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700644 EXPECT_FALSE(instr->IsDeoptimized(method_to_deoptimize));
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200645
646 DeoptimizeMethod(soa.Self(), method_to_deoptimize, true);
647
648 EXPECT_FALSE(instr->AreAllMethodsDeoptimized());
649 EXPECT_TRUE(instr->AreExitStubsInstalled());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700650 EXPECT_TRUE(instr->IsDeoptimized(method_to_deoptimize));
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200651
652 constexpr const char* instrumentation_key = "DeoptimizeDirectMethod";
653 UndeoptimizeMethod(soa.Self(), method_to_deoptimize, instrumentation_key, true);
654
655 EXPECT_FALSE(instr->AreAllMethodsDeoptimized());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700656 EXPECT_FALSE(instr->IsDeoptimized(method_to_deoptimize));
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200657}
658
659TEST_F(InstrumentationTest, FullDeoptimization) {
660 ScopedObjectAccess soa(Thread::Current());
661 Runtime* const runtime = Runtime::Current();
662 instrumentation::Instrumentation* instr = runtime->GetInstrumentation();
663 EXPECT_FALSE(instr->AreAllMethodsDeoptimized());
664
665 constexpr const char* instrumentation_key = "FullDeoptimization";
666 DeoptimizeEverything(soa.Self(), instrumentation_key, true);
667
668 EXPECT_TRUE(instr->AreAllMethodsDeoptimized());
669 EXPECT_TRUE(instr->AreExitStubsInstalled());
670
671 UndeoptimizeEverything(soa.Self(), instrumentation_key, true);
672
673 EXPECT_FALSE(instr->AreAllMethodsDeoptimized());
674}
675
676TEST_F(InstrumentationTest, MixedDeoptimization) {
677 ScopedObjectAccess soa(Thread::Current());
678 jobject class_loader = LoadDex("Instrumentation");
679 Runtime* const runtime = Runtime::Current();
680 instrumentation::Instrumentation* instr = runtime->GetInstrumentation();
681 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700682 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier0795f232016-09-27 18:43:30 -0700683 Handle<mirror::ClassLoader> loader(hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader)));
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200684 mirror::Class* klass = class_linker->FindClass(soa.Self(), "LInstrumentation;", loader);
685 ASSERT_TRUE(klass != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +0100686 ArtMethod* method_to_deoptimize =
687 klass->FindClassMethod("instanceMethod", "()V", kRuntimePointerSize);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700688 ASSERT_TRUE(method_to_deoptimize != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +0100689 ASSERT_TRUE(method_to_deoptimize->IsDirect());
690 ASSERT_TRUE(method_to_deoptimize->GetDeclaringClass() == klass);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200691
692 EXPECT_FALSE(instr->AreAllMethodsDeoptimized());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700693 EXPECT_FALSE(instr->IsDeoptimized(method_to_deoptimize));
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200694
695 DeoptimizeMethod(soa.Self(), method_to_deoptimize, true);
696 // Deoptimizing a method does not change instrumentation level.
697 EXPECT_EQ(Instrumentation::InstrumentationLevel::kInstrumentNothing,
698 GetCurrentInstrumentationLevel());
699 EXPECT_FALSE(instr->AreAllMethodsDeoptimized());
700 EXPECT_TRUE(instr->AreExitStubsInstalled());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700701 EXPECT_TRUE(instr->IsDeoptimized(method_to_deoptimize));
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200702
703 constexpr const char* instrumentation_key = "MixedDeoptimization";
704 DeoptimizeEverything(soa.Self(), instrumentation_key, false);
705 EXPECT_EQ(Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter,
706 GetCurrentInstrumentationLevel());
707 EXPECT_TRUE(instr->AreAllMethodsDeoptimized());
708 EXPECT_TRUE(instr->AreExitStubsInstalled());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700709 EXPECT_TRUE(instr->IsDeoptimized(method_to_deoptimize));
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200710
711 UndeoptimizeEverything(soa.Self(), instrumentation_key, false);
712 EXPECT_EQ(Instrumentation::InstrumentationLevel::kInstrumentNothing,
713 GetCurrentInstrumentationLevel());
714 EXPECT_FALSE(instr->AreAllMethodsDeoptimized());
715 EXPECT_TRUE(instr->AreExitStubsInstalled());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700716 EXPECT_TRUE(instr->IsDeoptimized(method_to_deoptimize));
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200717
718 UndeoptimizeMethod(soa.Self(), method_to_deoptimize, instrumentation_key, true);
719 EXPECT_EQ(Instrumentation::InstrumentationLevel::kInstrumentNothing,
720 GetCurrentInstrumentationLevel());
721 EXPECT_FALSE(instr->AreAllMethodsDeoptimized());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700722 EXPECT_FALSE(instr->IsDeoptimized(method_to_deoptimize));
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200723}
724
725TEST_F(InstrumentationTest, MethodTracing_Interpreter) {
726 ScopedObjectAccess soa(Thread::Current());
727 Runtime* const runtime = Runtime::Current();
728 instrumentation::Instrumentation* instr = runtime->GetInstrumentation();
729 EXPECT_FALSE(instr->AreAllMethodsDeoptimized());
730
731 constexpr const char* instrumentation_key = "MethodTracing";
732 EnableMethodTracing(soa.Self(), instrumentation_key, true);
733 EXPECT_EQ(Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter,
734 GetCurrentInstrumentationLevel());
735 EXPECT_TRUE(instr->AreAllMethodsDeoptimized());
736 EXPECT_TRUE(instr->AreExitStubsInstalled());
737
738 DisableMethodTracing(soa.Self(), instrumentation_key);
739 EXPECT_EQ(Instrumentation::InstrumentationLevel::kInstrumentNothing,
740 GetCurrentInstrumentationLevel());
741 EXPECT_FALSE(instr->AreAllMethodsDeoptimized());
742}
743
744TEST_F(InstrumentationTest, MethodTracing_InstrumentationEntryExitStubs) {
745 ScopedObjectAccess soa(Thread::Current());
746 Runtime* const runtime = Runtime::Current();
747 instrumentation::Instrumentation* instr = runtime->GetInstrumentation();
748 EXPECT_FALSE(instr->AreAllMethodsDeoptimized());
749
750 constexpr const char* instrumentation_key = "MethodTracing";
751 EnableMethodTracing(soa.Self(), instrumentation_key, false);
752 EXPECT_EQ(Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs,
753 GetCurrentInstrumentationLevel());
754 EXPECT_FALSE(instr->AreAllMethodsDeoptimized());
755 EXPECT_TRUE(instr->AreExitStubsInstalled());
756
757 DisableMethodTracing(soa.Self(), instrumentation_key);
758 EXPECT_EQ(Instrumentation::InstrumentationLevel::kInstrumentNothing,
759 GetCurrentInstrumentationLevel());
760 EXPECT_FALSE(instr->AreAllMethodsDeoptimized());
761}
762
763// We use a macro to print the line number where the test is failing.
764#define CHECK_INSTRUMENTATION(_level, _user_count) \
765 do { \
766 Instrumentation* const instr = Runtime::Current()->GetInstrumentation(); \
767 bool interpreter = \
Chih-Hung Hsieh1a0de6a2016-08-26 15:06:11 -0700768 ((_level) == Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter); \
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200769 EXPECT_EQ(_level, GetCurrentInstrumentationLevel()); \
770 EXPECT_EQ(_user_count, GetInstrumentationUserCount()); \
771 if (instr->IsForcedInterpretOnly()) { \
772 EXPECT_TRUE(instr->InterpretOnly()); \
773 } else if (interpreter) { \
774 EXPECT_TRUE(instr->InterpretOnly()); \
775 } else { \
776 EXPECT_FALSE(instr->InterpretOnly()); \
777 } \
778 if (interpreter) { \
779 EXPECT_TRUE(instr->AreAllMethodsDeoptimized()); \
780 } else { \
781 EXPECT_FALSE(instr->AreAllMethodsDeoptimized()); \
782 } \
783 } while (false)
784
785TEST_F(InstrumentationTest, ConfigureStubs_Nothing) {
786 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U);
787
788 // Check no-op.
789 CheckConfigureStubs(kClientOneKey, Instrumentation::InstrumentationLevel::kInstrumentNothing);
790 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U);
791}
792
793TEST_F(InstrumentationTest, ConfigureStubs_InstrumentationStubs) {
794 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U);
795
796 // Check we can switch to instrumentation stubs
797 CheckConfigureStubs(kClientOneKey,
798 Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs);
799 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs,
800 1U);
801
802 // Check we can disable instrumentation.
803 CheckConfigureStubs(kClientOneKey, Instrumentation::InstrumentationLevel::kInstrumentNothing);
804 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U);
805}
806
807TEST_F(InstrumentationTest, ConfigureStubs_Interpreter) {
808 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U);
809
810 // Check we can switch to interpreter
811 CheckConfigureStubs(kClientOneKey,
812 Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter);
813 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter, 1U);
814
815 // Check we can disable instrumentation.
816 CheckConfigureStubs(kClientOneKey, Instrumentation::InstrumentationLevel::kInstrumentNothing);
817 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U);
818}
819
820TEST_F(InstrumentationTest, ConfigureStubs_InstrumentationStubsToInterpreter) {
821 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U);
822
823 // Configure stubs with instrumentation stubs.
824 CheckConfigureStubs(kClientOneKey,
825 Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs);
826 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs,
827 1U);
828
829 // Configure stubs with interpreter.
830 CheckConfigureStubs(kClientOneKey,
831 Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter);
832 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter, 1U);
833
834 // Check we can disable instrumentation.
835 CheckConfigureStubs(kClientOneKey, Instrumentation::InstrumentationLevel::kInstrumentNothing);
836 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U);
837}
838
839TEST_F(InstrumentationTest, ConfigureStubs_InterpreterToInstrumentationStubs) {
840 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U);
841
842 // Configure stubs with interpreter.
843 CheckConfigureStubs(kClientOneKey,
844 Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter);
845 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter, 1U);
846
847 // Configure stubs with instrumentation stubs.
848 CheckConfigureStubs(kClientOneKey,
849 Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs);
850 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs,
851 1U);
852
853 // Check we can disable instrumentation.
854 CheckConfigureStubs(kClientOneKey, Instrumentation::InstrumentationLevel::kInstrumentNothing);
855 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U);
856}
857
858TEST_F(InstrumentationTest,
859 ConfigureStubs_InstrumentationStubsToInterpreterToInstrumentationStubs) {
860 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U);
861
862 // Configure stubs with instrumentation stubs.
863 CheckConfigureStubs(kClientOneKey,
864 Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs);
865 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs,
866 1U);
867
868 // Configure stubs with interpreter.
869 CheckConfigureStubs(kClientOneKey,
870 Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter);
871 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter, 1U);
872
873 // Configure stubs with instrumentation stubs again.
874 CheckConfigureStubs(kClientOneKey,
875 Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs);
876 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs,
877 1U);
878
879 // Check we can disable instrumentation.
880 CheckConfigureStubs(kClientOneKey, Instrumentation::InstrumentationLevel::kInstrumentNothing);
881 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U);
882}
883
884TEST_F(InstrumentationTest, MultiConfigureStubs_Nothing) {
885 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U);
886
887 // Check kInstrumentNothing with two clients.
888 CheckConfigureStubs(kClientOneKey, Instrumentation::InstrumentationLevel::kInstrumentNothing);
889 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U);
890
891 CheckConfigureStubs(kClientTwoKey, Instrumentation::InstrumentationLevel::kInstrumentNothing);
892 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U);
893}
894
895TEST_F(InstrumentationTest, MultiConfigureStubs_InstrumentationStubs) {
896 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U);
897
898 // Configure stubs with instrumentation stubs for 1st client.
899 CheckConfigureStubs(kClientOneKey,
900 Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs);
901 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs,
902 1U);
903
904 // Configure stubs with instrumentation stubs for 2nd client.
905 CheckConfigureStubs(kClientTwoKey,
906 Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs);
907 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs,
908 2U);
909
910 // 1st client requests instrumentation deactivation but 2nd client still needs
911 // instrumentation stubs.
912 CheckConfigureStubs(kClientOneKey, Instrumentation::InstrumentationLevel::kInstrumentNothing);
913 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs,
914 1U);
915
916 // 2nd client requests instrumentation deactivation
917 CheckConfigureStubs(kClientTwoKey, Instrumentation::InstrumentationLevel::kInstrumentNothing);
918 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U);
919}
920
921TEST_F(InstrumentationTest, MultiConfigureStubs_Interpreter) {
922 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U);
923
924 // Configure stubs with interpreter for 1st client.
925 CheckConfigureStubs(kClientOneKey,
926 Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter);
927 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter, 1U);
928
929 // Configure stubs with interpreter for 2nd client.
930 CheckConfigureStubs(kClientTwoKey,
931 Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter);
932 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter, 2U);
933
934 // 1st client requests instrumentation deactivation but 2nd client still needs interpreter.
935 CheckConfigureStubs(kClientOneKey, Instrumentation::InstrumentationLevel::kInstrumentNothing);
936 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter, 1U);
937
938 // 2nd client requests instrumentation deactivation
939 CheckConfigureStubs(kClientTwoKey, Instrumentation::InstrumentationLevel::kInstrumentNothing);
940 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U);
941}
942
943TEST_F(InstrumentationTest, MultiConfigureStubs_InstrumentationStubsThenInterpreter) {
944 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U);
945
946 // Configure stubs with instrumentation stubs for 1st client.
947 CheckConfigureStubs(kClientOneKey,
948 Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs);
949 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs,
950 1U);
951
952 // Configure stubs with interpreter for 2nd client.
953 CheckConfigureStubs(kClientTwoKey,
954 Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter);
955 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter, 2U);
956
957 // 1st client requests instrumentation deactivation but 2nd client still needs interpreter.
958 CheckConfigureStubs(kClientOneKey, Instrumentation::InstrumentationLevel::kInstrumentNothing);
959 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter, 1U);
960
961 // 2nd client requests instrumentation deactivation
962 CheckConfigureStubs(kClientTwoKey, Instrumentation::InstrumentationLevel::kInstrumentNothing);
963 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U);
964}
965
966TEST_F(InstrumentationTest, MultiConfigureStubs_InterpreterThenInstrumentationStubs) {
967 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U);
968
969 // Configure stubs with interpreter for 1st client.
970 CheckConfigureStubs(kClientOneKey,
971 Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter);
972 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter, 1U);
973
974 // Configure stubs with instrumentation stubs for 2nd client.
975 CheckConfigureStubs(kClientTwoKey,
976 Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs);
977 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter, 2U);
978
979 // 1st client requests instrumentation deactivation but 2nd client still needs
980 // instrumentation stubs.
981 CheckConfigureStubs(kClientOneKey, Instrumentation::InstrumentationLevel::kInstrumentNothing);
982 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentWithInstrumentationStubs,
983 1U);
984
985 // 2nd client requests instrumentation deactivation
986 CheckConfigureStubs(kClientTwoKey, Instrumentation::InstrumentationLevel::kInstrumentNothing);
987 CHECK_INSTRUMENTATION(Instrumentation::InstrumentationLevel::kInstrumentNothing, 0U);
988}
989
990} // namespace instrumentation
991} // namespace art