blob: 5647c0e724694f786920a244ce641d1d2da10dd3 [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
Elliott Hughes76160052012-12-12 16:31:20 -080020#include "base/macros.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080021#include "locks.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022
23#include <stdint.h>
Ian Rogers62d6c772013-02-27 08:32:07 -080024#include <list>
jeffhao725a9572012-11-13 18:20:12 -080025
26namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027namespace mirror {
Brian Carlstromea46f952013-07-30 01:26:50 -070028 class ArtMethod;
29 class Class;
30 class Object;
31 class Throwable;
Ian Rogers62d6c772013-02-27 08:32:07 -080032} // namespace mirror
33union JValue;
jeffhao725a9572012-11-13 18:20:12 -080034class Thread;
Ian Rogers62d6c772013-02-27 08:32:07 -080035class ThrowLocation;
jeffhao725a9572012-11-13 18:20:12 -080036
Ian Rogers62d6c772013-02-27 08:32:07 -080037namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080038
Sebastien Hertzee1997a2013-09-19 14:47:09 +020039// Interpreter handler tables.
40enum InterpreterHandlerTable {
41 kMainHandlerTable = 0, // Main handler table: no suspend check, no instrumentation.
42 kAlternativeHandlerTable = 1, // Alternative handler table: suspend check and/or instrumentation
43 // enabled.
44 kNumHandlerTables
45};
46
Ian Rogers62d6c772013-02-27 08:32:07 -080047// Instrumentation event listener API. Registered listeners will get the appropriate call back for
48// the events they are listening for. The call backs supply the thread, method and dex_pc the event
49// occurred upon. The thread may or may not be Thread::Current().
50struct InstrumentationListener {
51 InstrumentationListener() {}
52 virtual ~InstrumentationListener() {}
53
54 // Call-back for when a method is entered.
55 virtual void MethodEntered(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -070056 const mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -080057 uint32_t dex_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
58
59 // Call-back for when a method is exited.
60 // TODO: its likely passing the return value would be useful, however, we may need to get and
61 // parse the shorty to determine what kind of register holds the result.
62 virtual void MethodExited(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -070063 const mirror::ArtMethod* method, uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -080064 const JValue& return_value)
65 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
66
67 // Call-back for when a method is popped due to an exception throw. A method will either cause a
68 // MethodExited call-back or a MethodUnwind call-back when its activation is removed.
Brian Carlstromea46f952013-07-30 01:26:50 -070069 virtual void MethodUnwind(Thread* thread, const mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -080070 uint32_t dex_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
71
72 // Call-back for when the dex pc moves in a method.
73 virtual void DexPcMoved(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -070074 const mirror::ArtMethod* method, uint32_t new_dex_pc)
Ian Rogers62d6c772013-02-27 08:32:07 -080075 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
76
77 // Call-back when an exception is caught.
78 virtual void ExceptionCaught(Thread* thread, const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -070079 mirror::ArtMethod* catch_method, uint32_t catch_dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -080080 mirror::Throwable* exception_object)
81 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
jeffhao725a9572012-11-13 18:20:12 -080082};
83
Ian Rogers62d6c772013-02-27 08:32:07 -080084// Instrumentation is a catch-all for when extra information is required from the runtime. The
85// typical use for instrumentation is for profiling and debugging. Instrumentation may add stubs
86// to method entry and exit, it may also force execution to be switched to the interpreter and
87// trigger deoptimization.
jeffhao725a9572012-11-13 18:20:12 -080088class Instrumentation {
89 public:
Ian Rogers62d6c772013-02-27 08:32:07 -080090 enum InstrumentationEvent {
91 kMethodEntered = 1,
92 kMethodExited = 2,
93 kMethodUnwind = 4,
94 kDexPcMoved = 8,
95 kExceptionCaught = 16
96 };
jeffhao725a9572012-11-13 18:20:12 -080097
Ian Rogers62d6c772013-02-27 08:32:07 -080098 Instrumentation() :
99 instrumentation_stubs_installed_(false), entry_exit_stubs_installed_(false),
100 interpreter_stubs_installed_(false),
101 interpret_only_(false), forced_interpret_only_(false),
102 have_method_entry_listeners_(false), have_method_exit_listeners_(false),
103 have_method_unwind_listeners_(false), have_dex_pc_listeners_(false),
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200104 have_exception_caught_listeners_(false),
Ian Rogersfa824272013-11-05 16:12:57 -0800105 interpreter_handler_table_(kMainHandlerTable),
106 quick_alloc_entry_points_instrumentation_counter_(0) {}
jeffhao725a9572012-11-13 18:20:12 -0800107
Ian Rogers62d6c772013-02-27 08:32:07 -0800108 // Add a listener to be notified of the masked together sent of instrumentation events. This
109 // suspend the runtime to install stubs. You are expected to hold the mutator lock as a proxy
110 // for saying you should have suspended all threads (installing stubs while threads are running
111 // will break).
112 void AddListener(InstrumentationListener* listener, uint32_t events)
113 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
114 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800115
Ian Rogers62d6c772013-02-27 08:32:07 -0800116 // Removes a listener possibly removing instrumentation stubs.
117 void RemoveListener(InstrumentationListener* listener, uint32_t events)
118 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
119 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800120
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200121 InterpreterHandlerTable GetInterpreterHandlerTable() const {
122 return interpreter_handler_table_;
123 }
124
Ian Rogersfa824272013-11-05 16:12:57 -0800125 void InstrumentQuickAllocEntryPoints() LOCKS_EXCLUDED(Locks::thread_list_lock_);
126 void UninstrumentQuickAllocEntryPoints() LOCKS_EXCLUDED(Locks::thread_list_lock_);
127
Ian Rogers62d6c772013-02-27 08:32:07 -0800128 // Update the code of a method respecting any installed stubs.
Brian Carlstromea46f952013-07-30 01:26:50 -0700129 void UpdateMethodsCode(mirror::ArtMethod* method, const void* code) const;
Ian Rogers62d6c772013-02-27 08:32:07 -0800130
131 // Get the quick code for the given method. More efficient than asking the class linker as it
132 // will short-cut to GetCode if instrumentation and static method resolution stubs aren't
133 // installed.
Brian Carlstromea46f952013-07-30 01:26:50 -0700134 const void* GetQuickCodeFor(const mirror::ArtMethod* method) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800135 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
136
137 void ForceInterpretOnly() {
138 interpret_only_ = true;
139 forced_interpret_only_ = true;
140 }
141
Brian Carlstromea46f952013-07-30 01:26:50 -0700142 // Called by ArtMethod::Invoke to determine dispatch mechanism.
Ian Rogers62d6c772013-02-27 08:32:07 -0800143 bool InterpretOnly() const {
144 return interpret_only_;
145 }
146
147 bool ShouldPortableCodeDeoptimize() const {
148 return instrumentation_stubs_installed_;
149 }
150
151 bool AreExitStubsInstalled() const {
152 return instrumentation_stubs_installed_;
153 }
154
Sebastien Hertz74109f62013-06-07 17:40:09 +0200155 bool HasMethodEntryListeners() const {
156 return have_method_entry_listeners_;
157 }
158
159 bool HasMethodExitListeners() const {
160 return have_method_exit_listeners_;
161 }
162
163 bool HasDexPcListeners() const {
164 return have_dex_pc_listeners_;
165 }
166
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200167 bool IsActive() const {
168 return have_dex_pc_listeners_ || have_method_entry_listeners_ || have_method_exit_listeners_ ||
169 have_exception_caught_listeners_ || have_method_unwind_listeners_;
170 }
171
Ian Rogers62d6c772013-02-27 08:32:07 -0800172 // Inform listeners that a method has been entered. A dex PC is provided as we may install
173 // listeners into executing code and get method enter events for methods already on the stack.
174 void MethodEnterEvent(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700175 const mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800176 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200177 if (UNLIKELY(HasMethodEntryListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800178 MethodEnterEventImpl(thread, this_object, method, dex_pc);
179 }
180 }
181
182 // Inform listeners that a method has been exited.
183 void MethodExitEvent(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700184 const mirror::ArtMethod* method, uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800185 const JValue& return_value) const
186 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200187 if (UNLIKELY(HasMethodExitListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800188 MethodExitEventImpl(thread, this_object, method, dex_pc, return_value);
189 }
190 }
191
192 // Inform listeners that a method has been exited due to an exception.
193 void MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700194 const mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800195 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
196
197 // Inform listeners that the dex pc has moved (only supported by the interpreter).
198 void DexPcMovedEvent(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700199 const mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800200 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200201 if (UNLIKELY(HasDexPcListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800202 DexPcMovedEventImpl(thread, this_object, method, dex_pc);
203 }
204 }
205
206 // Inform listeners that an exception was caught.
207 void ExceptionCaughtEvent(Thread* thread, const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -0700208 mirror::ArtMethod* catch_method, uint32_t catch_dex_pc,
Sebastien Hertz947ff082013-09-17 14:10:13 +0200209 mirror::Throwable* exception_object) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800210 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
211
212 // Called when an instrumented method is entered. The intended link register (lr) is saved so
213 // that returning causes a branch to the method exit stub. Generates method enter events.
214 void PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700215 mirror::ArtMethod* method, uintptr_t lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700216 bool interpreter_entry)
Ian Rogers62d6c772013-02-27 08:32:07 -0800217 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
218
219 // Called when an instrumented method is exited. Removes the pushed instrumentation frame
220 // returning the intended link register. Generates method exit events.
221 uint64_t PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc, uint64_t gpr_result,
222 uint64_t fpr_result)
223 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
224
225 // Pops an instrumentation frame from the current thread and generate an unwind event.
226 void PopMethodForUnwind(Thread* self, bool is_deoptimization) const
227 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
228
229 // Call back for configure stubs.
230 bool InstallStubsForClass(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800231
232 private:
Ian Rogers62d6c772013-02-27 08:32:07 -0800233 // Does the job of installing or removing instrumentation code within methods.
234 void ConfigureStubs(bool require_entry_exit_stubs, bool require_interpreter)
235 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
236 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800237
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200238 void UpdateInterpreterHandlerTable() {
239 interpreter_handler_table_ = IsActive() ? kAlternativeHandlerTable : kMainHandlerTable;
240 }
241
Ian Rogers62d6c772013-02-27 08:32:07 -0800242 void MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700243 const mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800244 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
245 void MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700246 const mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800247 uint32_t dex_pc, const JValue& return_value) const
248 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
249 void DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700250 const mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800251 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800252
Brian Carlstromea46f952013-07-30 01:26:50 -0700253 // Have we hijacked ArtMethod::code_ so that it calls instrumentation/interpreter code?
Ian Rogers62d6c772013-02-27 08:32:07 -0800254 bool instrumentation_stubs_installed_;
255
Brian Carlstromea46f952013-07-30 01:26:50 -0700256 // Have we hijacked ArtMethod::code_ to reference the enter/exit stubs?
Ian Rogers62d6c772013-02-27 08:32:07 -0800257 bool entry_exit_stubs_installed_;
258
Brian Carlstromea46f952013-07-30 01:26:50 -0700259 // Have we hijacked ArtMethod::code_ to reference the enter interpreter stub?
Ian Rogers62d6c772013-02-27 08:32:07 -0800260 bool interpreter_stubs_installed_;
261
262 // Do we need the fidelity of events that we only get from running within the interpreter?
263 bool interpret_only_;
264
265 // Did the runtime request we only run in the interpreter? ie -Xint mode.
266 bool forced_interpret_only_;
267
268 // Do we have any listeners for method entry events? Short-cut to avoid taking the
269 // instrumentation_lock_.
270 bool have_method_entry_listeners_;
271
272 // Do we have any listeners for method exit events? Short-cut to avoid taking the
273 // instrumentation_lock_.
274 bool have_method_exit_listeners_;
275
276 // Do we have any listeners for method unwind events? Short-cut to avoid taking the
277 // instrumentation_lock_.
278 bool have_method_unwind_listeners_;
279
280 // Do we have any listeners for dex move events? Short-cut to avoid taking the
281 // instrumentation_lock_.
282 bool have_dex_pc_listeners_;
283
284 // Do we have any exception caught listeners? Short-cut to avoid taking the instrumentation_lock_.
285 bool have_exception_caught_listeners_;
286
287 // The event listeners, written to with the mutator_lock_ exclusively held.
288 std::list<InstrumentationListener*> method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
289 std::list<InstrumentationListener*> method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
290 std::list<InstrumentationListener*> method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
291 std::list<InstrumentationListener*> dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_);
292 std::list<InstrumentationListener*> exception_caught_listeners_ GUARDED_BY(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800293
Ian Rogersfa824272013-11-05 16:12:57 -0800294 // Current interpreter handler table. This is updated each time the thread state flags are
295 // modified.
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200296 InterpreterHandlerTable interpreter_handler_table_;
297
Ian Rogersfa824272013-11-05 16:12:57 -0800298 // Greater than 0 if quick alloc entry points instrumented.
299 // TODO: The access and changes to this is racy and should be guarded by a lock.
300 size_t quick_alloc_entry_points_instrumentation_counter_;
301
jeffhao725a9572012-11-13 18:20:12 -0800302 DISALLOW_COPY_AND_ASSIGN(Instrumentation);
303};
304
Ian Rogers62d6c772013-02-27 08:32:07 -0800305// An element in the instrumentation side stack maintained in art::Thread.
306struct InstrumentationStackFrame {
Brian Carlstromea46f952013-07-30 01:26:50 -0700307 InstrumentationStackFrame(mirror::Object* this_object, mirror::ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -0700308 uintptr_t return_pc, size_t frame_id, bool interpreter_entry)
309 : this_object_(this_object), method_(method), return_pc_(return_pc), frame_id_(frame_id),
310 interpreter_entry_(interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800311 }
312
313 std::string Dump() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
314
315 mirror::Object* this_object_;
Brian Carlstromea46f952013-07-30 01:26:50 -0700316 mirror::ArtMethod* method_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800317 const uintptr_t return_pc_;
318 const size_t frame_id_;
Jeff Hao65d15d92013-07-16 16:39:33 -0700319 const bool interpreter_entry_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800320};
321
322} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -0800323} // namespace art
324
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700325#endif // ART_RUNTIME_INSTRUMENTATION_H_