blob: 975f6998a75f0f77ff589d1ce59aaa84c83be39f [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001page.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 Main6261d872010-01-15 17:26:29 -08009 <li><a href="#additionaldebugging">Debug with Dev Tools</a></li>
10 <li><a href="#DebuggingWebPages">Debugging Web Pages</a></li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011 <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 Main6261d872010-01-15 17:26:29 -080021
22<p>The Android SDK includes a set of tools to help you debug and profile
23your 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 Project9066cfe2009-03-03 19:31:44 -080034 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 Main6261d872010-01-15 17:26:29 -080036 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
43 messages. The messages include a stack trace when the emulator throws an error,
44 as well as {@link android.util.Log} messages you've written from your application. To run
45 logcat, execute <code>adb logcat</code> or, from DDMS, select <strong>Device > Run
46 logcat</strong>.
47 <p>{@link android.util.Log} is a logging
48 class you can use to print out messages to the logcat. You can read messages
49 in real time if you run logcat on DDMS (covered next). Common logging methods include:
50 {@link android.util.Log#v(String,String)} (verbose), {@link
51 android.util.Log#d(String,String)} (debug), {@link android.util.Log#i(String,String)}
52 (information), {@link android.util.Log#w(String,String)} (warning) and {@link
53 android.util.Log#e(String,String)} (error). For example:</p>
54<pre class="no-pretty-print">
55Log.i("MyActivity", "MyClass.getView() &mdash; get item number " + position);
56</pre>
57 <p>The logcat will then output something like:</p>
58<pre class="no-pretty-print">
59I/MyActivity( 1557): MyClass.getView() &mdash; get item number 1
60</pre>
61 <p>Logcat is also the place to look when debugging a web page in the Android browser. All
62browser bugs will be output to logcat with the {@code WebCore} tag.
63</dl>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064
Scott Main6261d872010-01-15 17:26:29 -080065<p>For more information about all the development tools provided with the Android SDK, see the <a
66href="{@docRoot}guide/developing/tools/index.html">Tools</a> document.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067
Scott Main6261d872010-01-15 17:26:29 -080068<p>In addition to the above tools, you may also find the following useful for debugging:
69<dl>
70 <dt><a href="{@docRoot}guide/developing/eclipse-adt.html"><strong>Eclipse ADT
71plugin</strong></a></dt>
72 <dd>The ADT Plugin for Eclipse integrates a number of the Android development tools (ADB, DDMS,
73logcat output, and other functionality), so that you won't work with them directly but will utilize
74them through the Eclipse IDE.</dd>
75 <dt><strong>Developer Settings in the Dev Tools app</strong></dt>
76 <dd>The Dev Tools application included in the emulator system image exposes several settings
77 that provide useful information such as CPU usage and frame rate. See <a
78href="#additionaldebugging">Debugging and Testing with Dev Tools</a> below.</dd>
79</dl>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080
Scott Main6261d872010-01-15 17:26:29 -080081<h2 id="additionaldebugging">Debugging and Testing with Dev Tools</h2>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
Scott Main6261d872010-01-15 17:26:29 -080083<p>With the Dev Tools application, you can turn on a number of settings that will
84make it easier to test and debug your applications. The Dev Tools application is automatically
85installed on all system images included with the SDK. The source code for the Dev Tools application
86is also provided in the SDK samples so that you may build it and then install the application on any
87development device.</p>
88
89<p>To get to the development settings page on the emulator, launch the Dev Tools application and
90select Development Settings. This will open the Development Settings page with the
91following options (among others):</p>
92
93<dl>
94 <dt><strong>Debug app</strong></dt>
95 <dd>Lets you select the application to debug. You do not need to set this to attach a debugger,
96 but setting this value has two effects:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 <ul>
Scott Main6261d872010-01-15 17:26:29 -080098 <li>It will prevent Android from throwing an error if you pause on
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 a breakpoint for a long time while debugging.</li>
100 <li>It will enable you to select the <em>Wait for Debugger</em> option
101 to pause application startup until your debugger attaches (described
102 next). </li>
103 </ul>
Scott Main6261d872010-01-15 17:26:29 -0800104 </dd>
105 <dt><strong>Wait for debugger</strong></dt>
106 <dd>Blocks the selected application from loading until a debugger attaches. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 way you can set a breakpoint in onCreate(), which is important to debug
108 the startup process of an Activity. When you change this option, any
109 currently running instances of the selected application will be killed.
110 In order to check this box, you must have selected a debug application
111 as described in the previous option. You can do the same thing by adding
Scott Main6261d872010-01-15 17:26:29 -0800112 {@link android.os.Debug#waitForDebugger()} to your code.</dd>
113 <dt><strong>Show screen updates</strong></dt>
114 <dd>Flashes a momentary pink rectangle on any screen sections that are being
115 redrawn. This is very useful for discovering unnecessary screen drawing.</dd>
116 <dt><strong>Immediately destroy activities</strong></dt>
117 <dd>Tells the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 system to destroy an activity as soon as it is stopped (as if Android had to
119 reclaim memory).&nbsp; This is very useful for testing the {@link android.app.Activity#onSaveInstanceState}
120 / {@link android.app.Activity#onCreate(android.os.Bundle)} code path, which would
121 otherwise be difficult to force. Choosing this option will probably reveal
Scott Main6261d872010-01-15 17:26:29 -0800122 a number of problems in your application due to not saving state.</dd>
123 <dt><strong>Show CPU usage</strong></dt>
124 <dd>Displays CPU meters at the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 top of the screen, showing how much the CPU is being used. The top red bar
126 shows overall CPU usage, and the green bar underneath it shows the CPU time
127 spent in compositing the screen. <em>Note: You cannot turn this feature off
Scott Main6261d872010-01-15 17:26:29 -0800128 once it is on, without restarting the emulator.</em> </dd>
129 <dt><strong>Show background</strong></dt>
130 <dd>Displays a background pattern
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 when no activity screens are visible. This typically does not happen, but
Scott Main6261d872010-01-15 17:26:29 -0800132 can happen during debugging.</dd>
133</dl>
134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135<p>These settings will be remembered across emulator restarts. </p>
136
Scott Main6261d872010-01-15 17:26:29 -0800137<h2 id="DebuggingWebPages">Debugging Web Pages</h2>
138
139<p>If you're developing a web application for Android devices, you can debug your JavaScript on
140Android using the Console APIs, which will output messages to logcat. If you're familiar
141debugging web pages with Firefox's FireBug or WebKit's Web Inspector, then you're probably familiar
142with the Console APIs. The Android Browser (and {@link android.webkit.WebChromeClient}) supports
143most of the same APIs.</p>
144
145<p>When you call a function from the Console APIs (in the DOM's {@code window.console} object),
146you will see the output in logcat as a warning. For example, if your web page
147executes the following JavaScript:</p>
148<pre class="no-pretty-print">
149console.log("Hello World");
150</pre>
151<p>Then the logcat output from the Android Browser will look like this:</p>
152<pre class="no-pretty-print">
153W/browser ( 202): Console: Hello World :0
154</pre>
155
156<p class="note"><strong>Note:</strong> All Console messages from the Android
157Browser are tagged with the name "browser" on Android platforms running API Level 7 or higher and
158tagged with the name "WebCore" for platforms running API Level 6 or lower.</p>
159
160<p>Not all of the Console APIs available in Firefox or other WebKit browsers are implemented
161on Android. Mostly, you need to depend on basic text logging provided by
162functions like {@code console.log(String)}, {@code console.info(String)}, {@code
163console.warn(String)}, and {@code console.error(String)}. Although other Console functions may not
164be implemented, they will not raise run-time errors, but will simply not behave as you might
165expect.</p>
166
167<p>If you've implemented a custom {@link android.webkit.WebView} in your application, then in order
168to receive messages that are sent through the Console APIs, you must provide a {@link
169android.webkit.WebChromeClient} that implements the {@link
170android.webkit.WebChromeClient#onConsoleMessage(String,int,String) onConsoleMessage()} callback
171method. For example, assuming that the {@code myWebView} field references the {@link
172android.webkit.WebView} in your application, you can log debug messages like this:</p>
173<pre>
174myWebView.setWebChromeClient(new WebChromeClient() {
175 public void onConsoleMessage(String message, int lineNumber, String sourceID) {
176 Log.d("MyApplication", message);
177 }
178});
179</pre>
180<p>The {@link android.webkit.WebChromeClient#onConsoleMessage(String,int,String)
181onConsoleMessage()} method will be called each time one of the Console methods is called from
182within your {@link android.webkit.WebView}.</p>
183<p>When the "Hello World" log is executed through your {@link android.webkit.WebView}, it will
184now look like this:</p>
185<pre class="no-pretty-print">
186D/MyApplication ( 430): Hello World
187</pre>
188
189<p class="note"><strong>Note:</strong> The {@link
190android.webkit.WebChromeClient#onConsoleMessage(String,int,String) onConsoleMessage()} callback
191method was added with API Level 7. If you are targetting platforms running API Level 6 or lower,
192then your Console messages will automatically be sent to logcat with the "WebCore" logging tag.</p>
193
194
195
196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197<h2 id="toptips">Top Debugging Tips</h2>
Scott Main6261d872010-01-15 17:26:29 -0800198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199<dl>
Scott Main6261d872010-01-15 17:26:29 -0800200<dt><strong>Dump the stack trace</strong></dt>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201<dd>To obtain a stack dump from emulator, you can log
202in with <code>adb shell</code>, use &quot;ps&quot; to find the process you
203want, and then &quot;kill -3 &quot;. The stack trace appears in the log file.
204</dd>
205
Scott Main6261d872010-01-15 17:26:29 -0800206<dt><strong>Display useful info on the emulator screen</strong></dt>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207<dd>The device can display useful information such as CPU usage or highlights
208around redrawn areas. Turn these features on and off in the developer settings
209window as described in <a href="#additionaldebugging">Setting debug and test
210configurations on the emulator</a>.
211</dd>
212
Scott Main6261d872010-01-15 17:26:29 -0800213<dt><strong>Get system state information from the emulator (dumpstate)</strong></dt>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214<dd>You can access dumpstate information from the Dalvik Debug Monitor Service
215tool. See <a href="{@docRoot}guide/developing/tools/adb.html#dumpsys">dumpsys and
216dumpstate</a> on the adb topic page.</dd>
217
Scott Main6261d872010-01-15 17:26:29 -0800218<dt><strong>Get application state information from the emulator (dumpsys)</strong></dt>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219<dd>You can access dumpsys information from the Dalvik Debug Monitor Service
220tool. See <a href="{@docRoot}guide/developing/tools/adb.html#dumpsys">dumpsys and
221dumpstate</a> on the adb topic page.</dd>
222
Scott Main6261d872010-01-15 17:26:29 -0800223<dt><strong>Get wireless connectivity information</strong></dt>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224<dd>You can get information about wireless connectivity using the Dalvik Debug
225Monitor Service tool. From the <strong>Device</strong> menu, select &quot;Dump
226radio state&quot;.</dd>
227
Scott Main6261d872010-01-15 17:26:29 -0800228<dt><strong>Log trace data</strong></dt>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229<dd>You can log method calls and other tracing data in an activity by calling
Scott Main6261d872010-01-15 17:26:29 -0800230{@link android.os.Debug#startMethodTracing(String) startMethodTracing()}. See <a
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231href="{@docRoot}guide/developing/tools/traceview.html">Running the Traceview Debugging
232Program</a> for details. </dd>
233
Scott Main6261d872010-01-15 17:26:29 -0800234<dt><strong>Log radio data</strong></dt>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235<dd>By default, radio information is not logged to the system (it is a lot of
236data). However, you can enable radio logging using the following commands:
237
Scott Main6261d872010-01-15 17:26:29 -0800238<pre class="no-pretty-print">
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239adb shell
240logcat -b radio
241</pre>
242</dd>
243
Scott Main6261d872010-01-15 17:26:29 -0800244<dt><strong>Capture screenshots</strong></dt>
245<dd>The Dalvik Debug Monitor Server (DDMS) can capture screenshots from the emulator. Select
246<strong>Device > Screen capture</strong>.</dd>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247
Scott Main6261d872010-01-15 17:26:29 -0800248<dt><strong>Use debugging helper classes</strong></dt>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249<dd>Android provides debug helper classes such as {@link android.util.Log
250 util.Log} and {@link android.os.Debug} for your convenience. </dd>
251</dl>
252
Scott Main6261d872010-01-15 17:26:29 -0800253<p>Also see the <a href="{@docRoot}resources/faq/troubleshooting.html">Troubleshooting</a> document
254for answers to some common developing and debugging issues.</p>
255
256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257<h2 id="ide-debug-port">Configuring Your IDE to Attach to the Debugging Port</h2>
258
259<p>DDMS will assign a specific debugging port to every virtual machine that it
260 finds on the emulator. You must either attach your IDE to that
261 port (listed on the Info tab for that VM), or you can use a default port 8700
262 to connect to whatever application is currently selected on the list of discovered
263 virtual machines.</p>
264<p>Your IDE should attach to your application running on the emulator, showing you
265 its threads and allowing you to suspend them, inspect their state, and set breakpoints.
266 If you selected &quot;Wait for debugger&quot; in the Development settings panel
267 the application will run when Eclipse connects, so you will need to set any breakpoints
268 you want before connecting.</p>
269<p>Changing either the application being debugged or the &quot;Wait for debugger&quot;
270 option causes the system to kill the selected application if it is currently
271 running. You can use this to kill your application if it is in a bad state
272 by simply going to the settings and toggling the checkbox.</p>