14954f1db12c7047d3ee4a1cf22a9fec42f85ca1
[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/plugin.h>
31 #include <babeltrace/component/component.h>
32 #include <babeltrace/component/component-source.h>
33 #include <babeltrace/component/component-sink.h>
34 #include <babeltrace/component/component-filter.h>
35 #include <babeltrace/component/component-class.h>
36 #include <babeltrace/component/notification/iterator.h>
37 #include <babeltrace/ref.h>
38 #include <babeltrace/values.h>
39 #include <stdlib.h>
40 #include <babeltrace/ctf-ir/metadata.h> /* for clocks */
41 #include <popt.h>
42 #include <string.h>
43 #include <stdio.h>
44 #include <glib.h>
45 #include "babeltrace-cfg.h"
46 #include "default-cfg.h"
47
48 GPtrArray *loaded_plugins;
49
50 static
51 void init_loaded_plugins_array(void)
52 {
53 loaded_plugins = g_ptr_array_new_full(8, bt_put);
54 }
55
56 static
57 void fini_loaded_plugins_array(void)
58 {
59 g_ptr_array_free(loaded_plugins, TRUE);
60 }
61
62 static
63 struct bt_plugin *find_plugin(const char *name)
64 {
65 int i;
66 struct bt_plugin *plugin = NULL;
67
68 for (i = 0; i < loaded_plugins->len; i++) {
69 plugin = g_ptr_array_index(loaded_plugins, i);
70
71 if (strcmp(name, bt_plugin_get_name(plugin)) == 0) {
72 break;
73 }
74
75 plugin = NULL;
76 }
77
78 return bt_get(plugin);
79 }
80
81 static
82 struct bt_component_class *find_component_class(const char *plugin_name,
83 const char *comp_class_name,
84 enum bt_component_class_type comp_class_type)
85 {
86 struct bt_component_class *comp_class = NULL;
87 struct bt_plugin *plugin = find_plugin(plugin_name);
88
89 if (!plugin) {
90 goto end;
91 }
92
93 comp_class = bt_plugin_get_component_class_by_name_and_type(plugin,
94 comp_class_name, comp_class_type);
95 BT_PUT(plugin);
96 end:
97 return comp_class;
98 }
99
100 static
101 const char *component_type_str(enum bt_component_class_type type)
102 {
103 switch (type) {
104 case BT_COMPONENT_CLASS_TYPE_SOURCE:
105 return "source";
106 case BT_COMPONENT_CLASS_TYPE_SINK:
107 return "sink";
108 case BT_COMPONENT_CLASS_TYPE_FILTER:
109 return "filter";
110 case BT_COMPONENT_CLASS_TYPE_UNKNOWN:
111 default:
112 return "unknown";
113 }
114 }
115
116 static
117 void print_component_classes_found(void)
118 {
119 int plugins_count, component_classes_count = 0, i;
120
121 if (!babeltrace_verbose) {
122 return;
123 }
124
125 plugins_count = loaded_plugins->len;
126 if (plugins_count == 0) {
127 fprintf(stderr, "No plugins found. Please make sure your plug-in search path is set correctly.\n");
128 return;
129 }
130
131 for (i = 0; i < plugins_count; i++) {
132 struct bt_plugin *plugin = g_ptr_array_index(loaded_plugins, i);
133
134 component_classes_count += bt_plugin_get_component_class_count(plugin);
135 }
136
137 printf("Found %d component classes in %d plugins.\n",
138 component_classes_count, plugins_count);
139
140 for (i = 0; i < plugins_count; i++) {
141 int j;
142 struct bt_plugin *plugin = g_ptr_array_index(loaded_plugins, i);
143 unsigned int major, minor, patch;
144 const char *extra;
145 enum bt_plugin_status version_status;
146
147 component_classes_count =
148 bt_plugin_get_component_class_count(plugin);
149 version_status = bt_plugin_get_version(plugin, &major, &minor,
150 &patch, &extra);
151
152 for (j = 0; j < component_classes_count; j++) {
153 struct bt_component_class *comp_class =
154 bt_plugin_get_component_class(plugin, j);
155 const char *plugin_name = bt_plugin_get_name(plugin);
156 const char *comp_class_name =
157 bt_component_class_get_name(comp_class);
158 const char *path = bt_plugin_get_path(plugin);
159 const char *author = bt_plugin_get_author(plugin);
160 const char *license = bt_plugin_get_license(plugin);
161 const char *plugin_description =
162 bt_plugin_get_description(plugin);
163 const char *comp_class_description =
164 bt_component_class_get_description(comp_class);
165 enum bt_component_class_type type =
166 bt_component_class_get_type(comp_class);
167
168 printf("[%s - %s (%s)]\n", plugin_name,
169 comp_class_name, component_type_str(type));
170 printf("\tpath: %s\n", path ? path : "None");
171 printf("\tauthor: %s\n",
172 author ? author : "Unknown");
173 printf("\tlicense: %s\n",
174 license ? license : "Unknown");
175 printf("\tplugin description: %s\n",
176 plugin_description ? plugin_description : "None");
177
178 if (version_status == BT_PLUGIN_STATUS_OK) {
179 printf("\tplugin version: %u.%u.%u",
180 major, minor, patch);
181
182 if (extra) {
183 printf("%s", extra);
184 }
185
186 printf("\n");
187 }
188
189 printf("\tcomponent description: %s\n",
190 comp_class_description ? comp_class_description : "None");
191 bt_put(comp_class);
192 }
193 }
194 }
195
196 static
197 void print_indent(size_t indent)
198 {
199 size_t i;
200
201 for (i = 0; i < indent; i++) {
202 printf(" ");
203 }
204 }
205
206 static
207 void print_value(struct bt_value *, size_t, bool);
208
209 static
210 bool print_map_value(const char *key, struct bt_value *object, void *data)
211 {
212 size_t indent = (size_t) data;
213
214 print_indent(indent);
215 printf("\"%s\": ", key);
216 print_value(object, indent, false);
217
218 return true;
219 }
220
221 static
222 void print_value(struct bt_value *value, size_t indent, bool do_indent)
223 {
224 bool bool_val;
225 int64_t int_val;
226 double dbl_val;
227 const char *str_val;
228 int size;
229 int i;
230
231 if (!value) {
232 return;
233 }
234
235 if (do_indent) {
236 print_indent(indent);
237 }
238
239 switch (bt_value_get_type(value)) {
240 case BT_VALUE_TYPE_NULL:
241 printf("null\n");
242 break;
243 case BT_VALUE_TYPE_BOOL:
244 bt_value_bool_get(value, &bool_val);
245 printf("%s\n", bool_val ? "true" : "false");
246 break;
247 case BT_VALUE_TYPE_INTEGER:
248 bt_value_integer_get(value, &int_val);
249 printf("%" PRId64 "\n", int_val);
250 break;
251 case BT_VALUE_TYPE_FLOAT:
252 bt_value_float_get(value, &dbl_val);
253 printf("%lf\n", dbl_val);
254 break;
255 case BT_VALUE_TYPE_STRING:
256 bt_value_string_get(value, &str_val);
257 printf("\"%s\"\n", str_val);
258 break;
259 case BT_VALUE_TYPE_ARRAY:
260 size = bt_value_array_size(value);
261 printf("[\n");
262
263 for (i = 0; i < size; i++) {
264 struct bt_value *element =
265 bt_value_array_get(value, i);
266
267 print_value(element, indent + 2, true);
268 BT_PUT(element);
269 }
270
271 print_indent(indent);
272 printf("]\n");
273 break;
274 case BT_VALUE_TYPE_MAP:
275 if (bt_value_map_is_empty(value)) {
276 printf("{}\n");
277 return;
278 }
279
280 printf("{\n");
281 bt_value_map_foreach(value, print_map_value,
282 (void *) (indent + 2));
283 print_indent(indent);
284 printf("}\n");
285 break;
286 default:
287 assert(false);
288 }
289 }
290
291 static
292 void print_bt_config_component(struct bt_config_component *bt_config_component)
293 {
294 printf(" %s.%s\n", bt_config_component->plugin_name->str,
295 bt_config_component->component_name->str);
296 printf(" params:\n");
297 print_value(bt_config_component->params, 6, true);
298 }
299
300 static
301 void print_bt_config_components(GPtrArray *array)
302 {
303 size_t i;
304
305 for (i = 0; i < array->len; i++) {
306 struct bt_config_component *cfg_component =
307 bt_config_get_component(array, i);
308 print_bt_config_component(cfg_component);
309 BT_PUT(cfg_component);
310 }
311 }
312
313 static
314 void print_cfg(struct bt_config *cfg)
315 {
316 if (!babeltrace_verbose) {
317 return;
318 }
319
320 printf("debug: %d\n", cfg->debug);
321 printf("verbose: %d\n", cfg->verbose);
322 printf("do list: %d\n", cfg->do_list);
323 printf("force correlate: %d\n", cfg->force_correlate);
324 printf("plugin paths:\n");
325 print_value(cfg->plugin_paths, 2, true);
326 printf("sources:\n");
327 print_bt_config_components(cfg->sources);
328 printf("sinks:\n");
329 print_bt_config_components(cfg->sinks);
330 }
331
332 static
333 struct bt_component *create_trimmer(struct bt_config_component *source_cfg)
334 {
335 struct bt_component *trimmer = NULL;
336 struct bt_component_class *trimmer_class = NULL;
337 struct bt_value *trimmer_params = NULL;
338 struct bt_value *value;
339
340 trimmer_params = bt_value_map_create();
341 if (!trimmer_params) {
342 goto end;
343 }
344
345 value = bt_value_map_get(source_cfg->params, "begin");
346 if (value) {
347 enum bt_value_status ret;
348
349 ret = bt_value_map_insert(trimmer_params, "begin",
350 value);
351 BT_PUT(value);
352 if (ret) {
353 goto end;
354 }
355 }
356 value = bt_value_map_get(source_cfg->params, "end");
357 if (value) {
358 enum bt_value_status ret;
359
360 ret = bt_value_map_insert(trimmer_params, "end",
361 value);
362 BT_PUT(value);
363 if (ret) {
364 goto end;
365 }
366 }
367 value = bt_value_map_get(source_cfg->params, "clock-gmt");
368 if (value) {
369 enum bt_value_status ret;
370
371 ret = bt_value_map_insert(trimmer_params, "clock-gmt",
372 value);
373 BT_PUT(value);
374 if (ret) {
375 goto end;
376 }
377 }
378
379 trimmer_class = find_component_class("utils", "trimmer",
380 BT_COMPONENT_CLASS_TYPE_FILTER);
381 if (!trimmer_class) {
382 fprintf(stderr, "Could not find trimmer component class. Aborting...\n");
383 goto end;
384 }
385 trimmer = bt_component_create(trimmer_class, "source_trimmer",
386 trimmer_params);
387 if (!trimmer) {
388 goto end;
389 }
390 end:
391 bt_put(trimmer_params);
392 bt_put(trimmer_class);
393 return trimmer;
394 }
395
396 static
397 int connect_source_sink(struct bt_component *source,
398 struct bt_config_component *source_cfg,
399 struct bt_component *sink)
400 {
401 int ret = 0;
402 enum bt_component_status sink_status;
403 struct bt_component *trimmer = NULL;
404 struct bt_notification_iterator *source_it = NULL;
405 struct bt_notification_iterator *to_sink_it = NULL;
406
407 source_it = bt_component_source_create_notification_iterator(source);
408 if (!source_it) {
409 fprintf(stderr, "Failed to instantiate source iterator. Aborting...\n");
410 ret = -1;
411 goto end;
412 }
413
414 if (bt_value_map_has_key(source_cfg->params, "begin")
415 || bt_value_map_has_key(source_cfg->params, "end")) {
416 /* A trimmer must be inserted in the graph. */
417 enum bt_component_status trimmer_status;
418
419 trimmer = create_trimmer(source_cfg);
420 if (!trimmer) {
421 fprintf(stderr, "Failed to create trimmer component. Aborting...\n");
422 ret = -1;
423 goto end;
424 }
425
426 trimmer_status = bt_component_filter_add_iterator(trimmer,
427 source_it);
428 BT_PUT(source_it);
429 if (trimmer_status != BT_COMPONENT_STATUS_OK) {
430 fprintf(stderr, "Failed to connect source to trimmer. Aborting...\n");
431 ret = -1;
432 goto end;
433 }
434
435 to_sink_it = bt_component_filter_create_notification_iterator(trimmer);
436 if (!to_sink_it) {
437 fprintf(stderr, "Failed to instantiate trimmer iterator. Aborting...\n");
438 ret = -1;
439 goto end;
440 }
441 } else {
442 BT_MOVE(to_sink_it, source_it);
443 }
444
445 sink_status = bt_component_sink_add_iterator(sink, to_sink_it);
446 if (sink_status != BT_COMPONENT_STATUS_OK) {
447 fprintf(stderr, "Failed to connect to sink component. Aborting...\n");
448 ret = -1;
449 goto end;
450 }
451 end:
452 bt_put(trimmer);
453 bt_put(source_it);
454 bt_put(to_sink_it);
455 return ret;
456 }
457
458 static
459 void add_to_loaded_plugins(struct bt_plugin **plugins)
460 {
461 while (*plugins) {
462 struct bt_plugin *plugin = *plugins;
463 /* Check if it's already loaded (from another path). */
464 struct bt_plugin *loaded_plugin =
465 find_plugin(bt_plugin_get_name(plugin));
466
467 if (loaded_plugin) {
468 printf_verbose("Not loading plugin `%s`: already loaded from `%s`\n",
469 bt_plugin_get_path(plugin),
470 bt_plugin_get_path(loaded_plugin));
471 BT_PUT(loaded_plugin);
472 BT_PUT(plugin);
473 } else {
474 /* Transfer ownership to global array. */
475 g_ptr_array_add(loaded_plugins, plugin);
476 }
477 *(plugins++) = NULL;
478 }
479 }
480
481 static
482 int load_dynamic_plugins(struct bt_config *cfg)
483 {
484 int nr_paths, i, ret = 0;
485
486 nr_paths = bt_value_array_size(cfg->plugin_paths);
487 if (nr_paths < 0) {
488 ret = -1;
489 goto end;
490 }
491
492 for (i = 0; i < nr_paths; i++) {
493 struct bt_value *plugin_path_value = NULL;
494 const char *plugin_path;
495 struct bt_plugin **plugins;
496
497 plugin_path_value = bt_value_array_get(cfg->plugin_paths, i);
498 if (bt_value_string_get(plugin_path_value,
499 &plugin_path)) {
500 BT_PUT(plugin_path_value);
501 continue;
502 }
503
504 plugins = bt_plugin_create_all_from_dir(plugin_path, false);
505 if (!plugins) {
506 printf_debug("Unable to dynamically load plugins from path %s.\n",
507 plugin_path);
508 BT_PUT(plugin_path_value);
509 continue;
510 }
511
512 add_to_loaded_plugins(plugins);
513 free(plugins);
514
515 BT_PUT(plugin_path_value);
516 }
517 end:
518 return ret;
519 }
520
521 static
522 int load_static_plugins(void)
523 {
524 int ret = 0;
525 struct bt_plugin **plugins;
526
527 plugins = bt_plugin_create_all_from_static();
528 if (!plugins) {
529 printf_debug("Unable to load static plugins.\n");
530 ret = -1;
531 goto end;
532 }
533
534 add_to_loaded_plugins(plugins);
535 free(plugins);
536 end:
537 return ret;
538 }
539
540 int main(int argc, const char **argv)
541 {
542 int ret;
543 struct bt_component_class *source_class = NULL;
544 struct bt_component_class *sink_class = NULL;
545 struct bt_component *source = NULL, *sink = NULL;
546 struct bt_value *source_params = NULL, *sink_params = NULL;
547 struct bt_config *cfg;
548 enum bt_component_status sink_status;
549 struct bt_config_component *source_cfg = NULL, *sink_cfg = NULL;
550
551 init_loaded_plugins_array();
552
553 cfg = bt_config_create();
554 if (!cfg) {
555 fprintf(stderr, "Failed to create Babeltrace configuration\n");
556 ret = 1;
557 goto end;
558 }
559
560 ret = set_default_config(cfg);
561 if (ret) {
562 goto end;
563 }
564
565 ret = bt_config_init_from_args(cfg, argc, argv);
566 if (ret == 0) {
567 babeltrace_verbose = cfg->verbose;
568 babeltrace_debug = cfg->debug;
569 print_cfg(cfg);
570 } else {
571 goto end;
572 }
573
574 /* TODO handle more than 1 source and 1 sink. */
575 if (cfg->sources->len != 1 || cfg->sinks->len != 1) {
576 ret = -1;
577 goto end;
578 }
579
580 printf_verbose("Verbose mode active.\n");
581 printf_debug("Debug mode active.\n");
582
583 if (load_dynamic_plugins(cfg)) {
584 fprintf(stderr, "Failed to load dynamic plugins.\n");
585 ret = -1;
586 goto end;
587 }
588
589 if (load_static_plugins()) {
590 fprintf(stderr, "Failed to load static plugins.\n");
591 goto end;
592 }
593
594 print_component_classes_found();
595 source_cfg = bt_config_get_component(cfg->sources, 0);
596 source_params = bt_get(source_cfg->params);
597 source_class = find_component_class(source_cfg->plugin_name->str,
598 source_cfg->component_name->str,
599 BT_COMPONENT_CLASS_TYPE_SOURCE);
600 if (!source_class) {
601 fprintf(stderr, "Could not find %s.%s source component class. Aborting...\n",
602 source_cfg->plugin_name->str,
603 source_cfg->component_name->str);
604 ret = -1;
605 goto end;
606 }
607
608 sink_cfg = bt_config_get_component(cfg->sinks, 0);
609 sink_params = bt_get(sink_cfg->params);
610 sink_class = find_component_class(sink_cfg->plugin_name->str,
611 sink_cfg->component_name->str,
612 BT_COMPONENT_CLASS_TYPE_SINK);
613 if (!sink_class) {
614 fprintf(stderr, "Could not find %s.%s output component class. Aborting...\n",
615 sink_cfg->plugin_name->str,
616 sink_cfg->component_name->str);
617 ret = -1;
618 goto end;
619 }
620
621 source = bt_component_create(source_class, "source", source_params);
622 if (!source) {
623 fprintf(stderr, "Failed to instantiate selected source component. Aborting...\n");
624 ret = -1;
625 goto end;
626 }
627
628 sink = bt_component_create(sink_class, "sink", sink_params);
629 if (!sink) {
630 fprintf(stderr, "Failed to instantiate selected output component. Aborting...\n");
631 ret = -1;
632 goto end;
633 }
634
635 ret = connect_source_sink(source, source_cfg, sink);
636 if (ret) {
637 goto end;
638 }
639
640 while (true) {
641 sink_status = bt_component_sink_consume(sink);
642 switch (sink_status) {
643 case BT_COMPONENT_STATUS_AGAIN:
644 /* Wait for an arbitraty 500 ms. */
645 usleep(500000);
646 break;
647 case BT_COMPONENT_STATUS_OK:
648 break;
649 case BT_COMPONENT_STATUS_END:
650 goto end;
651 default:
652 fprintf(stderr, "Sink component returned an error, aborting...\n");
653 ret = -1;
654 goto end;
655 }
656 }
657 end:
658 BT_PUT(sink_class);
659 BT_PUT(source_class);
660 BT_PUT(source);
661 BT_PUT(sink);
662 BT_PUT(source_params);
663 BT_PUT(sink_params);
664 BT_PUT(cfg);
665 BT_PUT(sink_cfg);
666 BT_PUT(source_cfg);
667 fini_loaded_plugins_array();
668 return ret ? 1 : 0;
669 }
This page took 0.042009 seconds and 4 git commands to generate.