2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
15 #include <sys/types.h>
28 #include <lttng/lttng.h>
29 #include <common/common.h>
30 #include <common/utils.h>
32 #define DEFAULT_VIEWER "babeltrace"
34 #define COPY_BUFLEN 4096
35 #define RB_CRASH_DUMP_ABI_LEN 32
37 #define RB_CRASH_DUMP_ABI_MAGIC_LEN 16
40 * The 128-bit magic number is xor'd in the process data so it does not
41 * cause a false positive when searching for buffers by scanning memory.
42 * The actual magic number is:
43 * 0x17, 0x7B, 0xF1, 0x77, 0xBF, 0x17, 0x7B, 0xF1,
44 * 0x77, 0xBF, 0x17, 0x7B, 0xF1, 0x77, 0xBF, 0x17,
46 #define RB_CRASH_DUMP_ABI_MAGIC_XOR \
48 0x17 ^ 0xFF, 0x7B ^ 0xFF, 0xF1 ^ 0xFF, 0x77 ^ 0xFF, \
49 0xBF ^ 0xFF, 0x17 ^ 0xFF, 0x7B ^ 0xFF, 0xF1 ^ 0xFF, \
50 0x77 ^ 0xFF, 0xBF ^ 0xFF, 0x17 ^ 0xFF, 0x7B ^ 0xFF, \
51 0xF1 ^ 0xFF, 0x77 ^ 0xFF, 0xBF ^ 0xFF, 0x17 ^ 0xFF, \
54 static const char *help_msg
=
55 #ifdef LTTNG_EMBED_HELP
56 #include <lttng-crash.1.h>
63 * Non-static to ensure the compiler does not optimize away the xor.
65 uint8_t lttng_crash_expected_magic_xor
[] = RB_CRASH_DUMP_ABI_MAGIC_XOR
;
67 #define RB_CRASH_ENDIAN 0x1234
68 #define RB_CRASH_ENDIAN_REVERSE 0x3412
70 enum lttng_crash_type
{
71 LTTNG_CRASH_TYPE_UST
= 0,
72 LTTNG_CRASH_TYPE_KERNEL
= 1,
75 /* LTTng ring buffer defines (copied) */
77 #define HALF_ULONG_BITS(wl) (((wl) * CHAR_BIT) >> 1)
79 #define SB_ID_OFFSET_SHIFT(wl) (HALF_ULONG_BITS(wl) + 1)
80 #define SB_ID_OFFSET_COUNT(wl) (1UL << SB_ID_OFFSET_SHIFT(wl))
81 #define SB_ID_OFFSET_MASK(wl) (~(SB_ID_OFFSET_COUNT(wl) - 1))
83 * Lowest bit of top word half belongs to noref. Used only for overwrite mode.
85 #define SB_ID_NOREF_SHIFT(wl) (SB_ID_OFFSET_SHIFT(wl) - 1)
86 #define SB_ID_NOREF_COUNT(wl) (1UL << SB_ID_NOREF_SHIFT(wl))
87 #define SB_ID_NOREF_MASK(wl) SB_ID_NOREF_COUNT(wl)
89 * In overwrite mode: lowest half of word is used for index.
90 * Limit of 2^16 subbuffers per buffer on 32-bit, 2^32 on 64-bit.
91 * In producer-consumer mode: whole word used for index.
93 #define SB_ID_INDEX_SHIFT(wl) 0
94 #define SB_ID_INDEX_COUNT(wl) (1UL << SB_ID_INDEX_SHIFT(wl))
95 #define SB_ID_INDEX_MASK(wl) (SB_ID_NOREF_COUNT(wl) - 1)
98 RING_BUFFER_OVERWRITE
= 0, /* Overwrite when buffer full */
99 RING_BUFFER_DISCARD
= 1, /* Discard when buffer full */
102 struct crash_abi_unknown
{
103 uint8_t magic
[RB_CRASH_DUMP_ABI_MAGIC_LEN
];
104 uint64_t mmap_length
; /* Overall length of crash record */
106 * { 0x12, 0x34 }: big endian
107 * { 0x34, 0x12 }: little endian
109 uint16_t major
; /* Major number. */
110 uint16_t minor
; /* Minor number. */
111 uint8_t word_size
; /* Word size (bytes). */
112 uint8_t layout_type
; /* enum lttng_crash_layout */
113 } __attribute__((packed
));
115 struct crash_abi_0_0
{
116 struct crash_abi_unknown parent
;
119 uint32_t prod_offset
;
120 uint32_t consumed_offset
;
121 uint32_t commit_hot_array
;
122 uint32_t commit_hot_seq
;
123 uint32_t buf_wsb_array
;
126 uint32_t sb_array_shmp_offset
;
127 uint32_t sb_backend_p_offset
;
128 uint32_t content_size
;
129 uint32_t packet_size
;
130 } __attribute__((packed
)) offset
;
133 uint8_t consumed_offset
;
134 uint8_t commit_hot_seq
;
136 uint8_t sb_array_shmp_offset
;
137 uint8_t sb_backend_p_offset
;
138 uint8_t content_size
;
140 } __attribute__((packed
)) length
;
142 uint32_t commit_hot_array
;
143 uint32_t buf_wsb_array
;
145 } __attribute__((packed
)) stride
;
147 uint64_t buf_size
; /* Size of the buffer */
148 uint64_t subbuf_size
; /* Sub-buffer size */
149 uint64_t num_subbuf
; /* Number of sub-buffers for writer */
150 uint32_t mode
; /* Buffer mode: 0: overwrite, 1: discard */
151 } __attribute__((packed
));
153 struct lttng_crash_layout
{
155 int prod_offset
, consumed_offset
,
156 commit_hot_array
, commit_hot_seq
,
157 buf_wsb_array
, buf_wsb_id
,
158 sb_array
, sb_array_shmp_offset
,
159 sb_backend_p_offset
, content_size
,
163 int prod_offset
, consumed_offset
,
164 commit_hot_seq
, buf_wsb_id
,
165 sb_array_shmp_offset
, sb_backend_p_offset
,
166 content_size
, packet_size
;
169 int commit_hot_array
, buf_wsb_array
, sb_array
;
172 int reverse_byte_order
;
175 uint64_t mmap_length
; /* Length of crash record */
176 uint64_t buf_size
; /* Size of the buffer */
177 uint64_t subbuf_size
; /* Sub-buffer size */
178 uint64_t num_subbuf
; /* Number of sub-buffers for writer */
179 uint32_t mode
; /* Buffer mode: 0: overwrite, 1: discard */
183 static char *progname
,
184 *opt_viewer_path
= NULL
,
185 *opt_output_path
= NULL
;
187 static char *input_path
;
189 int lttng_opt_quiet
, lttng_opt_verbose
, lttng_opt_mi
;
195 /* Getopt options. No first level command. */
196 static struct option long_options
[] = {
197 { "version", 0, NULL
, 'V' },
198 { "help", 0, NULL
, 'h' },
199 { "verbose", 0, NULL
, 'v' },
200 { "viewer", 1, NULL
, 'e' },
201 { "extract", 1, NULL
, 'x' },
202 { "list-options", 0, NULL
, OPT_DUMP_OPTIONS
},
203 { NULL
, 0, NULL
, 0 },
206 static void usage(void)
208 int ret
= utils_show_help(1, "lttng-crash", help_msg
);
211 ERR("Cannot show --help for `lttng-crash`");
217 static void version(FILE *ofp
)
219 fprintf(ofp
, "%s (LTTng Crash Trace Viewer) " VERSION
" - " VERSION_NAME
222 GIT_VERSION
[0] == '\0' ? "" : " - " GIT_VERSION
,
223 EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " EXTRA_VERSION_NAME
);
229 * List options line by line. This is mostly for bash auto completion and to
230 * avoid difficult parsing.
232 static void list_options(FILE *ofp
)
235 struct option
*option
= NULL
;
237 option
= &long_options
[i
];
238 while (option
->name
!= NULL
) {
239 fprintf(ofp
, "--%s\n", option
->name
);
241 if (isprint(option
->val
)) {
242 fprintf(ofp
, "-%c\n", option
->val
);
246 option
= &long_options
[i
];
251 * Parse command line arguments.
253 * Return 0 if OK, else -1
255 static int parse_args(int argc
, char **argv
)
264 while ((opt
= getopt_long(argc
, argv
, "+Vhve:x:", long_options
, NULL
)) != -1) {
275 /* There is only 3 possible level of verbosity. (-vvv) */
276 if (lttng_opt_verbose
< 3) {
277 lttng_opt_verbose
+= 1;
281 free(opt_viewer_path
);
282 opt_viewer_path
= strdup(optarg
);
285 free(opt_output_path
);
286 opt_output_path
= strdup(optarg
);
288 case OPT_DUMP_OPTIONS
:
289 list_options(stdout
);
293 ERR("Unknown command-line option");
298 if (!opt_viewer_path
) {
299 opt_viewer_path
= DEFAULT_VIEWER
;
302 /* No leftovers, or more than one input path, print usage and quit */
303 if (argc
- optind
!= 1) {
304 ERR("Command-line error: Specify exactly one input path");
308 input_path
= argv
[optind
];
317 int copy_file(const char *file_dest
, const char *file_src
)
319 int fd_src
= -1, fd_dest
= -1;
320 ssize_t readlen
, writelen
;
321 char buf
[COPY_BUFLEN
];
324 DBG("Copy metadata file '%s' into '%s'", file_src
, file_dest
);
326 fd_src
= open(file_src
, O_RDONLY
);
328 PERROR("Error opening %s for reading", file_src
);
332 fd_dest
= open(file_dest
, O_RDWR
| O_CREAT
| O_EXCL
,
333 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
335 PERROR("Error opening %s for writing", file_dest
);
341 readlen
= lttng_read(fd_src
, buf
, COPY_BUFLEN
);
343 PERROR("Error reading input file");
350 writelen
= lttng_write(fd_dest
, buf
, readlen
);
351 if (writelen
< readlen
) {
352 PERROR("Error writing to output file");
361 if (close(fd_src
) < 0) {
362 PERROR("Error closing %s", file_src
);
367 if (close(fd_dest
) < 0) {
368 PERROR("Error closing %s", file_dest
);
375 uint64_t _crash_get_field(const struct lttng_crash_layout
*layout
,
376 const char *ptr
, size_t size
)
379 case 1: return *(uint8_t *) ptr
;
380 case 2: if (layout
->reverse_byte_order
) {
381 return __bswap_16(*(uint16_t *) ptr
);
383 return *(uint16_t *) ptr
;
386 case 4: if (layout
->reverse_byte_order
) {
387 return __bswap_32(*(uint32_t *) ptr
);
389 return *(uint32_t *) ptr
;
392 case 8: if (layout
->reverse_byte_order
) {
393 return __bswap_64(*(uint64_t *) ptr
);
395 return *(uint64_t *) ptr
;
404 #define crash_get_field(layout, map, name) \
405 _crash_get_field(layout, (map) + (layout)->offset.name, \
408 #define crash_get_array_field(layout, map, array_name, idx, field_name) \
409 _crash_get_field(layout, \
410 (map) + (layout)->offset.array_name \
411 + (idx * (layout)->stride.array_name) \
412 + (layout)->offset.field_name, \
413 (layout)->length.field_name)
415 #define crash_get_hdr_raw_field(layout, hdr, name) ((hdr)->name)
417 #define crash_get_hdr_field(layout, hdr, name) \
418 _crash_get_field(layout, (const char *) &(hdr)->name, \
421 #define crash_get_layout(layout, hdr, name) \
423 (layout)->name = crash_get_hdr_field(layout, hdr, \
425 DBG("layout.%s = %" PRIu64, #name, \
426 (uint64_t) (layout)->name); \
430 int get_crash_layout_0_0(struct lttng_crash_layout
*layout
,
433 const struct crash_abi_0_0
*abi
= (const struct crash_abi_0_0
*) map
;
435 crash_get_layout(layout
, abi
, offset
.prod_offset
);
436 crash_get_layout(layout
, abi
, offset
.consumed_offset
);
437 crash_get_layout(layout
, abi
, offset
.commit_hot_array
);
438 crash_get_layout(layout
, abi
, offset
.commit_hot_seq
);
439 crash_get_layout(layout
, abi
, offset
.buf_wsb_array
);
440 crash_get_layout(layout
, abi
, offset
.buf_wsb_id
);
441 crash_get_layout(layout
, abi
, offset
.sb_array
);
442 crash_get_layout(layout
, abi
, offset
.sb_array_shmp_offset
);
443 crash_get_layout(layout
, abi
, offset
.sb_backend_p_offset
);
444 crash_get_layout(layout
, abi
, offset
.content_size
);
445 crash_get_layout(layout
, abi
, offset
.packet_size
);
447 crash_get_layout(layout
, abi
, length
.prod_offset
);
448 crash_get_layout(layout
, abi
, length
.consumed_offset
);
449 crash_get_layout(layout
, abi
, length
.commit_hot_seq
);
450 crash_get_layout(layout
, abi
, length
.buf_wsb_id
);
451 crash_get_layout(layout
, abi
, length
.sb_array_shmp_offset
);
452 crash_get_layout(layout
, abi
, length
.sb_backend_p_offset
);
453 crash_get_layout(layout
, abi
, length
.content_size
);
454 crash_get_layout(layout
, abi
, length
.packet_size
);
456 crash_get_layout(layout
, abi
, stride
.commit_hot_array
);
457 crash_get_layout(layout
, abi
, stride
.buf_wsb_array
);
458 crash_get_layout(layout
, abi
, stride
.sb_array
);
460 crash_get_layout(layout
, abi
, buf_size
);
461 crash_get_layout(layout
, abi
, subbuf_size
);
462 crash_get_layout(layout
, abi
, num_subbuf
);
463 crash_get_layout(layout
, abi
, mode
);
469 void print_dbg_magic(const uint8_t *magic
)
471 DBG("magic: 0x%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X",
472 magic
[0], magic
[1], magic
[2], magic
[3],
473 magic
[4], magic
[5], magic
[6], magic
[7],
474 magic
[8], magic
[9], magic
[10], magic
[11],
475 magic
[12], magic
[13], magic
[14], magic
[15]);
479 int check_magic(const uint8_t *magic
)
483 for (i
= 0; i
< RB_CRASH_DUMP_ABI_MAGIC_LEN
; i
++) {
484 if ((magic
[i
] ^ 0xFF) != lttng_crash_expected_magic_xor
[i
]) {
492 int get_crash_layout(struct lttng_crash_layout
*layout
, int fd
,
493 const char *input_file
)
496 int ret
= 0, unmapret
;
497 const uint8_t *magic
;
498 uint64_t mmap_length
;
499 uint16_t major
, minor
;
501 const struct crash_abi_unknown
*abi
;
503 enum lttng_crash_type layout_type
;
506 ret
= fstat(fd
, &stat
);
508 PERROR("Failed to fstat '%s'", input_file
);
511 if (stat
.st_size
< RB_CRASH_DUMP_ABI_LEN
) {
512 ERR("File '%s' truncated: file length of %" PRIi64
513 " bytes does not meet the minimal expected "
514 "length of %d bytes",
515 input_file
, (int64_t) stat
.st_size
,
516 RB_CRASH_DUMP_ABI_LEN
);
519 map
= mmap(NULL
, RB_CRASH_DUMP_ABI_LEN
, PROT_READ
, MAP_PRIVATE
,
521 if (map
== MAP_FAILED
) {
522 PERROR("Mapping file");
525 abi
= (const struct crash_abi_unknown
*) map
;
526 magic
= crash_get_hdr_raw_field(layout
, abi
, magic
);
527 print_dbg_magic(magic
);
528 if (check_magic(magic
)) {
529 DBG("Unknown magic number");
530 ret
= 1; /* positive return value, skip */
533 endian
= crash_get_hdr_field(layout
, abi
, endian
);
535 case RB_CRASH_ENDIAN
:
537 case RB_CRASH_ENDIAN_REVERSE
:
538 layout
->reverse_byte_order
= 1;
541 DBG("Unknown endianness value: 0x%X", (unsigned int) endian
);
542 ret
= 1; /* positive return value, skip */
545 layout_type
= (enum lttng_crash_type
) crash_get_hdr_field(layout
, abi
, layout_type
);
546 switch (layout_type
) {
547 case LTTNG_CRASH_TYPE_UST
:
549 case LTTNG_CRASH_TYPE_KERNEL
:
550 ERR("lttng-modules buffer layout support not implemented");
551 ret
= 1; /* positive return value, skip */
554 ERR("Unknown layout type %u", (unsigned int) layout_type
);
555 ret
= 1; /* positive return value, skip */
558 mmap_length
= crash_get_hdr_field(layout
, abi
, mmap_length
);
559 DBG("mmap_length: %" PRIu64
, mmap_length
);
560 layout
->mmap_length
= mmap_length
;
561 major
= crash_get_hdr_field(layout
, abi
, major
);
562 DBG("major: %u", major
);
563 minor
= crash_get_hdr_field(layout
, abi
, minor
);
564 DBG("minor: %u", minor
);
565 word_size
= crash_get_hdr_field(layout
, abi
, word_size
);
566 DBG("word_size: %u", word_size
);
571 ret
= get_crash_layout_0_0(layout
, map
);
577 ERR("Unsupported crash ABI %u.%u\n", major
, minor
);
582 ERR("Unsupported crash ABI %u.%u\n", major
, minor
);
586 layout
->word_size
= word_size
;
588 unmapret
= munmap(map
, RB_CRASH_DUMP_ABI_LEN
);
595 /* buf_trunc mask selects only the buffer number. */
597 uint64_t buf_trunc(uint64_t offset
, uint64_t buf_size
)
599 return offset
& ~(buf_size
- 1);
602 /* subbuf_trunc mask selects the subbuffer number. */
604 uint64_t subbuf_trunc(uint64_t offset
, uint64_t subbuf_size
)
606 return offset
& ~(subbuf_size
- 1);
609 /* buf_offset mask selects only the offset within the current buffer. */
611 uint64_t buf_offset(uint64_t offset
, uint64_t buf_size
)
613 return offset
& (buf_size
- 1);
616 /* subbuf_offset mask selects the offset within the current subbuffer. */
618 uint64_t subbuf_offset(uint64_t offset
, uint64_t subbuf_size
)
620 return offset
& (subbuf_size
- 1);
623 /* subbuf_index returns the index of the current subbuffer within the buffer. */
625 uint64_t subbuf_index(uint64_t offset
, uint64_t buf_size
, uint64_t subbuf_size
)
627 return buf_offset(offset
, buf_size
) / subbuf_size
;
631 uint64_t subbuffer_id_get_index(uint32_t mode
, uint64_t id
,
634 if (mode
== RING_BUFFER_OVERWRITE
)
635 return id
& SB_ID_INDEX_MASK(wl
);
641 int copy_crash_subbuf(const struct lttng_crash_layout
*layout
,
642 int fd_dest
, char *buf
, uint64_t offset
)
644 uint64_t buf_size
, subbuf_size
, num_subbuf
, sbidx
, id
,
645 sb_bindex
, rpages_offset
, p_offset
, seq_cc
,
646 committed
, commit_count_mask
, consumed_cur
,
652 * Get the current subbuffer by applying the proper mask to
653 * "offset", and looking up the subbuf location within the
657 buf_size
= layout
->buf_size
;
658 subbuf_size
= layout
->subbuf_size
;
659 num_subbuf
= layout
->num_subbuf
;
661 switch (layout
->word_size
) {
662 case 4: commit_count_mask
= 0xFFFFFFFFULL
/ num_subbuf
;
664 case 8: commit_count_mask
= 0xFFFFFFFFFFFFFFFFULL
/ num_subbuf
;
667 ERR("Unsupported word size: %u",
668 (unsigned int) layout
->word_size
);
672 DBG("Copy crash subbuffer at offset %" PRIu64
, offset
);
673 sbidx
= subbuf_index(offset
, buf_size
, subbuf_size
);
676 * Find where the seq cc is located. Compute length of data to
679 seq_cc
= crash_get_array_field(layout
, buf
, commit_hot_array
,
680 sbidx
, commit_hot_seq
);
681 consumed_cur
= crash_get_field(layout
, buf
, consumed_offset
);
684 * Check that the buffer we are getting is after or at
685 * consumed_cur position.
687 if ((long) subbuf_trunc(offset
, subbuf_size
)
688 - (long) subbuf_trunc(consumed_cur
, subbuf_size
) < 0) {
689 DBG("No data: position is before consumed_cur");
694 * Check if subbuffer has been fully committed.
696 if (((seq_cc
- subbuf_size
) & commit_count_mask
)
697 - (buf_trunc(offset
, buf_size
) / num_subbuf
)
699 committed
= subbuf_size
;
701 committed
= subbuf_offset(seq_cc
, subbuf_size
);
703 DBG("No data committed, seq_cc: %" PRIu64
, seq_cc
);
708 /* Find actual physical offset in subbuffer table */
709 id
= crash_get_array_field(layout
, buf
, buf_wsb_array
,
711 sb_bindex
= subbuffer_id_get_index(layout
->mode
, id
,
713 rpages_offset
= crash_get_array_field(layout
, buf
, sb_array
,
714 sb_bindex
, sb_array_shmp_offset
);
715 p_offset
= crash_get_field(layout
, buf
+ rpages_offset
,
716 sb_backend_p_offset
);
717 subbuf_ptr
= buf
+ p_offset
;
719 if (committed
== subbuf_size
) {
721 * Packet header can be used.
723 if (layout
->length
.packet_size
) {
725 subbuf_ptr
+ layout
->offset
.packet_size
,
726 layout
->length
.packet_size
);
727 if (layout
->reverse_byte_order
) {
728 packet_size
= __bswap_64(packet_size
);
730 packet_size
/= CHAR_BIT
;
732 packet_size
= subbuf_size
;
738 * Find where to patch the sub-buffer header with actual
739 * readable data len and packet len, derived from seq
740 * cc. Patch it in our in-memory copy.
742 patch_size
= committed
* CHAR_BIT
;
743 if (layout
->reverse_byte_order
) {
744 patch_size
= __bswap_64(patch_size
);
746 if (layout
->length
.content_size
) {
747 memcpy(subbuf_ptr
+ layout
->offset
.content_size
,
748 &patch_size
, layout
->length
.content_size
);
750 if (layout
->length
.packet_size
) {
751 memcpy(subbuf_ptr
+ layout
->offset
.packet_size
,
752 &patch_size
, layout
->length
.packet_size
);
754 packet_size
= committed
;
758 * Copy packet into fd_dest.
760 writelen
= lttng_write(fd_dest
, subbuf_ptr
, packet_size
);
761 if (writelen
< packet_size
) {
762 PERROR("Error writing to output file");
765 DBG("Copied %" PRIu64
" bytes of data", packet_size
);
773 int copy_crash_data(const struct lttng_crash_layout
*layout
, int fd_dest
,
777 int ret
= 0, has_data
= 0;
780 uint64_t prod_offset
, consumed_offset
;
781 uint64_t offset
, subbuf_size
;
784 ret
= fstat(fd_src
, &statbuf
);
788 src_file_len
= layout
->mmap_length
;
789 buf
= zmalloc(src_file_len
);
793 readlen
= lttng_read(fd_src
, buf
, src_file_len
);
795 PERROR("Error reading input file");
800 prod_offset
= crash_get_field(layout
, buf
, prod_offset
);
801 DBG("prod_offset: 0x%" PRIx64
, prod_offset
);
802 consumed_offset
= crash_get_field(layout
, buf
, consumed_offset
);
803 DBG("consumed_offset: 0x%" PRIx64
, consumed_offset
);
804 subbuf_size
= layout
->subbuf_size
;
806 for (offset
= consumed_offset
; offset
< prod_offset
;
807 offset
+= subbuf_size
) {
808 ret
= copy_crash_subbuf(layout
, fd_dest
, buf
, offset
);
818 if (ret
&& ret
!= -ENODATA
) {
829 int extract_file(int output_dir_fd
, const char *output_file
,
830 int input_dir_fd
, const char *input_file
)
832 int fd_dest
, fd_src
, ret
= 0, closeret
;
833 struct lttng_crash_layout layout
;
835 layout
.reverse_byte_order
= 0; /* For reading magic number */
837 DBG("Extract file '%s'", input_file
);
838 fd_src
= openat(input_dir_fd
, input_file
, O_RDONLY
);
840 PERROR("Error opening '%s' for reading",
846 /* Query the crash ABI layout */
847 ret
= get_crash_layout(&layout
, fd_src
, input_file
);
852 fd_dest
= openat(output_dir_fd
, output_file
,
853 O_RDWR
| O_CREAT
| O_EXCL
,
854 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
856 PERROR("Error opening '%s' for writing",
862 ret
= copy_crash_data(&layout
, fd_dest
, fd_src
);
868 closeret
= close(fd_dest
);
872 if (ret
== -ENODATA
) {
873 closeret
= unlinkat(output_dir_fd
, output_file
, 0);
879 closeret
= close(fd_src
);
888 int extract_all_files(const char *output_path
,
889 const char *input_path
)
891 DIR *input_dir
, *output_dir
;
892 int input_dir_fd
, output_dir_fd
, ret
= 0, closeret
;
893 struct dirent
*entry
; /* input */
895 /* Open input directory */
896 input_dir
= opendir(input_path
);
898 PERROR("Cannot open '%s' path", input_path
);
901 input_dir_fd
= dirfd(input_dir
);
902 if (input_dir_fd
< 0) {
907 /* Open output directory */
908 output_dir
= opendir(output_path
);
910 PERROR("Cannot open '%s' path", output_path
);
913 output_dir_fd
= dirfd(output_dir
);
914 if (output_dir_fd
< 0) {
919 while ((entry
= readdir(input_dir
))) {
920 if (!strcmp(entry
->d_name
, ".")
921 || !strcmp(entry
->d_name
, ".."))
923 ret
= extract_file(output_dir_fd
, entry
->d_name
,
924 input_dir_fd
, entry
->d_name
);
925 if (ret
== -ENODATA
) {
926 DBG("No data in file '%s', skipping", entry
->d_name
);
929 } else if (ret
< 0) {
931 } else if (ret
> 0) {
932 DBG("Skipping file '%s'", entry
->d_name
);
937 closeret
= closedir(output_dir
);
941 closeret
= closedir(input_dir
);
949 int extract_one_trace(const char *output_path
,
950 const char *input_path
)
952 char dest
[PATH_MAX
], src
[PATH_MAX
];
955 DBG("Extract crash trace '%s' into '%s'", input_path
, output_path
);
958 strncpy(dest
, output_path
, PATH_MAX
);
959 dest
[PATH_MAX
- 1] = '\0';
960 strncat(dest
, "/metadata", PATH_MAX
- strlen(dest
) - 1);
962 strncpy(src
, input_path
, PATH_MAX
);
963 src
[PATH_MAX
- 1] = '\0';
964 strncat(src
, "/metadata", PATH_MAX
- strlen(dest
) - 1);
966 ret
= copy_file(dest
, src
);
971 /* Extract each other file that has expected header */
972 return extract_all_files(output_path
, input_path
);
976 int extract_trace_recursive(const char *output_path
,
977 const char *input_path
)
980 int dir_fd
, ret
= 0, closeret
;
981 struct dirent
*entry
;
986 dir
= opendir(input_path
);
988 PERROR("Cannot open '%s' path", input_path
);
992 path_len
= strlen(input_path
);
1000 while ((entry
= readdir(dir
))) {
1003 char filename
[PATH_MAX
];
1005 if (!strcmp(entry
->d_name
, ".")
1006 || !strcmp(entry
->d_name
, "..")) {
1010 name_len
= strlen(entry
->d_name
);
1011 if (path_len
+ name_len
+ 2 > sizeof(filename
)) {
1012 ERR("Failed to remove file: path name too long (%s/%s)",
1013 input_path
, entry
->d_name
);
1017 if (snprintf(filename
, sizeof(filename
), "%s/%s",
1018 input_path
, entry
->d_name
) < 0) {
1019 ERR("Failed to format path.");
1023 if (stat(filename
, &st
)) {
1028 if (S_ISDIR(st
.st_mode
)) {
1029 char output_subpath
[PATH_MAX
];
1030 char input_subpath
[PATH_MAX
];
1032 strncpy(output_subpath
, output_path
,
1033 sizeof(output_subpath
));
1034 output_subpath
[sizeof(output_subpath
) - 1] = '\0';
1035 strncat(output_subpath
, "/",
1036 sizeof(output_subpath
) - strlen(output_subpath
) - 1);
1037 strncat(output_subpath
, entry
->d_name
,
1038 sizeof(output_subpath
) - strlen(output_subpath
) - 1);
1040 ret
= mkdir(output_subpath
, S_IRWXU
| S_IRWXG
);
1047 strncpy(input_subpath
, input_path
,
1048 sizeof(input_subpath
));
1049 input_subpath
[sizeof(input_subpath
) - 1] = '\0';
1050 strncat(input_subpath
, "/",
1051 sizeof(input_subpath
) - strlen(input_subpath
) - 1);
1052 strncat(input_subpath
, entry
->d_name
,
1053 sizeof(input_subpath
) - strlen(input_subpath
) - 1);
1055 ret
= extract_trace_recursive(output_subpath
,
1060 } else if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
)) {
1061 if (!strcmp(entry
->d_name
, "metadata")) {
1062 ret
= extract_one_trace(output_path
,
1065 WARN("Error extracting trace '%s', continuing anyway.",
1076 closeret
= closedir(dir
);
1084 int delete_dir_recursive(const char *path
)
1087 int dir_fd
, ret
= 0, closeret
;
1089 struct dirent
*entry
;
1091 /* Open trace directory */
1092 dir
= opendir(path
);
1094 PERROR("Cannot open '%s' path", path
);
1096 goto end_no_closedir
;
1099 path_len
= strlen(path
);
1101 dir_fd
= dirfd(dir
);
1108 while ((entry
= readdir(dir
))) {
1111 char filename
[PATH_MAX
];
1113 if (!strcmp(entry
->d_name
, ".")
1114 || !strcmp(entry
->d_name
, "..")) {
1118 name_len
= strlen(entry
->d_name
);
1119 if (path_len
+ name_len
+ 2 > sizeof(filename
)) {
1120 ERR("Failed to remove file: path name too long (%s/%s)",
1121 path
, entry
->d_name
);
1125 if (snprintf(filename
, sizeof(filename
), "%s/%s",
1126 path
, entry
->d_name
) < 0) {
1127 ERR("Failed to format path.");
1131 if (stat(filename
, &st
)) {
1136 if (S_ISDIR(st
.st_mode
)) {
1137 char *subpath
= zmalloc(PATH_MAX
);
1140 PERROR("zmalloc path");
1144 strncpy(subpath
, path
, PATH_MAX
);
1145 subpath
[PATH_MAX
- 1] = '\0';
1146 strncat(subpath
, "/",
1147 PATH_MAX
- strlen(subpath
) - 1);
1148 strncat(subpath
, entry
->d_name
,
1149 PATH_MAX
- strlen(subpath
) - 1);
1151 ret
= delete_dir_recursive(subpath
);
1154 /* Error occurred, abort traversal. */
1157 } else if (S_ISREG(st
.st_mode
)) {
1158 ret
= unlinkat(dir_fd
, entry
->d_name
, 0);
1160 PERROR("Unlinking '%s'", entry
->d_name
);
1172 PERROR("rmdir '%s'", path
);
1175 closeret
= closedir(dir
);
1184 int view_trace(const char *viewer_path
, const char *trace_path
)
1193 } else if (pid
> 0) {
1197 pid
= waitpid(pid
, &status
, 0);
1198 if (pid
< 0 || !WIFEXITED(status
)) {
1205 ret
= execlp(viewer_path
, viewer_path
,
1206 trace_path
, (char *) NULL
);
1211 exit(EXIT_SUCCESS
); /* Never reached */
1219 int main(int argc
, char *argv
[])
1222 bool has_warning
= false;
1223 const char *output_path
= NULL
;
1224 char tmppath
[] = "/tmp/lttng-crash-XXXXXX";
1226 progname
= argv
[0] ? argv
[0] : "lttng-crash";
1228 ret
= parse_args(argc
, argv
);
1231 } else if (ret
< 0) {
1236 if (opt_output_path
) {
1237 output_path
= opt_output_path
;
1238 ret
= mkdir(output_path
, S_IRWXU
| S_IRWXG
);
1245 output_path
= mkdtemp(tmppath
);
1253 ret
= extract_trace_recursive(output_path
, input_path
);
1257 } else if (ret
> 0) {
1258 /* extract_trace_recursive reported a warning. */
1261 if (!opt_output_path
) {
1263 ret
= view_trace(opt_viewer_path
, output_path
);
1267 /* unlink temporary trace */
1268 ret
= delete_dir_recursive(output_path
);
1274 exit(has_warning
? EXIT_FAILURE
: EXIT_SUCCESS
);