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