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 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 21 | #include "arch/context.h" |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 22 | #include "atomic.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 23 | #include "base/unix_file/fd_file.h" |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 24 | #include "class_linker.h" |
| 25 | #include "debugger.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 26 | #include "dex_file-inl.h" |
Mathieu Chartier | d889178 | 2014-03-02 13:28:37 -0800 | [diff] [blame] | 27 | #include "entrypoints/quick/quick_alloc_entrypoints.h" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 28 | #include "gc_root-inl.h" |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 29 | #include "interpreter/interpreter.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 30 | #include "mirror/art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 31 | #include "mirror/class-inl.h" |
| 32 | #include "mirror/dex_cache.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 33 | #include "mirror/object_array-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 34 | #include "mirror/object-inl.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 35 | #include "nth_caller_visitor.h" |
Ian Rogers | c928de9 | 2013-02-27 14:30:44 -0800 | [diff] [blame] | 36 | #if !defined(ART_USE_PORTABLE_COMPILER) |
Ian Rogers | 166db04 | 2013-07-26 12:05:57 -0700 | [diff] [blame] | 37 | #include "entrypoints/quick/quick_entrypoints.h" |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 38 | #endif |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 39 | #include "os.h" |
| 40 | #include "scoped_thread_state_change.h" |
| 41 | #include "thread.h" |
| 42 | #include "thread_list.h" |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 43 | |
| 44 | namespace art { |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 45 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 46 | namespace instrumentation { |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 47 | |
Sebastien Hertz | 5bfd5c9 | 2013-11-15 11:36:07 +0100 | [diff] [blame] | 48 | const bool kVerboseInstrumentation = false; |
| 49 | |
Ian Rogers | 816432e | 2013-09-06 15:47:45 -0700 | [diff] [blame] | 50 | // Do we want to deoptimize for method entry and exit listeners or just try to intercept |
| 51 | // invocations? Deoptimization forces all code to run in the interpreter and considerably hurts the |
| 52 | // application's performance. |
Jeff Hao | b21f133 | 2014-08-11 18:00:29 -0700 | [diff] [blame] | 53 | static constexpr bool kDeoptimizeForAccurateMethodEntryExitListeners = true; |
Ian Rogers | 816432e | 2013-09-06 15:47:45 -0700 | [diff] [blame] | 54 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 55 | static bool InstallStubsClassVisitor(mirror::Class* klass, void* arg) |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 56 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 57 | Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg); |
| 58 | return instrumentation->InstallStubsForClass(klass); |
| 59 | } |
| 60 | |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 61 | Instrumentation::Instrumentation() |
| 62 | : instrumentation_stubs_installed_(false), entry_exit_stubs_installed_(false), |
| 63 | interpreter_stubs_installed_(false), |
| 64 | interpret_only_(false), forced_interpret_only_(false), |
| 65 | have_method_entry_listeners_(false), have_method_exit_listeners_(false), |
| 66 | have_method_unwind_listeners_(false), have_dex_pc_listeners_(false), |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 67 | have_field_read_listeners_(false), have_field_write_listeners_(false), |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 68 | have_exception_caught_listeners_(false), |
| 69 | deoptimized_methods_lock_("deoptimized methods lock"), |
| 70 | deoptimization_enabled_(false), |
| 71 | interpreter_handler_table_(kMainHandlerTable), |
| 72 | quick_alloc_entry_points_instrumentation_counter_(0) { |
| 73 | } |
| 74 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 75 | bool Instrumentation::InstallStubsForClass(mirror::Class* klass) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 76 | for (size_t i = 0, e = klass->NumDirectMethods(); i < e; i++) { |
| 77 | InstallStubsForMethod(klass->GetDirectMethod(i)); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 78 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 79 | for (size_t i = 0, e = klass->NumVirtualMethods(); i < e; i++) { |
| 80 | InstallStubsForMethod(klass->GetVirtualMethod(i)); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 81 | } |
| 82 | return true; |
| 83 | } |
| 84 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 85 | static void UpdateEntrypoints(mirror::ArtMethod* method, const void* quick_code, |
| 86 | const void* portable_code, bool have_portable_code) |
| 87 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 88 | #if defined(ART_USE_PORTABLE_COMPILER) |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 89 | method->SetEntryPointFromPortableCompiledCode(portable_code); |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 90 | #endif |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 91 | method->SetEntryPointFromQuickCompiledCode(quick_code); |
| 92 | bool portable_enabled = method->IsPortableCompiled(); |
| 93 | if (have_portable_code && !portable_enabled) { |
| 94 | method->SetIsPortableCompiled(); |
| 95 | } else if (portable_enabled) { |
| 96 | method->ClearIsPortableCompiled(); |
| 97 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 98 | if (!method->IsResolutionMethod()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 99 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Hiroshi Yamauchi | 563b47c | 2014-02-28 17:18:37 -0800 | [diff] [blame] | 100 | if (quick_code == GetQuickToInterpreterBridge() || |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 101 | quick_code == class_linker->GetQuickToInterpreterBridgeTrampoline() || |
| 102 | (quick_code == class_linker->GetQuickResolutionTrampoline() && |
Hiroshi Yamauchi | 563b47c | 2014-02-28 17:18:37 -0800 | [diff] [blame] | 103 | Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly() |
| 104 | && !method->IsNative() && !method->IsProxyMethod())) { |
| 105 | if (kIsDebugBuild) { |
| 106 | if (quick_code == GetQuickToInterpreterBridge()) { |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 107 | #if defined(ART_USE_PORTABLE_COMPILER) |
Hiroshi Yamauchi | 563b47c | 2014-02-28 17:18:37 -0800 | [diff] [blame] | 108 | DCHECK(portable_code == GetPortableToInterpreterBridge()); |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 109 | #endif |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 110 | } else if (quick_code == class_linker->GetQuickResolutionTrampoline()) { |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 111 | #if defined(ART_USE_PORTABLE_COMPILER) |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 112 | DCHECK(portable_code == class_linker->GetPortableResolutionTrampoline()); |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 113 | #endif |
Hiroshi Yamauchi | 563b47c | 2014-02-28 17:18:37 -0800 | [diff] [blame] | 114 | } |
| 115 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 116 | DCHECK(!method->IsNative()) << PrettyMethod(method); |
Hiroshi Yamauchi | 563b47c | 2014-02-28 17:18:37 -0800 | [diff] [blame] | 117 | DCHECK(!method->IsProxyMethod()) << PrettyMethod(method); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 118 | method->SetEntryPointFromInterpreter(art::interpreter::artInterpreterToInterpreterBridge); |
| 119 | } else { |
| 120 | method->SetEntryPointFromInterpreter(art::artInterpreterToCompiledCodeBridge); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | void Instrumentation::InstallStubsForMethod(mirror::ArtMethod* method) { |
| 126 | if (method->IsAbstract() || method->IsProxyMethod()) { |
| 127 | // Do not change stubs for these methods. |
| 128 | return; |
| 129 | } |
Jeff Hao | 5afe466 | 2014-08-19 10:17:36 -0700 | [diff] [blame] | 130 | // Don't stub Proxy.<init>. Note that the Proxy class itself is not a proxy class. |
| 131 | if (method->IsConstructor() && |
| 132 | method->GetDeclaringClass()->DescriptorEquals("Ljava/lang/reflect/Proxy;")) { |
Jeff Hao | 95b4c65 | 2014-08-14 17:18:52 -0700 | [diff] [blame] | 133 | return; |
| 134 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 135 | const void* new_portable_code; |
| 136 | const void* new_quick_code; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 137 | bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_; |
| 138 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 139 | bool is_class_initialized = method->GetDeclaringClass()->IsInitialized(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 140 | bool have_portable_code = false; |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 141 | #if !defined(ART_USE_PORTABLE_COMPILER) |
| 142 | new_portable_code = nullptr; |
| 143 | #endif |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 144 | if (uninstall) { |
| 145 | if ((forced_interpret_only_ || IsDeoptimized(method)) && !method->IsNative()) { |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 146 | #if defined(ART_USE_PORTABLE_COMPILER) |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 147 | new_portable_code = GetPortableToInterpreterBridge(); |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 148 | #endif |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 149 | new_quick_code = GetQuickToInterpreterBridge(); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 150 | } else if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) { |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 151 | #if defined(ART_USE_PORTABLE_COMPILER) |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 152 | new_portable_code = class_linker->GetPortableOatCodeFor(method, &have_portable_code); |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 153 | #endif |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 154 | new_quick_code = class_linker->GetQuickOatCodeFor(method); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 155 | } else { |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 156 | #if defined(ART_USE_PORTABLE_COMPILER) |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 157 | new_portable_code = class_linker->GetPortableResolutionTrampoline(); |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 158 | #endif |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 159 | new_quick_code = class_linker->GetQuickResolutionTrampoline(); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 160 | } |
| 161 | } else { // !uninstall |
Sebastien Hertz | bae182c | 2013-12-17 10:42:03 +0100 | [diff] [blame] | 162 | if ((interpreter_stubs_installed_ || forced_interpret_only_ || IsDeoptimized(method)) && |
| 163 | !method->IsNative()) { |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 164 | #if defined(ART_USE_PORTABLE_COMPILER) |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 165 | new_portable_code = GetPortableToInterpreterBridge(); |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 166 | #endif |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 167 | new_quick_code = GetQuickToInterpreterBridge(); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 168 | } else { |
| 169 | // Do not overwrite resolution trampoline. When the trampoline initializes the method's |
| 170 | // class, all its static methods code will be set to the instrumentation entry point. |
| 171 | // For more details, see ClassLinker::FixupStaticTrampolines. |
| 172 | if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) { |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 173 | if (entry_exit_stubs_installed_) { |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 174 | #if defined(ART_USE_PORTABLE_COMPILER) |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 175 | new_portable_code = GetPortableToInterpreterBridge(); |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 176 | #endif |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 177 | new_quick_code = GetQuickInstrumentationEntryPoint(); |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 178 | } else { |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 179 | #if defined(ART_USE_PORTABLE_COMPILER) |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 180 | new_portable_code = class_linker->GetPortableOatCodeFor(method, &have_portable_code); |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 181 | #endif |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 182 | new_quick_code = class_linker->GetQuickOatCodeFor(method); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 183 | DCHECK(new_quick_code != class_linker->GetQuickToInterpreterBridgeTrampoline()); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 184 | } |
| 185 | } else { |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 186 | #if defined(ART_USE_PORTABLE_COMPILER) |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 187 | new_portable_code = class_linker->GetPortableResolutionTrampoline(); |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 188 | #endif |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 189 | new_quick_code = class_linker->GetQuickResolutionTrampoline(); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 193 | UpdateEntrypoints(method, new_quick_code, new_portable_code, have_portable_code); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 196 | // Places the instrumentation exit pc as the return PC for every quick frame. This also allows |
| 197 | // deoptimization of quick frames to interpreter frames. |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 198 | // Since we may already have done this previously, we need to push new instrumentation frame before |
| 199 | // existing instrumentation frames. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 200 | static void InstrumentationInstallStack(Thread* thread, void* arg) |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 201 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 202 | struct InstallStackVisitor : public StackVisitor { |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 203 | InstallStackVisitor(Thread* thread, Context* context, uintptr_t instrumentation_exit_pc) |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 204 | : StackVisitor(thread, context), instrumentation_stack_(thread->GetInstrumentationStack()), |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 205 | instrumentation_exit_pc_(instrumentation_exit_pc), |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 206 | reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0), |
| 207 | last_return_pc_(0) { |
| 208 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 209 | |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 210 | virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 211 | mirror::ArtMethod* m = GetMethod(); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 212 | if (m == NULL) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 213 | if (kVerboseInstrumentation) { |
| 214 | LOG(INFO) << " Skipping upcall. Frame " << GetFrameId(); |
| 215 | } |
| 216 | last_return_pc_ = 0; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 217 | return true; // Ignore upcalls. |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 218 | } |
Jeff Hao | a15a81b | 2014-05-27 18:25:47 -0700 | [diff] [blame] | 219 | if (GetCurrentQuickFrame() == NULL) { |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 220 | bool interpreter_frame = !m->IsPortableCompiled(); |
| 221 | InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(), |
| 222 | interpreter_frame); |
Jeff Hao | a15a81b | 2014-05-27 18:25:47 -0700 | [diff] [blame] | 223 | if (kVerboseInstrumentation) { |
| 224 | LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump(); |
| 225 | } |
| 226 | shadow_stack_.push_back(instrumentation_frame); |
| 227 | return true; // Continue. |
| 228 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 229 | uintptr_t return_pc = GetReturnPc(); |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 230 | if (m->IsRuntimeMethod()) { |
| 231 | if (return_pc == instrumentation_exit_pc_) { |
| 232 | if (kVerboseInstrumentation) { |
| 233 | LOG(INFO) << " Handling quick to interpreter transition. Frame " << GetFrameId(); |
| 234 | } |
| 235 | CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size()); |
Daniel Mihalyi | 96add97 | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 236 | const InstrumentationStackFrame& frame = |
| 237 | instrumentation_stack_->at(instrumentation_stack_depth_); |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 238 | CHECK(frame.interpreter_entry_); |
| 239 | // This is an interpreter frame so method enter event must have been reported. However we |
| 240 | // need to push a DEX pc into the dex_pcs_ list to match size of instrumentation stack. |
| 241 | // Since we won't report method entry here, we can safely push any DEX pc. |
| 242 | dex_pcs_.push_back(0); |
| 243 | last_return_pc_ = frame.return_pc_; |
| 244 | ++instrumentation_stack_depth_; |
| 245 | return true; |
| 246 | } else { |
| 247 | if (kVerboseInstrumentation) { |
| 248 | LOG(INFO) << " Skipping runtime method. Frame " << GetFrameId(); |
| 249 | } |
| 250 | last_return_pc_ = GetReturnPc(); |
| 251 | return true; // Ignore unresolved methods since they will be instrumented after resolution. |
| 252 | } |
| 253 | } |
| 254 | if (kVerboseInstrumentation) { |
| 255 | LOG(INFO) << " Installing exit stub in " << DescribeLocation(); |
| 256 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 257 | if (return_pc == instrumentation_exit_pc_) { |
| 258 | // We've reached a frame which has already been installed with instrumentation exit stub. |
| 259 | // We should have already installed instrumentation on previous frames. |
| 260 | reached_existing_instrumentation_frames_ = true; |
| 261 | |
| 262 | CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size()); |
Daniel Mihalyi | 96add97 | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 263 | const InstrumentationStackFrame& frame = |
| 264 | instrumentation_stack_->at(instrumentation_stack_depth_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 265 | CHECK_EQ(m, frame.method_) << "Expected " << PrettyMethod(m) |
| 266 | << ", Found " << PrettyMethod(frame.method_); |
| 267 | return_pc = frame.return_pc_; |
| 268 | if (kVerboseInstrumentation) { |
| 269 | LOG(INFO) << "Ignoring already instrumented " << frame.Dump(); |
| 270 | } |
| 271 | } else { |
| 272 | CHECK_NE(return_pc, 0U); |
| 273 | CHECK(!reached_existing_instrumentation_frames_); |
| 274 | InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, return_pc, GetFrameId(), |
| 275 | false); |
| 276 | if (kVerboseInstrumentation) { |
| 277 | LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump(); |
| 278 | } |
| 279 | |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 280 | // Insert frame at the right position so we do not corrupt the instrumentation stack. |
| 281 | // Instrumentation stack frames are in descending frame id order. |
| 282 | auto it = instrumentation_stack_->begin(); |
| 283 | for (auto end = instrumentation_stack_->end(); it != end; ++it) { |
| 284 | const InstrumentationStackFrame& current = *it; |
| 285 | if (instrumentation_frame.frame_id_ >= current.frame_id_) { |
| 286 | break; |
| 287 | } |
| 288 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 289 | instrumentation_stack_->insert(it, instrumentation_frame); |
| 290 | SetReturnPc(instrumentation_exit_pc_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 291 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 292 | dex_pcs_.push_back(m->ToDexPc(last_return_pc_)); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 293 | last_return_pc_ = return_pc; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 294 | ++instrumentation_stack_depth_; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 295 | return true; // Continue. |
| 296 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 297 | std::deque<InstrumentationStackFrame>* const instrumentation_stack_; |
Jeff Hao | a15a81b | 2014-05-27 18:25:47 -0700 | [diff] [blame] | 298 | std::vector<InstrumentationStackFrame> shadow_stack_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 299 | std::vector<uint32_t> dex_pcs_; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 300 | const uintptr_t instrumentation_exit_pc_; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 301 | bool reached_existing_instrumentation_frames_; |
| 302 | size_t instrumentation_stack_depth_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 303 | uintptr_t last_return_pc_; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 304 | }; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 305 | if (kVerboseInstrumentation) { |
| 306 | std::string thread_name; |
| 307 | thread->GetThreadName(thread_name); |
| 308 | LOG(INFO) << "Installing exit stubs in " << thread_name; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 309 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 310 | |
| 311 | Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg); |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 312 | std::unique_ptr<Context> context(Context::Create()); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 313 | uintptr_t instrumentation_exit_pc = GetQuickInstrumentationExitPc(); |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 314 | InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 315 | visitor.WalkStack(true); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 316 | CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 317 | |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 318 | if (instrumentation->ShouldNotifyMethodEnterExitEvents()) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 319 | // Create method enter events for all methods currently on the thread's stack. We only do this |
| 320 | // if no debugger is attached to prevent from posting events twice. |
Jeff Hao | a15a81b | 2014-05-27 18:25:47 -0700 | [diff] [blame] | 321 | auto ssi = visitor.shadow_stack_.rbegin(); |
| 322 | for (auto isi = thread->GetInstrumentationStack()->rbegin(), |
| 323 | end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) { |
| 324 | while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) { |
| 325 | instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0); |
| 326 | ++ssi; |
| 327 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 328 | uint32_t dex_pc = visitor.dex_pcs_.back(); |
| 329 | visitor.dex_pcs_.pop_back(); |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 330 | if (!isi->interpreter_entry_) { |
| 331 | instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc); |
| 332 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 333 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 334 | } |
| 335 | thread->VerifyStack(); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 336 | } |
| 337 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 338 | // Removes the instrumentation exit pc as the return PC for every quick frame. |
| 339 | static void InstrumentationRestoreStack(Thread* thread, void* arg) |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 340 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 341 | struct RestoreStackVisitor : public StackVisitor { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 342 | RestoreStackVisitor(Thread* thread, uintptr_t instrumentation_exit_pc, |
| 343 | Instrumentation* instrumentation) |
| 344 | : StackVisitor(thread, NULL), thread_(thread), |
| 345 | instrumentation_exit_pc_(instrumentation_exit_pc), |
| 346 | instrumentation_(instrumentation), |
| 347 | instrumentation_stack_(thread->GetInstrumentationStack()), |
| 348 | frames_removed_(0) {} |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 349 | |
| 350 | virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 351 | if (instrumentation_stack_->size() == 0) { |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 352 | return false; // Stop. |
| 353 | } |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 354 | mirror::ArtMethod* m = GetMethod(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 355 | if (GetCurrentQuickFrame() == NULL) { |
| 356 | if (kVerboseInstrumentation) { |
Daniel Mihalyi | 96add97 | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 357 | LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId() |
| 358 | << " Method=" << PrettyMethod(m); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 359 | } |
| 360 | return true; // Ignore shadow frames. |
| 361 | } |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 362 | if (m == NULL) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 363 | if (kVerboseInstrumentation) { |
| 364 | LOG(INFO) << " Skipping upcall. Frame " << GetFrameId(); |
| 365 | } |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 366 | return true; // Ignore upcalls. |
| 367 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 368 | bool removed_stub = false; |
| 369 | // TODO: make this search more efficient? |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 370 | const size_t frameId = GetFrameId(); |
| 371 | for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) { |
| 372 | if (instrumentation_frame.frame_id_ == frameId) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 373 | if (kVerboseInstrumentation) { |
| 374 | LOG(INFO) << " Removing exit stub in " << DescribeLocation(); |
| 375 | } |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 376 | if (instrumentation_frame.interpreter_entry_) { |
| 377 | CHECK(m == Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)); |
| 378 | } else { |
| 379 | CHECK(m == instrumentation_frame.method_) << PrettyMethod(m); |
| 380 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 381 | SetReturnPc(instrumentation_frame.return_pc_); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 382 | if (instrumentation_->ShouldNotifyMethodEnterExitEvents()) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 383 | // Create the method exit events. As the methods didn't really exit the result is 0. |
| 384 | // We only do this if no debugger is attached to prevent from posting events twice. |
| 385 | instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m, |
| 386 | GetDexPc(), JValue()); |
| 387 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 388 | frames_removed_++; |
| 389 | removed_stub = true; |
| 390 | break; |
| 391 | } |
| 392 | } |
| 393 | if (!removed_stub) { |
| 394 | if (kVerboseInstrumentation) { |
| 395 | LOG(INFO) << " No exit stub in " << DescribeLocation(); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 396 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 397 | } |
| 398 | return true; // Continue. |
| 399 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 400 | Thread* const thread_; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 401 | const uintptr_t instrumentation_exit_pc_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 402 | Instrumentation* const instrumentation_; |
| 403 | std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_; |
| 404 | size_t frames_removed_; |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 405 | }; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 406 | if (kVerboseInstrumentation) { |
| 407 | std::string thread_name; |
| 408 | thread->GetThreadName(thread_name); |
| 409 | LOG(INFO) << "Removing exit stubs in " << thread_name; |
| 410 | } |
| 411 | std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack(); |
| 412 | if (stack->size() > 0) { |
| 413 | Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 414 | uintptr_t instrumentation_exit_pc = GetQuickInstrumentationExitPc(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 415 | RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation); |
| 416 | visitor.WalkStack(true); |
| 417 | CHECK_EQ(visitor.frames_removed_, stack->size()); |
| 418 | while (stack->size() > 0) { |
| 419 | stack->pop_front(); |
| 420 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 424 | void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) { |
| 425 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 426 | if ((events & kMethodEntered) != 0) { |
| 427 | method_entry_listeners_.push_back(listener); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 428 | have_method_entry_listeners_ = true; |
| 429 | } |
| 430 | if ((events & kMethodExited) != 0) { |
| 431 | method_exit_listeners_.push_back(listener); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 432 | have_method_exit_listeners_ = true; |
| 433 | } |
| 434 | if ((events & kMethodUnwind) != 0) { |
| 435 | method_unwind_listeners_.push_back(listener); |
| 436 | have_method_unwind_listeners_ = true; |
| 437 | } |
| 438 | if ((events & kDexPcMoved) != 0) { |
Daniel Mihalyi | 96add97 | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 439 | std::list<InstrumentationListener*>* modified; |
| 440 | if (have_dex_pc_listeners_) { |
| 441 | modified = new std::list<InstrumentationListener*>(*dex_pc_listeners_.get()); |
| 442 | } else { |
| 443 | modified = new std::list<InstrumentationListener*>(); |
| 444 | } |
| 445 | modified->push_back(listener); |
| 446 | dex_pc_listeners_.reset(modified); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 447 | have_dex_pc_listeners_ = true; |
| 448 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 449 | if ((events & kFieldRead) != 0) { |
Daniel Mihalyi | 96add97 | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 450 | std::list<InstrumentationListener*>* modified; |
| 451 | if (have_field_read_listeners_) { |
| 452 | modified = new std::list<InstrumentationListener*>(*field_read_listeners_.get()); |
| 453 | } else { |
| 454 | modified = new std::list<InstrumentationListener*>(); |
| 455 | } |
| 456 | modified->push_back(listener); |
| 457 | field_read_listeners_.reset(modified); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 458 | have_field_read_listeners_ = true; |
| 459 | } |
| 460 | if ((events & kFieldWritten) != 0) { |
Daniel Mihalyi | 96add97 | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 461 | std::list<InstrumentationListener*>* modified; |
| 462 | if (have_field_write_listeners_) { |
| 463 | modified = new std::list<InstrumentationListener*>(*field_write_listeners_.get()); |
| 464 | } else { |
| 465 | modified = new std::list<InstrumentationListener*>(); |
| 466 | } |
| 467 | modified->push_back(listener); |
| 468 | field_write_listeners_.reset(modified); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 469 | have_field_write_listeners_ = true; |
| 470 | } |
Jeff Hao | 14dd5a8 | 2013-04-11 10:23:36 -0700 | [diff] [blame] | 471 | if ((events & kExceptionCaught) != 0) { |
Daniel Mihalyi | 96add97 | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 472 | std::list<InstrumentationListener*>* modified; |
| 473 | if (have_exception_caught_listeners_) { |
| 474 | modified = new std::list<InstrumentationListener*>(*exception_caught_listeners_.get()); |
| 475 | } else { |
| 476 | modified = new std::list<InstrumentationListener*>(); |
| 477 | } |
| 478 | modified->push_back(listener); |
| 479 | exception_caught_listeners_.reset(modified); |
Jeff Hao | 14dd5a8 | 2013-04-11 10:23:36 -0700 | [diff] [blame] | 480 | have_exception_caught_listeners_ = true; |
| 481 | } |
Sebastien Hertz | ee1997a | 2013-09-19 14:47:09 +0200 | [diff] [blame] | 482 | UpdateInterpreterHandlerTable(); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 483 | } |
| 484 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 485 | void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) { |
| 486 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 487 | |
| 488 | if ((events & kMethodEntered) != 0) { |
Daniel Mihalyi | 96add97 | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 489 | if (have_method_entry_listeners_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 490 | method_entry_listeners_.remove(listener); |
Daniel Mihalyi | 96add97 | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 491 | have_method_entry_listeners_ = !method_entry_listeners_.empty(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 492 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 493 | } |
| 494 | if ((events & kMethodExited) != 0) { |
Daniel Mihalyi | 96add97 | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 495 | if (have_method_exit_listeners_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 496 | method_exit_listeners_.remove(listener); |
Daniel Mihalyi | 96add97 | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 497 | have_method_exit_listeners_ = !method_exit_listeners_.empty(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 498 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 499 | } |
| 500 | if ((events & kMethodUnwind) != 0) { |
Daniel Mihalyi | 96add97 | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 501 | if (have_method_unwind_listeners_) { |
| 502 | method_unwind_listeners_.remove(listener); |
| 503 | have_method_unwind_listeners_ = !method_unwind_listeners_.empty(); |
| 504 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 505 | } |
| 506 | if ((events & kDexPcMoved) != 0) { |
Daniel Mihalyi | 96add97 | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 507 | if (have_dex_pc_listeners_) { |
| 508 | std::list<InstrumentationListener*>* modified = |
| 509 | new std::list<InstrumentationListener*>(*dex_pc_listeners_.get()); |
| 510 | modified->remove(listener); |
| 511 | have_dex_pc_listeners_ = !modified->empty(); |
| 512 | if (have_dex_pc_listeners_) { |
| 513 | dex_pc_listeners_.reset(modified); |
| 514 | } else { |
| 515 | dex_pc_listeners_.reset(); |
| 516 | delete modified; |
| 517 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 518 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 519 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 520 | if ((events & kFieldRead) != 0) { |
Daniel Mihalyi | e246f33 | 2014-08-21 15:57:25 +0200 | [diff] [blame] | 521 | if (have_field_read_listeners_) { |
Daniel Mihalyi | 96add97 | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 522 | std::list<InstrumentationListener*>* modified = |
| 523 | new std::list<InstrumentationListener*>(*field_read_listeners_.get()); |
| 524 | modified->remove(listener); |
| 525 | have_field_read_listeners_ = !modified->empty(); |
| 526 | if (have_field_read_listeners_) { |
| 527 | field_read_listeners_.reset(modified); |
| 528 | } else { |
| 529 | field_read_listeners_.reset(); |
| 530 | delete modified; |
| 531 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 532 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 533 | } |
| 534 | if ((events & kFieldWritten) != 0) { |
Daniel Mihalyi | 96add97 | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 535 | if (have_field_write_listeners_) { |
| 536 | std::list<InstrumentationListener*>* modified = |
| 537 | new std::list<InstrumentationListener*>(*field_write_listeners_.get()); |
| 538 | modified->remove(listener); |
| 539 | have_field_write_listeners_ = !modified->empty(); |
| 540 | if (have_field_write_listeners_) { |
| 541 | field_write_listeners_.reset(modified); |
| 542 | } else { |
| 543 | field_write_listeners_.reset(); |
| 544 | delete modified; |
| 545 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 546 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 547 | } |
Jeff Hao | 14dd5a8 | 2013-04-11 10:23:36 -0700 | [diff] [blame] | 548 | if ((events & kExceptionCaught) != 0) { |
Daniel Mihalyi | 96add97 | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 549 | if (have_exception_caught_listeners_) { |
| 550 | std::list<InstrumentationListener*>* modified = |
| 551 | new std::list<InstrumentationListener*>(*exception_caught_listeners_.get()); |
| 552 | modified->remove(listener); |
| 553 | have_exception_caught_listeners_ = !modified->empty(); |
| 554 | if (have_exception_caught_listeners_) { |
| 555 | exception_caught_listeners_.reset(modified); |
| 556 | } else { |
| 557 | exception_caught_listeners_.reset(); |
| 558 | delete modified; |
| 559 | } |
| 560 | } |
Jeff Hao | 14dd5a8 | 2013-04-11 10:23:36 -0700 | [diff] [blame] | 561 | } |
Sebastien Hertz | ee1997a | 2013-09-19 14:47:09 +0200 | [diff] [blame] | 562 | UpdateInterpreterHandlerTable(); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 563 | } |
| 564 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 565 | void Instrumentation::ConfigureStubs(bool require_entry_exit_stubs, bool require_interpreter) { |
| 566 | interpret_only_ = require_interpreter || forced_interpret_only_; |
| 567 | // Compute what level of instrumentation is required and compare to current. |
| 568 | int desired_level, current_level; |
| 569 | if (require_interpreter) { |
| 570 | desired_level = 2; |
| 571 | } else if (require_entry_exit_stubs) { |
| 572 | desired_level = 1; |
| 573 | } else { |
| 574 | desired_level = 0; |
| 575 | } |
| 576 | if (interpreter_stubs_installed_) { |
| 577 | current_level = 2; |
| 578 | } else if (entry_exit_stubs_installed_) { |
| 579 | current_level = 1; |
| 580 | } else { |
| 581 | current_level = 0; |
| 582 | } |
| 583 | if (desired_level == current_level) { |
| 584 | // We're already set. |
| 585 | return; |
| 586 | } |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 587 | Thread* const self = Thread::Current(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 588 | Runtime* runtime = Runtime::Current(); |
| 589 | Locks::thread_list_lock_->AssertNotHeld(self); |
| 590 | if (desired_level > 0) { |
| 591 | if (require_interpreter) { |
| 592 | interpreter_stubs_installed_ = true; |
| 593 | } else { |
| 594 | CHECK(require_entry_exit_stubs); |
| 595 | entry_exit_stubs_installed_ = true; |
| 596 | } |
| 597 | runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this); |
| 598 | instrumentation_stubs_installed_ = true; |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 599 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 600 | runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this); |
| 601 | } else { |
| 602 | interpreter_stubs_installed_ = false; |
| 603 | entry_exit_stubs_installed_ = false; |
| 604 | runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 605 | // Restore stack only if there is no method currently deoptimized. |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 606 | bool empty; |
| 607 | { |
| 608 | ReaderMutexLock mu(self, deoptimized_methods_lock_); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 609 | empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation. |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 610 | } |
| 611 | if (empty) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 612 | instrumentation_stubs_installed_ = false; |
| 613 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 614 | Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this); |
| 615 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 616 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 617 | } |
| 618 | |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 619 | static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg) { |
| 620 | thread->ResetQuickAllocEntryPointsForThread(); |
| 621 | } |
| 622 | |
Jeff Hao | 1d6ee09 | 2014-09-15 18:03:41 -0700 | [diff] [blame] | 623 | void Instrumentation::SetEntrypointsInstrumented(bool instrumented, bool suspended) { |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 624 | Runtime* runtime = Runtime::Current(); |
| 625 | ThreadList* tl = runtime->GetThreadList(); |
Jeff Hao | 1d6ee09 | 2014-09-15 18:03:41 -0700 | [diff] [blame] | 626 | if (suspended) { |
| 627 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
| 628 | } |
| 629 | if (runtime->IsStarted() && !suspended) { |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 630 | tl->SuspendAll(); |
| 631 | } |
| 632 | { |
| 633 | MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_); |
| 634 | SetQuickAllocEntryPointsInstrumented(instrumented); |
| 635 | ResetQuickAllocEntryPoints(); |
| 636 | } |
Jeff Hao | 1d6ee09 | 2014-09-15 18:03:41 -0700 | [diff] [blame] | 637 | if (runtime->IsStarted() && !suspended) { |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 638 | tl->ResumeAll(); |
| 639 | } |
| 640 | } |
| 641 | |
Jeff Hao | 1d6ee09 | 2014-09-15 18:03:41 -0700 | [diff] [blame] | 642 | void Instrumentation::InstrumentQuickAllocEntryPoints(bool suspended) { |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 643 | // TODO: the read of quick_alloc_entry_points_instrumentation_counter_ is racey and this code |
| 644 | // should be guarded by a lock. |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 645 | DCHECK_GE(quick_alloc_entry_points_instrumentation_counter_.LoadSequentiallyConsistent(), 0); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 646 | const bool enable_instrumentation = |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 647 | quick_alloc_entry_points_instrumentation_counter_.FetchAndAddSequentiallyConsistent(1) == 0; |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 648 | if (enable_instrumentation) { |
Jeff Hao | 1d6ee09 | 2014-09-15 18:03:41 -0700 | [diff] [blame] | 649 | SetEntrypointsInstrumented(true, suspended); |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | |
Jeff Hao | 1d6ee09 | 2014-09-15 18:03:41 -0700 | [diff] [blame] | 653 | void Instrumentation::UninstrumentQuickAllocEntryPoints(bool suspended) { |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 654 | // TODO: the read of quick_alloc_entry_points_instrumentation_counter_ is racey and this code |
| 655 | // should be guarded by a lock. |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 656 | DCHECK_GT(quick_alloc_entry_points_instrumentation_counter_.LoadSequentiallyConsistent(), 0); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 657 | const bool disable_instrumentation = |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 658 | quick_alloc_entry_points_instrumentation_counter_.FetchAndSubSequentiallyConsistent(1) == 1; |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 659 | if (disable_instrumentation) { |
Jeff Hao | 1d6ee09 | 2014-09-15 18:03:41 -0700 | [diff] [blame] | 660 | SetEntrypointsInstrumented(false, suspended); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | |
| 664 | void Instrumentation::ResetQuickAllocEntryPoints() { |
| 665 | Runtime* runtime = Runtime::Current(); |
| 666 | if (runtime->IsStarted()) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 667 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
| 668 | runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, NULL); |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 669 | } |
| 670 | } |
| 671 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 672 | void Instrumentation::UpdateMethodsCode(mirror::ArtMethod* method, const void* quick_code, |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 673 | const void* portable_code, bool have_portable_code) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 674 | const void* new_portable_code; |
| 675 | const void* new_quick_code; |
| 676 | bool new_have_portable_code; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 677 | if (LIKELY(!instrumentation_stubs_installed_)) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 678 | new_portable_code = portable_code; |
| 679 | new_quick_code = quick_code; |
| 680 | new_have_portable_code = have_portable_code; |
Jeff Hao | 65d15d9 | 2013-07-16 16:39:33 -0700 | [diff] [blame] | 681 | } else { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 682 | if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) { |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 683 | #if defined(ART_USE_PORTABLE_COMPILER) |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 684 | new_portable_code = GetPortableToInterpreterBridge(); |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 685 | #else |
| 686 | new_portable_code = portable_code; |
| 687 | #endif |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 688 | new_quick_code = GetQuickToInterpreterBridge(); |
| 689 | new_have_portable_code = false; |
Jeff Hao | 65d15d9 | 2013-07-16 16:39:33 -0700 | [diff] [blame] | 690 | } else { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 691 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 692 | if (quick_code == class_linker->GetQuickResolutionTrampoline() || |
| 693 | quick_code == class_linker->GetQuickToInterpreterBridgeTrampoline() || |
| 694 | quick_code == GetQuickToInterpreterBridge()) { |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 695 | #if defined(ART_USE_PORTABLE_COMPILER) |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 696 | DCHECK((portable_code == class_linker->GetPortableResolutionTrampoline()) || |
| 697 | (portable_code == GetPortableToInterpreterBridge())); |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 698 | #endif |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 699 | new_portable_code = portable_code; |
| 700 | new_quick_code = quick_code; |
| 701 | new_have_portable_code = have_portable_code; |
| 702 | } else if (entry_exit_stubs_installed_) { |
| 703 | new_quick_code = GetQuickInstrumentationEntryPoint(); |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 704 | #if defined(ART_USE_PORTABLE_COMPILER) |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 705 | new_portable_code = GetPortableToInterpreterBridge(); |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 706 | #else |
| 707 | new_portable_code = portable_code; |
| 708 | #endif |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 709 | new_have_portable_code = false; |
| 710 | } else { |
| 711 | new_portable_code = portable_code; |
| 712 | new_quick_code = quick_code; |
| 713 | new_have_portable_code = have_portable_code; |
| 714 | } |
Jeff Hao | 65d15d9 | 2013-07-16 16:39:33 -0700 | [diff] [blame] | 715 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 716 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 717 | UpdateEntrypoints(method, new_quick_code, new_portable_code, new_have_portable_code); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 718 | } |
| 719 | |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 720 | bool Instrumentation::AddDeoptimizedMethod(mirror::ArtMethod* method) { |
| 721 | // Note that the insert() below isn't read barrier-aware. So, this |
| 722 | // FindDeoptimizedMethod() call is necessary or else we would end up |
| 723 | // storing the same method twice in the map (the from-space and the |
| 724 | // to-space ones). |
| 725 | if (FindDeoptimizedMethod(method)) { |
| 726 | // Already in the map. Return. |
| 727 | return false; |
| 728 | } |
| 729 | // Not found. Add it. |
| 730 | int32_t hash_code = method->IdentityHashCode(); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 731 | deoptimized_methods_.insert(std::make_pair(hash_code, GcRoot<mirror::ArtMethod>(method))); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 732 | return true; |
| 733 | } |
| 734 | |
| 735 | bool Instrumentation::FindDeoptimizedMethod(mirror::ArtMethod* method) { |
| 736 | int32_t hash_code = method->IdentityHashCode(); |
| 737 | auto range = deoptimized_methods_.equal_range(hash_code); |
| 738 | for (auto it = range.first; it != range.second; ++it) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 739 | mirror::ArtMethod* m = it->second.Read(); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 740 | if (m == method) { |
| 741 | // Found. |
| 742 | return true; |
| 743 | } |
| 744 | } |
| 745 | // Not found. |
| 746 | return false; |
| 747 | } |
| 748 | |
| 749 | mirror::ArtMethod* Instrumentation::BeginDeoptimizedMethod() { |
| 750 | auto it = deoptimized_methods_.begin(); |
| 751 | if (it == deoptimized_methods_.end()) { |
| 752 | // Empty. |
| 753 | return nullptr; |
| 754 | } |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 755 | return it->second.Read(); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | bool Instrumentation::RemoveDeoptimizedMethod(mirror::ArtMethod* method) { |
| 759 | int32_t hash_code = method->IdentityHashCode(); |
| 760 | auto range = deoptimized_methods_.equal_range(hash_code); |
| 761 | for (auto it = range.first; it != range.second; ++it) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 762 | mirror::ArtMethod* m = it->second.Read(); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 763 | if (m == method) { |
| 764 | // Found. Erase and return. |
| 765 | deoptimized_methods_.erase(it); |
| 766 | return true; |
| 767 | } |
| 768 | } |
| 769 | // Not found. |
| 770 | return false; |
| 771 | } |
| 772 | |
| 773 | bool Instrumentation::IsDeoptimizedMethodsEmpty() const { |
| 774 | return deoptimized_methods_.empty(); |
| 775 | } |
| 776 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 777 | void Instrumentation::Deoptimize(mirror::ArtMethod* method) { |
| 778 | CHECK(!method->IsNative()); |
| 779 | CHECK(!method->IsProxyMethod()); |
| 780 | CHECK(!method->IsAbstract()); |
| 781 | |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 782 | Thread* self = Thread::Current(); |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 783 | { |
| 784 | WriterMutexLock mu(self, deoptimized_methods_lock_); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 785 | bool has_not_been_deoptimized = AddDeoptimizedMethod(method); |
Daniel Mihalyi | 96add97 | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 786 | CHECK(has_not_been_deoptimized) << "Method " << PrettyMethod(method) |
| 787 | << " is already deoptimized"; |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 788 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 789 | if (!interpreter_stubs_installed_) { |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 790 | UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint(), |
| 791 | #if defined(ART_USE_PORTABLE_COMPILER) |
| 792 | GetPortableToInterpreterBridge(), |
| 793 | #else |
| 794 | nullptr, |
| 795 | #endif |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 796 | false); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 797 | |
| 798 | // Install instrumentation exit stub and instrumentation frames. We may already have installed |
| 799 | // these previously so it will only cover the newly created frames. |
| 800 | instrumentation_stubs_installed_ = true; |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 801 | MutexLock mu(self, *Locks::thread_list_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 802 | Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this); |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | void Instrumentation::Undeoptimize(mirror::ArtMethod* method) { |
| 807 | CHECK(!method->IsNative()); |
| 808 | CHECK(!method->IsProxyMethod()); |
| 809 | CHECK(!method->IsAbstract()); |
| 810 | |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 811 | Thread* self = Thread::Current(); |
| 812 | bool empty; |
| 813 | { |
| 814 | WriterMutexLock mu(self, deoptimized_methods_lock_); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 815 | bool found_and_erased = RemoveDeoptimizedMethod(method); |
| 816 | CHECK(found_and_erased) << "Method " << PrettyMethod(method) |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 817 | << " is not deoptimized"; |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 818 | empty = IsDeoptimizedMethodsEmpty(); |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 819 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 820 | |
| 821 | // Restore code and possibly stack only if we did not deoptimize everything. |
| 822 | if (!interpreter_stubs_installed_) { |
| 823 | // Restore its code or resolution trampoline. |
| 824 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 825 | if (method->IsStatic() && !method->IsConstructor() && |
| 826 | !method->GetDeclaringClass()->IsInitialized()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 827 | // TODO: we're updating to entrypoints in the image here, we can avoid the trampoline. |
| 828 | UpdateEntrypoints(method, class_linker->GetQuickResolutionTrampoline(), |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 829 | #if defined(ART_USE_PORTABLE_COMPILER) |
| 830 | class_linker->GetPortableResolutionTrampoline(), |
| 831 | #else |
| 832 | nullptr, |
| 833 | #endif |
| 834 | false); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 835 | } else { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 836 | bool have_portable_code = false; |
| 837 | const void* quick_code = class_linker->GetQuickOatCodeFor(method); |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 838 | #if defined(ART_USE_PORTABLE_COMPILER) |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 839 | const void* portable_code = class_linker->GetPortableOatCodeFor(method, &have_portable_code); |
Ian Rogers | 63bc11e | 2014-09-18 08:56:45 -0700 | [diff] [blame^] | 840 | #else |
| 841 | const void* portable_code = nullptr; |
| 842 | #endif |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 843 | UpdateEntrypoints(method, quick_code, portable_code, have_portable_code); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | // If there is no deoptimized method left, we can restore the stack of each thread. |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 847 | if (empty) { |
| 848 | MutexLock mu(self, *Locks::thread_list_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 849 | Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this); |
| 850 | instrumentation_stubs_installed_ = false; |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 855 | bool Instrumentation::IsDeoptimized(mirror::ArtMethod* method) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 856 | DCHECK(method != nullptr); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 857 | ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_); |
| 858 | return FindDeoptimizedMethod(method); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | void Instrumentation::EnableDeoptimization() { |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 862 | ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 863 | CHECK(IsDeoptimizedMethodsEmpty()); |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 864 | CHECK_EQ(deoptimization_enabled_, false); |
| 865 | deoptimization_enabled_ = true; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | void Instrumentation::DisableDeoptimization() { |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 869 | CHECK_EQ(deoptimization_enabled_, true); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 870 | // If we deoptimized everything, undo it. |
| 871 | if (interpreter_stubs_installed_) { |
| 872 | UndeoptimizeEverything(); |
| 873 | } |
| 874 | // Undeoptimized selected methods. |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 875 | while (true) { |
| 876 | mirror::ArtMethod* method; |
| 877 | { |
| 878 | ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 879 | if (IsDeoptimizedMethodsEmpty()) { |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 880 | break; |
| 881 | } |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 882 | method = BeginDeoptimizedMethod(); |
| 883 | CHECK(method != nullptr); |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 884 | } |
| 885 | Undeoptimize(method); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 886 | } |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 887 | deoptimization_enabled_ = false; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 888 | } |
| 889 | |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 890 | // Indicates if instrumentation should notify method enter/exit events to the listeners. |
| 891 | bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const { |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 892 | return !deoptimization_enabled_ && !interpreter_stubs_installed_; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | void Instrumentation::DeoptimizeEverything() { |
| 896 | CHECK(!interpreter_stubs_installed_); |
| 897 | ConfigureStubs(false, true); |
| 898 | } |
| 899 | |
| 900 | void Instrumentation::UndeoptimizeEverything() { |
| 901 | CHECK(interpreter_stubs_installed_); |
| 902 | ConfigureStubs(false, false); |
| 903 | } |
| 904 | |
| 905 | void Instrumentation::EnableMethodTracing() { |
| 906 | bool require_interpreter = kDeoptimizeForAccurateMethodEntryExitListeners; |
| 907 | ConfigureStubs(!require_interpreter, require_interpreter); |
| 908 | } |
| 909 | |
| 910 | void Instrumentation::DisableMethodTracing() { |
| 911 | ConfigureStubs(false, false); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 912 | } |
| 913 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 914 | const void* Instrumentation::GetQuickCodeFor(mirror::ArtMethod* method) const { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 915 | Runtime* runtime = Runtime::Current(); |
| 916 | if (LIKELY(!instrumentation_stubs_installed_)) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 917 | const void* code = method->GetEntryPointFromQuickCompiledCode(); |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 918 | DCHECK(code != nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 919 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 920 | if (LIKELY(code != class_linker->GetQuickResolutionTrampoline()) && |
| 921 | LIKELY(code != class_linker->GetQuickToInterpreterBridgeTrampoline()) && |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 922 | LIKELY(code != GetQuickToInterpreterBridge())) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 923 | return code; |
| 924 | } |
| 925 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 926 | return runtime->GetClassLinker()->GetQuickOatCodeFor(method); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 927 | } |
| 928 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 929 | void Instrumentation::MethodEnterEventImpl(Thread* thread, mirror::Object* this_object, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 930 | mirror::ArtMethod* method, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 931 | uint32_t dex_pc) const { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 932 | auto it = method_entry_listeners_.begin(); |
Jeff Hao | 65d15d9 | 2013-07-16 16:39:33 -0700 | [diff] [blame] | 933 | bool is_end = (it == method_entry_listeners_.end()); |
| 934 | // Implemented this way to prevent problems caused by modification of the list while iterating. |
| 935 | while (!is_end) { |
| 936 | InstrumentationListener* cur = *it; |
| 937 | ++it; |
| 938 | is_end = (it == method_entry_listeners_.end()); |
| 939 | cur->MethodEntered(thread, this_object, method, dex_pc); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 940 | } |
| 941 | } |
| 942 | |
| 943 | void Instrumentation::MethodExitEventImpl(Thread* thread, mirror::Object* this_object, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 944 | mirror::ArtMethod* method, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 945 | uint32_t dex_pc, const JValue& return_value) const { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 946 | auto it = method_exit_listeners_.begin(); |
Jeff Hao | 65d15d9 | 2013-07-16 16:39:33 -0700 | [diff] [blame] | 947 | bool is_end = (it == method_exit_listeners_.end()); |
| 948 | // Implemented this way to prevent problems caused by modification of the list while iterating. |
| 949 | while (!is_end) { |
| 950 | InstrumentationListener* cur = *it; |
| 951 | ++it; |
| 952 | is_end = (it == method_exit_listeners_.end()); |
| 953 | cur->MethodExited(thread, this_object, method, dex_pc, return_value); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 954 | } |
| 955 | } |
| 956 | |
| 957 | void Instrumentation::MethodUnwindEvent(Thread* thread, mirror::Object* this_object, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 958 | mirror::ArtMethod* method, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 959 | uint32_t dex_pc) const { |
| 960 | if (have_method_unwind_listeners_) { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 961 | for (InstrumentationListener* listener : method_unwind_listeners_) { |
Sebastien Hertz | 51db44a | 2013-11-19 10:00:29 +0100 | [diff] [blame] | 962 | listener->MethodUnwind(thread, this_object, method, dex_pc); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 963 | } |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | void Instrumentation::DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 968 | mirror::ArtMethod* method, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 969 | uint32_t dex_pc) const { |
Daniel Mihalyi | 96add97 | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 970 | if (HasDexPcListeners()) { |
| 971 | std::shared_ptr<std::list<InstrumentationListener*>> original(dex_pc_listeners_); |
| 972 | for (InstrumentationListener* listener : *original.get()) { |
| 973 | listener->DexPcMoved(thread, this_object, method, dex_pc); |
| 974 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 975 | } |
| 976 | } |
| 977 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 978 | void Instrumentation::FieldReadEventImpl(Thread* thread, mirror::Object* this_object, |
| 979 | mirror::ArtMethod* method, uint32_t dex_pc, |
| 980 | mirror::ArtField* field) const { |
Daniel Mihalyi | 96add97 | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 981 | if (HasFieldReadListeners()) { |
| 982 | std::shared_ptr<std::list<InstrumentationListener*>> original(field_read_listeners_); |
| 983 | for (InstrumentationListener* listener : *original.get()) { |
| 984 | listener->FieldRead(thread, this_object, method, dex_pc, field); |
| 985 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 986 | } |
| 987 | } |
| 988 | |
| 989 | void Instrumentation::FieldWriteEventImpl(Thread* thread, mirror::Object* this_object, |
| 990 | mirror::ArtMethod* method, uint32_t dex_pc, |
| 991 | mirror::ArtField* field, const JValue& field_value) const { |
Daniel Mihalyi | 96add97 | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 992 | if (HasFieldWriteListeners()) { |
| 993 | std::shared_ptr<std::list<InstrumentationListener*>> original(field_write_listeners_); |
| 994 | for (InstrumentationListener* listener : *original.get()) { |
| 995 | listener->FieldWritten(thread, this_object, method, dex_pc, field, field_value); |
| 996 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 997 | } |
| 998 | } |
| 999 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1000 | void Instrumentation::ExceptionCaughtEvent(Thread* thread, const ThrowLocation& throw_location, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1001 | mirror::ArtMethod* catch_method, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1002 | uint32_t catch_dex_pc, |
Sebastien Hertz | 947ff08 | 2013-09-17 14:10:13 +0200 | [diff] [blame] | 1003 | mirror::Throwable* exception_object) const { |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 1004 | if (HasExceptionCaughtListeners()) { |
| 1005 | DCHECK_EQ(thread->GetException(nullptr), exception_object); |
| 1006 | bool is_exception_reported = thread->IsExceptionReportedToInstrumentation(); |
Jeff Hao | c0bd4da | 2013-04-11 15:52:28 -0700 | [diff] [blame] | 1007 | thread->ClearException(); |
Daniel Mihalyi | 96add97 | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 1008 | std::shared_ptr<std::list<InstrumentationListener*>> original(exception_caught_listeners_); |
| 1009 | for (InstrumentationListener* listener : *original.get()) { |
| 1010 | listener->ExceptionCaught(thread, throw_location, catch_method, catch_dex_pc, |
| 1011 | exception_object); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1012 | } |
Jeff Hao | c0bd4da | 2013-04-11 15:52:28 -0700 | [diff] [blame] | 1013 | thread->SetException(throw_location, exception_object); |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 1014 | thread->SetExceptionReportedToInstrumentation(is_exception_reported); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame, |
| 1019 | int delta) |
| 1020 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1021 | size_t frame_id = StackVisitor::ComputeNumFrames(self) + delta; |
| 1022 | if (frame_id != instrumentation_frame.frame_id_) { |
| 1023 | LOG(ERROR) << "Expected frame_id=" << frame_id << " but found " |
| 1024 | << instrumentation_frame.frame_id_; |
| 1025 | StackVisitor::DescribeStack(self); |
| 1026 | CHECK_EQ(frame_id, instrumentation_frame.frame_id_); |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1031 | mirror::ArtMethod* method, |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 1032 | uintptr_t lr, bool interpreter_entry) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1033 | // We have a callee-save frame meaning this value is guaranteed to never be 0. |
| 1034 | size_t frame_id = StackVisitor::ComputeNumFrames(self); |
| 1035 | std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack(); |
| 1036 | if (kVerboseInstrumentation) { |
Brian Carlstrom | 2d88862 | 2013-07-18 17:02:00 -0700 | [diff] [blame] | 1037 | LOG(INFO) << "Entering " << PrettyMethod(method) << " from PC " << reinterpret_cast<void*>(lr); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1038 | } |
| 1039 | instrumentation::InstrumentationStackFrame instrumentation_frame(this_object, method, lr, |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 1040 | frame_id, interpreter_entry); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1041 | stack->push_front(instrumentation_frame); |
| 1042 | |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 1043 | if (!interpreter_entry) { |
| 1044 | MethodEnterEvent(self, this_object, method, 0); |
| 1045 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1046 | } |
| 1047 | |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame] | 1048 | TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc, |
| 1049 | uint64_t gpr_result, |
| 1050 | uint64_t fpr_result) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1051 | // Do the pop. |
| 1052 | std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack(); |
| 1053 | CHECK_GT(stack->size(), 0U); |
| 1054 | InstrumentationStackFrame instrumentation_frame = stack->front(); |
| 1055 | stack->pop_front(); |
| 1056 | |
| 1057 | // Set return PC and check the sanity of the stack. |
| 1058 | *return_pc = instrumentation_frame.return_pc_; |
| 1059 | CheckStackDepth(self, instrumentation_frame, 0); |
| 1060 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1061 | mirror::ArtMethod* method = instrumentation_frame.method_; |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1062 | uint32_t length; |
| 1063 | char return_shorty = method->GetShorty(&length)[0]; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1064 | JValue return_value; |
| 1065 | if (return_shorty == 'V') { |
| 1066 | return_value.SetJ(0); |
| 1067 | } else if (return_shorty == 'F' || return_shorty == 'D') { |
| 1068 | return_value.SetJ(fpr_result); |
| 1069 | } else { |
| 1070 | return_value.SetJ(gpr_result); |
| 1071 | } |
| 1072 | // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to |
| 1073 | // return_pc. |
| 1074 | uint32_t dex_pc = DexFile::kDexNoIndex; |
| 1075 | mirror::Object* this_object = instrumentation_frame.this_object_; |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 1076 | if (!instrumentation_frame.interpreter_entry_) { |
| 1077 | MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value); |
| 1078 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 1079 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 1080 | // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get |
| 1081 | // back to an upcall. |
| 1082 | NthCallerVisitor visitor(self, 1, true); |
| 1083 | visitor.WalkStack(true); |
| 1084 | bool deoptimize = (visitor.caller != NULL) && |
| 1085 | (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller)); |
| 1086 | if (deoptimize && kVerboseInstrumentation) { |
| 1087 | LOG(INFO) << "Deoptimizing into " << PrettyMethod(visitor.caller); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1088 | } |
| 1089 | if (deoptimize) { |
| 1090 | if (kVerboseInstrumentation) { |
| 1091 | LOG(INFO) << "Deoptimizing from " << PrettyMethod(method) |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 1092 | << " result is " << std::hex << return_value.GetJ(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1093 | } |
| 1094 | self->SetDeoptimizationReturnValue(return_value); |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame] | 1095 | return GetTwoWordSuccessValue(*return_pc, |
| 1096 | reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint())); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1097 | } else { |
| 1098 | if (kVerboseInstrumentation) { |
Brian Carlstrom | 2d88862 | 2013-07-18 17:02:00 -0700 | [diff] [blame] | 1099 | LOG(INFO) << "Returning from " << PrettyMethod(method) |
| 1100 | << " to PC " << reinterpret_cast<void*>(*return_pc); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1101 | } |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame] | 1102 | return GetTwoWordSuccessValue(0, *return_pc); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1103 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 1104 | } |
| 1105 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1106 | void Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const { |
| 1107 | // Do the pop. |
| 1108 | std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack(); |
| 1109 | CHECK_GT(stack->size(), 0U); |
| 1110 | InstrumentationStackFrame instrumentation_frame = stack->front(); |
| 1111 | // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2); |
| 1112 | stack->pop_front(); |
| 1113 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1114 | mirror::ArtMethod* method = instrumentation_frame.method_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1115 | if (is_deoptimization) { |
| 1116 | if (kVerboseInstrumentation) { |
| 1117 | LOG(INFO) << "Popping for deoptimization " << PrettyMethod(method); |
| 1118 | } |
| 1119 | } else { |
| 1120 | if (kVerboseInstrumentation) { |
| 1121 | LOG(INFO) << "Popping for unwind " << PrettyMethod(method); |
| 1122 | } |
| 1123 | |
| 1124 | // Notify listeners of method unwind. |
| 1125 | // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to |
| 1126 | // return_pc. |
| 1127 | uint32_t dex_pc = DexFile::kDexNoIndex; |
| 1128 | MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc); |
| 1129 | } |
| 1130 | } |
| 1131 | |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 1132 | void Instrumentation::VisitRoots(RootCallback* callback, void* arg) { |
| 1133 | WriterMutexLock mu(Thread::Current(), deoptimized_methods_lock_); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 1134 | if (IsDeoptimizedMethodsEmpty()) { |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 1135 | return; |
| 1136 | } |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 1137 | for (auto pair : deoptimized_methods_) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1138 | pair.second.VisitRoot(callback, arg, 0, kRootVMInternal); |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 1139 | } |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1142 | std::string InstrumentationStackFrame::Dump() const { |
| 1143 | std::ostringstream os; |
| 1144 | os << "Frame " << frame_id_ << " " << PrettyMethod(method_) << ":" |
| 1145 | << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_); |
| 1146 | return os.str(); |
| 1147 | } |
| 1148 | |
| 1149 | } // namespace instrumentation |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 1150 | } // namespace art |