Reference/ddms

From Android中文网

Android中文网(androidcn.net) 版权申明 : creativecommons licenses
Jump to: navigation, search

目录

[编辑] 使用Dalvik调适监视器服务工具

Android ships with a debugging tool called the Dalvik Debug Monitor Service (DDMS), which provides port-forwarding services, screen capture on the device,

thread and heap information on the device, logcat, process, and radio state information, and more features. DDMS ships in the tools/ directory of the SDK; to

run DDMS, simply type ddms (lowercase) in a shell window in that directory. DDMS will work with both the emulator and the device; if both are connected and

running simultaneously, DDMS defaults to the emulator. This page provides a brief list of DDMS features; it is not an exhaustive exploration of all the

features and capabilities.

Android附带了一个调试工具, 叫做Dalvik 调适监视器服务(DDMS), 提供了端口转发服务, 手机设备屏幕截图, 设备上的线程和堆栈信息, 日志, 进程, 射频状态信息, 以及更

多的功能. DDMS 位于SDK主目录下的tools目录中; 在该目录的命令控制台输入ddms(小写)即可运行DDMS. DDMS可以配合模拟器或设备使用; 如果模拟器和设别同时被连接并且同时

在运行, DDMS默认会连接模拟器. 该文档对DDMS的功能做了简要的介绍; 这里没有对DDMS的特性和功能做过与详细的解释.

You can set a number of DDMS preferences in File->Preferences. Preferences are saved to "$HOME/.ddmsrc".

你可以对DDMS进行一些设置, 点击菜单中的File->Preferences即可开始设置, 设置文件存放在"$HOME/.ddmsrc"目录下.


[编辑] 左下角面板

The left side of the window shows a list of all the virtual machines on the device/emulator that DDMS finds. VMs are identified by the package name of the

application it hosts; if you have several Activities in a single package, they will all be in the same VM. Use this list to find and attach to the VM running

the activity(ies) that you want to debug. Next to each VM in the list is one or two "debugger pass-through" port numbers; if you connect your debugger to one

of the the ports listed, you will be connected to the corresponding VM on the device. One nice feature is that Dalvik assigns an additional port, 8700, to

the currently selected VM in the list. This "currently selected" default port lets you switch applications without having to reconfigure your debugger's port

each time. When an application running in a Dalvik VM calls waitForDebugger() (or you select this option in the developer options), a red icon will be shown

next to the client name. When a debugger is connected, the icon will turn green.

左侧窗口中的列表显示了DDMS找到的设备或模拟器中的所有虚拟机(VM). 虚拟机用它的应用的包名来标识; 如果有多个Activities 在一个包内, 他们会在同一个虚拟机内运行. 利

用该列表找到你想调试的activity, 在该列表中, 每个虚拟机的前面会显示"调试端口"的端口号; 如果你使用调试器链接这些端口, 就相当于链接了该设备中相应的虚拟机. 一个

不错的特性是, Dalvik 还为当前被选择的虚拟机分配了一个附加端口号, 8700. 这个默认的"当前选择"端口可以让你在切换应用的时候不必重新配置调试端口(就是说调试器链接

8700, 之后不管你切换到哪个虚拟机, 8700都是你当前选择的虚拟机的调试口). 当一个运行在虚拟机中的应用执行了waitForDebugger()(或者你在手机的developer选项里面选择

了这项), 一个红色的图标会在客户端的名字后面显示, 当一个调试器链接上之后, 这个图标会变成绿色.

DDMS has a few tabs that display useful information about the currently selected VM:

DDMS的一些标签页显示了当前需选择的虚拟机的有用信息:

[编辑] 信息标签页

This tab some general information about the selected VM, including the process ID, and the port on the device that DDMS connects to. This is not the port

that you will configure your IDE to use.

这个标签页显示了所选虚拟机的常用信息, 包括进程的ID, DDMS链接的端口号. 这个不是您在IDE里面配置的那个端口号.

