2007 2013 Kandroid
www.kandroid.org »
kandroid s/w fundamentals 교육 »
안드로이드 Platform 
▶ HTC Dream Platform Images
Dalvik Debugger Support
작성자 인베인
작성일 2009-07-17 (금) 08:57
ㆍ추천: 0  ㆍ조회: 3517      
IP: 202.xxx.254
 
 

Dalvik Debugger Support

The Dalvik virtual machine supports source-level debugging with many popular development environments. Any tool that allows remote debugging over JDWP (the Java Debug Wire Protocol) is expected work. Supported debuggers include jdb, Eclipse, IntelliJ, and JSwat.
 
The VM does not support tools based on JVMTI (Java Virtual Machine Tool Interface). This is a relatively intrusive approach that relies on bytecode insertion, something the Dalvik VM does not currently support.
 
Dalvik's implementation of JDWP also includes hooks for supporting DDM (Dalvik Debug Monitor) features, notably as implemented by DDMS (Dalvik Debug Monitor Server) and the Eclipse ADT plugin. The protocol and VM interaction is described in some detail here.
 
All of the debugger support in the VM lives in the dalvik/vm/jdwp directory, and is almost entirely isolated from the rest of the VM sources. dalvik/vm/Debugger.c bridges the gap. The goal in doing so was to make it easier to re-use the JDWP code in other projects.
 
 
 

Implementation

Every VM that has debugging enabled starts a "JDWP" thread. The thread typically sits idle until DDMS or a debugger connects. The thread is only responsible for handling requests from the debugger; VM-initated communication, such as notifying the debugger when the VM has stopped at a breakpoint, are sent from the affected thread.
 
When the VM is embedded in the Android framework, debugging is enabled in the VM unless the system property ro.secure is set to 1. On these "secure" devices, debugging is only enabled in app processes whose manifest contains android:debuggable="true" in the <application> element.
 
The VM recognizes the difference between a connection from DDMS and a connection from a debugger (either directly or in concert with DDMS). A connection from DDMS alone doesn't result in a change in VM behavior, but when the VM sees debugger packets it allocates additional data structures and may switch to a different implementation of the interpreter.
Because Dalvik maps bytecode into memory read-only, some common techniques are difficult to implement without allocating additional memory. For example, suppose the debugger sets a breakpoint in a method. The quick way to handle this is to insert a breakpoint instruction directly into the code. When the instruction is reached, the breakpoint handler engages. Without this, it's necessary to perform an "is there a breakpoint here" scan. Even with some optimizations, the debug-enabled interpreter is much slower than the regular interpreter (perhaps 5x).
 
The JDWP protocol is stateless, so the VM handles individual debugger requests as they arrive, and posts events to the debugger as they happen.
 
 
 

Debug Data

Source code debug data, which includes mappings of source code to bytecode and lists describing which registers are used to hold method arguments and local variables, are optionally emitted by the Java compiler. When dx converts Java bytecode to Dalvik bytecode, it must also convert this debug data.
 
dx must also ensure that it doesn't perform operations that confuse the debugger. For example, re-using registers that hold method arguments and the "this" pointer is allowed in Dalvik bytecode if the values are never used or no longer needed. This can be very confusing for the debugger (and the programmer) since the values have method scope and aren't expected to disappear. For this reason, dx generates sub-optimal code in some situations when debugging support is enabled.
 
Some of the debug data is used for other purposes; in particular, having filename and line number data is necessary for generating useful exception stack traces. This data can be omitted by dx to make the DEX file smaller.
 
 

Usage

The Dalvik VM supports many of the same command-line flags that other popular desktop VMs do. To start a VM with debugging enabled, you add a command-line flag with some basic options. The basic incantation looks something like this:
-Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=y
or 
-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=y
 

After the initial prefix, options are provided as name=value pairs. The options currently supported by the Dalvik VM are:
transport (no default)
Communication transport mechanism to use. Dalvik supports TCP/IP sockets (dt_socket) and connection over USB through ADB (dt_android_adb).
 
server (default='n')
Determines whether the VM acts as a client or a server. When acting as a server, the VM waits for a debugger to connect to it. When acting as a client, the VM attempts to connect to a waiting debugger.
 
suspend (default='n')
If set to 'y', the VM will wait for a debugger connection before executing application code. When the debugger connects (or when the VM finishes connecting to the debugger), the VM tells the debugger that it has suspended, and will not proceed until told to resume. If set to 'n', the VM just plows ahead.
 
