打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
android adb源码分析(1)
版权声明:☆☆☆原创文章,转载请注明来自: https://blog.csdn.net/xgbing/article/details/52058390

ADB是Android debug bridge的缩写,它使用PC机可以通过USB或网络与android设备通讯。

adb的源码位于system/core/adb目录下,先来看下编译脚本Android.mk:

  1. # Copyright 2005 The Android Open Source Project
  2. #
  3. # Android.mk for adb
  4. #
  5. LOCAL_PATH:= $(call my-dir)
  6. # adb host tool
  7. # =========================================================
  8. include $(CLEAR_VARS)
  9. # Default to a virtual (sockets) usb interface
  10. USB_SRCS :=
  11. EXTRA_SRCS :=
  12. ifeq ($(HOST_OS),linux)
  13. USB_SRCS := usb_linux.c
  14. EXTRA_SRCS := get_my_path_linux.c
  15. LOCAL_LDLIBS += -lrt -ldl -lpthread
  16. LOCAL_CFLAGS += -DWORKAROUND_BUG6558362
  17. endif
  18. ifeq ($(HOST_OS),darwin)
  19. USB_SRCS := usb_osx.c
  20. EXTRA_SRCS := get_my_path_darwin.c
  21. LOCAL_LDLIBS += -lpthread -framework CoreFoundation -framework IOKit -framework Carbon
  22. endif
  23. ifeq ($(HOST_OS),freebsd)
  24. USB_SRCS := usb_libusb.c
  25. EXTRA_SRCS := get_my_path_freebsd.c
  26. LOCAL_LDLIBS += -lpthread -lusb
  27. endif
  28. ifeq ($(HOST_OS),windows)
  29. USB_SRCS := usb_windows.c
  30. EXTRA_SRCS := get_my_path_windows.c ../libcutils/list.c
  31. EXTRA_STATIC_LIBS := AdbWinApi
  32. ifneq ($(strip $(USE_CYGWIN)),)
  33. # Pure cygwin case
  34. LOCAL_LDLIBS += -lpthread -lgdi32
  35. LOCAL_C_INCLUDES += /usr/include/w32api/ddk
  36. endif
  37. ifneq ($(strip $(USE_MINGW)),)
  38. # MinGW under Linux case
  39. LOCAL_LDLIBS += -lws2_32 -lgdi32
  40. USE_SYSDEPS_WIN32 := 1
  41. LOCAL_C_INCLUDES += /usr/i586-mingw32msvc/include/ddk
  42. endif
  43. LOCAL_C_INCLUDES += development/host/windows/usb/api/
  44. endif
  45. LOCAL_SRC_FILES := \
  46. adb.c \
  47. console.c \
  48. transport.c \
  49. transport_local.c \
  50. transport_usb.c \
  51. commandline.c \
  52. adb_client.c \
  53. adb_auth_host.c \
  54. sockets.c \
  55. services.c \
  56. file_sync_client.c \
  57. $(EXTRA_SRCS) \
  58. $(USB_SRCS) \
  59. usb_vendors.c
  60. LOCAL_C_INCLUDES += external/openssl/include
  61. ifneq ($(USE_SYSDEPS_WIN32),)
  62. LOCAL_SRC_FILES += sysdeps_win32.c
  63. else
  64. LOCAL_SRC_FILES += fdevent.c
  65. endif
  66. LOCAL_CFLAGS += -O2 -g -DADB_HOST=1 -Wall -Wno-unused-parameter
  67. LOCAL_CFLAGS += -D_XOPEN_SOURCE -D_GNU_SOURCE
  68. LOCAL_MODULE := adb
  69. LOCAL_MODULE_TAGS := debug
  70. LOCAL_STATIC_LIBRARIES := libzipfile libunz libcrypto_static $(EXTRA_STATIC_LIBS)
  71. ifeq ($(USE_SYSDEPS_WIN32),)
  72. LOCAL_STATIC_LIBRARIES += libcutils
  73. endif
  74. include $(BUILD_HOST_EXECUTABLE)
  75. $(call dist-for-goals,dist_files sdk,$(LOCAL_BUILT_MODULE))
  76. ifeq ($(HOST_OS),windows)
  77. $(LOCAL_INSTALLED_MODULE): \
  78. $(HOST_OUT_EXECUTABLES)/AdbWinApi.dll \
  79. $(HOST_OUT_EXECUTABLES)/AdbWinUsbApi.dll
  80. endif
  81. # adbd device daemon
  82. # =========================================================
  83. include $(CLEAR_VARS)
  84. LOCAL_SRC_FILES := \
  85. adb.c \
  86. backup_service.c \
  87. fdevent.c \
  88. transport.c \
  89. transport_local.c \
  90. transport_usb.c \
  91. adb_auth_client.c \
  92. sockets.c \
  93. services.c \
  94. file_sync_service.c \
  95. jdwp_service.c \
  96. framebuffer_service.c \
  97. remount_service.c \
  98. usb_linux_client.c \
  99. log_service.c
  100. LOCAL_CFLAGS := -O2 -g -DADB_HOST=0 -Wall -Wno-unused-parameter
  101. LOCAL_CFLAGS += -D_XOPEN_SOURCE -D_GNU_SOURCE
  102. ifneq (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT)))
  103. LOCAL_CFLAGS += -DALLOW_ADBD_ROOT=1
  104. endif
  105. LOCAL_MODULE := adbd
  106. LOCAL_FORCE_STATIC_EXECUTABLE := true
  107. LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT_SBIN)
  108. LOCAL_UNSTRIPPED_PATH := $(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)
  109. LOCAL_STATIC_LIBRARIES := liblog libcutils libc libmincrypt
  110. include $(BUILD_EXECUTABLE)
  111. # adb host tool for device-as-host
  112. # =========================================================
  113. ifneq ($(SDK_ONLY),true)
  114. include $(CLEAR_VARS)
  115. LOCAL_LDLIBS := -lrt -ldl -lpthread
  116. LOCAL_SRC_FILES := \
  117. adb.c \
  118. console.c \
  119. transport.c \
  120. transport_local.c \
  121. transport_usb.c \
  122. commandline.c \
  123. adb_client.c \
  124. adb_auth_host.c \
  125. sockets.c \
  126. services.c \
  127. file_sync_client.c \
  128. get_my_path_linux.c \
  129. usb_linux.c \
  130. usb_vendors.c \
  131. fdevent.c
  132. LOCAL_CFLAGS := \
  133. -O2 \
  134. -g \
  135. -DADB_HOST=1 \
  136. -DADB_HOST_ON_TARGET=1 \
  137. -Wall \
  138. -Wno-unused-parameter \
  139. -D_XOPEN_SOURCE \
  140. -D_GNU_SOURCE
  141. LOCAL_C_INCLUDES += external/openssl/include
  142. LOCAL_MODULE := adb
  143. LOCAL_STATIC_LIBRARIES := libzipfile libunz libcutils
  144. LOCAL_SHARED_LIBRARIES := libcrypto
  145. include $(BUILD_EXECUTABLE)
  146. endif

