javasee 发表于 2013-2-1 11:13:54

Android培训班(31)

<!--@page { margin: 2cm }P { margin-bottom: 0.21cm }-->
在init.rc文件里,可以看到下面的服务加载并运行:
#adbd is controlled by the persist.service.adb.enable system property
serviceadbd /sbin/adbd
   disabled

adbd服务的代码在目录:Android-2.0/system/core/adb
adbd服务使用c语言实现,它不但可以在虚拟机里运行,也可以在实际的设备里运行。adbd服务是adb调试系统中的一部分,整个adb调试系统包括有三部分:手机运行的adbd服务、PC运行的服务器、PC运行的客户端。当android启动时,就运行adbd服务,创建一个调试端口,这样就可以让开发机器上的服务器连接过来,通过这个连接就可以发送调试信息给服务器,也可以接收到外面发送过来的调试命令。

先来分析编译文件Android.mk,adbd相关内容如下:
#adbd device daemon
#=========================================================

#build adbd in all non-simulator builds
BUILD_ADBD:= false
当设置为BUILD_ADBD为true时,就是编译运行在模拟器里的调试服务,否则就是运行到真实机器里的调试服务。

ifneq($(TARGET_SIMULATOR),true)
   BUILD_ADBD := true
endif


如果运行在linux里模拟器,就需要使用下面的判断。
#build adbd for the Linux simulator build
#so we can use it to test the adb USB gadget driver on x86
#ifeq($(HOST_OS),linux)
#   BUILD_ADBD := true
#endif


ifeq($(BUILD_ADBD),true)
include$(CLEAR_VARS)

LOCAL_SRC_FILES:= \
adb.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:= -O2 -g -DADB_HOST=0 -Wall -Wno-unused-parameter
LOCAL_CFLAGS+= -D_XOPEN_SOURCE -D_GNU_SOURCE

#TODO: This should probably be board specific, whether or not thekernel has
#the gadget driver; rather than relying on the architecture type.
ifeq($(TARGET_ARCH),arm)
LOCAL_CFLAGS+= -DANDROID_GADGET=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)

如果在模拟器里运行,就需要编译为线程模式,否则就需要连接静态库的方式。
ifeq($(TARGET_SIMULATOR),true)
LOCAL_STATIC_LIBRARIES := libcutils
LOCAL_LDLIBS += -lpthread
include $(BUILD_HOST_EXECUTABLE)
else
LOCAL_STATIC_LIBRARIES := libcutils libc
include $(BUILD_EXECUTABLE)
endif

接着来分析程序入口函数main处理过程,它的代码如下:
intmain(int argc, char **argv)
{

   adb_trace_init();
这行代码主要获取环境变量,用来判断到底输出什么样信息。

下面这段代码根据编译选项来决定编译为PC机里的服务,还是设备运行的服务。
#ifADB_HOST
   adb_sysdeps_init();
   return adb_commandline(argc - 1, argv + 1);
这里是选择编译为PC里的服务器。

#else

这段是选择为设备里的服务进程。
   if((argc > 1) &&(!strcmp(argv,"recovery"))) {
       adb_device_banner = "recovery";
       recovery_mode = 1;
    }
上面这段用来判断是否恢复模式。


   start_device_log();
这行代码开始输出调试LOG。

   return adb_main(0);
这行代码是进入adb服务进程处理。

#endif
}
页: [1]
查看完整版本: Android培训班(31)