blob: 1cc42dfd578138afe58e0bcea9d9b5a250ec0f75 [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"
Hiroshi Yamauchi09b07a92013-07-15 13:17:06 -070023#include "gc/space/dlmalloc_space.h"
24#include "gc/space/large_object_space.h"
25#include "gc/space/space-inl.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070026#include "hprof/hprof.h"
27#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/class.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070029#include "ScopedUtfChars.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070030#include "scoped_thread_state_change.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070031#include "toStringArray.h"
32#include "trace.h"
33
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070034namespace art {
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 Hughes1bac54f2012-03-16 12:48:31 -070053static jint VMDebug_getAllocCount(JNIEnv*, 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 Hughes1bac54f2012-03-16 12:48:31 -070061static void VMDebug_startMethodTracingDdmsImpl(JNIEnv*, 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
Ian Rogers00f7d0e2012-07-19 15:28:27 -070065static void VMDebug_startMethodTracingFd(JNIEnv* env, jclass, jstring javaTraceFilename,
66 jobject javaFd, jint bufferSize, jint flags) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070067 int originalFd = jniGetFDFromFileDescriptor(env, javaFd);
68 if (originalFd < 0) {
69 return;
70 }
71
72 int fd = dup(originalFd);
73 if (fd < 0) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070074 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -080075 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
76 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/RuntimeException;",
77 "dup(%d) failed: %s", originalFd, strerror(errno));
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070078 return;
79 }
80
81 ScopedUtfChars traceFilename(env, javaTraceFilename);
82 if (traceFilename.c_str() == NULL) {
83 return;
84 }
jeffhaoe343b762011-12-05 16:36:44 -080085 Trace::Start(traceFilename.c_str(), fd, bufferSize, flags, false);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070086}
87
Ian Rogers00f7d0e2012-07-19 15:28:27 -070088static void VMDebug_startMethodTracingFilename(JNIEnv* env, jclass, jstring javaTraceFilename,
89 jint bufferSize, jint flags) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070090 ScopedUtfChars traceFilename(env, javaTraceFilename);
91 if (traceFilename.c_str() == NULL) {
92 return;
93 }
jeffhaoe343b762011-12-05 16:36:44 -080094 Trace::Start(traceFilename.c_str(), -1, bufferSize, flags, false);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070095}
96
Elliott Hughes0512f022012-03-15 22:10:52 -070097static jboolean VMDebug_isMethodTracingActive(JNIEnv*, jclass) {
Ian Rogers62d6c772013-02-27 08:32:07 -080098 return Trace::IsMethodTracingActive();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070099}
100
Elliott Hughes0512f022012-03-15 22:10:52 -0700101static void VMDebug_stopMethodTracing(JNIEnv*, jclass) {
jeffhaoe343b762011-12-05 16:36:44 -0800102 Trace::Stop();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700103}
104
Elliott Hughes0512f022012-03-15 22:10:52 -0700105static void VMDebug_startEmulatorTracing(JNIEnv*, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700106 UNIMPLEMENTED(WARNING);
107 //dvmEmulatorTraceStart();
108}
109
Elliott Hughes0512f022012-03-15 22:10:52 -0700110static void VMDebug_stopEmulatorTracing(JNIEnv*, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700111 UNIMPLEMENTED(WARNING);
112 //dvmEmulatorTraceStop();
113}
114
Elliott Hughes0512f022012-03-15 22:10:52 -0700115static jboolean VMDebug_isDebuggerConnected(JNIEnv*, jclass) {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700116 return Dbg::IsDebuggerActive();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700117}
118
Elliott Hughes0512f022012-03-15 22:10:52 -0700119static jboolean VMDebug_isDebuggingEnabled(JNIEnv*, jclass) {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700120 return Dbg::IsJdwpConfigured();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700121}
122
Elliott Hughes0512f022012-03-15 22:10:52 -0700123static jlong VMDebug_lastDebuggerActivity(JNIEnv*, jclass) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700124 return Dbg::LastDebuggerActivity();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700125}
126
Ian Rogers62d6c772013-02-27 08:32:07 -0800127static void ThrowUnsupportedOperationException(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700128 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800129 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
130 soa.Self()->ThrowNewException(throw_location, "Ljava/lang/UnsupportedOperationException;", NULL);
131}
132
133static void VMDebug_startInstructionCounting(JNIEnv* env, jclass) {
134 ThrowUnsupportedOperationException(env);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700135}
136
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700137static void VMDebug_stopInstructionCounting(JNIEnv* env, jclass) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800138 ThrowUnsupportedOperationException(env);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700139}
140
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700141static void VMDebug_getInstructionCount(JNIEnv* env, jclass, jintArray /*javaCounts*/) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800142 ThrowUnsupportedOperationException(env);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700143}
144
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700145static void VMDebug_resetInstructionCount(JNIEnv* env, jclass) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800146 ThrowUnsupportedOperationException(env);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700147}
148
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700149static void VMDebug_printLoadedClasses(JNIEnv* env, jclass, jint flags) {
150 ScopedObjectAccess soa(env);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700151 return Runtime::Current()->GetClassLinker()->DumpAllClasses(flags);
152}
153
Elliott Hughes0512f022012-03-15 22:10:52 -0700154static jint VMDebug_getLoadedClassCount(JNIEnv*, jclass) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700155 return Runtime::Current()->GetClassLinker()->NumLoadedClasses();
156}
157
158/*
159 * Returns the thread-specific CPU-time clock value for the current thread,
160 * or -1 if the feature isn't supported.
161 */
Elliott Hughes0512f022012-03-15 22:10:52 -0700162static jlong VMDebug_threadCpuTimeNanos(JNIEnv*, jclass) {
163 return ThreadCpuNanoTime();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700164}
165
166/*
167 * static void dumpHprofData(String fileName, FileDescriptor fd)
168 *
169 * Cause "hprof" data to be dumped. We can throw an IOException if an
170 * error occurs during file handling.
171 */
Elliott Hughes0512f022012-03-15 22:10:52 -0700172static void VMDebug_dumpHprofData(JNIEnv* env, jclass, jstring javaFilename, jobject javaFd) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700173 // Only one of these may be NULL.
174 if (javaFilename == NULL && javaFd == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700175 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800176 ThrowNullPointerException(NULL, "fileName == null && fd == null");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700177 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) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700195 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800196 ThrowRuntimeException("Invalid file descriptor");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700197 return;
198 }
199 }
200
Elliott Hughes622a6982012-06-08 17:58:54 -0700201 hprof::DumpHeap(filename.c_str(), fd, false);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700202}
203
Elliott Hugheseac76672012-05-24 21:56:51 -0700204static void VMDebug_dumpHprofDataDdms(JNIEnv*, jclass) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700205 hprof::DumpHeap("[DDMS]", -1, true);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700206}
207
Elliott Hughes0512f022012-03-15 22:10:52 -0700208static void VMDebug_dumpReferenceTables(JNIEnv* env, jclass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700209 ScopedObjectAccess soa(env);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700210 LOG(INFO) << "--- reference table dump ---";
211
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700212 soa.Env()->DumpReferenceTables(LOG(INFO));
213 soa.Vm()->DumpReferenceTables(LOG(INFO));
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700214
215 LOG(INFO) << "---";
216}
217
Elliott Hughes0512f022012-03-15 22:10:52 -0700218static void VMDebug_crash(JNIEnv*, jclass) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700219 LOG(FATAL) << "Crashing runtime on request";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700220}
221
Elliott Hughes0512f022012-03-15 22:10:52 -0700222static void VMDebug_infopoint(JNIEnv*, jclass, jint id) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700223 LOG(INFO) << "VMDebug infopoint " << id << " hit";
224}
225
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700226static jlong VMDebug_countInstancesOfClass(JNIEnv* env, jclass, jclass javaClass,
227 jboolean countAssignable) {
228 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800229 mirror::Class* c = soa.Decode<mirror::Class*>(javaClass);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700230 if (c == NULL) {
231 return 0;
232 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800233 std::vector<mirror::Class*> classes;
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800234 classes.push_back(c);
235 uint64_t count = 0;
236 Runtime::Current()->GetHeap()->CountInstances(classes, countAssignable, &count);
237 return count;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700238}
239
Hiroshi Yamauchi09b07a92013-07-15 13:17:06 -0700240// We export the VM internal per-heap-space size/alloc/free metrics
241// for the zygote space, alloc space (application heap), and the large
242// object space for dumpsys meminfo. The other memory region data such
243// as PSS, private/shared dirty/shared data are available via
244// /proc/<pid>/smaps.
245static void VMDebug_getHeapSpaceStats(JNIEnv* env, jclass, jlongArray data) {
Ian Rogers6b99dd12013-07-25 12:11:32 -0700246 jlong* arr = reinterpret_cast<jlong*>(env->GetPrimitiveArrayCritical(data, 0));
Hiroshi Yamauchi09b07a92013-07-15 13:17:06 -0700247 if (arr == NULL || env->GetArrayLength(data) < 9) {
248 return;
249 }
250
251 size_t allocSize = 0;
252 size_t allocUsed = 0;
253 size_t zygoteSize = 0;
254 size_t zygoteUsed = 0;
255 size_t largeObjectsSize = 0;
256 size_t largeObjectsUsed = 0;
257
258 gc::Heap* heap = Runtime::Current()->GetHeap();
259 const std::vector<gc::space::ContinuousSpace*>& continuous_spaces = heap->GetContinuousSpaces();
260 const std::vector<gc::space::DiscontinuousSpace*>& discontinuous_spaces = heap->GetDiscontinuousSpaces();
261 typedef std::vector<gc::space::ContinuousSpace*>::const_iterator It;
262 for (It it = continuous_spaces.begin(), end = continuous_spaces.end(); it != end; ++it) {
263 gc::space::ContinuousSpace* space = *it;
264 if (space->IsImageSpace()) {
265 // Currently don't include the image space.
266 } else if (space->IsZygoteSpace()) {
267 gc::space::DlMallocSpace* dlmalloc_space = space->AsDlMallocSpace();
268 zygoteSize += dlmalloc_space->GetFootprint();
269 zygoteUsed += dlmalloc_space->GetBytesAllocated();
270 } else {
271 // This is the alloc space.
272 gc::space::DlMallocSpace* dlmalloc_space = space->AsDlMallocSpace();
273 allocSize += dlmalloc_space->GetFootprint();
274 allocUsed += dlmalloc_space->GetBytesAllocated();
275 }
276 }
277 typedef std::vector<gc::space::DiscontinuousSpace*>::const_iterator It2;
278 for (It2 it = discontinuous_spaces.begin(), end = discontinuous_spaces.end(); it != end; ++it) {
279 gc::space::DiscontinuousSpace* space = *it;
280 if (space->IsLargeObjectSpace()) {
281 largeObjectsSize += space->AsLargeObjectSpace()->GetBytesAllocated();
282 largeObjectsUsed += largeObjectsSize;
283 }
284 }
285
286 size_t allocFree = allocSize - allocUsed;
287 size_t zygoteFree = zygoteSize - zygoteUsed;
288 size_t largeObjectsFree = largeObjectsSize - largeObjectsUsed;
289
290 int j = 0;
291 arr[j++] = allocSize;
292 arr[j++] = allocUsed;
293 arr[j++] = allocFree;
294 arr[j++] = zygoteSize;
295 arr[j++] = zygoteUsed;
296 arr[j++] = zygoteFree;
297 arr[j++] = largeObjectsSize;
298 arr[j++] = largeObjectsUsed;
299 arr[j++] = largeObjectsFree;
300 env->ReleasePrimitiveArrayCritical(data, arr, 0);
301}
302
Elliott Hughes0512f022012-03-15 22:10:52 -0700303static JNINativeMethod gMethods[] = {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700304 NATIVE_METHOD(VMDebug, countInstancesOfClass, "(Ljava/lang/Class;Z)J"),
305 NATIVE_METHOD(VMDebug, crash, "()V"),
306 NATIVE_METHOD(VMDebug, dumpHprofData, "(Ljava/lang/String;Ljava/io/FileDescriptor;)V"),
307 NATIVE_METHOD(VMDebug, dumpHprofDataDdms, "()V"),
308 NATIVE_METHOD(VMDebug, dumpReferenceTables, "()V"),
309 NATIVE_METHOD(VMDebug, getAllocCount, "(I)I"),
Hiroshi Yamauchi09b07a92013-07-15 13:17:06 -0700310 NATIVE_METHOD(VMDebug, getHeapSpaceStats, "([J)V"),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700311 NATIVE_METHOD(VMDebug, getInstructionCount, "([I)V"),
312 NATIVE_METHOD(VMDebug, getLoadedClassCount, "()I"),
313 NATIVE_METHOD(VMDebug, getVmFeatureList, "()[Ljava/lang/String;"),
314 NATIVE_METHOD(VMDebug, infopoint, "(I)V"),
315 NATIVE_METHOD(VMDebug, isDebuggerConnected, "()Z"),
316 NATIVE_METHOD(VMDebug, isDebuggingEnabled, "()Z"),
317 NATIVE_METHOD(VMDebug, isMethodTracingActive, "()Z"),
318 NATIVE_METHOD(VMDebug, lastDebuggerActivity, "()J"),
319 NATIVE_METHOD(VMDebug, printLoadedClasses, "(I)V"),
320 NATIVE_METHOD(VMDebug, resetAllocCount, "(I)V"),
321 NATIVE_METHOD(VMDebug, resetInstructionCount, "()V"),
322 NATIVE_METHOD(VMDebug, startAllocCounting, "()V"),
323 NATIVE_METHOD(VMDebug, startEmulatorTracing, "()V"),
324 NATIVE_METHOD(VMDebug, startInstructionCounting, "()V"),
325 NATIVE_METHOD(VMDebug, startMethodTracingDdmsImpl, "(II)V"),
326 NATIVE_METHOD(VMDebug, startMethodTracingFd, "(Ljava/lang/String;Ljava/io/FileDescriptor;II)V"),
327 NATIVE_METHOD(VMDebug, startMethodTracingFilename, "(Ljava/lang/String;II)V"),
328 NATIVE_METHOD(VMDebug, stopAllocCounting, "()V"),
329 NATIVE_METHOD(VMDebug, stopEmulatorTracing, "()V"),
330 NATIVE_METHOD(VMDebug, stopInstructionCounting, "()V"),
331 NATIVE_METHOD(VMDebug, stopMethodTracing, "()V"),
332 NATIVE_METHOD(VMDebug, threadCpuTimeNanos, "()J"),
333};
334
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700335void register_dalvik_system_VMDebug(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700336 REGISTER_NATIVE_METHODS("dalvik/system/VMDebug");
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700337}
338
339} // namespace art