dalvik_system_VMDebug.cc revision 64caa7dcf46ed6139b766dbe77fbd7353899417f
19d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes/*
29d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes * Copyright (C) 2008 The Android Open Source Project
39d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes *
49d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
59d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes * you may not use this file except in compliance with the License.
69d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes * You may obtain a copy of the License at
79d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes *
89d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
99d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes *
109d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes * Unless required by applicable law or agreed to in writing, software
119d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
129d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes * See the License for the specific language governing permissions and
149d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes * limitations under the License.
159d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes */
169d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
17eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes#include <string.h>
18eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes#include <unistd.h>
19eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes
209d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes#include "class_linker.h"
2162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers#include "common_throws.h"
22872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes#include "debugger.h"
2309b07a96094086e205948717666025909a75163bHiroshi Yamauchi#include "gc/space/dlmalloc_space.h"
2409b07a96094086e205948717666025909a75163bHiroshi Yamauchi#include "gc/space/large_object_space.h"
2509b07a96094086e205948717666025909a75163bHiroshi Yamauchi#include "gc/space/space-inl.h"
261121e0b619d0c9df36c57396b2234a9d570b4923Jesse Wilson#include "hprof/hprof.h"
27eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes#include "jni_internal.h"
282dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class.h"
299d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes#include "ScopedUtfChars.h"
3000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "scoped_thread_state_change.h"
319d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes#include "toStringArray.h"
32eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes#include "trace.h"
339d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
349d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughesnamespace art {
359d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
360512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic jobjectArray VMDebug_getVmFeatureList(JNIEnv* env, jclass) {
379d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  std::vector<std::string> features;
38e343b76af81a005ef64f5e75a555389fd9147dabjeffhao  features.push_back("method-trace-profiling");
39e343b76af81a005ef64f5e75a555389fd9147dabjeffhao  features.push_back("method-trace-profiling-streaming");
4023009dca63c1699e28bfeaa8b45ca48fa0e86aceJeff Hao  features.push_back("method-sample-profiling");
41767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  features.push_back("hprof-heap-dump");
42767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  features.push_back("hprof-heap-dump-streaming");
439d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  return toStringArray(env, features);
449d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
459d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
460512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void VMDebug_startAllocCounting(JNIEnv*, jclass) {
479d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  Runtime::Current()->SetStatsEnabled(true);
489d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
499d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
500512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void VMDebug_stopAllocCounting(JNIEnv*, jclass) {
519d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  Runtime::Current()->SetStatsEnabled(false);
529d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
539d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
541bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughesstatic jint VMDebug_getAllocCount(JNIEnv*, jclass, jint kind) {
559d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  return Runtime::Current()->GetStat(kind);
569d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
579d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
580512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void VMDebug_resetAllocCount(JNIEnv*, jclass, jint kinds) {
599d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  Runtime::Current()->ResetStats(kinds);
609d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
619d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
6223009dca63c1699e28bfeaa8b45ca48fa0e86aceJeff Haostatic void VMDebug_startMethodTracingDdmsImpl(JNIEnv*, jclass, jint bufferSize, jint flags,
6323009dca63c1699e28bfeaa8b45ca48fa0e86aceJeff Hao                                               jboolean samplingEnabled, jint intervalUs) {
6423009dca63c1699e28bfeaa8b45ca48fa0e86aceJeff Hao  Trace::Start("[DDMS]", -1, bufferSize, flags, true, samplingEnabled, intervalUs);
659d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
669d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
6700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic void VMDebug_startMethodTracingFd(JNIEnv* env, jclass, jstring javaTraceFilename,
6800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                         jobject javaFd, jint bufferSize, jint flags) {
699d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  int originalFd = jniGetFDFromFileDescriptor(env, javaFd);
709d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  if (originalFd < 0) {
719d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    return;
729d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  }
739d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
749d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  int fd = dup(originalFd);
759d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  if (fd < 0) {
7600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(env);
7762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
7862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/RuntimeException;",
7962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                                   "dup(%d) failed: %s", originalFd, strerror(errno));
809d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    return;
819d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  }
829d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
839d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  ScopedUtfChars traceFilename(env, javaTraceFilename);
849d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  if (traceFilename.c_str() == NULL) {
859d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    return;
869d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  }
8723009dca63c1699e28bfeaa8b45ca48fa0e86aceJeff Hao  Trace::Start(traceFilename.c_str(), fd, bufferSize, flags, false, false, 0);
889d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
899d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
9000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic void VMDebug_startMethodTracingFilename(JNIEnv* env, jclass, jstring javaTraceFilename,
9100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                               jint bufferSize, jint flags) {
929d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  ScopedUtfChars traceFilename(env, javaTraceFilename);
939d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  if (traceFilename.c_str() == NULL) {
949d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    return;
959d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  }
9623009dca63c1699e28bfeaa8b45ca48fa0e86aceJeff Hao  Trace::Start(traceFilename.c_str(), -1, bufferSize, flags, false, false, 0);
979d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
989d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
9964caa7dcf46ed6139b766dbe77fbd7353899417fJeff Haostatic jint VMDebug_getMethodTracingMode(JNIEnv*, jclass) {
10064caa7dcf46ed6139b766dbe77fbd7353899417fJeff Hao  return Trace::GetMethodTracingMode();
1019d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
1029d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
1030512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void VMDebug_stopMethodTracing(JNIEnv*, jclass) {
104e343b76af81a005ef64f5e75a555389fd9147dabjeffhao  Trace::Stop();
1059d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
1069d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
1070512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void VMDebug_startEmulatorTracing(JNIEnv*, jclass) {
1089d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  UNIMPLEMENTED(WARNING);
1097934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  // dvmEmulatorTraceStart();
1109d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
1119d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
1120512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void VMDebug_stopEmulatorTracing(JNIEnv*, jclass) {
1139d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  UNIMPLEMENTED(WARNING);
1147934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  // dvmEmulatorTraceStop();
1159d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
1169d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
1170512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic jboolean VMDebug_isDebuggerConnected(JNIEnv*, jclass) {
118c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  return Dbg::IsDebuggerActive();
1199d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
1209d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
1210512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic jboolean VMDebug_isDebuggingEnabled(JNIEnv*, jclass) {
122c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  return Dbg::IsJdwpConfigured();
1239d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
1249d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
1250512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic jlong VMDebug_lastDebuggerActivity(JNIEnv*, jclass) {
126872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes  return Dbg::LastDebuggerActivity();
1279d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
1289d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
12962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogersstatic void ThrowUnsupportedOperationException(JNIEnv* env) {
13000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(env);
13162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
13262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  soa.Self()->ThrowNewException(throw_location, "Ljava/lang/UnsupportedOperationException;", NULL);
13362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers}
13462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
13562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogersstatic void VMDebug_startInstructionCounting(JNIEnv* env, jclass) {
13662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  ThrowUnsupportedOperationException(env);
1379d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
1389d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
13900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic void VMDebug_stopInstructionCounting(JNIEnv* env, jclass) {
14062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  ThrowUnsupportedOperationException(env);
1419d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
1429d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
14300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic void VMDebug_getInstructionCount(JNIEnv* env, jclass, jintArray /*javaCounts*/) {
14462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  ThrowUnsupportedOperationException(env);
1459d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
1469d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
14700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic void VMDebug_resetInstructionCount(JNIEnv* env, jclass) {
14862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  ThrowUnsupportedOperationException(env);
1499d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
1509d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
15100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic void VMDebug_printLoadedClasses(JNIEnv* env, jclass, jint flags) {
15200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(env);
1539d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  return Runtime::Current()->GetClassLinker()->DumpAllClasses(flags);
1549d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
1559d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
1567dfb28c066159e6cde8181720f0c451a700ef966Ian Rogersstatic jint VMDebug_getLoadedClassCount(JNIEnv* env, jclass) {
1577dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers  ScopedObjectAccess soa(env);
1589d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  return Runtime::Current()->GetClassLinker()->NumLoadedClasses();
1599d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
1609d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
1619d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes/*
1629d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes * Returns the thread-specific CPU-time clock value for the current thread,
1639d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes * or -1 if the feature isn't supported.
1649d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes */
1650512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic jlong VMDebug_threadCpuTimeNanos(JNIEnv*, jclass) {
1660512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughes  return ThreadCpuNanoTime();
1679d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
1689d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
1699d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes/*
1709d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes * static void dumpHprofData(String fileName, FileDescriptor fd)
1719d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes *
1729d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes * Cause "hprof" data to be dumped.  We can throw an IOException if an
1739d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes * error occurs during file handling.
1749d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes */
1750512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void VMDebug_dumpHprofData(JNIEnv* env, jclass, jstring javaFilename, jobject javaFd) {
1769d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  // Only one of these may be NULL.
1779d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  if (javaFilename == NULL && javaFd == NULL) {
17800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(env);
17962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    ThrowNullPointerException(NULL, "fileName == null && fd == null");
1809d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    return;
1819d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  }
1829d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
1839d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  std::string filename;
1849d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  if (javaFilename != NULL) {
1859d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    ScopedUtfChars chars(env, javaFilename);
1869d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    if (env->ExceptionCheck()) {
1879d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes      return;
1889d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    }
1899d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    filename = chars.c_str();
1909d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  } else {
1919d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    filename = "[fd]";
1929d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  }
1939d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
1949d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  int fd = -1;
1959d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  if (javaFd != NULL) {
1969d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    fd = jniGetFDFromFileDescriptor(env, javaFd);
1979d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    if (fd < 0) {
19800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      ScopedObjectAccess soa(env);
19962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      ThrowRuntimeException("Invalid file descriptor");
2009d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes      return;
2019d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    }
2029d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  }
2039d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
204622a6988351d77da0008142f4ce1ea447d838556Elliott Hughes  hprof::DumpHeap(filename.c_str(), fd, false);
2059d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
2069d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
207eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughesstatic void VMDebug_dumpHprofDataDdms(JNIEnv*, jclass) {
208622a6988351d77da0008142f4ce1ea447d838556Elliott Hughes  hprof::DumpHeap("[DDMS]", -1, true);
2099d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
2109d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
2110512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void VMDebug_dumpReferenceTables(JNIEnv* env, jclass) {
21200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(env);
2139d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  LOG(INFO) << "--- reference table dump ---";
2149d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
21500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  soa.Env()->DumpReferenceTables(LOG(INFO));
21600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  soa.Vm()->DumpReferenceTables(LOG(INFO));
2179d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
2189d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  LOG(INFO) << "---";
2199d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
2209d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
2210512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void VMDebug_crash(JNIEnv*, jclass) {
22281ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes  LOG(FATAL) << "Crashing runtime on request";
2239d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
2249d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
2250512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void VMDebug_infopoint(JNIEnv*, jclass, jint id) {
2269d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  LOG(INFO) << "VMDebug infopoint " << id << " hit";
2279d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
2289d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
22900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic jlong VMDebug_countInstancesOfClass(JNIEnv* env, jclass, jclass javaClass,
23000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                           jboolean countAssignable) {
23100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(env);
2322dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = soa.Decode<mirror::Class*>(javaClass);
2339d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  if (c == NULL) {
2349d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    return 0;
2359d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  }
2362dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Class*> classes;
237ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  classes.push_back(c);
238ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  uint64_t count = 0;
239ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  Runtime::Current()->GetHeap()->CountInstances(classes, countAssignable, &count);
240ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  return count;
2419d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
2429d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
24309b07a96094086e205948717666025909a75163bHiroshi Yamauchi// We export the VM internal per-heap-space size/alloc/free metrics
24409b07a96094086e205948717666025909a75163bHiroshi Yamauchi// for the zygote space, alloc space (application heap), and the large
24509b07a96094086e205948717666025909a75163bHiroshi Yamauchi// object space for dumpsys meminfo. The other memory region data such
24609b07a96094086e205948717666025909a75163bHiroshi Yamauchi// as PSS, private/shared dirty/shared data are available via
24709b07a96094086e205948717666025909a75163bHiroshi Yamauchi// /proc/<pid>/smaps.
24809b07a96094086e205948717666025909a75163bHiroshi Yamauchistatic void VMDebug_getHeapSpaceStats(JNIEnv* env, jclass, jlongArray data) {
2496b99dd10d8a4869bbf7e9263a350185e5ebe32e4Ian Rogers  jlong* arr = reinterpret_cast<jlong*>(env->GetPrimitiveArrayCritical(data, 0));
25009b07a96094086e205948717666025909a75163bHiroshi Yamauchi  if (arr == NULL || env->GetArrayLength(data) < 9) {
25109b07a96094086e205948717666025909a75163bHiroshi Yamauchi    return;
25209b07a96094086e205948717666025909a75163bHiroshi Yamauchi  }
25309b07a96094086e205948717666025909a75163bHiroshi Yamauchi
25409b07a96094086e205948717666025909a75163bHiroshi Yamauchi  size_t allocSize = 0;
25509b07a96094086e205948717666025909a75163bHiroshi Yamauchi  size_t allocUsed = 0;
25609b07a96094086e205948717666025909a75163bHiroshi Yamauchi  size_t zygoteSize = 0;
25709b07a96094086e205948717666025909a75163bHiroshi Yamauchi  size_t zygoteUsed = 0;
25809b07a96094086e205948717666025909a75163bHiroshi Yamauchi  size_t largeObjectsSize = 0;
25909b07a96094086e205948717666025909a75163bHiroshi Yamauchi  size_t largeObjectsUsed = 0;
26009b07a96094086e205948717666025909a75163bHiroshi Yamauchi
26109b07a96094086e205948717666025909a75163bHiroshi Yamauchi  gc::Heap* heap = Runtime::Current()->GetHeap();
26209b07a96094086e205948717666025909a75163bHiroshi Yamauchi  const std::vector<gc::space::ContinuousSpace*>& continuous_spaces = heap->GetContinuousSpaces();
26309b07a96094086e205948717666025909a75163bHiroshi Yamauchi  const std::vector<gc::space::DiscontinuousSpace*>& discontinuous_spaces = heap->GetDiscontinuousSpaces();
26409b07a96094086e205948717666025909a75163bHiroshi Yamauchi  typedef std::vector<gc::space::ContinuousSpace*>::const_iterator It;
26509b07a96094086e205948717666025909a75163bHiroshi Yamauchi  for (It it = continuous_spaces.begin(), end = continuous_spaces.end(); it != end; ++it) {
26609b07a96094086e205948717666025909a75163bHiroshi Yamauchi    gc::space::ContinuousSpace* space = *it;
26709b07a96094086e205948717666025909a75163bHiroshi Yamauchi    if (space->IsImageSpace()) {
26809b07a96094086e205948717666025909a75163bHiroshi Yamauchi      // Currently don't include the image space.
26909b07a96094086e205948717666025909a75163bHiroshi Yamauchi    } else if (space->IsZygoteSpace()) {
27009b07a96094086e205948717666025909a75163bHiroshi Yamauchi      gc::space::DlMallocSpace* dlmalloc_space = space->AsDlMallocSpace();
27109b07a96094086e205948717666025909a75163bHiroshi Yamauchi      zygoteSize += dlmalloc_space->GetFootprint();
27209b07a96094086e205948717666025909a75163bHiroshi Yamauchi      zygoteUsed += dlmalloc_space->GetBytesAllocated();
27309b07a96094086e205948717666025909a75163bHiroshi Yamauchi    } else {
27409b07a96094086e205948717666025909a75163bHiroshi Yamauchi      // This is the alloc space.
27509b07a96094086e205948717666025909a75163bHiroshi Yamauchi      gc::space::DlMallocSpace* dlmalloc_space = space->AsDlMallocSpace();
27609b07a96094086e205948717666025909a75163bHiroshi Yamauchi      allocSize += dlmalloc_space->GetFootprint();
27709b07a96094086e205948717666025909a75163bHiroshi Yamauchi      allocUsed += dlmalloc_space->GetBytesAllocated();
27809b07a96094086e205948717666025909a75163bHiroshi Yamauchi    }
27909b07a96094086e205948717666025909a75163bHiroshi Yamauchi  }
28009b07a96094086e205948717666025909a75163bHiroshi Yamauchi  typedef std::vector<gc::space::DiscontinuousSpace*>::const_iterator It2;
28109b07a96094086e205948717666025909a75163bHiroshi Yamauchi  for (It2 it = discontinuous_spaces.begin(), end = discontinuous_spaces.end(); it != end; ++it) {
28209b07a96094086e205948717666025909a75163bHiroshi Yamauchi    gc::space::DiscontinuousSpace* space = *it;
28309b07a96094086e205948717666025909a75163bHiroshi Yamauchi    if (space->IsLargeObjectSpace()) {
28409b07a96094086e205948717666025909a75163bHiroshi Yamauchi      largeObjectsSize += space->AsLargeObjectSpace()->GetBytesAllocated();
28509b07a96094086e205948717666025909a75163bHiroshi Yamauchi      largeObjectsUsed += largeObjectsSize;
28609b07a96094086e205948717666025909a75163bHiroshi Yamauchi    }
28709b07a96094086e205948717666025909a75163bHiroshi Yamauchi  }
28809b07a96094086e205948717666025909a75163bHiroshi Yamauchi
28909b07a96094086e205948717666025909a75163bHiroshi Yamauchi  size_t allocFree = allocSize - allocUsed;
29009b07a96094086e205948717666025909a75163bHiroshi Yamauchi  size_t zygoteFree = zygoteSize - zygoteUsed;
29109b07a96094086e205948717666025909a75163bHiroshi Yamauchi  size_t largeObjectsFree = largeObjectsSize - largeObjectsUsed;
29209b07a96094086e205948717666025909a75163bHiroshi Yamauchi
29309b07a96094086e205948717666025909a75163bHiroshi Yamauchi  int j = 0;
29409b07a96094086e205948717666025909a75163bHiroshi Yamauchi  arr[j++] = allocSize;
29509b07a96094086e205948717666025909a75163bHiroshi Yamauchi  arr[j++] = allocUsed;
29609b07a96094086e205948717666025909a75163bHiroshi Yamauchi  arr[j++] = allocFree;
29709b07a96094086e205948717666025909a75163bHiroshi Yamauchi  arr[j++] = zygoteSize;
29809b07a96094086e205948717666025909a75163bHiroshi Yamauchi  arr[j++] = zygoteUsed;
29909b07a96094086e205948717666025909a75163bHiroshi Yamauchi  arr[j++] = zygoteFree;
30009b07a96094086e205948717666025909a75163bHiroshi Yamauchi  arr[j++] = largeObjectsSize;
30109b07a96094086e205948717666025909a75163bHiroshi Yamauchi  arr[j++] = largeObjectsUsed;
30209b07a96094086e205948717666025909a75163bHiroshi Yamauchi  arr[j++] = largeObjectsFree;
30309b07a96094086e205948717666025909a75163bHiroshi Yamauchi  env->ReleasePrimitiveArrayCritical(data, arr, 0);
30409b07a96094086e205948717666025909a75163bHiroshi Yamauchi}
30509b07a96094086e205948717666025909a75163bHiroshi Yamauchi
3060512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic JNINativeMethod gMethods[] = {
3079d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, countInstancesOfClass, "(Ljava/lang/Class;Z)J"),
3089d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, crash, "()V"),
3099d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, dumpHprofData, "(Ljava/lang/String;Ljava/io/FileDescriptor;)V"),
3109d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, dumpHprofDataDdms, "()V"),
3119d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, dumpReferenceTables, "()V"),
3129d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, getAllocCount, "(I)I"),
31309b07a96094086e205948717666025909a75163bHiroshi Yamauchi  NATIVE_METHOD(VMDebug, getHeapSpaceStats, "([J)V"),
3149d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, getInstructionCount, "([I)V"),
3159d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, getLoadedClassCount, "()I"),
3169d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, getVmFeatureList, "()[Ljava/lang/String;"),
3179d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, infopoint, "(I)V"),
3189d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, isDebuggerConnected, "()Z"),
3199d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, isDebuggingEnabled, "()Z"),
32064caa7dcf46ed6139b766dbe77fbd7353899417fJeff Hao  NATIVE_METHOD(VMDebug, getMethodTracingMode, "()I"),
3219d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, lastDebuggerActivity, "()J"),
3229d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, printLoadedClasses, "(I)V"),
3239d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, resetAllocCount, "(I)V"),
3249d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, resetInstructionCount, "()V"),
3259d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, startAllocCounting, "()V"),
3269d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, startEmulatorTracing, "()V"),
3279d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, startInstructionCounting, "()V"),
32823009dca63c1699e28bfeaa8b45ca48fa0e86aceJeff Hao  NATIVE_METHOD(VMDebug, startMethodTracingDdmsImpl, "(IIZI)V"),
3299d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, startMethodTracingFd, "(Ljava/lang/String;Ljava/io/FileDescriptor;II)V"),
3309d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, startMethodTracingFilename, "(Ljava/lang/String;II)V"),
3319d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, stopAllocCounting, "()V"),
3329d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, stopEmulatorTracing, "()V"),
3339d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, stopInstructionCounting, "()V"),
3349d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, stopMethodTracing, "()V"),
3359d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, threadCpuTimeNanos, "()J"),
3369d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes};
3379d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
3389d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughesvoid register_dalvik_system_VMDebug(JNIEnv* env) {
339eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  REGISTER_NATIVE_METHODS("dalvik/system/VMDebug");
3409d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
3419d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
3429d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}  // namespace art
343