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" 271f3b5358b28a83f0929bdd8ce738f06908677fb7Mathieu Chartier#include "gc/space/zygote_space.h" 281121e0b619d0c9df36c57396b2234a9d570b4923Jesse Wilson#include "hprof/hprof.h" 29eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes#include "jni_internal.h" 302dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class.h" 31dd157d732b557a854b3689faf8a24170fb47c270Ian Rogers#include "ScopedLocalRef.h" 329d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes#include "ScopedUtfChars.h" 3353b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers#include "scoped_fast_native_object_access.h" 34eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes#include "trace.h" 35dd157d732b557a854b3689faf8a24170fb47c270Ian Rogers#include "well_known_classes.h" 369d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 379d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughesnamespace art { 389d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 390512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic jobjectArray VMDebug_getVmFeatureList(JNIEnv* env, jclass) { 40dd157d732b557a854b3689faf8a24170fb47c270Ian Rogers static const char* features[] = { 41dd157d732b557a854b3689faf8a24170fb47c270Ian Rogers "method-trace-profiling", 42dd157d732b557a854b3689faf8a24170fb47c270Ian Rogers "method-trace-profiling-streaming", 43dd157d732b557a854b3689faf8a24170fb47c270Ian Rogers "method-sample-profiling", 44dd157d732b557a854b3689faf8a24170fb47c270Ian Rogers "hprof-heap-dump", 45dd157d732b557a854b3689faf8a24170fb47c270Ian Rogers "hprof-heap-dump-streaming", 46dd157d732b557a854b3689faf8a24170fb47c270Ian Rogers }; 47dd157d732b557a854b3689faf8a24170fb47c270Ian Rogers jobjectArray result = env->NewObjectArray(arraysize(features), 48dd157d732b557a854b3689faf8a24170fb47c270Ian Rogers WellKnownClasses::java_lang_String, 49dd157d732b557a854b3689faf8a24170fb47c270Ian Rogers nullptr); 50dd157d732b557a854b3689faf8a24170fb47c270Ian Rogers if (result != nullptr) { 51dd157d732b557a854b3689faf8a24170fb47c270Ian Rogers for (size_t i = 0; i < arraysize(features); ++i) { 52dd157d732b557a854b3689faf8a24170fb47c270Ian Rogers ScopedLocalRef<jstring> jfeature(env, env->NewStringUTF(features[i])); 53dd157d732b557a854b3689faf8a24170fb47c270Ian Rogers if (jfeature.get() == nullptr) { 54dd157d732b557a854b3689faf8a24170fb47c270Ian Rogers return nullptr; 55dd157d732b557a854b3689faf8a24170fb47c270Ian Rogers } 56dd157d732b557a854b3689faf8a24170fb47c270Ian Rogers env->SetObjectArrayElement(result, i, jfeature.get()); 57dd157d732b557a854b3689faf8a24170fb47c270Ian Rogers } 58dd157d732b557a854b3689faf8a24170fb47c270Ian Rogers } 59dd157d732b557a854b3689faf8a24170fb47c270Ian Rogers return result; 609d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes} 619d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 620512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void VMDebug_startAllocCounting(JNIEnv*, jclass) { 63a98ffd745bbecb2e84a492194950c0b94966546bMathieu Chartier Runtime::Current()->SetStatsEnabled(true); 649d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes} 659d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 660512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void VMDebug_stopAllocCounting(JNIEnv*, jclass) { 67a98ffd745bbecb2e84a492194950c0b94966546bMathieu Chartier Runtime::Current()->SetStatsEnabled(false); 689d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes} 699d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 701bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughesstatic jint VMDebug_getAllocCount(JNIEnv*, jclass, jint kind) { 719d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes return Runtime::Current()->GetStat(kind); 729d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes} 739d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 740512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void VMDebug_resetAllocCount(JNIEnv*, jclass, jint kinds) { 759d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes Runtime::Current()->ResetStats(kinds); 769d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes} 779d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 7823009dca63c1699e28bfeaa8b45ca48fa0e86aceJeff Haostatic void VMDebug_startMethodTracingDdmsImpl(JNIEnv*, jclass, jint bufferSize, jint flags, 7923009dca63c1699e28bfeaa8b45ca48fa0e86aceJeff Hao jboolean samplingEnabled, jint intervalUs) { 8023009dca63c1699e28bfeaa8b45ca48fa0e86aceJeff Hao Trace::Start("[DDMS]", -1, bufferSize, flags, true, samplingEnabled, intervalUs); 819d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes} 829d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 8300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic void VMDebug_startMethodTracingFd(JNIEnv* env, jclass, jstring javaTraceFilename, 844044bdac490777cbc8a12d467bec675ef8aa6eb1Jeff Hao jobject javaFd, jint bufferSize, jint flags, 854044bdac490777cbc8a12d467bec675ef8aa6eb1Jeff Hao jboolean samplingEnabled, jint intervalUs) { 869d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes int originalFd = jniGetFDFromFileDescriptor(env, javaFd); 879d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes if (originalFd < 0) { 889d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes return; 899d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes } 909d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 919d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes int fd = dup(originalFd); 929d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes if (fd < 0) { 9300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccess soa(env); 9462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow(); 9562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/RuntimeException;", 9662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers "dup(%d) failed: %s", originalFd, strerror(errno)); 979d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes return; 989d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes } 999d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 1009d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes ScopedUtfChars traceFilename(env, javaTraceFilename); 1019d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes if (traceFilename.c_str() == NULL) { 1029d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes return; 1039d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes } 1044044bdac490777cbc8a12d467bec675ef8aa6eb1Jeff Hao Trace::Start(traceFilename.c_str(), fd, bufferSize, flags, false, samplingEnabled, intervalUs); 1059d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes} 1069d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 10700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic void VMDebug_startMethodTracingFilename(JNIEnv* env, jclass, jstring javaTraceFilename, 1084044bdac490777cbc8a12d467bec675ef8aa6eb1Jeff Hao jint bufferSize, jint flags, 1094044bdac490777cbc8a12d467bec675ef8aa6eb1Jeff Hao jboolean samplingEnabled, jint intervalUs) { 1109d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes ScopedUtfChars traceFilename(env, javaTraceFilename); 1119d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes if (traceFilename.c_str() == NULL) { 1129d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes return; 1139d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes } 1144044bdac490777cbc8a12d467bec675ef8aa6eb1Jeff Hao Trace::Start(traceFilename.c_str(), -1, bufferSize, flags, false, samplingEnabled, intervalUs); 1159d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes} 1169d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 11764caa7dcf46ed6139b766dbe77fbd7353899417fJeff Haostatic jint VMDebug_getMethodTracingMode(JNIEnv*, jclass) { 11864caa7dcf46ed6139b766dbe77fbd7353899417fJeff Hao return Trace::GetMethodTracingMode(); 1199d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes} 1209d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 1210512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void VMDebug_stopMethodTracing(JNIEnv*, jclass) { 122e343b76af81a005ef64f5e75a555389fd9147dabjeffhao Trace::Stop(); 1239d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes} 1249d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 1250512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void VMDebug_startEmulatorTracing(JNIEnv*, jclass) { 1269d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes UNIMPLEMENTED(WARNING); 1277934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom // dvmEmulatorTraceStart(); 1289d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes} 1299d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 1300512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void VMDebug_stopEmulatorTracing(JNIEnv*, jclass) { 1319d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes UNIMPLEMENTED(WARNING); 1327934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom // dvmEmulatorTraceStop(); 1339d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes} 1349d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 1350512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic jboolean VMDebug_isDebuggerConnected(JNIEnv*, jclass) { 136c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes return Dbg::IsDebuggerActive(); 1379d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes} 1389d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 1390512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic jboolean VMDebug_isDebuggingEnabled(JNIEnv*, jclass) { 140c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes return Dbg::IsJdwpConfigured(); 1419d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes} 1429d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 1430512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic jlong VMDebug_lastDebuggerActivity(JNIEnv*, jclass) { 144872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes return Dbg::LastDebuggerActivity(); 1459d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes} 1469d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 14762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogersstatic void ThrowUnsupportedOperationException(JNIEnv* env) { 14800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccess soa(env); 14962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow(); 15062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers soa.Self()->ThrowNewException(throw_location, "Ljava/lang/UnsupportedOperationException;", NULL); 15162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers} 15262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers 15362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogersstatic void VMDebug_startInstructionCounting(JNIEnv* env, jclass) { 15462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ThrowUnsupportedOperationException(env); 1559d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes} 1569d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 15700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic void VMDebug_stopInstructionCounting(JNIEnv* env, jclass) { 15862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ThrowUnsupportedOperationException(env); 1599d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes} 1609d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 16100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic void VMDebug_getInstructionCount(JNIEnv* env, jclass, jintArray /*javaCounts*/) { 16262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ThrowUnsupportedOperationException(env); 1639d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes} 1649d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 16500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic void VMDebug_resetInstructionCount(JNIEnv* env, jclass) { 16662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ThrowUnsupportedOperationException(env); 1679d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes} 1689d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 16900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic void VMDebug_printLoadedClasses(JNIEnv* env, jclass, jint flags) { 17053b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers ScopedFastNativeObjectAccess soa(env); 1719d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes return Runtime::Current()->GetClassLinker()->DumpAllClasses(flags); 1729d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes} 1739d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 1747dfb28c066159e6cde8181720f0c451a700ef966Ian Rogersstatic jint VMDebug_getLoadedClassCount(JNIEnv* env, jclass) { 17553b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers ScopedFastNativeObjectAccess soa(env); 1769d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes return Runtime::Current()->GetClassLinker()->NumLoadedClasses(); 1779d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes} 1789d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 1799d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes/* 1809d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes * Returns the thread-specific CPU-time clock value for the current thread, 1819d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes * or -1 if the feature isn't supported. 1829d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes */ 1830512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic jlong VMDebug_threadCpuTimeNanos(JNIEnv*, jclass) { 1840512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughes return ThreadCpuNanoTime(); 1859d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes} 1869d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 1879d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes/* 1889d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes * static void dumpHprofData(String fileName, FileDescriptor fd) 1899d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes * 1909d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes * Cause "hprof" data to be dumped. We can throw an IOException if an 1919d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes * error occurs during file handling. 1929d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes */ 1930512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void VMDebug_dumpHprofData(JNIEnv* env, jclass, jstring javaFilename, jobject javaFd) { 1949d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes // Only one of these may be NULL. 1959d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes if (javaFilename == NULL && javaFd == NULL) { 19600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccess soa(env); 19762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ThrowNullPointerException(NULL, "fileName == null && fd == null"); 1989d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes return; 1999d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes } 2009d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 2019d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes std::string filename; 2029d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes if (javaFilename != NULL) { 2039d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes ScopedUtfChars chars(env, javaFilename); 2049d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes if (env->ExceptionCheck()) { 2059d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes return; 2069d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes } 2079d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes filename = chars.c_str(); 2089d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes } else { 2099d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes filename = "[fd]"; 2109d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes } 2119d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 2129d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes int fd = -1; 2139d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes if (javaFd != NULL) { 2149d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes fd = jniGetFDFromFileDescriptor(env, javaFd); 2159d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes if (fd < 0) { 21600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccess soa(env); 21762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ThrowRuntimeException("Invalid file descriptor"); 2189d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes return; 2199d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes } 2209d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes } 2219d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 222622a6988351d77da0008142f4ce1ea447d838556Elliott Hughes hprof::DumpHeap(filename.c_str(), fd, false); 2239d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes} 2249d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 225eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughesstatic void VMDebug_dumpHprofDataDdms(JNIEnv*, jclass) { 226622a6988351d77da0008142f4ce1ea447d838556Elliott Hughes hprof::DumpHeap("[DDMS]", -1, true); 2279d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes} 2289d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 2290512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void VMDebug_dumpReferenceTables(JNIEnv* env, jclass) { 23000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccess soa(env); 2319d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes LOG(INFO) << "--- reference table dump ---"; 2329d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 23300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers soa.Env()->DumpReferenceTables(LOG(INFO)); 23400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers soa.Vm()->DumpReferenceTables(LOG(INFO)); 2359d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 2369d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes LOG(INFO) << "---"; 2379d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes} 2389d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 2390512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void VMDebug_crash(JNIEnv*, jclass) { 24081ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes LOG(FATAL) << "Crashing runtime on request"; 2419d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes} 2429d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 2430512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void VMDebug_infopoint(JNIEnv*, jclass, jint id) { 2449d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes LOG(INFO) << "VMDebug infopoint " << id << " hit"; 2459d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes} 2469d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 24700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic jlong VMDebug_countInstancesOfClass(JNIEnv* env, jclass, jclass javaClass, 24800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers jboolean countAssignable) { 24900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccess soa(env); 250412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier gc::Heap* heap = Runtime::Current()->GetHeap(); 25183c8ee000d525017ead8753fce6bc1020249b96aMathieu Chartier // We only want reachable instances, so do a GC. Heap::VisitObjects visits all of the heap 25283c8ee000d525017ead8753fce6bc1020249b96aMathieu Chartier // objects in the all spaces and the allocation stack. 253412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier heap->CollectGarbage(false); 2542dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::Class* c = soa.Decode<mirror::Class*>(javaClass); 255412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier if (c == nullptr) { 2569d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes return 0; 2579d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes } 2582dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers std::vector<mirror::Class*> classes; 259ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes classes.push_back(c); 260ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes uint64_t count = 0; 261412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier heap->CountInstances(classes, countAssignable, &count); 262ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes return count; 2639d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes} 2649d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 26509b07a96094086e205948717666025909a75163bHiroshi Yamauchi// We export the VM internal per-heap-space size/alloc/free metrics 26609b07a96094086e205948717666025909a75163bHiroshi Yamauchi// for the zygote space, alloc space (application heap), and the large 26709b07a96094086e205948717666025909a75163bHiroshi Yamauchi// object space for dumpsys meminfo. The other memory region data such 26809b07a96094086e205948717666025909a75163bHiroshi Yamauchi// as PSS, private/shared dirty/shared data are available via 26909b07a96094086e205948717666025909a75163bHiroshi Yamauchi// /proc/<pid>/smaps. 27009b07a96094086e205948717666025909a75163bHiroshi Yamauchistatic void VMDebug_getHeapSpaceStats(JNIEnv* env, jclass, jlongArray data) { 2716b99dd10d8a4869bbf7e9263a350185e5ebe32e4Ian Rogers jlong* arr = reinterpret_cast<jlong*>(env->GetPrimitiveArrayCritical(data, 0)); 2727410f29b4dae223befac036ea567d7f33351dad1Mathieu Chartier if (arr == nullptr || env->GetArrayLength(data) < 9) { 27309b07a96094086e205948717666025909a75163bHiroshi Yamauchi return; 27409b07a96094086e205948717666025909a75163bHiroshi Yamauchi } 27509b07a96094086e205948717666025909a75163bHiroshi Yamauchi 27609b07a96094086e205948717666025909a75163bHiroshi Yamauchi size_t allocSize = 0; 27709b07a96094086e205948717666025909a75163bHiroshi Yamauchi size_t allocUsed = 0; 27809b07a96094086e205948717666025909a75163bHiroshi Yamauchi size_t zygoteSize = 0; 27909b07a96094086e205948717666025909a75163bHiroshi Yamauchi size_t zygoteUsed = 0; 28009b07a96094086e205948717666025909a75163bHiroshi Yamauchi size_t largeObjectsSize = 0; 28109b07a96094086e205948717666025909a75163bHiroshi Yamauchi size_t largeObjectsUsed = 0; 28209b07a96094086e205948717666025909a75163bHiroshi Yamauchi gc::Heap* heap = Runtime::Current()->GetHeap(); 2837410f29b4dae223befac036ea567d7f33351dad1Mathieu Chartier for (gc::space::ContinuousSpace* space : heap->GetContinuousSpaces()) { 28409b07a96094086e205948717666025909a75163bHiroshi Yamauchi if (space->IsImageSpace()) { 28509b07a96094086e205948717666025909a75163bHiroshi Yamauchi // Currently don't include the image space. 28609b07a96094086e205948717666025909a75163bHiroshi Yamauchi } else if (space->IsZygoteSpace()) { 2871f3b5358b28a83f0929bdd8ce738f06908677fb7Mathieu Chartier gc::space::ZygoteSpace* zygote_space = space->AsZygoteSpace(); 2881f3b5358b28a83f0929bdd8ce738f06908677fb7Mathieu Chartier zygoteSize += zygote_space->Size(); 2891f3b5358b28a83f0929bdd8ce738f06908677fb7Mathieu Chartier zygoteUsed += zygote_space->GetBytesAllocated(); 2907410f29b4dae223befac036ea567d7f33351dad1Mathieu Chartier } else if (space->IsMallocSpace()) { 2917410f29b4dae223befac036ea567d7f33351dad1Mathieu Chartier // This is a malloc space. 292cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi gc::space::MallocSpace* malloc_space = space->AsMallocSpace(); 293cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi allocSize += malloc_space->GetFootprint(); 294cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi allocUsed += malloc_space->GetBytesAllocated(); 2957410f29b4dae223befac036ea567d7f33351dad1Mathieu Chartier } else if (space->IsBumpPointerSpace()) { 296692fafd9778141fa6ef0048c9569abd7ee0253bfMathieu Chartier ScopedObjectAccess soa(env); 2977410f29b4dae223befac036ea567d7f33351dad1Mathieu Chartier gc::space::BumpPointerSpace* bump_pointer_space = space->AsBumpPointerSpace(); 2987410f29b4dae223befac036ea567d7f33351dad1Mathieu Chartier allocSize += bump_pointer_space->Size(); 2997410f29b4dae223befac036ea567d7f33351dad1Mathieu Chartier allocUsed += bump_pointer_space->GetBytesAllocated(); 30009b07a96094086e205948717666025909a75163bHiroshi Yamauchi } 30109b07a96094086e205948717666025909a75163bHiroshi Yamauchi } 3027410f29b4dae223befac036ea567d7f33351dad1Mathieu Chartier for (gc::space::DiscontinuousSpace* space : heap->GetDiscontinuousSpaces()) { 30309b07a96094086e205948717666025909a75163bHiroshi Yamauchi if (space->IsLargeObjectSpace()) { 30409b07a96094086e205948717666025909a75163bHiroshi Yamauchi largeObjectsSize += space->AsLargeObjectSpace()->GetBytesAllocated(); 30509b07a96094086e205948717666025909a75163bHiroshi Yamauchi largeObjectsUsed += largeObjectsSize; 30609b07a96094086e205948717666025909a75163bHiroshi Yamauchi } 30709b07a96094086e205948717666025909a75163bHiroshi Yamauchi } 30809b07a96094086e205948717666025909a75163bHiroshi Yamauchi 30909b07a96094086e205948717666025909a75163bHiroshi Yamauchi size_t allocFree = allocSize - allocUsed; 31009b07a96094086e205948717666025909a75163bHiroshi Yamauchi size_t zygoteFree = zygoteSize - zygoteUsed; 31109b07a96094086e205948717666025909a75163bHiroshi Yamauchi size_t largeObjectsFree = largeObjectsSize - largeObjectsUsed; 31209b07a96094086e205948717666025909a75163bHiroshi Yamauchi 31309b07a96094086e205948717666025909a75163bHiroshi Yamauchi int j = 0; 31409b07a96094086e205948717666025909a75163bHiroshi Yamauchi arr[j++] = allocSize; 31509b07a96094086e205948717666025909a75163bHiroshi Yamauchi arr[j++] = allocUsed; 31609b07a96094086e205948717666025909a75163bHiroshi Yamauchi arr[j++] = allocFree; 31709b07a96094086e205948717666025909a75163bHiroshi Yamauchi arr[j++] = zygoteSize; 31809b07a96094086e205948717666025909a75163bHiroshi Yamauchi arr[j++] = zygoteUsed; 31909b07a96094086e205948717666025909a75163bHiroshi Yamauchi arr[j++] = zygoteFree; 32009b07a96094086e205948717666025909a75163bHiroshi Yamauchi arr[j++] = largeObjectsSize; 32109b07a96094086e205948717666025909a75163bHiroshi Yamauchi arr[j++] = largeObjectsUsed; 32209b07a96094086e205948717666025909a75163bHiroshi Yamauchi arr[j++] = largeObjectsFree; 32309b07a96094086e205948717666025909a75163bHiroshi Yamauchi env->ReleasePrimitiveArrayCritical(data, arr, 0); 32409b07a96094086e205948717666025909a75163bHiroshi Yamauchi} 32509b07a96094086e205948717666025909a75163bHiroshi Yamauchi 3260512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic JNINativeMethod gMethods[] = { 3279d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes NATIVE_METHOD(VMDebug, countInstancesOfClass, "(Ljava/lang/Class;Z)J"), 3289d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes NATIVE_METHOD(VMDebug, crash, "()V"), 3299d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes NATIVE_METHOD(VMDebug, dumpHprofData, "(Ljava/lang/String;Ljava/io/FileDescriptor;)V"), 3309d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes NATIVE_METHOD(VMDebug, dumpHprofDataDdms, "()V"), 3319d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes NATIVE_METHOD(VMDebug, dumpReferenceTables, "()V"), 3329d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes NATIVE_METHOD(VMDebug, getAllocCount, "(I)I"), 33309b07a96094086e205948717666025909a75163bHiroshi Yamauchi NATIVE_METHOD(VMDebug, getHeapSpaceStats, "([J)V"), 3349d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes NATIVE_METHOD(VMDebug, getInstructionCount, "([I)V"), 33553b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers NATIVE_METHOD(VMDebug, getLoadedClassCount, "!()I"), 3369d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes NATIVE_METHOD(VMDebug, getVmFeatureList, "()[Ljava/lang/String;"), 3379d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes NATIVE_METHOD(VMDebug, infopoint, "(I)V"), 33853b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers NATIVE_METHOD(VMDebug, isDebuggerConnected, "!()Z"), 33953b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers NATIVE_METHOD(VMDebug, isDebuggingEnabled, "!()Z"), 34064caa7dcf46ed6139b766dbe77fbd7353899417fJeff Hao NATIVE_METHOD(VMDebug, getMethodTracingMode, "()I"), 34153b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers NATIVE_METHOD(VMDebug, lastDebuggerActivity, "!()J"), 34253b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers NATIVE_METHOD(VMDebug, printLoadedClasses, "!(I)V"), 3439d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes NATIVE_METHOD(VMDebug, resetAllocCount, "(I)V"), 3449d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes NATIVE_METHOD(VMDebug, resetInstructionCount, "()V"), 3459d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes NATIVE_METHOD(VMDebug, startAllocCounting, "()V"), 3469d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes NATIVE_METHOD(VMDebug, startEmulatorTracing, "()V"), 3479d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes NATIVE_METHOD(VMDebug, startInstructionCounting, "()V"), 34823009dca63c1699e28bfeaa8b45ca48fa0e86aceJeff Hao NATIVE_METHOD(VMDebug, startMethodTracingDdmsImpl, "(IIZI)V"), 3494044bdac490777cbc8a12d467bec675ef8aa6eb1Jeff Hao NATIVE_METHOD(VMDebug, startMethodTracingFd, "(Ljava/lang/String;Ljava/io/FileDescriptor;IIZI)V"), 3504044bdac490777cbc8a12d467bec675ef8aa6eb1Jeff Hao NATIVE_METHOD(VMDebug, startMethodTracingFilename, "(Ljava/lang/String;IIZI)V"), 3519d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes NATIVE_METHOD(VMDebug, stopAllocCounting, "()V"), 3529d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes NATIVE_METHOD(VMDebug, stopEmulatorTracing, "()V"), 3539d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes NATIVE_METHOD(VMDebug, stopInstructionCounting, "()V"), 3549d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes NATIVE_METHOD(VMDebug, stopMethodTracing, "()V"), 35553b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers NATIVE_METHOD(VMDebug, threadCpuTimeNanos, "!()J"), 3569d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}; 3579d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 3589d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughesvoid register_dalvik_system_VMDebug(JNIEnv* env) { 359eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes REGISTER_NATIVE_METHODS("dalvik/system/VMDebug"); 3609d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes} 3619d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes 3629d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes} // namespace art 363