Debug.java revision 3025ef332c29e255388f74b2afefe05f64bce07c
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 android.os;
18
19import com.android.internal.util.TypedProperties;
20
21import android.util.Config;
22import android.util.Log;
23
24import java.io.FileDescriptor;
25import java.io.FileNotFoundException;
26import java.io.FileOutputStream;
27import java.io.FileReader;
28import java.io.IOException;
29import java.io.OutputStreamWriter;
30import java.io.PrintWriter;
31import java.io.Reader;
32import java.lang.reflect.Field;
33import java.lang.reflect.Modifier;
34import java.lang.annotation.Target;
35import java.lang.annotation.ElementType;
36import java.lang.annotation.Retention;
37import java.lang.annotation.RetentionPolicy;
38
39import org.apache.harmony.dalvik.ddmc.Chunk;
40import org.apache.harmony.dalvik.ddmc.ChunkHandler;
41import org.apache.harmony.dalvik.ddmc.DdmServer;
42
43import dalvik.bytecode.Opcodes;
44import dalvik.system.VMDebug;
45
46
47/**
48 * Provides various debugging functions for Android applications, including
49 * tracing and allocation counts.
50 * <p><strong>Logging Trace Files</strong></p>
51 * <p>Debug can create log files that give details about an application, such as
52 * a call stack and start/stop times for any running methods. See <a
53href="{@docRoot}guide/developing/tools/traceview.html">Traceview: A Graphical Log Viewer</a> for
54 * information about reading trace files. To start logging trace files, call one
55 * of the startMethodTracing() methods. To stop tracing, call
56 * {@link #stopMethodTracing()}.
57 */
58public final class Debug
59{
60    /**
61     * Flags for startMethodTracing().  These can be ORed together.
62     *
63     * TRACE_COUNT_ALLOCS adds the results from startAllocCounting to the
64     * trace key file.
65     */
66    public static final int TRACE_COUNT_ALLOCS  = VMDebug.TRACE_COUNT_ALLOCS;
67
68    /**
69     * Flags for printLoadedClasses().  Default behavior is to only show
70     * the class name.
71     */
72    public static final int SHOW_FULL_DETAIL    = 1;
73    public static final int SHOW_CLASSLOADER    = (1 << 1);
74    public static final int SHOW_INITIALIZED    = (1 << 2);
75
76    // set/cleared by waitForDebugger()
77    private static volatile boolean mWaiting = false;
78
79    private Debug() {}
80
81    /*
82     * How long to wait for the debugger to finish sending requests.  I've
83     * seen this hit 800msec on the device while waiting for a response
84     * to travel over USB and get processed, so we take that and add
85     * half a second.
86     */
87    private static final int MIN_DEBUGGER_IDLE = 1300;      // msec
88
89    /* how long to sleep when polling for activity */
90    private static final int SPIN_DELAY = 200;              // msec
91
92    /**
93     * Default trace file path and file
94     */
95    private static final String DEFAULT_TRACE_PATH_PREFIX = "/sdcard/";
96    private static final String DEFAULT_TRACE_BODY = "dmtrace";
97    private static final String DEFAULT_TRACE_EXTENSION = ".trace";
98    private static final String DEFAULT_TRACE_FILE_PATH =
99        DEFAULT_TRACE_PATH_PREFIX + DEFAULT_TRACE_BODY
100        + DEFAULT_TRACE_EXTENSION;
101
102
103    /**
104     * This class is used to retrieved various statistics about the memory mappings for this
105     * process. The returns info broken down by dalvik, native, and other. All results are in kB.
106     */
107    public static class MemoryInfo implements Parcelable {
108        /** The proportional set size for dalvik. */
109        public int dalvikPss;
110        /** The private dirty pages used by dalvik. */
111        public int dalvikPrivateDirty;
112        /** The shared dirty pages used by dalvik. */
113        public int dalvikSharedDirty;
114
115        /** The proportional set size for the native heap. */
116        public int nativePss;
117        /** The private dirty pages used by the native heap. */
118        public int nativePrivateDirty;
119        /** The shared dirty pages used by the native heap. */
120        public int nativeSharedDirty;
121
122        /** The proportional set size for everything else. */
123        public int otherPss;
124        /** The private dirty pages used by everything else. */
125        public int otherPrivateDirty;
126        /** The shared dirty pages used by everything else. */
127        public int otherSharedDirty;
128
129        public MemoryInfo() {
130        }
131
132        public int describeContents() {
133            return 0;
134        }
135
136        public void writeToParcel(Parcel dest, int flags) {
137            dest.writeInt(dalvikPss);
138            dest.writeInt(dalvikPrivateDirty);
139            dest.writeInt(dalvikSharedDirty);
140            dest.writeInt(nativePss);
141            dest.writeInt(nativePrivateDirty);
142            dest.writeInt(nativeSharedDirty);
143            dest.writeInt(otherPss);
144            dest.writeInt(otherPrivateDirty);
145            dest.writeInt(otherSharedDirty);
146        }
147
148        public void readFromParcel(Parcel source) {
149            dalvikPss = source.readInt();
150            dalvikPrivateDirty = source.readInt();
151            dalvikSharedDirty = source.readInt();
152            nativePss = source.readInt();
153            nativePrivateDirty = source.readInt();
154            nativeSharedDirty = source.readInt();
155            otherPss = source.readInt();
156            otherPrivateDirty = source.readInt();
157            otherSharedDirty = source.readInt();
158        }
159
160        public static final Creator<MemoryInfo> CREATOR = new Creator<MemoryInfo>() {
161            public MemoryInfo createFromParcel(Parcel source) {
162                return new MemoryInfo(source);
163            }
164            public MemoryInfo[] newArray(int size) {
165                return new MemoryInfo[size];
166            }
167        };
168
169        private MemoryInfo(Parcel source) {
170            readFromParcel(source);
171        }
172    }
173
174
175    /**
176     * Wait until a debugger attaches.  As soon as the debugger attaches,
177     * this returns, so you will need to place a breakpoint after the
178     * waitForDebugger() call if you want to start tracing immediately.
179     */
180    public static void waitForDebugger() {
181        if (!VMDebug.isDebuggingEnabled()) {
182            //System.out.println("debugging not enabled, not waiting");
183            return;
184        }
185        if (isDebuggerConnected())
186            return;
187
188        // if DDMS is listening, inform them of our plight
189        System.out.println("Sending WAIT chunk");
190        byte[] data = new byte[] { 0 };     // 0 == "waiting for debugger"
191        Chunk waitChunk = new Chunk(ChunkHandler.type("WAIT"), data, 0, 1);
192        DdmServer.sendChunk(waitChunk);
193
194        mWaiting = true;
195        while (!isDebuggerConnected()) {
196            try { Thread.sleep(SPIN_DELAY); }
197            catch (InterruptedException ie) {}
198        }
199        mWaiting = false;
200
201        System.out.println("Debugger has connected");
202
203        /*
204         * There is no "ready to go" signal from the debugger, and we're
205         * not allowed to suspend ourselves -- the debugger expects us to
206         * be running happily, and gets confused if we aren't.  We need to
207         * allow the debugger a chance to set breakpoints before we start
208         * running again.
209         *
210         * Sit and spin until the debugger has been idle for a short while.
211         */
212        while (true) {
213            long delta = VMDebug.lastDebuggerActivity();
214            if (delta < 0) {
215                System.out.println("debugger detached?");
216                break;
217            }
218
219            if (delta < MIN_DEBUGGER_IDLE) {
220                System.out.println("waiting for debugger to settle...");
221                try { Thread.sleep(SPIN_DELAY); }
222                catch (InterruptedException ie) {}
223            } else {
224                System.out.println("debugger has settled (" + delta + ")");
225                break;
226            }
227        }
228    }
229
230    /**
231     * Returns "true" if one or more threads is waiting for a debugger
232     * to attach.
233     */
234    public static boolean waitingForDebugger() {
235        return mWaiting;
236    }
237
238    /**
239     * Determine if a debugger is currently attached.
240     */
241    public static boolean isDebuggerConnected() {
242        return VMDebug.isDebuggerConnected();
243    }
244
245    /**
246     * Change the JDWP port.
247     *
248     * @deprecated no longer needed or useful
249     */
250    @Deprecated
251    public static void changeDebugPort(int port) {}
252
253    /**
254     * This is the pathname to the sysfs file that enables and disables
255     * tracing on the qemu emulator.
256     */
257    private static final String SYSFS_QEMU_TRACE_STATE = "/sys/qemu_trace/state";
258
259    /**
260     * Enable qemu tracing. For this to work requires running everything inside
261     * the qemu emulator; otherwise, this method will have no effect. The trace
262     * file is specified on the command line when the emulator is started. For
263     * example, the following command line <br />
264     * <code>emulator -trace foo</code><br />
265     * will start running the emulator and create a trace file named "foo". This
266     * method simply enables writing the trace records to the trace file.
267     *
268     * <p>
269     * The main differences between this and {@link #startMethodTracing()} are
270     * that tracing in the qemu emulator traces every cpu instruction of every
271     * process, including kernel code, so we have more complete information,
272     * including all context switches. We can also get more detailed information
273     * such as cache misses. The sequence of calls is determined by
274     * post-processing the instruction trace. The qemu tracing is also done
275     * without modifying the application or perturbing the timing of calls
276     * because no instrumentation is added to the application being traced.
277     * </p>
278     *
279     * <p>
280     * One limitation of using this method compared to using
281     * {@link #startMethodTracing()} on the real device is that the emulator
282     * does not model all of the real hardware effects such as memory and
283     * bus contention.  The emulator also has a simple cache model and cannot
284     * capture all the complexities of a real cache.
285     * </p>
286     */
287    public static void startNativeTracing() {
288        // Open the sysfs file for writing and write "1" to it.
289        PrintWriter outStream = null;
290        try {
291            FileOutputStream fos = new FileOutputStream(SYSFS_QEMU_TRACE_STATE);
292            outStream = new PrintWriter(new OutputStreamWriter(fos));
293            outStream.println("1");
294        } catch (Exception e) {
295        } finally {
296            if (outStream != null)
297                outStream.close();
298        }
299
300        VMDebug.startEmulatorTracing();
301    }
302
303    /**
304     * Stop qemu tracing.  See {@link #startNativeTracing()} to start tracing.
305     *
306     * <p>Tracing can be started and stopped as many times as desired.  When
307     * the qemu emulator itself is stopped then the buffered trace records
308     * are flushed and written to the trace file.  In fact, it is not necessary
309     * to call this method at all; simply killing qemu is sufficient.  But
310     * starting and stopping a trace is useful for examining a specific
311     * region of code.</p>
312     */
313    public static void stopNativeTracing() {
314        VMDebug.stopEmulatorTracing();
315
316        // Open the sysfs file for writing and write "0" to it.
317        PrintWriter outStream = null;
318        try {
319            FileOutputStream fos = new FileOutputStream(SYSFS_QEMU_TRACE_STATE);
320            outStream = new PrintWriter(new OutputStreamWriter(fos));
321            outStream.println("0");
322        } catch (Exception e) {
323            // We could print an error message here but we probably want
324            // to quietly ignore errors if we are not running in the emulator.
325        } finally {
326            if (outStream != null)
327                outStream.close();
328        }
329    }
330
331    /**
332     * Enable "emulator traces", in which information about the current
333     * method is made available to the "emulator -trace" feature.  There
334     * is no corresponding "disable" call -- this is intended for use by
335     * the framework when tracing should be turned on and left that way, so
336     * that traces captured with F9/F10 will include the necessary data.
337     *
338     * This puts the VM into "profile" mode, which has performance
339     * consequences.
340     *
341     * To temporarily enable tracing, use {@link #startNativeTracing()}.
342     */
343    public static void enableEmulatorTraceOutput() {
344        VMDebug.startEmulatorTracing();
345    }
346
347    /**
348     * Start method tracing with default log name and buffer size. See <a
349href="{@docRoot}guide/developing/tools/traceview.html">Traceview: A Graphical Log Viewer</a> for
350     * information about reading these files. Call stopMethodTracing() to stop
351     * tracing.
352     */
353    public static void startMethodTracing() {
354        VMDebug.startMethodTracing(DEFAULT_TRACE_FILE_PATH, 0, 0);
355    }
356
357    /**
358     * Start method tracing, specifying the trace log file name.  The trace
359     * file will be put under "/sdcard" unless an absolute path is given.
360     * See <a
361       href="{@docRoot}guide/developing/tools/traceview.html">Traceview: A Graphical Log Viewer</a> for
362     * information about reading trace files.
363     *
364     * @param traceName Name for the trace log file to create.
365     * If no name argument is given, this value defaults to "/sdcard/dmtrace.trace".
366     * If the files already exist, they will be truncated.
367     * If the trace file given does not end in ".trace", it will be appended for you.
368     */
369    public static void startMethodTracing(String traceName) {
370        startMethodTracing(traceName, 0, 0);
371    }
372
373    /**
374     * Start method tracing, specifying the trace log file name and the
375     * buffer size. The trace files will be put under "/sdcard" unless an
376     * absolute path is given. See <a
377       href="{@docRoot}guide/developing/tools/traceview.html">Traceview: A Graphical Log Viewer</a> for
378     * information about reading trace files.
379     * @param traceName    Name for the trace log file to create.
380     * If no name argument is given, this value defaults to "/sdcard/dmtrace.trace".
381     * If the files already exist, they will be truncated.
382     * If the trace file given does not end in ".trace", it will be appended for you.
383     *
384     * @param bufferSize    The maximum amount of trace data we gather. If not given, it defaults to 8MB.
385     */
386    public static void startMethodTracing(String traceName, int bufferSize) {
387        startMethodTracing(traceName, bufferSize, 0);
388    }
389
390    /**
391     * Start method tracing, specifying the trace log file name and the
392     * buffer size. The trace files will be put under "/sdcard" unless an
393     * absolute path is given. See <a
394       href="{@docRoot}guide/developing/tools/traceview.html">Traceview: A Graphical Log Viewer</a> for
395     * information about reading trace files.
396     *
397     * <p>
398     * When method tracing is enabled, the VM will run more slowly than
399     * usual, so the timings from the trace files should only be considered
400     * in relative terms (e.g. was run #1 faster than run #2).  The times
401     * for native methods will not change, so don't try to use this to
402     * compare the performance of interpreted and native implementations of the
403     * same method.  As an alternative, consider using "native" tracing
404     * in the emulator via {@link #startNativeTracing()}.
405     * </p>
406     *
407     * @param traceName    Name for the trace log file to create.
408     * If no name argument is given, this value defaults to "/sdcard/dmtrace.trace".
409     * If the files already exist, they will be truncated.
410     * If the trace file given does not end in ".trace", it will be appended for you.
411     * @param bufferSize    The maximum amount of trace data we gather. If not given, it defaults to 8MB.
412     */
413    public static void startMethodTracing(String traceName, int bufferSize,
414        int flags) {
415
416        String pathName = traceName;
417        if (pathName.charAt(0) != '/')
418            pathName = DEFAULT_TRACE_PATH_PREFIX + pathName;
419        if (!pathName.endsWith(DEFAULT_TRACE_EXTENSION))
420            pathName = pathName + DEFAULT_TRACE_EXTENSION;
421
422        VMDebug.startMethodTracing(pathName, bufferSize, flags);
423    }
424
425    /**
426     * Like startMethodTracing(String, int, int), but taking an already-opened
427     * FileDescriptor in which the trace is written.  The file name is also
428     * supplied simply for logging.  Makes a dup of the file descriptor.
429     *
430     * Not exposed in the SDK unless we are really comfortable with supporting
431     * this and find it would be useful.
432     * @hide
433     */
434    public static void startMethodTracing(String traceName, FileDescriptor fd,
435        int bufferSize, int flags) {
436        VMDebug.startMethodTracing(traceName, fd, bufferSize, flags);
437    }
438
439    /**
440     * Determine whether method tracing is currently active.
441     * @hide
442     */
443    public static boolean isMethodTracingActive() {
444        return VMDebug.isMethodTracingActive();
445    }
446
447    /**
448     * Stop method tracing.
449     */
450    public static void stopMethodTracing() {
451        VMDebug.stopMethodTracing();
452    }
453
454    /**
455     * Get an indication of thread CPU usage.  The value returned
456     * indicates the amount of time that the current thread has spent
457     * executing code or waiting for certain types of I/O.
458     *
459     * The time is expressed in nanoseconds, and is only meaningful
460     * when compared to the result from an earlier call.  Note that
461     * nanosecond resolution does not imply nanosecond accuracy.
462     *
463     * On system which don't support this operation, the call returns -1.
464     */
465    public static long threadCpuTimeNanos() {
466        return VMDebug.threadCpuTimeNanos();
467    }
468
469    /**
470     * Count the number and aggregate size of memory allocations between
471     * two points.
472     *
473     * The "start" function resets the counts and enables counting.  The
474     * "stop" function disables the counting so that the analysis code
475     * doesn't cause additional allocations.  The "get" function returns
476     * the specified value.
477     *
478     * Counts are kept for the system as a whole and for each thread.
479     * The per-thread counts for threads other than the current thread
480     * are not cleared by the "reset" or "start" calls.
481     */
482    public static void startAllocCounting() {
483        VMDebug.startAllocCounting();
484    }
485    public static void stopAllocCounting() {
486        VMDebug.stopAllocCounting();
487    }
488
489    public static int getGlobalAllocCount() {
490        return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_OBJECTS);
491    }
492    public static int getGlobalAllocSize() {
493        return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_BYTES);
494    }
495    public static int getGlobalFreedCount() {
496        return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_FREED_OBJECTS);
497    }
498    public static int getGlobalFreedSize() {
499        return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_FREED_BYTES);
500    }
501    public static int getGlobalExternalAllocCount() {
502        return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_EXT_ALLOCATED_OBJECTS);
503    }
504    public static int getGlobalExternalAllocSize() {
505        return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_EXT_ALLOCATED_BYTES);
506    }
507    public static int getGlobalExternalFreedCount() {
508        return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_EXT_FREED_OBJECTS);
509    }
510    public static int getGlobalExternalFreedSize() {
511        return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_EXT_FREED_BYTES);
512    }
513    public static int getGlobalGcInvocationCount() {
514        return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_GC_INVOCATIONS);
515    }
516    public static int getThreadAllocCount() {
517        return VMDebug.getAllocCount(VMDebug.KIND_THREAD_ALLOCATED_OBJECTS);
518    }
519    public static int getThreadAllocSize() {
520        return VMDebug.getAllocCount(VMDebug.KIND_THREAD_ALLOCATED_BYTES);
521    }
522    public static int getThreadExternalAllocCount() {
523        return VMDebug.getAllocCount(VMDebug.KIND_THREAD_EXT_ALLOCATED_OBJECTS);
524    }
525    public static int getThreadExternalAllocSize() {
526        return VMDebug.getAllocCount(VMDebug.KIND_THREAD_EXT_ALLOCATED_BYTES);
527    }
528    public static int getThreadGcInvocationCount() {
529        return VMDebug.getAllocCount(VMDebug.KIND_THREAD_GC_INVOCATIONS);
530    }
531
532    public static void resetGlobalAllocCount() {
533        VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_OBJECTS);
534    }
535    public static void resetGlobalAllocSize() {
536        VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_BYTES);
537    }
538    public static void resetGlobalFreedCount() {
539        VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_FREED_OBJECTS);
540    }
541    public static void resetGlobalFreedSize() {
542        VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_FREED_BYTES);
543    }
544    public static void resetGlobalExternalAllocCount() {
545        VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_EXT_ALLOCATED_OBJECTS);
546    }
547    public static void resetGlobalExternalAllocSize() {
548        VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_EXT_ALLOCATED_BYTES);
549    }
550    public static void resetGlobalExternalFreedCount() {
551        VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_EXT_FREED_OBJECTS);
552    }
553    public static void resetGlobalExternalFreedSize() {
554        VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_EXT_FREED_BYTES);
555    }
556    public static void resetGlobalGcInvocationCount() {
557        VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_GC_INVOCATIONS);
558    }
559    public static void resetThreadAllocCount() {
560        VMDebug.resetAllocCount(VMDebug.KIND_THREAD_ALLOCATED_OBJECTS);
561    }
562    public static void resetThreadAllocSize() {
563        VMDebug.resetAllocCount(VMDebug.KIND_THREAD_ALLOCATED_BYTES);
564    }
565    public static void resetThreadExternalAllocCount() {
566        VMDebug.resetAllocCount(VMDebug.KIND_THREAD_EXT_ALLOCATED_OBJECTS);
567    }
568    public static void resetThreadExternalAllocSize() {
569        VMDebug.resetAllocCount(VMDebug.KIND_THREAD_EXT_ALLOCATED_BYTES);
570    }
571    public static void resetThreadGcInvocationCount() {
572        VMDebug.resetAllocCount(VMDebug.KIND_THREAD_GC_INVOCATIONS);
573    }
574    public static void resetAllCounts() {
575        VMDebug.resetAllocCount(VMDebug.KIND_ALL_COUNTS);
576    }
577
578    /**
579     * Returns the size of the native heap.
580     * @return The size of the native heap in bytes.
581     */
582    public static native long getNativeHeapSize();
583
584    /**
585     * Returns the amount of allocated memory in the native heap.
586     * @return The allocated size in bytes.
587     */
588    public static native long getNativeHeapAllocatedSize();
589
590    /**
591     * Returns the amount of free memory in the native heap.
592     * @return The freed size in bytes.
593     */
594    public static native long getNativeHeapFreeSize();
595
596    /**
597     * Retrieves information about this processes memory usages. This information is broken down by
598     * how much is in use by dalivk, the native heap, and everything else.
599     */
600    public static native void getMemoryInfo(MemoryInfo memoryInfo);
601
602    /**
603     * Note: currently only works when the requested pid has the same UID
604     * as the caller.
605     * @hide
606     */
607    public static native void getMemoryInfo(int pid, MemoryInfo memoryInfo);
608
609    /**
610     * Establish an object allocation limit in the current thread.  Useful
611     * for catching regressions in code that is expected to operate
612     * without causing any allocations.
613     *
614     * Pass in the maximum number of allowed allocations.  Use -1 to disable
615     * the limit.  Returns the previous limit.
616     *
617     * The preferred way to use this is:
618     *
619     *  int prevLimit = -1;
620     *  try {
621     *      prevLimit = Debug.setAllocationLimit(0);
622     *      ... do stuff that's not expected to allocate memory ...
623     *  } finally {
624     *      Debug.setAllocationLimit(prevLimit);
625     *  }
626     *
627     * This allows limits to be nested.  The try/finally ensures that the
628     * limit is reset if something fails.
629     *
630     * Exceeding the limit causes a dalvik.system.AllocationLimitError to
631     * be thrown from a memory allocation call.  The limit is reset to -1
632     * when this happens.
633     *
634     * The feature may be disabled in the VM configuration.  If so, this
635     * call has no effect, and always returns -1.
636     */
637    public static int setAllocationLimit(int limit) {
638        return VMDebug.setAllocationLimit(limit);
639    }
640
641    /**
642     * Establish a global object allocation limit.  This is similar to
643     * {@link #setAllocationLimit(int)} but applies to all threads in
644     * the VM.  It will coexist peacefully with per-thread limits.
645     *
646     * [ The value of "limit" is currently restricted to 0 (no allocations
647     *   allowed) or -1 (no global limit).  This may be changed in a future
648     *   release. ]
649     */
650    public static int setGlobalAllocationLimit(int limit) {
651        if (limit != 0 && limit != -1)
652            throw new IllegalArgumentException("limit must be 0 or -1");
653        return VMDebug.setGlobalAllocationLimit(limit);
654    }
655
656    /**
657     * Dump a list of all currently loaded class to the log file.
658     *
659     * @param flags See constants above.
660     */
661    public static void printLoadedClasses(int flags) {
662        VMDebug.printLoadedClasses(flags);
663    }
664
665    /**
666     * Get the number of loaded classes.
667     * @return the number of loaded classes.
668     */
669    public static int getLoadedClassCount() {
670        return VMDebug.getLoadedClassCount();
671    }
672
673    /**
674     * Dump "hprof" data to the specified file.  This will cause a GC.
675     *
676     * @param fileName Full pathname of output file (e.g. "/sdcard/dump.hprof").
677     * @throws UnsupportedOperationException if the VM was built without
678     *         HPROF support.
679     * @throws IOException if an error occurs while opening or writing files.
680     */
681    public static void dumpHprofData(String fileName) throws IOException {
682        VMDebug.dumpHprofData(fileName);
683    }
684
685    /**
686     * Returns the number of sent transactions from this process.
687     * @return The number of sent transactions or -1 if it could not read t.
688     */
689    public static native int getBinderSentTransactions();
690
691    /**
692     * Returns the number of received transactions from the binder driver.
693     * @return The number of received transactions or -1 if it could not read the stats.
694     */
695    public static native int getBinderReceivedTransactions();
696
697    /**
698     * Returns the number of active local Binder objects that exist in the
699     * current process.
700     */
701    public static final native int getBinderLocalObjectCount();
702
703    /**
704     * Returns the number of references to remote proxy Binder objects that
705     * exist in the current process.
706     */
707    public static final native int getBinderProxyObjectCount();
708
709    /**
710     * Returns the number of death notification links to Binder objects that
711     * exist in the current process.
712     */
713    public static final native int getBinderDeathObjectCount();
714
715    /**
716     * Primes the register map cache.
717     *
718     * Only works for classes in the bootstrap class loader.  Does not
719     * cause classes to be loaded if they're not already present.
720     *
721     * The classAndMethodDesc argument is a concatentation of the VM-internal
722     * class descriptor, method name, and method descriptor.  Examples:
723     *     Landroid/os/Looper;.loop:()V
724     *     Landroid/app/ActivityThread;.main:([Ljava/lang/String;)V
725     *
726     * @param classAndMethodDesc the method to prepare
727     *
728     * @hide
729     */
730    public static final boolean cacheRegisterMap(String classAndMethodDesc) {
731        return VMDebug.cacheRegisterMap(classAndMethodDesc);
732    }
733
734    /**
735     * API for gathering and querying instruction counts.
736     *
737     * Example usage:
738     *   Debug.InstructionCount icount = new Debug.InstructionCount();
739     *   icount.resetAndStart();
740     *    [... do lots of stuff ...]
741     *   if (icount.collect()) {
742     *       System.out.println("Total instructions executed: "
743     *           + icount.globalTotal());
744     *       System.out.println("Method invocations: "
745     *           + icount.globalMethodInvocations());
746     *   }
747     */
748    public static class InstructionCount {
749        private static final int NUM_INSTR = 256;
750
751        private int[] mCounts;
752
753        public InstructionCount() {
754            mCounts = new int[NUM_INSTR];
755        }
756
757        /**
758         * Reset counters and ensure counts are running.  Counts may
759         * have already been running.
760         *
761         * @return true if counting was started
762         */
763        public boolean resetAndStart() {
764            try {
765                VMDebug.startInstructionCounting();
766                VMDebug.resetInstructionCount();
767            } catch (UnsupportedOperationException uoe) {
768                return false;
769            }
770            return true;
771        }
772
773        /**
774         * Collect instruction counts.  May or may not stop the
775         * counting process.
776         */
777        public boolean collect() {
778            try {
779                VMDebug.stopInstructionCounting();
780                VMDebug.getInstructionCount(mCounts);
781            } catch (UnsupportedOperationException uoe) {
782                return false;
783            }
784            return true;
785        }
786
787        /**
788         * Return the total number of instructions executed globally (i.e. in
789         * all threads).
790         */
791        public int globalTotal() {
792            int count = 0;
793            for (int i = 0; i < NUM_INSTR; i++)
794                count += mCounts[i];
795            return count;
796        }
797
798        /**
799         * Return the total number of method-invocation instructions
800         * executed globally.
801         */
802        public int globalMethodInvocations() {
803            int count = 0;
804
805            //count += mCounts[Opcodes.OP_EXECUTE_INLINE];
806            count += mCounts[Opcodes.OP_INVOKE_VIRTUAL];
807            count += mCounts[Opcodes.OP_INVOKE_SUPER];
808            count += mCounts[Opcodes.OP_INVOKE_DIRECT];
809            count += mCounts[Opcodes.OP_INVOKE_STATIC];
810            count += mCounts[Opcodes.OP_INVOKE_INTERFACE];
811            count += mCounts[Opcodes.OP_INVOKE_VIRTUAL_RANGE];
812            count += mCounts[Opcodes.OP_INVOKE_SUPER_RANGE];
813            count += mCounts[Opcodes.OP_INVOKE_DIRECT_RANGE];
814            count += mCounts[Opcodes.OP_INVOKE_STATIC_RANGE];
815            count += mCounts[Opcodes.OP_INVOKE_INTERFACE_RANGE];
816            //count += mCounts[Opcodes.OP_INVOKE_DIRECT_EMPTY];
817            count += mCounts[Opcodes.OP_INVOKE_VIRTUAL_QUICK];
818            count += mCounts[Opcodes.OP_INVOKE_VIRTUAL_QUICK_RANGE];
819            count += mCounts[Opcodes.OP_INVOKE_SUPER_QUICK];
820            count += mCounts[Opcodes.OP_INVOKE_SUPER_QUICK_RANGE];
821            return count;
822        }
823    }
824
825
826    /**
827     * A Map of typed debug properties.
828     */
829    private static final TypedProperties debugProperties;
830
831    /*
832     * Load the debug properties from the standard files into debugProperties.
833     */
834    static {
835        if (Config.DEBUG) {
836            final String TAG = "DebugProperties";
837            final String[] files = { "/system/debug.prop", "/debug.prop", "/data/debug.prop" };
838            final TypedProperties tp = new TypedProperties();
839
840            // Read the properties from each of the files, if present.
841            for (String file : files) {
842                Reader r;
843                try {
844                    r = new FileReader(file);
845                } catch (FileNotFoundException ex) {
846                    // It's ok if a file is missing.
847                    continue;
848                }
849
850                try {
851                    tp.load(r);
852                } catch (Exception ex) {
853                    throw new RuntimeException("Problem loading " + file, ex);
854                } finally {
855                    try {
856                        r.close();
857                    } catch (IOException ex) {
858                        // Ignore this error.
859                    }
860                }
861            }
862
863            debugProperties = tp.isEmpty() ? null : tp;
864        } else {
865            debugProperties = null;
866        }
867    }
868
869
870    /**
871     * Returns true if the type of the field matches the specified class.
872     * Handles the case where the class is, e.g., java.lang.Boolean, but
873     * the field is of the primitive "boolean" type.  Also handles all of
874     * the java.lang.Number subclasses.
875     */
876    private static boolean fieldTypeMatches(Field field, Class<?> cl) {
877        Class<?> fieldClass = field.getType();
878        if (fieldClass == cl) {
879            return true;
880        }
881        Field primitiveTypeField;
882        try {
883            /* All of the classes we care about (Boolean, Integer, etc.)
884             * have a Class field called "TYPE" that points to the corresponding
885             * primitive class.
886             */
887            primitiveTypeField = cl.getField("TYPE");
888        } catch (NoSuchFieldException ex) {
889            return false;
890        }
891        try {
892            return fieldClass == (Class<?>) primitiveTypeField.get(null);
893        } catch (IllegalAccessException ex) {
894            return false;
895        }
896    }
897
898
899    /**
900     * Looks up the property that corresponds to the field, and sets the field's value
901     * if the types match.
902     */
903    private static void modifyFieldIfSet(final Field field, final TypedProperties properties,
904                                         final String propertyName) {
905        if (field.getType() == java.lang.String.class) {
906            int stringInfo = properties.getStringInfo(propertyName);
907            switch (stringInfo) {
908                case TypedProperties.STRING_SET:
909                    // Handle as usual below.
910                    break;
911                case TypedProperties.STRING_NULL:
912                    try {
913                        field.set(null, null);  // null object for static fields; null string
914                    } catch (IllegalAccessException ex) {
915                        throw new IllegalArgumentException(
916                            "Cannot set field for " + propertyName, ex);
917                    }
918                    return;
919                case TypedProperties.STRING_NOT_SET:
920                    return;
921                case TypedProperties.STRING_TYPE_MISMATCH:
922                    throw new IllegalArgumentException(
923                        "Type of " + propertyName + " " +
924                        " does not match field type (" + field.getType() + ")");
925                default:
926                    throw new IllegalStateException(
927                        "Unexpected getStringInfo(" + propertyName + ") return value " +
928                        stringInfo);
929            }
930        }
931        Object value = properties.get(propertyName);
932        if (value != null) {
933            if (!fieldTypeMatches(field, value.getClass())) {
934                throw new IllegalArgumentException(
935                    "Type of " + propertyName + " (" + value.getClass() + ") " +
936                    " does not match field type (" + field.getType() + ")");
937            }
938            try {
939                field.set(null, value);  // null object for static fields
940            } catch (IllegalAccessException ex) {
941                throw new IllegalArgumentException(
942                    "Cannot set field for " + propertyName, ex);
943            }
944        }
945    }
946
947
948    /**
949     * Equivalent to <code>setFieldsOn(cl, false)</code>.
950     *
951     * @see #setFieldsOn(Class, boolean)
952     *
953     * @hide
954     */
955    public static void setFieldsOn(Class<?> cl) {
956        setFieldsOn(cl, false);
957    }
958
959    /**
960     * Reflectively sets static fields of a class based on internal debugging
961     * properties.  This method is a no-op if android.util.Config.DEBUG is
962     * false.
963     * <p>
964     * <strong>NOTE TO APPLICATION DEVELOPERS</strong>: Config.DEBUG will
965     * always be false in release builds.  This API is typically only useful
966     * for platform developers.
967     * </p>
968     * Class setup: define a class whose only fields are non-final, static
969     * primitive types (except for "char") or Strings.  In a static block
970     * after the field definitions/initializations, pass the class to
971     * this method, Debug.setFieldsOn(). Example:
972     * <pre>
973     * package com.example;
974     *
975     * import android.os.Debug;
976     *
977     * public class MyDebugVars {
978     *    public static String s = "a string";
979     *    public static String s2 = "second string";
980     *    public static String ns = null;
981     *    public static boolean b = false;
982     *    public static int i = 5;
983     *    @Debug.DebugProperty
984     *    public static float f = 0.1f;
985     *    @@Debug.DebugProperty
986     *    public static double d = 0.5d;
987     *
988     *    // This MUST appear AFTER all fields are defined and initialized!
989     *    static {
990     *        // Sets all the fields
991     *        Debug.setFieldsOn(MyDebugVars.class);
992     *
993     *        // Sets only the fields annotated with @Debug.DebugProperty
994     *        // Debug.setFieldsOn(MyDebugVars.class, true);
995     *    }
996     * }
997     * </pre>
998     * setFieldsOn() may override the value of any field in the class based
999     * on internal properties that are fixed at boot time.
1000     * <p>
1001     * These properties are only set during platform debugging, and are not
1002     * meant to be used as a general-purpose properties store.
1003     *
1004     * {@hide}
1005     *
1006     * @param cl The class to (possibly) modify
1007     * @param partial If false, sets all static fields, otherwise, only set
1008     *        fields with the {@link android.os.Debug.DebugProperty}
1009     *        annotation
1010     * @throws IllegalArgumentException if any fields are final or non-static,
1011     *         or if the type of the field does not match the type of
1012     *         the internal debugging property value.
1013     */
1014    public static void setFieldsOn(Class<?> cl, boolean partial) {
1015        if (Config.DEBUG) {
1016            if (debugProperties != null) {
1017                /* Only look for fields declared directly by the class,
1018                 * so we don't mysteriously change static fields in superclasses.
1019                 */
1020                for (Field field : cl.getDeclaredFields()) {
1021                    if (!partial || field.getAnnotation(DebugProperty.class) != null) {
1022                        final String propertyName = cl.getName() + "." + field.getName();
1023                        boolean isStatic = Modifier.isStatic(field.getModifiers());
1024                        boolean isFinal = Modifier.isFinal(field.getModifiers());
1025
1026                        if (!isStatic || isFinal) {
1027                            throw new IllegalArgumentException(propertyName +
1028                                " must be static and non-final");
1029                        }
1030                        modifyFieldIfSet(field, debugProperties, propertyName);
1031                    }
1032                }
1033            }
1034        } else {
1035            Log.w("android.os.Debug",
1036                  "setFieldsOn(" + (cl == null ? "null" : cl.getName()) +
1037                  ") called in non-DEBUG build");
1038        }
1039    }
1040
1041    /**
1042     * Annotation to put on fields you want to set with
1043     * {@link Debug#setFieldsOn(Class, boolean)}.
1044     *
1045     * @hide
1046     */
1047    @Target({ ElementType.FIELD })
1048    @Retention(RetentionPolicy.RUNTIME)
1049    public @interface DebugProperty {
1050    }
1051}
1052