可以看到,最终会有3个执行文件被生成,adbd和两个adb程序。adbd是手机终的守护进程;adb一个是windows、linux、darwin或freebsd运行的程序,另一个是目标机上运行的程序。

    宏ADB_HOST用于区分是PC端程序还是目标机端的程序。宏ADB_HOST_ON_TARGET用于区分adb程序是否是在目标机上运行。这3个程序使用的是同一份源码,在内部,使用这些宏来区别不同的程序。

    程序的入口在adb.c的main()函数:

  1. int main(int argc, char **argv)
  2. {
  3. #if ADB_HOST
  4. adb_sysdeps_init();
  5. adb_trace_init();
  6. D("Handling commandline()\n");
  7. return adb_commandline(argc - 1, argv + 1);
  8. #else
  9. /* If adbd runs inside the emulator this will enable adb tracing via
  10. * adb-debug qemud service in the emulator. */
  11. adb_qemu_trace_init();
  12. if((argc > 1) && (!strcmp(argv[1],"recovery"))) {
  13. adb_device_banner = "recovery";
  14. recovery_mode = 1;
  15. }
  16. start_device_log();
  17. D("Handling main()\n");
  18. return adb_main(0, DEFAULT_ADB_PORT);
  19. #endif
  20. }

先来看adbd程序,此时宏的设置是ADB_HOST=0。上面代码中start_device_log()是log的初始化操作,可以重定向输出的log信息,接着进入adb_main()函数。先来看下它的参数DEFAULT_ADB_PORT:

  1. #if ADB_HOST_ON_TARGET
  2. /* adb and adbd are coexisting on the target, so use 5038 for adb
  3. * to avoid conflicting with adbd's usage of 5037
  4. */
  5. # define DEFAULT_ADB_PORT 5038
  6. #else
  7. # define DEFAULT_ADB_PORT 5037
  8. #endif
