adb jdb调试相关

adb的概念
android debug bridge(adb) 是一个多功能的命令行工具,可以和模拟器或真机进行交互,它是一个包含三个组件的client-server(C/S)架构的程序

  • client-运行在PC上,当你使用adb命令时,当前的那个命令行窗口就相当于一个client,像ADT插件和DDMS(ADT,DDMS是eclipse用来的调试和监控用户程序的)等工具都可以理解成为client
  • server-运行在PC上的后台进程,它管理着client和adb daemon之间的通信,它占用本地PC的5037端口
  • daemon-adb的监控进程,运后在目标机或是模拟器的后台进程,等待adb来和它通信
  • 每个模拟器或真机都会启动从5554开始的两个连续的端口,例如
Emulator 1, console: 5554
Emulator 1, adb: 5555
Emulator 2, console: 5556
Emulator 2, adb: 5557 ... 
Emulator 1开始了两个端口,5554和5555,它们分别用来和console和adb server进行通信的
  • 通信过程为client <----------> adb server <----------> adb daemon
  • client可以有多个
  • server只会有一个它会占用PC的5037端口为做为client和adb deamon的中间协调者
  • deamon则是每个模拟器或目标机上只存在一个
  • adb存在于sdk/platform-tools下

adb提供了管理模拟器或真机常用命令,命令格式如下所示
adb [-d|-e|-s <serialNumber>] <command>
-d表示通过USB连接的设备
-e表示为模拟器

adb devices 例出所有的模拟器或真机
adb  [-d|-e|-s <serialNumber>] install <本地apk的路径>
adb [-d|-e|-s <serialNumber>]  pull <remote> <local>
adb [-d|-e|-s <serialNumber>]  push <local> <remote> for example adb push c:\tool.txt /sdcard/foo.txt
adb [-d|-e|-s <serialNumber>]  logcat
adb [-d|-e|-s <serialNumber>]  jdwp 显示所有支持jdwp调试的进行pid,一般情况下,用户级别的APK都会在这里被显示出来的
adb  forward  tcp:<本地机器的网络端口号>  tcp:<模拟器或是真机的网络端口号>
例:adb [-d|-e|-s <serialNumber>] forward tcp:6100 tcp:7100 表示把本机的6100端口号与模拟器的7100端口建立起相关,当模拟器或真机向自己的7100端口发送了数据,那们我们可以在本机的6100端口读取其发送的内容,这是一个很关键的命令,以后我们使用jdb调式apk之前,就要用它先把目标进程和本地端口建立起关联
logcat -b radio 显示无线通讯日志





jdb是一个支持java代码级调试的工具,它是由java jdk提供的,存在于xxx\Java\jdk1.6.0_21\bin之下

使用ddms调试时,主机会打开另外一个网络端口,在DDMS里查看,一般是8700。

启动DDMS,这时程序前面应该有个红色小虫,点上面的开始调试按钮。这步不是必须的,这步的工作其实相当于手动敲:

$ adb -d forward tcp:8700 jdwp:$PID

其中$PID为要调程序的进程号。

 


通过attach方式进行调试

adb jdwp显示所有可供调试的用户进程
使用本地端口号连接到终端的给定进程:
adb forward tcp:xxx jdwp:<pid>
直接使用本机绑定的通讯端口,不指定进程
jdb -connect com.sun.jdi.SocketAttach:hostname=localhost,port=xxx


转自:http://shine80769769.blog.163.com/blog/static/179148245201286114839627/

JDWP 协议介绍

这里首先要说明一下 debugger 和 target vm。Target vm 中运行着我们希望要调试的程序,它与一般运行的 Java 虚拟机没有什么区别,只是在启动时加载了 Agent JDWP 从而具备了调试功能。而 debugger 就是我们熟知的调试器,它向运行中的 target vm 发送命令来获取 target vm 运行时的状态和控制 Java 程序的执行。Debugger 和 target vm 分别在各自的进程中运行,他们之间的通信协议就是 JDWP。

用IDA调试时,运行android_server命令,会在手机终端网络端口23946打开一个监听。

shell@android:/data/local/tmp # ./android_server
./android_server
IDA Android 32-bit remote debug server(ST) v1.14. Hex-Rays (c) 2004-2011
Listening on port #23946...
=========================================================
[1] Accepting connection from localhost(127.0.0.1)...

IDA调试时,都是通过网络接口通信,一般电脑和终端都使用23946这个网络端口通信。通过指令adb  forward tcp:23946 tcp:23946 将双方网络端口号绑定。

 

Debugging Android Applications with jdb

In order to attach jdb to an Android application, which is running inside the Dalvik VM, we have to use adb, the Android debug bridge. adb bridges the gap between an application and a development/debugging environment. The Dalvik VM creates a JDWP thread for every application to allow debuggers to attach to it on certain ports/process IDs.

In order to find out the JDWP ID of a debuggable application, issue the adb jdwp command. This will return a list of currently active JDWP processes. The very last number corresponds to the JDWP the last debuggable application launched.

To attach jdb to the remote VM we have to have adb forward the remote JDWP port/process ID to a local port. This is done with the forward command, like so: adb forward tcp:7777 jdwp:JDWP_PORT. adb will open a local TCP socket that you can connect to, and will forward all data sent to the local TCP socket to the JDWP process running on the device/emulator.

Next, attach jdb like so: jdb -sourcepath /your/project/src -attach localhost:7777. The sourcepath is the path to your project’s src directory, if you’re launching jdb from you project directory simply state -sourcepath src or -sourcepath ./src. Press return and you should be attached to your application.

 

参考:

http://bbs.pediy.com/showthread.php?t=157622

http://bbs.pediy.com/showthread.php?t=178659

posted on 2014-06-14 18:05  各各他  阅读(2529)  评论(0编辑  收藏  举报

导航