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 | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 20 | #include "scoped_heap_lock.h" |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 21 | #include "scoped_thread_list_lock.h" |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 22 | #include "thread_list.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 23 | |
| 24 | #include "JniConstants.h" // Last to avoid problems with LOG redefinition. |
| 25 | |
| 26 | namespace art { |
| 27 | |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 28 | static jobject GetThreadStack(JNIEnv* env, jobject javaThread) { |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 29 | ScopedHeapLock heap_lock; |
Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 30 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 31 | Thread* thread = Thread::FromManagedThread(env, javaThread); |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 32 | return (thread != NULL) ? GetThreadStack(env, thread) : NULL; |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 35 | static jint VMStack_fillStackTraceElements(JNIEnv* env, jclass, jobject javaThread, jobjectArray javaSteArray) { |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 36 | 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 Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 45 | static jobject VMStack_getCallingClassLoader(JNIEnv* env, jclass) { |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 46 | // Returns the defining class loader of the caller's caller. |
Elliott Hughes | 6bbe8b0 | 2011-09-20 13:15:00 -0700 | [diff] [blame] | 47 | // TODO: need SmartFrame (Thread::WalkStack-like iterator). |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 48 | Frame frame = Thread::Current()->GetTopOfStack(); |
| 49 | frame.Next(); |
| 50 | frame.Next(); |
| 51 | Method* callerCaller = frame.GetMethod(); |
Brian Carlstrom | 5cb71bb | 2012-03-09 14:57:37 -0800 | [diff] [blame] | 52 | DCHECK(callerCaller != NULL); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 53 | const Object* cl = callerCaller->GetDeclaringClass()->GetClassLoader(); |
| 54 | return AddLocalReference<jobject>(env, cl); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 57 | static jobject VMStack_getClosestUserClassLoader(JNIEnv* env, jclass, jobject javaBootstrap, jobject javaSystem) { |
Brian Carlstrom | 5cb71bb | 2012-03-09 14:57:37 -0800 | [diff] [blame] | 58 | struct ClosestUserClassLoaderVisitor : public Thread::StackVisitor { |
| 59 | ClosestUserClassLoaderVisitor(Object* bootstrap, Object* system) |
| 60 | : bootstrap(bootstrap), system(system), class_loader(NULL) {} |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 61 | bool VisitFrame(const Frame& f, uintptr_t) { |
| 62 | DCHECK(class_loader == NULL); |
Brian Carlstrom | 5cb71bb | 2012-03-09 14:57:37 -0800 | [diff] [blame] | 63 | Class* c = f.GetMethod()->GetDeclaringClass(); |
| 64 | Object* cl = c->GetClassLoader(); |
| 65 | if (cl != NULL && cl != bootstrap && cl != system) { |
| 66 | class_loader = cl; |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 67 | return false; |
Brian Carlstrom | 5cb71bb | 2012-03-09 14:57:37 -0800 | [diff] [blame] | 68 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 69 | return true; |
Brian Carlstrom | 5cb71bb | 2012-03-09 14:57:37 -0800 | [diff] [blame] | 70 | } |
| 71 | Object* bootstrap; |
| 72 | Object* system; |
| 73 | Object* class_loader; |
| 74 | }; |
Elliott Hughes | 6bbe8b0 | 2011-09-20 13:15:00 -0700 | [diff] [blame] | 75 | Object* bootstrap = Decode<Object*>(env, javaBootstrap); |
| 76 | Object* system = Decode<Object*>(env, javaSystem); |
Brian Carlstrom | 5cb71bb | 2012-03-09 14:57:37 -0800 | [diff] [blame] | 77 | ClosestUserClassLoaderVisitor visitor(bootstrap, system); |
| 78 | Thread::Current()->WalkStack(&visitor); |
| 79 | return AddLocalReference<jobject>(env, visitor.class_loader); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 82 | static jclass VMStack_getStackClass2(JNIEnv* env, jclass) { |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 83 | // Returns the class of the caller's caller's caller. |
Elliott Hughes | 6bbe8b0 | 2011-09-20 13:15:00 -0700 | [diff] [blame] | 84 | // TODO: need SmartFrame (Thread::WalkStack-like iterator). |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 85 | Frame frame = Thread::Current()->GetTopOfStack(); |
| 86 | frame.Next(); |
| 87 | frame.Next(); |
| 88 | frame.Next(); |
| 89 | Method* callerCallerCaller = frame.GetMethod(); |
Brian Carlstrom | 5cb71bb | 2012-03-09 14:57:37 -0800 | [diff] [blame] | 90 | DCHECK(callerCallerCaller != NULL); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 91 | Class* c = callerCallerCaller->GetDeclaringClass(); |
| 92 | return AddLocalReference<jclass>(env, c); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 95 | static jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) { |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 96 | jobject trace = GetThreadStack(env, javaThread); |
| 97 | if (trace == NULL) { |
| 98 | return NULL; |
| 99 | } |
| 100 | return Thread::InternalStackTraceToStackTraceElementArray(env, trace); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static JNINativeMethod gMethods[] = { |
| 104 | NATIVE_METHOD(VMStack, fillStackTraceElements, "(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"), |
| 105 | NATIVE_METHOD(VMStack, getCallingClassLoader, "()Ljava/lang/ClassLoader;"), |
Elliott Hughes | 6bbe8b0 | 2011-09-20 13:15:00 -0700 | [diff] [blame] | 106 | 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] | 107 | NATIVE_METHOD(VMStack, getStackClass2, "()Ljava/lang/Class;"), |
| 108 | NATIVE_METHOD(VMStack, getThreadStackTrace, "(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"), |
| 109 | }; |
| 110 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 111 | void register_dalvik_system_VMStack(JNIEnv* env) { |
| 112 | jniRegisterNativeMethods(env, "dalvik/system/VMStack", gMethods, NELEM(gMethods)); |
| 113 | } |
| 114 | |
| 115 | } // namespace art |