Backport: Use EXTRA_VERSION_NAME and EXTRA_VERSION_DESCRIPTION
[lttng-tools.git] / src / bin / lttng-crash / lttng-crash.c
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 #include <getopt.h>
20 #include <signal.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/mman.h>
28 #include <fcntl.h>
29 #include <sys/wait.h>
30 #include <unistd.h>
31 #include <ctype.h>
32 #include <dirent.h>
33 #include <byteswap.h>
34 #include <inttypes.h>
35 #include <stdbool.h>
36
37 #include <version.h>
38 #include <lttng/lttng.h>
39 #include <common/common.h>
40 #include <common/utils.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];
106 uint64_t mmap_length; /* Overall length of crash record */
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,
186 *opt_viewer_path = NULL,
187 *opt_output_path = NULL;
188
189 static char *input_path;
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(void)
209 {
210 int ret = utils_show_man_page(1, "lttng-crash");
211
212 if (ret) {
213 ERR("Cannot view man page lttng-crash(1)");
214 perror("exec");
215 exit(EXIT_FAILURE);
216 }
217 }
218
219 static void version(FILE *ofp)
220 {
221 fprintf(ofp, "%s (LTTng Crash Trace Viewer) " VERSION " - " VERSION_NAME
222 "%s%s\n",
223 progname,
224 GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION,
225 EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " EXTRA_VERSION_NAME);
226 }
227
228 /*
229 * list_options
230 *
231 * List options line by line. This is mostly for bash auto completion and to
232 * avoid difficult parsing.
233 */
234 static void list_options(FILE *ofp)
235 {
236 int i = 0;
237 struct option *option = NULL;
238
239 option = &long_options[i];
240 while (option->name != NULL) {
241 fprintf(ofp, "--%s\n", option->name);
242
243 if (isprint(option->val)) {
244 fprintf(ofp, "-%c\n", option->val);
245 }
246
247 i++;
248 option = &long_options[i];
249 }
250 }
251
252 /*
253 * Parse command line arguments.
254 *
255 * Return 0 if OK, else -1
256 */
257 static int parse_args(int argc, char **argv)
258 {
259 int opt, ret = 0;
260
261 if (argc < 2) {
262 usage();
263 exit(EXIT_FAILURE);
264 }
265
266 while ((opt = getopt_long(argc, argv, "+Vhve:x:", long_options, NULL)) != -1) {
267 switch (opt) {
268 case 'V':
269 version(stdout);
270 ret = 1;
271 goto end;
272 case 'h':
273 usage();
274 ret = 1;
275 goto end;
276 case 'v':
277 /* There is only 3 possible level of verbosity. (-vvv) */
278 if (lttng_opt_verbose < 3) {
279 lttng_opt_verbose += 1;
280 }
281 break;
282 case 'e':
283 free(opt_viewer_path);
284 opt_viewer_path = strdup(optarg);
285 break;
286 case 'x':
287 free(opt_output_path);
288 opt_output_path = strdup(optarg);
289 break;
290 case OPT_DUMP_OPTIONS:
291 list_options(stdout);
292 ret = 1;
293 goto end;
294 default:
295 ERR("Unknown command-line option");
296 goto error;
297 }
298 }
299
300 if (!opt_viewer_path) {
301 opt_viewer_path = DEFAULT_VIEWER;
302 }
303
304 /* No leftovers, or more than one input path, print usage and quit */
305 if (argc - optind != 1) {
306 ERR("Command-line error: Specify exactly one input path");
307 goto error;
308 }
309
310 input_path = argv[optind];
311 end:
312 return ret;
313
314 error:
315 return -1;
316 }
317
318 static
319 int copy_file(const char *file_dest, const char *file_src)
320 {
321 int fd_src = -1, fd_dest = -1;
322 ssize_t readlen, writelen;
323 char buf[COPY_BUFLEN];
324 int ret;
325
326 DBG("Copy metadata file '%s' into '%s'", file_src, file_dest);
327
328 fd_src = open(file_src, O_RDONLY);
329 if (fd_src < 0) {
330 PERROR("Error opening %s for reading", file_src);
331 ret = -errno;
332 goto error;
333 }
334 fd_dest = open(file_dest, O_RDWR | O_CREAT | O_EXCL,
335 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
336 if (fd_dest < 0) {
337 PERROR("Error opening %s for writing", file_dest);
338 ret = -errno;
339 goto error;
340 }
341
342 for (;;) {
343 readlen = lttng_read(fd_src, buf, COPY_BUFLEN);
344 if (readlen < 0) {
345 PERROR("Error reading input file");
346 ret = -1;
347 goto error;
348 }
349 if (!readlen) {
350 break;
351 }
352 writelen = lttng_write(fd_dest, buf, readlen);
353 if (writelen < readlen) {
354 PERROR("Error writing to output file");
355 ret = -1;
356 goto error;
357 }
358 }
359
360 ret = 0;
361 error:
362 if (fd_src >= 0) {
363 if (close(fd_src) < 0) {
364 PERROR("Error closing %s", file_src);
365 }
366 }
367
368 if (fd_dest >= 0) {
369 if (close(fd_dest) < 0) {
370 PERROR("Error closing %s", file_dest);
371 }
372 }
373 return ret;
374 }
375
376 static
377 uint64_t _crash_get_field(const struct lttng_crash_layout *layout,
378 const char *ptr, size_t size)
379 {
380 switch (size) {
381 case 1: return *(uint8_t *) ptr;
382 case 2: if (layout->reverse_byte_order) {
383 return __bswap_16(*(uint16_t *) ptr);
384 } else {
385 return *(uint16_t *) ptr;
386
387 }
388 case 4: if (layout->reverse_byte_order) {
389 return __bswap_32(*(uint32_t *) ptr);
390 } else {
391 return *(uint32_t *) ptr;
392
393 }
394 case 8: if (layout->reverse_byte_order) {
395 return __bswap_64(*(uint64_t *) ptr);
396 } else {
397 return *(uint64_t *) ptr;
398 }
399 default:
400 abort();
401 return -1;
402 }
403
404 }
405
406 #define crash_get_field(layout, map, name) \
407 _crash_get_field(layout, (map) + (layout)->offset.name, \
408 layout->length.name)
409
410 #define crash_get_array_field(layout, map, array_name, idx, field_name) \
411 _crash_get_field(layout, \
412 (map) + (layout)->offset.array_name \
413 + (idx * (layout)->stride.array_name) \
414 + (layout)->offset.field_name, \
415 (layout)->length.field_name)
416
417 #define crash_get_hdr_raw_field(layout, hdr, name) ((hdr)->name)
418
419 #define crash_get_hdr_field(layout, hdr, name) \
420 _crash_get_field(layout, (const char *) &(hdr)->name, \
421 sizeof((hdr)->name))
422
423 #define crash_get_layout(layout, hdr, name) \
424 do { \
425 (layout)->name = crash_get_hdr_field(layout, hdr, \
426 name); \
427 DBG("layout.%s = %" PRIu64, #name, \
428 (uint64_t) (layout)->name); \
429 } while (0)
430
431 static
432 int get_crash_layout_0_0(struct lttng_crash_layout *layout,
433 char *map)
434 {
435 const struct crash_abi_0_0 *abi = (const struct crash_abi_0_0 *) map;
436
437 crash_get_layout(layout, abi, offset.prod_offset);
438 crash_get_layout(layout, abi, offset.consumed_offset);
439 crash_get_layout(layout, abi, offset.commit_hot_array);
440 crash_get_layout(layout, abi, offset.commit_hot_seq);
441 crash_get_layout(layout, abi, offset.buf_wsb_array);
442 crash_get_layout(layout, abi, offset.buf_wsb_id);
443 crash_get_layout(layout, abi, offset.sb_array);
444 crash_get_layout(layout, abi, offset.sb_array_shmp_offset);
445 crash_get_layout(layout, abi, offset.sb_backend_p_offset);
446 crash_get_layout(layout, abi, offset.content_size);
447 crash_get_layout(layout, abi, offset.packet_size);
448
449 crash_get_layout(layout, abi, length.prod_offset);
450 crash_get_layout(layout, abi, length.consumed_offset);
451 crash_get_layout(layout, abi, length.commit_hot_seq);
452 crash_get_layout(layout, abi, length.buf_wsb_id);
453 crash_get_layout(layout, abi, length.sb_array_shmp_offset);
454 crash_get_layout(layout, abi, length.sb_backend_p_offset);
455 crash_get_layout(layout, abi, length.content_size);
456 crash_get_layout(layout, abi, length.packet_size);
457
458 crash_get_layout(layout, abi, stride.commit_hot_array);
459 crash_get_layout(layout, abi, stride.buf_wsb_array);
460 crash_get_layout(layout, abi, stride.sb_array);
461
462 crash_get_layout(layout, abi, buf_size);
463 crash_get_layout(layout, abi, subbuf_size);
464 crash_get_layout(layout, abi, num_subbuf);
465 crash_get_layout(layout, abi, mode);
466
467 return 0;
468 }
469
470 static
471 void print_dbg_magic(const uint8_t *magic)
472 {
473 DBG("magic: 0x%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X",
474 magic[0], magic[1], magic[2], magic[3],
475 magic[4], magic[5], magic[6], magic[7],
476 magic[8], magic[9], magic[10], magic[11],
477 magic[12], magic[13], magic[14], magic[15]);
478 }
479
480 static
481 int check_magic(const uint8_t *magic)
482 {
483 int i;
484
485 for (i = 0; i < RB_CRASH_DUMP_ABI_MAGIC_LEN; i++) {
486 if ((magic[i] ^ 0xFF) != lttng_crash_expected_magic_xor[i]) {
487 return -1;
488 }
489 }
490 return 0;
491 }
492
493 static
494 int get_crash_layout(struct lttng_crash_layout *layout, int fd)
495 {
496 char *map;
497 int ret = 0, unmapret;
498 const uint8_t *magic;
499 uint64_t mmap_length;
500 uint16_t major, minor;
501 uint8_t word_size;
502 const struct crash_abi_unknown *abi;
503 uint16_t endian;
504 enum lttng_crash_type layout_type;
505
506 map = mmap(NULL, RB_CRASH_DUMP_ABI_LEN, PROT_READ, MAP_PRIVATE,
507 fd, 0);
508 if (map == MAP_FAILED) {
509 PERROR("Mapping file");
510 return -1;
511 }
512 abi = (const struct crash_abi_unknown *) map;
513 magic = crash_get_hdr_raw_field(layout, abi, magic);
514 print_dbg_magic(magic);
515 if (check_magic(magic)) {
516 DBG("Unknown magic number");
517 ret = 1; /* positive return value, skip */
518 goto end;
519 }
520 endian = crash_get_hdr_field(layout, abi, endian);
521 switch (endian) {
522 case RB_CRASH_ENDIAN:
523 break;
524 case RB_CRASH_ENDIAN_REVERSE:
525 layout->reverse_byte_order = 1;
526 break;
527 default:
528 DBG("Unknown endianness value: 0x%X", (unsigned int) endian);
529 ret = 1; /* positive return value, skip */
530 goto end;
531 }
532 layout_type = (enum lttng_crash_type) crash_get_hdr_field(layout, abi, layout_type);
533 switch (layout_type) {
534 case LTTNG_CRASH_TYPE_UST:
535 break;
536 case LTTNG_CRASH_TYPE_KERNEL:
537 ERR("lttng-modules buffer layout support not implemented");
538 ret = 1; /* positive return value, skip */
539 goto end;
540 default:
541 ERR("Unknown layout type %u", (unsigned int) layout_type);
542 ret = 1; /* positive return value, skip */
543 goto end;
544 }
545 mmap_length = crash_get_hdr_field(layout, abi, mmap_length);
546 DBG("mmap_length: %" PRIu64, mmap_length);
547 layout->mmap_length = mmap_length;
548 major = crash_get_hdr_field(layout, abi, major);
549 DBG("major: %u", major);
550 minor = crash_get_hdr_field(layout, abi, minor);
551 DBG("minor: %u", minor);
552 word_size = crash_get_hdr_field(layout, abi, word_size);
553 DBG("word_size: %u", word_size);
554 switch (major) {
555 case 0:
556 switch (minor) {
557 case 0:
558 ret = get_crash_layout_0_0(layout, map);
559 if (ret)
560 goto end;
561 break;
562 default:
563 ret = -1;
564 ERR("Unsupported crash ABI %u.%u\n", major, minor);
565 goto end;
566 }
567 break;
568 default:
569 ERR("Unsupported crash ABI %u.%u\n", major, minor);
570 ret = -1;
571 goto end;
572 }
573 layout->word_size = word_size;
574 end:
575 unmapret = munmap(map, RB_CRASH_DUMP_ABI_LEN);
576 if (unmapret) {
577 PERROR("munmap");
578 }
579 return ret;
580 }
581
582 /* buf_trunc mask selects only the buffer number. */
583 static inline
584 uint64_t buf_trunc(uint64_t offset, uint64_t buf_size)
585 {
586 return offset & ~(buf_size - 1);
587 }
588
589 /* subbuf_trunc mask selects the subbuffer number. */
590 static inline
591 uint64_t subbuf_trunc(uint64_t offset, uint64_t subbuf_size)
592 {
593 return offset & ~(subbuf_size - 1);
594 }
595
596 /* buf_offset mask selects only the offset within the current buffer. */
597 static inline
598 uint64_t buf_offset(uint64_t offset, uint64_t buf_size)
599 {
600 return offset & (buf_size - 1);
601 }
602
603 /* subbuf_offset mask selects the offset within the current subbuffer. */
604 static inline
605 uint64_t subbuf_offset(uint64_t offset, uint64_t subbuf_size)
606 {
607 return offset & (subbuf_size - 1);
608 }
609
610 /* subbuf_index returns the index of the current subbuffer within the buffer. */
611 static inline
612 uint64_t subbuf_index(uint64_t offset, uint64_t buf_size, uint64_t subbuf_size)
613 {
614 return buf_offset(offset, buf_size) / subbuf_size;
615 }
616
617 static inline
618 uint64_t subbuffer_id_get_index(uint32_t mode, uint64_t id,
619 unsigned int wl)
620 {
621 if (mode == RING_BUFFER_OVERWRITE)
622 return id & SB_ID_INDEX_MASK(wl);
623 else
624 return id;
625 }
626
627 static
628 int copy_crash_subbuf(const struct lttng_crash_layout *layout,
629 int fd_dest, char *buf, uint64_t offset)
630 {
631 uint64_t buf_size, subbuf_size, num_subbuf, sbidx, id,
632 sb_bindex, rpages_offset, p_offset, seq_cc,
633 committed, commit_count_mask, consumed_cur,
634 packet_size;
635 char *subbuf_ptr;
636 ssize_t writelen;
637
638 /*
639 * Get the current subbuffer by applying the proper mask to
640 * "offset", and looking up the subbuf location within the
641 * source file buf.
642 */
643
644 buf_size = layout->buf_size;
645 subbuf_size = layout->subbuf_size;
646 num_subbuf = layout->num_subbuf;
647
648 switch (layout->word_size) {
649 case 4: commit_count_mask = 0xFFFFFFFFULL / num_subbuf;
650 break;
651 case 8: commit_count_mask = 0xFFFFFFFFFFFFFFFFULL / num_subbuf;
652 break;
653 default:
654 ERR("Unsupported word size: %u",
655 (unsigned int) layout->word_size);
656 return -EINVAL;
657 }
658
659 DBG("Copy crash subbuffer at offset %" PRIu64, offset);
660 sbidx = subbuf_index(offset, buf_size, subbuf_size);
661
662 /*
663 * Find where the seq cc is located. Compute length of data to
664 * copy.
665 */
666 seq_cc = crash_get_array_field(layout, buf, commit_hot_array,
667 sbidx, commit_hot_seq);
668 consumed_cur = crash_get_field(layout, buf, consumed_offset);
669
670 /*
671 * Check that the buffer we are getting is after or at
672 * consumed_cur position.
673 */
674 if ((long) subbuf_trunc(offset, subbuf_size)
675 - (long) subbuf_trunc(consumed_cur, subbuf_size) < 0) {
676 DBG("No data: position is before consumed_cur");
677 goto nodata;
678 }
679
680 /*
681 * Check if subbuffer has been fully committed.
682 */
683 if (((seq_cc - subbuf_size) & commit_count_mask)
684 - (buf_trunc(offset, buf_size) / num_subbuf)
685 == 0) {
686 committed = subbuf_size;
687 } else {
688 committed = subbuf_offset(seq_cc, subbuf_size);
689 if (!committed) {
690 DBG("No data committed, seq_cc: %" PRIu64, seq_cc);
691 goto nodata;
692 }
693 }
694
695 /* Find actual physical offset in subbuffer table */
696 id = crash_get_array_field(layout, buf, buf_wsb_array,
697 sbidx, buf_wsb_id);
698 sb_bindex = subbuffer_id_get_index(layout->mode, id,
699 layout->word_size);
700 rpages_offset = crash_get_array_field(layout, buf, sb_array,
701 sb_bindex, sb_array_shmp_offset);
702 p_offset = crash_get_field(layout, buf + rpages_offset,
703 sb_backend_p_offset);
704 subbuf_ptr = buf + p_offset;
705
706 if (committed == subbuf_size) {
707 /*
708 * Packet header can be used.
709 */
710 if (layout->length.packet_size) {
711 memcpy(&packet_size,
712 subbuf_ptr + layout->offset.packet_size,
713 layout->length.packet_size);
714 if (layout->reverse_byte_order) {
715 packet_size = __bswap_64(packet_size);
716 }
717 packet_size /= CHAR_BIT;
718 } else {
719 packet_size = subbuf_size;
720 }
721 } else {
722 uint64_t patch_size;
723
724 /*
725 * Find where to patch the sub-buffer header with actual
726 * readable data len and packet len, derived from seq
727 * cc. Patch it in our in-memory copy.
728 */
729 patch_size = committed * CHAR_BIT;
730 if (layout->reverse_byte_order) {
731 patch_size = __bswap_64(patch_size);
732 }
733 if (layout->length.content_size) {
734 memcpy(subbuf_ptr + layout->offset.content_size,
735 &patch_size, layout->length.content_size);
736 }
737 if (layout->length.packet_size) {
738 memcpy(subbuf_ptr + layout->offset.packet_size,
739 &patch_size, layout->length.packet_size);
740 }
741 packet_size = committed;
742 }
743
744 /*
745 * Copy packet into fd_dest.
746 */
747 writelen = lttng_write(fd_dest, subbuf_ptr, packet_size);
748 if (writelen < packet_size) {
749 PERROR("Error writing to output file");
750 return -1;
751 }
752 DBG("Copied %" PRIu64 " bytes of data", packet_size);
753 return 0;
754
755 nodata:
756 return -ENODATA;
757 }
758
759 static
760 int copy_crash_data(const struct lttng_crash_layout *layout, int fd_dest,
761 int fd_src)
762 {
763 char *buf;
764 int ret = 0, has_data = 0;
765 struct stat statbuf;
766 size_t src_file_len;
767 uint64_t prod_offset, consumed_offset;
768 uint64_t offset, subbuf_size;
769 ssize_t readlen;
770
771 ret = fstat(fd_src, &statbuf);
772 if (ret) {
773 return ret;
774 }
775 src_file_len = layout->mmap_length;
776 buf = zmalloc(src_file_len);
777 if (!buf) {
778 return -1;
779 }
780 readlen = lttng_read(fd_src, buf, src_file_len);
781 if (readlen < 0) {
782 PERROR("Error reading input file");
783 ret = -1;
784 goto end;
785 }
786
787 prod_offset = crash_get_field(layout, buf, prod_offset);
788 DBG("prod_offset: 0x%" PRIx64, prod_offset);
789 consumed_offset = crash_get_field(layout, buf, consumed_offset);
790 DBG("consumed_offset: 0x%" PRIx64, consumed_offset);
791 subbuf_size = layout->subbuf_size;
792
793 for (offset = consumed_offset; offset < prod_offset;
794 offset += subbuf_size) {
795 ret = copy_crash_subbuf(layout, fd_dest, buf, offset);
796 if (!ret) {
797 has_data = 1;
798 }
799 if (ret) {
800 goto end;
801 }
802 }
803 end:
804 free(buf);
805 if (ret && ret != -ENODATA) {
806 return ret;
807 }
808 if (has_data) {
809 return 0;
810 } else {
811 return -ENODATA;
812 }
813 }
814
815 static
816 int extract_file(int output_dir_fd, const char *output_file,
817 int input_dir_fd, const char *input_file)
818 {
819 int fd_dest, fd_src, ret = 0, closeret;
820 struct lttng_crash_layout layout;
821
822 layout.reverse_byte_order = 0; /* For reading magic number */
823
824 DBG("Extract file '%s'", input_file);
825 fd_src = openat(input_dir_fd, input_file, O_RDONLY);
826 if (fd_src < 0) {
827 PERROR("Error opening '%s' for reading",
828 input_file);
829 ret = -1;
830 goto end;
831 }
832
833 /* Query the crash ABI layout */
834 ret = get_crash_layout(&layout, fd_src);
835 if (ret) {
836 goto close_src;
837 }
838
839 fd_dest = openat(output_dir_fd, output_file,
840 O_RDWR | O_CREAT | O_EXCL,
841 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
842 if (fd_dest < 0) {
843 PERROR("Error opening '%s' for writing",
844 output_file);
845 ret = -1;
846 goto close_src;
847 }
848
849 ret = copy_crash_data(&layout, fd_dest, fd_src);
850 if (ret) {
851 goto close_dest;
852 }
853
854 close_dest:
855 closeret = close(fd_dest);
856 if (closeret) {
857 PERROR("close");
858 }
859 if (ret == -ENODATA) {
860 closeret = unlinkat(output_dir_fd, output_file, 0);
861 if (closeret) {
862 PERROR("unlinkat");
863 }
864 }
865 close_src:
866 closeret = close(fd_src);
867 if (closeret) {
868 PERROR("close");
869 }
870 end:
871 return ret;
872 }
873
874 static
875 int extract_all_files(const char *output_path,
876 const char *input_path)
877 {
878 DIR *input_dir, *output_dir;
879 int input_dir_fd, output_dir_fd, ret = 0, closeret;
880 struct dirent *entry; /* input */
881
882 /* Open input directory */
883 input_dir = opendir(input_path);
884 if (!input_dir) {
885 PERROR("Cannot open '%s' path", input_path);
886 return -1;
887 }
888 input_dir_fd = dirfd(input_dir);
889 if (input_dir_fd < 0) {
890 PERROR("dirfd");
891 return -1;
892 }
893
894 /* Open output directory */
895 output_dir = opendir(output_path);
896 if (!output_dir) {
897 PERROR("Cannot open '%s' path", output_path);
898 return -1;
899 }
900 output_dir_fd = dirfd(output_dir);
901 if (output_dir_fd < 0) {
902 PERROR("dirfd");
903 return -1;
904 }
905
906 while ((entry = readdir(input_dir))) {
907 if (!strcmp(entry->d_name, ".")
908 || !strcmp(entry->d_name, ".."))
909 continue;
910 ret = extract_file(output_dir_fd, entry->d_name,
911 input_dir_fd, entry->d_name);
912 if (ret == -ENODATA) {
913 DBG("No data in file '%s', skipping", entry->d_name);
914 ret = 0;
915 continue;
916 } else if (ret < 0) {
917 break;
918 } else if (ret > 0) {
919 DBG("Skipping file '%s'", entry->d_name);
920 ret = 0;
921 continue;
922 }
923 }
924 closeret = closedir(output_dir);
925 if (closeret) {
926 PERROR("closedir");
927 }
928 closeret = closedir(input_dir);
929 if (closeret) {
930 PERROR("closedir");
931 }
932 return ret;
933 }
934
935 static
936 int extract_one_trace(const char *output_path,
937 const char *input_path)
938 {
939 char dest[PATH_MAX], src[PATH_MAX];
940 int ret;
941
942 DBG("Extract crash trace '%s' into '%s'", input_path, output_path);
943
944 /* Copy metadata */
945 strncpy(dest, output_path, PATH_MAX);
946 dest[PATH_MAX - 1] = '\0';
947 strncat(dest, "/metadata", PATH_MAX - strlen(dest) - 1);
948
949 strncpy(src, input_path, PATH_MAX);
950 src[PATH_MAX - 1] = '\0';
951 strncat(src, "/metadata", PATH_MAX - strlen(dest) - 1);
952
953 ret = copy_file(dest, src);
954 if (ret) {
955 return ret;
956 }
957
958 /* Extract each other file that has expected header */
959 return extract_all_files(output_path, input_path);
960 }
961
962 static
963 int extract_trace_recursive(const char *output_path,
964 const char *input_path)
965 {
966 DIR *dir;
967 int dir_fd, ret = 0, closeret;
968 struct dirent *entry;
969 size_t path_len;
970 int has_warning = 0;
971
972 /* Open directory */
973 dir = opendir(input_path);
974 if (!dir) {
975 PERROR("Cannot open '%s' path", input_path);
976 return -1;
977 }
978
979 path_len = strlen(input_path);
980
981 dir_fd = dirfd(dir);
982 if (dir_fd < 0) {
983 PERROR("dirfd");
984 return -1;
985 }
986
987 while ((entry = readdir(dir))) {
988 struct stat st;
989 size_t name_len;
990 char filename[PATH_MAX];
991
992 if (!strcmp(entry->d_name, ".")
993 || !strcmp(entry->d_name, "..")) {
994 continue;
995 }
996
997 name_len = strlen(entry->d_name);
998 if (path_len + name_len + 2 > sizeof(filename)) {
999 ERR("Failed to remove file: path name too long (%s/%s)",
1000 input_path, entry->d_name);
1001 continue;
1002 }
1003
1004 if (snprintf(filename, sizeof(filename), "%s/%s",
1005 input_path, entry->d_name) < 0) {
1006 ERR("Failed to format path.");
1007 continue;
1008 }
1009
1010 if (stat(filename, &st)) {
1011 PERROR("stat");
1012 continue;
1013 }
1014
1015 if (S_ISDIR(st.st_mode)) {
1016 char output_subpath[PATH_MAX];
1017 char input_subpath[PATH_MAX];
1018
1019 strncpy(output_subpath, output_path,
1020 sizeof(output_subpath));
1021 output_subpath[sizeof(output_subpath) - 1] = '\0';
1022 strncat(output_subpath, "/",
1023 sizeof(output_subpath) - strlen(output_subpath) - 1);
1024 strncat(output_subpath, entry->d_name,
1025 sizeof(output_subpath) - strlen(output_subpath) - 1);
1026
1027 ret = mkdir(output_subpath, S_IRWXU | S_IRWXG);
1028 if (ret) {
1029 PERROR("mkdir");
1030 has_warning = 1;
1031 goto end;
1032 }
1033
1034 strncpy(input_subpath, input_path,
1035 sizeof(input_subpath));
1036 input_subpath[sizeof(input_subpath) - 1] = '\0';
1037 strncat(input_subpath, "/",
1038 sizeof(input_subpath) - strlen(input_subpath) - 1);
1039 strncat(input_subpath, entry->d_name,
1040 sizeof(input_subpath) - strlen(input_subpath) - 1);
1041
1042 ret = extract_trace_recursive(output_subpath,
1043 input_subpath);
1044 if (ret) {
1045 has_warning = 1;
1046 }
1047 } else if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
1048 if (!strcmp(entry->d_name, "metadata")) {
1049 ret = extract_one_trace(output_path,
1050 input_path);
1051 if (ret) {
1052 WARN("Error extracting trace '%s', continuing anyway.",
1053 input_path);
1054 has_warning = 1;
1055 }
1056 }
1057 } else {
1058 has_warning = 1;
1059 goto end;
1060 }
1061 }
1062 end:
1063 closeret = closedir(dir);
1064 if (closeret) {
1065 PERROR("closedir");
1066 }
1067 return has_warning;
1068 }
1069
1070 static
1071 int delete_dir_recursive(const char *path)
1072 {
1073 DIR *dir;
1074 int dir_fd, ret = 0, closeret;
1075 size_t path_len;
1076 struct dirent *entry;
1077
1078 /* Open trace directory */
1079 dir = opendir(path);
1080 if (!dir) {
1081 PERROR("Cannot open '%s' path", path);
1082 ret = -errno;
1083 goto end;
1084 }
1085
1086 path_len = strlen(path);
1087
1088 dir_fd = dirfd(dir);
1089 if (dir_fd < 0) {
1090 PERROR("dirfd");
1091 ret = -errno;
1092 goto end;
1093 }
1094
1095 while ((entry = readdir(dir))) {
1096 struct stat st;
1097 size_t name_len;
1098 char filename[PATH_MAX];
1099
1100 if (!strcmp(entry->d_name, ".")
1101 || !strcmp(entry->d_name, "..")) {
1102 continue;
1103 }
1104
1105 name_len = strlen(entry->d_name);
1106 if (path_len + name_len + 2 > sizeof(filename)) {
1107 ERR("Failed to remove file: path name too long (%s/%s)",
1108 path, entry->d_name);
1109 continue;
1110 }
1111
1112 if (snprintf(filename, sizeof(filename), "%s/%s",
1113 path, entry->d_name) < 0) {
1114 ERR("Failed to format path.");
1115 continue;
1116 }
1117
1118 if (stat(filename, &st)) {
1119 PERROR("stat");
1120 continue;
1121 }
1122
1123 if (S_ISDIR(st.st_mode)) {
1124 char *subpath = zmalloc(PATH_MAX);
1125
1126 if (!subpath) {
1127 PERROR("zmalloc path");
1128 ret = -1;
1129 goto end;
1130 }
1131 strncpy(subpath, path, PATH_MAX);
1132 subpath[PATH_MAX - 1] = '\0';
1133 strncat(subpath, "/",
1134 PATH_MAX - strlen(subpath) - 1);
1135 strncat(subpath, entry->d_name,
1136 PATH_MAX - strlen(subpath) - 1);
1137
1138 ret = delete_dir_recursive(subpath);
1139 free(subpath);
1140 if (ret) {
1141 /* Error occurred, abort traversal. */
1142 goto end;
1143 }
1144 } else if (S_ISREG(st.st_mode)) {
1145 ret = unlinkat(dir_fd, entry->d_name, 0);
1146 if (ret) {
1147 PERROR("Unlinking '%s'", entry->d_name);
1148 goto end;
1149 }
1150 } else {
1151 ret = -EINVAL;
1152 goto end;
1153 }
1154 }
1155 end:
1156 if (!ret) {
1157 ret = rmdir(path);
1158 if (ret) {
1159 PERROR("rmdir '%s'", path);
1160 }
1161 }
1162 closeret = closedir(dir);
1163 if (closeret) {
1164 PERROR("closedir");
1165 }
1166 return ret;
1167 }
1168
1169 static
1170 int view_trace(const char *viewer_path, const char *trace_path)
1171 {
1172 pid_t pid;
1173
1174 pid = fork();
1175 if (pid < 0) {
1176 /* Error */
1177 PERROR("fork");
1178 return -1;
1179 } else if (pid > 0) {
1180 /* Parent */
1181 int status;
1182
1183 pid = waitpid(pid, &status, 0);
1184 if (pid < 0 || !WIFEXITED(status)) {
1185 return -1;
1186 }
1187 } else {
1188 /* Child */
1189 int ret;
1190
1191 ret = execlp(viewer_path, viewer_path,
1192 trace_path, (char *) NULL);
1193 if (ret) {
1194 PERROR("execlp");
1195 exit(EXIT_FAILURE);
1196 }
1197 exit(EXIT_SUCCESS); /* Never reached */
1198 }
1199 return 0;
1200 }
1201
1202 /*
1203 * main
1204 */
1205 int main(int argc, char *argv[])
1206 {
1207 int ret;
1208 bool has_warning = false;
1209 const char *output_path = NULL;
1210 char tmppath[] = "/tmp/lttng-crash-XXXXXX";
1211
1212 progname = argv[0] ? argv[0] : "lttng-crash";
1213
1214 ret = parse_args(argc, argv);
1215 if (ret > 0) {
1216 goto end;
1217 } else if (ret < 0) {
1218 has_warning = true;
1219 goto end;
1220 }
1221
1222 if (opt_output_path) {
1223 output_path = opt_output_path;
1224 ret = mkdir(output_path, S_IRWXU | S_IRWXG);
1225 if (ret) {
1226 PERROR("mkdir");
1227 has_warning = true;
1228 goto end;
1229 }
1230 } else {
1231 output_path = mkdtemp(tmppath);
1232 if (!output_path) {
1233 PERROR("mkdtemp");
1234 has_warning = true;
1235 goto end;
1236 }
1237 }
1238
1239 ret = extract_trace_recursive(output_path, input_path);
1240 if (ret < 0) {
1241 has_warning = true;
1242 goto end;
1243 } else if (ret > 0) {
1244 /* extract_trace_recursive reported a warning. */
1245 has_warning = true;
1246 }
1247 if (!opt_output_path) {
1248 /* View trace */
1249 ret = view_trace(opt_viewer_path, output_path);
1250 if (ret) {
1251 has_warning = true;
1252 }
1253 /* unlink temporary trace */
1254 ret = delete_dir_recursive(output_path);
1255 if (ret) {
1256 has_warning = true;
1257 }
1258 }
1259 end:
1260 exit(has_warning ? EXIT_FAILURE : EXIT_SUCCESS);
1261 }
This page took 0.084808 seconds and 5 git commands to generate.