Android DDMS如何使用?
By: 海市蜃楼 | In: Android开发
11九2009DDMS 的全称是Dalvik Debug Monitor Service,它为我们提供例如:为测试设备截屏,针对特定的进程查看正在运行的线程以及堆信息、Logcat、广播状态信息、模拟电话呼叫、接收SMS、虚拟地理坐标等等。
如何启动 DDMS
DDMS 工具存放在SDK – tools/路径下,启动DDMS方法如下:
- 直接双击ddms.bat运行;
- 在Eclipes调试程序的过程中启动DDMS,在Eclipes中的界面如下:
选择“Other”,界面如下:
双击DDMS就可以启动了。
DDMS对Emulator和外接测试机有同等效用。如果系统检测到它们(VM)同时运行,那么DDMS将会默认指向 Emulator。以上2种启动后的操作有些不一样,建议分别尝试下。
DDMS 的工作原理
DDMS将搭建起IDE与测试终端(Emulator 或者connected device)的链接,它们应用各自独立的端口监听调试器的信息,DDMS可以实时监测到测试终端的连接情况。当有新的测试终端连接后,DDMS将捕捉到终端的ID,并通过adb建立调试器,从而实现发送指令到测试终端的目的。
DDMS监听第一个终端App进程的端口为8600,APP进程将分配8601,如果有更多终端或者更多APP进程将按照这个顺序依次类推。DDMS通过8700端口(”base port”)接收所有终端的指令。
下边通过GUI详细了解DDMS的一些功能
Devices
在GUI的左上角可以看到标签为”Devices”的面板,这里可以查看到所有与DDMS连 接的终端的详细信息,以及每个终端正在运行的APP进程,每个进程最右边相对应的是与调试器链接的端口。因为Android是基于Linux内核开发的操 作平台,同时也保留了Linux中特有的进程ID,它介于进程名和端口号之间。
在面板的右上角有一排很重要的按键他们分别是Debug the selected process、Update Threads、Update Heap、Stop Process和ScreenShot。
Emulator Control
通过这个面板的一些功能可以非常容易的使测试终端模拟真实手机所具备的一些交互功能,比如:接听电话,根据选项模拟各种不同网络情况,模拟接受SMS消息和发送虚拟地址坐标用于测试GPS功能等。
Telephony Status: 通过选项模拟语音质量以及信号连接模式。
Telephony Actions: 模拟电话接听和发送SMS到测试终端。
Location Control: 模拟地理坐标或者模拟动态的路线坐标变化并显示预设的地理标识,可以通过以下3种方式:
- Manual: 手动为终端发送二维经纬坐标。
- GPX: 通过GPX文件导入序列动态变化地理坐标,从而模拟行进中GPS变化的数值。
- KML: 通过KML文件导入独特的地理标识,并以动态形式根据变化的地理坐标显示在测试终端。
Threads、Heap、File Exporler
这几项,我们在其他开发工具中也经常使用,就在不此详细说明了。通过File Exporler可以查看Android模拟器中的文件,可以很方便的导入/出文件。
Locate、Console
Locate:显示输出的调试信息,详见Android下如何调试程序?;
Console:是Android模拟器输出的信息,加载程序等信息;
使用DDMS模拟发送短信,操作过程如下:
在Emulator Control\Telephony Actions 中输入以下内容
单击发送后,在Android模拟器中打开Messaging,看到下面的短信:
单击新短信,详细查看短信内容:
中文显示为乱码,在未来的开发中,我们必须要注意中文字符的问题。
总结说明
DDMS是我们开发人员最好的调试工具,它将是每个从事Android开发的人员都不可缺少的。
Using DDMS
IN THIS DOCUMENT
- Running DDMS
- How DDMS Interacts with a Debugger
- Using DDMS
- Viewing heap usage for a process
- Tracking memory allocation of objects
- Working with an emulator or device's file system
- Examining thread information
- Starting method profiling
- Using the Network Traffic tool
- Using LogCat
- Emulating phone operations and location
Android ships with a debugging tool called the Dalvik Debug Monitor Server (DDMS), which provides port-forwarding services, screen capture on the device, thread and heap information on the device, logcat, process, and radio state information, incoming call and SMS spoofing, location data spoofing, and more. This page provides a modest discussion of DDMS features; it is not an exhaustive exploration of all the features and capabilities.
Running DDMS
DDMS is integrated into Eclipse and is also shipped in the tools/
directory of the SDK. DDMS works with both the emulator and a connected device. If both are connected and running simultaneously, DDMS defaults to the emulator.
- From Eclipse: Click Window > Open Perspective > Other... > DDMS.
- From the command line: Type
ddms
(or./ddms
on Mac/Linux) from thetools/
directory.
How DDMS Interacts with a Debugger
On Android, every application runs in its own process, each of which runs in its own virtual machine (VM). Each VM exposes a unique port that a debugger can attach to.
When DDMS starts, it connects to adb. When a device is connected, a VM monitoring service is created between adb
and DDMS, which notifies DDMS when a VM on the device is started or terminated. Once a VM is running, DDMS retrieves the VM's process ID (pid), via adb
, and opens a connection to the VM's debugger, through the adb daemon (adbd) on the device. DDMS can now talk to the VM using a custom wire protocol.
DDMS assigns a debugging port to each VM on the device. Typically, DDMS assigns port 8600 for the first debuggable VM, the next on 8601, and so on. When a debugger connects to one of these ports, all traffic is forwarded to the debugger from the associated VM. You can only attach a single debugger to a single port, but DDMS can handle multiple, attached debuggers.
By default, DDMS also listens on another debugging port, the DDMS "base port" (8700, by default). The base port is a port forwarder, which can accept VM traffic from any debugging port and forward it to the debugger on port 8700. This allows you to attach one debugger to port 8700, and debug all the VMs on a device. The traffic that is forwarded is determined by the currently selected process in the DDMS Devices view.
The following screenshot shows a typical DDMS screen in Eclipse. If you are starting DDMS from the command line, the screen is slightly different, but much of the functionality is identical. Notice that the highlighted process,com.android.email
, that is running in the emulator has the debugging port 8700 assigned to it as well as 8606. This signifies that DDMS is currently forwarding port 8606 to the static debugging port of 8700.

