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