android adb源码分析(1)
ADB是Android debug bridge的缩写,它使用PC机可以通过USB或网络与android设备通讯。
adb的源码位于system/core/adb目录下,先来看下编译脚本Android.mk:
- # Copyright 2005 The Android Open Source Project
- #
- # Android.mk for adb
- #
- LOCAL_PATH:= $(call my-dir)
- # adb host tool
- # =========================================================
- include $(CLEAR_VARS)
- # Default to a virtual (sockets) usb interface
- USB_SRCS :=
- EXTRA_SRCS :=
- ifeq ($(HOST_OS),linux)
- USB_SRCS := usb_linux.c
- EXTRA_SRCS := get_my_path_linux.c
- LOCAL_LDLIBS += -lrt -ldl -lpthread
- LOCAL_CFLAGS += -DWORKAROUND_BUG6558362
- endif
- ifeq ($(HOST_OS),darwin)
- USB_SRCS := usb_osx.c
- EXTRA_SRCS := get_my_path_darwin.c
- LOCAL_LDLIBS += -lpthread -framework CoreFoundation -framework IOKit -framework Carbon
- endif
- ifeq ($(HOST_OS),freebsd)
- USB_SRCS := usb_libusb.c
- EXTRA_SRCS := get_my_path_freebsd.c
- LOCAL_LDLIBS += -lpthread -lusb
- endif
- ifeq ($(HOST_OS),windows)
- USB_SRCS := usb_windows.c
- EXTRA_SRCS := get_my_path_windows.c ../libcutils/list.c
- EXTRA_STATIC_LIBS := AdbWinApi
- ifneq ($(strip $(USE_CYGWIN)),)
- # Pure cygwin case
- LOCAL_LDLIBS += -lpthread -lgdi32
- LOCAL_C_INCLUDES += /usr/include/w32api/ddk
- endif
- ifneq ($(strip $(USE_MINGW)),)
- # MinGW under Linux case
- LOCAL_LDLIBS += -lws2_32 -lgdi32
- USE_SYSDEPS_WIN32 := 1
- LOCAL_C_INCLUDES += /usr/i586-mingw32msvc/include/ddk
- endif
- LOCAL_C_INCLUDES += development/host/windows/usb/api/
- endif
- LOCAL_SRC_FILES := \
- adb.c \
- console.c \
- transport.c \
- transport_local.c \
- transport_usb.c \
- commandline.c \
- adb_client.c \
- adb_auth_host.c \
- sockets.c \
- services.c \
- file_sync_client.c \
- $(EXTRA_SRCS) \
- $(USB_SRCS) \
- usb_vendors.c
- LOCAL_C_INCLUDES += external/openssl/include
- ifneq ($(USE_SYSDEPS_WIN32),)
- LOCAL_SRC_FILES += sysdeps_win32.c
- else
- LOCAL_SRC_FILES += fdevent.c
- endif
- LOCAL_CFLAGS += -O2 -g -DADB_HOST=1 -Wall -Wno-unused-parameter
- LOCAL_CFLAGS += -D_XOPEN_SOURCE -D_GNU_SOURCE
- LOCAL_MODULE := adb
- LOCAL_MODULE_TAGS := debug
- LOCAL_STATIC_LIBRARIES := libzipfile libunz libcrypto_static $(EXTRA_STATIC_LIBS)
- ifeq ($(USE_SYSDEPS_WIN32),)
- LOCAL_STATIC_LIBRARIES += libcutils
- endif
- include $(BUILD_HOST_EXECUTABLE)
- $(call dist-for-goals,dist_files sdk,$(LOCAL_BUILT_MODULE))
- ifeq ($(HOST_OS),windows)
- $(LOCAL_INSTALLED_MODULE): \
- $(HOST_OUT_EXECUTABLES)/AdbWinApi.dll \
- $(HOST_OUT_EXECUTABLES)/AdbWinUsbApi.dll
- endif
- # adbd device daemon
- # =========================================================
- include $(CLEAR_VARS)
- LOCAL_SRC_FILES := \
- adb.c \
- backup_service.c \
- fdevent.c \
- transport.c \
- transport_local.c \
- transport_usb.c \
- adb_auth_client.c \
- sockets.c \
- services.c \
- file_sync_service.c \
- jdwp_service.c \
- framebuffer_service.c \
- remount_service.c \
- usb_linux_client.c \
- log_service.c
- LOCAL_CFLAGS := -O2 -g -DADB_HOST=0 -Wall -Wno-unused-parameter
- LOCAL_CFLAGS += -D_XOPEN_SOURCE -D_GNU_SOURCE
- ifneq (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT)))
- LOCAL_CFLAGS += -DALLOW_ADBD_ROOT=1
- endif
- LOCAL_MODULE := adbd
- LOCAL_FORCE_STATIC_EXECUTABLE := true
- LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT_SBIN)
- LOCAL_UNSTRIPPED_PATH := $(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)
- LOCAL_STATIC_LIBRARIES := liblog libcutils libc libmincrypt
- include $(BUILD_EXECUTABLE)
- # adb host tool for device-as-host
- # =========================================================
- ifneq ($(SDK_ONLY),true)
- include $(CLEAR_VARS)
- LOCAL_LDLIBS := -lrt -ldl -lpthread
- LOCAL_SRC_FILES := \
- adb.c \
- console.c \
- transport.c \
- transport_local.c \
- transport_usb.c \
- commandline.c \
- adb_client.c \
- adb_auth_host.c \
- sockets.c \
- services.c \
- file_sync_client.c \
- get_my_path_linux.c \
- usb_linux.c \
- usb_vendors.c \
- fdevent.c
- LOCAL_CFLAGS := \
- -O2 \
- -g \
- -DADB_HOST=1 \
- -DADB_HOST_ON_TARGET=1 \
- -Wall \
- -Wno-unused-parameter \
- -D_XOPEN_SOURCE \
- -D_GNU_SOURCE
- LOCAL_C_INCLUDES += external/openssl/include
- LOCAL_MODULE := adb
- LOCAL_STATIC_LIBRARIES := libzipfile libunz libcutils
- LOCAL_SHARED_LIBRARIES := libcrypto
- include $(BUILD_EXECUTABLE)
- endif
可以看到,最终会有3个执行文件被生成,adbd和两个adb程序。adbd是手机终的守护进程;adb一个是windows、linux、darwin或freebsd运行的程序,另一个是目标机上运行的程序。
宏ADB_HOST用于区分是PC端程序还是目标机端的程序。宏ADB_HOST_ON_TARGET用于区分adb程序是否是在目标机上运行。这3个程序使用的是同一份源码,在内部,使用这些宏来区别不同的程序。
程序的入口在adb.c的main()函数:
- 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
- }
先来看adbd程序,此时宏的设置是ADB_HOST=0。上面代码中start_device_log()是log的初始化操作,可以重定向输出的log信息,接着进入adb_main()函数。先来看下它的参数DEFAULT_ADB_PORT:
- #if ADB_HOST_ON_TARGET
- /* adb and adbd are coexisting on the target, so use 5038 for adb
- * to avoid conflicting with adbd's usage of 5037
- */
- # define DEFAULT_ADB_PORT 5038
- #else
- # define DEFAULT_ADB_PORT 5037
- #endif
如果是目标机程序,它的值是5038,否则它的值是5037。这里没有定义ADB_HOST_ON_TARGET, 所以它是5037。
adb_main()的源代码如下:
- int adb_main(int is_daemon, int server_port)
- {
- #if !ADB_HOST
- int port;
- char value[PROPERTY_VALUE_MAX];
- umask(000);
- #endif
- atexit(adb_cleanup);
- #ifdef HAVE_WIN32_PROC
- SetConsoleCtrlHandler( ctrlc_handler, TRUE );
- #elif defined(HAVE_FORKEXEC)
- // No SIGCHLD. Let the service subproc handle its children.
- signal(SIGPIPE, SIG_IGN);
- #endif
- init_transport_registration();
- #if ADB_HOST
- HOST = 1;
- usb_vendors_init();
- usb_init();
- local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
- adb_auth_init();
- char local_name[30];
- build_local_name(local_name, sizeof(local_name), server_port);
- if(install_listener(local_name, "*smartsocket*", NULL)) {
- exit(1);
- }
- #else
- property_get("ro.adb.secure", value, "0");
- auth_enabled = !strcmp(value, "1");
- if (auth_enabled)
- adb_auth_init();
- // Our external storage path may be different than apps, since
- // we aren't able to bind mount after dropping root.
- const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE");
- if (NULL != adb_external_storage) {
- setenv("EXTERNAL_STORAGE", adb_external_storage, 1);
- } else {
- D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE"
- " unchanged.\n");
- }
- /* don't listen on a port (default 5037) if running in secure mode */
- /* don't run as root if we are running in secure mode */
- if (should_drop_privileges()) {
- struct __user_cap_header_struct header;
- struct __user_cap_data_struct cap;
- if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) != 0) {
- exit(1);
- }
- /* add extra groups:
- ** AID_ADB to access the USB driver
- ** AID_LOG to read system logs (adb logcat)
- ** AID_INPUT to diagnose input issues (getevent)
- ** AID_INET to diagnose network issues (netcfg, ping)
- ** AID_GRAPHICS to access the frame buffer
- ** AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
- ** AID_SDCARD_R to allow reading from the SD card
- ** AID_SDCARD_RW to allow writing to the SD card
- ** AID_MOUNT to allow unmounting the SD card before rebooting
- ** AID_NET_BW_STATS to read out qtaguid statistics
- */
- gid_t groups[] = { AID_ADB, AID_LOG, AID_INPUT, AID_INET, AID_GRAPHICS,
- AID_NET_BT, AID_NET_BT_ADMIN, AID_SDCARD_R, AID_SDCARD_RW,
- AID_MOUNT, AID_NET_BW_STATS };
- if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
- exit(1);
- }
- /* then switch user and group to "shell" */
- if (setgid(AID_SHELL) != 0) {
- exit(1);
- }
- if (setuid(AID_SHELL) != 0) {
- exit(1);
- }
- /* set CAP_SYS_BOOT capability, so "adb reboot" will succeed */
- header.version = _LINUX_CAPABILITY_VERSION;
- header.pid = 0;
- cap.effective = cap.permitted = (1 << CAP_SYS_BOOT);
- cap.inheritable = 0;
- capset(&header, &cap);
- D("Local port disabled\n");
- } else {
- char local_name[30];
- build_local_name(local_name, sizeof(local_name), server_port);
- if(install_listener(local_name, "*smartsocket*", NULL)) {
- exit(1);
- }
- }
- int usb = 0;
- if (access(USB_ADB_PATH, F_OK) == 0 || access(USB_FFS_ADB_EP0, F_OK) == 0) {
- // listen on USB
- usb_init();
- usb = 1;
- }
- // If one of these properties is set, also listen on that port
- // If one of the properties isn't set and we couldn't listen on usb,
- // listen on the default port.
- property_get("service.adb.tcp.port", value, "");
- if (!value[0]) {
- property_get("persist.adb.tcp.port", value, "");
- }
- if (sscanf(value, "%d", &port) == 1 && port > 0) {
- printf("using port=%d\n", port);
- // listen on TCP port specified by service.adb.tcp.port property
- local_init(port);
- } else if (!usb) {
- // listen on default port
- local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
- }
- D("adb_main(): pre init_jdwp()\n");
- init_jdwp();
- D("adb_main(): post init_jdwp()\n");
- #endif
- if (is_daemon)
- {
- // inform our parent that we are up and running.
- #ifdef HAVE_WIN32_PROC
- DWORD count;
- WriteFile( GetStdHandle( STD_OUTPUT_HANDLE ), "OK\n", 3, &count, NULL );
- #elif defined(HAVE_FORKEXEC)
- fprintf(stderr, "OK\n");
- #endif
- start_logging();
- }
- D("Event loop starting\n");
- fdevent_loop();
- usb_cleanup();
- return 0;
- }
(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):
- 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;
- int r;
- int quote;
- transport_type ttype = kTransportAny;
- char* serial = NULL;
- char* server_port_str = NULL;
- /* 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;
- }
- // TODO: also try TARGET_PRODUCT/TARGET_DEVICE as a hint
- serial = getenv("ANDROID_SERIAL");
- /* Validate and assign the server port */
- server_port_str = getenv("ANDROID_ADB_SERVER_PORT");
- int server_port = DEFAULT_ADB_PORT;
- if (server_port_str && strlen(server_port_str) > 0) {
- server_port = (int) strtol(server_port_str, NULL, 0);
- if (server_port <= 0) {
- fprintf(stderr,
- "adb: Env var ANDROID_ADB_SERVER_PORT must be a positive number. Got \"%s\"\n",
- server_port_str);
- return usage();
- }
- }
- /* 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)) {
- const char *product = NULL;
- if (argv[0][2] == '\0') {
- if (argc < 2) return usage();
- product = argv[1];
- argc--;
- argv++;
- } else {
- product = argv[0] + 2;
- }
- gProductOutPath = find_product_out_path(product);
- if (gProductOutPath == NULL) {
- fprintf(stderr, "adb: could not resolve \"-p %s\"\n",
- product);
- return usage();
- }
- } else if (argv[0][0]=='-' && argv[0][1]=='s') {
- if (isdigit(argv[0][2])) {
- serial = argv[0] + 2;
- } else {
- if(argc < 2 || argv[0][2] != '\0') return usage();
- serial = argv[1];
- argc--;
- argv++;
- }
- } 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:
- if(argc == 0) {
- return usage();
- }
- /* adb_connect() commands */
- if(!strcmp(argv[0], "devices")) {
- char *tmp;
- char *listopt;
- if (argc < 2)
- listopt = "";
- else if (argc == 2 && !strcmp(argv[1], "-l"))
- listopt = argv[1];
- else {
- fprintf(stderr, "Usage: adb devices [-l]\n");
- return 1;
- }
- snprintf(buf, sizeof buf, "host:%s%s", argv[0], listopt);
- tmp = adb_query(buf);
- if(tmp) {
- printf("List of devices attached \n");
- printf("%s\n", tmp);
- return 0;
- } else {
- return 1;
- }
- }
- if(!strcmp(argv[0], "connect")) {
- char *tmp;
- if (argc != 2) {
- fprintf(stderr, "Usage: adb connect <host>[:<port>]\n");
- return 1;
- }
- snprintf(buf, sizeof buf, "host:connect:%s", argv[1]);
- tmp = adb_query(buf);
- if(tmp) {
- printf("%s\n", tmp);
- return 0;
- } else {
- return 1;
- }
- }
- if(!strcmp(argv[0], "disconnect")) {
- char *tmp;
- if (argc > 2) {
- fprintf(stderr, "Usage: adb disconnect [<host>[:<port>]]\n");
- return 1;
- }
- if (argc == 2) {
- snprintf(buf, sizeof buf, "host:disconnect:%s", argv[1]);
- } else {
- snprintf(buf, sizeof buf, "host:disconnect:");
- }
- tmp = adb_query(buf);
- if(tmp) {
- printf("%s\n", tmp);
- return 0;
- } else {
- return 1;
- }
- }
- if (!strcmp(argv[0], "emu")) {
- return adb_send_emulator_command(argc, argv);
- }
- if(!strcmp(argv[0], "shell") || !strcmp(argv[0], "hell")) {
- int r;
- int fd;
- char h = (argv[0][0] == 'h');
- if (h) {
- printf("\x1b[41;33m");
- fflush(stdout);
- }
- if(argc < 2) {
- D("starting interactive shell\n");
- r = interactive_shell();
- if (h) {
- printf("\x1b[0m");
- fflush(stdout);
- }
- return r;
- }
- snprintf(buf, sizeof buf, "shell:%s", argv[1]);
- argc -= 2;
- argv += 2;
- while(argc-- > 0) {
- strcat(buf, " ");
- /* quote empty strings and strings with spaces */
- quote = (**argv == 0 || strchr(*argv, ' '));
- if (quote)
- strcat(buf, "\"");
- strcat(buf, *argv++);
- if (quote)
- strcat(buf, "\"");
- }
- for(;;) {
- D("interactive shell loop. buff=%s\n", buf);
- fd = adb_connect(buf);
- if(fd >= 0) {
- D("about to read_and_dump(fd=%d)\n", fd);
- read_and_dump(fd);
- D("read_and_dump() done.\n");
- adb_close(fd);
- r = 0;
- } else {
- fprintf(stderr,"error: %s\n", adb_error());
- r = -1;
- }
- if(persist) {
- fprintf(stderr,"\n- waiting for device -\n");
- adb_sleep_ms(1000);
- do_cmd(ttype, serial, "wait-for-device", 0);
- } else {
- if (h) {
- printf("\x1b[0m");
- fflush(stdout);
- }
- D("interactive shell loop. return r=%d\n", r);
- return r;
- }
- }
- }
- if(!strcmp(argv[0], "kill-server")) {
- int fd;
- fd = _adb_connect("host:kill");
- if(fd == -1) {
- fprintf(stderr,"* server not running *\n");
- return 1;
- }
- return 0;
- }
- if(!strcmp(argv[0], "sideload")) {
- if(argc != 2) return usage();
- if(adb_download("sideload", argv[1], 1)) {
- return 1;
- } else {
- return 0;
- }
- }
- if(!strcmp(argv[0], "remount") || !strcmp(argv[0], "reboot")
- || !strcmp(argv[0], "reboot-bootloader")
- || !strcmp(argv[0], "tcpip") || !strcmp(argv[0], "usb")
- || !strcmp(argv[0], "root")) {
- char command[100];
- if (!strcmp(argv[0], "reboot-bootloader"))
- snprintf(command, sizeof(command), "reboot:bootloader");
- else if (argc > 1)
- snprintf(command, sizeof(command), "%s:%s", argv[0], argv[1]);
- else
- snprintf(command, sizeof(command), "%s:", argv[0]);
- int fd = adb_connect(command);
- if(fd >= 0) {
- read_and_dump(fd);
- adb_close(fd);
- return 0;
- }
- fprintf(stderr,"error: %s\n", adb_error());
- return 1;
- }
- if(!strcmp(argv[0], "bugreport")) {
- if (argc != 1) return usage();
- do_cmd(ttype, serial, "shell", "bugreport", 0);
- return 0;
- }
- /* adb_command() wrapper commands */
- if(!strncmp(argv[0], "wait-for-", strlen("wait-for-"))) {
- char* service = argv[0];
- if (!strncmp(service, "wait-for-device", strlen("wait-for-device"))) {
- if (ttype == kTransportUsb) {
- service = "wait-for-usb";
- } else if (ttype == kTransportLocal) {
- service = "wait-for-local";
- } else {
- service = "wait-for-any";
- }
- }
- format_host_command(buf, sizeof buf, service, ttype, serial);
- if (adb_command(buf)) {
- D("failure: %s *\n",adb_error());
- fprintf(stderr,"error: %s\n", adb_error());
- return 1;
- }
- /* Allow a command to be run after wait-for-device,
- * e.g. 'adb wait-for-device shell'.
- */
- if(argc > 1) {
- argc--;
- argv++;
- goto top;
- }
- return 0;
- }
- if(!strcmp(argv[0], "forward")) {
- if(argc != 3) return usage();
- if (serial) {
- snprintf(buf, sizeof buf, "host-serial:%s:forward:%s;%s",serial, argv[1], argv[2]);
- } else if (ttype == kTransportUsb) {
- snprintf(buf, sizeof buf, "host-usb:forward:%s;%s", argv[1], argv[2]);
- } else if (ttype == kTransportLocal) {
- snprintf(buf, sizeof buf, "host-local:forward:%s;%s", argv[1], argv[2]);
- } else {
- snprintf(buf, sizeof buf, "host:forward:%s;%s", argv[1], argv[2]);
- }
- if(adb_command(buf)) {
- fprintf(stderr,"error: %s\n", adb_error());
- return 1;
- }
- return 0;
- }
- /* do_sync_*() commands */
- if(!strcmp(argv[0], "ls")) {
- if(argc != 2) return usage();
- return do_sync_ls(argv[1]);
- }
- if(!strcmp(argv[0], "push")) {
- if(argc != 3) return usage();
- return do_sync_push(argv[1], argv[2], 0 /* no verify APK */);
- }
- if(!strcmp(argv[0], "pull")) {
- if (argc == 2) {
- return do_sync_pull(argv[1], ".");
- } else if (argc == 3) {
- return do_sync_pull(argv[1], argv[2]);
- } else {
- return usage();
- }
- }
- if(!strcmp(argv[0], "install")) {
- if (argc < 2) return usage();
- return install_app(ttype, serial, argc, argv);
- }
- if(!strcmp(argv[0], "uninstall")) {
- if (argc < 2) return usage();
- return uninstall_app(ttype, serial, argc, argv);
- }
- if(!strcmp(argv[0], "sync")) {
- char *srcarg, *android_srcpath, *data_srcpath;
- int listonly = 0;
- int ret;
- if(argc < 2) {
- /* No local path was specified. */
- srcarg = NULL;
- } else if (argc >= 2 && strcmp(argv[1], "-l") == 0) {
- listonly = 1;
- if (argc == 3) {
- srcarg = argv[2];
- } else {
- srcarg = NULL;
- }
- } else if(argc == 2) {
- /* A local path or "android"/"data" arg was specified. */
- srcarg = argv[1];
- } else {
- return usage();
- }
- ret = find_sync_dirs(srcarg, &android_srcpath, &data_srcpath);
- if(ret != 0) return usage();
- if(android_srcpath != NULL)
- ret = do_sync_sync(android_srcpath, "/system", listonly);
- if(ret == 0 && data_srcpath != NULL)
- ret = do_sync_sync(data_srcpath, "/data", listonly);
- free(android_srcpath);
- free(data_srcpath);
- return ret;
- }
- /* passthrough commands */
- if(!strcmp(argv[0],"get-state") ||
- !strcmp(argv[0],"get-serialno") ||
- !strcmp(argv[0],"get-devpath"))
- {
- char *tmp;
- format_host_command(buf, sizeof buf, argv[0], ttype, serial);
- tmp = adb_query(buf);
- if(tmp) {
- printf("%s\n", tmp);
- return 0;
- } else {
- return 1;
- }
- }
- /* other commands */
- if(!strcmp(argv[0],"status-window")) {
- status_window(ttype, serial);
- return 0;
- }
- if(!strcmp(argv[0],"logcat") || !strcmp(argv[0],"lolcat") || !strcmp(argv[0],"longcat")) {
- return logcat(ttype, serial, argc, argv);
- }
- if(!strcmp(argv[0],"ppp")) {
- return ppp(argc, argv);
- }
- if (!strcmp(argv[0], "start-server")) {
- return adb_connect("host:start-server");
- }
- if (!strcmp(argv[0], "backup")) {
- return backup(argc, argv);
- }
- if (!strcmp(argv[0], "restore")) {
- return restore(argc, argv);
- }
- if (!strcmp(argv[0], "jdwp")) {
- int fd = adb_connect("jdwp");
- if (fd >= 0) {
- read_and_dump(fd);
- adb_close(fd);
- return 0;
- } else {
- fprintf(stderr, "error: %s\n", adb_error());
- return -1;
- }
- }
- /* "adb /?" is a common idiom under Windows */
- if(!strcmp(argv[0], "help") || !strcmp(argv[0], "/?")) {
- help();
- return 0;
- }
- if(!strcmp(argv[0], "version")) {
- version(stdout);
- return 0;
- }
- usage();
- return 1;
- }
它的代码有点长,但可以很容易看明白它根据命令的参数执行不同的功能。adb程序可能以服务的方式或命令行客户端的方式运行。
----------
以上就是main()函数的执行过程,后续将分析其中的细节内容。
android adb源码分析(1)的更多相关文章
- android adb源码分析(5)【转】
本文转载自:http://blog.csdn.net/xgbing/article/details/52096880 本篇以“adb devices"命令为例,跟踪代码的执行流程. (1) ...
- Appium Android Bootstrap源码分析之启动运行
通过前面的两篇文章<Appium Android Bootstrap源码分析之控件AndroidElement>和<Appium Android Bootstrap源码分析之命令解析 ...
- ADB 源码分析(一) ——ADB模块简述【转】
ADB源码分析(一)——ADB模块简述 1.Adb 源码路径(system/core/adb). 2.要想很快的了解一个模块的基本情况,最直接的就是查看该模块的Android.mk文件,下面就来看看a ...
- Appium Android Bootstrap源码分析之命令解析执行
通过上一篇文章<Appium Android Bootstrap源码分析之控件AndroidElement>我们知道了Appium从pc端发送过来的命令如果是控件相关的话,最终目标控件在b ...
- Appium Android Bootstrap源码分析之控件AndroidElement
通过上一篇文章<Appium Android Bootstrap源码分析之简介>我们对bootstrap的定义以及其在appium和uiautomator处于一个什么样的位置有了一个初步的 ...
- Android HandlerThread 源码分析
HandlerThread 简介: 我们知道Thread线程是一次性消费品,当Thread线程执行完一个耗时的任务之后,线程就会被自动销毁了.如果此时我又有一 个耗时任务需要执行,我们不得不重新创建线 ...
- Android Choreographer 源码分析
Choreographer 的作用主要是配合 Vsync ,给上层 App 的渲染提供一个稳定的 Message 处理的时机,也就是 Vsync 到来的时候 ,系统通过对 Vsync 信号周期的调整, ...
- Appium Android Bootstrap源码分析之简介
在上一个系列中我们分析了UiAutomator的核心源码,对UiAutomator是怎么运行的原理有了根本的了解.今天我们会开始另外一个在安卓平台上基于UiAutomator的新起之秀--Appium ...
- Android base-adapter-helper 源码分析与扩展
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/44014941,本文出自:[张鸿洋的博客] 本篇博客是我加入Android 开源项 ...
随机推荐
- ftp服务及其实现之vsftpd
本文最早发布于我的51CTO博客,目前已迁移至博客园. 简介 FTP,File Transfer Protocol,文件传输协议 文件服务器,具备存储和共享文件(权限设置合理的情况下)的功能 命令端口 ...
- 解决Windows10下无法对docker容器进行端口访问(端口映射的问题)
解决Windows10下无法对docker容器进行端口访问(端口映射的问题) 问题详情 在Windows10系统服务器中安装了docker和docker-compose 并尝试在其中运行Nginx服务 ...
- 快速排序之Java实现
快速排序之Java实现 代码: package cn.com.zfc.lesson21.sort; /** * * @title QuickSort * @describe 快速排序 * @autho ...
- FireDAC 下的 Sqlite [1] - 前言
很长时间没静下心来写博客了, 现在回来, 是 Delphi 不断地进步让我感动.振奋. Delphi XE5 并入了 FireDAC, 第一印象非常好, 恐怕 dbExpress 等等都要靠边站了. ...
- 一个.net程序客户端更新方案
客户端程序一个很大的不便的地方就是程序集更新,本文这里简单的介绍一种通用的客户端更新方案.这个方案依赖程序集的动态加载,具体方案如下: 将程序集存储在一个文件数据库中,客户端所有程序集直接从文件数据库 ...
- HDU 4763 Theme Section (2013长春网络赛1005,KMP)
Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- Programming 2D Games 读书笔记(第四章)
示例一:Game Engine Part 1 更加完善游戏的基本流程 Graphics添加了以下几个方法,beginScene和endScene提高绘图,showBackbuffer去掉了clea ...
- [web.config]如何灵活使用配置文件
摘要 在实际项目中,经常遇到比较多的环境,比如开发环境,测试环境,生产环境.对于这些环境,可能会有不同接口调用,不同的数据库连接字符串等等.那么该如何实现不同环境的参数快速切换呢?当然,最笨的方式就是 ...
- Snmp学习总结系列——开篇
进入公司以来,一直参与到公司的产品研发工作当中去,在产品研发中有一个监控远程服务器CPU使用率,内存使用情况,硬盘的需求,技术总监提出了使用Snmp协议作为远程监控的技术解决方案,头一次听说Snmp这 ...
- 移植Python3到TQ2440(二)
接着前一篇博文. 在上一篇博文中我们用NFS挂载根文件系统的方式启动了系统,接下来我们把移植了Python3的根文件系统固化到NandFlash中,但是由于linux-4.9目前不支持Yaffs2文件 ...