blob: a88563da1fefdb3dd70645b0662dfeacc714b720 [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 Gampea14100c2017-04-24 15:09:56 -070019#include "nativehelper/jni_macros.h"
20
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "art_method-inl.h"
Mathieu Chartier4201cf02017-01-12 14:51:44 -080022#include "gc/task_processor.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070023#include "jni_internal.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070024#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "mirror/class_loader.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070026#include "mirror/object-inl.h"
Andreas Gampe87583b32017-05-25 11:22:18 -070027#include "native_util.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070028#include "nth_caller_visitor.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070029#include "scoped_fast_native_object_access-inl.h"
30#include "scoped_thread_state_change-inl.h"
Elliott Hughes01158d72011-09-19 19:47:10 -070031#include "thread_list.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070032
Elliott Hughes8daa0922011-09-11 13:46:25 -070033namespace art {
34
Ian Rogers53b8b092014-03-13 23:45:53 -070035static jobject GetThreadStack(const ScopedFastNativeObjectAccess& soa, jobject peer)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070036 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers53b8b092014-03-13 23:45:53 -070037 jobject trace = nullptr;
Mathieu Chartier4201cf02017-01-12 14:51:44 -080038 ObjPtr<mirror::Object> decoded_peer = soa.Decode<mirror::Object>(peer);
39 if (decoded_peer == soa.Self()->GetPeer()) {
Sebastien Hertzee1d79a2014-02-21 15:46:30 +010040 trace = soa.Self()->CreateInternalStackTrace<false>(soa);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070041 } else {
Mathieu Chartier4201cf02017-01-12 14:51:44 -080042 // Never allow suspending the heap task thread since it may deadlock if allocations are
43 // required for the stack trace.
44 Thread* heap_task_thread =
45 Runtime::Current()->GetHeap()->GetTaskProcessor()->GetRunningThread();
46 // heap_task_thread could be null if the daemons aren't yet started.
Nicolas Geoffrayffc8cad2017-02-10 10:59:22 +000047 if (heap_task_thread != nullptr && decoded_peer == heap_task_thread->GetPeerFromOtherThread()) {
Mathieu Chartier4201cf02017-01-12 14:51:44 -080048 return nullptr;
49 }
Ian Rogers53b8b092014-03-13 23:45:53 -070050 // Suspend thread to build stack trace.
Mathieu Chartier4f55e222015-09-04 13:26:21 -070051 ScopedThreadSuspension sts(soa.Self(), kNative);
Brian Carlstromba32de42014-08-27 23:43:46 -070052 ThreadList* thread_list = Runtime::Current()->GetThreadList();
Ian Rogers53b8b092014-03-13 23:45:53 -070053 bool timed_out;
Alex Light46f93402017-06-29 11:59:50 -070054 Thread* thread = thread_list->SuspendThreadByPeer(peer,
55 /* request_suspension */ true,
56 SuspendReason::kInternal,
57 &timed_out);
Ian Rogers53b8b092014-03-13 23:45:53 -070058 if (thread != nullptr) {
59 // Must be runnable to create returned array.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -070060 {
61 ScopedObjectAccess soa2(soa.Self());
62 trace = thread->CreateInternalStackTrace<false>(soa);
63 }
Ian Rogers53b8b092014-03-13 23:45:53 -070064 // Restart suspended thread.
Alex Light88fd7202017-06-30 08:31:59 -070065 bool resumed = thread_list->Resume(thread, SuspendReason::kInternal);
66 DCHECK(resumed);
Mathieu Chartier4f55e222015-09-04 13:26:21 -070067 } else if (timed_out) {
68 LOG(ERROR) << "Trying to get thread's stack failed as the thread failed to suspend within a "
69 "generous timeout.";
Ian Rogers15bf2d32012-08-28 17:33:04 -070070 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070071 }
Ian Rogers53b8b092014-03-13 23:45:53 -070072 return trace;
Elliott Hughes01158d72011-09-19 19:47:10 -070073}
74
Ian Rogers306057f2012-11-26 12:45:53 -080075static jint VMStack_fillStackTraceElements(JNIEnv* env, jclass, jobject javaThread,
76 jobjectArray javaSteArray) {
Ian Rogers53b8b092014-03-13 23:45:53 -070077 ScopedFastNativeObjectAccess soa(env);
78 jobject trace = GetThreadStack(soa, javaThread);
79 if (trace == nullptr) {
Elliott Hughes01158d72011-09-19 19:47:10 -070080 return 0;
81 }
82 int32_t depth;
Ian Rogers53b8b092014-03-13 23:45:53 -070083 Thread::InternalStackTraceToStackTraceElementArray(soa, trace, javaSteArray, &depth);
Elliott Hughes01158d72011-09-19 19:47:10 -070084 return depth;
Elliott Hughes8daa0922011-09-11 13:46:25 -070085}
86
Elliott Hughes6a144332012-04-03 13:07:11 -070087// Returns the defining class loader of the caller's caller.
Elliott Hughes0512f022012-03-15 22:10:52 -070088static jobject VMStack_getCallingClassLoader(JNIEnv* env, jclass) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070089 ScopedFastNativeObjectAccess soa(env);
Ian Rogers7a22fa62013-01-23 12:16:16 -080090 NthCallerVisitor visitor(soa.Self(), 2);
Ian Rogers0399dde2012-06-06 17:09:28 -070091 visitor.WalkStack();
Andreas Gampe718ac652014-08-11 18:51:53 -070092 if (UNLIKELY(visitor.caller == nullptr)) {
93 // The caller is an attached native thread.
94 return nullptr;
95 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070096 return soa.AddLocalReference<jobject>(visitor.caller->GetDeclaringClass()->GetClassLoader());
Elliott Hughes8daa0922011-09-11 13:46:25 -070097}
98
Brian Carlstrom59885472015-04-20 21:55:19 -070099static jobject VMStack_getClosestUserClassLoader(JNIEnv* env, jclass) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700100 struct ClosestUserClassLoaderVisitor : public StackVisitor {
Brian Carlstrom59885472015-04-20 21:55:19 -0700101 explicit ClosestUserClassLoaderVisitor(Thread* thread)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100102 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
103 class_loader(nullptr) {}
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700104
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700105 bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) {
Brian Carlstrom59885472015-04-20 21:55:19 -0700106 DCHECK(class_loader == nullptr);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700107 ObjPtr<mirror::Class> c = GetMethod()->GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700108 // c is null for runtime methods.
109 if (c != nullptr) {
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700110 ObjPtr<mirror::Object> cl = c->GetClassLoader();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700111 if (cl != nullptr) {
112 class_loader = cl;
113 return false;
114 }
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -0800115 }
Elliott Hughes530fa002012-03-12 11:44:49 -0700116 return true;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -0800117 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700118
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700119 ObjPtr<mirror::Object> class_loader;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -0800120 };
Ian Rogers1eb512d2013-10-18 15:42:20 -0700121 ScopedFastNativeObjectAccess soa(env);
Brian Carlstrom59885472015-04-20 21:55:19 -0700122 ClosestUserClassLoaderVisitor visitor(soa.Self());
Ian Rogers0399dde2012-06-06 17:09:28 -0700123 visitor.WalkStack();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700124 return soa.AddLocalReference<jobject>(visitor.class_loader);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700125}
126
Elliott Hughes6a144332012-04-03 13:07:11 -0700127// Returns the class of the caller's caller's caller.
Elliott Hughes0512f022012-03-15 22:10:52 -0700128static jclass VMStack_getStackClass2(JNIEnv* env, jclass) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700129 ScopedFastNativeObjectAccess soa(env);
Ian Rogers7a22fa62013-01-23 12:16:16 -0800130 NthCallerVisitor visitor(soa.Self(), 3);
Ian Rogers0399dde2012-06-06 17:09:28 -0700131 visitor.WalkStack();
Andreas Gampe0d334ce2014-08-13 23:05:38 -0700132 if (UNLIKELY(visitor.caller == nullptr)) {
133 // The caller is an attached native thread.
134 return nullptr;
135 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700136 return soa.AddLocalReference<jclass>(visitor.caller->GetDeclaringClass());
Elliott Hughes8daa0922011-09-11 13:46:25 -0700137}
138
Elliott Hughes0512f022012-03-15 22:10:52 -0700139static jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700140 ScopedFastNativeObjectAccess soa(env);
141 jobject trace = GetThreadStack(soa, javaThread);
142 if (trace == nullptr) {
143 return nullptr;
Elliott Hughes01158d72011-09-19 19:47:10 -0700144 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700145 return Thread::InternalStackTraceToStackTraceElementArray(soa, trace);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700146}
147
148static JNINativeMethod gMethods[] = {
Igor Murashkin3b6f4402017-02-16 16:13:17 -0800149 FAST_NATIVE_METHOD(VMStack, fillStackTraceElements, "(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"),
150 FAST_NATIVE_METHOD(VMStack, getCallingClassLoader, "()Ljava/lang/ClassLoader;"),
151 FAST_NATIVE_METHOD(VMStack, getClosestUserClassLoader, "()Ljava/lang/ClassLoader;"),
152 FAST_NATIVE_METHOD(VMStack, getStackClass2, "()Ljava/lang/Class;"),
153 FAST_NATIVE_METHOD(VMStack, getThreadStackTrace, "(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700154};
155
Elliott Hughes8daa0922011-09-11 13:46:25 -0700156void register_dalvik_system_VMStack(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700157 REGISTER_NATIVE_METHODS("dalvik/system/VMStack");
Elliott Hughes8daa0922011-09-11 13:46:25 -0700158}
159
160} // namespace art