blob: b667a40fc81a68535ddd143b8c2c8a8053e34c21 [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 {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020033 class ArtField;
Brian Carlstromea46f952013-07-30 01:26:50 -070034 class ArtMethod;
35 class Class;
36 class Object;
37 class Throwable;
Ian Rogers62d6c772013-02-27 08:32:07 -080038} // namespace mirror
39union JValue;
jeffhao725a9572012-11-13 18:20:12 -080040class Thread;
Ian Rogers62d6c772013-02-27 08:32:07 -080041class ThrowLocation;
jeffhao725a9572012-11-13 18:20:12 -080042
Ian Rogers62d6c772013-02-27 08:32:07 -080043namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080044
Sebastien Hertzee1997a2013-09-19 14:47:09 +020045// Interpreter handler tables.
46enum InterpreterHandlerTable {
47 kMainHandlerTable = 0, // Main handler table: no suspend check, no instrumentation.
48 kAlternativeHandlerTable = 1, // Alternative handler table: suspend check and/or instrumentation
49 // enabled.
50 kNumHandlerTables
51};
52
Ian Rogers62d6c772013-02-27 08:32:07 -080053// Instrumentation event listener API. Registered listeners will get the appropriate call back for
54// the events they are listening for. The call backs supply the thread, method and dex_pc the event
55// occurred upon. The thread may or may not be Thread::Current().
56struct InstrumentationListener {
57 InstrumentationListener() {}
58 virtual ~InstrumentationListener() {}
59
60 // Call-back for when a method is entered.
61 virtual void MethodEntered(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -080062 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -080063 uint32_t dex_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
64
65 // Call-back for when a method is exited.
66 // TODO: its likely passing the return value would be useful, however, we may need to get and
67 // parse the shorty to determine what kind of register holds the result.
68 virtual void MethodExited(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -080069 mirror::ArtMethod* method, uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -080070 const JValue& return_value)
71 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
72
73 // Call-back for when a method is popped due to an exception throw. A method will either cause a
74 // MethodExited call-back or a MethodUnwind call-back when its activation is removed.
Sebastien Hertz51db44a2013-11-19 10:00:29 +010075 virtual void MethodUnwind(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -080076 mirror::ArtMethod* method, uint32_t dex_pc)
Sebastien Hertz51db44a2013-11-19 10:00:29 +010077 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -080078
79 // Call-back for when the dex pc moves in a method.
80 virtual void DexPcMoved(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -080081 mirror::ArtMethod* method, uint32_t new_dex_pc)
Ian Rogers62d6c772013-02-27 08:32:07 -080082 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
83
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020084 // Call-back for when we read from a field.
85 virtual void FieldRead(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
86 uint32_t dex_pc, mirror::ArtField* field) = 0;
87
88 // Call-back for when we write into a field.
89 virtual void FieldWritten(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
90 uint32_t dex_pc, mirror::ArtField* field, const JValue& field_value) = 0;
91
Ian Rogers62d6c772013-02-27 08:32:07 -080092 // Call-back when an exception is caught.
93 virtual void ExceptionCaught(Thread* thread, const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -070094 mirror::ArtMethod* catch_method, uint32_t catch_dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -080095 mirror::Throwable* exception_object)
96 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080097
98 // Call-back for when we get a backward branch.
99 virtual void BackwardBranch(Thread* thread, mirror::ArtMethod* method, int32_t dex_pc_offset)
100 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
jeffhao725a9572012-11-13 18:20:12 -0800101};
102
Ian Rogers62d6c772013-02-27 08:32:07 -0800103// Instrumentation is a catch-all for when extra information is required from the runtime. The
104// typical use for instrumentation is for profiling and debugging. Instrumentation may add stubs
105// to method entry and exit, it may also force execution to be switched to the interpreter and
106// trigger deoptimization.
jeffhao725a9572012-11-13 18:20:12 -0800107class Instrumentation {
108 public:
Ian Rogers62d6c772013-02-27 08:32:07 -0800109 enum InstrumentationEvent {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800110 kMethodEntered = 0x1,
111 kMethodExited = 0x2,
112 kMethodUnwind = 0x4,
113 kDexPcMoved = 0x8,
114 kFieldRead = 0x10,
115 kFieldWritten = 0x20,
116 kExceptionCaught = 0x40,
117 kBackwardBranch = 0x80,
Ian Rogers62d6c772013-02-27 08:32:07 -0800118 };
jeffhao725a9572012-11-13 18:20:12 -0800119
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700120 Instrumentation();
jeffhao725a9572012-11-13 18:20:12 -0800121
Ian Rogers62d6c772013-02-27 08:32:07 -0800122 // Add a listener to be notified of the masked together sent of instrumentation events. This
123 // suspend the runtime to install stubs. You are expected to hold the mutator lock as a proxy
124 // for saying you should have suspended all threads (installing stubs while threads are running
125 // will break).
126 void AddListener(InstrumentationListener* listener, uint32_t events)
127 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
128 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800129
Ian Rogers62d6c772013-02-27 08:32:07 -0800130 // Removes a listener possibly removing instrumentation stubs.
131 void RemoveListener(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
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100135 // Deoptimization.
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100136 void EnableDeoptimization()
137 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700138 LOCKS_EXCLUDED(deoptimized_methods_lock_);
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100139 void DisableDeoptimization()
140 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100141 LOCKS_EXCLUDED(deoptimized_methods_lock_);
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100142 bool AreAllMethodsDeoptimized() const {
143 return interpreter_stubs_installed_;
144 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100145 bool ShouldNotifyMethodEnterExitEvents() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100146
147 // Executes everything with interpreter.
148 void DeoptimizeEverything()
149 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
150 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
151
152 // Executes everything with compiled code (or interpreter if there is no code).
153 void UndeoptimizeEverything()
154 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
155 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
156
157 // Deoptimize a method by forcing its execution with the interpreter. Nevertheless, a static
158 // method (except a class initializer) set to the resolution trampoline will be deoptimized only
159 // once its declaring class is initialized.
160 void Deoptimize(mirror::ArtMethod* method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700161 LOCKS_EXCLUDED(Locks::thread_list_lock_, deoptimized_methods_lock_)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100162 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
163
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.
167 void Undeoptimize(mirror::ArtMethod* method)
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100168 LOCKS_EXCLUDED(Locks::thread_list_lock_, deoptimized_methods_lock_)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100169 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
170
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700171 bool IsDeoptimized(mirror::ArtMethod* method)
172 LOCKS_EXCLUDED(deoptimized_methods_lock_)
173 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100174
175 // Enable method tracing by installing instrumentation entry/exit stubs.
176 void EnableMethodTracing()
177 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
178 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
179
180 // Disable method tracing by uninstalling instrumentation entry/exit stubs.
181 void DisableMethodTracing()
182 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
183 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
184
Sebastien Hertzed2be172014-08-19 15:33:43 +0200185 InterpreterHandlerTable GetInterpreterHandlerTable() const
186 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200187 return interpreter_handler_table_;
188 }
189
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700190 void InstrumentQuickAllocEntryPoints() LOCKS_EXCLUDED(Locks::instrument_entrypoints_lock_);
191 void UninstrumentQuickAllocEntryPoints() LOCKS_EXCLUDED(Locks::instrument_entrypoints_lock_);
192 void InstrumentQuickAllocEntryPointsLocked()
193 EXCLUSIVE_LOCKS_REQUIRED(Locks::instrument_entrypoints_lock_)
Jeff Hao69dbec62014-09-15 18:03:41 -0700194 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::runtime_shutdown_lock_);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700195 void UninstrumentQuickAllocEntryPointsLocked()
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 Chartierd8891782014-03-02 13:28:37 -0800198 void ResetQuickAllocEntryPoints() EXCLUSIVE_LOCKS_REQUIRED(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.
Elliott Hughes956af0f2014-12-11 14:34:28 -0800201 void UpdateMethodsCode(mirror::ArtMethod* method, const void* quick_code)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100202 SHARED_LOCKS_REQUIRED(Locks::mutator_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 Chartiera7dd0382014-11-20 17:08:58 -0800207 const void* GetQuickCodeFor(mirror::ArtMethod* method, size_t pointer_size) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800208 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
209
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
Sebastien Hertzed2be172014-08-19 15:33:43 +0200228 bool HasMethodEntryListeners() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200229 return have_method_entry_listeners_;
230 }
231
Sebastien Hertzed2be172014-08-19 15:33:43 +0200232 bool HasMethodExitListeners() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200233 return have_method_exit_listeners_;
234 }
235
Sebastien Hertzed2be172014-08-19 15:33:43 +0200236 bool HasDexPcListeners() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200237 return have_dex_pc_listeners_;
238 }
239
Sebastien Hertzed2be172014-08-19 15:33:43 +0200240 bool HasFieldReadListeners() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200241 return have_field_read_listeners_;
242 }
243
Sebastien Hertzed2be172014-08-19 15:33:43 +0200244 bool HasFieldWriteListeners() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200245 return have_field_write_listeners_;
246 }
247
Sebastien Hertzed2be172014-08-19 15:33:43 +0200248 bool HasExceptionCaughtListeners() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200249 return have_exception_caught_listeners_;
250 }
251
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800252 bool HasBackwardBranchListeners() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
253 return have_backward_branch_listeners_;
254 }
255
Sebastien Hertzed2be172014-08-19 15:33:43 +0200256 bool IsActive() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200257 return have_dex_pc_listeners_ || have_method_entry_listeners_ || have_method_exit_listeners_ ||
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200258 have_field_read_listeners_ || have_field_write_listeners_ ||
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200259 have_exception_caught_listeners_ || have_method_unwind_listeners_;
260 }
261
Ian Rogers62d6c772013-02-27 08:32:07 -0800262 // Inform listeners that a method has been entered. A dex PC is provided as we may install
263 // listeners into executing code and get method enter events for methods already on the stack.
264 void MethodEnterEvent(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800265 mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800266 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200267 if (UNLIKELY(HasMethodEntryListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800268 MethodEnterEventImpl(thread, this_object, method, dex_pc);
269 }
270 }
271
272 // Inform listeners that a method has been exited.
273 void MethodExitEvent(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800274 mirror::ArtMethod* method, uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800275 const JValue& return_value) const
276 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200277 if (UNLIKELY(HasMethodExitListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800278 MethodExitEventImpl(thread, this_object, method, dex_pc, return_value);
279 }
280 }
281
282 // Inform listeners that a method has been exited due to an exception.
283 void MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800284 mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800285 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
286
287 // Inform listeners that the dex pc has moved (only supported by the interpreter).
288 void DexPcMovedEvent(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800289 mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800290 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200291 if (UNLIKELY(HasDexPcListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800292 DexPcMovedEventImpl(thread, this_object, method, dex_pc);
293 }
294 }
295
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800296 // Inform listeners that a backward branch has been taken (only supported by the interpreter).
297 void BackwardBranch(Thread* thread, mirror::ArtMethod* method, int32_t offset) const
298 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
299 if (UNLIKELY(HasBackwardBranchListeners())) {
300 BackwardBranchImpl(thread, method, offset);
301 }
302 }
303
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200304 // Inform listeners that we read a field (only supported by the interpreter).
305 void FieldReadEvent(Thread* thread, mirror::Object* this_object,
306 mirror::ArtMethod* method, uint32_t dex_pc,
307 mirror::ArtField* field) const
308 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
309 if (UNLIKELY(HasFieldReadListeners())) {
310 FieldReadEventImpl(thread, this_object, method, dex_pc, field);
311 }
312 }
313
314 // Inform listeners that we write a field (only supported by the interpreter).
315 void FieldWriteEvent(Thread* thread, mirror::Object* this_object,
316 mirror::ArtMethod* method, uint32_t dex_pc,
317 mirror::ArtField* field, const JValue& field_value) const
318 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
319 if (UNLIKELY(HasFieldWriteListeners())) {
320 FieldWriteEventImpl(thread, this_object, method, dex_pc, field, field_value);
321 }
322 }
323
Ian Rogers62d6c772013-02-27 08:32:07 -0800324 // Inform listeners that an exception was caught.
325 void ExceptionCaughtEvent(Thread* thread, const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -0700326 mirror::ArtMethod* catch_method, uint32_t catch_dex_pc,
Sebastien Hertz947ff082013-09-17 14:10:13 +0200327 mirror::Throwable* exception_object) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800328 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
329
330 // Called when an instrumented method is entered. The intended link register (lr) is saved so
331 // that returning causes a branch to the method exit stub. Generates method enter events.
332 void PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700333 mirror::ArtMethod* method, uintptr_t lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700334 bool interpreter_entry)
Ian Rogers62d6c772013-02-27 08:32:07 -0800335 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
336
337 // Called when an instrumented method is exited. Removes the pushed instrumentation frame
338 // returning the intended link register. Generates method exit events.
Andreas Gamped58342c2014-06-05 14:18:08 -0700339 TwoWordReturn PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
340 uint64_t gpr_result, uint64_t fpr_result)
Ian Rogers62d6c772013-02-27 08:32:07 -0800341 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
342
343 // Pops an instrumentation frame from the current thread and generate an unwind event.
344 void PopMethodForUnwind(Thread* self, bool is_deoptimization) const
345 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
346
347 // Call back for configure stubs.
Sebastien Hertza10aa372015-01-21 17:30:58 +0100348 void InstallStubsForClass(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800349
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100350 void InstallStubsForMethod(mirror::ArtMethod* method)
351 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
352
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700353 void VisitRoots(RootCallback* callback, void* arg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
354 LOCKS_EXCLUDED(deoptimized_methods_lock_);
355
jeffhao725a9572012-11-13 18:20:12 -0800356 private:
Ian Rogers62d6c772013-02-27 08:32:07 -0800357 // Does the job of installing or removing instrumentation code within methods.
358 void ConfigureStubs(bool require_entry_exit_stubs, bool require_interpreter)
359 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700360 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_,
361 deoptimized_methods_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800362
Sebastien Hertzed2be172014-08-19 15:33:43 +0200363 void UpdateInterpreterHandlerTable() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200364 interpreter_handler_table_ = IsActive() ? kAlternativeHandlerTable : kMainHandlerTable;
365 }
366
Mathieu Chartier661974a2014-01-09 11:23:53 -0800367 // No thread safety analysis to get around SetQuickAllocEntryPointsInstrumented requiring
368 // exclusive access to mutator lock which you can't get if the runtime isn't started.
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700369 void SetEntrypointsInstrumented(bool instrumented) NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800370
Ian Rogers62d6c772013-02-27 08:32:07 -0800371 void MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800372 mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800373 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
374 void MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800375 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800376 uint32_t dex_pc, const JValue& return_value) const
377 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
378 void DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800379 mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800380 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800381 void BackwardBranchImpl(Thread* thread, mirror::ArtMethod* method, int32_t offset) const
382 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200383 void FieldReadEventImpl(Thread* thread, mirror::Object* this_object,
384 mirror::ArtMethod* method, uint32_t dex_pc,
385 mirror::ArtField* field) const
386 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
387 void FieldWriteEventImpl(Thread* thread, mirror::Object* this_object,
388 mirror::ArtMethod* method, uint32_t dex_pc,
389 mirror::ArtField* field, const JValue& field_value) const
390 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800391
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700392 // Read barrier-aware utility functions for accessing deoptimized_methods_
393 bool AddDeoptimizedMethod(mirror::ArtMethod* method)
394 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
395 EXCLUSIVE_LOCKS_REQUIRED(deoptimized_methods_lock_);
396 bool FindDeoptimizedMethod(mirror::ArtMethod* method)
397 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
398 SHARED_LOCKS_REQUIRED(deoptimized_methods_lock_);
399 bool RemoveDeoptimizedMethod(mirror::ArtMethod* method)
400 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
401 EXCLUSIVE_LOCKS_REQUIRED(deoptimized_methods_lock_);
402 mirror::ArtMethod* BeginDeoptimizedMethod()
403 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
404 SHARED_LOCKS_REQUIRED(deoptimized_methods_lock_);
405 bool IsDeoptimizedMethodsEmpty() const
406 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
407 SHARED_LOCKS_REQUIRED(deoptimized_methods_lock_);
408
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
Ian Rogers62d6c772013-02-27 08:32:07 -0800454 // The event listeners, written to with the mutator_lock_ exclusively held.
455 std::list<InstrumentationListener*> method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
456 std::list<InstrumentationListener*> method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
457 std::list<InstrumentationListener*> method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800458 std::list<InstrumentationListener*> backward_branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200459 std::shared_ptr<std::list<InstrumentationListener*>> dex_pc_listeners_
460 GUARDED_BY(Locks::mutator_lock_);
461 std::shared_ptr<std::list<InstrumentationListener*>> field_read_listeners_
462 GUARDED_BY(Locks::mutator_lock_);
463 std::shared_ptr<std::list<InstrumentationListener*>> field_write_listeners_
464 GUARDED_BY(Locks::mutator_lock_);
465 std::shared_ptr<std::list<InstrumentationListener*>> exception_caught_listeners_
466 GUARDED_BY(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800467
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100468 // The set of methods being deoptimized (by the debugger) which must be executed with interpreter
469 // only.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700470 mutable ReaderWriterMutex deoptimized_methods_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700471 std::multimap<int32_t, GcRoot<mirror::ArtMethod>> deoptimized_methods_
472 GUARDED_BY(deoptimized_methods_lock_);
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100473 bool deoptimization_enabled_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100474
Ian Rogersfa824272013-11-05 16:12:57 -0800475 // Current interpreter handler table. This is updated each time the thread state flags are
476 // modified.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200477 InterpreterHandlerTable interpreter_handler_table_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200478
Ian Rogersfa824272013-11-05 16:12:57 -0800479 // Greater than 0 if quick alloc entry points instrumented.
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700480 size_t quick_alloc_entry_points_instrumentation_counter_
481 GUARDED_BY(Locks::instrument_entrypoints_lock_);
Ian Rogersfa824272013-11-05 16:12:57 -0800482
jeffhao725a9572012-11-13 18:20:12 -0800483 DISALLOW_COPY_AND_ASSIGN(Instrumentation);
484};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700485std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationEvent& rhs);
jeffhao725a9572012-11-13 18:20:12 -0800486
Ian Rogers62d6c772013-02-27 08:32:07 -0800487// An element in the instrumentation side stack maintained in art::Thread.
488struct InstrumentationStackFrame {
Brian Carlstromea46f952013-07-30 01:26:50 -0700489 InstrumentationStackFrame(mirror::Object* this_object, mirror::ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -0700490 uintptr_t return_pc, size_t frame_id, bool interpreter_entry)
491 : this_object_(this_object), method_(method), return_pc_(return_pc), frame_id_(frame_id),
492 interpreter_entry_(interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800493 }
494
495 std::string Dump() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
496
497 mirror::Object* this_object_;
Brian Carlstromea46f952013-07-30 01:26:50 -0700498 mirror::ArtMethod* method_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100499 uintptr_t return_pc_;
500 size_t frame_id_;
501 bool interpreter_entry_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800502};
503
504} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -0800505} // namespace art
506
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700507#endif // ART_RUNTIME_INSTRUMENTATION_H_