In addition to the listed port, the currently selected VM from the lower left list is also forwarded to port 8700. Having this "currently selected" port

avoids your having to reconfigure Eclipse when debugging multiple Activities. (This "currently selected" port number is configurable in the "preferences"

dialog.)

另外, 在虚拟机的列表页面的中, 当前所选的虚拟机也会同样将端口信息转发到8700端口, 使用这个"当前选择"端口, 避免您在调适多个Activities时重新配置eclipse的端口信息

.(该"当前选择"端口号可以在"preferences"对话框中设置.)

[编辑] 线程标签页

The threads tab has a list of threads running in the process of the target VM. To reduce the amount of data sent over the wire, the thread updates are only

sent when explicitly enabled by toggling the "threads" button in the toolbar. This toggle is maintained per VM. This tab includes the following information:

线程标签页显示了在目标虚拟机中当前进程中的所有线程信息. 为了减少数据传输, 仅仅在工具栏的"threads"被按下时设备才会发送线程的信息. 每一个虚拟机都有一个按钮开关

, 该标签页显示了如下信息:

  • ID - a VM-assigned unique thread ID. In Dalvik, these are odd numbers starting from 3.
  • Tid - the Linux thread ID. For the main thread in a process, this will match the process ID.
  • Status - the VM thread status. Daemon threads are shown with a '*'. This will be one of the following:
  • running - executing application code
  • sleeping - called Thread.sleep()
  • monitor - waiting to acquire a monitor lock
  • wait - in Object.wait()
  • native - executing native code
  • vmwait - waiting on a VM resource
  • zombie - thread is in the process of dying
  • init - thread is initializing (you shouldn't see this)
  • starting - thread is about to start (you shouldn't see this either)
  • utime - cumulative time spent executing user code, in "jiffies" (usually 10ms). Only available under Linux.
  • stime - cumulative time spent executing system code, in "jiffies" (usually 10ms).
  • Name - the name of the thread
  • ID - 虚拟机分配的唯一线程ID. 在 Dalvik, 该数字是一个从3开始的奇数.
  • Tid - Linux 线程 ID. 进程中主线程的ID, 会同进程的ID相匹配.
  • 状态 - 虚拟机线程状态. 守护进程会附带一个'*'. 状态信息列表如下:
  • running - 正在执行应用程序
  • sleeping - 执行了Thread.sleep() 方法
  • monitor - 在正等待获取一个监听锁
  • wait - 在Object.wait() 方法中
  • native - 执行了原生代码
  • vmwait - 正在等待一个虚拟机资源
  • zombie - 该线程已死
  • init - 线程正在初始化 (你不会看到这个)
  • starting - 线程正在启动中 (这个你也不会看到)
  • utime - 执行用户代码的累计时间, 单位为"jiffies(表示系统启动以来的tick数)" (通常是 10ms). 仅在Linux系统中适用.
  • stime - 执行系统代码的累计时间, 单位为"jiffies(表示系统启动以来的tick数)".
  • 名字 - 线程的名字

"ID" and "Name" are set when the thread is started. The remaining fields are updated periodically (default is every 4 seconds).

"ID" 和 "Name" 在进程启动的时候就会显示. 其余的字段每个一段时间更新一次(默认是4秒钟)

[编辑] 堆标签页

Displays some heap stats, updated during garbage collection.

显示内存堆的状态信息, 每次垃圾回收的时候更新.

[编辑] 端口转发(调试器链接到设备)

DDMS acts as a middleman to connect the IDE to the running applications on the device. Android hosts multiple processes, each of which hosts its own virtual machine. Every application runs in its own process. Each process listens for a debugger on a different port: The first process listens for a debugger on port 8000, the next on 8001, and so on. DDMS scans ports 8000-8019 on the device by default and rescans for new processes every two seconds (you can change these settings in the DDMS preferences pages). When DDMS detects a virtual machine, it connects to the debugger port and begins requesting information. DDMS interacts with the Dalvik VM using an custom wire protocol. After it has connected, it begins forwarding this information to the specified port that your desktop IDE can attach to, as described in Configuring your IDE to attach to port 8700 for debugging.

DDMS扮演了IDE和设备上运行的应用的中间人角色. Android持有多个进程, 每一个进程都有自己的虚拟机. 每一个应用运行在自己的进程中. 每个进程都有一个唯一的调试监听端口: 第一个进程的调试监听端口是8000, 下一个是8001, 依次向下. DDMS默认情况下会扫描设备上的8000-8019端口, 并且每两秒扫描一次, 以获取新的进程信息(你可以在DDMS的配置页改变这些信息). 当DDMS检测到一个虚拟, 他会链接到调试端口并且开始发送请求信息. DDMS和Dalvik 通过一个自定义的有线通信协议来相互联系. 链接建立之后, 他就开始把信息转发到你的IDE能够接受的端口, 以显示出来, 你的IDE会使用8700端口来进行调试.

调试Dalvik时会出现的已知问题

Debugging an application in the Dalvik VM should work the same as it does in other VMs. There is one exception worth noting:

When single-stepping out of synchronized code, the "current line" cursor may jump to the last line in the method for one step.

在Dalvik 虚拟机中调试应用跟在其他虚拟机中一样. 有一个Exception没有价值:

当single-stepping 与代码不同步的时候, 点击下一个step之后, 显示当前行的光标可能会直接跳到方法的最后一行.

[编辑] 截图

You can capture screen images on the device or emulator by selecting Device > Screen capture... on the main menu.

您可以截取设备或虚拟机屏幕, 选择主菜单中的Device > Screen capture.

[编辑] 进程信息查看

You can see the output of ps -x for a specific VM by selecting Device > Show process status... on the main menu.

你可以查看指定虚拟机中 ps -x命令的输出, 在主菜单选择Device > Show process status.

[编辑] 手动垃圾回收

Cause garbage collection to occury by pressing the GC button on the toolbar.

点击工具条的GC按钮, 会引发一个垃圾回收操作.

[编辑] 在设备上执行 Dumpsys 和 Dumpstate (logcat)

To run dumpsys (logcat) from Dalvik, select Device > Run logcat... on the main menu.

执行Dalvik的dumpsys(logcat)命令, 选择主菜单的Device > Run logcat...

To run dumpstate from Dalvik, select Device > Dump device state... on the main menu.

执行Dalvik的dumpstate命令, 选择主菜单的Device > Dump device state...

Examine Radio State

检查射频状态

By default, radio state is not output during a standard logcat (it is a lot of information). To see radio information, either click Device > Dump radio state... or run logcat as described in Logging Radio Information.

默认情况下, 射频的状态信息是不会输出的(因为这个信息非常多). 若想查看该信息, 点击Device > Dump radio state... 或是像记录射频日志中描述那样执行logcat.

[编辑] 停止虚拟机

You can stop a virtual machine by selecting Actions > Halt VM. Pressing this button causes the VM to call System.exit(1).

你可以通过选择菜单中的Actions > Halt VM来停止一个虚拟机, 点击该按钮相当于让虚拟机执行System.exit(1).

[编辑] DDMS的已知问题

DDMS has the following known limitations:

DDMS目前有下列已知问题:

  • If you connect and disconnect a debugger, ddms drops and reconnects the client so the VM realizes that the debugger has gone away. This will be fixed eventually.
  • 如果你链接和断开一个调试器, ddms 断开和重连客户端时, 虚拟机会认为调试器已经失去连接了. 这个问题最终会解决.
  • Don't try to connect ddms to an app running in a Sun Java desktop VM. It will crash.
  • 不要尝试让ddms链接一个运行在Sun Java 桌面虚拟机, 这样会引起崩溃.
Personal tools