to top
Android APIs
public final class

Debug

extends Object
java.lang.Object
   ↳ android.os.Debug

Class Overview

Provides various debugging functions for Android applications, including tracing and allocation counts.

Logging Trace Files

Debug can create log files that give details about an application, such as a call stack and start/stop times for any running methods. See Traceview: A Graphical Log Viewer for information about reading trace files. To start logging trace files, call one of the startMethodTracing() methods. To stop tracing, call stopMethodTracing().

Summary

Nested Classes
class Debug.InstructionCount API for gathering and querying instruction counts. 
class Debug.MemoryInfo This class is used to retrieved various statistics about the memory mappings for this process. 
Constants
int SHOW_CLASSLOADER
int SHOW_FULL_DETAIL Flags for printLoadedClasses().
int SHOW_INITIALIZED
int TRACE_COUNT_ALLOCS Flags for startMethodTracing().
Public Methods
static void changeDebugPort(int port)
This method is deprecated. no longer needed or useful
static void dumpHprofData(String fileName)
Dump "hprof" data to the specified file.
static boolean dumpService(String name, FileDescriptor fd, String[] args)
Get a debugging dump of a system service by name.
static void enableEmulatorTraceOutput()
Enable "emulator traces", in which information about the current method is made available to the "emulator -trace" feature.
final static int getBinderDeathObjectCount()
Returns the number of death notification links to Binder objects that exist in the current process.
final static int getBinderLocalObjectCount()
Returns the number of active local Binder objects that exist in the current process.
final static int getBinderProxyObjectCount()
Returns the number of references to remote proxy Binder objects that exist in the current process.
static int getBinderReceivedTransactions()
Returns the number of received transactions from the binder driver.
static int getBinderSentTransactions()
Returns the number of sent transactions from this process.
static int getGlobalAllocCount()
static int getGlobalAllocSize()
static int getGlobalClassInitCount()
static int getGlobalClassInitTime()
static int getGlobalExternalAllocCount()
This method is deprecated. This method is now obsolete.
static int getGlobalExternalAllocSize()
This method is deprecated. This method is now obsolete.
static int getGlobalExternalFreedCount()
This method is deprecated. This method is now obsolete.
static int getGlobalExternalFreedSize()
This method is deprecated. This method is now obsolete.
static int getGlobalFreedCount()
static int getGlobalFreedSize()
static int getGlobalGcInvocationCount()
static int getLoadedClassCount()
Get the number of loaded classes.
static void getMemoryInfo(Debug.MemoryInfo memoryInfo)
Retrieves information about this processes memory usages.
static long getNativeHeapAllocatedSize()
Returns the amount of allocated memory in the native heap.
static long getNativeHeapFreeSize()
Returns the amount of free memory in the native heap.
static long getNativeHeapSize()
Returns the size of the native heap.
static long getPss()
Retrieves the PSS memory used by the process as given by the smaps.
static int getThreadAllocCount()
static int getThreadAllocSize()
static int getThreadExternalAllocCount()
This method is deprecated. This method is now obsolete.
static int getThreadExternalAllocSize()
This method is deprecated. This method is now obsolete.
static int getThreadGcInvocationCount()
static boolean isDebuggerConnected()
Determine if a debugger is currently attached.
static void printLoadedClasses(int flags)
Dump a list of all currently loaded class to the log file.
static void resetAllCounts()
static void resetGlobalAllocCount()
static void resetGlobalAllocSize()
static void resetGlobalClassInitCount()
static void resetGlobalClassInitTime()
static void resetGlobalExternalAllocCount()
This method is deprecated. This method is now obsolete.
static void resetGlobalExternalAllocSize()
This method is deprecated. This method is now obsolete.
static void resetGlobalExternalFreedCount()
This method is deprecated. This method is now obsolete.
static void resetGlobalExternalFreedSize()
This method is deprecated. This method is now obsolete.
static void resetGlobalFreedCount()
static void resetGlobalFreedSize()
static void resetGlobalGcInvocationCount()
static void resetThreadAllocCount()
static void resetThreadAllocSize()
static void resetThreadExternalAllocCount()
This method is deprecated. This method is now obsolete.
static void resetThreadExternalAllocSize()
This method is deprecated. This method is now obsolete.
static void resetThreadGcInvocationCount()
static int setAllocationLimit(int limit)
This method is deprecated. This method is now obsolete.
static int setGlobalAllocationLimit(int limit)
This method is deprecated. This method is now obsolete.
static void startAllocCounting()
Start counting the number and aggregate size of memory allocations.
static void startMethodTracing(String traceName)
Start method tracing, specifying the trace log file name.
static void startMethodTracing()
Start method tracing with default log name and buffer size.
static void startMethodTracing(String traceName, int bufferSize, int flags)
Start method tracing, specifying the trace log file name and the buffer size.
static void startMethodTracing(String traceName, int bufferSize)
Start method tracing, specifying the trace log file name and the buffer size.
static void startNativeTracing()
Enable qemu tracing.
static void stopAllocCounting()
Stop counting the number and aggregate size of memory allocations.
static void stopMethodTracing()
Stop method tracing.
static void stopNativeTracing()
Stop qemu tracing.
static long threadCpuTimeNanos()
Get an indication of thread CPU usage.
static void waitForDebugger()
Wait until a debugger attaches.
static boolean waitingForDebugger()
Returns "true" if one or more threads is waiting for a debugger to attach.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final int SHOW_CLASSLOADER

