blob: ab8269405667cd20d5d93e758da890eeff8387f1 [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
Elliott Hughes01158d72011-09-19 19:47:10 -070017#include "class_loader.h"
Elliott Hughes7756d542012-05-24 21:56:51 -070018#include "jni_internal.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
Elliott Hughes8daa0922011-09-11 13:46:25 -070025namespace art {
26
Elliott Hughesbfe487b2011-10-26 15:48:55 -070027static jobject GetThreadStack(JNIEnv* env, jobject javaThread) {
Elliott Hughesffb465f2012-03-01 18:46:05 -080028 ScopedHeapLock heap_lock;
Elliott Hughesbbd9d832011-11-07 14:40:00 -080029 ScopedThreadListLock thread_list_lock;
Elliott Hughes01158d72011-09-19 19:47:10 -070030 Thread* thread = Thread::FromManagedThread(env, javaThread);
Elliott Hughesbfe487b2011-10-26 15:48:55 -070031 return (thread != NULL) ? GetThreadStack(env, thread) : NULL;
Elliott Hughes01158d72011-09-19 19:47:10 -070032}
33
Elliott Hughes0512f022012-03-15 22:10:52 -070034static jint VMStack_fillStackTraceElements(JNIEnv* env, jclass, jobject javaThread, jobjectArray javaSteArray) {
Elliott Hughes01158d72011-09-19 19:47:10 -070035 jobject trace = GetThreadStack(env, javaThread);
36 if (trace == NULL) {
37 return 0;
38 }
39 int32_t depth;
40 Thread::InternalStackTraceToStackTraceElementArray(env, trace, javaSteArray, &depth);
41 return depth;
Elliott Hughes8daa0922011-09-11 13:46:25 -070042}
43
Elliott Hughes6a144332012-04-03 13:07:11 -070044// Returns the defining class loader of the caller's caller.
Elliott Hughes0512f022012-03-15 22:10:52 -070045static jobject VMStack_getCallingClassLoader(JNIEnv* env, jclass) {
Elliott Hughes6a144332012-04-03 13:07:11 -070046 NthCallerVisitor visitor(2);
47 Thread::Current()->WalkStack(&visitor);
TDYa1273f9137d2012-04-08 15:59:19 -070048 return AddLocalReference<jobject>(env, visitor.caller->GetDeclaringClass()->GetClassLoader());
Elliott Hughes8daa0922011-09-11 13:46:25 -070049}
50
Elliott Hughes0512f022012-03-15 22:10:52 -070051static jobject VMStack_getClosestUserClassLoader(JNIEnv* env, jclass, jobject javaBootstrap, jobject javaSystem) {
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080052 struct ClosestUserClassLoaderVisitor : public Thread::StackVisitor {
53 ClosestUserClassLoaderVisitor(Object* bootstrap, Object* system)
54 : bootstrap(bootstrap), system(system), class_loader(NULL) {}
Elliott Hughes530fa002012-03-12 11:44:49 -070055 bool VisitFrame(const Frame& f, uintptr_t) {
56 DCHECK(class_loader == NULL);
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080057 Class* c = f.GetMethod()->GetDeclaringClass();
58 Object* cl = c->GetClassLoader();
59 if (cl != NULL && cl != bootstrap && cl != system) {
60 class_loader = cl;
Elliott Hughes530fa002012-03-12 11:44:49 -070061 return false;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080062 }
Elliott Hughes530fa002012-03-12 11:44:49 -070063 return true;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080064 }
65 Object* bootstrap;
66 Object* system;
67 Object* class_loader;
68 };
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070069 Object* bootstrap = Decode<Object*>(env, javaBootstrap);
70 Object* system = Decode<Object*>(env, javaSystem);
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080071 ClosestUserClassLoaderVisitor visitor(bootstrap, system);
72 Thread::Current()->WalkStack(&visitor);
73 return AddLocalReference<jobject>(env, visitor.class_loader);
Elliott Hughes8daa0922011-09-11 13:46:25 -070074}
75
Elliott Hughes6a144332012-04-03 13:07:11 -070076// Returns the class of the caller's caller's caller.
Elliott Hughes0512f022012-03-15 22:10:52 -070077static jclass VMStack_getStackClass2(JNIEnv* env, jclass) {
Elliott Hughes6a144332012-04-03 13:07:11 -070078 NthCallerVisitor visitor(3);
79 Thread::Current()->WalkStack(&visitor);
TDYa1273f9137d2012-04-08 15:59:19 -070080 return AddLocalReference<jclass>(env, visitor.caller->GetDeclaringClass());
Elliott Hughes8daa0922011-09-11 13:46:25 -070081}
82
Elliott Hughes0512f022012-03-15 22:10:52 -070083static jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) {
Elliott Hughes01158d72011-09-19 19:47:10 -070084 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
Elliott Hughes8daa0922011-09-11 13:46:25 -070099void register_dalvik_system_VMStack(JNIEnv* env) {
Elliott Hughes7756d542012-05-24 21:56:51 -0700100 REGISTER_NATIVE_METHODS("dalvik/system/VMStack");
Elliott Hughes8daa0922011-09-11 13:46:25 -0700101}
102
103} // namespace art