2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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.
25 #include <sys/types.h>
38 #include <lttng/lttng.h>
39 #include <common/common.h>
40 #include <common/utils.h>
42 #define DEFAULT_VIEWER "babeltrace"
44 #define COPY_BUFLEN 4096
45 #define RB_CRASH_DUMP_ABI_LEN 32
47 #define RB_CRASH_DUMP_ABI_MAGIC_LEN 16
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,
56 #define RB_CRASH_DUMP_ABI_MAGIC_XOR \
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, \
64 static const char *help_msg
=
65 #ifdef LTTNG_EMBED_HELP
66 #include <lttng-crash.1.h>
73 * Non-static to ensure the compiler does not optimize away the xor.
75 uint8_t lttng_crash_expected_magic_xor
[] = RB_CRASH_DUMP_ABI_MAGIC_XOR
;
77 #define RB_CRASH_ENDIAN 0x1234
78 #define RB_CRASH_ENDIAN_REVERSE 0x3412
80 enum lttng_crash_type
{
81 LTTNG_CRASH_TYPE_UST
= 0,
82 LTTNG_CRASH_TYPE_KERNEL
= 1,
85 /* LTTng ring buffer defines (copied) */
87 #define HALF_ULONG_BITS(wl) (((wl) * CHAR_BIT) >> 1)
89 #define SB_ID_OFFSET_SHIFT(wl) (HALF_ULONG_BITS(wl) + 1)
90 #define SB_ID_OFFSET_COUNT(wl) (1UL << SB_ID_OFFSET_SHIFT(wl))
91 #define SB_ID_OFFSET_MASK(wl) (~(SB_ID_OFFSET_COUNT(wl) - 1))
93 * Lowest bit of top word half belongs to noref. Used only for overwrite mode.
95 #define SB_ID_NOREF_SHIFT(wl) (SB_ID_OFFSET_SHIFT(wl) - 1)
96 #define SB_ID_NOREF_COUNT(wl) (1UL << SB_ID_NOREF_SHIFT(wl))
97 #define SB_ID_NOREF_MASK(wl) SB_ID_NOREF_COUNT(wl)
99 * In overwrite mode: lowest half of word is used for index.
100 * Limit of 2^16 subbuffers per buffer on 32-bit, 2^32 on 64-bit.
101 * In producer-consumer mode: whole word used for index.
103 #define SB_ID_INDEX_SHIFT(wl) 0
104 #define SB_ID_INDEX_COUNT(wl) (1UL << SB_ID_INDEX_SHIFT(wl))
105 #define SB_ID_INDEX_MASK(wl) (SB_ID_NOREF_COUNT(wl) - 1)
108 RING_BUFFER_OVERWRITE
= 0, /* Overwrite when buffer full */
109 RING_BUFFER_DISCARD
= 1, /* Discard when buffer full */
112 struct crash_abi_unknown
{
113 uint8_t magic
[RB_CRASH_DUMP_ABI_MAGIC_LEN
];
114 uint64_t mmap_length
; /* Overall length of crash record */
116 * { 0x12, 0x34 }: big endian
117 * { 0x34, 0x12 }: little endian
119 uint16_t major
; /* Major number. */
120 uint16_t minor
; /* Minor number. */
121 uint8_t word_size
; /* Word size (bytes). */
122 uint8_t layout_type
; /* enum lttng_crash_layout */
123 } __attribute__((packed
));
125 struct crash_abi_0_0
{
126 struct crash_abi_unknown parent
;
129 uint32_t prod_offset
;
130 uint32_t consumed_offset
;
131 uint32_t commit_hot_array
;
132 uint32_t commit_hot_seq
;
133 uint32_t buf_wsb_array
;
136 uint32_t sb_array_shmp_offset
;
137 uint32_t sb_backend_p_offset
;
138 uint32_t content_size
;
139 uint32_t packet_size
;
140 } __attribute__((packed
)) offset
;
143 uint8_t consumed_offset
;
144 uint8_t commit_hot_seq
;
146 uint8_t sb_array_shmp_offset
;
147 uint8_t sb_backend_p_offset
;
148 uint8_t content_size
;
150 } __attribute__((packed
)) length
;
152 uint32_t commit_hot_array
;
153 uint32_t buf_wsb_array
;
155 } __attribute__((packed
)) stride
;
157 uint64_t buf_size
; /* Size of the buffer */
158 uint64_t subbuf_size
; /* Sub-buffer size */
159 uint64_t num_subbuf
; /* Number of sub-buffers for writer */
160 uint32_t mode
; /* Buffer mode: 0: overwrite, 1: discard */
161 } __attribute__((packed
));
163 struct lttng_crash_layout
{
165 int prod_offset
, consumed_offset
,
166 commit_hot_array
, commit_hot_seq
,
167 buf_wsb_array
, buf_wsb_id
,
168 sb_array
, sb_array_shmp_offset
,
169 sb_backend_p_offset
, content_size
,
173 int prod_offset
, consumed_offset
,
174 commit_hot_seq
, buf_wsb_id
,
175 sb_array_shmp_offset
, sb_backend_p_offset
,
176 content_size
, packet_size
;
179 int commit_hot_array
, buf_wsb_array
, sb_array
;
182 int reverse_byte_order
;
185 uint64_t mmap_length
; /* Length of crash record */
186 uint64_t buf_size
; /* Size of the buffer */
187 uint64_t subbuf_size
; /* Sub-buffer size */
188 uint64_t num_subbuf
; /* Number of sub-buffers for writer */
189 uint32_t mode
; /* Buffer mode: 0: overwrite, 1: discard */
193 static char *progname
,
194 *opt_viewer_path
= NULL
,
195 *opt_output_path
= NULL
;
197 static char *input_path
;
199 int lttng_opt_quiet
, lttng_opt_verbose
, lttng_opt_mi
;
205 /* Getopt options. No first level command. */
206 static struct option long_options
[] = {
207 { "version", 0, NULL
, 'V' },
208 { "help", 0, NULL
, 'h' },
209 { "verbose", 0, NULL
, 'v' },
210 { "viewer", 1, NULL
, 'e' },
211 { "extract", 1, NULL
, 'x' },
212 { "list-options", 0, NULL
, OPT_DUMP_OPTIONS
},
213 { NULL
, 0, NULL
, 0 },
216 static void usage(void)
218 int ret
= utils_show_help(1, "lttng-crash", help_msg
);
221 ERR("Cannot show --help for `lttng-crash`");
227 static void version(FILE *ofp
)
229 fprintf(ofp
, "%s (LTTng Crash Trace Viewer) " VERSION
" - " VERSION_NAME
232 GIT_VERSION
[0] == '\0' ? "" : " - " GIT_VERSION
);
238 * List options line by line. This is mostly for bash auto completion and to
239 * avoid difficult parsing.
241 static void list_options(FILE *ofp
)
244 struct option
*option
= NULL
;
246 option
= &long_options
[i
];
247 while (option
->name
!= NULL
) {
248 fprintf(ofp
, "--%s\n", option
->name
);
250 if (isprint(option
->val
)) {
251 fprintf(ofp
, "-%c\n", option
->val
);
255 option
= &long_options
[i
];
260 * Parse command line arguments.
262 * Return 0 if OK, else -1
264 static int parse_args(int argc
, char **argv
)
273 while ((opt
= getopt_long(argc
, argv
, "+Vhve:x:", long_options
, NULL
)) != -1) {
284 /* There is only 3 possible level of verbosity. (-vvv) */
285 if (lttng_opt_verbose
< 3) {
286 lttng_opt_verbose
+= 1;
290 free(opt_viewer_path
);
291 opt_viewer_path
= strdup(optarg
);
294 free(opt_output_path
);
295 opt_output_path
= strdup(optarg
);
297 case OPT_DUMP_OPTIONS
:
298 list_options(stdout
);
302 ERR("Unknown command-line option");
307 if (!opt_viewer_path
) {
308 opt_viewer_path
= DEFAULT_VIEWER
;
311 /* No leftovers, or more than one input path, print usage and quit */
312 if (argc
- optind
!= 1) {
313 ERR("Command-line error: Specify exactly one input path");
317 input_path
= argv
[optind
];
326 int copy_file(const char *file_dest
, const char *file_src
)
328 int fd_src
= -1, fd_dest
= -1;
329 ssize_t readlen
, writelen
;
330 char buf
[COPY_BUFLEN
];
333 DBG("Copy metadata file '%s' into '%s'", file_src
, file_dest
);
335 fd_src
= open(file_src
, O_RDONLY
);
337 PERROR("Error opening %s for reading", file_src
);
341 fd_dest
= open(file_dest
, O_RDWR
| O_CREAT
| O_EXCL
,
342 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
344 PERROR("Error opening %s for writing", file_dest
);
350 readlen
= lttng_read(fd_src
, buf
, COPY_BUFLEN
);
352 PERROR("Error reading input file");
359 writelen
= lttng_write(fd_dest
, buf
, readlen
);
360 if (writelen
< readlen
) {
361 PERROR("Error writing to output file");
370 if (close(fd_src
) < 0) {
371 PERROR("Error closing %s", file_src
);
376 if (close(fd_dest
) < 0) {
377 PERROR("Error closing %s", file_dest
);
384 uint64_t _crash_get_field(const struct lttng_crash_layout
*layout
,
385 const char *ptr
, size_t size
)
388 case 1: return *(uint8_t *) ptr
;
389 case 2: if (layout
->reverse_byte_order
) {
390 return __bswap_16(*(uint16_t *) ptr
);
392 return *(uint16_t *) ptr
;
395 case 4: if (layout
->reverse_byte_order
) {
396 return __bswap_32(*(uint32_t *) ptr
);
398 return *(uint32_t *) ptr
;
401 case 8: if (layout
->reverse_byte_order
) {
402 return __bswap_64(*(uint64_t *) ptr
);
404 return *(uint64_t *) ptr
;
413 #define crash_get_field(layout, map, name) \
414 _crash_get_field(layout, (map) + (layout)->offset.name, \
417 #define crash_get_array_field(layout, map, array_name, idx, field_name) \
418 _crash_get_field(layout, \
419 (map) + (layout)->offset.array_name \
420 + (idx * (layout)->stride.array_name) \
421 + (layout)->offset.field_name, \
422 (layout)->length.field_name)
424 #define crash_get_hdr_raw_field(layout, hdr, name) ((hdr)->name)
426 #define crash_get_hdr_field(layout, hdr, name) \
427 _crash_get_field(layout, (const char *) &(hdr)->name, \
430 #define crash_get_layout(layout, hdr, name) \
432 (layout)->name = crash_get_hdr_field(layout, hdr, \
434 DBG("layout.%s = %" PRIu64, #name, \
435 (uint64_t) (layout)->name); \
439 int get_crash_layout_0_0(struct lttng_crash_layout
*layout
,
442 const struct crash_abi_0_0
*abi
= (const struct crash_abi_0_0
*) map
;
444 crash_get_layout(layout
, abi
, offset
.prod_offset
);
445 crash_get_layout(layout
, abi
, offset
.consumed_offset
);
446 crash_get_layout(layout
, abi
, offset
.commit_hot_array
);
447 crash_get_layout(layout
, abi
, offset
.commit_hot_seq
);
448 crash_get_layout(layout
, abi
, offset
.buf_wsb_array
);
449 crash_get_layout(layout
, abi
, offset
.buf_wsb_id
);
450 crash_get_layout(layout
, abi
, offset
.sb_array
);
451 crash_get_layout(layout
, abi
, offset
.sb_array_shmp_offset
);
452 crash_get_layout(layout
, abi
, offset
.sb_backend_p_offset
);
453 crash_get_layout(layout
, abi
, offset
.content_size
);
454 crash_get_layout(layout
, abi
, offset
.packet_size
);
456 crash_get_layout(layout
, abi
, length
.prod_offset
);
457 crash_get_layout(layout
, abi
, length
.consumed_offset
);
458 crash_get_layout(layout
, abi
, length
.commit_hot_seq
);
459 crash_get_layout(layout
, abi
, length
.buf_wsb_id
);
460 crash_get_layout(layout
, abi
, length
.sb_array_shmp_offset
);
461 crash_get_layout(layout
, abi
, length
.sb_backend_p_offset
);
462 crash_get_layout(layout
, abi
, length
.content_size
);
463 crash_get_layout(layout
, abi
, length
.packet_size
);
465 crash_get_layout(layout
, abi
, stride
.commit_hot_array
);
466 crash_get_layout(layout
, abi
, stride
.buf_wsb_array
);
467 crash_get_layout(layout
, abi
, stride
.sb_array
);
469 crash_get_layout(layout
, abi
, buf_size
);
470 crash_get_layout(layout
, abi
, subbuf_size
);
471 crash_get_layout(layout
, abi
, num_subbuf
);
472 crash_get_layout(layout
, abi
, mode
);
478 void print_dbg_magic(const uint8_t *magic
)
480 DBG("magic: 0x%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X",
481 magic
[0], magic
[1], magic
[2], magic
[3],
482 magic
[4], magic
[5], magic
[6], magic
[7],
483 magic
[8], magic
[9], magic
[10], magic
[11],
484 magic
[12], magic
[13], magic
[14], magic
[15]);
488 int check_magic(const uint8_t *magic
)
492 for (i
= 0; i
< RB_CRASH_DUMP_ABI_MAGIC_LEN
; i
++) {
493 if ((magic
[i
] ^ 0xFF) != lttng_crash_expected_magic_xor
[i
]) {
501 int get_crash_layout(struct lttng_crash_layout
*layout
, int fd
)
504 int ret
= 0, unmapret
;
505 const uint8_t *magic
;
506 uint64_t mmap_length
;
507 uint16_t major
, minor
;
509 const struct crash_abi_unknown
*abi
;
511 enum lttng_crash_type layout_type
;
513 map
= mmap(NULL
, RB_CRASH_DUMP_ABI_LEN
, PROT_READ
, MAP_PRIVATE
,
515 if (map
== MAP_FAILED
) {
516 PERROR("Mapping file");
519 abi
= (const struct crash_abi_unknown
*) map
;
520 magic
= crash_get_hdr_raw_field(layout
, abi
, magic
);
521 print_dbg_magic(magic
);
522 if (check_magic(magic
)) {
523 DBG("Unknown magic number");
524 ret
= 1; /* positive return value, skip */
527 endian
= crash_get_hdr_field(layout
, abi
, endian
);
529 case RB_CRASH_ENDIAN
:
531 case RB_CRASH_ENDIAN_REVERSE
:
532 layout
->reverse_byte_order
= 1;
535 DBG("Unknown endianness value: 0x%X", (unsigned int) endian
);
536 ret
= 1; /* positive return value, skip */
539 layout_type
= (enum lttng_crash_type
) crash_get_hdr_field(layout
, abi
, layout_type
);
540 switch (layout_type
) {
541 case LTTNG_CRASH_TYPE_UST
:
543 case LTTNG_CRASH_TYPE_KERNEL
:
544 ERR("lttng-modules buffer layout support not implemented");
545 ret
= 1; /* positive return value, skip */
548 ERR("Unknown layout type %u", (unsigned int) layout_type
);
549 ret
= 1; /* positive return value, skip */
552 mmap_length
= crash_get_hdr_field(layout
, abi
, mmap_length
);
553 DBG("mmap_length: %" PRIu64
, mmap_length
);
554 layout
->mmap_length
= mmap_length
;
555 major
= crash_get_hdr_field(layout
, abi
, major
);
556 DBG("major: %u", major
);
557 minor
= crash_get_hdr_field(layout
, abi
, minor
);
558 DBG("minor: %u", minor
);
559 word_size
= crash_get_hdr_field(layout
, abi
, word_size
);
560 DBG("word_size: %u", word_size
);
565 ret
= get_crash_layout_0_0(layout
, map
);
571 ERR("Unsupported crash ABI %u.%u\n", major
, minor
);
576 ERR("Unsupported crash ABI %u.%u\n", major
, minor
);
580 layout
->word_size
= word_size
;
582 unmapret
= munmap(map
, RB_CRASH_DUMP_ABI_LEN
);
589 /* buf_trunc mask selects only the buffer number. */
591 uint64_t buf_trunc(uint64_t offset
, uint64_t buf_size
)
593 return offset
& ~(buf_size
- 1);
596 /* subbuf_trunc mask selects the subbuffer number. */
598 uint64_t subbuf_trunc(uint64_t offset
, uint64_t subbuf_size
)
600 return offset
& ~(subbuf_size
- 1);
603 /* buf_offset mask selects only the offset within the current buffer. */
605 uint64_t buf_offset(uint64_t offset
, uint64_t buf_size
)
607 return offset
& (buf_size
- 1);
610 /* subbuf_offset mask selects the offset within the current subbuffer. */
612 uint64_t subbuf_offset(uint64_t offset
, uint64_t subbuf_size
)
614 return offset
& (subbuf_size
- 1);
617 /* subbuf_index returns the index of the current subbuffer within the buffer. */
619 uint64_t subbuf_index(uint64_t offset
, uint64_t buf_size
, uint64_t subbuf_size
)
621 return buf_offset(offset
, buf_size
) / subbuf_size
;
625 uint64_t subbuffer_id_get_index(uint32_t mode
, uint64_t id
,
628 if (mode
== RING_BUFFER_OVERWRITE
)
629 return id
& SB_ID_INDEX_MASK(wl
);
635 int copy_crash_subbuf(const struct lttng_crash_layout
*layout
,
636 int fd_dest
, char *buf
, uint64_t offset
)
638 uint64_t buf_size
, subbuf_size
, num_subbuf
, sbidx
, id
,
639 sb_bindex
, rpages_offset
, p_offset
, seq_cc
,
640 committed
, commit_count_mask
, consumed_cur
,
646 * Get the current subbuffer by applying the proper mask to
647 * "offset", and looking up the subbuf location within the
651 buf_size
= layout
->buf_size
;
652 subbuf_size
= layout
->subbuf_size
;
653 num_subbuf
= layout
->num_subbuf
;
655 switch (layout
->word_size
) {
656 case 4: commit_count_mask
= 0xFFFFFFFFULL
/ num_subbuf
;
658 case 8: commit_count_mask
= 0xFFFFFFFFFFFFFFFFULL
/ num_subbuf
;
661 ERR("Unsupported word size: %u",
662 (unsigned int) layout
->word_size
);
666 DBG("Copy crash subbuffer at offset %" PRIu64
, offset
);
667 sbidx
= subbuf_index(offset
, buf_size
, subbuf_size
);
670 * Find where the seq cc is located. Compute length of data to
673 seq_cc
= crash_get_array_field(layout
, buf
, commit_hot_array
,
674 sbidx
, commit_hot_seq
);
675 consumed_cur
= crash_get_field(layout
, buf
, consumed_offset
);
678 * Check that the buffer we are getting is after or at
679 * consumed_cur position.
681 if ((long) subbuf_trunc(offset
, subbuf_size
)
682 - (long) subbuf_trunc(consumed_cur
, subbuf_size
) < 0) {
683 DBG("No data: position is before consumed_cur");
688 * Check if subbuffer has been fully committed.
690 if (((seq_cc
- subbuf_size
) & commit_count_mask
)
691 - (buf_trunc(offset
, buf_size
) / num_subbuf
)
693 committed
= subbuf_size
;
695 committed
= subbuf_offset(seq_cc
, subbuf_size
);
697 DBG("No data committed, seq_cc: %" PRIu64
, seq_cc
);
702 /* Find actual physical offset in subbuffer table */
703 id
= crash_get_array_field(layout
, buf
, buf_wsb_array
,
705 sb_bindex
= subbuffer_id_get_index(layout
->mode
, id
,
707 rpages_offset
= crash_get_array_field(layout
, buf
, sb_array
,
708 sb_bindex
, sb_array_shmp_offset
);
709 p_offset
= crash_get_field(layout
, buf
+ rpages_offset
,
710 sb_backend_p_offset
);
711 subbuf_ptr
= buf
+ p_offset
;
713 if (committed
== subbuf_size
) {
715 * Packet header can be used.
717 if (layout
->length
.packet_size
) {
719 subbuf_ptr
+ layout
->offset
.packet_size
,
720 layout
->length
.packet_size
);
721 if (layout
->reverse_byte_order
) {
722 packet_size
= __bswap_64(packet_size
);
724 packet_size
/= CHAR_BIT
;
726 packet_size
= subbuf_size
;
732 * Find where to patch the sub-buffer header with actual
733 * readable data len and packet len, derived from seq
734 * cc. Patch it in our in-memory copy.
736 patch_size
= committed
* CHAR_BIT
;
737 if (layout
->reverse_byte_order
) {
738 patch_size
= __bswap_64(patch_size
);
740 if (layout
->length
.content_size
) {
741 memcpy(subbuf_ptr
+ layout
->offset
.content_size
,
742 &patch_size
, layout
->length
.content_size
);
744 if (layout
->length
.packet_size
) {
745 memcpy(subbuf_ptr
+ layout
->offset
.packet_size
,
746 &patch_size
, layout
->length
.packet_size
);
748 packet_size
= committed
;
752 * Copy packet into fd_dest.
754 writelen
= lttng_write(fd_dest
, subbuf_ptr
, packet_size
);
755 if (writelen
< packet_size
) {
756 PERROR("Error writing to output file");
759 DBG("Copied %" PRIu64
" bytes of data", packet_size
);
767 int copy_crash_data(const struct lttng_crash_layout
*layout
, int fd_dest
,
771 int ret
= 0, has_data
= 0;
774 uint64_t prod_offset
, consumed_offset
;
775 uint64_t offset
, subbuf_size
;
778 ret
= fstat(fd_src
, &statbuf
);
782 src_file_len
= layout
->mmap_length
;
783 buf
= zmalloc(src_file_len
);
787 readlen
= lttng_read(fd_src
, buf
, src_file_len
);
789 PERROR("Error reading input file");
794 prod_offset
= crash_get_field(layout
, buf
, prod_offset
);
795 DBG("prod_offset: 0x%" PRIx64
, prod_offset
);
796 consumed_offset
= crash_get_field(layout
, buf
, consumed_offset
);
797 DBG("consumed_offset: 0x%" PRIx64
, consumed_offset
);
798 subbuf_size
= layout
->subbuf_size
;
800 for (offset
= consumed_offset
; offset
< prod_offset
;
801 offset
+= subbuf_size
) {
802 ret
= copy_crash_subbuf(layout
, fd_dest
, buf
, offset
);
812 if (ret
&& ret
!= -ENODATA
) {
823 int extract_file(int output_dir_fd
, const char *output_file
,
824 int input_dir_fd
, const char *input_file
)
826 int fd_dest
, fd_src
, ret
= 0, closeret
;
827 struct lttng_crash_layout layout
;
829 layout
.reverse_byte_order
= 0; /* For reading magic number */
831 DBG("Extract file '%s'", input_file
);
832 fd_src
= openat(input_dir_fd
, input_file
, O_RDONLY
);
834 PERROR("Error opening '%s' for reading",
840 /* Query the crash ABI layout */
841 ret
= get_crash_layout(&layout
, fd_src
);
846 fd_dest
= openat(output_dir_fd
, output_file
,
847 O_RDWR
| O_CREAT
| O_EXCL
,
848 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
850 PERROR("Error opening '%s' for writing",
856 ret
= copy_crash_data(&layout
, fd_dest
, fd_src
);
862 closeret
= close(fd_dest
);
866 if (ret
== -ENODATA
) {
867 closeret
= unlinkat(output_dir_fd
, output_file
, 0);
873 closeret
= close(fd_src
);
882 int extract_all_files(const char *output_path
,
883 const char *input_path
)
885 DIR *input_dir
, *output_dir
;
886 int input_dir_fd
, output_dir_fd
, ret
= 0, closeret
;
887 struct dirent
*entry
; /* input */
889 /* Open input directory */
890 input_dir
= opendir(input_path
);
892 PERROR("Cannot open '%s' path", input_path
);
895 input_dir_fd
= dirfd(input_dir
);
896 if (input_dir_fd
< 0) {
901 /* Open output directory */
902 output_dir
= opendir(output_path
);
904 PERROR("Cannot open '%s' path", output_path
);
907 output_dir_fd
= dirfd(output_dir
);
908 if (output_dir_fd
< 0) {
913 while ((entry
= readdir(input_dir
))) {
914 if (!strcmp(entry
->d_name
, ".")
915 || !strcmp(entry
->d_name
, ".."))
917 ret
= extract_file(output_dir_fd
, entry
->d_name
,
918 input_dir_fd
, entry
->d_name
);
919 if (ret
== -ENODATA
) {
920 DBG("No data in file '%s', skipping", entry
->d_name
);
923 } else if (ret
< 0) {
925 } else if (ret
> 0) {
926 DBG("Skipping file '%s'", entry
->d_name
);
931 closeret
= closedir(output_dir
);
935 closeret
= closedir(input_dir
);
943 int extract_one_trace(const char *output_path
,
944 const char *input_path
)
946 char dest
[PATH_MAX
], src
[PATH_MAX
];
949 DBG("Extract crash trace '%s' into '%s'", input_path
, output_path
);
952 strncpy(dest
, output_path
, PATH_MAX
);
953 dest
[PATH_MAX
- 1] = '\0';
954 strncat(dest
, "/metadata", PATH_MAX
- strlen(dest
) - 1);
956 strncpy(src
, input_path
, PATH_MAX
);
957 src
[PATH_MAX
- 1] = '\0';
958 strncat(src
, "/metadata", PATH_MAX
- strlen(dest
) - 1);
960 ret
= copy_file(dest
, src
);
965 /* Extract each other file that has expected header */
966 return extract_all_files(output_path
, input_path
);
970 int extract_trace_recursive(const char *output_path
,
971 const char *input_path
)
974 int dir_fd
, ret
= 0, closeret
;
975 struct dirent
*entry
;
980 dir
= opendir(input_path
);
982 PERROR("Cannot open '%s' path", input_path
);
986 path_len
= strlen(input_path
);
994 while ((entry
= readdir(dir
))) {
997 char filename
[PATH_MAX
];
999 if (!strcmp(entry
->d_name
, ".")
1000 || !strcmp(entry
->d_name
, "..")) {
1004 name_len
= strlen(entry
->d_name
);
1005 if (path_len
+ name_len
+ 2 > sizeof(filename
)) {
1006 ERR("Failed to remove file: path name too long (%s/%s)",
1007 input_path
, entry
->d_name
);
1011 if (snprintf(filename
, sizeof(filename
), "%s/%s",
1012 input_path
, entry
->d_name
) < 0) {
1013 ERR("Failed to format path.");
1017 if (stat(filename
, &st
)) {
1022 if (S_ISDIR(st
.st_mode
)) {
1023 char output_subpath
[PATH_MAX
];
1024 char input_subpath
[PATH_MAX
];
1026 strncpy(output_subpath
, output_path
,
1027 sizeof(output_subpath
));
1028 output_subpath
[sizeof(output_subpath
) - 1] = '\0';
1029 strncat(output_subpath
, "/",
1030 sizeof(output_subpath
) - strlen(output_subpath
) - 1);
1031 strncat(output_subpath
, entry
->d_name
,
1032 sizeof(output_subpath
) - strlen(output_subpath
) - 1);
1034 ret
= mkdir(output_subpath
, S_IRWXU
| S_IRWXG
);
1041 strncpy(input_subpath
, input_path
,
1042 sizeof(input_subpath
));
1043 input_subpath
[sizeof(input_subpath
) - 1] = '\0';
1044 strncat(input_subpath
, "/",
1045 sizeof(input_subpath
) - strlen(input_subpath
) - 1);
1046 strncat(input_subpath
, entry
->d_name
,
1047 sizeof(input_subpath
) - strlen(input_subpath
) - 1);
1049 ret
= extract_trace_recursive(output_subpath
,
1054 } else if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
)) {
1055 if (!strcmp(entry
->d_name
, "metadata")) {
1056 ret
= extract_one_trace(output_path
,
1059 WARN("Error extracting trace '%s', continuing anyway.",
1070 closeret
= closedir(dir
);
1078 int delete_dir_recursive(const char *path
)
1081 int dir_fd
, ret
= 0, closeret
;
1083 struct dirent
*entry
;
1085 /* Open trace directory */
1086 dir
= opendir(path
);
1088 PERROR("Cannot open '%s' path", path
);
1093 path_len
= strlen(path
);
1095 dir_fd
= dirfd(dir
);
1102 while ((entry
= readdir(dir
))) {
1105 char filename
[PATH_MAX
];
1107 if (!strcmp(entry
->d_name
, ".")
1108 || !strcmp(entry
->d_name
, "..")) {
1112 name_len
= strlen(entry
->d_name
);
1113 if (path_len
+ name_len
+ 2 > sizeof(filename
)) {
1114 ERR("Failed to remove file: path name too long (%s/%s)",
1115 path
, entry
->d_name
);
1119 if (snprintf(filename
, sizeof(filename
), "%s/%s",
1120 path
, entry
->d_name
) < 0) {
1121 ERR("Failed to format path.");
1125 if (stat(filename
, &st
)) {
1130 if (S_ISDIR(st
.st_mode
)) {
1131 char *subpath
= zmalloc(PATH_MAX
);
1134 PERROR("zmalloc path");
1138 strncpy(subpath
, path
, PATH_MAX
);
1139 subpath
[PATH_MAX
- 1] = '\0';
1140 strncat(subpath
, "/",
1141 PATH_MAX
- strlen(subpath
) - 1);
1142 strncat(subpath
, entry
->d_name
,
1143 PATH_MAX
- strlen(subpath
) - 1);
1145 ret
= delete_dir_recursive(subpath
);
1148 /* Error occurred, abort traversal. */
1151 } else if (S_ISREG(st
.st_mode
)) {
1152 ret
= unlinkat(dir_fd
, entry
->d_name
, 0);
1154 PERROR("Unlinking '%s'", entry
->d_name
);
1166 PERROR("rmdir '%s'", path
);
1169 closeret
= closedir(dir
);
1177 int view_trace(const char *viewer_path
, const char *trace_path
)
1186 } else if (pid
> 0) {
1190 pid
= waitpid(pid
, &status
, 0);
1191 if (pid
< 0 || !WIFEXITED(status
)) {
1198 ret
= execlp(viewer_path
, viewer_path
,
1199 trace_path
, (char *) NULL
);
1204 exit(EXIT_SUCCESS
); /* Never reached */
1212 int main(int argc
, char *argv
[])
1215 bool has_warning
= false;
1216 const char *output_path
= NULL
;
1217 char tmppath
[] = "/tmp/lttng-crash-XXXXXX";
1219 progname
= argv
[0] ? argv
[0] : "lttng-crash";
1221 ret
= parse_args(argc
, argv
);
1224 } else if (ret
< 0) {
1229 if (opt_output_path
) {
1230 output_path
= opt_output_path
;
1231 ret
= mkdir(output_path
, S_IRWXU
| S_IRWXG
);
1238 output_path
= mkdtemp(tmppath
);
1246 ret
= extract_trace_recursive(output_path
, input_path
);
1250 } else if (ret
> 0) {
1251 /* extract_trace_recursive reported a warning. */
1254 if (!opt_output_path
) {
1256 ret
= view_trace(opt_viewer_path
, output_path
);
1260 /* unlink temporary trace */
1261 ret
= delete_dir_recursive(output_path
);
1267 exit(has_warning
? EXIT_FAILURE
: EXIT_SUCCESS
);