blob: 6b7c27a66a94941c378bb0fe06f294a62475e0f4 [file] [log] [blame]
The Android Open Source Project9266c5582009-01-15 16:12:10 -08001page.title=Debugging Tasks
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07002@jd:body
3
The Android Open Source Project9266c5582009-01-15 16:12:10 -08004<div id="qv-wrapper">
5<div id="qv">
6 <h2>In this document</h2>
7 <ol>
8 <li><a href="#tools">Tools</a></li>
9 <li><a href="#additionaldebugging">Debug and Test Settings</a></li>
10 <li><a href="#toptips">Top Debugging Tips</a></li>
11 <li><a href="#ide-debug-port">Configuring Your IDE to Attach to the Debugging Port</a></li>
12 </ol>
13</div>
14</div>
15
16<p>This document offers some helpful guidance to debugging applications on Android.
17
18
19<h2 id="tools">Tools</h2>
20<p>The Android SDK includes a fairly extensive set of tools to help you debug your programs: </p>
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070021<ul>
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080022 <li><a href="{@docRoot}guide/developing/tools/ddms.html"><strong>DDMS</strong></a> - A graphical program that
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070023 supports port forwarding (so you can set up breakpoints in your code in your
24 IDE), screen captures on the emulator, thread and stack information,
25 and many other features. You can also run logcat to retrieve your Log messages.
26 See the linked topic for more information. </li>
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080027 <li><strong><a href="{@docRoot}guide/developing/tools/ddms.html#logcat">logcat</a></strong> - Dumps a log of system
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070028 messages. The messages include a stack trace when the emulator throws an error,
29 as well as Log messages. To run logcat, see the linked topic.
30
31 <pre>...
32I/MemoryDealer( 763): MemoryDealer (this=0x54bda0): Creating 2621440 bytes heap at 0x438db000
33<span style="background-color:#CCCCCC; border-bottom:medium">I/Logger( 1858): getView() requesting item number 0
34I/Logger( 1858): getView() requesting item number 1
35I/Logger( 1858): getView() requesting item number 2</span>
36D/ActivityManager( 763): Stopping: HistoryRecord{409dbb20 com.android.home.AllApps}
37...</pre>
38
39 </li>
40 <li><p><strong>{@link android.util.Log Android Log}</strong>- A logging
41 class to print out messages to a log file on the emulator. You can read messages
42 in real time if you run logcat on DDMS (covered next). Add a few logging
43 method calls to your code.</p>
44 <p>To use the <code>Log</code> class, you just call <code>Log.v()</code>
45 (verbose), <code>Log.d()</code> (debug), <code>Log.i()</code> (information),
46 <code>Log.w()</code> (warning) or <code>Log.e</code> (error) depending
47 on the importance you wish to assign the log message.</p>
48 <code>Log.i(&quot;MyActivity&quot;, &quot;MyClass.getView()
49 &mdash; Requesting item number &quot; + position)</code>
50 <p>You can use logcat to read these messages</p></li>
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080051 <li><strong><a href="{@docRoot}guide/developing/tools/traceview.html">Traceview</a> </strong>- Android can save
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070052 a log of method calls and times to a logging file that you can view in a
53 graphical reader called Traceview. See the linked topic for more information. </li>
54</ul>
55<ul>
The Android Open Source Projectd24b8182009-02-10 15:44:00 -080056 <li><a href="{@docRoot}guide/developing/eclipse-adt.html"><strong>Eclipse plugin</strong></a> - The ADT Plugin
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080057 for Eclipse integrates a number of these tools (ADB, DDMS, logcat output,
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070058 and other functionality). See the linked topic for more information. </li>
59 <li><strong>Debug and Test Device Settings</strong> - Android exposes several settings
60 that expose useful information such as CPU usage and frame rate. See <a href="#additionaldebugging">Debug
61 and Test Settings on the Emulator</a> below. </li>
62</ul>
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080063<p>Also, see the <a href="{@docRoot}guide/appendix/faq/troubleshooting.html">Troubleshooting</a> section
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070064 of the doc to figure out why your application isn't appearing on the emulator,
65 or why it's not starting. </p>
66
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070067
The Android Open Source Project9266c5582009-01-15 16:12:10 -080068<h2 id="additionaldebugging">Debug and Test Settings</h2>
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070069
The Android Open Source Project9266c5582009-01-15 16:12:10 -080070<p>With the <strong>Dev Tools</strong> application, you can turn on a number of settings that will make it easier to test
71 and debug your applications. To get to the development settings page on the emulator, launch the
72 <strong>Dev Tools</strong> application and open <strong>Development Settings</strong>.
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070073 This will open the development settings page with the following options (among
74 others):</p>
75<ul>
76 <li><strong>Debug app</strong>&nbsp;&nbsp;&nbsp;Selects the application that
77 will be debugged. You do not need to set this to attach a debugger, but setting
78 this value has two effects:
79 <ul>
80 <li>It will prevent Android from throwing an error if you pause on
81 a breakpoint for a long time while debugging.</li>
82 <li>It will enable you to select the <em>Wait for Debugger</em> option
83 to pause application startup until your debugger attaches (described
84 next). </li>
85 </ul>
86 </li>
87 <li><strong>Wait for debugger </strong> &nbsp;&nbsp;
88 Blocks the selected application from loading until a debugger attaches. This
89 way you can set a breakpoint in onCreate(), which is important to debug
90 the startup process of an Activity. When you change this option, any
91 currently running instances of the selected application will be killed.
92 In order to check this box, you must have selected a debug application
93 as described in the previous option. You can do the same thing by adding
94 {@link android.os.Debug#waitForDebugger()} to your code. </li>
95 <li><strong>Immediately destroy activities</strong>&nbsp;&nbsp;&nbsp;Tells the
96 system to destroy an activity as soon as it is stopped (as if Android had to
97 reclaim memory).&nbsp; This is very useful for testing the {@link android.app.Activity#onSaveInstanceState}
98 / {@link android.app.Activity#onCreate(android.os.Bundle)} code path, which would
99 otherwise be difficult to force. Choosing this option will probably reveal
100 a number of problems in your application due to not saving state.</li>
101 <li><strong>Show screen updates</strong>&nbsp;&nbsp;&nbsp;
102 Flashes a momentary pink rectangle on any screen sections that are being
103 redrawn. This is very useful for discovering unnecessary screen drawing. </li>
104 <li><strong>Show CPU usage</strong>&nbsp;&nbsp;&nbsp;Displays CPU meters at the
105 top of the screen, showing how much the CPU is being used. The top red bar
106 shows overall CPU usage, and the green bar underneath it shows the CPU time
107 spent in compositing the screen. <em>Note: You cannot turn this feature off
108 once it is on, without restarting the emulator.</em> </li>
109 <li><strong>Show background</strong>&nbsp;&nbsp;&nbsp;Displays a background pattern
110 when no activity screens are visible. This typically does not happen, but
111 can happen during debugging. </li>
112</ul>
113<p>These settings will be remembered across emulator restarts. </p>
114
The Android Open Source Project9266c5582009-01-15 16:12:10 -0800115<h2 id="toptips">Top Debugging Tips</h2>
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700116<!--
117<ul>
118 <li><a href="#stackdump">Quick stack dump</a></li>
119 <li><a href="#displayinfo">Displaying useful info on the emulator screen </a></li>
120 <li><a href="#dumpstate">Getting system state information from the emulator (dumpstate)</a></li>
121 <li><a href="#dumpsys">Getting application state information from the emulator (dumpsys)</a></li>
122 <li><a href="#radioinfo">Getting wireless connectivity information</a></li>
123 <li><a href="#loggingdata">Logging Trace Data</a></li>
124 <li><a href="#logradio">Logging Radio Data </a></li>
125 <li><a href="#adb">Running adb</a></li>
126 <li><a href="#screencaps">Getting screen captures from the emulator</a></li>
127 <li><a href="#debughelpers">Using debug helper classes</a></li>
128</ul>
129-->
130<dl>
131<dt>Quick stack dump <a name="stackdump" id="stackdump"></a></dt>
132<dd>To obtain a stack dump from emulator, you can log
133in with <code>adb shell</code>, use &quot;ps&quot; to find the process you
134want, and then &quot;kill -3 &quot;. The stack trace appears in the log file.
135</dd>
136
137<dt>Displaying useful info on the emulator screen<a name="displayinfo" id="displayinfo"></a></dt>
138<dd>The device can display useful information such as CPU usage or highlights
139around redrawn areas. Turn these features on and off in the developer settings
140window as described in <a href="#additionaldebugging">Setting debug and test
141configurations on the emulator</a>.
142</dd>
143
144<dt>Getting system state information from the emulator (dumpstate)<a name="dumpstate" id="dumpstate"></a> </dt>
145<dd>You can access dumpstate information from the Dalvik Debug Monitor Service
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800146tool. See <a href="{@docRoot}guide/developing/tools/adb.html#dumpsys">dumpsys and
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700147dumpstate</a> on the adb topic page.</dd>
148
149<dt>Getting application state information from the emulator (dumpsys)<a name="dumpsys" id="dumpsys"></a></dt>
150<dd>You can access dumpsys information from the Dalvik Debug Monitor Service
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800151tool. See <a href="{@docRoot}guide/developing/tools/adb.html#dumpsys">dumpsys and
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700152dumpstate</a> on the adb topic page.</dd>
153
154<dt>Getting wireless connectivity information <a name="radioinfo" id="radioinfo"></a></dt>
155<dd>You can get information about wireless connectivity using the Dalvik Debug
156Monitor Service tool. From the <strong>Device</strong> menu, select &quot;Dump
157radio state&quot;.</dd>
158
159<dt>Logging Trace Data<a name="loggingdata" id="loggingdata"></a></dt>
160<dd>You can log method calls and other tracing data in an activity by calling
161android.os.Debug.startMethodTracing(). See <a
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800162href="{@docRoot}guide/developing/tools/traceview.html">Running the Traceview Debugging
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700163Program</a> for details. </dd>
164
165<dt>Logging Radio Data<a name="logradio" id="logradio"></a></dt>
166<dd>By default, radio information is not logged to the system (it is a lot of
167data). However, you can enable radio logging using the following commands:
168
169<pre>
170adb shell
171logcat -b radio
172</pre>
173</dd>
174
175<dt>Running adb<a name="adb" id="adb"></a></dt>
176<dd>Android ships with a tool called adb that provides various capabilities, including
177moving and syncing files to the emulator, forwarding ports, and running a UNIX
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800178shell on the emulator. See <a href="{@docRoot}guide/developing/tools/adb.html">Using adb</a> for details.</dd>
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700179
180<dt>Getting screen captures from the emulator<a name="screencaps" id="screencaps"></a></dt>
181<dd> Dalvik Debug Monitor Server (DDMS) can capture screenshots from the emulator.</dd>
182
183
184<a name="debughelpers"></a>
185
186<dt>Using debugging helper classes</dt>
187
188<dd>Android provides debug helper classes such as {@link android.util.Log
189 util.Log} and {@link android.os.Debug} for your convenience. </dd>
190</dl>
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800191
The Android Open Source Project9266c5582009-01-15 16:12:10 -0800192<h2 id="ide-debug-port">Configuring Your IDE to Attach to the Debugging Port</h2>
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800193
194<p>DDMS will assign a specific debugging port to every virtual machine that it
195 finds on the emulator. You must either attach your IDE to that
196 port (listed on the Info tab for that VM), or you can use a default port 8700
197 to connect to whatever application is currently selected on the list of discovered
198 virtual machines.</p>
199<p>Your IDE should attach to your application running on the emulator, showing you
200 its threads and allowing you to suspend them, inspect their state, and set breakpoints.
201 If you selected &quot;Wait for debugger&quot; in the Development settings panel
202 the application will run when Eclipse connects, so you will need to set any breakpoints
203 you want before connecting.</p>
204<p>Changing either the application being debugged or the &quot;Wait for debugger&quot;
205 option causes the system to kill the selected application if it is currently
206 running. You can use this to kill your application if it is in a bad state
207 by simply going to the settings and toggling the checkbox.</p>