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 29namespace art { 30 31static jobject GetThreadStack(const ScopedFastNativeObjectAccess& soa, jobject peer) 32 SHARED_REQUIRES(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 ScopedThreadSuspension sts(soa.Self(), 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 { 45 ScopedObjectAccess soa2(soa.Self()); 46 trace = thread->CreateInternalStackTrace<false>(soa); 47 } 48 // Restart suspended thread. 49 thread_list->Resume(thread, false); 50 } else 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 return trace; 56} 57 58static jint VMStack_fillStackTraceElements(JNIEnv* env, jclass, jobject javaThread, 59 jobjectArray javaSteArray) { 60 ScopedFastNativeObjectAccess soa(env); 61 jobject trace = GetThreadStack(soa, javaThread); 62 if (trace == nullptr) { 63 return 0; 64 } 65 int32_t depth; 66 Thread::InternalStackTraceToStackTraceElementArray(soa, trace, javaSteArray, &depth); 67 return depth; 68} 69 70// Returns the defining class loader of the caller's caller. 71static jobject VMStack_getCallingClassLoader(JNIEnv* env, jclass) { 72 ScopedFastNativeObjectAccess soa(env); 73 NthCallerVisitor visitor(soa.Self(), 2); 74 visitor.WalkStack(); 75 if (UNLIKELY(visitor.caller == nullptr)) { 76 // The caller is an attached native thread. 77 return nullptr; 78 } 79 return soa.AddLocalReference<jobject>(visitor.caller->GetDeclaringClass()->GetClassLoader()); 80} 81 82static jobject VMStack_getClosestUserClassLoader(JNIEnv* env, jclass) { 83 struct ClosestUserClassLoaderVisitor : public StackVisitor { 84 explicit ClosestUserClassLoaderVisitor(Thread* thread) 85 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), 86 class_loader(nullptr) {} 87 88 bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) { 89 DCHECK(class_loader == nullptr); 90 mirror::Class* c = GetMethod()->GetDeclaringClass(); 91 // c is null for runtime methods. 92 if (c != nullptr) { 93 mirror::Object* cl = c->GetClassLoader(); 94 if (cl != nullptr) { 95 class_loader = cl; 96 return false; 97 } 98 } 99 return true; 100 } 101 102 mirror::Object* class_loader; 103 }; 104 ScopedFastNativeObjectAccess soa(env); 105 ClosestUserClassLoaderVisitor visitor(soa.Self()); 106 visitor.WalkStack(); 107 return soa.AddLocalReference<jobject>(visitor.class_loader); 108} 109 110// Returns the class of the caller's caller's caller. 111static jclass VMStack_getStackClass2(JNIEnv* env, jclass) { 112 ScopedFastNativeObjectAccess soa(env); 113 NthCallerVisitor visitor(soa.Self(), 3); 114 visitor.WalkStack(); 115 if (UNLIKELY(visitor.caller == nullptr)) { 116 // The caller is an attached native thread. 117 return nullptr; 118 } 119 return soa.AddLocalReference<jclass>(visitor.caller->GetDeclaringClass()); 120} 121 122static jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) { 123 ScopedFastNativeObjectAccess soa(env); 124 jobject trace = GetThreadStack(soa, javaThread); 125 if (trace == nullptr) { 126 return nullptr; 127 } 128 return Thread::InternalStackTraceToStackTraceElementArray(soa, trace); 129} 130 131static JNINativeMethod gMethods[] = { 132 NATIVE_METHOD(VMStack, fillStackTraceElements, "!(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"), 133 NATIVE_METHOD(VMStack, getCallingClassLoader, "!()Ljava/lang/ClassLoader;"), 134 NATIVE_METHOD(VMStack, getClosestUserClassLoader, "!()Ljava/lang/ClassLoader;"), 135 NATIVE_METHOD(VMStack, getStackClass2, "!()Ljava/lang/Class;"), 136 NATIVE_METHOD(VMStack, getThreadStackTrace, "!(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"), 137}; 138 139void register_dalvik_system_VMStack(JNIEnv* env) { 140 REGISTER_NATIVE_METHODS("dalvik/system/VMStack"); 141} 142 143} // namespace art 144