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