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