blob: 7461fc478709cbbce6831acb4346250481ba8867 [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"
Jesse Wilson1121e0b2011-11-07 15:37:42 -050020#include "hprof/hprof.h"
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070021#include "ScopedUtfChars.h"
22#include "toStringArray.h"
23
24#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
25
26#include <string.h>
27#include <unistd.h>
28
29namespace art {
30
31namespace {
32
33/*
34 * Return a set of strings describing available VM features (this is chiefly
35 * of interest to DDMS).
36 */
37jobjectArray VMDebug_getVmFeatureList(JNIEnv* env, jclass) {
38 std::vector<std::string> features;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -070039 // TODO: we might need to uncomment these to make them work.
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070040 //features.push_back("method-trace-profiling");
41 //features.push_back("method-trace-profiling-streaming");
Elliott Hughes767a1472011-10-26 18:49:02 -070042 features.push_back("hprof-heap-dump");
43 features.push_back("hprof-heap-dump-streaming");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070044 return toStringArray(env, features);
45}
46
47void VMDebug_startAllocCounting(JNIEnv*, jclass) {
48 Runtime::Current()->SetStatsEnabled(true);
49}
50
51void VMDebug_stopAllocCounting(JNIEnv*, jclass) {
52 Runtime::Current()->SetStatsEnabled(false);
53}
54
55jint VMDebug_getAllocCount(JNIEnv* env, jclass, jint kind) {
56 return Runtime::Current()->GetStat(kind);
57}
58
59void VMDebug_resetAllocCount(JNIEnv*, jclass, jint kinds) {
60 Runtime::Current()->ResetStats(kinds);
61}
62
63void VMDebug_startMethodTracingDdmsImpl(JNIEnv* env, jclass, jint bufferSize, jint flags) {
64 UNIMPLEMENTED(WARNING);
65 //dvmMethodTraceStart("[DDMS]", -1, bufferSize, flags, true);
66}
67
68void VMDebug_startMethodTracingFd(JNIEnv* env, jclass, jstring javaTraceFilename, jobject javaFd, jint bufferSize, jint flags) {
69 int originalFd = jniGetFDFromFileDescriptor(env, javaFd);
70 if (originalFd < 0) {
71 return;
72 }
73
74 int fd = dup(originalFd);
75 if (fd < 0) {
76 jniThrowExceptionFmt(env, "java/lang/RuntimeException", "dup(%d) failed: %s", originalFd, strerror(errno));
77 return;
78 }
79
80 ScopedUtfChars traceFilename(env, javaTraceFilename);
81 if (traceFilename.c_str() == NULL) {
82 return;
83 }
84 UNIMPLEMENTED(WARNING);
85 //dvmMethodTraceStart(traceFilename.c_str(), fd, bufferSize, flags, false);
86}
87
88void VMDebug_startMethodTracingFilename(JNIEnv* env, jclass, jstring javaTraceFilename, jint bufferSize, jint flags) {
89 ScopedUtfChars traceFilename(env, javaTraceFilename);
90 if (traceFilename.c_str() == NULL) {
91 return;
92 }
93 UNIMPLEMENTED(WARNING);
94 //dvmMethodTraceStart(traceFilename.c_str(), -1, bufferSize, flags, false);
95}
96
97jboolean VMDebug_isMethodTracingActive(JNIEnv*, jclass) {
98 UNIMPLEMENTED(WARNING);
99 return JNI_FALSE; //dvmIsMethodTraceActive();
100}
101
102void VMDebug_stopMethodTracing(JNIEnv*, jclass) {
103 UNIMPLEMENTED(WARNING);
104 //dvmMethodTraceStop();
105}
106
107void VMDebug_startEmulatorTracing(JNIEnv*, jclass) {
108 UNIMPLEMENTED(WARNING);
109 //dvmEmulatorTraceStart();
110}
111
112void VMDebug_stopEmulatorTracing(JNIEnv*, jclass) {
113 UNIMPLEMENTED(WARNING);
114 //dvmEmulatorTraceStop();
115}
116
117jboolean VMDebug_isDebuggerConnected(JNIEnv*, jclass) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700118 return Dbg::IsDebuggerConnected();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700119}
120
121jboolean VMDebug_isDebuggingEnabled(JNIEnv*, jclass) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700122 return Dbg::IsDebuggingEnabled();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700123}
124
125jlong VMDebug_lastDebuggerActivity(JNIEnv*, jclass) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700126 return Dbg::LastDebuggerActivity();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700127}
128
129void VMDebug_startInstructionCounting(JNIEnv* env, jclass) {
130 jniThrowException(env, "java/lang/UnsupportedOperationException", NULL);
131}
132
133void VMDebug_stopInstructionCounting(JNIEnv* env, jclass) {
134 jniThrowException(env, "java/lang/UnsupportedOperationException", NULL);
135}
136
137void VMDebug_getInstructionCount(JNIEnv* env, jclass, jintArray javaCounts) {
138 jniThrowException(env, "java/lang/UnsupportedOperationException", NULL);
139}
140
141void VMDebug_resetInstructionCount(JNIEnv* env, jclass) {
142 jniThrowException(env, "java/lang/UnsupportedOperationException", NULL);
143}
144
145void VMDebug_printLoadedClasses(JNIEnv*, jclass, jint flags) {
146 return Runtime::Current()->GetClassLinker()->DumpAllClasses(flags);
147}
148
149jint VMDebug_getLoadedClassCount(JNIEnv*, jclass) {
150 return Runtime::Current()->GetClassLinker()->NumLoadedClasses();
151}
152
153/*
154 * Returns the thread-specific CPU-time clock value for the current thread,
155 * or -1 if the feature isn't supported.
156 */
157jlong VMDebug_threadCpuTimeNanos(JNIEnv*, jclass) {
158#ifdef HAVE_POSIX_CLOCKS
159 struct timespec now;
160 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &now);
161 return static_cast<jlong>(now.tv_sec*1000000000LL + now.tv_nsec);
162#else
163 return -1LL;
164#endif
165}
166
167/*
168 * static void dumpHprofData(String fileName, FileDescriptor fd)
169 *
170 * Cause "hprof" data to be dumped. We can throw an IOException if an
171 * error occurs during file handling.
172 */
173void VMDebug_dumpHprofData(JNIEnv* env, jclass, jstring javaFilename, jobject javaFd) {
174 // Only one of these may be NULL.
175 if (javaFilename == NULL && javaFd == NULL) {
176 jniThrowNullPointerException(env, "fileName == null && fd == null");
177 return;
178 }
179
180 std::string filename;
181 if (javaFilename != NULL) {
182 ScopedUtfChars chars(env, javaFilename);
183 if (env->ExceptionCheck()) {
184 return;
185 }
186 filename = chars.c_str();
187 } else {
188 filename = "[fd]";
189 }
190
191 int fd = -1;
192 if (javaFd != NULL) {
193 fd = jniGetFDFromFileDescriptor(env, javaFd);
194 if (fd < 0) {
195 jniThrowException(env, "Ljava/lang/RuntimeException;", "Invalid file descriptor");
196 return;
197 }
198 }
199
Jesse Wilson1121e0b2011-11-07 15:37:42 -0500200 int result = hprof::hprofDumpHeap(filename.c_str(), fd, false);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700201 if (result != 0) {
202 // TODO: ideally we'd throw something more specific based on actual failure
203 jniThrowException(env, "Ljava/lang/RuntimeException;", "Failure during heap dump; check log output for details");
204 return;
205 }
206}
207
208void VMDebug_dumpHprofDataDdms(JNIEnv* env, jclass) {
209 UNIMPLEMENTED(WARNING);
Jesse Wilson1121e0b2011-11-07 15:37:42 -0500210 int result = hprof::hprofDumpHeap("[DDMS]", -1, true);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700211 if (result != 0) {
212 // TODO: ideally we'd throw something more specific based on actual failure
213 jniThrowException(env, "Ljava/lang/RuntimeException;", "Failure during heap dump; check log output for details");
214 return;
215 }
216}
217
218void VMDebug_dumpReferenceTables(JNIEnv* env, jclass) {
219 LOG(INFO) << "--- reference table dump ---";
220
221 JNIEnvExt* e = reinterpret_cast<JNIEnvExt*>(env);
222 e->DumpReferenceTables();
223 e->vm->DumpReferenceTables();
224
225 LOG(INFO) << "---";
226}
227
228/*
229 * Dump the current thread's interpreted stack and abort the VM. Useful
230 * for seeing both interpreted and native stack traces.
231 */
232void VMDebug_crash(JNIEnv*, jclass) {
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700233 std::ostringstream os;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700234 os << "Crashing VM on request:\n";
235 Thread::Current()->Dump(os);
236 LOG(FATAL) << os.str();
237}
238
239/*
240 * Provide a hook for gdb to hang to so that the VM can be stopped when
241 * user-tagged source locations are being executed.
242 */
243void VMDebug_infopoint(JNIEnv*, jclass, jint id) {
244 LOG(INFO) << "VMDebug infopoint " << id << " hit";
245}
246
247jlong VMDebug_countInstancesOfClass(JNIEnv* env, jclass, jclass javaClass, jboolean countAssignable) {
248 Class* c = Decode<Class*>(env, javaClass);
249 if (c == NULL) {
250 return 0;
251 }
252 return Heap::CountInstances(c, countAssignable);
253}
254
255JNINativeMethod gMethods[] = {
256 NATIVE_METHOD(VMDebug, countInstancesOfClass, "(Ljava/lang/Class;Z)J"),
257 NATIVE_METHOD(VMDebug, crash, "()V"),
258 NATIVE_METHOD(VMDebug, dumpHprofData, "(Ljava/lang/String;Ljava/io/FileDescriptor;)V"),
259 NATIVE_METHOD(VMDebug, dumpHprofDataDdms, "()V"),
260 NATIVE_METHOD(VMDebug, dumpReferenceTables, "()V"),
261 NATIVE_METHOD(VMDebug, getAllocCount, "(I)I"),
262 NATIVE_METHOD(VMDebug, getInstructionCount, "([I)V"),
263 NATIVE_METHOD(VMDebug, getLoadedClassCount, "()I"),
264 NATIVE_METHOD(VMDebug, getVmFeatureList, "()[Ljava/lang/String;"),
265 NATIVE_METHOD(VMDebug, infopoint, "(I)V"),
266 NATIVE_METHOD(VMDebug, isDebuggerConnected, "()Z"),
267 NATIVE_METHOD(VMDebug, isDebuggingEnabled, "()Z"),
268 NATIVE_METHOD(VMDebug, isMethodTracingActive, "()Z"),
269 NATIVE_METHOD(VMDebug, lastDebuggerActivity, "()J"),
270 NATIVE_METHOD(VMDebug, printLoadedClasses, "(I)V"),
271 NATIVE_METHOD(VMDebug, resetAllocCount, "(I)V"),
272 NATIVE_METHOD(VMDebug, resetInstructionCount, "()V"),
273 NATIVE_METHOD(VMDebug, startAllocCounting, "()V"),
274 NATIVE_METHOD(VMDebug, startEmulatorTracing, "()V"),
275 NATIVE_METHOD(VMDebug, startInstructionCounting, "()V"),
276 NATIVE_METHOD(VMDebug, startMethodTracingDdmsImpl, "(II)V"),
277 NATIVE_METHOD(VMDebug, startMethodTracingFd, "(Ljava/lang/String;Ljava/io/FileDescriptor;II)V"),
278 NATIVE_METHOD(VMDebug, startMethodTracingFilename, "(Ljava/lang/String;II)V"),
279 NATIVE_METHOD(VMDebug, stopAllocCounting, "()V"),
280 NATIVE_METHOD(VMDebug, stopEmulatorTracing, "()V"),
281 NATIVE_METHOD(VMDebug, stopInstructionCounting, "()V"),
282 NATIVE_METHOD(VMDebug, stopMethodTracing, "()V"),
283 NATIVE_METHOD(VMDebug, threadCpuTimeNanos, "()J"),
284};
285
286} // namespace
287
288void register_dalvik_system_VMDebug(JNIEnv* env) {
289 jniRegisterNativeMethods(env, "dalvik/system/VMDebug", gMethods, NELEM(gMethods));
290}
291
292} // namespace art