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