blob: e5a398ab4b82d7777eeff073b73132813d2dc4fb [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 Rogers00f7d0e2012-07-19 15:28:27 -070025#include "scoped_thread_state_change.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
Ian Rogers00f7d0e2012-07-19 15:28:27 -070060static void VMDebug_startMethodTracingFd(JNIEnv* env, jclass, jstring javaTraceFilename,
61 jobject javaFd, jint bufferSize, jint flags) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070062 int originalFd = jniGetFDFromFileDescriptor(env, javaFd);
63 if (originalFd < 0) {
64 return;
65 }
66
67 int fd = dup(originalFd);
68 if (fd < 0) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070069 ScopedObjectAccess soa(env);
70 Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;",
71 "dup(%d) failed: %s", originalFd, strerror(errno));
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070072 return;
73 }
74
75 ScopedUtfChars traceFilename(env, javaTraceFilename);
76 if (traceFilename.c_str() == NULL) {
77 return;
78 }
jeffhaoe343b762011-12-05 16:36:44 -080079 Trace::Start(traceFilename.c_str(), fd, bufferSize, flags, false);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070080}
81
Ian Rogers00f7d0e2012-07-19 15:28:27 -070082static void VMDebug_startMethodTracingFilename(JNIEnv* env, jclass, jstring javaTraceFilename,
83 jint bufferSize, jint flags) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070084 ScopedUtfChars traceFilename(env, javaTraceFilename);
85 if (traceFilename.c_str() == NULL) {
86 return;
87 }
jeffhaoe343b762011-12-05 16:36:44 -080088 Trace::Start(traceFilename.c_str(), -1, bufferSize, flags, false);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070089}
90
Elliott Hughes0512f022012-03-15 22:10:52 -070091static jboolean VMDebug_isMethodTracingActive(JNIEnv*, jclass) {
jeffhao2692b572011-12-16 15:42:28 -080092 return Runtime::Current()->IsMethodTracingActive();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070093}
94
Elliott Hughes0512f022012-03-15 22:10:52 -070095static void VMDebug_stopMethodTracing(JNIEnv*, jclass) {
jeffhaoe343b762011-12-05 16:36:44 -080096 Trace::Stop();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070097}
98
Elliott Hughes0512f022012-03-15 22:10:52 -070099static void VMDebug_startEmulatorTracing(JNIEnv*, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700100 UNIMPLEMENTED(WARNING);
101 //dvmEmulatorTraceStart();
102}
103
Elliott Hughes0512f022012-03-15 22:10:52 -0700104static void VMDebug_stopEmulatorTracing(JNIEnv*, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700105 UNIMPLEMENTED(WARNING);
106 //dvmEmulatorTraceStop();
107}
108
Elliott Hughes0512f022012-03-15 22:10:52 -0700109static jboolean VMDebug_isDebuggerConnected(JNIEnv*, jclass) {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700110 return Dbg::IsDebuggerActive();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700111}
112
Elliott Hughes0512f022012-03-15 22:10:52 -0700113static jboolean VMDebug_isDebuggingEnabled(JNIEnv*, jclass) {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700114 return Dbg::IsJdwpConfigured();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700115}
116
Elliott Hughes0512f022012-03-15 22:10:52 -0700117static jlong VMDebug_lastDebuggerActivity(JNIEnv*, jclass) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700118 return Dbg::LastDebuggerActivity();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700119}
120
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700121static void VMDebug_startInstructionCounting(JNIEnv* env, jclass) {
122 ScopedObjectAccess soa(env);
Elliott Hugheseac76672012-05-24 21:56:51 -0700123 Thread::Current()->ThrowNewException("Ljava/lang/UnsupportedOperationException;", "");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700124}
125
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700126static void VMDebug_stopInstructionCounting(JNIEnv* env, jclass) {
127 ScopedObjectAccess soa(env);
Elliott Hugheseac76672012-05-24 21:56:51 -0700128 Thread::Current()->ThrowNewException("Ljava/lang/UnsupportedOperationException;", "");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700129}
130
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700131static void VMDebug_getInstructionCount(JNIEnv* env, jclass, jintArray /*javaCounts*/) {
132 ScopedObjectAccess soa(env);
Elliott Hugheseac76672012-05-24 21:56:51 -0700133 Thread::Current()->ThrowNewException("Ljava/lang/UnsupportedOperationException;", "");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700134}
135
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700136static void VMDebug_resetInstructionCount(JNIEnv* env, jclass) {
137 ScopedObjectAccess soa(env);
Elliott Hugheseac76672012-05-24 21:56:51 -0700138 Thread::Current()->ThrowNewException("Ljava/lang/UnsupportedOperationException;", "");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700139}
140
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700141static void VMDebug_printLoadedClasses(JNIEnv* env, jclass, jint flags) {
142 ScopedObjectAccess soa(env);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700143 return Runtime::Current()->GetClassLinker()->DumpAllClasses(flags);
144}
145
Elliott Hughes0512f022012-03-15 22:10:52 -0700146static jint VMDebug_getLoadedClassCount(JNIEnv*, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700147 return Runtime::Current()->GetClassLinker()->NumLoadedClasses();
148}
149
150/*
151 * Returns the thread-specific CPU-time clock value for the current thread,
152 * or -1 if the feature isn't supported.
153 */
Elliott Hughes0512f022012-03-15 22:10:52 -0700154static jlong VMDebug_threadCpuTimeNanos(JNIEnv*, jclass) {
155 return ThreadCpuNanoTime();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700156}
157
158/*
159 * static void dumpHprofData(String fileName, FileDescriptor fd)
160 *
161 * Cause "hprof" data to be dumped. We can throw an IOException if an
162 * error occurs during file handling.
163 */
Elliott Hughes0512f022012-03-15 22:10:52 -0700164static void VMDebug_dumpHprofData(JNIEnv* env, jclass, jstring javaFilename, jobject javaFd) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700165 // Only one of these may be NULL.
166 if (javaFilename == NULL && javaFd == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700167 ScopedObjectAccess soa(env);
168 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;",
169 "fileName == null && fd == null");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700170 return;
171 }
172
173 std::string filename;
174 if (javaFilename != NULL) {
175 ScopedUtfChars chars(env, javaFilename);
176 if (env->ExceptionCheck()) {
177 return;
178 }
179 filename = chars.c_str();
180 } else {
181 filename = "[fd]";
182 }
183
184 int fd = -1;
185 if (javaFd != NULL) {
186 fd = jniGetFDFromFileDescriptor(env, javaFd);
187 if (fd < 0) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700188 ScopedObjectAccess soa(env);
189 Thread::Current()->ThrowNewException("Ljava/lang/RuntimeException;",
190 "Invalid file descriptor");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700191 return;
192 }
193 }
194
Elliott Hughes622a6982012-06-08 17:58:54 -0700195 hprof::DumpHeap(filename.c_str(), fd, false);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700196}
197
Elliott Hugheseac76672012-05-24 21:56:51 -0700198static void VMDebug_dumpHprofDataDdms(JNIEnv*, jclass) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700199 hprof::DumpHeap("[DDMS]", -1, true);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700200}
201
Elliott Hughes0512f022012-03-15 22:10:52 -0700202static void VMDebug_dumpReferenceTables(JNIEnv* env, jclass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700203 ScopedObjectAccess soa(env);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700204 LOG(INFO) << "--- reference table dump ---";
205
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700206 soa.Env()->DumpReferenceTables(LOG(INFO));
207 soa.Vm()->DumpReferenceTables(LOG(INFO));
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700208
209 LOG(INFO) << "---";
210}
211
Elliott Hughes0512f022012-03-15 22:10:52 -0700212static void VMDebug_crash(JNIEnv*, jclass) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700213 LOG(FATAL) << "Crashing runtime on request";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700214}
215
Elliott Hughes0512f022012-03-15 22:10:52 -0700216static void VMDebug_infopoint(JNIEnv*, jclass, jint id) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700217 LOG(INFO) << "VMDebug infopoint " << id << " hit";
218}
219
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700220static jlong VMDebug_countInstancesOfClass(JNIEnv* env, jclass, jclass javaClass,
221 jboolean countAssignable) {
222 ScopedObjectAccess soa(env);
223 Class* c = soa.Decode<Class*>(javaClass);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700224 if (c == NULL) {
225 return 0;
226 }
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800227 std::vector<Class*> classes;
228 classes.push_back(c);
229 uint64_t count = 0;
230 Runtime::Current()->GetHeap()->CountInstances(classes, countAssignable, &count);
231 return count;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700232}
233
Elliott Hughes0512f022012-03-15 22:10:52 -0700234static JNINativeMethod gMethods[] = {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700235 NATIVE_METHOD(VMDebug, countInstancesOfClass, "(Ljava/lang/Class;Z)J"),
236 NATIVE_METHOD(VMDebug, crash, "()V"),
237 NATIVE_METHOD(VMDebug, dumpHprofData, "(Ljava/lang/String;Ljava/io/FileDescriptor;)V"),
238 NATIVE_METHOD(VMDebug, dumpHprofDataDdms, "()V"),
239 NATIVE_METHOD(VMDebug, dumpReferenceTables, "()V"),
240 NATIVE_METHOD(VMDebug, getAllocCount, "(I)I"),
241 NATIVE_METHOD(VMDebug, getInstructionCount, "([I)V"),
242 NATIVE_METHOD(VMDebug, getLoadedClassCount, "()I"),
243 NATIVE_METHOD(VMDebug, getVmFeatureList, "()[Ljava/lang/String;"),
244 NATIVE_METHOD(VMDebug, infopoint, "(I)V"),
245 NATIVE_METHOD(VMDebug, isDebuggerConnected, "()Z"),
246 NATIVE_METHOD(VMDebug, isDebuggingEnabled, "()Z"),
247 NATIVE_METHOD(VMDebug, isMethodTracingActive, "()Z"),
248 NATIVE_METHOD(VMDebug, lastDebuggerActivity, "()J"),
249 NATIVE_METHOD(VMDebug, printLoadedClasses, "(I)V"),
250 NATIVE_METHOD(VMDebug, resetAllocCount, "(I)V"),
251 NATIVE_METHOD(VMDebug, resetInstructionCount, "()V"),
252 NATIVE_METHOD(VMDebug, startAllocCounting, "()V"),
253 NATIVE_METHOD(VMDebug, startEmulatorTracing, "()V"),
254 NATIVE_METHOD(VMDebug, startInstructionCounting, "()V"),
255 NATIVE_METHOD(VMDebug, startMethodTracingDdmsImpl, "(II)V"),
256 NATIVE_METHOD(VMDebug, startMethodTracingFd, "(Ljava/lang/String;Ljava/io/FileDescriptor;II)V"),
257 NATIVE_METHOD(VMDebug, startMethodTracingFilename, "(Ljava/lang/String;II)V"),
258 NATIVE_METHOD(VMDebug, stopAllocCounting, "()V"),
259 NATIVE_METHOD(VMDebug, stopEmulatorTracing, "()V"),
260 NATIVE_METHOD(VMDebug, stopInstructionCounting, "()V"),
261 NATIVE_METHOD(VMDebug, stopMethodTracing, "()V"),
262 NATIVE_METHOD(VMDebug, threadCpuTimeNanos, "()J"),
263};
264
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700265void register_dalvik_system_VMDebug(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700266 REGISTER_NATIVE_METHODS("dalvik/system/VMDebug");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700267}
268
269} // namespace art