blob: 317cd3d9168236f17060fccdb7fbdd7c79dc835c [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
17#include "class_linker.h"
Elliott Hughes872d4ec2011-10-21 17:07:15 -070018#include "debugger.h"
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070019#include "jni_internal.h"
jeffhaoe343b762011-12-05 16:36:44 -080020#include "trace.h"
Jesse Wilson1121e0b2011-11-07 15:37:42 -050021#include "hprof/hprof.h"
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070022#include "ScopedUtfChars.h"
23#include "toStringArray.h"
24
25#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
26
27#include <string.h>
28#include <unistd.h>
29
30namespace art {
31
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070032/*
33 * Return a set of strings describing available VM features (this is chiefly
34 * of interest to DDMS).
35 */
Elliott Hughes0512f022012-03-15 22:10:52 -070036static jobjectArray VMDebug_getVmFeatureList(JNIEnv* env, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070037 std::vector<std::string> features;
jeffhaoe343b762011-12-05 16:36:44 -080038 features.push_back("method-trace-profiling");
39 features.push_back("method-trace-profiling-streaming");
Elliott Hughes767a1472011-10-26 18:49:02 -070040 features.push_back("hprof-heap-dump");
41 features.push_back("hprof-heap-dump-streaming");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070042 return toStringArray(env, features);
43}
44
Elliott Hughes0512f022012-03-15 22:10:52 -070045static void VMDebug_startAllocCounting(JNIEnv*, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070046 Runtime::Current()->SetStatsEnabled(true);
47}
48
Elliott Hughes0512f022012-03-15 22:10:52 -070049static void VMDebug_stopAllocCounting(JNIEnv*, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070050 Runtime::Current()->SetStatsEnabled(false);
51}
52
Elliott Hughes0512f022012-03-15 22:10:52 -070053static jint VMDebug_getAllocCount(JNIEnv* env, jclass, jint kind) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070054 return Runtime::Current()->GetStat(kind);
55}
56
Elliott Hughes0512f022012-03-15 22:10:52 -070057static void VMDebug_resetAllocCount(JNIEnv*, jclass, jint kinds) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070058 Runtime::Current()->ResetStats(kinds);
59}
60
Elliott Hughes0512f022012-03-15 22:10:52 -070061static void VMDebug_startMethodTracingDdmsImpl(JNIEnv* env, jclass, jint bufferSize, jint flags) {
jeffhaoe343b762011-12-05 16:36:44 -080062 Trace::Start("[DDMS]", -1, bufferSize, flags, true);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070063}
64
Elliott Hughes0512f022012-03-15 22:10:52 -070065static void VMDebug_startMethodTracingFd(JNIEnv* env, jclass, jstring javaTraceFilename, jobject javaFd, jint bufferSize, jint flags) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070066 int originalFd = jniGetFDFromFileDescriptor(env, javaFd);
67 if (originalFd < 0) {
68 return;
69 }
70
71 int fd = dup(originalFd);
72 if (fd < 0) {
73 jniThrowExceptionFmt(env, "java/lang/RuntimeException", "dup(%d) failed: %s", originalFd, strerror(errno));
74 return;
75 }
76
77 ScopedUtfChars traceFilename(env, javaTraceFilename);
78 if (traceFilename.c_str() == NULL) {
79 return;
80 }
jeffhaoe343b762011-12-05 16:36:44 -080081 Trace::Start(traceFilename.c_str(), fd, bufferSize, flags, false);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070082}
83
Elliott Hughes0512f022012-03-15 22:10:52 -070084static void VMDebug_startMethodTracingFilename(JNIEnv* env, jclass, jstring javaTraceFilename, jint bufferSize, jint flags) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070085 ScopedUtfChars traceFilename(env, javaTraceFilename);
86 if (traceFilename.c_str() == NULL) {
87 return;
88 }
jeffhaoe343b762011-12-05 16:36:44 -080089 Trace::Start(traceFilename.c_str(), -1, bufferSize, flags, false);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070090}
91
Elliott Hughes0512f022012-03-15 22:10:52 -070092static jboolean VMDebug_isMethodTracingActive(JNIEnv*, jclass) {
jeffhao2692b572011-12-16 15:42:28 -080093 return Runtime::Current()->IsMethodTracingActive();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070094}
95
Elliott Hughes0512f022012-03-15 22:10:52 -070096static void VMDebug_stopMethodTracing(JNIEnv*, jclass) {
jeffhaoe343b762011-12-05 16:36:44 -080097 Trace::Stop();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070098}
99
Elliott Hughes0512f022012-03-15 22:10:52 -0700100static void VMDebug_startEmulatorTracing(JNIEnv*, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700101 UNIMPLEMENTED(WARNING);
102 //dvmEmulatorTraceStart();
103}
104
Elliott Hughes0512f022012-03-15 22:10:52 -0700105static void VMDebug_stopEmulatorTracing(JNIEnv*, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700106 UNIMPLEMENTED(WARNING);
107 //dvmEmulatorTraceStop();
108}
109
Elliott Hughes0512f022012-03-15 22:10:52 -0700110static jboolean VMDebug_isDebuggerConnected(JNIEnv*, jclass) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700111 return Dbg::IsDebuggerConnected();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700112}
113
Elliott Hughes0512f022012-03-15 22:10:52 -0700114static jboolean VMDebug_isDebuggingEnabled(JNIEnv*, jclass) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700115 return Dbg::IsDebuggingEnabled();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700116}
117
Elliott Hughes0512f022012-03-15 22:10:52 -0700118static jlong VMDebug_lastDebuggerActivity(JNIEnv*, jclass) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700119 return Dbg::LastDebuggerActivity();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700120}
121
Elliott Hughes0512f022012-03-15 22:10:52 -0700122static void VMDebug_startInstructionCounting(JNIEnv* env, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700123 jniThrowException(env, "java/lang/UnsupportedOperationException", NULL);
124}
125
Elliott Hughes0512f022012-03-15 22:10:52 -0700126static void VMDebug_stopInstructionCounting(JNIEnv* env, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700127 jniThrowException(env, "java/lang/UnsupportedOperationException", NULL);
128}
129
Elliott Hughes0512f022012-03-15 22:10:52 -0700130static void VMDebug_getInstructionCount(JNIEnv* env, jclass, jintArray javaCounts) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700131 jniThrowException(env, "java/lang/UnsupportedOperationException", NULL);
132}
133
Elliott Hughes0512f022012-03-15 22:10:52 -0700134static void VMDebug_resetInstructionCount(JNIEnv* env, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700135 jniThrowException(env, "java/lang/UnsupportedOperationException", NULL);
136}
137
Elliott Hughes0512f022012-03-15 22:10:52 -0700138static void VMDebug_printLoadedClasses(JNIEnv*, jclass, jint flags) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700139 return Runtime::Current()->GetClassLinker()->DumpAllClasses(flags);
140}
141
Elliott Hughes0512f022012-03-15 22:10:52 -0700142static jint VMDebug_getLoadedClassCount(JNIEnv*, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700143 return Runtime::Current()->GetClassLinker()->NumLoadedClasses();
144}
145
146/*
147 * Returns the thread-specific CPU-time clock value for the current thread,
148 * or -1 if the feature isn't supported.
149 */
Elliott Hughes0512f022012-03-15 22:10:52 -0700150static jlong VMDebug_threadCpuTimeNanos(JNIEnv*, jclass) {
151 return ThreadCpuNanoTime();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700152}
153
154/*
155 * static void dumpHprofData(String fileName, FileDescriptor fd)
156 *
157 * Cause "hprof" data to be dumped. We can throw an IOException if an
158 * error occurs during file handling.
159 */
Elliott Hughes0512f022012-03-15 22:10:52 -0700160static void VMDebug_dumpHprofData(JNIEnv* env, jclass, jstring javaFilename, jobject javaFd) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700161 // Only one of these may be NULL.
162 if (javaFilename == NULL && javaFd == NULL) {
163 jniThrowNullPointerException(env, "fileName == null && fd == null");
164 return;
165 }
166
167 std::string filename;
168 if (javaFilename != NULL) {
169 ScopedUtfChars chars(env, javaFilename);
170 if (env->ExceptionCheck()) {
171 return;
172 }
173 filename = chars.c_str();
174 } else {
175 filename = "[fd]";
176 }
177
178 int fd = -1;
179 if (javaFd != NULL) {
180 fd = jniGetFDFromFileDescriptor(env, javaFd);
181 if (fd < 0) {
182 jniThrowException(env, "Ljava/lang/RuntimeException;", "Invalid file descriptor");
183 return;
184 }
185 }
186
Elliott Hughesfbd84562011-11-07 18:56:13 -0800187 int result = hprof::DumpHeap(filename.c_str(), fd, false);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700188 if (result != 0) {
189 // TODO: ideally we'd throw something more specific based on actual failure
190 jniThrowException(env, "Ljava/lang/RuntimeException;", "Failure during heap dump; check log output for details");
191 return;
192 }
193}
194
Elliott Hughes0512f022012-03-15 22:10:52 -0700195static void VMDebug_dumpHprofDataDdms(JNIEnv* env, jclass) {
Elliott Hughesfbd84562011-11-07 18:56:13 -0800196 int result = hprof::DumpHeap("[DDMS]", -1, true);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700197 if (result != 0) {
198 // TODO: ideally we'd throw something more specific based on actual failure
199 jniThrowException(env, "Ljava/lang/RuntimeException;", "Failure during heap dump; check log output for details");
200 return;
201 }
202}
203
Elliott Hughes0512f022012-03-15 22:10:52 -0700204static void VMDebug_dumpReferenceTables(JNIEnv* env, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700205 LOG(INFO) << "--- reference table dump ---";
206
207 JNIEnvExt* e = reinterpret_cast<JNIEnvExt*>(env);
208 e->DumpReferenceTables();
209 e->vm->DumpReferenceTables();
210
211 LOG(INFO) << "---";
212}
213
214/*
215 * Dump the current thread's interpreted stack and abort the VM. Useful
216 * for seeing both interpreted and native stack traces.
217 */
Elliott Hughes0512f022012-03-15 22:10:52 -0700218static void VMDebug_crash(JNIEnv*, jclass) {
Elliott Hughes899e7892012-01-24 14:57:32 -0800219 LOG(FATAL) << "Crashing VM on request";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700220}
221
222/*
223 * Provide a hook for gdb to hang to so that the VM can be stopped when
224 * user-tagged source locations are being executed.
225 */
Elliott Hughes0512f022012-03-15 22:10:52 -0700226static void VMDebug_infopoint(JNIEnv*, jclass, jint id) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700227 LOG(INFO) << "VMDebug infopoint " << id << " hit";
228}
229
Elliott Hughes0512f022012-03-15 22:10:52 -0700230static jlong VMDebug_countInstancesOfClass(JNIEnv* env, jclass, jclass javaClass, jboolean countAssignable) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700231 Class* c = Decode<Class*>(env, javaClass);
232 if (c == NULL) {
233 return 0;
234 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800235 return Runtime::Current()->GetHeap()->CountInstances(c, countAssignable);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700236}
237
Elliott Hughes0512f022012-03-15 22:10:52 -0700238static JNINativeMethod gMethods[] = {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700239 NATIVE_METHOD(VMDebug, countInstancesOfClass, "(Ljava/lang/Class;Z)J"),
240 NATIVE_METHOD(VMDebug, crash, "()V"),
241 NATIVE_METHOD(VMDebug, dumpHprofData, "(Ljava/lang/String;Ljava/io/FileDescriptor;)V"),
242 NATIVE_METHOD(VMDebug, dumpHprofDataDdms, "()V"),
243 NATIVE_METHOD(VMDebug, dumpReferenceTables, "()V"),
244 NATIVE_METHOD(VMDebug, getAllocCount, "(I)I"),
245 NATIVE_METHOD(VMDebug, getInstructionCount, "([I)V"),
246 NATIVE_METHOD(VMDebug, getLoadedClassCount, "()I"),
247 NATIVE_METHOD(VMDebug, getVmFeatureList, "()[Ljava/lang/String;"),
248 NATIVE_METHOD(VMDebug, infopoint, "(I)V"),
249 NATIVE_METHOD(VMDebug, isDebuggerConnected, "()Z"),
250 NATIVE_METHOD(VMDebug, isDebuggingEnabled, "()Z"),
251 NATIVE_METHOD(VMDebug, isMethodTracingActive, "()Z"),
252 NATIVE_METHOD(VMDebug, lastDebuggerActivity, "()J"),
253 NATIVE_METHOD(VMDebug, printLoadedClasses, "(I)V"),
254 NATIVE_METHOD(VMDebug, resetAllocCount, "(I)V"),
255 NATIVE_METHOD(VMDebug, resetInstructionCount, "()V"),
256 NATIVE_METHOD(VMDebug, startAllocCounting, "()V"),
257 NATIVE_METHOD(VMDebug, startEmulatorTracing, "()V"),
258 NATIVE_METHOD(VMDebug, startInstructionCounting, "()V"),
259 NATIVE_METHOD(VMDebug, startMethodTracingDdmsImpl, "(II)V"),
260 NATIVE_METHOD(VMDebug, startMethodTracingFd, "(Ljava/lang/String;Ljava/io/FileDescriptor;II)V"),
261 NATIVE_METHOD(VMDebug, startMethodTracingFilename, "(Ljava/lang/String;II)V"),
262 NATIVE_METHOD(VMDebug, stopAllocCounting, "()V"),
263 NATIVE_METHOD(VMDebug, stopEmulatorTracing, "()V"),
264 NATIVE_METHOD(VMDebug, stopInstructionCounting, "()V"),
265 NATIVE_METHOD(VMDebug, stopMethodTracing, "()V"),
266 NATIVE_METHOD(VMDebug, threadCpuTimeNanos, "()J"),
267};
268
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700269void register_dalvik_system_VMDebug(JNIEnv* env) {
270 jniRegisterNativeMethods(env, "dalvik/system/VMDebug", gMethods, NELEM(gMethods));
271}
272
273} // namespace art