• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "dalvik_system_VMStack.h"
18 
19 #include "art_method-inl.h"
20 #include "gc/task_processor.h"
21 #include "jni_internal.h"
22 #include "nth_caller_visitor.h"
23 #include "mirror/class-inl.h"
24 #include "mirror/class_loader.h"
25 #include "mirror/object-inl.h"
26 #include "scoped_fast_native_object_access-inl.h"
27 #include "scoped_thread_state_change-inl.h"
28 #include "thread_list.h"
29 
30 namespace art {
31 
GetThreadStack(const ScopedFastNativeObjectAccess & soa,jobject peer)32 static jobject GetThreadStack(const ScopedFastNativeObjectAccess& soa, jobject peer)
33     REQUIRES_SHARED(Locks::mutator_lock_) {
34   jobject trace = nullptr;
35   ObjPtr<mirror::Object> decoded_peer = soa.Decode<mirror::Object>(peer);
36   if (decoded_peer == soa.Self()->GetPeer()) {
37     trace = soa.Self()->CreateInternalStackTrace<false>(soa);
38   } else {
39     // Never allow suspending the heap task thread since it may deadlock if allocations are
40     // required for the stack trace.
41     Thread* heap_task_thread =
42         Runtime::Current()->GetHeap()->GetTaskProcessor()->GetRunningThread();
43     // heap_task_thread could be null if the daemons aren't yet started.
44     if (heap_task_thread != nullptr && decoded_peer == heap_task_thread->GetPeerFromOtherThread()) {
45       return nullptr;
46     }
47     // Suspend thread to build stack trace.
48     ScopedThreadSuspension sts(soa.Self(), kNative);
49     ThreadList* thread_list = Runtime::Current()->GetThreadList();
50     bool timed_out;
51     Thread* thread = thread_list->SuspendThreadByPeer(peer, true, false, &timed_out);
52     if (thread != nullptr) {
53       // Must be runnable to create returned array.
54       {
55         ScopedObjectAccess soa2(soa.Self());
56         trace = thread->CreateInternalStackTrace<false>(soa);
57       }
58       // Restart suspended thread.
59       thread_list->Resume(thread, false);
60     } else if (timed_out) {
61       LOG(ERROR) << "Trying to get thread's stack failed as the thread failed to suspend within a "
62           "generous timeout.";
63     }
64   }
65   return trace;
66 }
67 
VMStack_fillStackTraceElements(JNIEnv * env,jclass,jobject javaThread,jobjectArray javaSteArray)68 static jint VMStack_fillStackTraceElements(JNIEnv* env, jclass, jobject javaThread,
69                                            jobjectArray javaSteArray) {
70   ScopedFastNativeObjectAccess soa(env);
71   jobject trace = GetThreadStack(soa, javaThread);
72   if (trace == nullptr) {
73     return 0;
74   }
75   int32_t depth;
76   Thread::InternalStackTraceToStackTraceElementArray(soa, trace, javaSteArray, &depth);
77   return depth;
78 }
79 
80 // Returns the defining class loader of the caller's caller.
VMStack_getCallingClassLoader(JNIEnv * env,jclass)81 static jobject VMStack_getCallingClassLoader(JNIEnv* env, jclass) {
82   ScopedFastNativeObjectAccess soa(env);
83   NthCallerVisitor visitor(soa.Self(), 2);
84   visitor.WalkStack();
85   if (UNLIKELY(visitor.caller == nullptr)) {
86     // The caller is an attached native thread.
87     return nullptr;
88   }
89   return soa.AddLocalReference<jobject>(visitor.caller->GetDeclaringClass()->GetClassLoader());
90 }
91 
VMStack_getClosestUserClassLoader(JNIEnv * env,jclass)92 static jobject VMStack_getClosestUserClassLoader(JNIEnv* env, jclass) {
93   struct ClosestUserClassLoaderVisitor : public StackVisitor {
94     explicit ClosestUserClassLoaderVisitor(Thread* thread)
95       : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
96         class_loader(nullptr) {}
97 
98     bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) {
99       DCHECK(class_loader == nullptr);
100       ObjPtr<mirror::Class> c = GetMethod()->GetDeclaringClass();
101       // c is null for runtime methods.
102       if (c != nullptr) {
103         ObjPtr<mirror::Object> cl = c->GetClassLoader();
104         if (cl != nullptr) {
105           class_loader = cl;
106           return false;
107         }
108       }
109       return true;
110     }
111 
112     ObjPtr<mirror::Object> class_loader;
113   };
114   ScopedFastNativeObjectAccess soa(env);
115   ClosestUserClassLoaderVisitor visitor(soa.Self());
116   visitor.WalkStack();
117   return soa.AddLocalReference<jobject>(visitor.class_loader);
118 }
119 
120 // Returns the class of the caller's caller's caller.
VMStack_getStackClass2(JNIEnv * env,jclass)121 static jclass VMStack_getStackClass2(JNIEnv* env, jclass) {
122   ScopedFastNativeObjectAccess soa(env);
123   NthCallerVisitor visitor(soa.Self(), 3);
124   visitor.WalkStack();
125   if (UNLIKELY(visitor.caller == nullptr)) {
126     // The caller is an attached native thread.
127     return nullptr;
128   }
129   return soa.AddLocalReference<jclass>(visitor.caller->GetDeclaringClass());
130 }
131 
VMStack_getThreadStackTrace(JNIEnv * env,jclass,jobject javaThread)132 static jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) {
133   ScopedFastNativeObjectAccess soa(env);
134   jobject trace = GetThreadStack(soa, javaThread);
135   if (trace == nullptr) {
136     return nullptr;
137   }
138   return Thread::InternalStackTraceToStackTraceElementArray(soa, trace);
139 }
140 
141 static JNINativeMethod gMethods[] = {
142   FAST_NATIVE_METHOD(VMStack, fillStackTraceElements, "(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"),
143   FAST_NATIVE_METHOD(VMStack, getCallingClassLoader, "()Ljava/lang/ClassLoader;"),
144   FAST_NATIVE_METHOD(VMStack, getClosestUserClassLoader, "()Ljava/lang/ClassLoader;"),
145   FAST_NATIVE_METHOD(VMStack, getStackClass2, "()Ljava/lang/Class;"),
146   FAST_NATIVE_METHOD(VMStack, getThreadStackTrace, "(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"),
147 };
148 
register_dalvik_system_VMStack(JNIEnv * env)149 void register_dalvik_system_VMStack(JNIEnv* env) {
150   REGISTER_NATIVE_METHODS("dalvik/system/VMStack");
151 }
152 
153 }  // namespace art
154