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
)
76 static struct poptOption long_options
[] = {
77 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
78 { "input-format", 'i', POPT_ARG_STRING
, &opt_input_format
, OPT_NONE
, NULL
, NULL
},
79 { "output-format", 'o', POPT_ARG_STRING
, &opt_output_format
, OPT_NONE
, NULL
, NULL
},
80 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
81 { "list", 'l', POPT_ARG_NONE
, NULL
, OPT_LIST
, NULL
, NULL
},
82 { "verbose", 'v', POPT_ARG_NONE
, NULL
, OPT_VERBOSE
, NULL
, NULL
},
83 { "debug", 'd', POPT_ARG_NONE
, NULL
, OPT_DEBUG
, NULL
, NULL
},
84 { "names", 'n', POPT_ARG_STRING
, NULL
, OPT_NAMES
, NULL
, NULL
},
85 { "fields", 'f', POPT_ARG_STRING
, NULL
, OPT_FIELDS
, NULL
, NULL
},
86 { "no-delta", 0, POPT_ARG_NONE
, NULL
, OPT_NO_DELTA
, NULL
, NULL
},
87 { "clock-offset", 0, POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET
, NULL
, NULL
},
88 { "clock-raw", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_RAW
, NULL
, NULL
},
89 { "clock-seconds", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_SECONDS
, NULL
, NULL
},
90 { "clock-date", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_DATE
, NULL
, NULL
},
91 { "clock-gmt", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_GMT
, NULL
, NULL
},
92 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
95 static void list_formats(FILE *fp
)
98 bt_fprintf_format_list(fp
);
101 static void usage(FILE *fp
)
103 fprintf(fp
, "BabelTrace Trace Viewer and Converter %s\n\n", VERSION
);
104 fprintf(fp
, "usage : babeltrace [OPTIONS] INPUT <OUTPUT>\n");
106 fprintf(fp
, " INPUT Input trace path\n");
107 fprintf(fp
, " OUTPUT Output trace path (default: stdout)\n");
109 fprintf(fp
, " -i, --input-format FORMAT Input trace format (default: ctf)\n");
110 fprintf(fp
, " -o, --output-format FORMAT Output trace format (default: text)\n");
112 fprintf(fp
, " -h, --help This help message\n");
113 fprintf(fp
, " -l, --list List available formats\n");
114 fprintf(fp
, " -v, --verbose Verbose mode\n");
115 fprintf(fp
, " (or set BABELTRACE_VERBOSE environment variable)\n");
116 fprintf(fp
, " -d, --debug Debug mode\n");
117 fprintf(fp
, " (or set BABELTRACE_DEBUG environment variable)\n");
118 fprintf(fp
, " --no-delta Do not print time delta between consecutive events\n");
119 fprintf(fp
, " -n, --names name1<,name2,...> Print field names:\n");
120 fprintf(fp
, " (payload OR args OR arg)\n");
121 fprintf(fp
, " all, scope, header, (context OR ctx)\n");
122 fprintf(fp
, " (payload active by default)\n");
123 fprintf(fp
, " -f, --fields name1<,name2,...> Print additional fields:\n");
124 fprintf(fp
, " all, trace, trace:domain, trace:procname,\n");
125 fprintf(fp
, " trace:vpid, loglevel.\n");
126 fprintf(fp
, " --clock-raw Disregard internal clock offset (use raw value)\n");
127 fprintf(fp
, " --clock-offset seconds Clock offset in seconds\n");
128 fprintf(fp
, " --clock-seconds Print the timestamps as [sec.ns]\n");
129 fprintf(fp
, " (default is: [hh:mm:ss.ns])\n");
130 fprintf(fp
, " --clock-date Print clock date\n");
131 fprintf(fp
, " --clock-gmt Print clock in GMT time zone (default: local time zone)\n");
136 static int get_names_args(poptContext
*pc
)
138 char *str
, *strlist
, *strctx
;
140 opt_payload_field_names
= 0;
141 strlist
= (char *) poptGetOptArg(*pc
);
145 str
= strtok_r(strlist
, ",", &strctx
);
147 if (!strcmp(str
, "all"))
148 opt_all_field_names
= 1;
149 else if (!strcmp(str
, "scope"))
150 opt_scope_field_names
= 1;
151 else if (!strcmp(str
, "context") || !strcmp(str
, "ctx"))
152 opt_context_field_names
= 1;
153 else if (!strcmp(str
, "header"))
154 opt_header_field_names
= 1;
155 else if (!strcmp(str
, "payload") || !strcmp(str
, "args") || !strcmp(str
, "arg"))
156 opt_payload_field_names
= 1;
158 fprintf(stderr
, "[error] unknown field name type %s\n", str
);
161 } while ((str
= strtok_r(NULL
, ",", &strctx
)));
165 static int get_fields_args(poptContext
*pc
)
167 char *str
, *strlist
, *strctx
;
169 strlist
= (char *) poptGetOptArg(*pc
);
173 str
= strtok_r(strlist
, ",", &strctx
);
175 if (!strcmp(str
, "all"))
177 else if (!strcmp(str
, "trace"))
179 else if (!strcmp(str
, "trace:domain"))
180 opt_trace_domain_field
= 1;
181 else if (!strcmp(str
, "trace:procname"))
182 opt_trace_procname_field
= 1;
183 else if (!strcmp(str
, "trace:vpid"))
184 opt_trace_vpid_field
= 1;
185 else if (!strcmp(str
, "loglevel"))
186 opt_loglevel_field
= 1;
188 fprintf(stderr
, "[error] unknown field type %s\n", str
);
191 } while ((str
= strtok_r(NULL
, ",", &strctx
)));
196 * Return 0 if caller should continue, < 0 if caller should return
197 * error, > 0 if caller should exit without reporting error.
199 static int parse_options(int argc
, char **argv
)
206 return 1; /* exit cleanly */
209 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
210 poptReadDefaultConfig(pc
, 0);
213 opt_payload_field_names
= 1;
215 while ((opt
= poptGetNextOpt(pc
)) != -1) {
219 ret
= 1; /* exit cleanly */
222 list_formats(stdout
);
226 babeltrace_verbose
= 1;
229 if (get_names_args(&pc
)) {
235 if (get_fields_args(&pc
)) {
241 babeltrace_debug
= 1;
249 case OPT_CLOCK_OFFSET
:
253 str
= poptGetOptArg(pc
);
255 fprintf(stderr
, "[error] Missing --clock-offset argument\n");
260 opt_clock_offset
= strtoull(str
, &endptr
, 0);
261 if (*endptr
!= '\0' || str
== endptr
|| errno
!= 0) {
262 fprintf(stderr
, "[error] Incorrect --clock-offset argument: %s\n", str
);
268 case OPT_CLOCK_SECONDS
:
269 opt_clock_seconds
= 1;
284 opt_input_path
= poptGetArg(pc
);
285 if (!opt_input_path
) {
289 opt_output_path
= poptGetArg(pc
);
302 int convert_trace(struct trace_descriptor
*td_write
,
303 struct bt_context
*ctx
)
305 struct bt_iter
*iter
;
306 struct ctf_stream
*stream
;
307 struct ctf_stream_event
*event
;
308 struct ctf_text_stream_pos
*sout
;
309 struct bt_iter_pos begin_pos
;
312 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
315 begin_pos
.type
= BT_SEEK_BEGIN
;
316 iter
= bt_iter_create(ctx
, &begin_pos
, NULL
);
321 while (bt_iter_read_event(iter
, &stream
, &event
) == 0) {
322 ret
= sout
->parent
.event_cb(&sout
->parent
, stream
);
324 fprintf(stderr
, "[error] Writing event failed.\n");
327 ret
= bt_iter_next(iter
);
334 bt_iter_destroy(iter
);
339 int main(int argc
, char **argv
)
342 struct format
*fmt_write
;
343 struct trace_descriptor
*td_write
;
344 struct bt_context
*ctx
;
346 ret
= parse_options(argc
, argv
);
348 fprintf(stderr
, "Error parsing options.\n\n");
351 } else if (ret
> 0) {
354 printf_verbose("Verbose mode active.\n");
355 printf_debug("Debug mode active.\n");
357 if (opt_input_format
)
358 strlower(opt_input_format
);
359 if (opt_output_format
)
360 strlower(opt_output_format
);
362 printf_verbose("Converting from directory: %s\n", opt_input_path
);
363 printf_verbose("Converting from format: %s\n",
364 opt_input_format
? : "ctf <default>");
365 printf_verbose("Converting to directory: %s\n",
366 opt_output_path
? : "<stdout>");
367 printf_verbose("Converting to format: %s\n",
368 opt_output_format
? : "text <default>");
370 if (!opt_input_format
)
371 opt_input_format
= "ctf";
372 if (!opt_output_format
)
373 opt_output_format
= "text";
374 fmt_read
= bt_lookup_format(g_quark_from_static_string(opt_input_format
));
376 fprintf(stderr
, "[error] Format \"%s\" is not supported.\n\n",
380 fmt_write
= bt_lookup_format(g_quark_from_static_string(opt_output_format
));
382 fprintf(stderr
, "[error] format \"%s\" is not supported.\n\n",
387 ctx
= bt_context_create();
389 ret
= bt_context_add_traces(ctx
, opt_input_path
,
392 fprintf(stderr
, "[error] opening trace \"%s\" for reading.\n\n",
397 td_write
= fmt_write
->open_trace(opt_output_path
, O_RDWR
, NULL
, NULL
);
399 fprintf(stderr
, "Error opening trace \"%s\" for writing.\n\n",
400 opt_output_path
? : "<none>");
404 ret
= convert_trace(td_write
, ctx
);
406 fprintf(stderr
, "Error printing trace.\n\n");
407 goto error_copy_trace
;
410 fmt_write
->close_trace(td_write
);
413 printf_verbose("finished converting. Output written to:\n%s\n",
414 opt_output_path
? : "<stdout>");
419 fmt_write
->close_trace(td_write
);
This page took 0.038278 seconds and 4 git commands to generate.