blob: cf310648e53e21f7b931b19e2113734434c85fbc [file] [log] [blame]
Elliott Hughes8daa0922011-09-11 13:46:25 -07001/*
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
Elliott Hugheseac76672012-05-24 21:56:51 -070017#include "jni_internal.h"
Elliott Hughes6a144332012-04-03 13:07:11 -070018#include "nth_caller_visitor.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070019#include "mirror/art_method-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070020#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "mirror/class_loader.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070022#include "mirror/object-inl.h"
Ian Rogers1eb512d2013-10-18 15:42:20 -070023#include "scoped_fast_native_object_access.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070024#include "scoped_thread_state_change.h"
Elliott Hughes01158d72011-09-19 19:47:10 -070025#include "thread_list.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070026
Elliott Hughes8daa0922011-09-11 13:46:25 -070027namespace art {
28
Ian Rogers53b8b092014-03-13 23:45:53 -070029static 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()) {
Sebastien Hertzee1d79a2014-02-21 15:46:30 +010033 trace = soa.Self()->CreateInternalStackTrace<false>(soa);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070034 } else {
Ian Rogers53b8b092014-03-13 23:45:53 -070035 // Suspend thread to build stack trace.
36 soa.Self()->TransitionFromRunnableToSuspended(kNative);
37 bool timed_out;
38 Thread* thread = ThreadList::SuspendThreadByPeer(peer, true, false, &timed_out);
39 if (thread != nullptr) {
40 // Must be runnable to create returned array.
41 CHECK_EQ(soa.Self()->TransitionFromSuspendedToRunnable(), kNative);
Sebastien Hertzee1d79a2014-02-21 15:46:30 +010042 trace = thread->CreateInternalStackTrace<false>(soa);
Ian Rogers53b8b092014-03-13 23:45:53 -070043 soa.Self()->TransitionFromRunnableToSuspended(kNative);
44 // Restart suspended thread.
45 Runtime::Current()->GetThreadList()->Resume(thread, false);
46 } else {
47 if (timed_out) {
48 LOG(ERROR) << "Trying to get thread's stack failed as the thread failed to suspend within a "
49 "generous timeout.";
50 }
Ian Rogers15bf2d32012-08-28 17:33:04 -070051 }
Ian Rogers53b8b092014-03-13 23:45:53 -070052 CHECK_EQ(soa.Self()->TransitionFromSuspendedToRunnable(), kNative);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070053 }
Ian Rogers53b8b092014-03-13 23:45:53 -070054 return trace;
Elliott Hughes01158d72011-09-19 19:47:10 -070055}
56
Ian Rogers306057f2012-11-26 12:45:53 -080057static jint VMStack_fillStackTraceElements(JNIEnv* env, jclass, jobject javaThread,
58 jobjectArray javaSteArray) {
Ian Rogers53b8b092014-03-13 23:45:53 -070059 ScopedFastNativeObjectAccess soa(env);
60 jobject trace = GetThreadStack(soa, javaThread);
61 if (trace == nullptr) {
Elliott Hughes01158d72011-09-19 19:47:10 -070062 return 0;
63 }
64 int32_t depth;
Ian Rogers53b8b092014-03-13 23:45:53 -070065 Thread::InternalStackTraceToStackTraceElementArray(soa, trace, javaSteArray, &depth);
Elliott Hughes01158d72011-09-19 19:47:10 -070066 return depth;
Elliott Hughes8daa0922011-09-11 13:46:25 -070067}
68
Elliott Hughes6a144332012-04-03 13:07:11 -070069// Returns the defining class loader of the caller's caller.
Elliott Hughes0512f022012-03-15 22:10:52 -070070static jobject VMStack_getCallingClassLoader(JNIEnv* env, jclass) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070071 ScopedFastNativeObjectAccess soa(env);
Ian Rogers7a22fa62013-01-23 12:16:16 -080072 NthCallerVisitor visitor(soa.Self(), 2);
Ian Rogers0399dde2012-06-06 17:09:28 -070073 visitor.WalkStack();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070074 return soa.AddLocalReference<jobject>(visitor.caller->GetDeclaringClass()->GetClassLoader());
Elliott Hughes8daa0922011-09-11 13:46:25 -070075}
76
Ian Rogers306057f2012-11-26 12:45:53 -080077static jobject VMStack_getClosestUserClassLoader(JNIEnv* env, jclass, jobject javaBootstrap,
78 jobject javaSystem) {
Ian Rogers0399dde2012-06-06 17:09:28 -070079 struct ClosestUserClassLoaderVisitor : public StackVisitor {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080 ClosestUserClassLoaderVisitor(Thread* thread, mirror::Object* bootstrap, mirror::Object* system)
Ian Rogers7a22fa62013-01-23 12:16:16 -080081 : StackVisitor(thread, NULL), bootstrap(bootstrap), system(system), class_loader(NULL) {}
Elliott Hughes08fc03a2012-06-26 17:34:00 -070082
Ian Rogersef7d42f2014-01-06 12:55:46 -080083 bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes530fa002012-03-12 11:44:49 -070084 DCHECK(class_loader == NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080085 mirror::Class* c = GetMethod()->GetDeclaringClass();
86 mirror::Object* cl = c->GetClassLoader();
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080087 if (cl != NULL && cl != bootstrap && cl != system) {
88 class_loader = cl;
Elliott Hughes530fa002012-03-12 11:44:49 -070089 return false;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080090 }
Elliott Hughes530fa002012-03-12 11:44:49 -070091 return true;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080092 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -070093
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080094 mirror::Object* bootstrap;
95 mirror::Object* system;
96 mirror::Object* class_loader;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080097 };
Ian Rogers1eb512d2013-10-18 15:42:20 -070098 ScopedFastNativeObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080099 mirror::Object* bootstrap = soa.Decode<mirror::Object*>(javaBootstrap);
100 mirror::Object* system = soa.Decode<mirror::Object*>(javaSystem);
Ian Rogers7a22fa62013-01-23 12:16:16 -0800101 ClosestUserClassLoaderVisitor visitor(soa.Self(), bootstrap, system);
Ian Rogers0399dde2012-06-06 17:09:28 -0700102 visitor.WalkStack();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700103 return soa.AddLocalReference<jobject>(visitor.class_loader);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700104}
105
Elliott Hughes6a144332012-04-03 13:07:11 -0700106// Returns the class of the caller's caller's caller.
Elliott Hughes0512f022012-03-15 22:10:52 -0700107static jclass VMStack_getStackClass2(JNIEnv* env, jclass) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700108 ScopedFastNativeObjectAccess soa(env);
Ian Rogers7a22fa62013-01-23 12:16:16 -0800109 NthCallerVisitor visitor(soa.Self(), 3);
Ian Rogers0399dde2012-06-06 17:09:28 -0700110 visitor.WalkStack();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700111 return soa.AddLocalReference<jclass>(visitor.caller->GetDeclaringClass());
Elliott Hughes8daa0922011-09-11 13:46:25 -0700112}
113
Elliott Hughes0512f022012-03-15 22:10:52 -0700114static jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700115 ScopedFastNativeObjectAccess soa(env);
116 jobject trace = GetThreadStack(soa, javaThread);
117 if (trace == nullptr) {
118 return nullptr;
Elliott Hughes01158d72011-09-19 19:47:10 -0700119 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700120 return Thread::InternalStackTraceToStackTraceElementArray(soa, trace);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700121}
122
123static JNINativeMethod gMethods[] = {
Ian Rogers53b8b092014-03-13 23:45:53 -0700124 NATIVE_METHOD(VMStack, fillStackTraceElements, "!(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"),
Ian Rogers1eb512d2013-10-18 15:42:20 -0700125 NATIVE_METHOD(VMStack, getCallingClassLoader, "!()Ljava/lang/ClassLoader;"),
126 NATIVE_METHOD(VMStack, getClosestUserClassLoader, "!(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)Ljava/lang/ClassLoader;"),
127 NATIVE_METHOD(VMStack, getStackClass2, "!()Ljava/lang/Class;"),
Ian Rogers53b8b092014-03-13 23:45:53 -0700128 NATIVE_METHOD(VMStack, getThreadStackTrace, "!(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700129};
130
Elliott Hughes8daa0922011-09-11 13:46:25 -0700131void register_dalvik_system_VMStack(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700132 REGISTER_NATIVE_METHODS("dalvik/system/VMStack");
Elliott Hughes8daa0922011-09-11 13:46:25 -0700133}
134
135} // namespace art