blob: 77314c60c0f4492a9d0192643fe2eb7edb81b23e [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;
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
Ian Rogers62d6c772013-02-27 08:32:07 -080052// Instrumentation event listener API. Registered listeners will get the appropriate call back for
53// the events they are listening for. The call backs supply the thread, method and dex_pc the event
54// occurred upon. The thread may or may not be Thread::Current().
55struct InstrumentationListener {
56 InstrumentationListener() {}
57 virtual ~InstrumentationListener() {}
58
59 // Call-back for when a method is entered.
60 virtual void MethodEntered(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -080061 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -080062 uint32_t dex_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
63
64 // Call-back for when a method is exited.
65 // TODO: its likely passing the return value would be useful, however, we may need to get and
66 // parse the shorty to determine what kind of register holds the result.
67 virtual void MethodExited(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -080068 mirror::ArtMethod* method, uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -080069 const JValue& return_value)
70 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
71
72 // Call-back for when a method is popped due to an exception throw. A method will either cause a
73 // MethodExited call-back or a MethodUnwind call-back when its activation is removed.
Sebastien Hertz51db44a2013-11-19 10:00:29 +010074 virtual void MethodUnwind(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -080075 mirror::ArtMethod* method, uint32_t dex_pc)
Sebastien Hertz51db44a2013-11-19 10:00:29 +010076 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -080077
78 // Call-back for when the dex pc moves in a method.
79 virtual void DexPcMoved(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -080080 mirror::ArtMethod* method, uint32_t new_dex_pc)
Ian Rogers62d6c772013-02-27 08:32:07 -080081 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
82
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020083 // Call-back for when we read from a field.
84 virtual void FieldRead(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
85 uint32_t dex_pc, mirror::ArtField* field) = 0;
86
87 // Call-back for when we write into a field.
88 virtual void FieldWritten(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
89 uint32_t dex_pc, mirror::ArtField* field, const JValue& field_value) = 0;
90
Ian Rogers62d6c772013-02-27 08:32:07 -080091 // Call-back when an exception is caught.
Nicolas Geoffray14691c52015-03-05 10:40:17 +000092 virtual void ExceptionCaught(Thread* thread, mirror::Throwable* exception_object)
Ian Rogers62d6c772013-02-27 08:32:07 -080093 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080094
95 // Call-back for when we get a backward branch.
96 virtual void BackwardBranch(Thread* thread, mirror::ArtMethod* method, int32_t dex_pc_offset)
97 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
jeffhao725a9572012-11-13 18:20:12 -080098};
99
Ian Rogers62d6c772013-02-27 08:32:07 -0800100// Instrumentation is a catch-all for when extra information is required from the runtime. The
101// typical use for instrumentation is for profiling and debugging. Instrumentation may add stubs
102// to method entry and exit, it may also force execution to be switched to the interpreter and
103// trigger deoptimization.
jeffhao725a9572012-11-13 18:20:12 -0800104class Instrumentation {
105 public:
Ian Rogers62d6c772013-02-27 08:32:07 -0800106 enum InstrumentationEvent {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800107 kMethodEntered = 0x1,
108 kMethodExited = 0x2,
109 kMethodUnwind = 0x4,
110 kDexPcMoved = 0x8,
111 kFieldRead = 0x10,
112 kFieldWritten = 0x20,
113 kExceptionCaught = 0x40,
114 kBackwardBranch = 0x80,
Ian Rogers62d6c772013-02-27 08:32:07 -0800115 };
jeffhao725a9572012-11-13 18:20:12 -0800116
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700117 Instrumentation();
jeffhao725a9572012-11-13 18:20:12 -0800118
Ian Rogers62d6c772013-02-27 08:32:07 -0800119 // Add a listener to be notified of the masked together sent of instrumentation events. This
120 // suspend the runtime to install stubs. You are expected to hold the mutator lock as a proxy
121 // for saying you should have suspended all threads (installing stubs while threads are running
122 // will break).
123 void AddListener(InstrumentationListener* listener, uint32_t events)
124 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
125 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800126
Ian Rogers62d6c772013-02-27 08:32:07 -0800127 // Removes a listener possibly removing instrumentation stubs.
128 void RemoveListener(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
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100132 // Deoptimization.
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100133 void EnableDeoptimization()
134 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700135 LOCKS_EXCLUDED(deoptimized_methods_lock_);
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100136 void DisableDeoptimization()
137 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100138 LOCKS_EXCLUDED(deoptimized_methods_lock_);
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100139 bool AreAllMethodsDeoptimized() const {
140 return interpreter_stubs_installed_;
141 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100142 bool ShouldNotifyMethodEnterExitEvents() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100143
144 // Executes everything with interpreter.
145 void DeoptimizeEverything()
146 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
147 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
148
149 // Executes everything with compiled code (or interpreter if there is no code).
150 void UndeoptimizeEverything()
151 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
152 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
153
154 // Deoptimize a method by forcing its execution with the interpreter. Nevertheless, a static
155 // method (except a class initializer) set to the resolution trampoline will be deoptimized only
156 // once its declaring class is initialized.
157 void Deoptimize(mirror::ArtMethod* method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700158 LOCKS_EXCLUDED(Locks::thread_list_lock_, deoptimized_methods_lock_)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100159 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
160
161 // Undeoptimze the method by restoring its entrypoints. Nevertheless, a static method
162 // (except a class initializer) set to the resolution trampoline will be updated only once its
163 // declaring class is initialized.
164 void Undeoptimize(mirror::ArtMethod* method)
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100165 LOCKS_EXCLUDED(Locks::thread_list_lock_, deoptimized_methods_lock_)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100166 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
167
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700168 bool IsDeoptimized(mirror::ArtMethod* method)
169 LOCKS_EXCLUDED(deoptimized_methods_lock_)
170 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100171
172 // Enable method tracing by installing instrumentation entry/exit stubs.
173 void EnableMethodTracing()
174 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
175 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
176
177 // Disable method tracing by uninstalling instrumentation entry/exit stubs.
178 void DisableMethodTracing()
179 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
180 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
181
Sebastien Hertzed2be172014-08-19 15:33:43 +0200182 InterpreterHandlerTable GetInterpreterHandlerTable() const
183 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200184 return interpreter_handler_table_;
185 }
186
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700187 void InstrumentQuickAllocEntryPoints() LOCKS_EXCLUDED(Locks::instrument_entrypoints_lock_);
188 void UninstrumentQuickAllocEntryPoints() LOCKS_EXCLUDED(Locks::instrument_entrypoints_lock_);
189 void InstrumentQuickAllocEntryPointsLocked()
190 EXCLUSIVE_LOCKS_REQUIRED(Locks::instrument_entrypoints_lock_)
Jeff Hao69dbec62014-09-15 18:03:41 -0700191 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::runtime_shutdown_lock_);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700192 void UninstrumentQuickAllocEntryPointsLocked()
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 Chartierd8891782014-03-02 13:28:37 -0800195 void ResetQuickAllocEntryPoints() EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_);
Ian Rogersfa824272013-11-05 16:12:57 -0800196
Ian Rogers62d6c772013-02-27 08:32:07 -0800197 // Update the code of a method respecting any installed stubs.
Elliott Hughes956af0f2014-12-11 14:34:28 -0800198 void UpdateMethodsCode(mirror::ArtMethod* method, const void* quick_code)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100199 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800200
201 // Get the quick code for the given method. More efficient than asking the class linker as it
202 // will short-cut to GetCode if instrumentation and static method resolution stubs aren't
203 // installed.
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800204 const void* GetQuickCodeFor(mirror::ArtMethod* method, size_t pointer_size) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800205 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
206
207 void ForceInterpretOnly() {
208 interpret_only_ = true;
209 forced_interpret_only_ = true;
210 }
211
Brian Carlstromea46f952013-07-30 01:26:50 -0700212 // Called by ArtMethod::Invoke to determine dispatch mechanism.
Ian Rogers62d6c772013-02-27 08:32:07 -0800213 bool InterpretOnly() const {
214 return interpret_only_;
215 }
216
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800217 bool IsForcedInterpretOnly() const {
218 return forced_interpret_only_;
219 }
220
Ian Rogers62d6c772013-02-27 08:32:07 -0800221 bool AreExitStubsInstalled() const {
222 return instrumentation_stubs_installed_;
223 }
224
Sebastien Hertzed2be172014-08-19 15:33:43 +0200225 bool HasMethodEntryListeners() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200226 return have_method_entry_listeners_;
227 }
228
Sebastien Hertzed2be172014-08-19 15:33:43 +0200229 bool HasMethodExitListeners() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200230 return have_method_exit_listeners_;
231 }
232
Sebastien Hertzed2be172014-08-19 15:33:43 +0200233 bool HasDexPcListeners() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200234 return have_dex_pc_listeners_;
235 }
236
Sebastien Hertzed2be172014-08-19 15:33:43 +0200237 bool HasFieldReadListeners() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200238 return have_field_read_listeners_;
239 }
240
Sebastien Hertzed2be172014-08-19 15:33:43 +0200241 bool HasFieldWriteListeners() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200242 return have_field_write_listeners_;
243 }
244
Sebastien Hertzed2be172014-08-19 15:33:43 +0200245 bool HasExceptionCaughtListeners() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200246 return have_exception_caught_listeners_;
247 }
248
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800249 bool HasBackwardBranchListeners() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
250 return have_backward_branch_listeners_;
251 }
252
Sebastien Hertzed2be172014-08-19 15:33:43 +0200253 bool IsActive() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200254 return have_dex_pc_listeners_ || have_method_entry_listeners_ || have_method_exit_listeners_ ||
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200255 have_field_read_listeners_ || have_field_write_listeners_ ||
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200256 have_exception_caught_listeners_ || have_method_unwind_listeners_;
257 }
258
Ian Rogers62d6c772013-02-27 08:32:07 -0800259 // Inform listeners that a method has been entered. A dex PC is provided as we may install
260 // listeners into executing code and get method enter events for methods already on the stack.
261 void MethodEnterEvent(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800262 mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800263 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200264 if (UNLIKELY(HasMethodEntryListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800265 MethodEnterEventImpl(thread, this_object, method, dex_pc);
266 }
267 }
268
269 // Inform listeners that a method has been exited.
270 void MethodExitEvent(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800271 mirror::ArtMethod* method, uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800272 const JValue& return_value) const
273 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200274 if (UNLIKELY(HasMethodExitListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800275 MethodExitEventImpl(thread, this_object, method, dex_pc, return_value);
276 }
277 }
278
279 // Inform listeners that a method has been exited due to an exception.
280 void MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800281 mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800282 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
283
284 // Inform listeners that the dex pc has moved (only supported by the interpreter).
285 void DexPcMovedEvent(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800286 mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800287 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200288 if (UNLIKELY(HasDexPcListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800289 DexPcMovedEventImpl(thread, this_object, method, dex_pc);
290 }
291 }
292
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800293 // Inform listeners that a backward branch has been taken (only supported by the interpreter).
294 void BackwardBranch(Thread* thread, mirror::ArtMethod* method, int32_t offset) const
295 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
296 if (UNLIKELY(HasBackwardBranchListeners())) {
297 BackwardBranchImpl(thread, method, offset);
298 }
299 }
300
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200301 // Inform listeners that we read a field (only supported by the interpreter).
302 void FieldReadEvent(Thread* thread, mirror::Object* this_object,
303 mirror::ArtMethod* method, uint32_t dex_pc,
304 mirror::ArtField* field) const
305 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
306 if (UNLIKELY(HasFieldReadListeners())) {
307 FieldReadEventImpl(thread, this_object, method, dex_pc, field);
308 }
309 }
310
311 // Inform listeners that we write a field (only supported by the interpreter).
312 void FieldWriteEvent(Thread* thread, mirror::Object* this_object,
313 mirror::ArtMethod* method, uint32_t dex_pc,
314 mirror::ArtField* field, const JValue& field_value) const
315 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
316 if (UNLIKELY(HasFieldWriteListeners())) {
317 FieldWriteEventImpl(thread, this_object, method, dex_pc, field, field_value);
318 }
319 }
320
Ian Rogers62d6c772013-02-27 08:32:07 -0800321 // Inform listeners that an exception was caught.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000322 void ExceptionCaughtEvent(Thread* thread, mirror::Throwable* exception_object) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800323 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
324
325 // Called when an instrumented method is entered. The intended link register (lr) is saved so
326 // that returning causes a branch to the method exit stub. Generates method enter events.
327 void PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700328 mirror::ArtMethod* method, uintptr_t lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700329 bool interpreter_entry)
Ian Rogers62d6c772013-02-27 08:32:07 -0800330 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
331
332 // Called when an instrumented method is exited. Removes the pushed instrumentation frame
333 // returning the intended link register. Generates method exit events.
Andreas Gamped58342c2014-06-05 14:18:08 -0700334 TwoWordReturn PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
335 uint64_t gpr_result, uint64_t fpr_result)
Ian Rogers62d6c772013-02-27 08:32:07 -0800336 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
337
338 // Pops an instrumentation frame from the current thread and generate an unwind event.
339 void PopMethodForUnwind(Thread* self, bool is_deoptimization) const
340 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
341
342 // Call back for configure stubs.
Sebastien Hertza10aa372015-01-21 17:30:58 +0100343 void InstallStubsForClass(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800344
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100345 void InstallStubsForMethod(mirror::ArtMethod* method)
346 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
347
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700348 void VisitRoots(RootVisitor* visitor) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700349 LOCKS_EXCLUDED(deoptimized_methods_lock_);
350
jeffhao725a9572012-11-13 18:20:12 -0800351 private:
Ian Rogers62d6c772013-02-27 08:32:07 -0800352 // Does the job of installing or removing instrumentation code within methods.
353 void ConfigureStubs(bool require_entry_exit_stubs, bool require_interpreter)
354 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700355 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_,
356 deoptimized_methods_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800357
Sebastien Hertzed2be172014-08-19 15:33:43 +0200358 void UpdateInterpreterHandlerTable() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200359 interpreter_handler_table_ = IsActive() ? kAlternativeHandlerTable : kMainHandlerTable;
360 }
361
Mathieu Chartier661974a2014-01-09 11:23:53 -0800362 // No thread safety analysis to get around SetQuickAllocEntryPointsInstrumented requiring
363 // exclusive access to mutator lock which you can't get if the runtime isn't started.
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700364 void SetEntrypointsInstrumented(bool instrumented) NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800365
Ian Rogers62d6c772013-02-27 08:32:07 -0800366 void MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800367 mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800368 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
369 void MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800370 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800371 uint32_t dex_pc, const JValue& return_value) const
372 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
373 void DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800374 mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800375 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800376 void BackwardBranchImpl(Thread* thread, mirror::ArtMethod* method, int32_t offset) const
377 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200378 void FieldReadEventImpl(Thread* thread, mirror::Object* this_object,
379 mirror::ArtMethod* method, uint32_t dex_pc,
380 mirror::ArtField* field) const
381 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
382 void FieldWriteEventImpl(Thread* thread, mirror::Object* this_object,
383 mirror::ArtMethod* method, uint32_t dex_pc,
384 mirror::ArtField* field, const JValue& field_value) const
385 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800386
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700387 // Read barrier-aware utility functions for accessing deoptimized_methods_
388 bool AddDeoptimizedMethod(mirror::ArtMethod* method)
389 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
390 EXCLUSIVE_LOCKS_REQUIRED(deoptimized_methods_lock_);
391 bool FindDeoptimizedMethod(mirror::ArtMethod* method)
392 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
393 SHARED_LOCKS_REQUIRED(deoptimized_methods_lock_);
394 bool RemoveDeoptimizedMethod(mirror::ArtMethod* method)
395 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
396 EXCLUSIVE_LOCKS_REQUIRED(deoptimized_methods_lock_);
397 mirror::ArtMethod* BeginDeoptimizedMethod()
398 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
399 SHARED_LOCKS_REQUIRED(deoptimized_methods_lock_);
400 bool IsDeoptimizedMethodsEmpty() const
401 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
402 SHARED_LOCKS_REQUIRED(deoptimized_methods_lock_);
403
Brian Carlstromea46f952013-07-30 01:26:50 -0700404 // Have we hijacked ArtMethod::code_ so that it calls instrumentation/interpreter code?
Ian Rogers62d6c772013-02-27 08:32:07 -0800405 bool instrumentation_stubs_installed_;
406
Brian Carlstromea46f952013-07-30 01:26:50 -0700407 // Have we hijacked ArtMethod::code_ to reference the enter/exit stubs?
Ian Rogers62d6c772013-02-27 08:32:07 -0800408 bool entry_exit_stubs_installed_;
409
Brian Carlstromea46f952013-07-30 01:26:50 -0700410 // Have we hijacked ArtMethod::code_ to reference the enter interpreter stub?
Ian Rogers62d6c772013-02-27 08:32:07 -0800411 bool interpreter_stubs_installed_;
412
413 // Do we need the fidelity of events that we only get from running within the interpreter?
414 bool interpret_only_;
415
416 // Did the runtime request we only run in the interpreter? ie -Xint mode.
417 bool forced_interpret_only_;
418
419 // Do we have any listeners for method entry events? Short-cut to avoid taking the
420 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200421 bool have_method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800422
423 // Do we have any listeners for method exit events? Short-cut to avoid taking the
424 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200425 bool have_method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800426
427 // Do we have any listeners for method unwind events? Short-cut to avoid taking the
428 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200429 bool have_method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800430
431 // Do we have any listeners for dex move events? Short-cut to avoid taking the
432 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200433 bool have_dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800434
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200435 // Do we have any listeners for field read events? Short-cut to avoid taking the
436 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200437 bool have_field_read_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200438
439 // Do we have any listeners for field write events? Short-cut to avoid taking the
440 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200441 bool have_field_write_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200442
Ian Rogers62d6c772013-02-27 08:32:07 -0800443 // Do we have any exception caught listeners? Short-cut to avoid taking the instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200444 bool have_exception_caught_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800445
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800446 // Do we have any backward branch listeners? Short-cut to avoid taking the instrumentation_lock_.
447 bool have_backward_branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
448
Ian Rogers62d6c772013-02-27 08:32:07 -0800449 // The event listeners, written to with the mutator_lock_ exclusively held.
450 std::list<InstrumentationListener*> method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
451 std::list<InstrumentationListener*> method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
452 std::list<InstrumentationListener*> method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800453 std::list<InstrumentationListener*> backward_branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200454 std::shared_ptr<std::list<InstrumentationListener*>> dex_pc_listeners_
455 GUARDED_BY(Locks::mutator_lock_);
456 std::shared_ptr<std::list<InstrumentationListener*>> field_read_listeners_
457 GUARDED_BY(Locks::mutator_lock_);
458 std::shared_ptr<std::list<InstrumentationListener*>> field_write_listeners_
459 GUARDED_BY(Locks::mutator_lock_);
460 std::shared_ptr<std::list<InstrumentationListener*>> exception_caught_listeners_
461 GUARDED_BY(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800462
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100463 // The set of methods being deoptimized (by the debugger) which must be executed with interpreter
464 // only.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700465 mutable ReaderWriterMutex deoptimized_methods_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700466 std::multimap<int32_t, GcRoot<mirror::ArtMethod>> deoptimized_methods_
467 GUARDED_BY(deoptimized_methods_lock_);
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100468 bool deoptimization_enabled_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100469
Ian Rogersfa824272013-11-05 16:12:57 -0800470 // Current interpreter handler table. This is updated each time the thread state flags are
471 // modified.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200472 InterpreterHandlerTable interpreter_handler_table_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200473
Ian Rogersfa824272013-11-05 16:12:57 -0800474 // Greater than 0 if quick alloc entry points instrumented.
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700475 size_t quick_alloc_entry_points_instrumentation_counter_
476 GUARDED_BY(Locks::instrument_entrypoints_lock_);
Ian Rogersfa824272013-11-05 16:12:57 -0800477
jeffhao725a9572012-11-13 18:20:12 -0800478 DISALLOW_COPY_AND_ASSIGN(Instrumentation);
479};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700480std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationEvent& rhs);
jeffhao725a9572012-11-13 18:20:12 -0800481
Ian Rogers62d6c772013-02-27 08:32:07 -0800482// An element in the instrumentation side stack maintained in art::Thread.
483struct InstrumentationStackFrame {
Brian Carlstromea46f952013-07-30 01:26:50 -0700484 InstrumentationStackFrame(mirror::Object* this_object, mirror::ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -0700485 uintptr_t return_pc, size_t frame_id, bool interpreter_entry)
486 : this_object_(this_object), method_(method), return_pc_(return_pc), frame_id_(frame_id),
487 interpreter_entry_(interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800488 }
489
490 std::string Dump() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
491
492 mirror::Object* this_object_;
Brian Carlstromea46f952013-07-30 01:26:50 -0700493 mirror::ArtMethod* method_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100494 uintptr_t return_pc_;
495 size_t frame_id_;
496 bool interpreter_entry_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800497};
498
499} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -0800500} // namespace art
501
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700502#endif // ART_RUNTIME_INSTRUMENTATION_H_