| 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 | |
| Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 17 | #include "dalvik_system_VMStack.h" |
| 18 | |
| Andreas Gampe | 6362e23 | 2017-12-11 20:43:25 -0800 | [diff] [blame] | 19 | #include <type_traits> |
| 20 | |
| Andreas Gampe | a14100c | 2017-04-24 15:09:56 -0700 | [diff] [blame] | 21 | #include "nativehelper/jni_macros.h" |
| 22 | |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 23 | #include "art_method-inl.h" |
| Mathieu Chartier | 4201cf0 | 2017-01-12 14:51:44 -0800 | [diff] [blame] | 24 | #include "gc/task_processor.h" |
| Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 25 | #include "jni/jni_internal.h" |
| Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 26 | #include "mirror/class-inl.h" |
| Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | #include "mirror/class_loader.h" |
| Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 28 | #include "mirror/object-inl.h" |
| Andreas Gampe | 87583b3 | 2017-05-25 11:22:18 -0700 | [diff] [blame] | 29 | #include "native_util.h" |
| Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 30 | #include "nth_caller_visitor.h" |
| Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 31 | #include "scoped_fast_native_object_access-inl.h" |
| 32 | #include "scoped_thread_state_change-inl.h" |
| Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 33 | #include "thread_list.h" |
| Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 34 | |
| Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 35 | namespace art { |
| 36 | |
| Andreas Gampe | 6362e23 | 2017-12-11 20:43:25 -0800 | [diff] [blame] | 37 | template <typename T, |
| 38 | typename ResultT = |
| 39 | typename std::result_of<T(Thread*, const ScopedFastNativeObjectAccess&)>::type> |
| 40 | static ResultT GetThreadStack(const ScopedFastNativeObjectAccess& soa, |
| 41 | jobject peer, |
| 42 | T fn) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 43 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Andreas Gampe | 6362e23 | 2017-12-11 20:43:25 -0800 | [diff] [blame] | 44 | ResultT trace = nullptr; |
| Mathieu Chartier | 4201cf0 | 2017-01-12 14:51:44 -0800 | [diff] [blame] | 45 | ObjPtr<mirror::Object> decoded_peer = soa.Decode<mirror::Object>(peer); |
| 46 | if (decoded_peer == soa.Self()->GetPeer()) { |
| Andreas Gampe | 6362e23 | 2017-12-11 20:43:25 -0800 | [diff] [blame] | 47 | trace = fn(soa.Self(), soa); |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 48 | } else { |
| Mathieu Chartier | 4201cf0 | 2017-01-12 14:51:44 -0800 | [diff] [blame] | 49 | // Never allow suspending the heap task thread since it may deadlock if allocations are |
| 50 | // required for the stack trace. |
| 51 | Thread* heap_task_thread = |
| 52 | Runtime::Current()->GetHeap()->GetTaskProcessor()->GetRunningThread(); |
| 53 | // heap_task_thread could be null if the daemons aren't yet started. |
| Nicolas Geoffray | ffc8cad | 2017-02-10 10:59:22 +0000 | [diff] [blame] | 54 | if (heap_task_thread != nullptr && decoded_peer == heap_task_thread->GetPeerFromOtherThread()) { |
| Mathieu Chartier | 4201cf0 | 2017-01-12 14:51:44 -0800 | [diff] [blame] | 55 | return nullptr; |
| 56 | } |
| Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 57 | // Suspend thread to build stack trace. |
| Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 58 | ScopedThreadSuspension sts(soa.Self(), kNative); |
| Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame] | 59 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 60 | bool timed_out; |
| Alex Light | 46f9340 | 2017-06-29 11:59:50 -0700 | [diff] [blame] | 61 | Thread* thread = thread_list->SuspendThreadByPeer(peer, |
| Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 62 | /* request_suspension= */ true, |
| Alex Light | 46f9340 | 2017-06-29 11:59:50 -0700 | [diff] [blame] | 63 | SuspendReason::kInternal, |
| 64 | &timed_out); |
| Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 65 | if (thread != nullptr) { |
| 66 | // Must be runnable to create returned array. |
| Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 67 | { |
| 68 | ScopedObjectAccess soa2(soa.Self()); |
| Andreas Gampe | 6362e23 | 2017-12-11 20:43:25 -0800 | [diff] [blame] | 69 | trace = fn(thread, soa); |
| Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 70 | } |
| Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 71 | // Restart suspended thread. |
| Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 72 | bool resumed = thread_list->Resume(thread, SuspendReason::kInternal); |
| 73 | DCHECK(resumed); |
| Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 74 | } else if (timed_out) { |
| 75 | LOG(ERROR) << "Trying to get thread's stack failed as the thread failed to suspend within a " |
| 76 | "generous timeout."; |
| Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 77 | } |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 78 | } |
| Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 79 | return trace; |
| Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 82 | static jint VMStack_fillStackTraceElements(JNIEnv* env, jclass, jobject javaThread, |
| 83 | jobjectArray javaSteArray) { |
| Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 84 | ScopedFastNativeObjectAccess soa(env); |
| Andreas Gampe | 6362e23 | 2017-12-11 20:43:25 -0800 | [diff] [blame] | 85 | auto fn = [](Thread* thread, const ScopedFastNativeObjectAccess& soaa) |
| 86 | REQUIRES_SHARED(Locks::mutator_lock_) -> jobject { |
| 87 | return thread->CreateInternalStackTrace<false>(soaa); |
| 88 | }; |
| 89 | jobject trace = GetThreadStack(soa, javaThread, fn); |
| Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 90 | if (trace == nullptr) { |
| Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 91 | return 0; |
| 92 | } |
| 93 | int32_t depth; |
| Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 94 | Thread::InternalStackTraceToStackTraceElementArray(soa, trace, javaSteArray, &depth); |
| Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 95 | return depth; |
| Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| Elliott Hughes | 6a14433 | 2012-04-03 13:07:11 -0700 | [diff] [blame] | 98 | // Returns the defining class loader of the caller's caller. |
| Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 99 | static jobject VMStack_getCallingClassLoader(JNIEnv* env, jclass) { |
| Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 100 | ScopedFastNativeObjectAccess soa(env); |
| Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 101 | NthCallerVisitor visitor(soa.Self(), 2); |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 102 | visitor.WalkStack(); |
| Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 103 | if (UNLIKELY(visitor.caller == nullptr)) { |
| 104 | // The caller is an attached native thread. |
| 105 | return nullptr; |
| 106 | } |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 107 | return soa.AddLocalReference<jobject>(visitor.caller->GetDeclaringClass()->GetClassLoader()); |
| Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| Brian Carlstrom | 5988547 | 2015-04-20 21:55:19 -0700 | [diff] [blame] | 110 | static jobject VMStack_getClosestUserClassLoader(JNIEnv* env, jclass) { |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 111 | struct ClosestUserClassLoaderVisitor : public StackVisitor { |
| Brian Carlstrom | 5988547 | 2015-04-20 21:55:19 -0700 | [diff] [blame] | 112 | explicit ClosestUserClassLoaderVisitor(Thread* thread) |
| Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 113 | : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 114 | class_loader(nullptr) {} |
| Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 115 | |
| Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 116 | bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) { |
| Brian Carlstrom | 5988547 | 2015-04-20 21:55:19 -0700 | [diff] [blame] | 117 | DCHECK(class_loader == nullptr); |
| Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 118 | ObjPtr<mirror::Class> c = GetMethod()->GetDeclaringClass(); |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 119 | // c is null for runtime methods. |
| 120 | if (c != nullptr) { |
| Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 121 | ObjPtr<mirror::Object> cl = c->GetClassLoader(); |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 122 | if (cl != nullptr) { |
| 123 | class_loader = cl; |
| 124 | return false; |
| 125 | } |
| Brian Carlstrom | 5cb71bb | 2012-03-09 14:57:37 -0800 | [diff] [blame] | 126 | } |
| Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 127 | return true; |
| Brian Carlstrom | 5cb71bb | 2012-03-09 14:57:37 -0800 | [diff] [blame] | 128 | } |
| Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 129 | |
| Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 130 | ObjPtr<mirror::Object> class_loader; |
| Brian Carlstrom | 5cb71bb | 2012-03-09 14:57:37 -0800 | [diff] [blame] | 131 | }; |
| Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 132 | ScopedFastNativeObjectAccess soa(env); |
| Brian Carlstrom | 5988547 | 2015-04-20 21:55:19 -0700 | [diff] [blame] | 133 | ClosestUserClassLoaderVisitor visitor(soa.Self()); |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 134 | visitor.WalkStack(); |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 135 | return soa.AddLocalReference<jobject>(visitor.class_loader); |
| Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| Elliott Hughes | 6a14433 | 2012-04-03 13:07:11 -0700 | [diff] [blame] | 138 | // Returns the class of the caller's caller's caller. |
| Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 139 | static jclass VMStack_getStackClass2(JNIEnv* env, jclass) { |
| Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 140 | ScopedFastNativeObjectAccess soa(env); |
| Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 141 | NthCallerVisitor visitor(soa.Self(), 3); |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 142 | visitor.WalkStack(); |
| Andreas Gampe | 0d334ce | 2014-08-13 23:05:38 -0700 | [diff] [blame] | 143 | if (UNLIKELY(visitor.caller == nullptr)) { |
| 144 | // The caller is an attached native thread. |
| 145 | return nullptr; |
| 146 | } |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 147 | return soa.AddLocalReference<jclass>(visitor.caller->GetDeclaringClass()); |
| Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 150 | static jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) { |
| Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 151 | ScopedFastNativeObjectAccess soa(env); |
| Andreas Gampe | 6362e23 | 2017-12-11 20:43:25 -0800 | [diff] [blame] | 152 | auto fn = [](Thread* thread, const ScopedFastNativeObjectAccess& soaa) |
| 153 | REQUIRES_SHARED(Locks::mutator_lock_) -> jobject { |
| 154 | return thread->CreateInternalStackTrace<false>(soaa); |
| 155 | }; |
| 156 | jobject trace = GetThreadStack(soa, javaThread, fn); |
| Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 157 | if (trace == nullptr) { |
| 158 | return nullptr; |
| Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 159 | } |
| Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 160 | return Thread::InternalStackTraceToStackTraceElementArray(soa, trace); |
| Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| Andreas Gampe | fb6b0b1 | 2017-12-11 20:47:56 -0800 | [diff] [blame] | 163 | static jobjectArray VMStack_getAnnotatedThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) { |
| 164 | ScopedFastNativeObjectAccess soa(env); |
| 165 | auto fn = [](Thread* thread, const ScopedFastNativeObjectAccess& soaa) |
| 166 | REQUIRES_SHARED(Locks::mutator_lock_) -> jobjectArray { |
| 167 | return thread->CreateAnnotatedStackTrace(soaa); |
| 168 | }; |
| 169 | return GetThreadStack(soa, javaThread, fn); |
| 170 | } |
| 171 | |
| Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 172 | static JNINativeMethod gMethods[] = { |
| Igor Murashkin | 3b6f440 | 2017-02-16 16:13:17 -0800 | [diff] [blame] | 173 | FAST_NATIVE_METHOD(VMStack, fillStackTraceElements, "(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"), |
| 174 | FAST_NATIVE_METHOD(VMStack, getCallingClassLoader, "()Ljava/lang/ClassLoader;"), |
| 175 | FAST_NATIVE_METHOD(VMStack, getClosestUserClassLoader, "()Ljava/lang/ClassLoader;"), |
| 176 | FAST_NATIVE_METHOD(VMStack, getStackClass2, "()Ljava/lang/Class;"), |
| 177 | FAST_NATIVE_METHOD(VMStack, getThreadStackTrace, "(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"), |
| Andreas Gampe | fb6b0b1 | 2017-12-11 20:47:56 -0800 | [diff] [blame] | 178 | FAST_NATIVE_METHOD(VMStack, getAnnotatedThreadStackTrace, "(Ljava/lang/Thread;)[Ldalvik/system/AnnotatedStackTraceElement;"), |
| Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 179 | }; |
| 180 | |
| Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 181 | void register_dalvik_system_VMStack(JNIEnv* env) { |
| Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 182 | REGISTER_NATIVE_METHODS("dalvik/system/VMStack"); |
| Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | } // namespace art |