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