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. | |
c462e188 MD |
19 | * |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
34ac0e6c | 27 | */ |
4c8bfb7e | 28 | |
95d36295 | 29 | #include <babeltrace/babeltrace.h> |
33bceaf8 | 30 | #include <babeltrace/plugin/component-factory.h> |
7c7c0433 JG |
31 | #include <babeltrace/plugin/plugin.h> |
32 | #include <babeltrace/plugin/component-class.h> | |
90298357 | 33 | #include <babeltrace/plugin/notification/iterator.h> |
2e339de1 JG |
34 | #include <babeltrace/ref.h> |
35 | #include <babeltrace/values.h> | |
34ac0e6c | 36 | #include <stdlib.h> |
a44bc4c9 | 37 | #include <babeltrace/ctf-ir/metadata.h> /* for clocks */ |
7f26a816 PP |
38 | #include <popt.h> |
39 | #include <string.h> | |
40 | #include <stdio.h> | |
a44bc4c9 | 41 | |
34ac0e6c | 42 | |
33bceaf8 | 43 | static char *opt_plugin_path; |
56a1cced | 44 | static char *opt_input_path; |
34ac0e6c | 45 | |
34ac0e6c MD |
46 | enum { |
47 | OPT_NONE = 0, | |
33bceaf8 | 48 | OPT_PLUGIN_PATH, |
56a1cced | 49 | OPT_INPUT_PATH, |
34ac0e6c MD |
50 | OPT_VERBOSE, |
51 | OPT_DEBUG, | |
7f26a816 | 52 | OPT_HELP, |
34ac0e6c MD |
53 | }; |
54 | ||
9fe26ec7 | 55 | /* |
33bceaf8 | 56 | * We are _not_ using POPT_ARG_STRING's ability to store directly into |
9fe26ec7 | 57 | * variables, because we want to cast the return to non-const, which is |
41075e78 MD |
58 | * not possible without using poptGetOptArg explicitly. This helps us |
59 | * controlling memory allocation correctly without making assumptions | |
60 | * about undocumented behaviors. poptGetOptArg is documented as | |
61 | * requiring the returned const char * to be freed by the caller. | |
9fe26ec7 | 62 | */ |
34ac0e6c MD |
63 | static struct poptOption long_options[] = { |
64 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
33bceaf8 | 65 | { "plugin-path", 0, POPT_ARG_STRING, NULL, OPT_PLUGIN_PATH, NULL, NULL }, |
56a1cced | 66 | { "input-path", 0, POPT_ARG_STRING, NULL, OPT_INPUT_PATH, NULL, NULL }, |
34ac0e6c MD |
67 | { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL }, |
68 | { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL }, | |
69 | { NULL, 0, 0, NULL, 0, NULL, NULL }, | |
70 | }; | |
71 | ||
34ac0e6c MD |
72 | /* |
73 | * Return 0 if caller should continue, < 0 if caller should return | |
74 | * error, > 0 if caller should exit without reporting error. | |
75 | */ | |
bbefb8dd | 76 | static int parse_options(int argc, char **argv) |
34ac0e6c MD |
77 | { |
78 | poptContext pc; | |
79 | int opt, ret = 0; | |
80 | ||
0f980a35 | 81 | if (argc == 1) { |
0f980a35 MD |
82 | return 1; /* exit cleanly */ |
83 | } | |
84 | ||
bbefb8dd | 85 | pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0); |
34ac0e6c MD |
86 | poptReadDefaultConfig(pc, 0); |
87 | ||
cba1661c | 88 | /* set default */ |
e505794f | 89 | opt_context_field_names = 1; |
cba1661c MD |
90 | opt_payload_field_names = 1; |
91 | ||
34ac0e6c MD |
92 | while ((opt = poptGetNextOpt(pc)) != -1) { |
93 | switch (opt) { | |
94 | case OPT_HELP: | |
34ac0e6c MD |
95 | ret = 1; /* exit cleanly */ |
96 | goto end; | |
33bceaf8 JG |
97 | case OPT_PLUGIN_PATH: |
98 | opt_plugin_path = (char *) poptGetOptArg(pc); | |
99 | if (!opt_plugin_path) { | |
100 | ret = -EINVAL; | |
101 | goto end; | |
56a1cced | 102 | } |
33bceaf8 | 103 | break; |
34ac0e6c MD |
104 | case OPT_VERBOSE: |
105 | babeltrace_verbose = 1; | |
106 | break; | |
107 | case OPT_DEBUG: | |
108 | babeltrace_debug = 1; | |
109 | break; | |
56a1cced JG |
110 | case OPT_INPUT_PATH: |
111 | opt_input_path = (char *) poptGetOptArg(pc); | |
112 | if (!opt_input_path) { | |
113 | ret = -EINVAL; | |
114 | goto end; | |
115 | } | |
116 | break; | |
34ac0e6c MD |
117 | default: |
118 | ret = -EINVAL; | |
119 | goto end; | |
120 | } | |
121 | } | |
122 | ||
34ac0e6c MD |
123 | end: |
124 | if (pc) { | |
125 | poptFreeContext(pc); | |
126 | } | |
127 | return ret; | |
128 | } | |
129 | ||
7c7c0433 JG |
130 | static |
131 | const char *component_type_str(enum bt_component_type type) | |
132 | { | |
133 | switch (type) { | |
134 | case BT_COMPONENT_TYPE_SOURCE: | |
135 | return "source"; | |
136 | case BT_COMPONENT_TYPE_SINK: | |
137 | return "sink"; | |
138 | case BT_COMPONENT_TYPE_FILTER: | |
139 | return "filter"; | |
140 | case BT_COMPONENT_TYPE_UNKNOWN: | |
141 | default: | |
142 | return "unknown"; | |
143 | } | |
144 | } | |
145 | ||
146 | static | |
56a1cced | 147 | void print_found_component_classes(struct bt_component_factory *factory) |
7c7c0433 JG |
148 | { |
149 | int count, i; | |
150 | ||
151 | if (!babeltrace_verbose) { | |
152 | return; | |
153 | } | |
154 | ||
155 | count = bt_component_factory_get_component_class_count(factory); | |
156 | if (count <= 0) { | |
157 | fprintf(stderr, "No component classes found. Please make sure your plug-in search path is set correctly."); | |
158 | return; | |
159 | } | |
160 | ||
161 | printf_verbose("Found %d component classes.\n", count); | |
162 | for (i = 0; i < count; i++) { | |
163 | struct bt_component_class *component_class = | |
164 | bt_component_factory_get_component_class_index( | |
165 | factory, i); | |
166 | struct bt_plugin *plugin = bt_component_class_get_plugin( | |
167 | component_class); | |
168 | const char *plugin_name = bt_plugin_get_name(plugin); | |
169 | const char *component_name = bt_component_class_get_name( | |
170 | component_class); | |
171 | const char *path = bt_plugin_get_path(plugin); | |
172 | const char *author = bt_plugin_get_author(plugin); | |
173 | const char *license = bt_plugin_get_license(plugin); | |
174 | const char *plugin_description = bt_plugin_get_description( | |
175 | plugin); | |
176 | const char *component_description = | |
177 | bt_component_class_get_description( | |
178 | component_class); | |
179 | enum bt_component_type type = bt_component_class_get_type( | |
180 | component_class); | |
181 | ||
182 | printf_verbose("[%s - %s (%s)]\n", plugin_name, component_name, | |
183 | component_type_str(type)); | |
184 | printf_verbose("\tpath: %s\n", path); | |
185 | printf_verbose("\tauthor: %s\n", author); | |
186 | printf_verbose("\tlicense: %s\n", license); | |
187 | printf_verbose("\tplugin description: %s\n", | |
188 | plugin_description ? plugin_description : "None"); | |
189 | printf_verbose("\tcomponent description: %s\n", | |
190 | component_description ? component_description : "None"); | |
0cc9e945 JG |
191 | |
192 | bt_put(plugin); | |
193 | bt_put(component_class); | |
7c7c0433 JG |
194 | } |
195 | } | |
196 | ||
bbefb8dd | 197 | int main(int argc, char **argv) |
34ac0e6c | 198 | { |
7f26a816 | 199 | int ret; |
56a1cced | 200 | enum bt_value_status value_status; |
7c7c0433 | 201 | struct bt_component_factory *component_factory = NULL; |
56a1cced | 202 | struct bt_component_class *source_class = NULL, *sink_class = NULL; |
7c7c0433 JG |
203 | struct bt_component *source = NULL, *sink = NULL; |
204 | struct bt_value *source_params = NULL, *sink_params = NULL; | |
90298357 | 205 | struct bt_notification_iterator *it = NULL; |
34ac0e6c MD |
206 | |
207 | ret = parse_options(argc, argv); | |
208 | if (ret < 0) { | |
3394d22e | 209 | fprintf(stderr, "Error parsing options.\n\n"); |
34ac0e6c MD |
210 | exit(EXIT_FAILURE); |
211 | } else if (ret > 0) { | |
212 | exit(EXIT_SUCCESS); | |
213 | } | |
7f26a816 | 214 | |
56a1cced JG |
215 | source_params = bt_value_map_create(); |
216 | if (!source_params) { | |
217 | fprintf(stderr, "Failed to create source parameters map, aborting...\n"); | |
218 | ret = -1; | |
219 | goto end; | |
220 | } | |
221 | ||
222 | value_status = bt_value_map_insert_string(source_params, "path", | |
223 | opt_input_path); | |
224 | if (value_status != BT_VALUE_STATUS_OK) { | |
225 | ret = -1; | |
226 | goto end; | |
227 | } | |
228 | ||
34ac0e6c MD |
229 | printf_verbose("Verbose mode active.\n"); |
230 | printf_debug("Debug mode active.\n"); | |
231 | ||
33bceaf8 JG |
232 | if (!opt_plugin_path) { |
233 | fprintf(stderr, "No plugin path specified, aborting...\n"); | |
234 | ret = -1; | |
235 | goto end; | |
236 | } | |
7c7c0433 | 237 | printf_verbose("Looking-up plugins at %s\n", |
33bceaf8 JG |
238 | opt_plugin_path ? opt_plugin_path : "Invalid"); |
239 | component_factory = bt_component_factory_create(); | |
240 | if (!component_factory) { | |
241 | fprintf(stderr, "Failed to create component factory.\n"); | |
242 | ret = -1; | |
243 | goto end; | |
244 | } | |
245 | ||
01c34eb9 | 246 | ret = bt_component_factory_load_recursive(component_factory, opt_plugin_path); |
33bceaf8 JG |
247 | if (ret) { |
248 | fprintf(stderr, "Failed to load plugins.\n"); | |
249 | goto end; | |
250 | } | |
251 | ||
56a1cced JG |
252 | print_found_component_classes(component_factory); |
253 | ||
254 | source_class = bt_component_factory_get_component_class( | |
255 | component_factory, "ctf", BT_COMPONENT_TYPE_SOURCE, | |
256 | "fs"); | |
257 | if (!source_class) { | |
258 | fprintf(stderr, "Could not find ctf-fs output component class. Aborting...\n"); | |
259 | ret = -1; | |
260 | goto end; | |
261 | } | |
7c7c0433 JG |
262 | |
263 | sink_class = bt_component_factory_get_component_class(component_factory, | |
56a1cced | 264 | "text", BT_COMPONENT_TYPE_SINK, "text"); |
7c7c0433 JG |
265 | if (!sink_class) { |
266 | fprintf(stderr, "Could not find text output component class. Aborting...\n"); | |
267 | ret = -1; | |
268 | goto end; | |
269 | } | |
270 | ||
56a1cced JG |
271 | source = bt_component_create(source_class, "ctf-fs", source_params); |
272 | if (!source) { | |
273 | fprintf(stderr, "Failed to instantiate source component. Aborting...\n"); | |
274 | ret = -1; | |
275 | goto end; | |
276 | } | |
277 | ||
7c7c0433 JG |
278 | sink = bt_component_create(sink_class, "bt_text_output", sink_params); |
279 | if (!sink) { | |
56a1cced | 280 | fprintf(stderr, "Failed to instantiate output component. Aborting...\n"); |
7c7c0433 | 281 | ret = -1; |
2e339de1 JG |
282 | goto end; |
283 | } | |
284 | ||
90298357 JG |
285 | it = bt_component_source_create_iterator(source); |
286 | if (!it) { | |
287 | fprintf(stderr, "Failed to instantiate source iterator. Aborting...\n"); | |
288 | ret = -1; | |
289 | goto end; | |
290 | } | |
291 | ||
292 | do { | |
78586d8a | 293 | enum bt_component_status sink_status; |
90298357 JG |
294 | struct bt_notification *notification = |
295 | bt_notification_iterator_get_notification(it); | |
78586d8a JG |
296 | |
297 | if (!notification) { | |
298 | /* | |
299 | * Should never happen in final code except after next | |
300 | * has returned BT_NOTIFICATION_ITERATOR_STATUS_END. | |
301 | * | |
302 | * Right now it happens at the first event since the | |
303 | * iterator is not completely initialized and we don't | |
304 | * have a notification "heap" in place. | |
305 | */ | |
306 | continue; | |
307 | } | |
308 | ||
309 | sink_status = bt_component_sink_handle_notification(sink, | |
310 | notification); | |
90298357 | 311 | BT_PUT(notification); |
78586d8a JG |
312 | if (sink_status != BT_COMPONENT_STATUS_OK) { |
313 | fprintf(stderr, "Sink component returned an error, aborting...\n"); | |
314 | break; | |
315 | } | |
90298357 JG |
316 | } while (bt_notification_iterator_next(it) == |
317 | BT_NOTIFICATION_ITERATOR_STATUS_OK); | |
11e1d048 MD |
318 | /* teardown and exit */ |
319 | end: | |
2e339de1 | 320 | BT_PUT(component_factory); |
7c7c0433 JG |
321 | BT_PUT(sink_class); |
322 | BT_PUT(source_class); | |
323 | BT_PUT(source); | |
324 | BT_PUT(sink); | |
325 | BT_PUT(source_params); | |
326 | BT_PUT(sink_params); | |
90298357 | 327 | BT_PUT(it); |
7f26a816 | 328 | return ret ? 1 : 0; |
4c8bfb7e | 329 | } |