blob: 2dd2cd7de8b31cb33b47138720f3c904c31776d7 [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>
21#include <set>
22#include <list>
23
Ian Rogersef7d42f2014-01-06 12:55:46 -080024#include "atomic.h"
Elliott Hughes76160052012-12-12 16:31:20 -080025#include "base/macros.h"
Ian Rogers719d1a32014-03-06 12:13:39 -080026#include "base/mutex.h"
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070027#include "object_callbacks.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028
jeffhao725a9572012-11-13 18:20:12 -080029namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030namespace mirror {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020031 class ArtField;
Brian Carlstromea46f952013-07-30 01:26:50 -070032 class ArtMethod;
33 class Class;
34 class Object;
35 class Throwable;
Ian Rogers62d6c772013-02-27 08:32:07 -080036} // namespace mirror
37union JValue;
jeffhao725a9572012-11-13 18:20:12 -080038class Thread;
Ian Rogers62d6c772013-02-27 08:32:07 -080039class ThrowLocation;
jeffhao725a9572012-11-13 18:20:12 -080040
Ian Rogers62d6c772013-02-27 08:32:07 -080041namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080042
Sebastien Hertzee1997a2013-09-19 14:47:09 +020043// Interpreter handler tables.
44enum InterpreterHandlerTable {
45 kMainHandlerTable = 0, // Main handler table: no suspend check, no instrumentation.
46 kAlternativeHandlerTable = 1, // Alternative handler table: suspend check and/or instrumentation
47 // enabled.
48 kNumHandlerTables
49};
50
Ian Rogers62d6c772013-02-27 08:32:07 -080051// Instrumentation event listener API. Registered listeners will get the appropriate call back for
52// the events they are listening for. The call backs supply the thread, method and dex_pc the event
53// occurred upon. The thread may or may not be Thread::Current().
54struct InstrumentationListener {
55 InstrumentationListener() {}
56 virtual ~InstrumentationListener() {}
57
58 // Call-back for when a method is entered.
59 virtual void MethodEntered(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -080060 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -080061 uint32_t dex_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
62
63 // Call-back for when a method is exited.
64 // TODO: its likely passing the return value would be useful, however, we may need to get and
65 // parse the shorty to determine what kind of register holds the result.
66 virtual void MethodExited(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -080067 mirror::ArtMethod* method, uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -080068 const JValue& return_value)
69 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
70
71 // Call-back for when a method is popped due to an exception throw. A method will either cause a
72 // MethodExited call-back or a MethodUnwind call-back when its activation is removed.
Sebastien Hertz51db44a2013-11-19 10:00:29 +010073 virtual void MethodUnwind(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -080074 mirror::ArtMethod* method, uint32_t dex_pc)
Sebastien Hertz51db44a2013-11-19 10:00:29 +010075 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -080076
77 // Call-back for when the dex pc moves in a method.
78 virtual void DexPcMoved(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -080079 mirror::ArtMethod* method, uint32_t new_dex_pc)
Ian Rogers62d6c772013-02-27 08:32:07 -080080 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
81
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020082 // Call-back for when we read from a field.
83 virtual void FieldRead(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
84 uint32_t dex_pc, mirror::ArtField* field) = 0;
85
86 // Call-back for when we write into a field.
87 virtual void FieldWritten(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
88 uint32_t dex_pc, mirror::ArtField* field, const JValue& field_value) = 0;
89
Ian Rogers62d6c772013-02-27 08:32:07 -080090 // Call-back when an exception is caught.
91 virtual void ExceptionCaught(Thread* thread, const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -070092 mirror::ArtMethod* catch_method, uint32_t catch_dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -080093 mirror::Throwable* exception_object)
94 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
jeffhao725a9572012-11-13 18:20:12 -080095};
96
Ian Rogers62d6c772013-02-27 08:32:07 -080097// Instrumentation is a catch-all for when extra information is required from the runtime. The
98// typical use for instrumentation is for profiling and debugging. Instrumentation may add stubs
99// to method entry and exit, it may also force execution to be switched to the interpreter and
100// trigger deoptimization.
jeffhao725a9572012-11-13 18:20:12 -0800101class Instrumentation {
102 public:
Ian Rogers62d6c772013-02-27 08:32:07 -0800103 enum InstrumentationEvent {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200104 kMethodEntered = 1 << 0,
105 kMethodExited = 1 << 1,
106 kMethodUnwind = 1 << 2,
107 kDexPcMoved = 1 << 3,
108 kFieldRead = 1 << 4,
109 kFieldWritten = 1 << 5,
110 kExceptionCaught = 1 << 6,
Ian Rogers62d6c772013-02-27 08:32:07 -0800111 };
jeffhao725a9572012-11-13 18:20:12 -0800112
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700113 Instrumentation();
jeffhao725a9572012-11-13 18:20:12 -0800114
Ian Rogers62d6c772013-02-27 08:32:07 -0800115 // Add a listener to be notified of the masked together sent of instrumentation events. This
116 // suspend the runtime to install stubs. You are expected to hold the mutator lock as a proxy
117 // for saying you should have suspended all threads (installing stubs while threads are running
118 // will break).
119 void AddListener(InstrumentationListener* listener, uint32_t events)
120 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
121 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800122
Ian Rogers62d6c772013-02-27 08:32:07 -0800123 // Removes a listener possibly removing instrumentation stubs.
124 void RemoveListener(InstrumentationListener* listener, uint32_t events)
125 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
126 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800127
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100128 // Deoptimization.
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100129 void EnableDeoptimization()
130 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700131 LOCKS_EXCLUDED(deoptimized_methods_lock_);
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100132 void DisableDeoptimization()
133 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100134 LOCKS_EXCLUDED(deoptimized_methods_lock_);
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100135 bool AreAllMethodsDeoptimized() const {
136 return interpreter_stubs_installed_;
137 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100138 bool ShouldNotifyMethodEnterExitEvents() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100139
140 // Executes everything with interpreter.
141 void DeoptimizeEverything()
142 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
143 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
144
145 // Executes everything with compiled code (or interpreter if there is no code).
146 void UndeoptimizeEverything()
147 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
148 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
149
150 // Deoptimize a method by forcing its execution with the interpreter. Nevertheless, a static
151 // method (except a class initializer) set to the resolution trampoline will be deoptimized only
152 // once its declaring class is initialized.
153 void Deoptimize(mirror::ArtMethod* method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700154 LOCKS_EXCLUDED(Locks::thread_list_lock_, deoptimized_methods_lock_)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100155 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
156
157 // Undeoptimze the method by restoring its entrypoints. Nevertheless, a static method
158 // (except a class initializer) set to the resolution trampoline will be updated only once its
159 // declaring class is initialized.
160 void Undeoptimize(mirror::ArtMethod* method)
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100161 LOCKS_EXCLUDED(Locks::thread_list_lock_, deoptimized_methods_lock_)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100162 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
163
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700164 bool IsDeoptimized(mirror::ArtMethod* method) const LOCKS_EXCLUDED(deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100165
166 // Enable method tracing by installing instrumentation entry/exit stubs.
167 void EnableMethodTracing()
168 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
169 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
170
171 // Disable method tracing by uninstalling instrumentation entry/exit stubs.
172 void DisableMethodTracing()
173 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
174 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
175
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200176 InterpreterHandlerTable GetInterpreterHandlerTable() const {
177 return interpreter_handler_table_;
178 }
179
Mathieu Chartierd8891782014-03-02 13:28:37 -0800180 void InstrumentQuickAllocEntryPoints() LOCKS_EXCLUDED(Locks::thread_list_lock_,
181 Locks::runtime_shutdown_lock_);
182 void UninstrumentQuickAllocEntryPoints() LOCKS_EXCLUDED(Locks::thread_list_lock_,
183 Locks::runtime_shutdown_lock_);
184 void ResetQuickAllocEntryPoints() EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_);
Ian Rogersfa824272013-11-05 16:12:57 -0800185
Ian Rogers62d6c772013-02-27 08:32:07 -0800186 // Update the code of a method respecting any installed stubs.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800187 void UpdateMethodsCode(mirror::ArtMethod* method, const void* quick_code,
188 const void* portable_code, bool have_portable_code) const
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100189 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800190
191 // Get the quick code for the given method. More efficient than asking the class linker as it
192 // will short-cut to GetCode if instrumentation and static method resolution stubs aren't
193 // installed.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800194 const void* GetQuickCodeFor(mirror::ArtMethod* method) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800195 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
196
197 void ForceInterpretOnly() {
198 interpret_only_ = true;
199 forced_interpret_only_ = true;
200 }
201
Brian Carlstromea46f952013-07-30 01:26:50 -0700202 // Called by ArtMethod::Invoke to determine dispatch mechanism.
Ian Rogers62d6c772013-02-27 08:32:07 -0800203 bool InterpretOnly() const {
204 return interpret_only_;
205 }
206
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800207 bool IsForcedInterpretOnly() const {
208 return forced_interpret_only_;
209 }
210
Ian Rogers62d6c772013-02-27 08:32:07 -0800211 bool ShouldPortableCodeDeoptimize() const {
212 return instrumentation_stubs_installed_;
213 }
214
215 bool AreExitStubsInstalled() const {
216 return instrumentation_stubs_installed_;
217 }
218
Sebastien Hertz74109f62013-06-07 17:40:09 +0200219 bool HasMethodEntryListeners() const {
220 return have_method_entry_listeners_;
221 }
222
223 bool HasMethodExitListeners() const {
224 return have_method_exit_listeners_;
225 }
226
227 bool HasDexPcListeners() const {
228 return have_dex_pc_listeners_;
229 }
230
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200231 bool HasFieldReadListeners() const {
232 return have_field_read_listeners_;
233 }
234
235 bool HasFieldWriteListeners() const {
236 return have_field_write_listeners_;
237 }
238
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200239 bool IsActive() const {
240 return have_dex_pc_listeners_ || have_method_entry_listeners_ || have_method_exit_listeners_ ||
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200241 have_field_read_listeners_ || have_field_write_listeners_ ||
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200242 have_exception_caught_listeners_ || have_method_unwind_listeners_;
243 }
244
Ian Rogers62d6c772013-02-27 08:32:07 -0800245 // Inform listeners that a method has been entered. A dex PC is provided as we may install
246 // listeners into executing code and get method enter events for methods already on the stack.
247 void MethodEnterEvent(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800248 mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800249 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200250 if (UNLIKELY(HasMethodEntryListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800251 MethodEnterEventImpl(thread, this_object, method, dex_pc);
252 }
253 }
254
255 // Inform listeners that a method has been exited.
256 void MethodExitEvent(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800257 mirror::ArtMethod* method, uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800258 const JValue& return_value) const
259 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200260 if (UNLIKELY(HasMethodExitListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800261 MethodExitEventImpl(thread, this_object, method, dex_pc, return_value);
262 }
263 }
264
265 // Inform listeners that a method has been exited due to an exception.
266 void MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800267 mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800268 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
269
270 // Inform listeners that the dex pc has moved (only supported by the interpreter).
271 void DexPcMovedEvent(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800272 mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800273 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200274 if (UNLIKELY(HasDexPcListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800275 DexPcMovedEventImpl(thread, this_object, method, dex_pc);
276 }
277 }
278
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200279 // Inform listeners that we read a field (only supported by the interpreter).
280 void FieldReadEvent(Thread* thread, mirror::Object* this_object,
281 mirror::ArtMethod* method, uint32_t dex_pc,
282 mirror::ArtField* field) const
283 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
284 if (UNLIKELY(HasFieldReadListeners())) {
285 FieldReadEventImpl(thread, this_object, method, dex_pc, field);
286 }
287 }
288
289 // Inform listeners that we write a field (only supported by the interpreter).
290 void FieldWriteEvent(Thread* thread, mirror::Object* this_object,
291 mirror::ArtMethod* method, uint32_t dex_pc,
292 mirror::ArtField* field, const JValue& field_value) const
293 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
294 if (UNLIKELY(HasFieldWriteListeners())) {
295 FieldWriteEventImpl(thread, this_object, method, dex_pc, field, field_value);
296 }
297 }
298
Ian Rogers62d6c772013-02-27 08:32:07 -0800299 // Inform listeners that an exception was caught.
300 void ExceptionCaughtEvent(Thread* thread, const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -0700301 mirror::ArtMethod* catch_method, uint32_t catch_dex_pc,
Sebastien Hertz947ff082013-09-17 14:10:13 +0200302 mirror::Throwable* exception_object) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800303 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
304
305 // Called when an instrumented method is entered. The intended link register (lr) is saved so
306 // that returning causes a branch to the method exit stub. Generates method enter events.
307 void PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700308 mirror::ArtMethod* method, uintptr_t lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700309 bool interpreter_entry)
Ian Rogers62d6c772013-02-27 08:32:07 -0800310 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
311
312 // Called when an instrumented method is exited. Removes the pushed instrumentation frame
313 // returning the intended link register. Generates method exit events.
314 uint64_t PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc, uint64_t gpr_result,
315 uint64_t fpr_result)
316 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
317
318 // Pops an instrumentation frame from the current thread and generate an unwind event.
319 void PopMethodForUnwind(Thread* self, bool is_deoptimization) const
320 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
321
322 // Call back for configure stubs.
323 bool InstallStubsForClass(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800324
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100325 void InstallStubsForMethod(mirror::ArtMethod* method)
326 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
327
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700328 void VisitRoots(RootCallback* callback, void* arg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
329 LOCKS_EXCLUDED(deoptimized_methods_lock_);
330
jeffhao725a9572012-11-13 18:20:12 -0800331 private:
Ian Rogers62d6c772013-02-27 08:32:07 -0800332 // Does the job of installing or removing instrumentation code within methods.
333 void ConfigureStubs(bool require_entry_exit_stubs, bool require_interpreter)
334 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700335 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_,
336 deoptimized_methods_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800337
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200338 void UpdateInterpreterHandlerTable() {
339 interpreter_handler_table_ = IsActive() ? kAlternativeHandlerTable : kMainHandlerTable;
340 }
341
Mathieu Chartier661974a2014-01-09 11:23:53 -0800342 // No thread safety analysis to get around SetQuickAllocEntryPointsInstrumented requiring
343 // exclusive access to mutator lock which you can't get if the runtime isn't started.
344 void SetEntrypointsInstrumented(bool instrumented) NO_THREAD_SAFETY_ANALYSIS;
345
Ian Rogers62d6c772013-02-27 08:32:07 -0800346 void MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800347 mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800348 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
349 void MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800350 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800351 uint32_t dex_pc, const JValue& return_value) const
352 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
353 void DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800354 mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800355 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200356 void FieldReadEventImpl(Thread* thread, mirror::Object* this_object,
357 mirror::ArtMethod* method, uint32_t dex_pc,
358 mirror::ArtField* field) const
359 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
360 void FieldWriteEventImpl(Thread* thread, mirror::Object* this_object,
361 mirror::ArtMethod* method, uint32_t dex_pc,
362 mirror::ArtField* field, const JValue& field_value) const
363 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800364
Brian Carlstromea46f952013-07-30 01:26:50 -0700365 // Have we hijacked ArtMethod::code_ so that it calls instrumentation/interpreter code?
Ian Rogers62d6c772013-02-27 08:32:07 -0800366 bool instrumentation_stubs_installed_;
367
Brian Carlstromea46f952013-07-30 01:26:50 -0700368 // Have we hijacked ArtMethod::code_ to reference the enter/exit stubs?
Ian Rogers62d6c772013-02-27 08:32:07 -0800369 bool entry_exit_stubs_installed_;
370
Brian Carlstromea46f952013-07-30 01:26:50 -0700371 // Have we hijacked ArtMethod::code_ to reference the enter interpreter stub?
Ian Rogers62d6c772013-02-27 08:32:07 -0800372 bool interpreter_stubs_installed_;
373
374 // Do we need the fidelity of events that we only get from running within the interpreter?
375 bool interpret_only_;
376
377 // Did the runtime request we only run in the interpreter? ie -Xint mode.
378 bool forced_interpret_only_;
379
380 // Do we have any listeners for method entry events? Short-cut to avoid taking the
381 // instrumentation_lock_.
382 bool have_method_entry_listeners_;
383
384 // Do we have any listeners for method exit events? Short-cut to avoid taking the
385 // instrumentation_lock_.
386 bool have_method_exit_listeners_;
387
388 // Do we have any listeners for method unwind events? Short-cut to avoid taking the
389 // instrumentation_lock_.
390 bool have_method_unwind_listeners_;
391
392 // Do we have any listeners for dex move events? Short-cut to avoid taking the
393 // instrumentation_lock_.
394 bool have_dex_pc_listeners_;
395
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200396 // Do we have any listeners for field read events? Short-cut to avoid taking the
397 // instrumentation_lock_.
398 bool have_field_read_listeners_;
399
400 // Do we have any listeners for field write events? Short-cut to avoid taking the
401 // instrumentation_lock_.
402 bool have_field_write_listeners_;
403
Ian Rogers62d6c772013-02-27 08:32:07 -0800404 // Do we have any exception caught listeners? Short-cut to avoid taking the instrumentation_lock_.
405 bool have_exception_caught_listeners_;
406
407 // The event listeners, written to with the mutator_lock_ exclusively held.
408 std::list<InstrumentationListener*> method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
409 std::list<InstrumentationListener*> method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
410 std::list<InstrumentationListener*> method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
411 std::list<InstrumentationListener*> dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200412 std::list<InstrumentationListener*> field_read_listeners_ GUARDED_BY(Locks::mutator_lock_);
413 std::list<InstrumentationListener*> field_write_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800414 std::list<InstrumentationListener*> exception_caught_listeners_ GUARDED_BY(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800415
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100416 // The set of methods being deoptimized (by the debugger) which must be executed with interpreter
417 // only.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700418 mutable ReaderWriterMutex deoptimized_methods_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
419 std::set<mirror::ArtMethod*> deoptimized_methods_ GUARDED_BY(deoptimized_methods_lock_);
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100420 bool deoptimization_enabled_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100421
Ian Rogersfa824272013-11-05 16:12:57 -0800422 // Current interpreter handler table. This is updated each time the thread state flags are
423 // modified.
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200424 InterpreterHandlerTable interpreter_handler_table_;
425
Ian Rogersfa824272013-11-05 16:12:57 -0800426 // Greater than 0 if quick alloc entry points instrumented.
427 // TODO: The access and changes to this is racy and should be guarded by a lock.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800428 AtomicInteger quick_alloc_entry_points_instrumentation_counter_;
Ian Rogersfa824272013-11-05 16:12:57 -0800429
jeffhao725a9572012-11-13 18:20:12 -0800430 DISALLOW_COPY_AND_ASSIGN(Instrumentation);
431};
432
Ian Rogers62d6c772013-02-27 08:32:07 -0800433// An element in the instrumentation side stack maintained in art::Thread.
434struct InstrumentationStackFrame {
Brian Carlstromea46f952013-07-30 01:26:50 -0700435 InstrumentationStackFrame(mirror::Object* this_object, mirror::ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -0700436 uintptr_t return_pc, size_t frame_id, bool interpreter_entry)
437 : this_object_(this_object), method_(method), return_pc_(return_pc), frame_id_(frame_id),
438 interpreter_entry_(interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800439 }
440
441 std::string Dump() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
442
443 mirror::Object* this_object_;
Brian Carlstromea46f952013-07-30 01:26:50 -0700444 mirror::ArtMethod* method_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100445 uintptr_t return_pc_;
446 size_t frame_id_;
447 bool interpreter_entry_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800448};
449
450} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -0800451} // namespace art
452
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700453#endif // ART_RUNTIME_INSTRUMENTATION_H_