Since: API Level 1

Constant Value: 2 (0x00000002)

public static final int SHOW_FULL_DETAIL

Since: API Level 1

Flags for printLoadedClasses(). Default behavior is to only show the class name.

Constant Value: 1 (0x00000001)

public static final int SHOW_INITIALIZED

Since: API Level 1

Constant Value: 4 (0x00000004)

public static final int TRACE_COUNT_ALLOCS

Since: API Level 1

Flags for startMethodTracing(). These can be ORed together. TRACE_COUNT_ALLOCS adds the results from startAllocCounting to the trace key file.

Constant Value: 1 (0x00000001)

Public Methods

public static void changeDebugPort (int port)

Since: API Level 1

This method is deprecated.
no longer needed or useful

Change the JDWP port.

public static void dumpHprofData (String fileName)

Since: API Level 3

Dump "hprof" data to the specified file. This may cause a GC.

Parameters
fileName Full pathname of output file (e.g. "/sdcard/dump.hprof").
Throws
UnsupportedOperationException if the VM was built without HPROF support.
IOException if an error occurs while opening or writing files.

public static boolean dumpService (String name, FileDescriptor fd, String[] args)

Since: API Level 8

Get a debugging dump of a system service by name.

Most services require the caller to hold android.permission.DUMP.

Parameters
name of the service to dump
fd to write dump output to (usually an output log file)
args to pass to the service's dump method, may be null
Returns
  • true if the service was dumped successfully, false if the service could not be found or had an error while dumping

public static void enableEmulatorTraceOutput ()

Since: API Level 1

Enable "emulator traces", in which information about the current method is made available to the "emulator -trace" feature. There is no corresponding "disable" call -- this is intended for use by the framework when tracing should be turned on and left that way, so that traces captured with F9/F10 will include the necessary data. This puts the VM into "profile" mode, which has performance consequences. To temporarily enable tracing, use startNativeTracing().

public static final int getBinderDeathObjectCount ()

Since: API Level 1

Returns the number of death notification links to Binder objects that exist in the current process.

public static final int getBinderLocalObjectCount ()

Since: API Level 1

Returns the number of active local Binder objects that exist in the current process.

public static final int getBinderProxyObjectCount ()

Since: API Level 1

Returns the number of references to remote proxy Binder objects that exist in the current process.

public static int getBinderReceivedTransactions ()

Since: API Level 1

Returns the number of received transactions from the binder driver.

Returns
  • The number of received transactions or -1 if it could not read the stats.

public static int getBinderSentTransactions ()

Since: API Level 1

Returns the number of sent transactions from this process.

Returns
  • The number of sent transactions or -1 if it could not read t.

public static int getGlobalAllocCount ()

Since: API Level 1

public static int getGlobalAllocSize ()

Since: API Level 1

public static int getGlobalClassInitCount ()

Since: API Level 8

public static int getGlobalClassInitTime ()

Since: API Level 8

public static int getGlobalExternalAllocCount ()

Since: API Level 1

This method is deprecated.
This method is now obsolete.

Returns the global count of external allocation requests. The external allocation tracking feature was removed in Honeycomb. This method exists for compatibility and always returns 0.

public static int getGlobalExternalAllocSize ()

Since: API Level 1

This method is deprecated.
This method is now obsolete.

Returns the global count of bytes externally allocated. The external allocation tracking feature was removed in Honeycomb. This method exists for compatibility and always returns 0.

public static int getGlobalExternalFreedCount ()

Since: API Level 1

This method is deprecated.
This method is now obsolete.

Returns the global count of freed external allocation requests. The external allocation tracking feature was removed in Honeycomb. This method exists for compatibility and always returns 0.

public static int getGlobalExternalFreedSize ()

Since: API Level 1

This method is deprecated.
This method is now obsolete.

Returns the global count of freed bytes from external allocation requests. The external allocation tracking feature was removed in Honeycomb. This method exists for compatibility and always returns 0.

public static int getGlobalFreedCount ()

Since: API Level 1

public static int getGlobalFreedSize ()

Since: API Level 1

