blob: 7a0aaf7858cec4cdf6165ea8b0c7045d38a27408 [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
Ian Rogers62d6c772013-02-27 08:32:07 -080039const bool kVerboseInstrumentation = false;
40
Sebastien Hertzee1997a2013-09-19 14:47:09 +020041// Interpreter handler tables.
42enum InterpreterHandlerTable {
43 kMainHandlerTable = 0, // Main handler table: no suspend check, no instrumentation.
44 kAlternativeHandlerTable = 1, // Alternative handler table: suspend check and/or instrumentation
45 // enabled.
46 kNumHandlerTables
47};
48
Ian Rogers62d6c772013-02-27 08:32:07 -080049// Instrumentation event listener API. Registered listeners will get the appropriate call back for
50// the events they are listening for. The call backs supply the thread, method and dex_pc the event
51// occurred upon. The thread may or may not be Thread::Current().
52struct InstrumentationListener {
53 InstrumentationListener() {}
54 virtual ~InstrumentationListener() {}
55
56 // Call-back for when a method is entered.
57 virtual void MethodEntered(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -070058 const mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -080059 uint32_t dex_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
60
61 // Call-back for when a method is exited.
62 // TODO: its likely passing the return value would be useful, however, we may need to get and
63 // parse the shorty to determine what kind of register holds the result.
64 virtual void MethodExited(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -070065 const mirror::ArtMethod* method, uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -080066 const JValue& return_value)
67 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
68
69 // Call-back for when a method is popped due to an exception throw. A method will either cause a
70 // MethodExited call-back or a MethodUnwind call-back when its activation is removed.
Brian Carlstromea46f952013-07-30 01:26:50 -070071 virtual void MethodUnwind(Thread* thread, const mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -080072 uint32_t dex_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
73
74 // Call-back for when the dex pc moves in a method.
75 virtual void DexPcMoved(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -070076 const mirror::ArtMethod* method, uint32_t new_dex_pc)
Ian Rogers62d6c772013-02-27 08:32:07 -080077 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
78
79 // Call-back when an exception is caught.
80 virtual void ExceptionCaught(Thread* thread, const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -070081 mirror::ArtMethod* catch_method, uint32_t catch_dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -080082 mirror::Throwable* exception_object)
83 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
jeffhao725a9572012-11-13 18:20:12 -080084};
85
Ian Rogers62d6c772013-02-27 08:32:07 -080086// Instrumentation is a catch-all for when extra information is required from the runtime. The
87// typical use for instrumentation is for profiling and debugging. Instrumentation may add stubs
88// to method entry and exit, it may also force execution to be switched to the interpreter and
89// trigger deoptimization.
jeffhao725a9572012-11-13 18:20:12 -080090class Instrumentation {
91 public:
Ian Rogers62d6c772013-02-27 08:32:07 -080092 enum InstrumentationEvent {
93 kMethodEntered = 1,
94 kMethodExited = 2,
95 kMethodUnwind = 4,
96 kDexPcMoved = 8,
97 kExceptionCaught = 16
98 };
jeffhao725a9572012-11-13 18:20:12 -080099
Ian Rogers62d6c772013-02-27 08:32:07 -0800100 Instrumentation() :
101 instrumentation_stubs_installed_(false), entry_exit_stubs_installed_(false),
102 interpreter_stubs_installed_(false),
103 interpret_only_(false), forced_interpret_only_(false),
104 have_method_entry_listeners_(false), have_method_exit_listeners_(false),
105 have_method_unwind_listeners_(false), have_dex_pc_listeners_(false),
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200106 have_exception_caught_listeners_(false),
107 interpreter_handler_table_(kMainHandlerTable) {}
jeffhao725a9572012-11-13 18:20:12 -0800108
Ian Rogers62d6c772013-02-27 08:32:07 -0800109 // Add a listener to be notified of the masked together sent of instrumentation events. This
110 // suspend the runtime to install stubs. You are expected to hold the mutator lock as a proxy
111 // for saying you should have suspended all threads (installing stubs while threads are running
112 // will break).
113 void AddListener(InstrumentationListener* listener, uint32_t events)
114 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
115 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800116
Ian Rogers62d6c772013-02-27 08:32:07 -0800117 // Removes a listener possibly removing instrumentation stubs.
118 void RemoveListener(InstrumentationListener* listener, uint32_t events)
119 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
120 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800121
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200122 InterpreterHandlerTable GetInterpreterHandlerTable() const {
123 return interpreter_handler_table_;
124 }
125
Ian Rogers62d6c772013-02-27 08:32:07 -0800126 // Update the code of a method respecting any installed stubs.
Brian Carlstromea46f952013-07-30 01:26:50 -0700127 void UpdateMethodsCode(mirror::ArtMethod* method, const void* code) const;
Ian Rogers62d6c772013-02-27 08:32:07 -0800128
129 // Get the quick code for the given method. More efficient than asking the class linker as it
130 // will short-cut to GetCode if instrumentation and static method resolution stubs aren't
131 // installed.
Brian Carlstromea46f952013-07-30 01:26:50 -0700132 const void* GetQuickCodeFor(const mirror::ArtMethod* method) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800133 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
134
135 void ForceInterpretOnly() {
136 interpret_only_ = true;
137 forced_interpret_only_ = true;
138 }
139
Brian Carlstromea46f952013-07-30 01:26:50 -0700140 // Called by ArtMethod::Invoke to determine dispatch mechanism.
Ian Rogers62d6c772013-02-27 08:32:07 -0800141 bool InterpretOnly() const {
142 return interpret_only_;
143 }
144
145 bool ShouldPortableCodeDeoptimize() const {
146 return instrumentation_stubs_installed_;
147 }
148
149 bool AreExitStubsInstalled() const {
150 return instrumentation_stubs_installed_;
151 }
152
Sebastien Hertz74109f62013-06-07 17:40:09 +0200153 bool HasMethodEntryListeners() const {
154 return have_method_entry_listeners_;
155 }
156
157 bool HasMethodExitListeners() const {
158 return have_method_exit_listeners_;
159 }
160
161 bool HasDexPcListeners() const {
162 return have_dex_pc_listeners_;
163 }
164
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200165 bool IsActive() const {
166 return have_dex_pc_listeners_ || have_method_entry_listeners_ || have_method_exit_listeners_ ||
167 have_exception_caught_listeners_ || have_method_unwind_listeners_;
168 }
169
Ian Rogers62d6c772013-02-27 08:32:07 -0800170 // Inform listeners that a method has been entered. A dex PC is provided as we may install
171 // listeners into executing code and get method enter events for methods already on the stack.
172 void MethodEnterEvent(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700173 const mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800174 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200175 if (UNLIKELY(HasMethodEntryListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800176 MethodEnterEventImpl(thread, this_object, method, dex_pc);
177 }
178 }
179
180 // Inform listeners that a method has been exited.
181 void MethodExitEvent(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700182 const mirror::ArtMethod* method, uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800183 const JValue& return_value) const
184 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200185 if (UNLIKELY(HasMethodExitListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800186 MethodExitEventImpl(thread, this_object, method, dex_pc, return_value);
187 }
188 }
189
190 // Inform listeners that a method has been exited due to an exception.
191 void MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700192 const mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800193 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
194
195 // Inform listeners that the dex pc has moved (only supported by the interpreter).
196 void DexPcMovedEvent(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700197 const mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800198 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200199 if (UNLIKELY(HasDexPcListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800200 DexPcMovedEventImpl(thread, this_object, method, dex_pc);
201 }
202 }
203
204 // Inform listeners that an exception was caught.
205 void ExceptionCaughtEvent(Thread* thread, const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -0700206 mirror::ArtMethod* catch_method, uint32_t catch_dex_pc,
Sebastien Hertz947ff082013-09-17 14:10:13 +0200207 mirror::Throwable* exception_object) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800208 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
209
210 // Called when an instrumented method is entered. The intended link register (lr) is saved so
211 // that returning causes a branch to the method exit stub. Generates method enter events.
212 void PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700213 mirror::ArtMethod* method, uintptr_t lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700214 bool interpreter_entry)
Ian Rogers62d6c772013-02-27 08:32:07 -0800215 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
216
217 // Called when an instrumented method is exited. Removes the pushed instrumentation frame
218 // returning the intended link register. Generates method exit events.
219 uint64_t PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc, uint64_t gpr_result,
220 uint64_t fpr_result)
221 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
222
223 // Pops an instrumentation frame from the current thread and generate an unwind event.
224 void PopMethodForUnwind(Thread* self, bool is_deoptimization) const
225 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
226
227 // Call back for configure stubs.
228 bool InstallStubsForClass(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800229
230 private:
Ian Rogers62d6c772013-02-27 08:32:07 -0800231 // Does the job of installing or removing instrumentation code within methods.
232 void ConfigureStubs(bool require_entry_exit_stubs, bool require_interpreter)
233 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
234 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800235
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200236 void UpdateInterpreterHandlerTable() {
237 interpreter_handler_table_ = IsActive() ? kAlternativeHandlerTable : kMainHandlerTable;
238 }
239
Ian Rogers62d6c772013-02-27 08:32:07 -0800240 void MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700241 const mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800242 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
243 void MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700244 const mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800245 uint32_t dex_pc, const JValue& return_value) const
246 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
247 void DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700248 const mirror::ArtMethod* method, uint32_t dex_pc) const
Ian Rogers62d6c772013-02-27 08:32:07 -0800249 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800250
Brian Carlstromea46f952013-07-30 01:26:50 -0700251 // Have we hijacked ArtMethod::code_ so that it calls instrumentation/interpreter code?
Ian Rogers62d6c772013-02-27 08:32:07 -0800252 bool instrumentation_stubs_installed_;
253
Brian Carlstromea46f952013-07-30 01:26:50 -0700254 // Have we hijacked ArtMethod::code_ to reference the enter/exit stubs?
Ian Rogers62d6c772013-02-27 08:32:07 -0800255 bool entry_exit_stubs_installed_;
256
Brian Carlstromea46f952013-07-30 01:26:50 -0700257 // Have we hijacked ArtMethod::code_ to reference the enter interpreter stub?
Ian Rogers62d6c772013-02-27 08:32:07 -0800258 bool interpreter_stubs_installed_;
259
260 // Do we need the fidelity of events that we only get from running within the interpreter?
261 bool interpret_only_;
262
263 // Did the runtime request we only run in the interpreter? ie -Xint mode.
264 bool forced_interpret_only_;
265
266 // Do we have any listeners for method entry events? Short-cut to avoid taking the
267 // instrumentation_lock_.
268 bool have_method_entry_listeners_;
269
270 // Do we have any listeners for method exit events? Short-cut to avoid taking the
271 // instrumentation_lock_.
272 bool have_method_exit_listeners_;
273
274 // Do we have any listeners for method unwind events? Short-cut to avoid taking the
275 // instrumentation_lock_.
276 bool have_method_unwind_listeners_;
277
278 // Do we have any listeners for dex move events? Short-cut to avoid taking the
279 // instrumentation_lock_.
280 bool have_dex_pc_listeners_;
281
282 // Do we have any exception caught listeners? Short-cut to avoid taking the instrumentation_lock_.
283 bool have_exception_caught_listeners_;
284
285 // The event listeners, written to with the mutator_lock_ exclusively held.
286 std::list<InstrumentationListener*> method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
287 std::list<InstrumentationListener*> method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
288 std::list<InstrumentationListener*> method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
289 std::list<InstrumentationListener*> dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_);
290 std::list<InstrumentationListener*> exception_caught_listeners_ GUARDED_BY(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800291
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200292 // Current interpreter handler table. This is updated each time the thread state flags are modified.
293 InterpreterHandlerTable interpreter_handler_table_;
294
jeffhao725a9572012-11-13 18:20:12 -0800295 DISALLOW_COPY_AND_ASSIGN(Instrumentation);
296};
297
Ian Rogers62d6c772013-02-27 08:32:07 -0800298// An element in the instrumentation side stack maintained in art::Thread.
299struct InstrumentationStackFrame {
Brian Carlstromea46f952013-07-30 01:26:50 -0700300 InstrumentationStackFrame(mirror::Object* this_object, mirror::ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -0700301 uintptr_t return_pc, size_t frame_id, bool interpreter_entry)
302 : this_object_(this_object), method_(method), return_pc_(return_pc), frame_id_(frame_id),
303 interpreter_entry_(interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800304 }
305
306 std::string Dump() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
307
308 mirror::Object* this_object_;
Brian Carlstromea46f952013-07-30 01:26:50 -0700309 mirror::ArtMethod* method_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800310 const uintptr_t return_pc_;
311 const size_t frame_id_;
Jeff Hao65d15d92013-07-16 16:39:33 -0700312 const bool interpreter_entry_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800313};
314
315} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -0800316} // namespace art
317
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700318#endif // ART_RUNTIME_INSTRUMENTATION_H_