blob: 32733a84092a76ad33a97100ff2e2c087328fb1c [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
Andreas Gampe277ccbd2014-11-03 21:36:10 -080017#include "dalvik_system_VMStack.h"
18
Andreas Gampe6362e232017-12-11 20:43:25 -080019#include <type_traits>
20
Andreas Gampea14100c2017-04-24 15:09:56 -070021#include "nativehelper/jni_macros.h"
22
Mathieu Chartiere401d142015-04-22 13:56:20 -070023#include "art_method-inl.h"
Mathieu Chartier4201cf02017-01-12 14:51:44 -080024#include "gc/task_processor.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010025#include "jni/jni_internal.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070026#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "mirror/class_loader.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070028#include "mirror/object-inl.h"
Andreas Gampe87583b32017-05-25 11:22:18 -070029#include "native_util.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070030#include "nth_caller_visitor.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070031#include "scoped_fast_native_object_access-inl.h"
32#include "scoped_thread_state_change-inl.h"
Elliott Hughes01158d72011-09-19 19:47:10 -070033#include "thread_list.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070034
Elliott Hughes8daa0922011-09-11 13:46:25 -070035namespace art {
36
Andreas Gampe6362e232017-12-11 20:43:25 -080037template <typename T,
38 typename ResultT =
39 typename std::result_of<T(Thread*, const ScopedFastNativeObjectAccess&)>::type>
40static ResultT GetThreadStack(const ScopedFastNativeObjectAccess& soa,
41 jobject peer,
42 T fn)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070043 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe6362e232017-12-11 20:43:25 -080044 ResultT trace = nullptr;
Mathieu Chartier4201cf02017-01-12 14:51:44 -080045 ObjPtr<mirror::Object> decoded_peer = soa.Decode<mirror::Object>(peer);
46 if (decoded_peer == soa.Self()->GetPeer()) {
Andreas Gampe6362e232017-12-11 20:43:25 -080047 trace = fn(soa.Self(), soa);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070048 } else {
Mathieu Chartier4201cf02017-01-12 14:51:44 -080049 // 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 Geoffrayffc8cad2017-02-10 10:59:22 +000054 if (heap_task_thread != nullptr && decoded_peer == heap_task_thread->GetPeerFromOtherThread()) {
Mathieu Chartier4201cf02017-01-12 14:51:44 -080055 return nullptr;
56 }
Ian Rogers53b8b092014-03-13 23:45:53 -070057 // Suspend thread to build stack trace.
Mathieu Chartier4f55e222015-09-04 13:26:21 -070058 ScopedThreadSuspension sts(soa.Self(), kNative);
Brian Carlstromba32de42014-08-27 23:43:46 -070059 ThreadList* thread_list = Runtime::Current()->GetThreadList();
Ian Rogers53b8b092014-03-13 23:45:53 -070060 bool timed_out;
Alex Light46f93402017-06-29 11:59:50 -070061 Thread* thread = thread_list->SuspendThreadByPeer(peer,
Andreas Gampe98ea9d92018-10-19 14:06:15 -070062 /* request_suspension= */ true,
Alex Light46f93402017-06-29 11:59:50 -070063 SuspendReason::kInternal,
64 &timed_out);
Ian Rogers53b8b092014-03-13 23:45:53 -070065 if (thread != nullptr) {
66 // Must be runnable to create returned array.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -070067 {
68 ScopedObjectAccess soa2(soa.Self());
Andreas Gampe6362e232017-12-11 20:43:25 -080069 trace = fn(thread, soa);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -070070 }
Ian Rogers53b8b092014-03-13 23:45:53 -070071 // Restart suspended thread.
Alex Light88fd7202017-06-30 08:31:59 -070072 bool resumed = thread_list->Resume(thread, SuspendReason::kInternal);
73 DCHECK(resumed);
Mathieu Chartier4f55e222015-09-04 13:26:21 -070074 } 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 Rogers15bf2d32012-08-28 17:33:04 -070077 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070078 }
Ian Rogers53b8b092014-03-13 23:45:53 -070079 return trace;
Elliott Hughes01158d72011-09-19 19:47:10 -070080}
81
Ian Rogers306057f2012-11-26 12:45:53 -080082static jint VMStack_fillStackTraceElements(JNIEnv* env, jclass, jobject javaThread,
83 jobjectArray javaSteArray) {
Ian Rogers53b8b092014-03-13 23:45:53 -070084 ScopedFastNativeObjectAccess soa(env);
Andreas Gampe6362e232017-12-11 20:43:25 -080085 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 Rogers53b8b092014-03-13 23:45:53 -070090 if (trace == nullptr) {
Elliott Hughes01158d72011-09-19 19:47:10 -070091 return 0;
92 }
93 int32_t depth;
Ian Rogers53b8b092014-03-13 23:45:53 -070094 Thread::InternalStackTraceToStackTraceElementArray(soa, trace, javaSteArray, &depth);
Elliott Hughes01158d72011-09-19 19:47:10 -070095 return depth;
Elliott Hughes8daa0922011-09-11 13:46:25 -070096}
97
Elliott Hughes6a144332012-04-03 13:07:11 -070098// Returns the defining class loader of the caller's caller.
Elliott Hughes0512f022012-03-15 22:10:52 -070099static jobject VMStack_getCallingClassLoader(JNIEnv* env, jclass) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700100 ScopedFastNativeObjectAccess soa(env);
Ian Rogers7a22fa62013-01-23 12:16:16 -0800101 NthCallerVisitor visitor(soa.Self(), 2);
Ian Rogers0399dde2012-06-06 17:09:28 -0700102 visitor.WalkStack();
Andreas Gampe718ac652014-08-11 18:51:53 -0700103 if (UNLIKELY(visitor.caller == nullptr)) {
104 // The caller is an attached native thread.
105 return nullptr;
106 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700107 return soa.AddLocalReference<jobject>(visitor.caller->GetDeclaringClass()->GetClassLoader());
Elliott Hughes8daa0922011-09-11 13:46:25 -0700108}
109
Brian Carlstrom59885472015-04-20 21:55:19 -0700110static jobject VMStack_getClosestUserClassLoader(JNIEnv* env, jclass) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700111 struct ClosestUserClassLoaderVisitor : public StackVisitor {
Brian Carlstrom59885472015-04-20 21:55:19 -0700112 explicit ClosestUserClassLoaderVisitor(Thread* thread)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100113 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
114 class_loader(nullptr) {}
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700115
Andreas Gampefa6a1b02018-09-07 08:11:55 -0700116 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
Brian Carlstrom59885472015-04-20 21:55:19 -0700117 DCHECK(class_loader == nullptr);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700118 ObjPtr<mirror::Class> c = GetMethod()->GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700119 // c is null for runtime methods.
120 if (c != nullptr) {
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700121 ObjPtr<mirror::Object> cl = c->GetClassLoader();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700122 if (cl != nullptr) {
123 class_loader = cl;
124 return false;
125 }
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -0800126 }
Elliott Hughes530fa002012-03-12 11:44:49 -0700127 return true;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -0800128 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700129
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700130 ObjPtr<mirror::Object> class_loader;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -0800131 };
Ian Rogers1eb512d2013-10-18 15:42:20 -0700132 ScopedFastNativeObjectAccess soa(env);
Brian Carlstrom59885472015-04-20 21:55:19 -0700133 ClosestUserClassLoaderVisitor visitor(soa.Self());
Ian Rogers0399dde2012-06-06 17:09:28 -0700134 visitor.WalkStack();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700135 return soa.AddLocalReference<jobject>(visitor.class_loader);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700136}
137
Elliott Hughes6a144332012-04-03 13:07:11 -0700138// Returns the class of the caller's caller's caller.
Elliott Hughes0512f022012-03-15 22:10:52 -0700139static jclass VMStack_getStackClass2(JNIEnv* env, jclass) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700140 ScopedFastNativeObjectAccess soa(env);
Ian Rogers7a22fa62013-01-23 12:16:16 -0800141 NthCallerVisitor visitor(soa.Self(), 3);
Ian Rogers0399dde2012-06-06 17:09:28 -0700142 visitor.WalkStack();
Andreas Gampe0d334ce2014-08-13 23:05:38 -0700143 if (UNLIKELY(visitor.caller == nullptr)) {
144 // The caller is an attached native thread.
145 return nullptr;
146 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700147 return soa.AddLocalReference<jclass>(visitor.caller->GetDeclaringClass());
Elliott Hughes8daa0922011-09-11 13:46:25 -0700148}
149
Elliott Hughes0512f022012-03-15 22:10:52 -0700150static jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700151 ScopedFastNativeObjectAccess soa(env);
Andreas Gampe6362e232017-12-11 20:43:25 -0800152 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 Rogers53b8b092014-03-13 23:45:53 -0700157 if (trace == nullptr) {
158 return nullptr;
Elliott Hughes01158d72011-09-19 19:47:10 -0700159 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700160 return Thread::InternalStackTraceToStackTraceElementArray(soa, trace);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700161}
162
Andreas Gampefb6b0b12017-12-11 20:47:56 -0800163static 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 Hughes8daa0922011-09-11 13:46:25 -0700172static JNINativeMethod gMethods[] = {
Igor Murashkin3b6f4402017-02-16 16:13:17 -0800173 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 Gampefb6b0b12017-12-11 20:47:56 -0800178 FAST_NATIVE_METHOD(VMStack, getAnnotatedThreadStackTrace, "(Ljava/lang/Thread;)[Ldalvik/system/AnnotatedStackTraceElement;"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700179};
180
Elliott Hughes8daa0922011-09-11 13:46:25 -0700181void register_dalvik_system_VMStack(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700182 REGISTER_NATIVE_METHODS("dalvik/system/VMStack");
Elliott Hughes8daa0922011-09-11 13:46:25 -0700183}
184
185} // namespace art