fed201e7d5fbe4cd228a598f8cc39f541d141320
[babeltrace.git] / converter / babeltrace.c
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 * 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.
27 */
28
29 #include <babeltrace/babeltrace.h>
30 #include <babeltrace/plugin/component-factory.h>
31 #include <babeltrace/plugin/plugin.h>
32 #include <babeltrace/plugin/component-class.h>
33 #include <babeltrace/plugin/notification/iterator.h>
34 #include <babeltrace/ref.h>
35 #include <babeltrace/values.h>
36 #include <stdlib.h>
37 #include <babeltrace/ctf-ir/metadata.h> /* for clocks */
38 #include <popt.h>
39 #include <string.h>
40 #include <stdio.h>
41 #include "babeltrace-cfg.h"
42
43 static
44 const char *component_type_str(enum bt_component_type type)
45 {
46 switch (type) {
47 case BT_COMPONENT_TYPE_SOURCE:
48 return "source";
49 case BT_COMPONENT_TYPE_SINK:
50 return "sink";
51 case BT_COMPONENT_TYPE_FILTER:
52 return "filter";
53 case BT_COMPONENT_TYPE_UNKNOWN:
54 default:
55 return "unknown";
56 }
57 }
58
59 static
60 void print_component_classes_found(struct bt_component_factory *factory)
61 {
62 int count, i;
63
64 babeltrace_verbose = 1;
65
66 if (!babeltrace_verbose) {
67 return;
68 }
69
70 count = bt_component_factory_get_component_class_count(factory);
71 if (count <= 0) {
72 fprintf(stderr, "No component classes found. Please make sure your plug-in search path is set correctly.");
73 return;
74 }
75
76 printf_verbose("Found %d component classes.\n", count);
77 for (i = 0; i < count; i++) {
78 struct bt_component_class *component_class =
79 bt_component_factory_get_component_class_index(
80 factory, i);
81 struct bt_plugin *plugin = bt_component_class_get_plugin(
82 component_class);
83 const char *plugin_name = bt_plugin_get_name(plugin);
84 const char *component_name = bt_component_class_get_name(
85 component_class);
86 const char *path = bt_plugin_get_path(plugin);
87 const char *author = bt_plugin_get_author(plugin);
88 const char *license = bt_plugin_get_license(plugin);
89 const char *plugin_description = bt_plugin_get_description(
90 plugin);
91 const char *component_description =
92 bt_component_class_get_description(
93 component_class);
94 enum bt_component_type type = bt_component_class_get_type(
95 component_class);
96
97 printf_verbose("[%s - %s (%s)]\n", plugin_name, component_name,
98 component_type_str(type));
99 printf_verbose("\tpath: %s\n", path);
100 printf_verbose("\tauthor: %s\n", author);
101 printf_verbose("\tlicense: %s\n", license);
102 printf_verbose("\tplugin description: %s\n",
103 plugin_description ? plugin_description : "None");
104 printf_verbose("\tcomponent description: %s\n",
105 component_description ? component_description : "None");
106
107 bt_put(plugin);
108 bt_put(component_class);
109 }
110 }
111
112 static
113 void print_indent(size_t indent)
114 {
115 size_t i;
116
117 for (i = 0; i < indent; i++) {
118 printf(" ");
119 }
120 }
121
122 static
123 void print_value(struct bt_value *, size_t, bool);
124
125 static
126 bool print_map_value(const char *key, struct bt_value *object, void *data)
127 {
128 size_t indent = (size_t) data;
129
130 print_indent(indent);
131 printf("\"%s\": ", key);
132 print_value(object, indent, false);
133
134 return true;
135 }
136
137 static
138 void print_value(struct bt_value *value, size_t indent, bool do_indent)
139 {
140 bool bool_val;
141 int64_t int_val;
142 double dbl_val;
143 const char *str_val;
144 int size;
145 int i;
146
147 if (!value) {
148 return;
149 }
150
151 if (do_indent) {
152 print_indent(indent);
153 }
154
155 switch (bt_value_get_type(value)) {
156 case BT_VALUE_TYPE_NULL:
157 printf("null\n");
158 break;
159 case BT_VALUE_TYPE_BOOL:
160 bt_value_bool_get(value, &bool_val);
161 printf("%s\n", bool_val ? "true" : "false");
162 break;
163 case BT_VALUE_TYPE_INTEGER:
164 bt_value_integer_get(value, &int_val);
165 printf("%" PRId64 "\n", int_val);
166 break;
167 case BT_VALUE_TYPE_FLOAT:
168 bt_value_float_get(value, &dbl_val);
169 printf("%lf\n", dbl_val);
170 break;
171 case BT_VALUE_TYPE_STRING:
172 bt_value_string_get(value, &str_val);
173 printf("\"%s\"\n", str_val);
174 break;
175 case BT_VALUE_TYPE_ARRAY:
176 size = bt_value_array_size(value);
177 printf("[\n");
178
179 for (i = 0; i < size; i++) {
180 struct bt_value *element =
181 bt_value_array_get(value, i);
182
183 print_value(element, indent + 2, true);
184 BT_PUT(element);
185 }
186
187 print_indent(indent);
188 printf("]\n");
189 break;
190 case BT_VALUE_TYPE_MAP:
191 if (bt_value_map_is_empty(value)) {
192 printf("{}\n");
193 return;
194 }
195
196 printf("{\n");
197 bt_value_map_foreach(value, print_map_value,
198 (void *) (indent + 2));
199 print_indent(indent);
200 printf("}\n");
201 break;
202 default:
203 assert(false);
204 }
205 }
206
207 static
208 void print_bt_config_component(struct bt_config_component *bt_config_component)
209 {
210 printf(" %s/%s\n", bt_config_component->plugin_name->str,
211 bt_config_component->component_name->str);
212 printf(" params:\n");
213 print_value(bt_config_component->params, 6, true);
214 }
215
216 static
217 void print_bt_config_components(GPtrArray *array)
218 {
219 size_t i;
220
221 for (i = 0; i < array->len; i++) {
222 struct bt_config_component *cfg_component =
223 bt_config_get_component(array, i);
224 print_bt_config_component(cfg_component);
225 BT_PUT(cfg_component);
226 }
227 }
228
229 static
230 void print_cfg(struct bt_config *cfg)
231 {
232 printf("debug: %d\n", cfg->debug);
233 printf("verbose: %d\n", cfg->verbose);
234 printf("do list: %d\n", cfg->do_list);
235 printf("force correlate: %d\n", cfg->force_correlate);
236 printf("plugin paths:\n");
237 print_value(cfg->plugin_paths, 2, true);
238 printf("sources:\n");
239 print_bt_config_components(cfg->sources);
240 printf("sinks:\n");
241 print_bt_config_components(cfg->sinks);
242 }
243
244 int main(int argc, char **argv)
245 {
246 int ret;
247 struct bt_component_factory *component_factory = NULL;
248 struct bt_component_class *source_class = NULL;
249 struct bt_component_class *sink_class = NULL;
250 struct bt_component *source = NULL, *sink = NULL;
251 struct bt_value *source_params = NULL, *sink_params = NULL;
252 struct bt_config *cfg;
253 struct bt_notification_iterator *it = NULL;
254 enum bt_component_status sink_status;
255 struct bt_value *first_plugin_path_value = NULL;
256 const char *first_plugin_path;
257 struct bt_config_component *cfg_component;
258
259 cfg = bt_config_from_args(argc, argv, &ret);
260 if (cfg) {
261 print_cfg(cfg);
262 } else {
263 goto end;
264 }
265
266 /* TODO handle more than 1 source and 1 sink. */
267 if (cfg->sources->len != 1 || cfg->sinks->len != 1) {
268 fprintf(stderr, "Unexpected configuration, aborting.../n");
269 ret = -1;
270 goto end;
271 }
272 cfg_component = bt_config_get_component(cfg->sources, 0);
273 source_params = bt_get(cfg_component->params);
274 BT_PUT(cfg_component);
275 cfg_component = bt_config_get_component(cfg->sinks, 0);
276 sink_params = bt_get(cfg_component->params);
277 BT_PUT(cfg_component);
278
279 printf_verbose("Verbose mode active.\n");
280 printf_debug("Debug mode active.\n");
281
282 if (bt_value_array_is_empty(cfg->plugin_paths)) {
283 fprintf(stderr, "No plugin path specified, aborting...\n");
284 ret = -1;
285 goto end;
286 }
287 component_factory = bt_component_factory_create();
288 if (!component_factory) {
289 fprintf(stderr, "Failed to create component factory.\n");
290 ret = -1;
291 goto end;
292 }
293
294 first_plugin_path_value = bt_value_array_get(cfg->plugin_paths, 0);
295 bt_value_string_get(first_plugin_path_value, &first_plugin_path);
296
297 ret = bt_component_factory_load_recursive(component_factory,
298 first_plugin_path);
299 if (ret) {
300 fprintf(stderr, "Failed to load plugins.\n");
301 goto end;
302 }
303
304 print_component_classes_found(component_factory);
305 source_class = bt_component_factory_get_component_class(
306 component_factory, "ctf", BT_COMPONENT_TYPE_SOURCE,
307 "fs");
308 if (!source_class) {
309 fprintf(stderr, "Could not find ctf-fs source component class. Aborting...\n");
310 ret = -1;
311 goto end;
312 }
313
314 sink_class = bt_component_factory_get_component_class(component_factory,
315 NULL, BT_COMPONENT_TYPE_SINK, "text");
316 if (!sink_class) {
317 fprintf(stderr, "Could not find text output component class. Aborting...\n");
318 ret = -1;
319 goto end;
320 }
321
322 source = bt_component_create(source_class, "ctf-fs", source_params);
323 if (!source) {
324 fprintf(stderr, "Failed to instantiate ctf-fs source component. Aborting...\n");
325 ret = -1;
326 goto end;
327 }
328
329 sink = bt_component_create(sink_class, "text", sink_params);
330 if (!sink) {
331 fprintf(stderr, "Failed to instanciate text output component. Aborting...\n");
332 ret = -1;
333 goto end;
334 }
335
336 it = bt_component_source_create_iterator(source);
337 if (!it) {
338 fprintf(stderr, "Failed to instantiate source iterator. Aborting...\n");
339 ret = -1;
340 goto end;
341 }
342
343 sink_status = bt_component_sink_add_iterator(sink, it);
344 if (sink_status != BT_COMPONENT_STATUS_OK) {
345 ret = -1;
346 goto end;
347 }
348
349 while (true) {
350 sink_status = bt_component_sink_consume(sink);
351 switch (sink_status) {
352 case BT_COMPONENT_STATUS_AGAIN:
353 /* Wait for an arbitraty 500 ms. */
354 usleep(500000);
355 break;
356 case BT_COMPONENT_STATUS_OK:
357 break;
358 case BT_COMPONENT_STATUS_END:
359 goto end;
360 default:
361 fprintf(stderr, "Sink component returned an error, aborting...\n");
362 ret = -1;
363 goto end;
364 }
365 }
366 end:
367 BT_PUT(component_factory);
368 BT_PUT(sink_class);
369 BT_PUT(source_class);
370 BT_PUT(source);
371 BT_PUT(sink);
372 BT_PUT(source_params);
373 BT_PUT(sink_params);
374 BT_PUT(it);
375 BT_PUT(cfg);
376 BT_PUT(first_plugin_path_value);
377 return ret ? 1 : 0;
378 }
This page took 0.037228 seconds and 3 git commands to generate.