blob: 8ce022da88689dd3beec7c742605c0b0330f5547 [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;
Ian Rogerscfaa4552012-11-26 21:00:08 -080028 {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070029 ScopedObjectAccess soa(env);
Ian Rogerscfaa4552012-11-26 21:00:08 -080030 if (soa.Decode<Object*>(peer) == soa.Self()->GetPeer()) {
31 return soa.Self()->CreateInternalStackTrace(soa);
32 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070033 }
34 // Suspend thread to build stack trace.
35 Thread* thread = Thread::SuspendForDebugger(peer, true, &timeout);
36 if (thread != NULL) {
37 jobject trace;
38 {
39 ScopedObjectAccess soa(env);
40 trace = thread->CreateInternalStackTrace(soa);
41 }
42 // Restart suspended thread.
43 Runtime::Current()->GetThreadList()->Resume(thread, true);
44 return trace;
45 } else {
Ian Rogers15bf2d32012-08-28 17:33:04 -070046 if (timeout) {
47 LOG(ERROR) << "Trying to get thread's stack failed as the thread failed to suspend within a "
48 "generous timeout.";
49 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070050 return NULL;
51 }
Elliott Hughes01158d72011-09-19 19:47:10 -070052}
53
Ian Rogers306057f2012-11-26 12:45:53 -080054static jint VMStack_fillStackTraceElements(JNIEnv* env, jclass, jobject javaThread,
55 jobjectArray javaSteArray) {
Elliott Hughes01158d72011-09-19 19:47:10 -070056 jobject trace = GetThreadStack(env, javaThread);
57 if (trace == NULL) {
58 return 0;
59 }
60 int32_t depth;
61 Thread::InternalStackTraceToStackTraceElementArray(env, trace, javaSteArray, &depth);
62 return depth;
Elliott Hughes8daa0922011-09-11 13:46:25 -070063}
64
Elliott Hughes6a144332012-04-03 13:07:11 -070065// Returns the defining class loader of the caller's caller.
Elliott Hughes0512f022012-03-15 22:10:52 -070066static jobject VMStack_getCallingClassLoader(JNIEnv* env, jclass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070067 ScopedObjectAccess soa(env);
jeffhao725a9572012-11-13 18:20:12 -080068 NthCallerVisitor visitor(soa.Self()->GetManagedStack(), soa.Self()->GetInstrumentationStack(), 2);
Ian Rogers0399dde2012-06-06 17:09:28 -070069 visitor.WalkStack();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070070 return soa.AddLocalReference<jobject>(visitor.caller->GetDeclaringClass()->GetClassLoader());
Elliott Hughes8daa0922011-09-11 13:46:25 -070071}
72
Ian Rogers306057f2012-11-26 12:45:53 -080073static jobject VMStack_getClosestUserClassLoader(JNIEnv* env, jclass, jobject javaBootstrap,
74 jobject javaSystem) {
Ian Rogers0399dde2012-06-06 17:09:28 -070075 struct ClosestUserClassLoaderVisitor : public StackVisitor {
76 ClosestUserClassLoaderVisitor(const ManagedStack* stack,
Ian Rogers306057f2012-11-26 12:45:53 -080077 const std::deque<InstrumentationStackFrame>* instrumentation_stack,
Ian Rogers0399dde2012-06-06 17:09:28 -070078 Object* bootstrap, Object* system)
jeffhao725a9572012-11-13 18:20:12 -080079 : StackVisitor(stack, instrumentation_stack, NULL),
Elliott Hughes08fc03a2012-06-26 17:34:00 -070080 bootstrap(bootstrap), system(system), class_loader(NULL) {}
81
Ian Rogers0399dde2012-06-06 17:09:28 -070082 bool VisitFrame() {
Elliott Hughes530fa002012-03-12 11:44:49 -070083 DCHECK(class_loader == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -070084 Class* c = GetMethod()->GetDeclaringClass();
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080085 Object* cl = c->GetClassLoader();
86 if (cl != NULL && cl != bootstrap && cl != system) {
87 class_loader = cl;
Elliott Hughes530fa002012-03-12 11:44:49 -070088 return false;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080089 }
Elliott Hughes530fa002012-03-12 11:44:49 -070090 return true;
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080091 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -070092
Brian Carlstrom5cb71bb2012-03-09 14:57:37 -080093 Object* bootstrap;
94 Object* system;
95 Object* class_loader;
96 };
Ian Rogers00f7d0e2012-07-19 15:28:27 -070097 ScopedObjectAccess soa(env);
98 Object* bootstrap = soa.Decode<Object*>(javaBootstrap);
99 Object* system = soa.Decode<Object*>(javaSystem);
jeffhao725a9572012-11-13 18:20:12 -0800100 ClosestUserClassLoaderVisitor visitor(soa.Self()->GetManagedStack(), soa.Self()->GetInstrumentationStack(),
Ian Rogers0399dde2012-06-06 17:09:28 -0700101 bootstrap, system);
102 visitor.WalkStack();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700103 return soa.AddLocalReference<jobject>(visitor.class_loader);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700104}
105
Elliott Hughes6a144332012-04-03 13:07:11 -0700106// Returns the class of the caller's caller's caller.
Elliott Hughes0512f022012-03-15 22:10:52 -0700107static jclass VMStack_getStackClass2(JNIEnv* env, jclass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700108 ScopedObjectAccess soa(env);
jeffhao725a9572012-11-13 18:20:12 -0800109 NthCallerVisitor visitor(soa.Self()->GetManagedStack(), soa.Self()->GetInstrumentationStack(), 3);
Ian Rogers0399dde2012-06-06 17:09:28 -0700110 visitor.WalkStack();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700111 return soa.AddLocalReference<jclass>(visitor.caller->GetDeclaringClass());
Elliott Hughes8daa0922011-09-11 13:46:25 -0700112}
113
Elliott Hughes0512f022012-03-15 22:10:52 -0700114static jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700115 jobject trace = GetThreadStack(env, javaThread);
116 if (trace == NULL) {
117 return NULL;
118 }
119 return Thread::InternalStackTraceToStackTraceElementArray(env, trace);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700120}
121
122static JNINativeMethod gMethods[] = {
123 NATIVE_METHOD(VMStack, fillStackTraceElements, "(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"),
124 NATIVE_METHOD(VMStack, getCallingClassLoader, "()Ljava/lang/ClassLoader;"),
Elliott Hughes6bbe8b02011-09-20 13:15:00 -0700125 NATIVE_METHOD(VMStack, getClosestUserClassLoader, "(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)Ljava/lang/ClassLoader;"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700126 NATIVE_METHOD(VMStack, getStackClass2, "()Ljava/lang/Class;"),
127 NATIVE_METHOD(VMStack, getThreadStackTrace, "(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"),
128};
129
Elliott Hughes8daa0922011-09-11 13:46:25 -0700130void register_dalvik_system_VMStack(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700131 REGISTER_NATIVE_METHODS("dalvik/system/VMStack");
Elliott Hughes8daa0922011-09-11 13:46:25 -0700132}
133
134} // namespace art