Home | History | Annotate | Download | only in native
      1 /*
      2  * Copyright (C) 2008 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_internal.h"
     18 #include "nth_caller_visitor.h"
     19 #include "mirror/art_method-inl.h"
     20 #include "mirror/class-inl.h"
     21 #include "mirror/class_loader.h"
     22 #include "mirror/object-inl.h"
     23 #include "scoped_fast_native_object_access.h"
     24 #include "scoped_thread_state_change.h"
     25 #include "thread_list.h"
     26 
     27 namespace art {
     28 
     29 static jobject GetThreadStack(const ScopedFastNativeObjectAccess& soa, jobject peer)
     30     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
     31   jobject trace = nullptr;
     32   if (soa.Decode<mirror::Object*>(peer) == soa.Self()->GetPeer()) {
     33     trace = soa.Self()->CreateInternalStackTrace<false>(soa);
     34   } else {
     35     // Suspend thread to build stack trace.
     36     soa.Self()->TransitionFromRunnableToSuspended(kNative);
     37     ThreadList* thread_list = Runtime::Current()->GetThreadList();
     38     bool timed_out;
     39     Thread* thread;
     40     {
     41       // Take suspend thread lock to avoid races with threads trying to suspend this one.
     42       MutexLock mu(soa.Self(), *Locks::thread_list_suspend_thread_lock_);
     43       thread = thread_list->SuspendThreadByPeer(peer, true, false, &timed_out);
     44     }
     45     if (thread != nullptr) {
     46       // Must be runnable to create returned array.
     47       CHECK_EQ(soa.Self()->TransitionFromSuspendedToRunnable(), kNative);
     48       trace = thread->CreateInternalStackTrace<false>(soa);
     49       soa.Self()->TransitionFromRunnableToSuspended(kNative);
     50       // Restart suspended thread.
     51       thread_list->Resume(thread, false);
     52     } else {
     53       if (timed_out) {
     54         LOG(ERROR) << "Trying to get thread's stack failed as the thread failed to suspend within a "
     55             "generous timeout.";
     56       }
     57     }
     58     CHECK_EQ(soa.Self()->TransitionFromSuspendedToRunnable(), kNative);
     59   }
     60   return trace;
     61 }
     62 
     63 static jint VMStack_fillStackTraceElements(JNIEnv* env, jclass, jobject javaThread,
     64                                            jobjectArray javaSteArray) {
     65   ScopedFastNativeObjectAccess soa(env);
     66   jobject trace = GetThreadStack(soa, javaThread);
     67   if (trace == nullptr) {
     68     return 0;
     69   }
     70   int32_t depth;
     71   Thread::InternalStackTraceToStackTraceElementArray(soa, trace, javaSteArray, &depth);
     72   return depth;
     73 }
     74 
     75 // Returns the defining class loader of the caller's caller.
     76 static jobject VMStack_getCallingClassLoader(JNIEnv* env, jclass) {
     77   ScopedFastNativeObjectAccess soa(env);
     78   NthCallerVisitor visitor(soa.Self(), 2);
     79   visitor.WalkStack();
     80   if (UNLIKELY(visitor.caller == nullptr)) {
     81     // The caller is an attached native thread.
     82     return nullptr;
     83   }
     84   return soa.AddLocalReference<jobject>(visitor.caller->GetDeclaringClass()->GetClassLoader());
     85 }
     86 
     87 static jobject VMStack_getClosestUserClassLoader(JNIEnv* env, jclass, jobject javaBootstrap,
     88                                                  jobject javaSystem) {
     89   struct ClosestUserClassLoaderVisitor : public StackVisitor {
     90     ClosestUserClassLoaderVisitor(Thread* thread, mirror::Object* bootstrap, mirror::Object* system)
     91       : StackVisitor(thread, NULL), bootstrap(bootstrap), system(system), class_loader(NULL) {}
     92 
     93     bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
     94       DCHECK(class_loader == NULL);
     95       mirror::Class* c = GetMethod()->GetDeclaringClass();
     96       mirror::Object* cl = c->GetClassLoader();
     97       if (cl != NULL && cl != bootstrap && cl != system) {
     98         class_loader = cl;
     99         return false;
    100       }
    101       return true;
    102     }
    103 
    104     mirror::Object* bootstrap;
    105     mirror::Object* system;
    106     mirror::Object* class_loader;
    107   };
    108   ScopedFastNativeObjectAccess soa(env);
    109   mirror::Object* bootstrap = soa.Decode<mirror::Object*>(javaBootstrap);
    110   mirror::Object* system = soa.Decode<mirror::Object*>(javaSystem);
    111   ClosestUserClassLoaderVisitor visitor(soa.Self(), bootstrap, system);
    112   visitor.WalkStack();
    113   return soa.AddLocalReference<jobject>(visitor.class_loader);
    114 }
    115 
    116 // Returns the class of the caller's caller's caller.
    117 static jclass VMStack_getStackClass2(JNIEnv* env, jclass) {
    118   ScopedFastNativeObjectAccess soa(env);
    119   NthCallerVisitor visitor(soa.Self(), 3);
    120   visitor.WalkStack();
    121   if (UNLIKELY(visitor.caller == nullptr)) {
    122     // The caller is an attached native thread.
    123     return nullptr;
    124   }
    125   return soa.AddLocalReference<jclass>(visitor.caller->GetDeclaringClass());
    126 }
    127 
    128 static jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) {
    129   ScopedFastNativeObjectAccess soa(env);
    130   jobject trace = GetThreadStack(soa, javaThread);
    131   if (trace == nullptr) {
    132     return nullptr;
    133   }
    134   return Thread::InternalStackTraceToStackTraceElementArray(soa, trace);
    135 }
    136 
    137 static JNINativeMethod gMethods[] = {
    138   NATIVE_METHOD(VMStack, fillStackTraceElements, "!(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"),
    139   NATIVE_METHOD(VMStack, getCallingClassLoader, "!()Ljava/lang/ClassLoader;"),
    140   NATIVE_METHOD(VMStack, getClosestUserClassLoader, "!(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)Ljava/lang/ClassLoader;"),
    141   NATIVE_METHOD(VMStack, getStackClass2, "!()Ljava/lang/Class;"),
    142   NATIVE_METHOD(VMStack, getThreadStackTrace, "!(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"),
    143 };
    144 
    145 void register_dalvik_system_VMStack(JNIEnv* env) {
    146   REGISTER_NATIVE_METHODS("dalvik/system/VMStack");
    147 }
    148 
    149 }  // namespace art
    150