VMDebug.java revision b7926325a1c1a370c84c81db80372f59af240a53
1/*
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
19import java.io.IOException;
20
21/**
22 * Provides access to some VM-specific debug features. Though this class and
23 * many of its members are public, this class is meant to be wrapped in a more
24 * friendly way for use by application developers. On the Android platform, the
25 * recommended way to access this functionality is through the class
26 * <code>android.os.Debug</code>.
27 *
28 * @cts Please complete the spec.
29 *
30 * @since Android 1.0
31 */
32public final class VMDebug {
33    /**
34     * Specifies the default method trace data file name.
35     */
36    static public final String DEFAULT_METHOD_TRACE_FILE_NAME = "/sdcard/dmtrace.trace";
37
38    /**
39     * flag for startMethodTracing(), which adds the results from
40     * startAllocCounting to the trace key file.
41     */
42    public static final int TRACE_COUNT_ALLOCS = 1;
43
44    /* constants for getAllocCount */
45    private static final int KIND_ALLOCATED_OBJECTS = 1<<0;
46    private static final int KIND_ALLOCATED_BYTES   = 1<<1;
47    private static final int KIND_FREED_OBJECTS     = 1<<2;
48    private static final int KIND_FREED_BYTES       = 1<<3;
49    private static final int KIND_GC_INVOCATIONS    = 1<<4;
50    private static final int KIND_EXT_ALLOCATED_OBJECTS = 1<<12;
51    private static final int KIND_EXT_ALLOCATED_BYTES   = 1<<13;
52    private static final int KIND_EXT_FREED_OBJECTS     = 1<<14;
53    private static final int KIND_EXT_FREED_BYTES       = 1<<15;
54
55    public static final int KIND_GLOBAL_ALLOCATED_OBJECTS =
56        KIND_ALLOCATED_OBJECTS;
57    public static final int KIND_GLOBAL_ALLOCATED_BYTES =
58        KIND_ALLOCATED_BYTES;
59    public static final int KIND_GLOBAL_FREED_OBJECTS =
60        KIND_FREED_OBJECTS;
61    public static final int KIND_GLOBAL_FREED_BYTES =
62        KIND_FREED_BYTES;
63    public static final int KIND_GLOBAL_GC_INVOCATIONS =
64        KIND_GC_INVOCATIONS;
65    public static final int KIND_GLOBAL_EXT_ALLOCATED_OBJECTS =
66        KIND_EXT_ALLOCATED_OBJECTS;
67    public static final int KIND_GLOBAL_EXT_ALLOCATED_BYTES =
68        KIND_EXT_ALLOCATED_BYTES;
69    public static final int KIND_GLOBAL_EXT_FREED_OBJECTS =
70        KIND_EXT_FREED_OBJECTS;
71    public static final int KIND_GLOBAL_EXT_FREED_BYTES =
72        KIND_EXT_FREED_BYTES;
73
74    public static final int KIND_THREAD_ALLOCATED_OBJECTS =
75        KIND_ALLOCATED_OBJECTS << 16;
76    public static final int KIND_THREAD_ALLOCATED_BYTES =
77        KIND_ALLOCATED_BYTES << 16;
78    public static final int KIND_THREAD_FREED_OBJECTS =
79        KIND_FREED_OBJECTS << 16;
80    public static final int KIND_THREAD_FREED_BYTES =
81        KIND_FREED_BYTES << 16;
82    public static final int KIND_THREAD_GC_INVOCATIONS =
83        KIND_GC_INVOCATIONS << 16;
84    public static final int KIND_THREAD_EXT_ALLOCATED_OBJECTS =
85        KIND_EXT_ALLOCATED_OBJECTS << 16;
86    public static final int KIND_THREAD_EXT_ALLOCATED_BYTES =
87        KIND_EXT_ALLOCATED_BYTES << 16;
88    public static final int KIND_THREAD_EXT_FREED_OBJECTS =
89        KIND_EXT_FREED_OBJECTS << 16;
90    public static final int KIND_THREAD_EXT_FREED_BYTES =
91        KIND_EXT_FREED_BYTES << 16;
92
93    public static final int KIND_ALL_COUNTS = 0xffffffff;
94
95    /* all methods are static */
96    private VMDebug() {}
97
98    /**
99     * Returns the time since the last known debugger activity.
100     *
101     * @return the time in milliseconds, or -1 if the debugger is not connected
102     */
103    public static native long lastDebuggerActivity();
104
105    /**
106     * Determines if debugging is enabled in this VM.  If debugging is not
107     * enabled, a debugger cannot be attached.
108     *
109     * @return true if debugging is enabled
110     */
111    public static native boolean isDebuggingEnabled();
112
113    /**
114     * Determines if a debugger is currently attached.
115     *
116     * @return true if (and only if) a debugger is connected
117     */
118    public static native boolean isDebuggerConnected();
119
120    /**
121     * Enable object allocation count logging and reporting.  Call with
122     * a depth of zero to disable.  This produces "top N" lists on every GC.
123     */
124    //public static native void enableTopAllocCounts(int depth);
125
126    /**
127     * Start method tracing with default name, size, and with <code>0</code>
128     * flags.
129     */
130    public static void startMethodTracing() {
131        startMethodTracing(DEFAULT_METHOD_TRACE_FILE_NAME, 0, 0);
132    }
133
134    /**
135     * Start method tracing, specifying a file name as well as a default
136     * buffer size. See <a
137     * href="{@docRoot}reference/traceview.html"> Running the
138     * Traceview Debugging Program</a> for information about reading
139     * trace files.
140     *
141     * <p>You can use either a fully qualified path and
142     * name, or just a name. If only a name is specified, the file will
143     * be created under the /sdcard/ directory. If a name is not given,
144     * the default is /sdcard/dmtrace.trace.</p>
145     *
146     * @param traceFileName name to give the trace file
147     * @param bufferSize the maximum size of both files combined. If passed
148     * as <code>0</code>, it defaults to 8MB.
149     * @param flags flags to control method tracing. The only one that
150     * is currently defined is {@link #TRACE_COUNT_ALLOCS}.
151     */
152    public static native void startMethodTracing(String traceFileName,
153        int bufferSize, int flags);
154
155    /**
156     * Stops method tracing.
157     */
158    public static native void stopMethodTracing();
159
160    /**
161     * Starts sending Dalvik method trace info to the emulator.
162     */
163    public static native void startEmulatorTracing();
164
165    /**
166     * Stops sending Dalvik method trace info to the emulator.
167     */
168    public static native void stopEmulatorTracing();
169
170    /**
171     * Get an indication of thread CPU usage. The value returned indicates the
172     * amount of time that the current thread has spent executing code or
173     * waiting for certain types of I/O.
174     * <p>
175     * The time is expressed in nanoseconds, and is only meaningful when
176     * compared to the result from an earlier call. Note that nanosecond
177     * resolution does not imply nanosecond accuracy.
178     *
179     * @return the CPU usage. A value of -1 means the system does not support
180     *         this feature.
181     */
182    public static native long threadCpuTimeNanos();
183
184    /**
185     * Count the number and aggregate size of memory allocations between
186     * two points.
187     */
188    public static native void startAllocCounting();
189    public static native void stopAllocCounting();
190    public static native int getAllocCount(int kind);
191    public static native void resetAllocCount(int kinds);
192
193    /**
194     * Establishes an object allocation limit in the current thread. Useful for
195     * catching regressions in code that is expected to operate without causing
196     * any allocations. The limit is valid from the return of this method until
197     * it is either changed or the thread terminates.
198     *
199     * @param limit
200     *            the new limit. A value of 0 means not a single new object may
201     *            be allocated. A value of -1 disables the limit.
202     *
203     * @return the previous limit, or -1 if no limit was set
204     *
205     * @see #setGlobalAllocationLimit(int)
206     */
207    public static native int setAllocationLimit(int limit);
208
209    /**
210     * Establishes an object allocation limit for the entire VM. Useful for
211     * catching regressions in code that is expected to operate without causing
212     * any allocations. The limit is valid from the return of this method until
213     * it is either changed or the thread terminates.
214     *
215     * @param limit
216     *            the new limit. A value of 0 means not a single new object may
217     *            be allocated. A value of -1 disables the limit.
218     *
219     * @return the previous limit, or -1 if no limit was set
220     *
221     * @see #setAllocationLimit(int)
222     */
223    public static native int setGlobalAllocationLimit(int limit);
224
225    /**
226     * Count the number of instructions executed between two points.
227     */
228    public static native void startInstructionCounting();
229    public static native void stopInstructionCounting();
230    public static native void getInstructionCount(int[] counts);
231    public static native void resetInstructionCount();
232
233    /**
234     * Dumps a list of loaded class to the log file.
235     */
236    public static native void printLoadedClasses(int flags);
237
238    /**
239     * Gets the number of loaded classes.
240     *
241     * @return the number of loaded classes
242     */
243    public static native int getLoadedClassCount();
244
245    /**
246     * Dump "hprof" data to the specified file.  This will cause a GC.
247     *
248     * The VM may create a temporary file in the same directory.
249     *
250     * @param fileName Full pathname of output file (e.g. "/sdcard/dump.hprof").
251     * @throws UnsupportedOperationException if the VM was built without
252     *         HPROF support.
253     * @throws IOException if an error occurs while opening or writing files.
254     */
255    public static native void dumpHprofData(String fileName) throws IOException;
256
257    /* don't ask */
258    static native void printThis(Object thisThing, int count, int thing);
259
260    /*
261     * Fake method, inserted into dmtrace output when the garbage collector
262     * runs.  Not actually called.
263     */
264    private static void startGC() {}
265
266    /*
267     * Fake method, inserted into dmtrace output during class preparation
268     * (loading and linking, but not verification or initialization).  Not
269     * actually called.
270     */
271    private static void startClassPrep() {}
272}
273