jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
| 19 | #include <sys/uio.h> |
| 20 | |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 21 | #include "base/unix_file/fd_file.h" |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 22 | #include "class_linker.h" |
| 23 | #include "debugger.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 24 | #include "mirror/class-inl.h" |
| 25 | #include "mirror/dex_cache.h" |
| 26 | #include "mirror/abstract_method-inl.h" |
| 27 | #include "mirror/object_array-inl.h" |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 28 | #if !defined(ART_USE_LLVM_COMPILER) |
| 29 | #include "oat/runtime/oat_support_entrypoints.h" |
| 30 | #endif |
| 31 | #include "object_utils.h" |
| 32 | #include "os.h" |
| 33 | #include "scoped_thread_state_change.h" |
| 34 | #include "thread.h" |
| 35 | #include "thread_list.h" |
| 36 | #include "trace.h" |
| 37 | |
| 38 | namespace art { |
| 39 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 40 | static bool InstallStubsClassVisitor(mirror::Class* klass, void*) |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 41 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 42 | Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 43 | for (size_t i = 0; i < klass->NumDirectMethods(); i++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 44 | mirror::AbstractMethod* method = klass->GetDirectMethod(i); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 45 | if (instrumentation->GetSavedCodeFromMap(method) == NULL) { |
| 46 | instrumentation->SaveAndUpdateCode(method); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | for (size_t i = 0; i < klass->NumVirtualMethods(); i++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 51 | mirror::AbstractMethod* method = klass->GetVirtualMethod(i); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 52 | if (instrumentation->GetSavedCodeFromMap(method) == NULL) { |
| 53 | instrumentation->SaveAndUpdateCode(method); |
| 54 | } |
| 55 | } |
| 56 | return true; |
| 57 | } |
| 58 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 59 | static bool UninstallStubsClassVisitor(mirror::Class* klass, void*) |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 60 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 61 | Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 62 | for (size_t i = 0; i < klass->NumDirectMethods(); i++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 63 | mirror::AbstractMethod* method = klass->GetDirectMethod(i); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 64 | if (instrumentation->GetSavedCodeFromMap(method) != NULL) { |
| 65 | instrumentation->ResetSavedCode(method); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | for (size_t i = 0; i < klass->NumVirtualMethods(); i++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 70 | mirror::AbstractMethod* method = klass->GetVirtualMethod(i); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 71 | if (instrumentation->GetSavedCodeFromMap(method) != NULL) { |
| 72 | instrumentation->ResetSavedCode(method); |
| 73 | } |
| 74 | } |
| 75 | return true; |
| 76 | } |
| 77 | |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 78 | void InstrumentationInstallStack(Thread* self, void* arg) |
| 79 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 80 | struct InstallStackVisitor : public StackVisitor { |
| 81 | InstallStackVisitor(Thread* self, uintptr_t instrumentation_exit_pc) |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 82 | : StackVisitor(self, NULL), self_(self), |
| 83 | instrumentation_exit_pc_(instrumentation_exit_pc) {} |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 84 | |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 85 | virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 86 | if (GetCurrentQuickFrame() == NULL) { |
| 87 | return true; // Ignore shadow frames. |
| 88 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 89 | mirror::AbstractMethod* m = GetMethod(); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 90 | if (m == NULL) { |
| 91 | return true; // Ignore upcalls. |
| 92 | } |
| 93 | if (m->GetDexMethodIndex() == DexFile::kDexNoIndex16) { |
| 94 | return true; // Ignore unresolved methods since they will be instrumented after resolution. |
| 95 | } |
| 96 | uintptr_t pc = GetReturnPc(); |
| 97 | InstrumentationStackFrame instrumentation_frame(m, pc, GetFrameId()); |
| 98 | self_->PushBackInstrumentationStackFrame(instrumentation_frame); |
| 99 | SetReturnPc(instrumentation_exit_pc_); |
| 100 | return true; // Continue. |
| 101 | } |
| 102 | Thread* const self_; |
| 103 | const uintptr_t instrumentation_exit_pc_; |
| 104 | }; |
| 105 | uintptr_t instrumentation_exit_pc = GetInstrumentationExitPc(); |
| 106 | InstallStackVisitor visitor(self, instrumentation_exit_pc); |
| 107 | visitor.WalkStack(true); |
| 108 | Trace* trace = reinterpret_cast<Trace*>(arg); |
| 109 | if (trace != NULL) { |
| 110 | std::deque<InstrumentationStackFrame>::const_reverse_iterator it = |
| 111 | self->GetInstrumentationStack()->rbegin(); |
| 112 | std::deque<InstrumentationStackFrame>::const_reverse_iterator end = |
| 113 | self->GetInstrumentationStack()->rend(); |
| 114 | for (; it != end; ++it) { |
| 115 | trace->LogMethodTraceEvent(self, (*it).method_, Trace::kMethodTraceEnter); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | static void InstrumentationRestoreStack(Thread* self, void*) |
| 121 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 122 | struct RestoreStackVisitor : public StackVisitor { |
| 123 | RestoreStackVisitor(Thread* self, uintptr_t instrumentation_exit_pc) |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 124 | : StackVisitor(self, NULL), self_(self), |
| 125 | instrumentation_exit_pc_(instrumentation_exit_pc) {} |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 126 | |
| 127 | virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 128 | if (self_->IsInstrumentationStackEmpty()) { |
| 129 | return false; // Stop. |
| 130 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 131 | mirror::AbstractMethod* m = GetMethod(); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 132 | if (m == NULL) { |
| 133 | return true; // Ignore upcalls. |
| 134 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 135 | uintptr_t pc = GetReturnPc(); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 136 | if (pc == instrumentation_exit_pc_) { |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 137 | InstrumentationStackFrame instrumentation_frame = self_->PopInstrumentationStackFrame(); |
| 138 | SetReturnPc(instrumentation_frame.return_pc_); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 139 | CHECK(m == instrumentation_frame.method_); |
| 140 | CHECK_EQ(GetFrameId(), instrumentation_frame.frame_id_); |
| 141 | Runtime* runtime = Runtime::Current(); |
| 142 | if (runtime->IsMethodTracingActive()) { |
| 143 | Trace* trace = runtime->GetInstrumentation()->GetTrace(); |
| 144 | trace->LogMethodTraceEvent(self_, m, Trace::kMethodTraceExit); |
| 145 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 146 | } |
| 147 | return true; // Continue. |
| 148 | } |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 149 | Thread* const self_; |
| 150 | const uintptr_t instrumentation_exit_pc_; |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 151 | }; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 152 | uintptr_t instrumentation_exit_pc = GetInstrumentationExitPc(); |
| 153 | RestoreStackVisitor visitor(self, instrumentation_exit_pc); |
| 154 | visitor.WalkStack(true); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | Instrumentation::~Instrumentation() { |
| 158 | delete trace_; |
| 159 | } |
| 160 | |
| 161 | void Instrumentation::InstallStubs() { |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 162 | Thread* self = Thread::Current(); |
| 163 | Locks::thread_list_lock_->AssertNotHeld(self); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 164 | Runtime::Current()->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, NULL); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 165 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 166 | Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, GetTrace()); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void Instrumentation::UninstallStubs() { |
| 170 | Thread* self = Thread::Current(); |
| 171 | Locks::thread_list_lock_->AssertNotHeld(self); |
| 172 | Runtime::Current()->GetClassLinker()->VisitClasses(UninstallStubsClassVisitor, NULL); |
| 173 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 174 | Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, NULL); |
| 175 | } |
| 176 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 177 | void Instrumentation::AddSavedCodeToMap(const mirror::AbstractMethod* method, const void* code) { |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 178 | saved_code_map_.Put(method, code); |
| 179 | } |
| 180 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 181 | void Instrumentation::RemoveSavedCodeFromMap(const mirror::AbstractMethod* method) { |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 182 | saved_code_map_.erase(method); |
| 183 | } |
| 184 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 185 | const void* Instrumentation::GetSavedCodeFromMap(const mirror::AbstractMethod* method) { |
| 186 | typedef SafeMap<const mirror::AbstractMethod*, const void*>::const_iterator It; // TODO: C++0x auto |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 187 | It it = saved_code_map_.find(method); |
| 188 | if (it == saved_code_map_.end()) { |
| 189 | return NULL; |
| 190 | } else { |
| 191 | return it->second; |
| 192 | } |
| 193 | } |
| 194 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 195 | void Instrumentation::SaveAndUpdateCode(mirror::AbstractMethod* method) { |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 196 | #if defined(ART_USE_LLVM_COMPILER) |
| 197 | UNUSED(method); |
| 198 | UNIMPLEMENTED(FATAL); |
| 199 | #else |
| 200 | void* instrumentation_stub = GetInstrumentationEntryPoint(); |
| 201 | CHECK(GetSavedCodeFromMap(method) == NULL); |
| 202 | AddSavedCodeToMap(method, method->GetCode()); |
| 203 | method->SetCode(instrumentation_stub); |
| 204 | #endif |
| 205 | } |
| 206 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 207 | void Instrumentation::ResetSavedCode(mirror::AbstractMethod* method) { |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 208 | CHECK(GetSavedCodeFromMap(method) != NULL); |
| 209 | method->SetCode(GetSavedCodeFromMap(method)); |
| 210 | RemoveSavedCodeFromMap(method); |
| 211 | } |
| 212 | |
| 213 | Trace* Instrumentation::GetTrace() const { |
| 214 | return trace_; |
| 215 | } |
| 216 | |
| 217 | void Instrumentation::SetTrace(Trace* trace) { |
| 218 | trace_ = trace; |
| 219 | } |
| 220 | |
| 221 | void Instrumentation::RemoveTrace() { |
| 222 | delete trace_; |
| 223 | trace_ = NULL; |
| 224 | } |
| 225 | |
| 226 | uint32_t InstrumentationMethodUnwindFromCode(Thread* self) { |
| 227 | Trace* trace = Runtime::Current()->GetInstrumentation()->GetTrace(); |
| 228 | InstrumentationStackFrame instrumentation_frame = self->PopInstrumentationStackFrame(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 229 | mirror::AbstractMethod* method = instrumentation_frame.method_; |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 230 | uint32_t lr = instrumentation_frame.return_pc_; |
| 231 | |
| 232 | trace->LogMethodTraceEvent(self, method, Trace::kMethodTraceUnwind); |
| 233 | |
| 234 | return lr; |
| 235 | } |
| 236 | |
| 237 | } // namespace art |