blob: 3284c979d8df6f0558003133d7971e647e8bccee [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 Hughes01158d72011-09-19 19:47:10 -070017#include "class_loader.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070018#include "jni_internal.h"
Elliott Hughes6a144332012-04-03 13:07:11 -070019#include "nth_caller_visitor.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070020#include "object.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070021#include "scoped_thread_state_change.h"
Elliott Hughes01158d72011-09-19 19:47:10 -070022#include "thread_list.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070023
Elliott Hughes8daa0922011-09-11 13:46:25 -070024namespace art {
25
Ian Rogers00f7d0e2012-07-19 15:28:27 -070026static jobject GetThreadStack(JNIEnv* env, jobject peer) {
27 bool timeout;
28 {
29 ScopedObjectAccess soa(env);
30 Thread* self = Thread::Current();
31 if (soa.Decode<Object*>(peer) == self->GetPeer()) {
32 return self->CreateInternalStackTrace(soa);
33 }
34 }
35 // Suspend thread to build stack trace.
36 Thread* thread = Thread::SuspendForDebugger(peer, true, &timeout);
37 if (thread != NULL) {
38 jobject trace;
39 {
40 ScopedObjectAccess soa(env);
41 trace = thread->CreateInternalStackTrace(soa);
42 }
43 // Restart suspended thread.
44 Runtime::Current()->GetThreadList()->Resume(thread, true);
45 return trace;
46 } else {
47 return NULL;
48 }
Elliott Hughes01158d72011-09-19 19:47:10 -070049}
50
Elliott Hughes0512f022012-03-15 22:10:52 -070051static jint VMStack_fillStackTraceElements(JNIEnv* env, jclass, jobject javaThread, jobjectArray javaSteArray) {
Elliott Hughes01158d72011-09-19 19:47:10 -070052 jobject trace = GetThreadStack(env, javaThread);
53 if (trace == NULL) {
54 return 0;
55 }
56 int32_t depth;
57 Thread::InternalStackTraceToStackTraceElementArray(env, trace, javaSteArray, &depth);
58 return depth;
Elliott Hughes8daa0922011-09-11 13:46:25 -070059}
60
Elliott Hughes6a144332012-04-03 13:07:11 -070061// Returns the defining class loader of the caller's caller.
Elliott Hughes0512f022012-03-15 22:10:52 -070062static jobject VMStack_getCallingClassLoader(JNIEnv* env, jclass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070063 ScopedObjectAccess soa(env);
64 NthCallerVisitor visitor(soa.Self()->GetManagedStack(), soa.Self()->GetTraceStack(), 2);
Ian Rogers0399dde2012-06-06 17:09:28 -070065 visitor.WalkStack();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070066 return soa.AddLocalReference<jobject>(visitor.caller->GetDeclaringClass()->GetClassLoader());
Elliott Hughes8daa0922011-09-11 13:46:25 -070067}
68
Elliott Hughes0512f022012-03-15 22:10:52 -070069static jobject VMStack_getClosestUserClassLoader(JNIEnv* env, jclass, jobject javaBootstrap, jobject javaSystem) {
Ian Rogers0399dde2012-06-06 17:09:28 -070070 struct ClosestUserClassLoaderVisitor : public StackVisitor {
71 ClosestUserClassLoaderVisitor(const ManagedStack* stack,
72 const std::vector<TraceStackFrame>* trace_stack,
73 Object* bootstrap, Object* system)
Elliott Hughes08fc03a2012-06-26 17:34:00 -070074 : StackVisitor(stack, trace_stack, NULL),
75 bootstrap(bootstrap), system(system), class_loader(NULL) {}
76
Ian Rogers0399dde2012-06-06 17:09:28 -070077 bool VisitFrame() {
Elliott Hughes530fa002012-03-12 11:44:49 -070078 DCHECK(class_loader == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -070079 Class* c = GetMethod()->GetDeclaringClass();
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080080 Object* cl = c->GetClassLoader();
81 if (cl != NULL && cl != bootstrap && cl != system) {
82 class_loader = cl;
Elliott Hughes530fa002012-03-12 11:44:49 -070083 return false;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080084 }
Elliott Hughes530fa002012-03-12 11:44:49 -070085 return true;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080086 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -070087
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080088 Object* bootstrap;
89 Object* system;
90 Object* class_loader;
91 };
Ian Rogers00f7d0e2012-07-19 15:28:27 -070092 ScopedObjectAccess soa(env);
93 Object* bootstrap = soa.Decode<Object*>(javaBootstrap);
94 Object* system = soa.Decode<Object*>(javaSystem);
95 ClosestUserClassLoaderVisitor visitor(soa.Self()->GetManagedStack(), soa.Self()->GetTraceStack(),
Ian Rogers0399dde2012-06-06 17:09:28 -070096 bootstrap, system);
97 visitor.WalkStack();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070098 return soa.AddLocalReference<jobject>(visitor.class_loader);
Elliott Hughes8daa0922011-09-11 13:46:25 -070099}
100
Elliott Hughes6a144332012-04-03 13:07:11 -0700101// Returns the class of the caller's caller's caller.
Elliott Hughes0512f022012-03-15 22:10:52 -0700102static jclass VMStack_getStackClass2(JNIEnv* env, jclass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700103 ScopedObjectAccess soa(env);
104 NthCallerVisitor visitor(soa.Self()->GetManagedStack(), soa.Self()->GetTraceStack(), 3);
Ian Rogers0399dde2012-06-06 17:09:28 -0700105 visitor.WalkStack();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700106 return soa.AddLocalReference<jclass>(visitor.caller->GetDeclaringClass());
Elliott Hughes8daa0922011-09-11 13:46:25 -0700107}
108
Elliott Hughes0512f022012-03-15 22:10:52 -0700109static jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700110 jobject trace = GetThreadStack(env, javaThread);
111 if (trace == NULL) {
112 return NULL;
113 }
114 return Thread::InternalStackTraceToStackTraceElementArray(env, trace);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700115}
116
117static JNINativeMethod gMethods[] = {
118 NATIVE_METHOD(VMStack, fillStackTraceElements, "(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"),
119 NATIVE_METHOD(VMStack, getCallingClassLoader, "()Ljava/lang/ClassLoader;"),
Elliott Hughes6bbe8b02011-09-20 13:15:00 -0700120 NATIVE_METHOD(VMStack, getClosestUserClassLoader, "(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)Ljava/lang/ClassLoader;"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700121 NATIVE_METHOD(VMStack, getStackClass2, "()Ljava/lang/Class;"),
122 NATIVE_METHOD(VMStack, getThreadStackTrace, "(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"),
123};
124
Elliott Hughes8daa0922011-09-11 13:46:25 -0700125void register_dalvik_system_VMStack(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700126 REGISTER_NATIVE_METHODS("dalvik/system/VMStack");
Elliott Hughes8daa0922011-09-11 13:46:25 -0700127}
128
129} // namespace art