| 1 | /* |
| 2 | * babeltrace.c |
| 3 | * |
| 4 | * Babeltrace Trace Converter |
| 5 | * |
| 6 | * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | */ |
| 18 | |
| 19 | #include <babeltrace/babeltrace.h> |
| 20 | #include <babeltrace/format.h> |
| 21 | #include <popt.h> |
| 22 | #include <errno.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <ctype.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <fcntl.h> |
| 28 | |
| 29 | static char *opt_input_format; |
| 30 | static char *opt_output_format; |
| 31 | |
| 32 | static const char *opt_input_path; |
| 33 | static const char *opt_output_path; |
| 34 | |
| 35 | int babeltrace_verbose, babeltrace_debug; |
| 36 | |
| 37 | void strlower(char *str) |
| 38 | { |
| 39 | while (*str) { |
| 40 | *str = tolower(*str); |
| 41 | str++; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | enum { |
| 46 | OPT_NONE = 0, |
| 47 | OPT_HELP, |
| 48 | OPT_LIST, |
| 49 | OPT_VERBOSE, |
| 50 | OPT_DEBUG, |
| 51 | }; |
| 52 | |
| 53 | static struct poptOption long_options[] = { |
| 54 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 55 | { "input-format", 'i', POPT_ARG_STRING, &opt_input_format, OPT_NONE, NULL, NULL }, |
| 56 | { "output-format", 'o', POPT_ARG_STRING, &opt_output_format, OPT_NONE, NULL, NULL }, |
| 57 | { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL }, |
| 58 | { "list", 'l', POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL }, |
| 59 | { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL }, |
| 60 | { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL }, |
| 61 | { NULL, 0, 0, NULL, 0, NULL, NULL }, |
| 62 | }; |
| 63 | |
| 64 | static void list_formats(FILE *fp) |
| 65 | { |
| 66 | fprintf(fp, "\n"); |
| 67 | bt_fprintf_format_list(fp); |
| 68 | } |
| 69 | |
| 70 | static void usage(FILE *fp) |
| 71 | { |
| 72 | fprintf(fp, "Babeltrace %u.%u\n\n", BABELTRACE_VERSION_MAJOR, |
| 73 | BABELTRACE_VERSION_MINOR); |
| 74 | fprintf(fp, "usage : babeltrace [OPTIONS] INPUT OUTPUT\n"); |
| 75 | fprintf(fp, "\n"); |
| 76 | fprintf(fp, " INPUT Input trace path\n"); |
| 77 | fprintf(fp, " OUTPUT Output trace path\n"); |
| 78 | fprintf(fp, "\n"); |
| 79 | fprintf(fp, " -i, --input-format Input trace path\n"); |
| 80 | fprintf(fp, " -o, --output-format Input trace path\n"); |
| 81 | fprintf(fp, "\n"); |
| 82 | fprintf(fp, " -h, --help This help message\n"); |
| 83 | fprintf(fp, " -l, --list List available formats\n"); |
| 84 | fprintf(fp, " -v, --verbose Verbose mode\n"); |
| 85 | fprintf(fp, " -d, --debug Debug mode\n"); |
| 86 | list_formats(fp); |
| 87 | fprintf(fp, "\n"); |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * Return 0 if caller should continue, < 0 if caller should return |
| 92 | * error, > 0 if caller should exit without reporting error. |
| 93 | */ |
| 94 | static int parse_options(int argc, char **argv) |
| 95 | { |
| 96 | poptContext pc; |
| 97 | int opt, ret = 0; |
| 98 | |
| 99 | pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0); |
| 100 | poptReadDefaultConfig(pc, 0); |
| 101 | |
| 102 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 103 | switch (opt) { |
| 104 | case OPT_HELP: |
| 105 | usage(stdout); |
| 106 | ret = 1; /* exit cleanly */ |
| 107 | goto end; |
| 108 | case OPT_LIST: |
| 109 | list_formats(stdout); |
| 110 | ret = 1; |
| 111 | goto end; |
| 112 | case OPT_VERBOSE: |
| 113 | babeltrace_verbose = 1; |
| 114 | break; |
| 115 | case OPT_DEBUG: |
| 116 | babeltrace_debug = 1; |
| 117 | break; |
| 118 | default: |
| 119 | ret = -EINVAL; |
| 120 | goto end; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | opt_input_path = poptGetArg(pc); |
| 125 | if (!opt_input_path) { |
| 126 | ret = -EINVAL; |
| 127 | goto end; |
| 128 | } |
| 129 | opt_output_path = poptGetArg(pc); |
| 130 | if (!opt_output_path) { |
| 131 | ret = -EINVAL; |
| 132 | goto end; |
| 133 | } |
| 134 | end: |
| 135 | if (pc) { |
| 136 | poptFreeContext(pc); |
| 137 | } |
| 138 | return ret; |
| 139 | } |
| 140 | |
| 141 | int main(int argc, char **argv) |
| 142 | { |
| 143 | int ret; |
| 144 | struct format *fmt_read, *fmt_write; |
| 145 | struct trace_descriptor *td_read, *td_write; |
| 146 | |
| 147 | ret = parse_options(argc, argv); |
| 148 | if (ret < 0) { |
| 149 | fprintf(stdout, "Error parsing options.\n\n"); |
| 150 | usage(stdout); |
| 151 | exit(EXIT_FAILURE); |
| 152 | } else if (ret > 0) { |
| 153 | exit(EXIT_SUCCESS); |
| 154 | } |
| 155 | printf_verbose("Verbose mode active.\n"); |
| 156 | printf_debug("Debug mode active.\n"); |
| 157 | |
| 158 | if (opt_input_format) |
| 159 | strlower(opt_input_format); |
| 160 | if (opt_output_format) |
| 161 | strlower(opt_output_format); |
| 162 | |
| 163 | printf_verbose("Converting from file: %s\n", opt_input_path); |
| 164 | printf_verbose("Converting from format: %s\n", |
| 165 | opt_input_format ? : "<autodetect>"); |
| 166 | printf_verbose("Converting to file: %s\n", opt_output_path); |
| 167 | printf_verbose("Converting to format: %s\n", |
| 168 | opt_output_format ? : "ctf"); |
| 169 | |
| 170 | if (!opt_input_format) { |
| 171 | fprintf(stdout, "Error: input format autodetection not implemented yet.\n\n"); |
| 172 | usage(stdout); |
| 173 | exit(EXIT_FAILURE); |
| 174 | } |
| 175 | fmt_read = bt_lookup_format(g_quark_from_static_string(opt_input_format)); |
| 176 | if (!fmt_read) { |
| 177 | fprintf(stdout, "Error: format \"%s\" is not supported.\n\n", |
| 178 | opt_input_format); |
| 179 | exit(EXIT_FAILURE); |
| 180 | } |
| 181 | if (!opt_output_format) |
| 182 | opt_output_format = "ctf"; |
| 183 | fmt_write = bt_lookup_format(g_quark_from_static_string(opt_output_format)); |
| 184 | if (!fmt_write) { |
| 185 | fprintf(stdout, "Error: format \"%s\" is not supported.\n\n", |
| 186 | opt_output_format); |
| 187 | exit(EXIT_FAILURE); |
| 188 | } |
| 189 | |
| 190 | td_read = fmt_read->open_trace(opt_input_path, O_RDONLY); |
| 191 | if (!td_read) { |
| 192 | fprintf(stdout, "Error opening trace \"%s\" for reading.\n\n", |
| 193 | opt_input_path); |
| 194 | goto error_td_read; |
| 195 | } |
| 196 | |
| 197 | td_write = fmt_write->open_trace(opt_output_path, O_WRONLY); |
| 198 | if (!td_write) { |
| 199 | fprintf(stdout, "Error opening trace \"%s\" for writing.\n\n", |
| 200 | opt_output_path); |
| 201 | goto error_td_write; |
| 202 | } |
| 203 | |
| 204 | fmt_write->close_trace(td_write); |
| 205 | fmt_read->close_trace(td_read); |
| 206 | exit(EXIT_SUCCESS); |
| 207 | |
| 208 | /* Error handling */ |
| 209 | fmt_write->close_trace(td_write); |
| 210 | error_td_write: |
| 211 | fmt_read->close_trace(td_read); |
| 212 | error_td_read: |
| 213 | exit(EXIT_FAILURE); |
| 214 | } |