blob: 696d723116c98c4f1b704b52087a5ffa38893a63 [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 Hughes88c5c352012-03-15 18:49:48 -070021#include "scoped_thread_list_lock.h"
TDYa127ee0d3fb2012-04-01 14:55:33 -070022#include "shadow_frame.h"
Elliott Hughes01158d72011-09-19 19:47:10 -070023#include "thread_list.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070024
25#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
26
27namespace art {
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
Elliott Hughes0512f022012-03-15 22:10:52 -070036static jint VMStack_fillStackTraceElements(JNIEnv* env, jclass, jobject javaThread, jobjectArray javaSteArray) {
Elliott Hughes01158d72011-09-19 19:47:10 -070037 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
Elliott Hughes0512f022012-03-15 22:10:52 -070046static jobject 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).
TDYa127ee0d3fb2012-04-01 14:55:33 -070049#if !defined(ART_USE_LLVM_COMPILER)
Elliott Hughes01158d72011-09-19 19:47:10 -070050 Frame frame = Thread::Current()->GetTopOfStack();
51 frame.Next();
52 frame.Next();
53 Method* callerCaller = frame.GetMethod();
TDYa127ee0d3fb2012-04-01 14:55:33 -070054#else
55 ShadowFrame* frame = Thread::Current()->GetTopOfShadowFrame();
56 frame = frame->GetLink();
57 frame = frame->GetLink();
58 Method* callerCaller = frame->GetMethod();
59#endif
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080060 DCHECK(callerCaller != NULL);
Elliott Hughes01158d72011-09-19 19:47:10 -070061 const Object* cl = callerCaller->GetDeclaringClass()->GetClassLoader();
62 return AddLocalReference<jobject>(env, cl);
Elliott Hughes8daa0922011-09-11 13:46:25 -070063}
64
Elliott Hughes0512f022012-03-15 22:10:52 -070065static jobject VMStack_getClosestUserClassLoader(JNIEnv* env, jclass, jobject javaBootstrap, jobject javaSystem) {
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080066 struct ClosestUserClassLoaderVisitor : public Thread::StackVisitor {
67 ClosestUserClassLoaderVisitor(Object* bootstrap, Object* system)
68 : bootstrap(bootstrap), system(system), class_loader(NULL) {}
Elliott Hughes530fa002012-03-12 11:44:49 -070069 bool VisitFrame(const Frame& f, uintptr_t) {
70 DCHECK(class_loader == NULL);
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080071 Class* c = f.GetMethod()->GetDeclaringClass();
72 Object* cl = c->GetClassLoader();
73 if (cl != NULL && cl != bootstrap && cl != system) {
74 class_loader = cl;
Elliott Hughes530fa002012-03-12 11:44:49 -070075 return false;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080076 }
Elliott Hughes530fa002012-03-12 11:44:49 -070077 return true;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080078 }
79 Object* bootstrap;
80 Object* system;
81 Object* class_loader;
82 };
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070083 Object* bootstrap = Decode<Object*>(env, javaBootstrap);
84 Object* system = Decode<Object*>(env, javaSystem);
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080085 ClosestUserClassLoaderVisitor visitor(bootstrap, system);
86 Thread::Current()->WalkStack(&visitor);
87 return AddLocalReference<jobject>(env, visitor.class_loader);
Elliott Hughes8daa0922011-09-11 13:46:25 -070088}
89
Elliott Hughes0512f022012-03-15 22:10:52 -070090static jclass VMStack_getStackClass2(JNIEnv* env, jclass) {
Elliott Hughes01158d72011-09-19 19:47:10 -070091 // Returns the class of the caller's caller's caller.
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070092 // TODO: need SmartFrame (Thread::WalkStack-like iterator).
Elliott Hughes01158d72011-09-19 19:47:10 -070093 Frame frame = Thread::Current()->GetTopOfStack();
94 frame.Next();
95 frame.Next();
96 frame.Next();
97 Method* callerCallerCaller = frame.GetMethod();
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080098 DCHECK(callerCallerCaller != NULL);
Elliott Hughes01158d72011-09-19 19:47:10 -070099 Class* c = callerCallerCaller->GetDeclaringClass();
100 return AddLocalReference<jclass>(env, c);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700101}
102
Elliott Hughes0512f022012-03-15 22:10:52 -0700103static jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700104 jobject trace = GetThreadStack(env, javaThread);
105 if (trace == NULL) {
106 return NULL;
107 }
108 return Thread::InternalStackTraceToStackTraceElementArray(env, trace);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700109}
110
111static JNINativeMethod gMethods[] = {
112 NATIVE_METHOD(VMStack, fillStackTraceElements, "(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"),
113 NATIVE_METHOD(VMStack, getCallingClassLoader, "()Ljava/lang/ClassLoader;"),
Elliott Hughes6bbe8b02011-09-20 13:15:00 -0700114 NATIVE_METHOD(VMStack, getClosestUserClassLoader, "(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)Ljava/lang/ClassLoader;"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700115 NATIVE_METHOD(VMStack, getStackClass2, "()Ljava/lang/Class;"),
116 NATIVE_METHOD(VMStack, getThreadStackTrace, "(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"),
117};
118
Elliott Hughes8daa0922011-09-11 13:46:25 -0700119void register_dalvik_system_VMStack(JNIEnv* env) {
120 jniRegisterNativeMethods(env, "dalvik/system/VMStack", gMethods, NELEM(gMethods));
121}
122
123} // namespace art