dalvik_system_VMStack.cc revision ab61295e55c38072095e498053b8f5bebf46ad63
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  if (UNLIKELY(visitor.caller == nullptr)) {
80    // The caller is an attached native thread.
81    return nullptr;
82  }
83  return soa.AddLocalReference<jobject>(visitor.caller->GetDeclaringClass()->GetClassLoader());
84}
85
86static jobject VMStack_getClosestUserClassLoader(JNIEnv* env, jclass, jobject javaBootstrap,
87                                                 jobject javaSystem) {
88  struct ClosestUserClassLoaderVisitor : public StackVisitor {
89    ClosestUserClassLoaderVisitor(Thread* thread, mirror::Object* bootstrap, mirror::Object* system)
90      : StackVisitor(thread, NULL), bootstrap(bootstrap), system(system), class_loader(NULL) {}
91
92    bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
93      DCHECK(class_loader == NULL);
94      mirror::Class* c = GetMethod()->GetDeclaringClass();
95      mirror::Object* cl = c->GetClassLoader();
96      if (cl != NULL && cl != bootstrap && cl != system) {
97        class_loader = cl;
98        return false;
99      }
100      return true;
101    }
102
103    mirror::Object* bootstrap;
104    mirror::Object* system;
105    mirror::Object* class_loader;
106  };
107  ScopedFastNativeObjectAccess soa(env);
108  mirror::Object* bootstrap = soa.Decode<mirror::Object*>(javaBootstrap);
109  mirror::Object* system = soa.Decode<mirror::Object*>(javaSystem);
110  ClosestUserClassLoaderVisitor visitor(soa.Self(), bootstrap, system);
111  visitor.WalkStack();
112  return soa.AddLocalReference<jobject>(visitor.class_loader);
113}
114
115// Returns the class of the caller's caller's caller.
116static jclass VMStack_getStackClass2(JNIEnv* env, jclass) {
117  ScopedFastNativeObjectAccess soa(env);
118  NthCallerVisitor visitor(soa.Self(), 3);
119  visitor.WalkStack();
120  if (UNLIKELY(visitor.caller == nullptr)) {
121    // The caller is an attached native thread.
122    return nullptr;
123  }
124  return soa.AddLocalReference<jclass>(visitor.caller->GetDeclaringClass());
125}
126
127static jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) {
128  ScopedFastNativeObjectAccess soa(env);
129  jobject trace = GetThreadStack(soa, javaThread);
130  if (trace == nullptr) {
131    return nullptr;
132  }
133  return Thread::InternalStackTraceToStackTraceElementArray(soa, trace);
134}
135
136static JNINativeMethod gMethods[] = {
137  NATIVE_METHOD(VMStack, fillStackTraceElements, "!(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"),
138  NATIVE_METHOD(VMStack, getCallingClassLoader, "!()Ljava/lang/ClassLoader;"),
139  NATIVE_METHOD(VMStack, getClosestUserClassLoader, "!(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)Ljava/lang/ClassLoader;"),
140  NATIVE_METHOD(VMStack, getStackClass2, "!()Ljava/lang/Class;"),
141  NATIVE_METHOD(VMStack, getThreadStackTrace, "!(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"),
142};
143
144void register_dalvik_system_VMStack(JNIEnv* env) {
145  REGISTER_NATIVE_METHODS("dalvik/system/VMStack");
146}
147
148}  // namespace art
149