blob: e3ecbd985eef4df8ec33458b5239da5e8a4a1098 [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"
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080021#include "scoped_heap_lock.h"
Ian Rogers0399dde2012-06-06 17:09:28 -070022#include "scoped_jni_thread_state.h"
Elliott Hughes88c5c352012-03-15 18:49:48 -070023#include "scoped_thread_list_lock.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
Elliott Hughesbfe487b2011-10-26 15:48:55 -070028static jobject GetThreadStack(JNIEnv* env, jobject javaThread) {
Elliott Hughesffb465f2012-03-01 18:46:05 -080029 ScopedHeapLock heap_lock;
Elliott Hughesbbd9d832011-11-07 14:40:00 -080030 ScopedThreadListLock thread_list_lock;
Elliott Hughes01158d72011-09-19 19:47:10 -070031 Thread* thread = Thread::FromManagedThread(env, javaThread);
Elliott Hughesbfe487b2011-10-26 15:48:55 -070032 return (thread != NULL) ? GetThreadStack(env, thread) : NULL;
Elliott Hughes01158d72011-09-19 19:47:10 -070033}
34
Elliott Hughes0512f022012-03-15 22:10:52 -070035static jint VMStack_fillStackTraceElements(JNIEnv* env, jclass, jobject javaThread, jobjectArray javaSteArray) {
Elliott Hughes01158d72011-09-19 19:47:10 -070036 jobject trace = GetThreadStack(env, javaThread);
37 if (trace == NULL) {
38 return 0;
39 }
40 int32_t depth;
41 Thread::InternalStackTraceToStackTraceElementArray(env, trace, javaSteArray, &depth);
42 return depth;
Elliott Hughes8daa0922011-09-11 13:46:25 -070043}
44
Elliott Hughes6a144332012-04-03 13:07:11 -070045// Returns the defining class loader of the caller's caller.
Elliott Hughes0512f022012-03-15 22:10:52 -070046static jobject VMStack_getCallingClassLoader(JNIEnv* env, jclass) {
Ian Rogers0399dde2012-06-06 17:09:28 -070047 ScopedJniThreadState ts(env, kNative); // Not a state change out of native.
48 NthCallerVisitor visitor(ts.Self()->GetManagedStack(), ts.Self()->GetTraceStack(), 2);
49 visitor.WalkStack();
TDYa1273f9137d2012-04-08 15:59:19 -070050 return AddLocalReference<jobject>(env, visitor.caller->GetDeclaringClass()->GetClassLoader());
Elliott Hughes8daa0922011-09-11 13:46:25 -070051}
52
Elliott Hughes0512f022012-03-15 22:10:52 -070053static jobject VMStack_getClosestUserClassLoader(JNIEnv* env, jclass, jobject javaBootstrap, jobject javaSystem) {
Ian Rogers0399dde2012-06-06 17:09:28 -070054 struct ClosestUserClassLoaderVisitor : public StackVisitor {
55 ClosestUserClassLoaderVisitor(const ManagedStack* stack,
56 const std::vector<TraceStackFrame>* trace_stack,
57 Object* bootstrap, Object* system)
58 : StackVisitor(stack, trace_stack), bootstrap(bootstrap), system(system),
59 class_loader(NULL) {}
60 bool VisitFrame() {
Elliott Hughes530fa002012-03-12 11:44:49 -070061 DCHECK(class_loader == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -070062 Class* c = GetMethod()->GetDeclaringClass();
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080063 Object* cl = c->GetClassLoader();
64 if (cl != NULL && cl != bootstrap && cl != system) {
65 class_loader = cl;
Elliott Hughes530fa002012-03-12 11:44:49 -070066 return false;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080067 }
Elliott Hughes530fa002012-03-12 11:44:49 -070068 return true;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080069 }
70 Object* bootstrap;
71 Object* system;
72 Object* class_loader;
73 };
Ian Rogers0399dde2012-06-06 17:09:28 -070074 ScopedJniThreadState ts(env);
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070075 Object* bootstrap = Decode<Object*>(env, javaBootstrap);
76 Object* system = Decode<Object*>(env, javaSystem);
Ian Rogers0399dde2012-06-06 17:09:28 -070077 ClosestUserClassLoaderVisitor visitor(ts.Self()->GetManagedStack(), ts.Self()->GetTraceStack(),
78 bootstrap, system);
79 visitor.WalkStack();
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080080 return AddLocalReference<jobject>(env, visitor.class_loader);
Elliott Hughes8daa0922011-09-11 13:46:25 -070081}
82
Elliott Hughes6a144332012-04-03 13:07:11 -070083// Returns the class of the caller's caller's caller.
Elliott Hughes0512f022012-03-15 22:10:52 -070084static jclass VMStack_getStackClass2(JNIEnv* env, jclass) {
Ian Rogers0399dde2012-06-06 17:09:28 -070085 ScopedJniThreadState ts(env, kNative); // Not a state change out of native.
86 NthCallerVisitor visitor(ts.Self()->GetManagedStack(), ts.Self()->GetTraceStack(), 3);
87 visitor.WalkStack();
TDYa1273f9137d2012-04-08 15:59:19 -070088 return AddLocalReference<jclass>(env, visitor.caller->GetDeclaringClass());
Elliott Hughes8daa0922011-09-11 13:46:25 -070089}
90
Elliott Hughes0512f022012-03-15 22:10:52 -070091static jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) {
Elliott Hughes01158d72011-09-19 19:47:10 -070092 jobject trace = GetThreadStack(env, javaThread);
93 if (trace == NULL) {
94 return NULL;
95 }
96 return Thread::InternalStackTraceToStackTraceElementArray(env, trace);
Elliott Hughes8daa0922011-09-11 13:46:25 -070097}
98
99static JNINativeMethod gMethods[] = {
100 NATIVE_METHOD(VMStack, fillStackTraceElements, "(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"),
101 NATIVE_METHOD(VMStack, getCallingClassLoader, "()Ljava/lang/ClassLoader;"),
Elliott Hughes6bbe8b02011-09-20 13:15:00 -0700102 NATIVE_METHOD(VMStack, getClosestUserClassLoader, "(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)Ljava/lang/ClassLoader;"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700103 NATIVE_METHOD(VMStack, getStackClass2, "()Ljava/lang/Class;"),
104 NATIVE_METHOD(VMStack, getThreadStackTrace, "(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"),
105};
106
Elliott Hughes8daa0922011-09-11 13:46:25 -0700107void register_dalvik_system_VMStack(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700108 REGISTER_NATIVE_METHODS("dalvik/system/VMStack");
Elliott Hughes8daa0922011-09-11 13:46:25 -0700109}
110
111} // namespace art