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