Home | History | Annotate | Download | only in StackWalk
      1 /*
      2  * Copyright (C) 2011 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 <stdio.h>
     18 
     19 #include "UniquePtr.h"
     20 #include "class_linker.h"
     21 #include "gc_map.h"
     22 #include "mirror/art_method.h"
     23 #include "mirror/art_method-inl.h"
     24 #include "mirror/class-inl.h"
     25 #include "mirror/object_array-inl.h"
     26 #include "mirror/object-inl.h"
     27 #include "object_utils.h"
     28 #include "jni.h"
     29 #include "scoped_thread_state_change.h"
     30 
     31 namespace art {
     32 
     33 #define REG(mh, reg_bitmap, reg) \
     34     (((reg) < mh.GetCodeItem()->registers_size_) && \
     35      ((*((reg_bitmap) + (reg)/8) >> ((reg) % 8) ) & 0x01))
     36 
     37 #define CHECK_REGS(...) if (!IsShadowFrame()) { \
     38     int t[] = {__VA_ARGS__}; \
     39     int t_size = sizeof(t) / sizeof(*t); \
     40     for (int i = 0; i < t_size; ++i) \
     41       CHECK(REG(mh, reg_bitmap, t[i])) << "Error: Reg " << i << " is not in RegisterMap"; \
     42   }
     43 
     44 static int gJava_StackWalk_refmap_calls = 0;
     45 
     46 struct TestReferenceMapVisitor : public StackVisitor {
     47   explicit TestReferenceMapVisitor(Thread* thread)
     48       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
     49       : StackVisitor(thread, NULL) {
     50   }
     51 
     52   bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
     53     mirror::ArtMethod* m = GetMethod();
     54     CHECK(m != NULL);
     55     LOG(INFO) << "At " << PrettyMethod(m, false);
     56 
     57     if (m->IsCalleeSaveMethod() || m->IsNative()) {
     58       LOG(WARNING) << "no PC for " << PrettyMethod(m);
     59       CHECK_EQ(GetDexPc(), DexFile::kDexNoIndex);
     60       return true;
     61     }
     62     const uint8_t* reg_bitmap = NULL;
     63     if (!IsShadowFrame()) {
     64       NativePcOffsetToReferenceMap map(m->GetNativeGcMap());
     65       reg_bitmap = map.FindBitMap(GetNativePcOffset());
     66     }
     67     MethodHelper mh(m);
     68     StringPiece m_name(mh.GetName());
     69 
     70     // Given the method name and the number of times the method has been called,
     71     // we know the Dex registers with live reference values. Assert that what we
     72     // find is what is expected.
     73     if (m_name == "f") {
     74       if (gJava_StackWalk_refmap_calls == 1) {
     75         CHECK_EQ(1U, GetDexPc());
     76         CHECK_REGS(1);
     77       } else {
     78         CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
     79         CHECK_EQ(5U, GetDexPc());
     80         CHECK_REGS(1);
     81       }
     82     } else if (m_name == "g") {
     83       if (gJava_StackWalk_refmap_calls == 1) {
     84         CHECK_EQ(0xcU, GetDexPc());
     85         CHECK_REGS(0, 2);  // Note that v1 is not in the minimal root set
     86       } else {
     87         CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
     88         CHECK_EQ(0xcU, GetDexPc());
     89         CHECK_REGS(0, 2);
     90       }
     91     } else if (m_name == "shlemiel") {
     92       if (gJava_StackWalk_refmap_calls == 1) {
     93         CHECK_EQ(0x380U, GetDexPc());
     94         CHECK_REGS(2, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 21, 25);
     95       } else {
     96         CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
     97         CHECK_EQ(0x380U, GetDexPc());
     98         CHECK_REGS(2, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 21, 25);
     99       }
    100     }
    101     LOG(INFO) << reinterpret_cast<const void*>(reg_bitmap);
    102 
    103     return true;
    104   }
    105 };
    106 
    107 extern "C" JNIEXPORT jint JNICALL Java_StackWalk_refmap(JNIEnv*, jobject, jint count) {
    108   ScopedObjectAccess soa(Thread::Current());
    109   CHECK_EQ(count, 0);
    110   gJava_StackWalk_refmap_calls++;
    111 
    112   // Visitor
    113   TestReferenceMapVisitor mapper(soa.Self());
    114   mapper.WalkStack();
    115 
    116   return count + 1;
    117 }
    118 
    119 extern "C" JNIEXPORT jint JNICALL Java_StackWalk2_refmap2(JNIEnv*, jobject, jint count) {
    120   ScopedObjectAccess soa(Thread::Current());
    121   gJava_StackWalk_refmap_calls++;
    122 
    123   // Visitor
    124   TestReferenceMapVisitor mapper(soa.Self());
    125   mapper.WalkStack();
    126 
    127   return count + 1;
    128 }
    129 
    130 }  // namespace art
    131