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