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