dalvik_system_VMDebug.cc revision 53b8b09fc80329539585dcf43657bc5f4ecefdff
1/*
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 <string.h>
18#include <unistd.h>
19
20#include "class_linker.h"
21#include "common_throws.h"
22#include "debugger.h"
23#include "gc/space/bump_pointer_space.h"
24#include "gc/space/dlmalloc_space.h"
25#include "gc/space/large_object_space.h"
26#include "gc/space/space-inl.h"
27#include "gc/space/zygote_space.h"
28#include "hprof/hprof.h"
29#include "jni_internal.h"
30#include "mirror/class.h"
31#include "ScopedUtfChars.h"
32#include "scoped_fast_native_object_access.h"
33#include "toStringArray.h"
34#include "trace.h"
35
36namespace art {
37
38static jobjectArray VMDebug_getVmFeatureList(JNIEnv* env, jclass) {
39  std::vector<std::string> features;
40  features.push_back("method-trace-profiling");
41  features.push_back("method-trace-profiling-streaming");
42  features.push_back("method-sample-profiling");
43  features.push_back("hprof-heap-dump");
44  features.push_back("hprof-heap-dump-streaming");
45  return toStringArray(env, features);
46}
47
48static void VMDebug_startAllocCounting(JNIEnv*, jclass) {
49  Runtime::Current()->SetStatsEnabled(true);
50}
51
52static void VMDebug_stopAllocCounting(JNIEnv*, jclass) {
53  Runtime::Current()->SetStatsEnabled(false);
54}
55
56static jint VMDebug_getAllocCount(JNIEnv*, jclass, jint kind) {
57  return Runtime::Current()->GetStat(kind);
58}
59
60static void VMDebug_resetAllocCount(JNIEnv*, jclass, jint kinds) {
61  Runtime::Current()->ResetStats(kinds);
62}
63
64static void VMDebug_startMethodTracingDdmsImpl(JNIEnv*, jclass, jint bufferSize, jint flags,
65                                               jboolean samplingEnabled, jint intervalUs) {
66  Trace::Start("[DDMS]", -1, bufferSize, flags, true, samplingEnabled, intervalUs);
67}
68
69static void VMDebug_startMethodTracingFd(JNIEnv* env, jclass, jstring javaTraceFilename,
70                                         jobject javaFd, jint bufferSize, jint flags,
71                                         jboolean samplingEnabled, jint intervalUs) {
72  int originalFd = jniGetFDFromFileDescriptor(env, javaFd);
73  if (originalFd < 0) {
74    return;
75  }
76
77  int fd = dup(originalFd);
78  if (fd < 0) {
79    ScopedObjectAccess soa(env);
80    ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
81    soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/RuntimeException;",
82                                   "dup(%d) failed: %s", originalFd, strerror(errno));
83    return;
84  }
85
86  ScopedUtfChars traceFilename(env, javaTraceFilename);
87  if (traceFilename.c_str() == NULL) {
88    return;
89  }
90  Trace::Start(traceFilename.c_str(), fd, bufferSize, flags, false, samplingEnabled, intervalUs);
91}
92
93static void VMDebug_startMethodTracingFilename(JNIEnv* env, jclass, jstring javaTraceFilename,
94                                               jint bufferSize, jint flags,
95                                               jboolean samplingEnabled, jint intervalUs) {
96  ScopedUtfChars traceFilename(env, javaTraceFilename);
97  if (traceFilename.c_str() == NULL) {
98    return;
99  }
100  Trace::Start(traceFilename.c_str(), -1, bufferSize, flags, false, samplingEnabled, intervalUs);
101}
102
103static jint VMDebug_getMethodTracingMode(JNIEnv*, jclass) {
104  return Trace::GetMethodTracingMode();
105}
106
107static void VMDebug_stopMethodTracing(JNIEnv*, jclass) {
108  Trace::Stop();
109}
110
111static void VMDebug_startEmulatorTracing(JNIEnv*, jclass) {
112  UNIMPLEMENTED(WARNING);
113  // dvmEmulatorTraceStart();
114}
115
116static void VMDebug_stopEmulatorTracing(JNIEnv*, jclass) {
117  UNIMPLEMENTED(WARNING);
118  // dvmEmulatorTraceStop();
119}
120
121static jboolean VMDebug_isDebuggerConnected(JNIEnv*, jclass) {
122  return Dbg::IsDebuggerActive();
123}
124
125static jboolean VMDebug_isDebuggingEnabled(JNIEnv*, jclass) {
126  return Dbg::IsJdwpConfigured();
127}
128
129static jlong VMDebug_lastDebuggerActivity(JNIEnv*, jclass) {
130  return Dbg::LastDebuggerActivity();
131}
132
133static void ThrowUnsupportedOperationException(JNIEnv* env) {
134  ScopedObjectAccess soa(env);
135  ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
136  soa.Self()->ThrowNewException(throw_location, "Ljava/lang/UnsupportedOperationException;", NULL);
137}
138
139static void VMDebug_startInstructionCounting(JNIEnv* env, jclass) {
140  ThrowUnsupportedOperationException(env);
141}
142
143static void VMDebug_stopInstructionCounting(JNIEnv* env, jclass) {
144  ThrowUnsupportedOperationException(env);
145}
146
147static void VMDebug_getInstructionCount(JNIEnv* env, jclass, jintArray /*javaCounts*/) {
148  ThrowUnsupportedOperationException(env);
149}
150
151static void VMDebug_resetInstructionCount(JNIEnv* env, jclass) {
152  ThrowUnsupportedOperationException(env);
153}
154
155static void VMDebug_printLoadedClasses(JNIEnv* env, jclass, jint flags) {
156  ScopedFastNativeObjectAccess soa(env);
157  return Runtime::Current()->GetClassLinker()->DumpAllClasses(flags);
158}
159
160static jint VMDebug_getLoadedClassCount(JNIEnv* env, jclass) {
161  ScopedFastNativeObjectAccess soa(env);
162  return Runtime::Current()->GetClassLinker()->NumLoadedClasses();
163}
164
165/*
166 * Returns the thread-specific CPU-time clock value for the current thread,
167 * or -1 if the feature isn't supported.
168 */
169static jlong VMDebug_threadCpuTimeNanos(JNIEnv*, jclass) {
170  return ThreadCpuNanoTime();
171}
172
173/*
174 * static void dumpHprofData(String fileName, FileDescriptor fd)
175 *
176 * Cause "hprof" data to be dumped.  We can throw an IOException if an
177 * error occurs during file handling.
178 */
179static void VMDebug_dumpHprofData(JNIEnv* env, jclass, jstring javaFilename, jobject javaFd) {
180  // Only one of these may be NULL.
181  if (javaFilename == NULL && javaFd == NULL) {
182    ScopedObjectAccess soa(env);
183    ThrowNullPointerException(NULL, "fileName == null && fd == null");
184    return;
185  }
186
187  std::string filename;
188  if (javaFilename != NULL) {
189    ScopedUtfChars chars(env, javaFilename);
190    if (env->ExceptionCheck()) {
191      return;
192    }
193    filename = chars.c_str();
194  } else {
195    filename = "[fd]";
196  }
197
198  int fd = -1;
199  if (javaFd != NULL) {
200    fd = jniGetFDFromFileDescriptor(env, javaFd);
201    if (fd < 0) {
202      ScopedObjectAccess soa(env);
203      ThrowRuntimeException("Invalid file descriptor");
204      return;
205    }
206  }
207
208  hprof::DumpHeap(filename.c_str(), fd, false);
209}
210
211static void VMDebug_dumpHprofDataDdms(JNIEnv*, jclass) {
212  hprof::DumpHeap("[DDMS]", -1, true);
213}
214
215static void VMDebug_dumpReferenceTables(JNIEnv* env, jclass) {
216  ScopedObjectAccess soa(env);
217  LOG(INFO) << "--- reference table dump ---";
218
219  soa.Env()->DumpReferenceTables(LOG(INFO));
220  soa.Vm()->DumpReferenceTables(LOG(INFO));
221
222  LOG(INFO) << "---";
223}
224
225static void VMDebug_crash(JNIEnv*, jclass) {
226  LOG(FATAL) << "Crashing runtime on request";
227}
228
229static void VMDebug_infopoint(JNIEnv*, jclass, jint id) {
230  LOG(INFO) << "VMDebug infopoint " << id << " hit";
231}
232
233static jlong VMDebug_countInstancesOfClass(JNIEnv* env, jclass, jclass javaClass,
234                                           jboolean countAssignable) {
235  ScopedObjectAccess soa(env);
236  gc::Heap* heap = Runtime::Current()->GetHeap();
237  // We only want reachable instances, so do a GC. Heap::VisitObjects visits all of the heap
238  // objects in the all spaces and the allocation stack.
239  heap->CollectGarbage(false);
240  mirror::Class* c = soa.Decode<mirror::Class*>(javaClass);
241  if (c == nullptr) {
242    return 0;
243  }
244  std::vector<mirror::Class*> classes;
245  classes.push_back(c);
246  uint64_t count = 0;
247  heap->CountInstances(classes, countAssignable, &count);
248  return count;
249}
250
251// We export the VM internal per-heap-space size/alloc/free metrics
252// for the zygote space, alloc space (application heap), and the large
253// object space for dumpsys meminfo. The other memory region data such
254// as PSS, private/shared dirty/shared data are available via
255// /proc/<pid>/smaps.
256static void VMDebug_getHeapSpaceStats(JNIEnv* env, jclass, jlongArray data) {
257  jlong* arr = reinterpret_cast<jlong*>(env->GetPrimitiveArrayCritical(data, 0));
258  if (arr == nullptr || env->GetArrayLength(data) < 9) {
259    return;
260  }
261
262  size_t allocSize = 0;
263  size_t allocUsed = 0;
264  size_t zygoteSize = 0;
265  size_t zygoteUsed = 0;
266  size_t largeObjectsSize = 0;
267  size_t largeObjectsUsed = 0;
268  gc::Heap* heap = Runtime::Current()->GetHeap();
269  for (gc::space::ContinuousSpace* space : heap->GetContinuousSpaces()) {
270    if (space->IsImageSpace()) {
271      // Currently don't include the image space.
272    } else if (space->IsZygoteSpace()) {
273      gc::space::ZygoteSpace* zygote_space = space->AsZygoteSpace();
274      zygoteSize += zygote_space->Size();
275      zygoteUsed += zygote_space->GetBytesAllocated();
276    } else if (space->IsMallocSpace()) {
277      // This is a malloc space.
278      gc::space::MallocSpace* malloc_space = space->AsMallocSpace();
279      allocSize += malloc_space->GetFootprint();
280      allocUsed += malloc_space->GetBytesAllocated();
281    } else if (space->IsBumpPointerSpace()) {
282      ScopedObjectAccess soa(env);
283      gc::space::BumpPointerSpace* bump_pointer_space = space->AsBumpPointerSpace();
284      allocSize += bump_pointer_space->Size();
285      allocUsed += bump_pointer_space->GetBytesAllocated();
286    }
287  }
288  for (gc::space::DiscontinuousSpace* space : heap->GetDiscontinuousSpaces()) {
289    if (space->IsLargeObjectSpace()) {
290      largeObjectsSize += space->AsLargeObjectSpace()->GetBytesAllocated();
291      largeObjectsUsed += largeObjectsSize;
292    }
293  }
294
295  size_t allocFree = allocSize - allocUsed;
296  size_t zygoteFree = zygoteSize - zygoteUsed;
297  size_t largeObjectsFree = largeObjectsSize - largeObjectsUsed;
298
299  int j = 0;
300  arr[j++] = allocSize;
301  arr[j++] = allocUsed;
302  arr[j++] = allocFree;
303  arr[j++] = zygoteSize;
304  arr[j++] = zygoteUsed;
305  arr[j++] = zygoteFree;
306  arr[j++] = largeObjectsSize;
307  arr[j++] = largeObjectsUsed;
308  arr[j++] = largeObjectsFree;
309  env->ReleasePrimitiveArrayCritical(data, arr, 0);
310}
311
312static JNINativeMethod gMethods[] = {
313  NATIVE_METHOD(VMDebug, countInstancesOfClass, "(Ljava/lang/Class;Z)J"),
314  NATIVE_METHOD(VMDebug, crash, "()V"),
315  NATIVE_METHOD(VMDebug, dumpHprofData, "(Ljava/lang/String;Ljava/io/FileDescriptor;)V"),
316  NATIVE_METHOD(VMDebug, dumpHprofDataDdms, "()V"),
317  NATIVE_METHOD(VMDebug, dumpReferenceTables, "()V"),
318  NATIVE_METHOD(VMDebug, getAllocCount, "(I)I"),
319  NATIVE_METHOD(VMDebug, getHeapSpaceStats, "([J)V"),
320  NATIVE_METHOD(VMDebug, getInstructionCount, "([I)V"),
321  NATIVE_METHOD(VMDebug, getLoadedClassCount, "!()I"),
322  NATIVE_METHOD(VMDebug, getVmFeatureList, "()[Ljava/lang/String;"),
323  NATIVE_METHOD(VMDebug, infopoint, "(I)V"),
324  NATIVE_METHOD(VMDebug, isDebuggerConnected, "!()Z"),
325  NATIVE_METHOD(VMDebug, isDebuggingEnabled, "!()Z"),
326  NATIVE_METHOD(VMDebug, getMethodTracingMode, "()I"),
327  NATIVE_METHOD(VMDebug, lastDebuggerActivity, "!()J"),
328  NATIVE_METHOD(VMDebug, printLoadedClasses, "!(I)V"),
329  NATIVE_METHOD(VMDebug, resetAllocCount, "(I)V"),
330  NATIVE_METHOD(VMDebug, resetInstructionCount, "()V"),
331  NATIVE_METHOD(VMDebug, startAllocCounting, "()V"),
332  NATIVE_METHOD(VMDebug, startEmulatorTracing, "()V"),
333  NATIVE_METHOD(VMDebug, startInstructionCounting, "()V"),
334  NATIVE_METHOD(VMDebug, startMethodTracingDdmsImpl, "(IIZI)V"),
335  NATIVE_METHOD(VMDebug, startMethodTracingFd, "(Ljava/lang/String;Ljava/io/FileDescriptor;IIZI)V"),
336  NATIVE_METHOD(VMDebug, startMethodTracingFilename, "(Ljava/lang/String;IIZI)V"),
337  NATIVE_METHOD(VMDebug, stopAllocCounting, "()V"),
338  NATIVE_METHOD(VMDebug, stopEmulatorTracing, "()V"),
339  NATIVE_METHOD(VMDebug, stopInstructionCounting, "()V"),
340  NATIVE_METHOD(VMDebug, stopMethodTracing, "()V"),
341  NATIVE_METHOD(VMDebug, threadCpuTimeNanos, "!()J"),
342};
343
344void register_dalvik_system_VMDebug(JNIEnv* env) {
345  REGISTER_NATIVE_METHODS("dalvik/system/VMDebug");
346}
347
348}  // namespace art
349