Figure 1. Screenshot of DDMS
If you are not using Eclipse and ADT, read Configuring your IDE to attach to the debugging port, for more information on attaching your debugger.
Tip: You can set a number of DDMS preferences in File > Preferences. Preferences are saved to$HOME/.android/ddms.cfg
.
Known debugging issues with Dalvik
Debugging an application in the Dalvik VM should work the same as it does in other VMs. However, when single-stepping out of synchronized code, the "current line" cursor may jump to the last line in the method for one step.
Using DDMS
The following sections describe how to use DDMS and the various tabs and panes that are part of the DDMS GUI. The Eclipse version and the command line version have minor UI differences, but the same functionality. For information on running DDMS, see the previous section in this document, Running DDMS.
Viewing heap usage for a process
DDMS allows you to view how much heap memory a process is using. This information is useful in tracking heap usage at a certain point of time during the execution of your application.
To view heap usage for a process:
- In the Devices tab, select the process that you want to see the heap information for.
- Click the Update Heap button to enable heap information for the process.
- In the Heap tab, click Cause GC to invoke garbage collection, which enables the collection of heap data. When the operation completes, you will see a group of object types and the memory that has been allocated for each type. You can click Cause GC again to refresh the data.
- Click on an object type in the list to see a bar graph that shows the number of objects allocated for a particular memory size in bytes.
Tracking memory allocation of objects
DDMS provides a feature to track objects that are being allocated to memory and to see which classes and threads are allocating the objects. This allows you to track, in real time, where objects are being allocated when you perform certain actions in your application. This information is valuable for assessing memory usage that can affect application performance.
To track memory allocation of objects:
- In the Devices tab, select the process that you want to enable allocation tracking for.
- In the Allocation Tracker tab, click the Start Tracking button to begin allocation tracking. At th