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