docs: Add supported versions and fix-backport policy
[lttng-tools.git] / src / bin / lttng-crash / lttng-crash.cpp
CommitLineData
d7ba1388 1/*
21cf9b6b 2 * Copyright (C) 2011 EfficiOS Inc.
d7ba1388
MD
3 * Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
ab5be9fa 5 * SPDX-License-Identifier: GPL-2.0-only
d7ba1388 6 *
d7ba1388
MD
7 */
8
28ab034a
JG
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>
d7ba1388 19#include <getopt.h>
28ab034a 20#include <inttypes.h>
d7ba1388 21#include <signal.h>
28ab034a 22#include <stdbool.h>
d7ba1388
MD
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
28ab034a 26#include <sys/mman.h>
d7ba1388
MD
27#include <sys/stat.h>
28#include <sys/types.h>
d7ba1388
MD
29#include <sys/wait.h>
30#include <unistd.h>
c9e313bc 31#include <version.hpp>
d7ba1388 32
28ab034a
JG
33#define COPY_BUFLEN 4096
34#define RB_CRASH_DUMP_ABI_LEN 32
d7ba1388 35
28ab034a 36#define RB_CRASH_DUMP_ABI_MAGIC_LEN 16
d7ba1388
MD
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 */
28ab034a
JG
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, \
d7ba1388
MD
50 }
51
4fc83d94
PP
52static const char *help_msg =
53#ifdef LTTNG_EMBED_HELP
54#include <lttng-crash.1.h>
55#else
cd9adb8b 56 nullptr
4fc83d94 57#endif
28ab034a 58 ;
4fc83d94 59
d7ba1388
MD
60/*
61 * Non-static to ensure the compiler does not optimize away the xor.
62 */
63uint8_t lttng_crash_expected_magic_xor[] = RB_CRASH_DUMP_ABI_MAGIC_XOR;
64
28ab034a
JG
65#define RB_CRASH_ENDIAN 0x1234
66#define RB_CRASH_ENDIAN_REVERSE 0x3412
d7ba1388
MD
67
68enum lttng_crash_type {
69 LTTNG_CRASH_TYPE_UST = 0,
70 LTTNG_CRASH_TYPE_KERNEL = 1,
71};
72
73/* LTTng ring buffer defines (copied) */
74
28ab034a 75#define HALF_ULONG_BITS(wl) (((wl) *CHAR_BIT) >> 1)
d7ba1388 76
28ab034a
JG
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))
d7ba1388
MD
80/*
81 * Lowest bit of top word half belongs to noref. Used only for overwrite mode.
82 */
28ab034a
JG
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)
d7ba1388
MD
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 */
28ab034a
JG
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)
d7ba1388
MD
94
95enum rb_modes {
28ab034a
JG
96 RING_BUFFER_OVERWRITE = 0, /* Overwrite when buffer full */
97 RING_BUFFER_DISCARD = 1, /* Discard when buffer full */
d7ba1388
MD
98};
99
f1494934 100namespace {
d7ba1388
MD
101struct crash_abi_unknown {
102 uint8_t magic[RB_CRASH_DUMP_ABI_MAGIC_LEN];
28ab034a
JG
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 */
d7ba1388
MD
112} __attribute__((packed));
113
114struct 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
28ab034a
JG
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 */
d7ba1388
MD
150} __attribute__((packed));
151
152struct lttng_crash_layout {
153 struct {
28ab034a
JG
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;
d7ba1388
MD
157 } offset;
158 struct {
28ab034a
JG
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;
d7ba1388
MD
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
28ab034a
JG
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 */
d7ba1388 174};
f1494934 175} /* namespace */
d7ba1388
MD
176
177/* Variables */
b53d4e59 178static const char *progname;
cd9adb8b
JG
179static char *opt_viewer_path = nullptr;
180static char *opt_output_path = nullptr;
d7ba1388 181
40b4fee7 182static char *the_input_path;
d7ba1388
MD
183
184int lttng_opt_quiet, lttng_opt_verbose, lttng_opt_mi;
185
186enum {
187 OPT_DUMP_OPTIONS,
188};
189
190/* Getopt options. No first level command. */
191static struct option long_options[] = {
cd9adb8b
JG
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 },
d7ba1388
MD
196};
197
cd9adb8b 198static void usage()
d7ba1388 199{
4fc83d94 200 int ret = utils_show_help(1, "lttng-crash", help_msg);
6ef14df5
PP
201
202 if (ret) {
4fc83d94 203 ERR("Cannot show --help for `lttng-crash`");
6ef14df5
PP
204 perror("exec");
205 exit(EXIT_FAILURE);
206 }
d7ba1388
MD
207}
208
209static void version(FILE *ofp)
210{
28ab034a
JG
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);
d7ba1388
MD
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 */
224static void list_options(FILE *ofp)
225{
226 int i = 0;
cd9adb8b 227 struct option *option = nullptr;
d7ba1388
MD
228
229 option = &long_options[i];
cd9adb8b 230 while (option->name != nullptr) {
d7ba1388
MD
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 */
247static int parse_args(int argc, char **argv)
248{
249 int opt, ret = 0;
250
251 if (argc < 2) {
6ef14df5 252 usage();
d7ba1388
MD
253 exit(EXIT_FAILURE);
254 }
255
cd9adb8b 256 while ((opt = getopt_long(argc, argv, "+Vhve:x:", long_options, nullptr)) != -1) {
d7ba1388
MD
257 switch (opt) {
258 case 'V':
259 version(stdout);
260 ret = 1;
261 goto end;
262 case 'h':
6ef14df5 263 usage();
d7ba1388
MD
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':
9030e9a9 273 free(opt_viewer_path);
d7ba1388
MD
274 opt_viewer_path = strdup(optarg);
275 break;
276 case 'x':
9030e9a9 277 free(opt_output_path);
d7ba1388
MD
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:
6ef14df5 285 ERR("Unknown command-line option");
d7ba1388
MD
286 goto error;
287 }
288 }
289
a424227e 290 /* No leftovers, or more than one input path, print usage and quit */
6ef14df5
PP
291 if (argc - optind != 1) {
292 ERR("Command-line error: Specify exactly one input path");
d7ba1388
MD
293 goto error;
294 }
295
40b4fee7 296 the_input_path = argv[optind];
d7ba1388
MD
297end:
298 return ret;
299
300error:
301 return -1;
302}
303
28ab034a 304static int copy_file(const char *file_dest, const char *file_src)
d7ba1388 305{
8726a045 306 int fd_src = -1, fd_dest = -1;
d7ba1388
MD
307 ssize_t readlen, writelen;
308 char buf[COPY_BUFLEN];
8726a045 309 int ret;
d7ba1388
MD
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);
8726a045
JR
316 ret = -errno;
317 goto error;
d7ba1388 318 }
28ab034a 319 fd_dest = open(file_dest, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
d7ba1388
MD
320 if (fd_dest < 0) {
321 PERROR("Error opening %s for writing", file_dest);
8726a045
JR
322 ret = -errno;
323 goto error;
d7ba1388
MD
324 }
325
326 for (;;) {
327 readlen = lttng_read(fd_src, buf, COPY_BUFLEN);
328 if (readlen < 0) {
329 PERROR("Error reading input file");
8726a045
JR
330 ret = -1;
331 goto error;
d7ba1388
MD
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");
8726a045
JR
339 ret = -1;
340 goto error;
341 }
342 }
343
344 ret = 0;
345error:
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);
d7ba1388
MD
355 }
356 }
8726a045 357 return ret;
d7ba1388
MD
358}
359
28ab034a
JG
360static uint64_t
361_crash_get_field(const struct lttng_crash_layout *layout, const char *ptr, size_t size)
d7ba1388
MD
362{
363 switch (size) {
28ab034a
JG
364 case 1:
365 return *(uint8_t *) ptr;
366 case 2:
367 if (layout->reverse_byte_order) {
4ff128af 368 return bswap_16(*(uint16_t *) ptr);
d7ba1388
MD
369 } else {
370 return *(uint16_t *) ptr;
d7ba1388 371 }
28ab034a
JG
372 case 4:
373 if (layout->reverse_byte_order) {
4ff128af 374 return bswap_32(*(uint32_t *) ptr);
d7ba1388
MD
375 } else {
376 return *(uint32_t *) ptr;
d7ba1388 377 }
28ab034a
JG
378 case 8:
379 if (layout->reverse_byte_order) {
4ff128af 380 return bswap_64(*(uint64_t *) ptr);
d7ba1388
MD
381 } else {
382 return *(uint64_t *) ptr;
383 }
384 default:
385 abort();
386 return -1;
387 }
d7ba1388
MD
388}
389
28ab034a 390#define crash_get_field(layout, map, name) \
5c7248cd 391 _crash_get_field(layout, (map) + (layout)->offset.name, (layout)->length.name)
28ab034a 392
5c7248cd
JG
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, \
28ab034a
JG
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); \
d7ba1388
MD
409 } while (0)
410
28ab034a 411static int get_crash_layout_0_0(struct lttng_crash_layout *layout, char *map)
d7ba1388
MD
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
28ab034a 448static void print_dbg_magic(const uint8_t *magic)
d7ba1388
MD
449{
450 DBG("magic: 0x%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X",
28ab034a
JG
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]);
d7ba1388
MD
467}
468
28ab034a 469static int check_magic(const uint8_t *magic)
d7ba1388
MD
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
28ab034a 481static int get_crash_layout(struct lttng_crash_layout *layout, int fd, const char *input_file)
d7ba1388
MD
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;
e6c6e235 492 struct stat stat;
d7ba1388 493
e6c6e235
MD
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
28ab034a
JG
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);
e6c6e235
MD
506 return -1;
507 }
cd9adb8b 508 map = (char *) mmap(nullptr, RB_CRASH_DUMP_ABI_LEN, PROT_READ, MAP_PRIVATE, fd, 0);
d7ba1388
MD
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");
28ab034a 518 ret = 1; /* positive return value, skip */
d7ba1388
MD
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);
28ab034a 530 ret = 1; /* positive return value, skip */
d7ba1388
MD
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");
28ab034a 539 ret = 1; /* positive return value, skip */
d7ba1388
MD
540 goto end;
541 default:
542 ERR("Unknown layout type %u", (unsigned int) layout_type);
28ab034a 543 ret = 1; /* positive return value, skip */
d7ba1388
MD
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;
575end:
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. */
28ab034a 584static inline uint64_t buf_trunc(uint64_t offset, uint64_t buf_size)
d7ba1388
MD
585{
586 return offset & ~(buf_size - 1);
587}
588
589/* subbuf_trunc mask selects the subbuffer number. */
28ab034a 590static inline uint64_t subbuf_trunc(uint64_t offset, uint64_t subbuf_size)
d7ba1388
MD
591{
592 return offset & ~(subbuf_size - 1);
593}
594
595/* buf_offset mask selects only the offset within the current buffer. */
28ab034a 596static inline uint64_t buf_offset(uint64_t offset, uint64_t buf_size)
d7ba1388
MD
597{
598 return offset & (buf_size - 1);
599}
600
601/* subbuf_offset mask selects the offset within the current subbuffer. */
28ab034a 602static inline uint64_t subbuf_offset(uint64_t offset, uint64_t subbuf_size)
d7ba1388
MD
603{
604 return offset & (subbuf_size - 1);
605}
606
607/* subbuf_index returns the index of the current subbuffer within the buffer. */
28ab034a 608static inline uint64_t subbuf_index(uint64_t offset, uint64_t buf_size, uint64_t subbuf_size)
d7ba1388
MD
609{
610 return buf_offset(offset, buf_size) / subbuf_size;
611}
612
28ab034a 613static inline uint64_t subbuffer_id_get_index(uint32_t mode, uint64_t id, unsigned int wl)
d7ba1388
MD
614{
615 if (mode == RING_BUFFER_OVERWRITE)
616 return id & SB_ID_INDEX_MASK(wl);
617 else
618 return id;
619}
620
28ab034a
JG
621static int
622copy_crash_subbuf(const struct lttng_crash_layout *layout, int fd_dest, char *buf, uint64_t offset)
d7ba1388 623{
28ab034a
JG
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;
d7ba1388
MD
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) {
28ab034a
JG
640 case 4:
641 commit_count_mask = 0xFFFFFFFFULL / num_subbuf;
d7ba1388 642 break;
28ab034a
JG
643 case 8:
644 commit_count_mask = 0xFFFFFFFFFFFFFFFFULL / num_subbuf;
d7ba1388
MD
645 break;
646 default:
28ab034a 647 ERR("Unsupported word size: %u", (unsigned int) layout->word_size);
d7ba1388
MD
648 return -EINVAL;
649 }
650
5f98d630 651 DBG("Copy crash subbuffer at offset %" PRIu64, offset);
d7ba1388
MD
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 */
28ab034a 658 seq_cc = crash_get_array_field(layout, buf, commit_hot_array, sbidx, commit_hot_seq);
d7ba1388
MD
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 */
28ab034a
JG
665 if ((long) subbuf_trunc(offset, subbuf_size) -
666 (long) subbuf_trunc(consumed_cur, subbuf_size) <
667 0) {
d7ba1388
MD
668 DBG("No data: position is before consumed_cur");
669 goto nodata;
670 }
671
672 /*
673 * Check if subbuffer has been fully committed.
674 */
28ab034a
JG
675 if (((seq_cc - subbuf_size) & commit_count_mask) -
676 (buf_trunc(offset, buf_size) / num_subbuf) ==
677 0) {
d7ba1388
MD
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 */
28ab034a
JG
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);
d7ba1388
MD
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,
28ab034a
JG
701 subbuf_ptr + layout->offset.packet_size,
702 layout->length.packet_size);
d7ba1388 703 if (layout->reverse_byte_order) {
4ff128af 704 packet_size = bswap_64(packet_size);
d7ba1388
MD
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) {
4ff128af 720 patch_size = bswap_64(patch_size);
d7ba1388
MD
721 }
722 if (layout->length.content_size) {
723 memcpy(subbuf_ptr + layout->offset.content_size,
28ab034a
JG
724 &patch_size,
725 layout->length.content_size);
d7ba1388
MD
726 }
727 if (layout->length.packet_size) {
728 memcpy(subbuf_ptr + layout->offset.packet_size,
28ab034a
JG
729 &patch_size,
730 layout->length.packet_size);
d7ba1388
MD
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
746nodata:
747 return -ENODATA;
748}
749
28ab034a 750static int copy_crash_data(const struct lttng_crash_layout *layout, int fd_dest, int fd_src)
d7ba1388
MD
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;
64803277 765 buf = calloc<char>(src_file_len);
d7ba1388
MD
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");
309fc9bf
MD
772 ret = -1;
773 goto end;
d7ba1388
MD
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
28ab034a 782 for (offset = consumed_offset; offset < prod_offset; offset += subbuf_size) {
d7ba1388
MD
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 }
791end:
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
28ab034a
JG
803static int
804extract_file(int output_dir_fd, const char *output_file, int input_dir_fd, const char *input_file)
d7ba1388
MD
805{
806 int fd_dest, fd_src, ret = 0, closeret;
807 struct lttng_crash_layout layout;
808
28ab034a 809 layout.reverse_byte_order = 0; /* For reading magic number */
d7ba1388
MD
810
811 DBG("Extract file '%s'", input_file);
812 fd_src = openat(input_dir_fd, input_file, O_RDONLY);
813 if (fd_src < 0) {
28ab034a 814 PERROR("Error opening '%s' for reading", input_file);
d7ba1388
MD
815 ret = -1;
816 goto end;
817 }
818
819 /* Query the crash ABI layout */
e6c6e235 820 ret = get_crash_layout(&layout, fd_src, input_file);
d7ba1388
MD
821 if (ret) {
822 goto close_src;
823 }
824
28ab034a
JG
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);
d7ba1388 829 if (fd_dest < 0) {
28ab034a 830 PERROR("Error opening '%s' for writing", output_file);
d7ba1388
MD
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
840close_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 }
851close_src:
852 closeret = close(fd_src);
853 if (closeret) {
854 PERROR("close");
855 }
856end:
857 return ret;
858}
859
28ab034a 860static int extract_all_files(const char *output_path, const char *input_path)
d7ba1388
MD
861{
862 DIR *input_dir, *output_dir;
863 int input_dir_fd, output_dir_fd, ret = 0, closeret;
28ab034a 864 struct dirent *entry; /* input */
d7ba1388
MD
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))) {
28ab034a 891 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
d7ba1388 892 continue;
28ab034a 893 ret = extract_file(output_dir_fd, entry->d_name, input_dir_fd, entry->d_name);
d7ba1388
MD
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
28ab034a 917static int extract_one_trace(const char *output_path, const char *input_path)
d7ba1388
MD
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
28ab034a 942static int extract_trace_recursive(const char *output_path, const char *input_path)
d7ba1388 943{
a424227e
MD
944 DIR *dir;
945 int dir_fd, ret = 0, closeret;
946 struct dirent *entry;
88ae485a 947 size_t path_len;
d7ba1388
MD
948 int has_warning = 0;
949
a424227e
MD
950 /* Open directory */
951 dir = opendir(input_path);
952 if (!dir) {
953 PERROR("Cannot open '%s' path", input_path);
954 return -1;
955 }
88ae485a
JR
956
957 path_len = strlen(input_path);
958
a424227e
MD
959 dir_fd = dirfd(dir);
960 if (dir_fd < 0) {
961 PERROR("dirfd");
d7ba1388
MD
962 return -1;
963 }
964
a424227e 965 while ((entry = readdir(dir))) {
88ae485a
JR
966 struct stat st;
967 size_t name_len;
968 char filename[PATH_MAX];
969
28ab034a 970 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
a424227e
MD
971 continue;
972 }
88ae485a
JR
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)",
28ab034a
JG
977 input_path,
978 entry->d_name);
88ae485a
JR
979 continue;
980 }
981
28ab034a 982 if (snprintf(filename, sizeof(filename), "%s/%s", input_path, entry->d_name) < 0) {
88ae485a
JR
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)) {
a424227e
MD
993 char output_subpath[PATH_MAX];
994 char input_subpath[PATH_MAX];
995
28ab034a 996 strncpy(output_subpath, output_path, sizeof(output_subpath));
a424227e 997 output_subpath[sizeof(output_subpath) - 1] = '\0';
28ab034a
JG
998 strncat(output_subpath,
999 "/",
a424227e 1000 sizeof(output_subpath) - strlen(output_subpath) - 1);
28ab034a
JG
1001 strncat(output_subpath,
1002 entry->d_name,
a424227e
MD
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
28ab034a 1012 strncpy(input_subpath, input_path, sizeof(input_subpath));
a424227e 1013 input_subpath[sizeof(input_subpath) - 1] = '\0';
28ab034a
JG
1014 strncat(input_subpath,
1015 "/",
a424227e 1016 sizeof(input_subpath) - strlen(input_subpath) - 1);
28ab034a
JG
1017 strncat(input_subpath,
1018 entry->d_name,
a424227e
MD
1019 sizeof(input_subpath) - strlen(input_subpath) - 1);
1020
28ab034a 1021 ret = extract_trace_recursive(output_subpath, input_subpath);
29786dfa
JG
1022 if (ret) {
1023 has_warning = 1;
1024 }
88ae485a 1025 } else if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
a424227e 1026 if (!strcmp(entry->d_name, "metadata")) {
28ab034a 1027 ret = extract_one_trace(output_path, input_path);
a424227e
MD
1028 if (ret) {
1029 WARN("Error extracting trace '%s', continuing anyway.",
28ab034a 1030 input_path);
a424227e
MD
1031 has_warning = 1;
1032 }
1033 }
88ae485a 1034 } else {
d7ba1388 1035 has_warning = 1;
a424227e 1036 goto end;
d7ba1388
MD
1037 }
1038 }
a424227e
MD
1039end:
1040 closeret = closedir(dir);
1041 if (closeret) {
1042 PERROR("closedir");
1043 }
d7ba1388
MD
1044 return has_warning;
1045}
1046
28ab034a 1047static int delete_dir_recursive(const char *path)
d7ba1388 1048{
afe4fd7c
JR
1049 DIR *dir;
1050 int dir_fd, ret = 0, closeret;
88ae485a 1051 size_t path_len;
d7ba1388
MD
1052 struct dirent *entry;
1053
1054 /* Open trace directory */
afe4fd7c
JR
1055 dir = opendir(path);
1056 if (!dir) {
1057 PERROR("Cannot open '%s' path", path);
920a187e 1058 ret = -errno;
28ab034a 1059 goto end_no_closedir;
d7ba1388 1060 }
88ae485a
JR
1061
1062 path_len = strlen(path);
1063
afe4fd7c
JR
1064 dir_fd = dirfd(dir);
1065 if (dir_fd < 0) {
d7ba1388 1066 PERROR("dirfd");
920a187e
JG
1067 ret = -errno;
1068 goto end;
d7ba1388
MD
1069 }
1070
afe4fd7c 1071 while ((entry = readdir(dir))) {
88ae485a
JR
1072 struct stat st;
1073 size_t name_len;
1074 char filename[PATH_MAX];
1075
28ab034a 1076 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
d7ba1388 1077 continue;
a424227e 1078 }
88ae485a
JR
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)",
28ab034a
JG
1083 path,
1084 entry->d_name);
88ae485a
JR
1085 continue;
1086 }
1087
28ab034a 1088 if (snprintf(filename, sizeof(filename), "%s/%s", path, entry->d_name) < 0) {
88ae485a
JR
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)) {
64803277 1099 char *subpath = calloc<char>(PATH_MAX);
afe4fd7c
JR
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';
28ab034a
JG
1108 strncat(subpath, "/", PATH_MAX - strlen(subpath) - 1);
1109 strncat(subpath, entry->d_name, PATH_MAX - strlen(subpath) - 1);
afe4fd7c
JR
1110
1111 ret = delete_dir_recursive(subpath);
1112 free(subpath);
1113 if (ret) {
83f4233d 1114 /* Error occurred, abort traversal. */
afe4fd7c
JR
1115 goto end;
1116 }
88ae485a 1117 } else if (S_ISREG(st.st_mode)) {
afe4fd7c
JR
1118 ret = unlinkat(dir_fd, entry->d_name, 0);
1119 if (ret) {
1120 PERROR("Unlinking '%s'", entry->d_name);
1121 goto end;
1122 }
88ae485a 1123 } else {
d7ba1388
MD
1124 ret = -EINVAL;
1125 goto end;
1126 }
1127 }
1128end:
afe4fd7c
JR
1129 if (!ret) {
1130 ret = rmdir(path);
1131 if (ret) {
1132 PERROR("rmdir '%s'", path);
1133 }
1134 }
1135 closeret = closedir(dir);
d7ba1388
MD
1136 if (closeret) {
1137 PERROR("closedir");
1138 }
ef6fc488 1139end_no_closedir:
d7ba1388
MD
1140 return ret;
1141}
1142
28ab034a 1143static int view_trace(const char *trace_path, char *viewer_path)
d7ba1388
MD
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
53866dcb 1164 ret = spawn_viewer(trace_path, viewer_path, false);
d7ba1388 1165 if (ret) {
d7ba1388
MD
1166 exit(EXIT_FAILURE);
1167 }
53866dcb
FD
1168 /* Never reached */
1169 exit(EXIT_SUCCESS);
d7ba1388
MD
1170 }
1171 return 0;
1172}
1173
1174/*
1175 * main
1176 */
1177int main(int argc, char *argv[])
1178{
aa25d686
JG
1179 int ret;
1180 bool has_warning = false;
cd9adb8b 1181 const char *output_path = nullptr;
d7ba1388
MD
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) {
aa25d686 1188 goto end;
d7ba1388 1189 } else if (ret < 0) {
aa25d686
JG
1190 has_warning = true;
1191 goto end;
d7ba1388
MD
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");
aa25d686
JG
1199 has_warning = true;
1200 goto end;
d7ba1388
MD
1201 }
1202 } else {
1203 output_path = mkdtemp(tmppath);
1204 if (!output_path) {
1205 PERROR("mkdtemp");
aa25d686
JG
1206 has_warning = true;
1207 goto end;
d7ba1388
MD
1208 }
1209 }
1210
40b4fee7 1211 ret = extract_trace_recursive(output_path, the_input_path);
d7ba1388 1212 if (ret < 0) {
aa25d686
JG
1213 has_warning = true;
1214 goto end;
d7ba1388 1215 } else if (ret > 0) {
aa25d686
JG
1216 /* extract_trace_recursive reported a warning. */
1217 has_warning = true;
d7ba1388
MD
1218 }
1219 if (!opt_output_path) {
1220 /* View trace */
53866dcb 1221 ret = view_trace(output_path, opt_viewer_path);
aa25d686
JG
1222 if (ret) {
1223 has_warning = true;
1224 }
d7ba1388 1225 /* unlink temporary trace */
afe4fd7c 1226 ret = delete_dir_recursive(output_path);
aa25d686
JG
1227 if (ret) {
1228 has_warning = true;
1229 }
d7ba1388 1230 }
aa25d686
JG
1231end:
1232 exit(has_warning ? EXIT_FAILURE : EXIT_SUCCESS);
d7ba1388 1233}
This page took 0.119322 seconds and 4 git commands to generate.