blob: 992998eb66063ed77cfaef7402f96898b3d7e159 [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"
Ian Rogers62d6c772013-02-27 08:32:07 -080021#include "common_throws.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070022#include "debugger.h"
23#include "hprof/hprof.h"
24#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "mirror/class.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070026#include "ScopedUtfChars.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070027#include "scoped_thread_state_change.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070028#include "toStringArray.h"
29#include "trace.h"
30
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070031namespace art {
32
Elliott Hughes0512f022012-03-15 22:10:52 -070033static jobjectArray VMDebug_getVmFeatureList(JNIEnv* env, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070034 std::vector<std::string> features;
jeffhaoe343b762011-12-05 16:36:44 -080035 features.push_back("method-trace-profiling");
36 features.push_back("method-trace-profiling-streaming");
Elliott Hughes767a1472011-10-26 18:49:02 -070037 features.push_back("hprof-heap-dump");
38 features.push_back("hprof-heap-dump-streaming");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070039 return toStringArray(env, features);
40}
41
Elliott Hughes0512f022012-03-15 22:10:52 -070042static void VMDebug_startAllocCounting(JNIEnv*, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070043 Runtime::Current()->SetStatsEnabled(true);
44}
45
Elliott Hughes0512f022012-03-15 22:10:52 -070046static void VMDebug_stopAllocCounting(JNIEnv*, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070047 Runtime::Current()->SetStatsEnabled(false);
48}
49
Elliott Hughes1bac54f2012-03-16 12:48:31 -070050static jint VMDebug_getAllocCount(JNIEnv*, jclass, jint kind) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070051 return Runtime::Current()->GetStat(kind);
52}
53
Elliott Hughes0512f022012-03-15 22:10:52 -070054static void VMDebug_resetAllocCount(JNIEnv*, jclass, jint kinds) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070055 Runtime::Current()->ResetStats(kinds);
56}
57
Elliott Hughes1bac54f2012-03-16 12:48:31 -070058static void VMDebug_startMethodTracingDdmsImpl(JNIEnv*, jclass, jint bufferSize, jint flags) {
jeffhaoe343b762011-12-05 16:36:44 -080059 Trace::Start("[DDMS]", -1, bufferSize, flags, true);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070060}
61
Ian Rogers00f7d0e2012-07-19 15:28:27 -070062static void VMDebug_startMethodTracingFd(JNIEnv* env, jclass, jstring javaTraceFilename,
63 jobject javaFd, jint bufferSize, jint flags) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070064 int originalFd = jniGetFDFromFileDescriptor(env, javaFd);
65 if (originalFd < 0) {
66 return;
67 }
68
69 int fd = dup(originalFd);
70 if (fd < 0) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070071 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -080072 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
73 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/RuntimeException;",
74 "dup(%d) failed: %s", originalFd, strerror(errno));
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070075 return;
76 }
77
78 ScopedUtfChars traceFilename(env, javaTraceFilename);
79 if (traceFilename.c_str() == NULL) {
80 return;
81 }
jeffhaoe343b762011-12-05 16:36:44 -080082 Trace::Start(traceFilename.c_str(), fd, bufferSize, flags, false);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070083}
84
Ian Rogers00f7d0e2012-07-19 15:28:27 -070085static void VMDebug_startMethodTracingFilename(JNIEnv* env, jclass, jstring javaTraceFilename,
86 jint bufferSize, jint flags) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070087 ScopedUtfChars traceFilename(env, javaTraceFilename);
88 if (traceFilename.c_str() == NULL) {
89 return;
90 }
jeffhaoe343b762011-12-05 16:36:44 -080091 Trace::Start(traceFilename.c_str(), -1, bufferSize, flags, false);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070092}
93
Elliott Hughes0512f022012-03-15 22:10:52 -070094static jboolean VMDebug_isMethodTracingActive(JNIEnv*, jclass) {
Ian Rogers62d6c772013-02-27 08:32:07 -080095 return Trace::IsMethodTracingActive();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070096}
97
Elliott Hughes0512f022012-03-15 22:10:52 -070098static void VMDebug_stopMethodTracing(JNIEnv*, jclass) {
jeffhaoe343b762011-12-05 16:36:44 -080099 Trace::Stop();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700100}
101
Elliott Hughes0512f022012-03-15 22:10:52 -0700102static void VMDebug_startEmulatorTracing(JNIEnv*, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700103 UNIMPLEMENTED(WARNING);
104 //dvmEmulatorTraceStart();
105}
106
Elliott Hughes0512f022012-03-15 22:10:52 -0700107static void VMDebug_stopEmulatorTracing(JNIEnv*, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700108 UNIMPLEMENTED(WARNING);
109 //dvmEmulatorTraceStop();
110}
111
Elliott Hughes0512f022012-03-15 22:10:52 -0700112static jboolean VMDebug_isDebuggerConnected(JNIEnv*, jclass) {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700113 return Dbg::IsDebuggerActive();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700114}
115
Elliott Hughes0512f022012-03-15 22:10:52 -0700116static jboolean VMDebug_isDebuggingEnabled(JNIEnv*, jclass) {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700117 return Dbg::IsJdwpConfigured();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700118}
119
Elliott Hughes0512f022012-03-15 22:10:52 -0700120static jlong VMDebug_lastDebuggerActivity(JNIEnv*, jclass) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700121 return Dbg::LastDebuggerActivity();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700122}
123
Ian Rogers62d6c772013-02-27 08:32:07 -0800124static void ThrowUnsupportedOperationException(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700125 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800126 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
127 soa.Self()->ThrowNewException(throw_location, "Ljava/lang/UnsupportedOperationException;", NULL);
128}
129
130static void VMDebug_startInstructionCounting(JNIEnv* env, jclass) {
131 ThrowUnsupportedOperationException(env);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700132}
133
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700134static void VMDebug_stopInstructionCounting(JNIEnv* env, jclass) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800135 ThrowUnsupportedOperationException(env);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700136}
137
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700138static void VMDebug_getInstructionCount(JNIEnv* env, jclass, jintArray /*javaCounts*/) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800139 ThrowUnsupportedOperationException(env);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700140}
141
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700142static void VMDebug_resetInstructionCount(JNIEnv* env, jclass) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800143 ThrowUnsupportedOperationException(env);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700144}
145
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700146static void VMDebug_printLoadedClasses(JNIEnv* env, jclass, jint flags) {
147 ScopedObjectAccess soa(env);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700148 return Runtime::Current()->GetClassLinker()->DumpAllClasses(flags);
149}
150
Elliott Hughes0512f022012-03-15 22:10:52 -0700151static jint VMDebug_getLoadedClassCount(JNIEnv*, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700152 return Runtime::Current()->GetClassLinker()->NumLoadedClasses();
153}
154
155/*
156 * Returns the thread-specific CPU-time clock value for the current thread,
157 * or -1 if the feature isn't supported.
158 */
Elliott Hughes0512f022012-03-15 22:10:52 -0700159static jlong VMDebug_threadCpuTimeNanos(JNIEnv*, jclass) {
160 return ThreadCpuNanoTime();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700161}
162
163/*
164 * static void dumpHprofData(String fileName, FileDescriptor fd)
165 *
166 * Cause "hprof" data to be dumped. We can throw an IOException if an
167 * error occurs during file handling.
168 */
Elliott Hughes0512f022012-03-15 22:10:52 -0700169static void VMDebug_dumpHprofData(JNIEnv* env, jclass, jstring javaFilename, jobject javaFd) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700170 // Only one of these may be NULL.
171 if (javaFilename == NULL && javaFd == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700172 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800173 ThrowNullPointerException(NULL, "fileName == null && fd == null");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700174 return;
175 }
176
177 std::string filename;
178 if (javaFilename != NULL) {
179 ScopedUtfChars chars(env, javaFilename);
180 if (env->ExceptionCheck()) {
181 return;
182 }
183 filename = chars.c_str();
184 } else {
185 filename = "[fd]";
186 }
187
188 int fd = -1;
189 if (javaFd != NULL) {
190 fd = jniGetFDFromFileDescriptor(env, javaFd);
191 if (fd < 0) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700192 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800193 ThrowRuntimeException("Invalid file descriptor");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700194 return;
195 }
196 }
197
Elliott Hughes622a6982012-06-08 17:58:54 -0700198 hprof::DumpHeap(filename.c_str(), fd, false);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700199}
200
Elliott Hugheseac76672012-05-24 21:56:51 -0700201static void VMDebug_dumpHprofDataDdms(JNIEnv*, jclass) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700202 hprof::DumpHeap("[DDMS]", -1, true);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700203}
204
Elliott Hughes0512f022012-03-15 22:10:52 -0700205static void VMDebug_dumpReferenceTables(JNIEnv* env, jclass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700206 ScopedObjectAccess soa(env);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700207 LOG(INFO) << "--- reference table dump ---";
208
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700209 soa.Env()->DumpReferenceTables(LOG(INFO));
210 soa.Vm()->DumpReferenceTables(LOG(INFO));
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700211
212 LOG(INFO) << "---";
213}
214
Elliott Hughes0512f022012-03-15 22:10:52 -0700215static void VMDebug_crash(JNIEnv*, jclass) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700216 LOG(FATAL) << "Crashing runtime on request";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700217}
218
Elliott Hughes0512f022012-03-15 22:10:52 -0700219static void VMDebug_infopoint(JNIEnv*, jclass, jint id) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700220 LOG(INFO) << "VMDebug infopoint " << id << " hit";
221}
222
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700223static jlong VMDebug_countInstancesOfClass(JNIEnv* env, jclass, jclass javaClass,
224 jboolean countAssignable) {
225 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800226 mirror::Class* c = soa.Decode<mirror::Class*>(javaClass);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700227 if (c == NULL) {
228 return 0;
229 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800230 std::vector<mirror::Class*> classes;
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800231 classes.push_back(c);
232 uint64_t count = 0;
233 Runtime::Current()->GetHeap()->CountInstances(classes, countAssignable, &count);
234 return count;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700235}
236
Elliott Hughes0512f022012-03-15 22:10:52 -0700237static JNINativeMethod gMethods[] = {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700238 NATIVE_METHOD(VMDebug, countInstancesOfClass, "(Ljava/lang/Class;Z)J"),
239 NATIVE_METHOD(VMDebug, crash, "()V"),
240 NATIVE_METHOD(VMDebug, dumpHprofData, "(Ljava/lang/String;Ljava/io/FileDescriptor;)V"),
241 NATIVE_METHOD(VMDebug, dumpHprofDataDdms, "()V"),
242 NATIVE_METHOD(VMDebug, dumpReferenceTables, "()V"),
243 NATIVE_METHOD(VMDebug, getAllocCount, "(I)I"),
244 NATIVE_METHOD(VMDebug, getInstructionCount, "([I)V"),
245 NATIVE_METHOD(VMDebug, getLoadedClassCount, "()I"),
246 NATIVE_METHOD(VMDebug, getVmFeatureList, "()[Ljava/lang/String;"),
247 NATIVE_METHOD(VMDebug, infopoint, "(I)V"),
248 NATIVE_METHOD(VMDebug, isDebuggerConnected, "()Z"),
249 NATIVE_METHOD(VMDebug, isDebuggingEnabled, "()Z"),
250 NATIVE_METHOD(VMDebug, isMethodTracingActive, "()Z"),
251 NATIVE_METHOD(VMDebug, lastDebuggerActivity, "()J"),
252 NATIVE_METHOD(VMDebug, printLoadedClasses, "(I)V"),
253 NATIVE_METHOD(VMDebug, resetAllocCount, "(I)V"),
254 NATIVE_METHOD(VMDebug, resetInstructionCount, "()V"),
255 NATIVE_METHOD(VMDebug, startAllocCounting, "()V"),
256 NATIVE_METHOD(VMDebug, startEmulatorTracing, "()V"),
257 NATIVE_METHOD(VMDebug, startInstructionCounting, "()V"),
258 NATIVE_METHOD(VMDebug, startMethodTracingDdmsImpl, "(II)V"),
259 NATIVE_METHOD(VMDebug, startMethodTracingFd, "(Ljava/lang/String;Ljava/io/FileDescriptor;II)V"),
260 NATIVE_METHOD(VMDebug, startMethodTracingFilename, "(Ljava/lang/String;II)V"),
261 NATIVE_METHOD(VMDebug, stopAllocCounting, "()V"),
262 NATIVE_METHOD(VMDebug, stopEmulatorTracing, "()V"),
263 NATIVE_METHOD(VMDebug, stopInstructionCounting, "()V"),
264 NATIVE_METHOD(VMDebug, stopMethodTracing, "()V"),
265 NATIVE_METHOD(VMDebug, threadCpuTimeNanos, "()J"),
266};
267
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700268void register_dalvik_system_VMDebug(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700269 REGISTER_NATIVE_METHODS("dalvik/system/VMDebug");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700270}
271
272} // namespace art