blob: bcfe29b58385ed8884027e978e5706f9092a4c9d [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) {}
62 virtual void VisitFrame(const Frame& f, uintptr_t) {
63 if (class_loader != NULL) {
64 // If we already found a result, nothing to do.
65 // TODO: need SmartFrame (Thread::WalkStack-like iterator).
66 // (or change VisitFrame to let us return bool to stop visiting)
67 return;
68 }
69 Class* c = f.GetMethod()->GetDeclaringClass();
70 Object* cl = c->GetClassLoader();
71 if (cl != NULL && cl != bootstrap && cl != system) {
72 class_loader = cl;
73 }
74 }
75 Object* bootstrap;
76 Object* system;
77 Object* class_loader;
78 };
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070079 Object* bootstrap = Decode<Object*>(env, javaBootstrap);
80 Object* system = Decode<Object*>(env, javaSystem);
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080081 ClosestUserClassLoaderVisitor visitor(bootstrap, system);
82 Thread::Current()->WalkStack(&visitor);
83 return AddLocalReference<jobject>(env, visitor.class_loader);
Elliott Hughes8daa0922011-09-11 13:46:25 -070084}
85
86jclass VMStack_getStackClass2(JNIEnv* env, jclass) {
Elliott Hughes01158d72011-09-19 19:47:10 -070087 // Returns the class of the caller's caller's caller.
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070088 // TODO: need SmartFrame (Thread::WalkStack-like iterator).
Elliott Hughes01158d72011-09-19 19:47:10 -070089 Frame frame = Thread::Current()->GetTopOfStack();
90 frame.Next();
91 frame.Next();
92 frame.Next();
93 Method* callerCallerCaller = frame.GetMethod();
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080094 DCHECK(callerCallerCaller != NULL);
Elliott Hughes01158d72011-09-19 19:47:10 -070095 Class* c = callerCallerCaller->GetDeclaringClass();
96 return AddLocalReference<jclass>(env, c);
Elliott Hughes8daa0922011-09-11 13:46:25 -070097}
98
Elliott Hughes01158d72011-09-19 19:47:10 -070099jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) {
100 jobject trace = GetThreadStack(env, javaThread);
101 if (trace == NULL) {
102 return NULL;
103 }
104 return Thread::InternalStackTraceToStackTraceElementArray(env, trace);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700105}
106
107static JNINativeMethod gMethods[] = {
108 NATIVE_METHOD(VMStack, fillStackTraceElements, "(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"),
109 NATIVE_METHOD(VMStack, getCallingClassLoader, "()Ljava/lang/ClassLoader;"),
Elliott Hughes6bbe8b02011-09-20 13:15:00 -0700110 NATIVE_METHOD(VMStack, getClosestUserClassLoader, "(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)Ljava/lang/ClassLoader;"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700111 NATIVE_METHOD(VMStack, getStackClass2, "()Ljava/lang/Class;"),
112 NATIVE_METHOD(VMStack, getThreadStackTrace, "(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"),
113};
114
115} // namespace
116
117void register_dalvik_system_VMStack(JNIEnv* env) {
118 jniRegisterNativeMethods(env, "dalvik/system/VMStack", gMethods, NELEM(gMethods));
119}
120
121} // namespace art