blob: 7d70d211bb9f5860120e033a8fcdbb0b4730dcf0 [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>
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -070022#include <map>
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 ArtMethod;
33 class Class;
34 class Object;
35 class Throwable;
Ian Rogers62d6c772013-02-27 08:32:07 -080036} // namespace mirror
Mathieu Chartierc7853442015-03-27 14:35:38 -070037class ArtField;
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,
Ian Rogersef7d42f2014-01-06 12:55:46 -080065 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -080066 uint32_t dex_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
67
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,
Ian Rogersef7d42f2014-01-06 12:55:46 -080070 mirror::ArtMethod* method, uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -080071 const JValue& return_value)
72 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
73
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,
Ian Rogersef7d42f2014-01-06 12:55:46 -080077 mirror::ArtMethod* method, uint32_t dex_pc)
Sebastien Hertz51db44a2013-11-19 10:00:29 +010078 SHARED_LOCKS_REQUIRED(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,
Ian Rogersef7d42f2014-01-06 12:55:46 -080082 mirror::ArtMethod* method, uint32_t new_dex_pc)
Ian Rogers62d6c772013-02-27 08:32:07 -080083 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
84
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020085 // Call-back for when we read from a field.
86 virtual void FieldRead(Thread* thread, mirror::Object* this_object, mirror::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.
90 virtual void FieldWritten(Thread* thread, mirror::Object* this_object, mirror::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)
Ian Rogers62d6c772013-02-27 08:32:07 -080095 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080096
97 // Call-back for when we get a backward branch.
98 virtual void BackwardBranch(Thread* thread, mirror::ArtMethod* method, int32_t dex_pc_offset)
99 SHARED_LOCKS_REQUIRED(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)
132 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
133 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800134
Ian Rogers62d6c772013-02-27 08:32:07 -0800135 // Removes a listener possibly removing instrumentation stubs.
136 void RemoveListener(InstrumentationListener* listener, uint32_t events)
137 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
138 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800139
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100140 // Deoptimization.
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100141 void EnableDeoptimization()
142 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700143 LOCKS_EXCLUDED(deoptimized_methods_lock_);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200144 void DisableDeoptimization(const char* key)
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100145 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100146 LOCKS_EXCLUDED(deoptimized_methods_lock_);
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100147 bool AreAllMethodsDeoptimized() const {
148 return interpreter_stubs_installed_;
149 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100150 bool ShouldNotifyMethodEnterExitEvents() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100151
152 // Executes everything with interpreter.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200153 void DeoptimizeEverything(const char* key)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100154 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
155 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
156
157 // Executes everything with compiled code (or interpreter if there is no code).
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200158 void UndeoptimizeEverything(const char* key)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100159 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
160 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
161
162 // Deoptimize a method by forcing its execution with the interpreter. Nevertheless, a static
163 // method (except a class initializer) set to the resolution trampoline will be deoptimized only
164 // once its declaring class is initialized.
165 void Deoptimize(mirror::ArtMethod* method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700166 LOCKS_EXCLUDED(Locks::thread_list_lock_, deoptimized_methods_lock_)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100167 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
168
169 // Undeoptimze the method by restoring its entrypoints. Nevertheless, a static method
170 // (except a class initializer) set to the resolution trampoline will be updated only once its
171 // declaring class is initialized.
172 void Undeoptimize(mirror::ArtMethod* method)
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100173 LOCKS_EXCLUDED(Locks::thread_list_lock_, deoptimized_methods_lock_)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100174 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
175
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200176 // Indicates whether the method has been deoptimized so it is executed with the interpreter.
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700177 bool IsDeoptimized(mirror::ArtMethod* method)
178 LOCKS_EXCLUDED(deoptimized_methods_lock_)
179 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100180
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200181 // Enable method tracing by installing instrumentation entry/exit stubs or interpreter.
182 void EnableMethodTracing(const char* key,
183 bool needs_interpreter = kDeoptimizeForAccurateMethodEntryExitListeners)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100184 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
185 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
186
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200187 // Disable method tracing by uninstalling instrumentation entry/exit stubs or interpreter.
188 void DisableMethodTracing(const char* key)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100189 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
190 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
191
Sebastien Hertzed2be172014-08-19 15:33:43 +0200192 InterpreterHandlerTable GetInterpreterHandlerTable() const
193 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200194 return interpreter_handler_table_;
195 }
196
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700197 void InstrumentQuickAllocEntryPoints() LOCKS_EXCLUDED(Locks::instrument_entrypoints_lock_);
198 void UninstrumentQuickAllocEntryPoints() LOCKS_EXCLUDED(Locks::instrument_entrypoints_lock_);
199 void InstrumentQuickAllocEntryPointsLocked()
200 EXCLUSIVE_LOCKS_REQUIRED(Locks::instrument_entrypoints_lock_)
Jeff Hao69dbec62014-09-15 18:03:41 -0700201 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::runtime_shutdown_lock_);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700202 void UninstrumentQuickAllocEntryPointsLocked()
203 EXCLUSIVE_LOCKS_REQUIRED(Locks::instrument_entrypoints_lock_)
Jeff Hao69dbec62014-09-15 18:03:41 -0700204 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::runtime_shutdown_lock_);
Mathieu Chartierd8891782014-03-02 13:28:37 -0800205 void ResetQuickAllocEntryPoints() EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_);
Ian Rogersfa824272013-11-05 16:12:57 -0800206
Ian Rogers62d6c772013-02-27 08:32:07 -0800207 // Update the code of a method respecting any installed stubs.
Elliott Hughes956af0f2014-12-11 14:34:28 -0800208 void UpdateMethodsCode(mirror::ArtMethod* method, const void* quick_code)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100209 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800210
211 // Get the quick code for the given method. More efficient than asking the class linker as it
212 // will short-cut to GetCode if instrumentation and static method resolution stubs aren't
213 // installed.
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800214 const void* GetQuickCodeFor(mirror::ArtMethod* method, size_t pointer_size) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800215 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
216
217 void ForceInterpretOnly() {
218 interpret_only_ = true;
219 forced_interpret_only_ = true;
220 }
221
Brian Carlstromea46f952013-07-30 01:26:50 -0700222 // Called by ArtMethod::Invoke to determine dispatch mechanism.
Ian Rogers62d6c772013-02-27 08:32:07 -0800223 bool InterpretOnly() const {
224 return interpret_only_;
225 }
226
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800227 bool IsForcedInterpretOnly() const {
228 return forced_interpret_only_;
229 }
230
Ian Rogers62d6c772013-02-27 08:32:07 -0800231 bool AreExitStubsInstalled() const {
232 return instrumentation_stubs_installed_;
233 }
234
Sebastien Hertzed2be172014-08-19 15:33:43 +0200235 bool HasMethodEntryListeners() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200236 return have_method_entry_listeners_;
237 }
238
Sebastien Hertzed2be172014-08-19 15:33:43 +0200239 bool HasMethodExitListeners() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200240 return have_method_exit_listeners_;
241 }
242
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200243 bool HasMethodUnwindListeners() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
244 return have_method_unwind_listeners_;
245 }
246
Sebastien Hertzed2be172014-08-19 15:33:43 +0200247 bool HasDexPcListeners() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200248 return have_dex_pc_listeners_;
249 }
250
Sebastien Hertzed2be172014-08-19 15:33:43 +0200251 bool HasFieldReadListeners() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200252 return have_field_read_listeners_;
253 }
254
Sebastien Hertzed2be172014-08-19 15:33:43 +0200255 bool HasFieldWriteListeners() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200256 return have_field_write_listeners_;
257 }
258
Sebastien Hertzed2be172014-08-19 15:33:43 +0200259 bool HasExceptionCaughtListeners() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200260 return have_exception_caught_listeners_;
261 }
262
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800263 bool HasBackwardBranchListeners() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
264 return have_backward_branch_listeners_;
265 }
266
Sebastien Hertzed2be172014-08-19 15:33:43 +0200267 bool IsActive() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200268 return have_dex_pc_listeners_ || have_method_entry_listeners_ || have_method_exit_listeners_ ||
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200269 have_field_read_listeners_ || have_field_write_listeners_ ||
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200270 have_exception_caught_listeners_ || have_method_unwind_listeners_;
271 }
272
Ian Rogers62d6c772013-02-27 08:32:07 -0800273 // Inform listeners that a method has been entered. A dex PC is provided as we may install
274 // listeners into executing code and get method enter events for methods already on the stack.
275 void MethodEnterEvent(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800276 mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800277 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200278 if (UNLIKELY(HasMethodEntryListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800279 MethodEnterEventImpl(thread, this_object, method, dex_pc);
280 }
281 }
282
283 // Inform listeners that a method has been exited.
284 void MethodExitEvent(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800285 mirror::ArtMethod* method, uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800286 const JValue& return_value) const
287 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200288 if (UNLIKELY(HasMethodExitListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800289 MethodExitEventImpl(thread, this_object, method, dex_pc, return_value);
290 }
291 }
292
293 // Inform listeners that a method has been exited due to an exception.
294 void MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800295 mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800296 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
297
298 // Inform listeners that the dex pc has moved (only supported by the interpreter).
299 void DexPcMovedEvent(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800300 mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800301 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200302 if (UNLIKELY(HasDexPcListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800303 DexPcMovedEventImpl(thread, this_object, method, dex_pc);
304 }
305 }
306
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800307 // Inform listeners that a backward branch has been taken (only supported by the interpreter).
308 void BackwardBranch(Thread* thread, mirror::ArtMethod* method, int32_t offset) const
309 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
310 if (UNLIKELY(HasBackwardBranchListeners())) {
311 BackwardBranchImpl(thread, method, offset);
312 }
313 }
314
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200315 // Inform listeners that we read a field (only supported by the interpreter).
316 void FieldReadEvent(Thread* thread, mirror::Object* this_object,
317 mirror::ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700318 ArtField* field) const
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200319 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
320 if (UNLIKELY(HasFieldReadListeners())) {
321 FieldReadEventImpl(thread, this_object, method, dex_pc, field);
322 }
323 }
324
325 // Inform listeners that we write a field (only supported by the interpreter).
326 void FieldWriteEvent(Thread* thread, mirror::Object* this_object,
327 mirror::ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700328 ArtField* field, const JValue& field_value) const
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200329 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
330 if (UNLIKELY(HasFieldWriteListeners())) {
331 FieldWriteEventImpl(thread, this_object, method, dex_pc, field, field_value);
332 }
333 }
334
Ian Rogers62d6c772013-02-27 08:32:07 -0800335 // Inform listeners that an exception was caught.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000336 void ExceptionCaughtEvent(Thread* thread, mirror::Throwable* exception_object) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800337 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
338
339 // Called when an instrumented method is entered. The intended link register (lr) is saved so
340 // that returning causes a branch to the method exit stub. Generates method enter events.
341 void PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700342 mirror::ArtMethod* method, uintptr_t lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700343 bool interpreter_entry)
Ian Rogers62d6c772013-02-27 08:32:07 -0800344 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
345
346 // Called when an instrumented method is exited. Removes the pushed instrumentation frame
347 // returning the intended link register. Generates method exit events.
Andreas Gamped58342c2014-06-05 14:18:08 -0700348 TwoWordReturn PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
349 uint64_t gpr_result, uint64_t fpr_result)
Ian Rogers62d6c772013-02-27 08:32:07 -0800350 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
351
352 // Pops an instrumentation frame from the current thread and generate an unwind event.
353 void PopMethodForUnwind(Thread* self, bool is_deoptimization) const
354 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
355
356 // Call back for configure stubs.
Sebastien Hertza10aa372015-01-21 17:30:58 +0100357 void InstallStubsForClass(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800358
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100359 void InstallStubsForMethod(mirror::ArtMethod* method)
360 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
361
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700362 void VisitRoots(RootVisitor* visitor) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700363 LOCKS_EXCLUDED(deoptimized_methods_lock_);
364
jeffhao725a9572012-11-13 18:20:12 -0800365 private:
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200366 InstrumentationLevel GetCurrentInstrumentationLevel() const;
367
Ian Rogers62d6c772013-02-27 08:32:07 -0800368 // Does the job of installing or removing instrumentation code within methods.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200369 // In order to support multiple clients using instrumentation at the same time,
370 // the caller must pass a unique key (a string) identifying it so we remind which
371 // instrumentation level it needs. Therefore the current instrumentation level
372 // becomes the highest instrumentation level required by a client.
373 void ConfigureStubs(const char* key, InstrumentationLevel desired_instrumentation_level)
Ian Rogers62d6c772013-02-27 08:32:07 -0800374 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700375 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_,
376 deoptimized_methods_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800377
Sebastien Hertzed2be172014-08-19 15:33:43 +0200378 void UpdateInterpreterHandlerTable() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200379 interpreter_handler_table_ = IsActive() ? kAlternativeHandlerTable : kMainHandlerTable;
380 }
381
Mathieu Chartier661974a2014-01-09 11:23:53 -0800382 // No thread safety analysis to get around SetQuickAllocEntryPointsInstrumented requiring
383 // exclusive access to mutator lock which you can't get if the runtime isn't started.
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700384 void SetEntrypointsInstrumented(bool instrumented) NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800385
Ian Rogers62d6c772013-02-27 08:32:07 -0800386 void MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800387 mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800388 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
389 void MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800390 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800391 uint32_t dex_pc, const JValue& return_value) const
392 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
393 void DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800394 mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800395 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800396 void BackwardBranchImpl(Thread* thread, mirror::ArtMethod* method, int32_t offset) const
397 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200398 void FieldReadEventImpl(Thread* thread, mirror::Object* this_object,
399 mirror::ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700400 ArtField* field) const
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200401 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
402 void FieldWriteEventImpl(Thread* thread, mirror::Object* this_object,
403 mirror::ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700404 ArtField* field, const JValue& field_value) const
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200405 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800406
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700407 // Read barrier-aware utility functions for accessing deoptimized_methods_
408 bool AddDeoptimizedMethod(mirror::ArtMethod* method)
409 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
410 EXCLUSIVE_LOCKS_REQUIRED(deoptimized_methods_lock_);
411 bool FindDeoptimizedMethod(mirror::ArtMethod* method)
412 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
413 SHARED_LOCKS_REQUIRED(deoptimized_methods_lock_);
414 bool RemoveDeoptimizedMethod(mirror::ArtMethod* method)
415 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
416 EXCLUSIVE_LOCKS_REQUIRED(deoptimized_methods_lock_);
417 mirror::ArtMethod* BeginDeoptimizedMethod()
418 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
419 SHARED_LOCKS_REQUIRED(deoptimized_methods_lock_);
420 bool IsDeoptimizedMethodsEmpty() const
421 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
422 SHARED_LOCKS_REQUIRED(deoptimized_methods_lock_);
423
Brian Carlstromea46f952013-07-30 01:26:50 -0700424 // Have we hijacked ArtMethod::code_ so that it calls instrumentation/interpreter code?
Ian Rogers62d6c772013-02-27 08:32:07 -0800425 bool instrumentation_stubs_installed_;
426
Brian Carlstromea46f952013-07-30 01:26:50 -0700427 // Have we hijacked ArtMethod::code_ to reference the enter/exit stubs?
Ian Rogers62d6c772013-02-27 08:32:07 -0800428 bool entry_exit_stubs_installed_;
429
Brian Carlstromea46f952013-07-30 01:26:50 -0700430 // Have we hijacked ArtMethod::code_ to reference the enter interpreter stub?
Ian Rogers62d6c772013-02-27 08:32:07 -0800431 bool interpreter_stubs_installed_;
432
433 // Do we need the fidelity of events that we only get from running within the interpreter?
434 bool interpret_only_;
435
436 // Did the runtime request we only run in the interpreter? ie -Xint mode.
437 bool forced_interpret_only_;
438
439 // Do we have any listeners for method entry events? Short-cut to avoid taking the
440 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200441 bool have_method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800442
443 // Do we have any listeners for method exit events? Short-cut to avoid taking the
444 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200445 bool have_method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800446
447 // Do we have any listeners for method unwind events? Short-cut to avoid taking the
448 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200449 bool have_method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800450
451 // Do we have any listeners for dex move events? Short-cut to avoid taking the
452 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200453 bool have_dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800454
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200455 // Do we have any listeners for field read events? Short-cut to avoid taking the
456 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200457 bool have_field_read_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200458
459 // Do we have any listeners for field write events? Short-cut to avoid taking the
460 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200461 bool have_field_write_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200462
Ian Rogers62d6c772013-02-27 08:32:07 -0800463 // Do we have any exception caught listeners? Short-cut to avoid taking the instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200464 bool have_exception_caught_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800465
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800466 // Do we have any backward branch listeners? Short-cut to avoid taking the instrumentation_lock_.
467 bool have_backward_branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
468
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200469 // Contains the instrumentation level required by each client of the instrumentation identified
470 // by a string key.
471 typedef SafeMap<const char*, InstrumentationLevel> InstrumentationLevelTable;
472 InstrumentationLevelTable requested_instrumentation_levels_ GUARDED_BY(Locks::mutator_lock_);
473
Ian Rogers62d6c772013-02-27 08:32:07 -0800474 // The event listeners, written to with the mutator_lock_ exclusively held.
475 std::list<InstrumentationListener*> method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
476 std::list<InstrumentationListener*> method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
477 std::list<InstrumentationListener*> method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800478 std::list<InstrumentationListener*> backward_branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200479 std::shared_ptr<std::list<InstrumentationListener*>> dex_pc_listeners_
480 GUARDED_BY(Locks::mutator_lock_);
481 std::shared_ptr<std::list<InstrumentationListener*>> field_read_listeners_
482 GUARDED_BY(Locks::mutator_lock_);
483 std::shared_ptr<std::list<InstrumentationListener*>> field_write_listeners_
484 GUARDED_BY(Locks::mutator_lock_);
485 std::shared_ptr<std::list<InstrumentationListener*>> exception_caught_listeners_
486 GUARDED_BY(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800487
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100488 // The set of methods being deoptimized (by the debugger) which must be executed with interpreter
489 // only.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700490 mutable ReaderWriterMutex deoptimized_methods_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700491 std::multimap<int32_t, GcRoot<mirror::ArtMethod>> deoptimized_methods_
492 GUARDED_BY(deoptimized_methods_lock_);
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100493 bool deoptimization_enabled_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100494
Ian Rogersfa824272013-11-05 16:12:57 -0800495 // Current interpreter handler table. This is updated each time the thread state flags are
496 // modified.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200497 InterpreterHandlerTable interpreter_handler_table_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200498
Ian Rogersfa824272013-11-05 16:12:57 -0800499 // Greater than 0 if quick alloc entry points instrumented.
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700500 size_t quick_alloc_entry_points_instrumentation_counter_
501 GUARDED_BY(Locks::instrument_entrypoints_lock_);
Ian Rogersfa824272013-11-05 16:12:57 -0800502
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200503 friend class InstrumentationTest; // For GetCurrentInstrumentationLevel and ConfigureStubs.
504
jeffhao725a9572012-11-13 18:20:12 -0800505 DISALLOW_COPY_AND_ASSIGN(Instrumentation);
506};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700507std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationEvent& rhs);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200508std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationLevel& rhs);
jeffhao725a9572012-11-13 18:20:12 -0800509
Ian Rogers62d6c772013-02-27 08:32:07 -0800510// An element in the instrumentation side stack maintained in art::Thread.
511struct InstrumentationStackFrame {
Brian Carlstromea46f952013-07-30 01:26:50 -0700512 InstrumentationStackFrame(mirror::Object* this_object, mirror::ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -0700513 uintptr_t return_pc, size_t frame_id, bool interpreter_entry)
514 : this_object_(this_object), method_(method), return_pc_(return_pc), frame_id_(frame_id),
515 interpreter_entry_(interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800516 }
517
518 std::string Dump() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
519
520 mirror::Object* this_object_;
Brian Carlstromea46f952013-07-30 01:26:50 -0700521 mirror::ArtMethod* method_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100522 uintptr_t return_pc_;
523 size_t frame_id_;
524 bool interpreter_entry_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800525};
526
527} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -0800528} // namespace art
529
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700530#endif // ART_RUNTIME_INSTRUMENTATION_H_