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