Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -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 | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 17 | #include "base/logging.h" |
Elliott Hughes | 76b6167 | 2012-12-12 17:47:30 -0800 | [diff] [blame] | 18 | #include "base/mutex.h" |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 19 | #include "debugger.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 20 | #include "jni_internal.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 21 | #include "scoped_thread_state_change.h" |
| 22 | #include "ScopedLocalRef.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 23 | #include "ScopedPrimitiveArray.h" |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 24 | #include "stack.h" |
| 25 | #include "thread_list.h" |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 26 | |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 27 | namespace art { |
| 28 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 29 | static void DdmVmInternal_enableRecentAllocations(JNIEnv*, jclass, jboolean enable) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 30 | Dbg::SetAllocTrackingEnabled(enable); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 33 | static jbyteArray DdmVmInternal_getRecentAllocations(JNIEnv* env, jclass) { |
| 34 | ScopedObjectAccess soa(env); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 35 | return Dbg::GetRecentAllocations(); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 38 | static jboolean DdmVmInternal_getRecentAllocationStatus(JNIEnv*, jclass) { |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 39 | return Dbg::IsAllocTrackingEnabled(); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | /* |
| 43 | * Get a stack trace as an array of StackTraceElement objects. Returns |
| 44 | * NULL on failure, e.g. if the threadId couldn't be found. |
| 45 | */ |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 46 | static jobjectArray DdmVmInternal_getStackTraceById(JNIEnv* env, jclass, jint thin_lock_id) { |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 47 | ScopedLocalRef<jobject> peer(env, NULL); |
| 48 | { |
| 49 | Thread* t = Runtime::Current()->GetThreadList()->FindThreadByThinLockId(thin_lock_id); |
| 50 | if (t == NULL) { |
| 51 | return NULL; |
| 52 | } |
| 53 | ScopedObjectAccess soa(env); |
| 54 | peer.reset(soa.AddLocalReference<jobject>(t->GetPeer())); |
| 55 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 56 | if (peer.get() == NULL) { |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 57 | return NULL; |
| 58 | } |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 59 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 60 | // Suspend thread to build stack trace. |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 61 | bool timed_out; |
| 62 | Thread* thread = Thread::SuspendForDebugger(peer.get(), true, &timed_out); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 63 | if (thread != NULL) { |
| 64 | jobject trace; |
| 65 | { |
| 66 | ScopedObjectAccess soa(env); |
| 67 | trace = thread->CreateInternalStackTrace(soa); |
| 68 | } |
| 69 | // Restart suspended thread. |
| 70 | Runtime::Current()->GetThreadList()->Resume(thread, true); |
| 71 | return Thread::InternalStackTraceToStackTraceElementArray(env, trace); |
| 72 | } else { |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 73 | if (timed_out) { |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 74 | LOG(ERROR) << "Trying to get thread's stack by id failed as the thread failed to suspend " |
| 75 | "within a generous timeout."; |
| 76 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 77 | return NULL; |
| 78 | } |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static void ThreadCountCallback(Thread*, void* context) { |
| 82 | uint16_t& count = *reinterpret_cast<uint16_t*>(context); |
| 83 | ++count; |
| 84 | } |
| 85 | |
| 86 | static const int kThstBytesPerEntry = 18; |
| 87 | static const int kThstHeaderLen = 4; |
| 88 | |
| 89 | static void ThreadStatsGetterCallback(Thread* t, void* context) { |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 90 | /* |
| 91 | * Generate the contents of a THST chunk. The data encompasses all known |
| 92 | * threads. |
| 93 | * |
| 94 | * Response has: |
| 95 | * (1b) header len |
| 96 | * (1b) bytes per entry |
| 97 | * (2b) thread count |
| 98 | * Then, for each thread: |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 99 | * (4b) thread id |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 100 | * (1b) thread status |
| 101 | * (4b) tid |
| 102 | * (4b) utime |
| 103 | * (4b) stime |
| 104 | * (1b) is daemon? |
| 105 | * |
| 106 | * The length fields exist in anticipation of adding additional fields |
| 107 | * without wanting to break ddms or bump the full protocol version. I don't |
| 108 | * think it warrants full versioning. They might be extraneous and could |
| 109 | * be removed from a future version. |
| 110 | */ |
Elliott Hughes | ba0b9c5 | 2012-09-20 11:25:12 -0700 | [diff] [blame] | 111 | char native_thread_state; |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 112 | int utime, stime, task_cpu; |
Elliott Hughes | ba0b9c5 | 2012-09-20 11:25:12 -0700 | [diff] [blame] | 113 | GetTaskStats(t->GetTid(), native_thread_state, utime, stime, task_cpu); |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 114 | |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 115 | std::vector<uint8_t>& bytes = *reinterpret_cast<std::vector<uint8_t>*>(context); |
| 116 | JDWP::Append4BE(bytes, t->GetThinLockId()); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 117 | JDWP::Append1BE(bytes, t->GetState()); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 118 | JDWP::Append4BE(bytes, t->GetTid()); |
| 119 | JDWP::Append4BE(bytes, utime); |
| 120 | JDWP::Append4BE(bytes, stime); |
| 121 | JDWP::Append1BE(bytes, t->IsDaemon()); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static jbyteArray DdmVmInternal_getThreadStats(JNIEnv* env, jclass) { |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 125 | std::vector<uint8_t> bytes; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 126 | Thread* self = static_cast<JNIEnvExt*>(env)->self; |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 127 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 128 | MutexLock mu(self, *Locks::thread_list_lock_); |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 129 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 130 | |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 131 | uint16_t thread_count = 0; |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 132 | thread_list->ForEach(ThreadCountCallback, &thread_count); |
| 133 | |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 134 | JDWP::Append1BE(bytes, kThstHeaderLen); |
| 135 | JDWP::Append1BE(bytes, kThstBytesPerEntry); |
| 136 | JDWP::Append2BE(bytes, thread_count); |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 137 | |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 138 | thread_list->ForEach(ThreadStatsGetterCallback, &bytes); |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | jbyteArray result = env->NewByteArray(bytes.size()); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 142 | if (result != NULL) { |
| 143 | env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0])); |
| 144 | } |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 145 | return result; |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 148 | static jint DdmVmInternal_heapInfoNotify(JNIEnv* env, jclass, jint when) { |
| 149 | ScopedObjectAccess soa(env); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 150 | return Dbg::DdmHandleHpifChunk(static_cast<Dbg::HpifWhen>(when)); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 153 | static jboolean DdmVmInternal_heapSegmentNotify(JNIEnv*, jclass, jint when, jint what, jboolean native) { |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 154 | return Dbg::DdmHandleHpsgNhsgChunk(static_cast<Dbg::HpsgWhen>(when), static_cast<Dbg::HpsgWhat>(what), native); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 157 | static void DdmVmInternal_threadNotify(JNIEnv*, jclass, jboolean enable) { |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 158 | Dbg::DdmSetThreadNotification(enable); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | static JNINativeMethod gMethods[] = { |
| 162 | NATIVE_METHOD(DdmVmInternal, enableRecentAllocations, "(Z)V"), |
| 163 | NATIVE_METHOD(DdmVmInternal, getRecentAllocations, "()[B"), |
| 164 | NATIVE_METHOD(DdmVmInternal, getRecentAllocationStatus, "()Z"), |
| 165 | NATIVE_METHOD(DdmVmInternal, getStackTraceById, "(I)[Ljava/lang/StackTraceElement;"), |
| 166 | NATIVE_METHOD(DdmVmInternal, getThreadStats, "()[B"), |
| 167 | NATIVE_METHOD(DdmVmInternal, heapInfoNotify, "(I)Z"), |
| 168 | NATIVE_METHOD(DdmVmInternal, heapSegmentNotify, "(IIZ)Z"), |
| 169 | NATIVE_METHOD(DdmVmInternal, threadNotify, "(Z)V"), |
| 170 | }; |
| 171 | |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 172 | void register_org_apache_harmony_dalvik_ddmc_DdmVmInternal(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 173 | REGISTER_NATIVE_METHODS("org/apache/harmony/dalvik/ddmc/DdmVmInternal"); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | } // namespace art |