• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "instrumentation.h"
23 #include "jni.h"
24 #include "scoped_thread_state_change.h"
25 #include "stack.h"
26 #include "thread.h"
27 
28 namespace art {
29 namespace StackWalkConcurrentInstrument {
30 
31 std::atomic<bool> instrument_waiting = false;
32 std::atomic<bool> instrumented = false;
33 
34 // Spin lock.
WaitForInstrument()35 static void WaitForInstrument() REQUIRES_SHARED(Locks::mutator_lock_) {
36   ScopedThreadSuspension sts(Thread::Current(), ThreadState::kWaitingForDeoptimization);
37   instrument_waiting = true;
38   while (!instrumented) {
39   }
40 }
41 
42 class SelfStackWalkVisitor : public StackVisitor {
43  public:
REQUIRES_SHARED(Locks::mutator_lock_)44   explicit SelfStackWalkVisitor(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_)
45       : StackVisitor(thread, Context::Create(), StackWalkKind::kIncludeInlinedFrames) {}
46 
VisitFrame()47   bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
48     if (GetMethod()->GetNameView() == "$noinline$f") {
49       CHECK(!found_f_);
50       found_f_ = true;
51     } else if (GetMethod()->GetNameView() == "$noinline$g") {
52       CHECK(!found_g_);
53       found_g_ = true;
54       WaitForInstrument();
55     } else if (GetMethod()->GetNameView() == "$noinline$h") {
56       CHECK(!found_h_);
57       found_h_ = true;
58     }
59     return true;
60   }
61 
62   bool found_f_ = false;
63   bool found_g_ = false;
64   bool found_h_ = false;
65 };
66 
Java_Main_resetTest(JNIEnv *,jobject)67 extern "C" JNIEXPORT void JNICALL Java_Main_resetTest(JNIEnv*, jobject) {
68   instrument_waiting = false;
69   instrumented = false;
70 }
71 
Java_Main_doSelfStackWalk(JNIEnv *,jobject)72 extern "C" JNIEXPORT void JNICALL Java_Main_doSelfStackWalk(JNIEnv*, jobject) {
73   ScopedObjectAccess soa(Thread::Current());
74   SelfStackWalkVisitor sswv(Thread::Current());
75   sswv.WalkStack();
76   CHECK(sswv.found_f_);
77   CHECK(sswv.found_g_);
78   CHECK(sswv.found_h_);
79 }
Java_Main_waitAndInstrumentStack(JNIEnv *,jobject,jobject target)80 extern "C" JNIEXPORT void JNICALL Java_Main_waitAndInstrumentStack(JNIEnv*,
81                                                                    jobject,
82                                                                    jobject target) {
83   while (!instrument_waiting) {
84   }
85   Thread* other = Runtime::Current()->GetThreadList()->SuspendThreadByPeer(
86       target, SuspendReason::kInternal);
87   CHECK(other != nullptr);
88   ScopedSuspendAll ssa(__FUNCTION__);
89   Runtime::Current()->GetInstrumentation()->InstrumentThreadStack(other,
90                                                                   /* force_deopt= */ false);
91   bool resumed = art::Runtime::Current()->GetThreadList()->Resume(other, SuspendReason::kInternal);
92   CHECK(resumed);
93   instrumented = true;
94   return;
95 }
96 
97 }  // namespace StackWalkConcurrentInstrument
98 }  // namespace art
99