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