CreateProcess 调用Adb 用管道接收结果

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()函数:

  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()函数的执行过程,后续将分析其中的细节内容。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Windows API 中的 `CreateProcess` 函数创建一个新的进程,并在其中运行 MATLAB 的 `matlab.exe` 程序,从而运行 MATLAB 程序。具体步骤如下: 1. 编译 MATLAB 代码,生成可执行文件 `mycode.exe`。 2. 在 C++ 代码中使用 `CreateProcess` 函数创建新的进程,并指定要运行的程序和传递的参数。示例如下: ```c++ #include <windows.h> #include <iostream> int main() { STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); ZeroMemory(&pi, sizeof(pi)); // 指定要运行的程序和参数 TCHAR command[] = TEXT("matlab.exe -r \"mycode('arg1', 'arg2', ...)\""); // 创建新进程 if (!CreateProcess(NULL, command, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) { std::cerr << "CreateProcess failed: " << GetLastError() << std::endl; return 1; } // 等待进程结束 WaitForSingleObject(pi.hProcess, INFINITE); // 获取进程的退出码 DWORD exitCode; if (!GetExitCodeProcess(pi.hProcess, &exitCode)) { std::cerr << "GetExitCodeProcess failed: " << GetLastError() << std::endl; return 1; } std::cout << "Process exited with code " << exitCode << std::endl; // 关闭进程和线程句柄 CloseHandle(pi.hProcess); CloseHandle(pi.hThread); return 0; } ``` 其中,`arg1`、`arg2` 等为传递给 MATLAB 程序的参数,可以根据需要进行修改。 需要注意的是,使用 `CreateProcess` 函数创建进程时,需要指定一些参数,如进程的安全属性、是否继承父进程的句柄等。在本例中,我们将这些参数都设为 `NULL` 或 `0`,表示使用默认值。如果需要更详细地控制进程的创建和运行,可以参考相关的 Windows API 文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值