blob: 6711ac3eb1588f7c8af25000903c00a72ae826c2 [file] [log] [blame]
jeffhao725a9572012-11-13 18:20:12 -08001/*
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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_INSTRUMENTATION_H_
18#define ART_RUNTIME_INSTRUMENTATION_H_
jeffhao725a9572012-11-13 18:20:12 -080019
Ian Rogers576ca0c2014-06-06 15:58:22 -070020#include <stdint.h>
Ian Rogers576ca0c2014-06-06 15:58:22 -070021#include <list>
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include <unordered_set>
Ian Rogers576ca0c2014-06-06 15:58:22 -070023
Ian Rogersd582fa42014-11-05 23:46:43 -080024#include "arch/instruction_set.h"
Elliott Hughes76160052012-12-12 16:31:20 -080025#include "base/macros.h"
Ian Rogers719d1a32014-03-06 12:13:39 -080026#include "base/mutex.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070027#include "gc_root.h"
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020028#include "safe_map.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029
jeffhao725a9572012-11-13 18:20:12 -080030namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031namespace mirror {
Brian Carlstromea46f952013-07-30 01:26:50 -070032 class Class;
33 class Object;
34 class Throwable;
Ian Rogers62d6c772013-02-27 08:32:07 -080035} // namespace mirror
Mathieu Chartierc7853442015-03-27 14:35:38 -070036class ArtField;
Mathieu Chartiere401d142015-04-22 13:56:20 -070037class ArtMethod;
Ian Rogers62d6c772013-02-27 08:32:07 -080038union JValue;
jeffhao725a9572012-11-13 18:20:12 -080039class Thread;
40
Ian Rogers62d6c772013-02-27 08:32:07 -080041namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080042
Sebastien Hertzee1997a2013-09-19 14:47:09 +020043// Interpreter handler tables.
44enum InterpreterHandlerTable {
45 kMainHandlerTable = 0, // Main handler table: no suspend check, no instrumentation.
46 kAlternativeHandlerTable = 1, // Alternative handler table: suspend check and/or instrumentation
47 // enabled.
48 kNumHandlerTables
49};
50
Andreas Gampe40da2862015-02-27 12:49:04 -080051// Do we want to deoptimize for method entry and exit listeners or just try to intercept
52// invocations? Deoptimization forces all code to run in the interpreter and considerably hurts the
53// application's performance.
54static constexpr bool kDeoptimizeForAccurateMethodEntryExitListeners = true;
55
Ian Rogers62d6c772013-02-27 08:32:07 -080056// Instrumentation event listener API. Registered listeners will get the appropriate call back for
57// the events they are listening for. The call backs supply the thread, method and dex_pc the event
58// occurred upon. The thread may or may not be Thread::Current().
59struct InstrumentationListener {
60 InstrumentationListener() {}
61 virtual ~InstrumentationListener() {}
62
63 // Call-back for when a method is entered.
64 virtual void MethodEntered(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -070065 ArtMethod* method,
Mathieu Chartier90443472015-07-16 20:32:27 -070066 uint32_t dex_pc) SHARED_REQUIRES(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -080067
68 // Call-back for when a method is exited.
Ian Rogers62d6c772013-02-27 08:32:07 -080069 virtual void MethodExited(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -070070 ArtMethod* method, uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -080071 const JValue& return_value)
Mathieu Chartier90443472015-07-16 20:32:27 -070072 SHARED_REQUIRES(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -080073
74 // Call-back for when a method is popped due to an exception throw. A method will either cause a
75 // MethodExited call-back or a MethodUnwind call-back when its activation is removed.
Sebastien Hertz51db44a2013-11-19 10:00:29 +010076 virtual void MethodUnwind(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -070077 ArtMethod* method, uint32_t dex_pc)
Mathieu Chartier90443472015-07-16 20:32:27 -070078 SHARED_REQUIRES(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -080079
80 // Call-back for when the dex pc moves in a method.
81 virtual void DexPcMoved(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -070082 ArtMethod* method, uint32_t new_dex_pc)
Mathieu Chartier90443472015-07-16 20:32:27 -070083 SHARED_REQUIRES(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -080084
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020085 // Call-back for when we read from a field.
Mathieu Chartiere401d142015-04-22 13:56:20 -070086 virtual void FieldRead(Thread* thread, mirror::Object* this_object, ArtMethod* method,
Mathieu Chartierc7853442015-03-27 14:35:38 -070087 uint32_t dex_pc, ArtField* field) = 0;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020088
89 // Call-back for when we write into a field.
Mathieu Chartiere401d142015-04-22 13:56:20 -070090 virtual void FieldWritten(Thread* thread, mirror::Object* this_object, ArtMethod* method,
Mathieu Chartierc7853442015-03-27 14:35:38 -070091 uint32_t dex_pc, ArtField* field, const JValue& field_value) = 0;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020092
Ian Rogers62d6c772013-02-27 08:32:07 -080093 // Call-back when an exception is caught.
Nicolas Geoffray14691c52015-03-05 10:40:17 +000094 virtual void ExceptionCaught(Thread* thread, mirror::Throwable* exception_object)
Mathieu Chartier90443472015-07-16 20:32:27 -070095 SHARED_REQUIRES(Locks::mutator_lock_) = 0;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080096
97 // Call-back for when we get a backward branch.
Mathieu Chartiere401d142015-04-22 13:56:20 -070098 virtual void BackwardBranch(Thread* thread, ArtMethod* method, int32_t dex_pc_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -070099 SHARED_REQUIRES(Locks::mutator_lock_) = 0;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100100
101 // Call-back for when we get an invokevirtual or an invokeinterface.
102 virtual void InvokeVirtualOrInterface(Thread* thread,
103 mirror::Object* this_object,
104 ArtMethod* caller,
105 uint32_t dex_pc,
106 ArtMethod* callee)
107 SHARED_REQUIRES(Locks::mutator_lock_) = 0;
jeffhao725a9572012-11-13 18:20:12 -0800108};
109
Ian Rogers62d6c772013-02-27 08:32:07 -0800110// Instrumentation is a catch-all for when extra information is required from the runtime. The
111// typical use for instrumentation is for profiling and debugging. Instrumentation may add stubs
112// to method entry and exit, it may also force execution to be switched to the interpreter and
113// trigger deoptimization.
jeffhao725a9572012-11-13 18:20:12 -0800114class Instrumentation {
115 public:
Ian Rogers62d6c772013-02-27 08:32:07 -0800116 enum InstrumentationEvent {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800117 kMethodEntered = 0x1,
118 kMethodExited = 0x2,
119 kMethodUnwind = 0x4,
120 kDexPcMoved = 0x8,
121 kFieldRead = 0x10,
122 kFieldWritten = 0x20,
123 kExceptionCaught = 0x40,
124 kBackwardBranch = 0x80,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100125 kInvokeVirtualOrInterface = 0x100,
Ian Rogers62d6c772013-02-27 08:32:07 -0800126 };
jeffhao725a9572012-11-13 18:20:12 -0800127
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200128 enum class InstrumentationLevel {
129 kInstrumentNothing, // execute without instrumentation
130 kInstrumentWithInstrumentationStubs, // execute with instrumentation entry/exit stubs
131 kInstrumentWithInterpreter // execute with interpreter
132 };
133
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700134 Instrumentation();
jeffhao725a9572012-11-13 18:20:12 -0800135
Ian Rogers62d6c772013-02-27 08:32:07 -0800136 // Add a listener to be notified of the masked together sent of instrumentation events. This
137 // suspend the runtime to install stubs. You are expected to hold the mutator lock as a proxy
138 // for saying you should have suspended all threads (installing stubs while threads are running
139 // will break).
140 void AddListener(InstrumentationListener* listener, uint32_t events)
Mathieu Chartier90443472015-07-16 20:32:27 -0700141 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800142
Ian Rogers62d6c772013-02-27 08:32:07 -0800143 // Removes a listener possibly removing instrumentation stubs.
144 void RemoveListener(InstrumentationListener* listener, uint32_t events)
Mathieu Chartier90443472015-07-16 20:32:27 -0700145 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800146
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100147 // Deoptimization.
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100148 void EnableDeoptimization()
Mathieu Chartier90443472015-07-16 20:32:27 -0700149 REQUIRES(Locks::mutator_lock_, !deoptimized_methods_lock_);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200150 void DisableDeoptimization(const char* key)
Mathieu Chartier90443472015-07-16 20:32:27 -0700151 REQUIRES(Locks::mutator_lock_, !deoptimized_methods_lock_);
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100152 bool AreAllMethodsDeoptimized() const {
153 return interpreter_stubs_installed_;
154 }
Mathieu Chartier90443472015-07-16 20:32:27 -0700155 bool ShouldNotifyMethodEnterExitEvents() const SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100156
157 // Executes everything with interpreter.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200158 void DeoptimizeEverything(const char* key)
Mathieu Chartier90443472015-07-16 20:32:27 -0700159 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_,
160 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100161
162 // Executes everything with compiled code (or interpreter if there is no code).
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200163 void UndeoptimizeEverything(const char* key)
Mathieu Chartier90443472015-07-16 20:32:27 -0700164 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_,
165 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100166
167 // Deoptimize a method by forcing its execution with the interpreter. Nevertheless, a static
168 // method (except a class initializer) set to the resolution trampoline will be deoptimized only
169 // once its declaring class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700170 void Deoptimize(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700171 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100172
173 // Undeoptimze the method by restoring its entrypoints. Nevertheless, a static method
174 // (except a class initializer) set to the resolution trampoline will be updated only once its
175 // declaring class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700176 void Undeoptimize(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700177 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100178
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200179 // Indicates whether the method has been deoptimized so it is executed with the interpreter.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700180 bool IsDeoptimized(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700181 REQUIRES(!deoptimized_methods_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100182
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200183 // Enable method tracing by installing instrumentation entry/exit stubs or interpreter.
184 void EnableMethodTracing(const char* key,
185 bool needs_interpreter = kDeoptimizeForAccurateMethodEntryExitListeners)
Mathieu Chartier90443472015-07-16 20:32:27 -0700186 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_,
187 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100188
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200189 // Disable method tracing by uninstalling instrumentation entry/exit stubs or interpreter.
190 void DisableMethodTracing(const char* key)
Mathieu Chartier90443472015-07-16 20:32:27 -0700191 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_,
192 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100193
Sebastien Hertzed2be172014-08-19 15:33:43 +0200194 InterpreterHandlerTable GetInterpreterHandlerTable() const
Mathieu Chartier90443472015-07-16 20:32:27 -0700195 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200196 return interpreter_handler_table_;
197 }
198
Mathieu Chartier90443472015-07-16 20:32:27 -0700199 void InstrumentQuickAllocEntryPoints() REQUIRES(!Locks::instrument_entrypoints_lock_);
200 void UninstrumentQuickAllocEntryPoints() REQUIRES(!Locks::instrument_entrypoints_lock_);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700201 void InstrumentQuickAllocEntryPointsLocked()
Mathieu Chartier90443472015-07-16 20:32:27 -0700202 REQUIRES(Locks::instrument_entrypoints_lock_, !Locks::thread_list_lock_,
203 !Locks::runtime_shutdown_lock_);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700204 void UninstrumentQuickAllocEntryPointsLocked()
Mathieu Chartier90443472015-07-16 20:32:27 -0700205 REQUIRES(Locks::instrument_entrypoints_lock_, !Locks::thread_list_lock_,
206 !Locks::runtime_shutdown_lock_);
207 void ResetQuickAllocEntryPoints() REQUIRES(Locks::runtime_shutdown_lock_);
Ian Rogersfa824272013-11-05 16:12:57 -0800208
Ian Rogers62d6c772013-02-27 08:32:07 -0800209 // Update the code of a method respecting any installed stubs.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700210 void UpdateMethodsCode(ArtMethod* method, const void* quick_code)
Mathieu Chartier90443472015-07-16 20:32:27 -0700211 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800212
213 // Get the quick code for the given method. More efficient than asking the class linker as it
214 // will short-cut to GetCode if instrumentation and static method resolution stubs aren't
215 // installed.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700216 const void* GetQuickCodeFor(ArtMethod* method, size_t pointer_size) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700217 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800218
219 void ForceInterpretOnly() {
220 interpret_only_ = true;
221 forced_interpret_only_ = true;
222 }
223
Brian Carlstromea46f952013-07-30 01:26:50 -0700224 // Called by ArtMethod::Invoke to determine dispatch mechanism.
Ian Rogers62d6c772013-02-27 08:32:07 -0800225 bool InterpretOnly() const {
226 return interpret_only_;
227 }
228
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800229 bool IsForcedInterpretOnly() const {
230 return forced_interpret_only_;
231 }
232
Ian Rogers62d6c772013-02-27 08:32:07 -0800233 bool AreExitStubsInstalled() const {
234 return instrumentation_stubs_installed_;
235 }
236
Mathieu Chartier90443472015-07-16 20:32:27 -0700237 bool HasMethodEntryListeners() const SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200238 return have_method_entry_listeners_;
239 }
240
Mathieu Chartier90443472015-07-16 20:32:27 -0700241 bool HasMethodExitListeners() const SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200242 return have_method_exit_listeners_;
243 }
244
Mathieu Chartier90443472015-07-16 20:32:27 -0700245 bool HasMethodUnwindListeners() const SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200246 return have_method_unwind_listeners_;
247 }
248
Mathieu Chartier90443472015-07-16 20:32:27 -0700249 bool HasDexPcListeners() const SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200250 return have_dex_pc_listeners_;
251 }
252
Mathieu Chartier90443472015-07-16 20:32:27 -0700253 bool HasFieldReadListeners() const SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200254 return have_field_read_listeners_;
255 }
256
Mathieu Chartier90443472015-07-16 20:32:27 -0700257 bool HasFieldWriteListeners() const SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200258 return have_field_write_listeners_;
259 }
260
Mathieu Chartier90443472015-07-16 20:32:27 -0700261 bool HasExceptionCaughtListeners() const SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200262 return have_exception_caught_listeners_;
263 }
264
Mathieu Chartier90443472015-07-16 20:32:27 -0700265 bool HasBackwardBranchListeners() const SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800266 return have_backward_branch_listeners_;
267 }
268
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100269 bool HasInvokeVirtualOrInterfaceListeners() const SHARED_REQUIRES(Locks::mutator_lock_) {
270 return have_invoke_virtual_or_interface_listeners_;
271 }
272
Mathieu Chartier90443472015-07-16 20:32:27 -0700273 bool IsActive() const SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200274 return have_dex_pc_listeners_ || have_method_entry_listeners_ || have_method_exit_listeners_ ||
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200275 have_field_read_listeners_ || have_field_write_listeners_ ||
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200276 have_exception_caught_listeners_ || have_method_unwind_listeners_;
277 }
278
Ian Rogers62d6c772013-02-27 08:32:07 -0800279 // Inform listeners that a method has been entered. A dex PC is provided as we may install
280 // listeners into executing code and get method enter events for methods already on the stack.
281 void MethodEnterEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700282 ArtMethod* method, uint32_t dex_pc) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700283 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200284 if (UNLIKELY(HasMethodEntryListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800285 MethodEnterEventImpl(thread, this_object, method, dex_pc);
286 }
287 }
288
289 // Inform listeners that a method has been exited.
290 void MethodExitEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700291 ArtMethod* method, uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800292 const JValue& return_value) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700293 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200294 if (UNLIKELY(HasMethodExitListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800295 MethodExitEventImpl(thread, this_object, method, dex_pc, return_value);
296 }
297 }
298
299 // Inform listeners that a method has been exited due to an exception.
300 void MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700301 ArtMethod* method, uint32_t dex_pc) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700302 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800303
304 // Inform listeners that the dex pc has moved (only supported by the interpreter).
305 void DexPcMovedEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700306 ArtMethod* method, uint32_t dex_pc) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700307 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200308 if (UNLIKELY(HasDexPcListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800309 DexPcMovedEventImpl(thread, this_object, method, dex_pc);
310 }
311 }
312
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800313 // Inform listeners that a backward branch has been taken (only supported by the interpreter).
Mathieu Chartiere401d142015-04-22 13:56:20 -0700314 void BackwardBranch(Thread* thread, ArtMethod* method, int32_t offset) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700315 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800316 if (UNLIKELY(HasBackwardBranchListeners())) {
317 BackwardBranchImpl(thread, method, offset);
318 }
319 }
320
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200321 // Inform listeners that we read a field (only supported by the interpreter).
322 void FieldReadEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700323 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700324 ArtField* field) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700325 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200326 if (UNLIKELY(HasFieldReadListeners())) {
327 FieldReadEventImpl(thread, this_object, method, dex_pc, field);
328 }
329 }
330
331 // Inform listeners that we write a field (only supported by the interpreter).
332 void FieldWriteEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700333 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700334 ArtField* field, const JValue& field_value) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700335 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200336 if (UNLIKELY(HasFieldWriteListeners())) {
337 FieldWriteEventImpl(thread, this_object, method, dex_pc, field, field_value);
338 }
339 }
340
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100341 void InvokeVirtualOrInterface(Thread* thread,
342 mirror::Object* this_object,
343 ArtMethod* caller,
344 uint32_t dex_pc,
345 ArtMethod* callee) const
346 SHARED_REQUIRES(Locks::mutator_lock_) {
347 if (UNLIKELY(HasInvokeVirtualOrInterfaceListeners())) {
348 InvokeVirtualOrInterfaceImpl(thread, this_object, caller, dex_pc, callee);
349 }
350 }
351
Ian Rogers62d6c772013-02-27 08:32:07 -0800352 // Inform listeners that an exception was caught.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000353 void ExceptionCaughtEvent(Thread* thread, mirror::Throwable* exception_object) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700354 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800355
356 // Called when an instrumented method is entered. The intended link register (lr) is saved so
357 // that returning causes a branch to the method exit stub. Generates method enter events.
358 void PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700359 ArtMethod* method, uintptr_t lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700360 bool interpreter_entry)
Mathieu Chartier90443472015-07-16 20:32:27 -0700361 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800362
363 // Called when an instrumented method is exited. Removes the pushed instrumentation frame
364 // returning the intended link register. Generates method exit events.
Andreas Gamped58342c2014-06-05 14:18:08 -0700365 TwoWordReturn PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
366 uint64_t gpr_result, uint64_t fpr_result)
Mathieu Chartier90443472015-07-16 20:32:27 -0700367 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800368
369 // Pops an instrumentation frame from the current thread and generate an unwind event.
370 void PopMethodForUnwind(Thread* self, bool is_deoptimization) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700371 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800372
373 // Call back for configure stubs.
Mathieu Chartier90443472015-07-16 20:32:27 -0700374 void InstallStubsForClass(mirror::Class* klass) SHARED_REQUIRES(Locks::mutator_lock_)
375 REQUIRES(!deoptimized_methods_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800376
Mathieu Chartiere401d142015-04-22 13:56:20 -0700377 void InstallStubsForMethod(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700378 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100379
jeffhao725a9572012-11-13 18:20:12 -0800380 private:
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200381 InstrumentationLevel GetCurrentInstrumentationLevel() const;
382
Ian Rogers62d6c772013-02-27 08:32:07 -0800383 // Does the job of installing or removing instrumentation code within methods.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200384 // In order to support multiple clients using instrumentation at the same time,
385 // the caller must pass a unique key (a string) identifying it so we remind which
386 // instrumentation level it needs. Therefore the current instrumentation level
387 // becomes the highest instrumentation level required by a client.
388 void ConfigureStubs(const char* key, InstrumentationLevel desired_instrumentation_level)
Mathieu Chartier90443472015-07-16 20:32:27 -0700389 REQUIRES(Locks::mutator_lock_, !deoptimized_methods_lock_, !Locks::thread_list_lock_,
390 !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800391
Mathieu Chartier90443472015-07-16 20:32:27 -0700392 void UpdateInterpreterHandlerTable() REQUIRES(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200393 interpreter_handler_table_ = IsActive() ? kAlternativeHandlerTable : kMainHandlerTable;
394 }
395
Mathieu Chartier661974a2014-01-09 11:23:53 -0800396 // No thread safety analysis to get around SetQuickAllocEntryPointsInstrumented requiring
397 // exclusive access to mutator lock which you can't get if the runtime isn't started.
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700398 void SetEntrypointsInstrumented(bool instrumented) NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800399
Ian Rogers62d6c772013-02-27 08:32:07 -0800400 void MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700401 ArtMethod* method, uint32_t dex_pc) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700402 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800403 void MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700404 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800405 uint32_t dex_pc, const JValue& return_value) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700406 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800407 void DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700408 ArtMethod* method, uint32_t dex_pc) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700409 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700410 void BackwardBranchImpl(Thread* thread, ArtMethod* method, int32_t offset) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700411 SHARED_REQUIRES(Locks::mutator_lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100412 void InvokeVirtualOrInterfaceImpl(Thread* thread,
413 mirror::Object* this_object,
414 ArtMethod* caller,
415 uint32_t dex_pc,
416 ArtMethod* callee) const
417 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200418 void FieldReadEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700419 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700420 ArtField* field) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700421 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200422 void FieldWriteEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700423 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700424 ArtField* field, const JValue& field_value) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700425 SHARED_REQUIRES(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800426
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700427 // Read barrier-aware utility functions for accessing deoptimized_methods_
Mathieu Chartiere401d142015-04-22 13:56:20 -0700428 bool AddDeoptimizedMethod(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700429 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700430 bool IsDeoptimizedMethod(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700431 SHARED_REQUIRES(Locks::mutator_lock_, deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700432 bool RemoveDeoptimizedMethod(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700433 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700434 ArtMethod* BeginDeoptimizedMethod()
Mathieu Chartier90443472015-07-16 20:32:27 -0700435 SHARED_REQUIRES(Locks::mutator_lock_, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700436 bool IsDeoptimizedMethodsEmpty() const
Mathieu Chartier90443472015-07-16 20:32:27 -0700437 SHARED_REQUIRES(Locks::mutator_lock_, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700438
Brian Carlstromea46f952013-07-30 01:26:50 -0700439 // Have we hijacked ArtMethod::code_ so that it calls instrumentation/interpreter code?
Ian Rogers62d6c772013-02-27 08:32:07 -0800440 bool instrumentation_stubs_installed_;
441
Brian Carlstromea46f952013-07-30 01:26:50 -0700442 // Have we hijacked ArtMethod::code_ to reference the enter/exit stubs?
Ian Rogers62d6c772013-02-27 08:32:07 -0800443 bool entry_exit_stubs_installed_;
444
Brian Carlstromea46f952013-07-30 01:26:50 -0700445 // Have we hijacked ArtMethod::code_ to reference the enter interpreter stub?
Ian Rogers62d6c772013-02-27 08:32:07 -0800446 bool interpreter_stubs_installed_;
447
448 // Do we need the fidelity of events that we only get from running within the interpreter?
449 bool interpret_only_;
450
451 // Did the runtime request we only run in the interpreter? ie -Xint mode.
452 bool forced_interpret_only_;
453
454 // Do we have any listeners for method entry events? Short-cut to avoid taking the
455 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200456 bool have_method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800457
458 // Do we have any listeners for method exit events? Short-cut to avoid taking the
459 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200460 bool have_method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800461
462 // Do we have any listeners for method unwind events? Short-cut to avoid taking the
463 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200464 bool have_method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800465
466 // Do we have any listeners for dex move events? Short-cut to avoid taking the
467 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200468 bool have_dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800469
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200470 // Do we have any listeners for field read events? Short-cut to avoid taking the
471 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200472 bool have_field_read_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200473
474 // Do we have any listeners for field write events? Short-cut to avoid taking the
475 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200476 bool have_field_write_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200477
Ian Rogers62d6c772013-02-27 08:32:07 -0800478 // Do we have any exception caught listeners? Short-cut to avoid taking the instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200479 bool have_exception_caught_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800480
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800481 // Do we have any backward branch listeners? Short-cut to avoid taking the instrumentation_lock_.
482 bool have_backward_branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
483
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100484 // Do we have any invoke listeners? Short-cut to avoid taking the instrumentation_lock_.
485 bool have_invoke_virtual_or_interface_listeners_ GUARDED_BY(Locks::mutator_lock_);
486
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200487 // Contains the instrumentation level required by each client of the instrumentation identified
488 // by a string key.
489 typedef SafeMap<const char*, InstrumentationLevel> InstrumentationLevelTable;
490 InstrumentationLevelTable requested_instrumentation_levels_ GUARDED_BY(Locks::mutator_lock_);
491
Ian Rogers62d6c772013-02-27 08:32:07 -0800492 // The event listeners, written to with the mutator_lock_ exclusively held.
493 std::list<InstrumentationListener*> method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
494 std::list<InstrumentationListener*> method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
495 std::list<InstrumentationListener*> method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800496 std::list<InstrumentationListener*> backward_branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100497 std::list<InstrumentationListener*> invoke_virtual_or_interface_listeners_
498 GUARDED_BY(Locks::mutator_lock_);
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200499 std::shared_ptr<std::list<InstrumentationListener*>> dex_pc_listeners_
500 GUARDED_BY(Locks::mutator_lock_);
501 std::shared_ptr<std::list<InstrumentationListener*>> field_read_listeners_
502 GUARDED_BY(Locks::mutator_lock_);
503 std::shared_ptr<std::list<InstrumentationListener*>> field_write_listeners_
504 GUARDED_BY(Locks::mutator_lock_);
505 std::shared_ptr<std::list<InstrumentationListener*>> exception_caught_listeners_
506 GUARDED_BY(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800507
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100508 // The set of methods being deoptimized (by the debugger) which must be executed with interpreter
509 // only.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700510 mutable ReaderWriterMutex deoptimized_methods_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700511 std::unordered_set<ArtMethod*> deoptimized_methods_ GUARDED_BY(deoptimized_methods_lock_);
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100512 bool deoptimization_enabled_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100513
Ian Rogersfa824272013-11-05 16:12:57 -0800514 // Current interpreter handler table. This is updated each time the thread state flags are
515 // modified.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200516 InterpreterHandlerTable interpreter_handler_table_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200517
Ian Rogersfa824272013-11-05 16:12:57 -0800518 // Greater than 0 if quick alloc entry points instrumented.
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700519 size_t quick_alloc_entry_points_instrumentation_counter_
520 GUARDED_BY(Locks::instrument_entrypoints_lock_);
Ian Rogersfa824272013-11-05 16:12:57 -0800521
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200522 friend class InstrumentationTest; // For GetCurrentInstrumentationLevel and ConfigureStubs.
523
jeffhao725a9572012-11-13 18:20:12 -0800524 DISALLOW_COPY_AND_ASSIGN(Instrumentation);
525};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700526std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationEvent& rhs);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200527std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationLevel& rhs);
jeffhao725a9572012-11-13 18:20:12 -0800528
Ian Rogers62d6c772013-02-27 08:32:07 -0800529// An element in the instrumentation side stack maintained in art::Thread.
530struct InstrumentationStackFrame {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700531 InstrumentationStackFrame(mirror::Object* this_object, ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -0700532 uintptr_t return_pc, size_t frame_id, bool interpreter_entry)
533 : this_object_(this_object), method_(method), return_pc_(return_pc), frame_id_(frame_id),
534 interpreter_entry_(interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800535 }
536
Mathieu Chartier90443472015-07-16 20:32:27 -0700537 std::string Dump() const SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800538
539 mirror::Object* this_object_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700540 ArtMethod* method_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100541 uintptr_t return_pc_;
542 size_t frame_id_;
543 bool interpreter_entry_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800544};
545
546} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -0800547} // namespace art
548
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700549#endif // ART_RUNTIME_INSTRUMENTATION_H_