Implement ctf.lttng-live component
[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 #include <babeltrace/babeltrace.h>
30 #include <babeltrace/plugin/plugin.h>
31 #include <babeltrace/common-internal.h>
32 #include <babeltrace/graph/component.h>
33 #include <babeltrace/graph/component-source.h>
34 #include <babeltrace/graph/component-sink.h>
35 #include <babeltrace/graph/component-filter.h>
36 #include <babeltrace/graph/component-class.h>
37 #include <babeltrace/graph/port.h>
38 #include <babeltrace/graph/graph.h>
39 #include <babeltrace/graph/connection.h>
40 #include <babeltrace/graph/notification-iterator.h>
41 #include <babeltrace/ref.h>
42 #include <babeltrace/values.h>
43 #include <babeltrace/logging.h>
44 #include <unistd.h>
45 #include <stdlib.h>
46 #include <popt.h>
47 #include <string.h>
48 #include <stdio.h>
49 #include <glib.h>
50 #include <inttypes.h>
51 #include <unistd.h>
52 #include "babeltrace-cfg.h"
53 #include "babeltrace-cfg-cli-args.h"
54 #include "babeltrace-cfg-cli-args-default.h"
55
56 #define BT_LOG_TAG "CLI"
57 #include "logging.h"
58
59 #define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH"
60
61 GPtrArray *loaded_plugins;
62
63 BT_HIDDEN
64 int bt_cli_log_level = BT_LOG_NONE;
65
66 static
67 void init_static_data(void)
68 {
69 loaded_plugins = g_ptr_array_new_with_free_func(bt_put);
70 }
71
72 static
73 void fini_static_data(void)
74 {
75 g_ptr_array_free(loaded_plugins, TRUE);
76 }
77
78 static
79 struct bt_plugin *find_plugin(const char *name)
80 {
81 int i;
82 struct bt_plugin *plugin = NULL;
83
84 assert(name);
85 BT_LOGD("Finding plugin: name=\"%s\"", name);
86
87 for (i = 0; i < loaded_plugins->len; i++) {
88 plugin = g_ptr_array_index(loaded_plugins, i);
89
90 if (strcmp(name, bt_plugin_get_name(plugin)) == 0) {
91 break;
92 }
93
94 plugin = NULL;
95 }
96
97 if (BT_LOG_ON_DEBUG) {
98 if (plugin) {
99 BT_LOGD("Found plugin: plugin-addr=%p", plugin);
100 } else {
101 BT_LOGD("Cannot find plugin.");
102 }
103 }
104
105 return bt_get(plugin);
106 }
107
108 static
109 struct bt_component_class *find_component_class(const char *plugin_name,
110 const char *comp_class_name,
111 enum bt_component_class_type comp_class_type)
112 {
113 struct bt_component_class *comp_class = NULL;
114 struct bt_plugin *plugin;
115
116 BT_LOGD("Finding component class: plugin-name=\"%s\", "
117 "comp-cls-name=\"%s\", comp-cls-type=%d",
118 plugin_name, comp_class_name, comp_class_type);
119
120 plugin = find_plugin(plugin_name);
121
122 if (!plugin) {
123 goto end;
124 }
125
126 comp_class = bt_plugin_get_component_class_by_name_and_type(plugin,
127 comp_class_name, comp_class_type);
128 BT_PUT(plugin);
129
130 end:
131 if (BT_LOG_ON_DEBUG) {
132 if (comp_class) {
133 BT_LOGD("Found component class: comp-cls-addr=%p",
134 comp_class);
135 } else {
136 BT_LOGD("Cannot find component class.");
137 }
138 }
139
140 return comp_class;
141 }
142
143 static
144 void print_indent(FILE *fp, size_t indent)
145 {
146 size_t i;
147
148 for (i = 0; i < indent; i++) {
149 fprintf(fp, " ");
150 }
151 }
152
153 static
154 const char *component_type_str(enum bt_component_class_type type)
155 {
156 switch (type) {
157 case BT_COMPONENT_CLASS_TYPE_SOURCE:
158 return "source";
159 case BT_COMPONENT_CLASS_TYPE_SINK:
160 return "sink";
161 case BT_COMPONENT_CLASS_TYPE_FILTER:
162 return "filter";
163 case BT_COMPONENT_CLASS_TYPE_UNKNOWN:
164 default:
165 return "unknown";
166 }
167 }
168
169 static
170 void print_plugin_comp_cls_opt(FILE *fh, const char *plugin_name,
171 const char *comp_cls_name, enum bt_component_class_type type)
172 {
173 GString *shell_plugin_name = NULL;
174 GString *shell_comp_cls_name = NULL;
175
176 shell_plugin_name = bt_common_shell_quote(plugin_name, false);
177 if (!shell_plugin_name) {
178 goto end;
179 }
180
181 shell_comp_cls_name = bt_common_shell_quote(comp_cls_name, false);
182 if (!shell_comp_cls_name) {
183 goto end;
184 }
185
186 fprintf(fh, "%s%s--%s%s %s'%s%s%s%s.%s%s%s'",
187 bt_common_color_bold(),
188 bt_common_color_fg_cyan(),
189 component_type_str(type),
190 bt_common_color_reset(),
191 bt_common_color_fg_default(),
192 bt_common_color_bold(),
193 bt_common_color_fg_blue(),
194 shell_plugin_name->str,
195 bt_common_color_fg_default(),
196 bt_common_color_fg_yellow(),
197 shell_comp_cls_name->str,
198 bt_common_color_reset());
199
200 end:
201 if (shell_plugin_name) {
202 g_string_free(shell_plugin_name, TRUE);
203 }
204
205 if (shell_comp_cls_name) {
206 g_string_free(shell_comp_cls_name, TRUE);
207 }
208 }
209
210 static
211 void print_value(FILE *, struct bt_value *, size_t);
212
213 static
214 void print_value_rec(FILE *, struct bt_value *, size_t);
215
216 struct print_map_value_data {
217 size_t indent;
218 FILE *fp;
219 };
220
221 static
222 bt_bool print_map_value(const char *key, struct bt_value *object, void *data)
223 {
224 struct print_map_value_data *print_map_value_data = data;
225
226 print_indent(print_map_value_data->fp, print_map_value_data->indent);
227 fprintf(print_map_value_data->fp, "%s: ", key);
228
229 if (bt_value_is_array(object) &&
230 bt_value_array_is_empty(object)) {
231 fprintf(print_map_value_data->fp, "[ ]\n");
232 return true;
233 }
234
235 if (bt_value_is_map(object) &&
236 bt_value_map_is_empty(object)) {
237 fprintf(print_map_value_data->fp, "{ }\n");
238 return true;
239 }
240
241 if (bt_value_is_array(object) ||
242 bt_value_is_map(object)) {
243 fprintf(print_map_value_data->fp, "\n");
244 }
245
246 print_value_rec(print_map_value_data->fp, object,
247 print_map_value_data->indent + 2);
248 return BT_TRUE;
249 }
250
251 static
252 void print_value_rec(FILE *fp, struct bt_value *value, size_t indent)
253 {
254 bt_bool bool_val;
255 int64_t int_val;
256 double dbl_val;
257 const char *str_val;
258 int size;
259 int i;
260
261 if (!value) {
262 return;
263 }
264
265 switch (bt_value_get_type(value)) {
266 case BT_VALUE_TYPE_NULL:
267 fprintf(fp, "%snull%s\n", bt_common_color_bold(),
268 bt_common_color_reset());
269 break;
270 case BT_VALUE_TYPE_BOOL:
271 bt_value_bool_get(value, &bool_val);
272 fprintf(fp, "%s%s%s%s\n", bt_common_color_bold(),
273 bt_common_color_fg_cyan(), bool_val ? "yes" : "no",
274 bt_common_color_reset());
275 break;
276 case BT_VALUE_TYPE_INTEGER:
277 bt_value_integer_get(value, &int_val);
278 fprintf(fp, "%s%s%" PRId64 "%s\n", bt_common_color_bold(),
279 bt_common_color_fg_red(), int_val,
280 bt_common_color_reset());
281 break;
282 case BT_VALUE_TYPE_FLOAT:
283 bt_value_float_get(value, &dbl_val);
284 fprintf(fp, "%s%s%lf%s\n", bt_common_color_bold(),
285 bt_common_color_fg_red(), dbl_val,
286 bt_common_color_reset());
287 break;
288 case BT_VALUE_TYPE_STRING:
289 bt_value_string_get(value, &str_val);
290 fprintf(fp, "%s%s%s%s\n", bt_common_color_bold(),
291 bt_common_color_fg_green(), str_val,
292 bt_common_color_reset());
293 break;
294 case BT_VALUE_TYPE_ARRAY:
295 size = bt_value_array_size(value);
296 assert(size >= 0);
297
298 if (size == 0) {
299 print_indent(fp, indent);
300 fprintf(fp, "[ ]\n");
301 break;
302 }
303
304 for (i = 0; i < size; i++) {
305 struct bt_value *element =
306 bt_value_array_get(value, i);
307
308 assert(element);
309 print_indent(fp, indent);
310 fprintf(fp, "- ");
311
312 if (bt_value_is_array(element) &&
313 bt_value_array_is_empty(element)) {
314 fprintf(fp, "[ ]\n");
315 continue;
316 }
317
318 if (bt_value_is_map(element) &&
319 bt_value_map_is_empty(element)) {
320 fprintf(fp, "{ }\n");
321 continue;
322 }
323
324 if (bt_value_is_array(element) ||
325 bt_value_is_map(element)) {
326 fprintf(fp, "\n");
327 }
328
329 print_value_rec(fp, element, indent + 2);
330 BT_PUT(element);
331 }
332 break;
333 case BT_VALUE_TYPE_MAP:
334 {
335 struct print_map_value_data data = {
336 .indent = indent,
337 .fp = fp,
338 };
339
340 if (bt_value_map_is_empty(value)) {
341 print_indent(fp, indent);
342 fprintf(fp, "{ }\n");
343 break;
344 }
345
346 bt_value_map_foreach(value, print_map_value, &data);
347 break;
348 }
349 default:
350 assert(false);
351 }
352 }
353
354 static
355 void print_value(FILE *fp, struct bt_value *value, size_t indent)
356 {
357 if (!bt_value_is_array(value) && !bt_value_is_map(value)) {
358 print_indent(fp, indent);
359 }
360
361 print_value_rec(fp, value, indent);
362 }
363
364 static
365 void print_bt_config_component(struct bt_config_component *bt_config_component)
366 {
367 fprintf(stderr, " ");
368 print_plugin_comp_cls_opt(stderr, bt_config_component->plugin_name->str,
369 bt_config_component->comp_cls_name->str,
370 bt_config_component->type);
371 fprintf(stderr, ":\n");
372
373 if (bt_config_component->instance_name->len > 0) {
374 fprintf(stderr, " Name: %s\n",
375 bt_config_component->instance_name->str);
376 }
377
378 fprintf(stderr, " Parameters:\n");
379 print_value(stderr, bt_config_component->params, 8);
380 }
381
382 static
383 void print_bt_config_components(GPtrArray *array)
384 {
385 size_t i;
386
387 for (i = 0; i < array->len; i++) {
388 struct bt_config_component *cfg_component =
389 bt_config_get_component(array, i);
390 print_bt_config_component(cfg_component);
391 BT_PUT(cfg_component);
392 }
393 }
394
395 static
396 void print_plugin_paths(struct bt_value *plugin_paths)
397 {
398 fprintf(stderr, " Plugin paths:\n");
399 print_value(stderr, plugin_paths, 4);
400 }
401
402 static
403 void print_cfg_run(struct bt_config *cfg)
404 {
405 size_t i;
406
407 print_plugin_paths(cfg->plugin_paths);
408 fprintf(stderr, " Source component instances:\n");
409 print_bt_config_components(cfg->cmd_data.run.sources);
410
411 if (cfg->cmd_data.run.filters->len > 0) {
412 fprintf(stderr, " Filter component instances:\n");
413 print_bt_config_components(cfg->cmd_data.run.filters);
414 }
415
416 fprintf(stderr, " Sink component instances:\n");
417 print_bt_config_components(cfg->cmd_data.run.sinks);
418 fprintf(stderr, " Connections:\n");
419
420 for (i = 0; i < cfg->cmd_data.run.connections->len; i++) {
421 struct bt_config_connection *cfg_connection =
422 g_ptr_array_index(cfg->cmd_data.run.connections,
423 i);
424
425 fprintf(stderr, " %s%s%s -> %s%s%s\n",
426 cfg_connection->upstream_comp_name->str,
427 cfg_connection->upstream_port_glob->len > 0 ? "." : "",
428 cfg_connection->upstream_port_glob->str,
429 cfg_connection->downstream_comp_name->str,
430 cfg_connection->downstream_port_glob->len > 0 ? "." : "",
431 cfg_connection->downstream_port_glob->str);
432 }
433 }
434
435 static
436 void print_cfg_list_plugins(struct bt_config *cfg)
437 {
438 print_plugin_paths(cfg->plugin_paths);
439 }
440
441 static
442 void print_cfg_help(struct bt_config *cfg)
443 {
444 print_plugin_paths(cfg->plugin_paths);
445 }
446
447 static
448 void print_cfg_print_ctf_metadata(struct bt_config *cfg)
449 {
450 print_plugin_paths(cfg->plugin_paths);
451 fprintf(stderr, " Path: %s\n",
452 cfg->cmd_data.print_ctf_metadata.path->str);
453 }
454
455 static
456 void print_cfg_print_lttng_live_sessions(struct bt_config *cfg)
457 {
458 print_plugin_paths(cfg->plugin_paths);
459 fprintf(stderr, " URL: %s\n",
460 cfg->cmd_data.print_lttng_live_sessions.url->str);
461 }
462
463 static
464 void print_cfg_query(struct bt_config *cfg)
465 {
466 print_plugin_paths(cfg->plugin_paths);
467 fprintf(stderr, " Object: `%s`\n", cfg->cmd_data.query.object->str);
468 fprintf(stderr, " Component class:\n");
469 print_bt_config_component(cfg->cmd_data.query.cfg_component);
470 }
471
472 static
473 void print_cfg(struct bt_config *cfg)
474 {
475 if (!BT_LOG_ON_INFO) {
476 return;
477 }
478
479 BT_LOGI_STR("Configuration:");
480 fprintf(stderr, " Debug mode: %s\n", cfg->debug ? "yes" : "no");
481 fprintf(stderr, " Verbose mode: %s\n", cfg->verbose ? "yes" : "no");
482
483 switch (cfg->command) {
484 case BT_CONFIG_COMMAND_RUN:
485 print_cfg_run(cfg);
486 break;
487 case BT_CONFIG_COMMAND_LIST_PLUGINS:
488 print_cfg_list_plugins(cfg);
489 break;
490 case BT_CONFIG_COMMAND_HELP:
491 print_cfg_help(cfg);
492 break;
493 case BT_CONFIG_COMMAND_QUERY:
494 print_cfg_query(cfg);
495 break;
496 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA:
497 print_cfg_print_ctf_metadata(cfg);
498 break;
499 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS:
500 print_cfg_print_lttng_live_sessions(cfg);
501 break;
502 default:
503 assert(false);
504 }
505 }
506
507 static
508 void add_to_loaded_plugins(struct bt_plugin_set *plugin_set)
509 {
510 int64_t i;
511 int64_t count;
512
513 count = bt_plugin_set_get_plugin_count(plugin_set);
514 assert(count >= 0);
515
516 for (i = 0; i < count; i++) {
517 struct bt_plugin *plugin =
518 bt_plugin_set_get_plugin(plugin_set, i);
519 struct bt_plugin *loaded_plugin =
520 find_plugin(bt_plugin_get_name(plugin));
521
522 assert(plugin);
523
524 if (loaded_plugin) {
525 BT_LOGI("Not using plugin: another one already exists with the same name: "
526 "plugin-name=\"%s\", plugin-path=\"%s\", "
527 "existing-plugin-path=\"%s\"",
528 bt_plugin_get_name(plugin),
529 bt_plugin_get_path(plugin),
530 bt_plugin_get_path(loaded_plugin));
531 bt_put(loaded_plugin);
532 } else {
533 /* Add to global array. */
534 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
535 bt_plugin_get_name(plugin));
536 g_ptr_array_add(loaded_plugins, bt_get(plugin));
537 }
538
539 bt_put(plugin);
540 }
541 }
542
543 static
544 int load_dynamic_plugins(struct bt_value *plugin_paths)
545 {
546 int nr_paths, i, ret = 0;
547
548 nr_paths = bt_value_array_size(plugin_paths);
549 if (nr_paths < 0) {
550 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
551 ret = -1;
552 goto end;
553 }
554
555 BT_LOGI("Loading dynamic plugins.");
556
557 for (i = 0; i < nr_paths; i++) {
558 struct bt_value *plugin_path_value = NULL;
559 const char *plugin_path;
560 struct bt_plugin_set *plugin_set;
561
562 plugin_path_value = bt_value_array_get(plugin_paths, i);
563 bt_value_string_get(plugin_path_value, &plugin_path);
564 assert(plugin_path);
565 plugin_set = bt_plugin_create_all_from_dir(plugin_path, false);
566 if (!plugin_set) {
567 BT_LOGD("Unable to load dynamic plugins: path=\"%s\"",
568 plugin_path);
569 BT_PUT(plugin_path_value);
570 continue;
571 }
572
573 add_to_loaded_plugins(plugin_set);
574 bt_put(plugin_set);
575 BT_PUT(plugin_path_value);
576 }
577 end:
578 return ret;
579 }
580
581 static
582 int load_static_plugins(void)
583 {
584 int ret = 0;
585 struct bt_plugin_set *plugin_set;
586
587 BT_LOGI("Loading static plugins.");
588 plugin_set = bt_plugin_create_all_from_static();
589 if (!plugin_set) {
590 BT_LOGE("Unable to load static plugins.");
591 ret = -1;
592 goto end;
593 }
594
595 add_to_loaded_plugins(plugin_set);
596 bt_put(plugin_set);
597 end:
598 return ret;
599 }
600
601 static
602 int load_all_plugins(struct bt_value *plugin_paths)
603 {
604 int ret = 0;
605
606 if (load_dynamic_plugins(plugin_paths)) {
607 ret = -1;
608 goto end;
609 }
610
611 if (load_static_plugins()) {
612 ret = -1;
613 goto end;
614 }
615
616 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins->len);
617
618 end:
619 return ret;
620 }
621
622 static
623 void print_plugin_info(struct bt_plugin *plugin)
624 {
625 unsigned int major, minor, patch;
626 const char *extra;
627 enum bt_plugin_status version_status;
628 const char *plugin_name;
629 const char *path;
630 const char *author;
631 const char *license;
632 const char *plugin_description;
633
634 plugin_name = bt_plugin_get_name(plugin);
635 path = bt_plugin_get_path(plugin);
636 author = bt_plugin_get_author(plugin);
637 license = bt_plugin_get_license(plugin);
638 plugin_description = bt_plugin_get_description(plugin);
639 version_status = bt_plugin_get_version(plugin, &major, &minor,
640 &patch, &extra);
641 printf("%s%s%s%s:\n", bt_common_color_bold(),
642 bt_common_color_fg_blue(), plugin_name,
643 bt_common_color_reset());
644 printf(" %sPath%s: %s\n", bt_common_color_bold(),
645 bt_common_color_reset(), path ? path : "(None)");
646
647 if (version_status == BT_PLUGIN_STATUS_OK) {
648 printf(" %sVersion%s: %u.%u.%u",
649 bt_common_color_bold(), bt_common_color_reset(),
650 major, minor, patch);
651
652 if (extra) {
653 printf("%s", extra);
654 }
655
656 printf("\n");
657 }
658
659 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
660 bt_common_color_reset(),
661 plugin_description ? plugin_description : "(None)");
662 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
663 bt_common_color_reset(), author ? author : "(Unknown)");
664 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
665 bt_common_color_reset(),
666 license ? license : "(Unknown)");
667 }
668
669 static
670 int cmd_query(struct bt_config *cfg)
671 {
672 int ret;
673 struct bt_component_class *comp_cls = NULL;
674 struct bt_value *results = NULL;
675
676 ret = load_all_plugins(cfg->plugin_paths);
677 if (ret) {
678 goto end;
679 }
680
681 comp_cls = find_component_class(cfg->cmd_data.query.cfg_component->plugin_name->str,
682 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
683 cfg->cmd_data.query.cfg_component->type);
684 if (!comp_cls) {
685 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
686 "comp-cls-name=\"%s\", comp-cls-type=%d",
687 cfg->cmd_data.query.cfg_component->plugin_name->str,
688 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
689 cfg->cmd_data.query.cfg_component->type);
690 fprintf(stderr, "%s%sCannot find component class %s",
691 bt_common_color_bold(),
692 bt_common_color_fg_red(),
693 bt_common_color_reset());
694 print_plugin_comp_cls_opt(stderr,
695 cfg->cmd_data.query.cfg_component->plugin_name->str,
696 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
697 cfg->cmd_data.query.cfg_component->type);
698 fprintf(stderr, "\n");
699 ret = -1;
700 goto end;
701 }
702
703 results = bt_component_class_query(comp_cls,
704 cfg->cmd_data.query.object->str,
705 cfg->cmd_data.query.cfg_component->params);
706 if (!results) {
707 BT_LOGE("Failed to query component class: plugin-name=\"%s\", "
708 "comp-cls-name=\"%s\", comp-cls-type=%d "
709 "object=\"%s\"",
710 cfg->cmd_data.query.cfg_component->plugin_name->str,
711 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
712 cfg->cmd_data.query.cfg_component->type,
713 cfg->cmd_data.query.object->str);
714 fprintf(stderr, "%s%sFailed to query info to %s",
715 bt_common_color_bold(),
716 bt_common_color_fg_red(),
717 bt_common_color_reset());
718 print_plugin_comp_cls_opt(stderr,
719 cfg->cmd_data.query.cfg_component->plugin_name->str,
720 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
721 cfg->cmd_data.query.cfg_component->type);
722 fprintf(stderr, "%s%s with object `%s`%s\n",
723 bt_common_color_bold(),
724 bt_common_color_fg_red(),
725 cfg->cmd_data.query.object->str,
726 bt_common_color_reset());
727 ret = -1;
728 goto end;
729 }
730
731 print_value(stdout, results, 0);
732
733 end:
734 bt_put(comp_cls);
735 bt_put(results);
736 return ret;
737 }
738
739 static
740 int cmd_help(struct bt_config *cfg)
741 {
742 int ret;
743 struct bt_plugin *plugin = NULL;
744 size_t i;
745
746 ret = load_all_plugins(cfg->plugin_paths);
747 if (ret) {
748 goto end;
749 }
750
751 plugin = find_plugin(cfg->cmd_data.help.cfg_component->plugin_name->str);
752 if (!plugin) {
753 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
754 cfg->cmd_data.help.cfg_component->plugin_name->str);
755 fprintf(stderr, "%s%sCannot find plugin %s%s%s\n",
756 bt_common_color_bold(), bt_common_color_fg_red(),
757 bt_common_color_fg_blue(),
758 cfg->cmd_data.help.cfg_component->plugin_name->str,
759 bt_common_color_reset());
760 ret = -1;
761 goto end;
762 }
763
764 print_plugin_info(plugin);
765 printf(" %sComponent classes%s: %d\n",
766 bt_common_color_bold(),
767 bt_common_color_reset(),
768 (int) bt_plugin_get_component_class_count(plugin));
769
770
771 if (cfg->cmd_data.help.cfg_component->type !=
772 BT_COMPONENT_CLASS_TYPE_UNKNOWN) {
773 struct bt_component_class *needed_comp_cls =
774 find_component_class(
775 cfg->cmd_data.help.cfg_component->plugin_name->str,
776 cfg->cmd_data.help.cfg_component->comp_cls_name->str,
777 cfg->cmd_data.help.cfg_component->type);
778
779 if (!needed_comp_cls) {
780 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
781 "comp-cls-name=\"%s\", comp-cls-type=%d",
782 cfg->cmd_data.help.cfg_component->plugin_name->str,
783 cfg->cmd_data.help.cfg_component->comp_cls_name->str,
784 cfg->cmd_data.help.cfg_component->type);
785 fprintf(stderr, "\n%s%sCannot find component class %s",
786 bt_common_color_bold(),
787 bt_common_color_fg_red(),
788 bt_common_color_reset());
789 print_plugin_comp_cls_opt(stderr,
790 cfg->cmd_data.help.cfg_component->plugin_name->str,
791 cfg->cmd_data.help.cfg_component->comp_cls_name->str,
792 cfg->cmd_data.help.cfg_component->type);
793 fprintf(stderr, "\n");
794 ret = -1;
795 goto end;
796 }
797
798 bt_put(needed_comp_cls);
799 }
800
801 for (i = 0; i < bt_plugin_get_component_class_count(plugin); i++) {
802 struct bt_component_class *comp_cls =
803 bt_plugin_get_component_class_by_index(plugin, i);
804 const char *comp_class_name =
805 bt_component_class_get_name(comp_cls);
806 const char *comp_class_description =
807 bt_component_class_get_description(comp_cls);
808 const char *comp_class_help =
809 bt_component_class_get_help(comp_cls);
810 enum bt_component_class_type type =
811 bt_component_class_get_type(comp_cls);
812
813 assert(comp_cls);
814
815 if (cfg->cmd_data.help.cfg_component->type !=
816 BT_COMPONENT_CLASS_TYPE_UNKNOWN) {
817 if (strcmp(cfg->cmd_data.help.cfg_component->comp_cls_name->str,
818 comp_class_name) != 0 &&
819 type ==
820 cfg->cmd_data.help.cfg_component->type) {
821 bt_put(comp_cls);
822 continue;
823 }
824 }
825
826 printf("\n");
827 print_plugin_comp_cls_opt(stdout,
828 cfg->cmd_data.help.cfg_component->plugin_name->str,
829 comp_class_name,
830 type);
831 printf("\n");
832 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
833 bt_common_color_reset(),
834 comp_class_description ? comp_class_description : "(None)");
835
836 if (comp_class_help) {
837 printf("\n%s\n", comp_class_help);
838 }
839
840 bt_put(comp_cls);
841 }
842
843 end:
844 bt_put(plugin);
845 return ret;
846 }
847
848 static
849 int cmd_list_plugins(struct bt_config *cfg)
850 {
851 int ret = 0;
852 int plugins_count, component_classes_count = 0, i;
853
854 ret = load_all_plugins(cfg->plugin_paths);
855 if (ret) {
856 goto end;
857 }
858
859 printf("From the following plugin paths:\n\n");
860 print_value(stdout, cfg->plugin_paths, 2);
861 printf("\n");
862 plugins_count = loaded_plugins->len;
863 if (plugins_count == 0) {
864 printf("No plugins found.\n");
865 goto end;
866 }
867
868 for (i = 0; i < plugins_count; i++) {
869 struct bt_plugin *plugin = g_ptr_array_index(loaded_plugins, i);
870
871 component_classes_count += bt_plugin_get_component_class_count(plugin);
872 }
873
874 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
875 bt_common_color_bold(),
876 component_classes_count,
877 bt_common_color_reset(),
878 bt_common_color_bold(),
879 plugins_count,
880 bt_common_color_reset());
881
882 for (i = 0; i < plugins_count; i++) {
883 int j;
884 struct bt_plugin *plugin = g_ptr_array_index(loaded_plugins, i);
885
886 component_classes_count =
887 bt_plugin_get_component_class_count(plugin);
888 printf("\n");
889 print_plugin_info(plugin);
890
891 if (component_classes_count == 0) {
892 printf(" %sComponent classes%s: (none)\n",
893 bt_common_color_bold(),
894 bt_common_color_reset());
895 } else {
896 printf(" %sComponent classes%s:\n",
897 bt_common_color_bold(),
898 bt_common_color_reset());
899 }
900
901 for (j = 0; j < component_classes_count; j++) {
902 struct bt_component_class *comp_class =
903 bt_plugin_get_component_class_by_index(
904 plugin, j);
905 const char *comp_class_name =
906 bt_component_class_get_name(comp_class);
907 const char *comp_class_description =
908 bt_component_class_get_description(comp_class);
909 enum bt_component_class_type type =
910 bt_component_class_get_type(comp_class);
911
912 printf(" ");
913 print_plugin_comp_cls_opt(stdout,
914 bt_plugin_get_name(plugin), comp_class_name,
915 type);
916
917 if (comp_class_description) {
918 printf(": %s", comp_class_description);
919 }
920
921 printf("\n");
922 bt_put(comp_class);
923 }
924 }
925
926 end:
927 return ret;
928 }
929
930 static
931 int cmd_print_lttng_live_sessions(struct bt_config *cfg)
932 {
933 printf("TODO\n");
934 return -1;
935 }
936
937 static
938 int cmd_print_ctf_metadata(struct bt_config *cfg)
939 {
940 int ret = 0;
941 struct bt_component_class *comp_cls = NULL;
942 struct bt_value *results = NULL;
943 struct bt_value *params = NULL;
944 struct bt_value *metadata_text_value = NULL;
945 const char *metadata_text = NULL;
946 static const char * const plugin_name = "ctf";
947 static const char * const comp_cls_name = "fs";
948 static const enum bt_component_class_type comp_cls_type =
949 BT_COMPONENT_CLASS_TYPE_SOURCE;
950
951 assert(cfg->cmd_data.print_ctf_metadata.path);
952 comp_cls = find_component_class(plugin_name, comp_cls_name,
953 comp_cls_type);
954 if (!comp_cls) {
955 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
956 "comp-cls-name=\"%s\", comp-cls-type=%d",
957 plugin_name, comp_cls_name,
958 BT_COMPONENT_CLASS_TYPE_SOURCE);
959 fprintf(stderr, "%s%sCannot find component class %s",
960 bt_common_color_bold(),
961 bt_common_color_fg_red(),
962 bt_common_color_reset());
963 print_plugin_comp_cls_opt(stderr, plugin_name,
964 comp_cls_name, comp_cls_type);
965 fprintf(stderr, "\n");
966 ret = -1;
967 goto end;
968 }
969
970 params = bt_value_map_create();
971 if (!params) {
972 ret = -1;
973 goto end;
974 }
975
976 ret = bt_value_map_insert_string(params, "path",
977 cfg->cmd_data.print_ctf_metadata.path->str);
978 if (ret) {
979 ret = -1;
980 goto end;
981 }
982
983 results = bt_component_class_query(comp_cls, "metadata-info",
984 params);
985 if (!results) {
986 ret = -1;
987 BT_LOGE_STR("Failed to query for metadata info.");
988 fprintf(stderr, "%s%sFailed to request metadata info%s\n",
989 bt_common_color_bold(),
990 bt_common_color_fg_red(),
991 bt_common_color_reset());
992 goto end;
993 }
994
995 metadata_text_value = bt_value_map_get(results, "text");
996 if (!metadata_text_value) {
997 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
998 ret = -1;
999 goto end;
1000 }
1001
1002 ret = bt_value_string_get(metadata_text_value, &metadata_text);
1003 assert(ret == 0);
1004 printf("%s\n", metadata_text);
1005
1006 end:
1007 bt_put(results);
1008 bt_put(params);
1009 bt_put(metadata_text_value);
1010 bt_put(comp_cls);
1011 return 0;
1012 }
1013
1014 struct cmd_run_ctx {
1015 /* Owned by this */
1016 GHashTable *components;
1017
1018 /* Owned by this */
1019 struct bt_graph *graph;
1020
1021 /* Weak */
1022 struct bt_config *cfg;
1023
1024 bool connect_ports;
1025 };
1026
1027 static
1028 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1029 struct cmd_run_ctx *ctx, struct bt_component *upstream_comp,
1030 struct bt_port *upstream_port,
1031 struct bt_config_connection *cfg_conn)
1032 {
1033 int ret = 0;
1034 GQuark downstreamp_comp_name_quark;
1035 struct bt_component *downstream_comp;
1036 int64_t downstream_port_count;
1037 uint64_t i;
1038 int64_t (*port_count_fn)(struct bt_component *);
1039 struct bt_port *(*port_by_index_fn)(struct bt_component *, uint64_t);
1040 void *conn = NULL;
1041
1042 BT_LOGI("Connecting upstream port to the next available downstream port: "
1043 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1044 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1045 upstream_port, bt_port_get_name(upstream_port),
1046 cfg_conn->downstream_comp_name->str,
1047 cfg_conn->arg->str);
1048 downstreamp_comp_name_quark = g_quark_from_string(
1049 cfg_conn->downstream_comp_name->str);
1050 assert(downstreamp_comp_name_quark > 0);
1051 downstream_comp = g_hash_table_lookup(ctx->components,
1052 (gpointer) (long) downstreamp_comp_name_quark);
1053 if (!downstream_comp) {
1054 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1055 "conn-arg=\"%s\"", cfg_conn->downstream_comp_name->str,
1056 cfg_conn->arg->str);
1057 fprintf(stderr, "Cannot create connection: cannot find downstream component: %s\n",
1058 cfg_conn->arg->str);
1059 goto error;
1060 }
1061
1062 if (bt_component_is_filter(downstream_comp)) {
1063 port_count_fn = bt_component_filter_get_input_port_count;
1064 port_by_index_fn = bt_component_filter_get_input_port_by_index;
1065 } else if (bt_component_is_sink(downstream_comp)) {
1066 port_count_fn = bt_component_sink_get_input_port_count;
1067 port_by_index_fn = bt_component_sink_get_input_port_by_index;
1068 } else {
1069 /*
1070 * Should never happen because the connections are
1071 * validated before we get here.
1072 */
1073 BT_LOGF("Invalid connection: downstream component is a source: "
1074 "conn-arg=\"%s\"", cfg_conn->arg->str);
1075 assert(false);
1076 }
1077
1078 downstream_port_count = port_count_fn(downstream_comp);
1079 assert(downstream_port_count >= 0);
1080
1081 for (i = 0; i < downstream_port_count; i++) {
1082 struct bt_port *downstream_port =
1083 port_by_index_fn(downstream_comp, i);
1084 const char *downstream_port_name;
1085
1086 assert(downstream_port);
1087
1088 /* Skip port if it's already connected */
1089 if (bt_port_is_connected(downstream_port)) {
1090 bt_put(downstream_port);
1091 BT_LOGD("Skipping downstream port: already connected: "
1092 "port-addr=%p, port-name=\"%s\"",
1093 downstream_port,
1094 bt_port_get_name(downstream_port));
1095 continue;
1096 }
1097
1098 downstream_port_name = bt_port_get_name(downstream_port);
1099 assert(downstream_port_name);
1100
1101 if (bt_common_star_glob_match(
1102 cfg_conn->downstream_port_glob->str, -1ULL,
1103 downstream_port_name, -1ULL)) {
1104 /* We have a winner! */
1105 conn = bt_graph_connect_ports(ctx->graph,
1106 upstream_port, downstream_port);
1107 bt_put(downstream_port);
1108 if (!conn) {
1109 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1110 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1111 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1112 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1113 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1114 "conn-arg=\"%s\"",
1115 upstream_comp, bt_component_get_name(upstream_comp),
1116 upstream_port, bt_port_get_name(upstream_port),
1117 downstream_comp, cfg_conn->downstream_comp_name->str,
1118 downstream_port, downstream_port_name,
1119 cfg_conn->arg->str);
1120 fprintf(stderr,
1121 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1122 bt_port_get_name(upstream_port),
1123 downstream_port_name,
1124 cfg_conn->arg->str);
1125 goto error;
1126 }
1127
1128 BT_LOGI("Connected component ports: "
1129 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1130 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1131 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1132 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1133 "conn-arg=\"%s\"",
1134 upstream_comp, bt_component_get_name(upstream_comp),
1135 upstream_port, bt_port_get_name(upstream_port),
1136 downstream_comp, cfg_conn->downstream_comp_name->str,
1137 downstream_port, downstream_port_name,
1138 cfg_conn->arg->str);
1139
1140 goto end;
1141 }
1142
1143 bt_put(downstream_port);
1144 }
1145
1146 if (!conn) {
1147 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1148 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1149 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1150 upstream_port, bt_port_get_name(upstream_port),
1151 cfg_conn->downstream_comp_name->str,
1152 cfg_conn->arg->str);
1153 fprintf(stderr,
1154 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1155 bt_port_get_name(upstream_port), cfg_conn->arg->str);
1156 goto error;
1157 }
1158
1159 goto end;
1160
1161 error:
1162 ret = -1;
1163
1164 end:
1165 bt_put(conn);
1166 return ret;
1167 }
1168
1169 static
1170 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx *ctx,
1171 struct bt_port *upstream_port)
1172 {
1173 int ret = 0;
1174 const char *upstream_port_name;
1175 const char *upstream_comp_name;
1176 struct bt_component *upstream_comp = NULL;
1177 size_t i;
1178
1179 assert(ctx);
1180 assert(upstream_port);
1181 upstream_port_name = bt_port_get_name(upstream_port);
1182 assert(upstream_port_name);
1183 upstream_comp = bt_port_get_component(upstream_port);
1184 if (!upstream_comp) {
1185 BT_LOGW("Upstream port to connect is not part of a component: "
1186 "port-addr=%p, port-name=\"%s\"",
1187 upstream_port, upstream_port_name);
1188 ret = -1;
1189 goto end;
1190 }
1191
1192 upstream_comp_name = bt_component_get_name(upstream_comp);
1193 assert(upstream_comp_name);
1194 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1195 "port-addr=%p, port-name=\"%s\"",
1196 upstream_comp, upstream_comp_name,
1197 upstream_port, upstream_port_name);
1198
1199 for (i = 0; i < ctx->cfg->cmd_data.run.connections->len; i++) {
1200 struct bt_config_connection *cfg_conn =
1201 g_ptr_array_index(
1202 ctx->cfg->cmd_data.run.connections, i);
1203
1204 if (strcmp(cfg_conn->upstream_comp_name->str,
1205 upstream_comp_name) == 0) {
1206 if (bt_common_star_glob_match(
1207 cfg_conn->upstream_port_glob->str,
1208 -1ULL, upstream_port_name, -1ULL)) {
1209 ret = cmd_run_ctx_connect_upstream_port_to_downstream_component(
1210 ctx, upstream_comp, upstream_port,
1211 cfg_conn);
1212 if (ret) {
1213 BT_LOGE("Cannot connect upstream port: "
1214 "port-addr=%p, port-name=\"%s\"",
1215 upstream_port,
1216 upstream_port_name);
1217 fprintf(stderr,
1218 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1219 upstream_port_name,
1220 upstream_comp_name,
1221 cfg_conn->arg->str);
1222 goto error;
1223 }
1224
1225 goto end;
1226 }
1227 }
1228 }
1229
1230 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1231 "port-addr=%p, port-name=\"%s\"", upstream_port,
1232 upstream_port_name);
1233 fprintf(stderr,
1234 "Cannot create connection: upstream port `%s` does not match any connection\n",
1235 upstream_port_name);
1236
1237 error:
1238 ret = -1;
1239
1240 end:
1241 bt_put(upstream_comp);
1242 return ret;
1243 }
1244
1245 static
1246 void graph_port_added_listener(struct bt_port *port, void *data)
1247 {
1248 struct bt_component *comp = NULL;
1249 struct cmd_run_ctx *ctx = data;
1250
1251 BT_LOGI("Port added to a graph's component: port-addr=%p, port-name=\"%s\"",
1252 port, bt_port_get_name(port));
1253
1254 if (bt_port_is_connected(port)) {
1255 BT_LOGW_STR("Port is already connected.");
1256 goto end;
1257 }
1258
1259 comp = bt_port_get_component(port);
1260 if (!comp) {
1261 BT_LOGW_STR("Port has no component.");
1262 goto end;
1263 }
1264
1265 if (!bt_port_is_output(port)) {
1266 BT_LOGI_STR("Skipping input port.");
1267 goto end;
1268 }
1269
1270 if (cmd_run_ctx_connect_upstream_port(ctx, port)) {
1271 BT_LOGF_STR("Cannot connect upstream port.");
1272 fprintf(stderr, "Added port could not be connected: aborting\n");
1273 abort();
1274 }
1275
1276 end:
1277 bt_put(comp);
1278 return;
1279 }
1280
1281 static
1282 void graph_port_removed_listener(struct bt_component *component,
1283 struct bt_port *port, void *data)
1284 {
1285 BT_LOGI("Port removed from a graph's component: comp-addr=%p, "
1286 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1287 component, bt_component_get_name(component),
1288 port, bt_port_get_name(port));
1289 }
1290
1291 static
1292 void graph_ports_connected_listener(struct bt_port *upstream_port,
1293 struct bt_port *downstream_port, void *data)
1294 {
1295 BT_LOGI("Graph's component ports connected: "
1296 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1297 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1298 upstream_port, bt_port_get_name(upstream_port),
1299 downstream_port, bt_port_get_name(downstream_port));
1300 }
1301
1302 static
1303 void graph_ports_disconnected_listener(
1304 struct bt_component *upstream_component,
1305 struct bt_component *downstream_component,
1306 struct bt_port *upstream_port, struct bt_port *downstream_port,
1307 void *data)
1308 {
1309 BT_LOGI("Graph's component ports disconnected: "
1310 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1311 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1312 upstream_port, bt_port_get_name(upstream_port),
1313 downstream_port, bt_port_get_name(downstream_port));
1314 }
1315
1316 static
1317 void cmd_run_ctx_destroy(struct cmd_run_ctx *ctx)
1318 {
1319 if (!ctx) {
1320 return;
1321 }
1322
1323 if (ctx->components) {
1324 g_hash_table_destroy(ctx->components);
1325 ctx->components = NULL;
1326 }
1327
1328 BT_PUT(ctx->graph);
1329 ctx->cfg = NULL;
1330 }
1331
1332 static
1333 int cmd_run_ctx_init(struct cmd_run_ctx *ctx, struct bt_config *cfg)
1334 {
1335 int ret = 0;
1336
1337 ctx->cfg = cfg;
1338 ctx->connect_ports = false;
1339 ctx->components = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1340 NULL, bt_put);
1341 if (!ctx->components) {
1342 goto error;
1343 }
1344
1345 ctx->graph = bt_graph_create();
1346 if (!ctx->graph) {
1347 goto error;
1348 }
1349
1350 ret = bt_graph_add_port_added_listener(ctx->graph,
1351 graph_port_added_listener, ctx);
1352 if (ret) {
1353 goto error;
1354 }
1355
1356 ret = bt_graph_add_port_removed_listener(ctx->graph,
1357 graph_port_removed_listener, ctx);
1358 if (ret) {
1359 goto error;
1360 }
1361
1362 ret = bt_graph_add_ports_connected_listener(ctx->graph,
1363 graph_ports_connected_listener, ctx);
1364 if (ret) {
1365 goto error;
1366 }
1367
1368 ret = bt_graph_add_ports_disconnected_listener(ctx->graph,
1369 graph_ports_disconnected_listener, ctx);
1370 if (ret) {
1371 goto error;
1372 }
1373
1374 goto end;
1375
1376 error:
1377 cmd_run_ctx_destroy(ctx);
1378 ret = -1;
1379
1380 end:
1381 return ret;
1382 }
1383
1384 static
1385 int cmd_run_ctx_create_components_from_config_components(
1386 struct cmd_run_ctx *ctx, GPtrArray *cfg_components)
1387 {
1388 size_t i;
1389 struct bt_component_class *comp_cls = NULL;
1390 struct bt_component *comp = NULL;
1391 int ret = 0;
1392
1393 for (i = 0; i < cfg_components->len; i++) {
1394 struct bt_config_component *cfg_comp =
1395 g_ptr_array_index(cfg_components, i);
1396 GQuark quark;
1397
1398 comp_cls = find_component_class(cfg_comp->plugin_name->str,
1399 cfg_comp->comp_cls_name->str, cfg_comp->type);
1400 if (!comp_cls) {
1401 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1402 "comp-cls-name=\"%s\", comp-cls-type=%d",
1403 cfg_comp->plugin_name->str,
1404 cfg_comp->comp_cls_name->str,
1405 cfg_comp->type);
1406 fprintf(stderr, "%s%sCannot find component class %s",
1407 bt_common_color_bold(),
1408 bt_common_color_fg_red(),
1409 bt_common_color_reset());
1410 print_plugin_comp_cls_opt(stderr,
1411 cfg_comp->plugin_name->str,
1412 cfg_comp->comp_cls_name->str,
1413 cfg_comp->type);
1414 fprintf(stderr, "\n");
1415 goto error;
1416 }
1417
1418 comp = bt_component_create(comp_cls,
1419 cfg_comp->instance_name->str, cfg_comp->params);
1420 if (!comp) {
1421 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
1422 "comp-cls-name=\"%s\", comp-cls-type=%d",
1423 "comp-name=\"%s\"",
1424 cfg_comp->plugin_name->str,
1425 cfg_comp->comp_cls_name->str,
1426 cfg_comp->type, cfg_comp->instance_name->str);
1427 fprintf(stderr, "%s%sCannot create component `%s`%s\n",
1428 bt_common_color_bold(),
1429 bt_common_color_fg_red(),
1430 cfg_comp->instance_name->str,
1431 bt_common_color_reset());
1432 goto error;
1433 }
1434
1435 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
1436 comp, cfg_comp->instance_name->str);
1437 quark = g_quark_from_string(cfg_comp->instance_name->str);
1438 assert(quark > 0);
1439 g_hash_table_insert(ctx->components,
1440 (gpointer) (long) quark, comp);
1441 comp = NULL;
1442 BT_PUT(comp_cls);
1443 }
1444
1445 goto end;
1446
1447 error:
1448 ret = -1;
1449
1450 end:
1451 bt_put(comp);
1452 bt_put(comp_cls);
1453 return ret;
1454 }
1455
1456 static
1457 int cmd_run_ctx_create_components(struct cmd_run_ctx *ctx)
1458 {
1459 int ret = 0;
1460
1461 /*
1462 * Make sure that, during this phase, our graph's "port added"
1463 * listener does not connect ports while we are creating the
1464 * components because we have a special, initial phase for
1465 * this.
1466 */
1467 ctx->connect_ports = false;
1468
1469 ret = cmd_run_ctx_create_components_from_config_components(
1470 ctx, ctx->cfg->cmd_data.run.sources);
1471 if (ret) {
1472 ret = -1;
1473 goto end;
1474 }
1475
1476 ret = cmd_run_ctx_create_components_from_config_components(
1477 ctx, ctx->cfg->cmd_data.run.filters);
1478 if (ret) {
1479 ret = -1;
1480 goto end;
1481 }
1482
1483 ret = cmd_run_ctx_create_components_from_config_components(
1484 ctx, ctx->cfg->cmd_data.run.sinks);
1485 if (ret) {
1486 ret = -1;
1487 goto end;
1488 }
1489
1490 end:
1491 return ret;
1492 }
1493
1494 static
1495 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx *ctx,
1496 struct bt_component *comp,
1497 int64_t (*port_count_fn)(struct bt_component *),
1498 struct bt_port *(*port_by_index_fn)(struct bt_component *, uint64_t))
1499 {
1500 int ret = 0;
1501 int64_t count;
1502 uint64_t i;
1503
1504 count = port_count_fn(comp);
1505 assert(count >= 0);
1506
1507 for (i = 0; i < count; i++) {
1508 struct bt_port *upstream_port = port_by_index_fn(comp, i);
1509
1510 assert(upstream_port);
1511 ret = cmd_run_ctx_connect_upstream_port(ctx, upstream_port);
1512 bt_put(upstream_port);
1513 if (ret) {
1514 goto end;
1515 }
1516 }
1517
1518 end:
1519 return ret;
1520 }
1521
1522 static
1523 int cmd_run_ctx_connect_ports(struct cmd_run_ctx *ctx)
1524 {
1525 int ret = 0;
1526 GHashTableIter iter;
1527 gpointer g_name_quark, g_comp;
1528
1529 ctx->connect_ports = true;
1530 g_hash_table_iter_init(&iter, ctx->components);
1531
1532 while (g_hash_table_iter_next(&iter, &g_name_quark, &g_comp)) {
1533 if (bt_component_is_source(g_comp)) {
1534 ret = cmd_run_ctx_connect_comp_ports(ctx,
1535 g_comp, bt_component_source_get_output_port_count,
1536 bt_component_source_get_output_port_by_index);
1537 } else if (bt_component_is_filter(g_comp)) {
1538 ret = cmd_run_ctx_connect_comp_ports(ctx,
1539 g_comp, bt_component_filter_get_output_port_count,
1540 bt_component_filter_get_output_port_by_index);
1541 }
1542
1543 if (ret) {
1544 goto end;
1545 }
1546 }
1547
1548 end:
1549 return ret;
1550 }
1551
1552 static
1553 int cmd_run(struct bt_config *cfg)
1554 {
1555 int ret = 0;
1556 struct cmd_run_ctx ctx = { 0 };
1557
1558 ret = load_all_plugins(cfg->plugin_paths);
1559 if (ret) {
1560 goto error;
1561 }
1562
1563 /* Initialize the command's context and the graph object */
1564 if (cmd_run_ctx_init(&ctx, cfg)) {
1565 BT_LOGE_STR("Cannot initialize the command's context.");
1566 fprintf(stderr, "Cannot initialize the command's context\n");
1567 goto error;
1568 }
1569
1570 /* Create the requested component instances */
1571 if (cmd_run_ctx_create_components(&ctx)) {
1572 BT_LOGE_STR("Cannot create components.");
1573 fprintf(stderr, "Cannot create components\n");
1574 goto error;
1575 }
1576
1577 /* Connect the initially visible component ports */
1578 if (cmd_run_ctx_connect_ports(&ctx)) {
1579 BT_LOGE_STR("Cannot connect initial component ports.");
1580 fprintf(stderr, "Cannot connect initial component ports\n");
1581 goto error;
1582 }
1583
1584 BT_LOGI_STR("Running the graph.");
1585
1586 /* Run the graph */
1587 while (true) {
1588 enum bt_graph_status graph_status = bt_graph_run(ctx.graph);
1589
1590 switch (graph_status) {
1591 case BT_GRAPH_STATUS_OK:
1592 break;
1593 case BT_GRAPH_STATUS_AGAIN:
1594 if (cfg->cmd_data.run.retry_duration_us > 0) {
1595 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
1596 "time-us=%" PRIu64,
1597 cfg->cmd_data.run.retry_duration_us);
1598
1599 if (usleep(cfg->cmd_data.run.retry_duration_us)) {
1600 // TODO: check EINTR and signal handler
1601 }
1602 }
1603 break;
1604 case BT_COMPONENT_STATUS_END:
1605 goto end;
1606 default:
1607 BT_LOGE_STR("Graph failed to complete successfully");
1608 fprintf(stderr, "Graph failed to complete successfully\n");
1609 goto error;
1610 }
1611 }
1612
1613 goto end;
1614
1615 error:
1616 if (ret == 0) {
1617 ret = -1;
1618 }
1619
1620 end:
1621 cmd_run_ctx_destroy(&ctx);
1622 return ret;
1623 }
1624
1625 static
1626 void warn_command_name_and_directory_clash(struct bt_config *cfg)
1627 {
1628 const char *env_clash;
1629
1630 if (!cfg->command_name) {
1631 return;
1632 }
1633
1634 env_clash = getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH);
1635 if (env_clash && strcmp(env_clash, "0") == 0) {
1636 return;
1637 }
1638
1639 if (g_file_test(cfg->command_name,
1640 G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
1641 fprintf(stderr, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
1642 cfg->command_name);
1643 fprintf(stderr, "trace located in the local `%s` directory, please use:\n",
1644 cfg->command_name);
1645 fprintf(stderr, "\n");
1646 fprintf(stderr, " babeltrace convert %s [OPTIONS]\n",
1647 cfg->command_name);
1648 }
1649 }
1650
1651 static
1652 void init_log_level(void)
1653 {
1654 enum bt_logging_level log_level = BT_LOG_NONE;
1655 const char *log_level_env = getenv("BABELTRACE_CLI_LOG_LEVEL");
1656
1657 if (!log_level_env) {
1658 goto set_level;
1659 }
1660
1661 if (strcmp(log_level_env, "VERBOSE") == 0) {
1662 log_level = BT_LOGGING_LEVEL_VERBOSE;
1663 } else if (strcmp(log_level_env, "DEBUG") == 0) {
1664 log_level = BT_LOGGING_LEVEL_DEBUG;
1665 } else if (strcmp(log_level_env, "INFO") == 0) {
1666 log_level = BT_LOGGING_LEVEL_INFO;
1667 } else if (strcmp(log_level_env, "WARN") == 0) {
1668 log_level = BT_LOGGING_LEVEL_WARN;
1669 } else if (strcmp(log_level_env, "ERROR") == 0) {
1670 log_level = BT_LOGGING_LEVEL_ERROR;
1671 } else if (strcmp(log_level_env, "FATAL") == 0) {
1672 log_level = BT_LOGGING_LEVEL_FATAL;
1673 } else if (strcmp(log_level_env, "NONE") == 0) {
1674 log_level = BT_LOGGING_LEVEL_NONE;
1675 }
1676
1677 set_level:
1678 bt_cli_log_level = log_level;
1679 }
1680
1681 int main(int argc, const char **argv)
1682 {
1683 int ret;
1684 int retcode;
1685 struct bt_config *cfg;
1686
1687 init_log_level();
1688 init_static_data();
1689 cfg = bt_config_cli_args_create_with_default(argc, argv, &retcode);
1690
1691 if (retcode < 0) {
1692 /* Quit without errors; typically usage/version */
1693 retcode = 0;
1694 BT_LOGI_STR("Quitting without errors.");
1695 goto end;
1696 }
1697
1698 if (retcode > 0) {
1699 BT_LOGE("Command-line error: retcode=%d", retcode);
1700 goto end;
1701 }
1702
1703 if (!cfg) {
1704 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
1705 fprintf(stderr, "Failed to create Babeltrace configuration\n");
1706 retcode = 1;
1707 goto end;
1708 }
1709
1710 if (cfg->verbose) {
1711 bt_cli_log_level = BT_LOGGING_LEVEL_VERBOSE;
1712 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE);
1713 // TODO: for backward compat., set the log level
1714 // environment variables of the known plugins
1715 // to VERBOSE
1716 } else if (cfg->debug) {
1717 bt_cli_log_level = BT_LOGGING_LEVEL_DEBUG;
1718 bt_logging_set_global_level(BT_LOGGING_LEVEL_DEBUG);
1719 // TODO: for backward compat., set the log level
1720 // environment variables of the known plugins
1721 // to DEBUG
1722 }
1723
1724 babeltrace_debug = cfg->debug;
1725 babeltrace_verbose = cfg->verbose;
1726 print_cfg(cfg);
1727
1728 if (cfg->command_needs_plugins) {
1729 ret = load_all_plugins(cfg->plugin_paths);
1730 if (ret) {
1731 BT_LOGE("Failed to load plugins: ret=%d", ret);
1732 retcode = 1;
1733 goto end;
1734 }
1735 }
1736
1737 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
1738 cfg->command, cfg->command_name);
1739
1740 switch (cfg->command) {
1741 case BT_CONFIG_COMMAND_RUN:
1742 ret = cmd_run(cfg);
1743 break;
1744 case BT_CONFIG_COMMAND_LIST_PLUGINS:
1745 ret = cmd_list_plugins(cfg);
1746 break;
1747 case BT_CONFIG_COMMAND_HELP:
1748 ret = cmd_help(cfg);
1749 break;
1750 case BT_CONFIG_COMMAND_QUERY:
1751 ret = cmd_query(cfg);
1752 break;
1753 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA:
1754 ret = cmd_print_ctf_metadata(cfg);
1755 break;
1756 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS:
1757 ret = cmd_print_lttng_live_sessions(cfg);
1758 break;
1759 default:
1760 BT_LOGF("Invalid command: cmd=%d", cfg->command);
1761 assert(false);
1762 }
1763
1764 warn_command_name_and_directory_clash(cfg);
1765 retcode = ret ? 1 : 0;
1766
1767 end:
1768 BT_PUT(cfg);
1769 fini_static_data();
1770 return retcode;
1771 }
This page took 0.101148 seconds and 4 git commands to generate.