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 | |
afb48eae | 21 | #define _XOPEN_SOURCE 700 |
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> | |
27 | #include <babeltrace/ctf-text/types.h> | |
6204d33c | 28 | #include <babeltrace/iterator.h> |
34ac0e6c MD |
29 | #include <popt.h> |
30 | #include <errno.h> | |
31 | #include <stdlib.h> | |
bbefb8dd MD |
32 | #include <ctype.h> |
33 | #include <sys/stat.h> | |
34 | #include <sys/types.h> | |
35 | #include <fcntl.h> | |
afb48eae AA |
36 | #include <ftw.h> |
37 | #include <dirent.h> | |
38 | #include <unistd.h> | |
70accc14 | 39 | #include <inttypes.h> |
4c8bfb7e | 40 | |
a44bc4c9 MD |
41 | #include <babeltrace/ctf-ir/metadata.h> /* for clocks */ |
42 | ||
afb48eae | 43 | #define DEFAULT_FILE_ARRAY_SIZE 1 |
bbefb8dd MD |
44 | static char *opt_input_format; |
45 | static char *opt_output_format; | |
34ac0e6c MD |
46 | |
47 | static const char *opt_input_path; | |
48 | static const char *opt_output_path; | |
49 | ||
afb48eae AA |
50 | static struct trace_collection trace_collection_read; |
51 | static struct format *fmt_read; | |
52 | ||
bbefb8dd MD |
53 | void strlower(char *str) |
54 | { | |
55 | while (*str) { | |
56 | *str = tolower(*str); | |
57 | str++; | |
58 | } | |
59 | } | |
60 | ||
34ac0e6c MD |
61 | enum { |
62 | OPT_NONE = 0, | |
63 | OPT_HELP, | |
7fb21036 | 64 | OPT_LIST, |
34ac0e6c MD |
65 | OPT_VERBOSE, |
66 | OPT_DEBUG, | |
d63ca2cd | 67 | OPT_NAMES, |
359d7456 | 68 | OPT_FIELDS, |
8d8ed9af | 69 | OPT_NO_DELTA, |
11ac6674 MD |
70 | OPT_CLOCK_OFFSET, |
71 | OPT_CLOCK_RAW, | |
72 | OPT_CLOCK_SECONDS, | |
73 | OPT_CLOCK_DATE, | |
74 | OPT_CLOCK_GMT, | |
34ac0e6c MD |
75 | }; |
76 | ||
77 | static struct poptOption long_options[] = { | |
78 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
79 | { "input-format", 'i', POPT_ARG_STRING, &opt_input_format, OPT_NONE, NULL, NULL }, | |
80 | { "output-format", 'o', POPT_ARG_STRING, &opt_output_format, OPT_NONE, NULL, NULL }, | |
81 | { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL }, | |
7fb21036 | 82 | { "list", 'l', POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL }, |
34ac0e6c MD |
83 | { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL }, |
84 | { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL }, | |
cba1661c | 85 | { "names", 'n', POPT_ARG_STRING, NULL, OPT_NAMES, NULL, NULL }, |
359d7456 | 86 | { "fields", 'f', POPT_ARG_STRING, NULL, OPT_FIELDS, NULL, NULL }, |
8d8ed9af | 87 | { "no-delta", 0, POPT_ARG_NONE, NULL, OPT_NO_DELTA, NULL, NULL }, |
11ac6674 MD |
88 | { "clock-offset", 0, POPT_ARG_STRING, NULL, OPT_CLOCK_OFFSET, NULL, NULL }, |
89 | { "clock-raw", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_RAW, NULL, NULL }, | |
90 | { "clock-seconds", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_SECONDS, NULL, NULL }, | |
91 | { "clock-date", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_DATE, NULL, NULL }, | |
92 | { "clock-gmt", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_GMT, NULL, NULL }, | |
34ac0e6c MD |
93 | { NULL, 0, 0, NULL, 0, NULL, NULL }, |
94 | }; | |
95 | ||
7fb21036 MD |
96 | static void list_formats(FILE *fp) |
97 | { | |
98 | fprintf(fp, "\n"); | |
99 | bt_fprintf_format_list(fp); | |
100 | } | |
101 | ||
34ac0e6c | 102 | static void usage(FILE *fp) |
4c8bfb7e | 103 | { |
00f7fbf0 | 104 | fprintf(fp, "BabelTrace Trace Converter %s\n\n", VERSION); |
4d5678b9 | 105 | fprintf(fp, "usage : babeltrace [OPTIONS] INPUT <OUTPUT>\n"); |
34ac0e6c | 106 | fprintf(fp, "\n"); |
4d5678b9 MD |
107 | fprintf(fp, " INPUT Input trace path\n"); |
108 | fprintf(fp, " OUTPUT Output trace path (default: stdout)\n"); | |
34ac0e6c | 109 | fprintf(fp, "\n"); |
d2b8ea6b MD |
110 | fprintf(fp, " -i, --input-format FORMAT Input trace format (default: ctf)\n"); |
111 | fprintf(fp, " -o, --output-format FORMAT Output trace format (default: text)\n"); | |
34ac0e6c | 112 | fprintf(fp, "\n"); |
4d5678b9 MD |
113 | fprintf(fp, " -h, --help This help message\n"); |
114 | fprintf(fp, " -l, --list List available formats\n"); | |
115 | fprintf(fp, " -v, --verbose Verbose mode\n"); | |
cba1661c | 116 | fprintf(fp, " (or set BABELTRACE_VERBOSE environment variable)\n"); |
4d5678b9 | 117 | fprintf(fp, " -d, --debug Debug mode\n"); |
cba1661c | 118 | fprintf(fp, " (or set BABELTRACE_DEBUG environment variable)\n"); |
8d8ed9af | 119 | fprintf(fp, " --no-delta Do not print time delta between consecutive events\n"); |
359d7456 | 120 | fprintf(fp, " -n, --names name1<,name2,...> Print field names:\n"); |
82662ad4 MD |
121 | fprintf(fp, " (payload OR args OR arg)\n"); |
122 | fprintf(fp, " all, scope, header, (context OR ctx)\n"); | |
cba1661c | 123 | fprintf(fp, " (payload active by default)\n"); |
359d7456 MD |
124 | fprintf(fp, " -f, --fields name1<,name2,...> Print additional fields:\n"); |
125 | fprintf(fp, " all, trace, trace:domain, trace:procname,\n"); | |
126 | fprintf(fp, " trace:vpid, loglevel.\n"); | |
11ac6674 MD |
127 | fprintf(fp, " --clock-raw Disregard internal clock offset (use raw value)\n"); |
128 | fprintf(fp, " --clock-offset seconds Clock offset in seconds\n"); | |
129 | fprintf(fp, " --clock-seconds Print the timestamps as [sec.ns]\n"); | |
130 | fprintf(fp, " (default is: [hh:mm:ss.ns])\n"); | |
131 | fprintf(fp, " --clock-date Print clock date\n"); | |
132 | fprintf(fp, " --clock-gmt Print clock in GMT time zone (default: local time zone)\n"); | |
7fb21036 | 133 | list_formats(fp); |
34ac0e6c MD |
134 | fprintf(fp, "\n"); |
135 | } | |
136 | ||
cba1661c MD |
137 | static int get_names_args(poptContext *pc) |
138 | { | |
139 | char *str, *strlist, *strctx; | |
140 | ||
141 | opt_payload_field_names = 0; | |
142 | strlist = (char *) poptGetOptArg(*pc); | |
143 | if (!strlist) { | |
144 | return -EINVAL; | |
145 | } | |
146 | str = strtok_r(strlist, ",", &strctx); | |
147 | do { | |
148 | if (!strcmp(str, "all")) | |
149 | opt_all_field_names = 1; | |
150 | else if (!strcmp(str, "scope")) | |
151 | opt_scope_field_names = 1; | |
152 | else if (!strcmp(str, "context") || !strcmp(str, "ctx")) | |
153 | opt_context_field_names = 1; | |
154 | else if (!strcmp(str, "header")) | |
155 | opt_header_field_names = 1; | |
156 | else if (!strcmp(str, "payload") || !strcmp(str, "args") || !strcmp(str, "arg")) | |
157 | opt_payload_field_names = 1; | |
359d7456 MD |
158 | else { |
159 | fprintf(stderr, "[error] unknown field name type %s\n", str); | |
160 | return -EINVAL; | |
161 | } | |
162 | } while ((str = strtok_r(NULL, ",", &strctx))); | |
163 | return 0; | |
164 | } | |
165 | ||
166 | static int get_fields_args(poptContext *pc) | |
167 | { | |
168 | char *str, *strlist, *strctx; | |
169 | ||
170 | opt_payload_field_names = 0; | |
171 | strlist = (char *) poptGetOptArg(*pc); | |
172 | if (!strlist) { | |
173 | return -EINVAL; | |
174 | } | |
175 | str = strtok_r(strlist, ",", &strctx); | |
176 | do { | |
177 | if (!strcmp(str, "all")) | |
178 | opt_all_fields = 1; | |
82662ad4 | 179 | else if (!strcmp(str, "trace")) |
359d7456 | 180 | opt_trace_field = 1; |
8c250d87 | 181 | else if (!strcmp(str, "trace:domain")) |
359d7456 | 182 | opt_trace_domain_field = 1; |
8c250d87 | 183 | else if (!strcmp(str, "trace:procname")) |
359d7456 | 184 | opt_trace_procname_field = 1; |
8c250d87 | 185 | else if (!strcmp(str, "trace:vpid")) |
359d7456 | 186 | opt_trace_vpid_field = 1; |
d86d62f8 | 187 | else if (!strcmp(str, "loglevel")) |
359d7456 | 188 | opt_loglevel_field = 1; |
cba1661c | 189 | else { |
359d7456 | 190 | fprintf(stderr, "[error] unknown field type %s\n", str); |
cba1661c MD |
191 | return -EINVAL; |
192 | } | |
193 | } while ((str = strtok_r(NULL, ",", &strctx))); | |
194 | return 0; | |
195 | } | |
196 | ||
34ac0e6c MD |
197 | /* |
198 | * Return 0 if caller should continue, < 0 if caller should return | |
199 | * error, > 0 if caller should exit without reporting error. | |
200 | */ | |
bbefb8dd | 201 | static int parse_options(int argc, char **argv) |
34ac0e6c MD |
202 | { |
203 | poptContext pc; | |
204 | int opt, ret = 0; | |
205 | ||
0f980a35 MD |
206 | if (argc == 1) { |
207 | usage(stdout); | |
208 | return 1; /* exit cleanly */ | |
209 | } | |
210 | ||
bbefb8dd | 211 | pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0); |
34ac0e6c MD |
212 | poptReadDefaultConfig(pc, 0); |
213 | ||
cba1661c MD |
214 | /* set default */ |
215 | opt_payload_field_names = 1; | |
216 | ||
34ac0e6c MD |
217 | while ((opt = poptGetNextOpt(pc)) != -1) { |
218 | switch (opt) { | |
219 | case OPT_HELP: | |
7fb21036 | 220 | usage(stdout); |
34ac0e6c MD |
221 | ret = 1; /* exit cleanly */ |
222 | goto end; | |
7fb21036 MD |
223 | case OPT_LIST: |
224 | list_formats(stdout); | |
225 | ret = 1; | |
226 | goto end; | |
34ac0e6c MD |
227 | case OPT_VERBOSE: |
228 | babeltrace_verbose = 1; | |
229 | break; | |
cba1661c MD |
230 | case OPT_NAMES: |
231 | if (get_names_args(&pc)) { | |
232 | ret = -EINVAL; | |
233 | goto end; | |
234 | } | |
235 | break; | |
359d7456 MD |
236 | case OPT_FIELDS: |
237 | if (get_fields_args(&pc)) { | |
238 | ret = -EINVAL; | |
239 | goto end; | |
240 | } | |
241 | break; | |
34ac0e6c MD |
242 | case OPT_DEBUG: |
243 | babeltrace_debug = 1; | |
244 | break; | |
8d8ed9af | 245 | case OPT_NO_DELTA: |
359d7456 | 246 | opt_delta_field = 0; |
8d8ed9af | 247 | break; |
11ac6674 MD |
248 | case OPT_CLOCK_RAW: |
249 | opt_clock_raw = 1; | |
250 | break; | |
251 | case OPT_CLOCK_OFFSET: | |
252 | { | |
253 | char *str, *endptr; | |
254 | ||
255 | str = poptGetOptArg(pc); | |
256 | if (!str) { | |
257 | fprintf(stderr, "[error] Missing --clock-offset argument\n"); | |
258 | ret = -EINVAL; | |
259 | goto end; | |
260 | } | |
261 | errno = 0; | |
262 | opt_clock_offset = strtoull(str, &endptr, 0); | |
263 | if (*endptr != '\0' || str == endptr || errno != 0) { | |
264 | fprintf(stderr, "[error] Incorrect --clock-offset argument: %s\n", str); | |
265 | ret = -EINVAL; | |
266 | goto end; | |
267 | } | |
268 | break; | |
269 | } | |
270 | case OPT_CLOCK_SECONDS: | |
271 | opt_clock_seconds = 1; | |
272 | break; | |
273 | case OPT_CLOCK_DATE: | |
274 | opt_clock_date = 1; | |
275 | break; | |
276 | case OPT_CLOCK_GMT: | |
277 | opt_clock_gmt = 1; | |
278 | break; | |
279 | ||
34ac0e6c MD |
280 | default: |
281 | ret = -EINVAL; | |
282 | goto end; | |
283 | } | |
284 | } | |
285 | ||
286 | opt_input_path = poptGetArg(pc); | |
287 | if (!opt_input_path) { | |
288 | ret = -EINVAL; | |
289 | goto end; | |
290 | } | |
291 | opt_output_path = poptGetArg(pc); | |
cba1661c | 292 | |
34ac0e6c MD |
293 | end: |
294 | if (pc) { | |
295 | poptFreeContext(pc); | |
296 | } | |
297 | return ret; | |
298 | } | |
299 | ||
afb48eae AA |
300 | static void init_trace_collection(struct trace_collection *tc) |
301 | { | |
302 | tc->array = g_ptr_array_sized_new(DEFAULT_FILE_ARRAY_SIZE); | |
a44bc4c9 | 303 | tc->clocks = g_hash_table_new(g_direct_hash, g_direct_equal); |
afb48eae AA |
304 | } |
305 | ||
306 | /* | |
307 | * finalize_trace_collection() closes the opened traces for read | |
308 | * and free the memory allocated for trace collection | |
309 | */ | |
310 | static void finalize_trace_collection(struct trace_collection *tc) | |
311 | { | |
312 | int i; | |
313 | ||
314 | for (i = 0; i < tc->array->len; i++) { | |
315 | struct trace_descriptor *temp = | |
316 | g_ptr_array_index(tc->array, i); | |
317 | fmt_read->close_trace(temp); | |
318 | } | |
319 | g_ptr_array_free(tc->array, TRUE); | |
a44bc4c9 MD |
320 | g_hash_table_destroy(tc->clocks); |
321 | } | |
322 | ||
323 | struct clock_match { | |
324 | GHashTable *clocks; | |
325 | struct ctf_clock *clock_match; | |
fa709ab2 | 326 | struct trace_collection *tc; |
a44bc4c9 MD |
327 | }; |
328 | ||
329 | static void check_clock_match(gpointer key, gpointer value, gpointer user_data) | |
330 | { | |
331 | struct clock_match *match = user_data; | |
332 | struct ctf_clock *clock_a = value, *clock_b; | |
333 | ||
334 | if (clock_a->uuid != 0) { | |
335 | /* | |
336 | * Lookup the the trace clocks into the collection | |
337 | * clocks. | |
338 | */ | |
339 | clock_b = g_hash_table_lookup(match->clocks, | |
340 | (gpointer) (unsigned long) clock_a->uuid); | |
341 | if (clock_b) { | |
342 | match->clock_match = clock_b; | |
343 | return; | |
344 | } | |
345 | } else if (clock_a->absolute) { | |
346 | /* | |
347 | * Absolute time references, such as NTP, are looked up | |
348 | * by clock name. | |
349 | */ | |
350 | clock_b = g_hash_table_lookup(match->clocks, | |
351 | (gpointer) (unsigned long) clock_a->name); | |
352 | if (clock_b) { | |
353 | match->clock_match = clock_b; | |
354 | return; | |
355 | } | |
356 | } | |
357 | } | |
358 | ||
359 | static void clock_add(gpointer key, gpointer value, gpointer user_data) | |
360 | { | |
fa709ab2 MD |
361 | struct clock_match *clock_match = user_data; |
362 | GHashTable *tc_clocks = clock_match->clocks; | |
a44bc4c9 MD |
363 | struct ctf_clock *t_clock = value; |
364 | GQuark v; | |
365 | ||
366 | if (t_clock->absolute) | |
367 | v = t_clock->name; | |
368 | else | |
369 | v = t_clock->uuid; | |
70accc14 MD |
370 | if (v) { |
371 | struct ctf_clock *tc_clock; | |
372 | ||
373 | tc_clock = g_hash_table_lookup(tc_clocks, | |
374 | (gpointer) (unsigned long) v); | |
375 | if (!tc_clock) { | |
fa709ab2 MD |
376 | /* |
377 | * For now, we only support CTF that has one | |
378 | * single clock. | |
379 | */ | |
380 | if (g_hash_table_size(tc_clocks) > 0) { | |
381 | fprintf(stderr, "[error] Only CTF traces with a single clock description are supported by this babeltrace version.\n"); | |
382 | } | |
383 | if (!clock_match->tc->single_clock) { | |
384 | clock_match->tc->single_clock = value; | |
385 | } | |
70accc14 MD |
386 | g_hash_table_insert(tc_clocks, |
387 | (gpointer) (unsigned long) v, | |
388 | value); | |
389 | } else { | |
390 | int64_t diff_ns; | |
391 | ||
392 | /* | |
393 | * Check that the offsets match. If not, warn | |
394 | * the user that we do an arbitrary choice. | |
395 | */ | |
396 | diff_ns = tc_clock->offset_s; | |
397 | diff_ns -= t_clock->offset_s; | |
398 | diff_ns *= 1000000000ULL; | |
399 | diff_ns += tc_clock->offset; | |
400 | diff_ns -= t_clock->offset; | |
401 | printf_debug("Clock \"%s\" offset between traces has a delta of %" PRIu64 " ns.", | |
402 | g_quark_to_string(tc_clock->name), | |
403 | diff_ns < 0 ? -diff_ns : diff_ns); | |
404 | if (diff_ns > 10000) { | |
405 | fprintf(stderr, "[warning] Clock \"%s\" offset differs between traces (delta %" PRIu64 " ns). Choosing one arbitrarily.\n", | |
fa709ab2 MD |
406 | g_quark_to_string(tc_clock->name), |
407 | diff_ns < 0 ? -diff_ns : diff_ns); | |
70accc14 MD |
408 | } |
409 | } | |
410 | } | |
afb48eae AA |
411 | } |
412 | ||
a44bc4c9 MD |
413 | /* |
414 | * Whenever we add a trace to the trace collection, check that we can | |
415 | * correlate this trace with at least one other clock in the trace. | |
416 | */ | |
417 | static int trace_collection_add(struct trace_collection *tc, | |
418 | struct trace_descriptor *td) | |
afb48eae | 419 | { |
a44bc4c9 MD |
420 | struct ctf_trace *trace = container_of(td, struct ctf_trace, parent); |
421 | ||
afb48eae | 422 | g_ptr_array_add(tc->array, td); |
fa709ab2 | 423 | trace->collection = tc; |
a44bc4c9 MD |
424 | |
425 | if (tc->array->len > 1) { | |
426 | struct clock_match clock_match = { | |
427 | .clocks = tc->clocks, | |
428 | .clock_match = NULL, | |
fa709ab2 | 429 | .tc = NULL, |
a44bc4c9 MD |
430 | }; |
431 | ||
432 | /* | |
433 | * With two or more traces, we need correlation info | |
434 | * avalable. | |
435 | */ | |
436 | g_hash_table_foreach(trace->clocks, | |
437 | check_clock_match, | |
438 | &clock_match); | |
439 | if (!clock_match.clock_match) { | |
440 | fprintf(stderr, "[error] No clocks can be correlated and multiple traces are added to the collection.\n"); | |
441 | goto error; | |
442 | } | |
443 | } | |
fa709ab2 MD |
444 | |
445 | { | |
446 | struct clock_match clock_match = { | |
447 | .clocks = tc->clocks, | |
448 | .clock_match = NULL, | |
449 | .tc = tc, | |
450 | }; | |
451 | ||
452 | /* | |
453 | * Add each clock from the trace clocks into the trace | |
454 | * collection clocks. | |
455 | */ | |
456 | g_hash_table_foreach(trace->clocks, | |
457 | clock_add, | |
458 | &clock_match); | |
459 | } | |
a44bc4c9 MD |
460 | return 0; |
461 | error: | |
462 | return -EPERM; | |
afb48eae AA |
463 | } |
464 | ||
95d36295 JD |
465 | int convert_trace(struct trace_descriptor *td_write, |
466 | struct bt_context *ctx) | |
467 | { | |
468 | struct babeltrace_iter *iter; | |
469 | struct ctf_stream *stream; | |
470 | struct ctf_stream_event *event; | |
471 | struct ctf_text_stream_pos *sout; | |
472 | struct trace_collection_pos begin_pos; | |
473 | int ret; | |
474 | ||
475 | sout = container_of(td_write, struct ctf_text_stream_pos, | |
476 | trace_descriptor); | |
477 | ||
478 | begin_pos.type = BT_SEEK_BEGIN; | |
479 | iter = babeltrace_iter_create(ctx, &begin_pos, NULL); | |
480 | if (!iter) { | |
481 | ret = -1; | |
482 | goto error_iter; | |
483 | } | |
484 | while (babeltrace_iter_read_event(iter, &stream, &event) == 0) { | |
485 | ret = sout->parent.event_cb(&sout->parent, stream); | |
486 | if (ret) { | |
3394d22e | 487 | fprintf(stderr, "[error] Writing event failed.\n"); |
95d36295 JD |
488 | goto end; |
489 | } | |
490 | ret = babeltrace_iter_next(iter); | |
491 | if (ret < 0) | |
492 | goto end; | |
493 | } | |
494 | ret = 0; | |
495 | ||
496 | end: | |
497 | babeltrace_iter_destroy(iter); | |
498 | error_iter: | |
499 | return ret; | |
500 | } | |
501 | ||
502 | ||
afb48eae AA |
503 | /* |
504 | * traverse_dir() is the callback functiion for File Tree Walk (nftw). | |
505 | * it receives the path of the current entry (file, dir, link..etc) with | |
506 | * a flag to indicate the type of the entry. | |
507 | * if the entry being visited is a directory and contains a metadata file, | |
508 | * then open it for reading and save a trace_descriptor to that directory | |
509 | * in the read trace collection. | |
510 | */ | |
511 | static int traverse_dir(const char *fpath, const struct stat *sb, | |
512 | int tflag, struct FTW *ftwbuf) | |
513 | { | |
514 | int dirfd; | |
515 | int fd; | |
516 | struct trace_descriptor *td_read; | |
a44bc4c9 | 517 | int ret; |
afb48eae AA |
518 | |
519 | if (tflag != FTW_D) | |
520 | return 0; | |
521 | dirfd = open(fpath, 0); | |
522 | if (dirfd < 0) { | |
3394d22e | 523 | fprintf(stderr, "[error] unable to open trace " |
afb48eae AA |
524 | "directory file descriptor.\n"); |
525 | return -1; | |
526 | } | |
527 | fd = openat(dirfd, "metadata", O_RDONLY); | |
528 | if (fd < 0) { | |
529 | close(dirfd); | |
530 | } else { | |
531 | close(fd); | |
532 | close(dirfd); | |
8c250d87 MD |
533 | td_read = fmt_read->open_trace(opt_input_path, |
534 | fpath, O_RDONLY, ctf_move_pos_slow, | |
ae23d232 | 535 | NULL); |
afb48eae | 536 | if (!td_read) { |
3394d22e | 537 | fprintf(stderr, "Error opening trace \"%s\" " |
afb48eae AA |
538 | "for reading.\n\n", fpath); |
539 | return -1; /* error */ | |
540 | } | |
a44bc4c9 MD |
541 | ret = trace_collection_add(&trace_collection_read, td_read); |
542 | if (ret) { | |
543 | return -1; | |
544 | } | |
afb48eae AA |
545 | } |
546 | return 0; /* success */ | |
547 | } | |
548 | ||
bbefb8dd | 549 | int main(int argc, char **argv) |
34ac0e6c MD |
550 | { |
551 | int ret; | |
afb48eae AA |
552 | struct format *fmt_write; |
553 | struct trace_descriptor *td_write; | |
95d36295 | 554 | struct bt_context *ctx; |
34ac0e6c MD |
555 | |
556 | ret = parse_options(argc, argv); | |
557 | if (ret < 0) { | |
3394d22e MD |
558 | fprintf(stderr, "Error parsing options.\n\n"); |
559 | usage(stderr); | |
34ac0e6c MD |
560 | exit(EXIT_FAILURE); |
561 | } else if (ret > 0) { | |
562 | exit(EXIT_SUCCESS); | |
563 | } | |
564 | printf_verbose("Verbose mode active.\n"); | |
565 | printf_debug("Debug mode active.\n"); | |
566 | ||
bbefb8dd MD |
567 | if (opt_input_format) |
568 | strlower(opt_input_format); | |
569 | if (opt_output_format) | |
570 | strlower(opt_output_format); | |
571 | ||
afb48eae | 572 | printf_verbose("Converting from directory: %s\n", opt_input_path); |
34ac0e6c | 573 | printf_verbose("Converting from format: %s\n", |
b61922b5 | 574 | opt_input_format ? : "ctf <default>"); |
afb48eae | 575 | printf_verbose("Converting to directory: %s\n", |
478b6389 | 576 | opt_output_path ? : "<stdout>"); |
34ac0e6c | 577 | printf_verbose("Converting to format: %s\n", |
b61922b5 | 578 | opt_output_format ? : "text <default>"); |
bbefb8dd | 579 | |
b61922b5 MD |
580 | if (!opt_input_format) |
581 | opt_input_format = "ctf"; | |
582 | if (!opt_output_format) | |
583 | opt_output_format = "text"; | |
bbefb8dd MD |
584 | fmt_read = bt_lookup_format(g_quark_from_static_string(opt_input_format)); |
585 | if (!fmt_read) { | |
3394d22e | 586 | fprintf(stderr, "[error] Format \"%s\" is not supported.\n\n", |
bbefb8dd MD |
587 | opt_input_format); |
588 | exit(EXIT_FAILURE); | |
589 | } | |
bbefb8dd MD |
590 | fmt_write = bt_lookup_format(g_quark_from_static_string(opt_output_format)); |
591 | if (!fmt_write) { | |
3394d22e | 592 | fprintf(stderr, "[error] format \"%s\" is not supported.\n\n", |
bbefb8dd MD |
593 | opt_output_format); |
594 | exit(EXIT_FAILURE); | |
595 | } | |
596 | ||
afb48eae AA |
597 | /* |
598 | * pass the input path to nftw() . | |
599 | * specify traverse_dir() as the callback function. | |
600 | * depth = 10 which is the max number of file descriptors | |
601 | * that nftw() can open at a given time. | |
602 | * flags = 0 check nftw documentation for more info . | |
603 | */ | |
604 | init_trace_collection(&trace_collection_read); | |
605 | ret = nftw(opt_input_path, traverse_dir, 10, 0); | |
606 | if (ret != 0) { | |
3394d22e | 607 | fprintf(stderr, "[error] opening trace \"%s\" for reading.\n\n", |
bbefb8dd MD |
608 | opt_input_path); |
609 | goto error_td_read; | |
610 | } | |
afb48eae | 611 | if (trace_collection_read.array->len == 0) { |
3394d22e | 612 | fprintf(stderr, "[warning] no metadata file was found." |
afb48eae AA |
613 | " no output was generated\n"); |
614 | return 0; | |
615 | } | |
95d36295 JD |
616 | ctx = bt_context_create(&trace_collection_read); |
617 | if (!ctx) { | |
3394d22e | 618 | fprintf(stderr, "Error allocating a new context\n"); |
95d36295 JD |
619 | goto error_td_read; |
620 | } | |
8c250d87 | 621 | td_write = fmt_write->open_trace(NULL, opt_output_path, O_RDWR, NULL, NULL); |
bbefb8dd | 622 | if (!td_write) { |
3394d22e | 623 | fprintf(stderr, "Error opening trace \"%s\" for writing.\n\n", |
b61922b5 | 624 | opt_output_path ? : "<none>"); |
bbefb8dd MD |
625 | goto error_td_write; |
626 | } | |
847bf71a | 627 | |
95d36295 | 628 | ret = convert_trace(td_write, ctx); |
46322b33 | 629 | if (ret) { |
3394d22e | 630 | fprintf(stderr, "Error printing trace.\n\n"); |
46322b33 MD |
631 | goto error_copy_trace; |
632 | } | |
847bf71a | 633 | |
bbefb8dd | 634 | fmt_write->close_trace(td_write); |
afb48eae | 635 | finalize_trace_collection(&trace_collection_read); |
95d36295 | 636 | bt_context_destroy(ctx); |
afb48eae AA |
637 | printf_verbose("finished converting. Output written to:\n%s\n", |
638 | opt_output_path ? : "<stdout>"); | |
bbefb8dd | 639 | exit(EXIT_SUCCESS); |
34ac0e6c | 640 | |
bbefb8dd | 641 | /* Error handling */ |
46322b33 | 642 | error_copy_trace: |
bbefb8dd MD |
643 | fmt_write->close_trace(td_write); |
644 | error_td_write: | |
afb48eae | 645 | finalize_trace_collection(&trace_collection_read); |
bbefb8dd MD |
646 | error_td_read: |
647 | exit(EXIT_FAILURE); | |
4c8bfb7e | 648 | } |