blob: 93ff567dc35f3140c381b3ef4f04c9ace22ad01f [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;
jeffhao725a9572012-11-13 18:20:12 -0800100};
101
Ian Rogers62d6c772013-02-27 08:32:07 -0800102// Instrumentation is a catch-all for when extra information is required from the runtime. The
103// typical use for instrumentation is for profiling and debugging. Instrumentation may add stubs
104// to method entry and exit, it may also force execution to be switched to the interpreter and
105// trigger deoptimization.
jeffhao725a9572012-11-13 18:20:12 -0800106class Instrumentation {
107 public:
Ian Rogers62d6c772013-02-27 08:32:07 -0800108 enum InstrumentationEvent {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800109 kMethodEntered = 0x1,
110 kMethodExited = 0x2,
111 kMethodUnwind = 0x4,
112 kDexPcMoved = 0x8,
113 kFieldRead = 0x10,
114 kFieldWritten = 0x20,
115 kExceptionCaught = 0x40,
116 kBackwardBranch = 0x80,
Ian Rogers62d6c772013-02-27 08:32:07 -0800117 };
jeffhao725a9572012-11-13 18:20:12 -0800118
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200119 enum class InstrumentationLevel {
120 kInstrumentNothing, // execute without instrumentation
121 kInstrumentWithInstrumentationStubs, // execute with instrumentation entry/exit stubs
122 kInstrumentWithInterpreter // execute with interpreter
123 };
124
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700125 Instrumentation();
jeffhao725a9572012-11-13 18:20:12 -0800126
Ian Rogers62d6c772013-02-27 08:32:07 -0800127 // Add a listener to be notified of the masked together sent of instrumentation events. This
128 // suspend the runtime to install stubs. You are expected to hold the mutator lock as a proxy
129 // for saying you should have suspended all threads (installing stubs while threads are running
130 // will break).
131 void AddListener(InstrumentationListener* listener, uint32_t events)
Mathieu Chartier90443472015-07-16 20:32:27 -0700132 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800133
Ian Rogers62d6c772013-02-27 08:32:07 -0800134 // Removes a listener possibly removing instrumentation stubs.
135 void RemoveListener(InstrumentationListener* listener, uint32_t events)
Mathieu Chartier90443472015-07-16 20:32:27 -0700136 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800137
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100138 // Deoptimization.
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100139 void EnableDeoptimization()
Mathieu Chartier90443472015-07-16 20:32:27 -0700140 REQUIRES(Locks::mutator_lock_, !deoptimized_methods_lock_);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200141 void DisableDeoptimization(const char* key)
Mathieu Chartier90443472015-07-16 20:32:27 -0700142 REQUIRES(Locks::mutator_lock_, !deoptimized_methods_lock_);
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100143 bool AreAllMethodsDeoptimized() const {
144 return interpreter_stubs_installed_;
145 }
Mathieu Chartier90443472015-07-16 20:32:27 -0700146 bool ShouldNotifyMethodEnterExitEvents() const SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100147
148 // Executes everything with interpreter.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200149 void DeoptimizeEverything(const char* key)
Mathieu Chartier90443472015-07-16 20:32:27 -0700150 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_,
151 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100152
153 // Executes everything with compiled code (or interpreter if there is no code).
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200154 void UndeoptimizeEverything(const char* key)
Mathieu Chartier90443472015-07-16 20:32:27 -0700155 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_,
156 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100157
158 // Deoptimize a method by forcing its execution with the interpreter. Nevertheless, a static
159 // method (except a class initializer) set to the resolution trampoline will be deoptimized only
160 // once its declaring class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700161 void Deoptimize(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700162 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100163
164 // Undeoptimze the method by restoring its entrypoints. Nevertheless, a static method
165 // (except a class initializer) set to the resolution trampoline will be updated only once its
166 // declaring class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700167 void Undeoptimize(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700168 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100169
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200170 // Indicates whether the method has been deoptimized so it is executed with the interpreter.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700171 bool IsDeoptimized(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700172 REQUIRES(!deoptimized_methods_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100173
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200174 // Enable method tracing by installing instrumentation entry/exit stubs or interpreter.
175 void EnableMethodTracing(const char* key,
176 bool needs_interpreter = kDeoptimizeForAccurateMethodEntryExitListeners)
Mathieu Chartier90443472015-07-16 20:32:27 -0700177 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_,
178 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100179
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200180 // Disable method tracing by uninstalling instrumentation entry/exit stubs or interpreter.
181 void DisableMethodTracing(const char* key)
Mathieu Chartier90443472015-07-16 20:32:27 -0700182 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_,
183 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100184
Sebastien Hertzed2be172014-08-19 15:33:43 +0200185 InterpreterHandlerTable GetInterpreterHandlerTable() const
Mathieu Chartier90443472015-07-16 20:32:27 -0700186 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200187 return interpreter_handler_table_;
188 }
189
Mathieu Chartier90443472015-07-16 20:32:27 -0700190 void InstrumentQuickAllocEntryPoints() REQUIRES(!Locks::instrument_entrypoints_lock_);
191 void UninstrumentQuickAllocEntryPoints() REQUIRES(!Locks::instrument_entrypoints_lock_);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700192 void InstrumentQuickAllocEntryPointsLocked()
Mathieu Chartier90443472015-07-16 20:32:27 -0700193 REQUIRES(Locks::instrument_entrypoints_lock_, !Locks::thread_list_lock_,
194 !Locks::runtime_shutdown_lock_);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700195 void UninstrumentQuickAllocEntryPointsLocked()
Mathieu Chartier90443472015-07-16 20:32:27 -0700196 REQUIRES(Locks::instrument_entrypoints_lock_, !Locks::thread_list_lock_,
197 !Locks::runtime_shutdown_lock_);
198 void ResetQuickAllocEntryPoints() REQUIRES(Locks::runtime_shutdown_lock_);
Ian Rogersfa824272013-11-05 16:12:57 -0800199
Ian Rogers62d6c772013-02-27 08:32:07 -0800200 // Update the code of a method respecting any installed stubs.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700201 void UpdateMethodsCode(ArtMethod* method, const void* quick_code)
Mathieu Chartier90443472015-07-16 20:32:27 -0700202 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800203
204 // Get the quick code for the given method. More efficient than asking the class linker as it
205 // will short-cut to GetCode if instrumentation and static method resolution stubs aren't
206 // installed.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700207 const void* GetQuickCodeFor(ArtMethod* method, size_t pointer_size) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700208 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800209
210 void ForceInterpretOnly() {
211 interpret_only_ = true;
212 forced_interpret_only_ = true;
213 }
214
Brian Carlstromea46f952013-07-30 01:26:50 -0700215 // Called by ArtMethod::Invoke to determine dispatch mechanism.
Ian Rogers62d6c772013-02-27 08:32:07 -0800216 bool InterpretOnly() const {
217 return interpret_only_;
218 }
219
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800220 bool IsForcedInterpretOnly() const {
221 return forced_interpret_only_;
222 }
223
Ian Rogers62d6c772013-02-27 08:32:07 -0800224 bool AreExitStubsInstalled() const {
225 return instrumentation_stubs_installed_;
226 }
227
Mathieu Chartier90443472015-07-16 20:32:27 -0700228 bool HasMethodEntryListeners() const SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200229 return have_method_entry_listeners_;
230 }
231
Mathieu Chartier90443472015-07-16 20:32:27 -0700232 bool HasMethodExitListeners() const SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200233 return have_method_exit_listeners_;
234 }
235
Mathieu Chartier90443472015-07-16 20:32:27 -0700236 bool HasMethodUnwindListeners() const SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200237 return have_method_unwind_listeners_;
238 }
239
Mathieu Chartier90443472015-07-16 20:32:27 -0700240 bool HasDexPcListeners() const SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200241 return have_dex_pc_listeners_;
242 }
243
Mathieu Chartier90443472015-07-16 20:32:27 -0700244 bool HasFieldReadListeners() const SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200245 return have_field_read_listeners_;
246 }
247
Mathieu Chartier90443472015-07-16 20:32:27 -0700248 bool HasFieldWriteListeners() const SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200249 return have_field_write_listeners_;
250 }
251
Mathieu Chartier90443472015-07-16 20:32:27 -0700252 bool HasExceptionCaughtListeners() const SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200253 return have_exception_caught_listeners_;
254 }
255
Mathieu Chartier90443472015-07-16 20:32:27 -0700256 bool HasBackwardBranchListeners() const SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800257 return have_backward_branch_listeners_;
258 }
259
Mathieu Chartier90443472015-07-16 20:32:27 -0700260 bool IsActive() const SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200261 return have_dex_pc_listeners_ || have_method_entry_listeners_ || have_method_exit_listeners_ ||
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200262 have_field_read_listeners_ || have_field_write_listeners_ ||
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200263 have_exception_caught_listeners_ || have_method_unwind_listeners_;
264 }
265
Ian Rogers62d6c772013-02-27 08:32:07 -0800266 // Inform listeners that a method has been entered. A dex PC is provided as we may install
267 // listeners into executing code and get method enter events for methods already on the stack.
268 void MethodEnterEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700269 ArtMethod* method, uint32_t dex_pc) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700270 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200271 if (UNLIKELY(HasMethodEntryListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800272 MethodEnterEventImpl(thread, this_object, method, dex_pc);
273 }
274 }
275
276 // Inform listeners that a method has been exited.
277 void MethodExitEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700278 ArtMethod* method, uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800279 const JValue& return_value) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700280 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200281 if (UNLIKELY(HasMethodExitListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800282 MethodExitEventImpl(thread, this_object, method, dex_pc, return_value);
283 }
284 }
285
286 // Inform listeners that a method has been exited due to an exception.
287 void MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700288 ArtMethod* method, uint32_t dex_pc) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700289 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800290
291 // Inform listeners that the dex pc has moved (only supported by the interpreter).
292 void DexPcMovedEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700293 ArtMethod* method, uint32_t dex_pc) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700294 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200295 if (UNLIKELY(HasDexPcListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800296 DexPcMovedEventImpl(thread, this_object, method, dex_pc);
297 }
298 }
299
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800300 // Inform listeners that a backward branch has been taken (only supported by the interpreter).
Mathieu Chartiere401d142015-04-22 13:56:20 -0700301 void BackwardBranch(Thread* thread, ArtMethod* method, int32_t offset) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700302 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800303 if (UNLIKELY(HasBackwardBranchListeners())) {
304 BackwardBranchImpl(thread, method, offset);
305 }
306 }
307
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200308 // Inform listeners that we read a field (only supported by the interpreter).
309 void FieldReadEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700310 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700311 ArtField* field) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700312 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200313 if (UNLIKELY(HasFieldReadListeners())) {
314 FieldReadEventImpl(thread, this_object, method, dex_pc, field);
315 }
316 }
317
318 // Inform listeners that we write a field (only supported by the interpreter).
319 void FieldWriteEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700320 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700321 ArtField* field, const JValue& field_value) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700322 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200323 if (UNLIKELY(HasFieldWriteListeners())) {
324 FieldWriteEventImpl(thread, this_object, method, dex_pc, field, field_value);
325 }
326 }
327
Ian Rogers62d6c772013-02-27 08:32:07 -0800328 // Inform listeners that an exception was caught.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000329 void ExceptionCaughtEvent(Thread* thread, mirror::Throwable* exception_object) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700330 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800331
332 // Called when an instrumented method is entered. The intended link register (lr) is saved so
333 // that returning causes a branch to the method exit stub. Generates method enter events.
334 void PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700335 ArtMethod* method, uintptr_t lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700336 bool interpreter_entry)
Mathieu Chartier90443472015-07-16 20:32:27 -0700337 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800338
339 // Called when an instrumented method is exited. Removes the pushed instrumentation frame
340 // returning the intended link register. Generates method exit events.
Andreas Gamped58342c2014-06-05 14:18:08 -0700341 TwoWordReturn PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
342 uint64_t gpr_result, uint64_t fpr_result)
Mathieu Chartier90443472015-07-16 20:32:27 -0700343 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800344
345 // Pops an instrumentation frame from the current thread and generate an unwind event.
346 void PopMethodForUnwind(Thread* self, bool is_deoptimization) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700347 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800348
349 // Call back for configure stubs.
Mathieu Chartier90443472015-07-16 20:32:27 -0700350 void InstallStubsForClass(mirror::Class* klass) SHARED_REQUIRES(Locks::mutator_lock_)
351 REQUIRES(!deoptimized_methods_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800352
Mathieu Chartiere401d142015-04-22 13:56:20 -0700353 void InstallStubsForMethod(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700354 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100355
jeffhao725a9572012-11-13 18:20:12 -0800356 private:
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200357 InstrumentationLevel GetCurrentInstrumentationLevel() const;
358
Ian Rogers62d6c772013-02-27 08:32:07 -0800359 // Does the job of installing or removing instrumentation code within methods.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200360 // In order to support multiple clients using instrumentation at the same time,
361 // the caller must pass a unique key (a string) identifying it so we remind which
362 // instrumentation level it needs. Therefore the current instrumentation level
363 // becomes the highest instrumentation level required by a client.
364 void ConfigureStubs(const char* key, InstrumentationLevel desired_instrumentation_level)
Mathieu Chartier90443472015-07-16 20:32:27 -0700365 REQUIRES(Locks::mutator_lock_, !deoptimized_methods_lock_, !Locks::thread_list_lock_,
366 !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800367
Mathieu Chartier90443472015-07-16 20:32:27 -0700368 void UpdateInterpreterHandlerTable() REQUIRES(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200369 interpreter_handler_table_ = IsActive() ? kAlternativeHandlerTable : kMainHandlerTable;
370 }
371
Mathieu Chartier661974a2014-01-09 11:23:53 -0800372 // No thread safety analysis to get around SetQuickAllocEntryPointsInstrumented requiring
373 // exclusive access to mutator lock which you can't get if the runtime isn't started.
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700374 void SetEntrypointsInstrumented(bool instrumented) NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800375
Ian Rogers62d6c772013-02-27 08:32:07 -0800376 void MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700377 ArtMethod* method, uint32_t dex_pc) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700378 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800379 void MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700380 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800381 uint32_t dex_pc, const JValue& return_value) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700382 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800383 void DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700384 ArtMethod* method, uint32_t dex_pc) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700385 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700386 void BackwardBranchImpl(Thread* thread, ArtMethod* method, int32_t offset) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700387 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200388 void FieldReadEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700389 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700390 ArtField* field) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700391 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200392 void FieldWriteEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700393 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700394 ArtField* field, const JValue& field_value) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700395 SHARED_REQUIRES(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800396
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700397 // Read barrier-aware utility functions for accessing deoptimized_methods_
Mathieu Chartiere401d142015-04-22 13:56:20 -0700398 bool AddDeoptimizedMethod(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700399 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700400 bool IsDeoptimizedMethod(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700401 SHARED_REQUIRES(Locks::mutator_lock_, deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700402 bool RemoveDeoptimizedMethod(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700403 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700404 ArtMethod* BeginDeoptimizedMethod()
Mathieu Chartier90443472015-07-16 20:32:27 -0700405 SHARED_REQUIRES(Locks::mutator_lock_, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700406 bool IsDeoptimizedMethodsEmpty() const
Mathieu Chartier90443472015-07-16 20:32:27 -0700407 SHARED_REQUIRES(Locks::mutator_lock_, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700408
Brian Carlstromea46f952013-07-30 01:26:50 -0700409 // Have we hijacked ArtMethod::code_ so that it calls instrumentation/interpreter code?
Ian Rogers62d6c772013-02-27 08:32:07 -0800410 bool instrumentation_stubs_installed_;
411
Brian Carlstromea46f952013-07-30 01:26:50 -0700412 // Have we hijacked ArtMethod::code_ to reference the enter/exit stubs?
Ian Rogers62d6c772013-02-27 08:32:07 -0800413 bool entry_exit_stubs_installed_;
414
Brian Carlstromea46f952013-07-30 01:26:50 -0700415 // Have we hijacked ArtMethod::code_ to reference the enter interpreter stub?
Ian Rogers62d6c772013-02-27 08:32:07 -0800416 bool interpreter_stubs_installed_;
417
418 // Do we need the fidelity of events that we only get from running within the interpreter?
419 bool interpret_only_;
420
421 // Did the runtime request we only run in the interpreter? ie -Xint mode.
422 bool forced_interpret_only_;
423
424 // Do we have any listeners for method entry events? Short-cut to avoid taking the
425 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200426 bool have_method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800427
428 // Do we have any listeners for method exit events? Short-cut to avoid taking the
429 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200430 bool have_method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800431
432 // Do we have any listeners for method unwind events? Short-cut to avoid taking the
433 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200434 bool have_method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800435
436 // Do we have any listeners for dex move events? Short-cut to avoid taking the
437 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200438 bool have_dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800439
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200440 // Do we have any listeners for field read events? Short-cut to avoid taking the
441 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200442 bool have_field_read_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200443
444 // Do we have any listeners for field write events? Short-cut to avoid taking the
445 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200446 bool have_field_write_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200447
Ian Rogers62d6c772013-02-27 08:32:07 -0800448 // Do we have any exception caught listeners? Short-cut to avoid taking the instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200449 bool have_exception_caught_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800450
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800451 // Do we have any backward branch listeners? Short-cut to avoid taking the instrumentation_lock_.
452 bool have_backward_branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
453
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200454 // Contains the instrumentation level required by each client of the instrumentation identified
455 // by a string key.
456 typedef SafeMap<const char*, InstrumentationLevel> InstrumentationLevelTable;
457 InstrumentationLevelTable requested_instrumentation_levels_ GUARDED_BY(Locks::mutator_lock_);
458
Ian Rogers62d6c772013-02-27 08:32:07 -0800459 // The event listeners, written to with the mutator_lock_ exclusively held.
460 std::list<InstrumentationListener*> method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
461 std::list<InstrumentationListener*> method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
462 std::list<InstrumentationListener*> method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800463 std::list<InstrumentationListener*> backward_branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200464 std::shared_ptr<std::list<InstrumentationListener*>> dex_pc_listeners_
465 GUARDED_BY(Locks::mutator_lock_);
466 std::shared_ptr<std::list<InstrumentationListener*>> field_read_listeners_
467 GUARDED_BY(Locks::mutator_lock_);
468 std::shared_ptr<std::list<InstrumentationListener*>> field_write_listeners_
469 GUARDED_BY(Locks::mutator_lock_);
470 std::shared_ptr<std::list<InstrumentationListener*>> exception_caught_listeners_
471 GUARDED_BY(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800472
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100473 // The set of methods being deoptimized (by the debugger) which must be executed with interpreter
474 // only.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700475 mutable ReaderWriterMutex deoptimized_methods_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700476 std::unordered_set<ArtMethod*> deoptimized_methods_ GUARDED_BY(deoptimized_methods_lock_);
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100477 bool deoptimization_enabled_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100478
Ian Rogersfa824272013-11-05 16:12:57 -0800479 // Current interpreter handler table. This is updated each time the thread state flags are
480 // modified.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200481 InterpreterHandlerTable interpreter_handler_table_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200482
Ian Rogersfa824272013-11-05 16:12:57 -0800483 // Greater than 0 if quick alloc entry points instrumented.
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700484 size_t quick_alloc_entry_points_instrumentation_counter_
485 GUARDED_BY(Locks::instrument_entrypoints_lock_);
Ian Rogersfa824272013-11-05 16:12:57 -0800486
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200487 friend class InstrumentationTest; // For GetCurrentInstrumentationLevel and ConfigureStubs.
488
jeffhao725a9572012-11-13 18:20:12 -0800489 DISALLOW_COPY_AND_ASSIGN(Instrumentation);
490};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700491std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationEvent& rhs);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200492std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationLevel& rhs);
jeffhao725a9572012-11-13 18:20:12 -0800493
Ian Rogers62d6c772013-02-27 08:32:07 -0800494// An element in the instrumentation side stack maintained in art::Thread.
495struct InstrumentationStackFrame {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700496 InstrumentationStackFrame(mirror::Object* this_object, ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -0700497 uintptr_t return_pc, size_t frame_id, bool interpreter_entry)
498 : this_object_(this_object), method_(method), return_pc_(return_pc), frame_id_(frame_id),
499 interpreter_entry_(interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800500 }
501
Mathieu Chartier90443472015-07-16 20:32:27 -0700502 std::string Dump() const SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800503
504 mirror::Object* this_object_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700505 ArtMethod* method_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100506 uintptr_t return_pc_;
507 size_t frame_id_;
508 bool interpreter_entry_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800509};
510
511} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -0800512} // namespace art
513
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700514#endif // ART_RUNTIME_INSTRUMENTATION_H_