ref.h: doc: fix typo
[babeltrace.git] / converter / babeltrace.c
CommitLineData
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>
c42c79ea 41#include "babeltrace-cfg.h"
c1870f57 42#include "default-cfg.h"
34ac0e6c 43
6c2f3ee5
JG
44static struct bt_component_factory *component_factory;
45
7c7c0433
JG
46static
47const char *component_type_str(enum bt_component_type type)
48{
49 switch (type) {
50 case BT_COMPONENT_TYPE_SOURCE:
51 return "source";
52 case BT_COMPONENT_TYPE_SINK:
53 return "sink";
54 case BT_COMPONENT_TYPE_FILTER:
55 return "filter";
56 case BT_COMPONENT_TYPE_UNKNOWN:
57 default:
58 return "unknown";
59 }
60}
61
62static
c42c79ea 63void print_component_classes_found(struct bt_component_factory *factory)
7c7c0433
JG
64{
65 int count, i;
66
67 if (!babeltrace_verbose) {
68 return;
69 }
70
71 count = bt_component_factory_get_component_class_count(factory);
72 if (count <= 0) {
5e86a071 73 fprintf(stderr, "No component classes found. Please make sure your plug-in search path is set correctly.\n");
7c7c0433
JG
74 return;
75 }
76
77 printf_verbose("Found %d component classes.\n", count);
78 for (i = 0; i < count; i++) {
79 struct bt_component_class *component_class =
80 bt_component_factory_get_component_class_index(
81 factory, i);
82 struct bt_plugin *plugin = bt_component_class_get_plugin(
83 component_class);
84 const char *plugin_name = bt_plugin_get_name(plugin);
85 const char *component_name = bt_component_class_get_name(
86 component_class);
87 const char *path = bt_plugin_get_path(plugin);
88 const char *author = bt_plugin_get_author(plugin);
89 const char *license = bt_plugin_get_license(plugin);
90 const char *plugin_description = bt_plugin_get_description(
91 plugin);
92 const char *component_description =
93 bt_component_class_get_description(
94 component_class);
95 enum bt_component_type type = bt_component_class_get_type(
96 component_class);
97
98 printf_verbose("[%s - %s (%s)]\n", plugin_name, component_name,
99 component_type_str(type));
85411a39
JG
100 printf_verbose("\tpath: %s\n", path ? path : "None");
101 printf_verbose("\tauthor: %s\n", author ? author : "Unknown");
102 printf_verbose("\tlicense: %s\n", license ? license : "Unknown");
7c7c0433
JG
103 printf_verbose("\tplugin description: %s\n",
104 plugin_description ? plugin_description : "None");
105 printf_verbose("\tcomponent description: %s\n",
106 component_description ? component_description : "None");
0cc9e945
JG
107
108 bt_put(plugin);
109 bt_put(component_class);
7c7c0433
JG
110 }
111}
112
c42c79ea
PP
113static
114void print_indent(size_t indent)
115{
116 size_t i;
117
118 for (i = 0; i < indent; i++) {
8d24f1c0 119 printf_verbose(" ");
c42c79ea
PP
120 }
121}
122
123static
124void print_value(struct bt_value *, size_t, bool);
125
126static
127bool print_map_value(const char *key, struct bt_value *object, void *data)
128{
129 size_t indent = (size_t) data;
130
131 print_indent(indent);
8d24f1c0 132 printf_verbose("\"%s\": ", key);
c42c79ea
PP
133 print_value(object, indent, false);
134
135 return true;
136}
137
138static
139void print_value(struct bt_value *value, size_t indent, bool do_indent)
140{
141 bool bool_val;
142 int64_t int_val;
143 double dbl_val;
144 const char *str_val;
145 int size;
146 int i;
147
148 if (!value) {
149 return;
150 }
151
152 if (do_indent) {
153 print_indent(indent);
154 }
155
156 switch (bt_value_get_type(value)) {
157 case BT_VALUE_TYPE_NULL:
8d24f1c0 158 printf_verbose("null\n");
c42c79ea
PP
159 break;
160 case BT_VALUE_TYPE_BOOL:
161 bt_value_bool_get(value, &bool_val);
8d24f1c0 162 printf_verbose("%s\n", bool_val ? "true" : "false");
c42c79ea
PP
163 break;
164 case BT_VALUE_TYPE_INTEGER:
165 bt_value_integer_get(value, &int_val);
8d24f1c0 166 printf_verbose("%" PRId64 "\n", int_val);
c42c79ea
PP
167 break;
168 case BT_VALUE_TYPE_FLOAT:
169 bt_value_float_get(value, &dbl_val);
8d24f1c0 170 printf_verbose("%lf\n", dbl_val);
c42c79ea
PP
171 break;
172 case BT_VALUE_TYPE_STRING:
173 bt_value_string_get(value, &str_val);
8d24f1c0 174 printf_verbose("\"%s\"\n", str_val);
c42c79ea
PP
175 break;
176 case BT_VALUE_TYPE_ARRAY:
177 size = bt_value_array_size(value);
8d24f1c0 178 printf_verbose("[\n");
c42c79ea
PP
179
180 for (i = 0; i < size; i++) {
181 struct bt_value *element =
182 bt_value_array_get(value, i);
183
184 print_value(element, indent + 2, true);
185 BT_PUT(element);
186 }
187
188 print_indent(indent);
8d24f1c0 189 printf_verbose("]\n");
c42c79ea
PP
190 break;
191 case BT_VALUE_TYPE_MAP:
192 if (bt_value_map_is_empty(value)) {
8d24f1c0 193 printf_verbose("{}\n");
c42c79ea
PP
194 return;
195 }
196
8d24f1c0 197 printf_verbose("{\n");
c42c79ea
PP
198 bt_value_map_foreach(value, print_map_value,
199 (void *) (indent + 2));
200 print_indent(indent);
8d24f1c0 201 printf_verbose("}\n");
c42c79ea
PP
202 break;
203 default:
204 assert(false);
205 }
206}
207
208static
209void print_bt_config_component(struct bt_config_component *bt_config_component)
210{
8d24f1c0 211 printf_verbose(" %s.%s\n", bt_config_component->plugin_name->str,
c42c79ea 212 bt_config_component->component_name->str);
8d24f1c0 213 printf_verbose(" params:\n");
c42c79ea
PP
214 print_value(bt_config_component->params, 6, true);
215}
216
217static
218void print_bt_config_components(GPtrArray *array)
219{
220 size_t i;
221
222 for (i = 0; i < array->len; i++) {
223 struct bt_config_component *cfg_component =
e5bc7f81 224 bt_config_get_component(array, i);
c42c79ea
PP
225 print_bt_config_component(cfg_component);
226 BT_PUT(cfg_component);
227 }
228}
229
230static
231void print_cfg(struct bt_config *cfg)
232{
8d24f1c0
JG
233 printf_verbose("debug: %d\n", cfg->debug);
234 printf_verbose("verbose: %d\n", cfg->verbose);
235 printf_verbose("do list: %d\n", cfg->do_list);
236 printf_verbose("force correlate: %d\n", cfg->force_correlate);
237 printf_verbose("plugin paths:\n");
c42c79ea 238 print_value(cfg->plugin_paths, 2, true);
8d24f1c0 239 printf_verbose("sources:\n");
c42c79ea 240 print_bt_config_components(cfg->sources);
8d24f1c0 241 printf_verbose("sinks:\n");
c42c79ea
PP
242 print_bt_config_components(cfg->sinks);
243}
244
6c2f3ee5
JG
245static
246struct bt_component *create_trimmer(struct bt_config_component *source_cfg)
247{
248 struct bt_component *trimmer = NULL;
249 struct bt_component_class *trimmer_class = NULL;
250 struct bt_value *trimmer_params = NULL;
528debdf 251 struct bt_value *value;
6c2f3ee5
JG
252
253 assert(component_factory);
254 trimmer_params = bt_value_map_create();
255 if (!trimmer_params) {
256 goto end;
257 }
258
528debdf
MD
259 value = bt_value_map_get(source_cfg->params, "begin");
260 if (value) {
6c2f3ee5 261 enum bt_value_status ret;
6c2f3ee5 262
528debdf 263 ret = bt_value_map_insert(trimmer_params, "begin",
6c2f3ee5
JG
264 value);
265 BT_PUT(value);
266 if (ret) {
267 goto end;
268 }
269 }
528debdf
MD
270 value = bt_value_map_get(source_cfg->params, "end");
271 if (value) {
6c2f3ee5 272 enum bt_value_status ret;
6c2f3ee5 273
528debdf
MD
274 ret = bt_value_map_insert(trimmer_params, "end",
275 value);
276 BT_PUT(value);
277 if (ret) {
6c2f3ee5
JG
278 goto end;
279 }
528debdf
MD
280 }
281 value = bt_value_map_get(source_cfg->params, "clock-gmt");
282 if (value) {
283 enum bt_value_status ret;
6c2f3ee5 284
528debdf 285 ret = bt_value_map_insert(trimmer_params, "clock-gmt",
6c2f3ee5
JG
286 value);
287 BT_PUT(value);
288 if (ret) {
289 goto end;
290 }
291 }
292
293 trimmer_class = bt_component_factory_get_component_class(
294 component_factory, "utils", BT_COMPONENT_TYPE_FILTER,
295 "trimmer");
296 if (!trimmer_class) {
297 fprintf(stderr, "Could not find trimmer component class. Aborting...\n");
298 goto end;
299 }
300 trimmer = bt_component_create(trimmer_class, "source_trimmer",
301 trimmer_params);
302 if (!trimmer) {
303 goto end;
304 }
305end:
306 bt_put(trimmer_params);
307 bt_put(trimmer_class);
308 return trimmer;
309}
310
311static
312int connect_source_sink(struct bt_component *source,
313 struct bt_config_component *source_cfg,
314 struct bt_component *sink)
315{
316 int ret = 0;
317 enum bt_component_status sink_status;
318 struct bt_component *trimmer = NULL;
319 struct bt_notification_iterator *source_it = NULL;
320 struct bt_notification_iterator *to_sink_it = NULL;
321
322 source_it = bt_component_source_create_iterator(source);
323 if (!source_it) {
324 fprintf(stderr, "Failed to instantiate source iterator. Aborting...\n");
325 ret = -1;
326 goto end;
327 }
328
528debdf
MD
329 if (bt_value_map_has_key(source_cfg->params, "begin")
330 || bt_value_map_has_key(source_cfg->params, "end")) {
6c2f3ee5
JG
331 /* A trimmer must be inserted in the graph. */
332 enum bt_component_status trimmer_status;
333
334 trimmer = create_trimmer(source_cfg);
335 if (!trimmer) {
336 fprintf(stderr, "Failed to create trimmer component. Aborting...\n");
337 ret = -1;
338 goto end;
339 }
340
341 trimmer_status = bt_component_filter_add_iterator(trimmer,
342 source_it);
343 BT_PUT(source_it);
344 if (trimmer_status != BT_COMPONENT_STATUS_OK) {
345 fprintf(stderr, "Failed to connect source to trimmer. Aborting...\n");
346 ret = -1;
347 goto end;
348 }
349
350 to_sink_it = bt_component_filter_create_iterator(trimmer);
351 if (!to_sink_it) {
352 fprintf(stderr, "Failed to instantiate trimmer iterator. Aborting...\n");
353 ret = -1;
354 goto end;
355 }
356 } else {
357 BT_MOVE(to_sink_it, source_it);
358 }
359
360 sink_status = bt_component_sink_add_iterator(sink, to_sink_it);
361 if (sink_status != BT_COMPONENT_STATUS_OK) {
362 fprintf(stderr, "Failed to connect to sink component. Aborting...\n");
363 ret = -1;
364 goto end;
365 }
366end:
367 bt_put(trimmer);
368 bt_put(source_it);
369 bt_put(to_sink_it);
370 return ret;
371}
372
98ecef32
MD
373static int load_plugins(struct bt_config *cfg)
374{
375 int nr_paths, i;
376
377 nr_paths = bt_value_array_size(cfg->plugin_paths);
378 if (nr_paths < 0) {
379 return -1;
380 }
381 for (i = 0; i < nr_paths; i++) {
382 struct bt_value *plugin_path_value = NULL;
383 const char *plugin_path;
384
385 plugin_path_value = bt_value_array_get(cfg->plugin_paths, i);
386 if (bt_value_string_get(plugin_path_value,
387 &plugin_path)) {
388 BT_PUT(plugin_path_value);
389 continue;
390 }
391 if (bt_component_factory_load_recursive(component_factory,
392 plugin_path)) {
393 printf_debug("Unable to dynamically load plugins from path %s.\n",
394 plugin_path);
395 }
396 BT_PUT(plugin_path_value);
397 }
398 return 0;
399}
400
528debdf 401int main(int argc, const char **argv)
34ac0e6c 402{
7f26a816 403 int ret;
c42c79ea
PP
404 struct bt_component_class *source_class = NULL;
405 struct bt_component_class *sink_class = NULL;
7c7c0433
JG
406 struct bt_component *source = NULL, *sink = NULL;
407 struct bt_value *source_params = NULL, *sink_params = NULL;
c42c79ea 408 struct bt_config *cfg;
fec2a9f2 409 enum bt_component_status sink_status;
e5bc7f81 410 struct bt_config_component *source_cfg = NULL, *sink_cfg = NULL;
c42c79ea 411
c1870f57
JG
412 cfg = bt_config_create();
413 if (!cfg) {
414 fprintf(stderr, "Failed to create Babeltrace configuration\n");
415 ret = 1;
416 goto end;
417 }
418
419 ret = set_default_config(cfg);
420 if (ret) {
421 goto end;
422 }
423
8d24f1c0 424 ret = bt_config_init_from_args(cfg, argc, argv);
c1870f57 425 if (ret == 0) {
8d24f1c0
JG
426 babeltrace_verbose = cfg->verbose;
427 babeltrace_debug = cfg->debug;
c42c79ea
PP
428 print_cfg(cfg);
429 } else {
56a1cced
JG
430 goto end;
431 }
432
c42c79ea
PP
433 /* TODO handle more than 1 source and 1 sink. */
434 if (cfg->sources->len != 1 || cfg->sinks->len != 1) {
56a1cced
JG
435 ret = -1;
436 goto end;
437 }
438
34ac0e6c
MD
439 printf_verbose("Verbose mode active.\n");
440 printf_debug("Debug mode active.\n");
33bceaf8
JG
441 component_factory = bt_component_factory_create();
442 if (!component_factory) {
443 fprintf(stderr, "Failed to create component factory.\n");
444 ret = -1;
445 goto end;
446 }
447
98ecef32
MD
448 if (load_plugins(cfg)) {
449 fprintf(stderr, "Failed to load plugins.\n");
450 ret = -1;
451 goto end;
33bceaf8
JG
452 }
453
cba174d5
JG
454 ret = bt_component_factory_load_static(component_factory);
455 if (ret) {
456 fprintf(stderr, "Failed to load static plugins.\n");
457 goto end;
458 }
459
c42c79ea 460 print_component_classes_found(component_factory);
e5bc7f81
JG
461
462 source_cfg = bt_config_get_component(cfg->sources, 0);
463 source_params = bt_get(source_cfg->params);
56a1cced 464 source_class = bt_component_factory_get_component_class(
e5bc7f81
JG
465 component_factory, source_cfg->plugin_name->str,
466 BT_COMPONENT_TYPE_SOURCE,
467 source_cfg->component_name->str);
56a1cced 468 if (!source_class) {
e5bc7f81
JG
469 fprintf(stderr, "Could not find %s.%s source component class. Aborting...\n",
470 source_cfg->plugin_name->str,
471 source_cfg->component_name->str);
56a1cced
JG
472 ret = -1;
473 goto end;
474 }
7c7c0433 475
e5bc7f81
JG
476 sink_cfg = bt_config_get_component(cfg->sinks, 0);
477 sink_params = bt_get(sink_cfg->params);
7c7c0433 478 sink_class = bt_component_factory_get_component_class(component_factory,
e5bc7f81
JG
479 sink_cfg->plugin_name->str, BT_COMPONENT_TYPE_SINK,
480 sink_cfg->component_name->str);
7c7c0433 481 if (!sink_class) {
e5bc7f81
JG
482 fprintf(stderr, "Could not find %s.%s output component class. Aborting...\n",
483 sink_cfg->plugin_name->str,
484 sink_cfg->component_name->str);
7c7c0433
JG
485 ret = -1;
486 goto end;
487 }
488
e5bc7f81 489 source = bt_component_create(source_class, "source", source_params);
56a1cced 490 if (!source) {
e5bc7f81 491 fprintf(stderr, "Failed to instantiate selected source component. Aborting...\n");
c42c79ea
PP
492 ret = -1;
493 goto end;
494 }
56a1cced 495
e5bc7f81 496 sink = bt_component_create(sink_class, "sink", sink_params);
7c7c0433 497 if (!sink) {
e5bc7f81 498 fprintf(stderr, "Failed to instantiate selected output component. Aborting...\n");
7c7c0433 499 ret = -1;
2e339de1
JG
500 goto end;
501 }
502
6c2f3ee5
JG
503 ret = connect_source_sink(source, source_cfg, sink);
504 if (ret) {
fec2a9f2
JG
505 goto end;
506 }
78586d8a 507
fec2a9f2
JG
508 while (true) {
509 sink_status = bt_component_sink_consume(sink);
fec2a9f2
JG
510 switch (sink_status) {
511 case BT_COMPONENT_STATUS_AGAIN:
512 /* Wait for an arbitraty 500 ms. */
513 usleep(500000);
78586d8a 514 break;
fec2a9f2
JG
515 case BT_COMPONENT_STATUS_OK:
516 break;
517 case BT_COMPONENT_STATUS_END:
518 goto end;
519 default:
520 fprintf(stderr, "Sink component returned an error, aborting...\n");
521 ret = -1;
522 goto end;
78586d8a 523 }
fec2a9f2 524 }
11e1d048 525end:
2e339de1 526 BT_PUT(component_factory);
7c7c0433
JG
527 BT_PUT(sink_class);
528 BT_PUT(source_class);
529 BT_PUT(source);
530 BT_PUT(sink);
531 BT_PUT(source_params);
532 BT_PUT(sink_params);
c42c79ea 533 BT_PUT(cfg);
e5bc7f81
JG
534 BT_PUT(sink_cfg);
535 BT_PUT(source_cfg);
7f26a816 536 return ret ? 1 : 0;
4c8bfb7e 537}
This page took 0.065152 seconds and 4 git commands to generate.