blob: 37ee8b2ed67fb7043b67a05c338e48ad10565340 [file] [log] [blame]
Elliott Hughes8daa0922011-09-11 13:46:25 -07001/*
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 Hughes01158d72011-09-19 19:47:10 -070018#include "class_loader.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070019#include "object.h"
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080020#include "scoped_heap_lock.h"
Elliott Hughes01158d72011-09-19 19:47:10 -070021#include "thread_list.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070022
23#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
24
25namespace art {
26
27namespace {
28
Elliott Hughesbfe487b2011-10-26 15:48:55 -070029static jobject GetThreadStack(JNIEnv* env, jobject javaThread) {
Elliott Hughesffb465f2012-03-01 18:46:05 -080030 ScopedHeapLock heap_lock;
Elliott Hughesbbd9d832011-11-07 14:40:00 -080031 ScopedThreadListLock thread_list_lock;
Elliott Hughes01158d72011-09-19 19:47:10 -070032 Thread* thread = Thread::FromManagedThread(env, javaThread);
Elliott Hughesbfe487b2011-10-26 15:48:55 -070033 return (thread != NULL) ? GetThreadStack(env, thread) : NULL;
Elliott Hughes01158d72011-09-19 19:47:10 -070034}
35
36jint 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 Hughes8daa0922011-09-11 13:46:25 -070044}
45
46jobject VMStack_getCallingClassLoader(JNIEnv* env, jclass) {
Elliott Hughes01158d72011-09-19 19:47:10 -070047 // Returns the defining class loader of the caller's caller.
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070048 // TODO: need SmartFrame (Thread::WalkStack-like iterator).
Elliott Hughes01158d72011-09-19 19:47:10 -070049 Frame frame = Thread::Current()->GetTopOfStack();
50 frame.Next();
51 frame.Next();
52 Method* callerCaller = frame.GetMethod();
53 const Object* cl = callerCaller->GetDeclaringClass()->GetClassLoader();
54 return AddLocalReference<jobject>(env, cl);
Elliott Hughes8daa0922011-09-11 13:46:25 -070055}
56
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070057jobject VMStack_getClosestUserClassLoader(JNIEnv* env, jclass, jobject javaBootstrap, jobject javaSystem) {
58 Thread* self = Thread::Current();
59 Object* bootstrap = Decode<Object*>(env, javaBootstrap);
60 Object* system = Decode<Object*>(env, javaSystem);
61 // TODO: need SmartFrame (Thread::WalkStack-like iterator).
62 for (Frame frame = self->GetTopOfStack(); frame.HasNext(); frame.Next()) {
63 Class* c = frame.GetMethod()->GetDeclaringClass();
Elliott Hughes1bba14f2011-12-01 18:00:36 -080064 Object* cl = c->GetClassLoader();
Ian Rogers3c92a182012-02-02 15:21:41 -080065 if (cl != NULL && cl != bootstrap && cl != system) {
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070066 return AddLocalReference<jobject>(env, cl);
67 }
68 }
Elliott Hughes8daa0922011-09-11 13:46:25 -070069 return NULL;
70}
71
72jclass VMStack_getStackClass2(JNIEnv* env, jclass) {
Elliott Hughes01158d72011-09-19 19:47:10 -070073 // Returns the class of the caller's caller's caller.
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070074 // TODO: need SmartFrame (Thread::WalkStack-like iterator).
Elliott Hughes01158d72011-09-19 19:47:10 -070075 Frame frame = Thread::Current()->GetTopOfStack();
76 frame.Next();
77 frame.Next();
78 frame.Next();
79 Method* callerCallerCaller = frame.GetMethod();
80 Class* c = callerCallerCaller->GetDeclaringClass();
81 return AddLocalReference<jclass>(env, c);
Elliott Hughes8daa0922011-09-11 13:46:25 -070082}
83
Elliott Hughes01158d72011-09-19 19:47:10 -070084jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) {
85 jobject trace = GetThreadStack(env, javaThread);
86 if (trace == NULL) {
87 return NULL;
88 }
89 return Thread::InternalStackTraceToStackTraceElementArray(env, trace);
Elliott Hughes8daa0922011-09-11 13:46:25 -070090}
91
92static JNINativeMethod gMethods[] = {
93 NATIVE_METHOD(VMStack, fillStackTraceElements, "(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"),
94 NATIVE_METHOD(VMStack, getCallingClassLoader, "()Ljava/lang/ClassLoader;"),
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070095 NATIVE_METHOD(VMStack, getClosestUserClassLoader, "(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)Ljava/lang/ClassLoader;"),
Elliott Hughes8daa0922011-09-11 13:46:25 -070096 NATIVE_METHOD(VMStack, getStackClass2, "()Ljava/lang/Class;"),
97 NATIVE_METHOD(VMStack, getThreadStackTrace, "(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"),
98};
99
100} // namespace
101
102void register_dalvik_system_VMStack(JNIEnv* env) {
103 jniRegisterNativeMethods(env, "dalvik/system/VMStack", gMethods, NELEM(gMethods));
104}
105
106} // namespace art