Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 17 | #include "jni_internal.h" |
Elliott Hughes | 6a14433 | 2012-04-03 13:07:11 -0700 | [diff] [blame] | 18 | #include "nth_caller_visitor.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 19 | #include "mirror/art_method-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 20 | #include "mirror/class-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 21 | #include "mirror/class_loader.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 22 | #include "mirror/object-inl.h" |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 23 | #include "scoped_fast_native_object_access.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 24 | #include "scoped_thread_state_change.h" |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 25 | #include "thread_list.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 26 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 27 | namespace art { |
| 28 | |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 29 | static 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 Hertz | ee1d79a | 2014-02-21 15:46:30 +0100 | [diff] [blame] | 33 | trace = soa.Self()->CreateInternalStackTrace<false>(soa); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 34 | } else { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 35 | // Suspend thread to build stack trace. |
| 36 | soa.Self()->TransitionFromRunnableToSuspended(kNative); |
Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame^] | 37 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 38 | bool timed_out; |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 39 | Thread* thread; |
| 40 | { |
| 41 | // Take suspend thread lock to avoid races with threads trying to suspend this one. |
| 42 | MutexLock mu(soa.Self(), *Locks::thread_list_suspend_thread_lock_); |
Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame^] | 43 | thread = thread_list->SuspendThreadByPeer(peer, true, false, &timed_out); |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 44 | } |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 45 | if (thread != nullptr) { |
| 46 | // Must be runnable to create returned array. |
| 47 | CHECK_EQ(soa.Self()->TransitionFromSuspendedToRunnable(), kNative); |
Sebastien Hertz | ee1d79a | 2014-02-21 15:46:30 +0100 | [diff] [blame] | 48 | trace = thread->CreateInternalStackTrace<false>(soa); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 49 | soa.Self()->TransitionFromRunnableToSuspended(kNative); |
| 50 | // Restart suspended thread. |
Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame^] | 51 | thread_list->Resume(thread, false); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 52 | } else { |
| 53 | if (timed_out) { |
| 54 | LOG(ERROR) << "Trying to get thread's stack failed as the thread failed to suspend within a " |
| 55 | "generous timeout."; |
| 56 | } |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 57 | } |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 58 | CHECK_EQ(soa.Self()->TransitionFromSuspendedToRunnable(), kNative); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 59 | } |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 60 | return trace; |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 63 | static jint VMStack_fillStackTraceElements(JNIEnv* env, jclass, jobject javaThread, |
| 64 | jobjectArray javaSteArray) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 65 | ScopedFastNativeObjectAccess soa(env); |
| 66 | jobject trace = GetThreadStack(soa, javaThread); |
| 67 | if (trace == nullptr) { |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 68 | return 0; |
| 69 | } |
| 70 | int32_t depth; |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 71 | Thread::InternalStackTraceToStackTraceElementArray(soa, trace, javaSteArray, &depth); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 72 | return depth; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Elliott Hughes | 6a14433 | 2012-04-03 13:07:11 -0700 | [diff] [blame] | 75 | // Returns the defining class loader of the caller's caller. |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 76 | static jobject VMStack_getCallingClassLoader(JNIEnv* env, jclass) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 77 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 78 | NthCallerVisitor visitor(soa.Self(), 2); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 79 | visitor.WalkStack(); |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 80 | if (UNLIKELY(visitor.caller == nullptr)) { |
| 81 | // The caller is an attached native thread. |
| 82 | return nullptr; |
| 83 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 84 | return soa.AddLocalReference<jobject>(visitor.caller->GetDeclaringClass()->GetClassLoader()); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 87 | static jobject VMStack_getClosestUserClassLoader(JNIEnv* env, jclass, jobject javaBootstrap, |
| 88 | jobject javaSystem) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 89 | struct ClosestUserClassLoaderVisitor : public StackVisitor { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 90 | ClosestUserClassLoaderVisitor(Thread* thread, mirror::Object* bootstrap, mirror::Object* system) |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 91 | : StackVisitor(thread, NULL), bootstrap(bootstrap), system(system), class_loader(NULL) {} |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 92 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 93 | bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 94 | DCHECK(class_loader == NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 95 | mirror::Class* c = GetMethod()->GetDeclaringClass(); |
| 96 | mirror::Object* cl = c->GetClassLoader(); |
Brian Carlstrom | 5cb71bb | 2012-03-09 14:57:37 -0800 | [diff] [blame] | 97 | if (cl != NULL && cl != bootstrap && cl != system) { |
| 98 | class_loader = cl; |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 99 | return false; |
Brian Carlstrom | 5cb71bb | 2012-03-09 14:57:37 -0800 | [diff] [blame] | 100 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 101 | return true; |
Brian Carlstrom | 5cb71bb | 2012-03-09 14:57:37 -0800 | [diff] [blame] | 102 | } |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 103 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 104 | mirror::Object* bootstrap; |
| 105 | mirror::Object* system; |
| 106 | mirror::Object* class_loader; |
Brian Carlstrom | 5cb71bb | 2012-03-09 14:57:37 -0800 | [diff] [blame] | 107 | }; |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 108 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 109 | mirror::Object* bootstrap = soa.Decode<mirror::Object*>(javaBootstrap); |
| 110 | mirror::Object* system = soa.Decode<mirror::Object*>(javaSystem); |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 111 | ClosestUserClassLoaderVisitor visitor(soa.Self(), bootstrap, system); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 112 | visitor.WalkStack(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 113 | return soa.AddLocalReference<jobject>(visitor.class_loader); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Elliott Hughes | 6a14433 | 2012-04-03 13:07:11 -0700 | [diff] [blame] | 116 | // Returns the class of the caller's caller's caller. |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 117 | static jclass VMStack_getStackClass2(JNIEnv* env, jclass) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 118 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 119 | NthCallerVisitor visitor(soa.Self(), 3); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 120 | visitor.WalkStack(); |
Andreas Gampe | 0d334ce | 2014-08-13 23:05:38 -0700 | [diff] [blame] | 121 | if (UNLIKELY(visitor.caller == nullptr)) { |
| 122 | // The caller is an attached native thread. |
| 123 | return nullptr; |
| 124 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 125 | return soa.AddLocalReference<jclass>(visitor.caller->GetDeclaringClass()); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 128 | static jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 129 | ScopedFastNativeObjectAccess soa(env); |
| 130 | jobject trace = GetThreadStack(soa, javaThread); |
| 131 | if (trace == nullptr) { |
| 132 | return nullptr; |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 133 | } |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 134 | return Thread::InternalStackTraceToStackTraceElementArray(soa, trace); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static JNINativeMethod gMethods[] = { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 138 | NATIVE_METHOD(VMStack, fillStackTraceElements, "!(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"), |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 139 | NATIVE_METHOD(VMStack, getCallingClassLoader, "!()Ljava/lang/ClassLoader;"), |
| 140 | NATIVE_METHOD(VMStack, getClosestUserClassLoader, "!(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)Ljava/lang/ClassLoader;"), |
| 141 | NATIVE_METHOD(VMStack, getStackClass2, "!()Ljava/lang/Class;"), |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 142 | NATIVE_METHOD(VMStack, getThreadStackTrace, "!(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"), |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 143 | }; |
| 144 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 145 | void register_dalvik_system_VMStack(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 146 | REGISTER_NATIVE_METHODS("dalvik/system/VMStack"); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | } // namespace art |