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