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