| 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 | |
| 21 | #define _XOPEN_SOURCE 700 |
| 22 | #include <config.h> |
| 23 | #include <babeltrace/babeltrace.h> |
| 24 | #include <babeltrace/format.h> |
| 25 | #include <babeltrace/context.h> |
| 26 | #include <babeltrace/ctf/types.h> |
| 27 | #include <babeltrace/ctf-text/types.h> |
| 28 | #include <babeltrace/iterator.h> |
| 29 | #include <popt.h> |
| 30 | #include <errno.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <ctype.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <fcntl.h> |
| 36 | #include <ftw.h> |
| 37 | #include <dirent.h> |
| 38 | #include <unistd.h> |
| 39 | #include <inttypes.h> |
| 40 | |
| 41 | #include <babeltrace/ctf-ir/metadata.h> /* for clocks */ |
| 42 | |
| 43 | #define DEFAULT_FILE_ARRAY_SIZE 1 |
| 44 | static char *opt_input_format; |
| 45 | static char *opt_output_format; |
| 46 | |
| 47 | static const char *opt_input_path; |
| 48 | static const char *opt_output_path; |
| 49 | |
| 50 | static struct format *fmt_read; |
| 51 | |
| 52 | void strlower(char *str) |
| 53 | { |
| 54 | while (*str) { |
| 55 | *str = tolower(*str); |
| 56 | str++; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | enum { |
| 61 | OPT_NONE = 0, |
| 62 | OPT_HELP, |
| 63 | OPT_LIST, |
| 64 | OPT_VERBOSE, |
| 65 | OPT_DEBUG, |
| 66 | OPT_NAMES, |
| 67 | OPT_FIELDS, |
| 68 | OPT_NO_DELTA, |
| 69 | OPT_CLOCK_OFFSET, |
| 70 | OPT_CLOCK_RAW, |
| 71 | OPT_CLOCK_SECONDS, |
| 72 | OPT_CLOCK_DATE, |
| 73 | OPT_CLOCK_GMT, |
| 74 | }; |
| 75 | |
| 76 | static struct poptOption long_options[] = { |
| 77 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 78 | { "input-format", 'i', POPT_ARG_STRING, &opt_input_format, OPT_NONE, NULL, NULL }, |
| 79 | { "output-format", 'o', POPT_ARG_STRING, &opt_output_format, OPT_NONE, NULL, NULL }, |
| 80 | { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL }, |
| 81 | { "list", 'l', POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL }, |
| 82 | { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL }, |
| 83 | { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL }, |
| 84 | { "names", 'n', POPT_ARG_STRING, NULL, OPT_NAMES, NULL, NULL }, |
| 85 | { "fields", 'f', POPT_ARG_STRING, NULL, OPT_FIELDS, NULL, NULL }, |
| 86 | { "no-delta", 0, POPT_ARG_NONE, NULL, OPT_NO_DELTA, NULL, NULL }, |
| 87 | { "clock-offset", 0, POPT_ARG_STRING, NULL, OPT_CLOCK_OFFSET, NULL, NULL }, |
| 88 | { "clock-raw", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_RAW, NULL, NULL }, |
| 89 | { "clock-seconds", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_SECONDS, NULL, NULL }, |
| 90 | { "clock-date", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_DATE, NULL, NULL }, |
| 91 | { "clock-gmt", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_GMT, NULL, NULL }, |
| 92 | { NULL, 0, 0, NULL, 0, NULL, NULL }, |
| 93 | }; |
| 94 | |
| 95 | static void list_formats(FILE *fp) |
| 96 | { |
| 97 | fprintf(fp, "\n"); |
| 98 | bt_fprintf_format_list(fp); |
| 99 | } |
| 100 | |
| 101 | static void usage(FILE *fp) |
| 102 | { |
| 103 | fprintf(fp, "BabelTrace Trace Viewer and Converter %s\n\n", VERSION); |
| 104 | fprintf(fp, "usage : babeltrace [OPTIONS] INPUT <OUTPUT>\n"); |
| 105 | fprintf(fp, "\n"); |
| 106 | fprintf(fp, " INPUT Input trace path\n"); |
| 107 | fprintf(fp, " OUTPUT Output trace path (default: stdout)\n"); |
| 108 | fprintf(fp, "\n"); |
| 109 | fprintf(fp, " -i, --input-format FORMAT Input trace format (default: ctf)\n"); |
| 110 | fprintf(fp, " -o, --output-format FORMAT Output trace format (default: text)\n"); |
| 111 | fprintf(fp, "\n"); |
| 112 | fprintf(fp, " -h, --help This help message\n"); |
| 113 | fprintf(fp, " -l, --list List available formats\n"); |
| 114 | fprintf(fp, " -v, --verbose Verbose mode\n"); |
| 115 | fprintf(fp, " (or set BABELTRACE_VERBOSE environment variable)\n"); |
| 116 | fprintf(fp, " -d, --debug Debug mode\n"); |
| 117 | fprintf(fp, " (or set BABELTRACE_DEBUG environment variable)\n"); |
| 118 | fprintf(fp, " --no-delta Do not print time delta between consecutive events\n"); |
| 119 | fprintf(fp, " -n, --names name1<,name2,...> Print field names:\n"); |
| 120 | fprintf(fp, " (payload OR args OR arg)\n"); |
| 121 | fprintf(fp, " all, scope, header, (context OR ctx)\n"); |
| 122 | fprintf(fp, " (payload active by default)\n"); |
| 123 | fprintf(fp, " -f, --fields name1<,name2,...> Print additional fields:\n"); |
| 124 | fprintf(fp, " all, trace, trace:domain, trace:procname,\n"); |
| 125 | fprintf(fp, " trace:vpid, loglevel.\n"); |
| 126 | fprintf(fp, " --clock-raw Disregard internal clock offset (use raw value)\n"); |
| 127 | fprintf(fp, " --clock-offset seconds Clock offset in seconds\n"); |
| 128 | fprintf(fp, " --clock-seconds Print the timestamps as [sec.ns]\n"); |
| 129 | fprintf(fp, " (default is: [hh:mm:ss.ns])\n"); |
| 130 | fprintf(fp, " --clock-date Print clock date\n"); |
| 131 | fprintf(fp, " --clock-gmt Print clock in GMT time zone (default: local time zone)\n"); |
| 132 | list_formats(fp); |
| 133 | fprintf(fp, "\n"); |
| 134 | } |
| 135 | |
| 136 | static int get_names_args(poptContext *pc) |
| 137 | { |
| 138 | char *str, *strlist, *strctx; |
| 139 | |
| 140 | opt_payload_field_names = 0; |
| 141 | strlist = (char *) poptGetOptArg(*pc); |
| 142 | if (!strlist) { |
| 143 | return -EINVAL; |
| 144 | } |
| 145 | str = strtok_r(strlist, ",", &strctx); |
| 146 | do { |
| 147 | if (!strcmp(str, "all")) |
| 148 | opt_all_field_names = 1; |
| 149 | else if (!strcmp(str, "scope")) |
| 150 | opt_scope_field_names = 1; |
| 151 | else if (!strcmp(str, "context") || !strcmp(str, "ctx")) |
| 152 | opt_context_field_names = 1; |
| 153 | else if (!strcmp(str, "header")) |
| 154 | opt_header_field_names = 1; |
| 155 | else if (!strcmp(str, "payload") || !strcmp(str, "args") || !strcmp(str, "arg")) |
| 156 | opt_payload_field_names = 1; |
| 157 | else { |
| 158 | fprintf(stderr, "[error] unknown field name type %s\n", str); |
| 159 | return -EINVAL; |
| 160 | } |
| 161 | } while ((str = strtok_r(NULL, ",", &strctx))); |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | static int get_fields_args(poptContext *pc) |
| 166 | { |
| 167 | char *str, *strlist, *strctx; |
| 168 | |
| 169 | strlist = (char *) poptGetOptArg(*pc); |
| 170 | if (!strlist) { |
| 171 | return -EINVAL; |
| 172 | } |
| 173 | str = strtok_r(strlist, ",", &strctx); |
| 174 | do { |
| 175 | if (!strcmp(str, "all")) |
| 176 | opt_all_fields = 1; |
| 177 | else if (!strcmp(str, "trace")) |
| 178 | opt_trace_field = 1; |
| 179 | else if (!strcmp(str, "trace:domain")) |
| 180 | opt_trace_domain_field = 1; |
| 181 | else if (!strcmp(str, "trace:procname")) |
| 182 | opt_trace_procname_field = 1; |
| 183 | else if (!strcmp(str, "trace:vpid")) |
| 184 | opt_trace_vpid_field = 1; |
| 185 | else if (!strcmp(str, "loglevel")) |
| 186 | opt_loglevel_field = 1; |
| 187 | else { |
| 188 | fprintf(stderr, "[error] unknown field type %s\n", str); |
| 189 | return -EINVAL; |
| 190 | } |
| 191 | } while ((str = strtok_r(NULL, ",", &strctx))); |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * Return 0 if caller should continue, < 0 if caller should return |
| 197 | * error, > 0 if caller should exit without reporting error. |
| 198 | */ |
| 199 | static int parse_options(int argc, char **argv) |
| 200 | { |
| 201 | poptContext pc; |
| 202 | int opt, ret = 0; |
| 203 | |
| 204 | if (argc == 1) { |
| 205 | usage(stdout); |
| 206 | return 1; /* exit cleanly */ |
| 207 | } |
| 208 | |
| 209 | pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0); |
| 210 | poptReadDefaultConfig(pc, 0); |
| 211 | |
| 212 | /* set default */ |
| 213 | opt_payload_field_names = 1; |
| 214 | |
| 215 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 216 | switch (opt) { |
| 217 | case OPT_HELP: |
| 218 | usage(stdout); |
| 219 | ret = 1; /* exit cleanly */ |
| 220 | goto end; |
| 221 | case OPT_LIST: |
| 222 | list_formats(stdout); |
| 223 | ret = 1; |
| 224 | goto end; |
| 225 | case OPT_VERBOSE: |
| 226 | babeltrace_verbose = 1; |
| 227 | break; |
| 228 | case OPT_NAMES: |
| 229 | if (get_names_args(&pc)) { |
| 230 | ret = -EINVAL; |
| 231 | goto end; |
| 232 | } |
| 233 | break; |
| 234 | case OPT_FIELDS: |
| 235 | if (get_fields_args(&pc)) { |
| 236 | ret = -EINVAL; |
| 237 | goto end; |
| 238 | } |
| 239 | break; |
| 240 | case OPT_DEBUG: |
| 241 | babeltrace_debug = 1; |
| 242 | break; |
| 243 | case OPT_NO_DELTA: |
| 244 | opt_delta_field = 0; |
| 245 | break; |
| 246 | case OPT_CLOCK_RAW: |
| 247 | opt_clock_raw = 1; |
| 248 | break; |
| 249 | case OPT_CLOCK_OFFSET: |
| 250 | { |
| 251 | char *str, *endptr; |
| 252 | |
| 253 | str = poptGetOptArg(pc); |
| 254 | if (!str) { |
| 255 | fprintf(stderr, "[error] Missing --clock-offset argument\n"); |
| 256 | ret = -EINVAL; |
| 257 | goto end; |
| 258 | } |
| 259 | errno = 0; |
| 260 | opt_clock_offset = strtoull(str, &endptr, 0); |
| 261 | if (*endptr != '\0' || str == endptr || errno != 0) { |
| 262 | fprintf(stderr, "[error] Incorrect --clock-offset argument: %s\n", str); |
| 263 | ret = -EINVAL; |
| 264 | goto end; |
| 265 | } |
| 266 | break; |
| 267 | } |
| 268 | case OPT_CLOCK_SECONDS: |
| 269 | opt_clock_seconds = 1; |
| 270 | break; |
| 271 | case OPT_CLOCK_DATE: |
| 272 | opt_clock_date = 1; |
| 273 | break; |
| 274 | case OPT_CLOCK_GMT: |
| 275 | opt_clock_gmt = 1; |
| 276 | break; |
| 277 | |
| 278 | default: |
| 279 | ret = -EINVAL; |
| 280 | goto end; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | opt_input_path = poptGetArg(pc); |
| 285 | if (!opt_input_path) { |
| 286 | ret = -EINVAL; |
| 287 | goto end; |
| 288 | } |
| 289 | opt_output_path = poptGetArg(pc); |
| 290 | |
| 291 | end: |
| 292 | if (pc) { |
| 293 | poptFreeContext(pc); |
| 294 | } |
| 295 | return ret; |
| 296 | } |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | int convert_trace(struct trace_descriptor *td_write, |
| 303 | struct bt_context *ctx) |
| 304 | { |
| 305 | struct bt_iter *iter; |
| 306 | struct ctf_stream *stream; |
| 307 | struct ctf_stream_event *event; |
| 308 | struct ctf_text_stream_pos *sout; |
| 309 | struct bt_iter_pos begin_pos; |
| 310 | int ret; |
| 311 | |
| 312 | sout = container_of(td_write, struct ctf_text_stream_pos, |
| 313 | trace_descriptor); |
| 314 | |
| 315 | begin_pos.type = BT_SEEK_BEGIN; |
| 316 | iter = bt_iter_create(ctx, &begin_pos, NULL); |
| 317 | if (!iter) { |
| 318 | ret = -1; |
| 319 | goto error_iter; |
| 320 | } |
| 321 | while (bt_iter_read_event(iter, &stream, &event) == 0) { |
| 322 | ret = sout->parent.event_cb(&sout->parent, stream); |
| 323 | if (ret) { |
| 324 | fprintf(stderr, "[error] Writing event failed.\n"); |
| 325 | goto end; |
| 326 | } |
| 327 | ret = bt_iter_next(iter); |
| 328 | if (ret < 0) |
| 329 | goto end; |
| 330 | } |
| 331 | ret = 0; |
| 332 | |
| 333 | end: |
| 334 | bt_iter_destroy(iter); |
| 335 | error_iter: |
| 336 | return ret; |
| 337 | } |
| 338 | |
| 339 | int main(int argc, char **argv) |
| 340 | { |
| 341 | int ret; |
| 342 | struct format *fmt_write; |
| 343 | struct trace_descriptor *td_write; |
| 344 | struct bt_context *ctx; |
| 345 | |
| 346 | ret = parse_options(argc, argv); |
| 347 | if (ret < 0) { |
| 348 | fprintf(stderr, "Error parsing options.\n\n"); |
| 349 | usage(stderr); |
| 350 | exit(EXIT_FAILURE); |
| 351 | } else if (ret > 0) { |
| 352 | exit(EXIT_SUCCESS); |
| 353 | } |
| 354 | printf_verbose("Verbose mode active.\n"); |
| 355 | printf_debug("Debug mode active.\n"); |
| 356 | |
| 357 | if (opt_input_format) |
| 358 | strlower(opt_input_format); |
| 359 | if (opt_output_format) |
| 360 | strlower(opt_output_format); |
| 361 | |
| 362 | printf_verbose("Converting from directory: %s\n", opt_input_path); |
| 363 | printf_verbose("Converting from format: %s\n", |
| 364 | opt_input_format ? : "ctf <default>"); |
| 365 | printf_verbose("Converting to directory: %s\n", |
| 366 | opt_output_path ? : "<stdout>"); |
| 367 | printf_verbose("Converting to format: %s\n", |
| 368 | opt_output_format ? : "text <default>"); |
| 369 | |
| 370 | if (!opt_input_format) |
| 371 | opt_input_format = "ctf"; |
| 372 | if (!opt_output_format) |
| 373 | opt_output_format = "text"; |
| 374 | fmt_read = bt_lookup_format(g_quark_from_static_string(opt_input_format)); |
| 375 | if (!fmt_read) { |
| 376 | fprintf(stderr, "[error] Format \"%s\" is not supported.\n\n", |
| 377 | opt_input_format); |
| 378 | exit(EXIT_FAILURE); |
| 379 | } |
| 380 | fmt_write = bt_lookup_format(g_quark_from_static_string(opt_output_format)); |
| 381 | if (!fmt_write) { |
| 382 | fprintf(stderr, "[error] format \"%s\" is not supported.\n\n", |
| 383 | opt_output_format); |
| 384 | exit(EXIT_FAILURE); |
| 385 | } |
| 386 | |
| 387 | ctx = bt_context_create(); |
| 388 | |
| 389 | ret = bt_context_add_traces_recursive(ctx, opt_input_path, |
| 390 | opt_input_format); |
| 391 | if (ret) { |
| 392 | fprintf(stderr, "[error] opening trace \"%s\" for reading.\n\n", |
| 393 | opt_input_path); |
| 394 | goto error_td_read; |
| 395 | } |
| 396 | |
| 397 | td_write = fmt_write->open_trace(opt_output_path, O_RDWR, NULL, NULL); |
| 398 | if (!td_write) { |
| 399 | fprintf(stderr, "Error opening trace \"%s\" for writing.\n\n", |
| 400 | opt_output_path ? : "<none>"); |
| 401 | goto error_td_write; |
| 402 | } |
| 403 | |
| 404 | ret = convert_trace(td_write, ctx); |
| 405 | if (ret) { |
| 406 | fprintf(stderr, "Error printing trace.\n\n"); |
| 407 | goto error_copy_trace; |
| 408 | } |
| 409 | |
| 410 | fmt_write->close_trace(td_write); |
| 411 | |
| 412 | bt_context_put(ctx); |
| 413 | printf_verbose("finished converting. Output written to:\n%s\n", |
| 414 | opt_output_path ? : "<stdout>"); |
| 415 | exit(EXIT_SUCCESS); |
| 416 | |
| 417 | /* Error handling */ |
| 418 | error_copy_trace: |
| 419 | fmt_write->close_trace(td_write); |
| 420 | error_td_write: |
| 421 | bt_context_put(ctx); |
| 422 | error_td_read: |
| 423 | exit(EXIT_FAILURE); |
| 424 | } |