Fix: lttng-crash: detect truncated files
[lttng-tools.git] / src / bin / lttng-crash / lttng-crash.c
CommitLineData
d7ba1388
MD
1/*
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
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.
8 *
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.
13 *
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.
17 */
18
d7ba1388
MD
19#include <getopt.h>
20#include <signal.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <sys/mman.h>
28#include <fcntl.h>
29#include <sys/wait.h>
30#include <unistd.h>
d7ba1388
MD
31#include <ctype.h>
32#include <dirent.h>
33#include <byteswap.h>
34#include <inttypes.h>
aa25d686 35#include <stdbool.h>
d7ba1388
MD
36
37#include <version.h>
38#include <lttng/lttng.h>
39#include <common/common.h>
6ef14df5 40#include <common/utils.h>
d7ba1388
MD
41
42#define DEFAULT_VIEWER "babeltrace"
43
44#define COPY_BUFLEN 4096
45#define RB_CRASH_DUMP_ABI_LEN 32
46
47#define RB_CRASH_DUMP_ABI_MAGIC_LEN 16
48
49/*
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,
55 */
56#define RB_CRASH_DUMP_ABI_MAGIC_XOR \
57 { \
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, \
62 }
63
64/*
65 * Non-static to ensure the compiler does not optimize away the xor.
66 */
67uint8_t lttng_crash_expected_magic_xor[] = RB_CRASH_DUMP_ABI_MAGIC_XOR;
68
69#define RB_CRASH_ENDIAN 0x1234
70#define RB_CRASH_ENDIAN_REVERSE 0x3412
71
72enum lttng_crash_type {
73 LTTNG_CRASH_TYPE_UST = 0,
74 LTTNG_CRASH_TYPE_KERNEL = 1,
75};
76
77/* LTTng ring buffer defines (copied) */
78
79#define HALF_ULONG_BITS(wl) (((wl) * CHAR_BIT) >> 1)
80
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))
84/*
85 * Lowest bit of top word half belongs to noref. Used only for overwrite mode.
86 */
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)
90/*
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.
94 */
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)
98
99enum rb_modes {
100 RING_BUFFER_OVERWRITE = 0, /* Overwrite when buffer full */
101 RING_BUFFER_DISCARD = 1, /* Discard when buffer full */
102};
103
104struct crash_abi_unknown {
105 uint8_t magic[RB_CRASH_DUMP_ABI_MAGIC_LEN];
8726a045 106 uint64_t mmap_length; /* Overall length of crash record */
d7ba1388
MD
107 uint16_t endian; /*
108 * { 0x12, 0x34 }: big endian
109 * { 0x34, 0x12 }: little endian
110 */
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));
116
117struct crash_abi_0_0 {
118 struct crash_abi_unknown parent;
119
120 struct {
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;
126 uint32_t buf_wsb_id;
127 uint32_t sb_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;
133 struct {
134 uint8_t prod_offset;
135 uint8_t consumed_offset;
136 uint8_t commit_hot_seq;
137 uint8_t buf_wsb_id;
138 uint8_t sb_array_shmp_offset;
139 uint8_t sb_backend_p_offset;
140 uint8_t content_size;
141 uint8_t packet_size;
142 } __attribute__((packed)) length;
143 struct {
144 uint32_t commit_hot_array;
145 uint32_t buf_wsb_array;
146 uint32_t sb_array;
147 } __attribute__((packed)) stride;
148
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));
154
155struct lttng_crash_layout {
156 struct {
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,
162 packet_size;
163 } offset;
164 struct {
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;
169 } length;
170 struct {
171 int commit_hot_array, buf_wsb_array, sb_array;
172 } stride;
173
174 int reverse_byte_order;
175 int word_size;
176
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 */
182};
183
184/* Variables */
185static char *progname,
9030e9a9
AB
186 *opt_viewer_path = NULL,
187 *opt_output_path = NULL;
d7ba1388 188
a424227e 189static char *input_path;
d7ba1388
MD
190
191int lttng_opt_quiet, lttng_opt_verbose, lttng_opt_mi;
192
193enum {
194 OPT_DUMP_OPTIONS,
195};
196
197/* Getopt options. No first level command. */
198static 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 },
206};
207
6ef14df5 208static void usage(void)
d7ba1388 209{
6ef14df5
PP
210 int ret = utils_show_man_page(1, "lttng-crash");
211
212 if (ret) {
213 ERR("Cannot view man page lttng-crash(1)");
214 perror("exec");
215 exit(EXIT_FAILURE);
216 }
d7ba1388
MD
217}
218
219static void version(FILE *ofp)
220{
221 fprintf(ofp, "%s (LTTng Crash Trace Viewer) " VERSION " - " VERSION_NAME
222"%s\n",
223 progname,
224 GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION);
225}
226
227/*
228 * list_options
229 *
230 * List options line by line. This is mostly for bash auto completion and to
231 * avoid difficult parsing.
232 */
233static void list_options(FILE *ofp)
234{
235 int i = 0;
236 struct option *option = NULL;
237
238 option = &long_options[i];
239 while (option->name != NULL) {
240 fprintf(ofp, "--%s\n", option->name);
241
242 if (isprint(option->val)) {
243 fprintf(ofp, "-%c\n", option->val);
244 }
245
246 i++;
247 option = &long_options[i];
248 }
249}
250
251/*
252 * Parse command line arguments.
253 *
254 * Return 0 if OK, else -1
255 */
256static int parse_args(int argc, char **argv)
257{
258 int opt, ret = 0;
259
260 if (argc < 2) {
6ef14df5 261 usage();
d7ba1388
MD
262 exit(EXIT_FAILURE);
263 }
264
d07f3d99 265 while ((opt = getopt_long(argc, argv, "+Vhve:x:", long_options, NULL)) != -1) {
d7ba1388
MD
266 switch (opt) {
267 case 'V':
268 version(stdout);
269 ret = 1;
270 goto end;
271 case 'h':
6ef14df5 272 usage();
d7ba1388
MD
273 ret = 1;
274 goto end;
275 case 'v':
276 /* There is only 3 possible level of verbosity. (-vvv) */
277 if (lttng_opt_verbose < 3) {
278 lttng_opt_verbose += 1;
279 }
280 break;
281 case 'e':
9030e9a9 282 free(opt_viewer_path);
d7ba1388
MD
283 opt_viewer_path = strdup(optarg);
284 break;
285 case 'x':
9030e9a9 286 free(opt_output_path);
d7ba1388
MD
287 opt_output_path = strdup(optarg);
288 break;
289 case OPT_DUMP_OPTIONS:
290 list_options(stdout);
291 ret = 1;
292 goto end;
293 default:
6ef14df5 294 ERR("Unknown command-line option");
d7ba1388
MD
295 goto error;
296 }
297 }
298
9030e9a9
AB
299 if (!opt_viewer_path) {
300 opt_viewer_path = DEFAULT_VIEWER;
301 }
302
a424227e 303 /* No leftovers, or more than one input path, print usage and quit */
6ef14df5
PP
304 if (argc - optind != 1) {
305 ERR("Command-line error: Specify exactly one input path");
d7ba1388
MD
306 goto error;
307 }
308
a424227e 309 input_path = argv[optind];
d7ba1388
MD
310end:
311 return ret;
312
313error:
314 return -1;
315}
316
317static
318int copy_file(const char *file_dest, const char *file_src)
319{
8726a045 320 int fd_src = -1, fd_dest = -1;
d7ba1388
MD
321 ssize_t readlen, writelen;
322 char buf[COPY_BUFLEN];
8726a045 323 int ret;
d7ba1388
MD
324
325 DBG("Copy metadata file '%s' into '%s'", file_src, file_dest);
326
327 fd_src = open(file_src, O_RDONLY);
328 if (fd_src < 0) {
329 PERROR("Error opening %s for reading", file_src);
8726a045
JR
330 ret = -errno;
331 goto error;
d7ba1388
MD
332 }
333 fd_dest = open(file_dest, O_RDWR | O_CREAT | O_EXCL,
334 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
335 if (fd_dest < 0) {
336 PERROR("Error opening %s for writing", file_dest);
8726a045
JR
337 ret = -errno;
338 goto error;
d7ba1388
MD
339 }
340
341 for (;;) {
342 readlen = lttng_read(fd_src, buf, COPY_BUFLEN);
343 if (readlen < 0) {
344 PERROR("Error reading input file");
8726a045
JR
345 ret = -1;
346 goto error;
d7ba1388
MD
347 }
348 if (!readlen) {
349 break;
350 }
351 writelen = lttng_write(fd_dest, buf, readlen);
352 if (writelen < readlen) {
353 PERROR("Error writing to output file");
8726a045
JR
354 ret = -1;
355 goto error;
356 }
357 }
358
359 ret = 0;
360error:
361 if (fd_src >= 0) {
362 if (close(fd_src) < 0) {
363 PERROR("Error closing %s", file_src);
364 }
365 }
366
367 if (fd_dest >= 0) {
368 if (close(fd_dest) < 0) {
369 PERROR("Error closing %s", file_dest);
d7ba1388
MD
370 }
371 }
8726a045 372 return ret;
d7ba1388
MD
373}
374
375static
376uint64_t _crash_get_field(const struct lttng_crash_layout *layout,
377 const char *ptr, size_t size)
378{
379 switch (size) {
380 case 1: return *(uint8_t *) ptr;
381 case 2: if (layout->reverse_byte_order) {
382 return __bswap_16(*(uint16_t *) ptr);
383 } else {
384 return *(uint16_t *) ptr;
385
386 }
387 case 4: if (layout->reverse_byte_order) {
388 return __bswap_32(*(uint32_t *) ptr);
389 } else {
390 return *(uint32_t *) ptr;
391
392 }
393 case 8: if (layout->reverse_byte_order) {
394 return __bswap_64(*(uint64_t *) ptr);
395 } else {
396 return *(uint64_t *) ptr;
397 }
398 default:
399 abort();
400 return -1;
401 }
402
403}
404
405#define crash_get_field(layout, map, name) \
406 _crash_get_field(layout, (map) + (layout)->offset.name, \
407 layout->length.name)
408
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)
415
416#define crash_get_hdr_raw_field(layout, hdr, name) ((hdr)->name)
417
418#define crash_get_hdr_field(layout, hdr, name) \
419 _crash_get_field(layout, (const char *) &(hdr)->name, \
420 sizeof((hdr)->name))
421
422#define crash_get_layout(layout, hdr, name) \
423 do { \
424 (layout)->name = crash_get_hdr_field(layout, hdr, \
425 name); \
426 DBG("layout.%s = %" PRIu64, #name, \
427 (uint64_t) (layout)->name); \
428 } while (0)
429
430static
431int get_crash_layout_0_0(struct lttng_crash_layout *layout,
432 char *map)
433{
434 const struct crash_abi_0_0 *abi = (const struct crash_abi_0_0 *) map;
435
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);
447
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);
456
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);
460
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);
465
466 return 0;
467}
468
469static
470void print_dbg_magic(const uint8_t *magic)
471{
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]);
477}
478
479static
480int check_magic(const uint8_t *magic)
481{
482 int i;
483
484 for (i = 0; i < RB_CRASH_DUMP_ABI_MAGIC_LEN; i++) {
485 if ((magic[i] ^ 0xFF) != lttng_crash_expected_magic_xor[i]) {
486 return -1;
487 }
488 }
489 return 0;
490}
491
492static
c2684eae
MD
493int get_crash_layout(struct lttng_crash_layout *layout, int fd,
494 const char *input_file)
d7ba1388
MD
495{
496 char *map;
497 int ret = 0, unmapret;
498 const uint8_t *magic;
499 uint64_t mmap_length;
500 uint16_t major, minor;
501 uint8_t word_size;
502 const struct crash_abi_unknown *abi;
503 uint16_t endian;
504 enum lttng_crash_type layout_type;
c2684eae 505 struct stat stat;
d7ba1388 506
c2684eae
MD
507 ret = fstat(fd, &stat);
508 if (ret < 0) {
509 PERROR("Failed to fstat '%s'", input_file);
510 return -1;
511 }
512 if (stat.st_size < RB_CRASH_DUMP_ABI_LEN) {
513 ERR("File '%s' truncated: file length of %" PRIi64
514 " bytes does not meet the minimal expected "
515 "length of %d bytes",
516 input_file, (int64_t) stat.st_size,
517 RB_CRASH_DUMP_ABI_LEN);
518 return -1;
519 }
d7ba1388
MD
520 map = mmap(NULL, RB_CRASH_DUMP_ABI_LEN, PROT_READ, MAP_PRIVATE,
521 fd, 0);
522 if (map == MAP_FAILED) {
523 PERROR("Mapping file");
524 return -1;
525 }
526 abi = (const struct crash_abi_unknown *) map;
527 magic = crash_get_hdr_raw_field(layout, abi, magic);
528 print_dbg_magic(magic);
529 if (check_magic(magic)) {
530 DBG("Unknown magic number");
531 ret = 1; /* positive return value, skip */
532 goto end;
533 }
534 endian = crash_get_hdr_field(layout, abi, endian);
535 switch (endian) {
536 case RB_CRASH_ENDIAN:
537 break;
538 case RB_CRASH_ENDIAN_REVERSE:
539 layout->reverse_byte_order = 1;
540 break;
541 default:
542 DBG("Unknown endianness value: 0x%X", (unsigned int) endian);
543 ret = 1; /* positive return value, skip */
544 goto end;
545 }
546 layout_type = (enum lttng_crash_type) crash_get_hdr_field(layout, abi, layout_type);
547 switch (layout_type) {
548 case LTTNG_CRASH_TYPE_UST:
549 break;
550 case LTTNG_CRASH_TYPE_KERNEL:
551 ERR("lttng-modules buffer layout support not implemented");
552 ret = 1; /* positive return value, skip */
553 goto end;
554 default:
555 ERR("Unknown layout type %u", (unsigned int) layout_type);
556 ret = 1; /* positive return value, skip */
557 goto end;
558 }
559 mmap_length = crash_get_hdr_field(layout, abi, mmap_length);
560 DBG("mmap_length: %" PRIu64, mmap_length);
561 layout->mmap_length = mmap_length;
562 major = crash_get_hdr_field(layout, abi, major);
563 DBG("major: %u", major);
564 minor = crash_get_hdr_field(layout, abi, minor);
565 DBG("minor: %u", minor);
566 word_size = crash_get_hdr_field(layout, abi, word_size);
567 DBG("word_size: %u", word_size);
568 switch (major) {
569 case 0:
570 switch (minor) {
571 case 0:
572 ret = get_crash_layout_0_0(layout, map);
573 if (ret)
574 goto end;
575 break;
576 default:
577 ret = -1;
578 ERR("Unsupported crash ABI %u.%u\n", major, minor);
579 goto end;
580 }
581 break;
582 default:
583 ERR("Unsupported crash ABI %u.%u\n", major, minor);
584 ret = -1;
585 goto end;
586 }
587 layout->word_size = word_size;
588end:
589 unmapret = munmap(map, RB_CRASH_DUMP_ABI_LEN);
590 if (unmapret) {
591 PERROR("munmap");
592 }
593 return ret;
594}
595
596/* buf_trunc mask selects only the buffer number. */
597static inline
598uint64_t buf_trunc(uint64_t offset, uint64_t buf_size)
599{
600 return offset & ~(buf_size - 1);
601}
602
603/* subbuf_trunc mask selects the subbuffer number. */
604static inline
605uint64_t subbuf_trunc(uint64_t offset, uint64_t subbuf_size)
606{
607 return offset & ~(subbuf_size - 1);
608}
609
610/* buf_offset mask selects only the offset within the current buffer. */
611static inline
612uint64_t buf_offset(uint64_t offset, uint64_t buf_size)
613{
614 return offset & (buf_size - 1);
615}
616
617/* subbuf_offset mask selects the offset within the current subbuffer. */
618static inline
619uint64_t subbuf_offset(uint64_t offset, uint64_t subbuf_size)
620{
621 return offset & (subbuf_size - 1);
622}
623
624/* subbuf_index returns the index of the current subbuffer within the buffer. */
625static inline
626uint64_t subbuf_index(uint64_t offset, uint64_t buf_size, uint64_t subbuf_size)
627{
628 return buf_offset(offset, buf_size) / subbuf_size;
629}
630
631static inline
632uint64_t subbuffer_id_get_index(uint32_t mode, uint64_t id,
633 unsigned int wl)
634{
635 if (mode == RING_BUFFER_OVERWRITE)
636 return id & SB_ID_INDEX_MASK(wl);
637 else
638 return id;
639}
640
641static
642int copy_crash_subbuf(const struct lttng_crash_layout *layout,
643 int fd_dest, char *buf, uint64_t offset)
644{
645 uint64_t buf_size, subbuf_size, num_subbuf, sbidx, id,
646 sb_bindex, rpages_offset, p_offset, seq_cc,
647 committed, commit_count_mask, consumed_cur,
648 packet_size;
649 char *subbuf_ptr;
650 ssize_t writelen;
651
652 /*
653 * Get the current subbuffer by applying the proper mask to
654 * "offset", and looking up the subbuf location within the
655 * source file buf.
656 */
657
658 buf_size = layout->buf_size;
659 subbuf_size = layout->subbuf_size;
660 num_subbuf = layout->num_subbuf;
661
662 switch (layout->word_size) {
663 case 4: commit_count_mask = 0xFFFFFFFFULL / num_subbuf;
664 break;
665 case 8: commit_count_mask = 0xFFFFFFFFFFFFFFFFULL / num_subbuf;
666 break;
667 default:
668 ERR("Unsupported word size: %u",
669 (unsigned int) layout->word_size);
670 return -EINVAL;
671 }
672
5f98d630 673 DBG("Copy crash subbuffer at offset %" PRIu64, offset);
d7ba1388
MD
674 sbidx = subbuf_index(offset, buf_size, subbuf_size);
675
676 /*
677 * Find where the seq cc is located. Compute length of data to
678 * copy.
679 */
680 seq_cc = crash_get_array_field(layout, buf, commit_hot_array,
681 sbidx, commit_hot_seq);
682 consumed_cur = crash_get_field(layout, buf, consumed_offset);
683
684 /*
685 * Check that the buffer we are getting is after or at
686 * consumed_cur position.
687 */
688 if ((long) subbuf_trunc(offset, subbuf_size)
689 - (long) subbuf_trunc(consumed_cur, subbuf_size) < 0) {
690 DBG("No data: position is before consumed_cur");
691 goto nodata;
692 }
693
694 /*
695 * Check if subbuffer has been fully committed.
696 */
697 if (((seq_cc - subbuf_size) & commit_count_mask)
698 - (buf_trunc(offset, buf_size) / num_subbuf)
699 == 0) {
700 committed = subbuf_size;
701 } else {
702 committed = subbuf_offset(seq_cc, subbuf_size);
703 if (!committed) {
704 DBG("No data committed, seq_cc: %" PRIu64, seq_cc);
705 goto nodata;
706 }
707 }
708
709 /* Find actual physical offset in subbuffer table */
710 id = crash_get_array_field(layout, buf, buf_wsb_array,
711 sbidx, buf_wsb_id);
712 sb_bindex = subbuffer_id_get_index(layout->mode, id,
713 layout->word_size);
714 rpages_offset = crash_get_array_field(layout, buf, sb_array,
715 sb_bindex, sb_array_shmp_offset);
716 p_offset = crash_get_field(layout, buf + rpages_offset,
717 sb_backend_p_offset);
718 subbuf_ptr = buf + p_offset;
719
720 if (committed == subbuf_size) {
721 /*
722 * Packet header can be used.
723 */
724 if (layout->length.packet_size) {
725 memcpy(&packet_size,
726 subbuf_ptr + layout->offset.packet_size,
727 layout->length.packet_size);
728 if (layout->reverse_byte_order) {
729 packet_size = __bswap_64(packet_size);
730 }
731 packet_size /= CHAR_BIT;
732 } else {
733 packet_size = subbuf_size;
734 }
735 } else {
736 uint64_t patch_size;
737
738 /*
739 * Find where to patch the sub-buffer header with actual
740 * readable data len and packet len, derived from seq
741 * cc. Patch it in our in-memory copy.
742 */
743 patch_size = committed * CHAR_BIT;
744 if (layout->reverse_byte_order) {
745 patch_size = __bswap_64(patch_size);
746 }
747 if (layout->length.content_size) {
748 memcpy(subbuf_ptr + layout->offset.content_size,
749 &patch_size, layout->length.content_size);
750 }
751 if (layout->length.packet_size) {
752 memcpy(subbuf_ptr + layout->offset.packet_size,
753 &patch_size, layout->length.packet_size);
754 }
755 packet_size = committed;
756 }
757
758 /*
759 * Copy packet into fd_dest.
760 */
761 writelen = lttng_write(fd_dest, subbuf_ptr, packet_size);
762 if (writelen < packet_size) {
763 PERROR("Error writing to output file");
764 return -1;
765 }
766 DBG("Copied %" PRIu64 " bytes of data", packet_size);
767 return 0;
768
769nodata:
770 return -ENODATA;
771}
772
773static
774int copy_crash_data(const struct lttng_crash_layout *layout, int fd_dest,
775 int fd_src)
776{
777 char *buf;
778 int ret = 0, has_data = 0;
779 struct stat statbuf;
780 size_t src_file_len;
781 uint64_t prod_offset, consumed_offset;
782 uint64_t offset, subbuf_size;
783 ssize_t readlen;
784
785 ret = fstat(fd_src, &statbuf);
786 if (ret) {
787 return ret;
788 }
789 src_file_len = layout->mmap_length;
790 buf = zmalloc(src_file_len);
791 if (!buf) {
792 return -1;
793 }
794 readlen = lttng_read(fd_src, buf, src_file_len);
795 if (readlen < 0) {
796 PERROR("Error reading input file");
309fc9bf
MD
797 ret = -1;
798 goto end;
d7ba1388
MD
799 }
800
801 prod_offset = crash_get_field(layout, buf, prod_offset);
802 DBG("prod_offset: 0x%" PRIx64, prod_offset);
803 consumed_offset = crash_get_field(layout, buf, consumed_offset);
804 DBG("consumed_offset: 0x%" PRIx64, consumed_offset);
805 subbuf_size = layout->subbuf_size;
806
807 for (offset = consumed_offset; offset < prod_offset;
808 offset += subbuf_size) {
809 ret = copy_crash_subbuf(layout, fd_dest, buf, offset);
810 if (!ret) {
811 has_data = 1;
812 }
813 if (ret) {
814 goto end;
815 }
816 }
817end:
818 free(buf);
819 if (ret && ret != -ENODATA) {
820 return ret;
821 }
822 if (has_data) {
823 return 0;
824 } else {
825 return -ENODATA;
826 }
827}
828
829static
830int extract_file(int output_dir_fd, const char *output_file,
831 int input_dir_fd, const char *input_file)
832{
833 int fd_dest, fd_src, ret = 0, closeret;
834 struct lttng_crash_layout layout;
835
836 layout.reverse_byte_order = 0; /* For reading magic number */
837
838 DBG("Extract file '%s'", input_file);
839 fd_src = openat(input_dir_fd, input_file, O_RDONLY);
840 if (fd_src < 0) {
841 PERROR("Error opening '%s' for reading",
842 input_file);
843 ret = -1;
844 goto end;
845 }
846
847 /* Query the crash ABI layout */
c2684eae 848 ret = get_crash_layout(&layout, fd_src, input_file);
d7ba1388
MD
849 if (ret) {
850 goto close_src;
851 }
852
853 fd_dest = openat(output_dir_fd, output_file,
854 O_RDWR | O_CREAT | O_EXCL,
855 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
856 if (fd_dest < 0) {
857 PERROR("Error opening '%s' for writing",
858 output_file);
859 ret = -1;
860 goto close_src;
861 }
862
863 ret = copy_crash_data(&layout, fd_dest, fd_src);
864 if (ret) {
865 goto close_dest;
866 }
867
868close_dest:
869 closeret = close(fd_dest);
870 if (closeret) {
871 PERROR("close");
872 }
873 if (ret == -ENODATA) {
874 closeret = unlinkat(output_dir_fd, output_file, 0);
875 if (closeret) {
876 PERROR("unlinkat");
877 }
878 }
879close_src:
880 closeret = close(fd_src);
881 if (closeret) {
882 PERROR("close");
883 }
884end:
885 return ret;
886}
887
888static
889int extract_all_files(const char *output_path,
890 const char *input_path)
891{
892 DIR *input_dir, *output_dir;
893 int input_dir_fd, output_dir_fd, ret = 0, closeret;
894 struct dirent *entry; /* input */
895
896 /* Open input directory */
897 input_dir = opendir(input_path);
898 if (!input_dir) {
899 PERROR("Cannot open '%s' path", input_path);
900 return -1;
901 }
902 input_dir_fd = dirfd(input_dir);
903 if (input_dir_fd < 0) {
904 PERROR("dirfd");
905 return -1;
906 }
907
908 /* Open output directory */
909 output_dir = opendir(output_path);
910 if (!output_dir) {
911 PERROR("Cannot open '%s' path", output_path);
912 return -1;
913 }
914 output_dir_fd = dirfd(output_dir);
915 if (output_dir_fd < 0) {
916 PERROR("dirfd");
917 return -1;
918 }
919
920 while ((entry = readdir(input_dir))) {
921 if (!strcmp(entry->d_name, ".")
922 || !strcmp(entry->d_name, ".."))
923 continue;
924 ret = extract_file(output_dir_fd, entry->d_name,
925 input_dir_fd, entry->d_name);
926 if (ret == -ENODATA) {
927 DBG("No data in file '%s', skipping", entry->d_name);
928 ret = 0;
929 continue;
930 } else if (ret < 0) {
931 break;
932 } else if (ret > 0) {
933 DBG("Skipping file '%s'", entry->d_name);
934 ret = 0;
935 continue;
936 }
937 }
938 closeret = closedir(output_dir);
939 if (closeret) {
940 PERROR("closedir");
941 }
942 closeret = closedir(input_dir);
943 if (closeret) {
944 PERROR("closedir");
945 }
946 return ret;
947}
948
949static
950int extract_one_trace(const char *output_path,
951 const char *input_path)
952{
953 char dest[PATH_MAX], src[PATH_MAX];
954 int ret;
955
956 DBG("Extract crash trace '%s' into '%s'", input_path, output_path);
957
958 /* Copy metadata */
959 strncpy(dest, output_path, PATH_MAX);
960 dest[PATH_MAX - 1] = '\0';
961 strncat(dest, "/metadata", PATH_MAX - strlen(dest) - 1);
962
963 strncpy(src, input_path, PATH_MAX);
964 src[PATH_MAX - 1] = '\0';
965 strncat(src, "/metadata", PATH_MAX - strlen(dest) - 1);
966
967 ret = copy_file(dest, src);
968 if (ret) {
969 return ret;
970 }
971
972 /* Extract each other file that has expected header */
973 return extract_all_files(output_path, input_path);
974}
975
976static
a424227e
MD
977int extract_trace_recursive(const char *output_path,
978 const char *input_path)
d7ba1388 979{
a424227e
MD
980 DIR *dir;
981 int dir_fd, ret = 0, closeret;
982 struct dirent *entry;
88ae485a 983 size_t path_len;
d7ba1388
MD
984 int has_warning = 0;
985
a424227e
MD
986 /* Open directory */
987 dir = opendir(input_path);
988 if (!dir) {
989 PERROR("Cannot open '%s' path", input_path);
990 return -1;
991 }
88ae485a
JR
992
993 path_len = strlen(input_path);
994
a424227e
MD
995 dir_fd = dirfd(dir);
996 if (dir_fd < 0) {
997 PERROR("dirfd");
d7ba1388
MD
998 return -1;
999 }
1000
a424227e 1001 while ((entry = readdir(dir))) {
88ae485a
JR
1002 struct stat st;
1003 size_t name_len;
1004 char filename[PATH_MAX];
1005
a424227e
MD
1006 if (!strcmp(entry->d_name, ".")
1007 || !strcmp(entry->d_name, "..")) {
1008 continue;
1009 }
88ae485a
JR
1010
1011 name_len = strlen(entry->d_name);
1012 if (path_len + name_len + 2 > sizeof(filename)) {
1013 ERR("Failed to remove file: path name too long (%s/%s)",
1014 input_path, entry->d_name);
1015 continue;
1016 }
1017
1018 if (snprintf(filename, sizeof(filename), "%s/%s",
1019 input_path, entry->d_name) < 0) {
1020 ERR("Failed to format path.");
1021 continue;
1022 }
1023
1024 if (stat(filename, &st)) {
1025 PERROR("stat");
1026 continue;
1027 }
1028
1029 if (S_ISDIR(st.st_mode)) {
a424227e
MD
1030 char output_subpath[PATH_MAX];
1031 char input_subpath[PATH_MAX];
1032
1033 strncpy(output_subpath, output_path,
1034 sizeof(output_subpath));
1035 output_subpath[sizeof(output_subpath) - 1] = '\0';
1036 strncat(output_subpath, "/",
1037 sizeof(output_subpath) - strlen(output_subpath) - 1);
1038 strncat(output_subpath, entry->d_name,
1039 sizeof(output_subpath) - strlen(output_subpath) - 1);
1040
1041 ret = mkdir(output_subpath, S_IRWXU | S_IRWXG);
1042 if (ret) {
1043 PERROR("mkdir");
1044 has_warning = 1;
1045 goto end;
1046 }
1047
1048 strncpy(input_subpath, input_path,
1049 sizeof(input_subpath));
1050 input_subpath[sizeof(input_subpath) - 1] = '\0';
1051 strncat(input_subpath, "/",
1052 sizeof(input_subpath) - strlen(input_subpath) - 1);
1053 strncat(input_subpath, entry->d_name,
1054 sizeof(input_subpath) - strlen(input_subpath) - 1);
1055
1056 ret = extract_trace_recursive(output_subpath,
1057 input_subpath);
29786dfa
JG
1058 if (ret) {
1059 has_warning = 1;
1060 }
88ae485a 1061 } else if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
a424227e
MD
1062 if (!strcmp(entry->d_name, "metadata")) {
1063 ret = extract_one_trace(output_path,
1064 input_path);
1065 if (ret) {
1066 WARN("Error extracting trace '%s', continuing anyway.",
1067 input_path);
1068 has_warning = 1;
1069 }
1070 }
88ae485a 1071 } else {
d7ba1388 1072 has_warning = 1;
a424227e 1073 goto end;
d7ba1388
MD
1074 }
1075 }
a424227e
MD
1076end:
1077 closeret = closedir(dir);
1078 if (closeret) {
1079 PERROR("closedir");
1080 }
d7ba1388
MD
1081 return has_warning;
1082}
1083
1084static
afe4fd7c 1085int delete_dir_recursive(const char *path)
d7ba1388 1086{
afe4fd7c
JR
1087 DIR *dir;
1088 int dir_fd, ret = 0, closeret;
88ae485a 1089 size_t path_len;
d7ba1388
MD
1090 struct dirent *entry;
1091
1092 /* Open trace directory */
afe4fd7c
JR
1093 dir = opendir(path);
1094 if (!dir) {
1095 PERROR("Cannot open '%s' path", path);
920a187e 1096 ret = -errno;
31ac82b0 1097 goto end_no_closedir;
d7ba1388 1098 }
88ae485a
JR
1099
1100 path_len = strlen(path);
1101
afe4fd7c
JR
1102 dir_fd = dirfd(dir);
1103 if (dir_fd < 0) {
d7ba1388 1104 PERROR("dirfd");
920a187e
JG
1105 ret = -errno;
1106 goto end;
d7ba1388
MD
1107 }
1108
afe4fd7c 1109 while ((entry = readdir(dir))) {
88ae485a
JR
1110 struct stat st;
1111 size_t name_len;
1112 char filename[PATH_MAX];
1113
d7ba1388 1114 if (!strcmp(entry->d_name, ".")
a424227e 1115 || !strcmp(entry->d_name, "..")) {
d7ba1388 1116 continue;
a424227e 1117 }
88ae485a
JR
1118
1119 name_len = strlen(entry->d_name);
1120 if (path_len + name_len + 2 > sizeof(filename)) {
1121 ERR("Failed to remove file: path name too long (%s/%s)",
1122 path, entry->d_name);
1123 continue;
1124 }
1125
1126 if (snprintf(filename, sizeof(filename), "%s/%s",
1127 path, entry->d_name) < 0) {
1128 ERR("Failed to format path.");
1129 continue;
1130 }
1131
1132 if (stat(filename, &st)) {
1133 PERROR("stat");
1134 continue;
1135 }
1136
1137 if (S_ISDIR(st.st_mode)) {
afe4fd7c
JR
1138 char *subpath = zmalloc(PATH_MAX);
1139
1140 if (!subpath) {
1141 PERROR("zmalloc path");
1142 ret = -1;
1143 goto end;
1144 }
1145 strncpy(subpath, path, PATH_MAX);
1146 subpath[PATH_MAX - 1] = '\0';
1147 strncat(subpath, "/",
1148 PATH_MAX - strlen(subpath) - 1);
1149 strncat(subpath, entry->d_name,
1150 PATH_MAX - strlen(subpath) - 1);
1151
1152 ret = delete_dir_recursive(subpath);
1153 free(subpath);
1154 if (ret) {
83f4233d 1155 /* Error occurred, abort traversal. */
afe4fd7c
JR
1156 goto end;
1157 }
88ae485a 1158 } else if (S_ISREG(st.st_mode)) {
afe4fd7c
JR
1159 ret = unlinkat(dir_fd, entry->d_name, 0);
1160 if (ret) {
1161 PERROR("Unlinking '%s'", entry->d_name);
1162 goto end;
1163 }
88ae485a 1164 } else {
d7ba1388
MD
1165 ret = -EINVAL;
1166 goto end;
1167 }
1168 }
1169end:
afe4fd7c
JR
1170 if (!ret) {
1171 ret = rmdir(path);
1172 if (ret) {
1173 PERROR("rmdir '%s'", path);
1174 }
1175 }
1176 closeret = closedir(dir);
d7ba1388
MD
1177 if (closeret) {
1178 PERROR("closedir");
1179 }
31ac82b0 1180end_no_closedir:
d7ba1388
MD
1181 return ret;
1182}
1183
d7ba1388
MD
1184static
1185int view_trace(const char *viewer_path, const char *trace_path)
1186{
1187 pid_t pid;
1188
1189 pid = fork();
1190 if (pid < 0) {
1191 /* Error */
1192 PERROR("fork");
1193 return -1;
1194 } else if (pid > 0) {
1195 /* Parent */
1196 int status;
1197
1198 pid = waitpid(pid, &status, 0);
1199 if (pid < 0 || !WIFEXITED(status)) {
1200 return -1;
1201 }
1202 } else {
1203 /* Child */
1204 int ret;
1205
1206 ret = execlp(viewer_path, viewer_path,
1207 trace_path, (char *) NULL);
1208 if (ret) {
1209 PERROR("execlp");
1210 exit(EXIT_FAILURE);
1211 }
1212 exit(EXIT_SUCCESS); /* Never reached */
1213 }
1214 return 0;
1215}
1216
1217/*
1218 * main
1219 */
1220int main(int argc, char *argv[])
1221{
aa25d686
JG
1222 int ret;
1223 bool has_warning = false;
d7ba1388
MD
1224 const char *output_path = NULL;
1225 char tmppath[] = "/tmp/lttng-crash-XXXXXX";
1226
1227 progname = argv[0] ? argv[0] : "lttng-crash";
1228
1229 ret = parse_args(argc, argv);
1230 if (ret > 0) {
aa25d686 1231 goto end;
d7ba1388 1232 } else if (ret < 0) {
aa25d686
JG
1233 has_warning = true;
1234 goto end;
d7ba1388
MD
1235 }
1236
1237 if (opt_output_path) {
1238 output_path = opt_output_path;
1239 ret = mkdir(output_path, S_IRWXU | S_IRWXG);
1240 if (ret) {
1241 PERROR("mkdir");
aa25d686
JG
1242 has_warning = true;
1243 goto end;
d7ba1388
MD
1244 }
1245 } else {
1246 output_path = mkdtemp(tmppath);
1247 if (!output_path) {
1248 PERROR("mkdtemp");
aa25d686
JG
1249 has_warning = true;
1250 goto end;
d7ba1388
MD
1251 }
1252 }
1253
a424227e 1254 ret = extract_trace_recursive(output_path, input_path);
d7ba1388 1255 if (ret < 0) {
aa25d686
JG
1256 has_warning = true;
1257 goto end;
d7ba1388 1258 } else if (ret > 0) {
aa25d686
JG
1259 /* extract_trace_recursive reported a warning. */
1260 has_warning = true;
d7ba1388
MD
1261 }
1262 if (!opt_output_path) {
1263 /* View trace */
1264 ret = view_trace(opt_viewer_path, output_path);
aa25d686
JG
1265 if (ret) {
1266 has_warning = true;
1267 }
d7ba1388 1268 /* unlink temporary trace */
afe4fd7c 1269 ret = delete_dir_recursive(output_path);
aa25d686
JG
1270 if (ret) {
1271 has_warning = true;
1272 }
d7ba1388 1273 }
aa25d686
JG
1274end:
1275 exit(has_warning ? EXIT_FAILURE : EXIT_SUCCESS);
d7ba1388 1276}
This page took 0.081121 seconds and 4 git commands to generate.