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