blob: 1a80d6286b3d4034da21288dbba40db6edc6385a [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019#include "mirror/abstract_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 Rogers00f7d0e2012-07-19 15:28:27 -070023#include "scoped_thread_state_change.h"
Elliott Hughes01158d72011-09-19 19:47:10 -070024#include "thread_list.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070025
Elliott Hughes8daa0922011-09-11 13:46:25 -070026namespace art {
27
Ian Rogers00f7d0e2012-07-19 15:28:27 -070028static jobject GetThreadStack(JNIEnv* env, jobject peer) {
Ian Rogerscfaa4552012-11-26 21:00:08 -080029 {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070030 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031 if (soa.Decode<mirror::Object*>(peer) == soa.Self()->GetPeer()) {
Ian Rogerscfaa4552012-11-26 21:00:08 -080032 return soa.Self()->CreateInternalStackTrace(soa);
33 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070034 }
35 // Suspend thread to build stack trace.
Elliott Hughesf327e072013-01-09 16:01:26 -080036 bool timed_out;
37 Thread* thread = Thread::SuspendForDebugger(peer, true, &timed_out);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070038 if (thread != NULL) {
39 jobject trace;
40 {
41 ScopedObjectAccess soa(env);
42 trace = thread->CreateInternalStackTrace(soa);
43 }
44 // Restart suspended thread.
45 Runtime::Current()->GetThreadList()->Resume(thread, true);
46 return trace;
47 } else {
Elliott Hughesf327e072013-01-09 16:01:26 -080048 if (timed_out) {
Ian Rogers15bf2d32012-08-28 17:33:04 -070049 LOG(ERROR) << "Trying to get thread's stack failed as the thread failed to suspend within a "
50 "generous timeout.";
51 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070052 return NULL;
53 }
Elliott Hughes01158d72011-09-19 19:47:10 -070054}
55
Ian Rogers306057f2012-11-26 12:45:53 -080056static jint VMStack_fillStackTraceElements(JNIEnv* env, jclass, jobject javaThread,
57 jobjectArray javaSteArray) {
Elliott Hughes01158d72011-09-19 19:47:10 -070058 jobject trace = GetThreadStack(env, javaThread);
59 if (trace == NULL) {
60 return 0;
61 }
62 int32_t depth;
63 Thread::InternalStackTraceToStackTraceElementArray(env, trace, javaSteArray, &depth);
64 return depth;
Elliott Hughes8daa0922011-09-11 13:46:25 -070065}
66
Elliott Hughes6a144332012-04-03 13:07:11 -070067// Returns the defining class loader of the caller's caller.
Elliott Hughes0512f022012-03-15 22:10:52 -070068static jobject VMStack_getCallingClassLoader(JNIEnv* env, jclass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070069 ScopedObjectAccess soa(env);
Ian Rogers7a22fa62013-01-23 12:16:16 -080070 NthCallerVisitor visitor(soa.Self(), 2);
Ian Rogers0399dde2012-06-06 17:09:28 -070071 visitor.WalkStack();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070072 return soa.AddLocalReference<jobject>(visitor.caller->GetDeclaringClass()->GetClassLoader());
Elliott Hughes8daa0922011-09-11 13:46:25 -070073}
74
Ian Rogers306057f2012-11-26 12:45:53 -080075static jobject VMStack_getClosestUserClassLoader(JNIEnv* env, jclass, jobject javaBootstrap,
76 jobject javaSystem) {
Ian Rogers0399dde2012-06-06 17:09:28 -070077 struct ClosestUserClassLoaderVisitor : public StackVisitor {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080078 ClosestUserClassLoaderVisitor(Thread* thread, mirror::Object* bootstrap, mirror::Object* system)
Ian Rogers7a22fa62013-01-23 12:16:16 -080079 : StackVisitor(thread, NULL), bootstrap(bootstrap), system(system), class_loader(NULL) {}
Elliott Hughes08fc03a2012-06-26 17:34:00 -070080
Ian Rogers0399dde2012-06-06 17:09:28 -070081 bool VisitFrame() {
Elliott Hughes530fa002012-03-12 11:44:49 -070082 DCHECK(class_loader == NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080083 mirror::Class* c = GetMethod()->GetDeclaringClass();
84 mirror::Object* cl = c->GetClassLoader();
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080085 if (cl != NULL && cl != bootstrap && cl != system) {
86 class_loader = cl;
Elliott Hughes530fa002012-03-12 11:44:49 -070087 return false;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080088 }
Elliott Hughes530fa002012-03-12 11:44:49 -070089 return true;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080090 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -070091
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080092 mirror::Object* bootstrap;
93 mirror::Object* system;
94 mirror::Object* class_loader;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080095 };
Ian Rogers00f7d0e2012-07-19 15:28:27 -070096 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080097 mirror::Object* bootstrap = soa.Decode<mirror::Object*>(javaBootstrap);
98 mirror::Object* system = soa.Decode<mirror::Object*>(javaSystem);
Ian Rogers7a22fa62013-01-23 12:16:16 -080099 ClosestUserClassLoaderVisitor visitor(soa.Self(), bootstrap, system);
Ian Rogers0399dde2012-06-06 17:09:28 -0700100 visitor.WalkStack();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700101 return soa.AddLocalReference<jobject>(visitor.class_loader);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700102}
103
Elliott Hughes6a144332012-04-03 13:07:11 -0700104// Returns the class of the caller's caller's caller.
Elliott Hughes0512f022012-03-15 22:10:52 -0700105static jclass VMStack_getStackClass2(JNIEnv* env, jclass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700106 ScopedObjectAccess soa(env);
Ian Rogers7a22fa62013-01-23 12:16:16 -0800107 NthCallerVisitor visitor(soa.Self(), 3);
Ian Rogers0399dde2012-06-06 17:09:28 -0700108 visitor.WalkStack();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700109 return soa.AddLocalReference<jclass>(visitor.caller->GetDeclaringClass());
Elliott Hughes8daa0922011-09-11 13:46:25 -0700110}
111
Elliott Hughes0512f022012-03-15 22:10:52 -0700112static jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700113 jobject trace = GetThreadStack(env, javaThread);
114 if (trace == NULL) {
115 return NULL;
116 }
117 return Thread::InternalStackTraceToStackTraceElementArray(env, trace);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700118}
119
120static JNINativeMethod gMethods[] = {
121 NATIVE_METHOD(VMStack, fillStackTraceElements, "(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"),
122 NATIVE_METHOD(VMStack, getCallingClassLoader, "()Ljava/lang/ClassLoader;"),
Elliott Hughes6bbe8b02011-09-20 13:15:00 -0700123 NATIVE_METHOD(VMStack, getClosestUserClassLoader, "(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)Ljava/lang/ClassLoader;"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700124 NATIVE_METHOD(VMStack, getStackClass2, "()Ljava/lang/Class;"),
125 NATIVE_METHOD(VMStack, getThreadStackTrace, "(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"),
126};
127
Elliott Hughes8daa0922011-09-11 13:46:25 -0700128void register_dalvik_system_VMStack(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700129 REGISTER_NATIVE_METHODS("dalvik/system/VMStack");
Elliott Hughes8daa0922011-09-11 13:46:25 -0700130}
131
132} // namespace art