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 | |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 19 | #include <sstream> |
| 20 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 21 | #include "arch/context.h" |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 22 | #include "art_field-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 23 | #include "art_method-inl.h" |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 24 | #include "atomic.h" |
Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 25 | #include "base/callee_save_type.h" |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 26 | #include "class_linker.h" |
| 27 | #include "debugger.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 28 | #include "dex_file-inl.h" |
Mathieu Chartier | d889178 | 2014-03-02 13:28:37 -0800 | [diff] [blame] | 29 | #include "entrypoints/quick/quick_alloc_entrypoints.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 30 | #include "entrypoints/quick/quick_entrypoints.h" |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 31 | #include "entrypoints/runtime_asm_entrypoints.h" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 32 | #include "gc_root-inl.h" |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 33 | #include "interpreter/interpreter.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 34 | #include "jit/jit.h" |
| 35 | #include "jit/jit_code_cache.h" |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 36 | #include "jvalue-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 37 | #include "mirror/class-inl.h" |
| 38 | #include "mirror/dex_cache.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 39 | #include "mirror/object-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 40 | #include "mirror/object_array-inl.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 41 | #include "nth_caller_visitor.h" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 42 | #include "oat_quick_method_header.h" |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 43 | #include "thread.h" |
| 44 | #include "thread_list.h" |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 45 | |
| 46 | namespace art { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 47 | namespace instrumentation { |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 48 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 49 | constexpr bool kVerboseInstrumentation = false; |
Sebastien Hertz | 5bfd5c9 | 2013-11-15 11:36:07 +0100 | [diff] [blame] | 50 | |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 51 | void InstrumentationListener::MethodExited(Thread* thread, |
| 52 | Handle<mirror::Object> this_object, |
| 53 | ArtMethod* method, |
| 54 | uint32_t dex_pc, |
| 55 | Handle<mirror::Object> return_value) { |
| 56 | DCHECK_EQ(method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetReturnTypePrimitive(), |
| 57 | Primitive::kPrimNot); |
| 58 | JValue v; |
| 59 | v.SetL(return_value.Get()); |
| 60 | MethodExited(thread, this_object, method, dex_pc, v); |
| 61 | } |
| 62 | |
| 63 | void InstrumentationListener::FieldWritten(Thread* thread, |
| 64 | Handle<mirror::Object> this_object, |
| 65 | ArtMethod* method, |
| 66 | uint32_t dex_pc, |
| 67 | ArtField* field, |
| 68 | Handle<mirror::Object> field_value) { |
| 69 | DCHECK(!field->IsPrimitiveType()); |
| 70 | JValue v; |
| 71 | v.SetL(field_value.Get()); |
| 72 | FieldWritten(thread, this_object, method, dex_pc, field, v); |
| 73 | } |
| 74 | |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 75 | // Instrumentation works on non-inlined frames by updating returned PCs |
| 76 | // of compiled frames. |
| 77 | static constexpr StackVisitor::StackWalkKind kInstrumentationStackWalk = |
| 78 | StackVisitor::StackWalkKind::kSkipInlinedFrames; |
| 79 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 80 | class InstallStubsClassVisitor : public ClassVisitor { |
| 81 | public: |
| 82 | explicit InstallStubsClassVisitor(Instrumentation* instrumentation) |
| 83 | : instrumentation_(instrumentation) {} |
| 84 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 85 | bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES(Locks::mutator_lock_) { |
| 86 | instrumentation_->InstallStubsForClass(klass.Ptr()); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 87 | return true; // we visit all classes. |
| 88 | } |
| 89 | |
| 90 | private: |
| 91 | Instrumentation* const instrumentation_; |
| 92 | }; |
| 93 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 94 | |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 95 | Instrumentation::Instrumentation() |
Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame] | 96 | : instrumentation_stubs_installed_(false), |
| 97 | entry_exit_stubs_installed_(false), |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 98 | interpreter_stubs_installed_(false), |
Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame] | 99 | interpret_only_(false), |
| 100 | forced_interpret_only_(false), |
| 101 | have_method_entry_listeners_(false), |
| 102 | have_method_exit_listeners_(false), |
| 103 | have_method_unwind_listeners_(false), |
| 104 | have_dex_pc_listeners_(false), |
| 105 | have_field_read_listeners_(false), |
| 106 | have_field_write_listeners_(false), |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 107 | have_exception_thrown_listeners_(false), |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 108 | have_watched_frame_pop_listeners_(false), |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 109 | have_branch_listeners_(false), |
Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame] | 110 | have_invoke_virtual_or_interface_listeners_(false), |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame^] | 111 | have_exception_handled_listeners_(false), |
Mathieu Chartier | b8aa1e4 | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 112 | deoptimized_methods_lock_("deoptimized methods lock", kDeoptimizedMethodsLock), |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 113 | deoptimization_enabled_(false), |
| 114 | interpreter_handler_table_(kMainHandlerTable), |
Mathieu Chartier | 50e9331 | 2016-03-16 11:25:29 -0700 | [diff] [blame] | 115 | quick_alloc_entry_points_instrumentation_counter_(0), |
| 116 | alloc_entrypoints_instrumented_(false) { |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Sebastien Hertz | a10aa37 | 2015-01-21 17:30:58 +0100 | [diff] [blame] | 119 | void Instrumentation::InstallStubsForClass(mirror::Class* klass) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 120 | if (!klass->IsResolved()) { |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 121 | // We need the class to be resolved to install/uninstall stubs. Otherwise its methods |
| 122 | // could not be initialized or linked with regards to class inheritance. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 123 | } else if (klass->IsErroneousResolved()) { |
| 124 | // We can't execute code in a erroneous class: do nothing. |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 125 | } else { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 126 | for (ArtMethod& method : klass->GetMethods(kRuntimePointerSize)) { |
Alex Light | 51a64d5 | 2015-12-17 13:55:59 -0800 | [diff] [blame] | 127 | InstallStubsForMethod(&method); |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 128 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 129 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 132 | static void UpdateEntrypoints(ArtMethod* method, const void* quick_code) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 133 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 134 | method->SetEntryPointFromQuickCompiledCode(quick_code); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 135 | } |
| 136 | |
Nicolas Geoffray | a0619e2 | 2016-12-20 13:57:43 +0000 | [diff] [blame] | 137 | bool Instrumentation::NeedDebugVersionFor(ArtMethod* method) const REQUIRES_SHARED(Locks::mutator_lock_) { |
Mingyao Yang | 6ea1a0e | 2016-01-29 12:12:49 -0800 | [diff] [blame] | 138 | return Dbg::IsDebuggerActive() && |
Nicolas Geoffray | a0619e2 | 2016-12-20 13:57:43 +0000 | [diff] [blame] | 139 | Runtime::Current()->IsJavaDebuggable() && |
Mingyao Yang | 6ea1a0e | 2016-01-29 12:12:49 -0800 | [diff] [blame] | 140 | !method->IsNative() && |
| 141 | !method->IsProxyMethod(); |
| 142 | } |
| 143 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 144 | void Instrumentation::InstallStubsForMethod(ArtMethod* method) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 145 | if (!method->IsInvokable() || method->IsProxyMethod()) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 146 | // Do not change stubs for these methods. |
| 147 | return; |
| 148 | } |
Jeff Hao | 5680277 | 2014-08-19 10:17:36 -0700 | [diff] [blame] | 149 | // Don't stub Proxy.<init>. Note that the Proxy class itself is not a proxy class. |
| 150 | if (method->IsConstructor() && |
| 151 | method->GetDeclaringClass()->DescriptorEquals("Ljava/lang/reflect/Proxy;")) { |
Jeff Hao | db8a664 | 2014-08-14 17:18:52 -0700 | [diff] [blame] | 152 | return; |
| 153 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 154 | const void* new_quick_code; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 155 | bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 156 | Runtime* const runtime = Runtime::Current(); |
| 157 | ClassLinker* const class_linker = runtime->GetClassLinker(); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 158 | bool is_class_initialized = method->GetDeclaringClass()->IsInitialized(); |
| 159 | if (uninstall) { |
| 160 | if ((forced_interpret_only_ || IsDeoptimized(method)) && !method->IsNative()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 161 | new_quick_code = GetQuickToInterpreterBridge(); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 162 | } else if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) { |
Nicolas Geoffray | a0619e2 | 2016-12-20 13:57:43 +0000 | [diff] [blame] | 163 | if (NeedDebugVersionFor(method)) { |
Mingyao Yang | 6ea1a0e | 2016-01-29 12:12:49 -0800 | [diff] [blame] | 164 | new_quick_code = GetQuickToInterpreterBridge(); |
Nicolas Geoffray | a0619e2 | 2016-12-20 13:57:43 +0000 | [diff] [blame] | 165 | } else { |
| 166 | new_quick_code = class_linker->GetQuickOatCodeFor(method); |
Mingyao Yang | 6ea1a0e | 2016-01-29 12:12:49 -0800 | [diff] [blame] | 167 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 168 | } else { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 169 | new_quick_code = GetQuickResolutionStub(); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 170 | } |
| 171 | } else { // !uninstall |
Sebastien Hertz | bae182c | 2013-12-17 10:42:03 +0100 | [diff] [blame] | 172 | if ((interpreter_stubs_installed_ || forced_interpret_only_ || IsDeoptimized(method)) && |
| 173 | !method->IsNative()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 174 | new_quick_code = GetQuickToInterpreterBridge(); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 175 | } else { |
| 176 | // Do not overwrite resolution trampoline. When the trampoline initializes the method's |
| 177 | // class, all its static methods code will be set to the instrumentation entry point. |
| 178 | // For more details, see ClassLinker::FixupStaticTrampolines. |
| 179 | if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) { |
Nicolas Geoffray | a0619e2 | 2016-12-20 13:57:43 +0000 | [diff] [blame] | 180 | if (NeedDebugVersionFor(method)) { |
Mingyao Yang | 6ea1a0e | 2016-01-29 12:12:49 -0800 | [diff] [blame] | 181 | // Oat code should not be used. Don't install instrumentation stub and |
| 182 | // use interpreter for instrumentation. |
| 183 | new_quick_code = GetQuickToInterpreterBridge(); |
| 184 | } else if (entry_exit_stubs_installed_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 185 | new_quick_code = GetQuickInstrumentationEntryPoint(); |
Nicolas Geoffray | a0619e2 | 2016-12-20 13:57:43 +0000 | [diff] [blame] | 186 | } else { |
| 187 | new_quick_code = class_linker->GetQuickOatCodeFor(method); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 188 | } |
| 189 | } else { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 190 | new_quick_code = GetQuickResolutionStub(); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 194 | UpdateEntrypoints(method, new_quick_code); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 195 | } |
| 196 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 197 | // Places the instrumentation exit pc as the return PC for every quick frame. This also allows |
| 198 | // deoptimization of quick frames to interpreter frames. |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 199 | // Since we may already have done this previously, we need to push new instrumentation frame before |
| 200 | // existing instrumentation frames. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 201 | static void InstrumentationInstallStack(Thread* thread, void* arg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 202 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 203 | struct InstallStackVisitor FINAL : public StackVisitor { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 204 | InstallStackVisitor(Thread* thread_in, Context* context, uintptr_t instrumentation_exit_pc) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 205 | : StackVisitor(thread_in, context, kInstrumentationStackWalk), |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 206 | instrumentation_stack_(thread_in->GetInstrumentationStack()), |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 207 | instrumentation_exit_pc_(instrumentation_exit_pc), |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 208 | reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0), |
| 209 | last_return_pc_(0) { |
| 210 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 211 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 212 | bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 213 | ArtMethod* m = GetMethod(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 214 | if (m == nullptr) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 215 | if (kVerboseInstrumentation) { |
| 216 | LOG(INFO) << " Skipping upcall. Frame " << GetFrameId(); |
| 217 | } |
| 218 | last_return_pc_ = 0; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 219 | return true; // Ignore upcalls. |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 220 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 221 | if (GetCurrentQuickFrame() == nullptr) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 222 | bool interpreter_frame = true; |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 223 | InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(), |
| 224 | interpreter_frame); |
Jeff Hao | a15a81b | 2014-05-27 18:25:47 -0700 | [diff] [blame] | 225 | if (kVerboseInstrumentation) { |
| 226 | LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump(); |
| 227 | } |
| 228 | shadow_stack_.push_back(instrumentation_frame); |
| 229 | return true; // Continue. |
| 230 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 231 | uintptr_t return_pc = GetReturnPc(); |
Nicolas Geoffray | 07c7028 | 2017-08-30 08:09:42 +0000 | [diff] [blame] | 232 | if (m->IsRuntimeMethod()) { |
| 233 | if (return_pc == instrumentation_exit_pc_) { |
| 234 | if (kVerboseInstrumentation) { |
| 235 | LOG(INFO) << " Handling quick to interpreter transition. Frame " << GetFrameId(); |
| 236 | } |
| 237 | CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size()); |
| 238 | const InstrumentationStackFrame& frame = |
| 239 | instrumentation_stack_->at(instrumentation_stack_depth_); |
| 240 | CHECK(frame.interpreter_entry_); |
| 241 | // This is an interpreter frame so method enter event must have been reported. However we |
| 242 | // need to push a DEX pc into the dex_pcs_ list to match size of instrumentation stack. |
| 243 | // Since we won't report method entry here, we can safely push any DEX pc. |
| 244 | dex_pcs_.push_back(0); |
| 245 | last_return_pc_ = frame.return_pc_; |
| 246 | ++instrumentation_stack_depth_; |
| 247 | return true; |
| 248 | } else { |
| 249 | if (kVerboseInstrumentation) { |
| 250 | LOG(INFO) << " Skipping runtime method. Frame " << GetFrameId(); |
| 251 | } |
| 252 | last_return_pc_ = GetReturnPc(); |
| 253 | return true; // Ignore unresolved methods since they will be instrumented after resolution. |
| 254 | } |
| 255 | } |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 256 | if (kVerboseInstrumentation) { |
| 257 | LOG(INFO) << " Installing exit stub in " << DescribeLocation(); |
| 258 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 259 | if (return_pc == instrumentation_exit_pc_) { |
| 260 | // We've reached a frame which has already been installed with instrumentation exit stub. |
| 261 | // We should have already installed instrumentation on previous frames. |
| 262 | reached_existing_instrumentation_frames_ = true; |
| 263 | |
Nicolas Geoffray | 07c7028 | 2017-08-30 08:09:42 +0000 | [diff] [blame] | 264 | CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size()); |
Daniel Mihalyi | ca1d06c | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 265 | const InstrumentationStackFrame& frame = |
| 266 | instrumentation_stack_->at(instrumentation_stack_depth_); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 267 | CHECK_EQ(m, frame.method_) << "Expected " << ArtMethod::PrettyMethod(m) |
| 268 | << ", Found " << ArtMethod::PrettyMethod(frame.method_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 269 | return_pc = frame.return_pc_; |
| 270 | if (kVerboseInstrumentation) { |
| 271 | LOG(INFO) << "Ignoring already instrumented " << frame.Dump(); |
| 272 | } |
| 273 | } else { |
| 274 | CHECK_NE(return_pc, 0U); |
| 275 | CHECK(!reached_existing_instrumentation_frames_); |
Nicolas Geoffray | 07c7028 | 2017-08-30 08:09:42 +0000 | [diff] [blame] | 276 | InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, return_pc, GetFrameId(), |
| 277 | false); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 278 | if (kVerboseInstrumentation) { |
| 279 | LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump(); |
| 280 | } |
| 281 | |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 282 | // Insert frame at the right position so we do not corrupt the instrumentation stack. |
| 283 | // Instrumentation stack frames are in descending frame id order. |
| 284 | auto it = instrumentation_stack_->begin(); |
| 285 | for (auto end = instrumentation_stack_->end(); it != end; ++it) { |
| 286 | const InstrumentationStackFrame& current = *it; |
| 287 | if (instrumentation_frame.frame_id_ >= current.frame_id_) { |
| 288 | break; |
| 289 | } |
| 290 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 291 | instrumentation_stack_->insert(it, instrumentation_frame); |
| 292 | SetReturnPc(instrumentation_exit_pc_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 293 | } |
Nicolas Geoffray | 07c7028 | 2017-08-30 08:09:42 +0000 | [diff] [blame] | 294 | dex_pcs_.push_back((GetCurrentOatQuickMethodHeader() == nullptr) |
| 295 | ? DexFile::kDexNoIndex |
| 296 | : GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_)); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 297 | last_return_pc_ = return_pc; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 298 | ++instrumentation_stack_depth_; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 299 | return true; // Continue. |
| 300 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 301 | std::deque<InstrumentationStackFrame>* const instrumentation_stack_; |
Jeff Hao | a15a81b | 2014-05-27 18:25:47 -0700 | [diff] [blame] | 302 | std::vector<InstrumentationStackFrame> shadow_stack_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 303 | std::vector<uint32_t> dex_pcs_; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 304 | const uintptr_t instrumentation_exit_pc_; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 305 | bool reached_existing_instrumentation_frames_; |
| 306 | size_t instrumentation_stack_depth_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 307 | uintptr_t last_return_pc_; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 308 | }; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 309 | if (kVerboseInstrumentation) { |
| 310 | std::string thread_name; |
| 311 | thread->GetThreadName(thread_name); |
| 312 | LOG(INFO) << "Installing exit stubs in " << thread_name; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 313 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 314 | |
| 315 | Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg); |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 316 | std::unique_ptr<Context> context(Context::Create()); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 317 | uintptr_t instrumentation_exit_pc = reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()); |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 318 | InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 319 | visitor.WalkStack(true); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 320 | CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 321 | |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 322 | if (instrumentation->ShouldNotifyMethodEnterExitEvents()) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 323 | // Create method enter events for all methods currently on the thread's stack. We only do this |
| 324 | // if no debugger is attached to prevent from posting events twice. |
Jeff Hao | a15a81b | 2014-05-27 18:25:47 -0700 | [diff] [blame] | 325 | auto ssi = visitor.shadow_stack_.rbegin(); |
| 326 | for (auto isi = thread->GetInstrumentationStack()->rbegin(), |
| 327 | end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) { |
| 328 | while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) { |
| 329 | instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0); |
| 330 | ++ssi; |
| 331 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 332 | uint32_t dex_pc = visitor.dex_pcs_.back(); |
| 333 | visitor.dex_pcs_.pop_back(); |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 334 | if (!isi->interpreter_entry_) { |
| 335 | instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc); |
| 336 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 337 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 338 | } |
| 339 | thread->VerifyStack(); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 342 | void Instrumentation::InstrumentThreadStack(Thread* thread) { |
| 343 | instrumentation_stubs_installed_ = true; |
| 344 | InstrumentationInstallStack(thread, this); |
| 345 | } |
| 346 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 347 | // Removes the instrumentation exit pc as the return PC for every quick frame. |
| 348 | static void InstrumentationRestoreStack(Thread* thread, void* arg) |
Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame] | 349 | REQUIRES(Locks::mutator_lock_) { |
| 350 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
| 351 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 352 | struct RestoreStackVisitor FINAL : public StackVisitor { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 353 | RestoreStackVisitor(Thread* thread_in, uintptr_t instrumentation_exit_pc, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 354 | Instrumentation* instrumentation) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 355 | : StackVisitor(thread_in, nullptr, kInstrumentationStackWalk), |
| 356 | thread_(thread_in), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 357 | instrumentation_exit_pc_(instrumentation_exit_pc), |
| 358 | instrumentation_(instrumentation), |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 359 | instrumentation_stack_(thread_in->GetInstrumentationStack()), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 360 | frames_removed_(0) {} |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 361 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 362 | bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 363 | if (instrumentation_stack_->size() == 0) { |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 364 | return false; // Stop. |
| 365 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 366 | ArtMethod* m = GetMethod(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 367 | if (GetCurrentQuickFrame() == nullptr) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 368 | if (kVerboseInstrumentation) { |
Daniel Mihalyi | ca1d06c | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 369 | LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId() |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 370 | << " Method=" << ArtMethod::PrettyMethod(m); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 371 | } |
| 372 | return true; // Ignore shadow frames. |
| 373 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 374 | if (m == nullptr) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 375 | if (kVerboseInstrumentation) { |
| 376 | LOG(INFO) << " Skipping upcall. Frame " << GetFrameId(); |
| 377 | } |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 378 | return true; // Ignore upcalls. |
| 379 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 380 | bool removed_stub = false; |
| 381 | // TODO: make this search more efficient? |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 382 | const size_t frameId = GetFrameId(); |
| 383 | for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) { |
| 384 | if (instrumentation_frame.frame_id_ == frameId) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 385 | if (kVerboseInstrumentation) { |
| 386 | LOG(INFO) << " Removing exit stub in " << DescribeLocation(); |
| 387 | } |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 388 | if (instrumentation_frame.interpreter_entry_) { |
Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 389 | CHECK(m == Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs)); |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 390 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 391 | CHECK(m == instrumentation_frame.method_) << ArtMethod::PrettyMethod(m); |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 392 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 393 | SetReturnPc(instrumentation_frame.return_pc_); |
Nicolas Geoffray | 07c7028 | 2017-08-30 08:09:42 +0000 | [diff] [blame] | 394 | if (instrumentation_->ShouldNotifyMethodEnterExitEvents()) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 395 | // Create the method exit events. As the methods didn't really exit the result is 0. |
| 396 | // We only do this if no debugger is attached to prevent from posting events twice. |
| 397 | instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m, |
| 398 | GetDexPc(), JValue()); |
| 399 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 400 | frames_removed_++; |
| 401 | removed_stub = true; |
| 402 | break; |
| 403 | } |
| 404 | } |
| 405 | if (!removed_stub) { |
| 406 | if (kVerboseInstrumentation) { |
| 407 | LOG(INFO) << " No exit stub in " << DescribeLocation(); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 408 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 409 | } |
| 410 | return true; // Continue. |
| 411 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 412 | Thread* const thread_; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 413 | const uintptr_t instrumentation_exit_pc_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 414 | Instrumentation* const instrumentation_; |
| 415 | std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_; |
| 416 | size_t frames_removed_; |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 417 | }; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 418 | if (kVerboseInstrumentation) { |
| 419 | std::string thread_name; |
| 420 | thread->GetThreadName(thread_name); |
| 421 | LOG(INFO) << "Removing exit stubs in " << thread_name; |
| 422 | } |
| 423 | std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack(); |
| 424 | if (stack->size() > 0) { |
| 425 | Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 426 | uintptr_t instrumentation_exit_pc = |
| 427 | reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 428 | RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation); |
| 429 | visitor.WalkStack(true); |
| 430 | CHECK_EQ(visitor.frames_removed_, stack->size()); |
| 431 | while (stack->size() > 0) { |
| 432 | stack->pop_front(); |
| 433 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 437 | static bool HasEvent(Instrumentation::InstrumentationEvent expected, uint32_t events) { |
| 438 | return (events & expected) != 0; |
| 439 | } |
| 440 | |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 441 | static void PotentiallyAddListenerTo(Instrumentation::InstrumentationEvent event, |
| 442 | uint32_t events, |
| 443 | std::list<InstrumentationListener*>& list, |
| 444 | InstrumentationListener* listener, |
| 445 | bool* has_listener) |
| 446 | REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) { |
| 447 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
| 448 | if (!HasEvent(event, events)) { |
| 449 | return; |
| 450 | } |
| 451 | // If there is a free slot in the list, we insert the listener in that slot. |
| 452 | // Otherwise we add it to the end of the list. |
| 453 | auto it = std::find(list.begin(), list.end(), nullptr); |
| 454 | if (it != list.end()) { |
| 455 | *it = listener; |
| 456 | } else { |
| 457 | list.push_back(listener); |
| 458 | } |
| 459 | *has_listener = true; |
| 460 | } |
| 461 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 462 | void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) { |
| 463 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 464 | PotentiallyAddListenerTo(kMethodEntered, |
| 465 | events, |
| 466 | method_entry_listeners_, |
| 467 | listener, |
| 468 | &have_method_entry_listeners_); |
| 469 | PotentiallyAddListenerTo(kMethodExited, |
| 470 | events, |
| 471 | method_exit_listeners_, |
| 472 | listener, |
| 473 | &have_method_exit_listeners_); |
| 474 | PotentiallyAddListenerTo(kMethodUnwind, |
| 475 | events, |
| 476 | method_unwind_listeners_, |
| 477 | listener, |
| 478 | &have_method_unwind_listeners_); |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 479 | PotentiallyAddListenerTo(kBranch, |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 480 | events, |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 481 | branch_listeners_, |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 482 | listener, |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 483 | &have_branch_listeners_); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 484 | PotentiallyAddListenerTo(kInvokeVirtualOrInterface, |
| 485 | events, |
| 486 | invoke_virtual_or_interface_listeners_, |
| 487 | listener, |
| 488 | &have_invoke_virtual_or_interface_listeners_); |
| 489 | PotentiallyAddListenerTo(kDexPcMoved, |
| 490 | events, |
| 491 | dex_pc_listeners_, |
| 492 | listener, |
| 493 | &have_dex_pc_listeners_); |
| 494 | PotentiallyAddListenerTo(kFieldRead, |
| 495 | events, |
| 496 | field_read_listeners_, |
| 497 | listener, |
| 498 | &have_field_read_listeners_); |
| 499 | PotentiallyAddListenerTo(kFieldWritten, |
| 500 | events, |
| 501 | field_write_listeners_, |
| 502 | listener, |
| 503 | &have_field_write_listeners_); |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 504 | PotentiallyAddListenerTo(kExceptionThrown, |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 505 | events, |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 506 | exception_thrown_listeners_, |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 507 | listener, |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 508 | &have_exception_thrown_listeners_); |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 509 | PotentiallyAddListenerTo(kWatchedFramePop, |
| 510 | events, |
| 511 | watched_frame_pop_listeners_, |
| 512 | listener, |
| 513 | &have_watched_frame_pop_listeners_); |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame^] | 514 | PotentiallyAddListenerTo(kExceptionHandled, |
| 515 | events, |
| 516 | exception_handled_listeners_, |
| 517 | listener, |
| 518 | &have_exception_handled_listeners_); |
Sebastien Hertz | ee1997a | 2013-09-19 14:47:09 +0200 | [diff] [blame] | 519 | UpdateInterpreterHandlerTable(); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 520 | } |
| 521 | |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 522 | static void PotentiallyRemoveListenerFrom(Instrumentation::InstrumentationEvent event, |
| 523 | uint32_t events, |
| 524 | std::list<InstrumentationListener*>& list, |
| 525 | InstrumentationListener* listener, |
| 526 | bool* has_listener) |
| 527 | REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) { |
| 528 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
| 529 | if (!HasEvent(event, events)) { |
| 530 | return; |
| 531 | } |
| 532 | auto it = std::find(list.begin(), list.end(), listener); |
| 533 | if (it != list.end()) { |
| 534 | // Just update the entry, do not remove from the list. Removing entries in the list |
| 535 | // is unsafe when mutators are iterating over it. |
| 536 | *it = nullptr; |
| 537 | } |
| 538 | |
| 539 | // Check if the list contains any non-null listener, and update 'has_listener'. |
| 540 | for (InstrumentationListener* l : list) { |
| 541 | if (l != nullptr) { |
| 542 | *has_listener = true; |
| 543 | return; |
| 544 | } |
| 545 | } |
| 546 | *has_listener = false; |
| 547 | } |
| 548 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 549 | void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) { |
| 550 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 551 | PotentiallyRemoveListenerFrom(kMethodEntered, |
| 552 | events, |
| 553 | method_entry_listeners_, |
| 554 | listener, |
| 555 | &have_method_entry_listeners_); |
| 556 | PotentiallyRemoveListenerFrom(kMethodExited, |
| 557 | events, |
| 558 | method_exit_listeners_, |
| 559 | listener, |
| 560 | &have_method_exit_listeners_); |
| 561 | PotentiallyRemoveListenerFrom(kMethodUnwind, |
| 562 | events, |
| 563 | method_unwind_listeners_, |
| 564 | listener, |
| 565 | &have_method_unwind_listeners_); |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 566 | PotentiallyRemoveListenerFrom(kBranch, |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 567 | events, |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 568 | branch_listeners_, |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 569 | listener, |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 570 | &have_branch_listeners_); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 571 | PotentiallyRemoveListenerFrom(kInvokeVirtualOrInterface, |
| 572 | events, |
| 573 | invoke_virtual_or_interface_listeners_, |
| 574 | listener, |
| 575 | &have_invoke_virtual_or_interface_listeners_); |
| 576 | PotentiallyRemoveListenerFrom(kDexPcMoved, |
| 577 | events, |
| 578 | dex_pc_listeners_, |
| 579 | listener, |
| 580 | &have_dex_pc_listeners_); |
| 581 | PotentiallyRemoveListenerFrom(kFieldRead, |
| 582 | events, |
| 583 | field_read_listeners_, |
| 584 | listener, |
| 585 | &have_field_read_listeners_); |
| 586 | PotentiallyRemoveListenerFrom(kFieldWritten, |
| 587 | events, |
| 588 | field_write_listeners_, |
| 589 | listener, |
| 590 | &have_field_write_listeners_); |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 591 | PotentiallyRemoveListenerFrom(kExceptionThrown, |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 592 | events, |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 593 | exception_thrown_listeners_, |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 594 | listener, |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 595 | &have_exception_thrown_listeners_); |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 596 | PotentiallyRemoveListenerFrom(kWatchedFramePop, |
| 597 | events, |
| 598 | watched_frame_pop_listeners_, |
| 599 | listener, |
| 600 | &have_watched_frame_pop_listeners_); |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame^] | 601 | PotentiallyRemoveListenerFrom(kExceptionHandled, |
| 602 | events, |
| 603 | exception_handled_listeners_, |
| 604 | listener, |
| 605 | &have_exception_handled_listeners_); |
Sebastien Hertz | ee1997a | 2013-09-19 14:47:09 +0200 | [diff] [blame] | 606 | UpdateInterpreterHandlerTable(); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 607 | } |
| 608 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 609 | Instrumentation::InstrumentationLevel Instrumentation::GetCurrentInstrumentationLevel() const { |
Alex Light | 4ba388a | 2017-01-27 10:26:49 -0800 | [diff] [blame] | 610 | if (interpreter_stubs_installed_) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 611 | return InstrumentationLevel::kInstrumentWithInterpreter; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 612 | } else if (entry_exit_stubs_installed_) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 613 | return InstrumentationLevel::kInstrumentWithInstrumentationStubs; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 614 | } else { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 615 | return InstrumentationLevel::kInstrumentNothing; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 616 | } |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 617 | } |
| 618 | |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 619 | bool Instrumentation::RequiresInstrumentationInstallation(InstrumentationLevel new_level) const { |
Alex Light | 4ba388a | 2017-01-27 10:26:49 -0800 | [diff] [blame] | 620 | // We need to reinstall instrumentation if we go to a different level. |
| 621 | return GetCurrentInstrumentationLevel() != new_level; |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 622 | } |
| 623 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 624 | void Instrumentation::ConfigureStubs(const char* key, InstrumentationLevel desired_level) { |
| 625 | // Store the instrumentation level for this key or remove it. |
| 626 | if (desired_level == InstrumentationLevel::kInstrumentNothing) { |
| 627 | // The client no longer needs instrumentation. |
| 628 | requested_instrumentation_levels_.erase(key); |
| 629 | } else { |
| 630 | // The client needs instrumentation. |
| 631 | requested_instrumentation_levels_.Overwrite(key, desired_level); |
| 632 | } |
| 633 | |
| 634 | // Look for the highest required instrumentation level. |
| 635 | InstrumentationLevel requested_level = InstrumentationLevel::kInstrumentNothing; |
| 636 | for (const auto& v : requested_instrumentation_levels_) { |
| 637 | requested_level = std::max(requested_level, v.second); |
| 638 | } |
| 639 | |
| 640 | interpret_only_ = (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) || |
| 641 | forced_interpret_only_; |
| 642 | |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 643 | if (!RequiresInstrumentationInstallation(requested_level)) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 644 | // We're already set. |
| 645 | return; |
| 646 | } |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 647 | Thread* const self = Thread::Current(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 648 | Runtime* runtime = Runtime::Current(); |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 649 | Locks::mutator_lock_->AssertExclusiveHeld(self); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 650 | Locks::thread_list_lock_->AssertNotHeld(self); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 651 | if (requested_level > InstrumentationLevel::kInstrumentNothing) { |
Alex Light | 4ba388a | 2017-01-27 10:26:49 -0800 | [diff] [blame] | 652 | if (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 653 | interpreter_stubs_installed_ = true; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 654 | entry_exit_stubs_installed_ = true; |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 655 | } else { |
| 656 | CHECK_EQ(requested_level, InstrumentationLevel::kInstrumentWithInstrumentationStubs); |
| 657 | entry_exit_stubs_installed_ = true; |
| 658 | interpreter_stubs_installed_ = false; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 659 | } |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 660 | InstallStubsClassVisitor visitor(this); |
| 661 | runtime->GetClassLinker()->VisitClasses(&visitor); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 662 | instrumentation_stubs_installed_ = true; |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 663 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 664 | runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this); |
| 665 | } else { |
| 666 | interpreter_stubs_installed_ = false; |
| 667 | entry_exit_stubs_installed_ = false; |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 668 | InstallStubsClassVisitor visitor(this); |
| 669 | runtime->GetClassLinker()->VisitClasses(&visitor); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 670 | // Restore stack only if there is no method currently deoptimized. |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 671 | bool empty; |
| 672 | { |
| 673 | ReaderMutexLock mu(self, deoptimized_methods_lock_); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 674 | empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation. |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 675 | } |
| 676 | if (empty) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 677 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 678 | Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this); |
Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame] | 679 | // Only do this after restoring, as walking the stack when restoring will see |
| 680 | // the instrumentation exit pc. |
| 681 | instrumentation_stubs_installed_ = false; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 682 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 683 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 684 | } |
| 685 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 686 | static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg ATTRIBUTE_UNUSED) { |
Mathieu Chartier | 5ace201 | 2016-11-30 10:15:41 -0800 | [diff] [blame] | 687 | thread->ResetQuickAllocEntryPointsForThread(kUseReadBarrier && thread->GetIsGcMarking()); |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 688 | } |
| 689 | |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 690 | void Instrumentation::SetEntrypointsInstrumented(bool instrumented) { |
| 691 | Thread* self = Thread::Current(); |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 692 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 693 | Locks::mutator_lock_->AssertNotHeld(self); |
| 694 | Locks::instrument_entrypoints_lock_->AssertHeld(self); |
| 695 | if (runtime->IsStarted()) { |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 696 | ScopedSuspendAll ssa(__FUNCTION__); |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 697 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 698 | SetQuickAllocEntryPointsInstrumented(instrumented); |
| 699 | ResetQuickAllocEntryPoints(); |
Mathieu Chartier | 50e9331 | 2016-03-16 11:25:29 -0700 | [diff] [blame] | 700 | alloc_entrypoints_instrumented_ = instrumented; |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 701 | } else { |
| 702 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
| 703 | SetQuickAllocEntryPointsInstrumented(instrumented); |
Andreas Gampe | 157c77e | 2016-10-17 17:44:41 -0700 | [diff] [blame] | 704 | |
| 705 | // Note: ResetQuickAllocEntryPoints only works when the runtime is started. Manually run the |
| 706 | // update for just this thread. |
Andreas Gampe | 162ae50 | 2016-10-18 10:03:42 -0700 | [diff] [blame] | 707 | // Note: self may be null. One of those paths is setting instrumentation in the Heap |
| 708 | // constructor for gcstress mode. |
| 709 | if (self != nullptr) { |
| 710 | ResetQuickAllocEntryPointsForThread(self, nullptr); |
| 711 | } |
Andreas Gampe | 157c77e | 2016-10-17 17:44:41 -0700 | [diff] [blame] | 712 | |
Mathieu Chartier | 50e9331 | 2016-03-16 11:25:29 -0700 | [diff] [blame] | 713 | alloc_entrypoints_instrumented_ = instrumented; |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 714 | } |
| 715 | } |
| 716 | |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 717 | void Instrumentation::InstrumentQuickAllocEntryPoints() { |
| 718 | MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_); |
| 719 | InstrumentQuickAllocEntryPointsLocked(); |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 720 | } |
| 721 | |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 722 | void Instrumentation::UninstrumentQuickAllocEntryPoints() { |
| 723 | MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_); |
| 724 | UninstrumentQuickAllocEntryPointsLocked(); |
| 725 | } |
| 726 | |
| 727 | void Instrumentation::InstrumentQuickAllocEntryPointsLocked() { |
| 728 | Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current()); |
| 729 | if (quick_alloc_entry_points_instrumentation_counter_ == 0) { |
| 730 | SetEntrypointsInstrumented(true); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 731 | } |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 732 | ++quick_alloc_entry_points_instrumentation_counter_; |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | void Instrumentation::UninstrumentQuickAllocEntryPointsLocked() { |
| 736 | Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current()); |
| 737 | CHECK_GT(quick_alloc_entry_points_instrumentation_counter_, 0U); |
| 738 | --quick_alloc_entry_points_instrumentation_counter_; |
| 739 | if (quick_alloc_entry_points_instrumentation_counter_ == 0) { |
| 740 | SetEntrypointsInstrumented(false); |
| 741 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | void Instrumentation::ResetQuickAllocEntryPoints() { |
| 745 | Runtime* runtime = Runtime::Current(); |
| 746 | if (runtime->IsStarted()) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 747 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 748 | runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, nullptr); |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
Mingyao Yang | 3fd448a | 2016-05-10 14:30:41 -0700 | [diff] [blame] | 752 | void Instrumentation::UpdateMethodsCodeImpl(ArtMethod* method, const void* quick_code) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 753 | const void* new_quick_code; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 754 | if (LIKELY(!instrumentation_stubs_installed_)) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 755 | new_quick_code = quick_code; |
Jeff Hao | 65d15d9 | 2013-07-16 16:39:33 -0700 | [diff] [blame] | 756 | } else { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 757 | if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 758 | new_quick_code = GetQuickToInterpreterBridge(); |
Jeff Hao | 65d15d9 | 2013-07-16 16:39:33 -0700 | [diff] [blame] | 759 | } else { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 760 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 761 | if (class_linker->IsQuickResolutionStub(quick_code) || |
| 762 | class_linker->IsQuickToInterpreterBridge(quick_code)) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 763 | new_quick_code = quick_code; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 764 | } else if (entry_exit_stubs_installed_) { |
| 765 | new_quick_code = GetQuickInstrumentationEntryPoint(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 766 | } else { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 767 | new_quick_code = quick_code; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 768 | } |
Jeff Hao | 65d15d9 | 2013-07-16 16:39:33 -0700 | [diff] [blame] | 769 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 770 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 771 | UpdateEntrypoints(method, new_quick_code); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 772 | } |
| 773 | |
Mingyao Yang | 3fd448a | 2016-05-10 14:30:41 -0700 | [diff] [blame] | 774 | void Instrumentation::UpdateMethodsCode(ArtMethod* method, const void* quick_code) { |
| 775 | DCHECK(method->GetDeclaringClass()->IsResolved()); |
| 776 | UpdateMethodsCodeImpl(method, quick_code); |
| 777 | } |
| 778 | |
Nicolas Geoffray | a0619e2 | 2016-12-20 13:57:43 +0000 | [diff] [blame] | 779 | void Instrumentation::UpdateMethodsCodeForJavaDebuggable(ArtMethod* method, |
| 780 | const void* quick_code) { |
| 781 | // When the runtime is set to Java debuggable, we may update the entry points of |
| 782 | // all methods of a class to the interpreter bridge. A method's declaring class |
| 783 | // might not be in resolved state yet in that case, so we bypass the DCHECK in |
| 784 | // UpdateMethodsCode. |
Mingyao Yang | 3fd448a | 2016-05-10 14:30:41 -0700 | [diff] [blame] | 785 | UpdateMethodsCodeImpl(method, quick_code); |
| 786 | } |
| 787 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 788 | bool Instrumentation::AddDeoptimizedMethod(ArtMethod* method) { |
| 789 | if (IsDeoptimizedMethod(method)) { |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 790 | // Already in the map. Return. |
| 791 | return false; |
| 792 | } |
| 793 | // Not found. Add it. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 794 | deoptimized_methods_.insert(method); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 795 | return true; |
| 796 | } |
| 797 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 798 | bool Instrumentation::IsDeoptimizedMethod(ArtMethod* method) { |
| 799 | return deoptimized_methods_.find(method) != deoptimized_methods_.end(); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 800 | } |
| 801 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 802 | ArtMethod* Instrumentation::BeginDeoptimizedMethod() { |
| 803 | if (deoptimized_methods_.empty()) { |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 804 | // Empty. |
| 805 | return nullptr; |
| 806 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 807 | return *deoptimized_methods_.begin(); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 808 | } |
| 809 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 810 | bool Instrumentation::RemoveDeoptimizedMethod(ArtMethod* method) { |
| 811 | auto it = deoptimized_methods_.find(method); |
| 812 | if (it == deoptimized_methods_.end()) { |
| 813 | return false; |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 814 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 815 | deoptimized_methods_.erase(it); |
| 816 | return true; |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | bool Instrumentation::IsDeoptimizedMethodsEmpty() const { |
| 820 | return deoptimized_methods_.empty(); |
| 821 | } |
| 822 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 823 | void Instrumentation::Deoptimize(ArtMethod* method) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 824 | CHECK(!method->IsNative()); |
| 825 | CHECK(!method->IsProxyMethod()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 826 | CHECK(method->IsInvokable()); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 827 | |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 828 | Thread* self = Thread::Current(); |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 829 | { |
| 830 | WriterMutexLock mu(self, deoptimized_methods_lock_); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 831 | bool has_not_been_deoptimized = AddDeoptimizedMethod(method); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 832 | CHECK(has_not_been_deoptimized) << "Method " << ArtMethod::PrettyMethod(method) |
Daniel Mihalyi | ca1d06c | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 833 | << " is already deoptimized"; |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 834 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 835 | if (!interpreter_stubs_installed_) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 836 | UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint()); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 837 | |
| 838 | // Install instrumentation exit stub and instrumentation frames. We may already have installed |
| 839 | // these previously so it will only cover the newly created frames. |
| 840 | instrumentation_stubs_installed_ = true; |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 841 | MutexLock mu(self, *Locks::thread_list_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 842 | Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this); |
| 843 | } |
| 844 | } |
| 845 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 846 | void Instrumentation::Undeoptimize(ArtMethod* method) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 847 | CHECK(!method->IsNative()); |
| 848 | CHECK(!method->IsProxyMethod()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 849 | CHECK(method->IsInvokable()); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 850 | |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 851 | Thread* self = Thread::Current(); |
| 852 | bool empty; |
| 853 | { |
| 854 | WriterMutexLock mu(self, deoptimized_methods_lock_); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 855 | bool found_and_erased = RemoveDeoptimizedMethod(method); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 856 | CHECK(found_and_erased) << "Method " << ArtMethod::PrettyMethod(method) |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 857 | << " is not deoptimized"; |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 858 | empty = IsDeoptimizedMethodsEmpty(); |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 859 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 860 | |
| 861 | // Restore code and possibly stack only if we did not deoptimize everything. |
| 862 | if (!interpreter_stubs_installed_) { |
| 863 | // Restore its code or resolution trampoline. |
| 864 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 865 | if (method->IsStatic() && !method->IsConstructor() && |
| 866 | !method->GetDeclaringClass()->IsInitialized()) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 867 | UpdateEntrypoints(method, GetQuickResolutionStub()); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 868 | } else { |
Nicolas Geoffray | a0619e2 | 2016-12-20 13:57:43 +0000 | [diff] [blame] | 869 | const void* quick_code = NeedDebugVersionFor(method) |
| 870 | ? GetQuickToInterpreterBridge() |
| 871 | : class_linker->GetQuickOatCodeFor(method); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 872 | UpdateEntrypoints(method, quick_code); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | // 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] | 876 | if (empty) { |
| 877 | MutexLock mu(self, *Locks::thread_list_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 878 | Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this); |
| 879 | instrumentation_stubs_installed_ = false; |
| 880 | } |
| 881 | } |
| 882 | } |
| 883 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 884 | bool Instrumentation::IsDeoptimized(ArtMethod* method) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 885 | DCHECK(method != nullptr); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 886 | ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 887 | return IsDeoptimizedMethod(method); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | void Instrumentation::EnableDeoptimization() { |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 891 | ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 892 | CHECK(IsDeoptimizedMethodsEmpty()); |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 893 | CHECK_EQ(deoptimization_enabled_, false); |
| 894 | deoptimization_enabled_ = true; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 895 | } |
| 896 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 897 | void Instrumentation::DisableDeoptimization(const char* key) { |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 898 | CHECK_EQ(deoptimization_enabled_, true); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 899 | // If we deoptimized everything, undo it. |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 900 | InstrumentationLevel level = GetCurrentInstrumentationLevel(); |
| 901 | if (level == InstrumentationLevel::kInstrumentWithInterpreter) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 902 | UndeoptimizeEverything(key); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 903 | } |
| 904 | // Undeoptimized selected methods. |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 905 | while (true) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 906 | ArtMethod* method; |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 907 | { |
| 908 | ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 909 | if (IsDeoptimizedMethodsEmpty()) { |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 910 | break; |
| 911 | } |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 912 | method = BeginDeoptimizedMethod(); |
| 913 | CHECK(method != nullptr); |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 914 | } |
| 915 | Undeoptimize(method); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 916 | } |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 917 | deoptimization_enabled_ = false; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 918 | } |
| 919 | |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 920 | // Indicates if instrumentation should notify method enter/exit events to the listeners. |
| 921 | bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 922 | if (!HasMethodEntryListeners() && !HasMethodExitListeners()) { |
| 923 | return false; |
| 924 | } |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 925 | return !deoptimization_enabled_ && !interpreter_stubs_installed_; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 926 | } |
| 927 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 928 | void Instrumentation::DeoptimizeEverything(const char* key) { |
| 929 | CHECK(deoptimization_enabled_); |
| 930 | ConfigureStubs(key, InstrumentationLevel::kInstrumentWithInterpreter); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 931 | } |
| 932 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 933 | void Instrumentation::UndeoptimizeEverything(const char* key) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 934 | CHECK(interpreter_stubs_installed_); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 935 | CHECK(deoptimization_enabled_); |
| 936 | ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 937 | } |
| 938 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 939 | void Instrumentation::EnableMethodTracing(const char* key, bool needs_interpreter) { |
| 940 | InstrumentationLevel level; |
| 941 | if (needs_interpreter) { |
| 942 | level = InstrumentationLevel::kInstrumentWithInterpreter; |
| 943 | } else { |
| 944 | level = InstrumentationLevel::kInstrumentWithInstrumentationStubs; |
| 945 | } |
| 946 | ConfigureStubs(key, level); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 947 | } |
| 948 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 949 | void Instrumentation::DisableMethodTracing(const char* key) { |
| 950 | ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 951 | } |
| 952 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 953 | const void* Instrumentation::GetQuickCodeFor(ArtMethod* method, PointerSize pointer_size) const { |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 954 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 955 | if (LIKELY(!instrumentation_stubs_installed_)) { |
Mathieu Chartier | a7dd038 | 2014-11-20 17:08:58 -0800 | [diff] [blame] | 956 | const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size); |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 957 | DCHECK(code != nullptr); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 958 | if (LIKELY(!class_linker->IsQuickResolutionStub(code) && |
| 959 | !class_linker->IsQuickToInterpreterBridge(code)) && |
| 960 | !class_linker->IsQuickResolutionStub(code) && |
| 961 | !class_linker->IsQuickToInterpreterBridge(code)) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 962 | return code; |
| 963 | } |
| 964 | } |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 965 | return class_linker->GetQuickOatCodeFor(method); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 966 | } |
| 967 | |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 968 | void Instrumentation::MethodEnterEventImpl(Thread* thread, |
| 969 | ObjPtr<mirror::Object> this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 970 | ArtMethod* method, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 971 | uint32_t dex_pc) const { |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 972 | if (HasMethodEntryListeners()) { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 973 | Thread* self = Thread::Current(); |
| 974 | StackHandleScope<1> hs(self); |
| 975 | Handle<mirror::Object> thiz(hs.NewHandle(this_object)); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 976 | for (InstrumentationListener* listener : method_entry_listeners_) { |
| 977 | if (listener != nullptr) { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 978 | listener->MethodEntered(thread, thiz, method, dex_pc); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 979 | } |
| 980 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 981 | } |
| 982 | } |
| 983 | |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 984 | void Instrumentation::MethodExitEventImpl(Thread* thread, |
| 985 | ObjPtr<mirror::Object> this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 986 | ArtMethod* method, |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 987 | uint32_t dex_pc, |
| 988 | const JValue& return_value) const { |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 989 | if (HasMethodExitListeners()) { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 990 | Thread* self = Thread::Current(); |
| 991 | StackHandleScope<2> hs(self); |
| 992 | Handle<mirror::Object> thiz(hs.NewHandle(this_object)); |
| 993 | if (method->GetInterfaceMethodIfProxy(kRuntimePointerSize) |
| 994 | ->GetReturnTypePrimitive() != Primitive::kPrimNot) { |
| 995 | for (InstrumentationListener* listener : method_exit_listeners_) { |
| 996 | if (listener != nullptr) { |
| 997 | listener->MethodExited(thread, thiz, method, dex_pc, return_value); |
| 998 | } |
| 999 | } |
| 1000 | } else { |
| 1001 | Handle<mirror::Object> ret(hs.NewHandle(return_value.GetL())); |
| 1002 | for (InstrumentationListener* listener : method_exit_listeners_) { |
| 1003 | if (listener != nullptr) { |
| 1004 | listener->MethodExited(thread, thiz, method, dex_pc, ret); |
| 1005 | } |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1006 | } |
| 1007 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1008 | } |
| 1009 | } |
| 1010 | |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1011 | void Instrumentation::MethodUnwindEvent(Thread* thread, |
| 1012 | mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1013 | ArtMethod* method, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1014 | uint32_t dex_pc) const { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 1015 | if (HasMethodUnwindListeners()) { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1016 | Thread* self = Thread::Current(); |
| 1017 | StackHandleScope<1> hs(self); |
| 1018 | Handle<mirror::Object> thiz(hs.NewHandle(this_object)); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1019 | for (InstrumentationListener* listener : method_unwind_listeners_) { |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1020 | if (listener != nullptr) { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1021 | listener->MethodUnwind(thread, thiz, method, dex_pc); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1022 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1023 | } |
| 1024 | } |
| 1025 | } |
| 1026 | |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1027 | void Instrumentation::DexPcMovedEventImpl(Thread* thread, |
| 1028 | ObjPtr<mirror::Object> this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1029 | ArtMethod* method, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1030 | uint32_t dex_pc) const { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1031 | Thread* self = Thread::Current(); |
| 1032 | StackHandleScope<1> hs(self); |
| 1033 | Handle<mirror::Object> thiz(hs.NewHandle(this_object)); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1034 | for (InstrumentationListener* listener : dex_pc_listeners_) { |
| 1035 | if (listener != nullptr) { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1036 | listener->DexPcMoved(thread, thiz, method, dex_pc); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1037 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1038 | } |
| 1039 | } |
| 1040 | |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 1041 | void Instrumentation::BranchImpl(Thread* thread, |
| 1042 | ArtMethod* method, |
| 1043 | uint32_t dex_pc, |
| 1044 | int32_t offset) const { |
| 1045 | for (InstrumentationListener* listener : branch_listeners_) { |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1046 | if (listener != nullptr) { |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 1047 | listener->Branch(thread, method, dex_pc, offset); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1048 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1049 | } |
| 1050 | } |
| 1051 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 1052 | void Instrumentation::InvokeVirtualOrInterfaceImpl(Thread* thread, |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1053 | ObjPtr<mirror::Object> this_object, |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 1054 | ArtMethod* caller, |
| 1055 | uint32_t dex_pc, |
| 1056 | ArtMethod* callee) const { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1057 | Thread* self = Thread::Current(); |
| 1058 | StackHandleScope<1> hs(self); |
| 1059 | Handle<mirror::Object> thiz(hs.NewHandle(this_object)); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 1060 | for (InstrumentationListener* listener : invoke_virtual_or_interface_listeners_) { |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1061 | if (listener != nullptr) { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1062 | listener->InvokeVirtualOrInterface(thread, thiz, caller, dex_pc, callee); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1063 | } |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 1064 | } |
| 1065 | } |
| 1066 | |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 1067 | void Instrumentation::WatchedFramePopImpl(Thread* thread, const ShadowFrame& frame) const { |
| 1068 | for (InstrumentationListener* listener : watched_frame_pop_listeners_) { |
| 1069 | if (listener != nullptr) { |
| 1070 | listener->WatchedFramePop(thread, frame); |
| 1071 | } |
| 1072 | } |
| 1073 | } |
| 1074 | |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1075 | void Instrumentation::FieldReadEventImpl(Thread* thread, |
| 1076 | ObjPtr<mirror::Object> this_object, |
| 1077 | ArtMethod* method, |
| 1078 | uint32_t dex_pc, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1079 | ArtField* field) const { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1080 | Thread* self = Thread::Current(); |
| 1081 | StackHandleScope<1> hs(self); |
| 1082 | Handle<mirror::Object> thiz(hs.NewHandle(this_object)); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1083 | for (InstrumentationListener* listener : field_read_listeners_) { |
| 1084 | if (listener != nullptr) { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1085 | listener->FieldRead(thread, thiz, method, dex_pc, field); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1086 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 1087 | } |
| 1088 | } |
| 1089 | |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1090 | void Instrumentation::FieldWriteEventImpl(Thread* thread, |
| 1091 | ObjPtr<mirror::Object> this_object, |
| 1092 | ArtMethod* method, |
| 1093 | uint32_t dex_pc, |
| 1094 | ArtField* field, |
| 1095 | const JValue& field_value) const { |
| 1096 | Thread* self = Thread::Current(); |
| 1097 | StackHandleScope<2> hs(self); |
| 1098 | Handle<mirror::Object> thiz(hs.NewHandle(this_object)); |
| 1099 | if (field->IsPrimitiveType()) { |
| 1100 | for (InstrumentationListener* listener : field_write_listeners_) { |
| 1101 | if (listener != nullptr) { |
| 1102 | listener->FieldWritten(thread, thiz, method, dex_pc, field, field_value); |
| 1103 | } |
| 1104 | } |
| 1105 | } else { |
| 1106 | Handle<mirror::Object> val(hs.NewHandle(field_value.GetL())); |
| 1107 | for (InstrumentationListener* listener : field_write_listeners_) { |
| 1108 | if (listener != nullptr) { |
| 1109 | listener->FieldWritten(thread, thiz, method, dex_pc, field, val); |
| 1110 | } |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1111 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 1112 | } |
| 1113 | } |
| 1114 | |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 1115 | void Instrumentation::ExceptionThrownEvent(Thread* thread, |
Sebastien Hertz | 947ff08 | 2013-09-17 14:10:13 +0200 | [diff] [blame] | 1116 | mirror::Throwable* exception_object) const { |
Hiroshi Yamauchi | 3481f7a | 2017-02-10 12:07:36 -0800 | [diff] [blame] | 1117 | Thread* self = Thread::Current(); |
| 1118 | StackHandleScope<1> hs(self); |
| 1119 | Handle<mirror::Throwable> h_exception(hs.NewHandle(exception_object)); |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 1120 | if (HasExceptionThrownListeners()) { |
Hiroshi Yamauchi | 3481f7a | 2017-02-10 12:07:36 -0800 | [diff] [blame] | 1121 | DCHECK_EQ(thread->GetException(), h_exception.Get()); |
Jeff Hao | c0bd4da | 2013-04-11 15:52:28 -0700 | [diff] [blame] | 1122 | thread->ClearException(); |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 1123 | for (InstrumentationListener* listener : exception_thrown_listeners_) { |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1124 | if (listener != nullptr) { |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 1125 | listener->ExceptionThrown(thread, h_exception); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1126 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1127 | } |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame^] | 1128 | // See b/65049545 for discussion about this behavior. |
| 1129 | thread->AssertNoPendingException(); |
Hiroshi Yamauchi | 3481f7a | 2017-02-10 12:07:36 -0800 | [diff] [blame] | 1130 | thread->SetException(h_exception.Get()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1131 | } |
| 1132 | } |
| 1133 | |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame^] | 1134 | void Instrumentation::ExceptionHandledEvent(Thread* thread, |
| 1135 | mirror::Throwable* exception_object) const { |
| 1136 | Thread* self = Thread::Current(); |
| 1137 | StackHandleScope<1> hs(self); |
| 1138 | Handle<mirror::Throwable> h_exception(hs.NewHandle(exception_object)); |
| 1139 | if (HasExceptionHandledListeners()) { |
| 1140 | // We should have cleared the exception so that callers can detect a new one. |
| 1141 | DCHECK(thread->GetException() == nullptr); |
| 1142 | for (InstrumentationListener* listener : exception_handled_listeners_) { |
| 1143 | if (listener != nullptr) { |
| 1144 | listener->ExceptionHandled(thread, h_exception); |
| 1145 | } |
| 1146 | } |
| 1147 | } |
| 1148 | } |
| 1149 | |
Sebastien Hertz | b2feaaf | 2015-10-12 13:40:10 +0000 | [diff] [blame] | 1150 | // Computes a frame ID by ignoring inlined frames. |
| 1151 | size_t Instrumentation::ComputeFrameId(Thread* self, |
| 1152 | size_t frame_depth, |
| 1153 | size_t inlined_frames_before_frame) { |
| 1154 | CHECK_GE(frame_depth, inlined_frames_before_frame); |
| 1155 | size_t no_inline_depth = frame_depth - inlined_frames_before_frame; |
| 1156 | return StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) - no_inline_depth; |
| 1157 | } |
| 1158 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1159 | static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame, |
| 1160 | int delta) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1161 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 1162 | size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) + delta; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1163 | if (frame_id != instrumentation_frame.frame_id_) { |
| 1164 | LOG(ERROR) << "Expected frame_id=" << frame_id << " but found " |
| 1165 | << instrumentation_frame.frame_id_; |
| 1166 | StackVisitor::DescribeStack(self); |
| 1167 | CHECK_EQ(frame_id, instrumentation_frame.frame_id_); |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1172 | ArtMethod* method, |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 1173 | uintptr_t lr, bool interpreter_entry) { |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1174 | DCHECK(!self->IsExceptionPending()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1175 | std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack(); |
| 1176 | if (kVerboseInstrumentation) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1177 | LOG(INFO) << "Entering " << ArtMethod::PrettyMethod(method) << " from PC " |
| 1178 | << reinterpret_cast<void*>(lr); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1179 | } |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1180 | |
| 1181 | // We send the enter event before pushing the instrumentation frame to make cleanup easier. If the |
| 1182 | // event causes an exception we can simply send the unwind event and return. |
| 1183 | StackHandleScope<1> hs(self); |
| 1184 | Handle<mirror::Object> h_this(hs.NewHandle(this_object)); |
| 1185 | if (!interpreter_entry) { |
| 1186 | MethodEnterEvent(self, h_this.Get(), method, 0); |
| 1187 | if (self->IsExceptionPending()) { |
| 1188 | MethodUnwindEvent(self, h_this.Get(), method, 0); |
| 1189 | return; |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | // We have a callee-save frame meaning this value is guaranteed to never be 0. |
| 1194 | DCHECK(!self->IsExceptionPending()); |
| 1195 | size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk); |
| 1196 | |
| 1197 | instrumentation::InstrumentationStackFrame instrumentation_frame(h_this.Get(), method, lr, |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 1198 | frame_id, interpreter_entry); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1199 | stack->push_front(instrumentation_frame); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1200 | } |
| 1201 | |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1202 | TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self, |
| 1203 | uintptr_t* return_pc, |
| 1204 | uint64_t* gpr_result, |
| 1205 | uint64_t* fpr_result) { |
| 1206 | DCHECK(gpr_result != nullptr); |
| 1207 | DCHECK(fpr_result != nullptr); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1208 | // Do the pop. |
| 1209 | std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack(); |
| 1210 | CHECK_GT(stack->size(), 0U); |
| 1211 | InstrumentationStackFrame instrumentation_frame = stack->front(); |
| 1212 | stack->pop_front(); |
| 1213 | |
| 1214 | // Set return PC and check the sanity of the stack. |
| 1215 | *return_pc = instrumentation_frame.return_pc_; |
| 1216 | CheckStackDepth(self, instrumentation_frame, 0); |
Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 1217 | self->VerifyStack(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1218 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1219 | ArtMethod* method = instrumentation_frame.method_; |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1220 | uint32_t length; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1221 | const PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
Nicolas Geoffray | 07c7028 | 2017-08-30 08:09:42 +0000 | [diff] [blame] | 1222 | char return_shorty = method->GetInterfaceMethodIfProxy(pointer_size)->GetShorty(&length)[0]; |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1223 | bool is_ref = return_shorty == '[' || return_shorty == 'L'; |
| 1224 | StackHandleScope<1> hs(self); |
| 1225 | MutableHandle<mirror::Object> res(hs.NewHandle<mirror::Object>(nullptr)); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1226 | JValue return_value; |
| 1227 | if (return_shorty == 'V') { |
| 1228 | return_value.SetJ(0); |
| 1229 | } else if (return_shorty == 'F' || return_shorty == 'D') { |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1230 | return_value.SetJ(*fpr_result); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1231 | } else { |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1232 | return_value.SetJ(*gpr_result); |
| 1233 | } |
| 1234 | if (is_ref) { |
| 1235 | // Take a handle to the return value so we won't lose it if we suspend. |
| 1236 | res.Assign(return_value.GetL()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1237 | } |
| 1238 | // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to |
| 1239 | // return_pc. |
| 1240 | uint32_t dex_pc = DexFile::kDexNoIndex; |
| 1241 | mirror::Object* this_object = instrumentation_frame.this_object_; |
Nicolas Geoffray | 07c7028 | 2017-08-30 08:09:42 +0000 | [diff] [blame] | 1242 | if (!instrumentation_frame.interpreter_entry_) { |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 1243 | MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value); |
| 1244 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 1245 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 1246 | // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get |
| 1247 | // back to an upcall. |
| 1248 | NthCallerVisitor visitor(self, 1, true); |
| 1249 | visitor.WalkStack(true); |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame] | 1250 | bool deoptimize = (visitor.caller != nullptr) && |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 1251 | (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller) || |
| 1252 | Dbg::IsForcedInterpreterNeededForUpcall(self, visitor.caller)); |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1253 | if (is_ref) { |
| 1254 | // Restore the return value if it's a reference since it might have moved. |
| 1255 | *reinterpret_cast<mirror::Object**>(gpr_result) = res.Get(); |
| 1256 | } |
Nicolas Geoffray | a0619e2 | 2016-12-20 13:57:43 +0000 | [diff] [blame] | 1257 | if (deoptimize && Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1258 | if (kVerboseInstrumentation) { |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 1259 | LOG(INFO) << "Deoptimizing " |
| 1260 | << visitor.caller->PrettyMethod() |
| 1261 | << " by returning from " |
| 1262 | << method->PrettyMethod() |
| 1263 | << " with result " |
| 1264 | << std::hex << return_value.GetJ() << std::dec |
| 1265 | << " in " |
| 1266 | << *self; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1267 | } |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1268 | self->PushDeoptimizationContext(return_value, |
Nicolas Geoffray | 07c7028 | 2017-08-30 08:09:42 +0000 | [diff] [blame] | 1269 | return_shorty == 'L', |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1270 | false /* from_code */, |
Nicolas Geoffray | 07c7028 | 2017-08-30 08:09:42 +0000 | [diff] [blame] | 1271 | nullptr /* no pending exception */); |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame] | 1272 | return GetTwoWordSuccessValue(*return_pc, |
| 1273 | reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint())); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1274 | } else { |
Nicolas Geoffray | a0619e2 | 2016-12-20 13:57:43 +0000 | [diff] [blame] | 1275 | if (deoptimize && !Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) { |
| 1276 | LOG(WARNING) << "Got a deoptimization request on un-deoptimizable " << method->PrettyMethod() |
| 1277 | << " at PC " << reinterpret_cast<void*>(*return_pc); |
| 1278 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1279 | if (kVerboseInstrumentation) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1280 | LOG(INFO) << "Returning from " << method->PrettyMethod() |
Brian Carlstrom | 2d88862 | 2013-07-18 17:02:00 -0700 | [diff] [blame] | 1281 | << " to PC " << reinterpret_cast<void*>(*return_pc); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1282 | } |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame] | 1283 | return GetTwoWordSuccessValue(0, *return_pc); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1284 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 1285 | } |
| 1286 | |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 1287 | uintptr_t Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1288 | // Do the pop. |
| 1289 | std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack(); |
| 1290 | CHECK_GT(stack->size(), 0U); |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1291 | size_t idx = stack->size(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1292 | InstrumentationStackFrame instrumentation_frame = stack->front(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1293 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1294 | ArtMethod* method = instrumentation_frame.method_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1295 | if (is_deoptimization) { |
| 1296 | if (kVerboseInstrumentation) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1297 | LOG(INFO) << "Popping for deoptimization " << ArtMethod::PrettyMethod(method); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1298 | } |
| 1299 | } else { |
| 1300 | if (kVerboseInstrumentation) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1301 | LOG(INFO) << "Popping for unwind " << ArtMethod::PrettyMethod(method); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | // Notify listeners of method unwind. |
| 1305 | // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to |
| 1306 | // return_pc. |
| 1307 | uint32_t dex_pc = DexFile::kDexNoIndex; |
Nicolas Geoffray | 07c7028 | 2017-08-30 08:09:42 +0000 | [diff] [blame] | 1308 | MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1309 | } |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1310 | // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2); |
| 1311 | CHECK_EQ(stack->size(), idx); |
| 1312 | DCHECK(instrumentation_frame.method_ == stack->front().method_); |
| 1313 | stack->pop_front(); |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 1314 | return instrumentation_frame.return_pc_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | std::string InstrumentationStackFrame::Dump() const { |
| 1318 | std::ostringstream os; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1319 | os << "Frame " << frame_id_ << " " << ArtMethod::PrettyMethod(method_) << ":" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1320 | << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_); |
| 1321 | return os.str(); |
| 1322 | } |
| 1323 | |
| 1324 | } // namespace instrumentation |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 1325 | } // namespace art |