address (default="")
This must be hostname:port when server=n, but can be just port when server=y. This specifies the IP address and port number to connect or listen to.
Listening on port 0 has a special meaning: try to listen on port 8000; if that fails, try 8001, 8002, and so on. (This behavior is non-standard and may be removed from a future release.)
This option has no meaning for transport=dt_android_adb.
 
help (no arguments)
If this is the only option, a brief usage message is displayed.
 
launch, onthrow, oncaught, timeout
These options are accepted but ignored.

To debug a program on an Android device using DDMS over USB, you could use a command like this:
% dalvikvm -agentlib:jdwp=transport=dt_android_adb,suspend=y,server=y -cp /data/foo.jar Foo
This tells the Dalvik VM to run the program with debugging enabled, listening for a connection from DDMS, and waiting for a debugger. The program will show up with an app name of "?" in the process list, because it wasn't started from the Android application framework. From here you would connect your debugger to the appropriate DDMS listen port (e.g. jdb -attach localhost:8700 after selecting it in the app list).
 

To debug a program on an Android device using TCP/IP bridged across ADB, you would first need to set up forwarding:
% adb forward tcp:8000 tcp:8000
% adb shell dalvikvm -agentlib:jdwp=transport=dt_socket,address=8000,suspend=y,server=y -cp /data/foo.jar Foo
and then jdb -attach localhost:8000.

(In the above examples, the VM will be suspended when you attach. In jdb, type cont to continue.)
The DDMS integration makes the dt_android_adb transport much more convenient when debugging on an Android device, but when working with Dalvik on the desktop it makes sense to use the TCP/IP transport.
 

Known Issues and Limitations


Most of the optional features JDWP allows are not implemented. These include field access watchpoints and better tracking of monitors.
 
Not all JDWP requests are implemented. In particular, anything that never gets emitted by the debuggers we've used is not supported and will result in error messages being logged. Support will be added when a use case is uncovered.

The debugger and garbage collector are somewhat loosely integrated at present. The VM currently guarantees that any object the debugger is aware of will not be garbage collected until after the debugger disconnects. This can result in a build-up over time while the debugger is connected.
 
The situation is exacerbated by a flaw in the exception processing code, which results in nearly all exceptions being added to the "do not discard" list, even if the debugger never sees them. Having a debugger attached to a program that throws lots of exceptions can result in out-of-memory errors. This will be fixed in a future release.
 
 
Copyright © 2009 The Android Open Source Project
덧글 쓰기 0
3500
※ 회원등급 레벨 0 이상 읽기가 가능한 게시판입니다.
    N     분류     제목    글쓴이 작성일 조회
95 [포팅]레노버 s10-3t 넷북에서 android-x86 & Windows XP 멀티.. [3] GunStar 2011-01-15 2693
94 Connect USB devices to your Nexus One [1] 들풀 2010-12-20 2951
93 Froyo 에서 i2c driver 의 suspend/resume 동작 [1]+1 손가락 2010-09-14 4820
92 Android용 Bluetooth solution [7] sandy 2010-05-04 6038
91 추방되었던 안드로이드 커널 다시 들어온다. [3]+2 인베인 2010-04-19 8282
90    [번역] Google in talks to re-admit Android to Linux kernel [3] 들풀 2010-04-20 3512
89 Android Virtualization (KVM for ARM) [4]+2 인베인 2010-04-16 5430
88 안드로이드 SDK Tools R5 릴리즈 (2010.03.20) [2]+1 인베인 2010-03-20 4207
87 OKL4(가상화) for the HTC dream 인베인 2010-02-26 3135
86 안드로이드 소스 변경 알리미,DRAG [8]+5 인베인 2009-12-19 3972
85 Android 2.0 Brief [6]+2 삼스 2009-12-01 5993
84 안드로이드 솔류션 센터 , ARM [5]+5 인베인 2009-11-17 3257
83 안드로이드 및 리눅스를 이용한 멀티 운영체제.. 인베인 2009-07-31 4074
82 L4(MicroVisor)을 이용한 Android 구동방법 인베인 2009-07-24 2555
81 Android 디버깅을 위한 CoWare 솔류션 [3]+1 인베인 2009-07-23 3293
80 Dalvik Debugger Support 인베인 2009-07-17 3517
1234567