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
,
233 EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " EXTRA_VERSION_NAME
);
239 * List options line by line. This is mostly for bash auto completion and to
240 * avoid difficult parsing.
242 static void list_options(FILE *ofp
)
245 struct option
*option
= NULL
;
247 option
= &long_options
[i
];
248 while (option
->name
!= NULL
) {
249 fprintf(ofp
, "--%s\n", option
->name
);
251 if (isprint(option
->val
)) {
252 fprintf(ofp
, "-%c\n", option
->val
);
256 option
= &long_options
[i
];
261 * Parse command line arguments.
263 * Return 0 if OK, else -1
265 static int parse_args(int argc
, char **argv
)
274 while ((opt
= getopt_long(argc
, argv
, "+Vhve:x:", long_options
, NULL
)) != -1) {
285 /* There is only 3 possible level of verbosity. (-vvv) */
286 if (lttng_opt_verbose
< 3) {
287 lttng_opt_verbose
+= 1;
291 free(opt_viewer_path
);
292 opt_viewer_path
= strdup(optarg
);
295 free(opt_output_path
);
296 opt_output_path
= strdup(optarg
);
298 case OPT_DUMP_OPTIONS
:
299 list_options(stdout
);
303 ERR("Unknown command-line option");
308 if (!opt_viewer_path
) {
309 opt_viewer_path
= DEFAULT_VIEWER
;
312 /* No leftovers, or more than one input path, print usage and quit */
313 if (argc
- optind
!= 1) {
314 ERR("Command-line error: Specify exactly one input path");
318 input_path
= argv
[optind
];
327 int copy_file(const char *file_dest
, const char *file_src
)
329 int fd_src
= -1, fd_dest
= -1;
330 ssize_t readlen
, writelen
;
331 char buf
[COPY_BUFLEN
];
334 DBG("Copy metadata file '%s' into '%s'", file_src
, file_dest
);
336 fd_src
= open(file_src
, O_RDONLY
);
338 PERROR("Error opening %s for reading", file_src
);
342 fd_dest
= open(file_dest
, O_RDWR
| O_CREAT
| O_EXCL
,
343 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
345 PERROR("Error opening %s for writing", file_dest
);
351 readlen
= lttng_read(fd_src
, buf
, COPY_BUFLEN
);
353 PERROR("Error reading input file");
360 writelen
= lttng_write(fd_dest
, buf
, readlen
);
361 if (writelen
< readlen
) {
362 PERROR("Error writing to output file");
371 if (close(fd_src
) < 0) {
372 PERROR("Error closing %s", file_src
);
377 if (close(fd_dest
) < 0) {
378 PERROR("Error closing %s", file_dest
);
385 uint64_t _crash_get_field(const struct lttng_crash_layout
*layout
,
386 const char *ptr
, size_t size
)
389 case 1: return *(uint8_t *) ptr
;
390 case 2: if (layout
->reverse_byte_order
) {
391 return __bswap_16(*(uint16_t *) ptr
);
393 return *(uint16_t *) ptr
;
396 case 4: if (layout
->reverse_byte_order
) {
397 return __bswap_32(*(uint32_t *) ptr
);
399 return *(uint32_t *) ptr
;
402 case 8: if (layout
->reverse_byte_order
) {
403 return __bswap_64(*(uint64_t *) ptr
);
405 return *(uint64_t *) ptr
;
414 #define crash_get_field(layout, map, name) \
415 _crash_get_field(layout, (map) + (layout)->offset.name, \
418 #define crash_get_array_field(layout, map, array_name, idx, field_name) \
419 _crash_get_field(layout, \
420 (map) + (layout)->offset.array_name \
421 + (idx * (layout)->stride.array_name) \
422 + (layout)->offset.field_name, \
423 (layout)->length.field_name)
425 #define crash_get_hdr_raw_field(layout, hdr, name) ((hdr)->name)
427 #define crash_get_hdr_field(layout, hdr, name) \
428 _crash_get_field(layout, (const char *) &(hdr)->name, \
431 #define crash_get_layout(layout, hdr, name) \
433 (layout)->name = crash_get_hdr_field(layout, hdr, \
435 DBG("layout.%s = %" PRIu64, #name, \
436 (uint64_t) (layout)->name); \
440 int get_crash_layout_0_0(struct lttng_crash_layout
*layout
,
443 const struct crash_abi_0_0
*abi
= (const struct crash_abi_0_0
*) map
;
445 crash_get_layout(layout
, abi
, offset
.prod_offset
);
446 crash_get_layout(layout
, abi
, offset
.consumed_offset
);
447 crash_get_layout(layout
, abi
, offset
.commit_hot_array
);
448 crash_get_layout(layout
, abi
, offset
.commit_hot_seq
);
449 crash_get_layout(layout
, abi
, offset
.buf_wsb_array
);
450 crash_get_layout(layout
, abi
, offset
.buf_wsb_id
);
451 crash_get_layout(layout
, abi
, offset
.sb_array
);
452 crash_get_layout(layout
, abi
, offset
.sb_array_shmp_offset
);
453 crash_get_layout(layout
, abi
, offset
.sb_backend_p_offset
);
454 crash_get_layout(layout
, abi
, offset
.content_size
);
455 crash_get_layout(layout
, abi
, offset
.packet_size
);
457 crash_get_layout(layout
, abi
, length
.prod_offset
);
458 crash_get_layout(layout
, abi
, length
.consumed_offset
);
459 crash_get_layout(layout
, abi
, length
.commit_hot_seq
);
460 crash_get_layout(layout
, abi
, length
.buf_wsb_id
);
461 crash_get_layout(layout
, abi
, length
.sb_array_shmp_offset
);
462 crash_get_layout(layout
, abi
, length
.sb_backend_p_offset
);
463 crash_get_layout(layout
, abi
, length
.content_size
);
464 crash_get_layout(layout
, abi
, length
.packet_size
);
466 crash_get_layout(layout
, abi
, stride
.commit_hot_array
);
467 crash_get_layout(layout
, abi
, stride
.buf_wsb_array
);
468 crash_get_layout(layout
, abi
, stride
.sb_array
);
470 crash_get_layout(layout
, abi
, buf_size
);
471 crash_get_layout(layout
, abi
, subbuf_size
);
472 crash_get_layout(layout
, abi
, num_subbuf
);
473 crash_get_layout(layout
, abi
, mode
);
479 void print_dbg_magic(const uint8_t *magic
)
481 DBG("magic: 0x%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X",
482 magic
[0], magic
[1], magic
[2], magic
[3],
483 magic
[4], magic
[5], magic
[6], magic
[7],
484 magic
[8], magic
[9], magic
[10], magic
[11],
485 magic
[12], magic
[13], magic
[14], magic
[15]);
489 int check_magic(const uint8_t *magic
)
493 for (i
= 0; i
< RB_CRASH_DUMP_ABI_MAGIC_LEN
; i
++) {
494 if ((magic
[i
] ^ 0xFF) != lttng_crash_expected_magic_xor
[i
]) {
502 int get_crash_layout(struct lttng_crash_layout
*layout
, int fd
,
503 const char *input_file
)
506 int ret
= 0, unmapret
;
507 const uint8_t *magic
;
508 uint64_t mmap_length
;
509 uint16_t major
, minor
;
511 const struct crash_abi_unknown
*abi
;
513 enum lttng_crash_type layout_type
;
516 ret
= fstat(fd
, &stat
);
518 PERROR("Failed to fstat '%s'", input_file
);
521 if (stat
.st_size
< RB_CRASH_DUMP_ABI_LEN
) {
522 ERR("File '%s' truncated: file length of %" PRIi64
523 " bytes does not meet the minimal expected "
524 "length of %d bytes",
525 input_file
, (int64_t) stat
.st_size
,
526 RB_CRASH_DUMP_ABI_LEN
);
529 map
= mmap(NULL
, RB_CRASH_DUMP_ABI_LEN
, PROT_READ
, MAP_PRIVATE
,
531 if (map
== MAP_FAILED
) {
532 PERROR("Mapping file");
535 abi
= (const struct crash_abi_unknown
*) map
;
536 magic
= crash_get_hdr_raw_field(layout
, abi
, magic
);
537 print_dbg_magic(magic
);
538 if (check_magic(magic
)) {
539 DBG("Unknown magic number");
540 ret
= 1; /* positive return value, skip */
543 endian
= crash_get_hdr_field(layout
, abi
, endian
);
545 case RB_CRASH_ENDIAN
:
547 case RB_CRASH_ENDIAN_REVERSE
:
548 layout
->reverse_byte_order
= 1;
551 DBG("Unknown endianness value: 0x%X", (unsigned int) endian
);
552 ret
= 1; /* positive return value, skip */
555 layout_type
= (enum lttng_crash_type
) crash_get_hdr_field(layout
, abi
, layout_type
);
556 switch (layout_type
) {
557 case LTTNG_CRASH_TYPE_UST
:
559 case LTTNG_CRASH_TYPE_KERNEL
:
560 ERR("lttng-modules buffer layout support not implemented");
561 ret
= 1; /* positive return value, skip */
564 ERR("Unknown layout type %u", (unsigned int) layout_type
);
565 ret
= 1; /* positive return value, skip */
568 mmap_length
= crash_get_hdr_field(layout
, abi
, mmap_length
);
569 DBG("mmap_length: %" PRIu64
, mmap_length
);
570 layout
->mmap_length
= mmap_length
;
571 major
= crash_get_hdr_field(layout
, abi
, major
);
572 DBG("major: %u", major
);
573 minor
= crash_get_hdr_field(layout
, abi
, minor
);
574 DBG("minor: %u", minor
);
575 word_size
= crash_get_hdr_field(layout
, abi
, word_size
);
576 DBG("word_size: %u", word_size
);
581 ret
= get_crash_layout_0_0(layout
, map
);
587 ERR("Unsupported crash ABI %u.%u\n", major
, minor
);
592 ERR("Unsupported crash ABI %u.%u\n", major
, minor
);
596 layout
->word_size
= word_size
;
598 unmapret
= munmap(map
, RB_CRASH_DUMP_ABI_LEN
);
605 /* buf_trunc mask selects only the buffer number. */
607 uint64_t buf_trunc(uint64_t offset
, uint64_t buf_size
)
609 return offset
& ~(buf_size
- 1);
612 /* subbuf_trunc mask selects the subbuffer number. */
614 uint64_t subbuf_trunc(uint64_t offset
, uint64_t subbuf_size
)
616 return offset
& ~(subbuf_size
- 1);
619 /* buf_offset mask selects only the offset within the current buffer. */
621 uint64_t buf_offset(uint64_t offset
, uint64_t buf_size
)
623 return offset
& (buf_size
- 1);
626 /* subbuf_offset mask selects the offset within the current subbuffer. */
628 uint64_t subbuf_offset(uint64_t offset
, uint64_t subbuf_size
)
630 return offset
& (subbuf_size
- 1);
633 /* subbuf_index returns the index of the current subbuffer within the buffer. */
635 uint64_t subbuf_index(uint64_t offset
, uint64_t buf_size
, uint64_t subbuf_size
)
637 return buf_offset(offset
, buf_size
) / subbuf_size
;
641 uint64_t subbuffer_id_get_index(uint32_t mode
, uint64_t id
,
644 if (mode
== RING_BUFFER_OVERWRITE
)
645 return id
& SB_ID_INDEX_MASK(wl
);
651 int copy_crash_subbuf(const struct lttng_crash_layout
*layout
,
652 int fd_dest
, char *buf
, uint64_t offset
)
654 uint64_t buf_size
, subbuf_size
, num_subbuf
, sbidx
, id
,
655 sb_bindex
, rpages_offset
, p_offset
, seq_cc
,
656 committed
, commit_count_mask
, consumed_cur
,
662 * Get the current subbuffer by applying the proper mask to
663 * "offset", and looking up the subbuf location within the
667 buf_size
= layout
->buf_size
;
668 subbuf_size
= layout
->subbuf_size
;
669 num_subbuf
= layout
->num_subbuf
;
671 switch (layout
->word_size
) {
672 case 4: commit_count_mask
= 0xFFFFFFFFULL
/ num_subbuf
;
674 case 8: commit_count_mask
= 0xFFFFFFFFFFFFFFFFULL
/ num_subbuf
;
677 ERR("Unsupported word size: %u",
678 (unsigned int) layout
->word_size
);
682 DBG("Copy crash subbuffer at offset %" PRIu64
, offset
);
683 sbidx
= subbuf_index(offset
, buf_size
, subbuf_size
);
686 * Find where the seq cc is located. Compute length of data to
689 seq_cc
= crash_get_array_field(layout
, buf
, commit_hot_array
,
690 sbidx
, commit_hot_seq
);
691 consumed_cur
= crash_get_field(layout
, buf
, consumed_offset
);
694 * Check that the buffer we are getting is after or at
695 * consumed_cur position.
697 if ((long) subbuf_trunc(offset
, subbuf_size
)
698 - (long) subbuf_trunc(consumed_cur
, subbuf_size
) < 0) {
699 DBG("No data: position is before consumed_cur");
704 * Check if subbuffer has been fully committed.
706 if (((seq_cc
- subbuf_size
) & commit_count_mask
)
707 - (buf_trunc(offset
, buf_size
) / num_subbuf
)
709 committed
= subbuf_size
;
711 committed
= subbuf_offset(seq_cc
, subbuf_size
);
713 DBG("No data committed, seq_cc: %" PRIu64
, seq_cc
);
718 /* Find actual physical offset in subbuffer table */
719 id
= crash_get_array_field(layout
, buf
, buf_wsb_array
,
721 sb_bindex
= subbuffer_id_get_index(layout
->mode
, id
,
723 rpages_offset
= crash_get_array_field(layout
, buf
, sb_array
,
724 sb_bindex
, sb_array_shmp_offset
);
725 p_offset
= crash_get_field(layout
, buf
+ rpages_offset
,
726 sb_backend_p_offset
);
727 subbuf_ptr
= buf
+ p_offset
;
729 if (committed
== subbuf_size
) {
731 * Packet header can be used.
733 if (layout
->length
.packet_size
) {
735 subbuf_ptr
+ layout
->offset
.packet_size
,
736 layout
->length
.packet_size
);
737 if (layout
->reverse_byte_order
) {
738 packet_size
= __bswap_64(packet_size
);
740 packet_size
/= CHAR_BIT
;
742 packet_size
= subbuf_size
;
748 * Find where to patch the sub-buffer header with actual
749 * readable data len and packet len, derived from seq
750 * cc. Patch it in our in-memory copy.
752 patch_size
= committed
* CHAR_BIT
;
753 if (layout
->reverse_byte_order
) {
754 patch_size
= __bswap_64(patch_size
);
756 if (layout
->length
.content_size
) {
757 memcpy(subbuf_ptr
+ layout
->offset
.content_size
,
758 &patch_size
, layout
->length
.content_size
);
760 if (layout
->length
.packet_size
) {
761 memcpy(subbuf_ptr
+ layout
->offset
.packet_size
,
762 &patch_size
, layout
->length
.packet_size
);
764 packet_size
= committed
;
768 * Copy packet into fd_dest.
770 writelen
= lttng_write(fd_dest
, subbuf_ptr
, packet_size
);
771 if (writelen
< packet_size
) {
772 PERROR("Error writing to output file");
775 DBG("Copied %" PRIu64
" bytes of data", packet_size
);
783 int copy_crash_data(const struct lttng_crash_layout
*layout
, int fd_dest
,
787 int ret
= 0, has_data
= 0;
790 uint64_t prod_offset
, consumed_offset
;
791 uint64_t offset
, subbuf_size
;
794 ret
= fstat(fd_src
, &statbuf
);
798 src_file_len
= layout
->mmap_length
;
799 buf
= zmalloc(src_file_len
);
803 readlen
= lttng_read(fd_src
, buf
, src_file_len
);
805 PERROR("Error reading input file");
810 prod_offset
= crash_get_field(layout
, buf
, prod_offset
);
811 DBG("prod_offset: 0x%" PRIx64
, prod_offset
);
812 consumed_offset
= crash_get_field(layout
, buf
, consumed_offset
);
813 DBG("consumed_offset: 0x%" PRIx64
, consumed_offset
);
814 subbuf_size
= layout
->subbuf_size
;
816 for (offset
= consumed_offset
; offset
< prod_offset
;
817 offset
+= subbuf_size
) {
818 ret
= copy_crash_subbuf(layout
, fd_dest
, buf
, offset
);
828 if (ret
&& ret
!= -ENODATA
) {
839 int extract_file(int output_dir_fd
, const char *output_file
,
840 int input_dir_fd
, const char *input_file
)
842 int fd_dest
, fd_src
, ret
= 0, closeret
;
843 struct lttng_crash_layout layout
;
845 layout
.reverse_byte_order
= 0; /* For reading magic number */
847 DBG("Extract file '%s'", input_file
);
848 fd_src
= openat(input_dir_fd
, input_file
, O_RDONLY
);
850 PERROR("Error opening '%s' for reading",
856 /* Query the crash ABI layout */
857 ret
= get_crash_layout(&layout
, fd_src
, input_file
);
862 fd_dest
= openat(output_dir_fd
, output_file
,
863 O_RDWR
| O_CREAT
| O_EXCL
,
864 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
866 PERROR("Error opening '%s' for writing",
872 ret
= copy_crash_data(&layout
, fd_dest
, fd_src
);
878 closeret
= close(fd_dest
);
882 if (ret
== -ENODATA
) {
883 closeret
= unlinkat(output_dir_fd
, output_file
, 0);
889 closeret
= close(fd_src
);
898 int extract_all_files(const char *output_path
,
899 const char *input_path
)
901 DIR *input_dir
, *output_dir
;
902 int input_dir_fd
, output_dir_fd
, ret
= 0, closeret
;
903 struct dirent
*entry
; /* input */
905 /* Open input directory */
906 input_dir
= opendir(input_path
);
908 PERROR("Cannot open '%s' path", input_path
);
911 input_dir_fd
= dirfd(input_dir
);
912 if (input_dir_fd
< 0) {
917 /* Open output directory */
918 output_dir
= opendir(output_path
);
920 PERROR("Cannot open '%s' path", output_path
);
923 output_dir_fd
= dirfd(output_dir
);
924 if (output_dir_fd
< 0) {
929 while ((entry
= readdir(input_dir
))) {
930 if (!strcmp(entry
->d_name
, ".")
931 || !strcmp(entry
->d_name
, ".."))
933 ret
= extract_file(output_dir_fd
, entry
->d_name
,
934 input_dir_fd
, entry
->d_name
);
935 if (ret
== -ENODATA
) {
936 DBG("No data in file '%s', skipping", entry
->d_name
);
939 } else if (ret
< 0) {
941 } else if (ret
> 0) {
942 DBG("Skipping file '%s'", entry
->d_name
);
947 closeret
= closedir(output_dir
);
951 closeret
= closedir(input_dir
);
959 int extract_one_trace(const char *output_path
,
960 const char *input_path
)
962 char dest
[PATH_MAX
], src
[PATH_MAX
];
965 DBG("Extract crash trace '%s' into '%s'", input_path
, output_path
);
968 strncpy(dest
, output_path
, PATH_MAX
);
969 dest
[PATH_MAX
- 1] = '\0';
970 strncat(dest
, "/metadata", PATH_MAX
- strlen(dest
) - 1);
972 strncpy(src
, input_path
, PATH_MAX
);
973 src
[PATH_MAX
- 1] = '\0';
974 strncat(src
, "/metadata", PATH_MAX
- strlen(dest
) - 1);
976 ret
= copy_file(dest
, src
);
981 /* Extract each other file that has expected header */
982 return extract_all_files(output_path
, input_path
);
986 int extract_trace_recursive(const char *output_path
,
987 const char *input_path
)
990 int dir_fd
, ret
= 0, closeret
;
991 struct dirent
*entry
;
996 dir
= opendir(input_path
);
998 PERROR("Cannot open '%s' path", input_path
);
1002 path_len
= strlen(input_path
);
1004 dir_fd
= dirfd(dir
);
1010 while ((entry
= readdir(dir
))) {
1013 char filename
[PATH_MAX
];
1015 if (!strcmp(entry
->d_name
, ".")
1016 || !strcmp(entry
->d_name
, "..")) {
1020 name_len
= strlen(entry
->d_name
);
1021 if (path_len
+ name_len
+ 2 > sizeof(filename
)) {
1022 ERR("Failed to remove file: path name too long (%s/%s)",
1023 input_path
, entry
->d_name
);
1027 if (snprintf(filename
, sizeof(filename
), "%s/%s",
1028 input_path
, entry
->d_name
) < 0) {
1029 ERR("Failed to format path.");
1033 if (stat(filename
, &st
)) {
1038 if (S_ISDIR(st
.st_mode
)) {
1039 char output_subpath
[PATH_MAX
];
1040 char input_subpath
[PATH_MAX
];
1042 strncpy(output_subpath
, output_path
,
1043 sizeof(output_subpath
));
1044 output_subpath
[sizeof(output_subpath
) - 1] = '\0';
1045 strncat(output_subpath
, "/",
1046 sizeof(output_subpath
) - strlen(output_subpath
) - 1);
1047 strncat(output_subpath
, entry
->d_name
,
1048 sizeof(output_subpath
) - strlen(output_subpath
) - 1);
1050 ret
= mkdir(output_subpath
, S_IRWXU
| S_IRWXG
);
1057 strncpy(input_subpath
, input_path
,
1058 sizeof(input_subpath
));
1059 input_subpath
[sizeof(input_subpath
) - 1] = '\0';
1060 strncat(input_subpath
, "/",
1061 sizeof(input_subpath
) - strlen(input_subpath
) - 1);
1062 strncat(input_subpath
, entry
->d_name
,
1063 sizeof(input_subpath
) - strlen(input_subpath
) - 1);
1065 ret
= extract_trace_recursive(output_subpath
,
1070 } else if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
)) {
1071 if (!strcmp(entry
->d_name
, "metadata")) {
1072 ret
= extract_one_trace(output_path
,
1075 WARN("Error extracting trace '%s', continuing anyway.",
1086 closeret
= closedir(dir
);
1094 int delete_dir_recursive(const char *path
)
1097 int dir_fd
, ret
= 0, closeret
;
1099 struct dirent
*entry
;
1101 /* Open trace directory */
1102 dir
= opendir(path
);
1104 PERROR("Cannot open '%s' path", path
);
1106 goto end_no_closedir
;
1109 path_len
= strlen(path
);
1111 dir_fd
= dirfd(dir
);
1118 while ((entry
= readdir(dir
))) {
1121 char filename
[PATH_MAX
];
1123 if (!strcmp(entry
->d_name
, ".")
1124 || !strcmp(entry
->d_name
, "..")) {
1128 name_len
= strlen(entry
->d_name
);
1129 if (path_len
+ name_len
+ 2 > sizeof(filename
)) {
1130 ERR("Failed to remove file: path name too long (%s/%s)",
1131 path
, entry
->d_name
);
1135 if (snprintf(filename
, sizeof(filename
), "%s/%s",
1136 path
, entry
->d_name
) < 0) {
1137 ERR("Failed to format path.");
1141 if (stat(filename
, &st
)) {
1146 if (S_ISDIR(st
.st_mode
)) {
1147 char *subpath
= zmalloc(PATH_MAX
);
1150 PERROR("zmalloc path");
1154 strncpy(subpath
, path
, PATH_MAX
);
1155 subpath
[PATH_MAX
- 1] = '\0';
1156 strncat(subpath
, "/",
1157 PATH_MAX
- strlen(subpath
) - 1);
1158 strncat(subpath
, entry
->d_name
,
1159 PATH_MAX
- strlen(subpath
) - 1);
1161 ret
= delete_dir_recursive(subpath
);
1164 /* Error occurred, abort traversal. */
1167 } else if (S_ISREG(st
.st_mode
)) {
1168 ret
= unlinkat(dir_fd
, entry
->d_name
, 0);
1170 PERROR("Unlinking '%s'", entry
->d_name
);
1182 PERROR("rmdir '%s'", path
);
1185 closeret
= closedir(dir
);
1194 int view_trace(const char *viewer_path
, const char *trace_path
)
1203 } else if (pid
> 0) {
1207 pid
= waitpid(pid
, &status
, 0);
1208 if (pid
< 0 || !WIFEXITED(status
)) {
1215 ret
= execlp(viewer_path
, viewer_path
,
1216 trace_path
, (char *) NULL
);
1221 exit(EXIT_SUCCESS
); /* Never reached */
1229 int main(int argc
, char *argv
[])
1232 bool has_warning
= false;
1233 const char *output_path
= NULL
;
1234 char tmppath
[] = "/tmp/lttng-crash-XXXXXX";
1236 progname
= argv
[0] ? argv
[0] : "lttng-crash";
1238 ret
= parse_args(argc
, argv
);
1241 } else if (ret
< 0) {
1246 if (opt_output_path
) {
1247 output_path
= opt_output_path
;
1248 ret
= mkdir(output_path
, S_IRWXU
| S_IRWXG
);
1255 output_path
= mkdtemp(tmppath
);
1263 ret
= extract_trace_recursive(output_path
, input_path
);
1267 } else if (ret
> 0) {
1268 /* extract_trace_recursive reported a warning. */
1271 if (!opt_output_path
) {
1273 ret
= view_trace(opt_viewer_path
, output_path
);
1277 /* unlink temporary trace */
1278 ret
= delete_dir_recursive(output_path
);
1284 exit(has_warning
? EXIT_FAILURE
: EXIT_SUCCESS
);