adb的代码不容易理解,因为包含3个功能:
1. device端的adbd
2. host端的server
3. host端的client
再加上需要兼容linux和windows,专门针对这两个平台的文件必须独立开来。
Makefile中的宏ADB_HOST用来控制代码是用于device端还是host端,定义了ADB_HOST时即表明你要生成运行在电脑上的adb.exe,否则就是设备端的adbd。
本文仅谈论adb源码中host端相关的部分,以1.0.31版为例。
程序入口在adb.cpp中:
int main(int argc, char **argv)
{
#if ADB_HOST
adb_sysdeps_init();
adb_trace_init();
D("Handling commandline()\n");
return adb_commandline(argc - 1, argv + 1);
#else
/* If adbd runs inside the emulator this will enable adb tracing via
* adb-debug qemud service in the emulator. */
adb_qemu_trace_init();
if((argc > 1) && (!strcmp(argv[1],"recovery"))) {
adb_device_banner = "recovery";
recovery_mode = 1;
}
start_device_log();
D("Handling main()\n");
return adb_main(0, DEFAULT_ADB_PORT);
#endif
}
adb_sysdeps_init()和adb_trace_init()初始化各种锁及trace相关的数组,
剥离了程序名后的参数会传入adb_commandline函数,这个函数是理解的关键。
int adb_commandline(int argc, char **argv)
{
char buf[4096];
int no_daemon = 0;
int is_daemon = 0;
int is_server = 0;
int persist = 0;
......
/* If defined, this should be an absolute path to
* the directory containing all of the various system images
* for a particular product. If not defined, and the adb
* command requires this information, then the user must
* specify the path using "-p".
*/
gProductOutPath = getenv("ANDROID_PRODUCT_OUT");
if (gProductOutPath == NULL || gProductOutPath[0] == '\0') {
gProductOutPath = NULL;
}
/* modifiers and flags */
while(argc > 0) {
if(!strcmp(argv[0],"server")) {
is_server = 1;
} else if(!strcmp(argv[0],"nodaemon")) {
no_daemon = 1;
} else if (!strcmp(argv[0], "fork-server")) {
/* this is a special flag used only when the ADB client launches the ADB Server */
is_daemon = 1;
} else if(!strcmp(argv[0],"persist")) {
persist = 1;
} else if(!strncmp(argv[0], "-p", 2)) {
...
gProductOutPath = find_product_out_path(product);
....
} else if (argv[0][0]=='-' && argv[0][1]=='s') {
...
} else if (!strcmp(argv[0],"-d")) {
ttype = kTransportUsb;
} else if (!strcmp(argv[0],"-e")) {
ttype = kTransportLocal;
} else {
/* out of recognized modifiers and flags */
break;
}
argc--;
argv++;
}
adb_set_transport(ttype, serial);
adb_set_tcp_specifics(server_port);
if (is_server) {
if (no_daemon || is_daemon) {
r = adb_main(is_daemon, server_port);
} else {
r = launch_server(server_port);
}
if(r) {
fprintf(stderr,"* could not start server *\n");
}
return r;
}
top:
....
}
这个函数以"top:"标签为界,分成了两部分。“top:”之上专门解析host server启动相关的参数,“top:”之下的是host client端参数。
host client的参数解析部分本文不讨论,无非就是通过do_sync_sync、do_cmd、adb_connect、adb_query之类的函数实现对应的功能,
需要说明的是,当client发现server未启动时,会通过launch_server函数来启动,launch_server通过Windows API CreateProcess调用adb.exe,带入的启动参数是fork-server server
top上面的部分,目的是设置is_server、no_daemon、is_daemon,其中is_server用来区分是host server还是host client,
no_daemon表明是否以无界面(后台)的形式运行server(为了处理调试用的命令adb nodaemon server),is_daemon为1时表明要后台运行,该参数由host client启动server时间接设置。
顺便提一下,gProductOutPath = getenv("ANDROID_PRODUCT_OUT");用来设置产品目录,设置了之后,adb可以用来在host和设备之间同步文件。