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