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, \
65 * Non-static to ensure the compiler does not optimize away the xor.
67 uint8_t lttng_crash_expected_magic_xor
[] = RB_CRASH_DUMP_ABI_MAGIC_XOR
;
69 #define RB_CRASH_ENDIAN 0x1234
70 #define RB_CRASH_ENDIAN_REVERSE 0x3412
72 enum lttng_crash_type
{
73 LTTNG_CRASH_TYPE_UST
= 0,
74 LTTNG_CRASH_TYPE_KERNEL
= 1,
77 /* LTTng ring buffer defines (copied) */
79 #define HALF_ULONG_BITS(wl) (((wl) * CHAR_BIT) >> 1)
81 #define SB_ID_OFFSET_SHIFT(wl) (HALF_ULONG_BITS(wl) + 1)
82 #define SB_ID_OFFSET_COUNT(wl) (1UL << SB_ID_OFFSET_SHIFT(wl))
83 #define SB_ID_OFFSET_MASK(wl) (~(SB_ID_OFFSET_COUNT(wl) - 1))
85 * Lowest bit of top word half belongs to noref. Used only for overwrite mode.
87 #define SB_ID_NOREF_SHIFT(wl) (SB_ID_OFFSET_SHIFT(wl) - 1)
88 #define SB_ID_NOREF_COUNT(wl) (1UL << SB_ID_NOREF_SHIFT(wl))
89 #define SB_ID_NOREF_MASK(wl) SB_ID_NOREF_COUNT(wl)
91 * In overwrite mode: lowest half of word is used for index.
92 * Limit of 2^16 subbuffers per buffer on 32-bit, 2^32 on 64-bit.
93 * In producer-consumer mode: whole word used for index.
95 #define SB_ID_INDEX_SHIFT(wl) 0
96 #define SB_ID_INDEX_COUNT(wl) (1UL << SB_ID_INDEX_SHIFT(wl))
97 #define SB_ID_INDEX_MASK(wl) (SB_ID_NOREF_COUNT(wl) - 1)
100 RING_BUFFER_OVERWRITE
= 0, /* Overwrite when buffer full */
101 RING_BUFFER_DISCARD
= 1, /* Discard when buffer full */
104 struct crash_abi_unknown
{
105 uint8_t magic
[RB_CRASH_DUMP_ABI_MAGIC_LEN
];
106 uint64_t mmap_length
; /* Overall length of crash record */
108 * { 0x12, 0x34 }: big endian
109 * { 0x34, 0x12 }: little endian
111 uint16_t major
; /* Major number. */
112 uint16_t minor
; /* Minor number. */
113 uint8_t word_size
; /* Word size (bytes). */
114 uint8_t layout_type
; /* enum lttng_crash_layout */
115 } __attribute__((packed
));
117 struct crash_abi_0_0
{
118 struct crash_abi_unknown parent
;
121 uint32_t prod_offset
;
122 uint32_t consumed_offset
;
123 uint32_t commit_hot_array
;
124 uint32_t commit_hot_seq
;
125 uint32_t buf_wsb_array
;
128 uint32_t sb_array_shmp_offset
;
129 uint32_t sb_backend_p_offset
;
130 uint32_t content_size
;
131 uint32_t packet_size
;
132 } __attribute__((packed
)) offset
;
135 uint8_t consumed_offset
;
136 uint8_t commit_hot_seq
;
138 uint8_t sb_array_shmp_offset
;
139 uint8_t sb_backend_p_offset
;
140 uint8_t content_size
;
142 } __attribute__((packed
)) length
;
144 uint32_t commit_hot_array
;
145 uint32_t buf_wsb_array
;
147 } __attribute__((packed
)) stride
;
149 uint64_t buf_size
; /* Size of the buffer */
150 uint64_t subbuf_size
; /* Sub-buffer size */
151 uint64_t num_subbuf
; /* Number of sub-buffers for writer */
152 uint32_t mode
; /* Buffer mode: 0: overwrite, 1: discard */
153 } __attribute__((packed
));
155 struct lttng_crash_layout
{
157 int prod_offset
, consumed_offset
,
158 commit_hot_array
, commit_hot_seq
,
159 buf_wsb_array
, buf_wsb_id
,
160 sb_array
, sb_array_shmp_offset
,
161 sb_backend_p_offset
, content_size
,
165 int prod_offset
, consumed_offset
,
166 commit_hot_seq
, buf_wsb_id
,
167 sb_array_shmp_offset
, sb_backend_p_offset
,
168 content_size
, packet_size
;
171 int commit_hot_array
, buf_wsb_array
, sb_array
;
174 int reverse_byte_order
;
177 uint64_t mmap_length
; /* Length of crash record */
178 uint64_t buf_size
; /* Size of the buffer */
179 uint64_t subbuf_size
; /* Sub-buffer size */
180 uint64_t num_subbuf
; /* Number of sub-buffers for writer */
181 uint32_t mode
; /* Buffer mode: 0: overwrite, 1: discard */
185 static char *progname
,
186 *opt_viewer_path
= NULL
,
187 *opt_output_path
= NULL
;
189 static char *input_path
;
191 int lttng_opt_quiet
, lttng_opt_verbose
, lttng_opt_mi
;
197 /* Getopt options. No first level command. */
198 static struct option long_options
[] = {
199 { "version", 0, NULL
, 'V' },
200 { "help", 0, NULL
, 'h' },
201 { "verbose", 0, NULL
, 'v' },
202 { "viewer", 1, NULL
, 'e' },
203 { "extract", 1, NULL
, 'x' },
204 { "list-options", 0, NULL
, OPT_DUMP_OPTIONS
},
205 { NULL
, 0, NULL
, 0 },
208 static void usage(void)
210 int ret
= utils_show_man_page(1, "lttng-crash");
213 ERR("Cannot view man page lttng-crash(1)");
219 static void version(FILE *ofp
)
221 fprintf(ofp
, "%s (LTTng Crash Trace Viewer) " VERSION
" - " VERSION_NAME
224 GIT_VERSION
[0] == '\0' ? "" : " - " GIT_VERSION
);
230 * List options line by line. This is mostly for bash auto completion and to
231 * avoid difficult parsing.
233 static void list_options(FILE *ofp
)
236 struct option
*option
= NULL
;
238 option
= &long_options
[i
];
239 while (option
->name
!= NULL
) {
240 fprintf(ofp
, "--%s\n", option
->name
);
242 if (isprint(option
->val
)) {
243 fprintf(ofp
, "-%c\n", option
->val
);
247 option
= &long_options
[i
];
252 * Parse command line arguments.
254 * Return 0 if OK, else -1
256 static int parse_args(int argc
, char **argv
)
265 while ((opt
= getopt_long(argc
, argv
, "+Vhve:x:", long_options
, NULL
)) != -1) {
276 /* There is only 3 possible level of verbosity. (-vvv) */
277 if (lttng_opt_verbose
< 3) {
278 lttng_opt_verbose
+= 1;
282 free(opt_viewer_path
);
283 opt_viewer_path
= strdup(optarg
);
286 free(opt_output_path
);
287 opt_output_path
= strdup(optarg
);
289 case OPT_DUMP_OPTIONS
:
290 list_options(stdout
);
294 ERR("Unknown command-line option");
299 if (!opt_viewer_path
) {
300 opt_viewer_path
= DEFAULT_VIEWER
;
303 /* No leftovers, or more than one input path, print usage and quit */
304 if (argc
- optind
!= 1) {
305 ERR("Command-line error: Specify exactly one input path");
309 input_path
= argv
[optind
];
318 int copy_file(const char *file_dest
, const char *file_src
)
320 int fd_src
= -1, fd_dest
= -1;
321 ssize_t readlen
, writelen
;
322 char buf
[COPY_BUFLEN
];
325 DBG("Copy metadata file '%s' into '%s'", file_src
, file_dest
);
327 fd_src
= open(file_src
, O_RDONLY
);
329 PERROR("Error opening %s for reading", file_src
);
333 fd_dest
= open(file_dest
, O_RDWR
| O_CREAT
| O_EXCL
,
334 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
336 PERROR("Error opening %s for writing", file_dest
);
342 readlen
= lttng_read(fd_src
, buf
, COPY_BUFLEN
);
344 PERROR("Error reading input file");
351 writelen
= lttng_write(fd_dest
, buf
, readlen
);
352 if (writelen
< readlen
) {
353 PERROR("Error writing to output file");
362 if (close(fd_src
) < 0) {
363 PERROR("Error closing %s", file_src
);
368 if (close(fd_dest
) < 0) {
369 PERROR("Error closing %s", file_dest
);
376 uint64_t _crash_get_field(const struct lttng_crash_layout
*layout
,
377 const char *ptr
, size_t size
)
380 case 1: return *(uint8_t *) ptr
;
381 case 2: if (layout
->reverse_byte_order
) {
382 return __bswap_16(*(uint16_t *) ptr
);
384 return *(uint16_t *) ptr
;
387 case 4: if (layout
->reverse_byte_order
) {
388 return __bswap_32(*(uint32_t *) ptr
);
390 return *(uint32_t *) ptr
;
393 case 8: if (layout
->reverse_byte_order
) {
394 return __bswap_64(*(uint64_t *) ptr
);
396 return *(uint64_t *) ptr
;
405 #define crash_get_field(layout, map, name) \
406 _crash_get_field(layout, (map) + (layout)->offset.name, \
409 #define crash_get_array_field(layout, map, array_name, idx, field_name) \
410 _crash_get_field(layout, \
411 (map) + (layout)->offset.array_name \
412 + (idx * (layout)->stride.array_name) \
413 + (layout)->offset.field_name, \
414 (layout)->length.field_name)
416 #define crash_get_hdr_raw_field(layout, hdr, name) ((hdr)->name)
418 #define crash_get_hdr_field(layout, hdr, name) \
419 _crash_get_field(layout, (const char *) &(hdr)->name, \
422 #define crash_get_layout(layout, hdr, name) \
424 (layout)->name = crash_get_hdr_field(layout, hdr, \
426 DBG("layout.%s = %" PRIu64, #name, \
427 (uint64_t) (layout)->name); \
431 int get_crash_layout_0_0(struct lttng_crash_layout
*layout
,
434 const struct crash_abi_0_0
*abi
= (const struct crash_abi_0_0
*) map
;
436 crash_get_layout(layout
, abi
, offset
.prod_offset
);
437 crash_get_layout(layout
, abi
, offset
.consumed_offset
);
438 crash_get_layout(layout
, abi
, offset
.commit_hot_array
);
439 crash_get_layout(layout
, abi
, offset
.commit_hot_seq
);
440 crash_get_layout(layout
, abi
, offset
.buf_wsb_array
);
441 crash_get_layout(layout
, abi
, offset
.buf_wsb_id
);
442 crash_get_layout(layout
, abi
, offset
.sb_array
);
443 crash_get_layout(layout
, abi
, offset
.sb_array_shmp_offset
);
444 crash_get_layout(layout
, abi
, offset
.sb_backend_p_offset
);
445 crash_get_layout(layout
, abi
, offset
.content_size
);
446 crash_get_layout(layout
, abi
, offset
.packet_size
);
448 crash_get_layout(layout
, abi
, length
.prod_offset
);
449 crash_get_layout(layout
, abi
, length
.consumed_offset
);
450 crash_get_layout(layout
, abi
, length
.commit_hot_seq
);
451 crash_get_layout(layout
, abi
, length
.buf_wsb_id
);
452 crash_get_layout(layout
, abi
, length
.sb_array_shmp_offset
);
453 crash_get_layout(layout
, abi
, length
.sb_backend_p_offset
);
454 crash_get_layout(layout
, abi
, length
.content_size
);
455 crash_get_layout(layout
, abi
, length
.packet_size
);
457 crash_get_layout(layout
, abi
, stride
.commit_hot_array
);
458 crash_get_layout(layout
, abi
, stride
.buf_wsb_array
);
459 crash_get_layout(layout
, abi
, stride
.sb_array
);
461 crash_get_layout(layout
, abi
, buf_size
);
462 crash_get_layout(layout
, abi
, subbuf_size
);
463 crash_get_layout(layout
, abi
, num_subbuf
);
464 crash_get_layout(layout
, abi
, mode
);
470 void print_dbg_magic(const uint8_t *magic
)
472 DBG("magic: 0x%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X",
473 magic
[0], magic
[1], magic
[2], magic
[3],
474 magic
[4], magic
[5], magic
[6], magic
[7],
475 magic
[8], magic
[9], magic
[10], magic
[11],
476 magic
[12], magic
[13], magic
[14], magic
[15]);
480 int check_magic(const uint8_t *magic
)
484 for (i
= 0; i
< RB_CRASH_DUMP_ABI_MAGIC_LEN
; i
++) {
485 if ((magic
[i
] ^ 0xFF) != lttng_crash_expected_magic_xor
[i
]) {
493 int get_crash_layout(struct lttng_crash_layout
*layout
, int fd
)
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
;
505 map
= mmap(NULL
, RB_CRASH_DUMP_ABI_LEN
, PROT_READ
, MAP_PRIVATE
,
507 if (map
== MAP_FAILED
) {
508 PERROR("Mapping file");
511 abi
= (const struct crash_abi_unknown
*) map
;
512 magic
= crash_get_hdr_raw_field(layout
, abi
, magic
);
513 print_dbg_magic(magic
);
514 if (check_magic(magic
)) {
515 DBG("Unknown magic number");
516 ret
= 1; /* positive return value, skip */
519 endian
= crash_get_hdr_field(layout
, abi
, endian
);
521 case RB_CRASH_ENDIAN
:
523 case RB_CRASH_ENDIAN_REVERSE
:
524 layout
->reverse_byte_order
= 1;
527 DBG("Unknown endianness value: 0x%X", (unsigned int) endian
);
528 ret
= 1; /* positive return value, skip */
531 layout_type
= (enum lttng_crash_type
) crash_get_hdr_field(layout
, abi
, layout_type
);
532 switch (layout_type
) {
533 case LTTNG_CRASH_TYPE_UST
:
535 case LTTNG_CRASH_TYPE_KERNEL
:
536 ERR("lttng-modules buffer layout support not implemented");
537 ret
= 1; /* positive return value, skip */
540 ERR("Unknown layout type %u", (unsigned int) layout_type
);
541 ret
= 1; /* positive return value, skip */
544 mmap_length
= crash_get_hdr_field(layout
, abi
, mmap_length
);
545 DBG("mmap_length: %" PRIu64
, mmap_length
);
546 layout
->mmap_length
= mmap_length
;
547 major
= crash_get_hdr_field(layout
, abi
, major
);
548 DBG("major: %u", major
);
549 minor
= crash_get_hdr_field(layout
, abi
, minor
);
550 DBG("minor: %u", minor
);
551 word_size
= crash_get_hdr_field(layout
, abi
, word_size
);
552 DBG("word_size: %u", word_size
);
557 ret
= get_crash_layout_0_0(layout
, map
);
563 ERR("Unsupported crash ABI %u.%u\n", major
, minor
);
568 ERR("Unsupported crash ABI %u.%u\n", major
, minor
);
572 layout
->word_size
= word_size
;
574 unmapret
= munmap(map
, RB_CRASH_DUMP_ABI_LEN
);
581 /* buf_trunc mask selects only the buffer number. */
583 uint64_t buf_trunc(uint64_t offset
, uint64_t buf_size
)
585 return offset
& ~(buf_size
- 1);
588 /* subbuf_trunc mask selects the subbuffer number. */
590 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. */
597 uint64_t buf_offset(uint64_t offset
, uint64_t buf_size
)
599 return offset
& (buf_size
- 1);
602 /* subbuf_offset mask selects the offset within the current subbuffer. */
604 uint64_t subbuf_offset(uint64_t offset
, uint64_t subbuf_size
)
606 return offset
& (subbuf_size
- 1);
609 /* subbuf_index returns the index of the current subbuffer within the buffer. */
611 uint64_t subbuf_index(uint64_t offset
, uint64_t buf_size
, uint64_t subbuf_size
)
613 return buf_offset(offset
, buf_size
) / subbuf_size
;
617 uint64_t subbuffer_id_get_index(uint32_t mode
, uint64_t id
,
620 if (mode
== RING_BUFFER_OVERWRITE
)
621 return id
& SB_ID_INDEX_MASK(wl
);
627 int copy_crash_subbuf(const struct lttng_crash_layout
*layout
,
628 int fd_dest
, char *buf
, uint64_t offset
)
630 uint64_t buf_size
, subbuf_size
, num_subbuf
, sbidx
, id
,
631 sb_bindex
, rpages_offset
, p_offset
, seq_cc
,
632 committed
, commit_count_mask
, consumed_cur
,
638 * Get the current subbuffer by applying the proper mask to
639 * "offset", and looking up the subbuf location within the
643 buf_size
= layout
->buf_size
;
644 subbuf_size
= layout
->subbuf_size
;
645 num_subbuf
= layout
->num_subbuf
;
647 switch (layout
->word_size
) {
648 case 4: commit_count_mask
= 0xFFFFFFFFULL
/ num_subbuf
;
650 case 8: commit_count_mask
= 0xFFFFFFFFFFFFFFFFULL
/ num_subbuf
;
653 ERR("Unsupported word size: %u",
654 (unsigned int) layout
->word_size
);
658 DBG("Copy crash subbuffer at offset %" PRIu64
, offset
);
659 sbidx
= subbuf_index(offset
, buf_size
, subbuf_size
);
662 * Find where the seq cc is located. Compute length of data to
665 seq_cc
= crash_get_array_field(layout
, buf
, commit_hot_array
,
666 sbidx
, commit_hot_seq
);
667 consumed_cur
= crash_get_field(layout
, buf
, consumed_offset
);
670 * Check that the buffer we are getting is after or at
671 * consumed_cur position.
673 if ((long) subbuf_trunc(offset
, subbuf_size
)
674 - (long) subbuf_trunc(consumed_cur
, subbuf_size
) < 0) {
675 DBG("No data: position is before consumed_cur");
680 * Check if subbuffer has been fully committed.
682 if (((seq_cc
- subbuf_size
) & commit_count_mask
)
683 - (buf_trunc(offset
, buf_size
) / num_subbuf
)
685 committed
= subbuf_size
;
687 committed
= subbuf_offset(seq_cc
, subbuf_size
);
689 DBG("No data committed, seq_cc: %" PRIu64
, seq_cc
);
694 /* Find actual physical offset in subbuffer table */
695 id
= crash_get_array_field(layout
, buf
, buf_wsb_array
,
697 sb_bindex
= subbuffer_id_get_index(layout
->mode
, id
,
699 rpages_offset
= crash_get_array_field(layout
, buf
, sb_array
,
700 sb_bindex
, sb_array_shmp_offset
);
701 p_offset
= crash_get_field(layout
, buf
+ rpages_offset
,
702 sb_backend_p_offset
);
703 subbuf_ptr
= buf
+ p_offset
;
705 if (committed
== subbuf_size
) {
707 * Packet header can be used.
709 if (layout
->length
.packet_size
) {
711 subbuf_ptr
+ layout
->offset
.packet_size
,
712 layout
->length
.packet_size
);
713 if (layout
->reverse_byte_order
) {
714 packet_size
= __bswap_64(packet_size
);
716 packet_size
/= CHAR_BIT
;
718 packet_size
= subbuf_size
;
724 * Find where to patch the sub-buffer header with actual
725 * readable data len and packet len, derived from seq
726 * cc. Patch it in our in-memory copy.
728 patch_size
= committed
* CHAR_BIT
;
729 if (layout
->reverse_byte_order
) {
730 patch_size
= __bswap_64(patch_size
);
732 if (layout
->length
.content_size
) {
733 memcpy(subbuf_ptr
+ layout
->offset
.content_size
,
734 &patch_size
, layout
->length
.content_size
);
736 if (layout
->length
.packet_size
) {
737 memcpy(subbuf_ptr
+ layout
->offset
.packet_size
,
738 &patch_size
, layout
->length
.packet_size
);
740 packet_size
= committed
;
744 * Copy packet into fd_dest.
746 writelen
= lttng_write(fd_dest
, subbuf_ptr
, packet_size
);
747 if (writelen
< packet_size
) {
748 PERROR("Error writing to output file");
751 DBG("Copied %" PRIu64
" bytes of data", packet_size
);
759 int copy_crash_data(const struct lttng_crash_layout
*layout
, int fd_dest
,
763 int ret
= 0, has_data
= 0;
766 uint64_t prod_offset
, consumed_offset
;
767 uint64_t offset
, subbuf_size
;
770 ret
= fstat(fd_src
, &statbuf
);
774 src_file_len
= layout
->mmap_length
;
775 buf
= zmalloc(src_file_len
);
779 readlen
= lttng_read(fd_src
, buf
, src_file_len
);
781 PERROR("Error reading input file");
786 prod_offset
= crash_get_field(layout
, buf
, prod_offset
);
787 DBG("prod_offset: 0x%" PRIx64
, prod_offset
);
788 consumed_offset
= crash_get_field(layout
, buf
, consumed_offset
);
789 DBG("consumed_offset: 0x%" PRIx64
, consumed_offset
);
790 subbuf_size
= layout
->subbuf_size
;
792 for (offset
= consumed_offset
; offset
< prod_offset
;
793 offset
+= subbuf_size
) {
794 ret
= copy_crash_subbuf(layout
, fd_dest
, buf
, offset
);
804 if (ret
&& ret
!= -ENODATA
) {
815 int extract_file(int output_dir_fd
, const char *output_file
,
816 int input_dir_fd
, const char *input_file
)
818 int fd_dest
, fd_src
, ret
= 0, closeret
;
819 struct lttng_crash_layout layout
;
821 layout
.reverse_byte_order
= 0; /* For reading magic number */
823 DBG("Extract file '%s'", input_file
);
824 fd_src
= openat(input_dir_fd
, input_file
, O_RDONLY
);
826 PERROR("Error opening '%s' for reading",
832 /* Query the crash ABI layout */
833 ret
= get_crash_layout(&layout
, fd_src
);
838 fd_dest
= openat(output_dir_fd
, output_file
,
839 O_RDWR
| O_CREAT
| O_EXCL
,
840 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
842 PERROR("Error opening '%s' for writing",
848 ret
= copy_crash_data(&layout
, fd_dest
, fd_src
);
854 closeret
= close(fd_dest
);
858 if (ret
== -ENODATA
) {
859 closeret
= unlinkat(output_dir_fd
, output_file
, 0);
865 closeret
= close(fd_src
);
874 int extract_all_files(const char *output_path
,
875 const char *input_path
)
877 DIR *input_dir
, *output_dir
;
878 int input_dir_fd
, output_dir_fd
, ret
= 0, closeret
;
879 struct dirent
*entry
; /* input */
881 /* Open input directory */
882 input_dir
= opendir(input_path
);
884 PERROR("Cannot open '%s' path", input_path
);
887 input_dir_fd
= dirfd(input_dir
);
888 if (input_dir_fd
< 0) {
893 /* Open output directory */
894 output_dir
= opendir(output_path
);
896 PERROR("Cannot open '%s' path", output_path
);
899 output_dir_fd
= dirfd(output_dir
);
900 if (output_dir_fd
< 0) {
905 while ((entry
= readdir(input_dir
))) {
906 if (!strcmp(entry
->d_name
, ".")
907 || !strcmp(entry
->d_name
, ".."))
909 ret
= extract_file(output_dir_fd
, entry
->d_name
,
910 input_dir_fd
, entry
->d_name
);
911 if (ret
== -ENODATA
) {
912 DBG("No data in file '%s', skipping", entry
->d_name
);
915 } else if (ret
< 0) {
917 } else if (ret
> 0) {
918 DBG("Skipping file '%s'", entry
->d_name
);
923 closeret
= closedir(output_dir
);
927 closeret
= closedir(input_dir
);
935 int extract_one_trace(const char *output_path
,
936 const char *input_path
)
938 char dest
[PATH_MAX
], src
[PATH_MAX
];
941 DBG("Extract crash trace '%s' into '%s'", input_path
, output_path
);
944 strncpy(dest
, output_path
, PATH_MAX
);
945 dest
[PATH_MAX
- 1] = '\0';
946 strncat(dest
, "/metadata", PATH_MAX
- strlen(dest
) - 1);
948 strncpy(src
, input_path
, PATH_MAX
);
949 src
[PATH_MAX
- 1] = '\0';
950 strncat(src
, "/metadata", PATH_MAX
- strlen(dest
) - 1);
952 ret
= copy_file(dest
, src
);
957 /* Extract each other file that has expected header */
958 return extract_all_files(output_path
, input_path
);
962 int extract_trace_recursive(const char *output_path
,
963 const char *input_path
)
966 int dir_fd
, ret
= 0, closeret
;
967 struct dirent
*entry
;
972 dir
= opendir(input_path
);
974 PERROR("Cannot open '%s' path", input_path
);
978 path_len
= strlen(input_path
);
986 while ((entry
= readdir(dir
))) {
989 char filename
[PATH_MAX
];
991 if (!strcmp(entry
->d_name
, ".")
992 || !strcmp(entry
->d_name
, "..")) {
996 name_len
= strlen(entry
->d_name
);
997 if (path_len
+ name_len
+ 2 > sizeof(filename
)) {
998 ERR("Failed to remove file: path name too long (%s/%s)",
999 input_path
, entry
->d_name
);
1003 if (snprintf(filename
, sizeof(filename
), "%s/%s",
1004 input_path
, entry
->d_name
) < 0) {
1005 ERR("Failed to format path.");
1009 if (stat(filename
, &st
)) {
1014 if (S_ISDIR(st
.st_mode
)) {
1015 char output_subpath
[PATH_MAX
];
1016 char input_subpath
[PATH_MAX
];
1018 strncpy(output_subpath
, output_path
,
1019 sizeof(output_subpath
));
1020 output_subpath
[sizeof(output_subpath
) - 1] = '\0';
1021 strncat(output_subpath
, "/",
1022 sizeof(output_subpath
) - strlen(output_subpath
) - 1);
1023 strncat(output_subpath
, entry
->d_name
,
1024 sizeof(output_subpath
) - strlen(output_subpath
) - 1);
1026 ret
= mkdir(output_subpath
, S_IRWXU
| S_IRWXG
);
1033 strncpy(input_subpath
, input_path
,
1034 sizeof(input_subpath
));
1035 input_subpath
[sizeof(input_subpath
) - 1] = '\0';
1036 strncat(input_subpath
, "/",
1037 sizeof(input_subpath
) - strlen(input_subpath
) - 1);
1038 strncat(input_subpath
, entry
->d_name
,
1039 sizeof(input_subpath
) - strlen(input_subpath
) - 1);
1041 ret
= extract_trace_recursive(output_subpath
,
1046 } else if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
)) {
1047 if (!strcmp(entry
->d_name
, "metadata")) {
1048 ret
= extract_one_trace(output_path
,
1051 WARN("Error extracting trace '%s', continuing anyway.",
1062 closeret
= closedir(dir
);
1070 int delete_dir_recursive(const char *path
)
1073 int dir_fd
, ret
= 0, closeret
;
1075 struct dirent
*entry
;
1077 /* Open trace directory */
1078 dir
= opendir(path
);
1080 PERROR("Cannot open '%s' path", path
);
1082 goto end_no_closedir
;
1085 path_len
= strlen(path
);
1087 dir_fd
= dirfd(dir
);
1094 while ((entry
= readdir(dir
))) {
1097 char filename
[PATH_MAX
];
1099 if (!strcmp(entry
->d_name
, ".")
1100 || !strcmp(entry
->d_name
, "..")) {
1104 name_len
= strlen(entry
->d_name
);
1105 if (path_len
+ name_len
+ 2 > sizeof(filename
)) {
1106 ERR("Failed to remove file: path name too long (%s/%s)",
1107 path
, entry
->d_name
);
1111 if (snprintf(filename
, sizeof(filename
), "%s/%s",
1112 path
, entry
->d_name
) < 0) {
1113 ERR("Failed to format path.");
1117 if (stat(filename
, &st
)) {
1122 if (S_ISDIR(st
.st_mode
)) {
1123 char *subpath
= zmalloc(PATH_MAX
);
1126 PERROR("zmalloc path");
1130 strncpy(subpath
, path
, PATH_MAX
);
1131 subpath
[PATH_MAX
- 1] = '\0';
1132 strncat(subpath
, "/",
1133 PATH_MAX
- strlen(subpath
) - 1);
1134 strncat(subpath
, entry
->d_name
,
1135 PATH_MAX
- strlen(subpath
) - 1);
1137 ret
= delete_dir_recursive(subpath
);
1140 /* Error occurred, abort traversal. */
1143 } else if (S_ISREG(st
.st_mode
)) {
1144 ret
= unlinkat(dir_fd
, entry
->d_name
, 0);
1146 PERROR("Unlinking '%s'", entry
->d_name
);
1158 PERROR("rmdir '%s'", path
);
1161 closeret
= closedir(dir
);
1170 int view_trace(const char *viewer_path
, const char *trace_path
)
1179 } else if (pid
> 0) {
1183 pid
= waitpid(pid
, &status
, 0);
1184 if (pid
< 0 || !WIFEXITED(status
)) {
1191 ret
= execlp(viewer_path
, viewer_path
,
1192 trace_path
, (char *) NULL
);
1197 exit(EXIT_SUCCESS
); /* Never reached */
1205 int main(int argc
, char *argv
[])
1208 bool has_warning
= false;
1209 const char *output_path
= NULL
;
1210 char tmppath
[] = "/tmp/lttng-crash-XXXXXX";
1212 progname
= argv
[0] ? argv
[0] : "lttng-crash";
1214 ret
= parse_args(argc
, argv
);
1217 } else if (ret
< 0) {
1222 if (opt_output_path
) {
1223 output_path
= opt_output_path
;
1224 ret
= mkdir(output_path
, S_IRWXU
| S_IRWXG
);
1231 output_path
= mkdtemp(tmppath
);
1239 ret
= extract_trace_recursive(output_path
, input_path
);
1243 } else if (ret
> 0) {
1244 /* extract_trace_recursive reported a warning. */
1247 if (!opt_output_path
) {
1249 ret
= view_trace(opt_viewer_path
, output_path
);
1253 /* unlink temporary trace */
1254 ret
= delete_dir_recursive(output_path
);
1260 exit(has_warning
? EXIT_FAILURE
: EXIT_SUCCESS
);