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