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