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