blob: 8b7fcca48e9aedf74041a5896bb6781a57a1ffa9 [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"
Ian Rogersef7d42f2014-01-06 12:55:46 -080025#include "atomic.h"
Elliott Hughes76160052012-12-12 16:31:20 -080026#include "base/macros.h"
Ian Rogers719d1a32014-03-06 12:13:39 -080027#include "base/mutex.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070028#include "gc_root.h"
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070029#include "object_callbacks.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030
jeffhao725a9572012-11-13 18:20:12 -080031namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032namespace mirror {
Brian Carlstromea46f952013-07-30 01:26:50 -070033 class ArtMethod;
34 class Class;
35 class Object;
36 class Throwable;
Ian Rogers62d6c772013-02-27 08:32:07 -080037} // namespace mirror
Mathieu Chartierc7853442015-03-27 14:35:38 -070038class ArtField;
Ian Rogers62d6c772013-02-27 08:32:07 -080039union JValue;
jeffhao725a9572012-11-13 18:20:12 -080040class Thread;
41
Ian Rogers62d6c772013-02-27 08:32:07 -080042namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080043
Sebastien Hertzee1997a2013-09-19 14:47:09 +020044// Interpreter handler tables.
45enum InterpreterHandlerTable {
46 kMainHandlerTable = 0, // Main handler table: no suspend check, no instrumentation.
47 kAlternativeHandlerTable = 1, // Alternative handler table: suspend check and/or instrumentation
48 // enabled.
49 kNumHandlerTables
50};
51
Andreas Gampe40da2862015-02-27 12:49:04 -080052// Do we want to deoptimize for method entry and exit listeners or just try to intercept
53// invocations? Deoptimization forces all code to run in the interpreter and considerably hurts the
54// application's performance.
55static constexpr bool kDeoptimizeForAccurateMethodEntryExitListeners = true;
56
Ian Rogers62d6c772013-02-27 08:32:07 -080057// Instrumentation event listener API. Registered listeners will get the appropriate call back for
58// the events they are listening for. The call backs supply the thread, method and dex_pc the event
59// occurred upon. The thread may or may not be Thread::Current().
60struct InstrumentationListener {
61 InstrumentationListener() {}
62 virtual ~InstrumentationListener() {}
63
64 // Call-back for when a method is entered.
65 virtual void MethodEntered(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -080066 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -080067 uint32_t dex_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
68
69 // Call-back for when a method is exited.
70 // TODO: its likely passing the return value would be useful, however, we may need to get and
71 // parse the shorty to determine what kind of register holds the result.
72 virtual void MethodExited(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -080073 mirror::ArtMethod* method, uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -080074 const JValue& return_value)
75 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
76
77 // Call-back for when a method is popped due to an exception throw. A method will either cause a
78 // MethodExited call-back or a MethodUnwind call-back when its activation is removed.
Sebastien Hertz51db44a2013-11-19 10:00:29 +010079 virtual void MethodUnwind(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -080080 mirror::ArtMethod* method, uint32_t dex_pc)
Sebastien Hertz51db44a2013-11-19 10:00:29 +010081 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -080082
83 // Call-back for when the dex pc moves in a method.
84 virtual void DexPcMoved(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -080085 mirror::ArtMethod* method, uint32_t new_dex_pc)
Ian Rogers62d6c772013-02-27 08:32:07 -080086 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
87
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020088 // Call-back for when we read from a field.
89 virtual void FieldRead(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
Mathieu Chartierc7853442015-03-27 14:35:38 -070090 uint32_t dex_pc, ArtField* field) = 0;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020091
92 // Call-back for when we write into a field.
93 virtual void FieldWritten(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
Mathieu Chartierc7853442015-03-27 14:35:38 -070094 uint32_t dex_pc, ArtField* field, const JValue& field_value) = 0;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020095
Ian Rogers62d6c772013-02-27 08:32:07 -080096 // Call-back when an exception is caught.
Nicolas Geoffray14691c52015-03-05 10:40:17 +000097 virtual void ExceptionCaught(Thread* thread, mirror::Throwable* exception_object)
Ian Rogers62d6c772013-02-27 08:32:07 -080098 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080099
100 // Call-back for when we get a backward branch.
101 virtual void BackwardBranch(Thread* thread, mirror::ArtMethod* method, int32_t dex_pc_offset)
102 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
jeffhao725a9572012-11-13 18:20:12 -0800103};
104
Ian Rogers62d6c772013-02-27 08:32:07 -0800105// Instrumentation is a catch-all for when extra information is required from the runtime. The
106// typical use for instrumentation is for profiling and debugging. Instrumentation may add stubs
107// to method entry and exit, it may also force execution to be switched to the interpreter and
108// trigger deoptimization.
jeffhao725a9572012-11-13 18:20:12 -0800109class Instrumentation {
110 public:
Ian Rogers62d6c772013-02-27 08:32:07 -0800111 enum InstrumentationEvent {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800112 kMethodEntered = 0x1,
113 kMethodExited = 0x2,
114 kMethodUnwind = 0x4,
115 kDexPcMoved = 0x8,
116 kFieldRead = 0x10,
117 kFieldWritten = 0x20,
118 kExceptionCaught = 0x40,
119 kBackwardBranch = 0x80,
Ian Rogers62d6c772013-02-27 08:32:07 -0800120 };
jeffhao725a9572012-11-13 18:20:12 -0800121
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700122 Instrumentation();
jeffhao725a9572012-11-13 18:20:12 -0800123
Ian Rogers62d6c772013-02-27 08:32:07 -0800124 // Add a listener to be notified of the masked together sent of instrumentation events. This
125 // suspend the runtime to install stubs. You are expected to hold the mutator lock as a proxy
126 // for saying you should have suspended all threads (installing stubs while threads are running
127 // will break).
128 void AddListener(InstrumentationListener* listener, uint32_t events)
129 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
130 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800131
Ian Rogers62d6c772013-02-27 08:32:07 -0800132 // Removes a listener possibly removing instrumentation stubs.
133 void RemoveListener(InstrumentationListener* listener, uint32_t events)
134 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
135 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800136
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100137 // Deoptimization.
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100138 void EnableDeoptimization()
139 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700140 LOCKS_EXCLUDED(deoptimized_methods_lock_);
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100141 void DisableDeoptimization()
142 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100143 LOCKS_EXCLUDED(deoptimized_methods_lock_);
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100144 bool AreAllMethodsDeoptimized() const {
145 return interpreter_stubs_installed_;
146 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100147 bool ShouldNotifyMethodEnterExitEvents() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100148
149 // Executes everything with interpreter.
150 void DeoptimizeEverything()
151 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
152 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
153
154 // Executes everything with compiled code (or interpreter if there is no code).
155 void UndeoptimizeEverything()
156 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
157 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
158
159 // Deoptimize a method by forcing its execution with the interpreter. Nevertheless, a static
160 // method (except a class initializer) set to the resolution trampoline will be deoptimized only
161 // once its declaring class is initialized.
162 void Deoptimize(mirror::ArtMethod* method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700163 LOCKS_EXCLUDED(Locks::thread_list_lock_, deoptimized_methods_lock_)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100164 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
165
166 // Undeoptimze the method by restoring its entrypoints. Nevertheless, a static method
167 // (except a class initializer) set to the resolution trampoline will be updated only once its
168 // declaring class is initialized.
169 void Undeoptimize(mirror::ArtMethod* method)
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100170 LOCKS_EXCLUDED(Locks::thread_list_lock_, deoptimized_methods_lock_)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100171 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
172
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700173 bool IsDeoptimized(mirror::ArtMethod* method)
174 LOCKS_EXCLUDED(deoptimized_methods_lock_)
175 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100176
177 // Enable method tracing by installing instrumentation entry/exit stubs.
Andreas Gampe40da2862015-02-27 12:49:04 -0800178 void EnableMethodTracing(
179 bool require_interpreter = kDeoptimizeForAccurateMethodEntryExitListeners)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100180 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
181 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
182
183 // Disable method tracing by uninstalling instrumentation entry/exit stubs.
184 void DisableMethodTracing()
185 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
186 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
187
Sebastien Hertzed2be172014-08-19 15:33:43 +0200188 InterpreterHandlerTable GetInterpreterHandlerTable() const
189 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200190 return interpreter_handler_table_;
191 }
192
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700193 void InstrumentQuickAllocEntryPoints() LOCKS_EXCLUDED(Locks::instrument_entrypoints_lock_);
194 void UninstrumentQuickAllocEntryPoints() LOCKS_EXCLUDED(Locks::instrument_entrypoints_lock_);
195 void InstrumentQuickAllocEntryPointsLocked()
196 EXCLUSIVE_LOCKS_REQUIRED(Locks::instrument_entrypoints_lock_)
Jeff Hao69dbec62014-09-15 18:03:41 -0700197 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::runtime_shutdown_lock_);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700198 void UninstrumentQuickAllocEntryPointsLocked()
199 EXCLUSIVE_LOCKS_REQUIRED(Locks::instrument_entrypoints_lock_)
Jeff Hao69dbec62014-09-15 18:03:41 -0700200 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::runtime_shutdown_lock_);
Mathieu Chartierd8891782014-03-02 13:28:37 -0800201 void ResetQuickAllocEntryPoints() EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_);
Ian Rogersfa824272013-11-05 16:12:57 -0800202
Ian Rogers62d6c772013-02-27 08:32:07 -0800203 // Update the code of a method respecting any installed stubs.
Elliott Hughes956af0f2014-12-11 14:34:28 -0800204 void UpdateMethodsCode(mirror::ArtMethod* method, const void* quick_code)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100205 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800206
207 // Get the quick code for the given method. More efficient than asking the class linker as it
208 // will short-cut to GetCode if instrumentation and static method resolution stubs aren't
209 // installed.
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800210 const void* GetQuickCodeFor(mirror::ArtMethod* method, size_t pointer_size) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800211 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
212
213 void ForceInterpretOnly() {
214 interpret_only_ = true;
215 forced_interpret_only_ = true;
216 }
217
Brian Carlstromea46f952013-07-30 01:26:50 -0700218 // Called by ArtMethod::Invoke to determine dispatch mechanism.
Ian Rogers62d6c772013-02-27 08:32:07 -0800219 bool InterpretOnly() const {
220 return interpret_only_;
221 }
222
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800223 bool IsForcedInterpretOnly() const {
224 return forced_interpret_only_;
225 }
226
Ian Rogers62d6c772013-02-27 08:32:07 -0800227 bool AreExitStubsInstalled() const {
228 return instrumentation_stubs_installed_;
229 }
230
Sebastien Hertzed2be172014-08-19 15:33:43 +0200231 bool HasMethodEntryListeners() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200232 return have_method_entry_listeners_;
233 }
234
Sebastien Hertzed2be172014-08-19 15:33:43 +0200235 bool HasMethodExitListeners() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200236 return have_method_exit_listeners_;
237 }
238
Sebastien Hertzed2be172014-08-19 15:33:43 +0200239 bool HasDexPcListeners() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200240 return have_dex_pc_listeners_;
241 }
242
Sebastien Hertzed2be172014-08-19 15:33:43 +0200243 bool HasFieldReadListeners() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200244 return have_field_read_listeners_;
245 }
246
Sebastien Hertzed2be172014-08-19 15:33:43 +0200247 bool HasFieldWriteListeners() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200248 return have_field_write_listeners_;
249 }
250
Sebastien Hertzed2be172014-08-19 15:33:43 +0200251 bool HasExceptionCaughtListeners() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200252 return have_exception_caught_listeners_;
253 }
254
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800255 bool HasBackwardBranchListeners() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
256 return have_backward_branch_listeners_;
257 }
258
Sebastien Hertzed2be172014-08-19 15:33:43 +0200259 bool IsActive() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200260 return have_dex_pc_listeners_ || have_method_entry_listeners_ || have_method_exit_listeners_ ||
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200261 have_field_read_listeners_ || have_field_write_listeners_ ||
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200262 have_exception_caught_listeners_ || have_method_unwind_listeners_;
263 }
264
Ian Rogers62d6c772013-02-27 08:32:07 -0800265 // Inform listeners that a method has been entered. A dex PC is provided as we may install
266 // listeners into executing code and get method enter events for methods already on the stack.
267 void MethodEnterEvent(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800268 mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800269 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200270 if (UNLIKELY(HasMethodEntryListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800271 MethodEnterEventImpl(thread, this_object, method, dex_pc);
272 }
273 }
274
275 // Inform listeners that a method has been exited.
276 void MethodExitEvent(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800277 mirror::ArtMethod* method, uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800278 const JValue& return_value) const
279 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200280 if (UNLIKELY(HasMethodExitListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800281 MethodExitEventImpl(thread, this_object, method, dex_pc, return_value);
282 }
283 }
284
285 // Inform listeners that a method has been exited due to an exception.
286 void MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800287 mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800288 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
289
290 // Inform listeners that the dex pc has moved (only supported by the interpreter).
291 void DexPcMovedEvent(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800292 mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800293 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200294 if (UNLIKELY(HasDexPcListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800295 DexPcMovedEventImpl(thread, this_object, method, dex_pc);
296 }
297 }
298
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800299 // Inform listeners that a backward branch has been taken (only supported by the interpreter).
300 void BackwardBranch(Thread* thread, mirror::ArtMethod* method, int32_t offset) const
301 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
302 if (UNLIKELY(HasBackwardBranchListeners())) {
303 BackwardBranchImpl(thread, method, offset);
304 }
305 }
306
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200307 // Inform listeners that we read a field (only supported by the interpreter).
308 void FieldReadEvent(Thread* thread, mirror::Object* this_object,
309 mirror::ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700310 ArtField* field) const
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200311 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
312 if (UNLIKELY(HasFieldReadListeners())) {
313 FieldReadEventImpl(thread, this_object, method, dex_pc, field);
314 }
315 }
316
317 // Inform listeners that we write a field (only supported by the interpreter).
318 void FieldWriteEvent(Thread* thread, mirror::Object* this_object,
319 mirror::ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700320 ArtField* field, const JValue& field_value) const
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200321 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
322 if (UNLIKELY(HasFieldWriteListeners())) {
323 FieldWriteEventImpl(thread, this_object, method, dex_pc, field, field_value);
324 }
325 }
326
Ian Rogers62d6c772013-02-27 08:32:07 -0800327 // Inform listeners that an exception was caught.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000328 void ExceptionCaughtEvent(Thread* thread, mirror::Throwable* exception_object) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800329 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
330
331 // Called when an instrumented method is entered. The intended link register (lr) is saved so
332 // that returning causes a branch to the method exit stub. Generates method enter events.
333 void PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700334 mirror::ArtMethod* method, uintptr_t lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700335 bool interpreter_entry)
Ian Rogers62d6c772013-02-27 08:32:07 -0800336 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
337
338 // Called when an instrumented method is exited. Removes the pushed instrumentation frame
339 // returning the intended link register. Generates method exit events.
Andreas Gamped58342c2014-06-05 14:18:08 -0700340 TwoWordReturn PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
341 uint64_t gpr_result, uint64_t fpr_result)
Ian Rogers62d6c772013-02-27 08:32:07 -0800342 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
343
344 // Pops an instrumentation frame from the current thread and generate an unwind event.
345 void PopMethodForUnwind(Thread* self, bool is_deoptimization) const
346 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
347
348 // Call back for configure stubs.
Sebastien Hertza10aa372015-01-21 17:30:58 +0100349 void InstallStubsForClass(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800350
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100351 void InstallStubsForMethod(mirror::ArtMethod* method)
352 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
353
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700354 void VisitRoots(RootVisitor* visitor) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700355 LOCKS_EXCLUDED(deoptimized_methods_lock_);
356
jeffhao725a9572012-11-13 18:20:12 -0800357 private:
Ian Rogers62d6c772013-02-27 08:32:07 -0800358 // Does the job of installing or removing instrumentation code within methods.
359 void ConfigureStubs(bool require_entry_exit_stubs, bool require_interpreter)
360 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700361 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_,
362 deoptimized_methods_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800363
Sebastien Hertzed2be172014-08-19 15:33:43 +0200364 void UpdateInterpreterHandlerTable() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200365 interpreter_handler_table_ = IsActive() ? kAlternativeHandlerTable : kMainHandlerTable;
366 }
367
Mathieu Chartier661974a2014-01-09 11:23:53 -0800368 // No thread safety analysis to get around SetQuickAllocEntryPointsInstrumented requiring
369 // exclusive access to mutator lock which you can't get if the runtime isn't started.
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700370 void SetEntrypointsInstrumented(bool instrumented) NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800371
Ian Rogers62d6c772013-02-27 08:32:07 -0800372 void MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800373 mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800374 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
375 void MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800376 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800377 uint32_t dex_pc, const JValue& return_value) const
378 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
379 void DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800380 mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800381 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800382 void BackwardBranchImpl(Thread* thread, mirror::ArtMethod* method, int32_t offset) const
383 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200384 void FieldReadEventImpl(Thread* thread, mirror::Object* this_object,
385 mirror::ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700386 ArtField* field) const
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200387 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
388 void FieldWriteEventImpl(Thread* thread, mirror::Object* this_object,
389 mirror::ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700390 ArtField* field, const JValue& field_value) const
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200391 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800392
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700393 // Read barrier-aware utility functions for accessing deoptimized_methods_
394 bool AddDeoptimizedMethod(mirror::ArtMethod* method)
395 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
396 EXCLUSIVE_LOCKS_REQUIRED(deoptimized_methods_lock_);
397 bool FindDeoptimizedMethod(mirror::ArtMethod* method)
398 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
399 SHARED_LOCKS_REQUIRED(deoptimized_methods_lock_);
400 bool RemoveDeoptimizedMethod(mirror::ArtMethod* method)
401 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
402 EXCLUSIVE_LOCKS_REQUIRED(deoptimized_methods_lock_);
403 mirror::ArtMethod* BeginDeoptimizedMethod()
404 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
405 SHARED_LOCKS_REQUIRED(deoptimized_methods_lock_);
406 bool IsDeoptimizedMethodsEmpty() const
407 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
408 SHARED_LOCKS_REQUIRED(deoptimized_methods_lock_);
409
Brian Carlstromea46f952013-07-30 01:26:50 -0700410 // Have we hijacked ArtMethod::code_ so that it calls instrumentation/interpreter code?
Ian Rogers62d6c772013-02-27 08:32:07 -0800411 bool instrumentation_stubs_installed_;
412
Brian Carlstromea46f952013-07-30 01:26:50 -0700413 // Have we hijacked ArtMethod::code_ to reference the enter/exit stubs?
Ian Rogers62d6c772013-02-27 08:32:07 -0800414 bool entry_exit_stubs_installed_;
415
Brian Carlstromea46f952013-07-30 01:26:50 -0700416 // Have we hijacked ArtMethod::code_ to reference the enter interpreter stub?
Ian Rogers62d6c772013-02-27 08:32:07 -0800417 bool interpreter_stubs_installed_;
418
419 // Do we need the fidelity of events that we only get from running within the interpreter?
420 bool interpret_only_;
421
422 // Did the runtime request we only run in the interpreter? ie -Xint mode.
423 bool forced_interpret_only_;
424
425 // Do we have any listeners for method entry events? Short-cut to avoid taking the
426 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200427 bool have_method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800428
429 // Do we have any listeners for method exit events? Short-cut to avoid taking the
430 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200431 bool have_method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800432
433 // Do we have any listeners for method unwind events? Short-cut to avoid taking the
434 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200435 bool have_method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800436
437 // Do we have any listeners for dex move events? Short-cut to avoid taking the
438 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200439 bool have_dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800440
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200441 // Do we have any listeners for field read events? Short-cut to avoid taking the
442 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200443 bool have_field_read_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200444
445 // Do we have any listeners for field write events? Short-cut to avoid taking the
446 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200447 bool have_field_write_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200448
Ian Rogers62d6c772013-02-27 08:32:07 -0800449 // Do we have any exception caught listeners? Short-cut to avoid taking the instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200450 bool have_exception_caught_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800451
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800452 // Do we have any backward branch listeners? Short-cut to avoid taking the instrumentation_lock_.
453 bool have_backward_branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
454
Ian Rogers62d6c772013-02-27 08:32:07 -0800455 // The event listeners, written to with the mutator_lock_ exclusively held.
456 std::list<InstrumentationListener*> method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
457 std::list<InstrumentationListener*> method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
458 std::list<InstrumentationListener*> method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800459 std::list<InstrumentationListener*> backward_branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200460 std::shared_ptr<std::list<InstrumentationListener*>> dex_pc_listeners_
461 GUARDED_BY(Locks::mutator_lock_);
462 std::shared_ptr<std::list<InstrumentationListener*>> field_read_listeners_
463 GUARDED_BY(Locks::mutator_lock_);
464 std::shared_ptr<std::list<InstrumentationListener*>> field_write_listeners_
465 GUARDED_BY(Locks::mutator_lock_);
466 std::shared_ptr<std::list<InstrumentationListener*>> exception_caught_listeners_
467 GUARDED_BY(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800468
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100469 // The set of methods being deoptimized (by the debugger) which must be executed with interpreter
470 // only.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700471 mutable ReaderWriterMutex deoptimized_methods_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700472 std::multimap<int32_t, GcRoot<mirror::ArtMethod>> deoptimized_methods_
473 GUARDED_BY(deoptimized_methods_lock_);
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100474 bool deoptimization_enabled_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100475
Ian Rogersfa824272013-11-05 16:12:57 -0800476 // Current interpreter handler table. This is updated each time the thread state flags are
477 // modified.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200478 InterpreterHandlerTable interpreter_handler_table_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200479
Ian Rogersfa824272013-11-05 16:12:57 -0800480 // Greater than 0 if quick alloc entry points instrumented.
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700481 size_t quick_alloc_entry_points_instrumentation_counter_
482 GUARDED_BY(Locks::instrument_entrypoints_lock_);
Ian Rogersfa824272013-11-05 16:12:57 -0800483
jeffhao725a9572012-11-13 18:20:12 -0800484 DISALLOW_COPY_AND_ASSIGN(Instrumentation);
485};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700486std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationEvent& rhs);
jeffhao725a9572012-11-13 18:20:12 -0800487
Ian Rogers62d6c772013-02-27 08:32:07 -0800488// An element in the instrumentation side stack maintained in art::Thread.
489struct InstrumentationStackFrame {
Brian Carlstromea46f952013-07-30 01:26:50 -0700490 InstrumentationStackFrame(mirror::Object* this_object, mirror::ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -0700491 uintptr_t return_pc, size_t frame_id, bool interpreter_entry)
492 : this_object_(this_object), method_(method), return_pc_(return_pc), frame_id_(frame_id),
493 interpreter_entry_(interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800494 }
495
496 std::string Dump() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
497
498 mirror::Object* this_object_;
Brian Carlstromea46f952013-07-30 01:26:50 -0700499 mirror::ArtMethod* method_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100500 uintptr_t return_pc_;
501 size_t frame_id_;
502 bool interpreter_entry_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800503};
504
505} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -0800506} // namespace art
507
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700508#endif // ART_RUNTIME_INSTRUMENTATION_H_