2.2、连接调试器
当DDM在所有vm的JWDP端口运行时,通过DDM连接调试器很重要。每个要调试的vm会有一个等待DDM监听的端口,这使得你可以将一个调试器和多个vm同时相连。
更常见的是,程序员只想将一个调试器和一个vm相连。一个端口会被DDM监听(例如8700),然后任何和它相连的东西会被“当前vm”连接(从UI中选择)。这使得程序员可以关注单个应用,而不需要每次重启设备时调整IDE设置以适应不同端口号,如果不是这样,序号就会不停变动。
2.3、包格式
信息以块(chunk)的形式传递,每个块开头如下:
U4 type
U4 length
然后包含了不同数量的特定类型(type)的数据。不识别的类型会导致客户端的空响应,这在DDM会被安静地忽略。(一般是返回ERROR,需要一个ERROR块类型及DDM端的handler)
同样的块类型可能在传递方向不同时有不同的含义,例如,同样的数据类型可能被用在询问和对应的响应上,为了清晰明了,特定的类型必须用在对应的功能上。
上面的包格式对于JDWP框架有些冗余,JDWP框架有4字节的length和2字节的类型码(“命令集”和“命令”,一组命令集用于“vendor-defined命令和扩展”)。
使用块的形式允许潜在传递(underlying transport)的独立性,避免了和JDWP客户端码元的插入集成(intrusive integration),提供了在单个传输单元中发送多个块的方法。(包含多个块的包是可以设计的,这里不讨论)
因为我们可能通过慢的USB连接发送数据,块会被压缩。压缩块也是块类型,但是指明了被压缩,压缩后的长度紧跟其后,最后是原来的块类型和未压缩的长度。例如,zlib的压缩算法而言,块类型是ZLIB。
基于JDWP模型,DDM到客户端的包都会被确认,但从客户端到DDM的包却不会,JDWP错误码码域总是被设为“no error”,因此特定请求未响应的情形一定要被编码到DDM信息中。
所谓的U4,是一个无符号32bit的值,U1是无符号的8bit的值。数据是大印第安序列形式以匹配JDWP。===================================CUT==========================
Dalvik VM
Debug Monitor
Status:Draft (as of March 6, 2007)
[authors] Modified: Sat Apr 11 2009 IntroductionIt's extremely useful to be able to monitor the live state of the VM. For Android, we need to monitor multiple VMs running on a device connected through USB or a wireless network connection. This document describes a debug monitor server that interacts with multiple VMs, and an API that VMs and applications can use to provide information to the monitor.
Some things we can monitor with the Dalvik Debug Monitor ("DDM"):
Thread states. Track thread creation/exit, busy/idle status. Overall heap status, useful for a heap bitmap display or fragmentation analysis.It is possible for something other than a VM to act as a DDM client, but that is a secondary goal. Examples include "logcat" log extraction and system monitors for virtual memory usage and load average.
It's also possible for the DDM server to be run on the device, with the information presented through the device UI. However, the initial goal is to provide a display tool that takes advantage of desktop tools and screen real estate.
This work is necessary because we are unable to use standard JVMTI-based tools with Dalvik. JVMTI relies on bytecode insertion, which is not currently possible because Dalvik doesn't support Java bytecode.
The DDM server is written in the Java programming language for portability. It uses a desktop UI toolkit (SWT) for its interface.
ProtocolTo take advantage of existing infrastructure we are piggy-backing the DDM protocol on top of JDWP (the Java Debug Wire Protocol, normally spoken between a VM and a debugger). To a non-DDM client, the DDM server just looks like a debugger.
The JDWP protocol is very close to what we want to use. In particular:
It explicitly allows for vendor-defined packets, so there is no need to "bend" the JDWP spec. Events may be posted from the VM at arbitrary points. Such events do not elicit a response from the debugger, meaning the client can post data and immediately resume work without worrying about the eventual response. The basic protocol is stateless and asynchronous. Request packets from the debugger side include a serial number, which the VM includes in the response packet. This allows multiple simultaneous conversations, which means the DDM traffic can be interleaved with debugger traffic.