blob: 9966d5e2fcef774eaaf5002b8ae732df0512b902 [file] [log] [blame]
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001/*
2 * Copyright (C) 2007 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
17package dalvik.system;
18
Dianne Hackborn8205fe42009-06-23 19:21:10 -070019import java.io.FileDescriptor;
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080020import java.io.IOException;
21
22/**
23 * Provides access to some VM-specific debug features. Though this class and
24 * many of its members are public, this class is meant to be wrapped in a more
25 * friendly way for use by application developers. On the Android platform, the
26 * recommended way to access this functionality is through the class
27 * <code>android.os.Debug</code>.
Elliott Hughesf33eae72010-05-13 12:36:25 -070028 *
Jesse Wilson8f273342010-04-01 15:44:05 -070029 * @hide
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080030 */
31public final class VMDebug {
32 /**
33 * Specifies the default method trace data file name.
Andy McFadden982744a2010-02-22 17:07:23 -080034 *
Elliott Hughes99b44892013-06-06 15:19:50 -070035 * @deprecated Only used in one place, which is unused and deprecated.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080036 */
Elliott Hughes866e7ae2010-12-08 19:19:13 -080037 @Deprecated
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080038 static public final String DEFAULT_METHOD_TRACE_FILE_NAME = "/sdcard/dmtrace.trace";
39
40 /**
41 * flag for startMethodTracing(), which adds the results from
42 * startAllocCounting to the trace key file.
43 */
44 public static final int TRACE_COUNT_ALLOCS = 1;
45
46 /* constants for getAllocCount */
Andy McFadden982744a2010-02-22 17:07:23 -080047 private static final int KIND_ALLOCATED_OBJECTS = 1<<0;
48 private static final int KIND_ALLOCATED_BYTES = 1<<1;
49 private static final int KIND_FREED_OBJECTS = 1<<2;
50 private static final int KIND_FREED_BYTES = 1<<3;
51 private static final int KIND_GC_INVOCATIONS = 1<<4;
52 private static final int KIND_CLASS_INIT_COUNT = 1<<5;
53 private static final int KIND_CLASS_INIT_TIME = 1<<6;
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080054 private static final int KIND_EXT_ALLOCATED_OBJECTS = 1<<12;
55 private static final int KIND_EXT_ALLOCATED_BYTES = 1<<13;
56 private static final int KIND_EXT_FREED_OBJECTS = 1<<14;
57 private static final int KIND_EXT_FREED_BYTES = 1<<15;
58
59 public static final int KIND_GLOBAL_ALLOCATED_OBJECTS =
60 KIND_ALLOCATED_OBJECTS;
61 public static final int KIND_GLOBAL_ALLOCATED_BYTES =
62 KIND_ALLOCATED_BYTES;
63 public static final int KIND_GLOBAL_FREED_OBJECTS =
64 KIND_FREED_OBJECTS;
65 public static final int KIND_GLOBAL_FREED_BYTES =
66 KIND_FREED_BYTES;
67 public static final int KIND_GLOBAL_GC_INVOCATIONS =
68 KIND_GC_INVOCATIONS;
Andy McFadden982744a2010-02-22 17:07:23 -080069 public static final int KIND_GLOBAL_CLASS_INIT_COUNT =
70 KIND_CLASS_INIT_COUNT;
71 public static final int KIND_GLOBAL_CLASS_INIT_TIME =
72 KIND_CLASS_INIT_TIME;
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080073 public static final int KIND_GLOBAL_EXT_ALLOCATED_OBJECTS =
74 KIND_EXT_ALLOCATED_OBJECTS;
75 public static final int KIND_GLOBAL_EXT_ALLOCATED_BYTES =
76 KIND_EXT_ALLOCATED_BYTES;
77 public static final int KIND_GLOBAL_EXT_FREED_OBJECTS =
78 KIND_EXT_FREED_OBJECTS;
79 public static final int KIND_GLOBAL_EXT_FREED_BYTES =
80 KIND_EXT_FREED_BYTES;
81
82 public static final int KIND_THREAD_ALLOCATED_OBJECTS =
83 KIND_ALLOCATED_OBJECTS << 16;
84 public static final int KIND_THREAD_ALLOCATED_BYTES =
85 KIND_ALLOCATED_BYTES << 16;
86 public static final int KIND_THREAD_FREED_OBJECTS =
87 KIND_FREED_OBJECTS << 16;
88 public static final int KIND_THREAD_FREED_BYTES =
89 KIND_FREED_BYTES << 16;
90 public static final int KIND_THREAD_GC_INVOCATIONS =
91 KIND_GC_INVOCATIONS << 16;
Andy McFadden982744a2010-02-22 17:07:23 -080092 public static final int KIND_THREAD_CLASS_INIT_COUNT =
93 KIND_CLASS_INIT_COUNT << 16;
94 public static final int KIND_THREAD_CLASS_INIT_TIME =
95 KIND_CLASS_INIT_TIME << 16;
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080096 public static final int KIND_THREAD_EXT_ALLOCATED_OBJECTS =
97 KIND_EXT_ALLOCATED_OBJECTS << 16;
98 public static final int KIND_THREAD_EXT_ALLOCATED_BYTES =
99 KIND_EXT_ALLOCATED_BYTES << 16;
100 public static final int KIND_THREAD_EXT_FREED_OBJECTS =
101 KIND_EXT_FREED_OBJECTS << 16;
102 public static final int KIND_THREAD_EXT_FREED_BYTES =
103 KIND_EXT_FREED_BYTES << 16;
104
105 public static final int KIND_ALL_COUNTS = 0xffffffff;
106
107 /* all methods are static */
108 private VMDebug() {}
109
110 /**
111 * Returns the time since the last known debugger activity.
Elliott Hughesf33eae72010-05-13 12:36:25 -0700112 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800113 * @return the time in milliseconds, or -1 if the debugger is not connected
114 */
115 public static native long lastDebuggerActivity();
116
117 /**
118 * Determines if debugging is enabled in this VM. If debugging is not
119 * enabled, a debugger cannot be attached.
120 *
121 * @return true if debugging is enabled
122 */
123 public static native boolean isDebuggingEnabled();
124
125 /**
126 * Determines if a debugger is currently attached.
Elliott Hughesf33eae72010-05-13 12:36:25 -0700127 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800128 * @return true if (and only if) a debugger is connected
129 */
130 public static native boolean isDebuggerConnected();
131
132 /**
Andy McFaddend3c419d2010-01-22 07:23:40 -0800133 * Returns an array of strings that identify VM features. This is
134 * used by DDMS to determine what sorts of operations the VM can
135 * perform.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800136 */
Andy McFaddend3c419d2010-01-22 07:23:40 -0800137 public static native String[] getVmFeatureList();
138
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800139 /**
140 * Start method tracing with default name, size, and with <code>0</code>
141 * flags.
Andy McFadden982744a2010-02-22 17:07:23 -0800142 *
Elliott Hughes99b44892013-06-06 15:19:50 -0700143 * @deprecated Not used, not needed.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800144 */
Elliott Hughes866e7ae2010-12-08 19:19:13 -0800145 @Deprecated
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800146 public static void startMethodTracing() {
147 startMethodTracing(DEFAULT_METHOD_TRACE_FILE_NAME, 0, 0);
148 }
149
150 /**
151 * Start method tracing, specifying a file name as well as a default
152 * buffer size. See <a
Scott Main016a87c2009-04-24 13:41:43 -0700153 * href="{@docRoot}guide/developing/tools/traceview.html"> Running the
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800154 * Traceview Debugging Program</a> for information about reading
155 * trace files.
Elliott Hughesf33eae72010-05-13 12:36:25 -0700156 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800157 * <p>You can use either a fully qualified path and
158 * name, or just a name. If only a name is specified, the file will
159 * be created under the /sdcard/ directory. If a name is not given,
160 * the default is /sdcard/dmtrace.trace.</p>
Elliott Hughesf33eae72010-05-13 12:36:25 -0700161 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800162 * @param traceFileName name to give the trace file
163 * @param bufferSize the maximum size of both files combined. If passed
164 * as <code>0</code>, it defaults to 8MB.
165 * @param flags flags to control method tracing. The only one that
166 * is currently defined is {@link #TRACE_COUNT_ALLOCS}.
167 */
Elliott Hughesc2d0a1f2012-12-04 11:54:39 -0800168 public static void startMethodTracing(String traceFileName, int bufferSize, int flags) {
Andy McFadden93714a12010-01-22 16:36:30 -0800169
170 if (traceFileName == null) {
Elliott Hughesc2d0a1f2012-12-04 11:54:39 -0800171 throw new NullPointerException("traceFileName == null");
Andy McFadden93714a12010-01-22 16:36:30 -0800172 }
173
174 startMethodTracingNative(traceFileName, null, bufferSize, flags);
Dianne Hackborn8205fe42009-06-23 19:21:10 -0700175 }
176
177 /**
178 * Like startMethodTracing(String, int, int), but taking an already-opened
179 * FileDescriptor in which the trace is written. The file name is also
180 * supplied simply for logging. Makes a dup of the file descriptor.
Dianne Hackborn8205fe42009-06-23 19:21:10 -0700181 */
Andy McFadden93714a12010-01-22 16:36:30 -0800182 public static void startMethodTracing(String traceFileName,
183 FileDescriptor fd, int bufferSize, int flags)
184 {
Elliott Hughesc2d0a1f2012-12-04 11:54:39 -0800185 if (traceFileName == null) {
186 throw new NullPointerException("traceFileName == null");
187 }
188 if (fd == null) {
189 throw new NullPointerException("fd == null");
Andy McFadden93714a12010-01-22 16:36:30 -0800190 }
191
192 startMethodTracingNative(traceFileName, fd, bufferSize, flags);
193 }
194
195 /**
196 * Starts method tracing without a backing file. When stopMethodTracing
197 * is called, the result is sent directly to DDMS. (If DDMS is not
198 * attached when tracing ends, the profiling data will be discarded.)
Andy McFadden93714a12010-01-22 16:36:30 -0800199 */
200 public static void startMethodTracingDdms(int bufferSize, int flags) {
201 startMethodTracingNative(null, null, bufferSize, flags);
202 }
203
204 /**
205 * Implements all startMethodTracing variants.
Andy McFadden93714a12010-01-22 16:36:30 -0800206 */
207 private static native void startMethodTracingNative(String traceFileName,
Dianne Hackborn8205fe42009-06-23 19:21:10 -0700208 FileDescriptor fd, int bufferSize, int flags);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800209
210 /**
The Android Open Source Project5d287a92009-03-18 22:20:24 -0700211 * Determine whether method tracing is currently active.
The Android Open Source Project5d287a92009-03-18 22:20:24 -0700212 */
213 public static native boolean isMethodTracingActive();
214
215 /**
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800216 * Stops method tracing.
217 */
218 public static native void stopMethodTracing();
219
220 /**
221 * Starts sending Dalvik method trace info to the emulator.
222 */
223 public static native void startEmulatorTracing();
224
225 /**
226 * Stops sending Dalvik method trace info to the emulator.
227 */
228 public static native void stopEmulatorTracing();
229
230 /**
231 * Get an indication of thread CPU usage. The value returned indicates the
232 * amount of time that the current thread has spent executing code or
233 * waiting for certain types of I/O.
234 * <p>
235 * The time is expressed in nanoseconds, and is only meaningful when
236 * compared to the result from an earlier call. Note that nanosecond
237 * resolution does not imply nanosecond accuracy.
Elliott Hughesf33eae72010-05-13 12:36:25 -0700238 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800239 * @return the CPU usage. A value of -1 means the system does not support
240 * this feature.
241 */
242 public static native long threadCpuTimeNanos();
243
244 /**
245 * Count the number and aggregate size of memory allocations between
246 * two points.
247 */
248 public static native void startAllocCounting();
249 public static native void stopAllocCounting();
250 public static native int getAllocCount(int kind);
251 public static native void resetAllocCount(int kinds);
252
253 /**
Carl Shapiro3ca860a2011-01-12 15:53:02 -0800254 * This method exists for binary compatibility. It was part of
255 * the allocation limits API which was removed in Honeycomb.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800256 */
Carl Shapiro3ca860a2011-01-12 15:53:02 -0800257 @Deprecated
258 public static int setAllocationLimit(int limit) {
259 return -1;
260 }
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800261
262 /**
Carl Shapiro3ca860a2011-01-12 15:53:02 -0800263 * This method exists for binary compatibility. It was part of
264 * the allocation limits API which was removed in Honeycomb.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800265 */
Carl Shapiro3ca860a2011-01-12 15:53:02 -0800266 @Deprecated
267 public static int setGlobalAllocationLimit(int limit) {
268 return -1;
269 }
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800270
271 /**
272 * Count the number of instructions executed between two points.
273 */
274 public static native void startInstructionCounting();
275 public static native void stopInstructionCounting();
276 public static native void getInstructionCount(int[] counts);
277 public static native void resetInstructionCount();
278
279 /**
280 * Dumps a list of loaded class to the log file.
281 */
282 public static native void printLoadedClasses(int flags);
283
284 /**
285 * Gets the number of loaded classes.
Elliott Hughesf33eae72010-05-13 12:36:25 -0700286 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800287 * @return the number of loaded classes
288 */
289 public static native int getLoadedClassCount();
290
291 /**
Andy McFadden478f5f92010-07-09 16:49:05 -0700292 * Dumps "hprof" data to the specified file. This may cause a GC.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800293 *
294 * The VM may create a temporary file in the same directory.
295 *
Elliott Hughesc2d0a1f2012-12-04 11:54:39 -0800296 * @param filename Full pathname of output file (e.g. "/sdcard/dump.hprof").
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800297 * @throws UnsupportedOperationException if the VM was built without
298 * HPROF support.
299 * @throws IOException if an error occurs while opening or writing files.
300 */
Elliott Hughesc2d0a1f2012-12-04 11:54:39 -0800301 public static void dumpHprofData(String filename) throws IOException {
302 if (filename == null) {
303 throw new NullPointerException("filename == null");
304 }
305 dumpHprofData(filename, null);
Andy McFadden478f5f92010-07-09 16:49:05 -0700306 }
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800307
Andy McFaddenb24c3852009-04-08 00:35:55 -0700308 /**
Andy McFadden478f5f92010-07-09 16:49:05 -0700309 * Collects "hprof" heap data and sends it to DDMS. This may cause a GC.
Andy McFadden21fc59c2010-01-28 17:01:39 -0800310 *
311 * @throws UnsupportedOperationException if the VM was built without
312 * HPROF support.
Andy McFadden21fc59c2010-01-28 17:01:39 -0800313 */
314 public static native void dumpHprofDataDdms();
315
316 /**
Andy McFadden478f5f92010-07-09 16:49:05 -0700317 * Dumps "hprof" heap data to a file, by name or descriptor.
318 *
319 * @param fileName Name of output file. If fd is non-null, the
320 * file name is only used in log messages (and may be null).
321 * @param fd Descriptor of open file that will receive the output.
322 * If this is null, the fileName is used instead.
Andy McFadden478f5f92010-07-09 16:49:05 -0700323 */
324 public static native void dumpHprofData(String fileName, FileDescriptor fd)
325 throws IOException;
326
327 /**
Andy McFaddenb24c3852009-04-08 00:35:55 -0700328 * Primes the register map cache.
Andy McFaddenb24c3852009-04-08 00:35:55 -0700329 */
330 public static native boolean cacheRegisterMap(String classAndMethodDesc);
331
Andy McFaddencd9ef3b2009-07-31 13:54:59 -0700332 /**
Andy McFaddenbb4b5ab2009-10-22 17:24:45 -0700333 * Dumps the contents of the VM reference tables (e.g. JNI locals and
334 * globals) to the log file.
Andy McFaddenbb4b5ab2009-10-22 17:24:45 -0700335 */
336 public static native void dumpReferenceTables();
337
338 /**
339 * Crashes the VM. Seriously. Dumps the interpreter stack trace for
340 * the current thread and then aborts the VM so you can see the native
341 * stack trace. Useful for figuring out how you got somewhere when
342 * lots of native code is involved.
Andy McFaddencd9ef3b2009-07-31 13:54:59 -0700343 */
344 public static native void crash();
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800345
Ben Chengdd60d482010-03-12 16:11:49 -0800346 /**
347 * Together with gdb, provide a handy way to stop the VM at user-tagged
348 * locations.
Ben Chengdd60d482010-03-12 16:11:49 -0800349 */
350 public static native void infopoint(int id);
351
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800352 /*
353 * Fake method, inserted into dmtrace output when the garbage collector
354 * runs. Not actually called.
355 */
356 private static void startGC() {}
357
358 /*
359 * Fake method, inserted into dmtrace output during class preparation
360 * (loading and linking, but not verification or initialization). Not
361 * actually called.
362 */
363 private static void startClassPrep() {}
Carl Shapiro2e386d42010-08-16 21:40:20 -0700364
365 /**
Carl Shapiro9e2e6042010-11-04 15:18:22 -0700366 * Counts the instances of a class.
Carl Shapiro2e386d42010-08-16 21:40:20 -0700367 *
Carl Shapiro9e2e6042010-11-04 15:18:22 -0700368 * @param klass the class to be counted.
369 * @param assignable if false, direct instances of klass are
370 * counted. If true, instances that are
371 * assignable to klass, as defined by
372 * {@link Class#isAssignableFrom} are counted.
Carl Shapiro79214492011-01-12 14:40:13 -0800373 * @return the number of matching instances.
Carl Shapiro2e386d42010-08-16 21:40:20 -0700374 */
Carl Shapiro9e2e6042010-11-04 15:18:22 -0700375 public static native long countInstancesOfClass(Class klass, boolean assignable);
Hiroshi Yamauchi929e3dc2013-07-15 14:15:53 -0700376
377 /**
378 * Export the heap per-space stats for dumpsys meminfo.
379 *
380 * The content of the array is:
381 *
382 * <pre>
383 * data[0] : the application heap space size
384 * data[1] : the application heap space allocated bytes
385 * data[2] : the application heap space free bytes
386 * data[3] : the zygote heap space size
387 * data[4] : the zygote heap space allocated size
388 * data[5] : the zygote heap space free size
389 * data[6] : the large object space size
390 * data[7] : the large object space allocated bytes
391 * data[8] : the large object space free bytes
392 * </pre>
393 *
394 * @param data the array into which the stats are written.
395 */
396 public static native void getHeapSpaceStats(long[] data);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800397}