如果是目标机程序,它的值是5038,否则它的值是5037。这里没有定义ADB_HOST_ON_TARGET, 所以它是5037。

adb_main()的源代码如下:

  1. int adb_main(int is_daemon, int server_port)
  2. {
  3. #if !ADB_HOST
  4. int port;
  5. char value[PROPERTY_VALUE_MAX];
  6. umask(000);
  7. #endif
  8. atexit(adb_cleanup);
  9. #ifdef HAVE_WIN32_PROC
  10. SetConsoleCtrlHandler( ctrlc_handler, TRUE );
  11. #elif defined(HAVE_FORKEXEC)
  12. // No SIGCHLD. Let the service subproc handle its children.
  13. signal(SIGPIPE, SIG_IGN);
  14. #endif
  15. init_transport_registration();
  16. #if ADB_HOST
  17. HOST = 1;
  18. usb_vendors_init();
  19. usb_init();
  20. local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
  21. adb_auth_init();
  22. char local_name[30];
  23. build_local_name(local_name, sizeof(local_name), server_port);
  24. if(install_listener(local_name, "*smartsocket*", NULL)) {
  25. exit(1);
  26. }
  27. #else
  28. property_get("ro.adb.secure", value, "0");
  29. auth_enabled = !strcmp(value, "1");
  30. if (auth_enabled)
  31. adb_auth_init();
  32. // Our external storage path may be different than apps, since
  33. // we aren't able to bind mount after dropping root.
  34. const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE");
  35. if (NULL != adb_external_storage) {
  36. setenv("EXTERNAL_STORAGE", adb_external_storage, 1);
  37. } else {
  38. D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE"
  39. " unchanged.\n");
  40. }
  41. /* don't listen on a port (default 5037) if running in secure mode */
  42. /* don't run as root if we are running in secure mode */
  43. if (should_drop_privileges()) {
  44. struct __user_cap_header_struct header;
  45. struct __user_cap_data_struct cap;
  46. if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) != 0) {
  47. exit(1);
  48. }
  49. /* add extra groups:
  50. ** AID_ADB to access the USB driver
  51. ** AID_LOG to read system logs (adb logcat)
  52. ** AID_INPUT to diagnose input issues (getevent)
  53. ** AID_INET to diagnose network issues (netcfg, ping)
  54. ** AID_GRAPHICS to access the frame buffer
  55. ** AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
  56. ** AID_SDCARD_R to allow reading from the SD card
  57. ** AID_SDCARD_RW to allow writing to the SD card
  58. ** AID_MOUNT to allow unmounting the SD card before rebooting
  59. ** AID_NET_BW_STATS to read out qtaguid statistics
  60. */
  61. gid_t groups[] = { AID_ADB, AID_LOG, AID_INPUT, AID_INET, AID_GRAPHICS,
  62. AID_NET_BT, AID_NET_BT_ADMIN, AID_SDCARD_R, AID_SDCARD_RW,
  63. AID_MOUNT, AID_NET_BW_STATS };
  64. if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
  65. exit(1);
  66. }
  67. /* then switch user and group to "shell" */
  68. if (setgid(AID_SHELL) != 0) {
  69. exit(1);
  70. }
  71. if (setuid(AID_SHELL) != 0) {
  72. exit(1);
  73. }
  74. /* set CAP_SYS_BOOT capability, so "adb reboot" will succeed */
  75. header.version = _LINUX_CAPABILITY_VERSION;
  76. header.pid = 0;
  77. cap.effective = cap.permitted = (1 << CAP_SYS_BOOT);
  78. cap.inheritable = 0;
  79. capset(&header, &cap);
  80. D("Local port disabled\n");
  81. } else {
  82. char local_name[30];
  83. build_local_name(local_name, sizeof(local_name), server_port);
  84. if(install_listener(local_name, "*smartsocket*", NULL)) {
  85. exit(1);
  86. }
  87. }
  88. int usb = 0;
  89. if (access(USB_ADB_PATH, F_OK) == 0 || access(USB_FFS_ADB_EP0, F_OK) == 0) {
  90. // listen on USB
  91. usb_init();
  92. usb = 1;
  93. }
  94. // If one of these properties is set, also listen on that port
  95. // If one of the properties isn't set and we couldn't listen on usb,
  96. // listen on the default port.
  97. property_get("service.adb.tcp.port", value, "");
  98. if (!value[0]) {
  99. property_get("persist.adb.tcp.port", value, "");
  100. }
  101. if (sscanf(value, "%d", &port) == 1 && port > 0) {
  102. printf("using port=%d\n", port);
  103. // listen on TCP port specified by service.adb.tcp.port property
  104. local_init(port);
  105. } else if (!usb) {
  106. // listen on default port
  107. local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
  108. }
  109. D("adb_main(): pre init_jdwp()\n");
  110. init_jdwp();
  111. D("adb_main(): post init_jdwp()\n");
  112. #endif
  113. if (is_daemon)
  114. {
  115. // inform our parent that we are up and running.
  116. #ifdef HAVE_WIN32_PROC
  117. DWORD count;
  118. WriteFile( GetStdHandle( STD_OUTPUT_HANDLE ), "OK\n", 3, &count, NULL );
  119. #elif defined(HAVE_FORKEXEC)
  120. fprintf(stderr, "OK\n");
  121. #endif
  122. start_logging();
  123. }
  124. D("Event loop starting\n");
  125. fdevent_loop();
  126. usb_cleanup();
  127. return 0;
  128. }

