Commit | Line | Data |
---|---|---|
34ac0e6c MD |
1 | /* |
2 | * babeltrace.c | |
3 | * | |
4 | * Babeltrace Trace Converter | |
5 | * | |
64fa3fec MD |
6 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation |
7 | * | |
8 | * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
34ac0e6c MD |
9 | * |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | */ | |
4c8bfb7e | 20 | |
20d24215 | 21 | #define _GNU_SOURCE |
00f7fbf0 | 22 | #include <config.h> |
95d36295 | 23 | #include <babeltrace/babeltrace.h> |
7fb21036 | 24 | #include <babeltrace/format.h> |
95d36295 JD |
25 | #include <babeltrace/context.h> |
26 | #include <babeltrace/ctf/types.h> | |
9843982d | 27 | #include <babeltrace/ctf/events.h> |
04ae3991 MD |
28 | /* TODO: fix object model for format-agnostic callbacks */ |
29 | #include <babeltrace/ctf/events-internal.h> | |
9efd5d76 | 30 | #include <babeltrace/ctf/iterator.h> |
31bdef5c | 31 | #include <babeltrace/ctf-text/types.h> |
6204d33c | 32 | #include <babeltrace/iterator.h> |
34ac0e6c MD |
33 | #include <popt.h> |
34 | #include <errno.h> | |
35 | #include <stdlib.h> | |
bbefb8dd MD |
36 | #include <ctype.h> |
37 | #include <sys/stat.h> | |
38 | #include <sys/types.h> | |
39 | #include <fcntl.h> | |
afb48eae | 40 | #include <unistd.h> |
70accc14 | 41 | #include <inttypes.h> |
a99343cf | 42 | #include <ftw.h> |
11e1d048 | 43 | #include <string.h> |
4c8bfb7e | 44 | |
a44bc4c9 MD |
45 | #include <babeltrace/ctf-ir/metadata.h> /* for clocks */ |
46 | ||
24d5af5b MD |
47 | #define PARTIAL_ERROR_SLEEP 3 /* 3 seconds */ |
48 | ||
afb48eae | 49 | #define DEFAULT_FILE_ARRAY_SIZE 1 |
9fe26ec7 MD |
50 | |
51 | /* | |
52 | * casting these to const char * to please libpopt. It allocates memory | |
53 | * behind our back (this is of course not documented). | |
54 | */ | |
11e1d048 | 55 | static char *opt_input_format, *opt_output_format; |
34ac0e6c | 56 | |
4e85cbfd | 57 | static GPtrArray *opt_input_paths; |
9fe26ec7 | 58 | static char *opt_output_path; |
34ac0e6c | 59 | |
afb48eae AA |
60 | static struct format *fmt_read; |
61 | ||
11e1d048 | 62 | static |
bbefb8dd MD |
63 | void strlower(char *str) |
64 | { | |
65 | while (*str) { | |
34224260 | 66 | *str = tolower((int) *str); |
bbefb8dd MD |
67 | str++; |
68 | } | |
69 | } | |
70 | ||
34ac0e6c MD |
71 | enum { |
72 | OPT_NONE = 0, | |
9fe26ec7 MD |
73 | OPT_OUTPUT_PATH, |
74 | OPT_INPUT_FORMAT, | |
75 | OPT_OUTPUT_FORMAT, | |
34ac0e6c | 76 | OPT_HELP, |
7fb21036 | 77 | OPT_LIST, |
34ac0e6c MD |
78 | OPT_VERBOSE, |
79 | OPT_DEBUG, | |
d63ca2cd | 80 | OPT_NAMES, |
359d7456 | 81 | OPT_FIELDS, |
8d8ed9af | 82 | OPT_NO_DELTA, |
11ac6674 | 83 | OPT_CLOCK_OFFSET, |
03798a93 | 84 | OPT_CLOCK_CYCLES, |
11ac6674 MD |
85 | OPT_CLOCK_SECONDS, |
86 | OPT_CLOCK_DATE, | |
87 | OPT_CLOCK_GMT, | |
82ace6d6 | 88 | OPT_CLOCK_FORCE_CORRELATE, |
34ac0e6c MD |
89 | }; |
90 | ||
9fe26ec7 MD |
91 | /* |
92 | * We are _not_ using POPT_ARG_STRING ability to store directly into | |
93 | * variables, because we want to cast the return to non-const, which is | |
94 | * not possible without using poptGetOptArg explicitly. | |
95 | */ | |
34ac0e6c MD |
96 | static struct poptOption long_options[] = { |
97 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
9fe26ec7 MD |
98 | { "output", 'w', POPT_ARG_STRING, NULL, OPT_NONE, NULL, NULL }, |
99 | { "input-format", 'i', POPT_ARG_STRING, NULL, OPT_INPUT_FORMAT, NULL, NULL }, | |
100 | { "output-format", 'o', POPT_ARG_STRING, NULL, OPT_OUTPUT_FORMAT, NULL, NULL }, | |
34ac0e6c | 101 | { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL }, |
7fb21036 | 102 | { "list", 'l', POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL }, |
34ac0e6c MD |
103 | { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL }, |
104 | { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL }, | |
cba1661c | 105 | { "names", 'n', POPT_ARG_STRING, NULL, OPT_NAMES, NULL, NULL }, |
359d7456 | 106 | { "fields", 'f', POPT_ARG_STRING, NULL, OPT_FIELDS, NULL, NULL }, |
8d8ed9af | 107 | { "no-delta", 0, POPT_ARG_NONE, NULL, OPT_NO_DELTA, NULL, NULL }, |
11ac6674 | 108 | { "clock-offset", 0, POPT_ARG_STRING, NULL, OPT_CLOCK_OFFSET, NULL, NULL }, |
03798a93 | 109 | { "clock-cycles", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_CYCLES, NULL, NULL }, |
11ac6674 MD |
110 | { "clock-seconds", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_SECONDS, NULL, NULL }, |
111 | { "clock-date", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_DATE, NULL, NULL }, | |
112 | { "clock-gmt", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_GMT, NULL, NULL }, | |
82ace6d6 | 113 | { "clock-force-correlate", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_FORCE_CORRELATE, NULL, NULL }, |
34ac0e6c MD |
114 | { NULL, 0, 0, NULL, 0, NULL, NULL }, |
115 | }; | |
116 | ||
7fb21036 MD |
117 | static void list_formats(FILE *fp) |
118 | { | |
119 | fprintf(fp, "\n"); | |
120 | bt_fprintf_format_list(fp); | |
121 | } | |
122 | ||
34ac0e6c | 123 | static void usage(FILE *fp) |
4c8bfb7e | 124 | { |
4c15b06b | 125 | fprintf(fp, "BabelTrace Trace Viewer and Converter %s\n\n", VERSION); |
4e85cbfd | 126 | fprintf(fp, "usage : babeltrace [OPTIONS] FILE...\n"); |
34ac0e6c | 127 | fprintf(fp, "\n"); |
4e85cbfd MD |
128 | fprintf(fp, " FILE Input trace file(s) and/or directory(ies)\n"); |
129 | fprintf(fp, " (space-separated)\n"); | |
130 | fprintf(fp, " -w, --output OUTPUT Output trace path (default: stdout)\n"); | |
34ac0e6c | 131 | fprintf(fp, "\n"); |
d2b8ea6b MD |
132 | fprintf(fp, " -i, --input-format FORMAT Input trace format (default: ctf)\n"); |
133 | fprintf(fp, " -o, --output-format FORMAT Output trace format (default: text)\n"); | |
34ac0e6c | 134 | fprintf(fp, "\n"); |
4d5678b9 MD |
135 | fprintf(fp, " -h, --help This help message\n"); |
136 | fprintf(fp, " -l, --list List available formats\n"); | |
137 | fprintf(fp, " -v, --verbose Verbose mode\n"); | |
cba1661c | 138 | fprintf(fp, " (or set BABELTRACE_VERBOSE environment variable)\n"); |
4d5678b9 | 139 | fprintf(fp, " -d, --debug Debug mode\n"); |
cba1661c | 140 | fprintf(fp, " (or set BABELTRACE_DEBUG environment variable)\n"); |
8d8ed9af | 141 | fprintf(fp, " --no-delta Do not print time delta between consecutive events\n"); |
359d7456 | 142 | fprintf(fp, " -n, --names name1<,name2,...> Print field names:\n"); |
82662ad4 | 143 | fprintf(fp, " (payload OR args OR arg)\n"); |
12c9c3bc MD |
144 | fprintf(fp, " none, all, scope, header, (context OR ctx)\n"); |
145 | fprintf(fp, " (default: payload,context)\n"); | |
359d7456 | 146 | fprintf(fp, " -f, --fields name1<,name2,...> Print additional fields:\n"); |
32cfb8ad | 147 | fprintf(fp, " all, trace, trace:hostname, trace:domain,\n"); |
f133896d | 148 | fprintf(fp, " trace:procname, trace:vpid, loglevel, emf, callsite.\n"); |
c3815874 | 149 | fprintf(fp, " (default: trace:hostname,trace:procname,trace:vpid)\n"); |
03798a93 | 150 | fprintf(fp, " --clock-cycles Timestamp in cycles\n"); |
11ac6674 MD |
151 | fprintf(fp, " --clock-offset seconds Clock offset in seconds\n"); |
152 | fprintf(fp, " --clock-seconds Print the timestamps as [sec.ns]\n"); | |
153 | fprintf(fp, " (default is: [hh:mm:ss.ns])\n"); | |
154 | fprintf(fp, " --clock-date Print clock date\n"); | |
155 | fprintf(fp, " --clock-gmt Print clock in GMT time zone (default: local time zone)\n"); | |
82ace6d6 MD |
156 | fprintf(fp, " --clock-force-correlate Assume that clocks are inherently correlated\n"); |
157 | fprintf(fp, " across traces.\n"); | |
7fb21036 | 158 | list_formats(fp); |
34ac0e6c MD |
159 | fprintf(fp, "\n"); |
160 | } | |
161 | ||
cba1661c MD |
162 | static int get_names_args(poptContext *pc) |
163 | { | |
164 | char *str, *strlist, *strctx; | |
9f2c779c | 165 | int ret = 0; |
cba1661c MD |
166 | |
167 | opt_payload_field_names = 0; | |
12c9c3bc | 168 | opt_context_field_names = 0; |
cba1661c MD |
169 | strlist = (char *) poptGetOptArg(*pc); |
170 | if (!strlist) { | |
171 | return -EINVAL; | |
172 | } | |
173 | str = strtok_r(strlist, ",", &strctx); | |
174 | do { | |
175 | if (!strcmp(str, "all")) | |
176 | opt_all_field_names = 1; | |
177 | else if (!strcmp(str, "scope")) | |
178 | opt_scope_field_names = 1; | |
179 | else if (!strcmp(str, "context") || !strcmp(str, "ctx")) | |
180 | opt_context_field_names = 1; | |
181 | else if (!strcmp(str, "header")) | |
182 | opt_header_field_names = 1; | |
183 | else if (!strcmp(str, "payload") || !strcmp(str, "args") || !strcmp(str, "arg")) | |
184 | opt_payload_field_names = 1; | |
12c9c3bc MD |
185 | else if (!strcmp(str, "none")) { |
186 | opt_all_field_names = 0; | |
187 | opt_scope_field_names = 0; | |
188 | opt_context_field_names = 0; | |
189 | opt_header_field_names = 0; | |
190 | opt_payload_field_names = 0; | |
191 | } else { | |
359d7456 | 192 | fprintf(stderr, "[error] unknown field name type %s\n", str); |
9f2c779c MD |
193 | free(strlist); |
194 | ret = -EINVAL; | |
195 | goto end; | |
359d7456 MD |
196 | } |
197 | } while ((str = strtok_r(NULL, ",", &strctx))); | |
9f2c779c MD |
198 | end: |
199 | free(strlist); | |
200 | return ret; | |
359d7456 MD |
201 | } |
202 | ||
203 | static int get_fields_args(poptContext *pc) | |
204 | { | |
205 | char *str, *strlist, *strctx; | |
9f2c779c | 206 | int ret = 0; |
359d7456 | 207 | |
359d7456 MD |
208 | strlist = (char *) poptGetOptArg(*pc); |
209 | if (!strlist) { | |
210 | return -EINVAL; | |
211 | } | |
212 | str = strtok_r(strlist, ",", &strctx); | |
213 | do { | |
c3815874 | 214 | opt_trace_default_fields = 0; |
359d7456 MD |
215 | if (!strcmp(str, "all")) |
216 | opt_all_fields = 1; | |
82662ad4 | 217 | else if (!strcmp(str, "trace")) |
359d7456 | 218 | opt_trace_field = 1; |
c3815874 MD |
219 | else if (!strcmp(str, "trace:hostname")) |
220 | opt_trace_hostname_field = 1; | |
8c250d87 | 221 | else if (!strcmp(str, "trace:domain")) |
359d7456 | 222 | opt_trace_domain_field = 1; |
8c250d87 | 223 | else if (!strcmp(str, "trace:procname")) |
359d7456 | 224 | opt_trace_procname_field = 1; |
8c250d87 | 225 | else if (!strcmp(str, "trace:vpid")) |
359d7456 | 226 | opt_trace_vpid_field = 1; |
d86d62f8 | 227 | else if (!strcmp(str, "loglevel")) |
359d7456 | 228 | opt_loglevel_field = 1; |
f6714e20 MD |
229 | else if (!strcmp(str, "emf")) |
230 | opt_emf_field = 1; | |
f133896d MD |
231 | else if (!strcmp(str, "callsite")) |
232 | opt_callsite_field = 1; | |
cba1661c | 233 | else { |
359d7456 | 234 | fprintf(stderr, "[error] unknown field type %s\n", str); |
9f2c779c MD |
235 | ret = -EINVAL; |
236 | goto end; | |
cba1661c MD |
237 | } |
238 | } while ((str = strtok_r(NULL, ",", &strctx))); | |
9f2c779c MD |
239 | end: |
240 | free(strlist); | |
241 | return ret; | |
cba1661c MD |
242 | } |
243 | ||
34ac0e6c MD |
244 | /* |
245 | * Return 0 if caller should continue, < 0 if caller should return | |
246 | * error, > 0 if caller should exit without reporting error. | |
247 | */ | |
bbefb8dd | 248 | static int parse_options(int argc, char **argv) |
34ac0e6c MD |
249 | { |
250 | poptContext pc; | |
251 | int opt, ret = 0; | |
9fe26ec7 | 252 | char *ipath; |
34ac0e6c | 253 | |
0f980a35 MD |
254 | if (argc == 1) { |
255 | usage(stdout); | |
256 | return 1; /* exit cleanly */ | |
257 | } | |
258 | ||
bbefb8dd | 259 | pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0); |
34ac0e6c MD |
260 | poptReadDefaultConfig(pc, 0); |
261 | ||
cba1661c | 262 | /* set default */ |
e505794f | 263 | opt_context_field_names = 1; |
cba1661c MD |
264 | opt_payload_field_names = 1; |
265 | ||
34ac0e6c MD |
266 | while ((opt = poptGetNextOpt(pc)) != -1) { |
267 | switch (opt) { | |
9fe26ec7 MD |
268 | case OPT_OUTPUT_PATH: |
269 | opt_output_path = (char *) poptGetOptArg(pc); | |
270 | if (!opt_output_path) { | |
271 | ret = -EINVAL; | |
272 | goto end; | |
273 | } | |
274 | break; | |
275 | case OPT_INPUT_FORMAT: | |
276 | opt_input_format = (char *) poptGetOptArg(pc); | |
277 | if (!opt_input_format) { | |
278 | ret = -EINVAL; | |
279 | goto end; | |
280 | } | |
281 | break; | |
282 | case OPT_OUTPUT_FORMAT: | |
283 | opt_output_format = (char *) poptGetOptArg(pc); | |
284 | if (!opt_output_format) { | |
285 | ret = -EINVAL; | |
286 | goto end; | |
287 | } | |
288 | break; | |
34ac0e6c | 289 | case OPT_HELP: |
7fb21036 | 290 | usage(stdout); |
34ac0e6c MD |
291 | ret = 1; /* exit cleanly */ |
292 | goto end; | |
7fb21036 MD |
293 | case OPT_LIST: |
294 | list_formats(stdout); | |
295 | ret = 1; | |
296 | goto end; | |
34ac0e6c MD |
297 | case OPT_VERBOSE: |
298 | babeltrace_verbose = 1; | |
299 | break; | |
cba1661c MD |
300 | case OPT_NAMES: |
301 | if (get_names_args(&pc)) { | |
302 | ret = -EINVAL; | |
303 | goto end; | |
304 | } | |
305 | break; | |
359d7456 MD |
306 | case OPT_FIELDS: |
307 | if (get_fields_args(&pc)) { | |
308 | ret = -EINVAL; | |
309 | goto end; | |
310 | } | |
311 | break; | |
34ac0e6c MD |
312 | case OPT_DEBUG: |
313 | babeltrace_debug = 1; | |
314 | break; | |
8d8ed9af | 315 | case OPT_NO_DELTA: |
359d7456 | 316 | opt_delta_field = 0; |
8d8ed9af | 317 | break; |
03798a93 JD |
318 | case OPT_CLOCK_CYCLES: |
319 | opt_clock_cycles = 1; | |
11ac6674 MD |
320 | break; |
321 | case OPT_CLOCK_OFFSET: | |
322 | { | |
9f2c779c | 323 | char *str; |
34224260 | 324 | char *endptr; |
11ac6674 | 325 | |
9f2c779c | 326 | str = (char *) poptGetOptArg(pc); |
11ac6674 MD |
327 | if (!str) { |
328 | fprintf(stderr, "[error] Missing --clock-offset argument\n"); | |
329 | ret = -EINVAL; | |
330 | goto end; | |
331 | } | |
332 | errno = 0; | |
333 | opt_clock_offset = strtoull(str, &endptr, 0); | |
334 | if (*endptr != '\0' || str == endptr || errno != 0) { | |
335 | fprintf(stderr, "[error] Incorrect --clock-offset argument: %s\n", str); | |
336 | ret = -EINVAL; | |
9f2c779c | 337 | free(str); |
11ac6674 MD |
338 | goto end; |
339 | } | |
9f2c779c | 340 | free(str); |
11ac6674 MD |
341 | break; |
342 | } | |
343 | case OPT_CLOCK_SECONDS: | |
344 | opt_clock_seconds = 1; | |
345 | break; | |
346 | case OPT_CLOCK_DATE: | |
347 | opt_clock_date = 1; | |
348 | break; | |
349 | case OPT_CLOCK_GMT: | |
350 | opt_clock_gmt = 1; | |
351 | break; | |
82ace6d6 MD |
352 | case OPT_CLOCK_FORCE_CORRELATE: |
353 | opt_clock_force_correlate = 1; | |
354 | break; | |
11ac6674 | 355 | |
34ac0e6c MD |
356 | default: |
357 | ret = -EINVAL; | |
358 | goto end; | |
359 | } | |
360 | } | |
361 | ||
4e85cbfd | 362 | do { |
9fe26ec7 | 363 | ipath = (char *) poptGetArg(pc); |
4e85cbfd MD |
364 | if (ipath) |
365 | g_ptr_array_add(opt_input_paths, (gpointer) ipath); | |
366 | } while (ipath); | |
367 | if (opt_input_paths->len == 0) { | |
34ac0e6c MD |
368 | ret = -EINVAL; |
369 | goto end; | |
370 | } | |
cba1661c | 371 | |
34ac0e6c MD |
372 | end: |
373 | if (pc) { | |
374 | poptFreeContext(pc); | |
375 | } | |
376 | return ret; | |
377 | } | |
378 | ||
a99343cf | 379 | static GPtrArray *traversed_paths = 0; |
afb48eae | 380 | |
a99343cf YB |
381 | /* |
382 | * traverse_trace_dir() is the callback function for File Tree Walk (nftw). | |
383 | * it receives the path of the current entry (file, dir, link..etc) with | |
384 | * a flag to indicate the type of the entry. | |
385 | * if the entry being visited is a directory and contains a metadata file, | |
386 | * then add the path to a global list to be processed later in | |
387 | * add_traces_recursive. | |
388 | */ | |
389 | static int traverse_trace_dir(const char *fpath, const struct stat *sb, | |
390 | int tflag, struct FTW *ftwbuf) | |
391 | { | |
392 | int dirfd, metafd; | |
393 | int closeret; | |
394 | ||
395 | if (tflag != FTW_D) | |
396 | return 0; | |
397 | ||
398 | dirfd = open(fpath, 0); | |
399 | if (dirfd < 0) { | |
400 | fprintf(stderr, "[error] [Context] Unable to open trace " | |
401 | "directory file descriptor.\n"); | |
402 | return 0; /* partial error */ | |
403 | } | |
404 | metafd = openat(dirfd, "metadata", O_RDONLY); | |
405 | if (metafd < 0) { | |
406 | closeret = close(dirfd); | |
407 | if (closeret < 0) { | |
408 | perror("close"); | |
409 | return -1; | |
410 | } | |
411 | /* No meta data, just return */ | |
412 | return 0; | |
413 | } else { | |
414 | closeret = close(metafd); | |
415 | if (closeret < 0) { | |
416 | perror("close"); | |
417 | return -1; /* failure */ | |
418 | } | |
419 | closeret = close(dirfd); | |
420 | if (closeret < 0) { | |
421 | perror("close"); | |
422 | return -1; /* failure */ | |
423 | } | |
424 | ||
425 | /* Add path to the global list */ | |
426 | if (traversed_paths == NULL) { | |
427 | fprintf(stderr, "[error] [Context] Invalid open path array.\n"); | |
428 | return -1; | |
429 | } | |
430 | g_ptr_array_add(traversed_paths, g_string_new(fpath)); | |
431 | } | |
432 | ||
433 | return 0; | |
434 | } | |
20d24215 YB |
435 | /* |
436 | * bt_context_add_traces_recursive: Open a trace recursively | |
437 | * | |
438 | * Find each trace present in the subdirectory starting from the given | |
613f532b MD |
439 | * path, and add them to the context. The packet_seek parameter can be |
440 | * NULL: this specify to use the default format packet_seek. | |
20d24215 | 441 | * |
b945d4a5 | 442 | * Return: 0 on success, < 0 on failure, > 0 on partial failure. |
20d24215 | 443 | * Unable to open toplevel: failure. |
b945d4a5 MD |
444 | * Unable to open some subdirectory or file: warn and continue (partial |
445 | * failure); | |
20d24215 YB |
446 | */ |
447 | int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path, | |
613f532b MD |
448 | const char *format_str, |
449 | void (*packet_seek)(struct stream_pos *pos, | |
450 | size_t offset, int whence)) | |
20d24215 | 451 | { |
a99343cf | 452 | |
20d24215 | 453 | GArray *trace_ids; |
b945d4a5 | 454 | int ret = 0; |
a99343cf | 455 | int i; |
20d24215 | 456 | |
a99343cf | 457 | /* Should lock traversed_paths mutex here if used in multithread */ |
20d24215 | 458 | |
a99343cf | 459 | traversed_paths = g_ptr_array_new(); |
20d24215 YB |
460 | trace_ids = g_array_new(FALSE, TRUE, sizeof(int)); |
461 | ||
a99343cf YB |
462 | ret = nftw(path, traverse_trace_dir, 10, 0); |
463 | ||
464 | /* Process the array if ntfw did not return a fatal error */ | |
465 | if (ret >= 0) { | |
466 | for (i = 0; i < traversed_paths->len; i++) { | |
467 | GString *trace_path = g_ptr_array_index(traversed_paths, | |
468 | i); | |
469 | int trace_id = bt_context_add_trace(ctx, | |
470 | trace_path->str, | |
471 | format_str, | |
472 | packet_seek, | |
473 | NULL, | |
474 | NULL); | |
20d24215 | 475 | if (trace_id < 0) { |
7f89ddce | 476 | fprintf(stderr, "[warning] [Context] cannot open trace \"%s\" from %s " |
a99343cf | 477 | "for reading.\n", trace_path->str, path); |
ca718275 | 478 | /* Allow to skip erroneous traces. */ |
b945d4a5 | 479 | ret = 1; /* partial error */ |
a99343cf YB |
480 | } else { |
481 | g_array_append_val(trace_ids, trace_id); | |
20d24215 | 482 | } |
a99343cf | 483 | g_string_free(trace_path, TRUE); |
20d24215 YB |
484 | } |
485 | } | |
a99343cf YB |
486 | g_ptr_array_free(traversed_paths, TRUE); |
487 | traversed_paths = NULL; | |
488 | ||
489 | /* Should unlock traversed paths mutex here if used in multithread */ | |
20d24215 | 490 | |
27cd3890 MD |
491 | /* |
492 | * Return an error if no trace can be opened. | |
493 | */ | |
b945d4a5 | 494 | if (trace_ids->len == 0) { |
27cd3890 | 495 | fprintf(stderr, "[error] Cannot open any trace for reading.\n\n"); |
b945d4a5 | 496 | ret = -ENOENT; /* failure */ |
27cd3890 MD |
497 | } |
498 | g_array_free(trace_ids, TRUE); | |
20d24215 YB |
499 | return ret; |
500 | } | |
a44bc4c9 | 501 | |
95d36295 JD |
502 | int convert_trace(struct trace_descriptor *td_write, |
503 | struct bt_context *ctx) | |
504 | { | |
e4195791 | 505 | struct bt_ctf_iter *iter; |
95d36295 | 506 | struct ctf_text_stream_pos *sout; |
e8c92a62 | 507 | struct bt_iter_pos begin_pos; |
c50d2a7a | 508 | struct bt_ctf_event *ctf_event; |
95d36295 JD |
509 | int ret; |
510 | ||
511 | sout = container_of(td_write, struct ctf_text_stream_pos, | |
512 | trace_descriptor); | |
513 | ||
514 | begin_pos.type = BT_SEEK_BEGIN; | |
e4195791 | 515 | iter = bt_ctf_iter_create(ctx, &begin_pos, NULL); |
95d36295 JD |
516 | if (!iter) { |
517 | ret = -1; | |
518 | goto error_iter; | |
519 | } | |
e4195791 | 520 | while ((ctf_event = bt_ctf_iter_read_event(iter))) { |
c50d2a7a | 521 | ret = sout->parent.event_cb(&sout->parent, ctf_event->parent->stream); |
95d36295 | 522 | if (ret) { |
3394d22e | 523 | fprintf(stderr, "[error] Writing event failed.\n"); |
95d36295 JD |
524 | goto end; |
525 | } | |
e4195791 | 526 | ret = bt_iter_next(bt_ctf_get_iter(iter)); |
95d36295 JD |
527 | if (ret < 0) |
528 | goto end; | |
529 | } | |
530 | ret = 0; | |
531 | ||
532 | end: | |
e4195791 | 533 | bt_ctf_iter_destroy(iter); |
95d36295 JD |
534 | error_iter: |
535 | return ret; | |
536 | } | |
537 | ||
bbefb8dd | 538 | int main(int argc, char **argv) |
34ac0e6c | 539 | { |
4e85cbfd | 540 | int ret, partial_error = 0, open_success = 0; |
afb48eae AA |
541 | struct format *fmt_write; |
542 | struct trace_descriptor *td_write; | |
95d36295 | 543 | struct bt_context *ctx; |
4e85cbfd MD |
544 | int i; |
545 | ||
546 | opt_input_paths = g_ptr_array_new(); | |
34ac0e6c MD |
547 | |
548 | ret = parse_options(argc, argv); | |
549 | if (ret < 0) { | |
3394d22e MD |
550 | fprintf(stderr, "Error parsing options.\n\n"); |
551 | usage(stderr); | |
4e85cbfd | 552 | g_ptr_array_free(opt_input_paths, TRUE); |
34ac0e6c MD |
553 | exit(EXIT_FAILURE); |
554 | } else if (ret > 0) { | |
4e85cbfd | 555 | g_ptr_array_free(opt_input_paths, TRUE); |
34ac0e6c MD |
556 | exit(EXIT_SUCCESS); |
557 | } | |
558 | printf_verbose("Verbose mode active.\n"); | |
559 | printf_debug("Debug mode active.\n"); | |
560 | ||
9fe26ec7 | 561 | if (opt_input_format) |
bbefb8dd | 562 | strlower(opt_input_format); |
9fe26ec7 | 563 | if (opt_output_format) |
bbefb8dd MD |
564 | strlower(opt_output_format); |
565 | ||
4e85cbfd MD |
566 | printf_verbose("Converting from directory(ies):\n"); |
567 | for (i = 0; i < opt_input_paths->len; i++) { | |
568 | const char *ipath = g_ptr_array_index(opt_input_paths, i); | |
569 | printf_verbose(" %s\n", ipath); | |
570 | } | |
34ac0e6c | 571 | printf_verbose("Converting from format: %s\n", |
b61922b5 | 572 | opt_input_format ? : "ctf <default>"); |
afb48eae | 573 | printf_verbose("Converting to directory: %s\n", |
478b6389 | 574 | opt_output_path ? : "<stdout>"); |
34ac0e6c | 575 | printf_verbose("Converting to format: %s\n", |
b61922b5 | 576 | opt_output_format ? : "text <default>"); |
bbefb8dd | 577 | |
11e1d048 MD |
578 | if (!opt_input_format) { |
579 | opt_input_format = strdup("ctf"); | |
580 | if (!opt_input_format) { | |
581 | partial_error = 1; | |
582 | goto end; | |
583 | } | |
584 | } | |
585 | if (!opt_output_format) { | |
586 | opt_output_format = strdup("text"); | |
587 | if (!opt_output_format) { | |
588 | partial_error = 1; | |
589 | goto end; | |
590 | } | |
591 | } | |
bbefb8dd MD |
592 | fmt_read = bt_lookup_format(g_quark_from_static_string(opt_input_format)); |
593 | if (!fmt_read) { | |
3394d22e | 594 | fprintf(stderr, "[error] Format \"%s\" is not supported.\n\n", |
bbefb8dd | 595 | opt_input_format); |
11e1d048 MD |
596 | partial_error = 1; |
597 | goto end; | |
bbefb8dd | 598 | } |
bbefb8dd MD |
599 | fmt_write = bt_lookup_format(g_quark_from_static_string(opt_output_format)); |
600 | if (!fmt_write) { | |
3394d22e | 601 | fprintf(stderr, "[error] format \"%s\" is not supported.\n\n", |
bbefb8dd | 602 | opt_output_format); |
11e1d048 MD |
603 | partial_error = 1; |
604 | goto end; | |
bbefb8dd MD |
605 | } |
606 | ||
6cba487f | 607 | ctx = bt_context_create(); |
d63c2d51 JD |
608 | if (!ctx) { |
609 | goto error_td_read; | |
610 | } | |
6cba487f | 611 | |
4e85cbfd MD |
612 | for (i = 0; i < opt_input_paths->len; i++) { |
613 | const char *ipath = g_ptr_array_index(opt_input_paths, i); | |
614 | ret = bt_context_add_traces_recursive(ctx, ipath, | |
615 | opt_input_format, NULL); | |
616 | if (ret < 0) { | |
617 | fprintf(stderr, "[error] opening trace \"%s\" for reading.\n\n", | |
618 | ipath); | |
619 | } else if (ret > 0) { | |
620 | fprintf(stderr, "[warning] errors occurred when opening trace \"%s\" for reading, continuing anyway.\n\n", | |
621 | ipath); | |
622 | open_success = 1; /* some traces were OK */ | |
623 | partial_error = 1; | |
624 | } else { | |
625 | open_success = 1; /* all traces were OK */ | |
626 | } | |
627 | } | |
628 | if (!open_success) { | |
629 | fprintf(stderr, "[error] none of the specified trace paths could be opened.\n\n"); | |
bbefb8dd MD |
630 | goto error_td_read; |
631 | } | |
6cba487f | 632 | |
5b80ddfb | 633 | td_write = fmt_write->open_trace(opt_output_path, O_RDWR, NULL, NULL); |
bbefb8dd | 634 | if (!td_write) { |
3394d22e | 635 | fprintf(stderr, "Error opening trace \"%s\" for writing.\n\n", |
b61922b5 | 636 | opt_output_path ? : "<none>"); |
bbefb8dd MD |
637 | goto error_td_write; |
638 | } | |
847bf71a | 639 | |
24d5af5b MD |
640 | /* |
641 | * Errors happened when opening traces, but we continue anyway. | |
642 | * sleep to let user see the stderr output before stdout. | |
643 | */ | |
4e85cbfd | 644 | if (partial_error) |
24d5af5b | 645 | sleep(PARTIAL_ERROR_SLEEP); |
24d5af5b | 646 | |
95d36295 | 647 | ret = convert_trace(td_write, ctx); |
46322b33 | 648 | if (ret) { |
3394d22e | 649 | fprintf(stderr, "Error printing trace.\n\n"); |
46322b33 MD |
650 | goto error_copy_trace; |
651 | } | |
847bf71a | 652 | |
bbefb8dd | 653 | fmt_write->close_trace(td_write); |
6cba487f MD |
654 | |
655 | bt_context_put(ctx); | |
afb48eae AA |
656 | printf_verbose("finished converting. Output written to:\n%s\n", |
657 | opt_output_path ? : "<stdout>"); | |
11e1d048 | 658 | goto end; |
34ac0e6c | 659 | |
bbefb8dd | 660 | /* Error handling */ |
46322b33 | 661 | error_copy_trace: |
bbefb8dd MD |
662 | fmt_write->close_trace(td_write); |
663 | error_td_write: | |
6cba487f | 664 | bt_context_put(ctx); |
bbefb8dd | 665 | error_td_read: |
11e1d048 MD |
666 | partial_error = 1; |
667 | ||
668 | /* teardown and exit */ | |
669 | end: | |
670 | free(opt_input_format); | |
671 | free(opt_output_format); | |
9fe26ec7 MD |
672 | free(opt_output_path); |
673 | for (i = 0; i < opt_input_paths->len; i++) { | |
674 | char *ipath = g_ptr_array_index(opt_input_paths, i); | |
675 | free(ipath); | |
676 | } | |
4e85cbfd | 677 | g_ptr_array_free(opt_input_paths, TRUE); |
11e1d048 MD |
678 | if (partial_error) |
679 | exit(EXIT_FAILURE); | |
680 | else | |
681 | exit(EXIT_SUCCESS); | |
4c8bfb7e | 682 | } |