blob: 12fa8db904d4065295ab498e7be4e8b394c0da21 [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)
Elliott Hughes08fc03a2012-06-26 17:34:00 -070059 : StackVisitor(stack, trace_stack, NULL),
60 bootstrap(bootstrap), system(system), class_loader(NULL) {}
61
Ian Rogers0399dde2012-06-06 17:09:28 -070062 bool VisitFrame() {
Elliott Hughes530fa002012-03-12 11:44:49 -070063 DCHECK(class_loader == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -070064 Class* c = GetMethod()->GetDeclaringClass();
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080065 Object* cl = c->GetClassLoader();
66 if (cl != NULL && cl != bootstrap && cl != system) {
67 class_loader = cl;
Elliott Hughes530fa002012-03-12 11:44:49 -070068 return false;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080069 }
Elliott Hughes530fa002012-03-12 11:44:49 -070070 return true;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080071 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -070072
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080073 Object* bootstrap;
74 Object* system;
75 Object* class_loader;
76 };
Ian Rogers0399dde2012-06-06 17:09:28 -070077 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -070078 Object* bootstrap = ts.Decode<Object*>(javaBootstrap);
79 Object* system = ts.Decode<Object*>(javaSystem);
Ian Rogers0399dde2012-06-06 17:09:28 -070080 ClosestUserClassLoaderVisitor visitor(ts.Self()->GetManagedStack(), ts.Self()->GetTraceStack(),
81 bootstrap, system);
82 visitor.WalkStack();
Ian Rogers365c1022012-06-22 15:05:28 -070083 return ts.AddLocalReference<jobject>(visitor.class_loader);
Elliott Hughes8daa0922011-09-11 13:46:25 -070084}
85
Elliott Hughes6a144332012-04-03 13:07:11 -070086// Returns the class of the caller's caller's caller.
Elliott Hughes0512f022012-03-15 22:10:52 -070087static jclass VMStack_getStackClass2(JNIEnv* env, jclass) {
Ian Rogers365c1022012-06-22 15:05:28 -070088 ScopedJniThreadState ts(env);
Ian Rogers0399dde2012-06-06 17:09:28 -070089 NthCallerVisitor visitor(ts.Self()->GetManagedStack(), ts.Self()->GetTraceStack(), 3);
90 visitor.WalkStack();
Ian Rogers365c1022012-06-22 15:05:28 -070091 return ts.AddLocalReference<jclass>(visitor.caller->GetDeclaringClass());
Elliott Hughes8daa0922011-09-11 13:46:25 -070092}
93
Elliott Hughes0512f022012-03-15 22:10:52 -070094static jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) {
Elliott Hughes01158d72011-09-19 19:47:10 -070095 jobject trace = GetThreadStack(env, javaThread);
96 if (trace == NULL) {
97 return NULL;
98 }
99 return Thread::InternalStackTraceToStackTraceElementArray(env, trace);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700100}
101
102static JNINativeMethod gMethods[] = {
103 NATIVE_METHOD(VMStack, fillStackTraceElements, "(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"),
104 NATIVE_METHOD(VMStack, getCallingClassLoader, "()Ljava/lang/ClassLoader;"),
Elliott Hughes6bbe8b02011-09-20 13:15:00 -0700105 NATIVE_METHOD(VMStack, getClosestUserClassLoader, "(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)Ljava/lang/ClassLoader;"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700106 NATIVE_METHOD(VMStack, getStackClass2, "()Ljava/lang/Class;"),
107 NATIVE_METHOD(VMStack, getThreadStackTrace, "(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"),
108};
109
Elliott Hughes8daa0922011-09-11 13:46:25 -0700110void register_dalvik_system_VMStack(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700111 REGISTER_NATIVE_METHODS("dalvik/system/VMStack");
Elliott Hughes8daa0922011-09-11 13:46:25 -0700112}
113
114} // namespace art