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