Alex Light | 673726b | 2018-05-21 14:19:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 17 | #include "android-base/macros.h" |
Alex Light | 673726b | 2018-05-21 14:19:15 -0700 | [diff] [blame] | 18 | #include "gc/scoped_gc_critical_section.h" |
| 19 | #include "instrumentation.h" |
| 20 | #include "runtime.h" |
| 21 | #include "runtime_callbacks.h" |
| 22 | #include "scoped_thread_state_change-inl.h" |
| 23 | #include "thread-inl.h" |
| 24 | #include "thread_list.h" |
| 25 | |
| 26 | namespace tracefast { |
| 27 | |
| 28 | #if ((!defined(TRACEFAST_INTERPRETER) && !defined(TRACEFAST_TRAMPOLINE)) || \ |
| 29 | (defined(TRACEFAST_INTERPRETER) && defined(TRACEFAST_TRAMPOLINE))) |
| 30 | #error Must set one of TRACEFAST_TRAMPOLINE or TRACEFAST_INTERPRETER during build |
| 31 | #endif |
| 32 | |
| 33 | |
| 34 | #ifdef TRACEFAST_INTERPRETER |
| 35 | static constexpr const char* kTracerInstrumentationKey = "tracefast_INTERPRETER"; |
| 36 | static constexpr bool kNeedsInterpreter = true; |
| 37 | #else // defined(TRACEFAST_TRAMPOLINE) |
| 38 | static constexpr const char* kTracerInstrumentationKey = "tracefast_TRAMPOLINE"; |
| 39 | static constexpr bool kNeedsInterpreter = false; |
| 40 | #endif // TRACEFAST_INITERPRETER |
| 41 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 42 | class Tracer final : public art::instrumentation::InstrumentationListener { |
Alex Light | 673726b | 2018-05-21 14:19:15 -0700 | [diff] [blame] | 43 | public: |
| 44 | Tracer() {} |
| 45 | |
| 46 | void MethodEntered(art::Thread* thread ATTRIBUTE_UNUSED, |
| 47 | art::Handle<art::mirror::Object> this_object ATTRIBUTE_UNUSED, |
| 48 | art::ArtMethod* method ATTRIBUTE_UNUSED, |
| 49 | uint32_t dex_pc ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 50 | override REQUIRES_SHARED(art::Locks::mutator_lock_) { } |
Alex Light | 673726b | 2018-05-21 14:19:15 -0700 | [diff] [blame] | 51 | |
| 52 | void MethodExited(art::Thread* thread ATTRIBUTE_UNUSED, |
| 53 | art::Handle<art::mirror::Object> this_object ATTRIBUTE_UNUSED, |
| 54 | art::ArtMethod* method ATTRIBUTE_UNUSED, |
| 55 | uint32_t dex_pc ATTRIBUTE_UNUSED, |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 56 | art::instrumentation::OptionalFrame frame ATTRIBUTE_UNUSED, |
| 57 | art::MutableHandle<art::mirror::Object>& return_value ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 58 | override REQUIRES_SHARED(art::Locks::mutator_lock_) { } |
Alex Light | 673726b | 2018-05-21 14:19:15 -0700 | [diff] [blame] | 59 | |
| 60 | void MethodExited(art::Thread* thread ATTRIBUTE_UNUSED, |
| 61 | art::Handle<art::mirror::Object> this_object ATTRIBUTE_UNUSED, |
| 62 | art::ArtMethod* method ATTRIBUTE_UNUSED, |
| 63 | uint32_t dex_pc ATTRIBUTE_UNUSED, |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 64 | art::instrumentation::OptionalFrame frame ATTRIBUTE_UNUSED, |
| 65 | art::JValue& return_value ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 66 | override REQUIRES_SHARED(art::Locks::mutator_lock_) { } |
Alex Light | 673726b | 2018-05-21 14:19:15 -0700 | [diff] [blame] | 67 | |
| 68 | void MethodUnwind(art::Thread* thread ATTRIBUTE_UNUSED, |
| 69 | art::Handle<art::mirror::Object> this_object ATTRIBUTE_UNUSED, |
| 70 | art::ArtMethod* method ATTRIBUTE_UNUSED, |
| 71 | uint32_t dex_pc ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 72 | override REQUIRES_SHARED(art::Locks::mutator_lock_) { } |
Alex Light | 673726b | 2018-05-21 14:19:15 -0700 | [diff] [blame] | 73 | |
| 74 | void DexPcMoved(art::Thread* thread ATTRIBUTE_UNUSED, |
| 75 | art::Handle<art::mirror::Object> this_object ATTRIBUTE_UNUSED, |
| 76 | art::ArtMethod* method ATTRIBUTE_UNUSED, |
| 77 | uint32_t new_dex_pc ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 78 | override REQUIRES_SHARED(art::Locks::mutator_lock_) { } |
Alex Light | 673726b | 2018-05-21 14:19:15 -0700 | [diff] [blame] | 79 | |
| 80 | void FieldRead(art::Thread* thread ATTRIBUTE_UNUSED, |
| 81 | art::Handle<art::mirror::Object> this_object ATTRIBUTE_UNUSED, |
| 82 | art::ArtMethod* method ATTRIBUTE_UNUSED, |
| 83 | uint32_t dex_pc ATTRIBUTE_UNUSED, |
| 84 | art::ArtField* field ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 85 | override REQUIRES_SHARED(art::Locks::mutator_lock_) { } |
Alex Light | 673726b | 2018-05-21 14:19:15 -0700 | [diff] [blame] | 86 | |
| 87 | void FieldWritten(art::Thread* thread ATTRIBUTE_UNUSED, |
| 88 | art::Handle<art::mirror::Object> this_object ATTRIBUTE_UNUSED, |
| 89 | art::ArtMethod* method ATTRIBUTE_UNUSED, |
| 90 | uint32_t dex_pc ATTRIBUTE_UNUSED, |
| 91 | art::ArtField* field ATTRIBUTE_UNUSED, |
| 92 | art::Handle<art::mirror::Object> field_value ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 93 | override REQUIRES_SHARED(art::Locks::mutator_lock_) { } |
Alex Light | 673726b | 2018-05-21 14:19:15 -0700 | [diff] [blame] | 94 | |
| 95 | void FieldWritten(art::Thread* thread ATTRIBUTE_UNUSED, |
| 96 | art::Handle<art::mirror::Object> this_object ATTRIBUTE_UNUSED, |
| 97 | art::ArtMethod* method ATTRIBUTE_UNUSED, |
| 98 | uint32_t dex_pc ATTRIBUTE_UNUSED, |
| 99 | art::ArtField* field ATTRIBUTE_UNUSED, |
| 100 | const art::JValue& field_value ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 101 | override REQUIRES_SHARED(art::Locks::mutator_lock_) { } |
Alex Light | 673726b | 2018-05-21 14:19:15 -0700 | [diff] [blame] | 102 | |
| 103 | void ExceptionThrown(art::Thread* thread ATTRIBUTE_UNUSED, |
| 104 | art::Handle<art::mirror::Throwable> exception_object ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 105 | override REQUIRES_SHARED(art::Locks::mutator_lock_) { } |
Alex Light | 673726b | 2018-05-21 14:19:15 -0700 | [diff] [blame] | 106 | |
| 107 | void ExceptionHandled(art::Thread* self ATTRIBUTE_UNUSED, |
| 108 | art::Handle<art::mirror::Throwable> throwable ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 109 | override REQUIRES_SHARED(art::Locks::mutator_lock_) { } |
Alex Light | 673726b | 2018-05-21 14:19:15 -0700 | [diff] [blame] | 110 | |
| 111 | void Branch(art::Thread* thread ATTRIBUTE_UNUSED, |
| 112 | art::ArtMethod* method ATTRIBUTE_UNUSED, |
| 113 | uint32_t dex_pc ATTRIBUTE_UNUSED, |
| 114 | int32_t dex_pc_offset ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 115 | override REQUIRES_SHARED(art::Locks::mutator_lock_) { } |
Alex Light | 673726b | 2018-05-21 14:19:15 -0700 | [diff] [blame] | 116 | |
Alex Light | 673726b | 2018-05-21 14:19:15 -0700 | [diff] [blame] | 117 | void WatchedFramePop(art::Thread* thread ATTRIBUTE_UNUSED, |
| 118 | const art::ShadowFrame& frame ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 119 | override REQUIRES_SHARED(art::Locks::mutator_lock_) { } |
Alex Light | 673726b | 2018-05-21 14:19:15 -0700 | [diff] [blame] | 120 | |
| 121 | private: |
| 122 | DISALLOW_COPY_AND_ASSIGN(Tracer); |
| 123 | }; |
| 124 | |
| 125 | Tracer gEmptyTracer; |
| 126 | |
| 127 | static void StartTracing() REQUIRES(!art::Locks::mutator_lock_, |
| 128 | !art::Locks::thread_list_lock_, |
| 129 | !art::Locks::thread_suspend_count_lock_) { |
| 130 | art::Thread* self = art::Thread::Current(); |
| 131 | art::Runtime* runtime = art::Runtime::Current(); |
| 132 | art::gc::ScopedGCCriticalSection gcs(self, |
| 133 | art::gc::kGcCauseInstrumentation, |
| 134 | art::gc::kCollectorTypeInstrumentation); |
| 135 | art::ScopedSuspendAll ssa("starting fast tracing"); |
| 136 | runtime->GetInstrumentation()->AddListener(&gEmptyTracer, |
| 137 | art::instrumentation::Instrumentation::kMethodEntered | |
| 138 | art::instrumentation::Instrumentation::kMethodExited | |
| 139 | art::instrumentation::Instrumentation::kMethodUnwind); |
| 140 | runtime->GetInstrumentation()->EnableMethodTracing(kTracerInstrumentationKey, kNeedsInterpreter); |
| 141 | } |
| 142 | |
| 143 | class TraceFastPhaseCB : public art::RuntimePhaseCallback { |
| 144 | public: |
| 145 | TraceFastPhaseCB() {} |
| 146 | |
| 147 | void NextRuntimePhase(art::RuntimePhaseCallback::RuntimePhase phase) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 148 | override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 673726b | 2018-05-21 14:19:15 -0700 | [diff] [blame] | 149 | if (phase == art::RuntimePhaseCallback::RuntimePhase::kInit) { |
| 150 | art::ScopedThreadSuspension sts(art::Thread::Current(), |
| 151 | art::ThreadState::kWaitingForMethodTracingStart); |
| 152 | StartTracing(); |
| 153 | } |
| 154 | } |
| 155 | }; |
| 156 | TraceFastPhaseCB gPhaseCallback; |
| 157 | |
| 158 | // The plugin initialization function. |
Alex Light | 3b08bcc | 2019-09-11 09:48:51 -0700 | [diff] [blame] | 159 | extern "C" bool ArtPlugin_Initialize() { |
Alex Light | 673726b | 2018-05-21 14:19:15 -0700 | [diff] [blame] | 160 | art::Runtime* runtime = art::Runtime::Current(); |
Alex Light | 3b08bcc | 2019-09-11 09:48:51 -0700 | [diff] [blame] | 161 | art::ScopedThreadStateChange stsc(art::Thread::Current(), |
| 162 | art::ThreadState::kWaitingForMethodTracingStart); |
Alex Light | 673726b | 2018-05-21 14:19:15 -0700 | [diff] [blame] | 163 | art::ScopedSuspendAll ssa("Add phase callback"); |
| 164 | runtime->GetRuntimeCallbacks()->AddRuntimePhaseCallback(&gPhaseCallback); |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | extern "C" bool ArtPlugin_Deinitialize() { |
| 169 | // Don't need to bother doing anything. |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | } // namespace tracefast |