blob: cabb0e9c27223a2e4d76ff98edc37b9ea37da567 [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 Rogersef7d42f2014-01-06 12:55:46 -080024#include "atomic.h"
Andreas Gamped58342c2014-06-05 14:18:08 -070025#include "instruction_set.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"
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070028#include "object_callbacks.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029
jeffhao725a9572012-11-13 18:20:12 -080030namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031namespace mirror {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020032 class ArtField;
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
38union JValue;
jeffhao725a9572012-11-13 18:20:12 -080039class Thread;
Ian Rogers62d6c772013-02-27 08:32:07 -080040class ThrowLocation;
jeffhao725a9572012-11-13 18:20:12 -080041
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.
92 virtual void ExceptionCaught(Thread* thread, const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -070093 mirror::ArtMethod* catch_method, uint32_t catch_dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -080094 mirror::Throwable* exception_object)
95 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
jeffhao725a9572012-11-13 18:20:12 -080096};
97
Ian Rogers62d6c772013-02-27 08:32:07 -080098// Instrumentation is a catch-all for when extra information is required from the runtime. The
99// typical use for instrumentation is for profiling and debugging. Instrumentation may add stubs
100// to method entry and exit, it may also force execution to be switched to the interpreter and
101// trigger deoptimization.
jeffhao725a9572012-11-13 18:20:12 -0800102class Instrumentation {
103 public:
Ian Rogers62d6c772013-02-27 08:32:07 -0800104 enum InstrumentationEvent {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200105 kMethodEntered = 1 << 0,
106 kMethodExited = 1 << 1,
107 kMethodUnwind = 1 << 2,
108 kDexPcMoved = 1 << 3,
109 kFieldRead = 1 << 4,
110 kFieldWritten = 1 << 5,
111 kExceptionCaught = 1 << 6,
Ian Rogers62d6c772013-02-27 08:32:07 -0800112 };
jeffhao725a9572012-11-13 18:20:12 -0800113
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700114 Instrumentation();
jeffhao725a9572012-11-13 18:20:12 -0800115
Ian Rogers62d6c772013-02-27 08:32:07 -0800116 // Add a listener to be notified of the masked together sent of instrumentation events. This
117 // suspend the runtime to install stubs. You are expected to hold the mutator lock as a proxy
118 // for saying you should have suspended all threads (installing stubs while threads are running
119 // will break).
120 void AddListener(InstrumentationListener* listener, uint32_t events)
121 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
122 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800123
Ian Rogers62d6c772013-02-27 08:32:07 -0800124 // Removes a listener possibly removing instrumentation stubs.
125 void RemoveListener(InstrumentationListener* listener, uint32_t events)
126 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
127 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800128
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100129 // Deoptimization.
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100130 void EnableDeoptimization()
131 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700132 LOCKS_EXCLUDED(deoptimized_methods_lock_);
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100133 void DisableDeoptimization()
134 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100135 LOCKS_EXCLUDED(deoptimized_methods_lock_);
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100136 bool AreAllMethodsDeoptimized() const {
137 return interpreter_stubs_installed_;
138 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100139 bool ShouldNotifyMethodEnterExitEvents() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100140
141 // Executes everything with interpreter.
142 void DeoptimizeEverything()
143 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
144 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
145
146 // Executes everything with compiled code (or interpreter if there is no code).
147 void UndeoptimizeEverything()
148 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
149 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
150
151 // Deoptimize a method by forcing its execution with the interpreter. Nevertheless, a static
152 // method (except a class initializer) set to the resolution trampoline will be deoptimized only
153 // once its declaring class is initialized.
154 void Deoptimize(mirror::ArtMethod* method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700155 LOCKS_EXCLUDED(Locks::thread_list_lock_, deoptimized_methods_lock_)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100156 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
157
158 // Undeoptimze the method by restoring its entrypoints. Nevertheless, a static method
159 // (except a class initializer) set to the resolution trampoline will be updated only once its
160 // declaring class is initialized.
161 void Undeoptimize(mirror::ArtMethod* method)
Sebastien Hertz4d25df32014-03-21 17:44:46 +0100162 LOCKS_EXCLUDED(Locks::thread_list_lock_, deoptimized_methods_lock_)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100163 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
164
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700165 bool IsDeoptimized(mirror::ArtMethod* method)
166 LOCKS_EXCLUDED(deoptimized_methods_lock_)
167 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100168
169 // Enable method tracing by installing instrumentation entry/exit stubs.
170 void EnableMethodTracing()
171 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
172 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
173
174 // Disable method tracing by uninstalling instrumentation entry/exit stubs.
175 void DisableMethodTracing()
176 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
177 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
178
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200179 InterpreterHandlerTable GetInterpreterHandlerTable() const {
180 return interpreter_handler_table_;
181 }
182
Mathieu Chartierd8891782014-03-02 13:28:37 -0800183 void InstrumentQuickAllocEntryPoints() LOCKS_EXCLUDED(Locks::thread_list_lock_,
184 Locks::runtime_shutdown_lock_);
185 void UninstrumentQuickAllocEntryPoints() LOCKS_EXCLUDED(Locks::thread_list_lock_,
186 Locks::runtime_shutdown_lock_);
187 void ResetQuickAllocEntryPoints() EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_);
Ian Rogersfa824272013-11-05 16:12:57 -0800188
Ian Rogers62d6c772013-02-27 08:32:07 -0800189 // Update the code of a method respecting any installed stubs.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800190 void UpdateMethodsCode(mirror::ArtMethod* method, const void* quick_code,
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700191 const void* portable_code, bool have_portable_code)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100192 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800193
194 // Get the quick code for the given method. More efficient than asking the class linker as it
195 // will short-cut to GetCode if instrumentation and static method resolution stubs aren't
196 // installed.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800197 const void* GetQuickCodeFor(mirror::ArtMethod* method) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800198 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
199
200 void ForceInterpretOnly() {
201 interpret_only_ = true;
202 forced_interpret_only_ = true;
203 }
204
Brian Carlstromea46f952013-07-30 01:26:50 -0700205 // Called by ArtMethod::Invoke to determine dispatch mechanism.
Ian Rogers62d6c772013-02-27 08:32:07 -0800206 bool InterpretOnly() const {
207 return interpret_only_;
208 }
209
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800210 bool IsForcedInterpretOnly() const {
211 return forced_interpret_only_;
212 }
213
Ian Rogers62d6c772013-02-27 08:32:07 -0800214 bool ShouldPortableCodeDeoptimize() const {
215 return instrumentation_stubs_installed_;
216 }
217
218 bool AreExitStubsInstalled() const {
219 return instrumentation_stubs_installed_;
220 }
221
Sebastien Hertz74109f62013-06-07 17:40:09 +0200222 bool HasMethodEntryListeners() const {
223 return have_method_entry_listeners_;
224 }
225
226 bool HasMethodExitListeners() const {
227 return have_method_exit_listeners_;
228 }
229
230 bool HasDexPcListeners() const {
231 return have_dex_pc_listeners_;
232 }
233
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200234 bool HasFieldReadListeners() const {
235 return have_field_read_listeners_;
236 }
237
238 bool HasFieldWriteListeners() const {
239 return have_field_write_listeners_;
240 }
241
Sebastien Hertz9f102032014-05-23 08:59:42 +0200242 bool HasExceptionCaughtListeners() const {
243 return have_exception_caught_listeners_;
244 }
245
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200246 bool IsActive() const {
247 return have_dex_pc_listeners_ || have_method_entry_listeners_ || have_method_exit_listeners_ ||
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200248 have_field_read_listeners_ || have_field_write_listeners_ ||
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200249 have_exception_caught_listeners_ || have_method_unwind_listeners_;
250 }
251
Ian Rogers62d6c772013-02-27 08:32:07 -0800252 // Inform listeners that a method has been entered. A dex PC is provided as we may install
253 // listeners into executing code and get method enter events for methods already on the stack.
254 void MethodEnterEvent(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800255 mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800256 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200257 if (UNLIKELY(HasMethodEntryListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800258 MethodEnterEventImpl(thread, this_object, method, dex_pc);
259 }
260 }
261
262 // Inform listeners that a method has been exited.
263 void MethodExitEvent(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800264 mirror::ArtMethod* method, uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800265 const JValue& return_value) const
266 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200267 if (UNLIKELY(HasMethodExitListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800268 MethodExitEventImpl(thread, this_object, method, dex_pc, return_value);
269 }
270 }
271
272 // Inform listeners that a method has been exited due to an exception.
273 void MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800274 mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800275 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
276
277 // Inform listeners that the dex pc has moved (only supported by the interpreter).
278 void DexPcMovedEvent(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800279 mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800280 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200281 if (UNLIKELY(HasDexPcListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800282 DexPcMovedEventImpl(thread, this_object, method, dex_pc);
283 }
284 }
285
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200286 // Inform listeners that we read a field (only supported by the interpreter).
287 void FieldReadEvent(Thread* thread, mirror::Object* this_object,
288 mirror::ArtMethod* method, uint32_t dex_pc,
289 mirror::ArtField* field) const
290 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
291 if (UNLIKELY(HasFieldReadListeners())) {
292 FieldReadEventImpl(thread, this_object, method, dex_pc, field);
293 }
294 }
295
296 // Inform listeners that we write a field (only supported by the interpreter).
297 void FieldWriteEvent(Thread* thread, mirror::Object* this_object,
298 mirror::ArtMethod* method, uint32_t dex_pc,
299 mirror::ArtField* field, const JValue& field_value) const
300 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
301 if (UNLIKELY(HasFieldWriteListeners())) {
302 FieldWriteEventImpl(thread, this_object, method, dex_pc, field, field_value);
303 }
304 }
305
Ian Rogers62d6c772013-02-27 08:32:07 -0800306 // Inform listeners that an exception was caught.
307 void ExceptionCaughtEvent(Thread* thread, const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -0700308 mirror::ArtMethod* catch_method, uint32_t catch_dex_pc,
Sebastien Hertz947ff082013-09-17 14:10:13 +0200309 mirror::Throwable* exception_object) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800310 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
311
312 // Called when an instrumented method is entered. The intended link register (lr) is saved so
313 // that returning causes a branch to the method exit stub. Generates method enter events.
314 void PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700315 mirror::ArtMethod* method, uintptr_t lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700316 bool interpreter_entry)
Ian Rogers62d6c772013-02-27 08:32:07 -0800317 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
318
319 // Called when an instrumented method is exited. Removes the pushed instrumentation frame
320 // returning the intended link register. Generates method exit events.
Andreas Gamped58342c2014-06-05 14:18:08 -0700321 TwoWordReturn PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
322 uint64_t gpr_result, uint64_t fpr_result)
Ian Rogers62d6c772013-02-27 08:32:07 -0800323 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
324
325 // Pops an instrumentation frame from the current thread and generate an unwind event.
326 void PopMethodForUnwind(Thread* self, bool is_deoptimization) const
327 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
328
329 // Call back for configure stubs.
330 bool InstallStubsForClass(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800331
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100332 void InstallStubsForMethod(mirror::ArtMethod* method)
333 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
334
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700335 void VisitRoots(RootCallback* callback, void* arg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
336 LOCKS_EXCLUDED(deoptimized_methods_lock_);
337
jeffhao725a9572012-11-13 18:20:12 -0800338 private:
Ian Rogers62d6c772013-02-27 08:32:07 -0800339 // Does the job of installing or removing instrumentation code within methods.
340 void ConfigureStubs(bool require_entry_exit_stubs, bool require_interpreter)
341 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700342 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_,
343 deoptimized_methods_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800344
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200345 void UpdateInterpreterHandlerTable() {
346 interpreter_handler_table_ = IsActive() ? kAlternativeHandlerTable : kMainHandlerTable;
347 }
348
Mathieu Chartier661974a2014-01-09 11:23:53 -0800349 // No thread safety analysis to get around SetQuickAllocEntryPointsInstrumented requiring
350 // exclusive access to mutator lock which you can't get if the runtime isn't started.
351 void SetEntrypointsInstrumented(bool instrumented) NO_THREAD_SAFETY_ANALYSIS;
352
Ian Rogers62d6c772013-02-27 08:32:07 -0800353 void MethodEnterEventImpl(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_);
356 void MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800357 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800358 uint32_t dex_pc, const JValue& return_value) const
359 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
360 void DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800361 mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800362 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200363 void FieldReadEventImpl(Thread* thread, mirror::Object* this_object,
364 mirror::ArtMethod* method, uint32_t dex_pc,
365 mirror::ArtField* field) const
366 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
367 void FieldWriteEventImpl(Thread* thread, mirror::Object* this_object,
368 mirror::ArtMethod* method, uint32_t dex_pc,
369 mirror::ArtField* field, const JValue& field_value) const
370 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800371
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700372 // Read barrier-aware utility functions for accessing deoptimized_methods_
373 bool AddDeoptimizedMethod(mirror::ArtMethod* method)
374 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
375 EXCLUSIVE_LOCKS_REQUIRED(deoptimized_methods_lock_);
376 bool FindDeoptimizedMethod(mirror::ArtMethod* method)
377 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
378 SHARED_LOCKS_REQUIRED(deoptimized_methods_lock_);
379 bool RemoveDeoptimizedMethod(mirror::ArtMethod* method)
380 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
381 EXCLUSIVE_LOCKS_REQUIRED(deoptimized_methods_lock_);
382 mirror::ArtMethod* BeginDeoptimizedMethod()
383 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
384 SHARED_LOCKS_REQUIRED(deoptimized_methods_lock_);
385 bool IsDeoptimizedMethodsEmpty() const
386 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
387 SHARED_LOCKS_REQUIRED(deoptimized_methods_lock_);
388
Brian Carlstromea46f952013-07-30 01:26:50 -0700389 // Have we hijacked ArtMethod::code_ so that it calls instrumentation/interpreter code?
Ian Rogers62d6c772013-02-27 08:32:07 -0800390 bool instrumentation_stubs_installed_;
391
Brian Carlstromea46f952013-07-30 01:26:50 -0700392 // Have we hijacked ArtMethod::code_ to reference the enter/exit stubs?
Ian Rogers62d6c772013-02-27 08:32:07 -0800393 bool entry_exit_stubs_installed_;
394
Brian Carlstromea46f952013-07-30 01:26:50 -0700395 // Have we hijacked ArtMethod::code_ to reference the enter interpreter stub?
Ian Rogers62d6c772013-02-27 08:32:07 -0800396 bool interpreter_stubs_installed_;
397
398 // Do we need the fidelity of events that we only get from running within the interpreter?
399 bool interpret_only_;
400
401 // Did the runtime request we only run in the interpreter? ie -Xint mode.
402 bool forced_interpret_only_;
403
404 // Do we have any listeners for method entry events? Short-cut to avoid taking the
405 // instrumentation_lock_.
406 bool have_method_entry_listeners_;
407
408 // Do we have any listeners for method exit events? Short-cut to avoid taking the
409 // instrumentation_lock_.
410 bool have_method_exit_listeners_;
411
412 // Do we have any listeners for method unwind events? Short-cut to avoid taking the
413 // instrumentation_lock_.
414 bool have_method_unwind_listeners_;
415
416 // Do we have any listeners for dex move events? Short-cut to avoid taking the
417 // instrumentation_lock_.
418 bool have_dex_pc_listeners_;
419
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200420 // Do we have any listeners for field read events? Short-cut to avoid taking the
421 // instrumentation_lock_.
422 bool have_field_read_listeners_;
423
424 // Do we have any listeners for field write events? Short-cut to avoid taking the
425 // instrumentation_lock_.
426 bool have_field_write_listeners_;
427
Ian Rogers62d6c772013-02-27 08:32:07 -0800428 // Do we have any exception caught listeners? Short-cut to avoid taking the instrumentation_lock_.
429 bool have_exception_caught_listeners_;
430
431 // The event listeners, written to with the mutator_lock_ exclusively held.
432 std::list<InstrumentationListener*> method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
433 std::list<InstrumentationListener*> method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
434 std::list<InstrumentationListener*> method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
435 std::list<InstrumentationListener*> dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200436 std::list<InstrumentationListener*> field_read_listeners_ GUARDED_BY(Locks::mutator_lock_);
437 std::list<InstrumentationListener*> field_write_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800438 std::list<InstrumentationListener*> exception_caught_listeners_ GUARDED_BY(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800439
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100440 // The set of methods being deoptimized (by the debugger) which must be executed with interpreter
441 // only.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700442 mutable ReaderWriterMutex deoptimized_methods_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700443 std::multimap<int32_t, mirror::ArtMethod*> deoptimized_methods_ GUARDED_BY(deoptimized_methods_lock_);
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100444 bool deoptimization_enabled_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100445
Ian Rogersfa824272013-11-05 16:12:57 -0800446 // Current interpreter handler table. This is updated each time the thread state flags are
447 // modified.
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200448 InterpreterHandlerTable interpreter_handler_table_;
449
Ian Rogersfa824272013-11-05 16:12:57 -0800450 // Greater than 0 if quick alloc entry points instrumented.
451 // TODO: The access and changes to this is racy and should be guarded by a lock.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800452 AtomicInteger quick_alloc_entry_points_instrumentation_counter_;
Ian Rogersfa824272013-11-05 16:12:57 -0800453
jeffhao725a9572012-11-13 18:20:12 -0800454 DISALLOW_COPY_AND_ASSIGN(Instrumentation);
455};
456
Ian Rogers62d6c772013-02-27 08:32:07 -0800457// An element in the instrumentation side stack maintained in art::Thread.
458struct InstrumentationStackFrame {
Brian Carlstromea46f952013-07-30 01:26:50 -0700459 InstrumentationStackFrame(mirror::Object* this_object, mirror::ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -0700460 uintptr_t return_pc, size_t frame_id, bool interpreter_entry)
461 : this_object_(this_object), method_(method), return_pc_(return_pc), frame_id_(frame_id),
462 interpreter_entry_(interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800463 }
464
465 std::string Dump() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
466
467 mirror::Object* this_object_;
Brian Carlstromea46f952013-07-30 01:26:50 -0700468 mirror::ArtMethod* method_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100469 uintptr_t return_pc_;
470 size_t frame_id_;
471 bool interpreter_entry_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800472};
473
474} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -0800475} // namespace art
476
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700477#endif // ART_RUNTIME_INSTRUMENTATION_H_