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