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 format
*fmt_read
;
52 void strlower(char *str
)
74 OPT_CLOCK_FORCE_CORRELATE
,
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 { "clock-force-correlate", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_FORCE_CORRELATE
, NULL
, NULL
},
94 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
97 static void list_formats(FILE *fp
)
100 bt_fprintf_format_list(fp
);
103 static void usage(FILE *fp
)
105 fprintf(fp
, "BabelTrace Trace Viewer and Converter %s\n\n", VERSION
);
106 fprintf(fp
, "usage : babeltrace [OPTIONS] INPUT <OUTPUT>\n");
108 fprintf(fp
, " INPUT Input trace path\n");
109 fprintf(fp
, " OUTPUT Output trace path (default: stdout)\n");
111 fprintf(fp
, " -i, --input-format FORMAT Input trace format (default: ctf)\n");
112 fprintf(fp
, " -o, --output-format FORMAT Output trace format (default: text)\n");
114 fprintf(fp
, " -h, --help This help message\n");
115 fprintf(fp
, " -l, --list List available formats\n");
116 fprintf(fp
, " -v, --verbose Verbose mode\n");
117 fprintf(fp
, " (or set BABELTRACE_VERBOSE environment variable)\n");
118 fprintf(fp
, " -d, --debug Debug mode\n");
119 fprintf(fp
, " (or set BABELTRACE_DEBUG environment variable)\n");
120 fprintf(fp
, " --no-delta Do not print time delta between consecutive events\n");
121 fprintf(fp
, " -n, --names name1<,name2,...> Print field names:\n");
122 fprintf(fp
, " (payload OR args OR arg)\n");
123 fprintf(fp
, " all, scope, header, (context OR ctx)\n");
124 fprintf(fp
, " (payload active by default)\n");
125 fprintf(fp
, " -f, --fields name1<,name2,...> Print additional fields:\n");
126 fprintf(fp
, " all, trace, trace:domain, trace:procname,\n");
127 fprintf(fp
, " trace:vpid, loglevel.\n");
128 fprintf(fp
, " --clock-raw Disregard internal clock offset (use raw value)\n");
129 fprintf(fp
, " --clock-offset seconds Clock offset in seconds\n");
130 fprintf(fp
, " --clock-seconds Print the timestamps as [sec.ns]\n");
131 fprintf(fp
, " (default is: [hh:mm:ss.ns])\n");
132 fprintf(fp
, " --clock-date Print clock date\n");
133 fprintf(fp
, " --clock-gmt Print clock in GMT time zone (default: local time zone)\n");
134 fprintf(fp
, " --clock-force-correlate Assume that clocks are inherently correlated\n");
135 fprintf(fp
, " across traces.\n");
140 static int get_names_args(poptContext
*pc
)
142 char *str
, *strlist
, *strctx
;
144 opt_payload_field_names
= 0;
145 strlist
= (char *) poptGetOptArg(*pc
);
149 str
= strtok_r(strlist
, ",", &strctx
);
151 if (!strcmp(str
, "all"))
152 opt_all_field_names
= 1;
153 else if (!strcmp(str
, "scope"))
154 opt_scope_field_names
= 1;
155 else if (!strcmp(str
, "context") || !strcmp(str
, "ctx"))
156 opt_context_field_names
= 1;
157 else if (!strcmp(str
, "header"))
158 opt_header_field_names
= 1;
159 else if (!strcmp(str
, "payload") || !strcmp(str
, "args") || !strcmp(str
, "arg"))
160 opt_payload_field_names
= 1;
162 fprintf(stderr
, "[error] unknown field name type %s\n", str
);
165 } while ((str
= strtok_r(NULL
, ",", &strctx
)));
169 static int get_fields_args(poptContext
*pc
)
171 char *str
, *strlist
, *strctx
;
173 strlist
= (char *) poptGetOptArg(*pc
);
177 str
= strtok_r(strlist
, ",", &strctx
);
179 if (!strcmp(str
, "all"))
181 else if (!strcmp(str
, "trace"))
183 else if (!strcmp(str
, "trace:domain"))
184 opt_trace_domain_field
= 1;
185 else if (!strcmp(str
, "trace:procname"))
186 opt_trace_procname_field
= 1;
187 else if (!strcmp(str
, "trace:vpid"))
188 opt_trace_vpid_field
= 1;
189 else if (!strcmp(str
, "loglevel"))
190 opt_loglevel_field
= 1;
192 fprintf(stderr
, "[error] unknown field type %s\n", str
);
195 } while ((str
= strtok_r(NULL
, ",", &strctx
)));
200 * Return 0 if caller should continue, < 0 if caller should return
201 * error, > 0 if caller should exit without reporting error.
203 static int parse_options(int argc
, char **argv
)
210 return 1; /* exit cleanly */
213 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
214 poptReadDefaultConfig(pc
, 0);
217 opt_payload_field_names
= 1;
219 while ((opt
= poptGetNextOpt(pc
)) != -1) {
223 ret
= 1; /* exit cleanly */
226 list_formats(stdout
);
230 babeltrace_verbose
= 1;
233 if (get_names_args(&pc
)) {
239 if (get_fields_args(&pc
)) {
245 babeltrace_debug
= 1;
253 case OPT_CLOCK_OFFSET
:
257 str
= poptGetOptArg(pc
);
259 fprintf(stderr
, "[error] Missing --clock-offset argument\n");
264 opt_clock_offset
= strtoull(str
, &endptr
, 0);
265 if (*endptr
!= '\0' || str
== endptr
|| errno
!= 0) {
266 fprintf(stderr
, "[error] Incorrect --clock-offset argument: %s\n", str
);
272 case OPT_CLOCK_SECONDS
:
273 opt_clock_seconds
= 1;
281 case OPT_CLOCK_FORCE_CORRELATE
:
282 opt_clock_force_correlate
= 1;
291 opt_input_path
= poptGetArg(pc
);
292 if (!opt_input_path
) {
296 opt_output_path
= poptGetArg(pc
);
309 int convert_trace(struct trace_descriptor
*td_write
,
310 struct bt_context
*ctx
)
312 struct bt_iter
*iter
;
313 struct ctf_stream
*stream
;
314 struct ctf_stream_event
*event
;
315 struct ctf_text_stream_pos
*sout
;
316 struct bt_iter_pos begin_pos
;
319 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
322 begin_pos
.type
= BT_SEEK_BEGIN
;
323 iter
= bt_iter_create(ctx
, &begin_pos
, NULL
);
328 while (bt_iter_read_event(iter
, &stream
, &event
) == 0) {
329 ret
= sout
->parent
.event_cb(&sout
->parent
, stream
);
331 fprintf(stderr
, "[error] Writing event failed.\n");
334 ret
= bt_iter_next(iter
);
341 bt_iter_destroy(iter
);
346 int main(int argc
, char **argv
)
349 struct format
*fmt_write
;
350 struct trace_descriptor
*td_write
;
351 struct bt_context
*ctx
;
353 ret
= parse_options(argc
, argv
);
355 fprintf(stderr
, "Error parsing options.\n\n");
358 } else if (ret
> 0) {
361 printf_verbose("Verbose mode active.\n");
362 printf_debug("Debug mode active.\n");
364 if (opt_input_format
)
365 strlower(opt_input_format
);
366 if (opt_output_format
)
367 strlower(opt_output_format
);
369 printf_verbose("Converting from directory: %s\n", opt_input_path
);
370 printf_verbose("Converting from format: %s\n",
371 opt_input_format
? : "ctf <default>");
372 printf_verbose("Converting to directory: %s\n",
373 opt_output_path
? : "<stdout>");
374 printf_verbose("Converting to format: %s\n",
375 opt_output_format
? : "text <default>");
377 if (!opt_input_format
)
378 opt_input_format
= "ctf";
379 if (!opt_output_format
)
380 opt_output_format
= "text";
381 fmt_read
= bt_lookup_format(g_quark_from_static_string(opt_input_format
));
383 fprintf(stderr
, "[error] Format \"%s\" is not supported.\n\n",
387 fmt_write
= bt_lookup_format(g_quark_from_static_string(opt_output_format
));
389 fprintf(stderr
, "[error] format \"%s\" is not supported.\n\n",
394 ctx
= bt_context_create();
396 ret
= bt_context_add_traces_recursive(ctx
, opt_input_path
,
399 fprintf(stderr
, "[error] opening trace \"%s\" for reading.\n\n",
404 td_write
= fmt_write
->open_trace(opt_output_path
, O_RDWR
, NULL
, NULL
);
406 fprintf(stderr
, "Error opening trace \"%s\" for writing.\n\n",
407 opt_output_path
? : "<none>");
411 ret
= convert_trace(td_write
, ctx
);
413 fprintf(stderr
, "Error printing trace.\n\n");
414 goto error_copy_trace
;
417 fmt_write
->close_trace(td_write
);
420 printf_verbose("finished converting. Output written to:\n%s\n",
421 opt_output_path
? : "<stdout>");
426 fmt_write
->close_trace(td_write
);