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