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