| Robert Ly | ce4d229 | 2010-12-16 17:26:11 -0800 | [diff] [blame^] | 1 | page.title=Debugging |
| 2 | @jd:body |
| 3 | |
| 4 | |
| 5 | <div id="qv-wrapper"> |
| 6 | <div id="qv"> |
| 7 | <h2>In this document</h2> |
| 8 | |
| 9 | <ol> |
| 10 | <li><a href="#stack">Debugging Environment</a></li> |
| 11 | |
| 12 | <li><a href="#addltools">Additional Debugging Tools</a></li> |
| 13 | </ol> |
| 14 | </div> |
| 15 | </div> |
| 16 | |
| 17 | <p>The Android SDK provides most of the tools that you need to debug your applications. You need |
| 18 | a JDWP-compliant debugger if you want to be able to do things such as step through code, |
| 19 | view variable values, and pause execution of an application. If you are using Eclipse, a |
| 20 | JDWP-compliant debugger is already included and there is no setup required. If you are using |
| 21 | another IDE, you can use the debugger that comes with it and attach the debugger to a special |
| 22 | port so it can communicate with the application VMs on your devices. The main components that |
| 23 | comprise a typical Android debugging environment are:</p> |
| 24 | |
| 25 | <dl> |
| 26 | <dt><a href="{@docRoot}guide/developing/tools/adb.html"><strong>adb</strong></a></dt> |
| 27 | |
| 28 | <dd><code>adb</code> acts as a middleman between a device and your development system. It provides various |
| 29 | device management capabilities, including moving and syncing files to the emulator, running a |
| 30 | UNIX shell on the device or emulator, and providing a general means to communicate with |
| 31 | connected emulators and devices.</dd> |
| 32 | |
| 33 | <dt><a href="{@docRoot}guide/developing/tools/ddms.html"><strong>Dalvik Debug Monitor |
| 34 | Server</strong></a></dt> |
| 35 | |
| 36 | <dd>DDMS is a graphical program that communicates with your devices through <code>adb</code>. DDMS can |
| 37 | capture screenshots, gather thread and stack information, spoof incoming calls and SMS |
| 38 | messages, and has many other features.</dd> |
| 39 | |
| 40 | <dt><a href="{@docRoot}guide/developing/devices/avds-devices.html"><strong>Device or |
| 41 | Emulator</strong></a></dt> |
| 42 | |
| 43 | <dd>Your application must run in a device or emulator so that it can be debugged. An <code>adb</code> device |
| 44 | daemon runs on the device or emulator and provides a means for the <code>adb</code> host daemon to |
| 45 | communicate with the device.</dd> |
| 46 | |
| 47 | <dt><strong>JDWP debugger</strong></dt> |
| 48 | |
| 49 | <dd>The Dalvik VM (Virtual Machine) supports the JDWP protocol to allow debuggers to attach to |
| 50 | a VM. Each application runs in a VM and exposes a unique port that you can attach a debugger to |
| 51 | via DDMS. If you want to debug multiple applications, attaching to each port might become |
| 52 | tedious, so DDMS provides a port forwarding feature that can forward a specific VM's debugging |
| 53 | port to port 8700. You can switch freely from application to application by highlighting it in the |
| 54 | Devices tab of DDMS. DDMS forwards the appropriate port to port 8700. Most modern Java IDEs include a JDWP debugger, |
| 55 | or you can use a command line debugger such as <a href="http://download.oracle.com/javase/6/docs/technotes/tools/"> |
| 56 | <code>jdb</code></a>.</dd> |
| 57 | </dl> |
| 58 | |
| 59 | <h2>Debugging Environment</h2> |
| 60 | |
| 61 | <p>Figure 1 shows how the various debugging tools work together in a typical |
| 62 | debugging environment.</p> |
| 63 | <img src="/images/debugging.png" |
| 64 | alt="Debugging workflow" /> |
| 65 | <p class="img-caption><strong>Figure 1. </strong> Debugging Workflow</p> |
| 66 | |
| 67 | <p>On your emulator or device, each application runs in its own instance of a Dalvik VM. The <code>adb</code> |
| 68 | device daemon allows communication with the VMs from an outside party.</p> |
| 69 | |
| 70 | <p>On your development machine, the <code>adb</code> host daemon communicates with the <code>adb</code> device daemon and |
| 71 | allows tools such as DDMS to communicate with the device or emulator. The <code>adb</code> host daemon also |
| 72 | allows you to access shell commands on the device as well as providing capabilities such as |
| 73 | application installation and file transferring.</p> |
| 74 | |
| 75 | <p>Each application VM on the device or emulator exposes a debugging port that you can attach to |
| 76 | via DDMS. DDMS can forward any of these ports to a static debugging port (typically port 8700) by |
| 77 | selecting the application that you want to debug in the DDMS user interface. A JDWP debugger can |
| 78 | attach to this static debugging port and debug all the applications that are running on the |
| 79 | device or emulator without having to attach to multiple ports.</p> |
| 80 | |
| 81 | <p>If you are using Eclipse, much of these interconnections are hidden from you. DDMS, <code>adb</code>, and a |
| 82 | JDWP debugger are all setup for you and you can access them through the Debug and DDMS |
| 83 | perspectives in Eclipse. If you are developing in a non-Eclipse environment, you have to invoke |
| 84 | these tools manually.</p> |
| 85 | |
| 86 | <h2 id="addltools">Additional Debugging Tools</h2> |
| 87 | |
| 88 | <p>In addition to the main debugging tools, the Android SDK provides additional tools to help you |
| 89 | debug and profile your applications:</p> |
| 90 | |
| 91 | <dl> |
| 92 | <dt><strong><a href="{@docRoot}guide/developing/debugging/debugging-ui.html">Heirarchy Viewer |
| 93 | and layoutopt</a></strong></dt> |
| 94 | |
| 95 | <dd>Graphical programs that let you debug and profile user interfaces.</dd> |
| 96 | |
| 97 | <dt><strong><a href= |
| 98 | "{@docRoot}guide/developing/debugging/debugging-tracing.html">Traceview</a></strong></dt> |
| 99 | |
| 100 | <dd>A graphical viewer that displays trace file data for method calls and times saved by your |
| 101 | application, which can help you profile the performance of your application.</dd> |
| 102 | |
| 103 | <dt><strong><a href="{@docRoot}guide/developing/debugging/debugging-devtools.html">Dev Tools |
| 104 | Android application</a></strong></dt> |
| 105 | |
| 106 | <dd>The Dev Tools application included in the emulator system image exposes several settings |
| 107 | that provide useful information such as CPU usage and frame rate. You can also transfer the |
| 108 | application to a hardware device.</dd> |
| 109 | </dl> |