4 * Babeltrace Trace Converter
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
21 #define _XOPEN_SOURCE 700
23 #include <babeltrace/babeltrace.h>
24 #include <babeltrace/format.h>
25 #include <babeltrace/context.h>
26 #include <babeltrace/ctf/types.h>
27 #include <babeltrace/ctf-text/types.h>
28 #include <babeltrace/iterator.h>
34 #include <sys/types.h>
41 #include <babeltrace/ctf-ir/metadata.h> /* for clocks */
43 #define DEFAULT_FILE_ARRAY_SIZE 1
44 static char *opt_input_format
;
45 static char *opt_output_format
;
47 static const char *opt_input_path
;
48 static const char *opt_output_path
;
50 static struct trace_collection trace_collection_read
;
51 static struct format
*fmt_read
;
53 void strlower(char *str
)
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
},
82 { "list", 'l', POPT_ARG_NONE
, NULL
, OPT_LIST
, NULL
, NULL
},
83 { "verbose", 'v', POPT_ARG_NONE
, NULL
, OPT_VERBOSE
, NULL
, NULL
},
84 { "debug", 'd', POPT_ARG_NONE
, NULL
, OPT_DEBUG
, NULL
, NULL
},
85 { "names", 'n', POPT_ARG_STRING
, NULL
, OPT_NAMES
, NULL
, NULL
},
86 { "fields", 'f', POPT_ARG_STRING
, NULL
, OPT_FIELDS
, NULL
, NULL
},
87 { "no-delta", 0, POPT_ARG_NONE
, NULL
, OPT_NO_DELTA
, NULL
, NULL
},
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
},
93 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
96 static void list_formats(FILE *fp
)
99 bt_fprintf_format_list(fp
);
102 static void usage(FILE *fp
)
104 fprintf(fp
, "BabelTrace Trace Converter %s\n\n", VERSION
);
105 fprintf(fp
, "usage : babeltrace [OPTIONS] INPUT <OUTPUT>\n");
107 fprintf(fp
, " INPUT Input trace path\n");
108 fprintf(fp
, " OUTPUT Output trace path (default: stdout)\n");
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");
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");
116 fprintf(fp
, " (or set BABELTRACE_VERBOSE environment variable)\n");
117 fprintf(fp
, " -d, --debug Debug mode\n");
118 fprintf(fp
, " (or set BABELTRACE_DEBUG environment variable)\n");
119 fprintf(fp
, " --no-delta Do not print time delta between consecutive events\n");
120 fprintf(fp
, " -n, --names name1<,name2,...> Print field names:\n");
121 fprintf(fp
, " (payload OR args OR arg)\n");
122 fprintf(fp
, " all, scope, header, (context OR ctx)\n");
123 fprintf(fp
, " (payload active by default)\n");
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");
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");
137 static int get_names_args(poptContext
*pc
)
139 char *str
, *strlist
, *strctx
;
141 opt_payload_field_names
= 0;
142 strlist
= (char *) poptGetOptArg(*pc
);
146 str
= strtok_r(strlist
, ",", &strctx
);
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;
159 fprintf(stderr
, "[error] unknown field name type %s\n", str
);
162 } while ((str
= strtok_r(NULL
, ",", &strctx
)));
166 static int get_fields_args(poptContext
*pc
)
168 char *str
, *strlist
, *strctx
;
170 opt_payload_field_names
= 0;
171 strlist
= (char *) poptGetOptArg(*pc
);
175 str
= strtok_r(strlist
, ",", &strctx
);
177 if (!strcmp(str
, "all"))
179 else if (!strcmp(str
, "trace"))
181 else if (!strcmp(str
, "trace:domain"))
182 opt_trace_domain_field
= 1;
183 else if (!strcmp(str
, "trace:procname"))
184 opt_trace_procname_field
= 1;
185 else if (!strcmp(str
, "trace:vpid"))
186 opt_trace_vpid_field
= 1;
187 else if (!strcmp(str
, "loglevel"))
188 opt_loglevel_field
= 1;
190 fprintf(stderr
, "[error] unknown field type %s\n", str
);
193 } while ((str
= strtok_r(NULL
, ",", &strctx
)));
198 * Return 0 if caller should continue, < 0 if caller should return
199 * error, > 0 if caller should exit without reporting error.
201 static int parse_options(int argc
, char **argv
)
208 return 1; /* exit cleanly */
211 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
212 poptReadDefaultConfig(pc
, 0);
215 opt_payload_field_names
= 1;
217 while ((opt
= poptGetNextOpt(pc
)) != -1) {
221 ret
= 1; /* exit cleanly */
224 list_formats(stdout
);
228 babeltrace_verbose
= 1;
231 if (get_names_args(&pc
)) {
237 if (get_fields_args(&pc
)) {
243 babeltrace_debug
= 1;
251 case OPT_CLOCK_OFFSET
:
255 str
= poptGetOptArg(pc
);
257 fprintf(stderr
, "[error] Missing --clock-offset argument\n");
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
);
270 case OPT_CLOCK_SECONDS
:
271 opt_clock_seconds
= 1;
286 opt_input_path
= poptGetArg(pc
);
287 if (!opt_input_path
) {
291 opt_output_path
= poptGetArg(pc
);
300 static void init_trace_collection(struct trace_collection
*tc
)
302 tc
->array
= g_ptr_array_sized_new(DEFAULT_FILE_ARRAY_SIZE
);
303 tc
->clocks
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
304 tc
->single_clock_offset_avg
= 0;
305 tc
->offset_first
= 0;
306 tc
->delta_offset_first_sum
= 0;
311 * finalize_trace_collection() closes the opened traces for read
312 * and free the memory allocated for trace collection
314 static void finalize_trace_collection(struct trace_collection
*tc
)
318 for (i
= 0; i
< tc
->array
->len
; i
++) {
319 struct trace_descriptor
*temp
=
320 g_ptr_array_index(tc
->array
, i
);
321 fmt_read
->close_trace(temp
);
323 g_ptr_array_free(tc
->array
, TRUE
);
324 g_hash_table_destroy(tc
->clocks
);
329 struct ctf_clock
*clock_match
;
330 struct trace_collection
*tc
;
333 static void check_clock_match(gpointer key
, gpointer value
, gpointer user_data
)
335 struct clock_match
*match
= user_data
;
336 struct ctf_clock
*clock_a
= value
, *clock_b
;
338 if (clock_a
->uuid
!= 0) {
340 * Lookup the the trace clocks into the collection
343 clock_b
= g_hash_table_lookup(match
->clocks
,
344 (gpointer
) (unsigned long) clock_a
->uuid
);
346 match
->clock_match
= clock_b
;
349 } else if (clock_a
->absolute
) {
351 * Absolute time references, such as NTP, are looked up
354 clock_b
= g_hash_table_lookup(match
->clocks
,
355 (gpointer
) (unsigned long) clock_a
->name
);
357 match
->clock_match
= clock_b
;
363 static void clock_add(gpointer key
, gpointer value
, gpointer user_data
)
365 struct clock_match
*clock_match
= user_data
;
366 GHashTable
*tc_clocks
= clock_match
->clocks
;
367 struct ctf_clock
*t_clock
= value
;
370 if (t_clock
->absolute
)
375 struct ctf_clock
*tc_clock
;
377 tc_clock
= g_hash_table_lookup(tc_clocks
,
378 (gpointer
) (unsigned long) v
);
381 * For now, we only support CTF that has one
382 * single clock uuid or name (absolute ref).
384 if (g_hash_table_size(tc_clocks
) > 0) {
385 fprintf(stderr
, "[error] Only CTF traces with a single clock description are supported by this babeltrace version.\n");
387 if (!clock_match
->tc
->offset_nr
) {
388 clock_match
->tc
->offset_first
=
389 (t_clock
->offset_s
* 1000000000ULL) + t_clock
->offset
;
390 clock_match
->tc
->delta_offset_first_sum
= 0;
391 clock_match
->tc
->offset_nr
++;
392 clock_match
->tc
->single_clock_offset_avg
=
393 clock_match
->tc
->offset_first
;
395 g_hash_table_insert(tc_clocks
,
396 (gpointer
) (unsigned long) v
,
402 * Check that the offsets match. If not, warn
403 * the user that we do an arbitrary choice.
405 diff_ns
= tc_clock
->offset_s
;
406 diff_ns
-= t_clock
->offset_s
;
407 diff_ns
*= 1000000000ULL;
408 diff_ns
+= tc_clock
->offset
;
409 diff_ns
-= t_clock
->offset
;
410 printf_debug("Clock \"%s\" offset between traces has a delta of %" PRIu64
" ns.",
411 g_quark_to_string(tc_clock
->name
),
412 diff_ns
< 0 ? -diff_ns
: diff_ns
);
413 if (diff_ns
> 10000) {
414 fprintf(stderr
, "[warning] Clock \"%s\" offset differs between traces (delta %" PRIu64
" ns). Using average.\n",
415 g_quark_to_string(tc_clock
->name
),
416 diff_ns
< 0 ? -diff_ns
: diff_ns
);
418 /* Compute average */
419 clock_match
->tc
->delta_offset_first_sum
+=
420 (t_clock
->offset_s
* 1000000000ULL) + t_clock
->offset
421 - clock_match
->tc
->offset_first
;
422 clock_match
->tc
->offset_nr
++;
423 clock_match
->tc
->single_clock_offset_avg
=
424 clock_match
->tc
->offset_first
425 + (clock_match
->tc
->delta_offset_first_sum
/ clock_match
->tc
->offset_nr
);
431 * Whenever we add a trace to the trace collection, check that we can
432 * correlate this trace with at least one other clock in the trace.
434 static int trace_collection_add(struct trace_collection
*tc
,
435 struct trace_descriptor
*td
)
437 struct ctf_trace
*trace
= container_of(td
, struct ctf_trace
, parent
);
439 g_ptr_array_add(tc
->array
, td
);
440 trace
->collection
= tc
;
442 if (tc
->array
->len
> 1) {
443 struct clock_match clock_match
= {
444 .clocks
= tc
->clocks
,
450 * With two or more traces, we need correlation info
453 g_hash_table_foreach(trace
->clocks
,
456 if (!clock_match
.clock_match
) {
457 fprintf(stderr
, "[error] No clocks can be correlated and multiple traces are added to the collection.\n");
463 struct clock_match clock_match
= {
464 .clocks
= tc
->clocks
,
470 * Add each clock from the trace clocks into the trace
473 g_hash_table_foreach(trace
->clocks
,
482 int convert_trace(struct trace_descriptor
*td_write
,
483 struct bt_context
*ctx
)
485 struct babeltrace_iter
*iter
;
486 struct ctf_stream
*stream
;
487 struct ctf_stream_event
*event
;
488 struct ctf_text_stream_pos
*sout
;
489 struct trace_collection_pos begin_pos
;
492 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
495 begin_pos
.type
= BT_SEEK_BEGIN
;
496 iter
= babeltrace_iter_create(ctx
, &begin_pos
, NULL
);
501 while (babeltrace_iter_read_event(iter
, &stream
, &event
) == 0) {
502 ret
= sout
->parent
.event_cb(&sout
->parent
, stream
);
504 fprintf(stderr
, "[error] Writing event failed.\n");
507 ret
= babeltrace_iter_next(iter
);
514 babeltrace_iter_destroy(iter
);
521 * traverse_dir() is the callback functiion for File Tree Walk (nftw).
522 * it receives the path of the current entry (file, dir, link..etc) with
523 * a flag to indicate the type of the entry.
524 * if the entry being visited is a directory and contains a metadata file,
525 * then open it for reading and save a trace_descriptor to that directory
526 * in the read trace collection.
528 static int traverse_dir(const char *fpath
, const struct stat
*sb
,
529 int tflag
, struct FTW
*ftwbuf
)
533 struct trace_descriptor
*td_read
;
538 dirfd
= open(fpath
, 0);
540 fprintf(stderr
, "[error] unable to open trace "
541 "directory file descriptor.\n");
544 fd
= openat(dirfd
, "metadata", O_RDONLY
);
550 td_read
= fmt_read
->open_trace(opt_input_path
,
551 fpath
, O_RDONLY
, ctf_move_pos_slow
,
554 fprintf(stderr
, "Error opening trace \"%s\" "
555 "for reading.\n\n", fpath
);
556 return -1; /* error */
558 ret
= trace_collection_add(&trace_collection_read
, td_read
);
563 return 0; /* success */
566 int main(int argc
, char **argv
)
569 struct format
*fmt_write
;
570 struct trace_descriptor
*td_write
;
571 struct bt_context
*ctx
;
573 ret
= parse_options(argc
, argv
);
575 fprintf(stderr
, "Error parsing options.\n\n");
578 } else if (ret
> 0) {
581 printf_verbose("Verbose mode active.\n");
582 printf_debug("Debug mode active.\n");
584 if (opt_input_format
)
585 strlower(opt_input_format
);
586 if (opt_output_format
)
587 strlower(opt_output_format
);
589 printf_verbose("Converting from directory: %s\n", opt_input_path
);
590 printf_verbose("Converting from format: %s\n",
591 opt_input_format
? : "ctf <default>");
592 printf_verbose("Converting to directory: %s\n",
593 opt_output_path
? : "<stdout>");
594 printf_verbose("Converting to format: %s\n",
595 opt_output_format
? : "text <default>");
597 if (!opt_input_format
)
598 opt_input_format
= "ctf";
599 if (!opt_output_format
)
600 opt_output_format
= "text";
601 fmt_read
= bt_lookup_format(g_quark_from_static_string(opt_input_format
));
603 fprintf(stderr
, "[error] Format \"%s\" is not supported.\n\n",
607 fmt_write
= bt_lookup_format(g_quark_from_static_string(opt_output_format
));
609 fprintf(stderr
, "[error] format \"%s\" is not supported.\n\n",
615 * pass the input path to nftw() .
616 * specify traverse_dir() as the callback function.
617 * depth = 10 which is the max number of file descriptors
618 * that nftw() can open at a given time.
619 * flags = 0 check nftw documentation for more info .
621 init_trace_collection(&trace_collection_read
);
622 ret
= nftw(opt_input_path
, traverse_dir
, 10, 0);
624 fprintf(stderr
, "[error] opening trace \"%s\" for reading.\n\n",
628 if (trace_collection_read
.array
->len
== 0) {
629 fprintf(stderr
, "[warning] no metadata file was found."
630 " no output was generated\n");
633 ctx
= bt_context_create(&trace_collection_read
);
635 fprintf(stderr
, "Error allocating a new context\n");
638 td_write
= fmt_write
->open_trace(NULL
, opt_output_path
, O_RDWR
, NULL
, NULL
);
640 fprintf(stderr
, "Error opening trace \"%s\" for writing.\n\n",
641 opt_output_path
? : "<none>");
645 ret
= convert_trace(td_write
, ctx
);
647 fprintf(stderr
, "Error printing trace.\n\n");
648 goto error_copy_trace
;
651 fmt_write
->close_trace(td_write
);
652 finalize_trace_collection(&trace_collection_read
);
653 bt_context_destroy(ctx
);
654 printf_verbose("finished converting. Output written to:\n%s\n",
655 opt_output_path
? : "<stdout>");
660 fmt_write
->close_trace(td_write
);
662 finalize_trace_collection(&trace_collection_read
);