Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 17 | #include "class_loader.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 18 | #include "jni_internal.h" |
Elliott Hughes | 6a14433 | 2012-04-03 13:07:11 -0700 | [diff] [blame] | 19 | #include "nth_caller_visitor.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 20 | #include "object.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 21 | #include "scoped_thread_state_change.h" |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 22 | #include "thread_list.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 23 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 24 | namespace art { |
| 25 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 26 | static jobject GetThreadStack(JNIEnv* env, jobject peer) { |
| 27 | bool timeout; |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 28 | { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 29 | ScopedObjectAccess soa(env); |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 30 | if (soa.Decode<Object*>(peer) == soa.Self()->GetPeer()) { |
| 31 | return soa.Self()->CreateInternalStackTrace(soa); |
| 32 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 33 | } |
| 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 Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 46 | 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 Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 50 | return NULL; |
| 51 | } |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame^] | 54 | static jint VMStack_fillStackTraceElements(JNIEnv* env, jclass, jobject javaThread, |
| 55 | jobjectArray javaSteArray) { |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 56 | 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 Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Elliott Hughes | 6a14433 | 2012-04-03 13:07:11 -0700 | [diff] [blame] | 65 | // Returns the defining class loader of the caller's caller. |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 66 | static jobject VMStack_getCallingClassLoader(JNIEnv* env, jclass) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 67 | ScopedObjectAccess soa(env); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 68 | NthCallerVisitor visitor(soa.Self()->GetManagedStack(), soa.Self()->GetInstrumentationStack(), 2); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 69 | visitor.WalkStack(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 70 | return soa.AddLocalReference<jobject>(visitor.caller->GetDeclaringClass()->GetClassLoader()); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame^] | 73 | static jobject VMStack_getClosestUserClassLoader(JNIEnv* env, jclass, jobject javaBootstrap, |
| 74 | jobject javaSystem) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 75 | struct ClosestUserClassLoaderVisitor : public StackVisitor { |
| 76 | ClosestUserClassLoaderVisitor(const ManagedStack* stack, |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame^] | 77 | const std::deque<InstrumentationStackFrame>* instrumentation_stack, |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 78 | Object* bootstrap, Object* system) |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 79 | : StackVisitor(stack, instrumentation_stack, NULL), |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 80 | bootstrap(bootstrap), system(system), class_loader(NULL) {} |
| 81 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 82 | bool VisitFrame() { |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 83 | DCHECK(class_loader == NULL); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 84 | Class* c = GetMethod()->GetDeclaringClass(); |
Brian Carlstrom | 5cb71bb | 2012-03-09 14:57:37 -0800 | [diff] [blame] | 85 | Object* cl = c->GetClassLoader(); |
| 86 | if (cl != NULL && cl != bootstrap && cl != system) { |
| 87 | class_loader = cl; |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 88 | return false; |
Brian Carlstrom | 5cb71bb | 2012-03-09 14:57:37 -0800 | [diff] [blame] | 89 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 90 | return true; |
Brian Carlstrom | 5cb71bb | 2012-03-09 14:57:37 -0800 | [diff] [blame] | 91 | } |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 92 | |
Brian Carlstrom | 5cb71bb | 2012-03-09 14:57:37 -0800 | [diff] [blame] | 93 | Object* bootstrap; |
| 94 | Object* system; |
| 95 | Object* class_loader; |
| 96 | }; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 97 | ScopedObjectAccess soa(env); |
| 98 | Object* bootstrap = soa.Decode<Object*>(javaBootstrap); |
| 99 | Object* system = soa.Decode<Object*>(javaSystem); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 100 | ClosestUserClassLoaderVisitor visitor(soa.Self()->GetManagedStack(), soa.Self()->GetInstrumentationStack(), |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 101 | bootstrap, system); |
| 102 | visitor.WalkStack(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 103 | return soa.AddLocalReference<jobject>(visitor.class_loader); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Elliott Hughes | 6a14433 | 2012-04-03 13:07:11 -0700 | [diff] [blame] | 106 | // Returns the class of the caller's caller's caller. |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 107 | static jclass VMStack_getStackClass2(JNIEnv* env, jclass) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 108 | ScopedObjectAccess soa(env); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 109 | NthCallerVisitor visitor(soa.Self()->GetManagedStack(), soa.Self()->GetInstrumentationStack(), 3); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 110 | visitor.WalkStack(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 111 | return soa.AddLocalReference<jclass>(visitor.caller->GetDeclaringClass()); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 114 | static jobjectArray VMStack_getThreadStackTrace(JNIEnv* env, jclass, jobject javaThread) { |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 115 | jobject trace = GetThreadStack(env, javaThread); |
| 116 | if (trace == NULL) { |
| 117 | return NULL; |
| 118 | } |
| 119 | return Thread::InternalStackTraceToStackTraceElementArray(env, trace); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | static JNINativeMethod gMethods[] = { |
| 123 | NATIVE_METHOD(VMStack, fillStackTraceElements, "(Ljava/lang/Thread;[Ljava/lang/StackTraceElement;)I"), |
| 124 | NATIVE_METHOD(VMStack, getCallingClassLoader, "()Ljava/lang/ClassLoader;"), |
Elliott Hughes | 6bbe8b0 | 2011-09-20 13:15:00 -0700 | [diff] [blame] | 125 | NATIVE_METHOD(VMStack, getClosestUserClassLoader, "(Ljava/lang/ClassLoader;Ljava/lang/ClassLoader;)Ljava/lang/ClassLoader;"), |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 126 | NATIVE_METHOD(VMStack, getStackClass2, "()Ljava/lang/Class;"), |
| 127 | NATIVE_METHOD(VMStack, getThreadStackTrace, "(Ljava/lang/Thread;)[Ljava/lang/StackTraceElement;"), |
| 128 | }; |
| 129 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 130 | void register_dalvik_system_VMStack(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 131 | REGISTER_NATIVE_METHODS("dalvik/system/VMStack"); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | } // namespace art |