Commit | Line | Data |
---|---|---|
34ac0e6c MD |
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 | */ | |
4c8bfb7e | 18 | |
34ac0e6c | 19 | #include <babeltrace/babeltrace.h> |
7fb21036 | 20 | #include <babeltrace/format.h> |
34ac0e6c MD |
21 | #include <popt.h> |
22 | #include <errno.h> | |
23 | #include <stdlib.h> | |
bbefb8dd MD |
24 | #include <ctype.h> |
25 | #include <sys/stat.h> | |
26 | #include <sys/types.h> | |
27 | #include <fcntl.h> | |
4c8bfb7e | 28 | |
bbefb8dd MD |
29 | static char *opt_input_format; |
30 | static char *opt_output_format; | |
34ac0e6c MD |
31 | |
32 | static const char *opt_input_path; | |
33 | static const char *opt_output_path; | |
34 | ||
35 | int babeltrace_verbose, babeltrace_debug; | |
36 | ||
bbefb8dd MD |
37 | void strlower(char *str) |
38 | { | |
39 | while (*str) { | |
40 | *str = tolower(*str); | |
41 | str++; | |
42 | } | |
43 | } | |
44 | ||
34ac0e6c MD |
45 | enum { |
46 | OPT_NONE = 0, | |
47 | OPT_HELP, | |
7fb21036 | 48 | OPT_LIST, |
34ac0e6c MD |
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 }, | |
7fb21036 | 58 | { "list", 'l', POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL }, |
34ac0e6c MD |
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 | ||
7fb21036 MD |
64 | static void list_formats(FILE *fp) |
65 | { | |
66 | fprintf(fp, "\n"); | |
67 | bt_fprintf_format_list(fp); | |
68 | } | |
69 | ||
34ac0e6c | 70 | static void usage(FILE *fp) |
4c8bfb7e | 71 | { |
0f980a35 MD |
72 | fprintf(fp, "BabelTrace Trace Converter %u.%u\n\n", |
73 | BABELTRACE_VERSION_MAJOR, | |
34ac0e6c MD |
74 | BABELTRACE_VERSION_MINOR); |
75 | fprintf(fp, "usage : babeltrace [OPTIONS] INPUT OUTPUT\n"); | |
76 | fprintf(fp, "\n"); | |
77 | fprintf(fp, " INPUT Input trace path\n"); | |
78 | fprintf(fp, " OUTPUT Output trace path\n"); | |
79 | fprintf(fp, "\n"); | |
80 | fprintf(fp, " -i, --input-format Input trace path\n"); | |
81 | fprintf(fp, " -o, --output-format Input trace path\n"); | |
82 | fprintf(fp, "\n"); | |
83 | fprintf(fp, " -h, --help This help message\n"); | |
7fb21036 | 84 | fprintf(fp, " -l, --list List available formats\n"); |
34ac0e6c MD |
85 | fprintf(fp, " -v, --verbose Verbose mode\n"); |
86 | fprintf(fp, " -d, --debug Debug mode\n"); | |
7fb21036 | 87 | list_formats(fp); |
34ac0e6c MD |
88 | fprintf(fp, "\n"); |
89 | } | |
90 | ||
91 | /* | |
92 | * Return 0 if caller should continue, < 0 if caller should return | |
93 | * error, > 0 if caller should exit without reporting error. | |
94 | */ | |
bbefb8dd | 95 | static int parse_options(int argc, char **argv) |
34ac0e6c MD |
96 | { |
97 | poptContext pc; | |
98 | int opt, ret = 0; | |
99 | ||
0f980a35 MD |
100 | if (argc == 1) { |
101 | usage(stdout); | |
102 | return 1; /* exit cleanly */ | |
103 | } | |
104 | ||
bbefb8dd | 105 | pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0); |
34ac0e6c MD |
106 | poptReadDefaultConfig(pc, 0); |
107 | ||
108 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
109 | switch (opt) { | |
110 | case OPT_HELP: | |
7fb21036 | 111 | usage(stdout); |
34ac0e6c MD |
112 | ret = 1; /* exit cleanly */ |
113 | goto end; | |
7fb21036 MD |
114 | case OPT_LIST: |
115 | list_formats(stdout); | |
116 | ret = 1; | |
117 | goto end; | |
34ac0e6c MD |
118 | case OPT_VERBOSE: |
119 | babeltrace_verbose = 1; | |
120 | break; | |
121 | case OPT_DEBUG: | |
122 | babeltrace_debug = 1; | |
123 | break; | |
124 | default: | |
125 | ret = -EINVAL; | |
126 | goto end; | |
127 | } | |
128 | } | |
129 | ||
130 | opt_input_path = poptGetArg(pc); | |
131 | if (!opt_input_path) { | |
132 | ret = -EINVAL; | |
133 | goto end; | |
134 | } | |
135 | opt_output_path = poptGetArg(pc); | |
34ac0e6c MD |
136 | end: |
137 | if (pc) { | |
138 | poptFreeContext(pc); | |
139 | } | |
140 | return ret; | |
141 | } | |
142 | ||
bbefb8dd | 143 | int main(int argc, char **argv) |
34ac0e6c MD |
144 | { |
145 | int ret; | |
bbefb8dd MD |
146 | struct format *fmt_read, *fmt_write; |
147 | struct trace_descriptor *td_read, *td_write; | |
34ac0e6c MD |
148 | |
149 | ret = parse_options(argc, argv); | |
150 | if (ret < 0) { | |
bbefb8dd | 151 | fprintf(stdout, "Error parsing options.\n\n"); |
34ac0e6c MD |
152 | usage(stdout); |
153 | exit(EXIT_FAILURE); | |
154 | } else if (ret > 0) { | |
155 | exit(EXIT_SUCCESS); | |
156 | } | |
157 | printf_verbose("Verbose mode active.\n"); | |
158 | printf_debug("Debug mode active.\n"); | |
159 | ||
bbefb8dd MD |
160 | if (opt_input_format) |
161 | strlower(opt_input_format); | |
162 | if (opt_output_format) | |
163 | strlower(opt_output_format); | |
164 | ||
34ac0e6c MD |
165 | printf_verbose("Converting from file: %s\n", opt_input_path); |
166 | printf_verbose("Converting from format: %s\n", | |
167 | opt_input_format ? : "<autodetect>"); | |
478b6389 MD |
168 | printf_verbose("Converting to file: %s\n", |
169 | opt_output_path ? : "<stdout>"); | |
34ac0e6c | 170 | printf_verbose("Converting to format: %s\n", |
bbefb8dd MD |
171 | opt_output_format ? : "ctf"); |
172 | ||
173 | if (!opt_input_format) { | |
c8b219a3 | 174 | fprintf(stdout, "[error] Input format autodetection not implemented yet.\n\n"); |
bbefb8dd MD |
175 | usage(stdout); |
176 | exit(EXIT_FAILURE); | |
177 | } | |
178 | fmt_read = bt_lookup_format(g_quark_from_static_string(opt_input_format)); | |
179 | if (!fmt_read) { | |
c8b219a3 | 180 | fprintf(stdout, "[error] Format \"%s\" is not supported.\n\n", |
bbefb8dd MD |
181 | opt_input_format); |
182 | exit(EXIT_FAILURE); | |
183 | } | |
184 | if (!opt_output_format) | |
185 | opt_output_format = "ctf"; | |
186 | fmt_write = bt_lookup_format(g_quark_from_static_string(opt_output_format)); | |
187 | if (!fmt_write) { | |
c8b219a3 | 188 | fprintf(stdout, "[error] format \"%s\" is not supported.\n\n", |
bbefb8dd MD |
189 | opt_output_format); |
190 | exit(EXIT_FAILURE); | |
191 | } | |
192 | ||
193 | td_read = fmt_read->open_trace(opt_input_path, O_RDONLY); | |
194 | if (!td_read) { | |
c8b219a3 | 195 | fprintf(stdout, "[error] opening trace \"%s\" for reading.\n\n", |
bbefb8dd MD |
196 | opt_input_path); |
197 | goto error_td_read; | |
198 | } | |
199 | ||
478b6389 MD |
200 | if (!opt_output_path) |
201 | opt_output_path = "/dev/stdout"; | |
bbefb8dd MD |
202 | td_write = fmt_write->open_trace(opt_output_path, O_WRONLY); |
203 | if (!td_write) { | |
204 | fprintf(stdout, "Error opening trace \"%s\" for writing.\n\n", | |
205 | opt_output_path); | |
206 | goto error_td_write; | |
207 | } | |
847bf71a MD |
208 | |
209 | ret = convert_trace(td_write, td_read); | |
46322b33 MD |
210 | if (ret) { |
211 | fprintf(stdout, "Error printing trace.\n\n"); | |
212 | goto error_copy_trace; | |
213 | } | |
847bf71a | 214 | |
bbefb8dd MD |
215 | fmt_write->close_trace(td_write); |
216 | fmt_read->close_trace(td_read); | |
217 | exit(EXIT_SUCCESS); | |
34ac0e6c | 218 | |
bbefb8dd | 219 | /* Error handling */ |
46322b33 | 220 | error_copy_trace: |
bbefb8dd MD |
221 | fmt_write->close_trace(td_write); |
222 | error_td_write: | |
223 | fmt_read->close_trace(td_read); | |
224 | error_td_read: | |
225 | exit(EXIT_FAILURE); | |
4c8bfb7e | 226 | } |