blob: dc07a31ee31ba19c90dedf939ceeea61ec8a33a1 [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "mirror/class.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070025#include "ScopedUtfChars.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070026#include "scoped_thread_state_change.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070027#include "toStringArray.h"
28#include "trace.h"
29
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070030namespace art {
31
Elliott Hughes0512f022012-03-15 22:10:52 -070032static jobjectArray VMDebug_getVmFeatureList(JNIEnv* env, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070033 std::vector<std::string> features;
jeffhaoe343b762011-12-05 16:36:44 -080034 features.push_back("method-trace-profiling");
35 features.push_back("method-trace-profiling-streaming");
Elliott Hughes767a1472011-10-26 18:49:02 -070036 features.push_back("hprof-heap-dump");
37 features.push_back("hprof-heap-dump-streaming");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070038 return toStringArray(env, features);
39}
40
Elliott Hughes0512f022012-03-15 22:10:52 -070041static void VMDebug_startAllocCounting(JNIEnv*, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070042 Runtime::Current()->SetStatsEnabled(true);
43}
44
Elliott Hughes0512f022012-03-15 22:10:52 -070045static void VMDebug_stopAllocCounting(JNIEnv*, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070046 Runtime::Current()->SetStatsEnabled(false);
47}
48
Elliott Hughes1bac54f2012-03-16 12:48:31 -070049static jint VMDebug_getAllocCount(JNIEnv*, jclass, jint kind) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070050 return Runtime::Current()->GetStat(kind);
51}
52
Elliott Hughes0512f022012-03-15 22:10:52 -070053static void VMDebug_resetAllocCount(JNIEnv*, jclass, jint kinds) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070054 Runtime::Current()->ResetStats(kinds);
55}
56
Elliott Hughes1bac54f2012-03-16 12:48:31 -070057static void VMDebug_startMethodTracingDdmsImpl(JNIEnv*, jclass, jint bufferSize, jint flags) {
jeffhaoe343b762011-12-05 16:36:44 -080058 Trace::Start("[DDMS]", -1, bufferSize, flags, true);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070059}
60
Ian Rogers00f7d0e2012-07-19 15:28:27 -070061static void VMDebug_startMethodTracingFd(JNIEnv* env, jclass, jstring javaTraceFilename,
62 jobject javaFd, jint bufferSize, jint flags) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070063 int originalFd = jniGetFDFromFileDescriptor(env, javaFd);
64 if (originalFd < 0) {
65 return;
66 }
67
68 int fd = dup(originalFd);
69 if (fd < 0) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070070 ScopedObjectAccess soa(env);
71 Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;",
72 "dup(%d) failed: %s", originalFd, strerror(errno));
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070073 return;
74 }
75
76 ScopedUtfChars traceFilename(env, javaTraceFilename);
77 if (traceFilename.c_str() == NULL) {
78 return;
79 }
jeffhaoe343b762011-12-05 16:36:44 -080080 Trace::Start(traceFilename.c_str(), fd, bufferSize, flags, false);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070081}
82
Ian Rogers00f7d0e2012-07-19 15:28:27 -070083static void VMDebug_startMethodTracingFilename(JNIEnv* env, jclass, jstring javaTraceFilename,
84 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 Hughesc0f09332012-03-26 13:27:06 -0700111 return Dbg::IsDebuggerActive();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700112}
113
Elliott Hughes0512f022012-03-15 22:10:52 -0700114static jboolean VMDebug_isDebuggingEnabled(JNIEnv*, jclass) {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700115 return Dbg::IsJdwpConfigured();
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
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700122static void VMDebug_startInstructionCounting(JNIEnv* env, jclass) {
123 ScopedObjectAccess soa(env);
Elliott Hugheseac76672012-05-24 21:56:51 -0700124 Thread::Current()->ThrowNewException("Ljava/lang/UnsupportedOperationException;", "");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700125}
126
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700127static void VMDebug_stopInstructionCounting(JNIEnv* env, jclass) {
128 ScopedObjectAccess soa(env);
Elliott Hugheseac76672012-05-24 21:56:51 -0700129 Thread::Current()->ThrowNewException("Ljava/lang/UnsupportedOperationException;", "");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700130}
131
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700132static void VMDebug_getInstructionCount(JNIEnv* env, jclass, jintArray /*javaCounts*/) {
133 ScopedObjectAccess soa(env);
Elliott Hugheseac76672012-05-24 21:56:51 -0700134 Thread::Current()->ThrowNewException("Ljava/lang/UnsupportedOperationException;", "");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700135}
136
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700137static void VMDebug_resetInstructionCount(JNIEnv* env, jclass) {
138 ScopedObjectAccess soa(env);
Elliott Hugheseac76672012-05-24 21:56:51 -0700139 Thread::Current()->ThrowNewException("Ljava/lang/UnsupportedOperationException;", "");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700140}
141
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700142static void VMDebug_printLoadedClasses(JNIEnv* env, jclass, jint flags) {
143 ScopedObjectAccess soa(env);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700144 return Runtime::Current()->GetClassLinker()->DumpAllClasses(flags);
145}
146
Elliott Hughes0512f022012-03-15 22:10:52 -0700147static jint VMDebug_getLoadedClassCount(JNIEnv*, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700148 return Runtime::Current()->GetClassLinker()->NumLoadedClasses();
149}
150
151/*
152 * Returns the thread-specific CPU-time clock value for the current thread,
153 * or -1 if the feature isn't supported.
154 */
Elliott Hughes0512f022012-03-15 22:10:52 -0700155static jlong VMDebug_threadCpuTimeNanos(JNIEnv*, jclass) {
156 return ThreadCpuNanoTime();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700157}
158
159/*
160 * static void dumpHprofData(String fileName, FileDescriptor fd)
161 *
162 * Cause "hprof" data to be dumped. We can throw an IOException if an
163 * error occurs during file handling.
164 */
Elliott Hughes0512f022012-03-15 22:10:52 -0700165static void VMDebug_dumpHprofData(JNIEnv* env, jclass, jstring javaFilename, jobject javaFd) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700166 // Only one of these may be NULL.
167 if (javaFilename == NULL && javaFd == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700168 ScopedObjectAccess soa(env);
169 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;",
170 "fileName == null && fd == null");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700171 return;
172 }
173
174 std::string filename;
175 if (javaFilename != NULL) {
176 ScopedUtfChars chars(env, javaFilename);
177 if (env->ExceptionCheck()) {
178 return;
179 }
180 filename = chars.c_str();
181 } else {
182 filename = "[fd]";
183 }
184
185 int fd = -1;
186 if (javaFd != NULL) {
187 fd = jniGetFDFromFileDescriptor(env, javaFd);
188 if (fd < 0) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700189 ScopedObjectAccess soa(env);
190 Thread::Current()->ThrowNewException("Ljava/lang/RuntimeException;",
191 "Invalid file descriptor");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700192 return;
193 }
194 }
195
Elliott Hughes622a6982012-06-08 17:58:54 -0700196 hprof::DumpHeap(filename.c_str(), fd, false);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700197}
198
Elliott Hugheseac76672012-05-24 21:56:51 -0700199static void VMDebug_dumpHprofDataDdms(JNIEnv*, jclass) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700200 hprof::DumpHeap("[DDMS]", -1, true);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700201}
202
Elliott Hughes0512f022012-03-15 22:10:52 -0700203static void VMDebug_dumpReferenceTables(JNIEnv* env, jclass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700204 ScopedObjectAccess soa(env);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700205 LOG(INFO) << "--- reference table dump ---";
206
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700207 soa.Env()->DumpReferenceTables(LOG(INFO));
208 soa.Vm()->DumpReferenceTables(LOG(INFO));
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700209
210 LOG(INFO) << "---";
211}
212
Elliott Hughes0512f022012-03-15 22:10:52 -0700213static void VMDebug_crash(JNIEnv*, jclass) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700214 LOG(FATAL) << "Crashing runtime on request";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700215}
216
Elliott Hughes0512f022012-03-15 22:10:52 -0700217static void VMDebug_infopoint(JNIEnv*, jclass, jint id) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700218 LOG(INFO) << "VMDebug infopoint " << id << " hit";
219}
220
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700221static jlong VMDebug_countInstancesOfClass(JNIEnv* env, jclass, jclass javaClass,
222 jboolean countAssignable) {
223 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800224 mirror::Class* c = soa.Decode<mirror::Class*>(javaClass);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700225 if (c == NULL) {
226 return 0;
227 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800228 std::vector<mirror::Class*> classes;
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800229 classes.push_back(c);
230 uint64_t count = 0;
231 Runtime::Current()->GetHeap()->CountInstances(classes, countAssignable, &count);
232 return count;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700233}
234
Elliott Hughes0512f022012-03-15 22:10:52 -0700235static JNINativeMethod gMethods[] = {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700236 NATIVE_METHOD(VMDebug, countInstancesOfClass, "(Ljava/lang/Class;Z)J"),
237 NATIVE_METHOD(VMDebug, crash, "()V"),
238 NATIVE_METHOD(VMDebug, dumpHprofData, "(Ljava/lang/String;Ljava/io/FileDescriptor;)V"),
239 NATIVE_METHOD(VMDebug, dumpHprofDataDdms, "()V"),
240 NATIVE_METHOD(VMDebug, dumpReferenceTables, "()V"),
241 NATIVE_METHOD(VMDebug, getAllocCount, "(I)I"),
242 NATIVE_METHOD(VMDebug, getInstructionCount, "([I)V"),
243 NATIVE_METHOD(VMDebug, getLoadedClassCount, "()I"),
244 NATIVE_METHOD(VMDebug, getVmFeatureList, "()[Ljava/lang/String;"),
245 NATIVE_METHOD(VMDebug, infopoint, "(I)V"),
246 NATIVE_METHOD(VMDebug, isDebuggerConnected, "()Z"),
247 NATIVE_METHOD(VMDebug, isDebuggingEnabled, "()Z"),
248 NATIVE_METHOD(VMDebug, isMethodTracingActive, "()Z"),
249 NATIVE_METHOD(VMDebug, lastDebuggerActivity, "()J"),
250 NATIVE_METHOD(VMDebug, printLoadedClasses, "(I)V"),
251 NATIVE_METHOD(VMDebug, resetAllocCount, "(I)V"),
252 NATIVE_METHOD(VMDebug, resetInstructionCount, "()V"),
253 NATIVE_METHOD(VMDebug, startAllocCounting, "()V"),
254 NATIVE_METHOD(VMDebug, startEmulatorTracing, "()V"),
255 NATIVE_METHOD(VMDebug, startInstructionCounting, "()V"),
256 NATIVE_METHOD(VMDebug, startMethodTracingDdmsImpl, "(II)V"),
257 NATIVE_METHOD(VMDebug, startMethodTracingFd, "(Ljava/lang/String;Ljava/io/FileDescriptor;II)V"),
258 NATIVE_METHOD(VMDebug, startMethodTracingFilename, "(Ljava/lang/String;II)V"),
259 NATIVE_METHOD(VMDebug, stopAllocCounting, "()V"),
260 NATIVE_METHOD(VMDebug, stopEmulatorTracing, "()V"),
261 NATIVE_METHOD(VMDebug, stopInstructionCounting, "()V"),
262 NATIVE_METHOD(VMDebug, stopMethodTracing, "()V"),
263 NATIVE_METHOD(VMDebug, threadCpuTimeNanos, "()J"),
264};
265
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700266void register_dalvik_system_VMDebug(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700267 REGISTER_NATIVE_METHODS("dalvik/system/VMDebug");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700268}
269
270} // namespace art