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