Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 18 | #include "class_loader.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 19 | #include "object.h" |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 20 | #include "thread_list.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 21 | |
| 22 | #include "JniConstants.h" // Last to avoid problems with LOG redefinition. |
| 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | namespace { |
| 27 | |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 28 | static jobject GetThreadStack(JNIEnv* env, jobject javaThread) { |
Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 29 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 30 | Thread* thread = Thread::FromManagedThread(env, javaThread); |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 31 | return (thread != NULL) ? GetThreadStack(env, thread) : NULL; |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | jint 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 Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | jobject VMStack_getCallingClassLoader(JNIEnv* env, jclass) { |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 45 | // Returns the defining class loader of the caller's caller. |
Elliott Hughes | 6bbe8b0 | 2011-09-20 13:15:00 -0700 | [diff] [blame] | 46 | // TODO: need SmartFrame (Thread::WalkStack-like iterator). |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 47 | 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 Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Elliott Hughes | 6bbe8b0 | 2011-09-20 13:15:00 -0700 | [diff] [blame] | 55 | jobject 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 Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 62 | Object* cl = c->GetClassLoader(); |
Ian Rogers | 3c92a18 | 2012-02-02 15:21:41 -0800 | [diff] [blame] | 63 | if (cl != NULL && cl != bootstrap && cl != system) { |
Elliott Hughes | 6bbe8b0 | 2011-09-20 13:15:00 -0700 | [diff] [blame] | 64 | return AddLocalReference<jobject>(env, cl); |
| 65 | } |
| 66 | } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 67 | return NULL; |
| 68 | } |
| 69 | |
| 70 | jclass VMStack_getStackClass2(JNIEnv* env, jclass) { |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 71 | // Returns the class of the caller's caller's caller. |
Elliott Hughes | 6bbe8b0 | 2011-09-20 13:15:00 -0700 | [diff] [blame] | 72 | // TODO: need SmartFrame (Thread::WalkStack-like iterator). |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 73 | 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 Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 82 | jobjectArray 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 Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static JNINativeMethod gMethods[] = { |
| 91 | NATIVE_METHOD(VMStack, fillStackTraceElements, "(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"), |
| 92 | NATIVE_METHOD(VMStack, getCallingClassLoader, "()Ljava/lang/ClassLoader;"), |
Elliott Hughes | 6bbe8b0 | 2011-09-20 13:15:00 -0700 | [diff] [blame] | 93 | NATIVE_METHOD(VMStack, getClosestUserClassLoader, "(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)Ljava/lang/ClassLoader;"), |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 94 | NATIVE_METHOD(VMStack, getStackClass2, "()Ljava/lang/Class;"), |
| 95 | NATIVE_METHOD(VMStack, getThreadStackTrace, "(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"), |
| 96 | }; |
| 97 | |
| 98 | } // namespace |
| 99 | |
| 100 | void register_dalvik_system_VMStack(JNIEnv* env) { |
| 101 | jniRegisterNativeMethods(env, "dalvik/system/VMStack", gMethods, NELEM(gMethods)); |
| 102 | } |
| 103 | |
| 104 | } // namespace art |