clock: show as time of day
[babeltrace.git] / converter / babeltrace.c
CommitLineData
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>
4c8bfb7e 39
afb48eae 40#define DEFAULT_FILE_ARRAY_SIZE 1
bbefb8dd
MD
41static char *opt_input_format;
42static char *opt_output_format;
34ac0e6c
MD
43
44static const char *opt_input_path;
45static const char *opt_output_path;
46
afb48eae
AA
47static struct trace_collection trace_collection_read;
48static struct format *fmt_read;
49
bbefb8dd
MD
50void strlower(char *str)
51{
52 while (*str) {
53 *str = tolower(*str);
54 str++;
55 }
56}
57
34ac0e6c
MD
58enum {
59 OPT_NONE = 0,
60 OPT_HELP,
7fb21036 61 OPT_LIST,
34ac0e6c
MD
62 OPT_VERBOSE,
63 OPT_DEBUG,
d63ca2cd 64 OPT_NAMES,
359d7456 65 OPT_FIELDS,
8d8ed9af 66 OPT_NO_DELTA,
11ac6674
MD
67 OPT_CLOCK_OFFSET,
68 OPT_CLOCK_RAW,
69 OPT_CLOCK_SECONDS,
70 OPT_CLOCK_DATE,
71 OPT_CLOCK_GMT,
34ac0e6c
MD
72};
73
74static struct poptOption long_options[] = {
75 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
76 { "input-format", 'i', POPT_ARG_STRING, &opt_input_format, OPT_NONE, NULL, NULL },
77 { "output-format", 'o', POPT_ARG_STRING, &opt_output_format, OPT_NONE, NULL, NULL },
78 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
7fb21036 79 { "list", 'l', POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL },
34ac0e6c
MD
80 { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL },
81 { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL },
cba1661c 82 { "names", 'n', POPT_ARG_STRING, NULL, OPT_NAMES, NULL, NULL },
359d7456 83 { "fields", 'f', POPT_ARG_STRING, NULL, OPT_FIELDS, NULL, NULL },
8d8ed9af 84 { "no-delta", 0, POPT_ARG_NONE, NULL, OPT_NO_DELTA, NULL, NULL },
11ac6674
MD
85 { "clock-offset", 0, POPT_ARG_STRING, NULL, OPT_CLOCK_OFFSET, NULL, NULL },
86 { "clock-raw", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_RAW, NULL, NULL },
87 { "clock-seconds", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_SECONDS, NULL, NULL },
88 { "clock-date", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_DATE, NULL, NULL },
89 { "clock-gmt", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_GMT, NULL, NULL },
34ac0e6c
MD
90 { NULL, 0, 0, NULL, 0, NULL, NULL },
91};
92
7fb21036
MD
93static void list_formats(FILE *fp)
94{
95 fprintf(fp, "\n");
96 bt_fprintf_format_list(fp);
97}
98
34ac0e6c 99static void usage(FILE *fp)
4c8bfb7e 100{
00f7fbf0 101 fprintf(fp, "BabelTrace Trace Converter %s\n\n", VERSION);
4d5678b9 102 fprintf(fp, "usage : babeltrace [OPTIONS] INPUT <OUTPUT>\n");
34ac0e6c 103 fprintf(fp, "\n");
4d5678b9
MD
104 fprintf(fp, " INPUT Input trace path\n");
105 fprintf(fp, " OUTPUT Output trace path (default: stdout)\n");
34ac0e6c 106 fprintf(fp, "\n");
d2b8ea6b
MD
107 fprintf(fp, " -i, --input-format FORMAT Input trace format (default: ctf)\n");
108 fprintf(fp, " -o, --output-format FORMAT Output trace format (default: text)\n");
34ac0e6c 109 fprintf(fp, "\n");
4d5678b9
MD
110 fprintf(fp, " -h, --help This help message\n");
111 fprintf(fp, " -l, --list List available formats\n");
112 fprintf(fp, " -v, --verbose Verbose mode\n");
cba1661c 113 fprintf(fp, " (or set BABELTRACE_VERBOSE environment variable)\n");
4d5678b9 114 fprintf(fp, " -d, --debug Debug mode\n");
cba1661c 115 fprintf(fp, " (or set BABELTRACE_DEBUG environment variable)\n");
8d8ed9af 116 fprintf(fp, " --no-delta Do not print time delta between consecutive events\n");
359d7456 117 fprintf(fp, " -n, --names name1<,name2,...> Print field names:\n");
82662ad4
MD
118 fprintf(fp, " (payload OR args OR arg)\n");
119 fprintf(fp, " all, scope, header, (context OR ctx)\n");
cba1661c 120 fprintf(fp, " (payload active by default)\n");
359d7456
MD
121 fprintf(fp, " -f, --fields name1<,name2,...> Print additional fields:\n");
122 fprintf(fp, " all, trace, trace:domain, trace:procname,\n");
123 fprintf(fp, " trace:vpid, loglevel.\n");
11ac6674
MD
124 fprintf(fp, " --clock-raw Disregard internal clock offset (use raw value)\n");
125 fprintf(fp, " --clock-offset seconds Clock offset in seconds\n");
126 fprintf(fp, " --clock-seconds Print the timestamps as [sec.ns]\n");
127 fprintf(fp, " (default is: [hh:mm:ss.ns])\n");
128 fprintf(fp, " --clock-date Print clock date\n");
129 fprintf(fp, " --clock-gmt Print clock in GMT time zone (default: local time zone)\n");
7fb21036 130 list_formats(fp);
34ac0e6c
MD
131 fprintf(fp, "\n");
132}
133
cba1661c
MD
134static int get_names_args(poptContext *pc)
135{
136 char *str, *strlist, *strctx;
137
138 opt_payload_field_names = 0;
139 strlist = (char *) poptGetOptArg(*pc);
140 if (!strlist) {
141 return -EINVAL;
142 }
143 str = strtok_r(strlist, ",", &strctx);
144 do {
145 if (!strcmp(str, "all"))
146 opt_all_field_names = 1;
147 else if (!strcmp(str, "scope"))
148 opt_scope_field_names = 1;
149 else if (!strcmp(str, "context") || !strcmp(str, "ctx"))
150 opt_context_field_names = 1;
151 else if (!strcmp(str, "header"))
152 opt_header_field_names = 1;
153 else if (!strcmp(str, "payload") || !strcmp(str, "args") || !strcmp(str, "arg"))
154 opt_payload_field_names = 1;
359d7456
MD
155 else {
156 fprintf(stderr, "[error] unknown field name type %s\n", str);
157 return -EINVAL;
158 }
159 } while ((str = strtok_r(NULL, ",", &strctx)));
160 return 0;
161}
162
163static int get_fields_args(poptContext *pc)
164{
165 char *str, *strlist, *strctx;
166
167 opt_payload_field_names = 0;
168 strlist = (char *) poptGetOptArg(*pc);
169 if (!strlist) {
170 return -EINVAL;
171 }
172 str = strtok_r(strlist, ",", &strctx);
173 do {
174 if (!strcmp(str, "all"))
175 opt_all_fields = 1;
82662ad4 176 else if (!strcmp(str, "trace"))
359d7456 177 opt_trace_field = 1;
8c250d87 178 else if (!strcmp(str, "trace:domain"))
359d7456 179 opt_trace_domain_field = 1;
8c250d87 180 else if (!strcmp(str, "trace:procname"))
359d7456 181 opt_trace_procname_field = 1;
8c250d87 182 else if (!strcmp(str, "trace:vpid"))
359d7456 183 opt_trace_vpid_field = 1;
d86d62f8 184 else if (!strcmp(str, "loglevel"))
359d7456 185 opt_loglevel_field = 1;
cba1661c 186 else {
359d7456 187 fprintf(stderr, "[error] unknown field type %s\n", str);
cba1661c
MD
188 return -EINVAL;
189 }
190 } while ((str = strtok_r(NULL, ",", &strctx)));
191 return 0;
192}
193
34ac0e6c
MD
194/*
195 * Return 0 if caller should continue, < 0 if caller should return
196 * error, > 0 if caller should exit without reporting error.
197 */
bbefb8dd 198static int parse_options(int argc, char **argv)
34ac0e6c
MD
199{
200 poptContext pc;
201 int opt, ret = 0;
202
0f980a35
MD
203 if (argc == 1) {
204 usage(stdout);
205 return 1; /* exit cleanly */
206 }
207
bbefb8dd 208 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
34ac0e6c
MD
209 poptReadDefaultConfig(pc, 0);
210
cba1661c
MD
211 /* set default */
212 opt_payload_field_names = 1;
213
34ac0e6c
MD
214 while ((opt = poptGetNextOpt(pc)) != -1) {
215 switch (opt) {
216 case OPT_HELP:
7fb21036 217 usage(stdout);
34ac0e6c
MD
218 ret = 1; /* exit cleanly */
219 goto end;
7fb21036
MD
220 case OPT_LIST:
221 list_formats(stdout);
222 ret = 1;
223 goto end;
34ac0e6c
MD
224 case OPT_VERBOSE:
225 babeltrace_verbose = 1;
226 break;
cba1661c
MD
227 case OPT_NAMES:
228 if (get_names_args(&pc)) {
229 ret = -EINVAL;
230 goto end;
231 }
232 break;
359d7456
MD
233 case OPT_FIELDS:
234 if (get_fields_args(&pc)) {
235 ret = -EINVAL;
236 goto end;
237 }
238 break;
34ac0e6c
MD
239 case OPT_DEBUG:
240 babeltrace_debug = 1;
241 break;
8d8ed9af 242 case OPT_NO_DELTA:
359d7456 243 opt_delta_field = 0;
8d8ed9af 244 break;
11ac6674
MD
245 case OPT_CLOCK_RAW:
246 opt_clock_raw = 1;
247 break;
248 case OPT_CLOCK_OFFSET:
249 {
250 char *str, *endptr;
251
252 str = poptGetOptArg(pc);
253 if (!str) {
254 fprintf(stderr, "[error] Missing --clock-offset argument\n");
255 ret = -EINVAL;
256 goto end;
257 }
258 errno = 0;
259 opt_clock_offset = strtoull(str, &endptr, 0);
260 if (*endptr != '\0' || str == endptr || errno != 0) {
261 fprintf(stderr, "[error] Incorrect --clock-offset argument: %s\n", str);
262 ret = -EINVAL;
263 goto end;
264 }
265 break;
266 }
267 case OPT_CLOCK_SECONDS:
268 opt_clock_seconds = 1;
269 break;
270 case OPT_CLOCK_DATE:
271 opt_clock_date = 1;
272 break;
273 case OPT_CLOCK_GMT:
274 opt_clock_gmt = 1;
275 break;
276
34ac0e6c
MD
277 default:
278 ret = -EINVAL;
279 goto end;
280 }
281 }
282
283 opt_input_path = poptGetArg(pc);
284 if (!opt_input_path) {
285 ret = -EINVAL;
286 goto end;
287 }
288 opt_output_path = poptGetArg(pc);
cba1661c 289
34ac0e6c
MD
290end:
291 if (pc) {
292 poptFreeContext(pc);
293 }
294 return ret;
295}
296
afb48eae
AA
297static void init_trace_collection(struct trace_collection *tc)
298{
299 tc->array = g_ptr_array_sized_new(DEFAULT_FILE_ARRAY_SIZE);
300}
301
302/*
303 * finalize_trace_collection() closes the opened traces for read
304 * and free the memory allocated for trace collection
305 */
306static void finalize_trace_collection(struct trace_collection *tc)
307{
308 int i;
309
310 for (i = 0; i < tc->array->len; i++) {
311 struct trace_descriptor *temp =
312 g_ptr_array_index(tc->array, i);
313 fmt_read->close_trace(temp);
314 }
315 g_ptr_array_free(tc->array, TRUE);
316}
317
318static void trace_collection_add(struct trace_collection *tc,
319 struct trace_descriptor *td)
320{
321 g_ptr_array_add(tc->array, td);
322}
323
95d36295
JD
324int convert_trace(struct trace_descriptor *td_write,
325 struct bt_context *ctx)
326{
327 struct babeltrace_iter *iter;
328 struct ctf_stream *stream;
329 struct ctf_stream_event *event;
330 struct ctf_text_stream_pos *sout;
331 struct trace_collection_pos begin_pos;
332 int ret;
333
334 sout = container_of(td_write, struct ctf_text_stream_pos,
335 trace_descriptor);
336
337 begin_pos.type = BT_SEEK_BEGIN;
338 iter = babeltrace_iter_create(ctx, &begin_pos, NULL);
339 if (!iter) {
340 ret = -1;
341 goto error_iter;
342 }
343 while (babeltrace_iter_read_event(iter, &stream, &event) == 0) {
344 ret = sout->parent.event_cb(&sout->parent, stream);
345 if (ret) {
3394d22e 346 fprintf(stderr, "[error] Writing event failed.\n");
95d36295
JD
347 goto end;
348 }
349 ret = babeltrace_iter_next(iter);
350 if (ret < 0)
351 goto end;
352 }
353 ret = 0;
354
355end:
356 babeltrace_iter_destroy(iter);
357error_iter:
358 return ret;
359}
360
361
afb48eae
AA
362/*
363 * traverse_dir() is the callback functiion for File Tree Walk (nftw).
364 * it receives the path of the current entry (file, dir, link..etc) with
365 * a flag to indicate the type of the entry.
366 * if the entry being visited is a directory and contains a metadata file,
367 * then open it for reading and save a trace_descriptor to that directory
368 * in the read trace collection.
369 */
370static int traverse_dir(const char *fpath, const struct stat *sb,
371 int tflag, struct FTW *ftwbuf)
372{
373 int dirfd;
374 int fd;
375 struct trace_descriptor *td_read;
376
377 if (tflag != FTW_D)
378 return 0;
379 dirfd = open(fpath, 0);
380 if (dirfd < 0) {
3394d22e 381 fprintf(stderr, "[error] unable to open trace "
afb48eae
AA
382 "directory file descriptor.\n");
383 return -1;
384 }
385 fd = openat(dirfd, "metadata", O_RDONLY);
386 if (fd < 0) {
387 close(dirfd);
388 } else {
389 close(fd);
390 close(dirfd);
8c250d87
MD
391 td_read = fmt_read->open_trace(opt_input_path,
392 fpath, O_RDONLY, ctf_move_pos_slow,
ae23d232 393 NULL);
afb48eae 394 if (!td_read) {
3394d22e 395 fprintf(stderr, "Error opening trace \"%s\" "
afb48eae
AA
396 "for reading.\n\n", fpath);
397 return -1; /* error */
398 }
399 trace_collection_add(&trace_collection_read, td_read);
400 }
401 return 0; /* success */
402}
403
bbefb8dd 404int main(int argc, char **argv)
34ac0e6c
MD
405{
406 int ret;
afb48eae
AA
407 struct format *fmt_write;
408 struct trace_descriptor *td_write;
95d36295 409 struct bt_context *ctx;
34ac0e6c
MD
410
411 ret = parse_options(argc, argv);
412 if (ret < 0) {
3394d22e
MD
413 fprintf(stderr, "Error parsing options.\n\n");
414 usage(stderr);
34ac0e6c
MD
415 exit(EXIT_FAILURE);
416 } else if (ret > 0) {
417 exit(EXIT_SUCCESS);
418 }
419 printf_verbose("Verbose mode active.\n");
420 printf_debug("Debug mode active.\n");
421
bbefb8dd
MD
422 if (opt_input_format)
423 strlower(opt_input_format);
424 if (opt_output_format)
425 strlower(opt_output_format);
426
afb48eae 427 printf_verbose("Converting from directory: %s\n", opt_input_path);
34ac0e6c 428 printf_verbose("Converting from format: %s\n",
b61922b5 429 opt_input_format ? : "ctf <default>");
afb48eae 430 printf_verbose("Converting to directory: %s\n",
478b6389 431 opt_output_path ? : "<stdout>");
34ac0e6c 432 printf_verbose("Converting to format: %s\n",
b61922b5 433 opt_output_format ? : "text <default>");
bbefb8dd 434
b61922b5
MD
435 if (!opt_input_format)
436 opt_input_format = "ctf";
437 if (!opt_output_format)
438 opt_output_format = "text";
bbefb8dd
MD
439 fmt_read = bt_lookup_format(g_quark_from_static_string(opt_input_format));
440 if (!fmt_read) {
3394d22e 441 fprintf(stderr, "[error] Format \"%s\" is not supported.\n\n",
bbefb8dd
MD
442 opt_input_format);
443 exit(EXIT_FAILURE);
444 }
bbefb8dd
MD
445 fmt_write = bt_lookup_format(g_quark_from_static_string(opt_output_format));
446 if (!fmt_write) {
3394d22e 447 fprintf(stderr, "[error] format \"%s\" is not supported.\n\n",
bbefb8dd
MD
448 opt_output_format);
449 exit(EXIT_FAILURE);
450 }
451
afb48eae
AA
452 /*
453 * pass the input path to nftw() .
454 * specify traverse_dir() as the callback function.
455 * depth = 10 which is the max number of file descriptors
456 * that nftw() can open at a given time.
457 * flags = 0 check nftw documentation for more info .
458 */
459 init_trace_collection(&trace_collection_read);
460 ret = nftw(opt_input_path, traverse_dir, 10, 0);
461 if (ret != 0) {
3394d22e 462 fprintf(stderr, "[error] opening trace \"%s\" for reading.\n\n",
bbefb8dd
MD
463 opt_input_path);
464 goto error_td_read;
465 }
afb48eae 466 if (trace_collection_read.array->len == 0) {
3394d22e 467 fprintf(stderr, "[warning] no metadata file was found."
afb48eae
AA
468 " no output was generated\n");
469 return 0;
470 }
95d36295
JD
471 ctx = bt_context_create(&trace_collection_read);
472 if (!ctx) {
3394d22e 473 fprintf(stderr, "Error allocating a new context\n");
95d36295
JD
474 goto error_td_read;
475 }
8c250d87 476 td_write = fmt_write->open_trace(NULL, opt_output_path, O_RDWR, NULL, NULL);
bbefb8dd 477 if (!td_write) {
3394d22e 478 fprintf(stderr, "Error opening trace \"%s\" for writing.\n\n",
b61922b5 479 opt_output_path ? : "<none>");
bbefb8dd
MD
480 goto error_td_write;
481 }
847bf71a 482
95d36295 483 ret = convert_trace(td_write, ctx);
46322b33 484 if (ret) {
3394d22e 485 fprintf(stderr, "Error printing trace.\n\n");
46322b33
MD
486 goto error_copy_trace;
487 }
847bf71a 488
bbefb8dd 489 fmt_write->close_trace(td_write);
afb48eae 490 finalize_trace_collection(&trace_collection_read);
95d36295 491 bt_context_destroy(ctx);
afb48eae
AA
492 printf_verbose("finished converting. Output written to:\n%s\n",
493 opt_output_path ? : "<stdout>");
bbefb8dd 494 exit(EXIT_SUCCESS);
34ac0e6c 495
bbefb8dd 496 /* Error handling */
46322b33 497error_copy_trace:
bbefb8dd
MD
498 fmt_write->close_trace(td_write);
499error_td_write:
afb48eae 500 finalize_trace_collection(&trace_collection_read);
bbefb8dd
MD
501error_td_read:
502 exit(EXIT_FAILURE);
4c8bfb7e 503}
This page took 0.04652 seconds and 4 git commands to generate.