Trimmer fix: don't emit end of trace on out-of-bound event
[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"
34ac0e6c 42
6c2f3ee5
JG
43static struct bt_component_factory *component_factory;
44
7c7c0433
JG
45static
46const char *component_type_str(enum bt_component_type type)
47{
48 switch (type) {
49 case BT_COMPONENT_TYPE_SOURCE:
50 return "source";
51 case BT_COMPONENT_TYPE_SINK:
52 return "sink";
53 case BT_COMPONENT_TYPE_FILTER:
54 return "filter";
55 case BT_COMPONENT_TYPE_UNKNOWN:
56 default:
57 return "unknown";
58 }
59}
60
61static
c42c79ea 62void print_component_classes_found(struct bt_component_factory *factory)
7c7c0433
JG
63{
64 int count, i;
65
66 if (!babeltrace_verbose) {
67 return;
68 }
69
70 count = bt_component_factory_get_component_class_count(factory);
71 if (count <= 0) {
5e86a071 72 fprintf(stderr, "No component classes found. Please make sure your plug-in search path is set correctly.\n");
7c7c0433
JG
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));
85411a39
JG
99 printf_verbose("\tpath: %s\n", path ? path : "None");
100 printf_verbose("\tauthor: %s\n", author ? author : "Unknown");
101 printf_verbose("\tlicense: %s\n", license ? license : "Unknown");
7c7c0433
JG
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");
0cc9e945
JG
106
107 bt_put(plugin);
108 bt_put(component_class);
7c7c0433
JG
109 }
110}
111
c42c79ea
PP
112static
113void print_indent(size_t indent)
114{
115 size_t i;
116
117 for (i = 0; i < indent; i++) {
118 printf(" ");
119 }
120}
121
122static
123void print_value(struct bt_value *, size_t, bool);
124
125static
126bool 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
137static
138void 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
207static
208void print_bt_config_component(struct bt_config_component *bt_config_component)
209{
5f45c11f 210 printf(" %s.%s\n", bt_config_component->plugin_name->str,
c42c79ea
PP
211 bt_config_component->component_name->str);
212 printf(" params:\n");
213 print_value(bt_config_component->params, 6, true);
214}
215
216static
217void 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 =
e5bc7f81 223 bt_config_get_component(array, i);
c42c79ea
PP
224 print_bt_config_component(cfg_component);
225 BT_PUT(cfg_component);
226 }
227}
228
229static
230void 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
6c2f3ee5
JG
244static
245struct bt_component *create_trimmer(struct bt_config_component *source_cfg)
246{
247 struct bt_component *trimmer = NULL;
248 struct bt_component_class *trimmer_class = NULL;
249 struct bt_value *trimmer_params = NULL;
528debdf 250 struct bt_value *value;
6c2f3ee5
JG
251
252 assert(component_factory);
253 trimmer_params = bt_value_map_create();
254 if (!trimmer_params) {
255 goto end;
256 }
257
528debdf
MD
258 value = bt_value_map_get(source_cfg->params, "begin");
259 if (value) {
6c2f3ee5 260 enum bt_value_status ret;
6c2f3ee5 261
528debdf 262 ret = bt_value_map_insert(trimmer_params, "begin",
6c2f3ee5
JG
263 value);
264 BT_PUT(value);
265 if (ret) {
266 goto end;
267 }
268 }
528debdf
MD
269 value = bt_value_map_get(source_cfg->params, "end");
270 if (value) {
6c2f3ee5 271 enum bt_value_status ret;
6c2f3ee5 272
528debdf
MD
273 ret = bt_value_map_insert(trimmer_params, "end",
274 value);
275 BT_PUT(value);
276 if (ret) {
6c2f3ee5
JG
277 goto end;
278 }
528debdf
MD
279 }
280 value = bt_value_map_get(source_cfg->params, "clock-gmt");
281 if (value) {
282 enum bt_value_status ret;
6c2f3ee5 283
528debdf 284 ret = bt_value_map_insert(trimmer_params, "clock-gmt",
6c2f3ee5
JG
285 value);
286 BT_PUT(value);
287 if (ret) {
288 goto end;
289 }
290 }
291
292 trimmer_class = bt_component_factory_get_component_class(
293 component_factory, "utils", BT_COMPONENT_TYPE_FILTER,
294 "trimmer");
295 if (!trimmer_class) {
296 fprintf(stderr, "Could not find trimmer component class. Aborting...\n");
297 goto end;
298 }
299 trimmer = bt_component_create(trimmer_class, "source_trimmer",
300 trimmer_params);
301 if (!trimmer) {
302 goto end;
303 }
304end:
305 bt_put(trimmer_params);
306 bt_put(trimmer_class);
307 return trimmer;
308}
309
310static
311int connect_source_sink(struct bt_component *source,
312 struct bt_config_component *source_cfg,
313 struct bt_component *sink)
314{
315 int ret = 0;
316 enum bt_component_status sink_status;
317 struct bt_component *trimmer = NULL;
318 struct bt_notification_iterator *source_it = NULL;
319 struct bt_notification_iterator *to_sink_it = NULL;
320
321 source_it = bt_component_source_create_iterator(source);
322 if (!source_it) {
323 fprintf(stderr, "Failed to instantiate source iterator. Aborting...\n");
324 ret = -1;
325 goto end;
326 }
327
528debdf
MD
328 if (bt_value_map_has_key(source_cfg->params, "begin")
329 || bt_value_map_has_key(source_cfg->params, "end")) {
6c2f3ee5
JG
330 /* A trimmer must be inserted in the graph. */
331 enum bt_component_status trimmer_status;
332
333 trimmer = create_trimmer(source_cfg);
334 if (!trimmer) {
335 fprintf(stderr, "Failed to create trimmer component. Aborting...\n");
336 ret = -1;
337 goto end;
338 }
339
340 trimmer_status = bt_component_filter_add_iterator(trimmer,
341 source_it);
342 BT_PUT(source_it);
343 if (trimmer_status != BT_COMPONENT_STATUS_OK) {
344 fprintf(stderr, "Failed to connect source to trimmer. Aborting...\n");
345 ret = -1;
346 goto end;
347 }
348
349 to_sink_it = bt_component_filter_create_iterator(trimmer);
350 if (!to_sink_it) {
351 fprintf(stderr, "Failed to instantiate trimmer iterator. Aborting...\n");
352 ret = -1;
353 goto end;
354 }
355 } else {
356 BT_MOVE(to_sink_it, source_it);
357 }
358
359 sink_status = bt_component_sink_add_iterator(sink, to_sink_it);
360 if (sink_status != BT_COMPONENT_STATUS_OK) {
361 fprintf(stderr, "Failed to connect to sink component. Aborting...\n");
362 ret = -1;
363 goto end;
364 }
365end:
366 bt_put(trimmer);
367 bt_put(source_it);
368 bt_put(to_sink_it);
369 return ret;
370}
371
98ecef32
MD
372static int load_plugins(struct bt_config *cfg)
373{
374 int nr_paths, i;
375
376 nr_paths = bt_value_array_size(cfg->plugin_paths);
377 if (nr_paths < 0) {
378 return -1;
379 }
380 for (i = 0; i < nr_paths; i++) {
381 struct bt_value *plugin_path_value = NULL;
382 const char *plugin_path;
383
384 plugin_path_value = bt_value_array_get(cfg->plugin_paths, i);
385 if (bt_value_string_get(plugin_path_value,
386 &plugin_path)) {
387 BT_PUT(plugin_path_value);
388 continue;
389 }
390 if (bt_component_factory_load_recursive(component_factory,
391 plugin_path)) {
392 printf_debug("Unable to dynamically load plugins from path %s.\n",
393 plugin_path);
394 }
395 BT_PUT(plugin_path_value);
396 }
397 return 0;
398}
399
528debdf 400int main(int argc, const char **argv)
34ac0e6c 401{
7f26a816 402 int ret;
c42c79ea
PP
403 struct bt_component_class *source_class = NULL;
404 struct bt_component_class *sink_class = NULL;
7c7c0433
JG
405 struct bt_component *source = NULL, *sink = NULL;
406 struct bt_value *source_params = NULL, *sink_params = NULL;
c42c79ea 407 struct bt_config *cfg;
fec2a9f2 408 enum bt_component_status sink_status;
e5bc7f81 409 struct bt_config_component *source_cfg = NULL, *sink_cfg = NULL;
c42c79ea
PP
410
411 cfg = bt_config_from_args(argc, argv, &ret);
412 if (cfg) {
413 print_cfg(cfg);
414 } else {
56a1cced
JG
415 goto end;
416 }
417
8b596b57
JG
418 babeltrace_verbose = cfg->verbose;
419 babeltrace_debug = cfg->debug;
420
c42c79ea
PP
421 /* TODO handle more than 1 source and 1 sink. */
422 if (cfg->sources->len != 1 || cfg->sinks->len != 1) {
5f45c11f 423 fprintf(stderr, "Unexpected configuration, aborting...\n");
56a1cced
JG
424 ret = -1;
425 goto end;
426 }
427
34ac0e6c
MD
428 printf_verbose("Verbose mode active.\n");
429 printf_debug("Debug mode active.\n");
33bceaf8
JG
430 component_factory = bt_component_factory_create();
431 if (!component_factory) {
432 fprintf(stderr, "Failed to create component factory.\n");
433 ret = -1;
434 goto end;
435 }
436
98ecef32
MD
437 if (load_plugins(cfg)) {
438 fprintf(stderr, "Failed to load plugins.\n");
439 ret = -1;
440 goto end;
33bceaf8
JG
441 }
442
cba174d5
JG
443 ret = bt_component_factory_load_static(component_factory);
444 if (ret) {
445 fprintf(stderr, "Failed to load static plugins.\n");
446 goto end;
447 }
448
c42c79ea 449 print_component_classes_found(component_factory);
e5bc7f81
JG
450
451 source_cfg = bt_config_get_component(cfg->sources, 0);
452 source_params = bt_get(source_cfg->params);
56a1cced 453 source_class = bt_component_factory_get_component_class(
e5bc7f81
JG
454 component_factory, source_cfg->plugin_name->str,
455 BT_COMPONENT_TYPE_SOURCE,
456 source_cfg->component_name->str);
56a1cced 457 if (!source_class) {
e5bc7f81
JG
458 fprintf(stderr, "Could not find %s.%s source component class. Aborting...\n",
459 source_cfg->plugin_name->str,
460 source_cfg->component_name->str);
56a1cced
JG
461 ret = -1;
462 goto end;
463 }
7c7c0433 464
e5bc7f81
JG
465 sink_cfg = bt_config_get_component(cfg->sinks, 0);
466 sink_params = bt_get(sink_cfg->params);
7c7c0433 467 sink_class = bt_component_factory_get_component_class(component_factory,
e5bc7f81
JG
468 sink_cfg->plugin_name->str, BT_COMPONENT_TYPE_SINK,
469 sink_cfg->component_name->str);
7c7c0433 470 if (!sink_class) {
e5bc7f81
JG
471 fprintf(stderr, "Could not find %s.%s output component class. Aborting...\n",
472 sink_cfg->plugin_name->str,
473 sink_cfg->component_name->str);
7c7c0433
JG
474 ret = -1;
475 goto end;
476 }
477
e5bc7f81 478 source = bt_component_create(source_class, "source", source_params);
56a1cced 479 if (!source) {
e5bc7f81 480 fprintf(stderr, "Failed to instantiate selected source component. Aborting...\n");
c42c79ea
PP
481 ret = -1;
482 goto end;
483 }
56a1cced 484
e5bc7f81 485 sink = bt_component_create(sink_class, "sink", sink_params);
7c7c0433 486 if (!sink) {
e5bc7f81 487 fprintf(stderr, "Failed to instantiate selected output component. Aborting...\n");
7c7c0433 488 ret = -1;
2e339de1
JG
489 goto end;
490 }
491
6c2f3ee5
JG
492 ret = connect_source_sink(source, source_cfg, sink);
493 if (ret) {
fec2a9f2
JG
494 goto end;
495 }
78586d8a 496
fec2a9f2
JG
497 while (true) {
498 sink_status = bt_component_sink_consume(sink);
fec2a9f2
JG
499 switch (sink_status) {
500 case BT_COMPONENT_STATUS_AGAIN:
501 /* Wait for an arbitraty 500 ms. */
502 usleep(500000);
78586d8a 503 break;
fec2a9f2
JG
504 case BT_COMPONENT_STATUS_OK:
505 break;
506 case BT_COMPONENT_STATUS_END:
507 goto end;
508 default:
509 fprintf(stderr, "Sink component returned an error, aborting...\n");
510 ret = -1;
511 goto end;
78586d8a 512 }
fec2a9f2 513 }
11e1d048 514end:
2e339de1 515 BT_PUT(component_factory);
7c7c0433
JG
516 BT_PUT(sink_class);
517 BT_PUT(source_class);
518 BT_PUT(source);
519 BT_PUT(sink);
520 BT_PUT(source_params);
521 BT_PUT(sink_params);
c42c79ea 522 BT_PUT(cfg);
e5bc7f81
JG
523 BT_PUT(sink_cfg);
524 BT_PUT(source_cfg);
7f26a816 525 return ret ? 1 : 0;
4c8bfb7e 526}
This page took 0.063466 seconds and 4 git commands to generate.