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