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