blob: 331a64f099831b2021ae781983328fbbc8255ba7 [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.
74 Frame frame = Thread::Current()->GetTopOfStack();
75 frame.Next();
76 frame.Next();
77 Method* callerCaller = frame.GetMethod();
78 const Object* cl = callerCaller->GetDeclaringClass()->GetClassLoader();
79 return AddLocalReference<jobject>(env, cl);
Elliott Hughes8daa0922011-09-11 13:46:25 -070080}
81
82jobjectArray VMStack_getClasses(JNIEnv* env, jclass, jint maxDepth) {
83 UNIMPLEMENTED(FATAL);
84 return NULL;
85}
86
87jclass VMStack_getStackClass2(JNIEnv* env, jclass) {
Elliott Hughes01158d72011-09-19 19:47:10 -070088 // Returns the class of the caller's caller's caller.
89 Frame frame = Thread::Current()->GetTopOfStack();
90 frame.Next();
91 frame.Next();
92 frame.Next();
93 Method* callerCallerCaller = frame.GetMethod();
94 Class* c = callerCallerCaller->GetDeclaringClass();
95 return AddLocalReference<jclass>(env, c);
Elliott Hughes8daa0922011-09-11 13:46:25 -070096}
97
Elliott Hughes01158d72011-09-19 19:47:10 -070098jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) {
99 jobject trace = GetThreadStack(env, javaThread);
100 if (trace == NULL) {
101 return NULL;
102 }
103 return Thread::InternalStackTraceToStackTraceElementArray(env, trace);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700104}
105
106static JNINativeMethod gMethods[] = {
107 NATIVE_METHOD(VMStack, fillStackTraceElements, "(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"),
108 NATIVE_METHOD(VMStack, getCallingClassLoader, "()Ljava/lang/ClassLoader;"),
109 NATIVE_METHOD(VMStack, getClasses, "(I)[Ljava/lang/Class;"),
110 NATIVE_METHOD(VMStack, getStackClass2, "()Ljava/lang/Class;"),
111 NATIVE_METHOD(VMStack, getThreadStackTrace, "(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"),
112};
113
114} // namespace
115
116void register_dalvik_system_VMStack(JNIEnv* env) {
117 jniRegisterNativeMethods(env, "dalvik/system/VMStack", gMethods, NELEM(gMethods));
118}
119
120} // namespace art