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