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