blob: 8401ec10b42c1cc284cb6080492cca7f03735c6b [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 Hughes6a144332012-04-03 13:07:11 -070019#include "nth_caller_visitor.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070020#include "object.h"
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080021#include "scoped_heap_lock.h"
Elliott Hughes88c5c352012-03-15 18:49:48 -070022#include "scoped_thread_list_lock.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 Hughes6a144332012-04-03 13:07:11 -070046// Returns the defining class loader of the caller's caller.
Elliott Hughes0512f022012-03-15 22:10:52 -070047static jobject VMStack_getCallingClassLoader(JNIEnv* env, jclass) {
Elliott Hughes6a144332012-04-03 13:07:11 -070048 NthCallerVisitor visitor(2);
49 Thread::Current()->WalkStack(&visitor);
50 return AddLocalReference<jobject>(env, visitor.class_loader);
Elliott Hughes8daa0922011-09-11 13:46:25 -070051}
52
Elliott Hughes0512f022012-03-15 22:10:52 -070053static jobject VMStack_getClosestUserClassLoader(JNIEnv* env, jclass, jobject javaBootstrap, jobject javaSystem) {
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080054 struct ClosestUserClassLoaderVisitor : public Thread::StackVisitor {
55 ClosestUserClassLoaderVisitor(Object* bootstrap, Object* system)
56 : bootstrap(bootstrap), system(system), class_loader(NULL) {}
Elliott Hughes530fa002012-03-12 11:44:49 -070057 bool VisitFrame(const Frame& f, uintptr_t) {
58 DCHECK(class_loader == NULL);
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080059 Class* c = f.GetMethod()->GetDeclaringClass();
60 Object* cl = c->GetClassLoader();
61 if (cl != NULL && cl != bootstrap && cl != system) {
62 class_loader = cl;
Elliott Hughes530fa002012-03-12 11:44:49 -070063 return false;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080064 }
Elliott Hughes530fa002012-03-12 11:44:49 -070065 return true;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080066 }
67 Object* bootstrap;
68 Object* system;
69 Object* class_loader;
70 };
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070071 Object* bootstrap = Decode<Object*>(env, javaBootstrap);
72 Object* system = Decode<Object*>(env, javaSystem);
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080073 ClosestUserClassLoaderVisitor visitor(bootstrap, system);
74 Thread::Current()->WalkStack(&visitor);
75 return AddLocalReference<jobject>(env, visitor.class_loader);
Elliott Hughes8daa0922011-09-11 13:46:25 -070076}
77
Elliott Hughes6a144332012-04-03 13:07:11 -070078// Returns the class of the caller's caller's caller.
Elliott Hughes0512f022012-03-15 22:10:52 -070079static jclass VMStack_getStackClass2(JNIEnv* env, jclass) {
Elliott Hughes6a144332012-04-03 13:07:11 -070080 NthCallerVisitor visitor(3);
81 Thread::Current()->WalkStack(&visitor);
82 return AddLocalReference<jclass>(env, visitor.declaring_class);
Elliott Hughes8daa0922011-09-11 13:46:25 -070083}
84
Elliott Hughes0512f022012-03-15 22:10:52 -070085static jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) {
Elliott Hughes01158d72011-09-19 19:47:10 -070086 jobject trace = GetThreadStack(env, javaThread);
87 if (trace == NULL) {
88 return NULL;
89 }
90 return Thread::InternalStackTraceToStackTraceElementArray(env, trace);
Elliott Hughes8daa0922011-09-11 13:46:25 -070091}
92
93static JNINativeMethod gMethods[] = {
94 NATIVE_METHOD(VMStack, fillStackTraceElements, "(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"),
95 NATIVE_METHOD(VMStack, getCallingClassLoader, "()Ljava/lang/ClassLoader;"),
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070096 NATIVE_METHOD(VMStack, getClosestUserClassLoader, "(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)Ljava/lang/ClassLoader;"),
Elliott Hughes8daa0922011-09-11 13:46:25 -070097 NATIVE_METHOD(VMStack, getStackClass2, "()Ljava/lang/Class;"),
98 NATIVE_METHOD(VMStack, getThreadStackTrace, "(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"),
99};
100
Elliott Hughes8daa0922011-09-11 13:46:25 -0700101void register_dalvik_system_VMStack(JNIEnv* env) {
102 jniRegisterNativeMethods(env, "dalvik/system/VMStack", gMethods, NELEM(gMethods));
103}
104
105} // namespace art