shm-path: remove directory hierarchy on destroy
[lttng-tools.git] / src / bin / lttng-crash / lttng-crash.c
CommitLineData
d7ba1388
MD
1/*
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
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
38#include <version.h>
39#include <lttng/lttng.h>
40#include <common/common.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/*
65 * Non-static to ensure the compiler does not optimize away the xor.
66 */
67uint8_t lttng_crash_expected_magic_xor[] = RB_CRASH_DUMP_ABI_MAGIC_XOR;
68
69#define RB_CRASH_ENDIAN 0x1234
70#define RB_CRASH_ENDIAN_REVERSE 0x3412
71
72enum lttng_crash_type {
73 LTTNG_CRASH_TYPE_UST = 0,
74 LTTNG_CRASH_TYPE_KERNEL = 1,
75};
76
77/* LTTng ring buffer defines (copied) */
78
79#define HALF_ULONG_BITS(wl) (((wl) * CHAR_BIT) >> 1)
80
81#define SB_ID_OFFSET_SHIFT(wl) (HALF_ULONG_BITS(wl) + 1)
82#define SB_ID_OFFSET_COUNT(wl) (1UL << SB_ID_OFFSET_SHIFT(wl))
83#define SB_ID_OFFSET_MASK(wl) (~(SB_ID_OFFSET_COUNT(wl) - 1))
84/*
85 * Lowest bit of top word half belongs to noref. Used only for overwrite mode.
86 */
87#define SB_ID_NOREF_SHIFT(wl) (SB_ID_OFFSET_SHIFT(wl) - 1)
88#define SB_ID_NOREF_COUNT(wl) (1UL << SB_ID_NOREF_SHIFT(wl))
89#define SB_ID_NOREF_MASK(wl) SB_ID_NOREF_COUNT(wl)
90/*
91 * In overwrite mode: lowest half of word is used for index.
92 * Limit of 2^16 subbuffers per buffer on 32-bit, 2^32 on 64-bit.
93 * In producer-consumer mode: whole word used for index.
94 */
95#define SB_ID_INDEX_SHIFT(wl) 0
96#define SB_ID_INDEX_COUNT(wl) (1UL << SB_ID_INDEX_SHIFT(wl))
97#define SB_ID_INDEX_MASK(wl) (SB_ID_NOREF_COUNT(wl) - 1)
98
99enum rb_modes {
100 RING_BUFFER_OVERWRITE = 0, /* Overwrite when buffer full */
101 RING_BUFFER_DISCARD = 1, /* Discard when buffer full */
102};
103
104struct crash_abi_unknown {
105 uint8_t magic[RB_CRASH_DUMP_ABI_MAGIC_LEN];
106 uint64_t mmap_length; /* Overall lenght of crash record */
107 uint16_t endian; /*
108 * { 0x12, 0x34 }: big endian
109 * { 0x34, 0x12 }: little endian
110 */
111 uint16_t major; /* Major number. */
112 uint16_t minor; /* Minor number. */
113 uint8_t word_size; /* Word size (bytes). */
114 uint8_t layout_type; /* enum lttng_crash_layout */
115} __attribute__((packed));
116
117struct crash_abi_0_0 {
118 struct crash_abi_unknown parent;
119
120 struct {
121 uint32_t prod_offset;
122 uint32_t consumed_offset;
123 uint32_t commit_hot_array;
124 uint32_t commit_hot_seq;
125 uint32_t buf_wsb_array;
126 uint32_t buf_wsb_id;
127 uint32_t sb_array;
128 uint32_t sb_array_shmp_offset;
129 uint32_t sb_backend_p_offset;
130 uint32_t content_size;
131 uint32_t packet_size;
132 } __attribute__((packed)) offset;
133 struct {
134 uint8_t prod_offset;
135 uint8_t consumed_offset;
136 uint8_t commit_hot_seq;
137 uint8_t buf_wsb_id;
138 uint8_t sb_array_shmp_offset;
139 uint8_t sb_backend_p_offset;
140 uint8_t content_size;
141 uint8_t packet_size;
142 } __attribute__((packed)) length;
143 struct {
144 uint32_t commit_hot_array;
145 uint32_t buf_wsb_array;
146 uint32_t sb_array;
147 } __attribute__((packed)) stride;
148
149 uint64_t buf_size; /* Size of the buffer */
150 uint64_t subbuf_size; /* Sub-buffer size */
151 uint64_t num_subbuf; /* Number of sub-buffers for writer */
152 uint32_t mode; /* Buffer mode: 0: overwrite, 1: discard */
153} __attribute__((packed));
154
155struct lttng_crash_layout {
156 struct {
157 int prod_offset, consumed_offset,
158 commit_hot_array, commit_hot_seq,
159 buf_wsb_array, buf_wsb_id,
160 sb_array, sb_array_shmp_offset,
161 sb_backend_p_offset, content_size,
162 packet_size;
163 } offset;
164 struct {
165 int prod_offset, consumed_offset,
166 commit_hot_seq, buf_wsb_id,
167 sb_array_shmp_offset, sb_backend_p_offset,
168 content_size, packet_size;
169 } length;
170 struct {
171 int commit_hot_array, buf_wsb_array, sb_array;
172 } stride;
173
174 int reverse_byte_order;
175 int word_size;
176
177 uint64_t mmap_length; /* Length of crash record */
178 uint64_t buf_size; /* Size of the buffer */
179 uint64_t subbuf_size; /* Sub-buffer size */
180 uint64_t num_subbuf; /* Number of sub-buffers for writer */
181 uint32_t mode; /* Buffer mode: 0: overwrite, 1: discard */
182};
183
184/* Variables */
185static char *progname,
186 *opt_viewer_path = DEFAULT_VIEWER,
187 *opt_output_path;
188
189static int nr_input_paths;
190static char **input_paths;
191
192int lttng_opt_quiet, lttng_opt_verbose, lttng_opt_mi;
193
194enum {
195 OPT_DUMP_OPTIONS,
196};
197
198/* Getopt options. No first level command. */
199static 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
209static 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 [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
232static 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 */
246static 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 */
269static 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, "+Vhvex:", 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 opt_viewer_path = strdup(optarg);
296 break;
297 case 'x':
298 opt_output_path = strdup(optarg);
299 break;
300 case OPT_DUMP_OPTIONS:
301 list_options(stdout);
302 ret = 1;
303 goto end;
304 default:
305 usage(stderr);
306 goto error;
307 }
308 }
309
310 /* No leftovers, print usage and quit */
311 if ((argc - optind) == 0) {
312 usage(stderr);
313 goto error;
314 }
315
316 input_paths = &argv[optind];
317 nr_input_paths = argc - optind;
318end:
319 return ret;
320
321error:
322 return -1;
323}
324
325static
326int copy_file(const char *file_dest, const char *file_src)
327{
328 int fd_src, fd_dest;
329 ssize_t readlen, writelen;
330 char buf[COPY_BUFLEN];
331
332 DBG("Copy metadata file '%s' into '%s'", file_src, file_dest);
333
334 fd_src = open(file_src, O_RDONLY);
335 if (fd_src < 0) {
336 PERROR("Error opening %s for reading", file_src);
337 return fd_src;
338 }
339 fd_dest = open(file_dest, O_RDWR | O_CREAT | O_EXCL,
340 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
341 if (fd_dest < 0) {
342 PERROR("Error opening %s for writing", file_dest);
343 return fd_dest;
344 }
345
346 for (;;) {
347 readlen = lttng_read(fd_src, buf, COPY_BUFLEN);
348 if (readlen < 0) {
349 PERROR("Error reading input file");
350 return -1;
351 }
352 if (!readlen) {
353 break;
354 }
355 writelen = lttng_write(fd_dest, buf, readlen);
356 if (writelen < readlen) {
357 PERROR("Error writing to output file");
358 return -1;
359 }
360 }
361 return 0;
362}
363
364static
365uint64_t _crash_get_field(const struct lttng_crash_layout *layout,
366 const char *ptr, size_t size)
367{
368 switch (size) {
369 case 1: return *(uint8_t *) ptr;
370 case 2: if (layout->reverse_byte_order) {
371 return __bswap_16(*(uint16_t *) ptr);
372 } else {
373 return *(uint16_t *) ptr;
374
375 }
376 case 4: if (layout->reverse_byte_order) {
377 return __bswap_32(*(uint32_t *) ptr);
378 } else {
379 return *(uint32_t *) ptr;
380
381 }
382 case 8: if (layout->reverse_byte_order) {
383 return __bswap_64(*(uint64_t *) ptr);
384 } else {
385 return *(uint64_t *) ptr;
386 }
387 default:
388 abort();
389 return -1;
390 }
391
392}
393
394#define crash_get_field(layout, map, name) \
395 _crash_get_field(layout, (map) + (layout)->offset.name, \
396 layout->length.name)
397
398#define crash_get_array_field(layout, map, array_name, idx, field_name) \
399 _crash_get_field(layout, \
400 (map) + (layout)->offset.array_name \
401 + (idx * (layout)->stride.array_name) \
402 + (layout)->offset.field_name, \
403 (layout)->length.field_name)
404
405#define crash_get_hdr_raw_field(layout, hdr, name) ((hdr)->name)
406
407#define crash_get_hdr_field(layout, hdr, name) \
408 _crash_get_field(layout, (const char *) &(hdr)->name, \
409 sizeof((hdr)->name))
410
411#define crash_get_layout(layout, hdr, name) \
412 do { \
413 (layout)->name = crash_get_hdr_field(layout, hdr, \
414 name); \
415 DBG("layout.%s = %" PRIu64, #name, \
416 (uint64_t) (layout)->name); \
417 } while (0)
418
419static
420int get_crash_layout_0_0(struct lttng_crash_layout *layout,
421 char *map)
422{
423 const struct crash_abi_0_0 *abi = (const struct crash_abi_0_0 *) map;
424
425 crash_get_layout(layout, abi, offset.prod_offset);
426 crash_get_layout(layout, abi, offset.consumed_offset);
427 crash_get_layout(layout, abi, offset.commit_hot_array);
428 crash_get_layout(layout, abi, offset.commit_hot_seq);
429 crash_get_layout(layout, abi, offset.buf_wsb_array);
430 crash_get_layout(layout, abi, offset.buf_wsb_id);
431 crash_get_layout(layout, abi, offset.sb_array);
432 crash_get_layout(layout, abi, offset.sb_array_shmp_offset);
433 crash_get_layout(layout, abi, offset.sb_backend_p_offset);
434 crash_get_layout(layout, abi, offset.content_size);
435 crash_get_layout(layout, abi, offset.packet_size);
436
437 crash_get_layout(layout, abi, length.prod_offset);
438 crash_get_layout(layout, abi, length.consumed_offset);
439 crash_get_layout(layout, abi, length.commit_hot_seq);
440 crash_get_layout(layout, abi, length.buf_wsb_id);
441 crash_get_layout(layout, abi, length.sb_array_shmp_offset);
442 crash_get_layout(layout, abi, length.sb_backend_p_offset);
443 crash_get_layout(layout, abi, length.content_size);
444 crash_get_layout(layout, abi, length.packet_size);
445
446 crash_get_layout(layout, abi, stride.commit_hot_array);
447 crash_get_layout(layout, abi, stride.buf_wsb_array);
448 crash_get_layout(layout, abi, stride.sb_array);
449
450 crash_get_layout(layout, abi, buf_size);
451 crash_get_layout(layout, abi, subbuf_size);
452 crash_get_layout(layout, abi, num_subbuf);
453 crash_get_layout(layout, abi, mode);
454
455 return 0;
456}
457
458static
459void print_dbg_magic(const uint8_t *magic)
460{
461 DBG("magic: 0x%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X",
462 magic[0], magic[1], magic[2], magic[3],
463 magic[4], magic[5], magic[6], magic[7],
464 magic[8], magic[9], magic[10], magic[11],
465 magic[12], magic[13], magic[14], magic[15]);
466}
467
468static
469int check_magic(const uint8_t *magic)
470{
471 int i;
472
473 for (i = 0; i < RB_CRASH_DUMP_ABI_MAGIC_LEN; i++) {
474 if ((magic[i] ^ 0xFF) != lttng_crash_expected_magic_xor[i]) {
475 return -1;
476 }
477 }
478 return 0;
479}
480
481static
482int get_crash_layout(struct lttng_crash_layout *layout, int fd)
483{
484 char *map;
485 int ret = 0, unmapret;
486 const uint8_t *magic;
487 uint64_t mmap_length;
488 uint16_t major, minor;
489 uint8_t word_size;
490 const struct crash_abi_unknown *abi;
491 uint16_t endian;
492 enum lttng_crash_type layout_type;
493
494 map = mmap(NULL, RB_CRASH_DUMP_ABI_LEN, PROT_READ, MAP_PRIVATE,
495 fd, 0);
496 if (map == MAP_FAILED) {
497 PERROR("Mapping file");
498 return -1;
499 }
500 abi = (const struct crash_abi_unknown *) map;
501 magic = crash_get_hdr_raw_field(layout, abi, magic);
502 print_dbg_magic(magic);
503 if (check_magic(magic)) {
504 DBG("Unknown magic number");
505 ret = 1; /* positive return value, skip */
506 goto end;
507 }
508 endian = crash_get_hdr_field(layout, abi, endian);
509 switch (endian) {
510 case RB_CRASH_ENDIAN:
511 break;
512 case RB_CRASH_ENDIAN_REVERSE:
513 layout->reverse_byte_order = 1;
514 break;
515 default:
516 DBG("Unknown endianness value: 0x%X", (unsigned int) endian);
517 ret = 1; /* positive return value, skip */
518 goto end;
519 }
520 layout_type = (enum lttng_crash_type) crash_get_hdr_field(layout, abi, layout_type);
521 switch (layout_type) {
522 case LTTNG_CRASH_TYPE_UST:
523 break;
524 case LTTNG_CRASH_TYPE_KERNEL:
525 ERR("lttng-modules buffer layout support not implemented");
526 ret = 1; /* positive return value, skip */
527 goto end;
528 default:
529 ERR("Unknown layout type %u", (unsigned int) layout_type);
530 ret = 1; /* positive return value, skip */
531 goto end;
532 }
533 mmap_length = crash_get_hdr_field(layout, abi, mmap_length);
534 DBG("mmap_length: %" PRIu64, mmap_length);
535 layout->mmap_length = mmap_length;
536 major = crash_get_hdr_field(layout, abi, major);
537 DBG("major: %u", major);
538 minor = crash_get_hdr_field(layout, abi, minor);
539 DBG("minor: %u", minor);
540 word_size = crash_get_hdr_field(layout, abi, word_size);
541 DBG("word_size: %u", word_size);
542 switch (major) {
543 case 0:
544 switch (minor) {
545 case 0:
546 ret = get_crash_layout_0_0(layout, map);
547 if (ret)
548 goto end;
549 break;
550 default:
551 ret = -1;
552 ERR("Unsupported crash ABI %u.%u\n", major, minor);
553 goto end;
554 }
555 break;
556 default:
557 ERR("Unsupported crash ABI %u.%u\n", major, minor);
558 ret = -1;
559 goto end;
560 }
561 layout->word_size = word_size;
562end:
563 unmapret = munmap(map, RB_CRASH_DUMP_ABI_LEN);
564 if (unmapret) {
565 PERROR("munmap");
566 }
567 return ret;
568}
569
570/* buf_trunc mask selects only the buffer number. */
571static inline
572uint64_t buf_trunc(uint64_t offset, uint64_t buf_size)
573{
574 return offset & ~(buf_size - 1);
575}
576
577/* subbuf_trunc mask selects the subbuffer number. */
578static inline
579uint64_t subbuf_trunc(uint64_t offset, uint64_t subbuf_size)
580{
581 return offset & ~(subbuf_size - 1);
582}
583
584/* buf_offset mask selects only the offset within the current buffer. */
585static inline
586uint64_t buf_offset(uint64_t offset, uint64_t buf_size)
587{
588 return offset & (buf_size - 1);
589}
590
591/* subbuf_offset mask selects the offset within the current subbuffer. */
592static inline
593uint64_t subbuf_offset(uint64_t offset, uint64_t subbuf_size)
594{
595 return offset & (subbuf_size - 1);
596}
597
598/* subbuf_index returns the index of the current subbuffer within the buffer. */
599static inline
600uint64_t subbuf_index(uint64_t offset, uint64_t buf_size, uint64_t subbuf_size)
601{
602 return buf_offset(offset, buf_size) / subbuf_size;
603}
604
605static inline
606uint64_t subbuffer_id_get_index(uint32_t mode, uint64_t id,
607 unsigned int wl)
608{
609 if (mode == RING_BUFFER_OVERWRITE)
610 return id & SB_ID_INDEX_MASK(wl);
611 else
612 return id;
613}
614
615static
616int copy_crash_subbuf(const struct lttng_crash_layout *layout,
617 int fd_dest, char *buf, uint64_t offset)
618{
619 uint64_t buf_size, subbuf_size, num_subbuf, sbidx, id,
620 sb_bindex, rpages_offset, p_offset, seq_cc,
621 committed, commit_count_mask, consumed_cur,
622 packet_size;
623 char *subbuf_ptr;
624 ssize_t writelen;
625
626 /*
627 * Get the current subbuffer by applying the proper mask to
628 * "offset", and looking up the subbuf location within the
629 * source file buf.
630 */
631
632 buf_size = layout->buf_size;
633 subbuf_size = layout->subbuf_size;
634 num_subbuf = layout->num_subbuf;
635
636 switch (layout->word_size) {
637 case 4: commit_count_mask = 0xFFFFFFFFULL / num_subbuf;
638 break;
639 case 8: commit_count_mask = 0xFFFFFFFFFFFFFFFFULL / num_subbuf;
640 break;
641 default:
642 ERR("Unsupported word size: %u",
643 (unsigned int) layout->word_size);
644 return -EINVAL;
645 }
646
647 DBG("Copy crash subbuffer at offset %lu", offset);
648 sbidx = subbuf_index(offset, buf_size, subbuf_size);
649
650 /*
651 * Find where the seq cc is located. Compute length of data to
652 * copy.
653 */
654 seq_cc = crash_get_array_field(layout, buf, commit_hot_array,
655 sbidx, commit_hot_seq);
656 consumed_cur = crash_get_field(layout, buf, consumed_offset);
657
658 /*
659 * Check that the buffer we are getting is after or at
660 * consumed_cur position.
661 */
662 if ((long) subbuf_trunc(offset, subbuf_size)
663 - (long) subbuf_trunc(consumed_cur, subbuf_size) < 0) {
664 DBG("No data: position is before consumed_cur");
665 goto nodata;
666 }
667
668 /*
669 * Check if subbuffer has been fully committed.
670 */
671 if (((seq_cc - subbuf_size) & commit_count_mask)
672 - (buf_trunc(offset, buf_size) / num_subbuf)
673 == 0) {
674 committed = subbuf_size;
675 } else {
676 committed = subbuf_offset(seq_cc, subbuf_size);
677 if (!committed) {
678 DBG("No data committed, seq_cc: %" PRIu64, seq_cc);
679 goto nodata;
680 }
681 }
682
683 /* Find actual physical offset in subbuffer table */
684 id = crash_get_array_field(layout, buf, buf_wsb_array,
685 sbidx, buf_wsb_id);
686 sb_bindex = subbuffer_id_get_index(layout->mode, id,
687 layout->word_size);
688 rpages_offset = crash_get_array_field(layout, buf, sb_array,
689 sb_bindex, sb_array_shmp_offset);
690 p_offset = crash_get_field(layout, buf + rpages_offset,
691 sb_backend_p_offset);
692 subbuf_ptr = buf + p_offset;
693
694 if (committed == subbuf_size) {
695 /*
696 * Packet header can be used.
697 */
698 if (layout->length.packet_size) {
699 memcpy(&packet_size,
700 subbuf_ptr + layout->offset.packet_size,
701 layout->length.packet_size);
702 if (layout->reverse_byte_order) {
703 packet_size = __bswap_64(packet_size);
704 }
705 packet_size /= CHAR_BIT;
706 } else {
707 packet_size = subbuf_size;
708 }
709 } else {
710 uint64_t patch_size;
711
712 /*
713 * Find where to patch the sub-buffer header with actual
714 * readable data len and packet len, derived from seq
715 * cc. Patch it in our in-memory copy.
716 */
717 patch_size = committed * CHAR_BIT;
718 if (layout->reverse_byte_order) {
719 patch_size = __bswap_64(patch_size);
720 }
721 if (layout->length.content_size) {
722 memcpy(subbuf_ptr + layout->offset.content_size,
723 &patch_size, layout->length.content_size);
724 }
725 if (layout->length.packet_size) {
726 memcpy(subbuf_ptr + layout->offset.packet_size,
727 &patch_size, layout->length.packet_size);
728 }
729 packet_size = committed;
730 }
731
732 /*
733 * Copy packet into fd_dest.
734 */
735 writelen = lttng_write(fd_dest, subbuf_ptr, packet_size);
736 if (writelen < packet_size) {
737 PERROR("Error writing to output file");
738 return -1;
739 }
740 DBG("Copied %" PRIu64 " bytes of data", packet_size);
741 return 0;
742
743nodata:
744 return -ENODATA;
745}
746
747static
748int copy_crash_data(const struct lttng_crash_layout *layout, int fd_dest,
749 int fd_src)
750{
751 char *buf;
752 int ret = 0, has_data = 0;
753 struct stat statbuf;
754 size_t src_file_len;
755 uint64_t prod_offset, consumed_offset;
756 uint64_t offset, subbuf_size;
757 ssize_t readlen;
758
759 ret = fstat(fd_src, &statbuf);
760 if (ret) {
761 return ret;
762 }
763 src_file_len = layout->mmap_length;
764 buf = zmalloc(src_file_len);
765 if (!buf) {
766 return -1;
767 }
768 readlen = lttng_read(fd_src, buf, src_file_len);
769 if (readlen < 0) {
770 PERROR("Error reading input file");
771 return -1;
772 }
773
774 prod_offset = crash_get_field(layout, buf, prod_offset);
775 DBG("prod_offset: 0x%" PRIx64, prod_offset);
776 consumed_offset = crash_get_field(layout, buf, consumed_offset);
777 DBG("consumed_offset: 0x%" PRIx64, consumed_offset);
778 subbuf_size = layout->subbuf_size;
779
780 for (offset = consumed_offset; offset < prod_offset;
781 offset += subbuf_size) {
782 ret = copy_crash_subbuf(layout, fd_dest, buf, offset);
783 if (!ret) {
784 has_data = 1;
785 }
786 if (ret) {
787 goto end;
788 }
789 }
790end:
791 free(buf);
792 if (ret && ret != -ENODATA) {
793 return ret;
794 }
795 if (has_data) {
796 return 0;
797 } else {
798 return -ENODATA;
799 }
800}
801
802static
803int extract_file(int output_dir_fd, const char *output_file,
804 int input_dir_fd, const char *input_file)
805{
806 int fd_dest, fd_src, ret = 0, closeret;
807 struct lttng_crash_layout layout;
808
809 layout.reverse_byte_order = 0; /* For reading magic number */
810
811 DBG("Extract file '%s'", input_file);
812 fd_src = openat(input_dir_fd, input_file, O_RDONLY);
813 if (fd_src < 0) {
814 PERROR("Error opening '%s' for reading",
815 input_file);
816 ret = -1;
817 goto end;
818 }
819
820 /* Query the crash ABI layout */
821 ret = get_crash_layout(&layout, fd_src);
822 if (ret) {
823 goto close_src;
824 }
825
826 fd_dest = openat(output_dir_fd, output_file,
827 O_RDWR | O_CREAT | O_EXCL,
828 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
829 if (fd_dest < 0) {
830 PERROR("Error opening '%s' for writing",
831 output_file);
832 ret = -1;
833 goto close_src;
834 }
835
836 ret = copy_crash_data(&layout, fd_dest, fd_src);
837 if (ret) {
838 goto close_dest;
839 }
840
841close_dest:
842 closeret = close(fd_dest);
843 if (closeret) {
844 PERROR("close");
845 }
846 if (ret == -ENODATA) {
847 closeret = unlinkat(output_dir_fd, output_file, 0);
848 if (closeret) {
849 PERROR("unlinkat");
850 }
851 }
852close_src:
853 closeret = close(fd_src);
854 if (closeret) {
855 PERROR("close");
856 }
857end:
858 return ret;
859}
860
861static
862int extract_all_files(const char *output_path,
863 const char *input_path)
864{
865 DIR *input_dir, *output_dir;
866 int input_dir_fd, output_dir_fd, ret = 0, closeret;
867 struct dirent *entry; /* input */
868
869 /* Open input directory */
870 input_dir = opendir(input_path);
871 if (!input_dir) {
872 PERROR("Cannot open '%s' path", input_path);
873 return -1;
874 }
875 input_dir_fd = dirfd(input_dir);
876 if (input_dir_fd < 0) {
877 PERROR("dirfd");
878 return -1;
879 }
880
881 /* Open output directory */
882 output_dir = opendir(output_path);
883 if (!output_dir) {
884 PERROR("Cannot open '%s' path", output_path);
885 return -1;
886 }
887 output_dir_fd = dirfd(output_dir);
888 if (output_dir_fd < 0) {
889 PERROR("dirfd");
890 return -1;
891 }
892
893 while ((entry = readdir(input_dir))) {
894 if (!strcmp(entry->d_name, ".")
895 || !strcmp(entry->d_name, ".."))
896 continue;
897 ret = extract_file(output_dir_fd, entry->d_name,
898 input_dir_fd, entry->d_name);
899 if (ret == -ENODATA) {
900 DBG("No data in file '%s', skipping", entry->d_name);
901 ret = 0;
902 continue;
903 } else if (ret < 0) {
904 break;
905 } else if (ret > 0) {
906 DBG("Skipping file '%s'", entry->d_name);
907 ret = 0;
908 continue;
909 }
910 }
911 closeret = closedir(output_dir);
912 if (closeret) {
913 PERROR("closedir");
914 }
915 closeret = closedir(input_dir);
916 if (closeret) {
917 PERROR("closedir");
918 }
919 return ret;
920}
921
922static
923int extract_one_trace(const char *output_path,
924 const char *input_path)
925{
926 char dest[PATH_MAX], src[PATH_MAX];
927 int ret;
928
929 DBG("Extract crash trace '%s' into '%s'", input_path, output_path);
930
931 /* Copy metadata */
932 strncpy(dest, output_path, PATH_MAX);
933 dest[PATH_MAX - 1] = '\0';
934 strncat(dest, "/metadata", PATH_MAX - strlen(dest) - 1);
935
936 strncpy(src, input_path, PATH_MAX);
937 src[PATH_MAX - 1] = '\0';
938 strncat(src, "/metadata", PATH_MAX - strlen(dest) - 1);
939
940 ret = copy_file(dest, src);
941 if (ret) {
942 return ret;
943 }
944
945 /* Extract each other file that has expected header */
946 return extract_all_files(output_path, input_path);
947}
948
949static
950int extract_all_traces(const char *output_path,
951 char **input_paths,
952 int nr_input_paths)
953{
954 int ret, i;
955 int has_warning = 0;
956
957 /* Only one input path is currently supported */
958 if (nr_input_paths != 1) {
959 ERR("Only a single input path is currently supported.\n");
960 return -1;
961 }
962
963 for (i = 0; i < nr_input_paths; i++) {
964 ret = extract_one_trace(output_path,
965 input_paths[i]);
966 if (ret) {
967 WARN("Error extracting trace '%s', continuing anyway.",
968 input_paths[i]);
969 has_warning = 1;
970 }
971 }
972 return has_warning;
973}
974
975static
976int delete_trace(const char *trace_path)
977{
978 DIR *trace_dir;
979 int trace_dir_fd, ret = 0, closeret;
980 struct dirent *entry;
981
982 /* Open trace directory */
983 trace_dir = opendir(trace_path);
984 if (!trace_dir) {
985 PERROR("Cannot open '%s' path", trace_path);
986 return -1;
987 }
988 trace_dir_fd = dirfd(trace_dir);
989 if (trace_dir_fd < 0) {
990 PERROR("dirfd");
991 return -1;
992 }
993
994 while ((entry = readdir(trace_dir))) {
995 if (!strcmp(entry->d_name, ".")
996 || !strcmp(entry->d_name, ".."))
997 continue;
998 switch (entry->d_type) {
999 case DT_DIR:
1000 unlinkat(trace_dir_fd, entry->d_name, AT_REMOVEDIR);
1001 break;
1002 case DT_REG:
1003 unlinkat(trace_dir_fd, entry->d_name, 0);
1004 break;
1005 default:
1006 ret = -EINVAL;
1007 goto end;
1008 }
1009 }
1010end:
1011 closeret = closedir(trace_dir);
1012 if (closeret) {
1013 PERROR("closedir");
1014 }
1015 if (!ret) {
1016 ret = rmdir(trace_path);
1017 }
1018 return ret;
1019}
1020
1021
1022static
1023int view_trace(const char *viewer_path, const char *trace_path)
1024{
1025 pid_t pid;
1026
1027 pid = fork();
1028 if (pid < 0) {
1029 /* Error */
1030 PERROR("fork");
1031 return -1;
1032 } else if (pid > 0) {
1033 /* Parent */
1034 int status;
1035
1036 pid = waitpid(pid, &status, 0);
1037 if (pid < 0 || !WIFEXITED(status)) {
1038 return -1;
1039 }
1040 } else {
1041 /* Child */
1042 int ret;
1043
1044 ret = execlp(viewer_path, viewer_path,
1045 trace_path, (char *) NULL);
1046 if (ret) {
1047 PERROR("execlp");
1048 exit(EXIT_FAILURE);
1049 }
1050 exit(EXIT_SUCCESS); /* Never reached */
1051 }
1052 return 0;
1053}
1054
1055/*
1056 * main
1057 */
1058int main(int argc, char *argv[])
1059{
1060 int ret, has_warning = 0;
1061 const char *output_path = NULL;
1062 char tmppath[] = "/tmp/lttng-crash-XXXXXX";
1063
1064 progname = argv[0] ? argv[0] : "lttng-crash";
1065
1066 ret = parse_args(argc, argv);
1067 if (ret > 0) {
1068 exit(EXIT_SUCCESS);
1069 } else if (ret < 0) {
1070 exit(EXIT_FAILURE);
1071 }
1072
1073 if (opt_output_path) {
1074 output_path = opt_output_path;
1075 ret = mkdir(output_path, S_IRWXU | S_IRWXG);
1076 if (ret) {
1077 PERROR("mkdir");
1078 exit(EXIT_FAILURE);
1079 }
1080 } else {
1081 output_path = mkdtemp(tmppath);
1082 if (!output_path) {
1083 PERROR("mkdtemp");
1084 exit(EXIT_FAILURE);
1085 }
1086 }
1087
1088 ret = extract_all_traces(output_path, input_paths,
1089 nr_input_paths);
1090 if (ret < 0) {
1091 exit(EXIT_FAILURE);
1092 } else if (ret > 0) {
1093 has_warning = 1;
1094 }
1095 if (!opt_output_path) {
1096 /* View trace */
1097 ret = view_trace(opt_viewer_path, output_path);
1098 if (ret)
1099 has_warning = 1;
1100
1101 /* unlink temporary trace */
1102 ret = delete_trace(output_path);
1103 if (ret)
1104 has_warning = 1;
1105 }
1106 if (has_warning)
1107 exit(EXIT_FAILURE);
1108 exit(EXIT_SUCCESS);
1109}
This page took 0.10224 seconds and 4 git commands to generate.