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