idevicedebug.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*
  2. * idevicedebug.c
  3. * Interact with the debugserver service of a device.
  4. *
  5. * Copyright (c) 2014-2015 Martin Szulecki All Rights Reserved.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <signal.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <time.h>
  26. #include <unistd.h>
  27. #include <libgen.h>
  28. #ifdef WIN32
  29. #include <windows.h>
  30. #define sleep(x) Sleep(x*1000)
  31. #endif
  32. #include <libimobiledevice/installation_proxy.h>
  33. #include <libimobiledevice/libimobiledevice.h>
  34. #include <libimobiledevice/debugserver.h>
  35. #include <plist/plist.h>
  36. #include "common/debug.h"
  37. enum cmd_mode {
  38. CMD_NONE = 0,
  39. CMD_RUN
  40. };
  41. static int quit_flag = 0;
  42. static void on_signal(int sig)
  43. {
  44. fprintf(stderr, "Exiting...\n");
  45. quit_flag++;
  46. }
  47. static instproxy_error_t instproxy_client_get_object_by_key_from_info_directionary_for_bundle_identifier(instproxy_client_t client, const char* appid, const char* key, plist_t* node)
  48. {
  49. if (!client || !appid || !key)
  50. return INSTPROXY_E_INVALID_ARG;
  51. plist_t apps = NULL;
  52. // create client options for any application types
  53. plist_t client_opts = instproxy_client_options_new();
  54. instproxy_client_options_add(client_opts, "ApplicationType", "Any", NULL);
  55. // only return attributes we need
  56. instproxy_client_options_set_return_attributes(client_opts, "CFBundleIdentifier", "CFBundleExecutable", key, NULL);
  57. // only query for specific appid
  58. const char* appids[] = {appid, NULL};
  59. // query device for list of apps
  60. instproxy_error_t ierr = instproxy_lookup(client, appids, client_opts, &apps);
  61. instproxy_client_options_free(client_opts);
  62. if (ierr != INSTPROXY_E_SUCCESS) {
  63. return ierr;
  64. }
  65. plist_t app_found = plist_access_path(apps, 1, appid);
  66. if (!app_found) {
  67. if (apps)
  68. plist_free(apps);
  69. *node = NULL;
  70. return INSTPROXY_E_OP_FAILED;
  71. }
  72. plist_t object = plist_dict_get_item(app_found, key);
  73. if (object) {
  74. *node = plist_copy(object);
  75. } else {
  76. debug_info("key %s not found", key);
  77. return INSTPROXY_E_OP_FAILED;
  78. }
  79. plist_free(apps);
  80. return INSTPROXY_E_SUCCESS;
  81. }
  82. static debugserver_error_t debugserver_client_handle_response(debugserver_client_t client, char** response, int send_reply)
  83. {
  84. debugserver_error_t dres = DEBUGSERVER_E_SUCCESS;
  85. debugserver_command_t command = NULL;
  86. char* o = NULL;
  87. char* r = *response;
  88. if (r[0] == 'O') {
  89. /* stdout/stderr */
  90. debugserver_decode_string(r + 1, strlen(r) - 1, &o);
  91. printf("%s", o);
  92. fflush(stdout);
  93. if (o != NULL) {
  94. free(o);
  95. o = NULL;
  96. }
  97. free(*response);
  98. *response = NULL;
  99. if (!send_reply)
  100. return dres;
  101. /* send reply */
  102. debugserver_command_new("OK", 0, NULL, &command);
  103. dres = debugserver_client_send_command(client, command, response);
  104. debug_info("result: %d", dres);
  105. debugserver_command_free(command);
  106. command = NULL;
  107. } else if (r[0] == 'T') {
  108. /* thread stopped information */
  109. debug_info("Thread stopped. Details:\n%s", r + 1);
  110. free(*response);
  111. *response = NULL;
  112. if (!send_reply)
  113. return dres;
  114. dres = DEBUGSERVER_E_UNKNOWN_ERROR;
  115. } else if (r[0] == 'E' || r[0] == 'W') {
  116. printf("%s: %s\n", (r[0] == 'E' ? "ERROR": "WARNING") , r + 1);
  117. free(*response);
  118. *response = NULL;
  119. if (!send_reply)
  120. return dres;
  121. /* send reply */
  122. debugserver_command_new("OK", 0, NULL, &command);
  123. dres = debugserver_client_send_command(client, command, response);
  124. debug_info("result: %d", dres);
  125. debugserver_command_free(command);
  126. command = NULL;
  127. } else if (r && strlen(r) == 0) {
  128. if (!send_reply)
  129. return dres;
  130. free(*response);
  131. *response = NULL;
  132. /* no command */
  133. debugserver_command_new("OK", 0, NULL, &command);
  134. dres = debugserver_client_send_command(client, command, response);
  135. debug_info("result: %d", dres);
  136. debugserver_command_free(command);
  137. command = NULL;
  138. } else {
  139. debug_info("ERROR: unhandled response", r);
  140. }
  141. return dres;
  142. }
  143. static void print_usage(int argc, char **argv)
  144. {
  145. char *name = NULL;
  146. name = strrchr(argv[0], '/');
  147. printf("Usage: %s [OPTIONS] COMMAND\n", (name ? name + 1: argv[0]));
  148. printf("Interact with the debugserver service of a device.\n\n");
  149. printf(" Where COMMAND is one of:\n");
  150. printf(" run BUNDLEID [ARGS...]\trun app with BUNDLEID and optional ARGS on device.\n");
  151. printf("\n");
  152. printf(" The following OPTIONS are accepted:\n");
  153. printf(" -e, --env NAME=VALUE\tset environment variable NAME to VALUE\n");
  154. printf(" -u, --udid UDID\ttarget specific device by its 40-digit device UDID\n");
  155. printf(" -d, --debug\t\tenable communication debugging\n");
  156. printf(" -h, --help\t\tprints usage information\n");
  157. printf("\n");
  158. printf("Homepage: <http://libimobiledevice.org>\n");
  159. }
  160. int main(int argc, char *argv[])
  161. {
  162. int res = -1;
  163. idevice_t device = NULL;
  164. idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR;
  165. instproxy_client_t instproxy_client = NULL;
  166. debugserver_client_t debugserver_client = NULL;
  167. int i;
  168. int debug_level = 0;
  169. int cmd = CMD_NONE;
  170. const char* udid = NULL;
  171. const char* bundle_identifier = NULL;
  172. char* path = NULL;
  173. char* working_directory = NULL;
  174. char **newlist = NULL;
  175. char** environment = NULL;
  176. int environment_index = 0;
  177. int environment_count = 0;
  178. char* response = NULL;
  179. debugserver_command_t command = NULL;
  180. debugserver_error_t dres = DEBUGSERVER_E_UNKNOWN_ERROR;
  181. /* map signals */
  182. signal(SIGINT, on_signal);
  183. signal(SIGTERM, on_signal);
  184. #ifndef WIN32
  185. signal(SIGQUIT, on_signal);
  186. signal(SIGPIPE, SIG_IGN);
  187. #endif
  188. /* parse command line arguments */
  189. for (i = 1; i < argc; i++) {
  190. if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) {
  191. debug_level++;
  192. idevice_set_debug_level(debug_level);
  193. continue;
  194. } else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--udid")) {
  195. i++;
  196. if (!argv[i] || (strlen(argv[i]) != 40)) {
  197. print_usage(argc, argv);
  198. res = 0;
  199. goto cleanup;
  200. }
  201. udid = argv[i];
  202. continue;
  203. } else if (!strcmp(argv[i], "-e") || !strcmp(argv[i], "--env")) {
  204. i++;
  205. if (!argv[i] || (strlen(argv[i]) <= 1) || strchr(argv[i], '=') == NULL) {
  206. print_usage(argc, argv);
  207. res = 0;
  208. goto cleanup;
  209. }
  210. /* add environment variable */
  211. if (!newlist)
  212. newlist = malloc((environment_count + 1) * sizeof(char*));
  213. else
  214. newlist = realloc(environment, (environment_count + 1) * sizeof(char*));
  215. newlist[environment_count++] = strdup(argv[i]);
  216. environment = newlist;
  217. continue;
  218. } else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
  219. print_usage(argc, argv);
  220. res = 0;
  221. goto cleanup;
  222. } else if (!strcmp(argv[i], "run")) {
  223. cmd = CMD_RUN;
  224. i++;
  225. if (!argv[i]) {
  226. /* make sure at least the bundle identifier was provided */
  227. printf("Please supply the bundle identifier of the app to run.\n");
  228. print_usage(argc, argv);
  229. res = 0;
  230. goto cleanup;
  231. }
  232. /* read bundle identifier */
  233. bundle_identifier = argv[i];
  234. break;
  235. } else {
  236. print_usage(argc, argv);
  237. res = 0;
  238. goto cleanup;
  239. }
  240. }
  241. if (environment) {
  242. newlist = realloc(environment, (environment_count + 1) * sizeof(char*));
  243. newlist[environment_count] = NULL;
  244. environment = newlist;
  245. }
  246. /* verify options */
  247. if (cmd == CMD_NONE) {
  248. print_usage(argc, argv);
  249. goto cleanup;
  250. }
  251. /* connect to the device */
  252. ret = idevice_new(&device, udid);
  253. if (ret != IDEVICE_E_SUCCESS) {
  254. if (udid) {
  255. printf("No device found with udid %s, is it plugged in?\n", udid);
  256. } else {
  257. printf("No device found, is it plugged in?\n");
  258. }
  259. goto cleanup;
  260. }
  261. switch (cmd) {
  262. case CMD_RUN:
  263. default:
  264. /* get the path to the app and it's working directory */
  265. if (instproxy_client_start_service(device, &instproxy_client, "idevicerun") != INSTPROXY_E_SUCCESS) {
  266. fprintf(stderr, "Could not start installation proxy service.\n");
  267. goto cleanup;
  268. }
  269. plist_t container = NULL;
  270. instproxy_client_get_object_by_key_from_info_directionary_for_bundle_identifier(instproxy_client, bundle_identifier, "Container", &container);
  271. instproxy_client_get_path_for_bundle_identifier(instproxy_client, bundle_identifier, &path);
  272. instproxy_client_free(instproxy_client);
  273. instproxy_client = NULL;
  274. if (container) {
  275. if (plist_get_node_type(container) == PLIST_STRING) {
  276. plist_get_string_val(container, &working_directory);
  277. debug_info("working_directory: %s\n", working_directory);
  278. plist_free(container);
  279. } else {
  280. plist_free(container);
  281. fprintf(stderr, "Could not determine container path for bundle identifier %s.\n", bundle_identifier);
  282. goto cleanup;
  283. }
  284. }
  285. /* start and connect to debugserver */
  286. if (debugserver_client_start_service(device, &debugserver_client, "idevicerun") != DEBUGSERVER_E_SUCCESS) {
  287. fprintf(stderr,
  288. "Could not start com.apple.debugserver!\n"
  289. "Please make sure to mount the developer disk image first:\n"
  290. " 1) Get the iOS version from `ideviceinfo -k ProductVersion`.\n"
  291. " 2) Find the matching iPhoneOS DeveloperDiskImage.dmg files.\n"
  292. " 3) Run `ideviceimagemounter` with the above path.\n");
  293. goto cleanup;
  294. }
  295. /* enable logging for the session in debug mode */
  296. if (debug_level) {
  297. debug_info("Setting logging bitmask...");
  298. debugserver_command_new("QSetLogging:bitmask=LOG_ALL|LOG_RNB_REMOTE|LOG_RNB_PACKETS", 0, NULL, &command);
  299. dres = debugserver_client_send_command(debugserver_client, command, &response);
  300. debugserver_command_free(command);
  301. command = NULL;
  302. if (response) {
  303. if (strncmp(response, "OK", 2)) {
  304. debugserver_client_handle_response(debugserver_client, &response, 0);
  305. goto cleanup;
  306. }
  307. free(response);
  308. response = NULL;
  309. }
  310. }
  311. /* set maximum packet size */
  312. debug_info("Setting maximum packet size...");
  313. char* packet_size[2] = {strdup("1024"), NULL};
  314. debugserver_command_new("QSetMaxPacketSize:", 1, packet_size, &command);
  315. free(packet_size[0]);
  316. dres = debugserver_client_send_command(debugserver_client, command, &response);
  317. debugserver_command_free(command);
  318. command = NULL;
  319. if (response) {
  320. if (strncmp(response, "OK", 2)) {
  321. debugserver_client_handle_response(debugserver_client, &response, 0);
  322. goto cleanup;
  323. }
  324. free(response);
  325. response = NULL;
  326. }
  327. /* set working directory */
  328. debug_info("Setting working directory...");
  329. char* working_dir[2] = {working_directory, NULL};
  330. debugserver_command_new("QSetWorkingDir:", 1, working_dir, &command);
  331. dres = debugserver_client_send_command(debugserver_client, command, &response);
  332. debugserver_command_free(command);
  333. command = NULL;
  334. if (response) {
  335. if (strncmp(response, "OK", 2)) {
  336. debugserver_client_handle_response(debugserver_client, &response, 0);
  337. goto cleanup;
  338. }
  339. free(response);
  340. response = NULL;
  341. }
  342. /* set environment */
  343. if (environment) {
  344. debug_info("Setting environment...");
  345. for (environment_index = 0; environment_index < environment_count; environment_index++) {
  346. debug_info("setting environment variable: %s", environment[environment_index]);
  347. debugserver_client_set_environment_hex_encoded(debugserver_client, environment[environment_index], NULL);
  348. }
  349. }
  350. /* set arguments and run app */
  351. debug_info("Setting argv...");
  352. i++; /* i is the offset of the bundle identifier, thus skip it */
  353. int app_argc = (argc - i + 2);
  354. char **app_argv = (char**)malloc(sizeof(char*) * app_argc);
  355. app_argv[0] = path;
  356. debug_info("app_argv[%d] = %s", 0, app_argv[0]);
  357. app_argc = 1;
  358. while (i < argc && argv && argv[i]) {
  359. debug_info("app_argv[%d] = %s", app_argc, argv[i]);
  360. app_argv[app_argc++] = argv[i];
  361. i++;
  362. }
  363. app_argv[app_argc] = NULL;
  364. debugserver_client_set_argv(debugserver_client, app_argc, app_argv, NULL);
  365. free(app_argv);
  366. /* check if launch succeeded */
  367. debug_info("Checking if launch succeeded...");
  368. debugserver_command_new("qLaunchSuccess", 0, NULL, &command);
  369. dres = debugserver_client_send_command(debugserver_client, command, &response);
  370. debugserver_command_free(command);
  371. command = NULL;
  372. if (response) {
  373. if (strncmp(response, "OK", 2)) {
  374. debugserver_client_handle_response(debugserver_client, &response, 0);
  375. goto cleanup;
  376. }
  377. free(response);
  378. response = NULL;
  379. }
  380. /* set thread */
  381. debug_info("Setting thread...");
  382. debugserver_command_new("Hc0", 0, NULL, &command);
  383. dres = debugserver_client_send_command(debugserver_client, command, &response);
  384. debugserver_command_free(command);
  385. command = NULL;
  386. if (response) {
  387. if (strncmp(response, "OK", 2)) {
  388. debugserver_client_handle_response(debugserver_client, &response, 0);
  389. goto cleanup;
  390. }
  391. free(response);
  392. response = NULL;
  393. }
  394. /* continue running process */
  395. debug_info("Continue running process...");
  396. debugserver_command_new("c", 0, NULL, &command);
  397. dres = debugserver_client_send_command(debugserver_client, command, &response);
  398. debugserver_command_free(command);
  399. command = NULL;
  400. /* main loop which is parsing/handling packets during the run */
  401. debug_info("Entering run loop...");
  402. while (!quit_flag) {
  403. if (dres != DEBUGSERVER_E_SUCCESS) {
  404. debug_info("failed to receive response");
  405. break;
  406. }
  407. if (response) {
  408. debug_info("response: %s", response);
  409. dres = debugserver_client_handle_response(debugserver_client, &response, 1);
  410. }
  411. sleep(1);
  412. }
  413. /* kill process after we finished */
  414. debug_info("Killing process...");
  415. debugserver_command_new("k", 0, NULL, &command);
  416. dres = debugserver_client_send_command(debugserver_client, command, &response);
  417. debugserver_command_free(command);
  418. command = NULL;
  419. if (response) {
  420. if (strncmp(response, "OK", 2)) {
  421. debugserver_client_handle_response(debugserver_client, &response, 0);
  422. goto cleanup;
  423. }
  424. free(response);
  425. response = NULL;
  426. }
  427. res = (dres == DEBUGSERVER_E_SUCCESS) ? 0: -1;
  428. break;
  429. }
  430. cleanup:
  431. /* cleanup the house */
  432. if (environment) {
  433. for (environment_index = 0; environment_index < environment_count; environment_index++) {
  434. free(environment[environment_index]);
  435. }
  436. free(environment);
  437. }
  438. if (working_directory)
  439. free(working_directory);
  440. if (path)
  441. free(path);
  442. if (response)
  443. free(response);
  444. if (debugserver_client)
  445. debugserver_client_free(debugserver_client);
  446. if (device)
  447. idevice_free(device);
  448. return res;
  449. }