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