Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 17 | #include <string.h> |
| 18 | #include <unistd.h> |
| 19 | |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame^] | 20 | #include "class_linker.h" |
| 21 | #include "debugger.h" |
| 22 | #include "hprof/hprof.h" |
| 23 | #include "jni_internal.h" |
| 24 | #include "ScopedUtfChars.h" |
| 25 | #include "toStringArray.h" |
| 26 | #include "trace.h" |
| 27 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 28 | namespace art { |
| 29 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 30 | static jobjectArray VMDebug_getVmFeatureList(JNIEnv* env, jclass) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 31 | std::vector<std::string> features; |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 32 | features.push_back("method-trace-profiling"); |
| 33 | features.push_back("method-trace-profiling-streaming"); |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 34 | features.push_back("hprof-heap-dump"); |
| 35 | features.push_back("hprof-heap-dump-streaming"); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 36 | return toStringArray(env, features); |
| 37 | } |
| 38 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 39 | static void VMDebug_startAllocCounting(JNIEnv*, jclass) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 40 | Runtime::Current()->SetStatsEnabled(true); |
| 41 | } |
| 42 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 43 | static void VMDebug_stopAllocCounting(JNIEnv*, jclass) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 44 | Runtime::Current()->SetStatsEnabled(false); |
| 45 | } |
| 46 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 47 | static jint VMDebug_getAllocCount(JNIEnv*, jclass, jint kind) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 48 | return Runtime::Current()->GetStat(kind); |
| 49 | } |
| 50 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 51 | static void VMDebug_resetAllocCount(JNIEnv*, jclass, jint kinds) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 52 | Runtime::Current()->ResetStats(kinds); |
| 53 | } |
| 54 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 55 | static void VMDebug_startMethodTracingDdmsImpl(JNIEnv*, jclass, jint bufferSize, jint flags) { |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 56 | Trace::Start("[DDMS]", -1, bufferSize, flags, true); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 59 | static void VMDebug_startMethodTracingFd(JNIEnv* env, jclass, jstring javaTraceFilename, jobject javaFd, jint bufferSize, jint flags) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 60 | int originalFd = jniGetFDFromFileDescriptor(env, javaFd); |
| 61 | if (originalFd < 0) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | int fd = dup(originalFd); |
| 66 | if (fd < 0) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame^] | 67 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "dup(%d) failed: %s", originalFd, strerror(errno)); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 68 | return; |
| 69 | } |
| 70 | |
| 71 | ScopedUtfChars traceFilename(env, javaTraceFilename); |
| 72 | if (traceFilename.c_str() == NULL) { |
| 73 | return; |
| 74 | } |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 75 | Trace::Start(traceFilename.c_str(), fd, bufferSize, flags, false); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 78 | static void VMDebug_startMethodTracingFilename(JNIEnv* env, jclass, jstring javaTraceFilename, jint bufferSize, jint flags) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 79 | ScopedUtfChars traceFilename(env, javaTraceFilename); |
| 80 | if (traceFilename.c_str() == NULL) { |
| 81 | return; |
| 82 | } |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 83 | Trace::Start(traceFilename.c_str(), -1, bufferSize, flags, false); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 86 | static jboolean VMDebug_isMethodTracingActive(JNIEnv*, jclass) { |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 87 | return Runtime::Current()->IsMethodTracingActive(); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 90 | static void VMDebug_stopMethodTracing(JNIEnv*, jclass) { |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 91 | Trace::Stop(); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 94 | static void VMDebug_startEmulatorTracing(JNIEnv*, jclass) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 95 | UNIMPLEMENTED(WARNING); |
| 96 | //dvmEmulatorTraceStart(); |
| 97 | } |
| 98 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 99 | static void VMDebug_stopEmulatorTracing(JNIEnv*, jclass) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 100 | UNIMPLEMENTED(WARNING); |
| 101 | //dvmEmulatorTraceStop(); |
| 102 | } |
| 103 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 104 | static jboolean VMDebug_isDebuggerConnected(JNIEnv*, jclass) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 105 | return Dbg::IsDebuggerActive(); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 108 | static jboolean VMDebug_isDebuggingEnabled(JNIEnv*, jclass) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 109 | return Dbg::IsJdwpConfigured(); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 112 | static jlong VMDebug_lastDebuggerActivity(JNIEnv*, jclass) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 113 | return Dbg::LastDebuggerActivity(); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame^] | 116 | static void VMDebug_startInstructionCounting(JNIEnv*, jclass) { |
| 117 | Thread::Current()->ThrowNewException("Ljava/lang/UnsupportedOperationException;", ""); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame^] | 120 | static void VMDebug_stopInstructionCounting(JNIEnv*, jclass) { |
| 121 | Thread::Current()->ThrowNewException("Ljava/lang/UnsupportedOperationException;", ""); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame^] | 124 | static void VMDebug_getInstructionCount(JNIEnv*, jclass, jintArray /*javaCounts*/) { |
| 125 | Thread::Current()->ThrowNewException("Ljava/lang/UnsupportedOperationException;", ""); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame^] | 128 | static void VMDebug_resetInstructionCount(JNIEnv*, jclass) { |
| 129 | Thread::Current()->ThrowNewException("Ljava/lang/UnsupportedOperationException;", ""); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 132 | static void VMDebug_printLoadedClasses(JNIEnv*, jclass, jint flags) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 133 | return Runtime::Current()->GetClassLinker()->DumpAllClasses(flags); |
| 134 | } |
| 135 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 136 | static jint VMDebug_getLoadedClassCount(JNIEnv*, jclass) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 137 | return Runtime::Current()->GetClassLinker()->NumLoadedClasses(); |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Returns the thread-specific CPU-time clock value for the current thread, |
| 142 | * or -1 if the feature isn't supported. |
| 143 | */ |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 144 | static jlong VMDebug_threadCpuTimeNanos(JNIEnv*, jclass) { |
| 145 | return ThreadCpuNanoTime(); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* |
| 149 | * static void dumpHprofData(String fileName, FileDescriptor fd) |
| 150 | * |
| 151 | * Cause "hprof" data to be dumped. We can throw an IOException if an |
| 152 | * error occurs during file handling. |
| 153 | */ |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 154 | static void VMDebug_dumpHprofData(JNIEnv* env, jclass, jstring javaFilename, jobject javaFd) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 155 | // Only one of these may be NULL. |
| 156 | if (javaFilename == NULL && javaFd == NULL) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame^] | 157 | Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "fileName == null && fd == null"); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 158 | return; |
| 159 | } |
| 160 | |
| 161 | std::string filename; |
| 162 | if (javaFilename != NULL) { |
| 163 | ScopedUtfChars chars(env, javaFilename); |
| 164 | if (env->ExceptionCheck()) { |
| 165 | return; |
| 166 | } |
| 167 | filename = chars.c_str(); |
| 168 | } else { |
| 169 | filename = "[fd]"; |
| 170 | } |
| 171 | |
| 172 | int fd = -1; |
| 173 | if (javaFd != NULL) { |
| 174 | fd = jniGetFDFromFileDescriptor(env, javaFd); |
| 175 | if (fd < 0) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame^] | 176 | Thread::Current()->ThrowNewException("Ljava/lang/RuntimeException;", "Invalid file descriptor"); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 177 | return; |
| 178 | } |
| 179 | } |
| 180 | |
Elliott Hughes | fbd8456 | 2011-11-07 18:56:13 -0800 | [diff] [blame] | 181 | int result = hprof::DumpHeap(filename.c_str(), fd, false); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 182 | if (result != 0) { |
| 183 | // TODO: ideally we'd throw something more specific based on actual failure |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame^] | 184 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "Failure during heap dump; check log output for details: %d", result); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 185 | return; |
| 186 | } |
| 187 | } |
| 188 | |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame^] | 189 | static void VMDebug_dumpHprofDataDdms(JNIEnv*, jclass) { |
Elliott Hughes | fbd8456 | 2011-11-07 18:56:13 -0800 | [diff] [blame] | 190 | int result = hprof::DumpHeap("[DDMS]", -1, true); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 191 | if (result != 0) { |
| 192 | // TODO: ideally we'd throw something more specific based on actual failure |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame^] | 193 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "Failure during heap dump; check log output for details: %d", result); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 194 | return; |
| 195 | } |
| 196 | } |
| 197 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 198 | static void VMDebug_dumpReferenceTables(JNIEnv* env, jclass) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 199 | LOG(INFO) << "--- reference table dump ---"; |
| 200 | |
| 201 | JNIEnvExt* e = reinterpret_cast<JNIEnvExt*>(env); |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 202 | e->DumpReferenceTables(LOG(INFO)); |
| 203 | e->vm->DumpReferenceTables(LOG(INFO)); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 204 | |
| 205 | LOG(INFO) << "---"; |
| 206 | } |
| 207 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 208 | static void VMDebug_crash(JNIEnv*, jclass) { |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 209 | LOG(FATAL) << "Crashing runtime on request"; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 212 | static void VMDebug_infopoint(JNIEnv*, jclass, jint id) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 213 | LOG(INFO) << "VMDebug infopoint " << id << " hit"; |
| 214 | } |
| 215 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 216 | static jlong VMDebug_countInstancesOfClass(JNIEnv* env, jclass, jclass javaClass, jboolean countAssignable) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 217 | Class* c = Decode<Class*>(env, javaClass); |
| 218 | if (c == NULL) { |
| 219 | return 0; |
| 220 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 221 | return Runtime::Current()->GetHeap()->CountInstances(c, countAssignable); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 224 | static JNINativeMethod gMethods[] = { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 225 | NATIVE_METHOD(VMDebug, countInstancesOfClass, "(Ljava/lang/Class;Z)J"), |
| 226 | NATIVE_METHOD(VMDebug, crash, "()V"), |
| 227 | NATIVE_METHOD(VMDebug, dumpHprofData, "(Ljava/lang/String;Ljava/io/FileDescriptor;)V"), |
| 228 | NATIVE_METHOD(VMDebug, dumpHprofDataDdms, "()V"), |
| 229 | NATIVE_METHOD(VMDebug, dumpReferenceTables, "()V"), |
| 230 | NATIVE_METHOD(VMDebug, getAllocCount, "(I)I"), |
| 231 | NATIVE_METHOD(VMDebug, getInstructionCount, "([I)V"), |
| 232 | NATIVE_METHOD(VMDebug, getLoadedClassCount, "()I"), |
| 233 | NATIVE_METHOD(VMDebug, getVmFeatureList, "()[Ljava/lang/String;"), |
| 234 | NATIVE_METHOD(VMDebug, infopoint, "(I)V"), |
| 235 | NATIVE_METHOD(VMDebug, isDebuggerConnected, "()Z"), |
| 236 | NATIVE_METHOD(VMDebug, isDebuggingEnabled, "()Z"), |
| 237 | NATIVE_METHOD(VMDebug, isMethodTracingActive, "()Z"), |
| 238 | NATIVE_METHOD(VMDebug, lastDebuggerActivity, "()J"), |
| 239 | NATIVE_METHOD(VMDebug, printLoadedClasses, "(I)V"), |
| 240 | NATIVE_METHOD(VMDebug, resetAllocCount, "(I)V"), |
| 241 | NATIVE_METHOD(VMDebug, resetInstructionCount, "()V"), |
| 242 | NATIVE_METHOD(VMDebug, startAllocCounting, "()V"), |
| 243 | NATIVE_METHOD(VMDebug, startEmulatorTracing, "()V"), |
| 244 | NATIVE_METHOD(VMDebug, startInstructionCounting, "()V"), |
| 245 | NATIVE_METHOD(VMDebug, startMethodTracingDdmsImpl, "(II)V"), |
| 246 | NATIVE_METHOD(VMDebug, startMethodTracingFd, "(Ljava/lang/String;Ljava/io/FileDescriptor;II)V"), |
| 247 | NATIVE_METHOD(VMDebug, startMethodTracingFilename, "(Ljava/lang/String;II)V"), |
| 248 | NATIVE_METHOD(VMDebug, stopAllocCounting, "()V"), |
| 249 | NATIVE_METHOD(VMDebug, stopEmulatorTracing, "()V"), |
| 250 | NATIVE_METHOD(VMDebug, stopInstructionCounting, "()V"), |
| 251 | NATIVE_METHOD(VMDebug, stopMethodTracing, "()V"), |
| 252 | NATIVE_METHOD(VMDebug, threadCpuTimeNanos, "()J"), |
| 253 | }; |
| 254 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 255 | void register_dalvik_system_VMDebug(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame^] | 256 | REGISTER_NATIVE_METHODS("dalvik/system/VMDebug"); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | } // namespace art |