Debian Codegraph @

tools/idevicecrashreport.c

raw
1 /*
2 * idevicecrashreport.c
3 * Simple utility to move crash reports from a device to a local directory.
4 *
5 * Copyright (c) 2014 Martin Szulecki. All Rights Reserved.
6 * Copyright (c) 2014 Nikias Bassen. All Rights Reserved.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #define TOOL_NAME "idevicecrashreport"
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <getopt.h>
34 #include <sys/stat.h>
35 #ifndef _WIN32
36 #include <signal.h>
37 #endif
38 #include <libimobiledevice-glue/utils.h>
39
40 #include <libimobiledevice/libimobiledevice.h>
41 #include <libimobiledevice/lockdown.h>
42 #include <libimobiledevice/service.h>
43 #include <libimobiledevice/afc.h>
44 #include <plist/plist.h>
45
46 #ifdef _WIN32
47 #include <windows.h>
48 #define S_IFLNK S_IFREG
49 #define S_IFSOCK S_IFREG
50 #endif
51
52 #define CRASH_REPORT_MOVER_SERVICE "com.apple.crashreportmover"
53 #define CRASH_REPORT_COPY_MOBILE_SERVICE "com.apple.crashreportcopymobile"
54
55 const char* target_directory = NULL;
56 static int extract_raw_crash_reports = 0;
57 static int keep_crash_reports = 0;
58 static int remove_all = 0;
59
60 static int file_exists(const char* path)
61 {
62 struct stat tst;
63 #ifdef _WIN32
64 return (stat(path, &tst) == 0);
65 #else
66 return (lstat(path, &tst) == 0);
67 #endif
68 }
69
70 static int extract_raw_crash_report(const char* filename)
71 {
72 int res = 0;
73 plist_t report = NULL;
74 char* raw = NULL;
75 char* raw_filename = strdup(filename);
76
77 /* create filename with '.crash' extension */
78 char* p = strrchr(raw_filename, '.');
79 if ((p == NULL) || (strcmp(p, ".plist") != 0)) {
80 free(raw_filename);
81 return res;
82 }
83 strcpy(p, ".crash");
84
85 /* read plist crash report */
86 if (plist_read_from_file(filename, &report, NULL)) {
87 plist_t description_node = plist_dict_get_item(report, "description");
88 if (description_node && plist_get_node_type(description_node) == PLIST_STRING) {
89 plist_get_string_val(description_node, &raw);
90
91 if (raw != NULL) {
92 /* write file */
93 buffer_write_to_filename(raw_filename, raw, strlen(raw));
94 free(raw);
95 res = 1;
96 }
97 }
98 }
99
100 if (report)
101 plist_free(report);
102
103 if (raw_filename)
104 free(raw_filename);
105
106 return res;
107 }
108
109 static int afc_client_copy_and_remove_crash_reports(afc_client_t afc, const char* device_directory, const char* host_directory, const char* filename_filter)
110 {
111 afc_error_t afc_error;
112 int k;
113 int res = -1;
114 int crash_report_count = 0;
115 uint64_t handle;
116 char source_filename[512];
117 char target_filename[512];
118
119 if (!afc)
120 return res;
121
122 char** list = NULL;
123 afc_error = afc_read_directory(afc, device_directory, &list);
124 if (afc_error != AFC_E_SUCCESS) {
125 fprintf(stderr, "ERROR: Could not read device directory '%s'\n", device_directory);
126 return res;
127 }
128
129 /* ensure we have a trailing slash */
130 strcpy(source_filename, device_directory);
131 if (source_filename[strlen(source_filename)-1] != '/') {
132 strcat(source_filename, "/");
133 }
134 int device_directory_length = strlen(source_filename);
135
136 /* ensure we have a trailing slash */
137 strcpy(target_filename, host_directory);
138 if (target_filename[strlen(target_filename)-1] != '/') {
139 strcat(target_filename, "/");
140 }
141 int host_directory_length = strlen(target_filename);
142
143 /* loop over file entries */
144 for (k = 0; list[k]; k++) {
145 if (!strcmp(list[k], ".") || !strcmp(list[k], "..")) {
146 continue;
147 }
148
149 plist_t fileinfo = NULL;
150 struct stat stbuf;
151 memset(&stbuf, '\0', sizeof(struct stat));
152
153 /* assemble absolute source filename */
154 strcpy(((char*)source_filename) + device_directory_length, list[k]);
155
156 /* assemble absolute target filename */
157 #ifdef _WIN32
158 /* replace every ':' with '-' since ':' is an illegal character for file names in windows */
159 char* current_pos = strchr(list[k], ':');
160 while (current_pos) {
161 *current_pos = '-';
162 current_pos = strchr(current_pos, ':');
163 }
164 #endif
165 char* p = strrchr(list[k], '.');
166 if (p != NULL && !strncmp(p, ".synced", 7)) {
167 /* make sure to strip ".synced" extension as seen on iOS 5 */
168 size_t newlen = p - list[k];
169 strncpy(((char*)target_filename) + host_directory_length, list[k], newlen);
170 target_filename[host_directory_length + newlen] = '\0';
171 } else {
172 strcpy(((char*)target_filename) + host_directory_length, list[k]);
173 }
174
175 /* get file information */
176 afc_get_file_info_plist(afc, source_filename, &fileinfo);
177 if (!fileinfo) {
178 printf("Failed to read information for '%s'. Skipping...\n", source_filename);
179 continue;
180 }
181
182 /* parse file information */
183 stbuf.st_size = plist_dict_get_uint(fileinfo, "st_size");
184 const char* s_ifmt = plist_get_string_ptr(plist_dict_get_item(fileinfo, "st_ifmt"), NULL);
185 if (s_ifmt) {
186 if (!strcmp(s_ifmt, "S_IFREG")) {
187 stbuf.st_mode = S_IFREG;
188 } else if (!strcmp(s_ifmt, "S_IFDIR")) {
189 stbuf.st_mode = S_IFDIR;
190 } else if (!strcmp(s_ifmt, "S_IFLNK")) {
191 stbuf.st_mode = S_IFLNK;
192 } else if (!strcmp(s_ifmt, "S_IFBLK")) {
193 stbuf.st_mode = S_IFBLK;
194 } else if (!strcmp(s_ifmt, "S_IFCHR")) {
195 stbuf.st_mode = S_IFCHR;
196 } else if (!strcmp(s_ifmt, "S_IFIFO")) {
197 stbuf.st_mode = S_IFIFO;
198 } else if (!strcmp(s_ifmt, "S_IFSOCK")) {
199 stbuf.st_mode = S_IFSOCK;
200 }
201 }
202 stbuf.st_nlink = plist_dict_get_uint(fileinfo, "st_nlink");
203 stbuf.st_mtime = (time_t)(plist_dict_get_uint(fileinfo, "st_mtime") / 1000000000);
204 const char* linktarget = plist_get_string_ptr(plist_dict_get_item(fileinfo, "LinkTarget"), NULL);
205 if (linktarget && !remove_all) {
206 /* report latest crash report filename */
207 printf("Link: %s\n", (char*)target_filename + strlen(target_directory));
208
209 /* remove any previous symlink */
210 if (file_exists(target_filename)) {
211 remove(target_filename);
212 }
213
214 #ifndef _WIN32
215 /* use relative filename */
216 const char* b = strrchr(linktarget, '/');
217 if (b == NULL) {
218 b = linktarget;
219 } else {
220 b++;
221 }
222
223 /* create a symlink pointing to latest log */
224 if (symlink(b, target_filename) < 0) {
225 fprintf(stderr, "Can't create symlink to %s\n", b);
226 }
227 #endif
228
229 if (!keep_crash_reports)
230 afc_remove_path(afc, source_filename);
231
232 res = 0;
233 }
234
235 /* free file information */
236 plist_free(fileinfo);
237
238 /* recurse into child directories */
239 if (S_ISDIR(stbuf.st_mode)) {
240 if (!remove_all) {
241 #ifdef _WIN32
242 mkdir(target_filename);
243 #else
244 mkdir(target_filename, 0755);
245 #endif
246 }
247 res = afc_client_copy_and_remove_crash_reports(afc, source_filename, target_filename, filename_filter);
248
249 /* remove directory from device */
250 if (!remove_all && !keep_crash_reports)
251 afc_remove_path(afc, source_filename);
252 } else if (S_ISREG(stbuf.st_mode)) {
253 if (filename_filter != NULL && strstr(source_filename, filename_filter) == NULL) {
254 continue;
255 }
256
257 if (remove_all) {
258 printf("Remove: %s\n", source_filename);
259 afc_remove_path(afc, source_filename);
260 continue;
261 }
262
263 /* copy file to host */
264 afc_error = afc_file_open(afc, source_filename, AFC_FOPEN_RDONLY, &handle);
265 if(afc_error != AFC_E_SUCCESS) {
266 if (afc_error == AFC_E_OBJECT_NOT_FOUND) {
267 continue;
268 }
269 fprintf(stderr, "Unable to open device file '%s' (%d). Skipping...\n", source_filename, afc_error);
270 continue;
271 }
272
273 FILE* output = fopen(target_filename, "wb");
274 if(output == NULL) {
275 fprintf(stderr, "Unable to open local file '%s'. Skipping...\n", target_filename);
276 afc_file_close(afc, handle);
277 continue;
278 }
279
280 printf("%s: %s\n", (keep_crash_reports ? "Copy": "Move") , (char*)target_filename + strlen(target_directory));
281
282 uint32_t bytes_read = 0;
283 uint32_t bytes_total = 0;
284 unsigned char data[0x1000];
285
286 afc_error = afc_file_read(afc, handle, (char*)data, 0x1000, &bytes_read);
287 while(afc_error == AFC_E_SUCCESS && bytes_read > 0) {
288 fwrite(data, 1, bytes_read, output);
289 bytes_total += bytes_read;
290 afc_error = afc_file_read(afc, handle, (char*)data, 0x1000, &bytes_read);
291 }
292 afc_file_close(afc, handle);
293 fclose(output);
294
295 if ((uint32_t)stbuf.st_size != bytes_total) {
296 fprintf(stderr, "File size mismatch. Skipping...\n");
297 continue;
298 }
299
300 /* remove file from device */
301 if (!keep_crash_reports) {
302 afc_remove_path(afc, source_filename);
303 }
304
305 /* extract raw crash information into separate '.crash' file */
306 if (extract_raw_crash_reports) {
307 extract_raw_crash_report(target_filename);
308 }
309
310 crash_report_count++;
311
312 res = 0;
313 }
314 }
315 afc_dictionary_free(list);
316
317 /* no reports, no error */
318 if (crash_report_count == 0)
319 res = 0;
320
321 return res;
322 }
323
324 static void print_usage(int argc, char **argv, int is_error)
325 {
326 char *name = strrchr(argv[0], '/');
327 fprintf(is_error ? stderr : stdout, "Usage: %s [OPTIONS] DIRECTORY\n", (name ? name + 1: argv[0]));
328 fprintf(is_error ? stderr : stdout,
329 "\n"
330 "Move crash reports from device to a local DIRECTORY.\n"
331 "\n"
332 "OPTIONS:\n"
333 " -u, --udid UDID target specific device by UDID\n"
334 " -n, --network connect to network device\n"
335 " -e, --extract extract raw crash report into separate '.crash' file\n"
336 " -k, --keep copy but do not remove crash reports from device\n"
337 " -d, --debug enable communication debugging\n"
338 " -f, --filter NAME filter crash reports by NAME (case sensitive)\n"
339 " -h, --help prints usage information\n"
340 " -v, --version prints version information\n"
341 " --remove-all remove all crash logs found\n"
342 "\n"
343 "Homepage: <" PACKAGE_URL ">\n"
344 "Bug Reports: <" PACKAGE_BUGREPORT ">\n"
345 );
346 }
347
348 int main(int argc, char* argv[])
349 {
350 idevice_t device = NULL;
351 lockdownd_client_t lockdownd = NULL;
352 afc_client_t afc = NULL;
353
354 idevice_error_t device_error = IDEVICE_E_SUCCESS;
355 lockdownd_error_t lockdownd_error = LOCKDOWN_E_SUCCESS;
356 afc_error_t afc_error = AFC_E_SUCCESS;
357
358 const char* udid = NULL;
359 int use_network = 0;
360 const char* filename_filter = NULL;
361
362 int c = 0;
363 const struct option longopts[] = {
364 { "debug", no_argument, NULL, 'd' },
365 { "help", no_argument, NULL, 'h' },
366 { "udid", required_argument, NULL, 'u' },
367 { "network", no_argument, NULL, 'n' },
368 { "version", no_argument, NULL, 'v' },
369 { "filter", required_argument, NULL, 'f' },
370 { "extract", no_argument, NULL, 'e' },
371 { "keep", no_argument, NULL, 'k' },
372 { "remove-all", no_argument, NULL, 1 },
373 { NULL, 0, NULL, 0}
374 };
375
376 #ifndef _WIN32
377 signal(SIGPIPE, SIG_IGN);
378 #endif
379
380 /* parse cmdline args */
381 while ((c = getopt_long(argc, argv, "dhu:nvf:ek", longopts, NULL)) != -1) {
382 switch (c) {
383 case 'd':
384 idevice_set_debug_level(1);
385 break;
386 case 'u':
387 if (!*optarg) {
388 fprintf(stderr, "ERROR: UDID argument must not be empty!\n");
389 print_usage(argc, argv, 1);
390 return 2;
391 }
392 udid = optarg;
393 break;
394 case 'n':
395 use_network = 1;
396 break;
397 case 'h':
398 print_usage(argc, argv, 0);
399 return 0;
400 case 'v':
401 printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION);
402 return 0;
403 case 'f':
404 if (!*optarg) {
405 fprintf(stderr, "ERROR: filter argument must not be empty!\n");
406 print_usage(argc, argv, 1);
407 return 2;
408 }
409 filename_filter = optarg;
410 break;
411 case 'e':
412 extract_raw_crash_reports = 1;
413 break;
414 case 'k':
415 keep_crash_reports = 1;
416 break;
417 case 1:
418 remove_all = 1;
419 break;
420 default:
421 print_usage(argc, argv, 1);
422 return 2;
423 }
424 }
425 argc -= optind;
426 argv += optind;
427
428 /* ensure a target directory was supplied */
429 if (!remove_all) {
430 if (!argv[0]) {
431 fprintf(stderr, "ERROR: missing target directory.\n");
432 print_usage(argc+optind, argv-optind, 1);
433 return 2;
434 }
435 target_directory = argv[0];
436 } else {
437 target_directory = ".";
438 }
439
440 /* check if target directory exists */
441 if (!file_exists(target_directory)) {
442 fprintf(stderr, "ERROR: Directory '%s' does not exist.\n", target_directory);
443 return 1;
444 }
445
446 device_error = idevice_new_with_options(&device, udid, (use_network) ? IDEVICE_LOOKUP_NETWORK : IDEVICE_LOOKUP_USBMUX);
447 if (device_error != IDEVICE_E_SUCCESS) {
448 if (udid) {
449 printf("No device found with udid %s.\n", udid);
450 } else {
451 printf("No device found.\n");
452 }
453 return -1;
454 }
455
456 lockdownd_error = lockdownd_client_new_with_handshake(device, &lockdownd, TOOL_NAME);
457 if (lockdownd_error != LOCKDOWN_E_SUCCESS) {
458 fprintf(stderr, "ERROR: Could not connect to lockdownd, error code %d\n", lockdownd_error);
459 idevice_free(device);
460 return -1;
461 }
462
463 /* start crash log mover service */
464 lockdownd_service_descriptor_t service = NULL;
465 lockdownd_error = lockdownd_start_service(lockdownd, CRASH_REPORT_MOVER_SERVICE, &service);
466 if (lockdownd_error != LOCKDOWN_E_SUCCESS) {
467 fprintf(stderr, "ERROR: Could not start service %s: %s\n", CRASH_REPORT_MOVER_SERVICE, lockdownd_strerror(lockdownd_error));
468 lockdownd_client_free(lockdownd);
469 idevice_free(device);
470 return -1;
471 }
472
473 /* trigger move operation on device */
474 service_client_t svcmove = NULL;
475 service_error_t service_error = service_client_new(device, service, &svcmove);
476 lockdownd_service_descriptor_free(service);
477 service = NULL;
478 if (service_error != SERVICE_E_SUCCESS) {
479 lockdownd_client_free(lockdownd);
480 idevice_free(device);
481 return -1;
482 }
483
484 /* read "ping" message which indicates the crash logs have been moved to a safe harbor */
485 char *ping = malloc(4);
486 memset(ping, '\0', 4);
487 int attempts = 0;
488 while ((strncmp(ping, "ping", 4) != 0) && (attempts < 10)) {
489 uint32_t bytes = 0;
490 service_error = service_receive_with_timeout(svcmove, ping, 4, &bytes, 2000);
491 if (service_error == SERVICE_E_SUCCESS || service_error == SERVICE_E_TIMEOUT) {
492 attempts++;
493 continue;
494 }
495
496 fprintf(stderr, "ERROR: Crash logs could not be moved. Connection interrupted (%d).\n", service_error);
497 break;
498 }
499 service_client_free(svcmove);
500 free(ping);
501
502 if (device_error != IDEVICE_E_SUCCESS || attempts > 10) {
503 fprintf(stderr, "ERROR: Failed to receive ping message from crash report mover.\n");
504 lockdownd_client_free(lockdownd);
505 idevice_free(device);
506 return -1;
507 }
508
509 lockdownd_error = lockdownd_start_service(lockdownd, CRASH_REPORT_COPY_MOBILE_SERVICE, &service);
510 if (lockdownd_error != LOCKDOWN_E_SUCCESS) {
511 fprintf(stderr, "ERROR: Could not start service %s: %s\n", CRASH_REPORT_COPY_MOBILE_SERVICE, lockdownd_strerror(lockdownd_error));
512 lockdownd_client_free(lockdownd);
513 idevice_free(device);
514 return -1;
515 }
516 lockdownd_client_free(lockdownd);
517
518 afc = NULL;
519 afc_error = afc_client_new(device, service, &afc);
520 if(afc_error != AFC_E_SUCCESS) {
521 lockdownd_client_free(lockdownd);
522 idevice_free(device);
523 return -1;
524 }
525
526 if (service) {
527 lockdownd_service_descriptor_free(service);
528 service = NULL;
529 }
530
531 /* recursively copy crash reports from the device to a local directory */
532 if (afc_client_copy_and_remove_crash_reports(afc, ".", target_directory, filename_filter) < 0) {
533 fprintf(stderr, "ERROR: Failed to get crash reports from device.\n");
534 afc_client_free(afc);
535 idevice_free(device);
536 return -1;
537 }
538
539 printf("Done.\n");
540
541 afc_client_free(afc);
542 idevice_free(device);
543
544 return 0;
545 }