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