blob: a10fe2e90524bc6623cc23e0ad037de65c83abd1 [file] [log] [blame]
Nicolas Geoffraye91e7952020-01-23 10:15:56 +00001/*
2 * Copyright 2020 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#include <atomic>
18#include <string_view>
19
20#include "arch/context.h"
21#include "art_method-inl.h"
22#include "jni.h"
23#include "scoped_thread_state_change.h"
24#include "stack.h"
25#include "thread.h"
26
27namespace art {
28namespace StackWalkConcurrentInstrument {
29
30std::atomic<bool> instrument_waiting = false;
31std::atomic<bool> instrumented = false;
32
33// Spin lock.
34static void WaitForInstrument() REQUIRES_SHARED(Locks::mutator_lock_) {
35 ScopedThreadSuspension sts(Thread::Current(), ThreadState::kWaitingForDeoptimization);
36 instrument_waiting = true;
37 while (!instrumented) {
38 }
39}
40
41class SelfStackWalkVisitor : public StackVisitor {
42 public:
43 explicit SelfStackWalkVisitor(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_)
44 : StackVisitor(thread, Context::Create(), StackWalkKind::kIncludeInlinedFrames) {}
45
46 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
47 if (GetMethod()->GetNameView() == "$noinline$f") {
48 CHECK(!found_f_);
49 found_f_ = true;
50 } else if (GetMethod()->GetNameView() == "$noinline$g") {
51 CHECK(!found_g_);
52 found_g_ = true;
53 WaitForInstrument();
54 } else if (GetMethod()->GetNameView() == "$noinline$h") {
55 CHECK(!found_h_);
56 found_h_ = true;
57 }
58 return true;
59 }
60
61 bool found_f_ = false;
62 bool found_g_ = false;
63 bool found_h_ = false;
64};
65
66extern "C" JNIEXPORT void JNICALL Java_Main_resetTest(JNIEnv*, jobject) {
67 instrument_waiting = false;
68 instrumented = false;
69}
70
71extern "C" JNIEXPORT void JNICALL Java_Main_doSelfStackWalk(JNIEnv*, jobject) {
72 ScopedObjectAccess soa(Thread::Current());
73 SelfStackWalkVisitor sswv(Thread::Current());
74 sswv.WalkStack();
75 CHECK(sswv.found_f_);
76 CHECK(sswv.found_g_);
77 CHECK(sswv.found_h_);
78}
79extern "C" JNIEXPORT void JNICALL Java_Main_waitAndDeopt(JNIEnv*, jobject, jobject target) {
80 while (!instrument_waiting) {
81 }
82 bool timed_out = false;
83 Thread* other = Runtime::Current()->GetThreadList()->SuspendThreadByPeer(
Hans Boehm9d27fbc2021-05-21 09:23:38 -070084 target, SuspendReason::kInternal, &timed_out);
Nicolas Geoffraye91e7952020-01-23 10:15:56 +000085 CHECK(!timed_out);
86 CHECK(other != nullptr);
87 ScopedSuspendAll ssa(__FUNCTION__);
88 Runtime::Current()->GetInstrumentation()->InstrumentThreadStack(other);
89 MutexLock mu(Thread::Current(), *Locks::thread_suspend_count_lock_);
90 bool updated = other->ModifySuspendCount(Thread::Current(), -1, nullptr, SuspendReason::kInternal);
91 CHECK(updated);
92 instrumented = true;
93 return;
94}
95
96} // namespace StackWalkConcurrentInstrument
97} // namespace art