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