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