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>
41 #define DEFAULT_VIEWER "babeltrace"
43 #define COPY_BUFLEN 4096
44 #define RB_CRASH_DUMP_ABI_LEN 32
46 #define RB_CRASH_DUMP_ABI_MAGIC_LEN 16
49 * The 128-bit magic number is xor'd in the process data so it does not
50 * cause a false positive when searching for buffers by scanning memory.
51 * The actual magic number is:
52 * 0x17, 0x7B, 0xF1, 0x77, 0xBF, 0x17, 0x7B, 0xF1,
53 * 0x77, 0xBF, 0x17, 0x7B, 0xF1, 0x77, 0xBF, 0x17,
55 #define RB_CRASH_DUMP_ABI_MAGIC_XOR \
57 0x17 ^ 0xFF, 0x7B ^ 0xFF, 0xF1 ^ 0xFF, 0x77 ^ 0xFF, \
58 0xBF ^ 0xFF, 0x17 ^ 0xFF, 0x7B ^ 0xFF, 0xF1 ^ 0xFF, \
59 0x77 ^ 0xFF, 0xBF ^ 0xFF, 0x17 ^ 0xFF, 0x7B ^ 0xFF, \
60 0xF1 ^ 0xFF, 0x77 ^ 0xFF, 0xBF ^ 0xFF, 0x17 ^ 0xFF, \
64 * Non-static to ensure the compiler does not optimize away the xor.
66 uint8_t lttng_crash_expected_magic_xor
[] = RB_CRASH_DUMP_ABI_MAGIC_XOR
;
68 #define RB_CRASH_ENDIAN 0x1234
69 #define RB_CRASH_ENDIAN_REVERSE 0x3412
71 enum lttng_crash_type
{
72 LTTNG_CRASH_TYPE_UST
= 0,
73 LTTNG_CRASH_TYPE_KERNEL
= 1,
76 /* LTTng ring buffer defines (copied) */
78 #define HALF_ULONG_BITS(wl) (((wl) * CHAR_BIT) >> 1)
80 #define SB_ID_OFFSET_SHIFT(wl) (HALF_ULONG_BITS(wl) + 1)
81 #define SB_ID_OFFSET_COUNT(wl) (1UL << SB_ID_OFFSET_SHIFT(wl))
82 #define SB_ID_OFFSET_MASK(wl) (~(SB_ID_OFFSET_COUNT(wl) - 1))
84 * Lowest bit of top word half belongs to noref. Used only for overwrite mode.
86 #define SB_ID_NOREF_SHIFT(wl) (SB_ID_OFFSET_SHIFT(wl) - 1)
87 #define SB_ID_NOREF_COUNT(wl) (1UL << SB_ID_NOREF_SHIFT(wl))
88 #define SB_ID_NOREF_MASK(wl) SB_ID_NOREF_COUNT(wl)
90 * In overwrite mode: lowest half of word is used for index.
91 * Limit of 2^16 subbuffers per buffer on 32-bit, 2^32 on 64-bit.
92 * In producer-consumer mode: whole word used for index.
94 #define SB_ID_INDEX_SHIFT(wl) 0
95 #define SB_ID_INDEX_COUNT(wl) (1UL << SB_ID_INDEX_SHIFT(wl))
96 #define SB_ID_INDEX_MASK(wl) (SB_ID_NOREF_COUNT(wl) - 1)
99 RING_BUFFER_OVERWRITE
= 0, /* Overwrite when buffer full */
100 RING_BUFFER_DISCARD
= 1, /* Discard when buffer full */
103 struct crash_abi_unknown
{
104 uint8_t magic
[RB_CRASH_DUMP_ABI_MAGIC_LEN
];
105 uint64_t mmap_length
; /* Overall length of crash record */
107 * { 0x12, 0x34 }: big endian
108 * { 0x34, 0x12 }: little endian
110 uint16_t major
; /* Major number. */
111 uint16_t minor
; /* Minor number. */
112 uint8_t word_size
; /* Word size (bytes). */
113 uint8_t layout_type
; /* enum lttng_crash_layout */
114 } __attribute__((packed
));
116 struct crash_abi_0_0
{
117 struct crash_abi_unknown parent
;
120 uint32_t prod_offset
;
121 uint32_t consumed_offset
;
122 uint32_t commit_hot_array
;
123 uint32_t commit_hot_seq
;
124 uint32_t buf_wsb_array
;
127 uint32_t sb_array_shmp_offset
;
128 uint32_t sb_backend_p_offset
;
129 uint32_t content_size
;
130 uint32_t packet_size
;
131 } __attribute__((packed
)) offset
;
134 uint8_t consumed_offset
;
135 uint8_t commit_hot_seq
;
137 uint8_t sb_array_shmp_offset
;
138 uint8_t sb_backend_p_offset
;
139 uint8_t content_size
;
141 } __attribute__((packed
)) length
;
143 uint32_t commit_hot_array
;
144 uint32_t buf_wsb_array
;
146 } __attribute__((packed
)) stride
;
148 uint64_t buf_size
; /* Size of the buffer */
149 uint64_t subbuf_size
; /* Sub-buffer size */
150 uint64_t num_subbuf
; /* Number of sub-buffers for writer */
151 uint32_t mode
; /* Buffer mode: 0: overwrite, 1: discard */
152 } __attribute__((packed
));
154 struct lttng_crash_layout
{
156 int prod_offset
, consumed_offset
,
157 commit_hot_array
, commit_hot_seq
,
158 buf_wsb_array
, buf_wsb_id
,
159 sb_array
, sb_array_shmp_offset
,
160 sb_backend_p_offset
, content_size
,
164 int prod_offset
, consumed_offset
,
165 commit_hot_seq
, buf_wsb_id
,
166 sb_array_shmp_offset
, sb_backend_p_offset
,
167 content_size
, packet_size
;
170 int commit_hot_array
, buf_wsb_array
, sb_array
;
173 int reverse_byte_order
;
176 uint64_t mmap_length
; /* Length of crash record */
177 uint64_t buf_size
; /* Size of the buffer */
178 uint64_t subbuf_size
; /* Sub-buffer size */
179 uint64_t num_subbuf
; /* Number of sub-buffers for writer */
180 uint32_t mode
; /* Buffer mode: 0: overwrite, 1: discard */
184 static char *progname
,
185 *opt_viewer_path
= NULL
,
186 *opt_output_path
= NULL
;
188 static char *input_path
;
190 int lttng_opt_quiet
, lttng_opt_verbose
, lttng_opt_mi
;
196 /* Getopt options. No first level command. */
197 static struct option long_options
[] = {
198 { "version", 0, NULL
, 'V' },
199 { "help", 0, NULL
, 'h' },
200 { "verbose", 0, NULL
, 'v' },
201 { "viewer", 1, NULL
, 'e' },
202 { "extract", 1, NULL
, 'x' },
203 { "list-options", 0, NULL
, OPT_DUMP_OPTIONS
},
204 { NULL
, 0, NULL
, 0 },
207 static void usage(FILE *ofp
)
209 fprintf(ofp
, "LTTng Crash Trace Viewer " VERSION
" - " VERSION_NAME
"%s\n\n",
210 GIT_VERSION
[0] == '\0' ? "" : " - " GIT_VERSION
);
211 fprintf(ofp
, "usage: lttng-crash [OPTIONS] FILE\n");
213 fprintf(ofp
, "Options:\n");
214 fprintf(ofp
, " -V, --version Show version.\n");
215 fprintf(ofp
, " -h, --help Show this help.\n");
216 fprintf(ofp
, " --list-options Simple listing of lttng-crash options.\n");
217 fprintf(ofp
, " -v, --verbose Increase verbosity.\n");
218 fprintf(ofp
, " -e, --viewer Specify viewer and/or options to use. This will\n"
219 " completely override the default viewers so please\n"
220 " make sure to specify the full command. The trace\n"
221 " directory paths appended at the end to the\n"
223 fprintf(ofp
, " -x, --extract PATH Extract trace(s) to specified path. Don't view\n"
226 fprintf(ofp
, "Please see the lttng-crash(1) man page for full documentation.\n");
227 fprintf(ofp
, "See http://lttng.org for updates, bug reports and news.\n");
230 static void version(FILE *ofp
)
232 fprintf(ofp
, "%s (LTTng Crash Trace Viewer) " VERSION
" - " VERSION_NAME
235 GIT_VERSION
[0] == '\0' ? "" : " - " GIT_VERSION
);
241 * List options line by line. This is mostly for bash auto completion and to
242 * avoid difficult parsing.
244 static void list_options(FILE *ofp
)
247 struct option
*option
= NULL
;
249 option
= &long_options
[i
];
250 while (option
->name
!= NULL
) {
251 fprintf(ofp
, "--%s\n", option
->name
);
253 if (isprint(option
->val
)) {
254 fprintf(ofp
, "-%c\n", option
->val
);
258 option
= &long_options
[i
];
263 * Parse command line arguments.
265 * Return 0 if OK, else -1
267 static int parse_args(int argc
, char **argv
)
276 while ((opt
= getopt_long(argc
, argv
, "+Vhve:x:", long_options
, NULL
)) != -1) {
287 /* There is only 3 possible level of verbosity. (-vvv) */
288 if (lttng_opt_verbose
< 3) {
289 lttng_opt_verbose
+= 1;
293 free(opt_viewer_path
);
294 opt_viewer_path
= strdup(optarg
);
297 free(opt_output_path
);
298 opt_output_path
= strdup(optarg
);
300 case OPT_DUMP_OPTIONS
:
301 list_options(stdout
);
310 if (!opt_viewer_path
) {
311 opt_viewer_path
= DEFAULT_VIEWER
;
314 /* No leftovers, or more than one input path, print usage and quit */
315 if ((argc
- optind
) == 0 || (argc
- optind
) > 1) {
320 input_path
= argv
[optind
];
329 int copy_file(const char *file_dest
, const char *file_src
)
331 int fd_src
= -1, fd_dest
= -1;
332 ssize_t readlen
, writelen
;
333 char buf
[COPY_BUFLEN
];
336 DBG("Copy metadata file '%s' into '%s'", file_src
, file_dest
);
338 fd_src
= open(file_src
, O_RDONLY
);
340 PERROR("Error opening %s for reading", file_src
);
344 fd_dest
= open(file_dest
, O_RDWR
| O_CREAT
| O_EXCL
,
345 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
347 PERROR("Error opening %s for writing", file_dest
);
353 readlen
= lttng_read(fd_src
, buf
, COPY_BUFLEN
);
355 PERROR("Error reading input file");
362 writelen
= lttng_write(fd_dest
, buf
, readlen
);
363 if (writelen
< readlen
) {
364 PERROR("Error writing to output file");
373 if (close(fd_src
) < 0) {
374 PERROR("Error closing %s", file_src
);
379 if (close(fd_dest
) < 0) {
380 PERROR("Error closing %s", file_dest
);
387 uint64_t _crash_get_field(const struct lttng_crash_layout
*layout
,
388 const char *ptr
, size_t size
)
391 case 1: return *(uint8_t *) ptr
;
392 case 2: if (layout
->reverse_byte_order
) {
393 return __bswap_16(*(uint16_t *) ptr
);
395 return *(uint16_t *) ptr
;
398 case 4: if (layout
->reverse_byte_order
) {
399 return __bswap_32(*(uint32_t *) ptr
);
401 return *(uint32_t *) ptr
;
404 case 8: if (layout
->reverse_byte_order
) {
405 return __bswap_64(*(uint64_t *) ptr
);
407 return *(uint64_t *) ptr
;
416 #define crash_get_field(layout, map, name) \
417 _crash_get_field(layout, (map) + (layout)->offset.name, \
420 #define crash_get_array_field(layout, map, array_name, idx, field_name) \
421 _crash_get_field(layout, \
422 (map) + (layout)->offset.array_name \
423 + (idx * (layout)->stride.array_name) \
424 + (layout)->offset.field_name, \
425 (layout)->length.field_name)
427 #define crash_get_hdr_raw_field(layout, hdr, name) ((hdr)->name)
429 #define crash_get_hdr_field(layout, hdr, name) \
430 _crash_get_field(layout, (const char *) &(hdr)->name, \
433 #define crash_get_layout(layout, hdr, name) \
435 (layout)->name = crash_get_hdr_field(layout, hdr, \
437 DBG("layout.%s = %" PRIu64, #name, \
438 (uint64_t) (layout)->name); \
442 int get_crash_layout_0_0(struct lttng_crash_layout
*layout
,
445 const struct crash_abi_0_0
*abi
= (const struct crash_abi_0_0
*) map
;
447 crash_get_layout(layout
, abi
, offset
.prod_offset
);
448 crash_get_layout(layout
, abi
, offset
.consumed_offset
);
449 crash_get_layout(layout
, abi
, offset
.commit_hot_array
);
450 crash_get_layout(layout
, abi
, offset
.commit_hot_seq
);
451 crash_get_layout(layout
, abi
, offset
.buf_wsb_array
);
452 crash_get_layout(layout
, abi
, offset
.buf_wsb_id
);
453 crash_get_layout(layout
, abi
, offset
.sb_array
);
454 crash_get_layout(layout
, abi
, offset
.sb_array_shmp_offset
);
455 crash_get_layout(layout
, abi
, offset
.sb_backend_p_offset
);
456 crash_get_layout(layout
, abi
, offset
.content_size
);
457 crash_get_layout(layout
, abi
, offset
.packet_size
);
459 crash_get_layout(layout
, abi
, length
.prod_offset
);
460 crash_get_layout(layout
, abi
, length
.consumed_offset
);
461 crash_get_layout(layout
, abi
, length
.commit_hot_seq
);
462 crash_get_layout(layout
, abi
, length
.buf_wsb_id
);
463 crash_get_layout(layout
, abi
, length
.sb_array_shmp_offset
);
464 crash_get_layout(layout
, abi
, length
.sb_backend_p_offset
);
465 crash_get_layout(layout
, abi
, length
.content_size
);
466 crash_get_layout(layout
, abi
, length
.packet_size
);
468 crash_get_layout(layout
, abi
, stride
.commit_hot_array
);
469 crash_get_layout(layout
, abi
, stride
.buf_wsb_array
);
470 crash_get_layout(layout
, abi
, stride
.sb_array
);
472 crash_get_layout(layout
, abi
, buf_size
);
473 crash_get_layout(layout
, abi
, subbuf_size
);
474 crash_get_layout(layout
, abi
, num_subbuf
);
475 crash_get_layout(layout
, abi
, mode
);
481 void print_dbg_magic(const uint8_t *magic
)
483 DBG("magic: 0x%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X",
484 magic
[0], magic
[1], magic
[2], magic
[3],
485 magic
[4], magic
[5], magic
[6], magic
[7],
486 magic
[8], magic
[9], magic
[10], magic
[11],
487 magic
[12], magic
[13], magic
[14], magic
[15]);
491 int check_magic(const uint8_t *magic
)
495 for (i
= 0; i
< RB_CRASH_DUMP_ABI_MAGIC_LEN
; i
++) {
496 if ((magic
[i
] ^ 0xFF) != lttng_crash_expected_magic_xor
[i
]) {
504 int get_crash_layout(struct lttng_crash_layout
*layout
, int fd
)
507 int ret
= 0, unmapret
;
508 const uint8_t *magic
;
509 uint64_t mmap_length
;
510 uint16_t major
, minor
;
512 const struct crash_abi_unknown
*abi
;
514 enum lttng_crash_type layout_type
;
516 map
= mmap(NULL
, RB_CRASH_DUMP_ABI_LEN
, PROT_READ
, MAP_PRIVATE
,
518 if (map
== MAP_FAILED
) {
519 PERROR("Mapping file");
522 abi
= (const struct crash_abi_unknown
*) map
;
523 magic
= crash_get_hdr_raw_field(layout
, abi
, magic
);
524 print_dbg_magic(magic
);
525 if (check_magic(magic
)) {
526 DBG("Unknown magic number");
527 ret
= 1; /* positive return value, skip */
530 endian
= crash_get_hdr_field(layout
, abi
, endian
);
532 case RB_CRASH_ENDIAN
:
534 case RB_CRASH_ENDIAN_REVERSE
:
535 layout
->reverse_byte_order
= 1;
538 DBG("Unknown endianness value: 0x%X", (unsigned int) endian
);
539 ret
= 1; /* positive return value, skip */
542 layout_type
= (enum lttng_crash_type
) crash_get_hdr_field(layout
, abi
, layout_type
);
543 switch (layout_type
) {
544 case LTTNG_CRASH_TYPE_UST
:
546 case LTTNG_CRASH_TYPE_KERNEL
:
547 ERR("lttng-modules buffer layout support not implemented");
548 ret
= 1; /* positive return value, skip */
551 ERR("Unknown layout type %u", (unsigned int) layout_type
);
552 ret
= 1; /* positive return value, skip */
555 mmap_length
= crash_get_hdr_field(layout
, abi
, mmap_length
);
556 DBG("mmap_length: %" PRIu64
, mmap_length
);
557 layout
->mmap_length
= mmap_length
;
558 major
= crash_get_hdr_field(layout
, abi
, major
);
559 DBG("major: %u", major
);
560 minor
= crash_get_hdr_field(layout
, abi
, minor
);
561 DBG("minor: %u", minor
);
562 word_size
= crash_get_hdr_field(layout
, abi
, word_size
);
563 DBG("word_size: %u", word_size
);
568 ret
= get_crash_layout_0_0(layout
, map
);
574 ERR("Unsupported crash ABI %u.%u\n", major
, minor
);
579 ERR("Unsupported crash ABI %u.%u\n", major
, minor
);
583 layout
->word_size
= word_size
;
585 unmapret
= munmap(map
, RB_CRASH_DUMP_ABI_LEN
);
592 /* buf_trunc mask selects only the buffer number. */
594 uint64_t buf_trunc(uint64_t offset
, uint64_t buf_size
)
596 return offset
& ~(buf_size
- 1);
599 /* subbuf_trunc mask selects the subbuffer number. */
601 uint64_t subbuf_trunc(uint64_t offset
, uint64_t subbuf_size
)
603 return offset
& ~(subbuf_size
- 1);
606 /* buf_offset mask selects only the offset within the current buffer. */
608 uint64_t buf_offset(uint64_t offset
, uint64_t buf_size
)
610 return offset
& (buf_size
- 1);
613 /* subbuf_offset mask selects the offset within the current subbuffer. */
615 uint64_t subbuf_offset(uint64_t offset
, uint64_t subbuf_size
)
617 return offset
& (subbuf_size
- 1);
620 /* subbuf_index returns the index of the current subbuffer within the buffer. */
622 uint64_t subbuf_index(uint64_t offset
, uint64_t buf_size
, uint64_t subbuf_size
)
624 return buf_offset(offset
, buf_size
) / subbuf_size
;
628 uint64_t subbuffer_id_get_index(uint32_t mode
, uint64_t id
,
631 if (mode
== RING_BUFFER_OVERWRITE
)
632 return id
& SB_ID_INDEX_MASK(wl
);
638 int copy_crash_subbuf(const struct lttng_crash_layout
*layout
,
639 int fd_dest
, char *buf
, uint64_t offset
)
641 uint64_t buf_size
, subbuf_size
, num_subbuf
, sbidx
, id
,
642 sb_bindex
, rpages_offset
, p_offset
, seq_cc
,
643 committed
, commit_count_mask
, consumed_cur
,
649 * Get the current subbuffer by applying the proper mask to
650 * "offset", and looking up the subbuf location within the
654 buf_size
= layout
->buf_size
;
655 subbuf_size
= layout
->subbuf_size
;
656 num_subbuf
= layout
->num_subbuf
;
658 switch (layout
->word_size
) {
659 case 4: commit_count_mask
= 0xFFFFFFFFULL
/ num_subbuf
;
661 case 8: commit_count_mask
= 0xFFFFFFFFFFFFFFFFULL
/ num_subbuf
;
664 ERR("Unsupported word size: %u",
665 (unsigned int) layout
->word_size
);
669 DBG("Copy crash subbuffer at offset %" PRIu64
, offset
);
670 sbidx
= subbuf_index(offset
, buf_size
, subbuf_size
);
673 * Find where the seq cc is located. Compute length of data to
676 seq_cc
= crash_get_array_field(layout
, buf
, commit_hot_array
,
677 sbidx
, commit_hot_seq
);
678 consumed_cur
= crash_get_field(layout
, buf
, consumed_offset
);
681 * Check that the buffer we are getting is after or at
682 * consumed_cur position.
684 if ((long) subbuf_trunc(offset
, subbuf_size
)
685 - (long) subbuf_trunc(consumed_cur
, subbuf_size
) < 0) {
686 DBG("No data: position is before consumed_cur");
691 * Check if subbuffer has been fully committed.
693 if (((seq_cc
- subbuf_size
) & commit_count_mask
)
694 - (buf_trunc(offset
, buf_size
) / num_subbuf
)
696 committed
= subbuf_size
;
698 committed
= subbuf_offset(seq_cc
, subbuf_size
);
700 DBG("No data committed, seq_cc: %" PRIu64
, seq_cc
);
705 /* Find actual physical offset in subbuffer table */
706 id
= crash_get_array_field(layout
, buf
, buf_wsb_array
,
708 sb_bindex
= subbuffer_id_get_index(layout
->mode
, id
,
710 rpages_offset
= crash_get_array_field(layout
, buf
, sb_array
,
711 sb_bindex
, sb_array_shmp_offset
);
712 p_offset
= crash_get_field(layout
, buf
+ rpages_offset
,
713 sb_backend_p_offset
);
714 subbuf_ptr
= buf
+ p_offset
;
716 if (committed
== subbuf_size
) {
718 * Packet header can be used.
720 if (layout
->length
.packet_size
) {
722 subbuf_ptr
+ layout
->offset
.packet_size
,
723 layout
->length
.packet_size
);
724 if (layout
->reverse_byte_order
) {
725 packet_size
= __bswap_64(packet_size
);
727 packet_size
/= CHAR_BIT
;
729 packet_size
= subbuf_size
;
735 * Find where to patch the sub-buffer header with actual
736 * readable data len and packet len, derived from seq
737 * cc. Patch it in our in-memory copy.
739 patch_size
= committed
* CHAR_BIT
;
740 if (layout
->reverse_byte_order
) {
741 patch_size
= __bswap_64(patch_size
);
743 if (layout
->length
.content_size
) {
744 memcpy(subbuf_ptr
+ layout
->offset
.content_size
,
745 &patch_size
, layout
->length
.content_size
);
747 if (layout
->length
.packet_size
) {
748 memcpy(subbuf_ptr
+ layout
->offset
.packet_size
,
749 &patch_size
, layout
->length
.packet_size
);
751 packet_size
= committed
;
755 * Copy packet into fd_dest.
757 writelen
= lttng_write(fd_dest
, subbuf_ptr
, packet_size
);
758 if (writelen
< packet_size
) {
759 PERROR("Error writing to output file");
762 DBG("Copied %" PRIu64
" bytes of data", packet_size
);
770 int copy_crash_data(const struct lttng_crash_layout
*layout
, int fd_dest
,
774 int ret
= 0, has_data
= 0;
777 uint64_t prod_offset
, consumed_offset
;
778 uint64_t offset
, subbuf_size
;
781 ret
= fstat(fd_src
, &statbuf
);
785 src_file_len
= layout
->mmap_length
;
786 buf
= zmalloc(src_file_len
);
790 readlen
= lttng_read(fd_src
, buf
, src_file_len
);
792 PERROR("Error reading input file");
797 prod_offset
= crash_get_field(layout
, buf
, prod_offset
);
798 DBG("prod_offset: 0x%" PRIx64
, prod_offset
);
799 consumed_offset
= crash_get_field(layout
, buf
, consumed_offset
);
800 DBG("consumed_offset: 0x%" PRIx64
, consumed_offset
);
801 subbuf_size
= layout
->subbuf_size
;
803 for (offset
= consumed_offset
; offset
< prod_offset
;
804 offset
+= subbuf_size
) {
805 ret
= copy_crash_subbuf(layout
, fd_dest
, buf
, offset
);
815 if (ret
&& ret
!= -ENODATA
) {
826 int extract_file(int output_dir_fd
, const char *output_file
,
827 int input_dir_fd
, const char *input_file
)
829 int fd_dest
, fd_src
, ret
= 0, closeret
;
830 struct lttng_crash_layout layout
;
832 layout
.reverse_byte_order
= 0; /* For reading magic number */
834 DBG("Extract file '%s'", input_file
);
835 fd_src
= openat(input_dir_fd
, input_file
, O_RDONLY
);
837 PERROR("Error opening '%s' for reading",
843 /* Query the crash ABI layout */
844 ret
= get_crash_layout(&layout
, fd_src
);
849 fd_dest
= openat(output_dir_fd
, output_file
,
850 O_RDWR
| O_CREAT
| O_EXCL
,
851 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
853 PERROR("Error opening '%s' for writing",
859 ret
= copy_crash_data(&layout
, fd_dest
, fd_src
);
865 closeret
= close(fd_dest
);
869 if (ret
== -ENODATA
) {
870 closeret
= unlinkat(output_dir_fd
, output_file
, 0);
876 closeret
= close(fd_src
);
885 int extract_all_files(const char *output_path
,
886 const char *input_path
)
888 DIR *input_dir
, *output_dir
;
889 int input_dir_fd
, output_dir_fd
, ret
= 0, closeret
;
890 struct dirent
*entry
; /* input */
892 /* Open input directory */
893 input_dir
= opendir(input_path
);
895 PERROR("Cannot open '%s' path", input_path
);
898 input_dir_fd
= dirfd(input_dir
);
899 if (input_dir_fd
< 0) {
904 /* Open output directory */
905 output_dir
= opendir(output_path
);
907 PERROR("Cannot open '%s' path", output_path
);
910 output_dir_fd
= dirfd(output_dir
);
911 if (output_dir_fd
< 0) {
916 while ((entry
= readdir(input_dir
))) {
917 if (!strcmp(entry
->d_name
, ".")
918 || !strcmp(entry
->d_name
, ".."))
920 ret
= extract_file(output_dir_fd
, entry
->d_name
,
921 input_dir_fd
, entry
->d_name
);
922 if (ret
== -ENODATA
) {
923 DBG("No data in file '%s', skipping", entry
->d_name
);
926 } else if (ret
< 0) {
928 } else if (ret
> 0) {
929 DBG("Skipping file '%s'", entry
->d_name
);
934 closeret
= closedir(output_dir
);
938 closeret
= closedir(input_dir
);
946 int extract_one_trace(const char *output_path
,
947 const char *input_path
)
949 char dest
[PATH_MAX
], src
[PATH_MAX
];
952 DBG("Extract crash trace '%s' into '%s'", input_path
, output_path
);
955 strncpy(dest
, output_path
, PATH_MAX
);
956 dest
[PATH_MAX
- 1] = '\0';
957 strncat(dest
, "/metadata", PATH_MAX
- strlen(dest
) - 1);
959 strncpy(src
, input_path
, PATH_MAX
);
960 src
[PATH_MAX
- 1] = '\0';
961 strncat(src
, "/metadata", PATH_MAX
- strlen(dest
) - 1);
963 ret
= copy_file(dest
, src
);
968 /* Extract each other file that has expected header */
969 return extract_all_files(output_path
, input_path
);
973 int extract_trace_recursive(const char *output_path
,
974 const char *input_path
)
977 int dir_fd
, ret
= 0, closeret
;
978 struct dirent
*entry
;
982 dir
= opendir(input_path
);
984 PERROR("Cannot open '%s' path", input_path
);
993 while ((entry
= readdir(dir
))) {
994 if (!strcmp(entry
->d_name
, ".")
995 || !strcmp(entry
->d_name
, "..")) {
998 switch (entry
->d_type
) {
1001 char output_subpath
[PATH_MAX
];
1002 char input_subpath
[PATH_MAX
];
1004 strncpy(output_subpath
, output_path
,
1005 sizeof(output_subpath
));
1006 output_subpath
[sizeof(output_subpath
) - 1] = '\0';
1007 strncat(output_subpath
, "/",
1008 sizeof(output_subpath
) - strlen(output_subpath
) - 1);
1009 strncat(output_subpath
, entry
->d_name
,
1010 sizeof(output_subpath
) - strlen(output_subpath
) - 1);
1012 ret
= mkdir(output_subpath
, S_IRWXU
| S_IRWXG
);
1019 strncpy(input_subpath
, input_path
,
1020 sizeof(input_subpath
));
1021 input_subpath
[sizeof(input_subpath
) - 1] = '\0';
1022 strncat(input_subpath
, "/",
1023 sizeof(input_subpath
) - strlen(input_subpath
) - 1);
1024 strncat(input_subpath
, entry
->d_name
,
1025 sizeof(input_subpath
) - strlen(input_subpath
) - 1);
1027 ret
= extract_trace_recursive(output_subpath
,
1036 if (!strcmp(entry
->d_name
, "metadata")) {
1037 ret
= extract_one_trace(output_path
,
1040 WARN("Error extracting trace '%s', continuing anyway.",
1045 /* Ignore other files */
1053 closeret
= closedir(dir
);
1061 int delete_dir_recursive(const char *path
)
1064 int dir_fd
, ret
= 0, closeret
;
1065 struct dirent
*entry
;
1067 /* Open trace directory */
1068 dir
= opendir(path
);
1070 PERROR("Cannot open '%s' path", path
);
1074 dir_fd
= dirfd(dir
);
1081 while ((entry
= readdir(dir
))) {
1082 if (!strcmp(entry
->d_name
, ".")
1083 || !strcmp(entry
->d_name
, "..")) {
1086 switch (entry
->d_type
) {
1089 char *subpath
= zmalloc(PATH_MAX
);
1092 PERROR("zmalloc path");
1096 strncpy(subpath
, path
, PATH_MAX
);
1097 subpath
[PATH_MAX
- 1] = '\0';
1098 strncat(subpath
, "/",
1099 PATH_MAX
- strlen(subpath
) - 1);
1100 strncat(subpath
, entry
->d_name
,
1101 PATH_MAX
- strlen(subpath
) - 1);
1103 ret
= delete_dir_recursive(subpath
);
1106 /* Error occured, abort traversal. */
1112 ret
= unlinkat(dir_fd
, entry
->d_name
, 0);
1114 PERROR("Unlinking '%s'", entry
->d_name
);
1127 PERROR("rmdir '%s'", path
);
1130 closeret
= closedir(dir
);
1138 int view_trace(const char *viewer_path
, const char *trace_path
)
1147 } else if (pid
> 0) {
1151 pid
= waitpid(pid
, &status
, 0);
1152 if (pid
< 0 || !WIFEXITED(status
)) {
1159 ret
= execlp(viewer_path
, viewer_path
,
1160 trace_path
, (char *) NULL
);
1165 exit(EXIT_SUCCESS
); /* Never reached */
1173 int main(int argc
, char *argv
[])
1176 bool has_warning
= false;
1177 const char *output_path
= NULL
;
1178 char tmppath
[] = "/tmp/lttng-crash-XXXXXX";
1180 progname
= argv
[0] ? argv
[0] : "lttng-crash";
1182 ret
= parse_args(argc
, argv
);
1185 } else if (ret
< 0) {
1190 if (opt_output_path
) {
1191 output_path
= opt_output_path
;
1192 ret
= mkdir(output_path
, S_IRWXU
| S_IRWXG
);
1199 output_path
= mkdtemp(tmppath
);
1207 ret
= extract_trace_recursive(output_path
, input_path
);
1211 } else if (ret
> 0) {
1212 /* extract_trace_recursive reported a warning. */
1215 if (!opt_output_path
) {
1217 ret
= view_trace(opt_viewer_path
, output_path
);
1221 /* unlink temporary trace */
1222 ret
= delete_dir_recursive(output_path
);
1228 exit(has_warning
? EXIT_FAILURE
: EXIT_SUCCESS
);