blob: 8560c220e5e4be24a0cc25deacbf50eb09bee0a3 [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
17#include "jni_internal.h"
Elliott Hughes01158d72011-09-19 19:47:10 -070018#include "class_loader.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070019#include "object.h"
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080020#include "scoped_heap_lock.h"
Elliott Hughes88c5c352012-03-15 18:49:48 -070021#include "scoped_thread_list_lock.h"
Elliott Hughes01158d72011-09-19 19:47:10 -070022#include "thread_list.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070023
24#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
25
26namespace art {
27
28namespace {
29
Elliott Hughesbfe487b2011-10-26 15:48:55 -070030static jobject GetThreadStack(JNIEnv* env, jobject javaThread) {
Elliott Hughesffb465f2012-03-01 18:46:05 -080031 ScopedHeapLock heap_lock;
Elliott Hughesbbd9d832011-11-07 14:40:00 -080032 ScopedThreadListLock thread_list_lock;
Elliott Hughes01158d72011-09-19 19:47:10 -070033 Thread* thread = Thread::FromManagedThread(env, javaThread);
Elliott Hughesbfe487b2011-10-26 15:48:55 -070034 return (thread != NULL) ? GetThreadStack(env, thread) : NULL;
Elliott Hughes01158d72011-09-19 19:47:10 -070035}
36
37jint VMStack_fillStackTraceElements(JNIEnv* env, jclass, jobject javaThread, jobjectArray javaSteArray) {
38 jobject trace = GetThreadStack(env, javaThread);
39 if (trace == NULL) {
40 return 0;
41 }
42 int32_t depth;
43 Thread::InternalStackTraceToStackTraceElementArray(env, trace, javaSteArray, &depth);
44 return depth;
Elliott Hughes8daa0922011-09-11 13:46:25 -070045}
46
47jobject VMStack_getCallingClassLoader(JNIEnv* env, jclass) {
Elliott Hughes01158d72011-09-19 19:47:10 -070048 // Returns the defining class loader of the caller's caller.
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070049 // TODO: need SmartFrame (Thread::WalkStack-like iterator).
Elliott Hughes01158d72011-09-19 19:47:10 -070050 Frame frame = Thread::Current()->GetTopOfStack();
51 frame.Next();
52 frame.Next();
53 Method* callerCaller = frame.GetMethod();
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080054 DCHECK(callerCaller != NULL);
Elliott Hughes01158d72011-09-19 19:47:10 -070055 const Object* cl = callerCaller->GetDeclaringClass()->GetClassLoader();
56 return AddLocalReference<jobject>(env, cl);
Elliott Hughes8daa0922011-09-11 13:46:25 -070057}
58
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070059jobject VMStack_getClosestUserClassLoader(JNIEnv* env, jclass, jobject javaBootstrap, jobject javaSystem) {
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080060 struct ClosestUserClassLoaderVisitor : public Thread::StackVisitor {
61 ClosestUserClassLoaderVisitor(Object* bootstrap, Object* system)
62 : bootstrap(bootstrap), system(system), class_loader(NULL) {}
Elliott Hughes530fa002012-03-12 11:44:49 -070063 bool VisitFrame(const Frame& f, uintptr_t) {
64 DCHECK(class_loader == NULL);
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080065 Class* c = f.GetMethod()->GetDeclaringClass();
66 Object* cl = c->GetClassLoader();
67 if (cl != NULL && cl != bootstrap && cl != system) {
68 class_loader = cl;
Elliott Hughes530fa002012-03-12 11:44:49 -070069 return false;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080070 }
Elliott Hughes530fa002012-03-12 11:44:49 -070071 return true;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080072 }
73 Object* bootstrap;
74 Object* system;
75 Object* class_loader;
76 };
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070077 Object* bootstrap = Decode<Object*>(env, javaBootstrap);
78 Object* system = Decode<Object*>(env, javaSystem);
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080079 ClosestUserClassLoaderVisitor visitor(bootstrap, system);
80 Thread::Current()->WalkStack(&visitor);
81 return AddLocalReference<jobject>(env, visitor.class_loader);
Elliott Hughes8daa0922011-09-11 13:46:25 -070082}
83
84jclass VMStack_getStackClass2(JNIEnv* env, jclass) {
Elliott Hughes01158d72011-09-19 19:47:10 -070085 // Returns the class of the caller's caller's caller.
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070086 // TODO: need SmartFrame (Thread::WalkStack-like iterator).
Elliott Hughes01158d72011-09-19 19:47:10 -070087 Frame frame = Thread::Current()->GetTopOfStack();
88 frame.Next();
89 frame.Next();
90 frame.Next();
91 Method* callerCallerCaller = frame.GetMethod();
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080092 DCHECK(callerCallerCaller != NULL);
Elliott Hughes01158d72011-09-19 19:47:10 -070093 Class* c = callerCallerCaller->GetDeclaringClass();
94 return AddLocalReference<jclass>(env, c);
Elliott Hughes8daa0922011-09-11 13:46:25 -070095}
96
Elliott Hughes01158d72011-09-19 19:47:10 -070097jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) {
98 jobject trace = GetThreadStack(env, javaThread);
99 if (trace == NULL) {
100 return NULL;
101 }
102 return Thread::InternalStackTraceToStackTraceElementArray(env, trace);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700103}
104
105static JNINativeMethod gMethods[] = {
106 NATIVE_METHOD(VMStack, fillStackTraceElements, "(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"),
107 NATIVE_METHOD(VMStack, getCallingClassLoader, "()Ljava/lang/ClassLoader;"),
Elliott Hughes6bbe8b02011-09-20 13:15:00 -0700108 NATIVE_METHOD(VMStack, getClosestUserClassLoader, "(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)Ljava/lang/ClassLoader;"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700109 NATIVE_METHOD(VMStack, getStackClass2, "()Ljava/lang/Class;"),
110 NATIVE_METHOD(VMStack, getThreadStackTrace, "(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"),
111};
112
113} // namespace
114
115void register_dalvik_system_VMStack(JNIEnv* env) {
116 jniRegisterNativeMethods(env, "dalvik/system/VMStack", gMethods, NELEM(gMethods));
117}
118
119} // namespace art