blob: a83ad454bfd171d2143fc58cba84c350b40ef55a [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();
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080053 DCHECK(callerCaller != NULL);
Elliott Hughes01158d72011-09-19 19:47:10 -070054 const Object* cl = callerCaller->GetDeclaringClass()->GetClassLoader();
55 return AddLocalReference<jobject>(env, cl);
Elliott Hughes8daa0922011-09-11 13:46:25 -070056}
57
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070058jobject VMStack_getClosestUserClassLoader(JNIEnv* env, jclass, jobject javaBootstrap, jobject javaSystem) {
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080059 struct ClosestUserClassLoaderVisitor : public Thread::StackVisitor {
60 ClosestUserClassLoaderVisitor(Object* bootstrap, Object* system)
61 : bootstrap(bootstrap), system(system), class_loader(NULL) {}
Elliott Hughes530fa002012-03-12 11:44:49 -070062 bool VisitFrame(const Frame& f, uintptr_t) {
63 DCHECK(class_loader == NULL);
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080064 Class* c = f.GetMethod()->GetDeclaringClass();
65 Object* cl = c->GetClassLoader();
66 if (cl != NULL && cl != bootstrap && cl != system) {
67 class_loader = cl;
Elliott Hughes530fa002012-03-12 11:44:49 -070068 return false;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080069 }
Elliott Hughes530fa002012-03-12 11:44:49 -070070 return true;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080071 }
72 Object* bootstrap;
73 Object* system;
74 Object* class_loader;
75 };
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070076 Object* bootstrap = Decode<Object*>(env, javaBootstrap);
77 Object* system = Decode<Object*>(env, javaSystem);
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080078 ClosestUserClassLoaderVisitor visitor(bootstrap, system);
79 Thread::Current()->WalkStack(&visitor);
80 return AddLocalReference<jobject>(env, visitor.class_loader);
Elliott Hughes8daa0922011-09-11 13:46:25 -070081}
82
83jclass VMStack_getStackClass2(JNIEnv* env, jclass) {
Elliott Hughes01158d72011-09-19 19:47:10 -070084 // Returns the class of the caller's caller's caller.
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070085 // TODO: need SmartFrame (Thread::WalkStack-like iterator).
Elliott Hughes01158d72011-09-19 19:47:10 -070086 Frame frame = Thread::Current()->GetTopOfStack();
87 frame.Next();
88 frame.Next();
89 frame.Next();
90 Method* callerCallerCaller = frame.GetMethod();
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080091 DCHECK(callerCallerCaller != NULL);
Elliott Hughes01158d72011-09-19 19:47:10 -070092 Class* c = callerCallerCaller->GetDeclaringClass();
93 return AddLocalReference<jclass>(env, c);
Elliott Hughes8daa0922011-09-11 13:46:25 -070094}
95
Elliott Hughes01158d72011-09-19 19:47:10 -070096jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) {
97 jobject trace = GetThreadStack(env, javaThread);
98 if (trace == NULL) {
99 return NULL;
100 }
101 return Thread::InternalStackTraceToStackTraceElementArray(env, trace);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700102}
103
104static JNINativeMethod gMethods[] = {
105 NATIVE_METHOD(VMStack, fillStackTraceElements, "(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"),
106 NATIVE_METHOD(VMStack, getCallingClassLoader, "()Ljava/lang/ClassLoader;"),
Elliott Hughes6bbe8b02011-09-20 13:15:00 -0700107 NATIVE_METHOD(VMStack, getClosestUserClassLoader, "(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)Ljava/lang/ClassLoader;"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700108 NATIVE_METHOD(VMStack, getStackClass2, "()Ljava/lang/Class;"),
109 NATIVE_METHOD(VMStack, getThreadStackTrace, "(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"),
110};
111
112} // namespace
113
114void register_dalvik_system_VMStack(JNIEnv* env) {
115 jniRegisterNativeMethods(env, "dalvik/system/VMStack", gMethods, NELEM(gMethods));
116}
117
118} // namespace art