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