blob: 682aed996324b38230be4a392a5820d95f2f560a [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 Hughes01158d72011-09-19 19:47:10 -070020#include "thread_list.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070021
22#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
23
24namespace art {
25
26namespace {
27
Elliott Hughesbfe487b2011-10-26 15:48:55 -070028static jobject GetThreadStack(JNIEnv* env, jobject javaThread) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080029 ScopedThreadListLock thread_list_lock;
Elliott Hughes01158d72011-09-19 19:47:10 -070030 Thread* thread = Thread::FromManagedThread(env, javaThread);
Elliott Hughesbfe487b2011-10-26 15:48:55 -070031 return (thread != NULL) ? GetThreadStack(env, thread) : NULL;
Elliott Hughes01158d72011-09-19 19:47:10 -070032}
33
34jint VMStack_fillStackTraceElements(JNIEnv* env, jclass, jobject javaThread, jobjectArray javaSteArray) {
35 jobject trace = GetThreadStack(env, javaThread);
36 if (trace == NULL) {
37 return 0;
38 }
39 int32_t depth;
40 Thread::InternalStackTraceToStackTraceElementArray(env, trace, javaSteArray, &depth);
41 return depth;
Elliott Hughes8daa0922011-09-11 13:46:25 -070042}
43
44jobject VMStack_getCallingClassLoader(JNIEnv* env, jclass) {
Elliott Hughes01158d72011-09-19 19:47:10 -070045 // Returns the defining class loader of the caller's caller.
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070046 // TODO: need SmartFrame (Thread::WalkStack-like iterator).
Elliott Hughes01158d72011-09-19 19:47:10 -070047 Frame frame = Thread::Current()->GetTopOfStack();
48 frame.Next();
49 frame.Next();
50 Method* callerCaller = frame.GetMethod();
51 const Object* cl = callerCaller->GetDeclaringClass()->GetClassLoader();
52 return AddLocalReference<jobject>(env, cl);
Elliott Hughes8daa0922011-09-11 13:46:25 -070053}
54
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070055jobject VMStack_getClosestUserClassLoader(JNIEnv* env, jclass, jobject javaBootstrap, jobject javaSystem) {
56 Thread* self = Thread::Current();
57 Object* bootstrap = Decode<Object*>(env, javaBootstrap);
58 Object* system = Decode<Object*>(env, javaSystem);
59 // TODO: need SmartFrame (Thread::WalkStack-like iterator).
60 for (Frame frame = self->GetTopOfStack(); frame.HasNext(); frame.Next()) {
61 Class* c = frame.GetMethod()->GetDeclaringClass();
Elliott Hughes1bba14f2011-12-01 18:00:36 -080062 Object* cl = c->GetClassLoader();
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070063 if (cl != bootstrap && cl != system) {
64 return AddLocalReference<jobject>(env, cl);
65 }
66 }
Elliott Hughes8daa0922011-09-11 13:46:25 -070067 return NULL;
68}
69
70jclass VMStack_getStackClass2(JNIEnv* env, jclass) {
Elliott Hughes01158d72011-09-19 19:47:10 -070071 // Returns the class of the caller's caller's caller.
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070072 // TODO: need SmartFrame (Thread::WalkStack-like iterator).
Elliott Hughes01158d72011-09-19 19:47:10 -070073 Frame frame = Thread::Current()->GetTopOfStack();
74 frame.Next();
75 frame.Next();
76 frame.Next();
77 Method* callerCallerCaller = frame.GetMethod();
78 Class* c = callerCallerCaller->GetDeclaringClass();
79 return AddLocalReference<jclass>(env, c);
Elliott Hughes8daa0922011-09-11 13:46:25 -070080}
81
Elliott Hughes01158d72011-09-19 19:47:10 -070082jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) {
83 jobject trace = GetThreadStack(env, javaThread);
84 if (trace == NULL) {
85 return NULL;
86 }
87 return Thread::InternalStackTraceToStackTraceElementArray(env, trace);
Elliott Hughes8daa0922011-09-11 13:46:25 -070088}
89
90static JNINativeMethod gMethods[] = {
91 NATIVE_METHOD(VMStack, fillStackTraceElements, "(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"),
92 NATIVE_METHOD(VMStack, getCallingClassLoader, "()Ljava/lang/ClassLoader;"),
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070093 NATIVE_METHOD(VMStack, getClosestUserClassLoader, "(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)Ljava/lang/ClassLoader;"),
Elliott Hughes8daa0922011-09-11 13:46:25 -070094 NATIVE_METHOD(VMStack, getStackClass2, "()Ljava/lang/Class;"),
95 NATIVE_METHOD(VMStack, getThreadStackTrace, "(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"),
96};
97
98} // namespace
99
100void register_dalvik_system_VMStack(JNIEnv* env) {
101 jniRegisterNativeMethods(env, "dalvik/system/VMStack", gMethods, NELEM(gMethods));
102}
103
104} // namespace art