blob: c5db4f62b9b877233f0b9722955ede4627ef907b [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 Hughesbfe487b2011-10-26 15:48:55 -070020#include "stack.h"
21#include "thread_list.h"
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -070022
23#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
24#include "ScopedPrimitiveArray.h" // Last to avoid problems with LOG redefinition.
25
26namespace art {
27
28namespace {
29
30static void DdmVmInternal_enableRecentAllocations(JNIEnv* env, jclass, jboolean enable) {
Elliott Hughes545a0642011-11-08 19:10:03 -080031 Dbg::SetAllocTrackingEnabled(enable);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -070032}
33
34static jbyteArray DdmVmInternal_getRecentAllocations(JNIEnv* env, jclass) {
Elliott Hughes545a0642011-11-08 19:10:03 -080035 return Dbg::GetRecentAllocations();
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -070036}
37
38static jboolean DdmVmInternal_getRecentAllocationStatus(JNIEnv* env, jclass) {
Elliott Hughes545a0642011-11-08 19:10:03 -080039 return Dbg::IsAllocTrackingEnabled();
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -070040}
41
Elliott Hughesbfe487b2011-10-26 15:48:55 -070042static Thread* FindThreadByThinLockId(uint32_t thin_lock_id) {
43 struct ThreadFinder {
Elliott Hughesba8eee12012-01-24 20:25:24 -080044 explicit ThreadFinder(uint32_t thin_lock_id) : thin_lock_id(thin_lock_id), thread(NULL) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -070045 }
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 Hughesf6a1e1e2011-10-25 16:28:04 -070062/*
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 Hughesbfe487b2011-10-26 15:48:55 -070066static jobjectArray DdmVmInternal_getStackTraceById(JNIEnv* env, jclass, jint thin_lock_id) {
Elliott Hughesffb465f2012-03-01 18:46:05 -080067 ScopedHeapLock heap_lock;
Elliott Hughesbbd9d832011-11-07 14:40:00 -080068 ScopedThreadListLock thread_list_lock;
Elliott Hughesbfe487b2011-10-26 15:48:55 -070069 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
77static void ThreadCountCallback(Thread*, void* context) {
78 uint16_t& count = *reinterpret_cast<uint16_t*>(context);
79 ++count;
80}
81
82static const int kThstBytesPerEntry = 18;
83static const int kThstHeaderLen = 4;
84
85static void ThreadStatsGetterCallback(Thread* t, void* context) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -070086 /*
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 Hughes21f32d72011-11-09 17:44:13 -080095 * (4b) thread id
Elliott Hughesbfe487b2011-10-26 15:48:55 -070096 * (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 Hughes21f32d72011-11-09 17:44:13 -0800110 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 Hughesf6a1e1e2011-10-25 16:28:04 -0700117}
118
119static jbyteArray DdmVmInternal_getThreadStats(JNIEnv* env, jclass) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700120 std::vector<uint8_t> bytes;
121 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800122 ScopedThreadListLock thread_list_lock;
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700123 ThreadList* thread_list = Runtime::Current()->GetThreadList();
124
Elliott Hughes21f32d72011-11-09 17:44:13 -0800125 uint16_t thread_count = 0;
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700126 thread_list->ForEach(ThreadCountCallback, &thread_count);
127
Elliott Hughes21f32d72011-11-09 17:44:13 -0800128 JDWP::Append1BE(bytes, kThstHeaderLen);
129 JDWP::Append1BE(bytes, kThstBytesPerEntry);
130 JDWP::Append2BE(bytes, thread_count);
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700131
Elliott Hughes21f32d72011-11-09 17:44:13 -0800132 thread_list->ForEach(ThreadStatsGetterCallback, &bytes);
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700133 }
134
135 jbyteArray result = env->NewByteArray(bytes.size());
Elliott Hughes545a0642011-11-08 19:10:03 -0800136 if (result != NULL) {
137 env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0]));
138 }
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700139 return result;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700140}
141
142static jint DdmVmInternal_heapInfoNotify(JNIEnv* env, jclass, jint when) {
Elliott Hughes767a1472011-10-26 18:49:02 -0700143 return Dbg::DdmHandleHpifChunk(static_cast<Dbg::HpifWhen>(when));
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700144}
145
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700146static jboolean DdmVmInternal_heapSegmentNotify(JNIEnv* env, jclass, jint when, jint what, jboolean native) {
Elliott Hughes767a1472011-10-26 18:49:02 -0700147 return Dbg::DdmHandleHpsgNhsgChunk(static_cast<Dbg::HpsgWhen>(when), static_cast<Dbg::HpsgWhat>(what), native);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700148}
149
150static void DdmVmInternal_threadNotify(JNIEnv* env, jclass, jboolean enable) {
Elliott Hughes47fce012011-10-25 18:37:19 -0700151 Dbg::DdmSetThreadNotification(enable);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700152}
153
154static 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
167void 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