blob: d456d83368ac37ed2a17013247aeead67974d2e4 [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.";
Andreas Gampec6bd42a2018-11-07 13:39:41 -0800142 // Note: use rwx allowed = true, because if this is the system server, we will not be
143 // allowed to allocate any JIT code cache, anyways.
144 runtime->CreateJitCodeCache(/*rwx_memory_allowed=*/true);
Alex Light60fbefc2018-04-18 15:19:15 -0700145 runtime->CreateJit();
146 if (runtime->GetJit() == nullptr) {
147 LOG(WARNING) << "Could not start jit for openjdkjvmti plugin. This process might be "
148 << "quite slow as it is running entirely in the interpreter. Try running "
149 << "'setenforce 0' and restarting this process.";
150 }
151 }
Alex Light2ce6fc82017-12-18 16:42:36 -0800152 }
153 runtime->DeoptimizeBootImage();
154 }
155}
156
Alex Light0fa17862017-10-24 13:43:05 -0700157bool DeoptManager::MethodHasBreakpoints(art::ArtMethod* method) {
Alex Lightf2858632018-04-02 11:28:50 -0700158 art::MutexLock lk(art::Thread::Current(), breakpoint_status_lock_);
Alex Light0fa17862017-10-24 13:43:05 -0700159 return MethodHasBreakpointsLocked(method);
160}
161
162bool DeoptManager::MethodHasBreakpointsLocked(art::ArtMethod* method) {
Alex Light0fa17862017-10-24 13:43:05 -0700163 auto elem = breakpoint_status_.find(method);
164 return elem != breakpoint_status_.end() && elem->second != 0;
165}
166
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000167void DeoptManager::RemoveDeoptimizeAllMethods() {
Alex Light0fa17862017-10-24 13:43:05 -0700168 art::Thread* self = art::Thread::Current();
169 art::ScopedThreadSuspension sts(self, art::kSuspended);
170 deoptimization_status_lock_.ExclusiveLock(self);
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000171 RemoveDeoptimizeAllMethodsLocked(self);
Alex Light0fa17862017-10-24 13:43:05 -0700172}
173
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000174void DeoptManager::AddDeoptimizeAllMethods() {
Alex Light0fa17862017-10-24 13:43:05 -0700175 art::Thread* self = art::Thread::Current();
176 art::ScopedThreadSuspension sts(self, art::kSuspended);
177 deoptimization_status_lock_.ExclusiveLock(self);
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000178 AddDeoptimizeAllMethodsLocked(self);
Alex Light0fa17862017-10-24 13:43:05 -0700179}
180
181void DeoptManager::AddMethodBreakpoint(art::ArtMethod* method) {
182 DCHECK(method->IsInvokable());
183 DCHECK(!method->IsProxyMethod()) << method->PrettyMethod();
184 DCHECK(!method->IsNative()) << method->PrettyMethod();
185
186 art::Thread* self = art::Thread::Current();
187 method = method->GetCanonicalMethod();
188 bool is_default = method->IsDefault();
189
190 art::ScopedThreadSuspension sts(self, art::kSuspended);
191 deoptimization_status_lock_.ExclusiveLock(self);
Alex Lightf2858632018-04-02 11:28:50 -0700192 {
193 breakpoint_status_lock_.ExclusiveLock(self);
Alex Light0fa17862017-10-24 13:43:05 -0700194
Alex Lightf2858632018-04-02 11:28:50 -0700195 DCHECK_GT(deopter_count_, 0u) << "unexpected deotpimization request";
Alex Light0fa17862017-10-24 13:43:05 -0700196
Alex Lightf2858632018-04-02 11:28:50 -0700197 if (MethodHasBreakpointsLocked(method)) {
198 // Don't need to do anything extra.
199 breakpoint_status_[method]++;
200 // Another thread might be deoptimizing the very method we just added new breakpoints for.
201 // Wait for any deopts to finish before moving on.
202 breakpoint_status_lock_.ExclusiveUnlock(self);
203 WaitForDeoptimizationToFinish(self);
204 return;
205 }
206 breakpoint_status_[method] = 1;
207 breakpoint_status_lock_.ExclusiveUnlock(self);
Alex Light0fa17862017-10-24 13:43:05 -0700208 }
Alex Light0fa17862017-10-24 13:43:05 -0700209 auto instrumentation = art::Runtime::Current()->GetInstrumentation();
210 if (instrumentation->IsForcedInterpretOnly()) {
211 // We are already interpreting everything so no need to do anything.
212 deoptimization_status_lock_.ExclusiveUnlock(self);
213 return;
214 } else if (is_default) {
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000215 AddDeoptimizeAllMethodsLocked(self);
Alex Light0fa17862017-10-24 13:43:05 -0700216 } else {
217 PerformLimitedDeoptimization(self, method);
218 }
219}
220
221void DeoptManager::RemoveMethodBreakpoint(art::ArtMethod* method) {
222 DCHECK(method->IsInvokable()) << method->PrettyMethod();
223 DCHECK(!method->IsProxyMethod()) << method->PrettyMethod();
224 DCHECK(!method->IsNative()) << method->PrettyMethod();
225
226 art::Thread* self = art::Thread::Current();
227 method = method->GetCanonicalMethod();
228 bool is_default = method->IsDefault();
229
230 art::ScopedThreadSuspension sts(self, art::kSuspended);
231 // Ideally we should do a ScopedSuspendAll right here to get the full mutator_lock_ that we might
232 // need but since that is very heavy we will instead just use a condition variable to make sure we
233 // don't race with ourselves.
234 deoptimization_status_lock_.ExclusiveLock(self);
Alex Lightf2858632018-04-02 11:28:50 -0700235 bool is_last_breakpoint;
236 {
237 art::MutexLock mu(self, breakpoint_status_lock_);
Alex Light0fa17862017-10-24 13:43:05 -0700238
Alex Lightf2858632018-04-02 11:28:50 -0700239 DCHECK_GT(deopter_count_, 0u) << "unexpected deotpimization request";
240 DCHECK(MethodHasBreakpointsLocked(method)) << "Breakpoint on a method was removed without "
241 << "breakpoints present!";
242 breakpoint_status_[method] -= 1;
243 is_last_breakpoint = (breakpoint_status_[method] == 0);
244 }
Alex Light0fa17862017-10-24 13:43:05 -0700245 auto instrumentation = art::Runtime::Current()->GetInstrumentation();
Alex Light0fa17862017-10-24 13:43:05 -0700246 if (UNLIKELY(instrumentation->IsForcedInterpretOnly())) {
247 // We don't need to do anything since we are interpreting everything anyway.
248 deoptimization_status_lock_.ExclusiveUnlock(self);
249 return;
Alex Lightf2858632018-04-02 11:28:50 -0700250 } else if (is_last_breakpoint) {
Alex Light0fa17862017-10-24 13:43:05 -0700251 if (UNLIKELY(is_default)) {
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000252 RemoveDeoptimizeAllMethodsLocked(self);
Alex Light0fa17862017-10-24 13:43:05 -0700253 } else {
254 PerformLimitedUndeoptimization(self, method);
255 }
256 } else {
257 // Another thread might be deoptimizing the very methods we just removed breakpoints from. Wait
258 // for any deopts to finish before moving on.
259 WaitForDeoptimizationToFinish(self);
260 }
261}
262
263void DeoptManager::WaitForDeoptimizationToFinishLocked(art::Thread* self) {
264 while (performing_deoptimization_) {
265 deoptimization_condition_.Wait(self);
266 }
267}
268
269void DeoptManager::WaitForDeoptimizationToFinish(art::Thread* self) {
270 WaitForDeoptimizationToFinishLocked(self);
271 deoptimization_status_lock_.ExclusiveUnlock(self);
272}
273
Alex Light3b8aa772018-08-13 15:55:44 -0700274// Users should make sure that only gc-critical-section safe code is used while a
275// ScopedDeoptimizationContext exists.
Alex Light0fa17862017-10-24 13:43:05 -0700276class ScopedDeoptimizationContext : public art::ValueObject {
277 public:
278 ScopedDeoptimizationContext(art::Thread* self, DeoptManager* deopt)
279 RELEASE(deopt->deoptimization_status_lock_)
280 ACQUIRE(art::Locks::mutator_lock_)
281 ACQUIRE(art::Roles::uninterruptible_)
Alex Light3b8aa772018-08-13 15:55:44 -0700282 : self_(self),
283 deopt_(deopt),
284 critical_section_(self_, "JVMTI Deoptimizing methods"),
285 uninterruptible_cause_(nullptr) {
Alex Light0fa17862017-10-24 13:43:05 -0700286 deopt_->WaitForDeoptimizationToFinishLocked(self_);
287 DCHECK(!deopt->performing_deoptimization_)
288 << "Already performing deoptimization on another thread!";
289 // Use performing_deoptimization_ to keep track of the lock.
290 deopt_->performing_deoptimization_ = true;
291 deopt_->deoptimization_status_lock_.Unlock(self_);
Alex Light3b8aa772018-08-13 15:55:44 -0700292 uninterruptible_cause_ = critical_section_.Enter(art::gc::kGcCauseInstrumentation,
293 art::gc::kCollectorTypeCriticalSection);
Alex Light0fa17862017-10-24 13:43:05 -0700294 art::Runtime::Current()->GetThreadList()->SuspendAll("JMVTI Deoptimizing methods",
Andreas Gampe6e897762018-10-16 13:09:32 -0700295 /*long_suspend=*/ false);
Alex Light0fa17862017-10-24 13:43:05 -0700296 }
297
298 ~ScopedDeoptimizationContext()
299 RELEASE(art::Locks::mutator_lock_)
300 RELEASE(art::Roles::uninterruptible_) {
301 // Can be suspended again.
Alex Light3b8aa772018-08-13 15:55:44 -0700302 critical_section_.Exit(uninterruptible_cause_);
Alex Light0fa17862017-10-24 13:43:05 -0700303 // Release the mutator lock.
304 art::Runtime::Current()->GetThreadList()->ResumeAll();
305 // Let other threads know it's fine to proceed.
306 art::MutexLock lk(self_, deopt_->deoptimization_status_lock_);
307 deopt_->performing_deoptimization_ = false;
308 deopt_->deoptimization_condition_.Broadcast(self_);
309 }
310
311 private:
312 art::Thread* self_;
313 DeoptManager* deopt_;
Alex Light3b8aa772018-08-13 15:55:44 -0700314 art::gc::GCCriticalSection critical_section_;
Alex Light0fa17862017-10-24 13:43:05 -0700315 const char* uninterruptible_cause_;
316};
317
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000318void DeoptManager::AddDeoptimizeAllMethodsLocked(art::Thread* self) {
Alex Light0fa17862017-10-24 13:43:05 -0700319 global_deopt_count_++;
320 if (global_deopt_count_ == 1) {
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000321 PerformGlobalDeoptimization(self);
Alex Light0fa17862017-10-24 13:43:05 -0700322 } else {
323 WaitForDeoptimizationToFinish(self);
324 }
325}
326
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000327void DeoptManager::RemoveDeoptimizeAllMethodsLocked(art::Thread* self) {
Roland Levillainef012222017-06-21 16:28:06 +0100328 DCHECK_GT(global_deopt_count_, 0u) << "Request to remove non-existent global deoptimization!";
Alex Light0fa17862017-10-24 13:43:05 -0700329 global_deopt_count_--;
330 if (global_deopt_count_ == 0) {
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000331 PerformGlobalUndeoptimization(self);
Alex Light0fa17862017-10-24 13:43:05 -0700332 } else {
333 WaitForDeoptimizationToFinish(self);
334 }
335}
336
337void DeoptManager::PerformLimitedDeoptimization(art::Thread* self, art::ArtMethod* method) {
338 ScopedDeoptimizationContext sdc(self, this);
339 art::Runtime::Current()->GetInstrumentation()->Deoptimize(method);
340}
341
342void DeoptManager::PerformLimitedUndeoptimization(art::Thread* self, art::ArtMethod* method) {
343 ScopedDeoptimizationContext sdc(self, this);
344 art::Runtime::Current()->GetInstrumentation()->Undeoptimize(method);
345}
346
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000347void DeoptManager::PerformGlobalDeoptimization(art::Thread* self) {
Alex Light0fa17862017-10-24 13:43:05 -0700348 ScopedDeoptimizationContext sdc(self, this);
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000349 art::Runtime::Current()->GetInstrumentation()->DeoptimizeEverything(
350 kDeoptManagerInstrumentationKey);
Alex Light0fa17862017-10-24 13:43:05 -0700351}
352
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000353void DeoptManager::PerformGlobalUndeoptimization(art::Thread* self) {
Alex Light0fa17862017-10-24 13:43:05 -0700354 ScopedDeoptimizationContext sdc(self, this);
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000355 art::Runtime::Current()->GetInstrumentation()->UndeoptimizeEverything(
356 kDeoptManagerInstrumentationKey);
Alex Light0fa17862017-10-24 13:43:05 -0700357}
358
359
360void DeoptManager::RemoveDeoptimizationRequester() {
361 art::Thread* self = art::Thread::Current();
362 art::ScopedThreadStateChange sts(self, art::kSuspended);
363 deoptimization_status_lock_.ExclusiveLock(self);
364 DCHECK_GT(deopter_count_, 0u) << "Removing deoptimization requester without any being present";
365 deopter_count_--;
366 if (deopter_count_ == 0) {
367 ScopedDeoptimizationContext sdc(self, this);
368 // TODO Give this a real key.
369 art::Runtime::Current()->GetInstrumentation()->DisableDeoptimization("");
370 return;
371 } else {
372 deoptimization_status_lock_.ExclusiveUnlock(self);
373 }
374}
375
376void DeoptManager::AddDeoptimizationRequester() {
377 art::Thread* self = art::Thread::Current();
378 art::ScopedThreadStateChange stsc(self, art::kSuspended);
379 deoptimization_status_lock_.ExclusiveLock(self);
380 deopter_count_++;
381 if (deopter_count_ == 1) {
382 ScopedDeoptimizationContext sdc(self, this);
383 art::Runtime::Current()->GetInstrumentation()->EnableDeoptimization();
384 return;
385 } else {
386 deoptimization_status_lock_.ExclusiveUnlock(self);
387 }
388}
389
390void DeoptManager::DeoptimizeThread(art::Thread* target) {
391 art::Runtime::Current()->GetInstrumentation()->InstrumentThreadStack(target);
392}
393
Alex Light0e841182018-02-12 17:42:50 +0000394extern DeoptManager* gDeoptManager;
Alex Light0fa17862017-10-24 13:43:05 -0700395DeoptManager* DeoptManager::Get() {
Alex Light0e841182018-02-12 17:42:50 +0000396 return gDeoptManager;
Alex Light0fa17862017-10-24 13:43:05 -0700397}
398
399} // namespace openjdkjvmti