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