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 | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 28 | class StackGetter { |
| 29 | public: |
| 30 | StackGetter(JNIEnv* env, Thread* thread) : env_(env), thread_(thread), trace_(NULL) { |
| 31 | } |
| 32 | |
| 33 | static void Callback(void* arg) { |
| 34 | reinterpret_cast<StackGetter*>(arg)->Callback(); |
| 35 | } |
| 36 | |
| 37 | jobject GetTrace() { |
| 38 | return trace_; |
| 39 | } |
| 40 | |
| 41 | private: |
| 42 | void Callback() { |
| 43 | trace_ = thread_->CreateInternalStackTrace(env_); |
| 44 | } |
| 45 | |
| 46 | JNIEnv* env_; |
| 47 | Thread* thread_; |
| 48 | jobject trace_; |
| 49 | }; |
| 50 | |
| 51 | jobject GetThreadStack(JNIEnv* env, jobject javaThread) { |
| 52 | Thread* thread = Thread::FromManagedThread(env, javaThread); |
| 53 | if (thread == NULL) { |
| 54 | return NULL; |
| 55 | } |
| 56 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 57 | StackGetter stack_getter(env, thread); |
| 58 | thread_list->RunWhileSuspended(thread, StackGetter::Callback, &stack_getter); |
| 59 | return stack_getter.GetTrace(); |
| 60 | } |
| 61 | |
| 62 | jint VMStack_fillStackTraceElements(JNIEnv* env, jclass, jobject javaThread, jobjectArray javaSteArray) { |
| 63 | jobject trace = GetThreadStack(env, javaThread); |
| 64 | if (trace == NULL) { |
| 65 | return 0; |
| 66 | } |
| 67 | int32_t depth; |
| 68 | Thread::InternalStackTraceToStackTraceElementArray(env, trace, javaSteArray, &depth); |
| 69 | return depth; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | jobject VMStack_getCallingClassLoader(JNIEnv* env, jclass) { |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 73 | // Returns the defining class loader of the caller's caller. |
Elliott Hughes | 6bbe8b0 | 2011-09-20 13:15:00 -0700 | [diff] [blame] | 74 | // TODO: need SmartFrame (Thread::WalkStack-like iterator). |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 75 | Frame frame = Thread::Current()->GetTopOfStack(); |
| 76 | frame.Next(); |
| 77 | frame.Next(); |
| 78 | Method* callerCaller = frame.GetMethod(); |
| 79 | const Object* cl = callerCaller->GetDeclaringClass()->GetClassLoader(); |
| 80 | return AddLocalReference<jobject>(env, cl); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Elliott Hughes | 6bbe8b0 | 2011-09-20 13:15:00 -0700 | [diff] [blame] | 83 | jobject VMStack_getClosestUserClassLoader(JNIEnv* env, jclass, jobject javaBootstrap, jobject javaSystem) { |
| 84 | Thread* self = Thread::Current(); |
| 85 | Object* bootstrap = Decode<Object*>(env, javaBootstrap); |
| 86 | Object* system = Decode<Object*>(env, javaSystem); |
| 87 | // TODO: need SmartFrame (Thread::WalkStack-like iterator). |
| 88 | for (Frame frame = self->GetTopOfStack(); frame.HasNext(); frame.Next()) { |
| 89 | Class* c = frame.GetMethod()->GetDeclaringClass(); |
| 90 | Object* cl = const_cast<ClassLoader*>(c->GetClassLoader()); |
| 91 | if (cl != bootstrap && cl != system) { |
| 92 | return AddLocalReference<jobject>(env, cl); |
| 93 | } |
| 94 | } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 95 | return NULL; |
| 96 | } |
| 97 | |
| 98 | jclass VMStack_getStackClass2(JNIEnv* env, jclass) { |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 99 | // Returns the class of the caller's caller's caller. |
Elliott Hughes | 6bbe8b0 | 2011-09-20 13:15:00 -0700 | [diff] [blame] | 100 | // TODO: need SmartFrame (Thread::WalkStack-like iterator). |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 101 | Frame frame = Thread::Current()->GetTopOfStack(); |
| 102 | frame.Next(); |
| 103 | frame.Next(); |
| 104 | frame.Next(); |
| 105 | Method* callerCallerCaller = frame.GetMethod(); |
| 106 | Class* c = callerCallerCaller->GetDeclaringClass(); |
| 107 | return AddLocalReference<jclass>(env, c); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 110 | jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) { |
| 111 | jobject trace = GetThreadStack(env, javaThread); |
| 112 | if (trace == NULL) { |
| 113 | return NULL; |
| 114 | } |
| 115 | return Thread::InternalStackTraceToStackTraceElementArray(env, trace); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static JNINativeMethod gMethods[] = { |
| 119 | NATIVE_METHOD(VMStack, fillStackTraceElements, "(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"), |
| 120 | NATIVE_METHOD(VMStack, getCallingClassLoader, "()Ljava/lang/ClassLoader;"), |
Elliott Hughes | 6bbe8b0 | 2011-09-20 13:15:00 -0700 | [diff] [blame] | 121 | 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] | 122 | NATIVE_METHOD(VMStack, getStackClass2, "()Ljava/lang/Class;"), |
| 123 | NATIVE_METHOD(VMStack, getThreadStackTrace, "(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"), |
| 124 | }; |
| 125 | |
| 126 | } // namespace |
| 127 | |
| 128 | void register_dalvik_system_VMStack(JNIEnv* env) { |
| 129 | jniRegisterNativeMethods(env, "dalvik/system/VMStack", gMethods, NELEM(gMethods)); |
| 130 | } |
| 131 | |
| 132 | } // namespace art |