blob: 9ce4a02f1b3f1e965e32a6bca8a67720c01a9e24 [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
Andreas Gampe277ccbd2014-11-03 21:36:10 -080017#include "org_apache_harmony_dalvik_ddmc_DdmVmInternal.h"
18
Elliott Hughes07ed66b2012-12-12 18:34:25 -080019#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080020#include "base/mutex.h"
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -070021#include "debugger.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070022#include "jni_internal.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070023#include "scoped_fast_native_object_access.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070024#include "ScopedLocalRef.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070025#include "ScopedPrimitiveArray.h"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070026#include "stack.h"
27#include "thread_list.h"
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -070028
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -070029namespace art {
30
Elliott Hughes1bac54f2012-03-16 12:48:31 -070031static void DdmVmInternal_enableRecentAllocations(JNIEnv*, jclass, jboolean enable) {
Elliott Hughes545a0642011-11-08 19:10:03 -080032 Dbg::SetAllocTrackingEnabled(enable);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -070033}
34
Ian Rogers00f7d0e2012-07-19 15:28:27 -070035static jbyteArray DdmVmInternal_getRecentAllocations(JNIEnv* env, jclass) {
Ian Rogers53b8b092014-03-13 23:45:53 -070036 ScopedFastNativeObjectAccess soa(env);
Elliott Hughes545a0642011-11-08 19:10:03 -080037 return Dbg::GetRecentAllocations();
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -070038}
39
Elliott Hughes1bac54f2012-03-16 12:48:31 -070040static jboolean DdmVmInternal_getRecentAllocationStatus(JNIEnv*, jclass) {
Man Cao8c2ff642015-05-27 17:25:30 -070041 return Runtime::Current()->GetHeap()->IsAllocTrackingEnabled();
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -070042}
43
44/*
45 * Get a stack trace as an array of StackTraceElement objects. Returns
Mathieu Chartier2cebb242015-04-21 16:50:40 -070046 * nullptr on failure, e.g. if the threadId couldn't be found.
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -070047 */
Elliott Hughesbfe487b2011-10-26 15:48:55 -070048static jobjectArray DdmVmInternal_getStackTraceById(JNIEnv* env, jclass, jint thin_lock_id) {
Ian Rogers53b8b092014-03-13 23:45:53 -070049 jobjectArray trace = nullptr;
Sebastien Hertz970c10e2014-03-25 17:53:48 +010050 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 Hertzee1d79a2014-02-21 15:46:30 +010054 jobject internal_trace = self->CreateInternalStackTrace<false>(soa);
Sebastien Hertz970c10e2014-03-25 17:53:48 +010055 trace = Thread::InternalStackTraceToStackTraceElementArray(soa, internal_trace);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070056 } else {
Sebastien Hertz970c10e2014-03-25 17:53:48 +010057 ThreadList* thread_list = Runtime::Current()->GetThreadList();
58 bool timed_out;
Johnnie Birch14e81442014-05-15 11:31:14 -070059
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 Rogers4ad5cd32014-11-11 23:08:07 -080066 Thread* thread = thread_list->SuspendThreadByThreadId(thin_lock_id, false, &timed_out);
Sebastien Hertz970c10e2014-03-25 17:53:48 +010067 if (thread != nullptr) {
68 {
69 ScopedObjectAccess soa(env);
Sebastien Hertzee1d79a2014-02-21 15:46:30 +010070 jobject internal_trace = thread->CreateInternalStackTrace<false>(soa);
Sebastien Hertz970c10e2014-03-25 17:53:48 +010071 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 Rogers15bf2d32012-08-28 17:33:04 -070080 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070081 }
Ian Rogers53b8b092014-03-13 23:45:53 -070082 return trace;
Elliott Hughesbfe487b2011-10-26 15:48:55 -070083}
84
85static void ThreadCountCallback(Thread*, void* context) {
86 uint16_t& count = *reinterpret_cast<uint16_t*>(context);
87 ++count;
88}
89
90static const int kThstBytesPerEntry = 18;
91static const int kThstHeaderLen = 4;
92
93static void ThreadStatsGetterCallback(Thread* t, void* context) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -070094 /*
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 Hughes21f32d72011-11-09 17:44:13 -0800103 * (4b) thread id
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700104 * (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 Hughesba0b9c52012-09-20 11:25:12 -0700115 char native_thread_state;
Brian Carlstrom29212012013-09-12 22:18:30 -0700116 int utime;
117 int stime;
118 int task_cpu;
119 GetTaskStats(t->GetTid(), &native_thread_state, &utime, &stime, &task_cpu);
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700120
Elliott Hughes21f32d72011-11-09 17:44:13 -0800121 std::vector<uint8_t>& bytes = *reinterpret_cast<std::vector<uint8_t>*>(context);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700122 JDWP::Append4BE(bytes, t->GetThreadId());
Jeff Hao920af3e2013-08-28 15:46:38 -0700123 JDWP::Append1BE(bytes, Dbg::ToJdwpThreadStatus(t->GetState()));
Elliott Hughes21f32d72011-11-09 17:44:13 -0800124 JDWP::Append4BE(bytes, t->GetTid());
125 JDWP::Append4BE(bytes, utime);
126 JDWP::Append4BE(bytes, stime);
127 JDWP::Append1BE(bytes, t->IsDaemon());
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700128}
129
130static jbyteArray DdmVmInternal_getThreadStats(JNIEnv* env, jclass) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700131 std::vector<uint8_t> bytes;
Ian Rogers50b35e22012-10-04 10:09:15 -0700132 Thread* self = static_cast<JNIEnvExt*>(env)->self;
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700133 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700134 MutexLock mu(self, *Locks::thread_list_lock_);
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700135 ThreadList* thread_list = Runtime::Current()->GetThreadList();
136
Elliott Hughes21f32d72011-11-09 17:44:13 -0800137 uint16_t thread_count = 0;
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700138 thread_list->ForEach(ThreadCountCallback, &thread_count);
139
Elliott Hughes21f32d72011-11-09 17:44:13 -0800140 JDWP::Append1BE(bytes, kThstHeaderLen);
141 JDWP::Append1BE(bytes, kThstBytesPerEntry);
142 JDWP::Append2BE(bytes, thread_count);
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700143
Elliott Hughes21f32d72011-11-09 17:44:13 -0800144 thread_list->ForEach(ThreadStatsGetterCallback, &bytes);
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700145 }
146
147 jbyteArray result = env->NewByteArray(bytes.size());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700148 if (result != nullptr) {
Elliott Hughes545a0642011-11-08 19:10:03 -0800149 env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0]));
150 }
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700151 return result;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700152}
153
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700154static jint DdmVmInternal_heapInfoNotify(JNIEnv* env, jclass, jint when) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700155 ScopedFastNativeObjectAccess soa(env);
Elliott Hughes767a1472011-10-26 18:49:02 -0700156 return Dbg::DdmHandleHpifChunk(static_cast<Dbg::HpifWhen>(when));
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700157}
158
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700159static jboolean DdmVmInternal_heapSegmentNotify(JNIEnv*, jclass, jint when, jint what, jboolean native) {
Elliott Hughes767a1472011-10-26 18:49:02 -0700160 return Dbg::DdmHandleHpsgNhsgChunk(static_cast<Dbg::HpsgWhen>(when), static_cast<Dbg::HpsgWhat>(what), native);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700161}
162
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700163static void DdmVmInternal_threadNotify(JNIEnv*, jclass, jboolean enable) {
Elliott Hughes47fce012011-10-25 18:37:19 -0700164 Dbg::DdmSetThreadNotification(enable);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700165}
166
167static JNINativeMethod gMethods[] = {
168 NATIVE_METHOD(DdmVmInternal, enableRecentAllocations, "(Z)V"),
Ian Rogers53b8b092014-03-13 23:45:53 -0700169 NATIVE_METHOD(DdmVmInternal, getRecentAllocations, "!()[B"),
170 NATIVE_METHOD(DdmVmInternal, getRecentAllocationStatus, "!()Z"),
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700171 NATIVE_METHOD(DdmVmInternal, getStackTraceById, "(I)[Ljava/lang/StackTraceElement;"),
172 NATIVE_METHOD(DdmVmInternal, getThreadStats, "()[B"),
Ian Rogers53b8b092014-03-13 23:45:53 -0700173 NATIVE_METHOD(DdmVmInternal, heapInfoNotify, "!(I)Z"),
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700174 NATIVE_METHOD(DdmVmInternal, heapSegmentNotify, "(IIZ)Z"),
175 NATIVE_METHOD(DdmVmInternal, threadNotify, "(Z)V"),
176};
177
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700178void register_org_apache_harmony_dalvik_ddmc_DdmVmInternal(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700179 REGISTER_NATIVE_METHODS("org/apache/harmony/dalvik/ddmc/DdmVmInternal");
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700180}
181
182} // namespace art