1 /*
2  * Copyright (C) 2015 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 "arch/context.h"
18 #include "art_method-inl.h"
19 #include "jni.h"
20 #include "scoped_thread_state_change.h"
21 #include "stack.h"
22 #include "thread.h"
23 
24 namespace art {
25 
26 namespace {
27 
28 class TestVisitor : public StackVisitor {
29  public:
TestVisitor(const ScopedObjectAccess & soa,Context * context,jobject expected_value)30   TestVisitor(const ScopedObjectAccess& soa, Context* context, jobject expected_value)
31       SHARED_REQUIRES(Locks::mutator_lock_)
32       : StackVisitor(soa.Self(), context, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
33         expected_value_(expected_value),
34         found_(false),
35         soa_(soa) {}
36 
VisitFrame()37   bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) {
38     ArtMethod* m = GetMethod();
39     std::string m_name(m->GetName());
40 
41     if (m_name == "testCase") {
42       found_ = true;
43       uint32_t value = 0;
44       CHECK(GetVReg(m, 1, kReferenceVReg, &value));
45       CHECK_EQ(reinterpret_cast<mirror::Object*>(value),
46                soa_.Decode<mirror::Object*>(expected_value_));
47     }
48     return true;
49   }
50 
51   jobject expected_value_;
52   bool found_;
53   const ScopedObjectAccess& soa_;
54 };
55 
56 }  // namespace
57 
Java_Main_lookForMyRegisters(JNIEnv *,jclass,jobject value)58 extern "C" JNIEXPORT void JNICALL Java_Main_lookForMyRegisters(JNIEnv*, jclass, jobject value) {
59   ScopedObjectAccess soa(Thread::Current());
60   std::unique_ptr<Context> context(Context::Create());
61   TestVisitor visitor(soa, context.get(), value);
62   visitor.WalkStack();
63   CHECK(visitor.found_);
64 }
65 
66 }  // namespace art
67