blob: f91536544a03486a92f19360a775b94d374c44da [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
Elliott Hugheseac76672012-05-24 21:56:51 -070017#include "jni_internal.h"
Elliott Hughes6a144332012-04-03 13:07:11 -070018#include "nth_caller_visitor.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070019#include "mirror/art_method-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070020#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "mirror/class_loader.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070022#include "mirror/object-inl.h"
Ian Rogers1eb512d2013-10-18 15:42:20 -070023#include "scoped_fast_native_object_access.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070024#include "scoped_thread_state_change.h"
Elliott Hughes01158d72011-09-19 19:47:10 -070025#include "thread_list.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070026
Elliott Hughes8daa0922011-09-11 13:46:25 -070027namespace art {
28
Ian Rogers00f7d0e2012-07-19 15:28:27 -070029static jobject GetThreadStack(JNIEnv* env, jobject peer) {
Ian Rogerscfaa4552012-11-26 21:00:08 -080030 {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070031 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032 if (soa.Decode<mirror::Object*>(peer) == soa.Self()->GetPeer()) {
Ian Rogerscfaa4552012-11-26 21:00:08 -080033 return soa.Self()->CreateInternalStackTrace(soa);
34 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070035 }
36 // Suspend thread to build stack trace.
Elliott Hughesf327e072013-01-09 16:01:26 -080037 bool timed_out;
Ian Rogersd9c4fc92013-10-01 19:45:43 -070038 Thread* thread = ThreadList::SuspendThreadByPeer(peer, true, false, &timed_out);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070039 if (thread != NULL) {
40 jobject trace;
41 {
42 ScopedObjectAccess soa(env);
43 trace = thread->CreateInternalStackTrace(soa);
44 }
45 // Restart suspended thread.
Ian Rogersd9c4fc92013-10-01 19:45:43 -070046 Runtime::Current()->GetThreadList()->Resume(thread, false);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070047 return trace;
48 } else {
Elliott Hughesf327e072013-01-09 16:01:26 -080049 if (timed_out) {
Ian Rogers15bf2d32012-08-28 17:33:04 -070050 LOG(ERROR) << "Trying to get thread's stack failed as the thread failed to suspend within a "
51 "generous timeout.";
52 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070053 return NULL;
54 }
Elliott Hughes01158d72011-09-19 19:47:10 -070055}
56
Ian Rogers306057f2012-11-26 12:45:53 -080057static jint VMStack_fillStackTraceElements(JNIEnv* env, jclass, jobject javaThread,
58 jobjectArray javaSteArray) {
Elliott Hughes01158d72011-09-19 19:47:10 -070059 jobject trace = GetThreadStack(env, javaThread);
60 if (trace == NULL) {
61 return 0;
62 }
63 int32_t depth;
64 Thread::InternalStackTraceToStackTraceElementArray(env, trace, javaSteArray, &depth);
65 return depth;
Elliott Hughes8daa0922011-09-11 13:46:25 -070066}
67
Elliott Hughes6a144332012-04-03 13:07:11 -070068// Returns the defining class loader of the caller's caller.
Elliott Hughes0512f022012-03-15 22:10:52 -070069static jobject VMStack_getCallingClassLoader(JNIEnv* env, jclass) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070070 ScopedFastNativeObjectAccess soa(env);
Ian Rogers7a22fa62013-01-23 12:16:16 -080071 NthCallerVisitor visitor(soa.Self(), 2);
Ian Rogers0399dde2012-06-06 17:09:28 -070072 visitor.WalkStack();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070073 return soa.AddLocalReference<jobject>(visitor.caller->GetDeclaringClass()->GetClassLoader());
Elliott Hughes8daa0922011-09-11 13:46:25 -070074}
75
Ian Rogers306057f2012-11-26 12:45:53 -080076static jobject VMStack_getClosestUserClassLoader(JNIEnv* env, jclass, jobject javaBootstrap,
77 jobject javaSystem) {
Ian Rogers0399dde2012-06-06 17:09:28 -070078 struct ClosestUserClassLoaderVisitor : public StackVisitor {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080079 ClosestUserClassLoaderVisitor(Thread* thread, mirror::Object* bootstrap, mirror::Object* system)
Ian Rogers7a22fa62013-01-23 12:16:16 -080080 : StackVisitor(thread, NULL), bootstrap(bootstrap), system(system), class_loader(NULL) {}
Elliott Hughes08fc03a2012-06-26 17:34:00 -070081
Ian Rogers0399dde2012-06-06 17:09:28 -070082 bool VisitFrame() {
Elliott Hughes530fa002012-03-12 11:44:49 -070083 DCHECK(class_loader == NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080084 mirror::Class* c = GetMethod()->GetDeclaringClass();
85 mirror::Object* cl = c->GetClassLoader();
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080086 if (cl != NULL && cl != bootstrap && cl != system) {
87 class_loader = cl;
Elliott Hughes530fa002012-03-12 11:44:49 -070088 return false;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080089 }
Elliott Hughes530fa002012-03-12 11:44:49 -070090 return true;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080091 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -070092
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080093 mirror::Object* bootstrap;
94 mirror::Object* system;
95 mirror::Object* class_loader;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080096 };
Ian Rogers1eb512d2013-10-18 15:42:20 -070097 ScopedFastNativeObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080098 mirror::Object* bootstrap = soa.Decode<mirror::Object*>(javaBootstrap);
99 mirror::Object* system = soa.Decode<mirror::Object*>(javaSystem);
Ian Rogers7a22fa62013-01-23 12:16:16 -0800100 ClosestUserClassLoaderVisitor visitor(soa.Self(), bootstrap, system);
Ian Rogers0399dde2012-06-06 17:09:28 -0700101 visitor.WalkStack();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700102 return soa.AddLocalReference<jobject>(visitor.class_loader);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700103}
104
Elliott Hughes6a144332012-04-03 13:07:11 -0700105// Returns the class of the caller's caller's caller.
Elliott Hughes0512f022012-03-15 22:10:52 -0700106static jclass VMStack_getStackClass2(JNIEnv* env, jclass) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700107 ScopedFastNativeObjectAccess soa(env);
Ian Rogers7a22fa62013-01-23 12:16:16 -0800108 NthCallerVisitor visitor(soa.Self(), 3);
Ian Rogers0399dde2012-06-06 17:09:28 -0700109 visitor.WalkStack();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700110 return soa.AddLocalReference<jclass>(visitor.caller->GetDeclaringClass());
Elliott Hughes8daa0922011-09-11 13:46:25 -0700111}
112
Elliott Hughes0512f022012-03-15 22:10:52 -0700113static jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700114 jobject trace = GetThreadStack(env, javaThread);
115 if (trace == NULL) {
116 return NULL;
117 }
118 return Thread::InternalStackTraceToStackTraceElementArray(env, trace);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700119}
120
121static JNINativeMethod gMethods[] = {
122 NATIVE_METHOD(VMStack, fillStackTraceElements, "(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"),
Ian Rogers1eb512d2013-10-18 15:42:20 -0700123 NATIVE_METHOD(VMStack, getCallingClassLoader, "!()Ljava/lang/ClassLoader;"),
124 NATIVE_METHOD(VMStack, getClosestUserClassLoader, "!(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)Ljava/lang/ClassLoader;"),
125 NATIVE_METHOD(VMStack, getStackClass2, "!()Ljava/lang/Class;"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700126 NATIVE_METHOD(VMStack, getThreadStackTrace, "(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"),
127};
128
Elliott Hughes8daa0922011-09-11 13:46:25 -0700129void register_dalvik_system_VMStack(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700130 REGISTER_NATIVE_METHODS("dalvik/system/VMStack");
Elliott Hughes8daa0922011-09-11 13:46:25 -0700131}
132
133} // namespace art