blob: e79c75efac9297a7b45874e8f1892230a321baba [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
17#ifndef ART_SRC_INSTRUMENTATION_H_
18#define ART_SRC_INSTRUMENTATION_H_
19
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 {
jeffhao725a9572012-11-13 18:20:12 -080028class AbstractMethod;
Ian Rogers62d6c772013-02-27 08:32:07 -080029class Class;
30class Object;
31class Throwable;
32} // 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
41// Instrumentation event listener API. Registered listeners will get the appropriate call back for
42// the events they are listening for. The call backs supply the thread, method and dex_pc the event
43// occurred upon. The thread may or may not be Thread::Current().
44struct InstrumentationListener {
45 InstrumentationListener() {}
46 virtual ~InstrumentationListener() {}
47
48 // Call-back for when a method is entered.
49 virtual void MethodEntered(Thread* thread, mirror::Object* this_object,
50 const mirror::AbstractMethod* method,
51 uint32_t dex_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
52
53 // Call-back for when a method is exited.
54 // TODO: its likely passing the return value would be useful, however, we may need to get and
55 // parse the shorty to determine what kind of register holds the result.
56 virtual void MethodExited(Thread* thread, mirror::Object* this_object,
57 const mirror::AbstractMethod* method, uint32_t dex_pc,
58 const JValue& return_value)
59 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
60
61 // Call-back for when a method is popped due to an exception throw. A method will either cause a
62 // MethodExited call-back or a MethodUnwind call-back when its activation is removed.
63 virtual void MethodUnwind(Thread* thread, const mirror::AbstractMethod* method,
64 uint32_t dex_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
65
66 // Call-back for when the dex pc moves in a method.
67 virtual void DexPcMoved(Thread* thread, mirror::Object* this_object,
68 const mirror::AbstractMethod* method, uint32_t new_dex_pc)
69 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
70
71 // Call-back when an exception is caught.
72 virtual void ExceptionCaught(Thread* thread, const ThrowLocation& throw_location,
73 mirror::AbstractMethod* catch_method, uint32_t catch_dex_pc,
74 mirror::Throwable* exception_object)
75 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
jeffhao725a9572012-11-13 18:20:12 -080076};
77
Ian Rogers62d6c772013-02-27 08:32:07 -080078// Instrumentation is a catch-all for when extra information is required from the runtime. The
79// typical use for instrumentation is for profiling and debugging. Instrumentation may add stubs
80// to method entry and exit, it may also force execution to be switched to the interpreter and
81// trigger deoptimization.
jeffhao725a9572012-11-13 18:20:12 -080082class Instrumentation {
83 public:
Ian Rogers62d6c772013-02-27 08:32:07 -080084 enum InstrumentationEvent {
85 kMethodEntered = 1,
86 kMethodExited = 2,
87 kMethodUnwind = 4,
88 kDexPcMoved = 8,
89 kExceptionCaught = 16
90 };
jeffhao725a9572012-11-13 18:20:12 -080091
Ian Rogers62d6c772013-02-27 08:32:07 -080092 Instrumentation() :
93 instrumentation_stubs_installed_(false), entry_exit_stubs_installed_(false),
94 interpreter_stubs_installed_(false),
95 interpret_only_(false), forced_interpret_only_(false),
96 have_method_entry_listeners_(false), have_method_exit_listeners_(false),
97 have_method_unwind_listeners_(false), have_dex_pc_listeners_(false),
98 have_exception_caught_listeners_(false) {}
jeffhao725a9572012-11-13 18:20:12 -080099
Ian Rogers62d6c772013-02-27 08:32:07 -0800100 // Add a listener to be notified of the masked together sent of instrumentation events. This
101 // suspend the runtime to install stubs. You are expected to hold the mutator lock as a proxy
102 // for saying you should have suspended all threads (installing stubs while threads are running
103 // will break).
104 void AddListener(InstrumentationListener* listener, uint32_t events)
105 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
106 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800107
Ian Rogers62d6c772013-02-27 08:32:07 -0800108 // Removes a listener possibly removing instrumentation stubs.
109 void RemoveListener(InstrumentationListener* listener, uint32_t events)
110 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
111 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800112
Ian Rogers62d6c772013-02-27 08:32:07 -0800113 // Update the code of a method respecting any installed stubs.
114 void UpdateMethodsCode(mirror::AbstractMethod* method, const void* code) const;
115
116 // Get the quick code for the given method. More efficient than asking the class linker as it
117 // will short-cut to GetCode if instrumentation and static method resolution stubs aren't
118 // installed.
119 const void* GetQuickCodeFor(const mirror::AbstractMethod* method) const
120 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
121
122 void ForceInterpretOnly() {
123 interpret_only_ = true;
124 forced_interpret_only_ = true;
125 }
126
127 // Called by AbstractMethod::Invoke to determine dispatch mechanism.
128 bool InterpretOnly() const {
129 return interpret_only_;
130 }
131
132 bool ShouldPortableCodeDeoptimize() const {
133 return instrumentation_stubs_installed_;
134 }
135
136 bool AreExitStubsInstalled() const {
137 return instrumentation_stubs_installed_;
138 }
139
Sebastien Hertz74109f62013-06-07 17:40:09 +0200140 bool HasMethodEntryListeners() const {
141 return have_method_entry_listeners_;
142 }
143
144 bool HasMethodExitListeners() const {
145 return have_method_exit_listeners_;
146 }
147
148 bool HasDexPcListeners() const {
149 return have_dex_pc_listeners_;
150 }
151
Ian Rogers62d6c772013-02-27 08:32:07 -0800152 // Inform listeners that a method has been entered. A dex PC is provided as we may install
153 // listeners into executing code and get method enter events for methods already on the stack.
154 void MethodEnterEvent(Thread* thread, mirror::Object* this_object,
155 const mirror::AbstractMethod* method, uint32_t dex_pc) const
156 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200157 if (UNLIKELY(HasMethodEntryListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800158 MethodEnterEventImpl(thread, this_object, method, dex_pc);
159 }
160 }
161
162 // Inform listeners that a method has been exited.
163 void MethodExitEvent(Thread* thread, mirror::Object* this_object,
164 const mirror::AbstractMethod* method, uint32_t dex_pc,
165 const JValue& return_value) const
166 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200167 if (UNLIKELY(HasMethodExitListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800168 MethodExitEventImpl(thread, this_object, method, dex_pc, return_value);
169 }
170 }
171
172 // Inform listeners that a method has been exited due to an exception.
173 void MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
174 const mirror::AbstractMethod* method, uint32_t dex_pc) const
175 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
176
177 // Inform listeners that the dex pc has moved (only supported by the interpreter).
178 void DexPcMovedEvent(Thread* thread, mirror::Object* this_object,
179 const mirror::AbstractMethod* method, uint32_t dex_pc) const
180 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200181 if (UNLIKELY(HasDexPcListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800182 DexPcMovedEventImpl(thread, this_object, method, dex_pc);
183 }
184 }
185
186 // Inform listeners that an exception was caught.
187 void ExceptionCaughtEvent(Thread* thread, const ThrowLocation& throw_location,
188 mirror::AbstractMethod* catch_method, uint32_t catch_dex_pc,
189 mirror::Throwable* exception_object)
190 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
191
192 // Called when an instrumented method is entered. The intended link register (lr) is saved so
193 // that returning causes a branch to the method exit stub. Generates method enter events.
194 void PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
195 mirror::AbstractMethod* method, uintptr_t lr)
196 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
197
198 // Called when an instrumented method is exited. Removes the pushed instrumentation frame
199 // returning the intended link register. Generates method exit events.
200 uint64_t PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc, uint64_t gpr_result,
201 uint64_t fpr_result)
202 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
203
204 // Pops an instrumentation frame from the current thread and generate an unwind event.
205 void PopMethodForUnwind(Thread* self, bool is_deoptimization) const
206 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
207
208 // Call back for configure stubs.
209 bool InstallStubsForClass(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800210
211 private:
Ian Rogers62d6c772013-02-27 08:32:07 -0800212 // Does the job of installing or removing instrumentation code within methods.
213 void ConfigureStubs(bool require_entry_exit_stubs, bool require_interpreter)
214 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
215 LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800216
Ian Rogers62d6c772013-02-27 08:32:07 -0800217 void MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
218 const mirror::AbstractMethod* method, uint32_t dex_pc) const
219 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
220 void MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
221 const mirror::AbstractMethod* method,
222 uint32_t dex_pc, const JValue& return_value) const
223 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
224 void DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object,
225 const mirror::AbstractMethod* method, uint32_t dex_pc) const
226 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800227
Ian Rogers62d6c772013-02-27 08:32:07 -0800228 // Have we hijacked AbstractMethod::code_ so that it calls instrumentation/interpreter code?
229 bool instrumentation_stubs_installed_;
230
231 // Have we hijacked AbstractMethod::code_ to reference the enter/exit stubs?
232 bool entry_exit_stubs_installed_;
233
234 // Have we hijacked AbstractMethod::code_ to reference the enter interpreter stub?
235 bool interpreter_stubs_installed_;
236
237 // Do we need the fidelity of events that we only get from running within the interpreter?
238 bool interpret_only_;
239
240 // Did the runtime request we only run in the interpreter? ie -Xint mode.
241 bool forced_interpret_only_;
242
243 // Do we have any listeners for method entry events? Short-cut to avoid taking the
244 // instrumentation_lock_.
245 bool have_method_entry_listeners_;
246
247 // Do we have any listeners for method exit events? Short-cut to avoid taking the
248 // instrumentation_lock_.
249 bool have_method_exit_listeners_;
250
251 // Do we have any listeners for method unwind events? Short-cut to avoid taking the
252 // instrumentation_lock_.
253 bool have_method_unwind_listeners_;
254
255 // Do we have any listeners for dex move events? Short-cut to avoid taking the
256 // instrumentation_lock_.
257 bool have_dex_pc_listeners_;
258
259 // Do we have any exception caught listeners? Short-cut to avoid taking the instrumentation_lock_.
260 bool have_exception_caught_listeners_;
261
262 // The event listeners, written to with the mutator_lock_ exclusively held.
263 std::list<InstrumentationListener*> method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
264 std::list<InstrumentationListener*> method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
265 std::list<InstrumentationListener*> method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
266 std::list<InstrumentationListener*> dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_);
267 std::list<InstrumentationListener*> exception_caught_listeners_ GUARDED_BY(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800268
269 DISALLOW_COPY_AND_ASSIGN(Instrumentation);
270};
271
Ian Rogers62d6c772013-02-27 08:32:07 -0800272// An element in the instrumentation side stack maintained in art::Thread.
273struct InstrumentationStackFrame {
274 InstrumentationStackFrame(mirror::Object* this_object, mirror::AbstractMethod* method,
275 uintptr_t return_pc, size_t frame_id)
276 : this_object_(this_object), method_(method), return_pc_(return_pc), frame_id_(frame_id) {
277 }
278
279 std::string Dump() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
280
281 mirror::Object* this_object_;
282 mirror::AbstractMethod* method_;
283 const uintptr_t return_pc_;
284 const size_t frame_id_;
285};
286
287} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -0800288} // namespace art
289
290#endif // ART_SRC_INSTRUMENTATION_H_