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