2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
9 #include <common/common.hpp>
10 #include <common/compat/endian.hpp>
11 #include <common/spawn-viewer.hpp>
12 #include <common/utils.hpp>
14 #include <lttng/lttng.h>
28 #include <sys/types.h>
31 #include <version.hpp>
33 #define COPY_BUFLEN 4096
34 #define RB_CRASH_DUMP_ABI_LEN 32
36 #define RB_CRASH_DUMP_ABI_MAGIC_LEN 16
39 * The 128-bit magic number is xor'd in the process data so it does not
40 * cause a false positive when searching for buffers by scanning memory.
41 * The actual magic number is:
42 * 0x17, 0x7B, 0xF1, 0x77, 0xBF, 0x17, 0x7B, 0xF1,
43 * 0x77, 0xBF, 0x17, 0x7B, 0xF1, 0x77, 0xBF, 0x17,
45 #define RB_CRASH_DUMP_ABI_MAGIC_XOR \
47 0x17 ^ 0xFF, 0x7B ^ 0xFF, 0xF1 ^ 0xFF, 0x77 ^ 0xFF, 0xBF ^ 0xFF, 0x17 ^ 0xFF, \
48 0x7B ^ 0xFF, 0xF1 ^ 0xFF, 0x77 ^ 0xFF, 0xBF ^ 0xFF, 0x17 ^ 0xFF, \
49 0x7B ^ 0xFF, 0xF1 ^ 0xFF, 0x77 ^ 0xFF, 0xBF ^ 0xFF, 0x17 ^ 0xFF, \
52 static const char *help_msg
=
53 #ifdef LTTNG_EMBED_HELP
54 #include <lttng-crash.1.h>
61 * Non-static to ensure the compiler does not optimize away the xor.
63 uint8_t lttng_crash_expected_magic_xor
[] = RB_CRASH_DUMP_ABI_MAGIC_XOR
;
65 #define RB_CRASH_ENDIAN 0x1234
66 #define RB_CRASH_ENDIAN_REVERSE 0x3412
68 enum lttng_crash_type
{
69 LTTNG_CRASH_TYPE_UST
= 0,
70 LTTNG_CRASH_TYPE_KERNEL
= 1,
73 /* LTTng ring buffer defines (copied) */
75 #define HALF_ULONG_BITS(wl) (((wl) *CHAR_BIT) >> 1)
77 #define SB_ID_OFFSET_SHIFT(wl) (HALF_ULONG_BITS(wl) + 1)
78 #define SB_ID_OFFSET_COUNT(wl) (1UL << SB_ID_OFFSET_SHIFT(wl))
79 #define SB_ID_OFFSET_MASK(wl) (~(SB_ID_OFFSET_COUNT(wl) - 1))
81 * Lowest bit of top word half belongs to noref. Used only for overwrite mode.
83 #define SB_ID_NOREF_SHIFT(wl) (SB_ID_OFFSET_SHIFT(wl) - 1)
84 #define SB_ID_NOREF_COUNT(wl) (1UL << SB_ID_NOREF_SHIFT(wl))
85 #define SB_ID_NOREF_MASK(wl) SB_ID_NOREF_COUNT(wl)
87 * In overwrite mode: lowest half of word is used for index.
88 * Limit of 2^16 subbuffers per buffer on 32-bit, 2^32 on 64-bit.
89 * In producer-consumer mode: whole word used for index.
91 #define SB_ID_INDEX_SHIFT(wl) 0
92 #define SB_ID_INDEX_COUNT(wl) (1UL << SB_ID_INDEX_SHIFT(wl))
93 #define SB_ID_INDEX_MASK(wl) (SB_ID_NOREF_COUNT(wl) - 1)
96 RING_BUFFER_OVERWRITE
= 0, /* Overwrite when buffer full */
97 RING_BUFFER_DISCARD
= 1, /* Discard when buffer full */
101 struct crash_abi_unknown
{
102 uint8_t magic
[RB_CRASH_DUMP_ABI_MAGIC_LEN
];
103 uint64_t mmap_length
; /* Overall length of crash record */
105 * { 0x12, 0x34 }: big endian
106 * { 0x34, 0x12 }: little endian
108 uint16_t major
; /* Major number. */
109 uint16_t minor
; /* Minor number. */
110 uint8_t word_size
; /* Word size (bytes). */
111 uint8_t layout_type
; /* enum lttng_crash_layout */
112 } __attribute__((packed
));
114 struct crash_abi_0_0
{
115 struct crash_abi_unknown parent
;
118 uint32_t prod_offset
;
119 uint32_t consumed_offset
;
120 uint32_t commit_hot_array
;
121 uint32_t commit_hot_seq
;
122 uint32_t buf_wsb_array
;
125 uint32_t sb_array_shmp_offset
;
126 uint32_t sb_backend_p_offset
;
127 uint32_t content_size
;
128 uint32_t packet_size
;
129 } __attribute__((packed
)) offset
;
132 uint8_t consumed_offset
;
133 uint8_t commit_hot_seq
;
135 uint8_t sb_array_shmp_offset
;
136 uint8_t sb_backend_p_offset
;
137 uint8_t content_size
;
139 } __attribute__((packed
)) length
;
141 uint32_t commit_hot_array
;
142 uint32_t buf_wsb_array
;
144 } __attribute__((packed
)) stride
;
146 uint64_t buf_size
; /* Size of the buffer */
147 uint64_t subbuf_size
; /* Sub-buffer size */
148 uint64_t num_subbuf
; /* Number of sub-buffers for writer */
149 uint32_t mode
; /* Buffer mode: 0: overwrite, 1: discard */
150 } __attribute__((packed
));
152 struct lttng_crash_layout
{
154 int prod_offset
, consumed_offset
, commit_hot_array
, commit_hot_seq
, buf_wsb_array
,
155 buf_wsb_id
, sb_array
, sb_array_shmp_offset
, sb_backend_p_offset
,
156 content_size
, packet_size
;
159 int prod_offset
, consumed_offset
, commit_hot_seq
, buf_wsb_id
, sb_array_shmp_offset
,
160 sb_backend_p_offset
, content_size
, packet_size
;
163 int commit_hot_array
, buf_wsb_array
, sb_array
;
166 int reverse_byte_order
;
169 uint64_t mmap_length
; /* Length of crash record */
170 uint64_t buf_size
; /* Size of the buffer */
171 uint64_t subbuf_size
; /* Sub-buffer size */
172 uint64_t num_subbuf
; /* Number of sub-buffers for writer */
173 uint32_t mode
; /* Buffer mode: 0: overwrite, 1: discard */
178 static const char *progname
;
179 static char *opt_viewer_path
= nullptr;
180 static char *opt_output_path
= nullptr;
182 static char *the_input_path
;
184 int lttng_opt_quiet
, lttng_opt_verbose
, lttng_opt_mi
;
190 /* Getopt options. No first level command. */
191 static struct option long_options
[] = {
192 { "version", 0, nullptr, 'V' }, { "help", 0, nullptr, 'h' },
193 { "verbose", 0, nullptr, 'v' }, { "viewer", 1, nullptr, 'e' },
194 { "extract", 1, nullptr, 'x' }, { "list-options", 0, nullptr, OPT_DUMP_OPTIONS
},
195 { nullptr, 0, nullptr, 0 },
200 int ret
= utils_show_help(1, "lttng-crash", help_msg
);
203 ERR("Cannot show --help for `lttng-crash`");
209 static void version(FILE *ofp
)
212 "%s (LTTng Crash Trace Viewer) " VERSION
" - " VERSION_NAME
"%s%s\n",
214 GIT_VERSION
[0] == '\0' ? "" : " - " GIT_VERSION
,
215 EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " EXTRA_VERSION_NAME
);
221 * List options line by line. This is mostly for bash auto completion and to
222 * avoid difficult parsing.
224 static void list_options(FILE *ofp
)
227 struct option
*option
= nullptr;
229 option
= &long_options
[i
];
230 while (option
->name
!= nullptr) {
231 fprintf(ofp
, "--%s\n", option
->name
);
233 if (isprint(option
->val
)) {
234 fprintf(ofp
, "-%c\n", option
->val
);
238 option
= &long_options
[i
];
243 * Parse command line arguments.
245 * Return 0 if OK, else -1
247 static int parse_args(int argc
, char **argv
)
256 while ((opt
= getopt_long(argc
, argv
, "+Vhve:x:", long_options
, nullptr)) != -1) {
267 /* There is only 3 possible level of verbosity. (-vvv) */
268 if (lttng_opt_verbose
< 3) {
269 lttng_opt_verbose
+= 1;
273 free(opt_viewer_path
);
274 opt_viewer_path
= strdup(optarg
);
277 free(opt_output_path
);
278 opt_output_path
= strdup(optarg
);
280 case OPT_DUMP_OPTIONS
:
281 list_options(stdout
);
285 ERR("Unknown command-line option");
290 /* No leftovers, or more than one input path, print usage and quit */
291 if (argc
- optind
!= 1) {
292 ERR("Command-line error: Specify exactly one input path");
296 the_input_path
= argv
[optind
];
304 static int copy_file(const char *file_dest
, const char *file_src
)
306 int fd_src
= -1, fd_dest
= -1;
307 ssize_t readlen
, writelen
;
308 char buf
[COPY_BUFLEN
];
311 DBG("Copy metadata file '%s' into '%s'", file_src
, file_dest
);
313 fd_src
= open(file_src
, O_RDONLY
);
315 PERROR("Error opening %s for reading", file_src
);
319 fd_dest
= open(file_dest
, O_RDWR
| O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
321 PERROR("Error opening %s for writing", file_dest
);
327 readlen
= lttng_read(fd_src
, buf
, COPY_BUFLEN
);
329 PERROR("Error reading input file");
336 writelen
= lttng_write(fd_dest
, buf
, readlen
);
337 if (writelen
< readlen
) {
338 PERROR("Error writing to output file");
347 if (close(fd_src
) < 0) {
348 PERROR("Error closing %s", file_src
);
353 if (close(fd_dest
) < 0) {
354 PERROR("Error closing %s", file_dest
);
361 _crash_get_field(const struct lttng_crash_layout
*layout
, const char *ptr
, size_t size
)
365 return *(uint8_t *) ptr
;
367 if (layout
->reverse_byte_order
) {
368 return bswap_16(*(uint16_t *) ptr
);
370 return *(uint16_t *) ptr
;
373 if (layout
->reverse_byte_order
) {
374 return bswap_32(*(uint32_t *) ptr
);
376 return *(uint32_t *) ptr
;
379 if (layout
->reverse_byte_order
) {
380 return bswap_64(*(uint64_t *) ptr
);
382 return *(uint64_t *) ptr
;
390 #define crash_get_field(layout, map, name) \
391 _crash_get_field(layout, (map) + (layout)->offset.name, (layout)->length.name)
393 #define crash_get_array_field(layout, map, array_name, idx, field_name) \
394 _crash_get_field(layout, \
395 (map) + (layout)->offset.array_name + \
396 ((idx) * (layout)->stride.array_name) + \
397 (layout)->offset.field_name, \
398 (layout)->length.field_name)
400 #define crash_get_hdr_raw_field(layout, hdr, name) ((hdr)->name)
402 #define crash_get_hdr_field(layout, hdr, name) \
403 _crash_get_field(layout, (const char *) &(hdr)->name, sizeof((hdr)->name))
405 #define crash_get_layout(layout, hdr, name) \
407 (layout)->name = crash_get_hdr_field(layout, hdr, name); \
408 DBG("layout.%s = %" PRIu64, #name, (uint64_t) (layout)->name); \
411 static int get_crash_layout_0_0(struct lttng_crash_layout
*layout
, char *map
)
413 const struct crash_abi_0_0
*abi
= (const struct crash_abi_0_0
*) map
;
415 crash_get_layout(layout
, abi
, offset
.prod_offset
);
416 crash_get_layout(layout
, abi
, offset
.consumed_offset
);
417 crash_get_layout(layout
, abi
, offset
.commit_hot_array
);
418 crash_get_layout(layout
, abi
, offset
.commit_hot_seq
);
419 crash_get_layout(layout
, abi
, offset
.buf_wsb_array
);
420 crash_get_layout(layout
, abi
, offset
.buf_wsb_id
);
421 crash_get_layout(layout
, abi
, offset
.sb_array
);
422 crash_get_layout(layout
, abi
, offset
.sb_array_shmp_offset
);
423 crash_get_layout(layout
, abi
, offset
.sb_backend_p_offset
);
424 crash_get_layout(layout
, abi
, offset
.content_size
);
425 crash_get_layout(layout
, abi
, offset
.packet_size
);
427 crash_get_layout(layout
, abi
, length
.prod_offset
);
428 crash_get_layout(layout
, abi
, length
.consumed_offset
);
429 crash_get_layout(layout
, abi
, length
.commit_hot_seq
);
430 crash_get_layout(layout
, abi
, length
.buf_wsb_id
);
431 crash_get_layout(layout
, abi
, length
.sb_array_shmp_offset
);
432 crash_get_layout(layout
, abi
, length
.sb_backend_p_offset
);
433 crash_get_layout(layout
, abi
, length
.content_size
);
434 crash_get_layout(layout
, abi
, length
.packet_size
);
436 crash_get_layout(layout
, abi
, stride
.commit_hot_array
);
437 crash_get_layout(layout
, abi
, stride
.buf_wsb_array
);
438 crash_get_layout(layout
, abi
, stride
.sb_array
);
440 crash_get_layout(layout
, abi
, buf_size
);
441 crash_get_layout(layout
, abi
, subbuf_size
);
442 crash_get_layout(layout
, abi
, num_subbuf
);
443 crash_get_layout(layout
, abi
, mode
);
448 static void print_dbg_magic(const uint8_t *magic
)
450 DBG("magic: 0x%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X",
469 static int check_magic(const uint8_t *magic
)
473 for (i
= 0; i
< RB_CRASH_DUMP_ABI_MAGIC_LEN
; i
++) {
474 if ((magic
[i
] ^ 0xFF) != lttng_crash_expected_magic_xor
[i
]) {
481 static int get_crash_layout(struct lttng_crash_layout
*layout
, int fd
, const char *input_file
)
484 int ret
= 0, unmapret
;
485 const uint8_t *magic
;
486 uint64_t mmap_length
;
487 uint16_t major
, minor
;
489 const struct crash_abi_unknown
*abi
;
491 enum lttng_crash_type layout_type
;
494 ret
= fstat(fd
, &stat
);
496 PERROR("Failed to fstat '%s'", input_file
);
499 if (stat
.st_size
< RB_CRASH_DUMP_ABI_LEN
) {
500 ERR("File '%s' truncated: file length of %" PRIi64
501 " bytes does not meet the minimal expected "
502 "length of %d bytes",
504 (int64_t) stat
.st_size
,
505 RB_CRASH_DUMP_ABI_LEN
);
508 map
= (char *) mmap(nullptr, RB_CRASH_DUMP_ABI_LEN
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
509 if (map
== MAP_FAILED
) {
510 PERROR("Mapping file");
513 abi
= (const struct crash_abi_unknown
*) map
;
514 magic
= crash_get_hdr_raw_field(layout
, abi
, magic
);
515 print_dbg_magic(magic
);
516 if (check_magic(magic
)) {
517 DBG("Unknown magic number");
518 ret
= 1; /* positive return value, skip */
521 endian
= crash_get_hdr_field(layout
, abi
, endian
);
523 case RB_CRASH_ENDIAN
:
525 case RB_CRASH_ENDIAN_REVERSE
:
526 layout
->reverse_byte_order
= 1;
529 DBG("Unknown endianness value: 0x%X", (unsigned int) endian
);
530 ret
= 1; /* positive return value, skip */
533 layout_type
= (enum lttng_crash_type
) crash_get_hdr_field(layout
, abi
, layout_type
);
534 switch (layout_type
) {
535 case LTTNG_CRASH_TYPE_UST
:
537 case LTTNG_CRASH_TYPE_KERNEL
:
538 ERR("lttng-modules buffer layout support not implemented");
539 ret
= 1; /* positive return value, skip */
542 ERR("Unknown layout type %u", (unsigned int) layout_type
);
543 ret
= 1; /* positive return value, skip */
546 mmap_length
= crash_get_hdr_field(layout
, abi
, mmap_length
);
547 DBG("mmap_length: %" PRIu64
, mmap_length
);
548 layout
->mmap_length
= mmap_length
;
549 major
= crash_get_hdr_field(layout
, abi
, major
);
550 DBG("major: %u", major
);
551 minor
= crash_get_hdr_field(layout
, abi
, minor
);
552 DBG("minor: %u", minor
);
553 word_size
= crash_get_hdr_field(layout
, abi
, word_size
);
554 DBG("word_size: %u", word_size
);
559 ret
= get_crash_layout_0_0(layout
, map
);
565 ERR("Unsupported crash ABI %u.%u\n", major
, minor
);
570 ERR("Unsupported crash ABI %u.%u\n", major
, minor
);
574 layout
->word_size
= word_size
;
576 unmapret
= munmap(map
, RB_CRASH_DUMP_ABI_LEN
);
583 /* buf_trunc mask selects only the buffer number. */
584 static inline uint64_t buf_trunc(uint64_t offset
, uint64_t buf_size
)
586 return offset
& ~(buf_size
- 1);
589 /* subbuf_trunc mask selects the subbuffer number. */
590 static inline uint64_t subbuf_trunc(uint64_t offset
, uint64_t subbuf_size
)
592 return offset
& ~(subbuf_size
- 1);
595 /* buf_offset mask selects only the offset within the current buffer. */
596 static inline uint64_t buf_offset(uint64_t offset
, uint64_t buf_size
)
598 return offset
& (buf_size
- 1);
601 /* subbuf_offset mask selects the offset within the current subbuffer. */
602 static inline uint64_t subbuf_offset(uint64_t offset
, uint64_t subbuf_size
)
604 return offset
& (subbuf_size
- 1);
607 /* subbuf_index returns the index of the current subbuffer within the buffer. */
608 static inline uint64_t subbuf_index(uint64_t offset
, uint64_t buf_size
, uint64_t subbuf_size
)
610 return buf_offset(offset
, buf_size
) / subbuf_size
;
613 static inline uint64_t subbuffer_id_get_index(uint32_t mode
, uint64_t id
, unsigned int wl
)
615 if (mode
== RING_BUFFER_OVERWRITE
)
616 return id
& SB_ID_INDEX_MASK(wl
);
622 copy_crash_subbuf(const struct lttng_crash_layout
*layout
, int fd_dest
, char *buf
, uint64_t offset
)
624 uint64_t buf_size
, subbuf_size
, num_subbuf
, sbidx
, id
, sb_bindex
, rpages_offset
, p_offset
,
625 seq_cc
, committed
, commit_count_mask
, consumed_cur
, packet_size
;
630 * Get the current subbuffer by applying the proper mask to
631 * "offset", and looking up the subbuf location within the
635 buf_size
= layout
->buf_size
;
636 subbuf_size
= layout
->subbuf_size
;
637 num_subbuf
= layout
->num_subbuf
;
639 switch (layout
->word_size
) {
641 commit_count_mask
= 0xFFFFFFFFULL
/ num_subbuf
;
644 commit_count_mask
= 0xFFFFFFFFFFFFFFFFULL
/ num_subbuf
;
647 ERR("Unsupported word size: %u", (unsigned int) layout
->word_size
);
651 DBG("Copy crash subbuffer at offset %" PRIu64
, offset
);
652 sbidx
= subbuf_index(offset
, buf_size
, subbuf_size
);
655 * Find where the seq cc is located. Compute length of data to
658 seq_cc
= crash_get_array_field(layout
, buf
, commit_hot_array
, sbidx
, commit_hot_seq
);
659 consumed_cur
= crash_get_field(layout
, buf
, consumed_offset
);
662 * Check that the buffer we are getting is after or at
663 * consumed_cur position.
665 if ((long) subbuf_trunc(offset
, subbuf_size
) -
666 (long) subbuf_trunc(consumed_cur
, subbuf_size
) <
668 DBG("No data: position is before consumed_cur");
673 * Check if subbuffer has been fully committed.
675 if (((seq_cc
- subbuf_size
) & commit_count_mask
) -
676 (buf_trunc(offset
, buf_size
) / num_subbuf
) ==
678 committed
= subbuf_size
;
680 committed
= subbuf_offset(seq_cc
, subbuf_size
);
682 DBG("No data committed, seq_cc: %" PRIu64
, seq_cc
);
687 /* Find actual physical offset in subbuffer table */
688 id
= crash_get_array_field(layout
, buf
, buf_wsb_array
, sbidx
, buf_wsb_id
);
689 sb_bindex
= subbuffer_id_get_index(layout
->mode
, id
, layout
->word_size
);
691 crash_get_array_field(layout
, buf
, sb_array
, sb_bindex
, sb_array_shmp_offset
);
692 p_offset
= crash_get_field(layout
, buf
+ rpages_offset
, sb_backend_p_offset
);
693 subbuf_ptr
= buf
+ p_offset
;
695 if (committed
== subbuf_size
) {
697 * Packet header can be used.
699 if (layout
->length
.packet_size
) {
701 subbuf_ptr
+ layout
->offset
.packet_size
,
702 layout
->length
.packet_size
);
703 if (layout
->reverse_byte_order
) {
704 packet_size
= bswap_64(packet_size
);
706 packet_size
/= CHAR_BIT
;
708 packet_size
= subbuf_size
;
714 * Find where to patch the sub-buffer header with actual
715 * readable data len and packet len, derived from seq
716 * cc. Patch it in our in-memory copy.
718 patch_size
= committed
* CHAR_BIT
;
719 if (layout
->reverse_byte_order
) {
720 patch_size
= bswap_64(patch_size
);
722 if (layout
->length
.content_size
) {
723 memcpy(subbuf_ptr
+ layout
->offset
.content_size
,
725 layout
->length
.content_size
);
727 if (layout
->length
.packet_size
) {
728 memcpy(subbuf_ptr
+ layout
->offset
.packet_size
,
730 layout
->length
.packet_size
);
732 packet_size
= committed
;
736 * Copy packet into fd_dest.
738 writelen
= lttng_write(fd_dest
, subbuf_ptr
, packet_size
);
739 if (writelen
< packet_size
) {
740 PERROR("Error writing to output file");
743 DBG("Copied %" PRIu64
" bytes of data", packet_size
);
750 static int copy_crash_data(const struct lttng_crash_layout
*layout
, int fd_dest
, int fd_src
)
753 int ret
= 0, has_data
= 0;
756 uint64_t prod_offset
, consumed_offset
;
757 uint64_t offset
, subbuf_size
;
760 ret
= fstat(fd_src
, &statbuf
);
764 src_file_len
= layout
->mmap_length
;
765 buf
= calloc
<char>(src_file_len
);
769 readlen
= lttng_read(fd_src
, buf
, src_file_len
);
771 PERROR("Error reading input file");
776 prod_offset
= crash_get_field(layout
, buf
, prod_offset
);
777 DBG("prod_offset: 0x%" PRIx64
, prod_offset
);
778 consumed_offset
= crash_get_field(layout
, buf
, consumed_offset
);
779 DBG("consumed_offset: 0x%" PRIx64
, consumed_offset
);
780 subbuf_size
= layout
->subbuf_size
;
782 for (offset
= consumed_offset
; offset
< prod_offset
; offset
+= subbuf_size
) {
783 ret
= copy_crash_subbuf(layout
, fd_dest
, buf
, offset
);
793 if (ret
&& ret
!= -ENODATA
) {
804 extract_file(int output_dir_fd
, const char *output_file
, int input_dir_fd
, const char *input_file
)
806 int fd_dest
, fd_src
, ret
= 0, closeret
;
807 struct lttng_crash_layout layout
;
809 layout
.reverse_byte_order
= 0; /* For reading magic number */
811 DBG("Extract file '%s'", input_file
);
812 fd_src
= openat(input_dir_fd
, input_file
, O_RDONLY
);
814 PERROR("Error opening '%s' for reading", input_file
);
819 /* Query the crash ABI layout */
820 ret
= get_crash_layout(&layout
, fd_src
, input_file
);
825 fd_dest
= openat(output_dir_fd
,
827 O_RDWR
| O_CREAT
| O_EXCL
,
828 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
830 PERROR("Error opening '%s' for writing", output_file
);
835 ret
= copy_crash_data(&layout
, fd_dest
, fd_src
);
841 closeret
= close(fd_dest
);
845 if (ret
== -ENODATA
) {
846 closeret
= unlinkat(output_dir_fd
, output_file
, 0);
852 closeret
= close(fd_src
);
860 static int extract_all_files(const char *output_path
, const char *input_path
)
862 DIR *input_dir
, *output_dir
;
863 int input_dir_fd
, output_dir_fd
, ret
= 0, closeret
;
864 struct dirent
*entry
; /* input */
866 /* Open input directory */
867 input_dir
= opendir(input_path
);
869 PERROR("Cannot open '%s' path", input_path
);
872 input_dir_fd
= dirfd(input_dir
);
873 if (input_dir_fd
< 0) {
878 /* Open output directory */
879 output_dir
= opendir(output_path
);
881 PERROR("Cannot open '%s' path", output_path
);
884 output_dir_fd
= dirfd(output_dir
);
885 if (output_dir_fd
< 0) {
890 while ((entry
= readdir(input_dir
))) {
891 if (!strcmp(entry
->d_name
, ".") || !strcmp(entry
->d_name
, ".."))
893 ret
= extract_file(output_dir_fd
, entry
->d_name
, input_dir_fd
, entry
->d_name
);
894 if (ret
== -ENODATA
) {
895 DBG("No data in file '%s', skipping", entry
->d_name
);
898 } else if (ret
< 0) {
900 } else if (ret
> 0) {
901 DBG("Skipping file '%s'", entry
->d_name
);
906 closeret
= closedir(output_dir
);
910 closeret
= closedir(input_dir
);
917 static int extract_one_trace(const char *output_path
, const char *input_path
)
919 char dest
[PATH_MAX
], src
[PATH_MAX
];
922 DBG("Extract crash trace '%s' into '%s'", input_path
, output_path
);
925 strncpy(dest
, output_path
, PATH_MAX
);
926 dest
[PATH_MAX
- 1] = '\0';
927 strncat(dest
, "/metadata", PATH_MAX
- strlen(dest
) - 1);
929 strncpy(src
, input_path
, PATH_MAX
);
930 src
[PATH_MAX
- 1] = '\0';
931 strncat(src
, "/metadata", PATH_MAX
- strlen(dest
) - 1);
933 ret
= copy_file(dest
, src
);
938 /* Extract each other file that has expected header */
939 return extract_all_files(output_path
, input_path
);
942 static int extract_trace_recursive(const char *output_path
, const char *input_path
)
945 int dir_fd
, ret
= 0, closeret
;
946 struct dirent
*entry
;
951 dir
= opendir(input_path
);
953 PERROR("Cannot open '%s' path", input_path
);
957 path_len
= strlen(input_path
);
965 while ((entry
= readdir(dir
))) {
968 char filename
[PATH_MAX
];
970 if (!strcmp(entry
->d_name
, ".") || !strcmp(entry
->d_name
, "..")) {
974 name_len
= strlen(entry
->d_name
);
975 if (path_len
+ name_len
+ 2 > sizeof(filename
)) {
976 ERR("Failed to remove file: path name too long (%s/%s)",
982 if (snprintf(filename
, sizeof(filename
), "%s/%s", input_path
, entry
->d_name
) < 0) {
983 ERR("Failed to format path.");
987 if (stat(filename
, &st
)) {
992 if (S_ISDIR(st
.st_mode
)) {
993 char output_subpath
[PATH_MAX
];
994 char input_subpath
[PATH_MAX
];
996 strncpy(output_subpath
, output_path
, sizeof(output_subpath
));
997 output_subpath
[sizeof(output_subpath
) - 1] = '\0';
998 strncat(output_subpath
,
1000 sizeof(output_subpath
) - strlen(output_subpath
) - 1);
1001 strncat(output_subpath
,
1003 sizeof(output_subpath
) - strlen(output_subpath
) - 1);
1005 ret
= mkdir(output_subpath
, S_IRWXU
| S_IRWXG
);
1012 strncpy(input_subpath
, input_path
, sizeof(input_subpath
));
1013 input_subpath
[sizeof(input_subpath
) - 1] = '\0';
1014 strncat(input_subpath
,
1016 sizeof(input_subpath
) - strlen(input_subpath
) - 1);
1017 strncat(input_subpath
,
1019 sizeof(input_subpath
) - strlen(input_subpath
) - 1);
1021 ret
= extract_trace_recursive(output_subpath
, input_subpath
);
1025 } else if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
)) {
1026 if (!strcmp(entry
->d_name
, "metadata")) {
1027 ret
= extract_one_trace(output_path
, input_path
);
1029 WARN("Error extracting trace '%s', continuing anyway.",
1040 closeret
= closedir(dir
);
1047 static int delete_dir_recursive(const char *path
)
1050 int dir_fd
, ret
= 0, closeret
;
1052 struct dirent
*entry
;
1054 /* Open trace directory */
1055 dir
= opendir(path
);
1057 PERROR("Cannot open '%s' path", path
);
1059 goto end_no_closedir
;
1062 path_len
= strlen(path
);
1064 dir_fd
= dirfd(dir
);
1071 while ((entry
= readdir(dir
))) {
1074 char filename
[PATH_MAX
];
1076 if (!strcmp(entry
->d_name
, ".") || !strcmp(entry
->d_name
, "..")) {
1080 name_len
= strlen(entry
->d_name
);
1081 if (path_len
+ name_len
+ 2 > sizeof(filename
)) {
1082 ERR("Failed to remove file: path name too long (%s/%s)",
1088 if (snprintf(filename
, sizeof(filename
), "%s/%s", path
, entry
->d_name
) < 0) {
1089 ERR("Failed to format path.");
1093 if (stat(filename
, &st
)) {
1098 if (S_ISDIR(st
.st_mode
)) {
1099 char *subpath
= calloc
<char>(PATH_MAX
);
1102 PERROR("zmalloc path");
1106 strncpy(subpath
, path
, PATH_MAX
);
1107 subpath
[PATH_MAX
- 1] = '\0';
1108 strncat(subpath
, "/", PATH_MAX
- strlen(subpath
) - 1);
1109 strncat(subpath
, entry
->d_name
, PATH_MAX
- strlen(subpath
) - 1);
1111 ret
= delete_dir_recursive(subpath
);
1114 /* Error occurred, abort traversal. */
1117 } else if (S_ISREG(st
.st_mode
)) {
1118 ret
= unlinkat(dir_fd
, entry
->d_name
, 0);
1120 PERROR("Unlinking '%s'", entry
->d_name
);
1132 PERROR("rmdir '%s'", path
);
1135 closeret
= closedir(dir
);
1143 static int view_trace(const char *trace_path
, char *viewer_path
)
1152 } else if (pid
> 0) {
1156 pid
= waitpid(pid
, &status
, 0);
1157 if (pid
< 0 || !WIFEXITED(status
)) {
1164 ret
= spawn_viewer(trace_path
, viewer_path
, false);
1177 int main(int argc
, char *argv
[])
1180 bool has_warning
= false;
1181 const char *output_path
= nullptr;
1182 char tmppath
[] = "/tmp/lttng-crash-XXXXXX";
1184 progname
= argv
[0] ? argv
[0] : "lttng-crash";
1186 ret
= parse_args(argc
, argv
);
1189 } else if (ret
< 0) {
1194 if (opt_output_path
) {
1195 output_path
= opt_output_path
;
1196 ret
= mkdir(output_path
, S_IRWXU
| S_IRWXG
);
1203 output_path
= mkdtemp(tmppath
);
1211 ret
= extract_trace_recursive(output_path
, the_input_path
);
1215 } else if (ret
> 0) {
1216 /* extract_trace_recursive reported a warning. */
1219 if (!opt_output_path
) {
1221 ret
= view_trace(output_path
, opt_viewer_path
);
1225 /* unlink temporary trace */
1226 ret
= delete_dir_recursive(output_path
);
1232 exit(has_warning
? EXIT_FAILURE
: EXIT_SUCCESS
);