blob: d796e69a29c62384473098cb9a4f80b16f3e4c35 [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 Hughes01158d72011-09-19 19:47:10 -070020#include "thread_list.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070021
22#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
23
24namespace art {
25
26namespace {
27
Elliott Hughes01158d72011-09-19 19:47:10 -070028class StackGetter {
29 public:
30 StackGetter(JNIEnv* env, Thread* thread) : env_(env), thread_(thread), trace_(NULL) {
31 }
32
33 static void Callback(void* arg) {
34 reinterpret_cast<StackGetter*>(arg)->Callback();
35 }
36
37 jobject GetTrace() {
38 return trace_;
39 }
40
41 private:
42 void Callback() {
43 trace_ = thread_->CreateInternalStackTrace(env_);
44 }
45
46 JNIEnv* env_;
47 Thread* thread_;
48 jobject trace_;
49};
50
51jobject GetThreadStack(JNIEnv* env, jobject javaThread) {
52 Thread* thread = Thread::FromManagedThread(env, javaThread);
53 if (thread == NULL) {
54 return NULL;
55 }
56 ThreadList* thread_list = Runtime::Current()->GetThreadList();
57 StackGetter stack_getter(env, thread);
58 thread_list->RunWhileSuspended(thread, StackGetter::Callback, &stack_getter);
59 return stack_getter.GetTrace();
60}
61
62jint VMStack_fillStackTraceElements(JNIEnv* env, jclass, jobject javaThread, jobjectArray javaSteArray) {
63 jobject trace = GetThreadStack(env, javaThread);
64 if (trace == NULL) {
65 return 0;
66 }
67 int32_t depth;
68 Thread::InternalStackTraceToStackTraceElementArray(env, trace, javaSteArray, &depth);
69 return depth;
Elliott Hughes8daa0922011-09-11 13:46:25 -070070}
71
72jobject VMStack_getCallingClassLoader(JNIEnv* env, jclass) {
Elliott Hughes01158d72011-09-19 19:47:10 -070073 // Returns the defining class loader of the caller's caller.
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070074 // TODO: need SmartFrame (Thread::WalkStack-like iterator).
Elliott Hughes01158d72011-09-19 19:47:10 -070075 Frame frame = Thread::Current()->GetTopOfStack();
76 frame.Next();
77 frame.Next();
78 Method* callerCaller = frame.GetMethod();
79 const Object* cl = callerCaller->GetDeclaringClass()->GetClassLoader();
80 return AddLocalReference<jobject>(env, cl);
Elliott Hughes8daa0922011-09-11 13:46:25 -070081}
82
Elliott Hughes6bbe8b02011-09-20 13:15:00 -070083jobject VMStack_getClosestUserClassLoader(JNIEnv* env, jclass, jobject javaBootstrap, jobject javaSystem) {
84 Thread* self = Thread::Current();
85 Object* bootstrap = Decode<Object*>(env, javaBootstrap);
86 Object* system = Decode<Object*>(env, javaSystem);
87 // TODO: need SmartFrame (Thread::WalkStack-like iterator).
88 for (Frame frame = self->GetTopOfStack(); frame.HasNext(); frame.Next()) {
89 Class* c = frame.GetMethod()->GetDeclaringClass();
90 Object* cl = const_cast<ClassLoader*>(c->GetClassLoader());
91 if (cl != bootstrap && cl != system) {
92 return AddLocalReference<jobject>(env, cl);
93 }
94 }
Elliott Hughes8daa0922011-09-11 13:46:25 -070095 return NULL;
96}
97
98jclass VMStack_getStackClass2(JNIEnv* env, jclass) {
Elliott Hughes01158d72011-09-19 19:47:10 -070099 // Returns the class of the caller's caller's caller.
Elliott Hughes6bbe8b02011-09-20 13:15:00 -0700100 // TODO: need SmartFrame (Thread::WalkStack-like iterator).
Elliott Hughes01158d72011-09-19 19:47:10 -0700101 Frame frame = Thread::Current()->GetTopOfStack();
102 frame.Next();
103 frame.Next();
104 frame.Next();
105 Method* callerCallerCaller = frame.GetMethod();
106 Class* c = callerCallerCaller->GetDeclaringClass();
107 return AddLocalReference<jclass>(env, c);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700108}
109
Elliott Hughes01158d72011-09-19 19:47:10 -0700110jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) {
111 jobject trace = GetThreadStack(env, javaThread);
112 if (trace == NULL) {
113 return NULL;
114 }
115 return Thread::InternalStackTraceToStackTraceElementArray(env, trace);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700116}
117
118static JNINativeMethod gMethods[] = {
119 NATIVE_METHOD(VMStack, fillStackTraceElements, "(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"),
120 NATIVE_METHOD(VMStack, getCallingClassLoader, "()Ljava/lang/ClassLoader;"),
Elliott Hughes6bbe8b02011-09-20 13:15:00 -0700121 NATIVE_METHOD(VMStack, getClosestUserClassLoader, "(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)Ljava/lang/ClassLoader;"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700122 NATIVE_METHOD(VMStack, getStackClass2, "()Ljava/lang/Class;"),
123 NATIVE_METHOD(VMStack, getThreadStackTrace, "(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"),
124};
125
126} // namespace
127
128void register_dalvik_system_VMStack(JNIEnv* env) {
129 jniRegisterNativeMethods(env, "dalvik/system/VMStack", gMethods, NELEM(gMethods));
130}
131
132} // namespace art