blob: 96c3e78a43ea9942414cdff5cfea00939cb3f7e9 [file] [log] [blame]
Elliott Hughes9d5ccec2011-09-19 13:19:50 -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
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070017#include <string.h>
18#include <unistd.h>
19
Elliott Hugheseac76672012-05-24 21:56:51 -070020#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080021#include "common_throws.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070022#include "debugger.h"
Hiroshi Yamauchi09b07a92013-07-15 13:17:06 -070023#include "gc/space/dlmalloc_space.h"
24#include "gc/space/large_object_space.h"
25#include "gc/space/space-inl.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070026#include "hprof/hprof.h"
27#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/class.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070029#include "ScopedUtfChars.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070030#include "scoped_thread_state_change.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070031#include "toStringArray.h"
32#include "trace.h"
33
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070034namespace art {
35
Elliott Hughes0512f022012-03-15 22:10:52 -070036static jobjectArray VMDebug_getVmFeatureList(JNIEnv* env, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070037 std::vector<std::string> features;
jeffhaoe343b762011-12-05 16:36:44 -080038 features.push_back("method-trace-profiling");
39 features.push_back("method-trace-profiling-streaming");
Jeff Hao23009dc2013-08-22 15:36:42 -070040 features.push_back("method-sample-profiling");
Elliott Hughes767a1472011-10-26 18:49:02 -070041 features.push_back("hprof-heap-dump");
42 features.push_back("hprof-heap-dump-streaming");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070043 return toStringArray(env, features);
44}
45
Elliott Hughes0512f022012-03-15 22:10:52 -070046static void VMDebug_startAllocCounting(JNIEnv*, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070047 Runtime::Current()->SetStatsEnabled(true);
48}
49
Elliott Hughes0512f022012-03-15 22:10:52 -070050static void VMDebug_stopAllocCounting(JNIEnv*, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070051 Runtime::Current()->SetStatsEnabled(false);
52}
53
Elliott Hughes1bac54f2012-03-16 12:48:31 -070054static jint VMDebug_getAllocCount(JNIEnv*, jclass, jint kind) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070055 return Runtime::Current()->GetStat(kind);
56}
57
Elliott Hughes0512f022012-03-15 22:10:52 -070058static void VMDebug_resetAllocCount(JNIEnv*, jclass, jint kinds) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070059 Runtime::Current()->ResetStats(kinds);
60}
61
Jeff Hao23009dc2013-08-22 15:36:42 -070062static void VMDebug_startMethodTracingDdmsImpl(JNIEnv*, jclass, jint bufferSize, jint flags,
63 jboolean samplingEnabled, jint intervalUs) {
64 Trace::Start("[DDMS]", -1, bufferSize, flags, true, samplingEnabled, intervalUs);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070065}
66
Ian Rogers00f7d0e2012-07-19 15:28:27 -070067static void VMDebug_startMethodTracingFd(JNIEnv* env, jclass, jstring javaTraceFilename,
68 jobject javaFd, jint bufferSize, jint flags) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070069 int originalFd = jniGetFDFromFileDescriptor(env, javaFd);
70 if (originalFd < 0) {
71 return;
72 }
73
74 int fd = dup(originalFd);
75 if (fd < 0) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070076 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -080077 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
78 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/RuntimeException;",
79 "dup(%d) failed: %s", originalFd, strerror(errno));
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070080 return;
81 }
82
83 ScopedUtfChars traceFilename(env, javaTraceFilename);
84 if (traceFilename.c_str() == NULL) {
85 return;
86 }
Jeff Hao23009dc2013-08-22 15:36:42 -070087 Trace::Start(traceFilename.c_str(), fd, bufferSize, flags, false, false, 0);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070088}
89
Ian Rogers00f7d0e2012-07-19 15:28:27 -070090static void VMDebug_startMethodTracingFilename(JNIEnv* env, jclass, jstring javaTraceFilename,
91 jint bufferSize, jint flags) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070092 ScopedUtfChars traceFilename(env, javaTraceFilename);
93 if (traceFilename.c_str() == NULL) {
94 return;
95 }
Jeff Hao23009dc2013-08-22 15:36:42 -070096 Trace::Start(traceFilename.c_str(), -1, bufferSize, flags, false, false, 0);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070097}
98
Jeff Hao64caa7d2013-08-29 11:18:01 -070099static jint VMDebug_getMethodTracingMode(JNIEnv*, jclass) {
100 return Trace::GetMethodTracingMode();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700101}
102
Elliott Hughes0512f022012-03-15 22:10:52 -0700103static void VMDebug_stopMethodTracing(JNIEnv*, jclass) {
jeffhaoe343b762011-12-05 16:36:44 -0800104 Trace::Stop();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700105}
106
Elliott Hughes0512f022012-03-15 22:10:52 -0700107static void VMDebug_startEmulatorTracing(JNIEnv*, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700108 UNIMPLEMENTED(WARNING);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700109 // dvmEmulatorTraceStart();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700110}
111
Elliott Hughes0512f022012-03-15 22:10:52 -0700112static void VMDebug_stopEmulatorTracing(JNIEnv*, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700113 UNIMPLEMENTED(WARNING);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700114 // dvmEmulatorTraceStop();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700115}
116
Elliott Hughes0512f022012-03-15 22:10:52 -0700117static jboolean VMDebug_isDebuggerConnected(JNIEnv*, jclass) {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700118 return Dbg::IsDebuggerActive();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700119}
120
Elliott Hughes0512f022012-03-15 22:10:52 -0700121static jboolean VMDebug_isDebuggingEnabled(JNIEnv*, jclass) {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700122 return Dbg::IsJdwpConfigured();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700123}
124
Elliott Hughes0512f022012-03-15 22:10:52 -0700125static jlong VMDebug_lastDebuggerActivity(JNIEnv*, jclass) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700126 return Dbg::LastDebuggerActivity();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700127}
128
Ian Rogers62d6c772013-02-27 08:32:07 -0800129static void ThrowUnsupportedOperationException(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700130 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800131 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
132 soa.Self()->ThrowNewException(throw_location, "Ljava/lang/UnsupportedOperationException;", NULL);
133}
134
135static void VMDebug_startInstructionCounting(JNIEnv* env, jclass) {
136 ThrowUnsupportedOperationException(env);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700137}
138
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700139static void VMDebug_stopInstructionCounting(JNIEnv* env, jclass) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800140 ThrowUnsupportedOperationException(env);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700141}
142
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700143static void VMDebug_getInstructionCount(JNIEnv* env, jclass, jintArray /*javaCounts*/) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800144 ThrowUnsupportedOperationException(env);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700145}
146
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700147static void VMDebug_resetInstructionCount(JNIEnv* env, jclass) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800148 ThrowUnsupportedOperationException(env);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700149}
150
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700151static void VMDebug_printLoadedClasses(JNIEnv* env, jclass, jint flags) {
152 ScopedObjectAccess soa(env);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700153 return Runtime::Current()->GetClassLinker()->DumpAllClasses(flags);
154}
155
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700156static jint VMDebug_getLoadedClassCount(JNIEnv* env, jclass) {
157 ScopedObjectAccess soa(env);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700158 return Runtime::Current()->GetClassLinker()->NumLoadedClasses();
159}
160
161/*
162 * Returns the thread-specific CPU-time clock value for the current thread,
163 * or -1 if the feature isn't supported.
164 */
Elliott Hughes0512f022012-03-15 22:10:52 -0700165static jlong VMDebug_threadCpuTimeNanos(JNIEnv*, jclass) {
166 return ThreadCpuNanoTime();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700167}
168
169/*
170 * static void dumpHprofData(String fileName, FileDescriptor fd)
171 *
172 * Cause "hprof" data to be dumped. We can throw an IOException if an
173 * error occurs during file handling.
174 */
Elliott Hughes0512f022012-03-15 22:10:52 -0700175static void VMDebug_dumpHprofData(JNIEnv* env, jclass, jstring javaFilename, jobject javaFd) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700176 // Only one of these may be NULL.
177 if (javaFilename == NULL && javaFd == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700178 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800179 ThrowNullPointerException(NULL, "fileName == null && fd == null");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700180 return;
181 }
182
183 std::string filename;
184 if (javaFilename != NULL) {
185 ScopedUtfChars chars(env, javaFilename);
186 if (env->ExceptionCheck()) {
187 return;
188 }
189 filename = chars.c_str();
190 } else {
191 filename = "[fd]";
192 }
193
194 int fd = -1;
195 if (javaFd != NULL) {
196 fd = jniGetFDFromFileDescriptor(env, javaFd);
197 if (fd < 0) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700198 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800199 ThrowRuntimeException("Invalid file descriptor");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700200 return;
201 }
202 }
203
Elliott Hughes622a6982012-06-08 17:58:54 -0700204 hprof::DumpHeap(filename.c_str(), fd, false);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700205}
206
Elliott Hugheseac76672012-05-24 21:56:51 -0700207static void VMDebug_dumpHprofDataDdms(JNIEnv*, jclass) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700208 hprof::DumpHeap("[DDMS]", -1, true);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700209}
210
Elliott Hughes0512f022012-03-15 22:10:52 -0700211static void VMDebug_dumpReferenceTables(JNIEnv* env, jclass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700212 ScopedObjectAccess soa(env);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700213 LOG(INFO) << "--- reference table dump ---";
214
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700215 soa.Env()->DumpReferenceTables(LOG(INFO));
216 soa.Vm()->DumpReferenceTables(LOG(INFO));
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700217
218 LOG(INFO) << "---";
219}
220
Elliott Hughes0512f022012-03-15 22:10:52 -0700221static void VMDebug_crash(JNIEnv*, jclass) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700222 LOG(FATAL) << "Crashing runtime on request";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700223}
224
Elliott Hughes0512f022012-03-15 22:10:52 -0700225static void VMDebug_infopoint(JNIEnv*, jclass, jint id) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700226 LOG(INFO) << "VMDebug infopoint " << id << " hit";
227}
228
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700229static jlong VMDebug_countInstancesOfClass(JNIEnv* env, jclass, jclass javaClass,
230 jboolean countAssignable) {
231 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800232 mirror::Class* c = soa.Decode<mirror::Class*>(javaClass);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700233 if (c == NULL) {
234 return 0;
235 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800236 std::vector<mirror::Class*> classes;
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800237 classes.push_back(c);
238 uint64_t count = 0;
239 Runtime::Current()->GetHeap()->CountInstances(classes, countAssignable, &count);
240 return count;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700241}
242
Hiroshi Yamauchi09b07a92013-07-15 13:17:06 -0700243// We export the VM internal per-heap-space size/alloc/free metrics
244// for the zygote space, alloc space (application heap), and the large
245// object space for dumpsys meminfo. The other memory region data such
246// as PSS, private/shared dirty/shared data are available via
247// /proc/<pid>/smaps.
248static void VMDebug_getHeapSpaceStats(JNIEnv* env, jclass, jlongArray data) {
Ian Rogers6b99dd12013-07-25 12:11:32 -0700249 jlong* arr = reinterpret_cast<jlong*>(env->GetPrimitiveArrayCritical(data, 0));
Hiroshi Yamauchi09b07a92013-07-15 13:17:06 -0700250 if (arr == NULL || env->GetArrayLength(data) < 9) {
251 return;
252 }
253
254 size_t allocSize = 0;
255 size_t allocUsed = 0;
256 size_t zygoteSize = 0;
257 size_t zygoteUsed = 0;
258 size_t largeObjectsSize = 0;
259 size_t largeObjectsUsed = 0;
260
261 gc::Heap* heap = Runtime::Current()->GetHeap();
262 const std::vector<gc::space::ContinuousSpace*>& continuous_spaces = heap->GetContinuousSpaces();
263 const std::vector<gc::space::DiscontinuousSpace*>& discontinuous_spaces = heap->GetDiscontinuousSpaces();
264 typedef std::vector<gc::space::ContinuousSpace*>::const_iterator It;
265 for (It it = continuous_spaces.begin(), end = continuous_spaces.end(); it != end; ++it) {
266 gc::space::ContinuousSpace* space = *it;
267 if (space->IsImageSpace()) {
268 // Currently don't include the image space.
269 } else if (space->IsZygoteSpace()) {
270 gc::space::DlMallocSpace* dlmalloc_space = space->AsDlMallocSpace();
271 zygoteSize += dlmalloc_space->GetFootprint();
272 zygoteUsed += dlmalloc_space->GetBytesAllocated();
273 } else {
274 // This is the alloc space.
275 gc::space::DlMallocSpace* dlmalloc_space = space->AsDlMallocSpace();
276 allocSize += dlmalloc_space->GetFootprint();
277 allocUsed += dlmalloc_space->GetBytesAllocated();
278 }
279 }
280 typedef std::vector<gc::space::DiscontinuousSpace*>::const_iterator It2;
281 for (It2 it = discontinuous_spaces.begin(), end = discontinuous_spaces.end(); it != end; ++it) {
282 gc::space::DiscontinuousSpace* space = *it;
283 if (space->IsLargeObjectSpace()) {
284 largeObjectsSize += space->AsLargeObjectSpace()->GetBytesAllocated();
285 largeObjectsUsed += largeObjectsSize;
286 }
287 }
288
289 size_t allocFree = allocSize - allocUsed;
290 size_t zygoteFree = zygoteSize - zygoteUsed;
291 size_t largeObjectsFree = largeObjectsSize - largeObjectsUsed;
292
293 int j = 0;
294 arr[j++] = allocSize;
295 arr[j++] = allocUsed;
296 arr[j++] = allocFree;
297 arr[j++] = zygoteSize;
298 arr[j++] = zygoteUsed;
299 arr[j++] = zygoteFree;
300 arr[j++] = largeObjectsSize;
301 arr[j++] = largeObjectsUsed;
302 arr[j++] = largeObjectsFree;
303 env->ReleasePrimitiveArrayCritical(data, arr, 0);
304}
305
Elliott Hughes0512f022012-03-15 22:10:52 -0700306static JNINativeMethod gMethods[] = {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700307 NATIVE_METHOD(VMDebug, countInstancesOfClass, "(Ljava/lang/Class;Z)J"),
308 NATIVE_METHOD(VMDebug, crash, "()V"),
309 NATIVE_METHOD(VMDebug, dumpHprofData, "(Ljava/lang/String;Ljava/io/FileDescriptor;)V"),
310 NATIVE_METHOD(VMDebug, dumpHprofDataDdms, "()V"),
311 NATIVE_METHOD(VMDebug, dumpReferenceTables, "()V"),
312 NATIVE_METHOD(VMDebug, getAllocCount, "(I)I"),
Hiroshi Yamauchi09b07a92013-07-15 13:17:06 -0700313 NATIVE_METHOD(VMDebug, getHeapSpaceStats, "([J)V"),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700314 NATIVE_METHOD(VMDebug, getInstructionCount, "([I)V"),
315 NATIVE_METHOD(VMDebug, getLoadedClassCount, "()I"),
316 NATIVE_METHOD(VMDebug, getVmFeatureList, "()[Ljava/lang/String;"),
317 NATIVE_METHOD(VMDebug, infopoint, "(I)V"),
318 NATIVE_METHOD(VMDebug, isDebuggerConnected, "()Z"),
319 NATIVE_METHOD(VMDebug, isDebuggingEnabled, "()Z"),
Jeff Hao64caa7d2013-08-29 11:18:01 -0700320 NATIVE_METHOD(VMDebug, getMethodTracingMode, "()I"),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700321 NATIVE_METHOD(VMDebug, lastDebuggerActivity, "()J"),
322 NATIVE_METHOD(VMDebug, printLoadedClasses, "(I)V"),
323 NATIVE_METHOD(VMDebug, resetAllocCount, "(I)V"),
324 NATIVE_METHOD(VMDebug, resetInstructionCount, "()V"),
325 NATIVE_METHOD(VMDebug, startAllocCounting, "()V"),
326 NATIVE_METHOD(VMDebug, startEmulatorTracing, "()V"),
327 NATIVE_METHOD(VMDebug, startInstructionCounting, "()V"),
Jeff Hao23009dc2013-08-22 15:36:42 -0700328 NATIVE_METHOD(VMDebug, startMethodTracingDdmsImpl, "(IIZI)V"),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700329 NATIVE_METHOD(VMDebug, startMethodTracingFd, "(Ljava/lang/String;Ljava/io/FileDescriptor;II)V"),
330 NATIVE_METHOD(VMDebug, startMethodTracingFilename, "(Ljava/lang/String;II)V"),
331 NATIVE_METHOD(VMDebug, stopAllocCounting, "()V"),
332 NATIVE_METHOD(VMDebug, stopEmulatorTracing, "()V"),
333 NATIVE_METHOD(VMDebug, stopInstructionCounting, "()V"),
334 NATIVE_METHOD(VMDebug, stopMethodTracing, "()V"),
335 NATIVE_METHOD(VMDebug, threadCpuTimeNanos, "()J"),
336};
337
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700338void register_dalvik_system_VMDebug(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700339 REGISTER_NATIVE_METHODS("dalvik/system/VMDebug");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700340}
341
342} // namespace art