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