blob: 88416af9b22f3bb8bf6f0cddaf21eb325530c1cd [file] [log] [blame]
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -07001/*
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 Hughesb3bd5f02012-03-08 21:05:27 -080019#include "scoped_heap_lock.h"
Elliott Hughes88c5c352012-03-15 18:49:48 -070020#include "scoped_thread_list_lock.h"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070021#include "stack.h"
22#include "thread_list.h"
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -070023
24#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
25#include "ScopedPrimitiveArray.h" // Last to avoid problems with LOG redefinition.
26
27namespace art {
28
Elliott Hughes1bac54f2012-03-16 12:48:31 -070029static void DdmVmInternal_enableRecentAllocations(JNIEnv*, jclass, jboolean enable) {
Elliott Hughes545a0642011-11-08 19:10:03 -080030 Dbg::SetAllocTrackingEnabled(enable);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -070031}
32
Elliott Hughes1bac54f2012-03-16 12:48:31 -070033static jbyteArray DdmVmInternal_getRecentAllocations(JNIEnv*, jclass) {
Elliott Hughes545a0642011-11-08 19:10:03 -080034 return Dbg::GetRecentAllocations();
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -070035}
36
Elliott Hughes1bac54f2012-03-16 12:48:31 -070037static jboolean DdmVmInternal_getRecentAllocationStatus(JNIEnv*, jclass) {
Elliott Hughes545a0642011-11-08 19:10:03 -080038 return Dbg::IsAllocTrackingEnabled();
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -070039}
40
Elliott Hughesbfe487b2011-10-26 15:48:55 -070041static Thread* FindThreadByThinLockId(uint32_t thin_lock_id) {
42 struct ThreadFinder {
Elliott Hughesba8eee12012-01-24 20:25:24 -080043 explicit ThreadFinder(uint32_t thin_lock_id) : thin_lock_id(thin_lock_id), thread(NULL) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -070044 }
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 Hughesf6a1e1e2011-10-25 16:28:04 -070061/*
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 Hughesbfe487b2011-10-26 15:48:55 -070065static jobjectArray DdmVmInternal_getStackTraceById(JNIEnv* env, jclass, jint thin_lock_id) {
Elliott Hughesffb465f2012-03-01 18:46:05 -080066 ScopedHeapLock heap_lock;
Elliott Hughesbbd9d832011-11-07 14:40:00 -080067 ScopedThreadListLock thread_list_lock;
Elliott Hughesbfe487b2011-10-26 15:48:55 -070068 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
76static void ThreadCountCallback(Thread*, void* context) {
77 uint16_t& count = *reinterpret_cast<uint16_t*>(context);
78 ++count;
79}
80
81static const int kThstBytesPerEntry = 18;
82static const int kThstHeaderLen = 4;
83
84static void ThreadStatsGetterCallback(Thread* t, void* context) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -070085 /*
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 Hughes21f32d72011-11-09 17:44:13 -080094 * (4b) thread id
Elliott Hughesbfe487b2011-10-26 15:48:55 -070095 * (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 Hughes21f32d72011-11-09 17:44:13 -0800109 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 Hughesf6a1e1e2011-10-25 16:28:04 -0700116}
117
118static jbyteArray DdmVmInternal_getThreadStats(JNIEnv* env, jclass) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700119 std::vector<uint8_t> bytes;
120 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800121 ScopedThreadListLock thread_list_lock;
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700122 ThreadList* thread_list = Runtime::Current()->GetThreadList();
123
Elliott Hughes21f32d72011-11-09 17:44:13 -0800124 uint16_t thread_count = 0;
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700125 thread_list->ForEach(ThreadCountCallback, &thread_count);
126
Elliott Hughes21f32d72011-11-09 17:44:13 -0800127 JDWP::Append1BE(bytes, kThstHeaderLen);
128 JDWP::Append1BE(bytes, kThstBytesPerEntry);
129 JDWP::Append2BE(bytes, thread_count);
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700130
Elliott Hughes21f32d72011-11-09 17:44:13 -0800131 thread_list->ForEach(ThreadStatsGetterCallback, &bytes);
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700132 }
133
134 jbyteArray result = env->NewByteArray(bytes.size());
Elliott Hughes545a0642011-11-08 19:10:03 -0800135 if (result != NULL) {
136 env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0]));
137 }
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700138 return result;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700139}
140
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700141static jint DdmVmInternal_heapInfoNotify(JNIEnv*, jclass, jint when) {
Elliott Hughes767a1472011-10-26 18:49:02 -0700142 return Dbg::DdmHandleHpifChunk(static_cast<Dbg::HpifWhen>(when));
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700143}
144
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700145static jboolean DdmVmInternal_heapSegmentNotify(JNIEnv*, jclass, jint when, jint what, jboolean native) {
Elliott Hughes767a1472011-10-26 18:49:02 -0700146 return Dbg::DdmHandleHpsgNhsgChunk(static_cast<Dbg::HpsgWhen>(when), static_cast<Dbg::HpsgWhat>(what), native);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700147}
148
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700149static void DdmVmInternal_threadNotify(JNIEnv*, jclass, jboolean enable) {
Elliott Hughes47fce012011-10-25 18:37:19 -0700150 Dbg::DdmSetThreadNotification(enable);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700151}
152
153static 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 Hughesf6a1e1e2011-10-25 16:28:04 -0700164void 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