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