public static int getGlobalGcInvocationCount ()

Since: API Level 1

public static int getLoadedClassCount ()

Since: API Level 1

Get the number of loaded classes.

Returns
  • the number of loaded classes.

public static void getMemoryInfo (Debug.MemoryInfo memoryInfo)

Since: API Level 1

Retrieves information about this processes memory usages. This information is broken down by how much is in use by dalivk, the native heap, and everything else.

public static long getNativeHeapAllocatedSize ()

Since: API Level 1

Returns the amount of allocated memory in the native heap.

Returns
  • The allocated size in bytes.

public static long getNativeHeapFreeSize ()

Since: API Level 1

Returns the amount of free memory in the native heap.

Returns
  • The freed size in bytes.

public static long getNativeHeapSize ()

Since: API Level 1

Returns the size of the native heap.

Returns
  • The size of the native heap in bytes.

public static long getPss ()

Since: API Level 14

Retrieves the PSS memory used by the process as given by the smaps.

public static int getThreadAllocCount ()

Since: API Level 1

public static int getThreadAllocSize ()

Since: API Level 1

public static int getThreadExternalAllocCount ()

Since: API Level 1

This method is deprecated.
This method is now obsolete.

Returns the count of external allocation requests made by the current thread. The external allocation tracking feature was removed in Honeycomb. This method exists for compatibility and always returns 0.

public static int getThreadExternalAllocSize ()

Since: API Level 1

This method is deprecated.
This method is now obsolete.

Returns the global count of bytes externally allocated. The external allocation tracking feature was removed in Honeycomb. This method exists for compatibility and always returns 0.

public static int getThreadGcInvocationCount ()

Since: API Level 1

public static boolean isDebuggerConnected ()

Since: API Level 1

Determine if a debugger is currently attached.

public static void printLoadedClasses (int flags)

Since: API Level 1

Dump a list of all currently loaded class to the log file.

Parameters
flags See constants above.

public static void resetAllCounts ()

Since: API Level 1

public static void resetGlobalAllocCount ()

Since: API Level 1

public static void resetGlobalAllocSize ()

Since: API Level 1

public static void resetGlobalClassInitCount ()

Since: API Level 8

public static void resetGlobalClassInitTime ()

Since: API Level 8

public static void resetGlobalExternalAllocCount ()

Since: API Level 1

This method is deprecated.
This method is now obsolete.

Resets the global count of external allocation requests. The external allocation tracking feature was removed in Honeycomb. This method exists for compatibility and has no effect.

public static void resetGlobalExternalAllocSize ()

Since: API Level 1

This method is deprecated.
This method is now obsolete.

Resets the global count of bytes externally allocated. The external allocation tracking feature was removed in Honeycomb. This method exists for compatibility and has no effect.

public static void resetGlobalExternalFreedCount ()

Since: API Level 1

This method is deprecated.
This method is now obsolete.

Resets the global count of freed external allocations. The external allocation tracking feature was removed in Honeycomb. This method exists for compatibility and has no effect.

public static void resetGlobalExternalFreedSize ()

Since: API Level 1

This method is deprecated.
This method is now obsolete.

Resets the global count counter of freed bytes from external allocations. The external allocation tracking feature was removed in Honeycomb. This method exists for compatibility and has no effect.

public static void resetGlobalFreedCount ()

Since: API Level 1

public static void resetGlobalFreedSize ()

Since: API Level 1

public static void resetGlobalGcInvocationCount ()

Since: API Level 1

public static void resetThreadAllocCount ()

Since: API Level 1

public static void resetThreadAllocSize ()

Since: API Level 1

public static void resetThreadExternalAllocCount ()

Since: API Level 1

This method is deprecated.
This method is now obsolete.

Resets the count of external allocation requests made by the current thread. The external allocation tracking feature was removed in Honeycomb. This method exists for compatibility and has no effect.

public static void resetThreadExternalAllocSize ()

Since: API Level 1

This method is deprecated.
This method is now obsolete.

Resets the count of bytes externally allocated by the current thread. The external allocation tracking feature was removed in Honeycomb. This method exists for compatibility and has no effect.

public static void resetThreadGcInvocationCount ()

Since: API Level 1

public static int setAllocationLimit (int limit)

Since: API Level 1

This method is deprecated.
This method is now obsolete.

Establish an object allocation limit in the current thread. This feature was never enabled in release builds. The allocation limits feature was removed in Honeycomb. This method exists for compatibility and always returns -1 and has no effect.

public static int setGlobalAllocationLimit (int limit)

Since: API Level 1

This method is deprecated.
This method is now obsolete.

Establish a global object allocation limit. This feature was never enabled in release builds. The allocation limits feature was removed in Honeycomb. This method exists for compatibility and always returns -1 and has no effect.

