adb研究

adb如何运行?

研究之...

首先

进入sourcecode/system/core/adb

mm -B >build.log

编译的log如下:

============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=4.1.2
TARGET_PRODUCT=cm_iyokan
TARGET_BUILD_VARIANT=userdebug
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm
TARGET_ARCH_VARIANT=armv7-a-neon
HOST_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-3.2.58-x86_64-with-Ubuntu-14.04-trusty
HOST_BUILD_TYPE=release
BUILD_ID=JZO54K
OUT_DIR=/work/cyanogenmod/out
============================================
PRODUCT_COPY_FILES device/semc/iyokan/config/qwerty.idc:system/usr/idc/qwerty.idc ignored.
PRODUCT_COPY_FILES device/semc/iyokan/config/qwerty2.idc:system/usr/idc/qwerty2.idc ignored.
No private recovery resources for TARGET_DEVICE iyokan
make:进入目录'/work/cyanogenmod'
[36mImport includes file:[0m /work/cyanogenmod/out/host/linux-x86/obj/EXECUTABLES/adb_intermediates/import_includes
[33mhost C:[0m adb <= system/core/adb/adb.c
[33mhost C:[0m adb <= system/core/adb/console.c
[33mhost C:[0m adb <= system/core/adb/transport.c
[33mhost C:[0m adb <= system/core/adb/transport_local.c
[33mhost C:[0m adb <= system/core/adb/transport_usb.c
[33mhost C:[0m adb <= system/core/adb/commandline.c
[33mhost C:[0m adb <= system/core/adb/adb_client.c
[33mhost C:[0m adb <= system/core/adb/sockets.c
[33mhost C:[0m adb <= system/core/adb/services.c
[33mhost C:[0m adb <= system/core/adb/file_sync_client.c
[33mhost C:[0m adb <= system/core/adb/get_my_path_linux.c
[33mhost C:[0m adb <= system/core/adb/usb_linux.c
[33mhost C:[0m adb <= system/core/adb/utils.c
[33mhost C:[0m adb <= system/core/adb/usb_vendors.c
[33mhost C:[0m adb <= system/core/adb/fdevent.c
[36mExport includes file:[0m system/core/adb/Android.mk -- /work/cyanogenmod/out/host/linux-x86/obj/EXECUTABLES/adb_intermediates/export_includes
[33mhost Executable:[0m adb (/work/cyanogenmod/out/host/linux-x86/obj/EXECUTABLES/adb_intermediates/adb)
true
[36mNotice file:[0m system/core/adb/NOTICE -- /work/cyanogenmod/out/host/linux-x86/obj/NOTICE_FILES/src//bin/adb.txt
[36mInstall: /work/cyanogenmod/out/host/linux-x86/bin/adb[0m
[36mImport includes file:[0m /work/cyanogenmod/out/target/product/iyokan/obj/EXECUTABLES/adb_intermediates/import_includes
[32mtarget thumb C:[0m adb <= system/core/adb/adb.c
[32mtarget thumb C:[0m adb <= system/core/adb/console.c
[32mtarget thumb C:[0m adb <= system/core/adb/transport.c
[32mtarget thumb C:[0m adb <= system/core/adb/transport_local.c
[32mtarget thumb C:[0m adb <= system/core/adb/transport_usb.c
[32mtarget thumb C:[0m adb <= system/core/adb/commandline.c
[32mtarget thumb C:[0m adb <= system/core/adb/adb_client.c
[32mtarget thumb C:[0m adb <= system/core/adb/sockets.c
[32mtarget thumb C:[0m adb <= system/core/adb/services.c
[32mtarget thumb C:[0m adb <= system/core/adb/file_sync_client.c
[32mtarget thumb C:[0m adb <= system/core/adb/get_my_path_linux.c
[32mtarget thumb C:[0m adb <= system/core/adb/usb_linux.c
[32mtarget thumb C:[0m adb <= system/core/adb/utils.c
[32mtarget thumb C:[0m adb <= system/core/adb/usb_vendors.c
[32mtarget thumb C:[0m adb <= system/core/adb/fdevent.c
[32mtarget Executable:[0m adb (/work/cyanogenmod/out/target/product/iyokan/obj/EXECUTABLES/adb_intermediates/LINKED/adb)
[32mtarget Symbolic:[0m adb (/work/cyanogenmod/out/target/product/iyokan/symbols/system/bin/adb)
[36mExport includes file:[0m system/core/adb/Android.mk -- /work/cyanogenmod/out/target/product/iyokan/obj/EXECUTABLES/adb_intermediates/export_includes
[32mtarget Strip:[0m adb (/work/cyanogenmod/out/target/product/iyokan/obj/EXECUTABLES/adb_intermediates/adb)
[36mNotice file:[0m system/core/adb/NOTICE -- /work/cyanogenmod/out/target/product/iyokan/obj/NOTICE_FILES/src//system/bin/adb.txt
[36mInstall: /work/cyanogenmod/out/target/product/iyokan/system/bin/adb[0m
[36mImport includes file:[0m /work/cyanogenmod/out/target/product/iyokan/obj/EXECUTABLES/adbd_intermediates/import_includes
[32mtarget thumb C:[0m adbd <= system/core/adb/adb.c
[32mtarget thumb C:[0m adbd <= system/core/adb/backup_service.c
[32mtarget thumb C:[0m adbd <= system/core/adb/fdevent.c
[32mtarget thumb C:[0m adbd <= system/core/adb/transport.c
[32mtarget thumb C:[0m adbd <= system/core/adb/transport_local.c
[32mtarget thumb C:[0m adbd <= system/core/adb/transport_usb.c
[32mtarget thumb C:[0m adbd <= system/core/adb/sockets.c
[32mtarget thumb C:[0m adbd <= system/core/adb/services.c
[32mtarget thumb C:[0m adbd <= system/core/adb/file_sync_service.c
[32mtarget thumb C:[0m adbd <= system/core/adb/jdwp_service.c
[32mtarget thumb C:[0m adbd <= system/core/adb/framebuffer_service.c
[32mtarget thumb C:[0m adbd <= system/core/adb/remount_service.c
[32mtarget thumb C:[0m adbd <= system/core/adb/usb_linux_client.c
[32mtarget thumb C:[0m adbd <= system/core/adb/log_service.c
[32mtarget thumb C:[0m adbd <= system/core/adb/utils.c
[32mtarget StaticExecutable:[0m adbd (/work/cyanogenmod/out/target/product/iyokan/obj/EXECUTABLES/adbd_intermediates/LINKED/adbd)
[32mtarget Symbolic:[0m adbd (/work/cyanogenmod/out/target/product/iyokan/symbols/sbin/adbd)
[36mExport includes file:[0m system/core/adb/Android.mk -- /work/cyanogenmod/out/target/product/iyokan/obj/EXECUTABLES/adbd_intermediates/export_includes
[32mtarget Strip:[0m adbd (/work/cyanogenmod/out/target/product/iyokan/obj/EXECUTABLES/adbd_intermediates/adbd)
[36mNotice file:[0m system/core/adb/NOTICE -- /work/cyanogenmod/out/target/product/iyokan/obj/NOTICE_FILES/src//root/sbin/adbd.txt
[36mInstall: /work/cyanogenmod/out/target/product/iyokan/root/sbin/adbd[0m
make:离开目录“/work/cyanogenmod”


查看其make文件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 -lncurses -lpthread
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
  EXTRA_STATIC_LIBS := AdbWinApi
  ifneq ($(strip $(USE_CYGWIN)),)
    # Pure cygwin case
    LOCAL_LDLIBS += -lpthread
    LOCAL_C_INCLUDES += /usr/include/w32api/ddk
  endif
  ifneq ($(strip $(USE_MINGW)),)
    # MinGW under Linux case
    LOCAL_LDLIBS += -lws2_32
    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 \
sockets.c \
services.c \
file_sync_client.c \
$(EXTRA_SRCS) \
$(USB_SRCS) \
utils.c \
usb_vendors.c




ifneq ($(USE_SYSDEPS_WIN32),)
  LOCAL_SRC_FILES += sysdeps_win32.c
else
  LOCAL_SRC_FILES += fdevent.c
endif


LOCAL_CFLAGS += -g -DADB_HOST=1  -Wall -Wno-unused-parameter
# adb can't be built without optimizations, so we enforce -O2 if no
# other optimization flag is set - but we don't override what the global
# flags are saying if something else is given (-Os or -O3 are useful)
ifeq ($(findstring -O, $(HOST_GLOBAL_CFLAGS)),)
LOCAL_CFLAGS += -O2
endif
ifneq ($(findstring -O0, $(HOST_GLOBAL_CFLAGS)),)
LOCAL_CFLAGS += -O2
endif
LOCAL_CFLAGS += -D_XOPEN_SOURCE -D_GNU_SOURCE
LOCAL_MODULE := adb


LOCAL_STATIC_LIBRARIES := libzipfile libunz $(EXTRA_STATIC_LIBS)
ifeq ($(USE_SYSDEPS_WIN32),)
LOCAL_STATIC_LIBRARIES += libcutils
endif


include $(BUILD_HOST_EXECUTABLE)


$(call dist-for-goals,dist_files,$(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 \
sockets.c \
services.c \
file_sync_service.c \
jdwp_service.c \
framebuffer_service.c \
remount_service.c \
usb_linux_client.c \
log_service.c \
utils.c


LOCAL_CFLAGS := -g -DADB_HOST=0 -Wall -Wno-unused-parameter
# adb can't be built without optimizations, so we enforce -O2 if no
# other optimization flag is set - but we don't override what the global
# flags are saying if something else is given (-Os or -O3 are useful)
ifeq ($(findstring -O, $(TARGET_GLOBAL_CFLAGS)),)
LOCAL_CFLAGS += -O2
endif
ifneq ($(findstring -O0, $(TARGET_GLOBAL_CFLAGS)),)
LOCAL_CFLAGS += -O2
endif
LOCAL_CFLAGS += -D_XOPEN_SOURCE -D_GNU_SOURCE


ifneq (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT)))
LOCAL_CFLAGS += -DALLOW_ADBD_ROOT=1
endif


ifeq ($(BOARD_ALWAYS_INSECURE),true)
LOCAL_CFLAGS += -DBOARD_ALWAYS_INSECURE
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 := libcutils libc
include $(BUILD_EXECUTABLE)




# adb host tool for device-as-host
# =========================================================
ifneq ($(SDK_ONLY),true)
include $(CLEAR_VARS)


LOCAL_LDLIBS := -lrt -lncurses -lpthread


LOCAL_SRC_FILES := \
adb.c \
console.c \
transport.c \
transport_local.c \
transport_usb.c \
commandline.c \
adb_client.c \
sockets.c \
services.c \
file_sync_client.c \
get_my_path_linux.c \
usb_linux.c \
utils.c \
usb_vendors.c \
fdevent.c


LOCAL_CFLAGS := \
-g \
-DADB_HOST=1 \
-DADB_HOST_ON_TARGET=1 \
-Wall \
-Wno-unused-parameter \
-D_XOPEN_SOURCE \
-D_GNU_SOURCE


# adb can't be built without optimizations, so we enforce -O2 if no
# other optimization flag is set - but we don't override what the global
# flags are saying if something else is given (-Os or -O3 are useful)
ifeq ($(findstring -O, $(TARGET_GLOBAL_CFLAGS)),)
LOCAL_CFLAGS += -O2
endif
ifneq ($(findstring -O0, $(TARGET_GLOBAL_CFLAGS)),)
LOCAL_CFLAGS += -O2
endif


LOCAL_MODULE := adb


LOCAL_STATIC_LIBRARIES := libzipfile libunz libcutils


include $(BUILD_EXECUTABLE)
endif


根据Makefile可知adb会生成Host端的adb和target端的adbd和adb三部分:

即生成的

1.Notice file: system/core/adb/NOTICE -- /work/cyanogenmod/out/host/linux-x86/obj/NOTICE_FILES/src//bin/adb.txt
Install: /work/cyanogenmod/out/host/linux-x86/bin/adb

2.Notice file: system/core/adb/NOTICE -- /work/cyanogenmod/out/target/product/iyokan/obj/NOTICE_FILES/src//system/bin/adb.txt
Install: /work/cyanogenmod/out/target/product/iyokan/system/bin/adb

3.Notice file: system/core/adb/NOTICE -- /work/cyanogenmod/out/target/product/iyokan/obj/NOTICE_FILES/src//root/sbin/adbd.txt
Install: /work/cyanogenmod/out/target/product/iyokan/root/sbin/adbd


adb的功能是通过usb连接手机后通过adb与手机进行交互.adb作为client,adbd作为server端.

没有server端谈client端就没有用处,所以先分析server端.


adbd依赖于

LOCAL_SRC_FILES := \
        adb.c \
        backup_service.c \
        fdevent.c \
        transport.c \
        transport_local.c \
        transport_usb.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 \
        utils.c

作为一个service通过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
}

通过adb_main(0,DEFAULT_ADB_PORT)启动了adbd等待客户端请求;

define DEFAULT_ADB_PORT 5037

这就是为什么每次通过adb start-server后显示

* daemon not running. starting it now on port 5037 *
* daemon started successfully *

的原因所在了;

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);


    char local_name[30];
    build_local_name(local_name, sizeof(local_name), server_port);
    if(install_listener(local_name, "*smartsocket*", NULL)) {
        exit(1);
    }
#else


    /* 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);
        }
    }


        /* for the device, start the usb transport if the
        ** android usb device exists and the "service.adb.tcp.port" and
        ** "persist.adb.tcp.port" properties are not set.
        ** Otherwise start the network transport.
        */
    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) {
        // listen on TCP port specified by service.adb.tcp.port property
        local_init(port);
    } else if (access("/dev/android_adb", F_OK) == 0) {
        // listen on USB
        usb_init();
    } else {
        // 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(void)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值