ctf-text: show timestamp in sec.nsec
[babeltrace.git] / converter / babeltrace.c
... / ...
CommitLineData
1/*
2 * babeltrace.c
3 *
4 * Babeltrace Trace Converter
5 *
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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 */
20
21#define _XOPEN_SOURCE 700
22#include <config.h>
23#include <babeltrace/babeltrace-internal.h>
24#include <babeltrace/format.h>
25#include <popt.h>
26#include <errno.h>
27#include <stdlib.h>
28#include <ctype.h>
29#include <sys/stat.h>
30#include <sys/types.h>
31#include <fcntl.h>
32#include <ftw.h>
33#include <dirent.h>
34#include <unistd.h>
35
36#define DEFAULT_FILE_ARRAY_SIZE 1
37static char *opt_input_format;
38static char *opt_output_format;
39
40static const char *opt_input_path;
41static const char *opt_output_path;
42
43static struct trace_collection trace_collection_read;
44static struct format *fmt_read;
45
46void strlower(char *str)
47{
48 while (*str) {
49 *str = tolower(*str);
50 str++;
51 }
52}
53
54enum {
55 OPT_NONE = 0,
56 OPT_HELP,
57 OPT_LIST,
58 OPT_VERBOSE,
59 OPT_DEBUG,
60 OPT_NAMES,
61};
62
63static struct poptOption long_options[] = {
64 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
65 { "input-format", 'i', POPT_ARG_STRING, &opt_input_format, OPT_NONE, NULL, NULL },
66 { "output-format", 'o', POPT_ARG_STRING, &opt_output_format, OPT_NONE, NULL, NULL },
67 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
68 { "list", 'l', POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL },
69 { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL },
70 { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL },
71 { "names", 'n', POPT_ARG_STRING, NULL, OPT_NAMES, NULL, NULL },
72 { NULL, 0, 0, NULL, 0, NULL, NULL },
73};
74
75static void list_formats(FILE *fp)
76{
77 fprintf(fp, "\n");
78 bt_fprintf_format_list(fp);
79}
80
81static void usage(FILE *fp)
82{
83 fprintf(fp, "BabelTrace Trace Converter %s\n\n", VERSION);
84 fprintf(fp, "usage : babeltrace [OPTIONS] INPUT <OUTPUT>\n");
85 fprintf(fp, "\n");
86 fprintf(fp, " INPUT Input trace path\n");
87 fprintf(fp, " OUTPUT Output trace path (default: stdout)\n");
88 fprintf(fp, "\n");
89 fprintf(fp, " -i, --input-format FORMAT Input trace format (default: ctf)\n");
90 fprintf(fp, " -o, --output-format FORMAT Output trace format (default: text)\n");
91 fprintf(fp, "\n");
92 fprintf(fp, " -h, --help This help message\n");
93 fprintf(fp, " -l, --list List available formats\n");
94 fprintf(fp, " -v, --verbose Verbose mode\n");
95 fprintf(fp, " (or set BABELTRACE_VERBOSE environment variable)\n");
96 fprintf(fp, " -d, --debug Debug mode\n");
97 fprintf(fp, " (or set BABELTRACE_DEBUG environment variable)\n");
98 fprintf(fp, " -n, --names name1<,name2,...> Print field names.\n");
99 fprintf(fp, " Available field names:\n");
100 fprintf(fp, " (payload OR args OR arg)\n");
101 fprintf(fp, " all, scope, header, (context OR ctx)\n");
102 fprintf(fp, " trace, trace:domain, trace:procname, trace:vpid,\n");
103 fprintf(fp, " loglevel.\n");
104 fprintf(fp, " (payload active by default)\n");
105 list_formats(fp);
106 fprintf(fp, "\n");
107}
108
109static int get_names_args(poptContext *pc)
110{
111 char *str, *strlist, *strctx;
112
113 opt_payload_field_names = 0;
114 strlist = (char *) poptGetOptArg(*pc);
115 if (!strlist) {
116 return -EINVAL;
117 }
118 str = strtok_r(strlist, ",", &strctx);
119 do {
120 if (!strcmp(str, "all"))
121 opt_all_field_names = 1;
122 else if (!strcmp(str, "scope"))
123 opt_scope_field_names = 1;
124 else if (!strcmp(str, "context") || !strcmp(str, "ctx"))
125 opt_context_field_names = 1;
126 else if (!strcmp(str, "header"))
127 opt_header_field_names = 1;
128 else if (!strcmp(str, "payload") || !strcmp(str, "args") || !strcmp(str, "arg"))
129 opt_payload_field_names = 1;
130 else if (!strcmp(str, "trace"))
131 opt_trace_name = 1;
132 else if (!strcmp(str, "trace:domain"))
133 opt_trace_domain = 1;
134 else if (!strcmp(str, "trace:procname"))
135 opt_trace_procname = 1;
136 else if (!strcmp(str, "trace:vpid"))
137 opt_trace_vpid = 1;
138 else if (!strcmp(str, "loglevel"))
139 opt_loglevel = 1;
140 else {
141 fprintf(stdout, "[error] unknown field name type %s\n", str);
142 return -EINVAL;
143 }
144 } while ((str = strtok_r(NULL, ",", &strctx)));
145 return 0;
146}
147
148/*
149 * Return 0 if caller should continue, < 0 if caller should return
150 * error, > 0 if caller should exit without reporting error.
151 */
152static int parse_options(int argc, char **argv)
153{
154 poptContext pc;
155 int opt, ret = 0;
156
157 if (argc == 1) {
158 usage(stdout);
159 return 1; /* exit cleanly */
160 }
161
162 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
163 poptReadDefaultConfig(pc, 0);
164
165 /* set default */
166 opt_payload_field_names = 1;
167
168 while ((opt = poptGetNextOpt(pc)) != -1) {
169 switch (opt) {
170 case OPT_HELP:
171 usage(stdout);
172 ret = 1; /* exit cleanly */
173 goto end;
174 case OPT_LIST:
175 list_formats(stdout);
176 ret = 1;
177 goto end;
178 case OPT_VERBOSE:
179 babeltrace_verbose = 1;
180 break;
181 case OPT_NAMES:
182 if (get_names_args(&pc)) {
183 ret = -EINVAL;
184 goto end;
185 }
186 break;
187 case OPT_DEBUG:
188 babeltrace_debug = 1;
189 break;
190 default:
191 ret = -EINVAL;
192 goto end;
193 }
194 }
195
196 opt_input_path = poptGetArg(pc);
197 if (!opt_input_path) {
198 ret = -EINVAL;
199 goto end;
200 }
201 opt_output_path = poptGetArg(pc);
202
203end:
204 if (pc) {
205 poptFreeContext(pc);
206 }
207 return ret;
208}
209
210static void init_trace_collection(struct trace_collection *tc)
211{
212 tc->array = g_ptr_array_sized_new(DEFAULT_FILE_ARRAY_SIZE);
213}
214
215/*
216 * finalize_trace_collection() closes the opened traces for read
217 * and free the memory allocated for trace collection
218 */
219static void finalize_trace_collection(struct trace_collection *tc)
220{
221 int i;
222
223 for (i = 0; i < tc->array->len; i++) {
224 struct trace_descriptor *temp =
225 g_ptr_array_index(tc->array, i);
226 fmt_read->close_trace(temp);
227 }
228 g_ptr_array_free(tc->array, TRUE);
229}
230
231static void trace_collection_add(struct trace_collection *tc,
232 struct trace_descriptor *td)
233{
234 g_ptr_array_add(tc->array, td);
235}
236
237/*
238 * traverse_dir() is the callback functiion for File Tree Walk (nftw).
239 * it receives the path of the current entry (file, dir, link..etc) with
240 * a flag to indicate the type of the entry.
241 * if the entry being visited is a directory and contains a metadata file,
242 * then open it for reading and save a trace_descriptor to that directory
243 * in the read trace collection.
244 */
245static int traverse_dir(const char *fpath, const struct stat *sb,
246 int tflag, struct FTW *ftwbuf)
247{
248 int dirfd;
249 int fd;
250 struct trace_descriptor *td_read;
251
252 if (tflag != FTW_D)
253 return 0;
254 dirfd = open(fpath, 0);
255 if (dirfd < 0) {
256 fprintf(stdout, "[error] unable to open trace "
257 "directory file descriptor.\n");
258 return -1;
259 }
260 fd = openat(dirfd, "metadata", O_RDONLY);
261 if (fd < 0) {
262 close(dirfd);
263 } else {
264 close(fd);
265 close(dirfd);
266 td_read = fmt_read->open_trace(opt_input_path,
267 fpath, O_RDONLY, ctf_move_pos_slow,
268 NULL);
269 if (!td_read) {
270 fprintf(stdout, "Error opening trace \"%s\" "
271 "for reading.\n\n", fpath);
272 return -1; /* error */
273 }
274 trace_collection_add(&trace_collection_read, td_read);
275 }
276 return 0; /* success */
277}
278
279int main(int argc, char **argv)
280{
281 int ret;
282 struct format *fmt_write;
283 struct trace_descriptor *td_write;
284
285 ret = parse_options(argc, argv);
286 if (ret < 0) {
287 fprintf(stdout, "Error parsing options.\n\n");
288 usage(stdout);
289 exit(EXIT_FAILURE);
290 } else if (ret > 0) {
291 exit(EXIT_SUCCESS);
292 }
293 printf_verbose("Verbose mode active.\n");
294 printf_debug("Debug mode active.\n");
295
296 if (opt_input_format)
297 strlower(opt_input_format);
298 if (opt_output_format)
299 strlower(opt_output_format);
300
301 printf_verbose("Converting from directory: %s\n", opt_input_path);
302 printf_verbose("Converting from format: %s\n",
303 opt_input_format ? : "ctf <default>");
304 printf_verbose("Converting to directory: %s\n",
305 opt_output_path ? : "<stdout>");
306 printf_verbose("Converting to format: %s\n",
307 opt_output_format ? : "text <default>");
308
309 if (!opt_input_format)
310 opt_input_format = "ctf";
311 if (!opt_output_format)
312 opt_output_format = "text";
313 fmt_read = bt_lookup_format(g_quark_from_static_string(opt_input_format));
314 if (!fmt_read) {
315 fprintf(stdout, "[error] Format \"%s\" is not supported.\n\n",
316 opt_input_format);
317 exit(EXIT_FAILURE);
318 }
319 fmt_write = bt_lookup_format(g_quark_from_static_string(opt_output_format));
320 if (!fmt_write) {
321 fprintf(stdout, "[error] format \"%s\" is not supported.\n\n",
322 opt_output_format);
323 exit(EXIT_FAILURE);
324 }
325
326 /*
327 * pass the input path to nftw() .
328 * specify traverse_dir() as the callback function.
329 * depth = 10 which is the max number of file descriptors
330 * that nftw() can open at a given time.
331 * flags = 0 check nftw documentation for more info .
332 */
333 init_trace_collection(&trace_collection_read);
334 ret = nftw(opt_input_path, traverse_dir, 10, 0);
335 if (ret != 0) {
336 fprintf(stdout, "[error] opening trace \"%s\" for reading.\n\n",
337 opt_input_path);
338 goto error_td_read;
339 }
340 if (trace_collection_read.array->len == 0) {
341 fprintf(stdout, "[warning] no metadata file was found."
342 " no output was generated\n");
343 return 0;
344 }
345
346 td_write = fmt_write->open_trace(NULL, opt_output_path, O_RDWR, NULL, NULL);
347 if (!td_write) {
348 fprintf(stdout, "Error opening trace \"%s\" for writing.\n\n",
349 opt_output_path ? : "<none>");
350 goto error_td_write;
351 }
352
353 ret = convert_trace(td_write, &trace_collection_read);
354 if (ret) {
355 fprintf(stdout, "Error printing trace.\n\n");
356 goto error_copy_trace;
357 }
358
359 fmt_write->close_trace(td_write);
360 finalize_trace_collection(&trace_collection_read);
361 printf_verbose("finished converting. Output written to:\n%s\n",
362 opt_output_path ? : "<stdout>");
363 exit(EXIT_SUCCESS);
364
365 /* Error handling */
366error_copy_trace:
367 fmt_write->close_trace(td_write);
368error_td_write:
369 finalize_trace_collection(&trace_collection_read);
370error_td_read:
371 exit(EXIT_FAILURE);
372}
This page took 0.023504 seconds and 4 git commands to generate.