public static void startAllocCounting ()

Since: API Level 1

Start counting the number and aggregate size of memory allocations.

The start function resets the counts and enables counting. The stop function disables the counting so that the analysis code doesn't cause additional allocations. The various get functions return the specified value. And the various reset functions reset the specified count.

Counts are kept for the system as a whole and for each thread. The per-thread counts for threads other than the current thread are not cleared by the "reset" or "start" calls.

public static void startMethodTracing (String traceName)

Since: API Level 1

Start method tracing, specifying the trace log file name. The trace file will be put under "/sdcard" unless an absolute path is given. See Traceview: A Graphical Log Viewer for information about reading trace files.

Parameters
traceName Name for the trace log file to create. If no name argument is given, this value defaults to "/sdcard/dmtrace.trace". If the files already exist, they will be truncated. If the trace file given does not end in ".trace", it will be appended for you.

public static void startMethodTracing ()

Since: API Level 1

Start method tracing with default log name and buffer size. See Traceview: A Graphical Log Viewer for information about reading these files. Call stopMethodTracing() to stop tracing.

public static void startMethodTracing (String traceName, int bufferSize, int flags)

Since: API Level 1

Start method tracing, specifying the trace log file name and the buffer size. The trace files will be put under "/sdcard" unless an absolute path is given. See Traceview: A Graphical Log Viewer for information about reading trace files.

When method tracing is enabled, the VM will run more slowly than usual, so the timings from the trace files should only be considered in relative terms (e.g. was run #1 faster than run #2). The times for native methods will not change, so don't try to use this to compare the performance of interpreted and native implementations of the same method. As an alternative, consider using "native" tracing in the emulator via startNativeTracing().

Parameters
traceName Name for the trace log file to create. If no name argument is given, this value defaults to "/sdcard/dmtrace.trace". If the files already exist, they will be truncated. If the trace file given does not end in ".trace", it will be appended for you.
bufferSize The maximum amount of trace data we gather. If not given, it defaults to 8MB.

public static void startMethodTracing (String traceName, int bufferSize)

Since: API Level 1

Start method tracing, specifying the trace log file name and the buffer size. The trace files will be put under "/sdcard" unless an absolute path is given. See Traceview: A Graphical Log Viewer for information about reading trace files.

Parameters
traceName Name for the trace log file to create. If no name argument is given, this value defaults to "/sdcard/dmtrace.trace". If the files already exist, they will be truncated. If the trace file given does not end in ".trace", it will be appended for you.
bufferSize The maximum amount of trace data we gather. If not given, it defaults to 8MB.

public static void startNativeTracing ()

Since: API Level 1

Enable qemu tracing. For this to work requires running everything inside the qemu emulator; otherwise, this method will have no effect. The trace file is specified on the command line when the emulator is started. For example, the following command line
emulator -trace foo
will start running the emulator and create a trace file named "foo". This method simply enables writing the trace records to the trace file.

The main differences between this and startMethodTracing() are that tracing in the qemu emulator traces every cpu instruction of every process, including kernel code, so we have more complete information, including all context switches. We can also get more detailed information such as cache misses. The sequence of calls is determined by post-processing the instruction trace. The qemu tracing is also done without modifying the application or perturbing the timing of calls because no instrumentation is added to the application being traced.

One limitation of using this method compared to using startMethodTracing() on the real device is that the emulator does not model all of the real hardware effects such as memory and bus contention. The emulator also has a simple cache model and cannot capture all the complexities of a real cache.

public static void stopAllocCounting ()

Since: API Level 1

Stop counting the number and aggregate size of memory allocations.

public static void stopMethodTracing ()

Since: API Level 1

Stop method tracing.

public static void stopNativeTracing ()

Since: API Level 1

Stop qemu tracing. See startNativeTracing() to start tracing.

Tracing can be started and stopped as many times as desired. When the qemu emulator itself is stopped then the buffered trace records are flushed and written to the trace file. In fact, it is not necessary to call this method at all; simply killing qemu is sufficient. But starting and stopping a trace is useful for examining a specific region of code.

public static long threadCpuTimeNanos ()

Since: API Level 1

Get an indication of thread CPU usage. The value returned indicates the amount of time that the current thread has spent executing code or waiting for certain types of I/O. The time is expressed in nanoseconds, and is only meaningful when compared to the result from an earlier call. Note that nanosecond resolution does not imply nanosecond accuracy. On system which don't support this operation, the call returns -1.

public static void waitForDebugger ()

Since: API Level 1

Wait until a debugger attaches. As soon as the debugger attaches, this returns, so you will need to place a breakpoint after the waitForDebugger() call if you want to start tracing immediately.

public static boolean waitingForDebugger ()

Since: API Level 1

Returns "true" if one or more threads is waiting for a debugger to attach.