stream-intersection: use the trace-info query results to insert trimmers
[babeltrace.git] / cli / 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 #define BT_LOG_TAG "CLI"
30 #include "logging.h"
31
32 #include <babeltrace/babeltrace.h>
33 #include <babeltrace/plugin/plugin.h>
34 #include <babeltrace/common-internal.h>
35 #include <babeltrace/graph/component.h>
36 #include <babeltrace/graph/component-source.h>
37 #include <babeltrace/graph/component-sink.h>
38 #include <babeltrace/graph/component-filter.h>
39 #include <babeltrace/graph/component-class.h>
40 #include <babeltrace/graph/port.h>
41 #include <babeltrace/graph/graph.h>
42 #include <babeltrace/graph/connection.h>
43 #include <babeltrace/graph/notification-iterator.h>
44 #include <babeltrace/ref.h>
45 #include <babeltrace/values.h>
46 #include <babeltrace/logging.h>
47 #include <unistd.h>
48 #include <stdlib.h>
49 #include <popt.h>
50 #include <string.h>
51 #include <stdio.h>
52 #include <glib.h>
53 #include <inttypes.h>
54 #include <unistd.h>
55 #include <signal.h>
56 #include "babeltrace-cfg.h"
57 #include "babeltrace-cfg-cli-args.h"
58 #include "babeltrace-cfg-cli-args-default.h"
59
60 #define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH"
61 #define ENV_BABELTRACE_CLI_LOG_LEVEL "BABELTRACE_CLI_LOG_LEVEL"
62 #define NSEC_PER_SEC 1000000000LL
63
64 /*
65 * Known environment variable names for the log levels of the project's
66 * modules.
67 */
68 static const char* log_level_env_var_names[] = {
69 "BABELTRACE_COMMON_LOG_LEVEL",
70 "BABELTRACE_PLUGIN_CTF_BTR_LOG_LEVEL",
71 "BABELTRACE_PLUGIN_CTF_FS_SRC_LOG_LEVEL",
72 "BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_SRC_LOG_LEVEL",
73 "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL",
74 "BABELTRACE_PLUGIN_CTF_NOTIF_ITER_LOG_LEVEL",
75 "BABELTRACE_PLUGIN_LTTNG_UTILS_DEBUG_INFO_FLT_LOG_LEVEL",
76 "BABELTRACE_PLUGIN_UTILS_TRIMMER_FLT_LOG_LEVEL",
77 "BABELTRACE_PYTHON_PLUGIN_PROVIDER_LOG_LEVEL",
78 NULL,
79 };
80
81 /* Application's processing graph (weak) */
82 static struct bt_graph *the_graph;
83 static bool canceled = false;
84
85 GPtrArray *loaded_plugins;
86
87 static
88 void sigint_handler(int signum)
89 {
90 if (signum != SIGINT) {
91 return;
92 }
93
94 if (the_graph) {
95 bt_graph_cancel(the_graph);
96 }
97
98 canceled = true;
99 }
100
101 static
102 void init_static_data(void)
103 {
104 loaded_plugins = g_ptr_array_new_with_free_func(bt_put);
105 }
106
107 static
108 void fini_static_data(void)
109 {
110 g_ptr_array_free(loaded_plugins, TRUE);
111 }
112
113 static
114 struct bt_plugin *find_plugin(const char *name)
115 {
116 int i;
117 struct bt_plugin *plugin = NULL;
118
119 assert(name);
120 BT_LOGD("Finding plugin: name=\"%s\"", name);
121
122 for (i = 0; i < loaded_plugins->len; i++) {
123 plugin = g_ptr_array_index(loaded_plugins, i);
124
125 if (strcmp(name, bt_plugin_get_name(plugin)) == 0) {
126 break;
127 }
128
129 plugin = NULL;
130 }
131
132 if (BT_LOG_ON_DEBUG) {
133 if (plugin) {
134 BT_LOGD("Found plugin: plugin-addr=%p", plugin);
135 } else {
136 BT_LOGD("Cannot find plugin.");
137 }
138 }
139
140 return bt_get(plugin);
141 }
142
143 static
144 struct bt_component_class *find_component_class(const char *plugin_name,
145 const char *comp_class_name,
146 enum bt_component_class_type comp_class_type)
147 {
148 struct bt_component_class *comp_class = NULL;
149 struct bt_plugin *plugin;
150
151 BT_LOGD("Finding component class: plugin-name=\"%s\", "
152 "comp-cls-name=\"%s\", comp-cls-type=%d",
153 plugin_name, comp_class_name, comp_class_type);
154
155 plugin = find_plugin(plugin_name);
156
157 if (!plugin) {
158 goto end;
159 }
160
161 comp_class = bt_plugin_get_component_class_by_name_and_type(plugin,
162 comp_class_name, comp_class_type);
163 BT_PUT(plugin);
164
165 end:
166 if (BT_LOG_ON_DEBUG) {
167 if (comp_class) {
168 BT_LOGD("Found component class: comp-cls-addr=%p",
169 comp_class);
170 } else {
171 BT_LOGD("Cannot find component class.");
172 }
173 }
174
175 return comp_class;
176 }
177
178 static
179 void print_indent(FILE *fp, size_t indent)
180 {
181 size_t i;
182
183 for (i = 0; i < indent; i++) {
184 fprintf(fp, " ");
185 }
186 }
187
188 static
189 const char *component_type_str(enum bt_component_class_type type)
190 {
191 switch (type) {
192 case BT_COMPONENT_CLASS_TYPE_SOURCE:
193 return "source";
194 case BT_COMPONENT_CLASS_TYPE_SINK:
195 return "sink";
196 case BT_COMPONENT_CLASS_TYPE_FILTER:
197 return "filter";
198 case BT_COMPONENT_CLASS_TYPE_UNKNOWN:
199 default:
200 return "(unknown)";
201 }
202 }
203
204 static
205 void print_plugin_comp_cls_opt(FILE *fh, const char *plugin_name,
206 const char *comp_cls_name, enum bt_component_class_type type)
207 {
208 GString *shell_plugin_name = NULL;
209 GString *shell_comp_cls_name = NULL;
210
211 shell_plugin_name = bt_common_shell_quote(plugin_name, false);
212 if (!shell_plugin_name) {
213 goto end;
214 }
215
216 shell_comp_cls_name = bt_common_shell_quote(comp_cls_name, false);
217 if (!shell_comp_cls_name) {
218 goto end;
219 }
220
221 fprintf(fh, "'%s%s%s%s.%s%s%s.%s%s%s'",
222 bt_common_color_bold(),
223 bt_common_color_fg_cyan(),
224 component_type_str(type),
225 bt_common_color_fg_default(),
226 bt_common_color_fg_blue(),
227 shell_plugin_name->str,
228 bt_common_color_fg_default(),
229 bt_common_color_fg_yellow(),
230 shell_comp_cls_name->str,
231 bt_common_color_reset());
232
233 end:
234 if (shell_plugin_name) {
235 g_string_free(shell_plugin_name, TRUE);
236 }
237
238 if (shell_comp_cls_name) {
239 g_string_free(shell_comp_cls_name, TRUE);
240 }
241 }
242
243 static
244 void print_value(FILE *, struct bt_value *, size_t);
245
246 static
247 void print_value_rec(FILE *, struct bt_value *, size_t);
248
249 struct print_map_value_data {
250 size_t indent;
251 FILE *fp;
252 };
253
254 static
255 bt_bool print_map_value(const char *key, struct bt_value *object, void *data)
256 {
257 struct print_map_value_data *print_map_value_data = data;
258
259 print_indent(print_map_value_data->fp, print_map_value_data->indent);
260 fprintf(print_map_value_data->fp, "%s: ", key);
261
262 if (bt_value_is_array(object) &&
263 bt_value_array_is_empty(object)) {
264 fprintf(print_map_value_data->fp, "[ ]\n");
265 return true;
266 }
267
268 if (bt_value_is_map(object) &&
269 bt_value_map_is_empty(object)) {
270 fprintf(print_map_value_data->fp, "{ }\n");
271 return true;
272 }
273
274 if (bt_value_is_array(object) ||
275 bt_value_is_map(object)) {
276 fprintf(print_map_value_data->fp, "\n");
277 }
278
279 print_value_rec(print_map_value_data->fp, object,
280 print_map_value_data->indent + 2);
281 return BT_TRUE;
282 }
283
284 static
285 void print_value_rec(FILE *fp, struct bt_value *value, size_t indent)
286 {
287 bt_bool bool_val;
288 int64_t int_val;
289 double dbl_val;
290 const char *str_val;
291 int size;
292 int i;
293
294 if (!value) {
295 return;
296 }
297
298 switch (bt_value_get_type(value)) {
299 case BT_VALUE_TYPE_NULL:
300 fprintf(fp, "%snull%s\n", bt_common_color_bold(),
301 bt_common_color_reset());
302 break;
303 case BT_VALUE_TYPE_BOOL:
304 bt_value_bool_get(value, &bool_val);
305 fprintf(fp, "%s%s%s%s\n", bt_common_color_bold(),
306 bt_common_color_fg_cyan(), bool_val ? "yes" : "no",
307 bt_common_color_reset());
308 break;
309 case BT_VALUE_TYPE_INTEGER:
310 bt_value_integer_get(value, &int_val);
311 fprintf(fp, "%s%s%" PRId64 "%s\n", bt_common_color_bold(),
312 bt_common_color_fg_red(), int_val,
313 bt_common_color_reset());
314 break;
315 case BT_VALUE_TYPE_FLOAT:
316 bt_value_float_get(value, &dbl_val);
317 fprintf(fp, "%s%s%lf%s\n", bt_common_color_bold(),
318 bt_common_color_fg_red(), dbl_val,
319 bt_common_color_reset());
320 break;
321 case BT_VALUE_TYPE_STRING:
322 bt_value_string_get(value, &str_val);
323 fprintf(fp, "%s%s%s%s\n", bt_common_color_bold(),
324 bt_common_color_fg_green(), str_val,
325 bt_common_color_reset());
326 break;
327 case BT_VALUE_TYPE_ARRAY:
328 size = bt_value_array_size(value);
329 assert(size >= 0);
330
331 if (size == 0) {
332 print_indent(fp, indent);
333 fprintf(fp, "[ ]\n");
334 break;
335 }
336
337 for (i = 0; i < size; i++) {
338 struct bt_value *element =
339 bt_value_array_get(value, i);
340
341 assert(element);
342 print_indent(fp, indent);
343 fprintf(fp, "- ");
344
345 if (bt_value_is_array(element) &&
346 bt_value_array_is_empty(element)) {
347 fprintf(fp, "[ ]\n");
348 continue;
349 }
350
351 if (bt_value_is_map(element) &&
352 bt_value_map_is_empty(element)) {
353 fprintf(fp, "{ }\n");
354 continue;
355 }
356
357 if (bt_value_is_array(element) ||
358 bt_value_is_map(element)) {
359 fprintf(fp, "\n");
360 }
361
362 print_value_rec(fp, element, indent + 2);
363 BT_PUT(element);
364 }
365 break;
366 case BT_VALUE_TYPE_MAP:
367 {
368 struct print_map_value_data data = {
369 .indent = indent,
370 .fp = fp,
371 };
372
373 if (bt_value_map_is_empty(value)) {
374 print_indent(fp, indent);
375 fprintf(fp, "{ }\n");
376 break;
377 }
378
379 bt_value_map_foreach(value, print_map_value, &data);
380 break;
381 }
382 default:
383 abort();
384 }
385 }
386
387 static
388 void print_value(FILE *fp, struct bt_value *value, size_t indent)
389 {
390 if (!bt_value_is_array(value) && !bt_value_is_map(value)) {
391 print_indent(fp, indent);
392 }
393
394 print_value_rec(fp, value, indent);
395 }
396
397 static
398 void print_bt_config_component(struct bt_config_component *bt_config_component)
399 {
400 fprintf(stderr, " ");
401 print_plugin_comp_cls_opt(stderr, bt_config_component->plugin_name->str,
402 bt_config_component->comp_cls_name->str,
403 bt_config_component->type);
404 fprintf(stderr, ":\n");
405
406 if (bt_config_component->instance_name->len > 0) {
407 fprintf(stderr, " Name: %s\n",
408 bt_config_component->instance_name->str);
409 }
410
411 fprintf(stderr, " Parameters:\n");
412 print_value(stderr, bt_config_component->params, 8);
413 }
414
415 static
416 void print_bt_config_components(GPtrArray *array)
417 {
418 size_t i;
419
420 for (i = 0; i < array->len; i++) {
421 struct bt_config_component *cfg_component =
422 bt_config_get_component(array, i);
423 print_bt_config_component(cfg_component);
424 BT_PUT(cfg_component);
425 }
426 }
427
428 static
429 void print_plugin_paths(struct bt_value *plugin_paths)
430 {
431 fprintf(stderr, " Plugin paths:\n");
432 print_value(stderr, plugin_paths, 4);
433 }
434
435 static
436 void print_cfg_run(struct bt_config *cfg)
437 {
438 size_t i;
439
440 print_plugin_paths(cfg->plugin_paths);
441 fprintf(stderr, " Source component instances:\n");
442 print_bt_config_components(cfg->cmd_data.run.sources);
443
444 if (cfg->cmd_data.run.filters->len > 0) {
445 fprintf(stderr, " Filter component instances:\n");
446 print_bt_config_components(cfg->cmd_data.run.filters);
447 }
448
449 fprintf(stderr, " Sink component instances:\n");
450 print_bt_config_components(cfg->cmd_data.run.sinks);
451 fprintf(stderr, " Connections:\n");
452
453 for (i = 0; i < cfg->cmd_data.run.connections->len; i++) {
454 struct bt_config_connection *cfg_connection =
455 g_ptr_array_index(cfg->cmd_data.run.connections,
456 i);
457
458 fprintf(stderr, " %s%s%s -> %s%s%s\n",
459 cfg_connection->upstream_comp_name->str,
460 cfg_connection->upstream_port_glob->len > 0 ? "." : "",
461 cfg_connection->upstream_port_glob->str,
462 cfg_connection->downstream_comp_name->str,
463 cfg_connection->downstream_port_glob->len > 0 ? "." : "",
464 cfg_connection->downstream_port_glob->str);
465 }
466 }
467
468 static
469 void print_cfg_list_plugins(struct bt_config *cfg)
470 {
471 print_plugin_paths(cfg->plugin_paths);
472 }
473
474 static
475 void print_cfg_help(struct bt_config *cfg)
476 {
477 print_plugin_paths(cfg->plugin_paths);
478 }
479
480 static
481 void print_cfg_print_ctf_metadata(struct bt_config *cfg)
482 {
483 print_plugin_paths(cfg->plugin_paths);
484 fprintf(stderr, " Path: %s\n",
485 cfg->cmd_data.print_ctf_metadata.path->str);
486 }
487
488 static
489 void print_cfg_print_lttng_live_sessions(struct bt_config *cfg)
490 {
491 print_plugin_paths(cfg->plugin_paths);
492 fprintf(stderr, " URL: %s\n",
493 cfg->cmd_data.print_lttng_live_sessions.url->str);
494 }
495
496 static
497 void print_cfg_query(struct bt_config *cfg)
498 {
499 print_plugin_paths(cfg->plugin_paths);
500 fprintf(stderr, " Object: `%s`\n", cfg->cmd_data.query.object->str);
501 fprintf(stderr, " Component class:\n");
502 print_bt_config_component(cfg->cmd_data.query.cfg_component);
503 }
504
505 static
506 void print_cfg(struct bt_config *cfg)
507 {
508 if (!BT_LOG_ON_INFO) {
509 return;
510 }
511
512 BT_LOGI_STR("Configuration:");
513 fprintf(stderr, " Debug mode: %s\n", cfg->debug ? "yes" : "no");
514 fprintf(stderr, " Verbose mode: %s\n", cfg->verbose ? "yes" : "no");
515
516 switch (cfg->command) {
517 case BT_CONFIG_COMMAND_RUN:
518 print_cfg_run(cfg);
519 break;
520 case BT_CONFIG_COMMAND_LIST_PLUGINS:
521 print_cfg_list_plugins(cfg);
522 break;
523 case BT_CONFIG_COMMAND_HELP:
524 print_cfg_help(cfg);
525 break;
526 case BT_CONFIG_COMMAND_QUERY:
527 print_cfg_query(cfg);
528 break;
529 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA:
530 print_cfg_print_ctf_metadata(cfg);
531 break;
532 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS:
533 print_cfg_print_lttng_live_sessions(cfg);
534 break;
535 default:
536 abort();
537 }
538 }
539
540 static
541 void add_to_loaded_plugins(struct bt_plugin_set *plugin_set)
542 {
543 int64_t i;
544 int64_t count;
545
546 count = bt_plugin_set_get_plugin_count(plugin_set);
547 assert(count >= 0);
548
549 for (i = 0; i < count; i++) {
550 struct bt_plugin *plugin =
551 bt_plugin_set_get_plugin(plugin_set, i);
552 struct bt_plugin *loaded_plugin =
553 find_plugin(bt_plugin_get_name(plugin));
554
555 assert(plugin);
556
557 if (loaded_plugin) {
558 BT_LOGI("Not using plugin: another one already exists with the same name: "
559 "plugin-name=\"%s\", plugin-path=\"%s\", "
560 "existing-plugin-path=\"%s\"",
561 bt_plugin_get_name(plugin),
562 bt_plugin_get_path(plugin),
563 bt_plugin_get_path(loaded_plugin));
564 bt_put(loaded_plugin);
565 } else {
566 /* Add to global array. */
567 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
568 bt_plugin_get_name(plugin));
569 g_ptr_array_add(loaded_plugins, bt_get(plugin));
570 }
571
572 bt_put(plugin);
573 }
574 }
575
576 static
577 int load_dynamic_plugins(struct bt_value *plugin_paths)
578 {
579 int nr_paths, i, ret = 0;
580
581 nr_paths = bt_value_array_size(plugin_paths);
582 if (nr_paths < 0) {
583 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
584 ret = -1;
585 goto end;
586 }
587
588 BT_LOGI("Loading dynamic plugins.");
589
590 for (i = 0; i < nr_paths; i++) {
591 struct bt_value *plugin_path_value = NULL;
592 const char *plugin_path;
593 struct bt_plugin_set *plugin_set;
594
595 plugin_path_value = bt_value_array_get(plugin_paths, i);
596 bt_value_string_get(plugin_path_value, &plugin_path);
597 assert(plugin_path);
598
599 /*
600 * Skip this if the directory does not exist because
601 * bt_plugin_create_all_from_dir() expects an existing
602 * directory.
603 */
604 if (!g_file_test(plugin_path, G_FILE_TEST_IS_DIR)) {
605 BT_LOGV("Skipping nonexistent directory path: "
606 "path=\"%s\"", plugin_path);
607 BT_PUT(plugin_path_value);
608 continue;
609 }
610
611 plugin_set = bt_plugin_create_all_from_dir(plugin_path, false);
612 if (!plugin_set) {
613 BT_LOGD("Unable to load dynamic plugins: path=\"%s\"",
614 plugin_path);
615 BT_PUT(plugin_path_value);
616 continue;
617 }
618
619 add_to_loaded_plugins(plugin_set);
620 bt_put(plugin_set);
621 BT_PUT(plugin_path_value);
622 }
623 end:
624 return ret;
625 }
626
627 static
628 int load_static_plugins(void)
629 {
630 int ret = 0;
631 struct bt_plugin_set *plugin_set;
632
633 BT_LOGI("Loading static plugins.");
634 plugin_set = bt_plugin_create_all_from_static();
635 if (!plugin_set) {
636 BT_LOGE("Unable to load static plugins.");
637 ret = -1;
638 goto end;
639 }
640
641 add_to_loaded_plugins(plugin_set);
642 bt_put(plugin_set);
643 end:
644 return ret;
645 }
646
647 static
648 int load_all_plugins(struct bt_value *plugin_paths)
649 {
650 int ret = 0;
651
652 if (load_dynamic_plugins(plugin_paths)) {
653 ret = -1;
654 goto end;
655 }
656
657 if (load_static_plugins()) {
658 ret = -1;
659 goto end;
660 }
661
662 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins->len);
663
664 end:
665 return ret;
666 }
667
668 static
669 void print_plugin_info(struct bt_plugin *plugin)
670 {
671 unsigned int major, minor, patch;
672 const char *extra;
673 enum bt_plugin_status version_status;
674 const char *plugin_name;
675 const char *path;
676 const char *author;
677 const char *license;
678 const char *plugin_description;
679
680 plugin_name = bt_plugin_get_name(plugin);
681 path = bt_plugin_get_path(plugin);
682 author = bt_plugin_get_author(plugin);
683 license = bt_plugin_get_license(plugin);
684 plugin_description = bt_plugin_get_description(plugin);
685 version_status = bt_plugin_get_version(plugin, &major, &minor,
686 &patch, &extra);
687 printf("%s%s%s%s:\n", bt_common_color_bold(),
688 bt_common_color_fg_blue(), plugin_name,
689 bt_common_color_reset());
690 printf(" %sPath%s: %s\n", bt_common_color_bold(),
691 bt_common_color_reset(), path ? path : "(None)");
692
693 if (version_status == BT_PLUGIN_STATUS_OK) {
694 printf(" %sVersion%s: %u.%u.%u",
695 bt_common_color_bold(), bt_common_color_reset(),
696 major, minor, patch);
697
698 if (extra) {
699 printf("%s", extra);
700 }
701
702 printf("\n");
703 }
704
705 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
706 bt_common_color_reset(),
707 plugin_description ? plugin_description : "(None)");
708 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
709 bt_common_color_reset(), author ? author : "(Unknown)");
710 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
711 bt_common_color_reset(),
712 license ? license : "(Unknown)");
713 }
714
715 static
716 int cmd_query(struct bt_config *cfg)
717 {
718 int ret = 0;
719 struct bt_component_class *comp_cls = NULL;
720 struct bt_value *results = NULL;
721
722 comp_cls = find_component_class(cfg->cmd_data.query.cfg_component->plugin_name->str,
723 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
724 cfg->cmd_data.query.cfg_component->type);
725 if (!comp_cls) {
726 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
727 "comp-cls-name=\"%s\", comp-cls-type=%d",
728 cfg->cmd_data.query.cfg_component->plugin_name->str,
729 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
730 cfg->cmd_data.query.cfg_component->type);
731 fprintf(stderr, "%s%sCannot find component class %s",
732 bt_common_color_bold(),
733 bt_common_color_fg_red(),
734 bt_common_color_reset());
735 print_plugin_comp_cls_opt(stderr,
736 cfg->cmd_data.query.cfg_component->plugin_name->str,
737 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
738 cfg->cmd_data.query.cfg_component->type);
739 fprintf(stderr, "\n");
740 ret = -1;
741 goto end;
742 }
743
744 results = bt_component_class_query(comp_cls,
745 cfg->cmd_data.query.object->str,
746 cfg->cmd_data.query.cfg_component->params);
747 if (!results) {
748 BT_LOGE("Failed to query component class: plugin-name=\"%s\", "
749 "comp-cls-name=\"%s\", comp-cls-type=%d "
750 "object=\"%s\"",
751 cfg->cmd_data.query.cfg_component->plugin_name->str,
752 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
753 cfg->cmd_data.query.cfg_component->type,
754 cfg->cmd_data.query.object->str);
755 fprintf(stderr, "%s%sFailed to query info to %s",
756 bt_common_color_bold(),
757 bt_common_color_fg_red(),
758 bt_common_color_reset());
759 print_plugin_comp_cls_opt(stderr,
760 cfg->cmd_data.query.cfg_component->plugin_name->str,
761 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
762 cfg->cmd_data.query.cfg_component->type);
763 fprintf(stderr, "%s%s with object `%s`%s\n",
764 bt_common_color_bold(),
765 bt_common_color_fg_red(),
766 cfg->cmd_data.query.object->str,
767 bt_common_color_reset());
768 ret = -1;
769 goto end;
770 }
771
772 print_value(stdout, results, 0);
773
774 end:
775 bt_put(comp_cls);
776 bt_put(results);
777 return ret;
778 }
779
780 static
781 int cmd_help(struct bt_config *cfg)
782 {
783 int ret = 0;
784 struct bt_plugin *plugin = NULL;
785 size_t i;
786
787 plugin = find_plugin(cfg->cmd_data.help.cfg_component->plugin_name->str);
788 if (!plugin) {
789 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
790 cfg->cmd_data.help.cfg_component->plugin_name->str);
791 fprintf(stderr, "%s%sCannot find plugin %s%s%s\n",
792 bt_common_color_bold(), bt_common_color_fg_red(),
793 bt_common_color_fg_blue(),
794 cfg->cmd_data.help.cfg_component->plugin_name->str,
795 bt_common_color_reset());
796 ret = -1;
797 goto end;
798 }
799
800 print_plugin_info(plugin);
801 printf(" %sComponent classes%s: %d\n",
802 bt_common_color_bold(),
803 bt_common_color_reset(),
804 (int) bt_plugin_get_component_class_count(plugin));
805
806
807 if (cfg->cmd_data.help.cfg_component->type !=
808 BT_COMPONENT_CLASS_TYPE_UNKNOWN) {
809 struct bt_component_class *needed_comp_cls =
810 find_component_class(
811 cfg->cmd_data.help.cfg_component->plugin_name->str,
812 cfg->cmd_data.help.cfg_component->comp_cls_name->str,
813 cfg->cmd_data.help.cfg_component->type);
814
815 if (!needed_comp_cls) {
816 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
817 "comp-cls-name=\"%s\", comp-cls-type=%d",
818 cfg->cmd_data.help.cfg_component->plugin_name->str,
819 cfg->cmd_data.help.cfg_component->comp_cls_name->str,
820 cfg->cmd_data.help.cfg_component->type);
821 fprintf(stderr, "\n%s%sCannot find component class %s",
822 bt_common_color_bold(),
823 bt_common_color_fg_red(),
824 bt_common_color_reset());
825 print_plugin_comp_cls_opt(stderr,
826 cfg->cmd_data.help.cfg_component->plugin_name->str,
827 cfg->cmd_data.help.cfg_component->comp_cls_name->str,
828 cfg->cmd_data.help.cfg_component->type);
829 fprintf(stderr, "\n");
830 ret = -1;
831 goto end;
832 }
833
834 bt_put(needed_comp_cls);
835 }
836
837 for (i = 0; i < bt_plugin_get_component_class_count(plugin); i++) {
838 struct bt_component_class *comp_cls =
839 bt_plugin_get_component_class_by_index(plugin, i);
840 const char *comp_class_name =
841 bt_component_class_get_name(comp_cls);
842 const char *comp_class_description =
843 bt_component_class_get_description(comp_cls);
844 const char *comp_class_help =
845 bt_component_class_get_help(comp_cls);
846 enum bt_component_class_type type =
847 bt_component_class_get_type(comp_cls);
848
849 assert(comp_cls);
850
851 if (cfg->cmd_data.help.cfg_component->type !=
852 BT_COMPONENT_CLASS_TYPE_UNKNOWN) {
853 if (strcmp(cfg->cmd_data.help.cfg_component->comp_cls_name->str,
854 comp_class_name) != 0 &&
855 type ==
856 cfg->cmd_data.help.cfg_component->type) {
857 bt_put(comp_cls);
858 continue;
859 }
860 }
861
862 printf("\n");
863 print_plugin_comp_cls_opt(stdout,
864 cfg->cmd_data.help.cfg_component->plugin_name->str,
865 comp_class_name,
866 type);
867 printf("\n");
868 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
869 bt_common_color_reset(),
870 comp_class_description ? comp_class_description : "(None)");
871
872 if (comp_class_help) {
873 printf("\n%s\n", comp_class_help);
874 }
875
876 bt_put(comp_cls);
877 }
878
879 end:
880 bt_put(plugin);
881 return ret;
882 }
883
884 static
885 int cmd_list_plugins(struct bt_config *cfg)
886 {
887 int ret = 0;
888 int plugins_count, component_classes_count = 0, i;
889
890 printf("From the following plugin paths:\n\n");
891 print_value(stdout, cfg->plugin_paths, 2);
892 printf("\n");
893 plugins_count = loaded_plugins->len;
894 if (plugins_count == 0) {
895 printf("No plugins found.\n");
896 goto end;
897 }
898
899 for (i = 0; i < plugins_count; i++) {
900 struct bt_plugin *plugin = g_ptr_array_index(loaded_plugins, i);
901
902 component_classes_count += bt_plugin_get_component_class_count(plugin);
903 }
904
905 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
906 bt_common_color_bold(),
907 component_classes_count,
908 bt_common_color_reset(),
909 bt_common_color_bold(),
910 plugins_count,
911 bt_common_color_reset());
912
913 for (i = 0; i < plugins_count; i++) {
914 int j;
915 struct bt_plugin *plugin = g_ptr_array_index(loaded_plugins, i);
916
917 component_classes_count =
918 bt_plugin_get_component_class_count(plugin);
919 printf("\n");
920 print_plugin_info(plugin);
921
922 if (component_classes_count == 0) {
923 printf(" %sComponent classes%s: (none)\n",
924 bt_common_color_bold(),
925 bt_common_color_reset());
926 } else {
927 printf(" %sComponent classes%s:\n",
928 bt_common_color_bold(),
929 bt_common_color_reset());
930 }
931
932 for (j = 0; j < component_classes_count; j++) {
933 struct bt_component_class *comp_class =
934 bt_plugin_get_component_class_by_index(
935 plugin, j);
936 const char *comp_class_name =
937 bt_component_class_get_name(comp_class);
938 const char *comp_class_description =
939 bt_component_class_get_description(comp_class);
940 enum bt_component_class_type type =
941 bt_component_class_get_type(comp_class);
942
943 printf(" ");
944 print_plugin_comp_cls_opt(stdout,
945 bt_plugin_get_name(plugin), comp_class_name,
946 type);
947
948 if (comp_class_description) {
949 printf(": %s", comp_class_description);
950 }
951
952 printf("\n");
953 bt_put(comp_class);
954 }
955 }
956
957 end:
958 return ret;
959 }
960
961 static
962 int cmd_print_lttng_live_sessions(struct bt_config *cfg)
963 {
964 int ret = 0;
965 struct bt_component_class *comp_cls = NULL;
966 struct bt_value *results = NULL;
967 struct bt_value *params = NULL;
968 struct bt_value *map = NULL;
969 struct bt_value *v = NULL;
970 static const char * const plugin_name = "ctf";
971 static const char * const comp_cls_name = "lttng-live";
972 static const enum bt_component_class_type comp_cls_type =
973 BT_COMPONENT_CLASS_TYPE_SOURCE;
974 int64_t array_size, i;
975
976 assert(cfg->cmd_data.print_lttng_live_sessions.url);
977 comp_cls = find_component_class(plugin_name, comp_cls_name,
978 comp_cls_type);
979 if (!comp_cls) {
980 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
981 "comp-cls-name=\"%s\", comp-cls-type=%d",
982 plugin_name, comp_cls_name,
983 BT_COMPONENT_CLASS_TYPE_SOURCE);
984 fprintf(stderr, "%s%sCannot find component class %s",
985 bt_common_color_bold(),
986 bt_common_color_fg_red(),
987 bt_common_color_reset());
988 print_plugin_comp_cls_opt(stderr, plugin_name,
989 comp_cls_name, comp_cls_type);
990 fprintf(stderr, "\n");
991 goto error;
992 }
993
994 params = bt_value_map_create();
995 if (!params) {
996 goto error;
997 }
998
999 ret = bt_value_map_insert_string(params, "url",
1000 cfg->cmd_data.print_lttng_live_sessions.url->str);
1001 if (ret) {
1002 goto error;
1003 }
1004
1005 results = bt_component_class_query(comp_cls, "sessions",
1006 params);
1007 if (!results) {
1008 BT_LOGE_STR("Failed to query for sessions.");
1009 fprintf(stderr, "%s%sFailed to request sessions%s\n",
1010 bt_common_color_bold(),
1011 bt_common_color_fg_red(),
1012 bt_common_color_reset());
1013 goto error;
1014 }
1015
1016 if (!bt_value_is_array(results)) {
1017 BT_LOGE_STR("Expecting an array for sessions query.");
1018 fprintf(stderr, "%s%sUnexpected type returned by session query%s\n",
1019 bt_common_color_bold(),
1020 bt_common_color_fg_red(),
1021 bt_common_color_reset());
1022 goto error;
1023 }
1024
1025 array_size = bt_value_array_size(results);
1026 for (i = 0; i < array_size; i++) {
1027 const char *url_text;
1028 int64_t timer_us, streams, clients;
1029
1030 map = bt_value_array_get(results, i);
1031 if (!map) {
1032 BT_LOGE_STR("Unexpected empty array entry.");
1033 goto error;
1034 }
1035 if (!bt_value_is_map(map)) {
1036 BT_LOGE_STR("Unexpected entry type.");
1037 goto error;
1038 }
1039
1040 v = bt_value_map_get(map, "url");
1041 if (!v) {
1042 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1043 goto error;
1044 }
1045 ret = bt_value_string_get(v, &url_text);
1046 assert(ret == 0);
1047 printf("%s", url_text);
1048 BT_PUT(v);
1049
1050 v = bt_value_map_get(map, "timer-us");
1051 if (!v) {
1052 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1053 goto error;
1054 }
1055 ret = bt_value_integer_get(v, &timer_us);
1056 assert(ret == 0);
1057 printf(" (timer = %" PRIu64 ", ", timer_us);
1058 BT_PUT(v);
1059
1060 v = bt_value_map_get(map, "stream-count");
1061 if (!v) {
1062 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1063 goto error;
1064 }
1065 ret = bt_value_integer_get(v, &streams);
1066 assert(ret == 0);
1067 printf("%" PRIu64 " stream(s), ", streams);
1068 BT_PUT(v);
1069
1070 v = bt_value_map_get(map, "client-count");
1071 if (!v) {
1072 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1073 goto error;
1074 }
1075 ret = bt_value_integer_get(v, &clients);
1076 assert(ret == 0);
1077 printf("%" PRIu64 " client(s) connected)\n", clients);
1078 BT_PUT(v);
1079
1080 BT_PUT(map);
1081 }
1082 end:
1083 bt_put(v);
1084 bt_put(map);
1085 bt_put(results);
1086 bt_put(params);
1087 bt_put(comp_cls);
1088 return 0;
1089
1090 error:
1091 ret = -1;
1092 goto end;
1093 }
1094
1095 static
1096 int cmd_print_ctf_metadata(struct bt_config *cfg)
1097 {
1098 int ret = 0;
1099 struct bt_component_class *comp_cls = NULL;
1100 struct bt_value *results = NULL;
1101 struct bt_value *params = NULL;
1102 struct bt_value *metadata_text_value = NULL;
1103 const char *metadata_text = NULL;
1104 static const char * const plugin_name = "ctf";
1105 static const char * const comp_cls_name = "fs";
1106 static const enum bt_component_class_type comp_cls_type =
1107 BT_COMPONENT_CLASS_TYPE_SOURCE;
1108
1109 assert(cfg->cmd_data.print_ctf_metadata.path);
1110 comp_cls = find_component_class(plugin_name, comp_cls_name,
1111 comp_cls_type);
1112 if (!comp_cls) {
1113 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1114 "comp-cls-name=\"%s\", comp-cls-type=%d",
1115 plugin_name, comp_cls_name,
1116 BT_COMPONENT_CLASS_TYPE_SOURCE);
1117 fprintf(stderr, "%s%sCannot find component class %s",
1118 bt_common_color_bold(),
1119 bt_common_color_fg_red(),
1120 bt_common_color_reset());
1121 print_plugin_comp_cls_opt(stderr, plugin_name,
1122 comp_cls_name, comp_cls_type);
1123 fprintf(stderr, "\n");
1124 ret = -1;
1125 goto end;
1126 }
1127
1128 params = bt_value_map_create();
1129 if (!params) {
1130 ret = -1;
1131 goto end;
1132 }
1133
1134 ret = bt_value_map_insert_string(params, "path",
1135 cfg->cmd_data.print_ctf_metadata.path->str);
1136 if (ret) {
1137 ret = -1;
1138 goto end;
1139 }
1140
1141 results = bt_component_class_query(comp_cls, "metadata-info",
1142 params);
1143 if (!results) {
1144 ret = -1;
1145 BT_LOGE_STR("Failed to query for metadata info.");
1146 fprintf(stderr, "%s%sFailed to request metadata info%s\n",
1147 bt_common_color_bold(),
1148 bt_common_color_fg_red(),
1149 bt_common_color_reset());
1150 goto end;
1151 }
1152
1153 metadata_text_value = bt_value_map_get(results, "text");
1154 if (!metadata_text_value) {
1155 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
1156 ret = -1;
1157 goto end;
1158 }
1159
1160 ret = bt_value_string_get(metadata_text_value, &metadata_text);
1161 assert(ret == 0);
1162 printf("%s\n", metadata_text);
1163
1164 end:
1165 bt_put(results);
1166 bt_put(params);
1167 bt_put(metadata_text_value);
1168 bt_put(comp_cls);
1169 return 0;
1170 }
1171
1172 struct port_id {
1173 char *instance_name;
1174 char *port_name;
1175 };
1176
1177 struct trace_range {
1178 uint64_t intersection_range_begin_ns;
1179 uint64_t intersection_range_end_ns;
1180 };
1181
1182 static
1183 guint port_id_hash(gconstpointer v)
1184 {
1185 const struct port_id *id = v;
1186
1187 assert(id->instance_name);
1188 assert(id->port_name);
1189
1190 return g_str_hash(id->instance_name) ^ g_str_hash(id->port_name);
1191 }
1192
1193 static
1194 gboolean port_id_equal(gconstpointer v1, gconstpointer v2)
1195 {
1196 const struct port_id *id1 = v1;
1197 const struct port_id *id2 = v2;
1198
1199 return !strcmp(id1->instance_name, id2->instance_name) &&
1200 !strcmp(id1->port_name, id2->port_name);
1201 }
1202
1203 static
1204 void port_id_destroy(gpointer data)
1205 {
1206 struct port_id *id = data;
1207
1208 free(id->instance_name);
1209 free(id->port_name);
1210 free(id);
1211 }
1212
1213 static
1214 void trace_range_destroy(gpointer data)
1215 {
1216 free(data);
1217 }
1218
1219 struct cmd_run_ctx {
1220 /* Owned by this */
1221 GHashTable *components;
1222
1223 /* Owned by this */
1224 struct bt_graph *graph;
1225
1226 /* Weak */
1227 struct bt_config *cfg;
1228
1229 bool connect_ports;
1230
1231 bool stream_intersection_mode;
1232
1233 /*
1234 * Association of struct port_id -> struct trace_range.
1235 */
1236 GHashTable *intersections;
1237 };
1238
1239 /* Returns a timestamp of the form "(-)s.ns" */
1240 static
1241 char *s_from_ns(int64_t ns)
1242 {
1243 int ret;
1244 char *s_ret = NULL;
1245 bool is_negative;
1246 int64_t ts_sec_abs, ts_nsec_abs;
1247 int64_t ts_sec = ns / NSEC_PER_SEC;
1248 int64_t ts_nsec = ns % NSEC_PER_SEC;
1249
1250 if (ts_sec >= 0 && ts_nsec >= 0) {
1251 is_negative = false;
1252 ts_sec_abs = ts_sec;
1253 ts_nsec_abs = ts_nsec;
1254 } else if (ts_sec > 0 && ts_nsec < 0) {
1255 is_negative = false;
1256 ts_sec_abs = ts_sec - 1;
1257 ts_nsec_abs = NSEC_PER_SEC + ts_nsec;
1258 } else if (ts_sec == 0 && ts_nsec < 0) {
1259 is_negative = true;
1260 ts_sec_abs = ts_sec;
1261 ts_nsec_abs = -ts_nsec;
1262 } else if (ts_sec < 0 && ts_nsec > 0) {
1263 is_negative = true;
1264 ts_sec_abs = -(ts_sec + 1);
1265 ts_nsec_abs = NSEC_PER_SEC - ts_nsec;
1266 } else if (ts_sec < 0 && ts_nsec == 0) {
1267 is_negative = true;
1268 ts_sec_abs = -ts_sec;
1269 ts_nsec_abs = ts_nsec;
1270 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1271 is_negative = true;
1272 ts_sec_abs = -ts_sec;
1273 ts_nsec_abs = -ts_nsec;
1274 }
1275
1276 ret = asprintf(&s_ret, "%s%" PRId64 ".%09" PRId64,
1277 is_negative ? "-" : "", ts_sec_abs, ts_nsec_abs);
1278 if (ret < 0) {
1279 s_ret = NULL;
1280 }
1281 return s_ret;
1282 }
1283
1284 static
1285 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1286 struct cmd_run_ctx *ctx, struct bt_component *upstream_comp,
1287 struct bt_port *upstream_port,
1288 struct bt_config_connection *cfg_conn)
1289 {
1290 int ret = 0;
1291 GQuark downstreamp_comp_name_quark;
1292 struct bt_component *downstream_comp;
1293 int64_t downstream_port_count;
1294 uint64_t i;
1295 int64_t (*port_count_fn)(struct bt_component *);
1296 struct bt_port *(*port_by_index_fn)(struct bt_component *, uint64_t);
1297 enum bt_graph_status status = BT_GRAPH_STATUS_ERROR;
1298 bool insert_trimmer = false;
1299 struct bt_value *trimmer_params = NULL;
1300 char *intersection_begin = NULL;
1301 char *intersection_end = NULL;
1302 struct bt_component *trimmer = NULL;
1303 struct bt_component_class *trimmer_class = NULL;
1304 struct bt_port *trimmer_input = NULL;
1305 struct bt_port *trimmer_output = NULL;
1306
1307 if (ctx->intersections &&
1308 bt_component_get_class_type(upstream_comp) ==
1309 BT_COMPONENT_CLASS_TYPE_SOURCE) {
1310 struct trace_range *range;
1311 struct port_id port_id = {
1312 .instance_name = (char *) bt_component_get_name(upstream_comp),
1313 .port_name = (char *) bt_port_get_name(upstream_port)
1314 };
1315
1316 if (!port_id.instance_name || !port_id.port_name) {
1317 goto error;
1318 }
1319
1320 range = (struct trace_range *) g_hash_table_lookup(
1321 ctx->intersections, &port_id);
1322 if (range) {
1323 enum bt_value_status status;
1324
1325 intersection_begin = s_from_ns(
1326 range->intersection_range_begin_ns);
1327 intersection_end = s_from_ns(
1328 range->intersection_range_end_ns);
1329 if (!intersection_begin || !intersection_end) {
1330 BT_LOGE_STR("Cannot create trimmer argument timestamp string.");
1331 goto error;
1332 }
1333
1334 insert_trimmer = true;
1335 trimmer_params = bt_value_map_create();
1336 if (!trimmer_params) {
1337 goto error;
1338 }
1339
1340 status = bt_value_map_insert_string(trimmer_params,
1341 "begin", intersection_begin);
1342 if (status != BT_VALUE_STATUS_OK) {
1343 goto error;
1344 }
1345 status = bt_value_map_insert_string(trimmer_params,
1346 "end", intersection_end);
1347 if (status != BT_VALUE_STATUS_OK) {
1348 goto error;
1349 }
1350 }
1351
1352 trimmer_class = find_component_class("utils", "trimmer",
1353 BT_COMPONENT_CLASS_TYPE_FILTER);
1354 if (!trimmer_class) {
1355 goto error;
1356 }
1357 }
1358
1359 BT_LOGI("Connecting upstream port to the next available downstream port: "
1360 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1361 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1362 upstream_port, bt_port_get_name(upstream_port),
1363 cfg_conn->downstream_comp_name->str,
1364 cfg_conn->arg->str);
1365 downstreamp_comp_name_quark = g_quark_from_string(
1366 cfg_conn->downstream_comp_name->str);
1367 assert(downstreamp_comp_name_quark > 0);
1368 downstream_comp = g_hash_table_lookup(ctx->components,
1369 GUINT_TO_POINTER(downstreamp_comp_name_quark));
1370 if (!downstream_comp) {
1371 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1372 "conn-arg=\"%s\"", cfg_conn->downstream_comp_name->str,
1373 cfg_conn->arg->str);
1374 fprintf(stderr, "Cannot create connection: cannot find downstream component: %s\n",
1375 cfg_conn->arg->str);
1376 goto error;
1377 }
1378
1379 if (bt_component_is_filter(downstream_comp)) {
1380 port_count_fn = bt_component_filter_get_input_port_count;
1381 port_by_index_fn = bt_component_filter_get_input_port_by_index;
1382 } else if (bt_component_is_sink(downstream_comp)) {
1383 port_count_fn = bt_component_sink_get_input_port_count;
1384 port_by_index_fn = bt_component_sink_get_input_port_by_index;
1385 } else {
1386 /*
1387 * Should never happen because the connections are
1388 * validated before we get here.
1389 */
1390 BT_LOGF("Invalid connection: downstream component is a source: "
1391 "conn-arg=\"%s\"", cfg_conn->arg->str);
1392 abort();
1393 }
1394
1395 downstream_port_count = port_count_fn(downstream_comp);
1396 assert(downstream_port_count >= 0);
1397
1398 for (i = 0; i < downstream_port_count; i++) {
1399 struct bt_port *downstream_port =
1400 port_by_index_fn(downstream_comp, i);
1401 const char *upstream_port_name;
1402 const char *downstream_port_name;
1403
1404 assert(downstream_port);
1405
1406 /* Skip port if it's already connected. */
1407 if (bt_port_is_connected(downstream_port)) {
1408 bt_put(downstream_port);
1409 BT_LOGD("Skipping downstream port: already connected: "
1410 "port-addr=%p, port-name=\"%s\"",
1411 downstream_port,
1412 bt_port_get_name(downstream_port));
1413 continue;
1414 }
1415
1416 downstream_port_name = bt_port_get_name(downstream_port);
1417 assert(downstream_port_name);
1418 upstream_port_name = bt_port_get_name(upstream_port);
1419 assert(upstream_port_name);
1420
1421 if (!bt_common_star_glob_match(
1422 cfg_conn->downstream_port_glob->str, -1ULL,
1423 downstream_port_name, -1ULL)) {
1424 bt_put(downstream_port);
1425 continue;
1426 }
1427
1428 if (insert_trimmer) {
1429 /*
1430 * In order to insert the trimmer between the two
1431 * components that were being connected, we create
1432 * a connection configuration entry which describes
1433 * a connection from the trimmer's output to the
1434 * original input that was being connected.
1435 *
1436 * Hence, the creation of the trimmer will cause the
1437 * graph "new port" listener to establish all downstream
1438 * connections as its output port is connected. We will
1439 * then establish the connection between the original
1440 * upstream source and the trimmer.
1441 */
1442 char *trimmer_name = NULL;
1443 enum bt_graph_status graph_status;
1444
1445 ret = asprintf(&trimmer_name, "%s-%s",
1446 "stream-intersection-trimmer",
1447 upstream_port_name);
1448 if (ret < 0) {
1449 goto error;
1450 }
1451 ret = 0;
1452
1453 ctx->connect_ports = false;
1454 graph_status = bt_graph_add_component(ctx->graph,
1455 trimmer_class, trimmer_name, trimmer_params,
1456 &trimmer);
1457 free(trimmer_name);
1458 if (graph_status != BT_GRAPH_STATUS_OK) {
1459 goto error;
1460 }
1461 assert(trimmer);
1462
1463 trimmer_input =
1464 bt_component_filter_get_input_port_by_index(
1465 trimmer, 0);
1466 if (!trimmer_input) {
1467 goto error;
1468 }
1469 trimmer_output =
1470 bt_component_filter_get_output_port_by_index(
1471 trimmer, 0);
1472 if (!trimmer_output) {
1473 goto error;
1474 }
1475
1476 /*
1477 * Replace the current downstream port by the trimmer's
1478 * upstream port.
1479 */
1480 BT_MOVE(downstream_port, trimmer_input);
1481 downstream_port_name = bt_port_get_name(
1482 downstream_port);
1483 if (!downstream_port_name) {
1484 goto error;
1485 }
1486 }
1487
1488 /* We have a winner! */
1489 status = bt_graph_connect_ports(ctx->graph,
1490 upstream_port, downstream_port, NULL);
1491 BT_PUT(downstream_port);
1492 switch (status) {
1493 case BT_GRAPH_STATUS_OK:
1494 break;
1495 case BT_GRAPH_STATUS_CANCELED:
1496 BT_LOGI_STR("Graph was canceled by user.");
1497 status = BT_GRAPH_STATUS_OK;
1498 break;
1499 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION:
1500 BT_LOGE("A component refused a connection to one of its ports: "
1501 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1502 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1503 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1504 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1505 "conn-arg=\"%s\"",
1506 upstream_comp, bt_component_get_name(upstream_comp),
1507 upstream_port, bt_port_get_name(upstream_port),
1508 downstream_comp, cfg_conn->downstream_comp_name->str,
1509 downstream_port, downstream_port_name,
1510 cfg_conn->arg->str);
1511 fprintf(stderr,
1512 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1513 bt_port_get_name(upstream_port),
1514 downstream_port_name,
1515 cfg_conn->arg->str);
1516 break;
1517 default:
1518 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1519 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1520 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1521 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1522 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1523 "conn-arg=\"%s\"",
1524 upstream_comp, bt_component_get_name(upstream_comp),
1525 upstream_port, bt_port_get_name(upstream_port),
1526 downstream_comp, cfg_conn->downstream_comp_name->str,
1527 downstream_port, downstream_port_name,
1528 cfg_conn->arg->str);
1529 fprintf(stderr,
1530 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1531 bt_port_get_name(upstream_port),
1532 downstream_port_name,
1533 cfg_conn->arg->str);
1534 goto error;
1535 }
1536
1537 BT_LOGI("Connected component ports: "
1538 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1539 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1540 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1541 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1542 "conn-arg=\"%s\"",
1543 upstream_comp, bt_component_get_name(upstream_comp),
1544 upstream_port, bt_port_get_name(upstream_port),
1545 downstream_comp, cfg_conn->downstream_comp_name->str,
1546 downstream_port, downstream_port_name,
1547 cfg_conn->arg->str);
1548
1549 if (insert_trimmer) {
1550 /*
1551 * The first connection, from the source to the trimmer,
1552 * has been done. We now connect the trimmer to the
1553 * original downstream port.
1554 */
1555 ret = cmd_run_ctx_connect_upstream_port_to_downstream_component(
1556 ctx, trimmer, trimmer_output, cfg_conn);
1557 if (ret) {
1558 goto error;
1559 }
1560 ctx->connect_ports = true;
1561 }
1562 goto end;
1563 }
1564
1565 if (status != BT_GRAPH_STATUS_OK) {
1566 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1567 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1568 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1569 upstream_port, bt_port_get_name(upstream_port),
1570 cfg_conn->downstream_comp_name->str,
1571 cfg_conn->arg->str);
1572 fprintf(stderr,
1573 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1574 bt_port_get_name(upstream_port), cfg_conn->arg->str);
1575 goto error;
1576 }
1577
1578 goto end;
1579
1580 error:
1581 ret = -1;
1582
1583 end:
1584 free(intersection_begin);
1585 free(intersection_end);
1586 BT_PUT(trimmer_params);
1587 BT_PUT(trimmer_class);
1588 BT_PUT(trimmer);
1589 BT_PUT(trimmer_input);
1590 BT_PUT(trimmer_output);
1591 return ret;
1592 }
1593
1594 static
1595 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx *ctx,
1596 struct bt_port *upstream_port)
1597 {
1598 int ret = 0;
1599 const char *upstream_port_name;
1600 const char *upstream_comp_name;
1601 struct bt_component *upstream_comp = NULL;
1602 size_t i;
1603
1604 assert(ctx);
1605 assert(upstream_port);
1606 upstream_port_name = bt_port_get_name(upstream_port);
1607 assert(upstream_port_name);
1608 upstream_comp = bt_port_get_component(upstream_port);
1609 if (!upstream_comp) {
1610 BT_LOGW("Upstream port to connect is not part of a component: "
1611 "port-addr=%p, port-name=\"%s\"",
1612 upstream_port, upstream_port_name);
1613 ret = -1;
1614 goto end;
1615 }
1616
1617 upstream_comp_name = bt_component_get_name(upstream_comp);
1618 assert(upstream_comp_name);
1619 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1620 "port-addr=%p, port-name=\"%s\"",
1621 upstream_comp, upstream_comp_name,
1622 upstream_port, upstream_port_name);
1623
1624 for (i = 0; i < ctx->cfg->cmd_data.run.connections->len; i++) {
1625 struct bt_config_connection *cfg_conn =
1626 g_ptr_array_index(
1627 ctx->cfg->cmd_data.run.connections, i);
1628
1629 if (strcmp(cfg_conn->upstream_comp_name->str,
1630 upstream_comp_name)) {
1631 continue;
1632 }
1633
1634 if (!bt_common_star_glob_match(
1635 cfg_conn->upstream_port_glob->str,
1636 -1ULL, upstream_port_name, -1ULL)) {
1637 continue;
1638 }
1639
1640 ret = cmd_run_ctx_connect_upstream_port_to_downstream_component(
1641 ctx, upstream_comp, upstream_port, cfg_conn);
1642 if (ret) {
1643 BT_LOGE("Cannot connect upstream port: "
1644 "port-addr=%p, port-name=\"%s\"",
1645 upstream_port,
1646 upstream_port_name);
1647 fprintf(stderr,
1648 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1649 upstream_port_name,
1650 upstream_comp_name,
1651 cfg_conn->arg->str);
1652 goto error;
1653 }
1654 goto end;
1655 }
1656
1657 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1658 "port-addr=%p, port-name=\"%s\"", upstream_port,
1659 upstream_port_name);
1660 fprintf(stderr,
1661 "Cannot create connection: upstream port `%s` does not match any connection\n",
1662 upstream_port_name);
1663
1664 error:
1665 ret = -1;
1666
1667 end:
1668 bt_put(upstream_comp);
1669 return ret;
1670 }
1671
1672 static
1673 void graph_port_added_listener(struct bt_port *port, void *data)
1674 {
1675 struct bt_component *comp = NULL;
1676 struct cmd_run_ctx *ctx = data;
1677
1678 comp = bt_port_get_component(port);
1679 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1680 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1681 comp, comp ? bt_component_get_name(comp) : "",
1682 port, bt_port_get_name(port));
1683
1684 if (!ctx->connect_ports) {
1685 goto end;
1686 }
1687
1688 if (!comp) {
1689 BT_LOGW_STR("Port has no component.");
1690 goto end;
1691 }
1692
1693 if (bt_port_is_connected(port)) {
1694 BT_LOGW_STR("Port is already connected.");
1695 goto end;
1696 }
1697
1698 if (!bt_port_is_output(port)) {
1699 BT_LOGI_STR("Skipping input port.");
1700 goto end;
1701 }
1702
1703 if (cmd_run_ctx_connect_upstream_port(ctx, port)) {
1704 BT_LOGF_STR("Cannot connect upstream port.");
1705 fprintf(stderr, "Added port could not be connected: aborting\n");
1706 abort();
1707 }
1708
1709 end:
1710 bt_put(comp);
1711 return;
1712 }
1713
1714 static
1715 void graph_port_removed_listener(struct bt_component *component,
1716 struct bt_port *port, void *data)
1717 {
1718 BT_LOGI("Port removed from a graph's component: comp-addr=%p, "
1719 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1720 component, bt_component_get_name(component),
1721 port, bt_port_get_name(port));
1722 }
1723
1724 static
1725 void graph_ports_connected_listener(struct bt_port *upstream_port,
1726 struct bt_port *downstream_port, void *data)
1727 {
1728 struct bt_component *upstream_comp = bt_port_get_component(upstream_port);
1729 struct bt_component *downstream_comp = bt_port_get_component(downstream_port);
1730
1731 assert(upstream_comp);
1732 assert(downstream_comp);
1733 BT_LOGI("Graph's component ports connected: "
1734 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1735 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1736 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1737 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1738 upstream_comp, bt_component_get_name(upstream_comp),
1739 upstream_port, bt_port_get_name(upstream_port),
1740 downstream_comp, bt_component_get_name(downstream_comp),
1741 downstream_port, bt_port_get_name(downstream_port));
1742 bt_put(upstream_comp);
1743 bt_put(downstream_comp);
1744 }
1745
1746 static
1747 void graph_ports_disconnected_listener(
1748 struct bt_component *upstream_component,
1749 struct bt_component *downstream_component,
1750 struct bt_port *upstream_port, struct bt_port *downstream_port,
1751 void *data)
1752 {
1753 BT_LOGI("Graph's component ports disconnected: "
1754 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1755 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1756 upstream_port, bt_port_get_name(upstream_port),
1757 downstream_port, bt_port_get_name(downstream_port));
1758 }
1759
1760 static
1761 void cmd_run_ctx_destroy(struct cmd_run_ctx *ctx)
1762 {
1763 if (!ctx) {
1764 return;
1765 }
1766
1767 if (ctx->components) {
1768 g_hash_table_destroy(ctx->components);
1769 ctx->components = NULL;
1770 }
1771
1772 if (ctx->intersections) {
1773 g_hash_table_destroy(ctx->intersections);
1774 ctx->components = NULL;
1775 }
1776
1777 BT_PUT(ctx->graph);
1778 the_graph = NULL;
1779 ctx->cfg = NULL;
1780 }
1781
1782 static
1783 int cmd_run_ctx_init(struct cmd_run_ctx *ctx, struct bt_config *cfg)
1784 {
1785 int ret = 0;
1786
1787 ctx->cfg = cfg;
1788 ctx->connect_ports = false;
1789 ctx->components = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1790 NULL, bt_put);
1791 if (!ctx->components) {
1792 goto error;
1793 }
1794
1795 if (cfg->cmd_data.run.stream_intersection_mode) {
1796 ctx->stream_intersection_mode = true;
1797 ctx->intersections = g_hash_table_new_full(port_id_hash,
1798 port_id_equal, port_id_destroy, trace_range_destroy);
1799 if (!ctx->intersections) {
1800 goto error;
1801 }
1802 }
1803
1804 ctx->graph = bt_graph_create();
1805 if (!ctx->graph) {
1806 goto error;
1807 }
1808
1809 the_graph = ctx->graph;
1810 ret = bt_graph_add_port_added_listener(ctx->graph,
1811 graph_port_added_listener, ctx);
1812 if (ret < 0) {
1813 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
1814 goto error;
1815 }
1816
1817 ret = bt_graph_add_port_removed_listener(ctx->graph,
1818 graph_port_removed_listener, ctx);
1819 if (ret < 0) {
1820 BT_LOGE_STR("Cannot add \"port removed\" listener to graph.");
1821 goto error;
1822 }
1823
1824 ret = bt_graph_add_ports_connected_listener(ctx->graph,
1825 graph_ports_connected_listener, ctx);
1826 if (ret < 0) {
1827 BT_LOGE_STR("Cannot add \"ports connected\" listener to graph.");
1828 goto error;
1829 }
1830
1831 ret = bt_graph_add_ports_disconnected_listener(ctx->graph,
1832 graph_ports_disconnected_listener, ctx);
1833 if (ret < 0) {
1834 BT_LOGE_STR("Cannot add \"ports disconnected\" listener to graph.");
1835 goto error;
1836 }
1837
1838 goto end;
1839
1840 error:
1841 cmd_run_ctx_destroy(ctx);
1842 ret = -1;
1843
1844 end:
1845 return ret;
1846 }
1847
1848 static
1849 int set_stream_intersections(struct cmd_run_ctx *ctx,
1850 struct bt_config_component *cfg_comp,
1851 struct bt_component_class *comp_cls)
1852 {
1853 int ret = 0;
1854 uint64_t trace_idx;
1855 int64_t trace_count;
1856 enum bt_value_status value_status;
1857 const char *path = NULL;
1858 struct bt_value *component_path_value = NULL;
1859 struct bt_value *query_params = NULL;
1860 struct bt_value *query_result = NULL;
1861 struct bt_value *trace_info = NULL;
1862 struct bt_value *intersection_range = NULL;
1863 struct bt_value *intersection_begin = NULL;
1864 struct bt_value *intersection_end = NULL;
1865 struct bt_value *stream_path_value = NULL;
1866 struct bt_value *stream_paths = NULL;
1867 struct bt_value *stream_infos = NULL;
1868 struct bt_value *stream_info = NULL;
1869 struct port_id *port_id = NULL;
1870 struct trace_range *trace_range = NULL;
1871
1872 component_path_value = bt_value_map_get(cfg_comp->params, "path");
1873 if (!bt_value_is_string(component_path_value)) {
1874 BT_LOGD("Cannot get path parameter: component-name=%s",
1875 cfg_comp->instance_name->str);
1876 ret = -1;
1877 goto error;
1878 }
1879
1880 value_status = bt_value_string_get(component_path_value, &path);
1881 if (value_status != BT_VALUE_STATUS_OK) {
1882 BT_LOGD("Cannot get path string value: component-name=%s",
1883 cfg_comp->instance_name->str);
1884 ret = -1;
1885 goto error;
1886 }
1887
1888 query_params = bt_value_map_create();
1889 if (!query_params) {
1890 BT_LOGE_STR("Cannot create query parameters.");
1891 ret = -1;
1892 goto error;
1893 }
1894
1895 value_status = bt_value_map_insert(query_params, "path", component_path_value);
1896 if (value_status != BT_VALUE_STATUS_OK) {
1897 BT_LOGE_STR("Cannot insert path parameter in query paramater map.");
1898 ret = -1;
1899 goto error;
1900 }
1901
1902 query_result = bt_component_class_query(comp_cls, "trace-info",
1903 query_params);
1904 if (!query_result) {
1905 BT_LOGD("Component class \'%s\' does not support the \'trace-info\' query.",
1906 bt_component_class_get_name(comp_cls));
1907 ret = -1;
1908 goto error;
1909 }
1910
1911 if (!bt_value_is_array(query_result)) {
1912 BT_LOGD("Unexpected format of \'trace-info\' query result: "
1913 "component-class-name=%s",
1914 bt_component_class_get_name(comp_cls));
1915 ret = -1;
1916 goto error;
1917 }
1918
1919 trace_count = bt_value_array_size(query_result);
1920 if (trace_count < 0) {
1921 ret = -1;
1922 goto error;
1923 }
1924
1925 for (trace_idx = 0; trace_idx < trace_count; trace_idx++) {
1926 int64_t begin, end;
1927 uint64_t stream_idx;
1928 int64_t stream_count;
1929
1930 trace_info = bt_value_array_get(query_result, trace_idx);
1931 if (!trace_info || !bt_value_is_map(trace_info)) {
1932 ret = -1;
1933 BT_LOGD_STR("Cannot retrieve trace from query result.");
1934 goto error;
1935 }
1936
1937 intersection_range = bt_value_map_get(trace_info,
1938 "intersection-range-ns");
1939 if (!intersection_range) {
1940 ret = -1;
1941 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
1942 goto error;
1943 }
1944
1945 intersection_begin = bt_value_map_get(intersection_range,
1946 "begin");
1947 if (!intersection_begin) {
1948 ret = -1;
1949 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
1950 goto error;
1951 }
1952
1953 intersection_end = bt_value_map_get(intersection_range,
1954 "end");
1955 if (!intersection_end) {
1956 ret = -1;
1957 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
1958 goto error;
1959 }
1960
1961 value_status = bt_value_integer_get(intersection_begin, &begin);
1962 if (value_status != BT_VALUE_STATUS_OK) {
1963 ret = -1;
1964 BT_LOGD_STR("Cannot retrieve value of intersection-range-ns \'begin\' field from query result.");
1965 goto error;
1966 }
1967
1968 value_status = bt_value_integer_get(intersection_end, &end);
1969 if (value_status != BT_VALUE_STATUS_OK) {
1970 ret = -1;
1971 BT_LOGD_STR("Cannot retrieve value of intersection-range-ns \'end\' field from query result.");
1972 goto error;
1973 }
1974
1975 if (begin < 0 || end < 0 || end < begin) {
1976 BT_LOGW("Invalid trace stream intersection values: "
1977 "intersection-range-ns:begin=%" PRId64
1978 ", intersection-range-ns:end=%" PRId64,
1979 begin, end);
1980 ret = -1;
1981 goto error;
1982 }
1983
1984 stream_infos = bt_value_map_get(trace_info, "streams");
1985 if (!stream_infos || !bt_value_is_array(stream_infos)) {
1986 ret = -1;
1987 BT_LOGD_STR("Cannot retrieve stream informations from trace in query result.");
1988 goto error;
1989 }
1990
1991 stream_count = bt_value_array_size(stream_infos);
1992 if (stream_count < 0) {
1993 ret = -1;
1994 goto error;
1995 }
1996
1997 /*
1998 * FIXME
1999 *
2000 * The first path of a stream's "paths" is currently used to
2001 * associate streams/ports to a given trace intersection.
2002 *
2003 * This is a fragile hack as it relies on the port names
2004 * being set to the various streams path.
2005 *
2006 * A stream name should be introduced as part of the trace-info
2007 * query result.
2008 */
2009 for (stream_idx = 0; stream_idx < stream_count; stream_idx++) {
2010 const char *stream_path;
2011 gboolean hash_ret;
2012
2013 port_id = g_new0(struct port_id, 1);
2014 if (!port_id) {
2015 ret = -1;
2016 BT_LOGE_STR("Cannot allocate memory for port_id structure.");
2017 goto error;
2018 }
2019 port_id->instance_name = strdup(cfg_comp->instance_name->str);
2020 if (!port_id->instance_name) {
2021 ret = -1;
2022 BT_LOGE_STR("Cannot allocate memory for port_id component instance name.");
2023 goto error;
2024 }
2025
2026 trace_range = g_new0(struct trace_range, 1);
2027 if (!trace_range) {
2028 ret = -1;
2029 BT_LOGE_STR("Cannot allocate memory for trace_range structure.");
2030 goto error;
2031 }
2032 trace_range->intersection_range_begin_ns = begin;
2033 trace_range->intersection_range_end_ns = end;
2034
2035 stream_info = bt_value_array_get(stream_infos,
2036 stream_idx);
2037 if (!stream_info || !bt_value_is_map(stream_info)) {
2038 ret = -1;
2039 BT_LOGD_STR("Cannot retrieve stream informations from trace in query result.");
2040 goto error;
2041 }
2042
2043 stream_paths = bt_value_map_get(stream_info, "paths");
2044 if (!stream_paths || !bt_value_is_array(stream_paths)) {
2045 ret = -1;
2046 BT_LOGD_STR("Cannot retrieve stream paths from trace in query result.");
2047 goto error;
2048 }
2049
2050 stream_path_value = bt_value_array_get(stream_paths, 0);
2051 if (!stream_path_value ||
2052 !bt_value_is_string(stream_path_value)) {
2053 ret = -1;
2054 BT_LOGD_STR("Cannot retrieve stream path value from trace in query result.");
2055 goto error;
2056 }
2057
2058 value_status = bt_value_string_get(stream_path_value,
2059 &stream_path);
2060 if (value_status != BT_VALUE_STATUS_OK) {
2061 ret = -1;
2062 goto error;
2063 }
2064
2065 port_id->port_name = strdup(stream_path);
2066 if (!port_id->port_name) {
2067 ret = -1;
2068 BT_LOGE_STR("Cannot allocate memory for port_id port_name.");
2069 goto error;
2070 }
2071
2072 BT_LOGD("Inserting stream intersection ");
2073
2074 hash_ret = g_hash_table_insert(ctx->intersections,
2075 port_id, trace_range);
2076 assert(hash_ret);
2077
2078 port_id = NULL;
2079 trace_range = NULL;
2080 BT_PUT(stream_info);
2081 BT_PUT(stream_paths);
2082 BT_PUT(stream_path_value);
2083 }
2084
2085 BT_PUT(trace_info);
2086 BT_PUT(stream_paths);
2087 BT_PUT(stream_path_value);
2088 BT_PUT(intersection_range);
2089 BT_PUT(intersection_begin);
2090 BT_PUT(intersection_end);
2091 BT_PUT(stream_paths);
2092 BT_PUT(stream_path_value);
2093 }
2094
2095 goto end;
2096
2097 error:
2098 fprintf(stderr, "%s%sCannot determine stream intersection of trace at path \'%s\'.%s\n",
2099 bt_common_color_bold(),
2100 bt_common_color_fg_yellow(),
2101 path ? path : "(unknown)",
2102 bt_common_color_reset());
2103 end:
2104 bt_put(component_path_value);
2105 bt_put(query_params);
2106 bt_put(query_result);
2107 bt_put(trace_info);
2108 bt_put(intersection_range);
2109 bt_put(intersection_begin);
2110 bt_put(intersection_end);
2111 bt_put(stream_infos);
2112 bt_put(stream_info);
2113 bt_put(stream_paths);
2114 bt_put(stream_path_value);
2115 g_free(port_id);
2116 g_free(trace_range);
2117 return ret;
2118 }
2119
2120 static
2121 int cmd_run_ctx_create_components_from_config_components(
2122 struct cmd_run_ctx *ctx, GPtrArray *cfg_components)
2123 {
2124 size_t i;
2125 struct bt_component_class *comp_cls = NULL;
2126 struct bt_component *comp = NULL;
2127 int ret = 0;
2128
2129 for (i = 0; i < cfg_components->len; i++) {
2130 struct bt_config_component *cfg_comp =
2131 g_ptr_array_index(cfg_components, i);
2132 GQuark quark;
2133
2134 comp_cls = find_component_class(cfg_comp->plugin_name->str,
2135 cfg_comp->comp_cls_name->str, cfg_comp->type);
2136 if (!comp_cls) {
2137 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
2138 "comp-cls-name=\"%s\", comp-cls-type=%d",
2139 cfg_comp->plugin_name->str,
2140 cfg_comp->comp_cls_name->str,
2141 cfg_comp->type);
2142 fprintf(stderr, "%s%sCannot find component class %s",
2143 bt_common_color_bold(),
2144 bt_common_color_fg_red(),
2145 bt_common_color_reset());
2146 print_plugin_comp_cls_opt(stderr,
2147 cfg_comp->plugin_name->str,
2148 cfg_comp->comp_cls_name->str,
2149 cfg_comp->type);
2150 fprintf(stderr, "\n");
2151 goto error;
2152 }
2153
2154 ret = bt_graph_add_component(ctx->graph, comp_cls,
2155 cfg_comp->instance_name->str, cfg_comp->params, &comp);
2156 if (ret) {
2157 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
2158 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2159 "comp-name=\"%s\"",
2160 cfg_comp->plugin_name->str,
2161 cfg_comp->comp_cls_name->str,
2162 cfg_comp->type, cfg_comp->instance_name->str);
2163 fprintf(stderr, "%s%sCannot create component `%s`%s\n",
2164 bt_common_color_bold(),
2165 bt_common_color_fg_red(),
2166 cfg_comp->instance_name->str,
2167 bt_common_color_reset());
2168 goto error;
2169 }
2170
2171 if (ctx->stream_intersection_mode &&
2172 cfg_comp->type == BT_COMPONENT_CLASS_TYPE_SOURCE) {
2173 ret = set_stream_intersections(ctx, cfg_comp, comp_cls);
2174 if (ret) {
2175 goto error;
2176 }
2177 }
2178
2179 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2180 comp, cfg_comp->instance_name->str);
2181 quark = g_quark_from_string(cfg_comp->instance_name->str);
2182 assert(quark > 0);
2183 g_hash_table_insert(ctx->components,
2184 GUINT_TO_POINTER(quark), comp);
2185 comp = NULL;
2186 BT_PUT(comp_cls);
2187 }
2188
2189 goto end;
2190
2191 error:
2192 ret = -1;
2193
2194 end:
2195 bt_put(comp);
2196 bt_put(comp_cls);
2197 return ret;
2198 }
2199
2200 static
2201 int cmd_run_ctx_create_components(struct cmd_run_ctx *ctx)
2202 {
2203 int ret = 0;
2204
2205 /*
2206 * Make sure that, during this phase, our graph's "port added"
2207 * listener does not connect ports while we are creating the
2208 * components because we have a special, initial phase for
2209 * this.
2210 */
2211 ctx->connect_ports = false;
2212
2213 ret = cmd_run_ctx_create_components_from_config_components(
2214 ctx, ctx->cfg->cmd_data.run.sources);
2215 if (ret) {
2216 ret = -1;
2217 goto end;
2218 }
2219
2220 ret = cmd_run_ctx_create_components_from_config_components(
2221 ctx, ctx->cfg->cmd_data.run.filters);
2222 if (ret) {
2223 ret = -1;
2224 goto end;
2225 }
2226
2227 ret = cmd_run_ctx_create_components_from_config_components(
2228 ctx, ctx->cfg->cmd_data.run.sinks);
2229 if (ret) {
2230 ret = -1;
2231 goto end;
2232 }
2233
2234 end:
2235 return ret;
2236 }
2237
2238 static
2239 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx *ctx,
2240 struct bt_component *comp,
2241 int64_t (*port_count_fn)(struct bt_component *),
2242 struct bt_port *(*port_by_index_fn)(struct bt_component *, uint64_t))
2243 {
2244 int ret = 0;
2245 int64_t count;
2246 uint64_t i;
2247
2248 count = port_count_fn(comp);
2249 assert(count >= 0);
2250
2251 for (i = 0; i < count; i++) {
2252 struct bt_port *upstream_port = port_by_index_fn(comp, i);
2253
2254 assert(upstream_port);
2255 ret = cmd_run_ctx_connect_upstream_port(ctx, upstream_port);
2256 bt_put(upstream_port);
2257 if (ret) {
2258 goto end;
2259 }
2260 }
2261
2262 end:
2263 return ret;
2264 }
2265
2266 static
2267 int cmd_run_ctx_connect_ports(struct cmd_run_ctx *ctx)
2268 {
2269 int ret = 0;
2270 GHashTableIter iter;
2271 gpointer g_name_quark, g_comp;
2272
2273 ctx->connect_ports = true;
2274 g_hash_table_iter_init(&iter, ctx->components);
2275
2276 while (g_hash_table_iter_next(&iter, &g_name_quark, &g_comp)) {
2277 int64_t (*port_count_fn)(struct bt_component *);
2278 struct bt_port *(*port_by_index_fn)(struct bt_component *, uint64_t);
2279
2280 if (bt_component_is_source(g_comp)) {
2281 port_count_fn =
2282 bt_component_source_get_output_port_count;
2283 port_by_index_fn =
2284 bt_component_source_get_output_port_by_index;
2285 } else if (bt_component_is_filter(g_comp)) {
2286 port_count_fn =
2287 bt_component_filter_get_output_port_count;
2288 port_by_index_fn =
2289 bt_component_filter_get_output_port_by_index;
2290 } else {
2291 continue;
2292 }
2293
2294 ret = cmd_run_ctx_connect_comp_ports(ctx,
2295 g_comp, port_count_fn, port_by_index_fn);
2296 if (ret) {
2297 goto end;
2298 }
2299 }
2300
2301 end:
2302 return ret;
2303 }
2304
2305 static inline
2306 const char *bt_graph_status_str(enum bt_graph_status status)
2307 {
2308 switch (status) {
2309 case BT_GRAPH_STATUS_CANCELED:
2310 return "BT_GRAPH_STATUS_CANCELED";
2311 case BT_GRAPH_STATUS_AGAIN:
2312 return "BT_GRAPH_STATUS_AGAIN";
2313 case BT_GRAPH_STATUS_END:
2314 return "BT_GRAPH_STATUS_END";
2315 case BT_GRAPH_STATUS_OK:
2316 return "BT_GRAPH_STATUS_OK";
2317 case BT_GRAPH_STATUS_INVALID:
2318 return "BT_GRAPH_STATUS_INVALID";
2319 case BT_GRAPH_STATUS_NO_SINK:
2320 return "BT_GRAPH_STATUS_NO_SINK";
2321 case BT_GRAPH_STATUS_ERROR:
2322 return "BT_GRAPH_STATUS_ERROR";
2323 default:
2324 return "(unknown)";
2325 }
2326 }
2327
2328 static
2329 int cmd_run(struct bt_config *cfg)
2330 {
2331 int ret = 0;
2332 struct cmd_run_ctx ctx = { 0 };
2333
2334 /* Initialize the command's context and the graph object */
2335 if (cmd_run_ctx_init(&ctx, cfg)) {
2336 BT_LOGE_STR("Cannot initialize the command's context.");
2337 fprintf(stderr, "Cannot initialize the command's context\n");
2338 goto error;
2339 }
2340
2341 if (canceled) {
2342 BT_LOGI_STR("Canceled by user before creating components.");
2343 goto error;
2344 }
2345
2346 BT_LOGI_STR("Creating components.");
2347
2348 /* Create the requested component instances */
2349 if (cmd_run_ctx_create_components(&ctx)) {
2350 BT_LOGE_STR("Cannot create components.");
2351 fprintf(stderr, "Cannot create components\n");
2352 goto error;
2353 }
2354
2355 if (canceled) {
2356 BT_LOGI_STR("Canceled by user before connecting components.");
2357 goto error;
2358 }
2359
2360 BT_LOGI_STR("Connecting components.");
2361
2362 /* Connect the initially visible component ports */
2363 if (cmd_run_ctx_connect_ports(&ctx)) {
2364 BT_LOGE_STR("Cannot connect initial component ports.");
2365 fprintf(stderr, "Cannot connect initial component ports\n");
2366 goto error;
2367 }
2368
2369 if (canceled) {
2370 BT_LOGI_STR("Canceled by user before running the graph.");
2371 goto error;
2372 }
2373
2374 BT_LOGI_STR("Running the graph.");
2375
2376 /* Run the graph */
2377 while (true) {
2378 enum bt_graph_status graph_status = bt_graph_run(ctx.graph);
2379
2380 /*
2381 * Reset console in case something messed with console
2382 * codes during the graph's execution.
2383 */
2384 printf("%s", bt_common_color_reset());
2385 fflush(stdout);
2386 fprintf(stderr, "%s", bt_common_color_reset());
2387 BT_LOGV("bt_graph_run() returned: status=%s",
2388 bt_graph_status_str(graph_status));
2389
2390 switch (graph_status) {
2391 case BT_GRAPH_STATUS_OK:
2392 break;
2393 case BT_GRAPH_STATUS_CANCELED:
2394 BT_LOGI_STR("Graph was canceled by user.");
2395 goto error;
2396 case BT_GRAPH_STATUS_AGAIN:
2397 if (bt_graph_is_canceled(ctx.graph)) {
2398 BT_LOGI_STR("Graph was canceled by user.");
2399 goto error;
2400 }
2401
2402 if (cfg->cmd_data.run.retry_duration_us > 0) {
2403 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
2404 "time-us=%" PRIu64,
2405 cfg->cmd_data.run.retry_duration_us);
2406
2407 if (usleep(cfg->cmd_data.run.retry_duration_us)) {
2408 if (bt_graph_is_canceled(ctx.graph)) {
2409 BT_LOGI_STR("Graph was canceled by user.");
2410 goto error;
2411 }
2412 }
2413 }
2414 break;
2415 case BT_COMPONENT_STATUS_END:
2416 goto end;
2417 default:
2418 BT_LOGE_STR("Graph failed to complete successfully");
2419 fprintf(stderr, "Graph failed to complete successfully\n");
2420 goto error;
2421 }
2422 }
2423
2424 goto end;
2425
2426 error:
2427 if (ret == 0) {
2428 ret = -1;
2429 }
2430
2431 end:
2432 cmd_run_ctx_destroy(&ctx);
2433 return ret;
2434 }
2435
2436 static
2437 void warn_command_name_and_directory_clash(struct bt_config *cfg)
2438 {
2439 const char *env_clash;
2440
2441 if (!cfg->command_name) {
2442 return;
2443 }
2444
2445 env_clash = getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH);
2446 if (env_clash && strcmp(env_clash, "0") == 0) {
2447 return;
2448 }
2449
2450 if (g_file_test(cfg->command_name,
2451 G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
2452 fprintf(stderr, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
2453 cfg->command_name);
2454 fprintf(stderr, "trace located in the local `%s` directory, please use:\n",
2455 cfg->command_name);
2456 fprintf(stderr, "\n");
2457 fprintf(stderr, " babeltrace convert %s [OPTIONS]\n",
2458 cfg->command_name);
2459 }
2460 }
2461
2462 static
2463 void init_log_level(void)
2464 {
2465 bt_cli_log_level = bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL);
2466 }
2467
2468 static
2469 void set_auto_log_levels(struct bt_config *cfg)
2470 {
2471 const char **env_var_name;
2472
2473 /*
2474 * Override the configuration's default log level if
2475 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2476 * are found for backward compatibility with legacy Babetrace 1.
2477 */
2478 if (getenv("BABELTRACE_DEBUG") &&
2479 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2480 cfg->log_level = 'V';
2481 } else if (getenv("BABELTRACE_VERBOSE") &&
2482 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2483 cfg->log_level = 'I';
2484 }
2485
2486 /*
2487 * Set log levels according to --debug or --verbose. For
2488 * backward compatibility, --debug is more verbose than
2489 * --verbose. So:
2490 *
2491 * --verbose: INFO log level
2492 * --debug: VERBOSE log level (includes DEBUG, which is
2493 * is less verbose than VERBOSE in the internal
2494 * logging framework)
2495 */
2496 if (!getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL")) {
2497 if (cfg->verbose) {
2498 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO);
2499 } else if (cfg->debug) {
2500 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE);
2501 } else {
2502 /*
2503 * Set library's default log level if not
2504 * explicitly specified.
2505 */
2506 switch (cfg->log_level) {
2507 case 'N':
2508 bt_logging_set_global_level(BT_LOGGING_LEVEL_NONE);
2509 break;
2510 case 'V':
2511 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE);
2512 break;
2513 case 'D':
2514 bt_logging_set_global_level(BT_LOGGING_LEVEL_DEBUG);
2515 break;
2516 case 'I':
2517 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO);
2518 break;
2519 case 'W':
2520 bt_logging_set_global_level(BT_LOGGING_LEVEL_WARN);
2521 break;
2522 case 'E':
2523 bt_logging_set_global_level(BT_LOGGING_LEVEL_ERROR);
2524 break;
2525 case 'F':
2526 bt_logging_set_global_level(BT_LOGGING_LEVEL_FATAL);
2527 break;
2528 default:
2529 abort();
2530 }
2531 }
2532 }
2533
2534 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL)) {
2535 if (cfg->verbose) {
2536 bt_cli_log_level = BT_LOG_INFO;
2537 } else if (cfg->debug) {
2538 bt_cli_log_level = BT_LOG_VERBOSE;
2539 } else {
2540 /*
2541 * Set CLI's default log level if not explicitly
2542 * specified.
2543 */
2544 switch (cfg->log_level) {
2545 case 'N':
2546 bt_cli_log_level = BT_LOG_NONE;
2547 break;
2548 case 'V':
2549 bt_cli_log_level = BT_LOG_VERBOSE;
2550 break;
2551 case 'D':
2552 bt_cli_log_level = BT_LOG_DEBUG;
2553 break;
2554 case 'I':
2555 bt_cli_log_level = BT_LOG_INFO;
2556 break;
2557 case 'W':
2558 bt_cli_log_level = BT_LOG_WARN;
2559 break;
2560 case 'E':
2561 bt_cli_log_level = BT_LOG_ERROR;
2562 break;
2563 case 'F':
2564 bt_cli_log_level = BT_LOG_FATAL;
2565 break;
2566 default:
2567 abort();
2568 }
2569 }
2570 }
2571
2572 env_var_name = log_level_env_var_names;
2573
2574 while (*env_var_name) {
2575 if (!getenv(*env_var_name)) {
2576 if (cfg->verbose) {
2577 setenv(*env_var_name, "I", 1);
2578 } else if (cfg->debug) {
2579 setenv(*env_var_name, "V", 1);
2580 } else {
2581 char val[2] = { 0 };
2582
2583 /*
2584 * Set module's default log level if not
2585 * explicitly specified.
2586 */
2587 val[0] = cfg->log_level;
2588 setenv(*env_var_name, val, 1);
2589 }
2590 }
2591
2592 env_var_name++;
2593 }
2594 }
2595
2596 static
2597 void set_sigint_handler(void)
2598 {
2599 struct sigaction new_action, old_action;
2600
2601 new_action.sa_handler = sigint_handler;
2602 sigemptyset(&new_action.sa_mask);
2603 new_action.sa_flags = 0;
2604 sigaction(SIGINT, NULL, &old_action);
2605
2606 if (old_action.sa_handler != SIG_IGN) {
2607 sigaction(SIGINT, &new_action, NULL);
2608 }
2609 }
2610
2611 int main(int argc, const char **argv)
2612 {
2613 int ret;
2614 int retcode;
2615 struct bt_config *cfg;
2616
2617 init_log_level();
2618 set_sigint_handler();
2619 init_static_data();
2620 cfg = bt_config_cli_args_create_with_default(argc, argv, &retcode);
2621
2622 if (retcode < 0) {
2623 /* Quit without errors; typically usage/version */
2624 retcode = 0;
2625 BT_LOGI_STR("Quitting without errors.");
2626 goto end;
2627 }
2628
2629 if (retcode > 0) {
2630 BT_LOGE("Command-line error: retcode=%d", retcode);
2631 goto end;
2632 }
2633
2634 if (!cfg) {
2635 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
2636 fprintf(stderr, "Failed to create Babeltrace configuration\n");
2637 retcode = 1;
2638 goto end;
2639 }
2640
2641 set_auto_log_levels(cfg);
2642 print_cfg(cfg);
2643
2644 if (cfg->command_needs_plugins) {
2645 ret = load_all_plugins(cfg->plugin_paths);
2646 if (ret) {
2647 BT_LOGE("Failed to load plugins: ret=%d", ret);
2648 retcode = 1;
2649 goto end;
2650 }
2651 }
2652
2653 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2654 cfg->command, cfg->command_name);
2655
2656 switch (cfg->command) {
2657 case BT_CONFIG_COMMAND_RUN:
2658 ret = cmd_run(cfg);
2659 break;
2660 case BT_CONFIG_COMMAND_LIST_PLUGINS:
2661 ret = cmd_list_plugins(cfg);
2662 break;
2663 case BT_CONFIG_COMMAND_HELP:
2664 ret = cmd_help(cfg);
2665 break;
2666 case BT_CONFIG_COMMAND_QUERY:
2667 ret = cmd_query(cfg);
2668 break;
2669 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA:
2670 ret = cmd_print_ctf_metadata(cfg);
2671 break;
2672 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS:
2673 ret = cmd_print_lttng_live_sessions(cfg);
2674 break;
2675 default:
2676 BT_LOGF("Invalid/unknown command: cmd=%d", cfg->command);
2677 abort();
2678 }
2679
2680 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2681 cfg->command, cfg->command_name, ret);
2682 warn_command_name_and_directory_clash(cfg);
2683 retcode = ret ? 1 : 0;
2684
2685 end:
2686 BT_PUT(cfg);
2687 fini_static_data();
2688 return retcode;
2689 }
This page took 0.086994 seconds and 4 git commands to generate.