| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | page.title=Debugging Tasks |
| 2 | @jd:body |
| 3 | |
| 4 | <div id="qv-wrapper"> |
| 5 | <div id="qv"> |
| 6 | <h2>In this document</h2> |
| 7 | <ol> |
| 8 | <li><a href="#tools">Tools</a></li> |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 9 | <li><a href="#additionaldebugging">Debug with Dev Tools</a></li> |
| 10 | <li><a href="#DebuggingWebPages">Debugging Web Pages</a></li> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11 | <li><a href="#toptips">Top Debugging Tips</a></li> |
| 12 | <li><a href="#ide-debug-port">Configuring Your IDE to Attach to the Debugging Port</a></li> |
| 13 | </ol> |
| 14 | </div> |
| 15 | </div> |
| 16 | |
| 17 | <p>This document offers some helpful guidance to debugging applications on Android. |
| 18 | |
| 19 | |
| 20 | <h2 id="tools">Tools</h2> |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 21 | |
| 22 | <p>The Android SDK includes a set of tools to help you debug and profile |
| 23 | your applications. Here are some tools that you'll use most often:</p> |
| 24 | |
| 25 | <dl> |
| 26 | <dt><strong><a href="{@docRoot}guide/developing/tools/adb.html">Android Debug Bridge |
| 27 | (ADB)</a></strong></dt> |
| 28 | <dd>Provides various device management capabilities, including |
| 29 | moving and syncing files to the emulator, forwarding ports, and running a UNIX |
| 30 | shell on the emulator.</dd> |
| 31 | <dt><strong><a href="{@docRoot}guide/developing/tools/ddms.html">Dalvik Debug Monitor Server |
| 32 | (DDMS)</a></strong></dt> |
| 33 | <dd>A graphical program that |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | supports port forwarding (so you can set up breakpoints in your code in your |
| 35 | IDE), screen captures on the emulator, thread and stack information, |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 36 | and many other features. You can also run logcat to retrieve your Log messages.</dd> |
| 37 | </dd> |
| 38 | <dt><strong><a href="{@docRoot}guide/developing/tools/traceview.html">Traceview</a></strong></dt> |
| 39 | <dd>A graphical viewer that displays trace file data for method calls and times saved by |
| 40 | your application, which can help you profile the performance of your application.</dd> |
| 41 | <dt><strong><a href="{@docRoot}guide/developing/tools/ddms.html#logcat">logcat</a></strong></dt> |
| 42 | <dd>Dumps a log of system |
| Scott Main | 65e62f4 | 2010-09-20 12:46:34 -0700 | [diff] [blame] | 43 | messages. The messages include a stack trace when the device throws an error, |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 44 | as well as {@link android.util.Log} messages you've written from your application. To run |
| Scott Main | 65e62f4 | 2010-09-20 12:46:34 -0700 | [diff] [blame] | 45 | logcat, execute <code>adb logcat</code> from your Android SDK {@code tools/} directory or, |
| 46 | from DDMS, select <strong>Device > Run |
| 47 | logcat</strong>. When using the <a href="{@docRoot}sdk/eclipse-adt.html">ADT plugin for |
| 48 | Eclipse</a>, you can also view logcat messages by opening the Logcat view, available from |
| 49 | <strong>Window > Show View > Other > Android > Logcat</strong>. |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 50 | <p>{@link android.util.Log} is a logging |
| 51 | class you can use to print out messages to the logcat. You can read messages |
| 52 | in real time if you run logcat on DDMS (covered next). Common logging methods include: |
| 53 | {@link android.util.Log#v(String,String)} (verbose), {@link |
| 54 | android.util.Log#d(String,String)} (debug), {@link android.util.Log#i(String,String)} |
| 55 | (information), {@link android.util.Log#w(String,String)} (warning) and {@link |
| 56 | android.util.Log#e(String,String)} (error). For example:</p> |
| 57 | <pre class="no-pretty-print"> |
| 58 | Log.i("MyActivity", "MyClass.getView() — get item number " + position); |
| 59 | </pre> |
| 60 | <p>The logcat will then output something like:</p> |
| 61 | <pre class="no-pretty-print"> |
| 62 | I/MyActivity( 1557): MyClass.getView() — get item number 1 |
| 63 | </pre> |
| Scott Main | 7b02e92 | 2010-01-29 10:28:18 -0800 | [diff] [blame] | 64 | <p>Logcat is also the place to look when debugging a web page in the Android Browser app. See |
| 65 | <a href="#DebuggingWebPages">Debugging Web Pages</a> below.</p> |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 66 | </dl> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 68 | <p>For more information about all the development tools provided with the Android SDK, see the <a |
| 69 | href="{@docRoot}guide/developing/tools/index.html">Tools</a> document.</p> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 71 | <p>In addition to the above tools, you may also find the following useful for debugging: |
| 72 | <dl> |
| 73 | <dt><a href="{@docRoot}guide/developing/eclipse-adt.html"><strong>Eclipse ADT |
| 74 | plugin</strong></a></dt> |
| 75 | <dd>The ADT Plugin for Eclipse integrates a number of the Android development tools (ADB, DDMS, |
| 76 | logcat output, and other functionality), so that you won't work with them directly but will utilize |
| 77 | them through the Eclipse IDE.</dd> |
| 78 | <dt><strong>Developer Settings in the Dev Tools app</strong></dt> |
| 79 | <dd>The Dev Tools application included in the emulator system image exposes several settings |
| 80 | that provide useful information such as CPU usage and frame rate. See <a |
| 81 | href="#additionaldebugging">Debugging and Testing with Dev Tools</a> below.</dd> |
| 82 | </dl> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 84 | <h2 id="additionaldebugging">Debugging and Testing with Dev Tools</h2> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | |
| Scott Main | 2c9d7fe | 2010-01-20 14:11:56 -0800 | [diff] [blame] | 86 | <p>With the Dev Tools application, you can enable a number of settings on your device that will |
| 87 | make it easier to test and debug your applications.</p> |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 88 | |
| Scott Main | 2c9d7fe | 2010-01-20 14:11:56 -0800 | [diff] [blame] | 89 | <p>The Dev Tools application is installed by default |
| 90 | on all system images included with the SDK, so you can use it with the Android Emulator. If you'd |
| 91 | like to install the Dev Tools application on a real development device, you can copy the |
| 92 | application from your emulator and then install it on your device using ADB. To copy the |
| 93 | application from a running emulator, execute: |
| 94 | </p> |
| 95 | <pre> |
| 96 | adb -e pull /system/app/Development.apk ./Development.apk |
| 97 | </pre> |
| 98 | <p>This copies the .apk file into the current directory. Then install it on your connected device |
| 99 | with:</p> |
| 100 | <pre> |
| 101 | adb -d install Development.apk |
| 102 | </pre> |
| 103 | |
| 104 | <p>To get started, launch the Dev Tools application and |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 105 | select Development Settings. This will open the Development Settings page with the |
| 106 | following options (among others):</p> |
| 107 | |
| 108 | <dl> |
| 109 | <dt><strong>Debug app</strong></dt> |
| 110 | <dd>Lets you select the application to debug. You do not need to set this to attach a debugger, |
| 111 | but setting this value has two effects: |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | <ul> |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 113 | <li>It will prevent Android from throwing an error if you pause on |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | a breakpoint for a long time while debugging.</li> |
| 115 | <li>It will enable you to select the <em>Wait for Debugger</em> option |
| 116 | to pause application startup until your debugger attaches (described |
| 117 | next). </li> |
| 118 | </ul> |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 119 | </dd> |
| 120 | <dt><strong>Wait for debugger</strong></dt> |
| 121 | <dd>Blocks the selected application from loading until a debugger attaches. This |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | way you can set a breakpoint in onCreate(), which is important to debug |
| 123 | the startup process of an Activity. When you change this option, any |
| 124 | currently running instances of the selected application will be killed. |
| 125 | In order to check this box, you must have selected a debug application |
| 126 | as described in the previous option. You can do the same thing by adding |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 127 | {@link android.os.Debug#waitForDebugger()} to your code.</dd> |
| 128 | <dt><strong>Show screen updates</strong></dt> |
| 129 | <dd>Flashes a momentary pink rectangle on any screen sections that are being |
| 130 | redrawn. This is very useful for discovering unnecessary screen drawing.</dd> |
| 131 | <dt><strong>Immediately destroy activities</strong></dt> |
| 132 | <dd>Tells the |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | system to destroy an activity as soon as it is stopped (as if Android had to |
| 134 | reclaim memory). This is very useful for testing the {@link android.app.Activity#onSaveInstanceState} |
| 135 | / {@link android.app.Activity#onCreate(android.os.Bundle)} code path, which would |
| 136 | otherwise be difficult to force. Choosing this option will probably reveal |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 137 | a number of problems in your application due to not saving state.</dd> |
| 138 | <dt><strong>Show CPU usage</strong></dt> |
| 139 | <dd>Displays CPU meters at the |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | top of the screen, showing how much the CPU is being used. The top red bar |
| 141 | shows overall CPU usage, and the green bar underneath it shows the CPU time |
| 142 | spent in compositing the screen. <em>Note: You cannot turn this feature off |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 143 | once it is on, without restarting the emulator.</em> </dd> |
| 144 | <dt><strong>Show background</strong></dt> |
| 145 | <dd>Displays a background pattern |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | when no activity screens are visible. This typically does not happen, but |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 147 | can happen during debugging.</dd> |
| 148 | </dl> |
| 149 | |
| Scott Main | 2c9d7fe | 2010-01-20 14:11:56 -0800 | [diff] [blame] | 150 | <p>These settings will be remembered across emulator restarts.</p> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 152 | <h2 id="DebuggingWebPages">Debugging Web Pages</h2> |
| 153 | |
| Scott Main | 65e62f4 | 2010-09-20 12:46:34 -0700 | [diff] [blame] | 154 | <p>See the <a href="{@docRoot}guide/webapps/debugging.html">Debugging Web Apps</a> document.</p> |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 155 | |
| 156 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | <h2 id="toptips">Top Debugging Tips</h2> |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 158 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | <dl> |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 160 | <dt><strong>Dump the stack trace</strong></dt> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | <dd>To obtain a stack dump from emulator, you can log |
| 162 | in with <code>adb shell</code>, use "ps" to find the process you |
| 163 | want, and then "kill -3 ". The stack trace appears in the log file. |
| 164 | </dd> |
| 165 | |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 166 | <dt><strong>Display useful info on the emulator screen</strong></dt> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | <dd>The device can display useful information such as CPU usage or highlights |
| 168 | around redrawn areas. Turn these features on and off in the developer settings |
| 169 | window as described in <a href="#additionaldebugging">Setting debug and test |
| 170 | configurations on the emulator</a>. |
| 171 | </dd> |
| 172 | |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 173 | <dt><strong>Get system state information from the emulator (dumpstate)</strong></dt> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | <dd>You can access dumpstate information from the Dalvik Debug Monitor Service |
| 175 | tool. See <a href="{@docRoot}guide/developing/tools/adb.html#dumpsys">dumpsys and |
| 176 | dumpstate</a> on the adb topic page.</dd> |
| 177 | |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 178 | <dt><strong>Get application state information from the emulator (dumpsys)</strong></dt> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | <dd>You can access dumpsys information from the Dalvik Debug Monitor Service |
| 180 | tool. See <a href="{@docRoot}guide/developing/tools/adb.html#dumpsys">dumpsys and |
| 181 | dumpstate</a> on the adb topic page.</dd> |
| 182 | |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 183 | <dt><strong>Get wireless connectivity information</strong></dt> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | <dd>You can get information about wireless connectivity using the Dalvik Debug |
| 185 | Monitor Service tool. From the <strong>Device</strong> menu, select "Dump |
| 186 | radio state".</dd> |
| 187 | |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 188 | <dt><strong>Log trace data</strong></dt> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | <dd>You can log method calls and other tracing data in an activity by calling |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 190 | {@link android.os.Debug#startMethodTracing(String) startMethodTracing()}. See <a |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | href="{@docRoot}guide/developing/tools/traceview.html">Running the Traceview Debugging |
| 192 | Program</a> for details. </dd> |
| 193 | |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 194 | <dt><strong>Log radio data</strong></dt> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | <dd>By default, radio information is not logged to the system (it is a lot of |
| 196 | data). However, you can enable radio logging using the following commands: |
| 197 | |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 198 | <pre class="no-pretty-print"> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | adb shell |
| 200 | logcat -b radio |
| 201 | </pre> |
| 202 | </dd> |
| 203 | |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 204 | <dt><strong>Capture screenshots</strong></dt> |
| 205 | <dd>The Dalvik Debug Monitor Server (DDMS) can capture screenshots from the emulator. Select |
| 206 | <strong>Device > Screen capture</strong>.</dd> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 208 | <dt><strong>Use debugging helper classes</strong></dt> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | <dd>Android provides debug helper classes such as {@link android.util.Log |
| 210 | util.Log} and {@link android.os.Debug} for your convenience. </dd> |
| 211 | </dl> |
| 212 | |
| Scott Main | 6261d87 | 2010-01-15 17:26:29 -0800 | [diff] [blame] | 213 | <p>Also see the <a href="{@docRoot}resources/faq/troubleshooting.html">Troubleshooting</a> document |
| 214 | for answers to some common developing and debugging issues.</p> |
| 215 | |
| 216 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | <h2 id="ide-debug-port">Configuring Your IDE to Attach to the Debugging Port</h2> |
| 218 | |
| 219 | <p>DDMS will assign a specific debugging port to every virtual machine that it |
| 220 | finds on the emulator. You must either attach your IDE to that |
| 221 | port (listed on the Info tab for that VM), or you can use a default port 8700 |
| 222 | to connect to whatever application is currently selected on the list of discovered |
| 223 | virtual machines.</p> |
| 224 | <p>Your IDE should attach to your application running on the emulator, showing you |
| 225 | its threads and allowing you to suspend them, inspect their state, and set breakpoints. |
| 226 | If you selected "Wait for debugger" in the Development settings panel |
| 227 | the application will run when Eclipse connects, so you will need to set any breakpoints |
| 228 | you want before connecting.</p> |
| 229 | <p>Changing either the application being debugged or the "Wait for debugger" |
| 230 | option causes the system to kill the selected application if it is currently |
| 231 | running. You can use this to kill your application if it is in a bad state |
| 232 | by simply going to the settings and toggling the checkbox.</p> |