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