| 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 <popt.h> |
| 26 | #include <errno.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <ctype.h> |
| 29 | #include <sys/stat.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <ftw.h> |
| 33 | #include <dirent.h> |
| 34 | #include <unistd.h> |
| 35 | |
| 36 | #define DEFAULT_FILE_ARRAY_SIZE 1 |
| 37 | static char *opt_input_format; |
| 38 | static char *opt_output_format; |
| 39 | |
| 40 | static const char *opt_input_path; |
| 41 | static const char *opt_output_path; |
| 42 | |
| 43 | int babeltrace_verbose, babeltrace_debug; |
| 44 | int opt_field_names; |
| 45 | |
| 46 | static struct trace_collection trace_collection_read; |
| 47 | static struct format *fmt_read; |
| 48 | |
| 49 | void strlower(char *str) |
| 50 | { |
| 51 | while (*str) { |
| 52 | *str = tolower(*str); |
| 53 | str++; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | enum { |
| 58 | OPT_NONE = 0, |
| 59 | OPT_HELP, |
| 60 | OPT_LIST, |
| 61 | OPT_VERBOSE, |
| 62 | OPT_DEBUG, |
| 63 | OPT_NAMES, |
| 64 | }; |
| 65 | |
| 66 | static struct poptOption long_options[] = { |
| 67 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 68 | { "input-format", 'i', POPT_ARG_STRING, &opt_input_format, OPT_NONE, NULL, NULL }, |
| 69 | { "output-format", 'o', POPT_ARG_STRING, &opt_output_format, OPT_NONE, NULL, NULL }, |
| 70 | { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL }, |
| 71 | { "list", 'l', POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL }, |
| 72 | { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL }, |
| 73 | { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL }, |
| 74 | { "names", 'n', POPT_ARG_NONE, NULL, OPT_NAMES, NULL, NULL }, |
| 75 | { NULL, 0, 0, NULL, 0, NULL, NULL }, |
| 76 | }; |
| 77 | |
| 78 | static void list_formats(FILE *fp) |
| 79 | { |
| 80 | fprintf(fp, "\n"); |
| 81 | bt_fprintf_format_list(fp); |
| 82 | } |
| 83 | |
| 84 | static void usage(FILE *fp) |
| 85 | { |
| 86 | fprintf(fp, "BabelTrace Trace Converter %s\n\n", VERSION); |
| 87 | fprintf(fp, "usage : babeltrace [OPTIONS] INPUT <OUTPUT>\n"); |
| 88 | fprintf(fp, "\n"); |
| 89 | fprintf(fp, " INPUT Input trace path\n"); |
| 90 | fprintf(fp, " OUTPUT Output trace path (default: stdout)\n"); |
| 91 | fprintf(fp, "\n"); |
| 92 | fprintf(fp, " -i, --input-format FORMAT Input trace format (default: ctf)\n"); |
| 93 | fprintf(fp, " -o, --output-format FORMAT Output trace format (default: text)\n"); |
| 94 | fprintf(fp, "\n"); |
| 95 | fprintf(fp, " -h, --help This help message\n"); |
| 96 | fprintf(fp, " -l, --list List available formats\n"); |
| 97 | fprintf(fp, " -v, --verbose Verbose mode\n"); |
| 98 | fprintf(fp, " -d, --debug Debug mode\n"); |
| 99 | fprintf(fp, " -n, --names Print field names\n"); |
| 100 | list_formats(fp); |
| 101 | fprintf(fp, "\n"); |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * Return 0 if caller should continue, < 0 if caller should return |
| 106 | * error, > 0 if caller should exit without reporting error. |
| 107 | */ |
| 108 | static int parse_options(int argc, char **argv) |
| 109 | { |
| 110 | poptContext pc; |
| 111 | int opt, ret = 0; |
| 112 | |
| 113 | if (argc == 1) { |
| 114 | usage(stdout); |
| 115 | return 1; /* exit cleanly */ |
| 116 | } |
| 117 | |
| 118 | pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0); |
| 119 | poptReadDefaultConfig(pc, 0); |
| 120 | |
| 121 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 122 | switch (opt) { |
| 123 | case OPT_HELP: |
| 124 | usage(stdout); |
| 125 | ret = 1; /* exit cleanly */ |
| 126 | goto end; |
| 127 | case OPT_LIST: |
| 128 | list_formats(stdout); |
| 129 | ret = 1; |
| 130 | goto end; |
| 131 | case OPT_VERBOSE: |
| 132 | babeltrace_verbose = 1; |
| 133 | break; |
| 134 | case OPT_DEBUG: |
| 135 | babeltrace_debug = 1; |
| 136 | break; |
| 137 | case OPT_NAMES: |
| 138 | opt_field_names = 1; |
| 139 | break; |
| 140 | default: |
| 141 | ret = -EINVAL; |
| 142 | goto end; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | opt_input_path = poptGetArg(pc); |
| 147 | if (!opt_input_path) { |
| 148 | ret = -EINVAL; |
| 149 | goto end; |
| 150 | } |
| 151 | opt_output_path = poptGetArg(pc); |
| 152 | end: |
| 153 | if (pc) { |
| 154 | poptFreeContext(pc); |
| 155 | } |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | static void init_trace_collection(struct trace_collection *tc) |
| 160 | { |
| 161 | tc->array = g_ptr_array_sized_new(DEFAULT_FILE_ARRAY_SIZE); |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * finalize_trace_collection() closes the opened traces for read |
| 166 | * and free the memory allocated for trace collection |
| 167 | */ |
| 168 | static void finalize_trace_collection(struct trace_collection *tc) |
| 169 | { |
| 170 | int i; |
| 171 | |
| 172 | for (i = 0; i < tc->array->len; i++) { |
| 173 | struct trace_descriptor *temp = |
| 174 | g_ptr_array_index(tc->array, i); |
| 175 | fmt_read->close_trace(temp); |
| 176 | } |
| 177 | g_ptr_array_free(tc->array, TRUE); |
| 178 | } |
| 179 | |
| 180 | static void trace_collection_add(struct trace_collection *tc, |
| 181 | struct trace_descriptor *td) |
| 182 | { |
| 183 | g_ptr_array_add(tc->array, td); |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * traverse_dir() is the callback functiion for File Tree Walk (nftw). |
| 188 | * it receives the path of the current entry (file, dir, link..etc) with |
| 189 | * a flag to indicate the type of the entry. |
| 190 | * if the entry being visited is a directory and contains a metadata file, |
| 191 | * then open it for reading and save a trace_descriptor to that directory |
| 192 | * in the read trace collection. |
| 193 | */ |
| 194 | static int traverse_dir(const char *fpath, const struct stat *sb, |
| 195 | int tflag, struct FTW *ftwbuf) |
| 196 | { |
| 197 | int dirfd; |
| 198 | int fd; |
| 199 | struct trace_descriptor *td_read; |
| 200 | |
| 201 | if (tflag != FTW_D) |
| 202 | return 0; |
| 203 | dirfd = open(fpath, 0); |
| 204 | if (dirfd < 0) { |
| 205 | fprintf(stdout, "[error] unable to open trace " |
| 206 | "directory file descriptor.\n"); |
| 207 | return -1; |
| 208 | } |
| 209 | fd = openat(dirfd, "metadata", O_RDONLY); |
| 210 | if (fd < 0) { |
| 211 | close(dirfd); |
| 212 | } else { |
| 213 | close(fd); |
| 214 | close(dirfd); |
| 215 | td_read = fmt_read->open_trace(fpath, O_RDONLY); |
| 216 | if (!td_read) { |
| 217 | fprintf(stdout, "Error opening trace \"%s\" " |
| 218 | "for reading.\n\n", fpath); |
| 219 | return -1; /* error */ |
| 220 | } |
| 221 | trace_collection_add(&trace_collection_read, td_read); |
| 222 | } |
| 223 | return 0; /* success */ |
| 224 | } |
| 225 | |
| 226 | int main(int argc, char **argv) |
| 227 | { |
| 228 | int ret; |
| 229 | struct format *fmt_write; |
| 230 | struct trace_descriptor *td_write; |
| 231 | |
| 232 | ret = parse_options(argc, argv); |
| 233 | if (ret < 0) { |
| 234 | fprintf(stdout, "Error parsing options.\n\n"); |
| 235 | usage(stdout); |
| 236 | exit(EXIT_FAILURE); |
| 237 | } else if (ret > 0) { |
| 238 | exit(EXIT_SUCCESS); |
| 239 | } |
| 240 | printf_verbose("Verbose mode active.\n"); |
| 241 | printf_debug("Debug mode active.\n"); |
| 242 | |
| 243 | if (opt_input_format) |
| 244 | strlower(opt_input_format); |
| 245 | if (opt_output_format) |
| 246 | strlower(opt_output_format); |
| 247 | |
| 248 | printf_verbose("Converting from directory: %s\n", opt_input_path); |
| 249 | printf_verbose("Converting from format: %s\n", |
| 250 | opt_input_format ? : "ctf <default>"); |
| 251 | printf_verbose("Converting to directory: %s\n", |
| 252 | opt_output_path ? : "<stdout>"); |
| 253 | printf_verbose("Converting to format: %s\n", |
| 254 | opt_output_format ? : "text <default>"); |
| 255 | |
| 256 | if (!opt_input_format) |
| 257 | opt_input_format = "ctf"; |
| 258 | if (!opt_output_format) |
| 259 | opt_output_format = "text"; |
| 260 | fmt_read = bt_lookup_format(g_quark_from_static_string(opt_input_format)); |
| 261 | if (!fmt_read) { |
| 262 | fprintf(stdout, "[error] Format \"%s\" is not supported.\n\n", |
| 263 | opt_input_format); |
| 264 | exit(EXIT_FAILURE); |
| 265 | } |
| 266 | fmt_write = bt_lookup_format(g_quark_from_static_string(opt_output_format)); |
| 267 | if (!fmt_write) { |
| 268 | fprintf(stdout, "[error] format \"%s\" is not supported.\n\n", |
| 269 | opt_output_format); |
| 270 | exit(EXIT_FAILURE); |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * pass the input path to nftw() . |
| 275 | * specify traverse_dir() as the callback function. |
| 276 | * depth = 10 which is the max number of file descriptors |
| 277 | * that nftw() can open at a given time. |
| 278 | * flags = 0 check nftw documentation for more info . |
| 279 | */ |
| 280 | init_trace_collection(&trace_collection_read); |
| 281 | ret = nftw(opt_input_path, traverse_dir, 10, 0); |
| 282 | if (ret != 0) { |
| 283 | fprintf(stdout, "[error] opening trace \"%s\" for reading.\n\n", |
| 284 | opt_input_path); |
| 285 | goto error_td_read; |
| 286 | } |
| 287 | if (trace_collection_read.array->len == 0) { |
| 288 | fprintf(stdout, "[warning] no metadata file was found." |
| 289 | " no output was generated\n"); |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | td_write = fmt_write->open_trace(opt_output_path, O_RDWR); |
| 294 | if (!td_write) { |
| 295 | fprintf(stdout, "Error opening trace \"%s\" for writing.\n\n", |
| 296 | opt_output_path ? : "<none>"); |
| 297 | goto error_td_write; |
| 298 | } |
| 299 | |
| 300 | ret = convert_trace(td_write, &trace_collection_read); |
| 301 | if (ret) { |
| 302 | fprintf(stdout, "Error printing trace.\n\n"); |
| 303 | goto error_copy_trace; |
| 304 | } |
| 305 | |
| 306 | fmt_write->close_trace(td_write); |
| 307 | finalize_trace_collection(&trace_collection_read); |
| 308 | printf_verbose("finished converting. Output written to:\n%s\n", |
| 309 | opt_output_path ? : "<stdout>"); |
| 310 | exit(EXIT_SUCCESS); |
| 311 | |
| 312 | /* Error handling */ |
| 313 | error_copy_trace: |
| 314 | fmt_write->close_trace(td_write); |
| 315 | error_td_write: |
| 316 | finalize_trace_collection(&trace_collection_read); |
| 317 | error_td_read: |
| 318 | exit(EXIT_FAILURE); |
| 319 | } |