blob: 5ef512a6f3da87f18f9f940bee22680fdde2afdc [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"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070021#include "scoped_thread_state_change.h"
Elliott Hughes01158d72011-09-19 19:47:10 -070022#include "thread_list.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070023
Elliott Hughes8daa0922011-09-11 13:46:25 -070024namespace art {
25
Ian Rogers00f7d0e2012-07-19 15:28:27 -070026static jobject GetThreadStack(JNIEnv* env, jobject peer) {
27 bool timeout;
Mathieu Chartierdbe6f462012-09-25 16:54:50 -070028 Thread* self = Thread::Current();
29 if (env->IsSameObject(peer, self->GetPeer())) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070030 ScopedObjectAccess soa(env);
Mathieu Chartierdbe6f462012-09-25 16:54:50 -070031 return self->CreateInternalStackTrace(soa);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070032 }
33 // Suspend thread to build stack trace.
34 Thread* thread = Thread::SuspendForDebugger(peer, true, &timeout);
35 if (thread != NULL) {
36 jobject trace;
37 {
38 ScopedObjectAccess soa(env);
39 trace = thread->CreateInternalStackTrace(soa);
40 }
41 // Restart suspended thread.
42 Runtime::Current()->GetThreadList()->Resume(thread, true);
43 return trace;
44 } else {
Ian Rogers15bf2d32012-08-28 17:33:04 -070045 if (timeout) {
46 LOG(ERROR) << "Trying to get thread's stack failed as the thread failed to suspend within a "
47 "generous timeout.";
48 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070049 return NULL;
50 }
Elliott Hughes01158d72011-09-19 19:47:10 -070051}
52
Elliott Hughes0512f022012-03-15 22:10:52 -070053static jint VMStack_fillStackTraceElements(JNIEnv* env, jclass, jobject javaThread, jobjectArray javaSteArray) {
Elliott Hughes01158d72011-09-19 19:47:10 -070054 jobject trace = GetThreadStack(env, javaThread);
55 if (trace == NULL) {
56 return 0;
57 }
58 int32_t depth;
59 Thread::InternalStackTraceToStackTraceElementArray(env, trace, javaSteArray, &depth);
60 return depth;
Elliott Hughes8daa0922011-09-11 13:46:25 -070061}
62
Elliott Hughes6a144332012-04-03 13:07:11 -070063// Returns the defining class loader of the caller's caller.
Elliott Hughes0512f022012-03-15 22:10:52 -070064static jobject VMStack_getCallingClassLoader(JNIEnv* env, jclass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070065 ScopedObjectAccess soa(env);
jeffhao725a9572012-11-13 18:20:12 -080066 NthCallerVisitor visitor(soa.Self()->GetManagedStack(), soa.Self()->GetInstrumentationStack(), 2);
Ian Rogers0399dde2012-06-06 17:09:28 -070067 visitor.WalkStack();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070068 return soa.AddLocalReference<jobject>(visitor.caller->GetDeclaringClass()->GetClassLoader());
Elliott Hughes8daa0922011-09-11 13:46:25 -070069}
70
Elliott Hughes0512f022012-03-15 22:10:52 -070071static jobject VMStack_getClosestUserClassLoader(JNIEnv* env, jclass, jobject javaBootstrap, jobject javaSystem) {
Ian Rogers0399dde2012-06-06 17:09:28 -070072 struct ClosestUserClassLoaderVisitor : public StackVisitor {
73 ClosestUserClassLoaderVisitor(const ManagedStack* stack,
jeffhao725a9572012-11-13 18:20:12 -080074 const std::vector<InstrumentationStackFrame>* instrumentation_stack,
Ian Rogers0399dde2012-06-06 17:09:28 -070075 Object* bootstrap, Object* system)
jeffhao725a9572012-11-13 18:20:12 -080076 : StackVisitor(stack, instrumentation_stack, NULL),
Elliott Hughes08fc03a2012-06-26 17:34:00 -070077 bootstrap(bootstrap), system(system), class_loader(NULL) {}
78
Ian Rogers0399dde2012-06-06 17:09:28 -070079 bool VisitFrame() {
Elliott Hughes530fa002012-03-12 11:44:49 -070080 DCHECK(class_loader == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -070081 Class* c = GetMethod()->GetDeclaringClass();
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080082 Object* cl = c->GetClassLoader();
83 if (cl != NULL && cl != bootstrap && cl != system) {
84 class_loader = cl;
Elliott Hughes530fa002012-03-12 11:44:49 -070085 return false;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080086 }
Elliott Hughes530fa002012-03-12 11:44:49 -070087 return true;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080088 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -070089
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080090 Object* bootstrap;
91 Object* system;
92 Object* class_loader;
93 };
Ian Rogers00f7d0e2012-07-19 15:28:27 -070094 ScopedObjectAccess soa(env);
95 Object* bootstrap = soa.Decode<Object*>(javaBootstrap);
96 Object* system = soa.Decode<Object*>(javaSystem);
jeffhao725a9572012-11-13 18:20:12 -080097 ClosestUserClassLoaderVisitor visitor(soa.Self()->GetManagedStack(), soa.Self()->GetInstrumentationStack(),
Ian Rogers0399dde2012-06-06 17:09:28 -070098 bootstrap, system);
99 visitor.WalkStack();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700100 return soa.AddLocalReference<jobject>(visitor.class_loader);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700101}
102
Elliott Hughes6a144332012-04-03 13:07:11 -0700103// Returns the class of the caller's caller's caller.
Elliott Hughes0512f022012-03-15 22:10:52 -0700104static jclass VMStack_getStackClass2(JNIEnv* env, jclass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700105 ScopedObjectAccess soa(env);
jeffhao725a9572012-11-13 18:20:12 -0800106 NthCallerVisitor visitor(soa.Self()->GetManagedStack(), soa.Self()->GetInstrumentationStack(), 3);
Ian Rogers0399dde2012-06-06 17:09:28 -0700107 visitor.WalkStack();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700108 return soa.AddLocalReference<jclass>(visitor.caller->GetDeclaringClass());
Elliott Hughes8daa0922011-09-11 13:46:25 -0700109}
110
Elliott Hughes0512f022012-03-15 22:10:52 -0700111static jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700112 jobject trace = GetThreadStack(env, javaThread);
113 if (trace == NULL) {
114 return NULL;
115 }
116 return Thread::InternalStackTraceToStackTraceElementArray(env, trace);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700117}
118
119static JNINativeMethod gMethods[] = {
120 NATIVE_METHOD(VMStack, fillStackTraceElements, "(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"),
121 NATIVE_METHOD(VMStack, getCallingClassLoader, "()Ljava/lang/ClassLoader;"),
Elliott Hughes6bbe8b02011-09-20 13:15:00 -0700122 NATIVE_METHOD(VMStack, getClosestUserClassLoader, "(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)Ljava/lang/ClassLoader;"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700123 NATIVE_METHOD(VMStack, getStackClass2, "()Ljava/lang/Class;"),
124 NATIVE_METHOD(VMStack, getThreadStackTrace, "(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"),
125};
126
Elliott Hughes8daa0922011-09-11 13:46:25 -0700127void register_dalvik_system_VMStack(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700128 REGISTER_NATIVE_METHODS("dalvik/system/VMStack");
Elliott Hughes8daa0922011-09-11 13:46:25 -0700129}
130
131} // namespace art