Commit | Line | Data |
---|---|---|
d7ba1388 MD |
1 | /* |
2 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> | |
3 | * Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License, version 2 only, | |
7 | * as published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License along | |
15 | * with this program; if not, write to the Free Software Foundation, Inc., | |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
17 | */ | |
18 | ||
19 | #define _GNU_SOURCE | |
20 | #include <getopt.h> | |
21 | #include <signal.h> | |
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
25 | #include <sys/stat.h> | |
26 | #include <sys/types.h> | |
27 | #include <sys/stat.h> | |
28 | #include <sys/mman.h> | |
29 | #include <fcntl.h> | |
30 | #include <sys/wait.h> | |
31 | #include <unistd.h> | |
32 | #include <config.h> | |
33 | #include <ctype.h> | |
34 | #include <dirent.h> | |
35 | #include <byteswap.h> | |
36 | #include <inttypes.h> | |
37 | ||
38 | #include <version.h> | |
39 | #include <lttng/lttng.h> | |
40 | #include <common/common.h> | |
41 | ||
42 | #define DEFAULT_VIEWER "babeltrace" | |
43 | ||
44 | #define COPY_BUFLEN 4096 | |
45 | #define RB_CRASH_DUMP_ABI_LEN 32 | |
46 | ||
47 | #define RB_CRASH_DUMP_ABI_MAGIC_LEN 16 | |
48 | ||
49 | /* | |
50 | * The 128-bit magic number is xor'd in the process data so it does not | |
51 | * cause a false positive when searching for buffers by scanning memory. | |
52 | * The actual magic number is: | |
53 | * 0x17, 0x7B, 0xF1, 0x77, 0xBF, 0x17, 0x7B, 0xF1, | |
54 | * 0x77, 0xBF, 0x17, 0x7B, 0xF1, 0x77, 0xBF, 0x17, | |
55 | */ | |
56 | #define RB_CRASH_DUMP_ABI_MAGIC_XOR \ | |
57 | { \ | |
58 | 0x17 ^ 0xFF, 0x7B ^ 0xFF, 0xF1 ^ 0xFF, 0x77 ^ 0xFF, \ | |
59 | 0xBF ^ 0xFF, 0x17 ^ 0xFF, 0x7B ^ 0xFF, 0xF1 ^ 0xFF, \ | |
60 | 0x77 ^ 0xFF, 0xBF ^ 0xFF, 0x17 ^ 0xFF, 0x7B ^ 0xFF, \ | |
61 | 0xF1 ^ 0xFF, 0x77 ^ 0xFF, 0xBF ^ 0xFF, 0x17 ^ 0xFF, \ | |
62 | } | |
63 | ||
64 | /* | |
65 | * Non-static to ensure the compiler does not optimize away the xor. | |
66 | */ | |
67 | uint8_t lttng_crash_expected_magic_xor[] = RB_CRASH_DUMP_ABI_MAGIC_XOR; | |
68 | ||
69 | #define RB_CRASH_ENDIAN 0x1234 | |
70 | #define RB_CRASH_ENDIAN_REVERSE 0x3412 | |
71 | ||
72 | enum lttng_crash_type { | |
73 | LTTNG_CRASH_TYPE_UST = 0, | |
74 | LTTNG_CRASH_TYPE_KERNEL = 1, | |
75 | }; | |
76 | ||
77 | /* LTTng ring buffer defines (copied) */ | |
78 | ||
79 | #define HALF_ULONG_BITS(wl) (((wl) * CHAR_BIT) >> 1) | |
80 | ||
81 | #define SB_ID_OFFSET_SHIFT(wl) (HALF_ULONG_BITS(wl) + 1) | |
82 | #define SB_ID_OFFSET_COUNT(wl) (1UL << SB_ID_OFFSET_SHIFT(wl)) | |
83 | #define SB_ID_OFFSET_MASK(wl) (~(SB_ID_OFFSET_COUNT(wl) - 1)) | |
84 | /* | |
85 | * Lowest bit of top word half belongs to noref. Used only for overwrite mode. | |
86 | */ | |
87 | #define SB_ID_NOREF_SHIFT(wl) (SB_ID_OFFSET_SHIFT(wl) - 1) | |
88 | #define SB_ID_NOREF_COUNT(wl) (1UL << SB_ID_NOREF_SHIFT(wl)) | |
89 | #define SB_ID_NOREF_MASK(wl) SB_ID_NOREF_COUNT(wl) | |
90 | /* | |
91 | * In overwrite mode: lowest half of word is used for index. | |
92 | * Limit of 2^16 subbuffers per buffer on 32-bit, 2^32 on 64-bit. | |
93 | * In producer-consumer mode: whole word used for index. | |
94 | */ | |
95 | #define SB_ID_INDEX_SHIFT(wl) 0 | |
96 | #define SB_ID_INDEX_COUNT(wl) (1UL << SB_ID_INDEX_SHIFT(wl)) | |
97 | #define SB_ID_INDEX_MASK(wl) (SB_ID_NOREF_COUNT(wl) - 1) | |
98 | ||
99 | enum rb_modes { | |
100 | RING_BUFFER_OVERWRITE = 0, /* Overwrite when buffer full */ | |
101 | RING_BUFFER_DISCARD = 1, /* Discard when buffer full */ | |
102 | }; | |
103 | ||
104 | struct crash_abi_unknown { | |
105 | uint8_t magic[RB_CRASH_DUMP_ABI_MAGIC_LEN]; | |
2f18b16d | 106 | uint64_t mmap_length; /* Overall length of crash record */ |
d7ba1388 MD |
107 | uint16_t endian; /* |
108 | * { 0x12, 0x34 }: big endian | |
109 | * { 0x34, 0x12 }: little endian | |
110 | */ | |
111 | uint16_t major; /* Major number. */ | |
112 | uint16_t minor; /* Minor number. */ | |
113 | uint8_t word_size; /* Word size (bytes). */ | |
114 | uint8_t layout_type; /* enum lttng_crash_layout */ | |
115 | } __attribute__((packed)); | |
116 | ||
117 | struct crash_abi_0_0 { | |
118 | struct crash_abi_unknown parent; | |
119 | ||
120 | struct { | |
121 | uint32_t prod_offset; | |
122 | uint32_t consumed_offset; | |
123 | uint32_t commit_hot_array; | |
124 | uint32_t commit_hot_seq; | |
125 | uint32_t buf_wsb_array; | |
126 | uint32_t buf_wsb_id; | |
127 | uint32_t sb_array; | |
128 | uint32_t sb_array_shmp_offset; | |
129 | uint32_t sb_backend_p_offset; | |
130 | uint32_t content_size; | |
131 | uint32_t packet_size; | |
132 | } __attribute__((packed)) offset; | |
133 | struct { | |
134 | uint8_t prod_offset; | |
135 | uint8_t consumed_offset; | |
136 | uint8_t commit_hot_seq; | |
137 | uint8_t buf_wsb_id; | |
138 | uint8_t sb_array_shmp_offset; | |
139 | uint8_t sb_backend_p_offset; | |
140 | uint8_t content_size; | |
141 | uint8_t packet_size; | |
142 | } __attribute__((packed)) length; | |
143 | struct { | |
144 | uint32_t commit_hot_array; | |
145 | uint32_t buf_wsb_array; | |
146 | uint32_t sb_array; | |
147 | } __attribute__((packed)) stride; | |
148 | ||
149 | uint64_t buf_size; /* Size of the buffer */ | |
150 | uint64_t subbuf_size; /* Sub-buffer size */ | |
151 | uint64_t num_subbuf; /* Number of sub-buffers for writer */ | |
152 | uint32_t mode; /* Buffer mode: 0: overwrite, 1: discard */ | |
153 | } __attribute__((packed)); | |
154 | ||
155 | struct lttng_crash_layout { | |
156 | struct { | |
157 | int prod_offset, consumed_offset, | |
158 | commit_hot_array, commit_hot_seq, | |
159 | buf_wsb_array, buf_wsb_id, | |
160 | sb_array, sb_array_shmp_offset, | |
161 | sb_backend_p_offset, content_size, | |
162 | packet_size; | |
163 | } offset; | |
164 | struct { | |
165 | int prod_offset, consumed_offset, | |
166 | commit_hot_seq, buf_wsb_id, | |
167 | sb_array_shmp_offset, sb_backend_p_offset, | |
168 | content_size, packet_size; | |
169 | } length; | |
170 | struct { | |
171 | int commit_hot_array, buf_wsb_array, sb_array; | |
172 | } stride; | |
173 | ||
174 | int reverse_byte_order; | |
175 | int word_size; | |
176 | ||
177 | uint64_t mmap_length; /* Length of crash record */ | |
178 | uint64_t buf_size; /* Size of the buffer */ | |
179 | uint64_t subbuf_size; /* Sub-buffer size */ | |
180 | uint64_t num_subbuf; /* Number of sub-buffers for writer */ | |
181 | uint32_t mode; /* Buffer mode: 0: overwrite, 1: discard */ | |
182 | }; | |
183 | ||
184 | /* Variables */ | |
185 | static char *progname, | |
04eb14f3 AB |
186 | *opt_viewer_path = NULL, |
187 | *opt_output_path = NULL; | |
d7ba1388 | 188 | |
a424227e | 189 | static char *input_path; |
d7ba1388 MD |
190 | |
191 | int lttng_opt_quiet, lttng_opt_verbose, lttng_opt_mi; | |
192 | ||
193 | enum { | |
194 | OPT_DUMP_OPTIONS, | |
195 | }; | |
196 | ||
197 | /* Getopt options. No first level command. */ | |
198 | static struct option long_options[] = { | |
199 | { "version", 0, NULL, 'V' }, | |
200 | { "help", 0, NULL, 'h' }, | |
201 | { "verbose", 0, NULL, 'v' }, | |
202 | { "viewer", 1, NULL, 'e' }, | |
203 | { "extract", 1, NULL, 'x' }, | |
204 | { "list-options", 0, NULL, OPT_DUMP_OPTIONS }, | |
205 | { NULL, 0, NULL, 0 }, | |
206 | }; | |
207 | ||
208 | static void usage(FILE *ofp) | |
209 | { | |
210 | fprintf(ofp, "LTTng Crash Trace Viewer " VERSION " - " VERSION_NAME "%s\n\n", | |
211 | GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION); | |
acade31f | 212 | fprintf(ofp, "usage: lttng-crash [OPTIONS] FILE\n"); |
d7ba1388 MD |
213 | fprintf(ofp, "\n"); |
214 | fprintf(ofp, "Options:\n"); | |
215 | fprintf(ofp, " -V, --version Show version.\n"); | |
216 | fprintf(ofp, " -h, --help Show this help.\n"); | |
217 | fprintf(ofp, " --list-options Simple listing of lttng-crash options.\n"); | |
218 | fprintf(ofp, " -v, --verbose Increase verbosity.\n"); | |
219 | fprintf(ofp, " -e, --viewer Specify viewer and/or options to use. This will\n" | |
220 | " completely override the default viewers so please\n" | |
221 | " make sure to specify the full command. The trace\n" | |
222 | " directory paths appended at the end to the\n" | |
223 | " arguments.\n"); | |
224 | fprintf(ofp, " -x, --extract PATH Extract trace(s) to specified path. Don't view\n" | |
225 | " trace.\n"); | |
226 | fprintf(ofp, "\n"); | |
227 | fprintf(ofp, "Please see the lttng-crash(1) man page for full documentation.\n"); | |
228 | fprintf(ofp, "See http://lttng.org for updates, bug reports and news.\n"); | |
229 | } | |
230 | ||
231 | static void version(FILE *ofp) | |
232 | { | |
233 | fprintf(ofp, "%s (LTTng Crash Trace Viewer) " VERSION " - " VERSION_NAME | |
234 | "%s\n", | |
235 | progname, | |
236 | GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION); | |
237 | } | |
238 | ||
239 | /* | |
240 | * list_options | |
241 | * | |
242 | * List options line by line. This is mostly for bash auto completion and to | |
243 | * avoid difficult parsing. | |
244 | */ | |
245 | static void list_options(FILE *ofp) | |
246 | { | |
247 | int i = 0; | |
248 | struct option *option = NULL; | |
249 | ||
250 | option = &long_options[i]; | |
251 | while (option->name != NULL) { | |
252 | fprintf(ofp, "--%s\n", option->name); | |
253 | ||
254 | if (isprint(option->val)) { | |
255 | fprintf(ofp, "-%c\n", option->val); | |
256 | } | |
257 | ||
258 | i++; | |
259 | option = &long_options[i]; | |
260 | } | |
261 | } | |
262 | ||
263 | /* | |
264 | * Parse command line arguments. | |
265 | * | |
266 | * Return 0 if OK, else -1 | |
267 | */ | |
268 | static int parse_args(int argc, char **argv) | |
269 | { | |
270 | int opt, ret = 0; | |
271 | ||
272 | if (argc < 2) { | |
273 | usage(stderr); | |
274 | exit(EXIT_FAILURE); | |
275 | } | |
276 | ||
78940451 | 277 | while ((opt = getopt_long(argc, argv, "+Vhve:x:", long_options, NULL)) != -1) { |
d7ba1388 MD |
278 | switch (opt) { |
279 | case 'V': | |
280 | version(stdout); | |
281 | ret = 1; | |
282 | goto end; | |
283 | case 'h': | |
284 | usage(stdout); | |
285 | ret = 1; | |
286 | goto end; | |
287 | case 'v': | |
288 | /* There is only 3 possible level of verbosity. (-vvv) */ | |
289 | if (lttng_opt_verbose < 3) { | |
290 | lttng_opt_verbose += 1; | |
291 | } | |
292 | break; | |
293 | case 'e': | |
04eb14f3 | 294 | free(opt_viewer_path); |
d7ba1388 MD |
295 | opt_viewer_path = strdup(optarg); |
296 | break; | |
297 | case 'x': | |
04eb14f3 | 298 | free(opt_output_path); |
d7ba1388 MD |
299 | opt_output_path = strdup(optarg); |
300 | break; | |
301 | case OPT_DUMP_OPTIONS: | |
302 | list_options(stdout); | |
303 | ret = 1; | |
304 | goto end; | |
305 | default: | |
306 | usage(stderr); | |
307 | goto error; | |
308 | } | |
309 | } | |
310 | ||
04eb14f3 AB |
311 | if (!opt_viewer_path) { |
312 | opt_viewer_path = DEFAULT_VIEWER; | |
313 | } | |
314 | ||
a424227e MD |
315 | /* No leftovers, or more than one input path, print usage and quit */ |
316 | if ((argc - optind) == 0 || (argc - optind) > 1) { | |
d7ba1388 MD |
317 | usage(stderr); |
318 | goto error; | |
319 | } | |
320 | ||
a424227e | 321 | input_path = argv[optind]; |
d7ba1388 MD |
322 | end: |
323 | return ret; | |
324 | ||
325 | error: | |
326 | return -1; | |
327 | } | |
328 | ||
329 | static | |
330 | int copy_file(const char *file_dest, const char *file_src) | |
331 | { | |
2f18b16d | 332 | int fd_src = -1, fd_dest = -1; |
d7ba1388 MD |
333 | ssize_t readlen, writelen; |
334 | char buf[COPY_BUFLEN]; | |
2f18b16d | 335 | int ret; |
d7ba1388 MD |
336 | |
337 | DBG("Copy metadata file '%s' into '%s'", file_src, file_dest); | |
338 | ||
339 | fd_src = open(file_src, O_RDONLY); | |
340 | if (fd_src < 0) { | |
341 | PERROR("Error opening %s for reading", file_src); | |
2f18b16d JR |
342 | ret = -errno; |
343 | goto error; | |
d7ba1388 MD |
344 | } |
345 | fd_dest = open(file_dest, O_RDWR | O_CREAT | O_EXCL, | |
346 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
347 | if (fd_dest < 0) { | |
348 | PERROR("Error opening %s for writing", file_dest); | |
2f18b16d JR |
349 | ret = -errno; |
350 | goto error; | |
d7ba1388 MD |
351 | } |
352 | ||
353 | for (;;) { | |
354 | readlen = lttng_read(fd_src, buf, COPY_BUFLEN); | |
355 | if (readlen < 0) { | |
356 | PERROR("Error reading input file"); | |
2f18b16d JR |
357 | ret = -1; |
358 | goto error; | |
d7ba1388 MD |
359 | } |
360 | if (!readlen) { | |
361 | break; | |
362 | } | |
363 | writelen = lttng_write(fd_dest, buf, readlen); | |
364 | if (writelen < readlen) { | |
365 | PERROR("Error writing to output file"); | |
2f18b16d JR |
366 | ret = -1; |
367 | goto error; | |
368 | } | |
369 | } | |
370 | ||
371 | ret = 0; | |
372 | error: | |
373 | if (fd_src >= 0) { | |
374 | if (close(fd_src) < 0) { | |
375 | PERROR("Error closing %s", file_src); | |
376 | } | |
377 | } | |
378 | ||
379 | if (fd_dest >= 0) { | |
380 | if (close(fd_dest) < 0) { | |
381 | PERROR("Error closing %s", file_dest); | |
d7ba1388 MD |
382 | } |
383 | } | |
2f18b16d | 384 | return ret; |
d7ba1388 MD |
385 | } |
386 | ||
387 | static | |
388 | uint64_t _crash_get_field(const struct lttng_crash_layout *layout, | |
389 | const char *ptr, size_t size) | |
390 | { | |
391 | switch (size) { | |
392 | case 1: return *(uint8_t *) ptr; | |
393 | case 2: if (layout->reverse_byte_order) { | |
394 | return __bswap_16(*(uint16_t *) ptr); | |
395 | } else { | |
396 | return *(uint16_t *) ptr; | |
397 | ||
398 | } | |
399 | case 4: if (layout->reverse_byte_order) { | |
400 | return __bswap_32(*(uint32_t *) ptr); | |
401 | } else { | |
402 | return *(uint32_t *) ptr; | |
403 | ||
404 | } | |
405 | case 8: if (layout->reverse_byte_order) { | |
406 | return __bswap_64(*(uint64_t *) ptr); | |
407 | } else { | |
408 | return *(uint64_t *) ptr; | |
409 | } | |
410 | default: | |
411 | abort(); | |
412 | return -1; | |
413 | } | |
414 | ||
415 | } | |
416 | ||
417 | #define crash_get_field(layout, map, name) \ | |
418 | _crash_get_field(layout, (map) + (layout)->offset.name, \ | |
419 | layout->length.name) | |
420 | ||
421 | #define crash_get_array_field(layout, map, array_name, idx, field_name) \ | |
422 | _crash_get_field(layout, \ | |
423 | (map) + (layout)->offset.array_name \ | |
424 | + (idx * (layout)->stride.array_name) \ | |
425 | + (layout)->offset.field_name, \ | |
426 | (layout)->length.field_name) | |
427 | ||
428 | #define crash_get_hdr_raw_field(layout, hdr, name) ((hdr)->name) | |
429 | ||
430 | #define crash_get_hdr_field(layout, hdr, name) \ | |
431 | _crash_get_field(layout, (const char *) &(hdr)->name, \ | |
432 | sizeof((hdr)->name)) | |
433 | ||
434 | #define crash_get_layout(layout, hdr, name) \ | |
435 | do { \ | |
436 | (layout)->name = crash_get_hdr_field(layout, hdr, \ | |
437 | name); \ | |
438 | DBG("layout.%s = %" PRIu64, #name, \ | |
439 | (uint64_t) (layout)->name); \ | |
440 | } while (0) | |
441 | ||
442 | static | |
443 | int get_crash_layout_0_0(struct lttng_crash_layout *layout, | |
444 | char *map) | |
445 | { | |
446 | const struct crash_abi_0_0 *abi = (const struct crash_abi_0_0 *) map; | |
447 | ||
448 | crash_get_layout(layout, abi, offset.prod_offset); | |
449 | crash_get_layout(layout, abi, offset.consumed_offset); | |
450 | crash_get_layout(layout, abi, offset.commit_hot_array); | |
451 | crash_get_layout(layout, abi, offset.commit_hot_seq); | |
452 | crash_get_layout(layout, abi, offset.buf_wsb_array); | |
453 | crash_get_layout(layout, abi, offset.buf_wsb_id); | |
454 | crash_get_layout(layout, abi, offset.sb_array); | |
455 | crash_get_layout(layout, abi, offset.sb_array_shmp_offset); | |
456 | crash_get_layout(layout, abi, offset.sb_backend_p_offset); | |
457 | crash_get_layout(layout, abi, offset.content_size); | |
458 | crash_get_layout(layout, abi, offset.packet_size); | |
459 | ||
460 | crash_get_layout(layout, abi, length.prod_offset); | |
461 | crash_get_layout(layout, abi, length.consumed_offset); | |
462 | crash_get_layout(layout, abi, length.commit_hot_seq); | |
463 | crash_get_layout(layout, abi, length.buf_wsb_id); | |
464 | crash_get_layout(layout, abi, length.sb_array_shmp_offset); | |
465 | crash_get_layout(layout, abi, length.sb_backend_p_offset); | |
466 | crash_get_layout(layout, abi, length.content_size); | |
467 | crash_get_layout(layout, abi, length.packet_size); | |
468 | ||
469 | crash_get_layout(layout, abi, stride.commit_hot_array); | |
470 | crash_get_layout(layout, abi, stride.buf_wsb_array); | |
471 | crash_get_layout(layout, abi, stride.sb_array); | |
472 | ||
473 | crash_get_layout(layout, abi, buf_size); | |
474 | crash_get_layout(layout, abi, subbuf_size); | |
475 | crash_get_layout(layout, abi, num_subbuf); | |
476 | crash_get_layout(layout, abi, mode); | |
477 | ||
478 | return 0; | |
479 | } | |
480 | ||
481 | static | |
482 | void print_dbg_magic(const uint8_t *magic) | |
483 | { | |
484 | DBG("magic: 0x%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X", | |
485 | magic[0], magic[1], magic[2], magic[3], | |
486 | magic[4], magic[5], magic[6], magic[7], | |
487 | magic[8], magic[9], magic[10], magic[11], | |
488 | magic[12], magic[13], magic[14], magic[15]); | |
489 | } | |
490 | ||
491 | static | |
492 | int check_magic(const uint8_t *magic) | |
493 | { | |
494 | int i; | |
495 | ||
496 | for (i = 0; i < RB_CRASH_DUMP_ABI_MAGIC_LEN; i++) { | |
497 | if ((magic[i] ^ 0xFF) != lttng_crash_expected_magic_xor[i]) { | |
498 | return -1; | |
499 | } | |
500 | } | |
501 | return 0; | |
502 | } | |
503 | ||
504 | static | |
505 | int get_crash_layout(struct lttng_crash_layout *layout, int fd) | |
506 | { | |
507 | char *map; | |
508 | int ret = 0, unmapret; | |
509 | const uint8_t *magic; | |
510 | uint64_t mmap_length; | |
511 | uint16_t major, minor; | |
512 | uint8_t word_size; | |
513 | const struct crash_abi_unknown *abi; | |
514 | uint16_t endian; | |
515 | enum lttng_crash_type layout_type; | |
516 | ||
517 | map = mmap(NULL, RB_CRASH_DUMP_ABI_LEN, PROT_READ, MAP_PRIVATE, | |
518 | fd, 0); | |
519 | if (map == MAP_FAILED) { | |
520 | PERROR("Mapping file"); | |
521 | return -1; | |
522 | } | |
523 | abi = (const struct crash_abi_unknown *) map; | |
524 | magic = crash_get_hdr_raw_field(layout, abi, magic); | |
525 | print_dbg_magic(magic); | |
526 | if (check_magic(magic)) { | |
527 | DBG("Unknown magic number"); | |
528 | ret = 1; /* positive return value, skip */ | |
529 | goto end; | |
530 | } | |
531 | endian = crash_get_hdr_field(layout, abi, endian); | |
532 | switch (endian) { | |
533 | case RB_CRASH_ENDIAN: | |
534 | break; | |
535 | case RB_CRASH_ENDIAN_REVERSE: | |
536 | layout->reverse_byte_order = 1; | |
537 | break; | |
538 | default: | |
539 | DBG("Unknown endianness value: 0x%X", (unsigned int) endian); | |
540 | ret = 1; /* positive return value, skip */ | |
541 | goto end; | |
542 | } | |
543 | layout_type = (enum lttng_crash_type) crash_get_hdr_field(layout, abi, layout_type); | |
544 | switch (layout_type) { | |
545 | case LTTNG_CRASH_TYPE_UST: | |
546 | break; | |
547 | case LTTNG_CRASH_TYPE_KERNEL: | |
548 | ERR("lttng-modules buffer layout support not implemented"); | |
549 | ret = 1; /* positive return value, skip */ | |
550 | goto end; | |
551 | default: | |
552 | ERR("Unknown layout type %u", (unsigned int) layout_type); | |
553 | ret = 1; /* positive return value, skip */ | |
554 | goto end; | |
555 | } | |
556 | mmap_length = crash_get_hdr_field(layout, abi, mmap_length); | |
557 | DBG("mmap_length: %" PRIu64, mmap_length); | |
558 | layout->mmap_length = mmap_length; | |
559 | major = crash_get_hdr_field(layout, abi, major); | |
560 | DBG("major: %u", major); | |
561 | minor = crash_get_hdr_field(layout, abi, minor); | |
562 | DBG("minor: %u", minor); | |
563 | word_size = crash_get_hdr_field(layout, abi, word_size); | |
564 | DBG("word_size: %u", word_size); | |
565 | switch (major) { | |
566 | case 0: | |
567 | switch (minor) { | |
568 | case 0: | |
569 | ret = get_crash_layout_0_0(layout, map); | |
570 | if (ret) | |
571 | goto end; | |
572 | break; | |
573 | default: | |
574 | ret = -1; | |
575 | ERR("Unsupported crash ABI %u.%u\n", major, minor); | |
576 | goto end; | |
577 | } | |
578 | break; | |
579 | default: | |
580 | ERR("Unsupported crash ABI %u.%u\n", major, minor); | |
581 | ret = -1; | |
582 | goto end; | |
583 | } | |
584 | layout->word_size = word_size; | |
585 | end: | |
586 | unmapret = munmap(map, RB_CRASH_DUMP_ABI_LEN); | |
587 | if (unmapret) { | |
588 | PERROR("munmap"); | |
589 | } | |
590 | return ret; | |
591 | } | |
592 | ||
593 | /* buf_trunc mask selects only the buffer number. */ | |
594 | static inline | |
595 | uint64_t buf_trunc(uint64_t offset, uint64_t buf_size) | |
596 | { | |
597 | return offset & ~(buf_size - 1); | |
598 | } | |
599 | ||
600 | /* subbuf_trunc mask selects the subbuffer number. */ | |
601 | static inline | |
602 | uint64_t subbuf_trunc(uint64_t offset, uint64_t subbuf_size) | |
603 | { | |
604 | return offset & ~(subbuf_size - 1); | |
605 | } | |
606 | ||
607 | /* buf_offset mask selects only the offset within the current buffer. */ | |
608 | static inline | |
609 | uint64_t buf_offset(uint64_t offset, uint64_t buf_size) | |
610 | { | |
611 | return offset & (buf_size - 1); | |
612 | } | |
613 | ||
614 | /* subbuf_offset mask selects the offset within the current subbuffer. */ | |
615 | static inline | |
616 | uint64_t subbuf_offset(uint64_t offset, uint64_t subbuf_size) | |
617 | { | |
618 | return offset & (subbuf_size - 1); | |
619 | } | |
620 | ||
621 | /* subbuf_index returns the index of the current subbuffer within the buffer. */ | |
622 | static inline | |
623 | uint64_t subbuf_index(uint64_t offset, uint64_t buf_size, uint64_t subbuf_size) | |
624 | { | |
625 | return buf_offset(offset, buf_size) / subbuf_size; | |
626 | } | |
627 | ||
628 | static inline | |
629 | uint64_t subbuffer_id_get_index(uint32_t mode, uint64_t id, | |
630 | unsigned int wl) | |
631 | { | |
632 | if (mode == RING_BUFFER_OVERWRITE) | |
633 | return id & SB_ID_INDEX_MASK(wl); | |
634 | else | |
635 | return id; | |
636 | } | |
637 | ||
638 | static | |
639 | int copy_crash_subbuf(const struct lttng_crash_layout *layout, | |
640 | int fd_dest, char *buf, uint64_t offset) | |
641 | { | |
642 | uint64_t buf_size, subbuf_size, num_subbuf, sbidx, id, | |
643 | sb_bindex, rpages_offset, p_offset, seq_cc, | |
644 | committed, commit_count_mask, consumed_cur, | |
645 | packet_size; | |
646 | char *subbuf_ptr; | |
647 | ssize_t writelen; | |
648 | ||
649 | /* | |
650 | * Get the current subbuffer by applying the proper mask to | |
651 | * "offset", and looking up the subbuf location within the | |
652 | * source file buf. | |
653 | */ | |
654 | ||
655 | buf_size = layout->buf_size; | |
656 | subbuf_size = layout->subbuf_size; | |
657 | num_subbuf = layout->num_subbuf; | |
658 | ||
659 | switch (layout->word_size) { | |
660 | case 4: commit_count_mask = 0xFFFFFFFFULL / num_subbuf; | |
661 | break; | |
662 | case 8: commit_count_mask = 0xFFFFFFFFFFFFFFFFULL / num_subbuf; | |
663 | break; | |
664 | default: | |
665 | ERR("Unsupported word size: %u", | |
666 | (unsigned int) layout->word_size); | |
667 | return -EINVAL; | |
668 | } | |
669 | ||
dd7cb87a | 670 | DBG("Copy crash subbuffer at offset %" PRIu64, offset); |
d7ba1388 MD |
671 | sbidx = subbuf_index(offset, buf_size, subbuf_size); |
672 | ||
673 | /* | |
674 | * Find where the seq cc is located. Compute length of data to | |
675 | * copy. | |
676 | */ | |
677 | seq_cc = crash_get_array_field(layout, buf, commit_hot_array, | |
678 | sbidx, commit_hot_seq); | |
679 | consumed_cur = crash_get_field(layout, buf, consumed_offset); | |
680 | ||
681 | /* | |
682 | * Check that the buffer we are getting is after or at | |
683 | * consumed_cur position. | |
684 | */ | |
685 | if ((long) subbuf_trunc(offset, subbuf_size) | |
686 | - (long) subbuf_trunc(consumed_cur, subbuf_size) < 0) { | |
687 | DBG("No data: position is before consumed_cur"); | |
688 | goto nodata; | |
689 | } | |
690 | ||
691 | /* | |
692 | * Check if subbuffer has been fully committed. | |
693 | */ | |
694 | if (((seq_cc - subbuf_size) & commit_count_mask) | |
695 | - (buf_trunc(offset, buf_size) / num_subbuf) | |
696 | == 0) { | |
697 | committed = subbuf_size; | |
698 | } else { | |
699 | committed = subbuf_offset(seq_cc, subbuf_size); | |
700 | if (!committed) { | |
701 | DBG("No data committed, seq_cc: %" PRIu64, seq_cc); | |
702 | goto nodata; | |
703 | } | |
704 | } | |
705 | ||
706 | /* Find actual physical offset in subbuffer table */ | |
707 | id = crash_get_array_field(layout, buf, buf_wsb_array, | |
708 | sbidx, buf_wsb_id); | |
709 | sb_bindex = subbuffer_id_get_index(layout->mode, id, | |
710 | layout->word_size); | |
711 | rpages_offset = crash_get_array_field(layout, buf, sb_array, | |
712 | sb_bindex, sb_array_shmp_offset); | |
713 | p_offset = crash_get_field(layout, buf + rpages_offset, | |
714 | sb_backend_p_offset); | |
715 | subbuf_ptr = buf + p_offset; | |
716 | ||
717 | if (committed == subbuf_size) { | |
718 | /* | |
719 | * Packet header can be used. | |
720 | */ | |
721 | if (layout->length.packet_size) { | |
722 | memcpy(&packet_size, | |
723 | subbuf_ptr + layout->offset.packet_size, | |
724 | layout->length.packet_size); | |
725 | if (layout->reverse_byte_order) { | |
726 | packet_size = __bswap_64(packet_size); | |
727 | } | |
728 | packet_size /= CHAR_BIT; | |
729 | } else { | |
730 | packet_size = subbuf_size; | |
731 | } | |
732 | } else { | |
733 | uint64_t patch_size; | |
734 | ||
735 | /* | |
736 | * Find where to patch the sub-buffer header with actual | |
737 | * readable data len and packet len, derived from seq | |
738 | * cc. Patch it in our in-memory copy. | |
739 | */ | |
740 | patch_size = committed * CHAR_BIT; | |
741 | if (layout->reverse_byte_order) { | |
742 | patch_size = __bswap_64(patch_size); | |
743 | } | |
744 | if (layout->length.content_size) { | |
745 | memcpy(subbuf_ptr + layout->offset.content_size, | |
746 | &patch_size, layout->length.content_size); | |
747 | } | |
748 | if (layout->length.packet_size) { | |
749 | memcpy(subbuf_ptr + layout->offset.packet_size, | |
750 | &patch_size, layout->length.packet_size); | |
751 | } | |
752 | packet_size = committed; | |
753 | } | |
754 | ||
755 | /* | |
756 | * Copy packet into fd_dest. | |
757 | */ | |
758 | writelen = lttng_write(fd_dest, subbuf_ptr, packet_size); | |
759 | if (writelen < packet_size) { | |
760 | PERROR("Error writing to output file"); | |
761 | return -1; | |
762 | } | |
763 | DBG("Copied %" PRIu64 " bytes of data", packet_size); | |
764 | return 0; | |
765 | ||
766 | nodata: | |
767 | return -ENODATA; | |
768 | } | |
769 | ||
770 | static | |
771 | int copy_crash_data(const struct lttng_crash_layout *layout, int fd_dest, | |
772 | int fd_src) | |
773 | { | |
774 | char *buf; | |
775 | int ret = 0, has_data = 0; | |
776 | struct stat statbuf; | |
777 | size_t src_file_len; | |
778 | uint64_t prod_offset, consumed_offset; | |
779 | uint64_t offset, subbuf_size; | |
780 | ssize_t readlen; | |
781 | ||
782 | ret = fstat(fd_src, &statbuf); | |
783 | if (ret) { | |
784 | return ret; | |
785 | } | |
786 | src_file_len = layout->mmap_length; | |
787 | buf = zmalloc(src_file_len); | |
788 | if (!buf) { | |
789 | return -1; | |
790 | } | |
791 | readlen = lttng_read(fd_src, buf, src_file_len); | |
792 | if (readlen < 0) { | |
793 | PERROR("Error reading input file"); | |
309fc9bf MD |
794 | ret = -1; |
795 | goto end; | |
d7ba1388 MD |
796 | } |
797 | ||
798 | prod_offset = crash_get_field(layout, buf, prod_offset); | |
799 | DBG("prod_offset: 0x%" PRIx64, prod_offset); | |
800 | consumed_offset = crash_get_field(layout, buf, consumed_offset); | |
801 | DBG("consumed_offset: 0x%" PRIx64, consumed_offset); | |
802 | subbuf_size = layout->subbuf_size; | |
803 | ||
804 | for (offset = consumed_offset; offset < prod_offset; | |
805 | offset += subbuf_size) { | |
806 | ret = copy_crash_subbuf(layout, fd_dest, buf, offset); | |
807 | if (!ret) { | |
808 | has_data = 1; | |
809 | } | |
810 | if (ret) { | |
811 | goto end; | |
812 | } | |
813 | } | |
814 | end: | |
815 | free(buf); | |
816 | if (ret && ret != -ENODATA) { | |
817 | return ret; | |
818 | } | |
819 | if (has_data) { | |
820 | return 0; | |
821 | } else { | |
822 | return -ENODATA; | |
823 | } | |
824 | } | |
825 | ||
826 | static | |
827 | int extract_file(int output_dir_fd, const char *output_file, | |
828 | int input_dir_fd, const char *input_file) | |
829 | { | |
830 | int fd_dest, fd_src, ret = 0, closeret; | |
831 | struct lttng_crash_layout layout; | |
832 | ||
833 | layout.reverse_byte_order = 0; /* For reading magic number */ | |
834 | ||
835 | DBG("Extract file '%s'", input_file); | |
836 | fd_src = openat(input_dir_fd, input_file, O_RDONLY); | |
837 | if (fd_src < 0) { | |
838 | PERROR("Error opening '%s' for reading", | |
839 | input_file); | |
840 | ret = -1; | |
841 | goto end; | |
842 | } | |
843 | ||
844 | /* Query the crash ABI layout */ | |
845 | ret = get_crash_layout(&layout, fd_src); | |
846 | if (ret) { | |
847 | goto close_src; | |
848 | } | |
849 | ||
850 | fd_dest = openat(output_dir_fd, output_file, | |
851 | O_RDWR | O_CREAT | O_EXCL, | |
852 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
853 | if (fd_dest < 0) { | |
854 | PERROR("Error opening '%s' for writing", | |
855 | output_file); | |
856 | ret = -1; | |
857 | goto close_src; | |
858 | } | |
859 | ||
860 | ret = copy_crash_data(&layout, fd_dest, fd_src); | |
861 | if (ret) { | |
862 | goto close_dest; | |
863 | } | |
864 | ||
865 | close_dest: | |
866 | closeret = close(fd_dest); | |
867 | if (closeret) { | |
868 | PERROR("close"); | |
869 | } | |
870 | if (ret == -ENODATA) { | |
871 | closeret = unlinkat(output_dir_fd, output_file, 0); | |
872 | if (closeret) { | |
873 | PERROR("unlinkat"); | |
874 | } | |
875 | } | |
876 | close_src: | |
877 | closeret = close(fd_src); | |
878 | if (closeret) { | |
879 | PERROR("close"); | |
880 | } | |
881 | end: | |
882 | return ret; | |
883 | } | |
884 | ||
885 | static | |
886 | int extract_all_files(const char *output_path, | |
887 | const char *input_path) | |
888 | { | |
889 | DIR *input_dir, *output_dir; | |
890 | int input_dir_fd, output_dir_fd, ret = 0, closeret; | |
891 | struct dirent *entry; /* input */ | |
892 | ||
893 | /* Open input directory */ | |
894 | input_dir = opendir(input_path); | |
895 | if (!input_dir) { | |
896 | PERROR("Cannot open '%s' path", input_path); | |
897 | return -1; | |
898 | } | |
899 | input_dir_fd = dirfd(input_dir); | |
900 | if (input_dir_fd < 0) { | |
901 | PERROR("dirfd"); | |
902 | return -1; | |
903 | } | |
904 | ||
905 | /* Open output directory */ | |
906 | output_dir = opendir(output_path); | |
907 | if (!output_dir) { | |
908 | PERROR("Cannot open '%s' path", output_path); | |
909 | return -1; | |
910 | } | |
911 | output_dir_fd = dirfd(output_dir); | |
912 | if (output_dir_fd < 0) { | |
913 | PERROR("dirfd"); | |
914 | return -1; | |
915 | } | |
916 | ||
917 | while ((entry = readdir(input_dir))) { | |
918 | if (!strcmp(entry->d_name, ".") | |
919 | || !strcmp(entry->d_name, "..")) | |
920 | continue; | |
921 | ret = extract_file(output_dir_fd, entry->d_name, | |
922 | input_dir_fd, entry->d_name); | |
923 | if (ret == -ENODATA) { | |
924 | DBG("No data in file '%s', skipping", entry->d_name); | |
925 | ret = 0; | |
926 | continue; | |
927 | } else if (ret < 0) { | |
928 | break; | |
929 | } else if (ret > 0) { | |
930 | DBG("Skipping file '%s'", entry->d_name); | |
931 | ret = 0; | |
932 | continue; | |
933 | } | |
934 | } | |
935 | closeret = closedir(output_dir); | |
936 | if (closeret) { | |
937 | PERROR("closedir"); | |
938 | } | |
939 | closeret = closedir(input_dir); | |
940 | if (closeret) { | |
941 | PERROR("closedir"); | |
942 | } | |
943 | return ret; | |
944 | } | |
945 | ||
946 | static | |
947 | int extract_one_trace(const char *output_path, | |
948 | const char *input_path) | |
949 | { | |
950 | char dest[PATH_MAX], src[PATH_MAX]; | |
951 | int ret; | |
952 | ||
953 | DBG("Extract crash trace '%s' into '%s'", input_path, output_path); | |
954 | ||
955 | /* Copy metadata */ | |
956 | strncpy(dest, output_path, PATH_MAX); | |
957 | dest[PATH_MAX - 1] = '\0'; | |
958 | strncat(dest, "/metadata", PATH_MAX - strlen(dest) - 1); | |
959 | ||
960 | strncpy(src, input_path, PATH_MAX); | |
961 | src[PATH_MAX - 1] = '\0'; | |
962 | strncat(src, "/metadata", PATH_MAX - strlen(dest) - 1); | |
963 | ||
964 | ret = copy_file(dest, src); | |
965 | if (ret) { | |
966 | return ret; | |
967 | } | |
968 | ||
969 | /* Extract each other file that has expected header */ | |
970 | return extract_all_files(output_path, input_path); | |
971 | } | |
972 | ||
973 | static | |
a424227e MD |
974 | int extract_trace_recursive(const char *output_path, |
975 | const char *input_path) | |
d7ba1388 | 976 | { |
a424227e MD |
977 | DIR *dir; |
978 | int dir_fd, ret = 0, closeret; | |
979 | struct dirent *entry; | |
d7ba1388 MD |
980 | int has_warning = 0; |
981 | ||
a424227e MD |
982 | /* Open directory */ |
983 | dir = opendir(input_path); | |
984 | if (!dir) { | |
985 | PERROR("Cannot open '%s' path", input_path); | |
986 | return -1; | |
987 | } | |
988 | dir_fd = dirfd(dir); | |
989 | if (dir_fd < 0) { | |
990 | PERROR("dirfd"); | |
d7ba1388 MD |
991 | return -1; |
992 | } | |
993 | ||
a424227e MD |
994 | while ((entry = readdir(dir))) { |
995 | if (!strcmp(entry->d_name, ".") | |
996 | || !strcmp(entry->d_name, "..")) { | |
997 | continue; | |
998 | } | |
999 | switch (entry->d_type) { | |
1000 | case DT_DIR: | |
1001 | { | |
1002 | char output_subpath[PATH_MAX]; | |
1003 | char input_subpath[PATH_MAX]; | |
1004 | ||
1005 | strncpy(output_subpath, output_path, | |
1006 | sizeof(output_subpath)); | |
1007 | output_subpath[sizeof(output_subpath) - 1] = '\0'; | |
1008 | strncat(output_subpath, "/", | |
1009 | sizeof(output_subpath) - strlen(output_subpath) - 1); | |
1010 | strncat(output_subpath, entry->d_name, | |
1011 | sizeof(output_subpath) - strlen(output_subpath) - 1); | |
1012 | ||
1013 | ret = mkdir(output_subpath, S_IRWXU | S_IRWXG); | |
1014 | if (ret) { | |
1015 | PERROR("mkdir"); | |
1016 | has_warning = 1; | |
1017 | goto end; | |
1018 | } | |
1019 | ||
1020 | strncpy(input_subpath, input_path, | |
1021 | sizeof(input_subpath)); | |
1022 | input_subpath[sizeof(input_subpath) - 1] = '\0'; | |
1023 | strncat(input_subpath, "/", | |
1024 | sizeof(input_subpath) - strlen(input_subpath) - 1); | |
1025 | strncat(input_subpath, entry->d_name, | |
1026 | sizeof(input_subpath) - strlen(input_subpath) - 1); | |
1027 | ||
1028 | ret = extract_trace_recursive(output_subpath, | |
1029 | input_subpath); | |
29786dfa JG |
1030 | if (ret) { |
1031 | has_warning = 1; | |
1032 | } | |
a424227e MD |
1033 | break; |
1034 | } | |
1035 | case DT_REG: | |
e507f57f | 1036 | case DT_LNK: |
a424227e MD |
1037 | if (!strcmp(entry->d_name, "metadata")) { |
1038 | ret = extract_one_trace(output_path, | |
1039 | input_path); | |
1040 | if (ret) { | |
1041 | WARN("Error extracting trace '%s', continuing anyway.", | |
1042 | input_path); | |
1043 | has_warning = 1; | |
1044 | } | |
1045 | } | |
1046 | /* Ignore other files */ | |
1047 | break; | |
1048 | default: | |
d7ba1388 | 1049 | has_warning = 1; |
a424227e | 1050 | goto end; |
d7ba1388 MD |
1051 | } |
1052 | } | |
a424227e MD |
1053 | end: |
1054 | closeret = closedir(dir); | |
1055 | if (closeret) { | |
1056 | PERROR("closedir"); | |
1057 | } | |
d7ba1388 MD |
1058 | return has_warning; |
1059 | } | |
1060 | ||
1061 | static | |
1189c882 | 1062 | int delete_dir_recursive(const char *path) |
d7ba1388 | 1063 | { |
1189c882 JR |
1064 | DIR *dir; |
1065 | int dir_fd, ret = 0, closeret; | |
d7ba1388 MD |
1066 | struct dirent *entry; |
1067 | ||
1068 | /* Open trace directory */ | |
1189c882 JR |
1069 | dir = opendir(path); |
1070 | if (!dir) { | |
1071 | PERROR("Cannot open '%s' path", path); | |
47282601 JG |
1072 | ret = -errno; |
1073 | goto end; | |
d7ba1388 | 1074 | } |
1189c882 JR |
1075 | dir_fd = dirfd(dir); |
1076 | if (dir_fd < 0) { | |
d7ba1388 | 1077 | PERROR("dirfd"); |
47282601 JG |
1078 | ret = -errno; |
1079 | goto end; | |
d7ba1388 MD |
1080 | } |
1081 | ||
1189c882 | 1082 | while ((entry = readdir(dir))) { |
d7ba1388 | 1083 | if (!strcmp(entry->d_name, ".") |
a424227e | 1084 | || !strcmp(entry->d_name, "..")) { |
d7ba1388 | 1085 | continue; |
a424227e | 1086 | } |
d7ba1388 MD |
1087 | switch (entry->d_type) { |
1088 | case DT_DIR: | |
1189c882 JR |
1089 | { |
1090 | char *subpath = zmalloc(PATH_MAX); | |
1091 | ||
1092 | if (!subpath) { | |
1093 | PERROR("zmalloc path"); | |
1094 | ret = -1; | |
1095 | goto end; | |
1096 | } | |
1097 | strncpy(subpath, path, PATH_MAX); | |
1098 | subpath[PATH_MAX - 1] = '\0'; | |
1099 | strncat(subpath, "/", | |
1100 | PATH_MAX - strlen(subpath) - 1); | |
1101 | strncat(subpath, entry->d_name, | |
1102 | PATH_MAX - strlen(subpath) - 1); | |
1103 | ||
1104 | ret = delete_dir_recursive(subpath); | |
1105 | free(subpath); | |
1106 | if (ret) { | |
1107 | /* Error occured, abort traversal. */ | |
1108 | goto end; | |
1109 | } | |
d7ba1388 | 1110 | break; |
1189c882 | 1111 | } |
d7ba1388 | 1112 | case DT_REG: |
1189c882 JR |
1113 | ret = unlinkat(dir_fd, entry->d_name, 0); |
1114 | if (ret) { | |
1115 | PERROR("Unlinking '%s'", entry->d_name); | |
1116 | goto end; | |
1117 | } | |
d7ba1388 MD |
1118 | break; |
1119 | default: | |
1120 | ret = -EINVAL; | |
1121 | goto end; | |
1122 | } | |
1123 | } | |
1124 | end: | |
1189c882 JR |
1125 | if (!ret) { |
1126 | ret = rmdir(path); | |
1127 | if (ret) { | |
1128 | PERROR("rmdir '%s'", path); | |
1129 | } | |
1130 | } | |
1131 | closeret = closedir(dir); | |
d7ba1388 MD |
1132 | if (closeret) { |
1133 | PERROR("closedir"); | |
1134 | } | |
d7ba1388 MD |
1135 | return ret; |
1136 | } | |
1137 | ||
d7ba1388 MD |
1138 | static |
1139 | int view_trace(const char *viewer_path, const char *trace_path) | |
1140 | { | |
1141 | pid_t pid; | |
1142 | ||
1143 | pid = fork(); | |
1144 | if (pid < 0) { | |
1145 | /* Error */ | |
1146 | PERROR("fork"); | |
1147 | return -1; | |
1148 | } else if (pid > 0) { | |
1149 | /* Parent */ | |
1150 | int status; | |
1151 | ||
1152 | pid = waitpid(pid, &status, 0); | |
1153 | if (pid < 0 || !WIFEXITED(status)) { | |
1154 | return -1; | |
1155 | } | |
1156 | } else { | |
1157 | /* Child */ | |
1158 | int ret; | |
1159 | ||
1160 | ret = execlp(viewer_path, viewer_path, | |
1161 | trace_path, (char *) NULL); | |
1162 | if (ret) { | |
1163 | PERROR("execlp"); | |
1164 | exit(EXIT_FAILURE); | |
1165 | } | |
1166 | exit(EXIT_SUCCESS); /* Never reached */ | |
1167 | } | |
1168 | return 0; | |
1169 | } | |
1170 | ||
1171 | /* | |
1172 | * main | |
1173 | */ | |
1174 | int main(int argc, char *argv[]) | |
1175 | { | |
1176 | int ret, has_warning = 0; | |
1177 | const char *output_path = NULL; | |
1178 | char tmppath[] = "/tmp/lttng-crash-XXXXXX"; | |
1179 | ||
1180 | progname = argv[0] ? argv[0] : "lttng-crash"; | |
1181 | ||
1182 | ret = parse_args(argc, argv); | |
1183 | if (ret > 0) { | |
1184 | exit(EXIT_SUCCESS); | |
1185 | } else if (ret < 0) { | |
1186 | exit(EXIT_FAILURE); | |
1187 | } | |
1188 | ||
1189 | if (opt_output_path) { | |
1190 | output_path = opt_output_path; | |
1191 | ret = mkdir(output_path, S_IRWXU | S_IRWXG); | |
1192 | if (ret) { | |
1193 | PERROR("mkdir"); | |
1194 | exit(EXIT_FAILURE); | |
1195 | } | |
1196 | } else { | |
1197 | output_path = mkdtemp(tmppath); | |
1198 | if (!output_path) { | |
1199 | PERROR("mkdtemp"); | |
1200 | exit(EXIT_FAILURE); | |
1201 | } | |
1202 | } | |
1203 | ||
a424227e | 1204 | ret = extract_trace_recursive(output_path, input_path); |
d7ba1388 MD |
1205 | if (ret < 0) { |
1206 | exit(EXIT_FAILURE); | |
1207 | } else if (ret > 0) { | |
1208 | has_warning = 1; | |
1209 | } | |
1210 | if (!opt_output_path) { | |
1211 | /* View trace */ | |
1212 | ret = view_trace(opt_viewer_path, output_path); | |
1213 | if (ret) | |
1214 | has_warning = 1; | |
1215 | ||
1216 | /* unlink temporary trace */ | |
1189c882 JR |
1217 | ret = delete_dir_recursive(output_path); |
1218 | if (ret) { | |
d7ba1388 | 1219 | has_warning = 1; |
1189c882 | 1220 | } |
d7ba1388 MD |
1221 | } |
1222 | if (has_warning) | |
1223 | exit(EXIT_FAILURE); | |
1224 | exit(EXIT_SUCCESS); | |
1225 | } |