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