Fix: lttng-crash: segfault when parsing options
[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>
aa25d686 37#include <stdbool.h>
d7ba1388
MD
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 */
68uint8_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
73enum 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
100enum rb_modes {
101 RING_BUFFER_OVERWRITE = 0, /* Overwrite when buffer full */
102 RING_BUFFER_DISCARD = 1, /* Discard when buffer full */
103};
104
105struct crash_abi_unknown {
106 uint8_t magic[RB_CRASH_DUMP_ABI_MAGIC_LEN];
8726a045 107 uint64_t mmap_length; /* Overall length of crash record */
d7ba1388
MD
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
118struct 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
156struct 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 */
186static char *progname,
187 *opt_viewer_path = DEFAULT_VIEWER,
188 *opt_output_path;
189
a424227e 190static char *input_path;
d7ba1388
MD
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);
acade31f 213 fprintf(ofp, "usage: lttng-crash [OPTIONS] FILE\n");
d7ba1388
MD
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
d07f3d99 278 while ((opt = getopt_long(argc, argv, "+Vhve:x:", long_options, NULL)) != -1) {
d7ba1388
MD
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
a424227e
MD
310 /* No leftovers, or more than one input path, print usage and quit */
311 if ((argc - optind) == 0 || (argc - optind) > 1) {
d7ba1388
MD
312 usage(stderr);
313 goto error;
314 }
315
a424227e 316 input_path = argv[optind];
d7ba1388
MD
317end:
318 return ret;
319
320error:
321 return -1;
322}
323
324static
325int copy_file(const char *file_dest, const char *file_src)
326{
8726a045 327 int fd_src = -1, fd_dest = -1;
d7ba1388
MD
328 ssize_t readlen, writelen;
329 char buf[COPY_BUFLEN];
8726a045 330 int ret;
d7ba1388
MD
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);
8726a045
JR
337 ret = -errno;
338 goto error;
d7ba1388
MD
339 }
340 fd_dest = open(file_dest, O_RDWR | O_CREAT | O_EXCL,
341 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
342 if (fd_dest < 0) {
343 PERROR("Error opening %s for writing", file_dest);
8726a045
JR
344 ret = -errno;
345 goto error;
d7ba1388
MD
346 }
347
348 for (;;) {
349 readlen = lttng_read(fd_src, buf, COPY_BUFLEN);
350 if (readlen < 0) {
351 PERROR("Error reading input file");
8726a045
JR
352 ret = -1;
353 goto error;
d7ba1388
MD
354 }
355 if (!readlen) {
356 break;
357 }
358 writelen = lttng_write(fd_dest, buf, readlen);
359 if (writelen < readlen) {
360 PERROR("Error writing to output file");
8726a045
JR
361 ret = -1;
362 goto error;
363 }
364 }
365
366 ret = 0;
367error:
368 if (fd_src >= 0) {
369 if (close(fd_src) < 0) {
370 PERROR("Error closing %s", file_src);
371 }
372 }
373
374 if (fd_dest >= 0) {
375 if (close(fd_dest) < 0) {
376 PERROR("Error closing %s", file_dest);
d7ba1388
MD
377 }
378 }
8726a045 379 return ret;
d7ba1388
MD
380}
381
382static
383uint64_t _crash_get_field(const struct lttng_crash_layout *layout,
384 const char *ptr, size_t size)
385{
386 switch (size) {
387 case 1: return *(uint8_t *) ptr;
388 case 2: if (layout->reverse_byte_order) {
389 return __bswap_16(*(uint16_t *) ptr);
390 } else {
391 return *(uint16_t *) ptr;
392
393 }
394 case 4: if (layout->reverse_byte_order) {
395 return __bswap_32(*(uint32_t *) ptr);
396 } else {
397 return *(uint32_t *) ptr;
398
399 }
400 case 8: if (layout->reverse_byte_order) {
401 return __bswap_64(*(uint64_t *) ptr);
402 } else {
403 return *(uint64_t *) ptr;
404 }
405 default:
406 abort();
407 return -1;
408 }
409
410}
411
412#define crash_get_field(layout, map, name) \
413 _crash_get_field(layout, (map) + (layout)->offset.name, \
414 layout->length.name)
415
416#define crash_get_array_field(layout, map, array_name, idx, field_name) \
417 _crash_get_field(layout, \
418 (map) + (layout)->offset.array_name \
419 + (idx * (layout)->stride.array_name) \
420 + (layout)->offset.field_name, \
421 (layout)->length.field_name)
422
423#define crash_get_hdr_raw_field(layout, hdr, name) ((hdr)->name)
424
425#define crash_get_hdr_field(layout, hdr, name) \
426 _crash_get_field(layout, (const char *) &(hdr)->name, \
427 sizeof((hdr)->name))
428
429#define crash_get_layout(layout, hdr, name) \
430 do { \
431 (layout)->name = crash_get_hdr_field(layout, hdr, \
432 name); \
433 DBG("layout.%s = %" PRIu64, #name, \
434 (uint64_t) (layout)->name); \
435 } while (0)
436
437static
438int get_crash_layout_0_0(struct lttng_crash_layout *layout,
439 char *map)
440{
441 const struct crash_abi_0_0 *abi = (const struct crash_abi_0_0 *) map;
442
443 crash_get_layout(layout, abi, offset.prod_offset);
444 crash_get_layout(layout, abi, offset.consumed_offset);
445 crash_get_layout(layout, abi, offset.commit_hot_array);
446 crash_get_layout(layout, abi, offset.commit_hot_seq);
447 crash_get_layout(layout, abi, offset.buf_wsb_array);
448 crash_get_layout(layout, abi, offset.buf_wsb_id);
449 crash_get_layout(layout, abi, offset.sb_array);
450 crash_get_layout(layout, abi, offset.sb_array_shmp_offset);
451 crash_get_layout(layout, abi, offset.sb_backend_p_offset);
452 crash_get_layout(layout, abi, offset.content_size);
453 crash_get_layout(layout, abi, offset.packet_size);
454
455 crash_get_layout(layout, abi, length.prod_offset);
456 crash_get_layout(layout, abi, length.consumed_offset);
457 crash_get_layout(layout, abi, length.commit_hot_seq);
458 crash_get_layout(layout, abi, length.buf_wsb_id);
459 crash_get_layout(layout, abi, length.sb_array_shmp_offset);
460 crash_get_layout(layout, abi, length.sb_backend_p_offset);
461 crash_get_layout(layout, abi, length.content_size);
462 crash_get_layout(layout, abi, length.packet_size);
463
464 crash_get_layout(layout, abi, stride.commit_hot_array);
465 crash_get_layout(layout, abi, stride.buf_wsb_array);
466 crash_get_layout(layout, abi, stride.sb_array);
467
468 crash_get_layout(layout, abi, buf_size);
469 crash_get_layout(layout, abi, subbuf_size);
470 crash_get_layout(layout, abi, num_subbuf);
471 crash_get_layout(layout, abi, mode);
472
473 return 0;
474}
475
476static
477void print_dbg_magic(const uint8_t *magic)
478{
479 DBG("magic: 0x%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X",
480 magic[0], magic[1], magic[2], magic[3],
481 magic[4], magic[5], magic[6], magic[7],
482 magic[8], magic[9], magic[10], magic[11],
483 magic[12], magic[13], magic[14], magic[15]);
484}
485
486static
487int check_magic(const uint8_t *magic)
488{
489 int i;
490
491 for (i = 0; i < RB_CRASH_DUMP_ABI_MAGIC_LEN; i++) {
492 if ((magic[i] ^ 0xFF) != lttng_crash_expected_magic_xor[i]) {
493 return -1;
494 }
495 }
496 return 0;
497}
498
499static
500int get_crash_layout(struct lttng_crash_layout *layout, int fd)
501{
502 char *map;
503 int ret = 0, unmapret;
504 const uint8_t *magic;
505 uint64_t mmap_length;
506 uint16_t major, minor;
507 uint8_t word_size;
508 const struct crash_abi_unknown *abi;
509 uint16_t endian;
510 enum lttng_crash_type layout_type;
511
512 map = mmap(NULL, RB_CRASH_DUMP_ABI_LEN, PROT_READ, MAP_PRIVATE,
513 fd, 0);
514 if (map == MAP_FAILED) {
515 PERROR("Mapping file");
516 return -1;
517 }
518 abi = (const struct crash_abi_unknown *) map;
519 magic = crash_get_hdr_raw_field(layout, abi, magic);
520 print_dbg_magic(magic);
521 if (check_magic(magic)) {
522 DBG("Unknown magic number");
523 ret = 1; /* positive return value, skip */
524 goto end;
525 }
526 endian = crash_get_hdr_field(layout, abi, endian);
527 switch (endian) {
528 case RB_CRASH_ENDIAN:
529 break;
530 case RB_CRASH_ENDIAN_REVERSE:
531 layout->reverse_byte_order = 1;
532 break;
533 default:
534 DBG("Unknown endianness value: 0x%X", (unsigned int) endian);
535 ret = 1; /* positive return value, skip */
536 goto end;
537 }
538 layout_type = (enum lttng_crash_type) crash_get_hdr_field(layout, abi, layout_type);
539 switch (layout_type) {
540 case LTTNG_CRASH_TYPE_UST:
541 break;
542 case LTTNG_CRASH_TYPE_KERNEL:
543 ERR("lttng-modules buffer layout support not implemented");
544 ret = 1; /* positive return value, skip */
545 goto end;
546 default:
547 ERR("Unknown layout type %u", (unsigned int) layout_type);
548 ret = 1; /* positive return value, skip */
549 goto end;
550 }
551 mmap_length = crash_get_hdr_field(layout, abi, mmap_length);
552 DBG("mmap_length: %" PRIu64, mmap_length);
553 layout->mmap_length = mmap_length;
554 major = crash_get_hdr_field(layout, abi, major);
555 DBG("major: %u", major);
556 minor = crash_get_hdr_field(layout, abi, minor);
557 DBG("minor: %u", minor);
558 word_size = crash_get_hdr_field(layout, abi, word_size);
559 DBG("word_size: %u", word_size);
560 switch (major) {
561 case 0:
562 switch (minor) {
563 case 0:
564 ret = get_crash_layout_0_0(layout, map);
565 if (ret)
566 goto end;
567 break;
568 default:
569 ret = -1;
570 ERR("Unsupported crash ABI %u.%u\n", major, minor);
571 goto end;
572 }
573 break;
574 default:
575 ERR("Unsupported crash ABI %u.%u\n", major, minor);
576 ret = -1;
577 goto end;
578 }
579 layout->word_size = word_size;
580end:
581 unmapret = munmap(map, RB_CRASH_DUMP_ABI_LEN);
582 if (unmapret) {
583 PERROR("munmap");
584 }
585 return ret;
586}
587
588/* buf_trunc mask selects only the buffer number. */
589static inline
590uint64_t buf_trunc(uint64_t offset, uint64_t buf_size)
591{
592 return offset & ~(buf_size - 1);
593}
594
595/* subbuf_trunc mask selects the subbuffer number. */
596static inline
597uint64_t subbuf_trunc(uint64_t offset, uint64_t subbuf_size)
598{
599 return offset & ~(subbuf_size - 1);
600}
601
602/* buf_offset mask selects only the offset within the current buffer. */
603static inline
604uint64_t buf_offset(uint64_t offset, uint64_t buf_size)
605{
606 return offset & (buf_size - 1);
607}
608
609/* subbuf_offset mask selects the offset within the current subbuffer. */
610static inline
611uint64_t subbuf_offset(uint64_t offset, uint64_t subbuf_size)
612{
613 return offset & (subbuf_size - 1);
614}
615
616/* subbuf_index returns the index of the current subbuffer within the buffer. */
617static inline
618uint64_t subbuf_index(uint64_t offset, uint64_t buf_size, uint64_t subbuf_size)
619{
620 return buf_offset(offset, buf_size) / subbuf_size;
621}
622
623static inline
624uint64_t subbuffer_id_get_index(uint32_t mode, uint64_t id,
625 unsigned int wl)
626{
627 if (mode == RING_BUFFER_OVERWRITE)
628 return id & SB_ID_INDEX_MASK(wl);
629 else
630 return id;
631}
632
633static
634int copy_crash_subbuf(const struct lttng_crash_layout *layout,
635 int fd_dest, char *buf, uint64_t offset)
636{
637 uint64_t buf_size, subbuf_size, num_subbuf, sbidx, id,
638 sb_bindex, rpages_offset, p_offset, seq_cc,
639 committed, commit_count_mask, consumed_cur,
640 packet_size;
641 char *subbuf_ptr;
642 ssize_t writelen;
643
644 /*
645 * Get the current subbuffer by applying the proper mask to
646 * "offset", and looking up the subbuf location within the
647 * source file buf.
648 */
649
650 buf_size = layout->buf_size;
651 subbuf_size = layout->subbuf_size;
652 num_subbuf = layout->num_subbuf;
653
654 switch (layout->word_size) {
655 case 4: commit_count_mask = 0xFFFFFFFFULL / num_subbuf;
656 break;
657 case 8: commit_count_mask = 0xFFFFFFFFFFFFFFFFULL / num_subbuf;
658 break;
659 default:
660 ERR("Unsupported word size: %u",
661 (unsigned int) layout->word_size);
662 return -EINVAL;
663 }
664
665 DBG("Copy crash subbuffer at offset %lu", offset);
666 sbidx = subbuf_index(offset, buf_size, subbuf_size);
667
668 /*
669 * Find where the seq cc is located. Compute length of data to
670 * copy.
671 */
672 seq_cc = crash_get_array_field(layout, buf, commit_hot_array,
673 sbidx, commit_hot_seq);
674 consumed_cur = crash_get_field(layout, buf, consumed_offset);
675
676 /*
677 * Check that the buffer we are getting is after or at
678 * consumed_cur position.
679 */
680 if ((long) subbuf_trunc(offset, subbuf_size)
681 - (long) subbuf_trunc(consumed_cur, subbuf_size) < 0) {
682 DBG("No data: position is before consumed_cur");
683 goto nodata;
684 }
685
686 /*
687 * Check if subbuffer has been fully committed.
688 */
689 if (((seq_cc - subbuf_size) & commit_count_mask)
690 - (buf_trunc(offset, buf_size) / num_subbuf)
691 == 0) {
692 committed = subbuf_size;
693 } else {
694 committed = subbuf_offset(seq_cc, subbuf_size);
695 if (!committed) {
696 DBG("No data committed, seq_cc: %" PRIu64, seq_cc);
697 goto nodata;
698 }
699 }
700
701 /* Find actual physical offset in subbuffer table */
702 id = crash_get_array_field(layout, buf, buf_wsb_array,
703 sbidx, buf_wsb_id);
704 sb_bindex = subbuffer_id_get_index(layout->mode, id,
705 layout->word_size);
706 rpages_offset = crash_get_array_field(layout, buf, sb_array,
707 sb_bindex, sb_array_shmp_offset);
708 p_offset = crash_get_field(layout, buf + rpages_offset,
709 sb_backend_p_offset);
710 subbuf_ptr = buf + p_offset;
711
712 if (committed == subbuf_size) {
713 /*
714 * Packet header can be used.
715 */
716 if (layout->length.packet_size) {
717 memcpy(&packet_size,
718 subbuf_ptr + layout->offset.packet_size,
719 layout->length.packet_size);
720 if (layout->reverse_byte_order) {
721 packet_size = __bswap_64(packet_size);
722 }
723 packet_size /= CHAR_BIT;
724 } else {
725 packet_size = subbuf_size;
726 }
727 } else {
728 uint64_t patch_size;
729
730 /*
731 * Find where to patch the sub-buffer header with actual
732 * readable data len and packet len, derived from seq
733 * cc. Patch it in our in-memory copy.
734 */
735 patch_size = committed * CHAR_BIT;
736 if (layout->reverse_byte_order) {
737 patch_size = __bswap_64(patch_size);
738 }
739 if (layout->length.content_size) {
740 memcpy(subbuf_ptr + layout->offset.content_size,
741 &patch_size, layout->length.content_size);
742 }
743 if (layout->length.packet_size) {
744 memcpy(subbuf_ptr + layout->offset.packet_size,
745 &patch_size, layout->length.packet_size);
746 }
747 packet_size = committed;
748 }
749
750 /*
751 * Copy packet into fd_dest.
752 */
753 writelen = lttng_write(fd_dest, subbuf_ptr, packet_size);
754 if (writelen < packet_size) {
755 PERROR("Error writing to output file");
756 return -1;
757 }
758 DBG("Copied %" PRIu64 " bytes of data", packet_size);
759 return 0;
760
761nodata:
762 return -ENODATA;
763}
764
765static
766int copy_crash_data(const struct lttng_crash_layout *layout, int fd_dest,
767 int fd_src)
768{
769 char *buf;
770 int ret = 0, has_data = 0;
771 struct stat statbuf;
772 size_t src_file_len;
773 uint64_t prod_offset, consumed_offset;
774 uint64_t offset, subbuf_size;
775 ssize_t readlen;
776
777 ret = fstat(fd_src, &statbuf);
778 if (ret) {
779 return ret;
780 }
781 src_file_len = layout->mmap_length;
782 buf = zmalloc(src_file_len);
783 if (!buf) {
784 return -1;
785 }
786 readlen = lttng_read(fd_src, buf, src_file_len);
787 if (readlen < 0) {
788 PERROR("Error reading input file");
309fc9bf
MD
789 ret = -1;
790 goto end;
d7ba1388
MD
791 }
792
793 prod_offset = crash_get_field(layout, buf, prod_offset);
794 DBG("prod_offset: 0x%" PRIx64, prod_offset);
795 consumed_offset = crash_get_field(layout, buf, consumed_offset);
796 DBG("consumed_offset: 0x%" PRIx64, consumed_offset);
797 subbuf_size = layout->subbuf_size;
798
799 for (offset = consumed_offset; offset < prod_offset;
800 offset += subbuf_size) {
801 ret = copy_crash_subbuf(layout, fd_dest, buf, offset);
802 if (!ret) {
803 has_data = 1;
804 }
805 if (ret) {
806 goto end;
807 }
808 }
809end:
810 free(buf);
811 if (ret && ret != -ENODATA) {
812 return ret;
813 }
814 if (has_data) {
815 return 0;
816 } else {
817 return -ENODATA;
818 }
819}
820
821static
822int extract_file(int output_dir_fd, const char *output_file,
823 int input_dir_fd, const char *input_file)
824{
825 int fd_dest, fd_src, ret = 0, closeret;
826 struct lttng_crash_layout layout;
827
828 layout.reverse_byte_order = 0; /* For reading magic number */
829
830 DBG("Extract file '%s'", input_file);
831 fd_src = openat(input_dir_fd, input_file, O_RDONLY);
832 if (fd_src < 0) {
833 PERROR("Error opening '%s' for reading",
834 input_file);
835 ret = -1;
836 goto end;
837 }
838
839 /* Query the crash ABI layout */
840 ret = get_crash_layout(&layout, fd_src);
841 if (ret) {
842 goto close_src;
843 }
844
845 fd_dest = openat(output_dir_fd, output_file,
846 O_RDWR | O_CREAT | O_EXCL,
847 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
848 if (fd_dest < 0) {
849 PERROR("Error opening '%s' for writing",
850 output_file);
851 ret = -1;
852 goto close_src;
853 }
854
855 ret = copy_crash_data(&layout, fd_dest, fd_src);
856 if (ret) {
857 goto close_dest;
858 }
859
860close_dest:
861 closeret = close(fd_dest);
862 if (closeret) {
863 PERROR("close");
864 }
865 if (ret == -ENODATA) {
866 closeret = unlinkat(output_dir_fd, output_file, 0);
867 if (closeret) {
868 PERROR("unlinkat");
869 }
870 }
871close_src:
872 closeret = close(fd_src);
873 if (closeret) {
874 PERROR("close");
875 }
876end:
877 return ret;
878}
879
880static
881int extract_all_files(const char *output_path,
882 const char *input_path)
883{
884 DIR *input_dir, *output_dir;
885 int input_dir_fd, output_dir_fd, ret = 0, closeret;
886 struct dirent *entry; /* input */
887
888 /* Open input directory */
889 input_dir = opendir(input_path);
890 if (!input_dir) {
891 PERROR("Cannot open '%s' path", input_path);
892 return -1;
893 }
894 input_dir_fd = dirfd(input_dir);
895 if (input_dir_fd < 0) {
896 PERROR("dirfd");
897 return -1;
898 }
899
900 /* Open output directory */
901 output_dir = opendir(output_path);
902 if (!output_dir) {
903 PERROR("Cannot open '%s' path", output_path);
904 return -1;
905 }
906 output_dir_fd = dirfd(output_dir);
907 if (output_dir_fd < 0) {
908 PERROR("dirfd");
909 return -1;
910 }
911
912 while ((entry = readdir(input_dir))) {
913 if (!strcmp(entry->d_name, ".")
914 || !strcmp(entry->d_name, ".."))
915 continue;
916 ret = extract_file(output_dir_fd, entry->d_name,
917 input_dir_fd, entry->d_name);
918 if (ret == -ENODATA) {
919 DBG("No data in file '%s', skipping", entry->d_name);
920 ret = 0;
921 continue;
922 } else if (ret < 0) {
923 break;
924 } else if (ret > 0) {
925 DBG("Skipping file '%s'", entry->d_name);
926 ret = 0;
927 continue;
928 }
929 }
930 closeret = closedir(output_dir);
931 if (closeret) {
932 PERROR("closedir");
933 }
934 closeret = closedir(input_dir);
935 if (closeret) {
936 PERROR("closedir");
937 }
938 return ret;
939}
940
941static
942int extract_one_trace(const char *output_path,
943 const char *input_path)
944{
945 char dest[PATH_MAX], src[PATH_MAX];
946 int ret;
947
948 DBG("Extract crash trace '%s' into '%s'", input_path, output_path);
949
950 /* Copy metadata */
951 strncpy(dest, output_path, PATH_MAX);
952 dest[PATH_MAX - 1] = '\0';
953 strncat(dest, "/metadata", PATH_MAX - strlen(dest) - 1);
954
955 strncpy(src, input_path, PATH_MAX);
956 src[PATH_MAX - 1] = '\0';
957 strncat(src, "/metadata", PATH_MAX - strlen(dest) - 1);
958
959 ret = copy_file(dest, src);
960 if (ret) {
961 return ret;
962 }
963
964 /* Extract each other file that has expected header */
965 return extract_all_files(output_path, input_path);
966}
967
968static
a424227e
MD
969int extract_trace_recursive(const char *output_path,
970 const char *input_path)
d7ba1388 971{
a424227e
MD
972 DIR *dir;
973 int dir_fd, ret = 0, closeret;
974 struct dirent *entry;
d7ba1388
MD
975 int has_warning = 0;
976
a424227e
MD
977 /* Open directory */
978 dir = opendir(input_path);
979 if (!dir) {
980 PERROR("Cannot open '%s' path", input_path);
981 return -1;
982 }
983 dir_fd = dirfd(dir);
984 if (dir_fd < 0) {
985 PERROR("dirfd");
d7ba1388
MD
986 return -1;
987 }
988
a424227e
MD
989 while ((entry = readdir(dir))) {
990 if (!strcmp(entry->d_name, ".")
991 || !strcmp(entry->d_name, "..")) {
992 continue;
993 }
994 switch (entry->d_type) {
995 case DT_DIR:
996 {
997 char output_subpath[PATH_MAX];
998 char input_subpath[PATH_MAX];
999
1000 strncpy(output_subpath, output_path,
1001 sizeof(output_subpath));
1002 output_subpath[sizeof(output_subpath) - 1] = '\0';
1003 strncat(output_subpath, "/",
1004 sizeof(output_subpath) - strlen(output_subpath) - 1);
1005 strncat(output_subpath, entry->d_name,
1006 sizeof(output_subpath) - strlen(output_subpath) - 1);
1007
1008 ret = mkdir(output_subpath, S_IRWXU | S_IRWXG);
1009 if (ret) {
1010 PERROR("mkdir");
1011 has_warning = 1;
1012 goto end;
1013 }
1014
1015 strncpy(input_subpath, input_path,
1016 sizeof(input_subpath));
1017 input_subpath[sizeof(input_subpath) - 1] = '\0';
1018 strncat(input_subpath, "/",
1019 sizeof(input_subpath) - strlen(input_subpath) - 1);
1020 strncat(input_subpath, entry->d_name,
1021 sizeof(input_subpath) - strlen(input_subpath) - 1);
1022
1023 ret = extract_trace_recursive(output_subpath,
1024 input_subpath);
29786dfa
JG
1025 if (ret) {
1026 has_warning = 1;
1027 }
a424227e
MD
1028 break;
1029 }
1030 case DT_REG:
1031 if (!strcmp(entry->d_name, "metadata")) {
1032 ret = extract_one_trace(output_path,
1033 input_path);
1034 if (ret) {
1035 WARN("Error extracting trace '%s', continuing anyway.",
1036 input_path);
1037 has_warning = 1;
1038 }
1039 }
1040 /* Ignore other files */
1041 break;
1042 default:
d7ba1388 1043 has_warning = 1;
a424227e 1044 goto end;
d7ba1388
MD
1045 }
1046 }
a424227e
MD
1047end:
1048 closeret = closedir(dir);
1049 if (closeret) {
1050 PERROR("closedir");
1051 }
d7ba1388
MD
1052 return has_warning;
1053}
1054
1055static
afe4fd7c 1056int delete_dir_recursive(const char *path)
d7ba1388 1057{
afe4fd7c
JR
1058 DIR *dir;
1059 int dir_fd, ret = 0, closeret;
d7ba1388
MD
1060 struct dirent *entry;
1061
1062 /* Open trace directory */
afe4fd7c
JR
1063 dir = opendir(path);
1064 if (!dir) {
1065 PERROR("Cannot open '%s' path", path);
920a187e
JG
1066 ret = -errno;
1067 goto end;
d7ba1388 1068 }
afe4fd7c
JR
1069 dir_fd = dirfd(dir);
1070 if (dir_fd < 0) {
d7ba1388 1071 PERROR("dirfd");
920a187e
JG
1072 ret = -errno;
1073 goto end;
d7ba1388
MD
1074 }
1075
afe4fd7c 1076 while ((entry = readdir(dir))) {
d7ba1388 1077 if (!strcmp(entry->d_name, ".")
a424227e 1078 || !strcmp(entry->d_name, "..")) {
d7ba1388 1079 continue;
a424227e 1080 }
d7ba1388
MD
1081 switch (entry->d_type) {
1082 case DT_DIR:
afe4fd7c
JR
1083 {
1084 char *subpath = zmalloc(PATH_MAX);
1085
1086 if (!subpath) {
1087 PERROR("zmalloc path");
1088 ret = -1;
1089 goto end;
1090 }
1091 strncpy(subpath, path, PATH_MAX);
1092 subpath[PATH_MAX - 1] = '\0';
1093 strncat(subpath, "/",
1094 PATH_MAX - strlen(subpath) - 1);
1095 strncat(subpath, entry->d_name,
1096 PATH_MAX - strlen(subpath) - 1);
1097
1098 ret = delete_dir_recursive(subpath);
1099 free(subpath);
1100 if (ret) {
1101 /* Error occured, abort traversal. */
1102 goto end;
1103 }
d7ba1388 1104 break;
afe4fd7c 1105 }
d7ba1388 1106 case DT_REG:
afe4fd7c
JR
1107 ret = unlinkat(dir_fd, entry->d_name, 0);
1108 if (ret) {
1109 PERROR("Unlinking '%s'", entry->d_name);
1110 goto end;
1111 }
d7ba1388
MD
1112 break;
1113 default:
1114 ret = -EINVAL;
1115 goto end;
1116 }
1117 }
1118end:
afe4fd7c
JR
1119 if (!ret) {
1120 ret = rmdir(path);
1121 if (ret) {
1122 PERROR("rmdir '%s'", path);
1123 }
1124 }
1125 closeret = closedir(dir);
d7ba1388
MD
1126 if (closeret) {
1127 PERROR("closedir");
1128 }
d7ba1388
MD
1129 return ret;
1130}
1131
d7ba1388
MD
1132static
1133int view_trace(const char *viewer_path, const char *trace_path)
1134{
1135 pid_t pid;
1136
1137 pid = fork();
1138 if (pid < 0) {
1139 /* Error */
1140 PERROR("fork");
1141 return -1;
1142 } else if (pid > 0) {
1143 /* Parent */
1144 int status;
1145
1146 pid = waitpid(pid, &status, 0);
1147 if (pid < 0 || !WIFEXITED(status)) {
1148 return -1;
1149 }
1150 } else {
1151 /* Child */
1152 int ret;
1153
1154 ret = execlp(viewer_path, viewer_path,
1155 trace_path, (char *) NULL);
1156 if (ret) {
1157 PERROR("execlp");
1158 exit(EXIT_FAILURE);
1159 }
1160 exit(EXIT_SUCCESS); /* Never reached */
1161 }
1162 return 0;
1163}
1164
1165/*
1166 * main
1167 */
1168int main(int argc, char *argv[])
1169{
aa25d686
JG
1170 int ret;
1171 bool has_warning = false;
d7ba1388
MD
1172 const char *output_path = NULL;
1173 char tmppath[] = "/tmp/lttng-crash-XXXXXX";
1174
1175 progname = argv[0] ? argv[0] : "lttng-crash";
1176
1177 ret = parse_args(argc, argv);
1178 if (ret > 0) {
aa25d686 1179 goto end;
d7ba1388 1180 } else if (ret < 0) {
aa25d686
JG
1181 has_warning = true;
1182 goto end;
d7ba1388
MD
1183 }
1184
1185 if (opt_output_path) {
1186 output_path = opt_output_path;
1187 ret = mkdir(output_path, S_IRWXU | S_IRWXG);
1188 if (ret) {
1189 PERROR("mkdir");
aa25d686
JG
1190 has_warning = true;
1191 goto end;
d7ba1388
MD
1192 }
1193 } else {
1194 output_path = mkdtemp(tmppath);
1195 if (!output_path) {
1196 PERROR("mkdtemp");
aa25d686
JG
1197 has_warning = true;
1198 goto end;
d7ba1388
MD
1199 }
1200 }
1201
a424227e 1202 ret = extract_trace_recursive(output_path, input_path);
d7ba1388 1203 if (ret < 0) {
aa25d686
JG
1204 has_warning = true;
1205 goto end;
d7ba1388 1206 } else if (ret > 0) {
aa25d686
JG
1207 /* extract_trace_recursive reported a warning. */
1208 has_warning = true;
d7ba1388
MD
1209 }
1210 if (!opt_output_path) {
1211 /* View trace */
1212 ret = view_trace(opt_viewer_path, output_path);
aa25d686
JG
1213 if (ret) {
1214 has_warning = true;
1215 }
d7ba1388 1216 /* unlink temporary trace */
afe4fd7c 1217 ret = delete_dir_recursive(output_path);
aa25d686
JG
1218 if (ret) {
1219 has_warning = true;
1220 }
d7ba1388 1221 }
aa25d686
JG
1222end:
1223 exit(has_warning ? EXIT_FAILURE : EXIT_SUCCESS);
d7ba1388 1224}
This page took 0.070763 seconds and 4 git commands to generate.