(1) init_transport_registration()初始化fevent transport_registration_fde;

(2) 判断系统属性ro.adb.secure,目标板没有设置这个宏;

(3) 没有定义环境变量adb_external_storage;

(4) should_drop_privileges()根据android编译环境should_drop_privileges返回不同的值,如果它的值是userdebug或eng,宏ALLOW_ADBD_ROOT的值被定义为1,执行install_listener(),否则不会定义,这种情况下,由于adbd运行在root下,为保证它的安全性,它需要降级运行;

(5) 判断是否存在设备文件USB_ADB_PATH或USB_FFS_ADB_EP0,存在则执行usb_init();

(6) 读取属性service.adb.tcp.port或persist.adb.tcp.port(目标板均未定义),执行local_init(),它内部会创建adb thread;

(7) 执行init_jdwp(),jdwp是java调试体系中的一种,具体可百度;

(8) 调用fdevent_loop()监听fdevent并处理;

(9) 程序结束。


再来看adb程序的执行:

在main()中调用return adb_commandline(argc - 1, argv + 1):

  1. int adb_commandline(int argc, char **argv)
  2. {
  3. char buf[4096];
  4. int no_daemon = 0;
  5. int is_daemon = 0;
  6. int is_server = 0;
  7. int persist = 0;
  8. int r;
  9. int quote;
  10. transport_type ttype = kTransportAny;
  11. char* serial = NULL;
  12. char* server_port_str = NULL;
  13. /* If defined, this should be an absolute path to
  14. * the directory containing all of the various system images
  15. * for a particular product. If not defined, and the adb
  16. * command requires this information, then the user must
  17. * specify the path using "-p".
  18. */
  19. gProductOutPath = getenv("ANDROID_PRODUCT_OUT");
  20. if (gProductOutPath == NULL || gProductOutPath[0] == '\0') {
  21. gProductOutPath = NULL;
  22. }
  23. // TODO: also try TARGET_PRODUCT/TARGET_DEVICE as a hint
  24. serial = getenv("ANDROID_SERIAL");
  25. /* Validate and assign the server port */
  26. server_port_str = getenv("ANDROID_ADB_SERVER_PORT");
  27. int server_port = DEFAULT_ADB_PORT;
  28. if (server_port_str && strlen(server_port_str) > 0) {
  29. server_port = (int) strtol(server_port_str, NULL, 0);
  30. if (server_port <= 0) {
  31. fprintf(stderr,
  32. "adb: Env var ANDROID_ADB_SERVER_PORT must be a positive number. Got \"%s\"\n",
  33. server_port_str);
  34. return usage();
  35. }
  36. }
  37. /* modifiers and flags */
  38. while(argc > 0) {
  39. if(!strcmp(argv[0],"server")) {
  40. is_server = 1;
  41. } else if(!strcmp(argv[0],"nodaemon")) {
  42. no_daemon = 1;
  43. } else if (!strcmp(argv[0], "fork-server")) {
  44. /* this is a special flag used only when the ADB client launches the ADB Server */
  45. is_daemon = 1;
  46. } else if(!strcmp(argv[0],"persist")) {
  47. persist = 1;
  48. } else if(!strncmp(argv[0], "-p", 2)) {
  49. const char *product = NULL;
  50. if (argv[0][2] == '\0') {
  51. if (argc < 2) return usage();
  52. product = argv[1];
  53. argc--;
  54. argv++;
  55. } else {
  56. product = argv[0] + 2;
  57. }
  58. gProductOutPath = find_product_out_path(product);
  59. if (gProductOutPath == NULL) {
  60. fprintf(stderr, "adb: could not resolve \"-p %s\"\n",
  61. product);
  62. return usage();
  63. }
  64. } else if (argv[0][0]=='-' && argv[0][1]=='s') {
  65. if (isdigit(argv[0][2])) {
  66. serial = argv[0] + 2;
  67. } else {
  68. if(argc < 2 || argv[0][2] != '\0') return usage();
  69. serial = argv[1];
  70. argc--;
  71. argv++;
  72. }
  73. } else if (!strcmp(argv[0],"-d")) {
  74. ttype = kTransportUsb;
  75. } else if (!strcmp(argv[0],"-e")) {
  76. ttype = kTransportLocal;
  77. } else {
  78. /* out of recognized modifiers and flags */
  79. break;
  80. }
  81. argc--;
  82. argv++;
  83. }
  84. adb_set_transport(ttype, serial);
  85. adb_set_tcp_specifics(server_port);
  86. if (is_server) {
  87. if (no_daemon || is_daemon) {
  88. r = adb_main(is_daemon, server_port);
  89. } else {
  90. r = launch_server(server_port);
  91. }
  92. if(r) {
  93. fprintf(stderr,"* could not start server *\n");
  94. }
  95. return r;
  96. }
  97. top:
  98. if(argc == 0) {
  99. return usage();
  100. }
  101. /* adb_connect() commands */
  102. if(!strcmp(argv[0], "devices")) {
  103. char *tmp;
  104. char *listopt;
  105. if (argc < 2)
  106. listopt = "";
  107. else if (argc == 2 && !strcmp(argv[1], "-l"))
  108. listopt = argv[1];
  109. else {
  110. fprintf(stderr, "Usage: adb devices [-l]\n");
  111. return 1;
  112. }
  113. snprintf(buf, sizeof buf, "host:%s%s", argv[0], listopt);
  114. tmp = adb_query(buf);
  115. if(tmp) {
  116. printf("List of devices attached \n");
  117. printf("%s\n", tmp);
  118. return 0;
  119. } else {
  120. return 1;
  121. }
  122. }
  123. if(!strcmp(argv[0], "connect")) {
  124. char *tmp;
  125. if (argc != 2) {
  126. fprintf(stderr, "Usage: adb connect <host>[:<port>]\n");
  127. return 1;
  128. }
  129. snprintf(buf, sizeof buf, "host:connect:%s", argv[1]);
  130. tmp = adb_query(buf);
  131. if(tmp) {
  132. printf("%s\n", tmp);
  133. return 0;
  134. } else {
  135. return 1;
  136. }
  137. }
  138. if(!strcmp(argv[0], "disconnect")) {
  139. char *tmp;
  140. if (argc > 2) {
  141. fprintf(stderr, "Usage: adb disconnect [<host>[:<port>]]\n");
  142. return 1;
  143. }
  144. if (argc == 2) {
  145. snprintf(buf, sizeof buf, "host:disconnect:%s", argv[1]);
  146. } else {
  147. snprintf(buf, sizeof buf, "host:disconnect:");
  148. }
  149. tmp = adb_query(buf);
  150. if(tmp) {
  151. printf("%s\n", tmp);
  152. return 0;
  153. } else {
  154. return 1;
  155. }
  156. }
  157. if (!strcmp(argv[0], "emu")) {
  158. return adb_send_emulator_command(argc, argv);
  159. }
  160. if(!strcmp(argv[0], "shell") || !strcmp(argv[0], "hell")) {
  161. int r;
  162. int fd;
  163. char h = (argv[0][0] == 'h');
  164. if (h) {
  165. printf("\x1b[41;33m");
  166. fflush(stdout);
  167. }
  168. if(argc < 2) {
  169. D("starting interactive shell\n");
  170. r = interactive_shell();
  171. if (h) {
  172. printf("\x1b[0m");
  173. fflush(stdout);
  174. }
  175. return r;
  176. }
  177. snprintf(buf, sizeof buf, "shell:%s", argv[1]);
  178. argc -= 2;
  179. argv += 2;
  180. while(argc-- > 0) {
  181. strcat(buf, " ");
  182. /* quote empty strings and strings with spaces */
  183. quote = (**argv == 0 || strchr(*argv, ' '));
  184. if (quote)
  185. strcat(buf, "\"");
  186. strcat(buf, *argv++);
  187. if (quote)
  188. strcat(buf, "\"");
  189. }
  190. for(;;) {
  191. D("interactive shell loop. buff=%s\n", buf);
  192. fd = adb_connect(buf);
  193. if(fd >= 0) {
  194. D("about to read_and_dump(fd=%d)\n", fd);
  195. read_and_dump(fd);
  196. D("read_and_dump() done.\n");
  197. adb_close(fd);
  198. r = 0;
  199. } else {
  200. fprintf(stderr,"error: %s\n", adb_error());
  201. r = -1;
  202. }
  203. if(persist) {
  204. fprintf(stderr,"\n- waiting for device -\n");
  205. adb_sleep_ms(1000);
  206. do_cmd(ttype, serial, "wait-for-device", 0);
  207. } else {
  208. if (h) {
  209. printf("\x1b[0m");
  210. fflush(stdout);
  211. }
  212. D("interactive shell loop. return r=%d\n", r);
  213. return r;
  214. }
  215. }
  216. }
  217. if(!strcmp(argv[0], "kill-server")) {
  218. int fd;
  219. fd = _adb_connect("host:kill");
  220. if(fd == -1) {
  221. fprintf(stderr,"* server not running *\n");
  222. return 1;
  223. }
  224. return 0;
  225. }
  226. if(!strcmp(argv[0], "sideload")) {
  227. if(argc != 2) return usage();
  228. if(adb_download("sideload", argv[1], 1)) {
  229. return 1;
  230. } else {
  231. return 0;
  232. }
  233. }
  234. if(!strcmp(argv[0], "remount") || !strcmp(argv[0], "reboot")
  235. || !strcmp(argv[0], "reboot-bootloader")
  236. || !strcmp(argv[0], "tcpip") || !strcmp(argv[0], "usb")
  237. || !strcmp(argv[0], "root")) {
  238. char command[100];
  239. if (!strcmp(argv[0], "reboot-bootloader"))
  240. snprintf(command, sizeof(command), "reboot:bootloader");
  241. else if (argc > 1)
  242. snprintf(command, sizeof(command), "%s:%s", argv[0], argv[1]);
  243. else
  244. snprintf(command, sizeof(command), "%s:", argv[0]);
  245. int fd = adb_connect(command);
  246. if(fd >= 0) {
  247. read_and_dump(fd);
  248. adb_close(fd);
  249. return 0;
  250. }
  251. fprintf(stderr,"error: %s\n", adb_error());
  252. return 1;
  253. }
  254. if(!strcmp(argv[0], "bugreport")) {
  255. if (argc != 1) return usage();
  256. do_cmd(ttype, serial, "shell", "bugreport", 0);
  257. return 0;
  258. }
  259. /* adb_command() wrapper commands */
  260. if(!strncmp(argv[0], "wait-for-", strlen("wait-for-"))) {
  261. char* service = argv[0];
  262. if (!strncmp(service, "wait-for-device", strlen("wait-for-device"))) {
  263. if (ttype == kTransportUsb) {
  264. service = "wait-for-usb";
  265. } else if (ttype == kTransportLocal) {
  266. service = "wait-for-local";
  267. } else {
  268. service = "wait-for-any";
  269. }
  270. }
  271. format_host_command(buf, sizeof buf, service, ttype, serial);
  272. if (adb_command(buf)) {
  273. D("failure: %s *\n",adb_error());
  274. fprintf(stderr,"error: %s\n", adb_error());
  275. return 1;
  276. }
  277. /* Allow a command to be run after wait-for-device,
  278. * e.g. 'adb wait-for-device shell'.
  279. */
  280. if(argc > 1) {
  281. argc--;
  282. argv++;
  283. goto top;
  284. }
  285. return 0;
  286. }
  287. if(!strcmp(argv[0], "forward")) {
  288. if(argc != 3) return usage();
  289. if (serial) {
  290. snprintf(buf, sizeof buf, "host-serial:%s:forward:%s;%s",serial, argv[1], argv[2]);
  291. } else if (ttype == kTransportUsb) {
  292. snprintf(buf, sizeof buf, "host-usb:forward:%s;%s", argv[1], argv[2]);
  293. } else if (ttype == kTransportLocal) {
  294. snprintf(buf, sizeof buf, "host-local:forward:%s;%s", argv[1], argv[2]);
  295. } else {
  296. snprintf(buf, sizeof buf, "host:forward:%s;%s", argv[1], argv[2]);
  297. }
  298. if(adb_command(buf)) {
  299. fprintf(stderr,"error: %s\n", adb_error());
  300. return 1;
  301. }
  302. return 0;
  303. }
  304. /* do_sync_*() commands */
  305. if(!strcmp(argv[0], "ls")) {
  306. if(argc != 2) return usage();
  307. return do_sync_ls(argv[1]);
  308. }
  309. if(!strcmp(argv[0], "push")) {
  310. if(argc != 3) return usage();
  311. return do_sync_push(argv[1], argv[2], 0 /* no verify APK */);
  312. }
  313. if(!strcmp(argv[0], "pull")) {
  314. if (argc == 2) {
  315. return do_sync_pull(argv[1], ".");
  316. } else if (argc == 3) {
  317. return do_sync_pull(argv[1], argv[2]);
  318. } else {
  319. return usage();
  320. }
  321. }
  322. if(!strcmp(argv[0], "install")) {
  323. if (argc < 2) return usage();
  324. return install_app(ttype, serial, argc, argv);
  325. }
  326. if(!strcmp(argv[0], "uninstall")) {
  327. if (argc < 2) return usage();
  328. return uninstall_app(ttype, serial, argc, argv);
  329. }
  330. if(!strcmp(argv[0], "sync")) {
  331. char *srcarg, *android_srcpath, *data_srcpath;
  332. int listonly = 0;
  333. int ret;
  334. if(argc < 2) {
  335. /* No local path was specified. */
  336. srcarg = NULL;
  337. } else if (argc >= 2 && strcmp(argv[1], "-l") == 0) {
  338. listonly = 1;
  339. if (argc == 3) {
  340. srcarg = argv[2];
  341. } else {
  342. srcarg = NULL;
  343. }
  344. } else if(argc == 2) {
  345. /* A local path or "android"/"data" arg was specified. */
  346. srcarg = argv[1];
  347. } else {
  348. return usage();
  349. }
  350. ret = find_sync_dirs(srcarg, &android_srcpath, &data_srcpath);
  351. if(ret != 0) return usage();
  352. if(android_srcpath != NULL)
  353. ret = do_sync_sync(android_srcpath, "/system", listonly);
  354. if(ret == 0 && data_srcpath != NULL)
  355. ret = do_sync_sync(data_srcpath, "/data", listonly);
  356. free(android_srcpath);
  357. free(data_srcpath);
  358. return ret;
  359. }
  360. /* passthrough commands */
  361. if(!strcmp(argv[0],"get-state") ||
  362. !strcmp(argv[0],"get-serialno") ||
  363. !strcmp(argv[0],"get-devpath"))
  364. {
  365. char *tmp;
  366. format_host_command(buf, sizeof buf, argv[0], ttype, serial);
  367. tmp = adb_query(buf);
  368. if(tmp) {
  369. printf("%s\n", tmp);
  370. return 0;
  371. } else {
  372. return 1;
  373. }
  374. }
  375. /* other commands */
  376. if(!strcmp(argv[0],"status-window")) {
  377. status_window(ttype, serial);
  378. return 0;
  379. }
  380. if(!strcmp(argv[0],"logcat") || !strcmp(argv[0],"lolcat") || !strcmp(argv[0],"longcat")) {
  381. return logcat(ttype, serial, argc, argv);
  382. }
  383. if(!strcmp(argv[0],"ppp")) {
  384. return ppp(argc, argv);
  385. }
  386. if (!strcmp(argv[0], "start-server")) {
  387. return adb_connect("host:start-server");
  388. }
  389. if (!strcmp(argv[0], "backup")) {
  390. return backup(argc, argv);
  391. }
  392. if (!strcmp(argv[0], "restore")) {
  393. return restore(argc, argv);
  394. }
  395. if (!strcmp(argv[0], "jdwp")) {
  396. int fd = adb_connect("jdwp");
  397. if (fd >= 0) {
  398. read_and_dump(fd);
  399. adb_close(fd);
  400. return 0;
  401. } else {
  402. fprintf(stderr, "error: %s\n", adb_error());
  403. return -1;
  404. }
  405. }
  406. /* "adb /?" is a common idiom under Windows */
  407. if(!strcmp(argv[0], "help") || !strcmp(argv[0], "/?")) {
  408. help();
  409. return 0;
  410. }
  411. if(!strcmp(argv[0], "version")) {
  412. version(stdout);
  413. return 0;
  414. }
  415. usage();
  416. return 1;
  417. }

它的代码有点长,但可以很容易看明白它根据命令的参数执行不同的功能。adb程序可能以服务的方式或命令行客户端的方式运行。

----------

以上就是main()函数的执行过程,后续将分析其中的细节内容。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
通过 WIFI 用 adb 调试 Android 设备
android wifi热点代码流程跟踪分析
Android-vold源码分析之runCommand(7)
PX4模块设计之四十五:param模块
测试flash之小工具
iproute2
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服