blob: 70067fe0160e63289bd1f9f37d3b91d2bdf33588 [file] [log] [blame]
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001/*
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 Hughes9d5ccec2011-09-19 13:19:50 -070017#include <string.h>
18#include <unistd.h>
19
Elliott Hugheseac76672012-05-24 21:56:51 -070020#include "class_linker.h"
21#include "debugger.h"
22#include "hprof/hprof.h"
23#include "jni_internal.h"
24#include "ScopedUtfChars.h"
Ian Rogers365c1022012-06-22 15:05:28 -070025#include "scoped_jni_thread_state.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070026#include "toStringArray.h"
27#include "trace.h"
28
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070029namespace art {
30
Elliott Hughes0512f022012-03-15 22:10:52 -070031static jobjectArray VMDebug_getVmFeatureList(JNIEnv* env, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070032 std::vector<std::string> features;
jeffhaoe343b762011-12-05 16:36:44 -080033 features.push_back("method-trace-profiling");
34 features.push_back("method-trace-profiling-streaming");
Elliott Hughes767a1472011-10-26 18:49:02 -070035 features.push_back("hprof-heap-dump");
36 features.push_back("hprof-heap-dump-streaming");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070037 return toStringArray(env, features);
38}
39
Elliott Hughes0512f022012-03-15 22:10:52 -070040static void VMDebug_startAllocCounting(JNIEnv*, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070041 Runtime::Current()->SetStatsEnabled(true);
42}
43
Elliott Hughes0512f022012-03-15 22:10:52 -070044static void VMDebug_stopAllocCounting(JNIEnv*, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070045 Runtime::Current()->SetStatsEnabled(false);
46}
47
Elliott Hughes1bac54f2012-03-16 12:48:31 -070048static jint VMDebug_getAllocCount(JNIEnv*, jclass, jint kind) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070049 return Runtime::Current()->GetStat(kind);
50}
51
Elliott Hughes0512f022012-03-15 22:10:52 -070052static void VMDebug_resetAllocCount(JNIEnv*, jclass, jint kinds) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070053 Runtime::Current()->ResetStats(kinds);
54}
55
Elliott Hughes1bac54f2012-03-16 12:48:31 -070056static void VMDebug_startMethodTracingDdmsImpl(JNIEnv*, jclass, jint bufferSize, jint flags) {
jeffhaoe343b762011-12-05 16:36:44 -080057 Trace::Start("[DDMS]", -1, bufferSize, flags, true);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070058}
59
Elliott Hughes0512f022012-03-15 22:10:52 -070060static void VMDebug_startMethodTracingFd(JNIEnv* env, jclass, jstring javaTraceFilename, jobject javaFd, jint bufferSize, jint flags) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070061 int originalFd = jniGetFDFromFileDescriptor(env, javaFd);
62 if (originalFd < 0) {
63 return;
64 }
65
66 int fd = dup(originalFd);
67 if (fd < 0) {
Elliott Hugheseac76672012-05-24 21:56:51 -070068 Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "dup(%d) failed: %s", originalFd, strerror(errno));
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070069 return;
70 }
71
72 ScopedUtfChars traceFilename(env, javaTraceFilename);
73 if (traceFilename.c_str() == NULL) {
74 return;
75 }
jeffhaoe343b762011-12-05 16:36:44 -080076 Trace::Start(traceFilename.c_str(), fd, bufferSize, flags, false);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070077}
78
Elliott Hughes0512f022012-03-15 22:10:52 -070079static void VMDebug_startMethodTracingFilename(JNIEnv* env, jclass, jstring javaTraceFilename, jint bufferSize, jint flags) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070080 ScopedUtfChars traceFilename(env, javaTraceFilename);
81 if (traceFilename.c_str() == NULL) {
82 return;
83 }
jeffhaoe343b762011-12-05 16:36:44 -080084 Trace::Start(traceFilename.c_str(), -1, bufferSize, flags, false);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070085}
86
Elliott Hughes0512f022012-03-15 22:10:52 -070087static jboolean VMDebug_isMethodTracingActive(JNIEnv*, jclass) {
jeffhao2692b572011-12-16 15:42:28 -080088 return Runtime::Current()->IsMethodTracingActive();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070089}
90
Elliott Hughes0512f022012-03-15 22:10:52 -070091static void VMDebug_stopMethodTracing(JNIEnv*, jclass) {
jeffhaoe343b762011-12-05 16:36:44 -080092 Trace::Stop();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070093}
94
Elliott Hughes0512f022012-03-15 22:10:52 -070095static void VMDebug_startEmulatorTracing(JNIEnv*, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070096 UNIMPLEMENTED(WARNING);
97 //dvmEmulatorTraceStart();
98}
99
Elliott Hughes0512f022012-03-15 22:10:52 -0700100static void VMDebug_stopEmulatorTracing(JNIEnv*, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700101 UNIMPLEMENTED(WARNING);
102 //dvmEmulatorTraceStop();
103}
104
Elliott Hughes0512f022012-03-15 22:10:52 -0700105static jboolean VMDebug_isDebuggerConnected(JNIEnv*, jclass) {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700106 return Dbg::IsDebuggerActive();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700107}
108
Elliott Hughes0512f022012-03-15 22:10:52 -0700109static jboolean VMDebug_isDebuggingEnabled(JNIEnv*, jclass) {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700110 return Dbg::IsJdwpConfigured();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700111}
112
Elliott Hughes0512f022012-03-15 22:10:52 -0700113static jlong VMDebug_lastDebuggerActivity(JNIEnv*, jclass) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700114 return Dbg::LastDebuggerActivity();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700115}
116
Elliott Hugheseac76672012-05-24 21:56:51 -0700117static void VMDebug_startInstructionCounting(JNIEnv*, jclass) {
118 Thread::Current()->ThrowNewException("Ljava/lang/UnsupportedOperationException;", "");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700119}
120
Elliott Hugheseac76672012-05-24 21:56:51 -0700121static void VMDebug_stopInstructionCounting(JNIEnv*, jclass) {
122 Thread::Current()->ThrowNewException("Ljava/lang/UnsupportedOperationException;", "");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700123}
124
Elliott Hugheseac76672012-05-24 21:56:51 -0700125static void VMDebug_getInstructionCount(JNIEnv*, jclass, jintArray /*javaCounts*/) {
126 Thread::Current()->ThrowNewException("Ljava/lang/UnsupportedOperationException;", "");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700127}
128
Elliott Hugheseac76672012-05-24 21:56:51 -0700129static void VMDebug_resetInstructionCount(JNIEnv*, jclass) {
130 Thread::Current()->ThrowNewException("Ljava/lang/UnsupportedOperationException;", "");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700131}
132
Elliott Hughes0512f022012-03-15 22:10:52 -0700133static void VMDebug_printLoadedClasses(JNIEnv*, jclass, jint flags) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700134 return Runtime::Current()->GetClassLinker()->DumpAllClasses(flags);
135}
136
Elliott Hughes0512f022012-03-15 22:10:52 -0700137static jint VMDebug_getLoadedClassCount(JNIEnv*, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700138 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 Hughes0512f022012-03-15 22:10:52 -0700145static jlong VMDebug_threadCpuTimeNanos(JNIEnv*, jclass) {
146 return ThreadCpuNanoTime();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700147}
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 Hughes0512f022012-03-15 22:10:52 -0700155static void VMDebug_dumpHprofData(JNIEnv* env, jclass, jstring javaFilename, jobject javaFd) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700156 // Only one of these may be NULL.
157 if (javaFilename == NULL && javaFd == NULL) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700158 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "fileName == null && fd == null");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700159 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 Hugheseac76672012-05-24 21:56:51 -0700177 Thread::Current()->ThrowNewException("Ljava/lang/RuntimeException;", "Invalid file descriptor");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700178 return;
179 }
180 }
181
Elliott Hughes622a6982012-06-08 17:58:54 -0700182 hprof::DumpHeap(filename.c_str(), fd, false);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700183}
184
Elliott Hugheseac76672012-05-24 21:56:51 -0700185static void VMDebug_dumpHprofDataDdms(JNIEnv*, jclass) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700186 hprof::DumpHeap("[DDMS]", -1, true);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700187}
188
Elliott Hughes0512f022012-03-15 22:10:52 -0700189static void VMDebug_dumpReferenceTables(JNIEnv* env, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700190 LOG(INFO) << "--- reference table dump ---";
191
192 JNIEnvExt* e = reinterpret_cast<JNIEnvExt*>(env);
Elliott Hughes73e66f72012-05-09 09:34:45 -0700193 e->DumpReferenceTables(LOG(INFO));
194 e->vm->DumpReferenceTables(LOG(INFO));
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700195
196 LOG(INFO) << "---";
197}
198
Elliott Hughes0512f022012-03-15 22:10:52 -0700199static void VMDebug_crash(JNIEnv*, jclass) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700200 LOG(FATAL) << "Crashing runtime on request";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700201}
202
Elliott Hughes0512f022012-03-15 22:10:52 -0700203static void VMDebug_infopoint(JNIEnv*, jclass, jint id) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700204 LOG(INFO) << "VMDebug infopoint " << id << " hit";
205}
206
Elliott Hughes0512f022012-03-15 22:10:52 -0700207static jlong VMDebug_countInstancesOfClass(JNIEnv* env, jclass, jclass javaClass, jboolean countAssignable) {
Ian Rogers365c1022012-06-22 15:05:28 -0700208 ScopedJniThreadState ts(env);
209 Class* c = ts.Decode<Class*>(javaClass);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700210 if (c == NULL) {
211 return 0;
212 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800213 return Runtime::Current()->GetHeap()->CountInstances(c, countAssignable);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700214}
215
Elliott Hughes0512f022012-03-15 22:10:52 -0700216static JNINativeMethod gMethods[] = {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700217 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 Hughes9d5ccec2011-09-19 13:19:50 -0700247void register_dalvik_system_VMDebug(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700248 REGISTER_NATIVE_METHODS("dalvik/system/VMDebug");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700249}
250
251} // namespace art