blob: 1d7d853431ebd151418dd08033798c49fe5e1b2b [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
Elliott Hugheseac76672012-05-24 21:56:51 -070019#include "jni_internal.h"
Elliott Hughes6a144332012-04-03 13:07:11 -070020#include "nth_caller_visitor.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070021#include "mirror/art_method-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070022#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "mirror/class_loader.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070024#include "mirror/object-inl.h"
Ian Rogers1eb512d2013-10-18 15:42:20 -070025#include "scoped_fast_native_object_access.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070026#include "scoped_thread_state_change.h"
Elliott Hughes01158d72011-09-19 19:47:10 -070027#include "thread_list.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070028
Elliott Hughes8daa0922011-09-11 13:46:25 -070029namespace art {
30
Ian Rogers53b8b092014-03-13 23:45:53 -070031static jobject GetThreadStack(const ScopedFastNativeObjectAccess& soa, jobject peer)
32 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
33 jobject trace = nullptr;
34 if (soa.Decode<mirror::Object*>(peer) == soa.Self()->GetPeer()) {
Sebastien Hertzee1d79a2014-02-21 15:46:30 +010035 trace = soa.Self()->CreateInternalStackTrace<false>(soa);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070036 } else {
Ian Rogers53b8b092014-03-13 23:45:53 -070037 // Suspend thread to build stack trace.
38 soa.Self()->TransitionFromRunnableToSuspended(kNative);
Brian Carlstromba32de42014-08-27 23:43:46 -070039 ThreadList* thread_list = Runtime::Current()->GetThreadList();
Ian Rogers53b8b092014-03-13 23:45:53 -070040 bool timed_out;
Ian Rogers4ad5cd32014-11-11 23:08:07 -080041 Thread* thread = thread_list->SuspendThreadByPeer(peer, true, false, &timed_out);
Ian Rogers53b8b092014-03-13 23:45:53 -070042 if (thread != nullptr) {
43 // Must be runnable to create returned array.
44 CHECK_EQ(soa.Self()->TransitionFromSuspendedToRunnable(), kNative);
Sebastien Hertzee1d79a2014-02-21 15:46:30 +010045 trace = thread->CreateInternalStackTrace<false>(soa);
Ian Rogers53b8b092014-03-13 23:45:53 -070046 soa.Self()->TransitionFromRunnableToSuspended(kNative);
47 // Restart suspended thread.
Brian Carlstromba32de42014-08-27 23:43:46 -070048 thread_list->Resume(thread, false);
Ian Rogers53b8b092014-03-13 23:45:53 -070049 } else {
50 if (timed_out) {
51 LOG(ERROR) << "Trying to get thread's stack failed as the thread failed to suspend within a "
52 "generous timeout.";
53 }
Ian Rogers15bf2d32012-08-28 17:33:04 -070054 }
Ian Rogers53b8b092014-03-13 23:45:53 -070055 CHECK_EQ(soa.Self()->TransitionFromSuspendedToRunnable(), kNative);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070056 }
Ian Rogers53b8b092014-03-13 23:45:53 -070057 return trace;
Elliott Hughes01158d72011-09-19 19:47:10 -070058}
59
Ian Rogers306057f2012-11-26 12:45:53 -080060static jint VMStack_fillStackTraceElements(JNIEnv* env, jclass, jobject javaThread,
61 jobjectArray javaSteArray) {
Ian Rogers53b8b092014-03-13 23:45:53 -070062 ScopedFastNativeObjectAccess soa(env);
63 jobject trace = GetThreadStack(soa, javaThread);
64 if (trace == nullptr) {
Elliott Hughes01158d72011-09-19 19:47:10 -070065 return 0;
66 }
67 int32_t depth;
Ian Rogers53b8b092014-03-13 23:45:53 -070068 Thread::InternalStackTraceToStackTraceElementArray(soa, trace, javaSteArray, &depth);
Elliott Hughes01158d72011-09-19 19:47:10 -070069 return depth;
Elliott Hughes8daa0922011-09-11 13:46:25 -070070}
71
Elliott Hughes6a144332012-04-03 13:07:11 -070072// Returns the defining class loader of the caller's caller.
Elliott Hughes0512f022012-03-15 22:10:52 -070073static jobject VMStack_getCallingClassLoader(JNIEnv* env, jclass) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070074 ScopedFastNativeObjectAccess soa(env);
Ian Rogers7a22fa62013-01-23 12:16:16 -080075 NthCallerVisitor visitor(soa.Self(), 2);
Ian Rogers0399dde2012-06-06 17:09:28 -070076 visitor.WalkStack();
Andreas Gampe718ac652014-08-11 18:51:53 -070077 if (UNLIKELY(visitor.caller == nullptr)) {
78 // The caller is an attached native thread.
79 return nullptr;
80 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070081 return soa.AddLocalReference<jobject>(visitor.caller->GetDeclaringClass()->GetClassLoader());
Elliott Hughes8daa0922011-09-11 13:46:25 -070082}
83
Brian Carlstrom59885472015-04-20 21:55:19 -070084static jobject VMStack_getClosestUserClassLoader(JNIEnv* env, jclass) {
Ian Rogers0399dde2012-06-06 17:09:28 -070085 struct ClosestUserClassLoaderVisitor : public StackVisitor {
Brian Carlstrom59885472015-04-20 21:55:19 -070086 explicit ClosestUserClassLoaderVisitor(Thread* thread)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010087 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
88 class_loader(nullptr) {}
Elliott Hughes08fc03a2012-06-26 17:34:00 -070089
Ian Rogersef7d42f2014-01-06 12:55:46 -080090 bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom59885472015-04-20 21:55:19 -070091 DCHECK(class_loader == nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080092 mirror::Class* c = GetMethod()->GetDeclaringClass();
93 mirror::Object* cl = c->GetClassLoader();
Brian Carlstrom59885472015-04-20 21:55:19 -070094 if (cl != nullptr) {
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080095 class_loader = cl;
Elliott Hughes530fa002012-03-12 11:44:49 -070096 return false;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080097 }
Elliott Hughes530fa002012-03-12 11:44:49 -070098 return true;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080099 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700100
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800101 mirror::Object* class_loader;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -0800102 };
Ian Rogers1eb512d2013-10-18 15:42:20 -0700103 ScopedFastNativeObjectAccess soa(env);
Brian Carlstrom59885472015-04-20 21:55:19 -0700104 ClosestUserClassLoaderVisitor visitor(soa.Self());
Ian Rogers0399dde2012-06-06 17:09:28 -0700105 visitor.WalkStack();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700106 return soa.AddLocalReference<jobject>(visitor.class_loader);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700107}
108
Elliott Hughes6a144332012-04-03 13:07:11 -0700109// Returns the class of the caller's caller's caller.
Elliott Hughes0512f022012-03-15 22:10:52 -0700110static jclass VMStack_getStackClass2(JNIEnv* env, jclass) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700111 ScopedFastNativeObjectAccess soa(env);
Ian Rogers7a22fa62013-01-23 12:16:16 -0800112 NthCallerVisitor visitor(soa.Self(), 3);
Ian Rogers0399dde2012-06-06 17:09:28 -0700113 visitor.WalkStack();
Andreas Gampe0d334ce2014-08-13 23:05:38 -0700114 if (UNLIKELY(visitor.caller == nullptr)) {
115 // The caller is an attached native thread.
116 return nullptr;
117 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700118 return soa.AddLocalReference<jclass>(visitor.caller->GetDeclaringClass());
Elliott Hughes8daa0922011-09-11 13:46:25 -0700119}
120
Elliott Hughes0512f022012-03-15 22:10:52 -0700121static jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700122 ScopedFastNativeObjectAccess soa(env);
123 jobject trace = GetThreadStack(soa, javaThread);
124 if (trace == nullptr) {
125 return nullptr;
Elliott Hughes01158d72011-09-19 19:47:10 -0700126 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700127 return Thread::InternalStackTraceToStackTraceElementArray(soa, trace);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700128}
129
130static JNINativeMethod gMethods[] = {
Ian Rogers53b8b092014-03-13 23:45:53 -0700131 NATIVE_METHOD(VMStack, fillStackTraceElements, "!(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"),
Ian Rogers1eb512d2013-10-18 15:42:20 -0700132 NATIVE_METHOD(VMStack, getCallingClassLoader, "!()Ljava/lang/ClassLoader;"),
Brian Carlstrom59885472015-04-20 21:55:19 -0700133 NATIVE_METHOD(VMStack, getClosestUserClassLoader, "!()Ljava/lang/ClassLoader;"),
Ian Rogers1eb512d2013-10-18 15:42:20 -0700134 NATIVE_METHOD(VMStack, getStackClass2, "!()Ljava/lang/Class;"),
Ian Rogers53b8b092014-03-13 23:45:53 -0700135 NATIVE_METHOD(VMStack, getThreadStackTrace, "!(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700136};
137
Elliott Hughes8daa0922011-09-11 13:46:25 -0700138void register_dalvik_system_VMStack(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700139 REGISTER_NATIVE_METHODS("dalvik/system/VMStack");
Elliott Hughes8daa0922011-09-11 13:46:25 -0700140}
141
142} // namespace art