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