| Robert Ly | 044d7ff | 2010-12-29 16:24:24 -0800 | [diff] [blame^] | 1 | page.title=Using DDMS |
| Robert Ly | ce4d229 | 2010-12-16 17:26:11 -0800 | [diff] [blame] | 2 | @jd:body |
| 3 | |
| 4 | <div id="qv-wrapper"> |
| 5 | <div id="qv"> |
| 6 | <h2>In this document</h2> |
| 7 | |
| 8 | <ol> |
| 9 | <li><a href="#running">Running DDMS</a></li> |
| 10 | <li><a href="#how-ddms-works">How DDMS Interacts with a Debugger</a></li> |
| 11 | |
| 12 | <li><a href="#using-ddms">Using DDMS</a></li> |
| 13 | </ol> |
| 14 | </div> |
| 15 | </div> |
| 16 | |
| 17 | <p>Android ships with a debugging tool called the Dalvik Debug Monitor Server (DDMS), which |
| 18 | provides port-forwarding services, screen capture on the device, thread and heap information on |
| 19 | the device, logcat, process, and radio state information, incoming call and SMS spoofing, |
| 20 | location data spoofing, and more. This page provides a modest discussion of DDMS features; it is |
| 21 | not an exhaustive exploration of all the features and capabilities.</p> |
| 22 | |
| 23 | <h2 id="running">Running DDMS</h2> |
| 24 | <p>DDMS is integrated into Eclipse and is also shipped in the <code>tools/</code> directory of the |
| 25 | SDK. DDMS works with both the emulator and a connected device. If both are connected and running simultaneously, |
| 26 | DDMS defaults to the emulator.</p> |
| 27 | |
| 28 | <ul> |
| 29 | <li>From Eclipse: Click <strong>Window > Open Perspective > Other... > DDMS</strong>.</li> |
| 30 | <li>From the command line: Type <code>ddms</code> (or <code>./ddms</code> on Mac/Linux) from the <code>tools/</code> |
| 31 | directory. </li> |
| 32 | </ul> |
| 33 | |
| 34 | |
| 35 | <h2 id="how-ddms-works">How DDMS Interacts with a Debugger</h2> |
| 36 | |
| 37 | <p>On Android, every application runs in its own process, each of which runs in its own virtual machine |
| 38 | (VM). Each VM exposes a unique port that a debugger can attach to.</p> |
| 39 | |
| 40 | <p>When DDMS starts, it connects to <a href="{@docRoot}guide/developing/tools/adb.html">adb</a>. |
| 41 | When a device is connected, a VM monitoring service is created between |
| 42 | <code>adb</code> and DDMS, which notifies DDMS when a VM on the device is started or terminated. Once a VM |
| 43 | is running, DDMS retrieves the the VM's process ID (pid), via <code>adb</code>, and opens a connection to the |
| 44 | VM's debugger, through the adb daemon (adbd) on the device. DDMS can now talk to the VM using a |
| 45 | custom wire protocol.</p> |
| 46 | |
| 47 | <p>DDMS assigns a debugging port to each VM on the device. Typically, |
| 48 | DDMS assigns port 8600 for the first debuggable VM, the next on 8601, and so on. When a debugger |
| 49 | connects to one of these ports, all traffic is forwarded to the debugger from the associated |
| 50 | VM. You can only attach a single debugger to a single port, but DDMS can handle multiple, attached |
| 51 | debuggers.</p> |
| 52 | |
| 53 | <p>By default, DDMS also listens on another debugging port, the DDMS "base port" (8700, by default). |
| 54 | The base port is a port forwarder, which can accept VM traffic from any debugging port and forward |
| 55 | it to the debugger on port 8700. This allows you to attach one debugger to port 8700, and debug |
| 56 | all the VMs on a device. The traffic that is forwarded is determined by the currently selected process |
| 57 | in the DDMS Devices view.</p> |
| 58 | |
| 59 | <p>The following screenshot shows a typical DDMS screen in Eclipse. If you are starting DDMS from |
| 60 | the command line, the screen is slightly different, but much of the functionality is identical. |
| 61 | Notice that the highlighted process, <code>com.example.android.notepad</code>, that is running in the emulator |
| 62 | has the debugging port 8700 assigned to it as well as 8609. This signifies that DDMS is currently |
| 63 | forwarding port 8609 to the static debugging port of 8700.</p> |
| 64 | |
| 65 | <img src="{@docRoot}images/debug-ddms.png" |
| 66 | width="1024" /> |
| 67 | <p class="img-caption"><strong>Figure 1.</strong> |
| 68 | Screenshot of DDMS</p> |
| 69 | |
| 70 | <p>If you are not using Eclipse and ADT, read <a href= |
| 71 | "{@docRoot}guide/developing/debugging/debugging-projects-cmdline.html#debuggingPort">Configuring |
| 72 | your IDE to attach to the debugging port</a>, for more information on attaching your |
| 73 | debugger.</p> |
| 74 | |
| 75 | <p class="note"><strong>Tip:</strong> You can set a number of DDMS preferences in |
| 76 | <strong>File</strong> > <strong>Preferences</strong>. Preferences are saved to |
| 77 | <code>$HOME/.ddmsrc</code>.</p> |
| 78 | |
| 79 | <p class="warning"><strong>Known debugging issues with Dalvik</strong><br /> |
| 80 | Debugging an application in the Dalvik VM should work the same as it does in other VMs. However, |
| 81 | when single-stepping out of synchronized code, the "current line" cursor may jump to the last |
| 82 | line in the method for one step.</p> |
| 83 | |
| 84 | <h2 id="using-ddms">Using DDMS</h2> |
| 85 | The following sections describe how to use DDMS and the various tabs and panes that are part of the |
| 86 | DDMS GUI. The Eclipse version and the command line version have minor UI differences, but the |
| 87 | same functionality. For information on running DDMS, see the previous section in this document, |
| 88 | <a href="#running">Running DDMS</a>. |
| 89 | |
| 90 | |
| 91 | <h3>Viewing heap usage for a process</h3> |
| 92 | |
| 93 | <p>DDMS allows you to view how much heap memory a process is using. This information is useful in |
| 94 | tracking heap usage at a certain point of time during the execution of your application.</p> |
| 95 | <p>To view heap usage for a process:</p> |
| 96 | <ol> |
| 97 | <li>In the Devices tab, select the process that you want to see the heap information for.</li> |
| 98 | |
| 99 | <li>Click the <strong>Update Heap</strong> button to enable heap information for the |
| 100 | process.</li> |
| 101 | |
| 102 | <li>In the Heap tab, click <strong>Cause GC</strong> to invoke garbage collection, which |
| 103 | enables the collection of heap data. When the operation completes, you will see a group of |
| 104 | object types and the memory that has been allocated for each type. You can click <strong>Cause |
| 105 | GC</strong> again to refresh the data.</li> |
| 106 | |
| 107 | <li>Click on an object type in the list to see a bar graph that shows the number of objects |
| 108 | allocated for a particular memory size in bytes.</li> |
| 109 | </ol> |
| 110 | |
| 111 | <h3>Tracking memory allocation of objects</h3> |
| 112 | |
| 113 | <p>DDMS provides a feature to track objects that are being allocated to memory and to see which |
| 114 | classes and threads are allocating the objects. This allows you to track, in real time, where |
| 115 | objects are being allocated when you perform certain actions in your application. This |
| 116 | information is valuable for assessing memory usage that can affect application performance. |
| 117 | If you want more granular control over where allocation data is collected, use the |
| 118 | {@link android.os.Debug#startAllocCounting()} and {@link android.os.Debug#stopAllocCounting()} |
| 119 | methods.</p> |
| 120 | |
| 121 | <p>To track memory allocation of objects:</p> |
| 122 | <ol> |
| 123 | <li>In the Devices tab, select the process that you want to enable allocation tracking |
| 124 | for.</li> |
| 125 | |
| 126 | <li>In the Allocation Tracker tab, click the <strong>Start Tracking</strong> button to begin |
| 127 | allocation tracking. At this point, anything you do in your application will be tracked.</li> |
| 128 | |
| 129 | <li>Click <strong>Get Allocations</strong> to see a list of objects that have been allocated |
| 130 | since you clicked on the <strong>Start Tracking</strong> button. You can click on <strong>Get |
| 131 | Allocations</strong> again to append to the list new objects that that have been |
| 132 | allocated.</li> |
| 133 | |
| 134 | <li>To stop tracking or to clear the data and start over, click the <strong>Stop Tracking |
| 135 | button</strong>.</li> |
| 136 | |
| 137 | <li>Click on a specific row in the list to see more detailed information such as the method and |
| 138 | line number of the code that allocated the object.</li> |
| 139 | </ol> |
| 140 | |
| 141 | <h3>Working with an emulator or device's file system</h3> |
| 142 | |
| 143 | <p>DDMS provides a File Explorer tab that allows you to view, copy, and delete files on the |
| 144 | device. This feature is useful in examining files that are created by your application or if you |
| 145 | want to transfer files to and from the device.</p> |
| 146 | |
| 147 | <p>To work with an emulator or device's file system:</p> |
| 148 | <ol> |
| 149 | <li>In the Devices tab, select the emulator that you want to view the file system for.</li> |
| 150 | |
| 151 | <li>To copy a file from the device, locate the file in the File Explorer and click the |
| 152 | <strong>Pull file</strong> button.</li> |
| 153 | |
| 154 | <li>To copy a file to the device, click the <strong>Push file</strong> button on the File |
| 155 | Explorer tab.</li> |
| 156 | </ol> |
| 157 | |
| 158 | <!-- Need to elaborate more on where things are stored in the file system, |
| 159 | databases, apks, user info, files that are important to look at --> |
| 160 | |
| 161 | <h3>Examining thread information</h3> |
| 162 | |
| 163 | <p>The Threads tab in DDMS shows you the currently running threads for a selected process.</p> |
| 164 | |
| 165 | <ol> |
| 166 | <li>In the Devices tab, select the process that you want to examine the threads for.</li> |
| 167 | |
| 168 | <li>Click the <strong>Update Threads</strong> button.</li> |
| 169 | |
| 170 | <li>In the Threads tab, you can view the thread information for the selected process.</li> |
| 171 | </ol> |
| 172 | |
| 173 | <h3 id="profiling">Starting method profiling</h3> |
| 174 | |
| 175 | <p>Method profiling is a means to track certain metrics about a method, such as number of calls, |
| 176 | execution time, and time spent executing the method. If you want more granular control over |
| 177 | where profiling data is collected, use the {@link android.os.Debug#startMethodTracing()} and |
| 178 | {@link android.os.Debug#stopMethodTracing()} methods. For more information about generating trace logs, see |
| 179 | <a href="debugging-tracing.html">Profiling and Debugging UIs</a>.</p> |
| 180 | |
| 181 | <p>Before you start method profiling in DDMS, be aware of the following restrictions:</p> |
| 182 | <ul> |
| 183 | <li>Android 1.5 devices are not supported.</li> |
| 184 | <li>Android 2.1 and earlier devices must |
| 185 | have an SD card present and your application must have permission to write to the SD card. |
| 186 | <li>Android 2.2 and later devices do not need an SD card. The trace log files are |
| 187 | streamed directly to your development machine.</li> |
| 188 | </ul> |
| 189 | |
| 190 | <p>To start method profiling:</p> |
| 191 | <ol> |
| 192 | <li>On the Devices tab, select the process that you want to enable method profiling for.</li> |
| 193 | |
| 194 | <li>Click the <strong>Start Method Profiling</strong> button.</li> |
| 195 | |
| 196 | <li>Interact with your application to start the methods that you want to profile.</li> |
| 197 | |
| 198 | <li>Click the <strong>Stop Method Profiling</strong> button. DDMS stops profiling your |
| 199 | application and opens <a href="{@docRoot}guide/developing/debugging/debugging-ui.html">Traceview</a> |
| 200 | with the method profiling information that was collected |
| 201 | between the time you clicked on <strong>Start Method Profiling</strong> and <strong>Stop Method |
| 202 | Profiling</strong>.</li> |
| 203 | </ol> |
| 204 | |
| 205 | <h3 id="logcat">Using LogCat</h3> |
| 206 | |
| 207 | <p>LogCat is integrated into DDMS, and outputs the messages that you print out using the {@link android.util.Log} |
| 208 | class along with other system messages such as stack traces when exceptions are thrown. View the |
| 209 | <a href="{@docRoot}guide/developing/debugging/debugging-log.html">Reading and |
| 210 | Writing Log Messages.</a> topic for more information on how to log messages to the LogCat.</p> |
| 211 | |
| 212 | <p>When you have set up your logging, you can use the LogCat feature of DDMS to filter certain |
| 213 | messages with the following buttons:</p> |
| 214 | |
| 215 | <ul> |
| 216 | <li>Verbose</li> |
| 217 | |
| 218 | <li>Debug</li> |
| 219 | |
| 220 | <li>Info</li> |
| 221 | |
| 222 | <li>Warn</li> |
| 223 | |
| 224 | <li>Error</li> |
| 225 | </ul> |
| 226 | |
| 227 | <p>You can also setup your own custom filter to specify more details such as filtering messages |
| 228 | with the log tags or with the process id that generated the log message. The add filter, |
| 229 | edit filter, and delete filter buttons let you manage your custom filters.</p> |
| 230 | |
| 231 | <h3>Emulating phone operations and location</h3> |
| 232 | <p>The Emulator control tab lets you simulate a |
| 233 | phone's voice and data network status. This is useful when you want to test your application's |
| 234 | robustness in differing network environments.</p> |
| 235 | |
| 236 | <h4>Changing network state, speed, and latency</h4> |
| 237 | <p>The Telephony Status section of the Emulator |
| 238 | controls tab lets you change different aspects of the phone's networks status, speed and latency. |
| 239 | The following options are available to you and are effective immediately after you set them:</p> |
| 240 | |
| 241 | <ul> |
| 242 | <li>Voice - unregistered, home, roaming, searching, denied</li> |
| 243 | |
| 244 | <li>Data - unregistered, home, roaming, searching, denied</li> |
| 245 | |
| 246 | <li>Speed - Full, GSM, HSCSD, GPRS, EDGE, UMTS, HSDPA</li> |
| 247 | |
| 248 | <li>Latency - GPRS, EDGE, UMTS</li> |
| 249 | </ul> |
| 250 | |
| 251 | <h4>Spoofing calls or SMS text messages</h4> |
| 252 | <p>The Telephony Actions section of the Emulator |
| 253 | controls tab lets you spoof calls and messages. This is useful when you want to to test your |
| 254 | application's robustness in responding to incoming calls and messages that are sent to the phone. |
| 255 | The following actions are available to you:</p> |
| 256 | |
| 257 | <ul> |
| 258 | <li>Voice - Enter a number in the <strong>Incoming number</strong> field and click |
| 259 | <strong>Call</strong> to send a simulated call to the emulator or phone. Click the |
| 260 | <strong>Hang up</strong> button to terminate the call.</li> |
| 261 | |
| 262 | <li>SMS - Enter a number in the <strong>Incoming number</strong> field and a message in the |
| 263 | <strong>Message:</strong> field and click the <strong>Send</strong> button to send the |
| 264 | message.</li> |
| 265 | </ul> |
| 266 | |
| 267 | <h4>Setting the location of the phone</h4> |
| 268 | <p>If your application depends on the location of the phone, you can have DDMS send your |
| 269 | device or AVD a mock location. This is useful if you |
| 270 | want to test different aspects of your application's location specific features without |
| 271 | physically moving. The following geolocation data types are available to you:</p> |
| 272 | |
| 273 | <ul> |
| 274 | <li>Manual - set the location by manually specifying decimal or sexagesimal longitude and |
| 275 | latitude values.</li> |
| 276 | |
| 277 | <li>GPX - GPS eXchange file</li> |
| 278 | |
| 279 | <li>KML - Keyhole Markup Language file</li> |
| 280 | </ul> |
| 281 | |
| 282 | For more information about providing mock location data, see |
| 283 | <a href="guide/topics/location/obtaining-user-location.html#MockData">Obtaining User Location</a>. |
| 284 | |