dalvik_system_VMStack.cc revision 8e5bd18fc665d7ec5461ea068e98740a65da754c
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 "jni_internal.h"
20#include "nth_caller_visitor.h"
21#include "mirror/art_method-inl.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_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
60static 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.
73static 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
84static 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      mirror::Object* cl = c->GetClassLoader();
94      if (cl != nullptr) {
95        class_loader = cl;
96        return false;
97      }
98      return true;
99    }
100
101    mirror::Object* class_loader;
102  };
103  ScopedFastNativeObjectAccess soa(env);
104  ClosestUserClassLoaderVisitor visitor(soa.Self());
105  visitor.WalkStack();
106  return soa.AddLocalReference<jobject>(visitor.class_loader);
107}
108
109// Returns the class of the caller's caller's caller.
110static jclass VMStack_getStackClass2(JNIEnv* env, jclass) {
111  ScopedFastNativeObjectAccess soa(env);
112  NthCallerVisitor visitor(soa.Self(), 3);
113  visitor.WalkStack();
114  if (UNLIKELY(visitor.caller == nullptr)) {
115    // The caller is an attached native thread.
116    return nullptr;
117  }
118  return soa.AddLocalReference<jclass>(visitor.caller->GetDeclaringClass());
119}
120
121static jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) {
122  ScopedFastNativeObjectAccess soa(env);
123  jobject trace = GetThreadStack(soa, javaThread);
124  if (trace == nullptr) {
125    return nullptr;
126  }
127  return Thread::InternalStackTraceToStackTraceElementArray(soa, trace);
128}
129
130static JNINativeMethod gMethods[] = {
131  NATIVE_METHOD(VMStack, fillStackTraceElements, "!(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"),
132  NATIVE_METHOD(VMStack, getCallingClassLoader, "!()Ljava/lang/ClassLoader;"),
133  NATIVE_METHOD(VMStack, getClosestUserClassLoader, "!()Ljava/lang/ClassLoader;"),
134  NATIVE_METHOD(VMStack, getStackClass2, "!()Ljava/lang/Class;"),
135  NATIVE_METHOD(VMStack, getThreadStackTrace, "!(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"),
136};
137
138void register_dalvik_system_VMStack(JNIEnv* env) {
139  REGISTER_NATIVE_METHODS("dalvik/system/VMStack");
140}
141
142}  // namespace art
143