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. | |
c462e188 MD |
19 | * |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
34ac0e6c | 27 | */ |
4c8bfb7e | 28 | |
20d24215 | 29 | #define _GNU_SOURCE |
00f7fbf0 | 30 | #include <config.h> |
95d36295 | 31 | #include <babeltrace/babeltrace.h> |
7fb21036 | 32 | #include <babeltrace/format.h> |
95d36295 | 33 | #include <babeltrace/context.h> |
7237592a | 34 | #include <babeltrace/context-internal.h> |
95d36295 | 35 | #include <babeltrace/ctf/types.h> |
9843982d | 36 | #include <babeltrace/ctf/events.h> |
04ae3991 MD |
37 | /* TODO: fix object model for format-agnostic callbacks */ |
38 | #include <babeltrace/ctf/events-internal.h> | |
9efd5d76 | 39 | #include <babeltrace/ctf/iterator.h> |
31bdef5c | 40 | #include <babeltrace/ctf-text/types.h> |
6204d33c | 41 | #include <babeltrace/iterator.h> |
34ac0e6c MD |
42 | #include <popt.h> |
43 | #include <errno.h> | |
44 | #include <stdlib.h> | |
bbefb8dd MD |
45 | #include <ctype.h> |
46 | #include <sys/stat.h> | |
47 | #include <sys/types.h> | |
48 | #include <fcntl.h> | |
afb48eae | 49 | #include <unistd.h> |
70accc14 | 50 | #include <inttypes.h> |
a99343cf | 51 | #include <ftw.h> |
11e1d048 | 52 | #include <string.h> |
4c8bfb7e | 53 | |
a44bc4c9 MD |
54 | #include <babeltrace/ctf-ir/metadata.h> /* for clocks */ |
55 | ||
24d5af5b MD |
56 | #define PARTIAL_ERROR_SLEEP 3 /* 3 seconds */ |
57 | ||
afb48eae | 58 | #define DEFAULT_FILE_ARRAY_SIZE 1 |
9fe26ec7 | 59 | |
11e1d048 | 60 | static char *opt_input_format, *opt_output_format; |
34ac0e6c | 61 | |
41075e78 MD |
62 | /* |
63 | * We are not freeing opt_input_paths ipath elements when exiting from | |
64 | * main() for backward compatibility with libpop 0.13, which does not | |
65 | * allocate copies for arguments returned by poptGetArg(), and for | |
66 | * general compatibility with the documented behavior. This is known to | |
67 | * cause a small memory leak with libpop 0.16. | |
68 | */ | |
4e85cbfd | 69 | static GPtrArray *opt_input_paths; |
9fe26ec7 | 70 | static char *opt_output_path; |
34ac0e6c | 71 | |
37b99bdb | 72 | static struct bt_format *fmt_read; |
afb48eae | 73 | |
11e1d048 | 74 | static |
bbefb8dd MD |
75 | void strlower(char *str) |
76 | { | |
77 | while (*str) { | |
34224260 | 78 | *str = tolower((int) *str); |
bbefb8dd MD |
79 | str++; |
80 | } | |
81 | } | |
82 | ||
34ac0e6c MD |
83 | enum { |
84 | OPT_NONE = 0, | |
9fe26ec7 MD |
85 | OPT_OUTPUT_PATH, |
86 | OPT_INPUT_FORMAT, | |
87 | OPT_OUTPUT_FORMAT, | |
34ac0e6c | 88 | OPT_HELP, |
7fb21036 | 89 | OPT_LIST, |
34ac0e6c MD |
90 | OPT_VERBOSE, |
91 | OPT_DEBUG, | |
d63ca2cd | 92 | OPT_NAMES, |
359d7456 | 93 | OPT_FIELDS, |
8d8ed9af | 94 | OPT_NO_DELTA, |
11ac6674 | 95 | OPT_CLOCK_OFFSET, |
65923160 | 96 | OPT_CLOCK_OFFSET_NS, |
03798a93 | 97 | OPT_CLOCK_CYCLES, |
11ac6674 MD |
98 | OPT_CLOCK_SECONDS, |
99 | OPT_CLOCK_DATE, | |
100 | OPT_CLOCK_GMT, | |
82ace6d6 | 101 | OPT_CLOCK_FORCE_CORRELATE, |
34ac0e6c MD |
102 | }; |
103 | ||
9fe26ec7 MD |
104 | /* |
105 | * We are _not_ using POPT_ARG_STRING ability to store directly into | |
106 | * variables, because we want to cast the return to non-const, which is | |
41075e78 MD |
107 | * not possible without using poptGetOptArg explicitly. This helps us |
108 | * controlling memory allocation correctly without making assumptions | |
109 | * about undocumented behaviors. poptGetOptArg is documented as | |
110 | * requiring the returned const char * to be freed by the caller. | |
9fe26ec7 | 111 | */ |
34ac0e6c MD |
112 | static struct poptOption long_options[] = { |
113 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
c790c052 | 114 | { "output", 'w', POPT_ARG_STRING, NULL, OPT_OUTPUT_PATH, NULL, NULL }, |
9fe26ec7 MD |
115 | { "input-format", 'i', POPT_ARG_STRING, NULL, OPT_INPUT_FORMAT, NULL, NULL }, |
116 | { "output-format", 'o', POPT_ARG_STRING, NULL, OPT_OUTPUT_FORMAT, NULL, NULL }, | |
34ac0e6c | 117 | { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL }, |
7fb21036 | 118 | { "list", 'l', POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL }, |
34ac0e6c MD |
119 | { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL }, |
120 | { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL }, | |
cba1661c | 121 | { "names", 'n', POPT_ARG_STRING, NULL, OPT_NAMES, NULL, NULL }, |
359d7456 | 122 | { "fields", 'f', POPT_ARG_STRING, NULL, OPT_FIELDS, NULL, NULL }, |
8d8ed9af | 123 | { "no-delta", 0, POPT_ARG_NONE, NULL, OPT_NO_DELTA, NULL, NULL }, |
11ac6674 | 124 | { "clock-offset", 0, POPT_ARG_STRING, NULL, OPT_CLOCK_OFFSET, NULL, NULL }, |
65923160 | 125 | { "clock-offset-ns", 0, POPT_ARG_STRING, NULL, OPT_CLOCK_OFFSET_NS, NULL, NULL }, |
03798a93 | 126 | { "clock-cycles", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_CYCLES, NULL, NULL }, |
11ac6674 MD |
127 | { "clock-seconds", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_SECONDS, NULL, NULL }, |
128 | { "clock-date", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_DATE, NULL, NULL }, | |
129 | { "clock-gmt", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_GMT, NULL, NULL }, | |
82ace6d6 | 130 | { "clock-force-correlate", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_FORCE_CORRELATE, NULL, NULL }, |
34ac0e6c MD |
131 | { NULL, 0, 0, NULL, 0, NULL, NULL }, |
132 | }; | |
133 | ||
7fb21036 MD |
134 | static void list_formats(FILE *fp) |
135 | { | |
136 | fprintf(fp, "\n"); | |
137 | bt_fprintf_format_list(fp); | |
138 | } | |
139 | ||
34ac0e6c | 140 | static void usage(FILE *fp) |
4c8bfb7e | 141 | { |
4c15b06b | 142 | fprintf(fp, "BabelTrace Trace Viewer and Converter %s\n\n", VERSION); |
4e85cbfd | 143 | fprintf(fp, "usage : babeltrace [OPTIONS] FILE...\n"); |
34ac0e6c | 144 | fprintf(fp, "\n"); |
4e85cbfd MD |
145 | fprintf(fp, " FILE Input trace file(s) and/or directory(ies)\n"); |
146 | fprintf(fp, " (space-separated)\n"); | |
147 | fprintf(fp, " -w, --output OUTPUT Output trace path (default: stdout)\n"); | |
34ac0e6c | 148 | fprintf(fp, "\n"); |
d2b8ea6b MD |
149 | fprintf(fp, " -i, --input-format FORMAT Input trace format (default: ctf)\n"); |
150 | fprintf(fp, " -o, --output-format FORMAT Output trace format (default: text)\n"); | |
34ac0e6c | 151 | fprintf(fp, "\n"); |
4d5678b9 MD |
152 | fprintf(fp, " -h, --help This help message\n"); |
153 | fprintf(fp, " -l, --list List available formats\n"); | |
154 | fprintf(fp, " -v, --verbose Verbose mode\n"); | |
cba1661c | 155 | fprintf(fp, " (or set BABELTRACE_VERBOSE environment variable)\n"); |
4d5678b9 | 156 | fprintf(fp, " -d, --debug Debug mode\n"); |
cba1661c | 157 | fprintf(fp, " (or set BABELTRACE_DEBUG environment variable)\n"); |
8d8ed9af | 158 | fprintf(fp, " --no-delta Do not print time delta between consecutive events\n"); |
359d7456 | 159 | fprintf(fp, " -n, --names name1<,name2,...> Print field names:\n"); |
82662ad4 | 160 | fprintf(fp, " (payload OR args OR arg)\n"); |
12c9c3bc MD |
161 | fprintf(fp, " none, all, scope, header, (context OR ctx)\n"); |
162 | fprintf(fp, " (default: payload,context)\n"); | |
359d7456 | 163 | fprintf(fp, " -f, --fields name1<,name2,...> Print additional fields:\n"); |
32cfb8ad | 164 | fprintf(fp, " all, trace, trace:hostname, trace:domain,\n"); |
f133896d | 165 | fprintf(fp, " trace:procname, trace:vpid, loglevel, emf, callsite.\n"); |
c3815874 | 166 | fprintf(fp, " (default: trace:hostname,trace:procname,trace:vpid)\n"); |
03798a93 | 167 | fprintf(fp, " --clock-cycles Timestamp in cycles\n"); |
11ac6674 | 168 | fprintf(fp, " --clock-offset seconds Clock offset in seconds\n"); |
65923160 | 169 | fprintf(fp, " --clock-offset-ns ns Clock offset in nanoseconds\n"); |
11ac6674 MD |
170 | fprintf(fp, " --clock-seconds Print the timestamps as [sec.ns]\n"); |
171 | fprintf(fp, " (default is: [hh:mm:ss.ns])\n"); | |
172 | fprintf(fp, " --clock-date Print clock date\n"); | |
173 | fprintf(fp, " --clock-gmt Print clock in GMT time zone (default: local time zone)\n"); | |
82ace6d6 MD |
174 | fprintf(fp, " --clock-force-correlate Assume that clocks are inherently correlated\n"); |
175 | fprintf(fp, " across traces.\n"); | |
7fb21036 | 176 | list_formats(fp); |
34ac0e6c MD |
177 | fprintf(fp, "\n"); |
178 | } | |
179 | ||
cba1661c MD |
180 | static int get_names_args(poptContext *pc) |
181 | { | |
182 | char *str, *strlist, *strctx; | |
9f2c779c | 183 | int ret = 0; |
cba1661c MD |
184 | |
185 | opt_payload_field_names = 0; | |
12c9c3bc | 186 | opt_context_field_names = 0; |
cba1661c MD |
187 | strlist = (char *) poptGetOptArg(*pc); |
188 | if (!strlist) { | |
189 | return -EINVAL; | |
190 | } | |
191 | str = strtok_r(strlist, ",", &strctx); | |
192 | do { | |
193 | if (!strcmp(str, "all")) | |
194 | opt_all_field_names = 1; | |
195 | else if (!strcmp(str, "scope")) | |
196 | opt_scope_field_names = 1; | |
197 | else if (!strcmp(str, "context") || !strcmp(str, "ctx")) | |
198 | opt_context_field_names = 1; | |
199 | else if (!strcmp(str, "header")) | |
200 | opt_header_field_names = 1; | |
201 | else if (!strcmp(str, "payload") || !strcmp(str, "args") || !strcmp(str, "arg")) | |
202 | opt_payload_field_names = 1; | |
12c9c3bc MD |
203 | else if (!strcmp(str, "none")) { |
204 | opt_all_field_names = 0; | |
205 | opt_scope_field_names = 0; | |
206 | opt_context_field_names = 0; | |
207 | opt_header_field_names = 0; | |
208 | opt_payload_field_names = 0; | |
209 | } else { | |
359d7456 | 210 | fprintf(stderr, "[error] unknown field name type %s\n", str); |
9f2c779c MD |
211 | ret = -EINVAL; |
212 | goto end; | |
359d7456 MD |
213 | } |
214 | } while ((str = strtok_r(NULL, ",", &strctx))); | |
9f2c779c MD |
215 | end: |
216 | free(strlist); | |
217 | return ret; | |
359d7456 MD |
218 | } |
219 | ||
220 | static int get_fields_args(poptContext *pc) | |
221 | { | |
222 | char *str, *strlist, *strctx; | |
9f2c779c | 223 | int ret = 0; |
359d7456 | 224 | |
359d7456 MD |
225 | strlist = (char *) poptGetOptArg(*pc); |
226 | if (!strlist) { | |
227 | return -EINVAL; | |
228 | } | |
229 | str = strtok_r(strlist, ",", &strctx); | |
230 | do { | |
c3815874 | 231 | opt_trace_default_fields = 0; |
359d7456 MD |
232 | if (!strcmp(str, "all")) |
233 | opt_all_fields = 1; | |
82662ad4 | 234 | else if (!strcmp(str, "trace")) |
359d7456 | 235 | opt_trace_field = 1; |
c3815874 MD |
236 | else if (!strcmp(str, "trace:hostname")) |
237 | opt_trace_hostname_field = 1; | |
8c250d87 | 238 | else if (!strcmp(str, "trace:domain")) |
359d7456 | 239 | opt_trace_domain_field = 1; |
8c250d87 | 240 | else if (!strcmp(str, "trace:procname")) |
359d7456 | 241 | opt_trace_procname_field = 1; |
8c250d87 | 242 | else if (!strcmp(str, "trace:vpid")) |
359d7456 | 243 | opt_trace_vpid_field = 1; |
d86d62f8 | 244 | else if (!strcmp(str, "loglevel")) |
359d7456 | 245 | opt_loglevel_field = 1; |
f6714e20 MD |
246 | else if (!strcmp(str, "emf")) |
247 | opt_emf_field = 1; | |
f133896d MD |
248 | else if (!strcmp(str, "callsite")) |
249 | opt_callsite_field = 1; | |
cba1661c | 250 | else { |
359d7456 | 251 | fprintf(stderr, "[error] unknown field type %s\n", str); |
9f2c779c MD |
252 | ret = -EINVAL; |
253 | goto end; | |
cba1661c MD |
254 | } |
255 | } while ((str = strtok_r(NULL, ",", &strctx))); | |
9f2c779c MD |
256 | end: |
257 | free(strlist); | |
258 | return ret; | |
cba1661c MD |
259 | } |
260 | ||
34ac0e6c MD |
261 | /* |
262 | * Return 0 if caller should continue, < 0 if caller should return | |
263 | * error, > 0 if caller should exit without reporting error. | |
264 | */ | |
bbefb8dd | 265 | static int parse_options(int argc, char **argv) |
34ac0e6c MD |
266 | { |
267 | poptContext pc; | |
268 | int opt, ret = 0; | |
41075e78 | 269 | const char *ipath; |
34ac0e6c | 270 | |
0f980a35 MD |
271 | if (argc == 1) { |
272 | usage(stdout); | |
273 | return 1; /* exit cleanly */ | |
274 | } | |
275 | ||
bbefb8dd | 276 | pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0); |
34ac0e6c MD |
277 | poptReadDefaultConfig(pc, 0); |
278 | ||
cba1661c | 279 | /* set default */ |
e505794f | 280 | opt_context_field_names = 1; |
cba1661c MD |
281 | opt_payload_field_names = 1; |
282 | ||
34ac0e6c MD |
283 | while ((opt = poptGetNextOpt(pc)) != -1) { |
284 | switch (opt) { | |
9fe26ec7 MD |
285 | case OPT_OUTPUT_PATH: |
286 | opt_output_path = (char *) poptGetOptArg(pc); | |
287 | if (!opt_output_path) { | |
288 | ret = -EINVAL; | |
289 | goto end; | |
290 | } | |
291 | break; | |
292 | case OPT_INPUT_FORMAT: | |
293 | opt_input_format = (char *) poptGetOptArg(pc); | |
294 | if (!opt_input_format) { | |
295 | ret = -EINVAL; | |
296 | goto end; | |
297 | } | |
298 | break; | |
299 | case OPT_OUTPUT_FORMAT: | |
300 | opt_output_format = (char *) poptGetOptArg(pc); | |
301 | if (!opt_output_format) { | |
302 | ret = -EINVAL; | |
303 | goto end; | |
304 | } | |
305 | break; | |
34ac0e6c | 306 | case OPT_HELP: |
7fb21036 | 307 | usage(stdout); |
34ac0e6c MD |
308 | ret = 1; /* exit cleanly */ |
309 | goto end; | |
7fb21036 MD |
310 | case OPT_LIST: |
311 | list_formats(stdout); | |
312 | ret = 1; | |
313 | goto end; | |
34ac0e6c MD |
314 | case OPT_VERBOSE: |
315 | babeltrace_verbose = 1; | |
316 | break; | |
cba1661c MD |
317 | case OPT_NAMES: |
318 | if (get_names_args(&pc)) { | |
319 | ret = -EINVAL; | |
320 | goto end; | |
321 | } | |
322 | break; | |
359d7456 MD |
323 | case OPT_FIELDS: |
324 | if (get_fields_args(&pc)) { | |
325 | ret = -EINVAL; | |
326 | goto end; | |
327 | } | |
328 | break; | |
34ac0e6c MD |
329 | case OPT_DEBUG: |
330 | babeltrace_debug = 1; | |
331 | break; | |
8d8ed9af | 332 | case OPT_NO_DELTA: |
359d7456 | 333 | opt_delta_field = 0; |
8d8ed9af | 334 | break; |
03798a93 JD |
335 | case OPT_CLOCK_CYCLES: |
336 | opt_clock_cycles = 1; | |
11ac6674 MD |
337 | break; |
338 | case OPT_CLOCK_OFFSET: | |
339 | { | |
9f2c779c | 340 | char *str; |
34224260 | 341 | char *endptr; |
11ac6674 | 342 | |
9f2c779c | 343 | str = (char *) poptGetOptArg(pc); |
11ac6674 MD |
344 | if (!str) { |
345 | fprintf(stderr, "[error] Missing --clock-offset argument\n"); | |
346 | ret = -EINVAL; | |
347 | goto end; | |
348 | } | |
349 | errno = 0; | |
350 | opt_clock_offset = strtoull(str, &endptr, 0); | |
351 | if (*endptr != '\0' || str == endptr || errno != 0) { | |
352 | fprintf(stderr, "[error] Incorrect --clock-offset argument: %s\n", str); | |
353 | ret = -EINVAL; | |
9f2c779c | 354 | free(str); |
11ac6674 MD |
355 | goto end; |
356 | } | |
9f2c779c | 357 | free(str); |
11ac6674 MD |
358 | break; |
359 | } | |
360 | case OPT_CLOCK_SECONDS: | |
361 | opt_clock_seconds = 1; | |
362 | break; | |
65923160 IJ |
363 | case OPT_CLOCK_OFFSET_NS: |
364 | { | |
365 | char *str; | |
366 | char *endptr; | |
367 | ||
368 | str = (char *) poptGetOptArg(pc); | |
369 | if (!str) { | |
370 | fprintf(stderr, "[error] Missing --clock-offset-ns argument\n"); | |
371 | ret = -EINVAL; | |
372 | goto end; | |
373 | } | |
374 | errno = 0; | |
375 | opt_clock_offset_ns = strtoull(str, &endptr, 0); | |
376 | if (*endptr != '\0' || str == endptr || errno != 0) { | |
377 | fprintf(stderr, "[error] Incorrect --clock-offset-ns argument: %s\n", str); | |
378 | ret = -EINVAL; | |
379 | free(str); | |
380 | goto end; | |
381 | } | |
382 | free(str); | |
383 | break; | |
384 | } | |
385 | ||
11ac6674 MD |
386 | case OPT_CLOCK_DATE: |
387 | opt_clock_date = 1; | |
388 | break; | |
389 | case OPT_CLOCK_GMT: | |
390 | opt_clock_gmt = 1; | |
391 | break; | |
82ace6d6 MD |
392 | case OPT_CLOCK_FORCE_CORRELATE: |
393 | opt_clock_force_correlate = 1; | |
394 | break; | |
11ac6674 | 395 | |
34ac0e6c MD |
396 | default: |
397 | ret = -EINVAL; | |
398 | goto end; | |
399 | } | |
400 | } | |
401 | ||
4e85cbfd | 402 | do { |
41075e78 | 403 | ipath = poptGetArg(pc); |
4e85cbfd MD |
404 | if (ipath) |
405 | g_ptr_array_add(opt_input_paths, (gpointer) ipath); | |
406 | } while (ipath); | |
407 | if (opt_input_paths->len == 0) { | |
34ac0e6c MD |
408 | ret = -EINVAL; |
409 | goto end; | |
410 | } | |
cba1661c | 411 | |
34ac0e6c MD |
412 | end: |
413 | if (pc) { | |
414 | poptFreeContext(pc); | |
415 | } | |
416 | return ret; | |
417 | } | |
418 | ||
a99343cf | 419 | static GPtrArray *traversed_paths = 0; |
afb48eae | 420 | |
a99343cf YB |
421 | /* |
422 | * traverse_trace_dir() is the callback function for File Tree Walk (nftw). | |
423 | * it receives the path of the current entry (file, dir, link..etc) with | |
424 | * a flag to indicate the type of the entry. | |
425 | * if the entry being visited is a directory and contains a metadata file, | |
426 | * then add the path to a global list to be processed later in | |
427 | * add_traces_recursive. | |
428 | */ | |
429 | static int traverse_trace_dir(const char *fpath, const struct stat *sb, | |
430 | int tflag, struct FTW *ftwbuf) | |
431 | { | |
432 | int dirfd, metafd; | |
433 | int closeret; | |
434 | ||
435 | if (tflag != FTW_D) | |
436 | return 0; | |
437 | ||
438 | dirfd = open(fpath, 0); | |
439 | if (dirfd < 0) { | |
440 | fprintf(stderr, "[error] [Context] Unable to open trace " | |
441 | "directory file descriptor.\n"); | |
442 | return 0; /* partial error */ | |
443 | } | |
444 | metafd = openat(dirfd, "metadata", O_RDONLY); | |
445 | if (metafd < 0) { | |
446 | closeret = close(dirfd); | |
447 | if (closeret < 0) { | |
448 | perror("close"); | |
449 | return -1; | |
450 | } | |
451 | /* No meta data, just return */ | |
452 | return 0; | |
453 | } else { | |
9544a40b MD |
454 | int err_close = 0; |
455 | ||
a99343cf YB |
456 | closeret = close(metafd); |
457 | if (closeret < 0) { | |
458 | perror("close"); | |
9544a40b | 459 | err_close = 1; |
a99343cf YB |
460 | } |
461 | closeret = close(dirfd); | |
462 | if (closeret < 0) { | |
463 | perror("close"); | |
9544a40b MD |
464 | err_close = 1; |
465 | } | |
466 | if (err_close) { | |
467 | return -1; | |
a99343cf YB |
468 | } |
469 | ||
470 | /* Add path to the global list */ | |
471 | if (traversed_paths == NULL) { | |
472 | fprintf(stderr, "[error] [Context] Invalid open path array.\n"); | |
473 | return -1; | |
474 | } | |
475 | g_ptr_array_add(traversed_paths, g_string_new(fpath)); | |
476 | } | |
477 | ||
478 | return 0; | |
479 | } | |
d0d82191 | 480 | |
20d24215 YB |
481 | /* |
482 | * bt_context_add_traces_recursive: Open a trace recursively | |
483 | * | |
484 | * Find each trace present in the subdirectory starting from the given | |
613f532b MD |
485 | * path, and add them to the context. The packet_seek parameter can be |
486 | * NULL: this specify to use the default format packet_seek. | |
20d24215 | 487 | * |
b945d4a5 | 488 | * Return: 0 on success, < 0 on failure, > 0 on partial failure. |
20d24215 | 489 | * Unable to open toplevel: failure. |
b945d4a5 MD |
490 | * Unable to open some subdirectory or file: warn and continue (partial |
491 | * failure); | |
20d24215 YB |
492 | */ |
493 | int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path, | |
613f532b | 494 | const char *format_str, |
1cf393f6 | 495 | void (*packet_seek)(struct bt_stream_pos *pos, |
613f532b | 496 | size_t offset, int whence)) |
20d24215 | 497 | { |
fd94f11c | 498 | int ret = 0, trace_ids = 0; |
20d24215 | 499 | |
a99343cf | 500 | /* Should lock traversed_paths mutex here if used in multithread */ |
20d24215 | 501 | |
a99343cf | 502 | traversed_paths = g_ptr_array_new(); |
a99343cf YB |
503 | ret = nftw(path, traverse_trace_dir, 10, 0); |
504 | ||
505 | /* Process the array if ntfw did not return a fatal error */ | |
506 | if (ret >= 0) { | |
08c82b90 MD |
507 | int i; |
508 | ||
a99343cf YB |
509 | for (i = 0; i < traversed_paths->len; i++) { |
510 | GString *trace_path = g_ptr_array_index(traversed_paths, | |
511 | i); | |
512 | int trace_id = bt_context_add_trace(ctx, | |
513 | trace_path->str, | |
514 | format_str, | |
515 | packet_seek, | |
516 | NULL, | |
517 | NULL); | |
20d24215 | 518 | if (trace_id < 0) { |
7f89ddce | 519 | fprintf(stderr, "[warning] [Context] cannot open trace \"%s\" from %s " |
a99343cf | 520 | "for reading.\n", trace_path->str, path); |
ca718275 | 521 | /* Allow to skip erroneous traces. */ |
b945d4a5 | 522 | ret = 1; /* partial error */ |
a99343cf | 523 | } else { |
fd94f11c | 524 | trace_ids++; |
20d24215 | 525 | } |
a99343cf | 526 | g_string_free(trace_path, TRUE); |
20d24215 YB |
527 | } |
528 | } | |
d0d82191 | 529 | |
a99343cf YB |
530 | g_ptr_array_free(traversed_paths, TRUE); |
531 | traversed_paths = NULL; | |
532 | ||
533 | /* Should unlock traversed paths mutex here if used in multithread */ | |
20d24215 | 534 | |
27cd3890 MD |
535 | /* |
536 | * Return an error if no trace can be opened. | |
537 | */ | |
fd94f11c | 538 | if (trace_ids == 0) { |
27cd3890 | 539 | fprintf(stderr, "[error] Cannot open any trace for reading.\n\n"); |
b945d4a5 | 540 | ret = -ENOENT; /* failure */ |
27cd3890 | 541 | } |
20d24215 YB |
542 | return ret; |
543 | } | |
a44bc4c9 | 544 | |
7237592a MD |
545 | static |
546 | int trace_pre_handler(struct bt_trace_descriptor *td_write, | |
547 | struct bt_context *ctx) | |
548 | { | |
549 | struct ctf_text_stream_pos *sout; | |
550 | struct trace_collection *tc; | |
551 | int ret, i; | |
552 | ||
553 | sout = container_of(td_write, struct ctf_text_stream_pos, | |
554 | trace_descriptor); | |
555 | ||
556 | if (!sout->parent.pre_trace_cb) | |
557 | return 0; | |
558 | ||
559 | tc = ctx->tc; | |
560 | for (i = 0; i < tc->array->len; i++) { | |
561 | struct bt_trace_descriptor *td = | |
562 | g_ptr_array_index(tc->array, i); | |
563 | ||
564 | ret = sout->parent.pre_trace_cb(&sout->parent, td); | |
565 | if (ret) { | |
566 | fprintf(stderr, "[error] Writing to trace pre handler failed.\n"); | |
567 | goto end; | |
568 | } | |
569 | } | |
570 | ret = 0; | |
571 | end: | |
572 | return ret; | |
573 | } | |
574 | ||
575 | static | |
576 | int trace_post_handler(struct bt_trace_descriptor *td_write, | |
577 | struct bt_context *ctx) | |
578 | { | |
579 | struct ctf_text_stream_pos *sout; | |
580 | struct trace_collection *tc; | |
581 | int ret, i; | |
582 | ||
583 | sout = container_of(td_write, struct ctf_text_stream_pos, | |
584 | trace_descriptor); | |
585 | ||
586 | if (!sout->parent.post_trace_cb) | |
587 | return 0; | |
588 | ||
589 | tc = ctx->tc; | |
590 | for (i = 0; i < tc->array->len; i++) { | |
591 | struct bt_trace_descriptor *td = | |
592 | g_ptr_array_index(tc->array, i); | |
593 | ||
594 | ret = sout->parent.post_trace_cb(&sout->parent, td); | |
595 | if (ret) { | |
596 | fprintf(stderr, "[error] Writing to trace post handler failed.\n"); | |
597 | goto end; | |
598 | } | |
599 | } | |
600 | ret = 0; | |
601 | end: | |
602 | return ret; | |
603 | } | |
604 | ||
605 | static | |
1b8455b7 | 606 | int convert_trace(struct bt_trace_descriptor *td_write, |
95d36295 JD |
607 | struct bt_context *ctx) |
608 | { | |
e4195791 | 609 | struct bt_ctf_iter *iter; |
95d36295 | 610 | struct ctf_text_stream_pos *sout; |
e8c92a62 | 611 | struct bt_iter_pos begin_pos; |
c50d2a7a | 612 | struct bt_ctf_event *ctf_event; |
95d36295 JD |
613 | int ret; |
614 | ||
615 | sout = container_of(td_write, struct ctf_text_stream_pos, | |
616 | trace_descriptor); | |
617 | ||
7237592a MD |
618 | if (!sout->parent.event_cb) |
619 | return 0; | |
620 | ||
95d36295 | 621 | begin_pos.type = BT_SEEK_BEGIN; |
e4195791 | 622 | iter = bt_ctf_iter_create(ctx, &begin_pos, NULL); |
95d36295 JD |
623 | if (!iter) { |
624 | ret = -1; | |
625 | goto error_iter; | |
626 | } | |
e4195791 | 627 | while ((ctf_event = bt_ctf_iter_read_event(iter))) { |
c50d2a7a | 628 | ret = sout->parent.event_cb(&sout->parent, ctf_event->parent->stream); |
95d36295 | 629 | if (ret) { |
3394d22e | 630 | fprintf(stderr, "[error] Writing event failed.\n"); |
95d36295 JD |
631 | goto end; |
632 | } | |
e4195791 | 633 | ret = bt_iter_next(bt_ctf_get_iter(iter)); |
95d36295 JD |
634 | if (ret < 0) |
635 | goto end; | |
636 | } | |
637 | ret = 0; | |
638 | ||
639 | end: | |
e4195791 | 640 | bt_ctf_iter_destroy(iter); |
95d36295 JD |
641 | error_iter: |
642 | return ret; | |
643 | } | |
644 | ||
bbefb8dd | 645 | int main(int argc, char **argv) |
34ac0e6c | 646 | { |
4e85cbfd | 647 | int ret, partial_error = 0, open_success = 0; |
37b99bdb | 648 | struct bt_format *fmt_write; |
1b8455b7 | 649 | struct bt_trace_descriptor *td_write; |
95d36295 | 650 | struct bt_context *ctx; |
4e85cbfd MD |
651 | int i; |
652 | ||
653 | opt_input_paths = g_ptr_array_new(); | |
34ac0e6c MD |
654 | |
655 | ret = parse_options(argc, argv); | |
656 | if (ret < 0) { | |
3394d22e MD |
657 | fprintf(stderr, "Error parsing options.\n\n"); |
658 | usage(stderr); | |
4e85cbfd | 659 | g_ptr_array_free(opt_input_paths, TRUE); |
34ac0e6c MD |
660 | exit(EXIT_FAILURE); |
661 | } else if (ret > 0) { | |
4e85cbfd | 662 | g_ptr_array_free(opt_input_paths, TRUE); |
34ac0e6c MD |
663 | exit(EXIT_SUCCESS); |
664 | } | |
665 | printf_verbose("Verbose mode active.\n"); | |
666 | printf_debug("Debug mode active.\n"); | |
667 | ||
9fe26ec7 | 668 | if (opt_input_format) |
bbefb8dd | 669 | strlower(opt_input_format); |
9fe26ec7 | 670 | if (opt_output_format) |
bbefb8dd MD |
671 | strlower(opt_output_format); |
672 | ||
4e85cbfd MD |
673 | printf_verbose("Converting from directory(ies):\n"); |
674 | for (i = 0; i < opt_input_paths->len; i++) { | |
675 | const char *ipath = g_ptr_array_index(opt_input_paths, i); | |
676 | printf_verbose(" %s\n", ipath); | |
677 | } | |
34ac0e6c | 678 | printf_verbose("Converting from format: %s\n", |
b61922b5 | 679 | opt_input_format ? : "ctf <default>"); |
c3e4d5dc | 680 | printf_verbose("Converting to target: %s\n", |
478b6389 | 681 | opt_output_path ? : "<stdout>"); |
34ac0e6c | 682 | printf_verbose("Converting to format: %s\n", |
b61922b5 | 683 | opt_output_format ? : "text <default>"); |
bbefb8dd | 684 | |
11e1d048 MD |
685 | if (!opt_input_format) { |
686 | opt_input_format = strdup("ctf"); | |
687 | if (!opt_input_format) { | |
688 | partial_error = 1; | |
689 | goto end; | |
690 | } | |
691 | } | |
692 | if (!opt_output_format) { | |
693 | opt_output_format = strdup("text"); | |
694 | if (!opt_output_format) { | |
695 | partial_error = 1; | |
696 | goto end; | |
697 | } | |
698 | } | |
bbefb8dd | 699 | fmt_read = bt_lookup_format(g_quark_from_static_string(opt_input_format)); |
05cab525 | 700 | if (!fmt_read || fmt_read->name != g_quark_from_static_string("ctf")) { |
3394d22e | 701 | fprintf(stderr, "[error] Format \"%s\" is not supported.\n\n", |
bbefb8dd | 702 | opt_input_format); |
11e1d048 MD |
703 | partial_error = 1; |
704 | goto end; | |
bbefb8dd | 705 | } |
bbefb8dd MD |
706 | fmt_write = bt_lookup_format(g_quark_from_static_string(opt_output_format)); |
707 | if (!fmt_write) { | |
3394d22e | 708 | fprintf(stderr, "[error] format \"%s\" is not supported.\n\n", |
bbefb8dd | 709 | opt_output_format); |
11e1d048 MD |
710 | partial_error = 1; |
711 | goto end; | |
bbefb8dd MD |
712 | } |
713 | ||
6cba487f | 714 | ctx = bt_context_create(); |
d63c2d51 JD |
715 | if (!ctx) { |
716 | goto error_td_read; | |
717 | } | |
6cba487f | 718 | |
4e85cbfd MD |
719 | for (i = 0; i < opt_input_paths->len; i++) { |
720 | const char *ipath = g_ptr_array_index(opt_input_paths, i); | |
721 | ret = bt_context_add_traces_recursive(ctx, ipath, | |
722 | opt_input_format, NULL); | |
723 | if (ret < 0) { | |
724 | fprintf(stderr, "[error] opening trace \"%s\" for reading.\n\n", | |
725 | ipath); | |
726 | } else if (ret > 0) { | |
727 | fprintf(stderr, "[warning] errors occurred when opening trace \"%s\" for reading, continuing anyway.\n\n", | |
728 | ipath); | |
729 | open_success = 1; /* some traces were OK */ | |
730 | partial_error = 1; | |
731 | } else { | |
732 | open_success = 1; /* all traces were OK */ | |
733 | } | |
734 | } | |
735 | if (!open_success) { | |
736 | fprintf(stderr, "[error] none of the specified trace paths could be opened.\n\n"); | |
bbefb8dd MD |
737 | goto error_td_read; |
738 | } | |
6cba487f | 739 | |
5b80ddfb | 740 | td_write = fmt_write->open_trace(opt_output_path, O_RDWR, NULL, NULL); |
bbefb8dd | 741 | if (!td_write) { |
3394d22e | 742 | fprintf(stderr, "Error opening trace \"%s\" for writing.\n\n", |
b61922b5 | 743 | opt_output_path ? : "<none>"); |
bbefb8dd MD |
744 | goto error_td_write; |
745 | } | |
847bf71a | 746 | |
24d5af5b MD |
747 | /* |
748 | * Errors happened when opening traces, but we continue anyway. | |
749 | * sleep to let user see the stderr output before stdout. | |
750 | */ | |
4e85cbfd | 751 | if (partial_error) |
24d5af5b | 752 | sleep(PARTIAL_ERROR_SLEEP); |
24d5af5b | 753 | |
7237592a MD |
754 | ret = trace_pre_handler(td_write, ctx); |
755 | if (ret) { | |
756 | fprintf(stderr, "Error in trace pre handle.\n\n"); | |
757 | goto error_copy_trace; | |
758 | } | |
759 | ||
95d36295 | 760 | ret = convert_trace(td_write, ctx); |
46322b33 | 761 | if (ret) { |
3394d22e | 762 | fprintf(stderr, "Error printing trace.\n\n"); |
46322b33 MD |
763 | goto error_copy_trace; |
764 | } | |
847bf71a | 765 | |
7237592a MD |
766 | ret = trace_post_handler(td_write, ctx); |
767 | if (ret) { | |
768 | fprintf(stderr, "Error in trace post handle.\n\n"); | |
769 | goto error_copy_trace; | |
770 | } | |
771 | ||
bbefb8dd | 772 | fmt_write->close_trace(td_write); |
6cba487f MD |
773 | |
774 | bt_context_put(ctx); | |
afb48eae AA |
775 | printf_verbose("finished converting. Output written to:\n%s\n", |
776 | opt_output_path ? : "<stdout>"); | |
11e1d048 | 777 | goto end; |
34ac0e6c | 778 | |
bbefb8dd | 779 | /* Error handling */ |
46322b33 | 780 | error_copy_trace: |
bbefb8dd MD |
781 | fmt_write->close_trace(td_write); |
782 | error_td_write: | |
6cba487f | 783 | bt_context_put(ctx); |
bbefb8dd | 784 | error_td_read: |
11e1d048 MD |
785 | partial_error = 1; |
786 | ||
787 | /* teardown and exit */ | |
788 | end: | |
789 | free(opt_input_format); | |
790 | free(opt_output_format); | |
9fe26ec7 | 791 | free(opt_output_path); |
4e85cbfd | 792 | g_ptr_array_free(opt_input_paths, TRUE); |
11e1d048 MD |
793 | if (partial_error) |
794 | exit(EXIT_FAILURE); | |
795 | else | |
796 | exit(EXIT_SUCCESS); | |
4c8bfb7e | 797 | } |