blob: d20c7565226ab11b24764c36a71da75fc4bfa138 [file] [log] [blame]
Alex Light0fa17862017-10-24 13:43:05 -07001/* Copyright (C) 2017 The Android Open Source Project
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This file implements interfaces from the file jvmti.h. This implementation
5 * is licensed under the same terms as the file jvmti.h. The
6 * copyright and license information for the file jvmti.h follows.
7 *
8 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
9 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10 *
11 * This code is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 only, as
13 * published by the Free Software Foundation. Oracle designates this
14 * particular file as subject to the "Classpath" exception as provided
15 * by Oracle in the LICENSE file that accompanied this code.
16 *
17 * This code is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * version 2 for more details (a copy is included in the LICENSE file that
21 * accompanied this code).
22 *
23 * You should have received a copy of the GNU General Public License version
24 * 2 along with this work; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26 *
27 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
28 * or visit www.oracle.com if you need additional information or have any
29 * questions.
30 */
31
32#include <functional>
33
34#include "deopt_manager.h"
35
36#include "art_jvmti.h"
37#include "art_method-inl.h"
38#include "base/enums.h"
39#include "base/mutex-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080040#include "dex/dex_file_annotations.h"
David Sehr8c0961f2018-01-23 16:11:38 -080041#include "dex/modifiers.h"
Alex Light0fa17862017-10-24 13:43:05 -070042#include "events-inl.h"
Alex Light3b8aa772018-08-13 15:55:44 -070043#include "gc/heap.h"
44#include "gc/scoped_gc_critical_section.h"
Alex Light60fbefc2018-04-18 15:19:15 -070045#include "jit/jit.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010046#include "jni/jni_internal.h"
Alex Light0fa17862017-10-24 13:43:05 -070047#include "mirror/class-inl.h"
48#include "mirror/object_array-inl.h"
Alex Light0fa17862017-10-24 13:43:05 -070049#include "nativehelper/scoped_local_ref.h"
50#include "runtime_callbacks.h"
51#include "scoped_thread_state_change-inl.h"
52#include "thread-current-inl.h"
53#include "thread_list.h"
54#include "ti_phase.h"
55
56namespace openjdkjvmti {
57
58// TODO We should make this much more selective in the future so we only return true when we
Alex Lightf2858632018-04-02 11:28:50 -070059// actually care about the method at this time (ie active frames had locals changed). For now we
60// just assume that if anything has changed any frame's locals we care about all methods. If nothing
61// has we only care about methods with active breakpoints on them. In the future we should probably
62// rewrite all of this to instead do this at the ShadowFrame or thread granularity.
63bool JvmtiMethodInspectionCallback::IsMethodBeingInspected(art::ArtMethod* method) {
64 // Non-java-debuggable runtimes we need to assume that any method might not be debuggable and
65 // therefore potentially being inspected (due to inlines). If we are debuggable we rely hard on
66 // inlining not being done since we don't keep track of which methods get inlined where and simply
67 // look to see if the method is breakpointed.
68 return !art::Runtime::Current()->IsJavaDebuggable() ||
69 manager_->HaveLocalsChanged() ||
70 manager_->MethodHasBreakpoints(method);
Alex Light0fa17862017-10-24 13:43:05 -070071}
72
73bool JvmtiMethodInspectionCallback::IsMethodSafeToJit(art::ArtMethod* method) {
74 return !manager_->MethodHasBreakpoints(method);
75}
76
Alex Lightf2858632018-04-02 11:28:50 -070077bool JvmtiMethodInspectionCallback::MethodNeedsDebugVersion(
78 art::ArtMethod* method ATTRIBUTE_UNUSED) {
79 return true;
80}
81
Alex Light0fa17862017-10-24 13:43:05 -070082DeoptManager::DeoptManager()
Alex Light2ce6fc82017-12-18 16:42:36 -080083 : deoptimization_status_lock_("JVMTI_DeoptimizationStatusLock",
84 static_cast<art::LockLevel>(
85 art::LockLevel::kClassLinkerClassesLock + 1)),
Alex Light0fa17862017-10-24 13:43:05 -070086 deoptimization_condition_("JVMTI_DeoptimizationCondition", deoptimization_status_lock_),
87 performing_deoptimization_(false),
88 global_deopt_count_(0),
89 deopter_count_(0),
Alex Lightf2858632018-04-02 11:28:50 -070090 breakpoint_status_lock_("JVMTI_BreakpointStatusLock",
91 static_cast<art::LockLevel>(art::LockLevel::kAbortLock + 1)),
92 inspection_callback_(this),
David Srbeckyd25eb2c2018-07-19 12:17:04 +000093 set_local_variable_called_(false) { }
Alex Light0fa17862017-10-24 13:43:05 -070094
95void DeoptManager::Setup() {
96 art::ScopedThreadStateChange stsc(art::Thread::Current(),
97 art::ThreadState::kWaitingForDebuggerToAttach);
98 art::ScopedSuspendAll ssa("Add method Inspection Callback");
99 art::RuntimeCallbacks* callbacks = art::Runtime::Current()->GetRuntimeCallbacks();
100 callbacks->AddMethodInspectionCallback(&inspection_callback_);
101}
102
103void DeoptManager::Shutdown() {
104 art::ScopedThreadStateChange stsc(art::Thread::Current(),
105 art::ThreadState::kWaitingForDebuggerToAttach);
106 art::ScopedSuspendAll ssa("remove method Inspection Callback");
107 art::RuntimeCallbacks* callbacks = art::Runtime::Current()->GetRuntimeCallbacks();
108 callbacks->RemoveMethodInspectionCallback(&inspection_callback_);
109}
110
Alex Light2ce6fc82017-12-18 16:42:36 -0800111void DeoptManager::FinishSetup() {
112 art::Thread* self = art::Thread::Current();
113 art::MutexLock mu(self, deoptimization_status_lock_);
114
115 art::Runtime* runtime = art::Runtime::Current();
116 // See if we need to do anything.
117 if (!runtime->IsJavaDebuggable()) {
118 // See if we can enable all JVMTI functions. If this is false, only kArtTiVersion agents can be
119 // retrieved and they will all be best-effort.
120 if (PhaseUtil::GetPhaseUnchecked() == JVMTI_PHASE_ONLOAD) {
121 // We are still early enough to change the compiler options and get full JVMTI support.
122 LOG(INFO) << "Openjdkjvmti plugin loaded on a non-debuggable runtime. Changing runtime to "
123 << "debuggable state. Please pass '--debuggable' to dex2oat and "
124 << "'-Xcompiler-option --debuggable' to dalvikvm in the future.";
125 DCHECK(runtime->GetJit() == nullptr) << "Jit should not be running yet!";
126 runtime->AddCompilerOption("--debuggable");
127 runtime->SetJavaDebuggable(true);
128 } else {
129 LOG(WARNING) << "Openjdkjvmti plugin was loaded on a non-debuggable Runtime. Plugin was "
130 << "loaded too late to change runtime state to DEBUGGABLE. Only kArtTiVersion "
131 << "(0x" << std::hex << kArtTiVersion << ") environments are available. Some "
132 << "functionality might not work properly.";
Alex Light60fbefc2018-04-18 15:19:15 -0700133 if (runtime->GetJit() == nullptr &&
134 runtime->GetJITOptions()->UseJitCompilation() &&
135 !runtime->GetInstrumentation()->IsForcedInterpretOnly()) {
136 // If we don't have a jit we should try to start the jit for performance reasons. We only
137 // need to do this for late attach on non-debuggable processes because for debuggable
138 // processes we already rely on jit and we cannot force this jit to start if we are still in
139 // OnLoad since the runtime hasn't started up sufficiently. This is only expected to happen
140 // on userdebug/eng builds.
141 LOG(INFO) << "Attempting to start jit for openjdkjvmti plugin.";
142 runtime->CreateJit();
143 if (runtime->GetJit() == nullptr) {
144 LOG(WARNING) << "Could not start jit for openjdkjvmti plugin. This process might be "
145 << "quite slow as it is running entirely in the interpreter. Try running "
146 << "'setenforce 0' and restarting this process.";
147 }
148 }
Alex Light2ce6fc82017-12-18 16:42:36 -0800149 }
150 runtime->DeoptimizeBootImage();
151 }
152}
153
Alex Light0fa17862017-10-24 13:43:05 -0700154bool DeoptManager::MethodHasBreakpoints(art::ArtMethod* method) {
Alex Lightf2858632018-04-02 11:28:50 -0700155 art::MutexLock lk(art::Thread::Current(), breakpoint_status_lock_);
Alex Light0fa17862017-10-24 13:43:05 -0700156 return MethodHasBreakpointsLocked(method);
157}
158
159bool DeoptManager::MethodHasBreakpointsLocked(art::ArtMethod* method) {
Alex Light0fa17862017-10-24 13:43:05 -0700160 auto elem = breakpoint_status_.find(method);
161 return elem != breakpoint_status_.end() && elem->second != 0;
162}
163
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000164void DeoptManager::RemoveDeoptimizeAllMethods() {
Alex Light0fa17862017-10-24 13:43:05 -0700165 art::Thread* self = art::Thread::Current();
166 art::ScopedThreadSuspension sts(self, art::kSuspended);
167 deoptimization_status_lock_.ExclusiveLock(self);
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000168 RemoveDeoptimizeAllMethodsLocked(self);
Alex Light0fa17862017-10-24 13:43:05 -0700169}
170
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000171void DeoptManager::AddDeoptimizeAllMethods() {
Alex Light0fa17862017-10-24 13:43:05 -0700172 art::Thread* self = art::Thread::Current();
173 art::ScopedThreadSuspension sts(self, art::kSuspended);
174 deoptimization_status_lock_.ExclusiveLock(self);
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000175 AddDeoptimizeAllMethodsLocked(self);
Alex Light0fa17862017-10-24 13:43:05 -0700176}
177
178void DeoptManager::AddMethodBreakpoint(art::ArtMethod* method) {
179 DCHECK(method->IsInvokable());
180 DCHECK(!method->IsProxyMethod()) << method->PrettyMethod();
181 DCHECK(!method->IsNative()) << method->PrettyMethod();
182
183 art::Thread* self = art::Thread::Current();
184 method = method->GetCanonicalMethod();
185 bool is_default = method->IsDefault();
186
187 art::ScopedThreadSuspension sts(self, art::kSuspended);
188 deoptimization_status_lock_.ExclusiveLock(self);
Alex Lightf2858632018-04-02 11:28:50 -0700189 {
190 breakpoint_status_lock_.ExclusiveLock(self);
Alex Light0fa17862017-10-24 13:43:05 -0700191
Alex Lightf2858632018-04-02 11:28:50 -0700192 DCHECK_GT(deopter_count_, 0u) << "unexpected deotpimization request";
Alex Light0fa17862017-10-24 13:43:05 -0700193
Alex Lightf2858632018-04-02 11:28:50 -0700194 if (MethodHasBreakpointsLocked(method)) {
195 // Don't need to do anything extra.
196 breakpoint_status_[method]++;
197 // Another thread might be deoptimizing the very method we just added new breakpoints for.
198 // Wait for any deopts to finish before moving on.
199 breakpoint_status_lock_.ExclusiveUnlock(self);
200 WaitForDeoptimizationToFinish(self);
201 return;
202 }
203 breakpoint_status_[method] = 1;
204 breakpoint_status_lock_.ExclusiveUnlock(self);
Alex Light0fa17862017-10-24 13:43:05 -0700205 }
Alex Light0fa17862017-10-24 13:43:05 -0700206 auto instrumentation = art::Runtime::Current()->GetInstrumentation();
207 if (instrumentation->IsForcedInterpretOnly()) {
208 // We are already interpreting everything so no need to do anything.
209 deoptimization_status_lock_.ExclusiveUnlock(self);
210 return;
211 } else if (is_default) {
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000212 AddDeoptimizeAllMethodsLocked(self);
Alex Light0fa17862017-10-24 13:43:05 -0700213 } else {
214 PerformLimitedDeoptimization(self, method);
215 }
216}
217
218void DeoptManager::RemoveMethodBreakpoint(art::ArtMethod* method) {
219 DCHECK(method->IsInvokable()) << method->PrettyMethod();
220 DCHECK(!method->IsProxyMethod()) << method->PrettyMethod();
221 DCHECK(!method->IsNative()) << method->PrettyMethod();
222
223 art::Thread* self = art::Thread::Current();
224 method = method->GetCanonicalMethod();
225 bool is_default = method->IsDefault();
226
227 art::ScopedThreadSuspension sts(self, art::kSuspended);
228 // Ideally we should do a ScopedSuspendAll right here to get the full mutator_lock_ that we might
229 // need but since that is very heavy we will instead just use a condition variable to make sure we
230 // don't race with ourselves.
231 deoptimization_status_lock_.ExclusiveLock(self);
Alex Lightf2858632018-04-02 11:28:50 -0700232 bool is_last_breakpoint;
233 {
234 art::MutexLock mu(self, breakpoint_status_lock_);
Alex Light0fa17862017-10-24 13:43:05 -0700235
Alex Lightf2858632018-04-02 11:28:50 -0700236 DCHECK_GT(deopter_count_, 0u) << "unexpected deotpimization request";
237 DCHECK(MethodHasBreakpointsLocked(method)) << "Breakpoint on a method was removed without "
238 << "breakpoints present!";
239 breakpoint_status_[method] -= 1;
240 is_last_breakpoint = (breakpoint_status_[method] == 0);
241 }
Alex Light0fa17862017-10-24 13:43:05 -0700242 auto instrumentation = art::Runtime::Current()->GetInstrumentation();
Alex Light0fa17862017-10-24 13:43:05 -0700243 if (UNLIKELY(instrumentation->IsForcedInterpretOnly())) {
244 // We don't need to do anything since we are interpreting everything anyway.
245 deoptimization_status_lock_.ExclusiveUnlock(self);
246 return;
Alex Lightf2858632018-04-02 11:28:50 -0700247 } else if (is_last_breakpoint) {
Alex Light0fa17862017-10-24 13:43:05 -0700248 if (UNLIKELY(is_default)) {
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000249 RemoveDeoptimizeAllMethodsLocked(self);
Alex Light0fa17862017-10-24 13:43:05 -0700250 } else {
251 PerformLimitedUndeoptimization(self, method);
252 }
253 } else {
254 // Another thread might be deoptimizing the very methods we just removed breakpoints from. Wait
255 // for any deopts to finish before moving on.
256 WaitForDeoptimizationToFinish(self);
257 }
258}
259
260void DeoptManager::WaitForDeoptimizationToFinishLocked(art::Thread* self) {
261 while (performing_deoptimization_) {
262 deoptimization_condition_.Wait(self);
263 }
264}
265
266void DeoptManager::WaitForDeoptimizationToFinish(art::Thread* self) {
267 WaitForDeoptimizationToFinishLocked(self);
268 deoptimization_status_lock_.ExclusiveUnlock(self);
269}
270
Alex Light3b8aa772018-08-13 15:55:44 -0700271// Users should make sure that only gc-critical-section safe code is used while a
272// ScopedDeoptimizationContext exists.
Alex Light0fa17862017-10-24 13:43:05 -0700273class ScopedDeoptimizationContext : public art::ValueObject {
274 public:
275 ScopedDeoptimizationContext(art::Thread* self, DeoptManager* deopt)
276 RELEASE(deopt->deoptimization_status_lock_)
277 ACQUIRE(art::Locks::mutator_lock_)
278 ACQUIRE(art::Roles::uninterruptible_)
Alex Light3b8aa772018-08-13 15:55:44 -0700279 : self_(self),
280 deopt_(deopt),
281 critical_section_(self_, "JVMTI Deoptimizing methods"),
282 uninterruptible_cause_(nullptr) {
Alex Light0fa17862017-10-24 13:43:05 -0700283 deopt_->WaitForDeoptimizationToFinishLocked(self_);
284 DCHECK(!deopt->performing_deoptimization_)
285 << "Already performing deoptimization on another thread!";
286 // Use performing_deoptimization_ to keep track of the lock.
287 deopt_->performing_deoptimization_ = true;
288 deopt_->deoptimization_status_lock_.Unlock(self_);
Alex Light3b8aa772018-08-13 15:55:44 -0700289 uninterruptible_cause_ = critical_section_.Enter(art::gc::kGcCauseInstrumentation,
290 art::gc::kCollectorTypeCriticalSection);
Alex Light0fa17862017-10-24 13:43:05 -0700291 art::Runtime::Current()->GetThreadList()->SuspendAll("JMVTI Deoptimizing methods",
292 /*long_suspend*/ false);
Alex Light0fa17862017-10-24 13:43:05 -0700293 }
294
295 ~ScopedDeoptimizationContext()
296 RELEASE(art::Locks::mutator_lock_)
297 RELEASE(art::Roles::uninterruptible_) {
298 // Can be suspended again.
Alex Light3b8aa772018-08-13 15:55:44 -0700299 critical_section_.Exit(uninterruptible_cause_);
Alex Light0fa17862017-10-24 13:43:05 -0700300 // Release the mutator lock.
301 art::Runtime::Current()->GetThreadList()->ResumeAll();
302 // Let other threads know it's fine to proceed.
303 art::MutexLock lk(self_, deopt_->deoptimization_status_lock_);
304 deopt_->performing_deoptimization_ = false;
305 deopt_->deoptimization_condition_.Broadcast(self_);
306 }
307
308 private:
309 art::Thread* self_;
310 DeoptManager* deopt_;
Alex Light3b8aa772018-08-13 15:55:44 -0700311 art::gc::GCCriticalSection critical_section_;
Alex Light0fa17862017-10-24 13:43:05 -0700312 const char* uninterruptible_cause_;
313};
314
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000315void DeoptManager::AddDeoptimizeAllMethodsLocked(art::Thread* self) {
Alex Light0fa17862017-10-24 13:43:05 -0700316 global_deopt_count_++;
317 if (global_deopt_count_ == 1) {
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000318 PerformGlobalDeoptimization(self);
Alex Light0fa17862017-10-24 13:43:05 -0700319 } else {
320 WaitForDeoptimizationToFinish(self);
321 }
322}
323
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000324void DeoptManager::RemoveDeoptimizeAllMethodsLocked(art::Thread* self) {
Roland Levillainef012222017-06-21 16:28:06 +0100325 DCHECK_GT(global_deopt_count_, 0u) << "Request to remove non-existent global deoptimization!";
Alex Light0fa17862017-10-24 13:43:05 -0700326 global_deopt_count_--;
327 if (global_deopt_count_ == 0) {
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000328 PerformGlobalUndeoptimization(self);
Alex Light0fa17862017-10-24 13:43:05 -0700329 } else {
330 WaitForDeoptimizationToFinish(self);
331 }
332}
333
334void DeoptManager::PerformLimitedDeoptimization(art::Thread* self, art::ArtMethod* method) {
335 ScopedDeoptimizationContext sdc(self, this);
336 art::Runtime::Current()->GetInstrumentation()->Deoptimize(method);
337}
338
339void DeoptManager::PerformLimitedUndeoptimization(art::Thread* self, art::ArtMethod* method) {
340 ScopedDeoptimizationContext sdc(self, this);
341 art::Runtime::Current()->GetInstrumentation()->Undeoptimize(method);
342}
343
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000344void DeoptManager::PerformGlobalDeoptimization(art::Thread* self) {
Alex Light0fa17862017-10-24 13:43:05 -0700345 ScopedDeoptimizationContext sdc(self, this);
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000346 art::Runtime::Current()->GetInstrumentation()->DeoptimizeEverything(
347 kDeoptManagerInstrumentationKey);
Alex Light0fa17862017-10-24 13:43:05 -0700348}
349
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000350void DeoptManager::PerformGlobalUndeoptimization(art::Thread* self) {
Alex Light0fa17862017-10-24 13:43:05 -0700351 ScopedDeoptimizationContext sdc(self, this);
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000352 art::Runtime::Current()->GetInstrumentation()->UndeoptimizeEverything(
353 kDeoptManagerInstrumentationKey);
Alex Light0fa17862017-10-24 13:43:05 -0700354}
355
356
357void DeoptManager::RemoveDeoptimizationRequester() {
358 art::Thread* self = art::Thread::Current();
359 art::ScopedThreadStateChange sts(self, art::kSuspended);
360 deoptimization_status_lock_.ExclusiveLock(self);
361 DCHECK_GT(deopter_count_, 0u) << "Removing deoptimization requester without any being present";
362 deopter_count_--;
363 if (deopter_count_ == 0) {
364 ScopedDeoptimizationContext sdc(self, this);
365 // TODO Give this a real key.
366 art::Runtime::Current()->GetInstrumentation()->DisableDeoptimization("");
367 return;
368 } else {
369 deoptimization_status_lock_.ExclusiveUnlock(self);
370 }
371}
372
373void DeoptManager::AddDeoptimizationRequester() {
374 art::Thread* self = art::Thread::Current();
375 art::ScopedThreadStateChange stsc(self, art::kSuspended);
376 deoptimization_status_lock_.ExclusiveLock(self);
377 deopter_count_++;
378 if (deopter_count_ == 1) {
379 ScopedDeoptimizationContext sdc(self, this);
380 art::Runtime::Current()->GetInstrumentation()->EnableDeoptimization();
381 return;
382 } else {
383 deoptimization_status_lock_.ExclusiveUnlock(self);
384 }
385}
386
387void DeoptManager::DeoptimizeThread(art::Thread* target) {
388 art::Runtime::Current()->GetInstrumentation()->InstrumentThreadStack(target);
389}
390
Alex Light0e841182018-02-12 17:42:50 +0000391extern DeoptManager* gDeoptManager;
Alex Light0fa17862017-10-24 13:43:05 -0700392DeoptManager* DeoptManager::Get() {
Alex Light0e841182018-02-12 17:42:50 +0000393 return gDeoptManager;
Alex Light0fa17862017-10-24 13:43:05 -0700394}
395
396} // namespace openjdkjvmti