blob: 74258473beea01f4c2b8e801b8f3cb480c006b71 [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 Hughes01158d72011-09-19 19:47:10 -070020#include "thread_list.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070021
22#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
23
24namespace art {
25
26namespace {
27
Elliott Hughesbfe487b2011-10-26 15:48:55 -070028static jobject GetThreadStack(JNIEnv* env, jobject javaThread) {
Elliott Hughesffb465f2012-03-01 18:46:05 -080029 ScopedHeapLock heap_lock;
Elliott Hughesbbd9d832011-11-07 14:40:00 -080030 ScopedThreadListLock thread_list_lock;
Elliott Hughes01158d72011-09-19 19:47:10 -070031 Thread* thread = Thread::FromManagedThread(env, javaThread);
Elliott Hughesbfe487b2011-10-26 15:48:55 -070032 return (thread != NULL) ? GetThreadStack(env, thread) : NULL;
Elliott Hughes01158d72011-09-19 19:47:10 -070033}
34
35jint VMStack_fillStackTraceElements(JNIEnv* env, jclass, jobject javaThread, jobjectArray javaSteArray) {
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 Hughes8daa0922011-09-11 13:46:25 -070043}
44
45jobject VMStack_getCallingClassLoader(JNIEnv* env, jclass) {
Elliott Hughes01158d72011-09-19 19:47:10 -070046 // Returns the defining class loader of the caller's caller.
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070047 // TODO: need SmartFrame (Thread::WalkStack-like iterator).
Elliott Hughes01158d72011-09-19 19:47:10 -070048 Frame frame = Thread::Current()->GetTopOfStack();
49 frame.Next();
50 frame.Next();
51 Method* callerCaller = frame.GetMethod();
52 const Object* cl = callerCaller->GetDeclaringClass()->GetClassLoader();
53 return AddLocalReference<jobject>(env, cl);
Elliott Hughes8daa0922011-09-11 13:46:25 -070054}
55
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070056jobject VMStack_getClosestUserClassLoader(JNIEnv* env, jclass, jobject javaBootstrap, jobject javaSystem) {
57 Thread* self = Thread::Current();
58 Object* bootstrap = Decode<Object*>(env, javaBootstrap);
59 Object* system = Decode<Object*>(env, javaSystem);
60 // TODO: need SmartFrame (Thread::WalkStack-like iterator).
61 for (Frame frame = self->GetTopOfStack(); frame.HasNext(); frame.Next()) {
62 Class* c = frame.GetMethod()->GetDeclaringClass();
Elliott Hughes1bba14f2011-12-01 18:00:36 -080063 Object* cl = c->GetClassLoader();
Ian Rogers3c92a182012-02-02 15:21:41 -080064 if (cl != NULL && cl != bootstrap && cl != system) {
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070065 return AddLocalReference<jobject>(env, cl);
66 }
67 }
Elliott Hughes8daa0922011-09-11 13:46:25 -070068 return NULL;
69}
70
71jclass VMStack_getStackClass2(JNIEnv* env, jclass) {
Elliott Hughes01158d72011-09-19 19:47:10 -070072 // Returns the class of the caller's caller's caller.
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070073 // TODO: need SmartFrame (Thread::WalkStack-like iterator).
Elliott Hughes01158d72011-09-19 19:47:10 -070074 Frame frame = Thread::Current()->GetTopOfStack();
75 frame.Next();
76 frame.Next();
77 frame.Next();
78 Method* callerCallerCaller = frame.GetMethod();
79 Class* c = callerCallerCaller->GetDeclaringClass();
80 return AddLocalReference<jclass>(env, c);
Elliott Hughes8daa0922011-09-11 13:46:25 -070081}
82
Elliott Hughes01158d72011-09-19 19:47:10 -070083jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) {
84 jobject trace = GetThreadStack(env, javaThread);
85 if (trace == NULL) {
86 return NULL;
87 }
88 return Thread::InternalStackTraceToStackTraceElementArray(env, trace);
Elliott Hughes8daa0922011-09-11 13:46:25 -070089}
90
91static JNINativeMethod gMethods[] = {
92 NATIVE_METHOD(VMStack, fillStackTraceElements, "(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"),
93 NATIVE_METHOD(VMStack, getCallingClassLoader, "()Ljava/lang/ClassLoader;"),
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070094 NATIVE_METHOD(VMStack, getClosestUserClassLoader, "(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)Ljava/lang/ClassLoader;"),
Elliott Hughes8daa0922011-09-11 13:46:25 -070095 NATIVE_METHOD(VMStack, getStackClass2, "()Ljava/lang/Class;"),
96 NATIVE_METHOD(VMStack, getThreadStackTrace, "(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"),
97};
98
99} // namespace
100
101void register_dalvik_system_VMStack(JNIEnv* env) {
102 jniRegisterNativeMethods(env, "dalvik/system/VMStack", gMethods, NELEM(gMethods));
103}
104
105} // namespace art