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