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" |
| 24 | #include "dex_cache.h" |
| 25 | #if !defined(ART_USE_LLVM_COMPILER) |
| 26 | #include "oat/runtime/oat_support_entrypoints.h" |
| 27 | #endif |
| 28 | #include "object_utils.h" |
| 29 | #include "os.h" |
| 30 | #include "scoped_thread_state_change.h" |
| 31 | #include "thread.h" |
| 32 | #include "thread_list.h" |
| 33 | #include "trace.h" |
| 34 | |
| 35 | namespace art { |
| 36 | |
| 37 | static bool InstallStubsClassVisitor(Class* klass, void*) |
| 38 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 39 | Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 40 | for (size_t i = 0; i < klass->NumDirectMethods(); i++) { |
| 41 | AbstractMethod* method = klass->GetDirectMethod(i); |
| 42 | if (instrumentation->GetSavedCodeFromMap(method) == NULL) { |
| 43 | instrumentation->SaveAndUpdateCode(method); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | for (size_t i = 0; i < klass->NumVirtualMethods(); i++) { |
| 48 | AbstractMethod* method = klass->GetVirtualMethod(i); |
| 49 | if (instrumentation->GetSavedCodeFromMap(method) == NULL) { |
| 50 | instrumentation->SaveAndUpdateCode(method); |
| 51 | } |
| 52 | } |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | static bool UninstallStubsClassVisitor(Class* klass, void*) |
| 57 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 58 | Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 59 | for (size_t i = 0; i < klass->NumDirectMethods(); i++) { |
| 60 | AbstractMethod* method = klass->GetDirectMethod(i); |
| 61 | if (instrumentation->GetSavedCodeFromMap(method) != NULL) { |
| 62 | instrumentation->ResetSavedCode(method); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | for (size_t i = 0; i < klass->NumVirtualMethods(); i++) { |
| 67 | AbstractMethod* method = klass->GetVirtualMethod(i); |
| 68 | if (instrumentation->GetSavedCodeFromMap(method) != NULL) { |
| 69 | instrumentation->ResetSavedCode(method); |
| 70 | } |
| 71 | } |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | static void InstrumentationRestoreStack(Thread* self, void*) NO_THREAD_SAFETY_ANALYSIS { |
| 76 | struct RestoreStackVisitor : public StackVisitor { |
| 77 | RestoreStackVisitor(Thread* self) |
| 78 | : StackVisitor(self->GetManagedStack(), self->GetInstrumentationStack(), NULL), self_(self) {} |
| 79 | |
| 80 | virtual bool VisitFrame() { |
| 81 | if (self_->IsInstrumentationStackEmpty()) { |
| 82 | return false; // Stop. |
| 83 | } |
| 84 | uintptr_t pc = GetReturnPc(); |
| 85 | if (IsInstrumentationExitPc(pc)) { |
| 86 | InstrumentationStackFrame instrumentation_frame = self_->PopInstrumentationStackFrame(); |
| 87 | SetReturnPc(instrumentation_frame.return_pc_); |
| 88 | CHECK(GetMethod() == instrumentation_frame.method_); |
| 89 | } |
| 90 | return true; // Continue. |
| 91 | } |
| 92 | |
| 93 | Thread* self_; |
| 94 | }; |
| 95 | RestoreStackVisitor visitor(self); |
| 96 | visitor.WalkStack(); |
| 97 | } |
| 98 | |
| 99 | Instrumentation::~Instrumentation() { |
| 100 | delete trace_; |
| 101 | } |
| 102 | |
| 103 | void Instrumentation::InstallStubs() { |
| 104 | Runtime::Current()->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, NULL); |
| 105 | } |
| 106 | |
| 107 | void Instrumentation::UninstallStubs() { |
| 108 | Thread* self = Thread::Current(); |
| 109 | Locks::thread_list_lock_->AssertNotHeld(self); |
| 110 | Runtime::Current()->GetClassLinker()->VisitClasses(UninstallStubsClassVisitor, NULL); |
| 111 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 112 | Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, NULL); |
| 113 | } |
| 114 | |
| 115 | void Instrumentation::AddSavedCodeToMap(const AbstractMethod* method, const void* code) { |
| 116 | saved_code_map_.Put(method, code); |
| 117 | } |
| 118 | |
| 119 | void Instrumentation::RemoveSavedCodeFromMap(const AbstractMethod* method) { |
| 120 | saved_code_map_.erase(method); |
| 121 | } |
| 122 | |
| 123 | const void* Instrumentation::GetSavedCodeFromMap(const AbstractMethod* method) { |
| 124 | typedef SafeMap<const AbstractMethod*, const void*>::const_iterator It; // TODO: C++0x auto |
| 125 | It it = saved_code_map_.find(method); |
| 126 | if (it == saved_code_map_.end()) { |
| 127 | return NULL; |
| 128 | } else { |
| 129 | return it->second; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | void Instrumentation::SaveAndUpdateCode(AbstractMethod* method) { |
| 134 | #if defined(ART_USE_LLVM_COMPILER) |
| 135 | UNUSED(method); |
| 136 | UNIMPLEMENTED(FATAL); |
| 137 | #else |
| 138 | void* instrumentation_stub = GetInstrumentationEntryPoint(); |
| 139 | CHECK(GetSavedCodeFromMap(method) == NULL); |
| 140 | AddSavedCodeToMap(method, method->GetCode()); |
| 141 | method->SetCode(instrumentation_stub); |
| 142 | #endif |
| 143 | } |
| 144 | |
| 145 | void Instrumentation::ResetSavedCode(AbstractMethod* method) { |
| 146 | CHECK(GetSavedCodeFromMap(method) != NULL); |
| 147 | method->SetCode(GetSavedCodeFromMap(method)); |
| 148 | RemoveSavedCodeFromMap(method); |
| 149 | } |
| 150 | |
| 151 | Trace* Instrumentation::GetTrace() const { |
| 152 | return trace_; |
| 153 | } |
| 154 | |
| 155 | void Instrumentation::SetTrace(Trace* trace) { |
| 156 | trace_ = trace; |
| 157 | } |
| 158 | |
| 159 | void Instrumentation::RemoveTrace() { |
| 160 | delete trace_; |
| 161 | trace_ = NULL; |
| 162 | } |
| 163 | |
| 164 | uint32_t InstrumentationMethodUnwindFromCode(Thread* self) { |
| 165 | Trace* trace = Runtime::Current()->GetInstrumentation()->GetTrace(); |
| 166 | InstrumentationStackFrame instrumentation_frame = self->PopInstrumentationStackFrame(); |
| 167 | AbstractMethod* method = instrumentation_frame.method_; |
| 168 | uint32_t lr = instrumentation_frame.return_pc_; |
| 169 | |
| 170 | trace->LogMethodTraceEvent(self, method, Trace::kMethodTraceUnwind); |
| 171 | |
| 172 | return lr; |
| 173 | } |
| 174 | |
| 175 | } // namespace art |