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 | |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 17 | #include "class_loader.h" |
Elliott Hughes | 7756d54 | 2012-05-24 21:56:51 -0700 | [diff] [blame^] | 18 | #include "jni_internal.h" |
Elliott Hughes | 6a14433 | 2012-04-03 13:07:11 -0700 | [diff] [blame] | 19 | #include "nth_caller_visitor.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 20 | #include "object.h" |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 21 | #include "scoped_heap_lock.h" |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 22 | #include "scoped_thread_list_lock.h" |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 23 | #include "thread_list.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 24 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 25 | namespace art { |
| 26 | |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 27 | static jobject GetThreadStack(JNIEnv* env, jobject javaThread) { |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 28 | ScopedHeapLock heap_lock; |
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 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 34 | static jint VMStack_fillStackTraceElements(JNIEnv* env, jclass, jobject javaThread, jobjectArray javaSteArray) { |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | 6a14433 | 2012-04-03 13:07:11 -0700 | [diff] [blame] | 44 | // Returns the defining class loader of the caller's caller. |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 45 | static jobject VMStack_getCallingClassLoader(JNIEnv* env, jclass) { |
Elliott Hughes | 6a14433 | 2012-04-03 13:07:11 -0700 | [diff] [blame] | 46 | NthCallerVisitor visitor(2); |
| 47 | Thread::Current()->WalkStack(&visitor); |
TDYa127 | 3f9137d | 2012-04-08 15:59:19 -0700 | [diff] [blame] | 48 | return AddLocalReference<jobject>(env, visitor.caller->GetDeclaringClass()->GetClassLoader()); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 51 | static jobject VMStack_getClosestUserClassLoader(JNIEnv* env, jclass, jobject javaBootstrap, jobject javaSystem) { |
Brian Carlstrom | 5cb71bb | 2012-03-09 14:57:37 -0800 | [diff] [blame] | 52 | struct ClosestUserClassLoaderVisitor : public Thread::StackVisitor { |
| 53 | ClosestUserClassLoaderVisitor(Object* bootstrap, Object* system) |
| 54 | : bootstrap(bootstrap), system(system), class_loader(NULL) {} |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 55 | bool VisitFrame(const Frame& f, uintptr_t) { |
| 56 | DCHECK(class_loader == NULL); |
Brian Carlstrom | 5cb71bb | 2012-03-09 14:57:37 -0800 | [diff] [blame] | 57 | Class* c = f.GetMethod()->GetDeclaringClass(); |
| 58 | Object* cl = c->GetClassLoader(); |
| 59 | if (cl != NULL && cl != bootstrap && cl != system) { |
| 60 | class_loader = cl; |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 61 | return false; |
Brian Carlstrom | 5cb71bb | 2012-03-09 14:57:37 -0800 | [diff] [blame] | 62 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 63 | return true; |
Brian Carlstrom | 5cb71bb | 2012-03-09 14:57:37 -0800 | [diff] [blame] | 64 | } |
| 65 | Object* bootstrap; |
| 66 | Object* system; |
| 67 | Object* class_loader; |
| 68 | }; |
Elliott Hughes | 6bbe8b0 | 2011-09-20 13:15:00 -0700 | [diff] [blame] | 69 | Object* bootstrap = Decode<Object*>(env, javaBootstrap); |
| 70 | Object* system = Decode<Object*>(env, javaSystem); |
Brian Carlstrom | 5cb71bb | 2012-03-09 14:57:37 -0800 | [diff] [blame] | 71 | ClosestUserClassLoaderVisitor visitor(bootstrap, system); |
| 72 | Thread::Current()->WalkStack(&visitor); |
| 73 | return AddLocalReference<jobject>(env, visitor.class_loader); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Elliott Hughes | 6a14433 | 2012-04-03 13:07:11 -0700 | [diff] [blame] | 76 | // Returns the class of the caller's caller's caller. |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 77 | static jclass VMStack_getStackClass2(JNIEnv* env, jclass) { |
Elliott Hughes | 6a14433 | 2012-04-03 13:07:11 -0700 | [diff] [blame] | 78 | NthCallerVisitor visitor(3); |
| 79 | Thread::Current()->WalkStack(&visitor); |
TDYa127 | 3f9137d | 2012-04-08 15:59:19 -0700 | [diff] [blame] | 80 | return AddLocalReference<jclass>(env, visitor.caller->GetDeclaringClass()); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 83 | static jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) { |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 84 | jobject trace = GetThreadStack(env, javaThread); |
| 85 | if (trace == NULL) { |
| 86 | return NULL; |
| 87 | } |
| 88 | return Thread::InternalStackTraceToStackTraceElementArray(env, trace); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static JNINativeMethod gMethods[] = { |
| 92 | NATIVE_METHOD(VMStack, fillStackTraceElements, "(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"), |
| 93 | NATIVE_METHOD(VMStack, getCallingClassLoader, "()Ljava/lang/ClassLoader;"), |
Elliott Hughes | 6bbe8b0 | 2011-09-20 13:15:00 -0700 | [diff] [blame] | 94 | 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] | 95 | NATIVE_METHOD(VMStack, getStackClass2, "()Ljava/lang/Class;"), |
| 96 | NATIVE_METHOD(VMStack, getThreadStackTrace, "(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"), |
| 97 | }; |
| 98 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 99 | void register_dalvik_system_VMStack(JNIEnv* env) { |
Elliott Hughes | 7756d54 | 2012-05-24 21:56:51 -0700 | [diff] [blame^] | 100 | REGISTER_NATIVE_METHODS("dalvik/system/VMStack"); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | } // namespace art |