blob: 933a5d5b10907d22d0f16979b343a5a32f84b82e [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 Hugheseac76672012-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"
Ian Rogers0399dde2012-06-06 17:09:28 -070022#include "scoped_jni_thread_state.h"
Elliott Hughes88c5c352012-03-15 18:49:48 -070023#include "scoped_thread_list_lock.h"
Elliott Hughes01158d72011-09-19 19:47:10 -070024#include "thread_list.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070025
Elliott Hughes8daa0922011-09-11 13:46:25 -070026namespace art {
27
Elliott Hughesbfe487b2011-10-26 15:48:55 -070028static jobject GetThreadStack(JNIEnv* env, jobject javaThread) {
Ian Rogers365c1022012-06-22 15:05:28 -070029 ScopedJniThreadState ts(env);
Elliott Hughesffb465f2012-03-01 18:46:05 -080030 ScopedHeapLock heap_lock;
Elliott Hughesbbd9d832011-11-07 14:40:00 -080031 ScopedThreadListLock thread_list_lock;
Ian Rogers365c1022012-06-22 15:05:28 -070032 Thread* thread = Thread::FromManagedThread(ts, javaThread);
33 return (thread != NULL) ? GetThreadStack(ts, 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) {
Ian Rogers365c1022012-06-22 15:05:28 -070048 ScopedJniThreadState ts(env);
Ian Rogers0399dde2012-06-06 17:09:28 -070049 NthCallerVisitor visitor(ts.Self()->GetManagedStack(), ts.Self()->GetTraceStack(), 2);
50 visitor.WalkStack();
Ian Rogers365c1022012-06-22 15:05:28 -070051 return ts.AddLocalReference<jobject>(visitor.caller->GetDeclaringClass()->GetClassLoader());
Elliott Hughes8daa0922011-09-11 13:46:25 -070052}
53
Elliott Hughes0512f022012-03-15 22:10:52 -070054static jobject VMStack_getClosestUserClassLoader(JNIEnv* env, jclass, jobject javaBootstrap, jobject javaSystem) {
Ian Rogers0399dde2012-06-06 17:09:28 -070055 struct ClosestUserClassLoaderVisitor : public StackVisitor {
56 ClosestUserClassLoaderVisitor(const ManagedStack* stack,
57 const std::vector<TraceStackFrame>* trace_stack,
58 Object* bootstrap, Object* system)
59 : StackVisitor(stack, trace_stack), bootstrap(bootstrap), system(system),
60 class_loader(NULL) {}
61 bool VisitFrame() {
Elliott Hughes530fa002012-03-12 11:44:49 -070062 DCHECK(class_loader == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -070063 Class* c = GetMethod()->GetDeclaringClass();
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080064 Object* cl = c->GetClassLoader();
65 if (cl != NULL && cl != bootstrap && cl != system) {
66 class_loader = cl;
Elliott Hughes530fa002012-03-12 11:44:49 -070067 return false;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080068 }
Elliott Hughes530fa002012-03-12 11:44:49 -070069 return true;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080070 }
71 Object* bootstrap;
72 Object* system;
73 Object* class_loader;
74 };
Ian Rogers0399dde2012-06-06 17:09:28 -070075 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -070076 Object* bootstrap = ts.Decode<Object*>(javaBootstrap);
77 Object* system = ts.Decode<Object*>(javaSystem);
Ian Rogers0399dde2012-06-06 17:09:28 -070078 ClosestUserClassLoaderVisitor visitor(ts.Self()->GetManagedStack(), ts.Self()->GetTraceStack(),
79 bootstrap, system);
80 visitor.WalkStack();
Ian Rogers365c1022012-06-22 15:05:28 -070081 return ts.AddLocalReference<jobject>(visitor.class_loader);
Elliott Hughes8daa0922011-09-11 13:46:25 -070082}
83
Elliott Hughes6a144332012-04-03 13:07:11 -070084// Returns the class of the caller's caller's caller.
Elliott Hughes0512f022012-03-15 22:10:52 -070085static jclass VMStack_getStackClass2(JNIEnv* env, jclass) {
Ian Rogers365c1022012-06-22 15:05:28 -070086 ScopedJniThreadState ts(env);
Ian Rogers0399dde2012-06-06 17:09:28 -070087 NthCallerVisitor visitor(ts.Self()->GetManagedStack(), ts.Self()->GetTraceStack(), 3);
88 visitor.WalkStack();
Ian Rogers365c1022012-06-22 15:05:28 -070089 return ts.AddLocalReference<jclass>(visitor.caller->GetDeclaringClass());
Elliott Hughes8daa0922011-09-11 13:46:25 -070090}
91
Elliott Hughes0512f022012-03-15 22:10:52 -070092static jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) {
Elliott Hughes01158d72011-09-19 19:47:10 -070093 jobject trace = GetThreadStack(env, javaThread);
94 if (trace == NULL) {
95 return NULL;
96 }
97 return Thread::InternalStackTraceToStackTraceElementArray(env, trace);
Elliott Hughes8daa0922011-09-11 13:46:25 -070098}
99
100static JNINativeMethod gMethods[] = {
101 NATIVE_METHOD(VMStack, fillStackTraceElements, "(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"),
102 NATIVE_METHOD(VMStack, getCallingClassLoader, "()Ljava/lang/ClassLoader;"),
Elliott Hughes6bbe8b02011-09-20 13:15:00 -0700103 NATIVE_METHOD(VMStack, getClosestUserClassLoader, "(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)Ljava/lang/ClassLoader;"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700104 NATIVE_METHOD(VMStack, getStackClass2, "()Ljava/lang/Class;"),
105 NATIVE_METHOD(VMStack, getThreadStackTrace, "(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"),
106};
107
Elliott Hughes8daa0922011-09-11 13:46:25 -0700108void register_dalvik_system_VMStack(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700109 REGISTER_NATIVE_METHODS("dalvik/system/VMStack");
Elliott Hughes8daa0922011-09-11 13:46:25 -0700110}
111
112} // namespace art