1 /*
2 * Copyright (C) 2017 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 "jni.h"
18
19 #include <iostream>
20
21 #include "android-base/logging.h"
22
23 #include "arch/context.h"
24 #include "art_method.h"
25 #include "base/macros.h"
26 #include "base/mutex.h"
27 #include "mirror/object-inl.h"
28 #include "mirror/string.h"
29 #include "monitor.h"
30 #include "scoped_thread_state_change-inl.h"
31 #include "stack.h"
32 #include "thread-current-inl.h"
33
34 namespace art {
35
Java_Main_testVisitLocks(JNIEnv *,jclass)36 extern "C" JNIEXPORT void JNICALL Java_Main_testVisitLocks(JNIEnv*, jclass) {
37 ScopedObjectAccess soa(Thread::Current());
38
39 class VisitLocks : public StackVisitor {
40 public:
41 VisitLocks(Thread* thread, Context* context)
42 : StackVisitor(thread, context, StackWalkKind::kIncludeInlinedFrames) {
43 }
44
45 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
46 ArtMethod* m = GetMethod();
47
48 // Ignore runtime methods.
49 if (m == nullptr || m->IsRuntimeMethod()) {
50 return true;
51 }
52
53 if (m->PrettyMethod() == "void TestSync.run()") {
54 // Interesting frame.
55 Monitor::VisitLocks(this, Callback, nullptr);
56 return false;
57 }
58
59 return true;
60 }
61
62 static void Callback(mirror::Object* obj, void*) REQUIRES_SHARED(Locks::mutator_lock_) {
63 CHECK(obj != nullptr);
64 CHECK(obj->IsString());
65 std::cerr << obj->AsString()->ToModifiedUtf8() << std::endl;
66 }
67 };
68 Context* context = Context::Create();
69 VisitLocks vl(soa.Self(), context);
70 vl.WalkStack();
71 delete context;
72 }
73
74 } // namespace art
75