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