dalvik_system_VMDebug.cc revision 7410f29b4dae223befac036ea567d7f33351dad1
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"
237410f29b4dae223befac036ea567d7f33351dad1Mathieu Chartier#include "gc/space/bump_pointer_space.h"
2409b07a96094086e205948717666025909a75163bHiroshi Yamauchi#include "gc/space/dlmalloc_space.h"
2509b07a96094086e205948717666025909a75163bHiroshi Yamauchi#include "gc/space/large_object_space.h"
2609b07a96094086e205948717666025909a75163bHiroshi Yamauchi#include "gc/space/space-inl.h"
271121e0b619d0c9df36c57396b2234a9d570b4923Jesse Wilson#include "hprof/hprof.h"
28eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes#include "jni_internal.h"
292dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class.h"
309d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes#include "ScopedUtfChars.h"
3100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "scoped_thread_state_change.h"
329d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes#include "toStringArray.h"
33eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes#include "trace.h"
349d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
359d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughesnamespace art {
369d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
370512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic jobjectArray VMDebug_getVmFeatureList(JNIEnv* env, jclass) {
389d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  std::vector<std::string> features;
39e343b76af81a005ef64f5e75a555389fd9147dabjeffhao  features.push_back("method-trace-profiling");
40e343b76af81a005ef64f5e75a555389fd9147dabjeffhao  features.push_back("method-trace-profiling-streaming");
4123009dca63c1699e28bfeaa8b45ca48fa0e86aceJeff Hao  features.push_back("method-sample-profiling");
42767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  features.push_back("hprof-heap-dump");
43767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  features.push_back("hprof-heap-dump-streaming");
449d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  return toStringArray(env, features);
459d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
469d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
470512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void VMDebug_startAllocCounting(JNIEnv*, jclass) {
489d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  Runtime::Current()->SetStatsEnabled(true);
499d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
509d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
510512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void VMDebug_stopAllocCounting(JNIEnv*, jclass) {
529d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  Runtime::Current()->SetStatsEnabled(false);
539d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
549d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
551bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughesstatic jint VMDebug_getAllocCount(JNIEnv*, jclass, jint kind) {
569d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  return Runtime::Current()->GetStat(kind);
579d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
589d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
590512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void VMDebug_resetAllocCount(JNIEnv*, jclass, jint kinds) {
609d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  Runtime::Current()->ResetStats(kinds);
619d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
629d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
6323009dca63c1699e28bfeaa8b45ca48fa0e86aceJeff Haostatic void VMDebug_startMethodTracingDdmsImpl(JNIEnv*, jclass, jint bufferSize, jint flags,
6423009dca63c1699e28bfeaa8b45ca48fa0e86aceJeff Hao                                               jboolean samplingEnabled, jint intervalUs) {
6523009dca63c1699e28bfeaa8b45ca48fa0e86aceJeff Hao  Trace::Start("[DDMS]", -1, bufferSize, flags, true, samplingEnabled, intervalUs);
669d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
679d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
6800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic void VMDebug_startMethodTracingFd(JNIEnv* env, jclass, jstring javaTraceFilename,
6900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                         jobject javaFd, jint bufferSize, jint flags) {
709d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  int originalFd = jniGetFDFromFileDescriptor(env, javaFd);
719d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  if (originalFd < 0) {
729d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    return;
739d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  }
749d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
759d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  int fd = dup(originalFd);
769d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  if (fd < 0) {
7700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(env);
7862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
7962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/RuntimeException;",
8062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                                   "dup(%d) failed: %s", originalFd, strerror(errno));
819d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    return;
829d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  }
839d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
849d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  ScopedUtfChars traceFilename(env, javaTraceFilename);
859d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  if (traceFilename.c_str() == NULL) {
869d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    return;
879d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  }
8823009dca63c1699e28bfeaa8b45ca48fa0e86aceJeff Hao  Trace::Start(traceFilename.c_str(), fd, bufferSize, flags, false, false, 0);
899d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
909d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
9100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic void VMDebug_startMethodTracingFilename(JNIEnv* env, jclass, jstring javaTraceFilename,
9200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                               jint bufferSize, jint flags) {
939d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  ScopedUtfChars traceFilename(env, javaTraceFilename);
949d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  if (traceFilename.c_str() == NULL) {
959d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    return;
969d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  }
9723009dca63c1699e28bfeaa8b45ca48fa0e86aceJeff Hao  Trace::Start(traceFilename.c_str(), -1, bufferSize, flags, false, false, 0);
989d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
999d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
10064caa7dcf46ed6139b766dbe77fbd7353899417fJeff Haostatic jint VMDebug_getMethodTracingMode(JNIEnv*, jclass) {
10164caa7dcf46ed6139b766dbe77fbd7353899417fJeff Hao  return Trace::GetMethodTracingMode();
1029d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
1039d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
1040512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void VMDebug_stopMethodTracing(JNIEnv*, jclass) {
105e343b76af81a005ef64f5e75a555389fd9147dabjeffhao  Trace::Stop();
1069d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
1079d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
1080512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void VMDebug_startEmulatorTracing(JNIEnv*, jclass) {
1099d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  UNIMPLEMENTED(WARNING);
1107934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  // dvmEmulatorTraceStart();
1119d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
1129d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
1130512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void VMDebug_stopEmulatorTracing(JNIEnv*, jclass) {
1149d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  UNIMPLEMENTED(WARNING);
1157934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  // dvmEmulatorTraceStop();
1169d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
1179d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
1180512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic jboolean VMDebug_isDebuggerConnected(JNIEnv*, jclass) {
119c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  return Dbg::IsDebuggerActive();
1209d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
1219d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
1220512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic jboolean VMDebug_isDebuggingEnabled(JNIEnv*, jclass) {
123c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  return Dbg::IsJdwpConfigured();
1249d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
1259d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
1260512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic jlong VMDebug_lastDebuggerActivity(JNIEnv*, jclass) {
127872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes  return Dbg::LastDebuggerActivity();
1289d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
1299d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
13062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogersstatic void ThrowUnsupportedOperationException(JNIEnv* env) {
13100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(env);
13262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
13362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  soa.Self()->ThrowNewException(throw_location, "Ljava/lang/UnsupportedOperationException;", NULL);
13462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers}
13562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
13662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogersstatic void VMDebug_startInstructionCounting(JNIEnv* env, jclass) {
13762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  ThrowUnsupportedOperationException(env);
1389d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
1399d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
14000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic void VMDebug_stopInstructionCounting(JNIEnv* env, jclass) {
14162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  ThrowUnsupportedOperationException(env);
1429d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
1439d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
14400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic void VMDebug_getInstructionCount(JNIEnv* env, jclass, jintArray /*javaCounts*/) {
14562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  ThrowUnsupportedOperationException(env);
1469d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
1479d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
14800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic void VMDebug_resetInstructionCount(JNIEnv* env, jclass) {
14962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  ThrowUnsupportedOperationException(env);
1509d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
1519d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
15200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic void VMDebug_printLoadedClasses(JNIEnv* env, jclass, jint flags) {
15300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(env);
1549d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  return Runtime::Current()->GetClassLinker()->DumpAllClasses(flags);
1559d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
1569d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
1577dfb28c066159e6cde8181720f0c451a700ef966Ian Rogersstatic jint VMDebug_getLoadedClassCount(JNIEnv* env, jclass) {
1587dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers  ScopedObjectAccess soa(env);
1599d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  return Runtime::Current()->GetClassLinker()->NumLoadedClasses();
1609d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
1619d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
1629d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes/*
1639d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes * Returns the thread-specific CPU-time clock value for the current thread,
1649d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes * or -1 if the feature isn't supported.
1659d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes */
1660512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic jlong VMDebug_threadCpuTimeNanos(JNIEnv*, jclass) {
1670512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughes  return ThreadCpuNanoTime();
1689d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
1699d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
1709d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes/*
1719d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes * static void dumpHprofData(String fileName, FileDescriptor fd)
1729d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes *
1739d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes * Cause "hprof" data to be dumped.  We can throw an IOException if an
1749d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes * error occurs during file handling.
1759d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes */
1760512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void VMDebug_dumpHprofData(JNIEnv* env, jclass, jstring javaFilename, jobject javaFd) {
1779d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  // Only one of these may be NULL.
1789d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  if (javaFilename == NULL && javaFd == NULL) {
17900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(env);
18062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    ThrowNullPointerException(NULL, "fileName == null && fd == null");
1819d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    return;
1829d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  }
1839d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
1849d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  std::string filename;
1859d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  if (javaFilename != NULL) {
1869d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    ScopedUtfChars chars(env, javaFilename);
1879d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    if (env->ExceptionCheck()) {
1889d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes      return;
1899d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    }
1909d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    filename = chars.c_str();
1919d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  } else {
1929d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    filename = "[fd]";
1939d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  }
1949d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
1959d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  int fd = -1;
1969d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  if (javaFd != NULL) {
1979d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    fd = jniGetFDFromFileDescriptor(env, javaFd);
1989d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    if (fd < 0) {
19900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      ScopedObjectAccess soa(env);
20062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      ThrowRuntimeException("Invalid file descriptor");
2019d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes      return;
2029d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    }
2039d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  }
2049d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
205622a6988351d77da0008142f4ce1ea447d838556Elliott Hughes  hprof::DumpHeap(filename.c_str(), fd, false);
2069d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
2079d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
208eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughesstatic void VMDebug_dumpHprofDataDdms(JNIEnv*, jclass) {
209622a6988351d77da0008142f4ce1ea447d838556Elliott Hughes  hprof::DumpHeap("[DDMS]", -1, true);
2109d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
2119d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
2120512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void VMDebug_dumpReferenceTables(JNIEnv* env, jclass) {
21300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(env);
2149d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  LOG(INFO) << "--- reference table dump ---";
2159d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
21600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  soa.Env()->DumpReferenceTables(LOG(INFO));
21700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  soa.Vm()->DumpReferenceTables(LOG(INFO));
2189d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
2199d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  LOG(INFO) << "---";
2209d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
2219d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
2220512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void VMDebug_crash(JNIEnv*, jclass) {
22381ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes  LOG(FATAL) << "Crashing runtime on request";
2249d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
2259d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
2260512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void VMDebug_infopoint(JNIEnv*, jclass, jint id) {
2279d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  LOG(INFO) << "VMDebug infopoint " << id << " hit";
2289d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
2299d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
23000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic jlong VMDebug_countInstancesOfClass(JNIEnv* env, jclass, jclass javaClass,
23100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                           jboolean countAssignable) {
23200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(env);
2332dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = soa.Decode<mirror::Class*>(javaClass);
2349d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  if (c == NULL) {
2359d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    return 0;
2369d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  }
2372dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Class*> classes;
238ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  classes.push_back(c);
239ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  uint64_t count = 0;
240ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  Runtime::Current()->GetHeap()->CountInstances(classes, countAssignable, &count);
241ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  return count;
2429d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
2439d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
24409b07a96094086e205948717666025909a75163bHiroshi Yamauchi// We export the VM internal per-heap-space size/alloc/free metrics
24509b07a96094086e205948717666025909a75163bHiroshi Yamauchi// for the zygote space, alloc space (application heap), and the large
24609b07a96094086e205948717666025909a75163bHiroshi Yamauchi// object space for dumpsys meminfo. The other memory region data such
24709b07a96094086e205948717666025909a75163bHiroshi Yamauchi// as PSS, private/shared dirty/shared data are available via
24809b07a96094086e205948717666025909a75163bHiroshi Yamauchi// /proc/<pid>/smaps.
24909b07a96094086e205948717666025909a75163bHiroshi Yamauchistatic void VMDebug_getHeapSpaceStats(JNIEnv* env, jclass, jlongArray data) {
2506b99dd10d8a4869bbf7e9263a350185e5ebe32e4Ian Rogers  jlong* arr = reinterpret_cast<jlong*>(env->GetPrimitiveArrayCritical(data, 0));
2517410f29b4dae223befac036ea567d7f33351dad1Mathieu Chartier  if (arr == nullptr || env->GetArrayLength(data) < 9) {
25209b07a96094086e205948717666025909a75163bHiroshi Yamauchi    return;
25309b07a96094086e205948717666025909a75163bHiroshi Yamauchi  }
25409b07a96094086e205948717666025909a75163bHiroshi Yamauchi
25509b07a96094086e205948717666025909a75163bHiroshi Yamauchi  size_t allocSize = 0;
25609b07a96094086e205948717666025909a75163bHiroshi Yamauchi  size_t allocUsed = 0;
25709b07a96094086e205948717666025909a75163bHiroshi Yamauchi  size_t zygoteSize = 0;
25809b07a96094086e205948717666025909a75163bHiroshi Yamauchi  size_t zygoteUsed = 0;
25909b07a96094086e205948717666025909a75163bHiroshi Yamauchi  size_t largeObjectsSize = 0;
26009b07a96094086e205948717666025909a75163bHiroshi Yamauchi  size_t largeObjectsUsed = 0;
26109b07a96094086e205948717666025909a75163bHiroshi Yamauchi  gc::Heap* heap = Runtime::Current()->GetHeap();
2627410f29b4dae223befac036ea567d7f33351dad1Mathieu Chartier  for (gc::space::ContinuousSpace* space : heap->GetContinuousSpaces()) {
26309b07a96094086e205948717666025909a75163bHiroshi Yamauchi    if (space->IsImageSpace()) {
26409b07a96094086e205948717666025909a75163bHiroshi Yamauchi      // Currently don't include the image space.
26509b07a96094086e205948717666025909a75163bHiroshi Yamauchi    } else if (space->IsZygoteSpace()) {
266cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi      gc::space::MallocSpace* malloc_space = space->AsMallocSpace();
267cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi      zygoteSize += malloc_space->GetFootprint();
268cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi      zygoteUsed += malloc_space->GetBytesAllocated();
2697410f29b4dae223befac036ea567d7f33351dad1Mathieu Chartier    } else if (space->IsMallocSpace()) {
2707410f29b4dae223befac036ea567d7f33351dad1Mathieu Chartier      // This is a malloc space.
271cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi      gc::space::MallocSpace* malloc_space = space->AsMallocSpace();
272cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi      allocSize += malloc_space->GetFootprint();
273cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi      allocUsed += malloc_space->GetBytesAllocated();
2747410f29b4dae223befac036ea567d7f33351dad1Mathieu Chartier    } else if (space->IsBumpPointerSpace()) {
2757410f29b4dae223befac036ea567d7f33351dad1Mathieu Chartier      gc::space::BumpPointerSpace* bump_pointer_space = space->AsBumpPointerSpace();
2767410f29b4dae223befac036ea567d7f33351dad1Mathieu Chartier      allocSize += bump_pointer_space->Size();
2777410f29b4dae223befac036ea567d7f33351dad1Mathieu Chartier      allocUsed += bump_pointer_space->GetBytesAllocated();
27809b07a96094086e205948717666025909a75163bHiroshi Yamauchi    }
27909b07a96094086e205948717666025909a75163bHiroshi Yamauchi  }
2807410f29b4dae223befac036ea567d7f33351dad1Mathieu Chartier  for (gc::space::DiscontinuousSpace* space : heap->GetDiscontinuousSpaces()) {
28109b07a96094086e205948717666025909a75163bHiroshi Yamauchi    if (space->IsLargeObjectSpace()) {
28209b07a96094086e205948717666025909a75163bHiroshi Yamauchi      largeObjectsSize += space->AsLargeObjectSpace()->GetBytesAllocated();
28309b07a96094086e205948717666025909a75163bHiroshi Yamauchi      largeObjectsUsed += largeObjectsSize;
28409b07a96094086e205948717666025909a75163bHiroshi Yamauchi    }
28509b07a96094086e205948717666025909a75163bHiroshi Yamauchi  }
28609b07a96094086e205948717666025909a75163bHiroshi Yamauchi
28709b07a96094086e205948717666025909a75163bHiroshi Yamauchi  size_t allocFree = allocSize - allocUsed;
28809b07a96094086e205948717666025909a75163bHiroshi Yamauchi  size_t zygoteFree = zygoteSize - zygoteUsed;
28909b07a96094086e205948717666025909a75163bHiroshi Yamauchi  size_t largeObjectsFree = largeObjectsSize - largeObjectsUsed;
29009b07a96094086e205948717666025909a75163bHiroshi Yamauchi
29109b07a96094086e205948717666025909a75163bHiroshi Yamauchi  int j = 0;
29209b07a96094086e205948717666025909a75163bHiroshi Yamauchi  arr[j++] = allocSize;
29309b07a96094086e205948717666025909a75163bHiroshi Yamauchi  arr[j++] = allocUsed;
29409b07a96094086e205948717666025909a75163bHiroshi Yamauchi  arr[j++] = allocFree;
29509b07a96094086e205948717666025909a75163bHiroshi Yamauchi  arr[j++] = zygoteSize;
29609b07a96094086e205948717666025909a75163bHiroshi Yamauchi  arr[j++] = zygoteUsed;
29709b07a96094086e205948717666025909a75163bHiroshi Yamauchi  arr[j++] = zygoteFree;
29809b07a96094086e205948717666025909a75163bHiroshi Yamauchi  arr[j++] = largeObjectsSize;
29909b07a96094086e205948717666025909a75163bHiroshi Yamauchi  arr[j++] = largeObjectsUsed;
30009b07a96094086e205948717666025909a75163bHiroshi Yamauchi  arr[j++] = largeObjectsFree;
30109b07a96094086e205948717666025909a75163bHiroshi Yamauchi  env->ReleasePrimitiveArrayCritical(data, arr, 0);
30209b07a96094086e205948717666025909a75163bHiroshi Yamauchi}
30309b07a96094086e205948717666025909a75163bHiroshi Yamauchi
3040512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic JNINativeMethod gMethods[] = {
3059d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, countInstancesOfClass, "(Ljava/lang/Class;Z)J"),
3069d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, crash, "()V"),
3079d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, dumpHprofData, "(Ljava/lang/String;Ljava/io/FileDescriptor;)V"),
3089d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, dumpHprofDataDdms, "()V"),
3099d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, dumpReferenceTables, "()V"),
3109d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, getAllocCount, "(I)I"),
31109b07a96094086e205948717666025909a75163bHiroshi Yamauchi  NATIVE_METHOD(VMDebug, getHeapSpaceStats, "([J)V"),
3129d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, getInstructionCount, "([I)V"),
3139d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, getLoadedClassCount, "()I"),
3149d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, getVmFeatureList, "()[Ljava/lang/String;"),
3159d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, infopoint, "(I)V"),
3169d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, isDebuggerConnected, "()Z"),
3179d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, isDebuggingEnabled, "()Z"),
31864caa7dcf46ed6139b766dbe77fbd7353899417fJeff Hao  NATIVE_METHOD(VMDebug, getMethodTracingMode, "()I"),
3199d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, lastDebuggerActivity, "()J"),
3209d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, printLoadedClasses, "(I)V"),
3219d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, resetAllocCount, "(I)V"),
3229d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, resetInstructionCount, "()V"),
3239d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, startAllocCounting, "()V"),
3249d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, startEmulatorTracing, "()V"),
3259d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, startInstructionCounting, "()V"),
32623009dca63c1699e28bfeaa8b45ca48fa0e86aceJeff Hao  NATIVE_METHOD(VMDebug, startMethodTracingDdmsImpl, "(IIZI)V"),
3279d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, startMethodTracingFd, "(Ljava/lang/String;Ljava/io/FileDescriptor;II)V"),
3289d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, startMethodTracingFilename, "(Ljava/lang/String;II)V"),
3299d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, stopAllocCounting, "()V"),
3309d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, stopEmulatorTracing, "()V"),
3319d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, stopInstructionCounting, "()V"),
3329d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, stopMethodTracing, "()V"),
3339d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  NATIVE_METHOD(VMDebug, threadCpuTimeNanos, "()J"),
3349d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes};
3359d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
3369d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughesvoid register_dalvik_system_VMDebug(JNIEnv* env) {
337eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  REGISTER_NATIVE_METHODS("dalvik/system/VMDebug");
3389d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
3399d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
3409d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}  // namespace art
341