How ADB works
**ADB (Android Debug Bridge): How it works? **
2012.2.6 early draft
Tetsuyuki Kobayashi
What is ADB?
- If you are an Android builder, you have used
adb logcat
,adb shell
. - Even if you only use DDMS in Eclipse, adb is working under the hood.
- Using adb, you can connect emulator or actual target device.
- "adb kill-server"? what does it mean?
How to connect?
2 roles of ADB
- Providing "Transport"
- communication path between host and target device.
- USE or TCP: but clients don't have to aware.
- Providing "Services"
- executing something on the target devices through the transport.
adb shell
for executing commandadb push/pull
for file transfer
- executing something on the target devices through the transport.
3 elements of ADB
- adb clients
- executable with subcommand
adb shell
,adb logcat
: the end point of host side
- adb server
- running on host on back-ground
- act as proxy between adb clients and adbd
- adb daemon (adbd)
- running on target device
- started by init, if die, restarted by init again.
When does adb server start?
- Explicitly
adb start-server
- It starts adb server as back-ground process.
- Usually it does automatically on demand. You don't have to do
adb start-server
. - When you want to restart adb server, do
adb kill-server
. - Actually, adb clients and adb server shares same executable
adb start-server
equalsadb fork-server server &
ADB internal
- Source code
- How to get ADB logs
- Sequence chart
- Simple ruby script to connect adb server
- Command details
- Secure mode
- Add USB Vendor ID
- Switching transport mode
Source code
- system/core/adb in Android source tree
- From this directory adb and adbd are built
- Don't confuse
- comman files between adb and adbd
- adb.c, fdevent.c, transort.c transport_local.c, transport_usb.c, service.c, sockets.c, util.c
- files only for adbd
- backup_service.c, file_sync_service.c, jdwp_service.c, framebuffer_service.c, remount_services.c, usb_linux_clients.c, log_service.c
- files only for adb
- console.c, adb_clients.c, file_sync_client.c, usb_vendors.c, get_my_path_{linux, darwin, windows, freebsd}.c, usb_{linux, macos, libusb, windows}.c
#if ADB_HOST
/ * code for adb */
#else
/* code for adbd */
#endif
How to get ADB logs
- For adb clients and adb server, set environment variable ADB_TRACE
- For adbd, set system property
persist.adb.trace_mask
.
Sequence chart
Simple ruby script to connect to adb server
require 'socket'
def error_exit
puts "Error"
exit 1
end
def send_to_adb(s, msg)
s.printf("%04x%s", msg.length, msg)
end
def check_okay(s)
(s.read(4) == "OKAY")
end
def check_version(s)
(s.read(12) == "OKAY004001d")
end
hostname = 'localhost'
port = 5037
s = TCPSocket.open(hostname, port)
send_to_adb(s, "host:version")
error_exit if ! check_version(s)
s.close
s = TCPSocket.open(hostname, port)
send_to_adb(s, "host:transport-any")
error_exit if ! check_okay(s)
send_to_adb(s, "shell:ls")
error_exit if ! check_okay(s)
while line = s.gets
puts line.chop
end
s.close
change "shell:ls" as you like.
Command details
- adb logcat
- adb install/uninstall
- adb reboot
- screen capture from DDMS
Secure mode
- Android smart phone products have adbd. Usually it runs on secure mode. (secure = 1)
- if secure == 1, change adbd as SHELL user(= not privileged), else it keeps running as root user
- In secure mode, all serivces invoked by adbd ran as SHELL user. Some causes "permission denied".
How secure mode decided
- Running on emulator -> secure = 0
- System property "ro.secure" == 1 -> secure = 1
- if "ro.debuggable" == 1, you can restart adb unsecure by "adb root"
- All Android phone products are shipped in "ro.secure" = 1, "ro.debuggable" = 0.
- See adb.c: adb_main
Add USB Vendor ID
- When connecting USB device, adb checks USB Vendor ID
- Many USB Vendor IDs are hard coded in adb.(But not enough)
- To add USB Vendor ID, make "$HOME/.adnroid/adb_usb.ini" and write one ID in one line.
- See usb_vendors.c
Switching transport mode
Switching USB mode to TCP mode
$ adb shell netcfg
lo UP 127.0.0.1 255.0.0.0 0x00000049
eth0 UP 192.168.1.139 255.255.255.0 0x00001043
$ adb tcpip 5555
restarting in TCP mode port : 5555
$ adb devices
List of devices attached
disconnected from USB. Then restart adb server with specifying target IP address.
$ adb kill-server
$ ADBHOST=192.16.8.1.139 adb devices
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
List of evices attached
emulator-5554 device
Switching transport mode (What happen inside?)
- See service.c restart_tcp_service
- property_set("service.adb.tcp.port", value);
- Note: before Android 4.0, this cause "permission denied" in secure mode and ignored silently!
- After that, exit(1);
- init restarts adbd.
- "service.adb.tcp.port" is checked in adb.c adb_main
Tips
- adb emu
- adb backup/restore
- Joke commands
- Modify emulatro to allow any server socket
adb emu
- You can send a single command to emulator console easily
- send only, can not receive.
- For emulator console: http://developer.android.com/guide/developing/devices/emulator.html
- Simple example. "adb emu window scale 0.5" after starting emulator.
adb backup/restore
- New in Android 4.0
- You can backup/restore installed applications with their saved status.
Joke commands
- adb shell
- same as "adb shell" except "hell" color
- Just try.
- adb lolcat
- same as "adb logcat"
Modify emulator to allow any server socket
All server sockets in Android emulator accepts only from localhost.
If you feel inconvenient in this restriction, apply the patch in next page.
Patch to allow any server socket(for experience)
diff --git a/slirp-android/socket.c b/slirp-android/socket.c
index 439590a..ed16d5a 100644
--- a/slirp-android/socket.c
+++ b/slirp-android/socket.c
@@ -650,7 +650,7 @@ solisten(u_int port, u_int32_t laddr, u_int lport, int
flags)
so->so_laddr_ip = laddr; /* Ditto */
so->so_haddr_port = port;
- s = socket_loopback_server( port, SOCKET_STREAM );
+ s = socket_inaddr_any_server( port, SOCKET_STREAM );
if (s < 0)
return NULL;
diff --git a/sockets.c b/sockets.c
index 1063339..55b5d57 100644
--- a/sockets.c
+++ b/sockets.c
@@ -1337,6 +1337,11 @@ socket_in_client( SockAddress* to, SocketType type )
return socket_connect_client( s, to );
}
+int
+socket_inaddr_any_server( int port, SocketType type )
+{
+ return socket_in_server( INADDR_ANY, port, type );
+}
int
socket_loopback_server( int port, SocketType type )
Advanced Topics
- adb in Android device
- Port adbd to other than Android
adb in Android device
- Usually, adb is running on the host side, such as Linux, MacOS and Windows
- In Android 4.0 there is /system/bin/adb in Android file system
- What for is this?
Connect its own adbd by adb
- Restart adbd in TCP mode
- Then type "adb devices"
- It can connect its own adbd by local loop back
Connect other Android device by adb on Android
At the serial console on KZM-A9-Dual board
# adb devices
* daemon not running. starting it now on port 5038 *
* daemon started successfully *
List of devices attached
HT015P803242 device
#
It worked!
You can do "adb shell", "adb logcat", too.
Even you can install application from the board to NexusOne by "adb install foo.apk".
Port adbd to other than Android
- Quick hack!
- Consider dependency for better porting.
Quick hack!
- /sbin/adbd is statically linked executable.
- Just copy this file to ARM Ubuntu 11.10 just for experience (using Android patched kernel)
- Somehow, it worked without any recompilation
- sudo chmod 666 /dev/android_adb*
- make symbolic link /bin/sh to /system/bin/sh
- adbd runs in secure mode
- It worked "adb shell", "adb push/pull"
Consider dependency for better porting
- Some service requires other android commands
- adb install/uninstall, adb bugreport
- framebuffer service invokes /system/in/screeencap
- Adbd uses Android property system
- At the binary experiment, all property_get returns default values.
- That's why adbd ran in secure mode.
- At the binary experiment, all property_get returns default values.
- Switching adbd mode assumed that init restarts adbd process.
How ADB works的更多相关文章
- Android adb 命令使用总结
adb原理 参考文档 How ADB works http://www.cnblogs.com/ifantastic/p/5186362.html http://blog.csdn.ne ...
- Android Debug Bridge
Android Debug Bridge Introduction Android Debug Bridge (adb) is a versatile command line tool th ...
- Remove openjdk in Ubuntu/Configure jdk and running adb in 64-bit Ubuntu
sudo apt-get autoremove openjdk-7-jre sudo apt-get purge openjdk* java -version No openjdk available ...
- 一月份实现Adb小程序
As Brian said: According to a post on xda-developers, you can enable ADB over WiFi from the device w ...
- 转 Android adb root权限
永久root带文件 因为开发需要,我经常会用到adb这个工具(Android Debug Bridge),我们都知道adb shell默认是没有root权限的,修改系统文件就很不方便了,adb pus ...
- adb概览及协议參考
原文:https://github.com/android/platform_system_core/blob/master/adb/OVERVIEW.TXT) Implementation note ...
- adb概览及协议参考
原文:https://github.com/android/platform_system_core/blob/master/adb/OVERVIEW.TXT) Implementation note ...
- Android KitCat 4.4.2 ADB 官方所支持的所有Services格式翻译
在之前的文章中有转帖网上同行制作的ADB协议表格<<adb概览及协议参考>>,但不够详尽,所以这里自己另外基于Android 4.4.2的技术文档重新做一次翻译. HOST S ...
- 在调试安卓系统的时候需要这个 ”adb disable-verity“
在调试设备的时候.想要对文件进行读写 于是使用adb remount 出现提示. 请使用 ”adb disable-verity“ 于是使用adb disable-verity 的命令. 得到如下 ...
随机推荐
- Python面试题(一)
**晚上在公司的论坛上看到一道面试题,题目如下:随机给定一字符串和字符,要求重排,比如:'abde','c'.重排之后变成'abcde' **看到他们给的答案很多都是二分法重排,既然是字符类的处理,当 ...
- js系列(10)js的运用(二)
本节继续介绍在html页面中js的运用. (1)数码时钟:(http://files.cnblogs.com/files/MenAngel/text05.zip) <!DOCTYPE ...
- GO語言基礎教程:序章
首先自我介紹一下我自己,我是一個coder,目前主要從事B/S程序開發工作,懂點PHP;ASP;JSP;JS;VB;C;DELPHI;JAVA,另外知道幾個數據庫,除此之外別無所長,那麼我為何會選擇學 ...
- 我用了13行代碼開發出来的PHP框架
我只用13行代碼開發的PHP框架,如果您對框架不理解,不知道框架究竟幫您做了什麽事,可以下載此框架看一下, 另外如果您想開發自己的框架也可以由這個框架的思路進行擴展. 源碼下載地址:http://do ...
- 了解 JavaScript (5)– 翻转器(rollover)
用 JavaScript 最常用的效果就是,当用户将鼠标移动到图片上时,会改变网页上的图像,这样页面就能对用户的操作及时作出反应,这种称为 翻转器(rollover)效果很容易实现,而且有很多应用场合 ...
- Design / UX Consultation
Looking for a bit of creative inspiration, perhaps? Then get assistance with your app or project by ...
- 关闭/开启 ubuntu 自动更新提示
发现vps登陆后只有apt update后才知道有多少包需要更新不是很傻么,本地的ubuntu在登录时就有很好的提示,并且还能告知系统负载情况,很有用,这里就想开起来.首先这个提示的名字叫Motd. ...
- 一个批量移除BOM头的bash脚本
有时候我们的文件可能不需要BOM头,例如:我们公司的SVN服务器提供的代码都UTF8编码保存(不能有BOM头)否则代码提交不上去. 文件很多的时候就需要批量操作. 脚本使用方法:remove-bom. ...
- libevent
libevent doc example #include <event2/event.h> void cb_func(evutil_socket_t fd, short what, vo ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...